blob: fdf51f89754a108f4e43fe87f79824639f69715b [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI event handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/unaligned.h>
28
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
Mikel Astizf0d6a0e2012-08-09 09:52:30 +020031#include <net/bluetooth/mgmt.h>
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070032
Johan Hedberg0857dd32014-12-19 13:40:20 +020033#include "hci_request.h"
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +010034#include "hci_debugfs.h"
Marcel Holtmann70247282013-10-10 14:54:15 -070035#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070036#include "amp.h"
Johan Hedberg2ceba532014-06-16 19:25:16 +030037#include "smp.h"
Miao-chen Chou145373c2020-04-03 21:44:01 +020038#include "msft.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Marcel Holtmannaa5b0342015-01-27 16:04:33 -080040#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
41 "\x00\x00\x00\x00\x00\x00\x00\x00"
42
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -070043#define secs_to_jiffies(_secs) msecs_to_jiffies((_secs) * 1000)
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* Handle HCI Event packets */
46
Sonny Sasakaadf1d692020-05-06 12:55:03 -070047static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb,
48 u8 *new_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020050 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030052 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Sonny Sasakaadf1d692020-05-06 12:55:03 -070054 /* 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.
60 */
61 if (status == 0x0c && !test_bit(HCI_INQUIRY, &hdev->flags)) {
62 bt_dev_warn(hdev, "Ignoring error of Inquiry Cancel command");
63 status = 0x00;
64 }
65
66 *new_status = status;
67
Andre Guedes82f47852013-04-30 15:29:34 -030068 if (status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Andre Guedes89352e72011-11-04 14:16:53 -030071 clear_bit(HCI_INQUIRY, &hdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010072 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -030073 wake_up_bit(&hdev->flags, HCI_INQUIRY);
Andre Guedes89352e72011-11-04 14:16:53 -030074
Johan Hedberg50143a42014-06-10 14:05:57 +030075 hci_dev_lock(hdev);
Jakub Pawlowski168b8a22015-10-16 10:07:49 +030076 /* Set discovery state to stopped if we're not doing LE active
77 * scanning.
78 */
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);
Johan Hedberg50143a42014-06-10 14:05:57 +030082 hci_dev_unlock(hdev);
83
Marcel Holtmanna9de9242007-10-20 13:33:56 +020084 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
Andre Guedes4d934832012-03-21 00:03:35 -030087static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
88{
89 __u8 status = *((__u8 *) skb->data);
90
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030091 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesae854a72012-03-21 00:03:36 -030092
93 if (status)
94 return;
95
Marcel Holtmanna1536da2015-03-13 02:11:01 -070096 hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
Andre Guedes4d934832012-03-21 00:03:35 -030097}
98
Marcel Holtmanna9de9242007-10-20 13:33:56 +020099static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200101 __u8 status = *((__u8 *) skb->data);
102
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300103 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200104
105 if (status)
106 return;
107
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700108 hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
Andre Guedesae854a72012-03-21 00:03:36 -0300109
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200110 hci_conn_check_pending(hdev);
111}
112
Gustavo Padovan807deac2012-05-17 00:36:24 -0300113static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
114 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200115{
116 BT_DBG("%s", hdev->name);
117}
118
119static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
120{
121 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300124 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200126 if (rp->status)
127 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200129 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200131 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Johan Hedberg40bef302014-07-16 11:42:27 +0300132 if (conn)
133 conn->role = rp->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200134
135 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200138static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
139{
140 struct hci_rp_read_link_policy *rp = (void *) skb->data;
141 struct hci_conn *conn;
142
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300143 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200144
145 if (rp->status)
146 return;
147
148 hci_dev_lock(hdev);
149
150 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
151 if (conn)
152 conn->link_policy = __le16_to_cpu(rp->policy);
153
154 hci_dev_unlock(hdev);
155}
156
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200157static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200159 struct hci_rp_write_link_policy *rp = (void *) skb->data;
160 struct hci_conn *conn;
161 void *sent;
162
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300163 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200164
165 if (rp->status)
166 return;
167
168 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
169 if (!sent)
170 return;
171
172 hci_dev_lock(hdev);
173
174 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200175 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700176 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200177
178 hci_dev_unlock(hdev);
179}
180
Gustavo Padovan807deac2012-05-17 00:36:24 -0300181static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
182 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200183{
184 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
185
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300186 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200187
188 if (rp->status)
189 return;
190
191 hdev->link_policy = __le16_to_cpu(rp->policy);
192}
193
Gustavo Padovan807deac2012-05-17 00:36:24 -0300194static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
195 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200196{
197 __u8 status = *((__u8 *) skb->data);
198 void *sent;
199
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300200 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200201
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200202 if (status)
203 return;
204
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200205 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
206 if (!sent)
207 return;
208
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200209 hdev->link_policy = get_unaligned_le16(sent);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200210}
211
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200212static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
213{
214 __u8 status = *((__u8 *) skb->data);
215
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300216 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200217
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300218 clear_bit(HCI_RESET, &hdev->flags);
219
Marcel Holtmann8761f9d2014-11-02 02:45:58 +0100220 if (status)
221 return;
222
Johan Hedberga297e972012-02-21 17:55:47 +0200223 /* Reset all non-persistent flags */
Marcel Holtmanneacb44d2015-03-13 09:04:17 -0700224 hci_dev_clear_volatile_flags(hdev);
Andre Guedes69775ff2012-02-23 16:50:05 +0200225
Johan Hedberg39c5d972015-01-28 19:56:01 +0200226 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
227
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100228 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
229 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
Johan Hedberg3f0f5242012-11-08 01:23:00 +0100230
231 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
232 hdev->adv_data_len = 0;
Marcel Holtmannf8e808b2013-10-16 00:16:47 -0700233
234 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
235 hdev->scan_rsp_data_len = 0;
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700236
Marcel Holtmann533553f2014-03-21 12:18:10 -0700237 hdev->le_scan_type = LE_SCAN_PASSIVE;
238
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700239 hdev->ssp_debug_mode = 0;
Marcel Holtmanna4d55042014-10-29 23:37:53 +0100240
Archie Pusaka3d4f9c02021-06-04 16:26:27 +0800241 hci_bdaddr_list_clear(&hdev->le_accept_list);
Ankit Navikcfdb0c22018-06-29 12:12:50 +0530242 hci_bdaddr_list_clear(&hdev->le_resolv_list);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200243}
244
Marcel Holtmannc2f0f972015-01-12 09:21:25 -0800245static void hci_cc_read_stored_link_key(struct hci_dev *hdev,
246 struct sk_buff *skb)
247{
248 struct hci_rp_read_stored_link_key *rp = (void *)skb->data;
249 struct hci_cp_read_stored_link_key *sent;
250
251 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
252
253 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_STORED_LINK_KEY);
254 if (!sent)
255 return;
256
257 if (!rp->status && sent->read_all == 0x01) {
258 hdev->stored_max_keys = rp->max_keys;
259 hdev->stored_num_keys = rp->num_keys;
260 }
261}
262
Marcel Holtmanna93661202015-01-12 09:21:28 -0800263static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
264 struct sk_buff *skb)
265{
266 struct hci_rp_delete_stored_link_key *rp = (void *)skb->data;
267
268 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
269
270 if (rp->status)
271 return;
272
273 if (rp->num_keys <= hdev->stored_num_keys)
274 hdev->stored_num_keys -= rp->num_keys;
275 else
276 hdev->stored_num_keys = 0;
277}
278
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200279static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
280{
281 __u8 status = *((__u8 *) skb->data);
282 void *sent;
283
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300284 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200285
286 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
287 if (!sent)
288 return;
289
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200290 hci_dev_lock(hdev);
291
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700292 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200293 mgmt_set_local_name_complete(hdev, sent, status);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +0200294 else if (!status)
295 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200296
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200297 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200298}
299
300static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
301{
302 struct hci_rp_read_local_name *rp = (void *) skb->data;
303
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300304 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200305
306 if (rp->status)
307 return;
308
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700309 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
310 hci_dev_test_flag(hdev, HCI_CONFIG))
Johan Hedbergdb99b5f2012-02-22 20:14:22 +0200311 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200312}
313
314static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
315{
316 __u8 status = *((__u8 *) skb->data);
317 void *sent;
318
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300319 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200320
321 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
322 if (!sent)
323 return;
324
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530325 hci_dev_lock(hdev);
326
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200327 if (!status) {
328 __u8 param = *((__u8 *) sent);
329
330 if (param == AUTH_ENABLED)
331 set_bit(HCI_AUTH, &hdev->flags);
332 else
333 clear_bit(HCI_AUTH, &hdev->flags);
334 }
335
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700336 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg33ef95e2012-02-16 23:56:27 +0200337 mgmt_auth_enable_complete(hdev, status);
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530338
339 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200340}
341
342static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
343{
344 __u8 status = *((__u8 *) skb->data);
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200345 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200346 void *sent;
347
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300348 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200349
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200350 if (status)
351 return;
352
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200353 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
354 if (!sent)
355 return;
356
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200357 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200358
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200359 if (param)
360 set_bit(HCI_ENCRYPT, &hdev->flags);
361 else
362 clear_bit(HCI_ENCRYPT, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200363}
364
365static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
366{
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200367 __u8 status = *((__u8 *) skb->data);
368 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200369 void *sent;
370
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300371 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200372
373 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
374 if (!sent)
375 return;
376
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200377 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200378
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200379 hci_dev_lock(hdev);
380
Mikel Astizfa1bd912012-08-09 09:52:29 +0200381 if (status) {
Johan Hedberg2d7cee52011-11-07 22:16:03 +0200382 hdev->discov_timeout = 0;
383 goto done;
384 }
385
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300386 if (param & SCAN_INQUIRY)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200387 set_bit(HCI_ISCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300388 else
389 clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200390
Johan Hedberg031547d2014-07-10 12:09:06 +0300391 if (param & SCAN_PAGE)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200392 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300393 else
Johan Hedberg204e3992014-07-28 15:45:31 +0300394 clear_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200395
396done:
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200397 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200398}
399
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -0800400static void hci_cc_set_event_filter(struct hci_dev *hdev, struct sk_buff *skb)
401{
402 __u8 status = *((__u8 *)skb->data);
403 struct hci_cp_set_event_filter *cp;
404 void *sent;
405
406 BT_DBG("%s status 0x%2.2x", hdev->name, status);
407
408 if (status)
409 return;
410
411 sent = hci_sent_cmd_data(hdev, HCI_OP_SET_EVENT_FLT);
412 if (!sent)
413 return;
414
415 cp = (struct hci_cp_set_event_filter *)sent;
416
417 if (cp->flt_type == HCI_FLT_CLEAR_ALL)
418 hci_dev_clear_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
419 else
420 hci_dev_set_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
421}
422
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200423static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
424{
425 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
426
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300427 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200428
429 if (rp->status)
430 return;
431
432 memcpy(hdev->dev_class, rp->dev_class, 3);
433
434 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300435 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200436}
437
438static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
439{
440 __u8 status = *((__u8 *) skb->data);
441 void *sent;
442
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300443 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200444
445 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
446 if (!sent)
447 return;
448
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100449 hci_dev_lock(hdev);
450
451 if (status == 0)
452 memcpy(hdev->dev_class, sent, 3);
453
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700454 if (hci_dev_test_flag(hdev, HCI_MGMT))
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100455 mgmt_set_class_of_dev_complete(hdev, sent, status);
456
457 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200458}
459
460static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
461{
462 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200464
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300465 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200466
467 if (rp->status)
468 return;
469
470 setting = __le16_to_cpu(rp->voice_setting);
471
Marcel Holtmannf383f272008-07-14 20:13:47 +0200472 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200473 return;
474
475 hdev->voice_setting = setting;
476
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300477 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200478
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200479 if (hdev->notify)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200480 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200481}
482
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300483static void hci_cc_write_voice_setting(struct hci_dev *hdev,
484 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200485{
486 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200487 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 void *sent;
489
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300490 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Marcel Holtmannf383f272008-07-14 20:13:47 +0200492 if (status)
493 return;
494
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200495 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
496 if (!sent)
497 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Marcel Holtmannf383f272008-07-14 20:13:47 +0200499 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Marcel Holtmannf383f272008-07-14 20:13:47 +0200501 if (hdev->voice_setting == setting)
502 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Marcel Holtmannf383f272008-07-14 20:13:47 +0200504 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300506 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200507
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200508 if (hdev->notify)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200509 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700512static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
513 struct sk_buff *skb)
514{
515 struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
516
517 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
518
519 if (rp->status)
520 return;
521
522 hdev->num_iac = rp->num_iac;
523
524 BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
525}
526
Marcel Holtmann333140b2008-07-14 20:13:48 +0200527static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
528{
529 __u8 status = *((__u8 *) skb->data);
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300530 struct hci_cp_write_ssp_mode *sent;
Marcel Holtmann333140b2008-07-14 20:13:48 +0200531
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300532 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200533
Marcel Holtmann333140b2008-07-14 20:13:48 +0200534 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
535 if (!sent)
536 return;
537
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530538 hci_dev_lock(hdev);
539
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300540 if (!status) {
541 if (sent->mode)
Johan Hedbergcad718e2013-04-17 15:00:51 +0300542 hdev->features[1][0] |= LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300543 else
Johan Hedbergcad718e2013-04-17 15:00:51 +0300544 hdev->features[1][0] &= ~LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300545 }
546
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700547 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300548 mgmt_ssp_enable_complete(hdev, sent->mode, status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200549 else if (!status) {
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300550 if (sent->mode)
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700551 hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200552 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700553 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200554 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530555
556 hci_dev_unlock(hdev);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200557}
558
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800559static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
560{
561 u8 status = *((u8 *) skb->data);
562 struct hci_cp_write_sc_support *sent;
563
564 BT_DBG("%s status 0x%2.2x", hdev->name, status);
565
566 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
567 if (!sent)
568 return;
569
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530570 hci_dev_lock(hdev);
571
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800572 if (!status) {
573 if (sent->support)
574 hdev->features[1][0] |= LMP_HOST_SC;
575 else
576 hdev->features[1][0] &= ~LMP_HOST_SC;
577 }
578
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700579 if (!hci_dev_test_flag(hdev, HCI_MGMT) && !status) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800580 if (sent->support)
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700581 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800582 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700583 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800584 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530585
586 hci_dev_unlock(hdev);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800587}
588
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200589static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
590{
591 struct hci_rp_read_local_version *rp = (void *) skb->data;
592
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300593 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200594
595 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200596 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200597
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700598 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
599 hci_dev_test_flag(hdev, HCI_CONFIG)) {
Marcel Holtmann0d5551f2013-10-18 12:04:50 -0700600 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);
605 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200606}
607
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300608static void hci_cc_read_local_commands(struct hci_dev *hdev,
609 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200610{
611 struct hci_rp_read_local_commands *rp = (void *) skb->data;
612
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300613 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200614
Marcel Holtmann6a070e62013-10-31 04:54:33 -0700615 if (rp->status)
616 return;
617
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700618 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
619 hci_dev_test_flag(hdev, HCI_CONFIG))
Johan Hedberg2177bab2013-03-05 20:37:43 +0200620 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200621}
622
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +0530623static void hci_cc_read_auth_payload_timeout(struct hci_dev *hdev,
624 struct sk_buff *skb)
625{
626 struct hci_rp_read_auth_payload_to *rp = (void *)skb->data;
627 struct hci_conn *conn;
628
629 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
630
631 if (rp->status)
632 return;
633
634 hci_dev_lock(hdev);
635
636 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
637 if (conn)
638 conn->auth_payload_timeout = __le16_to_cpu(rp->timeout);
639
640 hci_dev_unlock(hdev);
641}
642
643static void hci_cc_write_auth_payload_timeout(struct hci_dev *hdev,
644 struct sk_buff *skb)
645{
646 struct hci_rp_write_auth_payload_to *rp = (void *)skb->data;
647 struct hci_conn *conn;
648 void *sent;
649
650 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
651
652 if (rp->status)
653 return;
654
655 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO);
656 if (!sent)
657 return;
658
659 hci_dev_lock(hdev);
660
661 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
662 if (conn)
663 conn->auth_payload_timeout = get_unaligned_le16(sent + 2);
664
665 hci_dev_unlock(hdev);
666}
667
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300668static void hci_cc_read_local_features(struct hci_dev *hdev,
669 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200670{
671 struct hci_rp_read_local_features *rp = (void *) skb->data;
672
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300673 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200674
675 if (rp->status)
676 return;
677
678 memcpy(hdev->features, rp->features, 8);
679
680 /* Adjust default settings according to features
681 * supported by device. */
682
Johan Hedbergcad718e2013-04-17 15:00:51 +0300683 if (hdev->features[0][0] & LMP_3SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200684 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
685
Johan Hedbergcad718e2013-04-17 15:00:51 +0300686 if (hdev->features[0][0] & LMP_5SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200687 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
688
Johan Hedbergcad718e2013-04-17 15:00:51 +0300689 if (hdev->features[0][1] & LMP_HV2) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200690 hdev->pkt_type |= (HCI_HV2);
691 hdev->esco_type |= (ESCO_HV2);
692 }
693
Johan Hedbergcad718e2013-04-17 15:00:51 +0300694 if (hdev->features[0][1] & LMP_HV3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200695 hdev->pkt_type |= (HCI_HV3);
696 hdev->esco_type |= (ESCO_HV3);
697 }
698
Andre Guedes45db810f2012-07-24 15:03:49 -0300699 if (lmp_esco_capable(hdev))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200700 hdev->esco_type |= (ESCO_EV3);
701
Johan Hedbergcad718e2013-04-17 15:00:51 +0300702 if (hdev->features[0][4] & LMP_EV4)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200703 hdev->esco_type |= (ESCO_EV4);
704
Johan Hedbergcad718e2013-04-17 15:00:51 +0300705 if (hdev->features[0][4] & LMP_EV5)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200706 hdev->esco_type |= (ESCO_EV5);
707
Johan Hedbergcad718e2013-04-17 15:00:51 +0300708 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100709 hdev->esco_type |= (ESCO_2EV3);
710
Johan Hedbergcad718e2013-04-17 15:00:51 +0300711 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100712 hdev->esco_type |= (ESCO_3EV3);
713
Johan Hedbergcad718e2013-04-17 15:00:51 +0300714 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100715 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200716}
717
Andre Guedes971e3a42011-06-30 19:20:52 -0300718static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300719 struct sk_buff *skb)
Andre Guedes971e3a42011-06-30 19:20:52 -0300720{
721 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
722
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300723 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andre Guedes971e3a42011-06-30 19:20:52 -0300724
725 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200726 return;
Andre Guedes971e3a42011-06-30 19:20:52 -0300727
Marcel Holtmann57af75a2013-10-18 12:04:47 -0700728 if (hdev->max_page < rp->max_page)
729 hdev->max_page = rp->max_page;
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300730
Johan Hedbergcad718e2013-04-17 15:00:51 +0300731 if (rp->page < HCI_MAX_PAGES)
732 memcpy(hdev->features[rp->page], rp->features, 8);
Andre Guedes971e3a42011-06-30 19:20:52 -0300733}
734
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200735static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300736 struct sk_buff *skb)
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200737{
738 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
739
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300740 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200741
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200742 if (rp->status)
743 return;
744
745 hdev->flow_ctl_mode = rp->mode;
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200746}
747
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200748static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
749{
750 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
751
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300752 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200753
754 if (rp->status)
755 return;
756
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);
761
762 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
763 hdev->sco_mtu = 64;
764 hdev->sco_pkts = 8;
765 }
766
767 hdev->acl_cnt = hdev->acl_pkts;
768 hdev->sco_cnt = hdev->sco_pkts;
769
Gustavo Padovan807deac2012-05-17 00:36:24 -0300770 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);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200772}
773
774static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
775{
776 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
777
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300778 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200779
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200780 if (rp->status)
781 return;
782
783 if (test_bit(HCI_INIT, &hdev->flags))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200784 bacpy(&hdev->bdaddr, &rp->bdaddr);
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200785
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700786 if (hci_dev_test_flag(hdev, HCI_SETUP))
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200787 bacpy(&hdev->setup_addr, &rp->bdaddr);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200788}
789
Marcel Holtmanna4790362020-04-03 21:44:04 +0200790static void hci_cc_read_local_pairing_opts(struct hci_dev *hdev,
791 struct sk_buff *skb)
792{
793 struct hci_rp_read_local_pairing_opts *rp = (void *) skb->data;
794
795 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
796
797 if (rp->status)
798 return;
799
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;
804 }
805}
806
Johan Hedbergf332ec62013-03-15 17:07:11 -0500807static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
808 struct sk_buff *skb)
809{
810 struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
811
812 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
813
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200814 if (rp->status)
815 return;
816
817 if (test_bit(HCI_INIT, &hdev->flags)) {
Johan Hedbergf332ec62013-03-15 17:07:11 -0500818 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
819 hdev->page_scan_window = __le16_to_cpu(rp->window);
820 }
821}
822
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500823static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
824 struct sk_buff *skb)
825{
826 u8 status = *((u8 *) skb->data);
827 struct hci_cp_write_page_scan_activity *sent;
828
829 BT_DBG("%s status 0x%2.2x", hdev->name, status);
830
831 if (status)
832 return;
833
834 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
835 if (!sent)
836 return;
837
838 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
839 hdev->page_scan_window = __le16_to_cpu(sent->window);
840}
841
Johan Hedbergf332ec62013-03-15 17:07:11 -0500842static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
843 struct sk_buff *skb)
844{
845 struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
846
847 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
848
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200849 if (rp->status)
850 return;
851
852 if (test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergf332ec62013-03-15 17:07:11 -0500853 hdev->page_scan_type = rp->type;
854}
855
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500856static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
857 struct sk_buff *skb)
858{
859 u8 status = *((u8 *) skb->data);
860 u8 *type;
861
862 BT_DBG("%s status 0x%2.2x", hdev->name, status);
863
864 if (status)
865 return;
866
867 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
868 if (type)
869 hdev->page_scan_type = *type;
870}
871
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200872static void hci_cc_read_data_block_size(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300873 struct sk_buff *skb)
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200874{
875 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
876
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300877 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200878
879 if (rp->status)
880 return;
881
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);
885
886 hdev->block_cnt = hdev->num_blocks;
887
888 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300889 hdev->block_cnt, hdev->block_len);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200890}
891
Johan Hedberg33f35722014-06-28 17:54:06 +0300892static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
893{
894 struct hci_rp_read_clock *rp = (void *) skb->data;
895 struct hci_cp_read_clock *cp;
896 struct hci_conn *conn;
897
898 BT_DBG("%s", hdev->name);
899
900 if (skb->len < sizeof(*rp))
901 return;
902
903 if (rp->status)
904 return;
905
906 hci_dev_lock(hdev);
907
908 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
909 if (!cp)
910 goto unlock;
911
912 if (cp->which == 0x00) {
913 hdev->clock = le32_to_cpu(rp->clock);
914 goto unlock;
915 }
916
917 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
918 if (conn) {
919 conn->clock = le32_to_cpu(rp->clock);
920 conn->clock_accuracy = le16_to_cpu(rp->accuracy);
921 }
922
923unlock:
924 hci_dev_unlock(hdev);
925}
926
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300927static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300928 struct sk_buff *skb)
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300929{
930 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
931
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300932 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300933
934 if (rp->status)
Arron Wang83927882015-07-24 17:10:16 +0800935 return;
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300936
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);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300947}
948
Johan Hedbergd5859e22011-01-25 01:19:58 +0200949static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300950 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200951{
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700952 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200953
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300954 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200955
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200956 if (rp->status)
957 return;
958
959 hdev->inq_tx_power = rp->tx_power;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200960}
961
Alain Michaud00bce3f2020-03-05 16:14:59 +0000962static void hci_cc_read_def_err_data_reporting(struct hci_dev *hdev,
963 struct sk_buff *skb)
964{
965 struct hci_rp_read_def_err_data_reporting *rp = (void *)skb->data;
966
967 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
968
969 if (rp->status)
970 return;
971
972 hdev->err_data_reporting = rp->err_data_reporting;
973}
974
975static void hci_cc_write_def_err_data_reporting(struct hci_dev *hdev,
976 struct sk_buff *skb)
977{
978 __u8 status = *((__u8 *)skb->data);
979 struct hci_cp_write_def_err_data_reporting *cp;
980
981 BT_DBG("%s status 0x%2.2x", hdev->name, status);
982
983 if (status)
984 return;
985
986 cp = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING);
987 if (!cp)
988 return;
989
990 hdev->err_data_reporting = cp->err_data_reporting;
991}
992
Johan Hedberg980e1a52011-01-22 06:10:07 +0200993static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
994{
995 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
996 struct hci_cp_pin_code_reply *cp;
997 struct hci_conn *conn;
998
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300999 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001000
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001001 hci_dev_lock(hdev);
1002
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001003 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001004 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001005
Mikel Astizfa1bd912012-08-09 09:52:29 +02001006 if (rp->status)
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001007 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001008
1009 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
1010 if (!cp)
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001011 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001012
1013 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1014 if (conn)
1015 conn->pin_length = cp->pin_len;
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001016
1017unlock:
1018 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001019}
1020
1021static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
1022{
1023 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
1024
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001025 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001026
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001027 hci_dev_lock(hdev);
1028
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001029 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001030 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001031 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001032
1033 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001034}
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001035
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001036static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
1037 struct sk_buff *skb)
1038{
1039 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
1040
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001041 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001042
1043 if (rp->status)
1044 return;
1045
1046 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
1047 hdev->le_pkts = rp->le_max_pkt;
1048
1049 hdev->le_cnt = hdev->le_pkts;
1050
1051 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001052}
Johan Hedberg980e1a52011-01-22 06:10:07 +02001053
Johan Hedberg60e77322013-01-22 14:01:59 +02001054static void hci_cc_le_read_local_features(struct hci_dev *hdev,
1055 struct sk_buff *skb)
1056{
1057 struct hci_rp_le_read_local_features *rp = (void *) skb->data;
1058
1059 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1060
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001061 if (rp->status)
1062 return;
1063
1064 memcpy(hdev->le_features, rp->features, 8);
Johan Hedberg60e77322013-01-22 14:01:59 +02001065}
1066
Johan Hedberg8fa19092012-10-19 20:57:49 +03001067static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
1068 struct sk_buff *skb)
1069{
1070 struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
1071
1072 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1073
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001074 if (rp->status)
1075 return;
1076
1077 hdev->adv_tx_power = rp->tx_power;
Johan Hedberg8fa19092012-10-19 20:57:49 +03001078}
1079
Johan Hedberga5c29682011-02-19 12:05:57 -03001080static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
1081{
1082 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1083
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001084 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -03001085
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001086 hci_dev_lock(hdev);
1087
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001088 if (hci_dev_test_flag(hdev, HCI_MGMT))
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001089 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
1090 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001091
1092 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03001093}
1094
1095static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001096 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03001097{
1098 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1099
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001100 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -03001101
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001102 hci_dev_lock(hdev);
1103
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001104 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001105 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001106 ACL_LINK, 0, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001107
1108 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03001109}
1110
Brian Gix1143d452011-11-23 08:28:34 -08001111static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
1112{
1113 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1114
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001115 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001116
1117 hci_dev_lock(hdev);
1118
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001119 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg272d90d2012-02-09 15:26:12 +02001120 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001121 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001122
1123 hci_dev_unlock(hdev);
1124}
1125
1126static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001127 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08001128{
1129 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1130
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001131 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001132
1133 hci_dev_lock(hdev);
1134
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001135 if (hci_dev_test_flag(hdev, HCI_MGMT))
Brian Gix1143d452011-11-23 08:28:34 -08001136 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001137 ACL_LINK, 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001138
1139 hci_dev_unlock(hdev);
1140}
1141
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001142static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
1143 struct sk_buff *skb)
Szymon Jancc35938b2011-03-22 13:12:21 +01001144{
1145 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
1146
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001147 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001148}
1149
1150static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
1151 struct sk_buff *skb)
1152{
1153 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
1154
1155 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Szymon Jancc35938b2011-03-22 13:12:21 +01001156}
1157
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001158static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
1159{
1160 __u8 status = *((__u8 *) skb->data);
1161 bdaddr_t *sent;
1162
1163 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1164
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001165 if (status)
1166 return;
1167
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001168 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1169 if (!sent)
1170 return;
1171
1172 hci_dev_lock(hdev);
1173
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001174 bacpy(&hdev->random_addr, sent);
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001175
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001176 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));
1180 }
1181
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001182 hci_dev_unlock(hdev);
1183}
1184
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05301185static void hci_cc_le_set_default_phy(struct hci_dev *hdev, struct sk_buff *skb)
1186{
1187 __u8 status = *((__u8 *) skb->data);
1188 struct hci_cp_le_set_default_phy *cp;
1189
1190 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1191
1192 if (status)
1193 return;
1194
1195 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_DEFAULT_PHY);
1196 if (!cp)
1197 return;
1198
1199 hci_dev_lock(hdev);
1200
1201 hdev->le_tx_def_phys = cp->tx_phys;
1202 hdev->le_rx_def_phys = cp->rx_phys;
1203
1204 hci_dev_unlock(hdev);
1205}
1206
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301207static void hci_cc_le_set_adv_set_random_addr(struct hci_dev *hdev,
1208 struct sk_buff *skb)
1209{
1210 __u8 status = *((__u8 *) skb->data);
1211 struct hci_cp_le_set_adv_set_rand_addr *cp;
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001212 struct adv_info *adv;
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301213
1214 if (status)
1215 return;
1216
1217 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR);
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001218 /* 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.
1221 */
1222 if (!cp || !cp->handle)
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301223 return;
1224
1225 hci_dev_lock(hdev);
1226
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001227 adv = hci_find_adv_instance(hdev, cp->handle);
1228 if (adv) {
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));
1235 }
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301236 }
1237
1238 hci_dev_unlock(hdev);
1239}
1240
Daniel Winkler7c395ea2020-12-03 12:12:51 -08001241static void hci_cc_le_read_transmit_power(struct hci_dev *hdev,
1242 struct sk_buff *skb)
1243{
1244 struct hci_rp_le_read_transmit_power *rp = (void *)skb->data;
1245
1246 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1247
1248 if (rp->status)
1249 return;
1250
1251 hdev->min_le_tx_power = rp->min_le_tx_power;
1252 hdev->max_le_tx_power = rp->max_le_tx_power;
1253}
1254
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001255static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1256{
1257 __u8 *sent, status = *((__u8 *) skb->data);
1258
1259 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1260
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001261 if (status)
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001262 return;
1263
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001264 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1265 if (!sent)
Johan Hedberg3c857752014-03-25 10:30:49 +02001266 return;
1267
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001268 hci_dev_lock(hdev);
1269
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001270 /* If we're doing connection initiation as peripheral. Set a
Johan Hedberg3c857752014-03-25 10:30:49 +02001271 * timeout in case something goes wrong.
1272 */
1273 if (*sent) {
1274 struct hci_conn *conn;
1275
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001276 hci_dev_set_flag(hdev, HCI_LE_ADV);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001277
Jakub Pawlowskie7d9ab72015-08-07 20:22:52 +02001278 conn = hci_lookup_le_connect(hdev);
Johan Hedberg3c857752014-03-25 10:30:49 +02001279 if (conn)
1280 queue_delayed_work(hdev->workqueue,
1281 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001282 conn->conn_timeout);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001283 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001284 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedberg3c857752014-03-25 10:30:49 +02001285 }
1286
Johan Hedberg04b4edc2013-03-15 17:07:01 -05001287 hci_dev_unlock(hdev);
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001288}
1289
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301290static void hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev,
1291 struct sk_buff *skb)
1292{
1293 struct hci_cp_le_set_ext_adv_enable *cp;
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001294 struct hci_cp_ext_adv_set *set;
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301295 __u8 status = *((__u8 *) skb->data);
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001296 struct adv_info *adv = NULL, *n;
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301297
1298 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1299
1300 if (status)
1301 return;
1302
1303 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE);
1304 if (!cp)
1305 return;
1306
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001307 set = (void *)cp->data;
1308
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301309 hci_dev_lock(hdev);
1310
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001311 if (cp->num_of_sets)
1312 adv = hci_find_adv_instance(hdev, set->handle);
1313
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301314 if (cp->enable) {
1315 struct hci_conn *conn;
1316
1317 hci_dev_set_flag(hdev, HCI_LE_ADV);
1318
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001319 if (adv)
1320 adv->enabled = true;
1321
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301322 conn = hci_lookup_le_connect(hdev);
1323 if (conn)
1324 queue_delayed_work(hdev->workqueue,
1325 &conn->le_conn_timeout,
1326 conn->conn_timeout);
Jaganath Kanakkassery45b77492018-07-19 17:09:43 +05301327 } else {
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001328 if (adv) {
1329 adv->enabled = false;
1330 /* If just one instance was disabled check if there are
1331 * any other instance enabled before clearing HCI_LE_ADV
1332 */
1333 list_for_each_entry_safe(adv, n, &hdev->adv_instances,
1334 list) {
1335 if (adv->enabled)
1336 goto unlock;
1337 }
1338 } else {
1339 /* All instances shall be considered disabled */
1340 list_for_each_entry_safe(adv, n, &hdev->adv_instances,
1341 list)
1342 adv->enabled = false;
1343 }
1344
Jaganath Kanakkassery45b77492018-07-19 17:09:43 +05301345 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301346 }
1347
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001348unlock:
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301349 hci_dev_unlock(hdev);
1350}
1351
Marcel Holtmann533553f2014-03-21 12:18:10 -07001352static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1353{
1354 struct hci_cp_le_set_scan_param *cp;
1355 __u8 status = *((__u8 *) skb->data);
1356
1357 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1358
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001359 if (status)
1360 return;
1361
Marcel Holtmann533553f2014-03-21 12:18:10 -07001362 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1363 if (!cp)
1364 return;
1365
1366 hci_dev_lock(hdev);
1367
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001368 hdev->le_scan_type = cp->type;
Marcel Holtmann533553f2014-03-21 12:18:10 -07001369
1370 hci_dev_unlock(hdev);
1371}
1372
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301373static void hci_cc_le_set_ext_scan_param(struct hci_dev *hdev,
1374 struct sk_buff *skb)
1375{
1376 struct hci_cp_le_set_ext_scan_params *cp;
1377 __u8 status = *((__u8 *) skb->data);
1378 struct hci_cp_le_scan_phy_params *phy_param;
1379
1380 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1381
1382 if (status)
1383 return;
1384
1385 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS);
1386 if (!cp)
1387 return;
1388
1389 phy_param = (void *)cp->data;
1390
1391 hci_dev_lock(hdev);
1392
1393 hdev->le_scan_type = phy_param->type;
1394
1395 hci_dev_unlock(hdev);
1396}
1397
Johan Hedbergb9a63282014-03-25 10:51:52 +02001398static bool has_pending_adv_report(struct hci_dev *hdev)
1399{
1400 struct discovery_state *d = &hdev->discovery;
1401
1402 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1403}
1404
1405static void clear_pending_adv_report(struct hci_dev *hdev)
1406{
1407 struct discovery_state *d = &hdev->discovery;
1408
1409 bacpy(&d->last_adv_addr, BDADDR_ANY);
1410 d->last_adv_data_len = 0;
1411}
1412
1413static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001414 u8 bdaddr_type, s8 rssi, u32 flags,
1415 u8 *data, u8 len)
Johan Hedbergb9a63282014-03-25 10:51:52 +02001416{
1417 struct discovery_state *d = &hdev->discovery;
1418
Alain Michauda2ec9052020-07-27 20:48:55 +00001419 if (len > HCI_MAX_AD_LENGTH)
1420 return;
1421
Johan Hedbergb9a63282014-03-25 10:51:52 +02001422 bacpy(&d->last_adv_addr, bdaddr);
1423 d->last_adv_addr_type = bdaddr_type;
Johan Hedbergff5cd292014-03-25 14:40:52 +02001424 d->last_adv_rssi = rssi;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001425 d->last_adv_flags = flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02001426 memcpy(d->last_adv_data, data, len);
1427 d->last_adv_data_len = len;
1428}
1429
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301430static void le_set_scan_enable_complete(struct hci_dev *hdev, u8 enable)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001431{
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301432 hci_dev_lock(hdev);
1433
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301434 switch (enable) {
Andre Guedes76a388be2013-04-04 20:21:02 -03001435 case LE_SCAN_ENABLE:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001436 hci_dev_set_flag(hdev, HCI_LE_SCAN);
Johan Hedbergb9a63282014-03-25 10:51:52 +02001437 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1438 clear_pending_adv_report(hdev);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001439 break;
1440
Andre Guedes76a388be2013-04-04 20:21:02 -03001441 case LE_SCAN_DISABLE:
Johan Hedbergb9a63282014-03-25 10:51:52 +02001442 /* We do this here instead of when setting DISCOVERY_STOPPED
1443 * since the latter would potentially require waiting for
1444 * inquiry to stop too.
1445 */
1446 if (has_pending_adv_report(hdev)) {
1447 struct discovery_state *d = &hdev->discovery;
1448
1449 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001450 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001451 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001452 d->last_adv_data,
Johan Hedbergb9a63282014-03-25 10:51:52 +02001453 d->last_adv_data_len, NULL, 0);
1454 }
1455
Johan Hedberg317ac8c2014-02-28 20:26:12 +02001456 /* Cancel this timer so that we don't try to disable scanning
1457 * when it's already disabled.
1458 */
1459 cancel_delayed_work(&hdev->le_scan_disable);
1460
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001461 hci_dev_clear_flag(hdev, HCI_LE_SCAN);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001462
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001463 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1464 * interrupted scanning due to a connect request. Mark
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001465 * therefore discovery as stopped. If this was not
1466 * because of a connect request advertising might have
1467 * been disabled because of active scanning, so
1468 * re-enable it again if necessary.
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001469 */
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001470 if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001471 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001472 else if (!hci_dev_test_flag(hdev, HCI_LE_ADV) &&
Johan Hedberg34722272014-07-08 16:05:05 +03001473 hdev->discovery.state == DISCOVERY_FINDING)
Johan Hedbergf2252572015-11-18 12:49:20 +02001474 hci_req_reenable_advertising(hdev);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001475
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001476 break;
1477
1478 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001479 bt_dev_err(hdev, "use of reserved LE_Scan_Enable param %d",
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301480 enable);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001481 break;
Andre Guedes35815082011-05-26 16:23:53 -03001482 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301483
1484 hci_dev_unlock(hdev);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001485}
1486
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301487static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
1488 struct sk_buff *skb)
1489{
1490 struct hci_cp_le_set_scan_enable *cp;
1491 __u8 status = *((__u8 *) skb->data);
1492
1493 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1494
1495 if (status)
1496 return;
1497
1498 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1499 if (!cp)
1500 return;
1501
1502 le_set_scan_enable_complete(hdev, cp->enable);
1503}
1504
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301505static void hci_cc_le_set_ext_scan_enable(struct hci_dev *hdev,
1506 struct sk_buff *skb)
1507{
1508 struct hci_cp_le_set_ext_scan_enable *cp;
1509 __u8 status = *((__u8 *) skb->data);
1510
1511 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1512
1513 if (status)
1514 return;
1515
1516 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE);
1517 if (!cp)
1518 return;
1519
1520 le_set_scan_enable_complete(hdev, cp->enable);
1521}
1522
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05301523static void hci_cc_le_read_num_adv_sets(struct hci_dev *hdev,
1524 struct sk_buff *skb)
1525{
1526 struct hci_rp_le_read_num_supported_adv_sets *rp = (void *) skb->data;
1527
1528 BT_DBG("%s status 0x%2.2x No of Adv sets %u", hdev->name, rp->status,
1529 rp->num_of_sets);
1530
1531 if (rp->status)
1532 return;
1533
1534 hdev->le_num_of_adv_sets = rp->num_of_sets;
1535}
1536
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001537static void hci_cc_le_read_accept_list_size(struct hci_dev *hdev,
1538 struct sk_buff *skb)
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001539{
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001540 struct hci_rp_le_read_accept_list_size *rp = (void *)skb->data;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001541
1542 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1543
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001544 if (rp->status)
1545 return;
1546
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001547 hdev->le_accept_list_size = rp->size;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001548}
1549
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001550static void hci_cc_le_clear_accept_list(struct hci_dev *hdev,
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001551 struct sk_buff *skb)
1552{
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001553 __u8 status = *((__u8 *) skb->data);
1554
1555 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1556
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001557 if (status)
1558 return;
1559
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001560 hci_bdaddr_list_clear(&hdev->le_accept_list);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001561}
1562
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001563static void hci_cc_le_add_to_accept_list(struct hci_dev *hdev,
1564 struct sk_buff *skb)
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001565{
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001566 struct hci_cp_le_add_to_accept_list *sent;
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001567 __u8 status = *((__u8 *) skb->data);
1568
1569 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1570
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001571 if (status)
1572 return;
1573
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001574 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001575 if (!sent)
1576 return;
1577
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001578 hci_bdaddr_list_add(&hdev->le_accept_list, &sent->bdaddr,
1579 sent->bdaddr_type);
1580}
1581
1582static void hci_cc_le_del_from_accept_list(struct hci_dev *hdev,
1583 struct sk_buff *skb)
1584{
1585 struct hci_cp_le_del_from_accept_list *sent;
1586 __u8 status = *((__u8 *) skb->data);
1587
1588 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1589
1590 if (status)
1591 return;
1592
1593 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST);
1594 if (!sent)
1595 return;
1596
1597 hci_bdaddr_list_del(&hdev->le_accept_list, &sent->bdaddr,
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001598 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001599}
1600
Johan Hedberg9b008c02013-01-22 14:02:01 +02001601static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
1602 struct sk_buff *skb)
1603{
1604 struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
1605
1606 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1607
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001608 if (rp->status)
1609 return;
1610
1611 memcpy(hdev->le_states, rp->le_states, 8);
Johan Hedberg9b008c02013-01-22 14:02:01 +02001612}
1613
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01001614static void hci_cc_le_read_def_data_len(struct hci_dev *hdev,
1615 struct sk_buff *skb)
1616{
1617 struct hci_rp_le_read_def_data_len *rp = (void *) skb->data;
1618
1619 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1620
1621 if (rp->status)
1622 return;
1623
1624 hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
1625 hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
1626}
1627
1628static void hci_cc_le_write_def_data_len(struct hci_dev *hdev,
1629 struct sk_buff *skb)
1630{
1631 struct hci_cp_le_write_def_data_len *sent;
1632 __u8 status = *((__u8 *) skb->data);
1633
1634 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1635
1636 if (status)
1637 return;
1638
1639 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
1640 if (!sent)
1641 return;
1642
1643 hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
1644 hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
1645}
1646
Ankit Navikb950aa82018-08-17 07:29:19 +05301647static void hci_cc_le_add_to_resolv_list(struct hci_dev *hdev,
1648 struct sk_buff *skb)
1649{
1650 struct hci_cp_le_add_to_resolv_list *sent;
1651 __u8 status = *((__u8 *) skb->data);
1652
1653 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1654
1655 if (status)
1656 return;
1657
1658 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST);
1659 if (!sent)
1660 return;
1661
1662 hci_bdaddr_list_add_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
1663 sent->bdaddr_type, sent->peer_irk,
1664 sent->local_irk);
1665}
1666
1667static void hci_cc_le_del_from_resolv_list(struct hci_dev *hdev,
1668 struct sk_buff *skb)
1669{
1670 struct hci_cp_le_del_from_resolv_list *sent;
1671 __u8 status = *((__u8 *) skb->data);
1672
1673 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1674
1675 if (status)
1676 return;
1677
1678 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST);
1679 if (!sent)
1680 return;
1681
1682 hci_bdaddr_list_del_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
1683 sent->bdaddr_type);
1684}
1685
Ankit Navik545f2592018-06-29 12:13:20 +05301686static void hci_cc_le_clear_resolv_list(struct hci_dev *hdev,
1687 struct sk_buff *skb)
1688{
1689 __u8 status = *((__u8 *) skb->data);
1690
1691 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1692
1693 if (status)
1694 return;
1695
1696 hci_bdaddr_list_clear(&hdev->le_resolv_list);
1697}
1698
Ankit Navikcfdb0c22018-06-29 12:12:50 +05301699static void hci_cc_le_read_resolv_list_size(struct hci_dev *hdev,
1700 struct sk_buff *skb)
1701{
1702 struct hci_rp_le_read_resolv_list_size *rp = (void *) skb->data;
1703
1704 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1705
1706 if (rp->status)
1707 return;
1708
1709 hdev->le_resolv_list_size = rp->size;
1710}
1711
Ankit Navikaa12af72018-08-07 13:16:35 +05301712static void hci_cc_le_set_addr_resolution_enable(struct hci_dev *hdev,
1713 struct sk_buff *skb)
1714{
1715 __u8 *sent, status = *((__u8 *) skb->data);
1716
1717 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1718
1719 if (status)
1720 return;
1721
1722 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE);
1723 if (!sent)
1724 return;
1725
1726 hci_dev_lock(hdev);
1727
1728 if (*sent)
1729 hci_dev_set_flag(hdev, HCI_LL_RPA_RESOLUTION);
1730 else
1731 hci_dev_clear_flag(hdev, HCI_LL_RPA_RESOLUTION);
1732
1733 hci_dev_unlock(hdev);
1734}
1735
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01001736static void hci_cc_le_read_max_data_len(struct hci_dev *hdev,
1737 struct sk_buff *skb)
1738{
1739 struct hci_rp_le_read_max_data_len *rp = (void *) skb->data;
1740
1741 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1742
1743 if (rp->status)
1744 return;
1745
1746 hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
1747 hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
1748 hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
1749 hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
1750}
1751
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001752static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1753 struct sk_buff *skb)
Andre Guedesf9b49302011-06-30 19:20:53 -03001754{
Johan Hedberg06199cf2012-02-22 16:37:11 +02001755 struct hci_cp_write_le_host_supported *sent;
Andre Guedesf9b49302011-06-30 19:20:53 -03001756 __u8 status = *((__u8 *) skb->data);
1757
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001758 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesf9b49302011-06-30 19:20:53 -03001759
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001760 if (status)
1761 return;
1762
Johan Hedberg06199cf2012-02-22 16:37:11 +02001763 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001764 if (!sent)
Andre Guedesf9b49302011-06-30 19:20:53 -03001765 return;
1766
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301767 hci_dev_lock(hdev);
1768
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001769 if (sent->le) {
1770 hdev->features[1][0] |= LMP_HOST_LE;
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001771 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001772 } else {
1773 hdev->features[1][0] &= ~LMP_HOST_LE;
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001774 hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
1775 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001776 }
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001777
1778 if (sent->simul)
1779 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
1780 else
1781 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301782
1783 hci_dev_unlock(hdev);
Andre Guedesf9b49302011-06-30 19:20:53 -03001784}
1785
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02001786static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1787{
1788 struct hci_cp_le_set_adv_param *cp;
1789 u8 status = *((u8 *) skb->data);
1790
1791 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1792
1793 if (status)
1794 return;
1795
1796 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
1797 if (!cp)
1798 return;
1799
1800 hci_dev_lock(hdev);
1801 hdev->adv_addr_type = cp->own_address_type;
1802 hci_dev_unlock(hdev);
1803}
1804
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301805static void hci_cc_set_ext_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1806{
1807 struct hci_rp_le_set_ext_adv_params *rp = (void *) skb->data;
1808 struct hci_cp_le_set_ext_adv_params *cp;
1809 struct adv_info *adv_instance;
1810
1811 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1812
1813 if (rp->status)
1814 return;
1815
1816 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS);
1817 if (!cp)
1818 return;
1819
1820 hci_dev_lock(hdev);
1821 hdev->adv_addr_type = cp->own_addr_type;
Daniel Winkler25e70882021-04-05 16:33:04 -07001822 if (!cp->handle) {
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301823 /* Store in hdev for instance 0 */
1824 hdev->adv_tx_power = rp->tx_power;
1825 } else {
Daniel Winkler25e70882021-04-05 16:33:04 -07001826 adv_instance = hci_find_adv_instance(hdev, cp->handle);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301827 if (adv_instance)
1828 adv_instance->tx_power = rp->tx_power;
1829 }
Jaganath Kanakkasserya0fb3722018-07-19 17:09:42 +05301830 /* Update adv data as tx power is known now */
Daniel Winkler25e70882021-04-05 16:33:04 -07001831 hci_req_update_adv_data(hdev, cp->handle);
Daniel Winkler12410572020-12-03 12:12:49 -08001832
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301833 hci_dev_unlock(hdev);
1834}
1835
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02001836static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
1837{
1838 struct hci_rp_read_rssi *rp = (void *) skb->data;
1839 struct hci_conn *conn;
1840
1841 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1842
1843 if (rp->status)
1844 return;
1845
1846 hci_dev_lock(hdev);
1847
1848 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1849 if (conn)
1850 conn->rssi = rp->rssi;
1851
1852 hci_dev_unlock(hdev);
1853}
1854
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001855static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
1856{
1857 struct hci_cp_read_tx_power *sent;
1858 struct hci_rp_read_tx_power *rp = (void *) skb->data;
1859 struct hci_conn *conn;
1860
1861 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1862
1863 if (rp->status)
1864 return;
1865
1866 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
1867 if (!sent)
1868 return;
1869
1870 hci_dev_lock(hdev);
1871
1872 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001873 if (!conn)
1874 goto unlock;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001875
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001876 switch (sent->type) {
1877 case 0x00:
1878 conn->tx_power = rp->tx_power;
1879 break;
1880 case 0x01:
1881 conn->max_tx_power = rp->tx_power;
1882 break;
1883 }
1884
1885unlock:
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001886 hci_dev_unlock(hdev);
1887}
1888
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08001889static void hci_cc_write_ssp_debug_mode(struct hci_dev *hdev, struct sk_buff *skb)
1890{
1891 u8 status = *((u8 *) skb->data);
1892 u8 *mode;
1893
1894 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1895
1896 if (status)
1897 return;
1898
1899 mode = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE);
1900 if (mode)
1901 hdev->ssp_debug_mode = *mode;
1902}
1903
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001904static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001905{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001906 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001907
1908 if (status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001909 hci_conn_check_pending(hdev);
Johan Hedberg314b2382011-04-27 10:29:57 -04001910 return;
1911 }
1912
Andre Guedes89352e72011-11-04 14:16:53 -03001913 set_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001914}
1915
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001916static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001918 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001921 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001922
1923 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if (!cp)
1925 return;
1926
1927 hci_dev_lock(hdev);
1928
1929 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1930
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001931 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
1933 if (status) {
1934 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001935 if (status != 0x0c || conn->attempt > 2) {
1936 conn->state = BT_CLOSED;
Johan Hedberg539c4962015-02-18 14:53:57 +02001937 hci_connect_cfm(conn, status);
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001938 hci_conn_del(conn);
1939 } else
1940 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942 } else {
1943 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03001944 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr,
1945 HCI_ROLE_MASTER);
1946 if (!conn)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001947 bt_dev_err(hdev, "no memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949 }
1950
1951 hci_dev_unlock(hdev);
1952}
1953
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001954static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001956 struct hci_cp_add_sco *cp;
1957 struct hci_conn *acl, *sco;
1958 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001960 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001961
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001962 if (!status)
1963 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001965 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1966 if (!cp)
1967 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001969 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001971 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001972
1973 hci_dev_lock(hdev);
1974
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001975 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001976 if (acl) {
1977 sco = acl->link;
1978 if (sco) {
1979 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001980
Johan Hedberg539c4962015-02-18 14:53:57 +02001981 hci_connect_cfm(sco, status);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001982 hci_conn_del(sco);
1983 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001984 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001985
1986 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987}
1988
Marcel Holtmannf8558552008-07-14 20:13:49 +02001989static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1990{
1991 struct hci_cp_auth_requested *cp;
1992 struct hci_conn *conn;
1993
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001994 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001995
1996 if (!status)
1997 return;
1998
1999 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
2000 if (!cp)
2001 return;
2002
2003 hci_dev_lock(hdev);
2004
2005 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2006 if (conn) {
2007 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002008 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002009 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02002010 }
2011 }
2012
2013 hci_dev_unlock(hdev);
2014}
2015
2016static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
2017{
2018 struct hci_cp_set_conn_encrypt *cp;
2019 struct hci_conn *conn;
2020
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002021 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02002022
2023 if (!status)
2024 return;
2025
2026 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
2027 if (!cp)
2028 return;
2029
2030 hci_dev_lock(hdev);
2031
2032 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2033 if (conn) {
2034 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002035 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002036 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02002037 }
2038 }
2039
2040 hci_dev_unlock(hdev);
2041}
2042
Johan Hedberg127178d2010-11-18 22:22:29 +02002043static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002044 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02002045{
Johan Hedberg392599b2010-11-18 22:22:28 +02002046 if (conn->state != BT_CONFIG || !conn->out)
2047 return 0;
2048
Johan Hedberg765c2a92011-01-19 12:06:52 +05302049 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02002050 return 0;
2051
2052 /* Only request authentication for SSP connections or non-SSP
Johan Hedberg264b8b42014-01-08 16:40:39 +02002053 * devices with sec_level MEDIUM or HIGH or if MITM protection
2054 * is requested.
2055 */
Gustavo Padovan807deac2012-05-17 00:36:24 -03002056 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
Johan Hedberg7e3691e2014-05-30 14:45:19 +03002057 conn->pending_sec_level != BT_SECURITY_FIPS &&
Johan Hedberg264b8b42014-01-08 16:40:39 +02002058 conn->pending_sec_level != BT_SECURITY_HIGH &&
2059 conn->pending_sec_level != BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02002060 return 0;
2061
Johan Hedberg392599b2010-11-18 22:22:28 +02002062 return 1;
2063}
2064
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002065static int hci_resolve_name(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002066 struct inquiry_entry *e)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002067{
2068 struct hci_cp_remote_name_req cp;
2069
2070 memset(&cp, 0, sizeof(cp));
2071
2072 bacpy(&cp.bdaddr, &e->data.bdaddr);
2073 cp.pscan_rep_mode = e->data.pscan_rep_mode;
2074 cp.pscan_mode = e->data.pscan_mode;
2075 cp.clock_offset = e->data.clock_offset;
2076
2077 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2078}
2079
Johan Hedbergb644ba32012-01-17 21:48:47 +02002080static bool hci_resolve_next_name(struct hci_dev *hdev)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002081{
2082 struct discovery_state *discov = &hdev->discovery;
2083 struct inquiry_entry *e;
2084
Johan Hedbergb644ba32012-01-17 21:48:47 +02002085 if (list_empty(&discov->resolve))
2086 return false;
2087
2088 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
Ram Malovanyc8100892012-07-19 10:26:09 +03002089 if (!e)
2090 return false;
2091
Johan Hedbergb644ba32012-01-17 21:48:47 +02002092 if (hci_resolve_name(hdev, e) == 0) {
2093 e->name_state = NAME_PENDING;
2094 return true;
2095 }
2096
2097 return false;
2098}
2099
2100static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002101 bdaddr_t *bdaddr, u8 *name, u8 name_len)
Johan Hedbergb644ba32012-01-17 21:48:47 +02002102{
2103 struct discovery_state *discov = &hdev->discovery;
2104 struct inquiry_entry *e;
2105
Johan Hedberg60cb49d2014-11-11 11:33:24 +02002106 /* Update the mgmt connected state if necessary. Be careful with
2107 * conn objects that exist but are not (yet) connected however.
2108 * Only those in BT_CONFIG or BT_CONNECTED states can be
2109 * considered connected.
2110 */
2111 if (conn &&
2112 (conn->state == BT_CONFIG || conn->state == BT_CONNECTED) &&
Jaganath Kanakkasserycb77c3e2014-11-07 16:39:09 +05302113 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07002114 mgmt_device_connected(hdev, conn, name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002115
2116 if (discov->state == DISCOVERY_STOPPED)
2117 return;
2118
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002119 if (discov->state == DISCOVERY_STOPPING)
2120 goto discov_complete;
2121
2122 if (discov->state != DISCOVERY_RESOLVING)
2123 return;
2124
2125 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
Ram Malovany7cc83802012-07-19 10:26:10 +03002126 /* If the device was not found in a list of found devices names of which
2127 * are pending. there is no need to continue resolving a next name as it
2128 * will be done upon receiving another Remote Name Request Complete
2129 * Event */
2130 if (!e)
2131 return;
2132
2133 list_del(&e->list);
2134 if (name) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002135 e->name_state = NAME_KNOWN;
Ram Malovany7cc83802012-07-19 10:26:10 +03002136 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
2137 e->data.rssi, name, name_len);
Ram Malovanyc3e7c0d2012-07-19 10:26:11 +03002138 } else {
2139 e->name_state = NAME_NOT_KNOWN;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002140 }
2141
Johan Hedbergb644ba32012-01-17 21:48:47 +02002142 if (hci_resolve_next_name(hdev))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002143 return;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002144
2145discov_complete:
2146 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2147}
2148
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002149static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
2150{
Johan Hedberg127178d2010-11-18 22:22:29 +02002151 struct hci_cp_remote_name_req *cp;
2152 struct hci_conn *conn;
2153
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002154 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02002155
2156 /* If successful wait for the name req complete event before
2157 * checking for the need to do authentication */
2158 if (!status)
2159 return;
2160
2161 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
2162 if (!cp)
2163 return;
2164
2165 hci_dev_lock(hdev);
2166
2167 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002168
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002169 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergb644ba32012-01-17 21:48:47 +02002170 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
2171
Johan Hedberg79c6c702011-04-28 11:28:55 -07002172 if (!conn)
2173 goto unlock;
2174
2175 if (!hci_outgoing_auth_needed(hdev, conn))
2176 goto unlock;
2177
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002178 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02002179 struct hci_cp_auth_requested auth_cp;
2180
Johan Hedberg977f8fc2014-07-17 15:35:39 +03002181 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2182
Johannes Bergc1f23a22013-10-07 18:19:16 +02002183 auth_cp.handle = __cpu_to_le16(conn->handle);
2184 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
2185 sizeof(auth_cp), &auth_cp);
Johan Hedberg127178d2010-11-18 22:22:29 +02002186 }
2187
Johan Hedberg79c6c702011-04-28 11:28:55 -07002188unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02002189 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002190}
2191
Marcel Holtmann769be972008-07-14 20:13:49 +02002192static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
2193{
2194 struct hci_cp_read_remote_features *cp;
2195 struct hci_conn *conn;
2196
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002197 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02002198
2199 if (!status)
2200 return;
2201
2202 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
2203 if (!cp)
2204 return;
2205
2206 hci_dev_lock(hdev);
2207
2208 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2209 if (conn) {
2210 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002211 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002212 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002213 }
2214 }
2215
2216 hci_dev_unlock(hdev);
2217}
2218
2219static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
2220{
2221 struct hci_cp_read_remote_ext_features *cp;
2222 struct hci_conn *conn;
2223
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002224 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02002225
2226 if (!status)
2227 return;
2228
2229 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
2230 if (!cp)
2231 return;
2232
2233 hci_dev_lock(hdev);
2234
2235 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2236 if (conn) {
2237 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002238 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002239 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002240 }
2241 }
2242
2243 hci_dev_unlock(hdev);
2244}
2245
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002246static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2247{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002248 struct hci_cp_setup_sync_conn *cp;
2249 struct hci_conn *acl, *sco;
2250 __u16 handle;
2251
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002252 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002253
2254 if (!status)
2255 return;
2256
2257 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
2258 if (!cp)
2259 return;
2260
2261 handle = __le16_to_cpu(cp->handle);
2262
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002263 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002264
2265 hci_dev_lock(hdev);
2266
2267 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002268 if (acl) {
2269 sco = acl->link;
2270 if (sco) {
2271 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002272
Johan Hedberg539c4962015-02-18 14:53:57 +02002273 hci_connect_cfm(sco, status);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002274 hci_conn_del(sco);
2275 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002276 }
2277
2278 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002279}
2280
Kiran Kb2af2642021-09-07 15:42:43 +05302281static void hci_cs_enhanced_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2282{
2283 struct hci_cp_enhanced_setup_sync_conn *cp;
2284 struct hci_conn *acl, *sco;
2285 __u16 handle;
2286
2287 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2288
2289 if (!status)
2290 return;
2291
2292 cp = hci_sent_cmd_data(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN);
2293 if (!cp)
2294 return;
2295
2296 handle = __le16_to_cpu(cp->handle);
2297
2298 bt_dev_dbg(hdev, "handle 0x%4.4x", handle);
2299
2300 hci_dev_lock(hdev);
2301
2302 acl = hci_conn_hash_lookup_handle(hdev, handle);
2303 if (acl) {
2304 sco = acl->link;
2305 if (sco) {
2306 sco->state = BT_CLOSED;
2307
2308 hci_connect_cfm(sco, status);
2309 hci_conn_del(sco);
2310 }
2311 }
2312
2313 hci_dev_unlock(hdev);
2314}
2315
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002316static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
2317{
2318 struct hci_cp_sniff_mode *cp;
2319 struct hci_conn *conn;
2320
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002321 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002322
2323 if (!status)
2324 return;
2325
2326 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
2327 if (!cp)
2328 return;
2329
2330 hci_dev_lock(hdev);
2331
2332 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002333 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002334 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002335
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002336 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002337 hci_sco_setup(conn, status);
2338 }
2339
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002340 hci_dev_unlock(hdev);
2341}
2342
2343static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
2344{
2345 struct hci_cp_exit_sniff_mode *cp;
2346 struct hci_conn *conn;
2347
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002348 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002349
2350 if (!status)
2351 return;
2352
2353 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
2354 if (!cp)
2355 return;
2356
2357 hci_dev_lock(hdev);
2358
2359 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002360 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002361 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002362
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002363 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002364 hci_sco_setup(conn, status);
2365 }
2366
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002367 hci_dev_unlock(hdev);
2368}
2369
Johan Hedberg88c3df12012-02-09 14:27:38 +02002370static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
2371{
2372 struct hci_cp_disconnect *cp;
2373 struct hci_conn *conn;
2374
2375 if (!status)
2376 return;
2377
2378 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
2379 if (!cp)
2380 return;
2381
2382 hci_dev_lock(hdev);
2383
2384 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Joseph Hwangb8d29052020-03-11 19:20:14 -07002385 if (conn) {
Johan Hedberg88c3df12012-02-09 14:27:38 +02002386 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002387 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002388
Luiz Augusto von Dentz1eeaa1a2021-08-20 16:40:38 -07002389 if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07002390 hdev->cur_adv_instance = conn->adv_instance;
2391 hci_req_reenable_advertising(hdev);
2392 }
2393
Joseph Hwangb8d29052020-03-11 19:20:14 -07002394 /* If the disconnection failed for any reason, the upper layer
2395 * does not retry to disconnect in current implementation.
2396 * Hence, we need to do some basic cleanup here and re-enable
2397 * advertising if necessary.
2398 */
2399 hci_conn_del(conn);
Joseph Hwangb8d29052020-03-11 19:20:14 -07002400 }
2401
Johan Hedberg88c3df12012-02-09 14:27:38 +02002402 hci_dev_unlock(hdev);
2403}
2404
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002405static u8 ev_bdaddr_type(struct hci_dev *hdev, u8 type, bool *resolved)
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002406{
2407 /* When using controller based address resolution, then the new
2408 * address types 0x02 and 0x03 are used. These types need to be
2409 * converted back into either public address or random address type
2410 */
2411 switch (type) {
2412 case ADDR_LE_DEV_PUBLIC_RESOLVED:
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002413 if (resolved)
2414 *resolved = true;
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002415 return ADDR_LE_DEV_PUBLIC;
2416 case ADDR_LE_DEV_RANDOM_RESOLVED:
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002417 if (resolved)
2418 *resolved = true;
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002419 return ADDR_LE_DEV_RANDOM;
2420 }
2421
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002422 if (resolved)
2423 *resolved = false;
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002424 return type;
2425}
2426
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302427static void cs_le_create_conn(struct hci_dev *hdev, bdaddr_t *peer_addr,
2428 u8 peer_addr_type, u8 own_address_type,
2429 u8 filter_policy)
2430{
2431 struct hci_conn *conn;
2432
2433 conn = hci_conn_hash_lookup_le(hdev, peer_addr,
2434 peer_addr_type);
2435 if (!conn)
2436 return;
2437
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002438 own_address_type = ev_bdaddr_type(hdev, own_address_type, NULL);
Sathish Narasimmanb31bc002020-07-23 18:08:59 +05302439
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302440 /* Store the initiator and responder address information which
2441 * is needed for SMP. These values will not change during the
2442 * lifetime of the connection.
2443 */
2444 conn->init_addr_type = own_address_type;
2445 if (own_address_type == ADDR_LE_DEV_RANDOM)
2446 bacpy(&conn->init_addr, &hdev->random_addr);
2447 else
2448 bacpy(&conn->init_addr, &hdev->bdaddr);
2449
2450 conn->resp_addr_type = peer_addr_type;
2451 bacpy(&conn->resp_addr, peer_addr);
2452
2453 /* We don't want the connection attempt to stick around
2454 * indefinitely since LE doesn't have a page timeout concept
2455 * like BR/EDR. Set a timer for any connection that doesn't use
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002456 * the accept list for connecting.
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302457 */
2458 if (filter_policy == HCI_LE_USE_PEER_ADDR)
2459 queue_delayed_work(conn->hdev->workqueue,
2460 &conn->le_conn_timeout,
2461 conn->conn_timeout);
2462}
2463
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002464static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
2465{
2466 struct hci_cp_le_create_conn *cp;
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002467
2468 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2469
2470 /* All connection failure handling is taken care of by the
2471 * hci_le_conn_failed function which is triggered by the HCI
2472 * request completion callbacks used for connecting.
2473 */
2474 if (status)
2475 return;
2476
2477 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
2478 if (!cp)
2479 return;
2480
2481 hci_dev_lock(hdev);
2482
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302483 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2484 cp->own_address_type, cp->filter_policy);
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002485
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002486 hci_dev_unlock(hdev);
2487}
2488
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02002489static void hci_cs_le_ext_create_conn(struct hci_dev *hdev, u8 status)
2490{
2491 struct hci_cp_le_ext_create_conn *cp;
2492
2493 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2494
2495 /* All connection failure handling is taken care of by the
2496 * hci_le_conn_failed function which is triggered by the HCI
2497 * request completion callbacks used for connecting.
2498 */
2499 if (status)
2500 return;
2501
2502 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_EXT_CREATE_CONN);
2503 if (!cp)
2504 return;
2505
2506 hci_dev_lock(hdev);
2507
2508 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2509 cp->own_addr_type, cp->filter_policy);
2510
2511 hci_dev_unlock(hdev);
2512}
2513
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07002514static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
2515{
2516 struct hci_cp_le_read_remote_features *cp;
2517 struct hci_conn *conn;
2518
2519 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2520
2521 if (!status)
2522 return;
2523
2524 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
2525 if (!cp)
2526 return;
2527
2528 hci_dev_lock(hdev);
2529
2530 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2531 if (conn) {
2532 if (conn->state == BT_CONFIG) {
2533 hci_connect_cfm(conn, status);
2534 hci_conn_drop(conn);
2535 }
2536 }
2537
2538 hci_dev_unlock(hdev);
2539}
2540
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02002541static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
2542{
2543 struct hci_cp_le_start_enc *cp;
2544 struct hci_conn *conn;
2545
2546 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2547
2548 if (!status)
2549 return;
2550
2551 hci_dev_lock(hdev);
2552
2553 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
2554 if (!cp)
2555 goto unlock;
2556
2557 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2558 if (!conn)
2559 goto unlock;
2560
2561 if (conn->state != BT_CONNECTED)
2562 goto unlock;
2563
2564 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2565 hci_conn_drop(conn);
2566
2567unlock:
2568 hci_dev_unlock(hdev);
2569}
2570
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01002571static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
2572{
2573 struct hci_cp_switch_role *cp;
2574 struct hci_conn *conn;
2575
2576 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2577
2578 if (!status)
2579 return;
2580
2581 cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
2582 if (!cp)
2583 return;
2584
2585 hci_dev_lock(hdev);
2586
2587 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2588 if (conn)
2589 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
2590
2591 hci_dev_unlock(hdev);
2592}
2593
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002594static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002595{
2596 __u8 status = *((__u8 *) skb->data);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002597 struct discovery_state *discov = &hdev->discovery;
2598 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002599
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002600 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002601
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002602 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03002603
2604 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
2605 return;
2606
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002607 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -03002608 wake_up_bit(&hdev->flags, HCI_INQUIRY);
2609
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002610 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002611 return;
2612
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002613 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002614
Andre Guedes343f9352012-02-17 20:39:37 -03002615 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002616 goto unlock;
2617
2618 if (list_empty(&discov->resolve)) {
Jakub Pawlowski07d23342015-03-17 09:04:14 -07002619 /* When BR/EDR inquiry is active and no LE scanning is in
2620 * progress, then change discovery state to indicate completion.
2621 *
2622 * When running LE scanning and BR/EDR inquiry simultaneously
2623 * and the LE scan already finished, then change the discovery
2624 * state to indicate completion.
2625 */
2626 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2627 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2628 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002629 goto unlock;
2630 }
2631
2632 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
2633 if (e && hci_resolve_name(hdev, e) == 0) {
2634 e->name_state = NAME_PENDING;
2635 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
2636 } else {
Jakub Pawlowski07d23342015-03-17 09:04:14 -07002637 /* When BR/EDR inquiry is active and no LE scanning is in
2638 * progress, then change discovery state to indicate completion.
2639 *
2640 * When running LE scanning and BR/EDR inquiry simultaneously
2641 * and the LE scan already finished, then change the discovery
2642 * state to indicate completion.
2643 */
2644 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2645 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2646 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002647 }
2648
2649unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002650 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002651}
2652
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002653static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002655 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002656 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 int num_rsp = *((__u8 *) skb->data);
2658
2659 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2660
Peilin Ye75bbd2e2020-07-10 17:39:18 -04002661 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002662 return;
2663
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002664 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03002665 return;
2666
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002668
Johan Hedberge17acd42011-03-30 23:57:16 +03002669 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02002670 u32 flags;
Johan Hedberg31754052012-01-04 13:39:52 +02002671
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 bacpy(&data.bdaddr, &info->bdaddr);
2673 data.pscan_rep_mode = info->pscan_rep_mode;
2674 data.pscan_period_mode = info->pscan_period_mode;
2675 data.pscan_mode = info->pscan_mode;
2676 memcpy(data.dev_class, info->dev_class, 3);
2677 data.clock_offset = info->clock_offset;
Marcel Holtmannefb25132014-12-05 13:03:34 +01002678 data.rssi = HCI_RSSI_INVALID;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002679 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02002680
Marcel Holtmannaf589252014-07-01 14:11:20 +02002681 flags = hci_inquiry_cache_update(hdev, &data, false);
2682
Johan Hedberg48264f02011-11-09 13:58:58 +02002683 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannefb25132014-12-05 13:03:34 +01002684 info->dev_class, HCI_RSSI_INVALID,
2685 flags, NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002687
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 hci_dev_unlock(hdev);
2689}
2690
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002691static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002693 struct hci_ev_conn_complete *ev = (void *) skb->data;
2694 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002696 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002699
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002700 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02002701 if (!conn) {
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002702 /* Connection may not exist if auto-connected. Check the bredr
2703 * allowlist to see if this device is allowed to auto connect.
2704 * If link is an ACL type, create a connection class
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002705 * automatically.
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002706 *
2707 * Auto-connect will only occur if the event filter is
2708 * programmed with a given address. Right now, event filter is
2709 * only used during suspend.
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002710 */
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002711 if (ev->link_type == ACL_LINK &&
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002712 hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002713 &ev->bdaddr,
2714 BDADDR_BREDR)) {
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002715 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2716 HCI_ROLE_SLAVE);
2717 if (!conn) {
2718 bt_dev_err(hdev, "no memory for new conn");
2719 goto unlock;
2720 }
Abhishek Pandit-Subedi2d186fc2020-03-19 17:07:13 -07002721 } else {
2722 if (ev->link_type != SCO_LINK)
2723 goto unlock;
2724
2725 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK,
2726 &ev->bdaddr);
2727 if (!conn)
2728 goto unlock;
2729
2730 conn->type = SCO_LINK;
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002731 }
Marcel Holtmann94992372009-04-19 19:30:03 +02002732 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002733
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002734 if (!ev->status) {
2735 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02002736
2737 if (conn->type == ACL_LINK) {
2738 conn->state = BT_CONFIG;
2739 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02002740
2741 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
2742 !hci_find_link_key(hdev, &ev->bdaddr))
2743 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2744 else
2745 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02002746 } else
2747 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002748
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01002749 hci_debugfs_create_conn(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002750 hci_conn_add_sysfs(conn);
2751
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002752 if (test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002753 set_bit(HCI_CONN_AUTH, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002754
2755 if (test_bit(HCI_ENCRYPT, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002756 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002757
2758 /* Get remote features */
2759 if (conn->type == ACL_LINK) {
2760 struct hci_cp_read_remote_features cp;
2761 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02002762 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002763 sizeof(cp), &cp);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002764
Johan Hedberg01b1cb82015-11-16 12:52:21 +02002765 hci_req_update_scan(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002766 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002767
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002768 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02002769 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002770 struct hci_cp_change_conn_ptype cp;
2771 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02002772 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002773 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
2774 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002775 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02002776 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002777 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02002778 if (conn->type == ACL_LINK)
Marcel Holtmann64c7b772014-02-18 14:22:20 -08002779 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002780 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02002781 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002782
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002783 if (conn->type == ACL_LINK)
2784 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002785
Marcel Holtmann769be972008-07-14 20:13:49 +02002786 if (ev->status) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002787 hci_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002788 hci_conn_del(conn);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02002789 } else if (ev->link_type == SCO_LINK) {
2790 switch (conn->setting & SCO_AIRMODE_MASK) {
2791 case SCO_AIRMODE_CVSD:
2792 if (hdev->notify)
2793 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
2794 break;
2795 }
2796
Johan Hedberg539c4962015-02-18 14:53:57 +02002797 hci_connect_cfm(conn, ev->status);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02002798 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002799
2800unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002802
2803 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
Johan Hedberg70c46422014-07-09 12:59:17 +03002806static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
2807{
2808 struct hci_cp_reject_conn_req cp;
2809
2810 bacpy(&cp.bdaddr, bdaddr);
2811 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
2812 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
2813}
2814
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002815static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002817 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 int mask = hdev->link_mode;
Johan Hedberg70c46422014-07-09 12:59:17 +03002819 struct inquiry_entry *ie;
2820 struct hci_conn *conn;
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01002821 __u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002823 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002824 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01002826 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
2827 &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Johan Hedberg70c46422014-07-09 12:59:17 +03002829 if (!(mask & HCI_LM_ACCEPT)) {
2830 hci_reject_conn(hdev, &ev->bdaddr);
2831 return;
2832 }
2833
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002834 if (hci_bdaddr_list_lookup(&hdev->reject_list, &ev->bdaddr,
Johan Hedberg46c4c942014-07-16 16:19:21 +03002835 BDADDR_BREDR)) {
2836 hci_reject_conn(hdev, &ev->bdaddr);
2837 return;
2838 }
2839
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002840 /* Require HCI_CONNECTABLE or an accept list entry to accept the
Johan Hedberg6a8fc952014-12-24 20:43:11 +02002841 * connection. These features are only touched through mgmt so
2842 * only do the checks if HCI_MGMT is set.
2843 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002844 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
2845 !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002846 !hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, &ev->bdaddr,
Abhishek Pandit-Subedi8baaa402020-06-17 16:39:08 +02002847 BDADDR_BREDR)) {
2848 hci_reject_conn(hdev, &ev->bdaddr);
2849 return;
Johan Hedberg70c46422014-07-09 12:59:17 +03002850 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
Johan Hedberg70c46422014-07-09 12:59:17 +03002852 /* Connection accepted */
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002853
Johan Hedberg70c46422014-07-09 12:59:17 +03002854 hci_dev_lock(hdev);
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02002855
Johan Hedberg70c46422014-07-09 12:59:17 +03002856 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2857 if (ie)
2858 memcpy(ie->data.dev_class, ev->dev_class, 3);
2859
2860 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
2861 &ev->bdaddr);
2862 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03002863 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2864 HCI_ROLE_SLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002866 bt_dev_err(hdev, "no memory for new connection");
Johan Hedberg70c46422014-07-09 12:59:17 +03002867 hci_dev_unlock(hdev);
2868 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
Johan Hedberg70c46422014-07-09 12:59:17 +03002870 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002871
Johan Hedberg70c46422014-07-09 12:59:17 +03002872 memcpy(conn->dev_class, ev->dev_class, 3);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002873
Johan Hedberg70c46422014-07-09 12:59:17 +03002874 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Johan Hedberg70c46422014-07-09 12:59:17 +03002876 if (ev->link_type == ACL_LINK ||
2877 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
2878 struct hci_cp_accept_conn_req cp;
2879 conn->state = BT_CONNECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 bacpy(&cp.bdaddr, &ev->bdaddr);
Johan Hedberg70c46422014-07-09 12:59:17 +03002882
2883 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
Archie Pusaka74be5232021-06-04 16:26:25 +08002884 cp.role = 0x00; /* Become central */
Johan Hedberg70c46422014-07-09 12:59:17 +03002885 else
Archie Pusaka74be5232021-06-04 16:26:25 +08002886 cp.role = 0x01; /* Remain peripheral */
Johan Hedberg70c46422014-07-09 12:59:17 +03002887
2888 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
2889 } else if (!(flags & HCI_PROTO_DEFER)) {
2890 struct hci_cp_accept_sync_conn_req cp;
2891 conn->state = BT_CONNECT;
2892
2893 bacpy(&cp.bdaddr, &ev->bdaddr);
2894 cp.pkt_type = cpu_to_le16(conn->pkt_type);
2895
2896 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
2897 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
2898 cp.max_latency = cpu_to_le16(0xffff);
2899 cp.content_format = cpu_to_le16(hdev->voice_setting);
2900 cp.retrans_effort = 0xff;
2901
2902 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
2903 &cp);
2904 } else {
2905 conn->state = BT_CONNECT2;
Johan Hedberg539c4962015-02-18 14:53:57 +02002906 hci_connect_cfm(conn, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
2908}
2909
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02002910static u8 hci_to_mgmt_reason(u8 err)
2911{
2912 switch (err) {
2913 case HCI_ERROR_CONNECTION_TIMEOUT:
2914 return MGMT_DEV_DISCONN_TIMEOUT;
2915 case HCI_ERROR_REMOTE_USER_TERM:
2916 case HCI_ERROR_REMOTE_LOW_RESOURCES:
2917 case HCI_ERROR_REMOTE_POWER_OFF:
2918 return MGMT_DEV_DISCONN_REMOTE;
2919 case HCI_ERROR_LOCAL_HOST_TERM:
2920 return MGMT_DEV_DISCONN_LOCAL_HOST;
2921 default:
2922 return MGMT_DEV_DISCONN_UNKNOWN;
2923 }
2924}
2925
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002926static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002928 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Szymon Janc160b9252016-07-12 02:12:16 +02002929 u8 reason;
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002930 struct hci_conn_params *params;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002931 struct hci_conn *conn;
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002932 bool mgmt_connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002934 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 hci_dev_lock(hdev);
2937
Marcel Holtmann04837f62006-07-03 10:02:33 +02002938 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02002939 if (!conn)
2940 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002941
Andre Guedesabf54a52013-11-07 17:36:09 -03002942 if (ev->status) {
2943 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2944 conn->dst_type, ev->status);
2945 goto unlock;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02002946 }
Johan Hedbergf7520542011-01-20 12:34:39 +02002947
Andre Guedes38462202013-11-07 17:36:10 -03002948 conn->state = BT_CLOSED;
2949
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002950 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
Szymon Janc160b9252016-07-12 02:12:16 +02002951
2952 if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
2953 reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
2954 else
2955 reason = hci_to_mgmt_reason(ev->reason);
2956
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002957 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2958 reason, mgmt_connected);
Andre Guedesabf54a52013-11-07 17:36:09 -03002959
Johan Hedberg22f433d2014-08-01 11:13:32 +03002960 if (conn->type == ACL_LINK) {
2961 if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2962 hci_remove_link_key(hdev, &conn->dst);
2963
Johan Hedberg01b1cb82015-11-16 12:52:21 +02002964 hci_req_update_scan(hdev);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002965 }
Johan Hedberg22102462013-10-05 12:01:06 +02002966
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002967 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2968 if (params) {
2969 switch (params->auto_connect) {
2970 case HCI_AUTO_CONN_LINK_LOSS:
2971 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2972 break;
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05002973 fallthrough;
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002974
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02002975 case HCI_AUTO_CONN_DIRECT:
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002976 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg418025d2014-07-04 12:37:24 +03002977 list_del_init(&params->action);
2978 list_add(&params->action, &hdev->pend_le_conns);
2979 hci_update_background_scan(hdev);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002980 break;
2981
2982 default:
2983 break;
2984 }
2985 }
2986
Johan Hedberg3a6d5762015-02-18 14:53:58 +02002987 hci_disconn_cfm(conn, ev->reason);
Andre Guedes38462202013-11-07 17:36:10 -03002988
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002989 /* The suspend notifier is waiting for all devices to disconnect so
2990 * clear the bit from pending tasks and inform the wait queue.
2991 */
2992 if (list_empty(&hdev->conn_hash.list) &&
2993 test_and_clear_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks)) {
2994 wake_up(&hdev->suspend_wait_q);
2995 }
2996
Andre Guedes38462202013-11-07 17:36:10 -03002997 /* Re-enable advertising if necessary, since it might
2998 * have been disabled by the connection. From the
2999 * HCI_LE_Set_Advertise_Enable command description in
3000 * the core specification (v4.0):
3001 * "The Controller shall continue advertising until the Host
3002 * issues an LE_Set_Advertise_Enable command with
3003 * Advertising_Enable set to 0x00 (Advertising is disabled)
3004 * or until a connection is created or until the Advertising
3005 * is timed out due to Directed Advertising."
3006 */
Luiz Augusto von Dentz1eeaa1a2021-08-20 16:40:38 -07003007 if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07003008 hdev->cur_adv_instance = conn->adv_instance;
Johan Hedbergf2252572015-11-18 12:49:20 +02003009 hci_req_reenable_advertising(hdev);
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07003010 }
3011
3012 hci_conn_del(conn);
Johan Hedbergf7520542011-01-20 12:34:39 +02003013
3014unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 hci_dev_unlock(hdev);
3016}
3017
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003018static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003019{
3020 struct hci_ev_auth_complete *ev = (void *) skb->data;
3021 struct hci_conn *conn;
3022
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003023 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003024
3025 hci_dev_lock(hdev);
3026
3027 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003028 if (!conn)
3029 goto unlock;
3030
3031 if (!ev->status) {
Szymon Janc160b9252016-07-12 02:12:16 +02003032 clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3033
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02003034 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03003035 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003036 bt_dev_info(hdev, "re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03003037 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03003038 set_bit(HCI_CONN_AUTH, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003039 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03003040 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003041 } else {
Szymon Janc160b9252016-07-12 02:12:16 +02003042 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3043 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3044
Johan Hedberge1e930f2014-09-08 17:09:49 -07003045 mgmt_auth_failed(conn, ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003046 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003047
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003048 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
3049 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003050
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003051 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02003052 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003053 struct hci_cp_set_conn_encrypt cp;
3054 cp.handle = ev->handle;
3055 cp.encrypt = 0x01;
3056 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03003057 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003058 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003059 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02003060 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003061 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003062 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003063 } else {
3064 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003065
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003066 hci_conn_hold(conn);
3067 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003068 hci_conn_drop(conn);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003069 }
3070
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003071 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003072 if (!ev->status) {
3073 struct hci_cp_set_conn_encrypt cp;
3074 cp.handle = ev->handle;
3075 cp.encrypt = 0x01;
3076 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03003077 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003078 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003079 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003080 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003081 }
3082 }
3083
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003084unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003085 hci_dev_unlock(hdev);
3086}
3087
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003088static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003089{
Johan Hedberg127178d2010-11-18 22:22:29 +02003090 struct hci_ev_remote_name *ev = (void *) skb->data;
3091 struct hci_conn *conn;
3092
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003093 BT_DBG("%s", hdev->name);
3094
3095 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02003096
3097 hci_dev_lock(hdev);
3098
3099 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003100
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003101 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergb644ba32012-01-17 21:48:47 +02003102 goto check_auth;
3103
3104 if (ev->status == 0)
3105 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003106 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02003107 else
3108 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
3109
3110check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07003111 if (!conn)
3112 goto unlock;
3113
3114 if (!hci_outgoing_auth_needed(hdev, conn))
3115 goto unlock;
3116
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003117 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003118 struct hci_cp_auth_requested cp;
Johan Hedberg977f8fc2014-07-17 15:35:39 +03003119
3120 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
3121
Johan Hedberg127178d2010-11-18 22:22:29 +02003122 cp.handle = __cpu_to_le16(conn->handle);
3123 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
3124 }
3125
Johan Hedberg79c6c702011-04-28 11:28:55 -07003126unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02003127 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003128}
3129
Johan Hedberg821f3762015-06-11 13:52:29 +03003130static void read_enc_key_size_complete(struct hci_dev *hdev, u8 status,
3131 u16 opcode, struct sk_buff *skb)
3132{
3133 const struct hci_rp_read_enc_key_size *rp;
3134 struct hci_conn *conn;
3135 u16 handle;
3136
3137 BT_DBG("%s status 0x%02x", hdev->name, status);
3138
3139 if (!skb || skb->len < sizeof(*rp)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003140 bt_dev_err(hdev, "invalid read key size response");
Johan Hedberg821f3762015-06-11 13:52:29 +03003141 return;
3142 }
3143
3144 rp = (void *)skb->data;
3145 handle = le16_to_cpu(rp->handle);
3146
3147 hci_dev_lock(hdev);
3148
3149 conn = hci_conn_hash_lookup_handle(hdev, handle);
3150 if (!conn)
3151 goto unlock;
3152
Alain Michaud32b50722020-03-25 14:48:34 +00003153 /* While unexpected, the read_enc_key_size command may fail. The most
3154 * secure approach is to then assume the key size is 0 to force a
3155 * disconnection.
Johan Hedberg821f3762015-06-11 13:52:29 +03003156 */
3157 if (rp->status) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003158 bt_dev_err(hdev, "failed to read key size for handle %u",
3159 handle);
Alain Michaud32b50722020-03-25 14:48:34 +00003160 conn->enc_key_size = 0;
Johan Hedberg821f3762015-06-11 13:52:29 +03003161 } else {
3162 conn->enc_key_size = rp->key_size;
3163 }
3164
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003165 hci_encrypt_cfm(conn, 0);
Johan Hedberg821f3762015-06-11 13:52:29 +03003166
3167unlock:
3168 hci_dev_unlock(hdev);
3169}
3170
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003171static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003172{
3173 struct hci_ev_encrypt_change *ev = (void *) skb->data;
3174 struct hci_conn *conn;
3175
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003176 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003177
3178 hci_dev_lock(hdev);
3179
3180 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003181 if (!conn)
3182 goto unlock;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003183
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003184 if (!ev->status) {
3185 if (ev->encrypt) {
3186 /* Encryption implies authentication */
Johan Hedberg4dae2792014-06-24 17:03:50 +03003187 set_bit(HCI_CONN_AUTH, &conn->flags);
3188 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003189 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003190
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08003191 /* P-256 authentication key implies FIPS */
3192 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
Johan Hedberg4dae2792014-06-24 17:03:50 +03003193 set_bit(HCI_CONN_FIPS, &conn->flags);
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08003194
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003195 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
3196 conn->type == LE_LINK)
3197 set_bit(HCI_CONN_AES_CCM, &conn->flags);
3198 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03003199 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003200 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
3201 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003202 }
3203
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07003204 /* We should disregard the current RPA and generate a new one
3205 * whenever the encryption procedure fails.
3206 */
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303207 if (ev->status && conn->type == LE_LINK) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07003208 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303209 hci_adv_instances_set_rpa_expired(hdev, true);
3210 }
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07003211
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003212 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3213
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07003214 /* Check link security requirements are met */
3215 if (!hci_conn_check_link_mode(conn))
3216 ev->status = HCI_ERROR_AUTH_FAILURE;
3217
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003218 if (ev->status && conn->state == BT_CONNECTED) {
Szymon Janc160b9252016-07-12 02:12:16 +02003219 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3220 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3221
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07003222 /* Notify upper layers so they can cleanup before
3223 * disconnecting.
3224 */
3225 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003226 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
3227 hci_conn_drop(conn);
3228 goto unlock;
3229 }
3230
Johan Hedberg821f3762015-06-11 13:52:29 +03003231 /* Try reading the encryption key size for encrypted ACL links */
3232 if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
3233 struct hci_cp_read_enc_key_size cp;
3234 struct hci_request req;
3235
3236 /* Only send HCI_Read_Encryption_Key_Size if the
3237 * controller really supports it. If it doesn't, assume
3238 * the default size (16).
3239 */
3240 if (!(hdev->commands[20] & 0x10)) {
3241 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3242 goto notify;
3243 }
3244
3245 hci_req_init(&req, hdev);
3246
3247 cp.handle = cpu_to_le16(conn->handle);
3248 hci_req_add(&req, HCI_OP_READ_ENC_KEY_SIZE, sizeof(cp), &cp);
3249
3250 if (hci_req_run_skb(&req, read_enc_key_size_complete)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003251 bt_dev_err(hdev, "sending read key size failed");
Johan Hedberg821f3762015-06-11 13:52:29 +03003252 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3253 goto notify;
3254 }
3255
3256 goto unlock;
3257 }
3258
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +05303259 /* Set the default Authenticated Payload Timeout after
3260 * an LE Link is established. As per Core Spec v5.0, Vol 2, Part B
3261 * Section 3.3, the HCI command WRITE_AUTH_PAYLOAD_TIMEOUT should be
3262 * sent when the link is active and Encryption is enabled, the conn
3263 * type can be either LE or ACL and controller must support LMP Ping.
3264 * Ensure for AES-CCM encryption as well.
3265 */
3266 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
3267 test_bit(HCI_CONN_AES_CCM, &conn->flags) &&
3268 ((conn->type == ACL_LINK && lmp_ping_capable(hdev)) ||
3269 (conn->type == LE_LINK && (hdev->le_features[0] & HCI_LE_PING)))) {
3270 struct hci_cp_write_auth_payload_to cp;
3271
3272 cp.handle = cpu_to_le16(conn->handle);
3273 cp.timeout = cpu_to_le16(hdev->auth_payload_timeout);
3274 hci_send_cmd(conn->hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO,
3275 sizeof(cp), &cp);
3276 }
3277
Johan Hedberg821f3762015-06-11 13:52:29 +03003278notify:
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003279 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003280
Gustavo Padovana7d77232012-05-13 03:20:07 -03003281unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003282 hci_dev_unlock(hdev);
3283}
3284
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003285static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
3286 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003287{
3288 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
3289 struct hci_conn *conn;
3290
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003291 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003292
3293 hci_dev_lock(hdev);
3294
3295 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3296 if (conn) {
3297 if (!ev->status)
Johan Hedberg4dae2792014-06-24 17:03:50 +03003298 set_bit(HCI_CONN_SECURE, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003299
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003300 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003301
3302 hci_key_change_cfm(conn, ev->status);
3303 }
3304
3305 hci_dev_unlock(hdev);
3306}
3307
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003308static void hci_remote_features_evt(struct hci_dev *hdev,
3309 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003310{
3311 struct hci_ev_remote_features *ev = (void *) skb->data;
3312 struct hci_conn *conn;
3313
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003314 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003315
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003316 hci_dev_lock(hdev);
3317
3318 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02003319 if (!conn)
3320 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02003321
Johan Hedbergccd556f2010-11-10 17:11:51 +02003322 if (!ev->status)
Johan Hedbergcad718e2013-04-17 15:00:51 +03003323 memcpy(conn->features[0], ev->features, 8);
Johan Hedbergccd556f2010-11-10 17:11:51 +02003324
3325 if (conn->state != BT_CONFIG)
3326 goto unlock;
3327
Szymon Jancac363cf2015-01-29 16:36:59 +01003328 if (!ev->status && lmp_ext_feat_capable(hdev) &&
3329 lmp_ext_feat_capable(conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003330 struct hci_cp_read_remote_ext_features cp;
3331 cp.handle = ev->handle;
3332 cp.page = 0x01;
3333 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003334 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02003335 goto unlock;
3336 }
3337
Johan Hedberg671267b2012-05-12 16:11:50 -03003338 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003339 struct hci_cp_remote_name_req cp;
3340 memset(&cp, 0, sizeof(cp));
3341 bacpy(&cp.bdaddr, &conn->dst);
3342 cp.pscan_rep_mode = 0x02;
3343 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003344 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07003345 mgmt_device_connected(hdev, conn, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02003346
Johan Hedberg127178d2010-11-18 22:22:29 +02003347 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003348 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02003349 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003350 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02003351 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003352
Johan Hedbergccd556f2010-11-10 17:11:51 +02003353unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003354 hci_dev_unlock(hdev);
3355}
3356
Kiran Kecb71f252021-08-16 05:07:47 +05303357static inline void handle_cmd_cnt_and_timer(struct hci_dev *hdev, u8 ncmd)
Manish Mandlikde75cd02021-04-29 10:24:22 -07003358{
Kiran Kecb71f252021-08-16 05:07:47 +05303359 cancel_delayed_work(&hdev->cmd_timer);
Manish Mandlikde75cd02021-04-29 10:24:22 -07003360
3361 if (!test_bit(HCI_RESET, &hdev->flags)) {
3362 if (ncmd) {
3363 cancel_delayed_work(&hdev->ncmd_timer);
3364 atomic_set(&hdev->cmd_cnt, 1);
3365 } else {
3366 schedule_delayed_work(&hdev->ncmd_timer,
3367 HCI_NCMD_TIMEOUT);
3368 }
3369 }
3370}
3371
Johan Hedberge62144872015-04-02 13:41:08 +03003372static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
3373 u16 *opcode, u8 *status,
3374 hci_req_complete_t *req_complete,
3375 hci_req_complete_skb_t *req_complete_skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003376{
3377 struct hci_ev_cmd_complete *ev = (void *) skb->data;
Johan Hedberge62144872015-04-02 13:41:08 +03003378
3379 *opcode = __le16_to_cpu(ev->opcode);
3380 *status = skb->data[sizeof(*ev)];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003381
3382 skb_pull(skb, sizeof(*ev));
3383
Johan Hedberge62144872015-04-02 13:41:08 +03003384 switch (*opcode) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003385 case HCI_OP_INQUIRY_CANCEL:
Sonny Sasakaadf1d692020-05-06 12:55:03 -07003386 hci_cc_inquiry_cancel(hdev, skb, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003387 break;
3388
Andre Guedes4d934832012-03-21 00:03:35 -03003389 case HCI_OP_PERIODIC_INQ:
3390 hci_cc_periodic_inq(hdev, skb);
3391 break;
3392
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003393 case HCI_OP_EXIT_PERIODIC_INQ:
3394 hci_cc_exit_periodic_inq(hdev, skb);
3395 break;
3396
3397 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
3398 hci_cc_remote_name_req_cancel(hdev, skb);
3399 break;
3400
3401 case HCI_OP_ROLE_DISCOVERY:
3402 hci_cc_role_discovery(hdev, skb);
3403 break;
3404
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02003405 case HCI_OP_READ_LINK_POLICY:
3406 hci_cc_read_link_policy(hdev, skb);
3407 break;
3408
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003409 case HCI_OP_WRITE_LINK_POLICY:
3410 hci_cc_write_link_policy(hdev, skb);
3411 break;
3412
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02003413 case HCI_OP_READ_DEF_LINK_POLICY:
3414 hci_cc_read_def_link_policy(hdev, skb);
3415 break;
3416
3417 case HCI_OP_WRITE_DEF_LINK_POLICY:
3418 hci_cc_write_def_link_policy(hdev, skb);
3419 break;
3420
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003421 case HCI_OP_RESET:
3422 hci_cc_reset(hdev, skb);
3423 break;
3424
Marcel Holtmannc2f0f972015-01-12 09:21:25 -08003425 case HCI_OP_READ_STORED_LINK_KEY:
3426 hci_cc_read_stored_link_key(hdev, skb);
3427 break;
3428
Marcel Holtmanna93661202015-01-12 09:21:28 -08003429 case HCI_OP_DELETE_STORED_LINK_KEY:
3430 hci_cc_delete_stored_link_key(hdev, skb);
3431 break;
3432
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003433 case HCI_OP_WRITE_LOCAL_NAME:
3434 hci_cc_write_local_name(hdev, skb);
3435 break;
3436
3437 case HCI_OP_READ_LOCAL_NAME:
3438 hci_cc_read_local_name(hdev, skb);
3439 break;
3440
3441 case HCI_OP_WRITE_AUTH_ENABLE:
3442 hci_cc_write_auth_enable(hdev, skb);
3443 break;
3444
3445 case HCI_OP_WRITE_ENCRYPT_MODE:
3446 hci_cc_write_encrypt_mode(hdev, skb);
3447 break;
3448
3449 case HCI_OP_WRITE_SCAN_ENABLE:
3450 hci_cc_write_scan_enable(hdev, skb);
3451 break;
3452
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -08003453 case HCI_OP_SET_EVENT_FLT:
3454 hci_cc_set_event_filter(hdev, skb);
3455 break;
3456
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003457 case HCI_OP_READ_CLASS_OF_DEV:
3458 hci_cc_read_class_of_dev(hdev, skb);
3459 break;
3460
3461 case HCI_OP_WRITE_CLASS_OF_DEV:
3462 hci_cc_write_class_of_dev(hdev, skb);
3463 break;
3464
3465 case HCI_OP_READ_VOICE_SETTING:
3466 hci_cc_read_voice_setting(hdev, skb);
3467 break;
3468
3469 case HCI_OP_WRITE_VOICE_SETTING:
3470 hci_cc_write_voice_setting(hdev, skb);
3471 break;
3472
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -07003473 case HCI_OP_READ_NUM_SUPPORTED_IAC:
3474 hci_cc_read_num_supported_iac(hdev, skb);
3475 break;
3476
Marcel Holtmann333140b2008-07-14 20:13:48 +02003477 case HCI_OP_WRITE_SSP_MODE:
3478 hci_cc_write_ssp_mode(hdev, skb);
3479 break;
3480
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08003481 case HCI_OP_WRITE_SC_SUPPORT:
3482 hci_cc_write_sc_support(hdev, skb);
3483 break;
3484
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +05303485 case HCI_OP_READ_AUTH_PAYLOAD_TO:
3486 hci_cc_read_auth_payload_timeout(hdev, skb);
3487 break;
3488
3489 case HCI_OP_WRITE_AUTH_PAYLOAD_TO:
3490 hci_cc_write_auth_payload_timeout(hdev, skb);
3491 break;
3492
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003493 case HCI_OP_READ_LOCAL_VERSION:
3494 hci_cc_read_local_version(hdev, skb);
3495 break;
3496
3497 case HCI_OP_READ_LOCAL_COMMANDS:
3498 hci_cc_read_local_commands(hdev, skb);
3499 break;
3500
3501 case HCI_OP_READ_LOCAL_FEATURES:
3502 hci_cc_read_local_features(hdev, skb);
3503 break;
3504
Andre Guedes971e3a42011-06-30 19:20:52 -03003505 case HCI_OP_READ_LOCAL_EXT_FEATURES:
3506 hci_cc_read_local_ext_features(hdev, skb);
3507 break;
3508
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003509 case HCI_OP_READ_BUFFER_SIZE:
3510 hci_cc_read_buffer_size(hdev, skb);
3511 break;
3512
3513 case HCI_OP_READ_BD_ADDR:
3514 hci_cc_read_bd_addr(hdev, skb);
3515 break;
3516
Marcel Holtmanna4790362020-04-03 21:44:04 +02003517 case HCI_OP_READ_LOCAL_PAIRING_OPTS:
3518 hci_cc_read_local_pairing_opts(hdev, skb);
3519 break;
3520
Johan Hedbergf332ec62013-03-15 17:07:11 -05003521 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
3522 hci_cc_read_page_scan_activity(hdev, skb);
3523 break;
3524
Johan Hedberg4a3ee762013-03-15 17:07:12 -05003525 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
3526 hci_cc_write_page_scan_activity(hdev, skb);
3527 break;
3528
Johan Hedbergf332ec62013-03-15 17:07:11 -05003529 case HCI_OP_READ_PAGE_SCAN_TYPE:
3530 hci_cc_read_page_scan_type(hdev, skb);
3531 break;
3532
Johan Hedberg4a3ee762013-03-15 17:07:12 -05003533 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
3534 hci_cc_write_page_scan_type(hdev, skb);
3535 break;
3536
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02003537 case HCI_OP_READ_DATA_BLOCK_SIZE:
3538 hci_cc_read_data_block_size(hdev, skb);
3539 break;
3540
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02003541 case HCI_OP_READ_FLOW_CONTROL_MODE:
3542 hci_cc_read_flow_control_mode(hdev, skb);
3543 break;
3544
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03003545 case HCI_OP_READ_LOCAL_AMP_INFO:
3546 hci_cc_read_local_amp_info(hdev, skb);
3547 break;
3548
Johan Hedberg33f35722014-06-28 17:54:06 +03003549 case HCI_OP_READ_CLOCK:
3550 hci_cc_read_clock(hdev, skb);
3551 break;
3552
Johan Hedbergd5859e22011-01-25 01:19:58 +02003553 case HCI_OP_READ_INQ_RSP_TX_POWER:
3554 hci_cc_read_inq_rsp_tx_power(hdev, skb);
3555 break;
3556
Alain Michaud00bce3f2020-03-05 16:14:59 +00003557 case HCI_OP_READ_DEF_ERR_DATA_REPORTING:
3558 hci_cc_read_def_err_data_reporting(hdev, skb);
3559 break;
3560
3561 case HCI_OP_WRITE_DEF_ERR_DATA_REPORTING:
3562 hci_cc_write_def_err_data_reporting(hdev, skb);
3563 break;
3564
Johan Hedberg980e1a52011-01-22 06:10:07 +02003565 case HCI_OP_PIN_CODE_REPLY:
3566 hci_cc_pin_code_reply(hdev, skb);
3567 break;
3568
3569 case HCI_OP_PIN_CODE_NEG_REPLY:
3570 hci_cc_pin_code_neg_reply(hdev, skb);
3571 break;
3572
Szymon Jancc35938b2011-03-22 13:12:21 +01003573 case HCI_OP_READ_LOCAL_OOB_DATA:
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003574 hci_cc_read_local_oob_data(hdev, skb);
3575 break;
3576
3577 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
3578 hci_cc_read_local_oob_ext_data(hdev, skb);
Szymon Jancc35938b2011-03-22 13:12:21 +01003579 break;
3580
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003581 case HCI_OP_LE_READ_BUFFER_SIZE:
3582 hci_cc_le_read_buffer_size(hdev, skb);
3583 break;
3584
Johan Hedberg60e77322013-01-22 14:01:59 +02003585 case HCI_OP_LE_READ_LOCAL_FEATURES:
3586 hci_cc_le_read_local_features(hdev, skb);
3587 break;
3588
Johan Hedberg8fa19092012-10-19 20:57:49 +03003589 case HCI_OP_LE_READ_ADV_TX_POWER:
3590 hci_cc_le_read_adv_tx_power(hdev, skb);
3591 break;
3592
Johan Hedberga5c29682011-02-19 12:05:57 -03003593 case HCI_OP_USER_CONFIRM_REPLY:
3594 hci_cc_user_confirm_reply(hdev, skb);
3595 break;
3596
3597 case HCI_OP_USER_CONFIRM_NEG_REPLY:
3598 hci_cc_user_confirm_neg_reply(hdev, skb);
3599 break;
3600
Brian Gix1143d452011-11-23 08:28:34 -08003601 case HCI_OP_USER_PASSKEY_REPLY:
3602 hci_cc_user_passkey_reply(hdev, skb);
3603 break;
3604
3605 case HCI_OP_USER_PASSKEY_NEG_REPLY:
3606 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02003607 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09003608
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08003609 case HCI_OP_LE_SET_RANDOM_ADDR:
3610 hci_cc_le_set_random_addr(hdev, skb);
3611 break;
3612
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01003613 case HCI_OP_LE_SET_ADV_ENABLE:
3614 hci_cc_le_set_adv_enable(hdev, skb);
3615 break;
3616
Marcel Holtmann533553f2014-03-21 12:18:10 -07003617 case HCI_OP_LE_SET_SCAN_PARAM:
3618 hci_cc_le_set_scan_param(hdev, skb);
3619 break;
3620
Andre Guedeseb9d91f2011-05-26 16:23:52 -03003621 case HCI_OP_LE_SET_SCAN_ENABLE:
3622 hci_cc_le_set_scan_enable(hdev, skb);
3623 break;
3624
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003625 case HCI_OP_LE_READ_ACCEPT_LIST_SIZE:
3626 hci_cc_le_read_accept_list_size(hdev, skb);
Johan Hedbergcf1d0812013-01-22 14:02:00 +02003627 break;
3628
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003629 case HCI_OP_LE_CLEAR_ACCEPT_LIST:
3630 hci_cc_le_clear_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08003631 break;
3632
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003633 case HCI_OP_LE_ADD_TO_ACCEPT_LIST:
3634 hci_cc_le_add_to_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08003635 break;
3636
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003637 case HCI_OP_LE_DEL_FROM_ACCEPT_LIST:
3638 hci_cc_le_del_from_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08003639 break;
3640
Johan Hedberg9b008c02013-01-22 14:02:01 +02003641 case HCI_OP_LE_READ_SUPPORTED_STATES:
3642 hci_cc_le_read_supported_states(hdev, skb);
3643 break;
3644
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01003645 case HCI_OP_LE_READ_DEF_DATA_LEN:
3646 hci_cc_le_read_def_data_len(hdev, skb);
3647 break;
3648
3649 case HCI_OP_LE_WRITE_DEF_DATA_LEN:
3650 hci_cc_le_write_def_data_len(hdev, skb);
3651 break;
3652
Ankit Navikb950aa82018-08-17 07:29:19 +05303653 case HCI_OP_LE_ADD_TO_RESOLV_LIST:
3654 hci_cc_le_add_to_resolv_list(hdev, skb);
3655 break;
3656
3657 case HCI_OP_LE_DEL_FROM_RESOLV_LIST:
3658 hci_cc_le_del_from_resolv_list(hdev, skb);
3659 break;
3660
Ankit Navik545f2592018-06-29 12:13:20 +05303661 case HCI_OP_LE_CLEAR_RESOLV_LIST:
3662 hci_cc_le_clear_resolv_list(hdev, skb);
3663 break;
3664
Ankit Navikcfdb0c22018-06-29 12:12:50 +05303665 case HCI_OP_LE_READ_RESOLV_LIST_SIZE:
3666 hci_cc_le_read_resolv_list_size(hdev, skb);
3667 break;
3668
Ankit Navikaa12af72018-08-07 13:16:35 +05303669 case HCI_OP_LE_SET_ADDR_RESOLV_ENABLE:
3670 hci_cc_le_set_addr_resolution_enable(hdev, skb);
3671 break;
3672
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01003673 case HCI_OP_LE_READ_MAX_DATA_LEN:
3674 hci_cc_le_read_max_data_len(hdev, skb);
3675 break;
3676
Andre Guedesf9b49302011-06-30 19:20:53 -03003677 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
3678 hci_cc_write_le_host_supported(hdev, skb);
3679 break;
3680
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02003681 case HCI_OP_LE_SET_ADV_PARAM:
3682 hci_cc_set_adv_param(hdev, skb);
3683 break;
3684
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02003685 case HCI_OP_READ_RSSI:
3686 hci_cc_read_rssi(hdev, skb);
3687 break;
3688
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02003689 case HCI_OP_READ_TX_POWER:
3690 hci_cc_read_tx_power(hdev, skb);
3691 break;
3692
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08003693 case HCI_OP_WRITE_SSP_DEBUG_MODE:
3694 hci_cc_write_ssp_debug_mode(hdev, skb);
3695 break;
3696
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05303697 case HCI_OP_LE_SET_EXT_SCAN_PARAMS:
3698 hci_cc_le_set_ext_scan_param(hdev, skb);
3699 break;
3700
3701 case HCI_OP_LE_SET_EXT_SCAN_ENABLE:
3702 hci_cc_le_set_ext_scan_enable(hdev, skb);
3703 break;
3704
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303705 case HCI_OP_LE_SET_DEFAULT_PHY:
3706 hci_cc_le_set_default_phy(hdev, skb);
3707 break;
3708
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05303709 case HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS:
3710 hci_cc_le_read_num_adv_sets(hdev, skb);
3711 break;
3712
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05303713 case HCI_OP_LE_SET_EXT_ADV_PARAMS:
3714 hci_cc_set_ext_adv_param(hdev, skb);
3715 break;
3716
3717 case HCI_OP_LE_SET_EXT_ADV_ENABLE:
3718 hci_cc_le_set_ext_adv_enable(hdev, skb);
3719 break;
3720
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303721 case HCI_OP_LE_SET_ADV_SET_RAND_ADDR:
3722 hci_cc_le_set_adv_set_random_addr(hdev, skb);
3723 break;
3724
Daniel Winkler7c395ea2020-12-03 12:12:51 -08003725 case HCI_OP_LE_READ_TRANSMIT_POWER:
3726 hci_cc_le_read_transmit_power(hdev, skb);
3727 break;
3728
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003729 default:
Johan Hedberge62144872015-04-02 13:41:08 +03003730 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003731 break;
3732 }
3733
Kiran Kecb71f252021-08-16 05:07:47 +05303734 handle_cmd_cnt_and_timer(hdev, ev->ncmd);
Johan Hedberg600b2152015-03-28 11:17:36 +02003735
Johan Hedberge62144872015-04-02 13:41:08 +03003736 hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
3737 req_complete_skb);
Johan Hedberg9238f362013-03-05 20:37:48 +02003738
João Paulo Rechi Vitaf80c5da2019-05-02 10:01:52 +08003739 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
3740 bt_dev_err(hdev,
3741 "unexpected event for opcode 0x%4.4x", *opcode);
3742 return;
3743 }
3744
Johan Hedberg600b2152015-03-28 11:17:36 +02003745 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3746 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003747}
3748
Johan Hedberge62144872015-04-02 13:41:08 +03003749static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb,
3750 u16 *opcode, u8 *status,
3751 hci_req_complete_t *req_complete,
3752 hci_req_complete_skb_t *req_complete_skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003753{
3754 struct hci_ev_cmd_status *ev = (void *) skb->data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003755
3756 skb_pull(skb, sizeof(*ev));
3757
Johan Hedberge62144872015-04-02 13:41:08 +03003758 *opcode = __le16_to_cpu(ev->opcode);
3759 *status = ev->status;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003760
Johan Hedberge62144872015-04-02 13:41:08 +03003761 switch (*opcode) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003762 case HCI_OP_INQUIRY:
3763 hci_cs_inquiry(hdev, ev->status);
3764 break;
3765
3766 case HCI_OP_CREATE_CONN:
3767 hci_cs_create_conn(hdev, ev->status);
3768 break;
3769
Kuba Pawlak9645c762014-11-06 19:36:53 +01003770 case HCI_OP_DISCONNECT:
3771 hci_cs_disconnect(hdev, ev->status);
3772 break;
3773
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003774 case HCI_OP_ADD_SCO:
3775 hci_cs_add_sco(hdev, ev->status);
3776 break;
3777
Marcel Holtmannf8558552008-07-14 20:13:49 +02003778 case HCI_OP_AUTH_REQUESTED:
3779 hci_cs_auth_requested(hdev, ev->status);
3780 break;
3781
3782 case HCI_OP_SET_CONN_ENCRYPT:
3783 hci_cs_set_conn_encrypt(hdev, ev->status);
3784 break;
3785
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003786 case HCI_OP_REMOTE_NAME_REQ:
3787 hci_cs_remote_name_req(hdev, ev->status);
3788 break;
3789
Marcel Holtmann769be972008-07-14 20:13:49 +02003790 case HCI_OP_READ_REMOTE_FEATURES:
3791 hci_cs_read_remote_features(hdev, ev->status);
3792 break;
3793
3794 case HCI_OP_READ_REMOTE_EXT_FEATURES:
3795 hci_cs_read_remote_ext_features(hdev, ev->status);
3796 break;
3797
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003798 case HCI_OP_SETUP_SYNC_CONN:
3799 hci_cs_setup_sync_conn(hdev, ev->status);
3800 break;
3801
Kiran Kb2af2642021-09-07 15:42:43 +05303802 case HCI_OP_ENHANCED_SETUP_SYNC_CONN:
3803 hci_cs_enhanced_setup_sync_conn(hdev, ev->status);
3804 break;
3805
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003806 case HCI_OP_SNIFF_MODE:
3807 hci_cs_sniff_mode(hdev, ev->status);
3808 break;
3809
3810 case HCI_OP_EXIT_SNIFF_MODE:
3811 hci_cs_exit_sniff_mode(hdev, ev->status);
3812 break;
3813
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01003814 case HCI_OP_SWITCH_ROLE:
3815 hci_cs_switch_role(hdev, ev->status);
3816 break;
3817
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02003818 case HCI_OP_LE_CREATE_CONN:
3819 hci_cs_le_create_conn(hdev, ev->status);
3820 break;
3821
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07003822 case HCI_OP_LE_READ_REMOTE_FEATURES:
3823 hci_cs_le_read_remote_features(hdev, ev->status);
3824 break;
3825
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02003826 case HCI_OP_LE_START_ENC:
3827 hci_cs_le_start_enc(hdev, ev->status);
3828 break;
3829
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02003830 case HCI_OP_LE_EXT_CREATE_CONN:
3831 hci_cs_le_ext_create_conn(hdev, ev->status);
3832 break;
3833
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003834 default:
Johan Hedberge62144872015-04-02 13:41:08 +03003835 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003836 break;
3837 }
3838
Kiran Kecb71f252021-08-16 05:07:47 +05303839 handle_cmd_cnt_and_timer(hdev, ev->ncmd);
Johan Hedberg600b2152015-03-28 11:17:36 +02003840
Johan Hedberg444c6dd2015-04-02 13:41:07 +03003841 /* Indicate request completion if the command failed. Also, if
3842 * we're not waiting for a special event and we get a success
3843 * command status we should try to flag the request as completed
3844 * (since for this kind of commands there will not be a command
3845 * complete event).
3846 */
Johan Hedberg02350a72013-04-03 21:50:29 +03003847 if (ev->status ||
Marcel Holtmann242c0eb2015-10-25 22:45:53 +01003848 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->hci.req_event))
Johan Hedberge62144872015-04-02 13:41:08 +03003849 hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
3850 req_complete_skb);
Johan Hedberg9238f362013-03-05 20:37:48 +02003851
João Paulo Rechi Vitaf80c5da2019-05-02 10:01:52 +08003852 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
3853 bt_dev_err(hdev,
3854 "unexpected event for opcode 0x%4.4x", *opcode);
3855 return;
3856 }
3857
Johan Hedberg600b2152015-03-28 11:17:36 +02003858 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3859 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003860}
3861
Marcel Holtmann24dfa342014-11-02 02:56:41 +01003862static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
3863{
3864 struct hci_ev_hardware_error *ev = (void *) skb->data;
3865
Marcel Holtmannc7741d12015-01-28 11:09:55 -08003866 hdev->hw_error_code = ev->code;
3867
3868 queue_work(hdev->req_workqueue, &hdev->error_reset);
Marcel Holtmann24dfa342014-11-02 02:56:41 +01003869}
3870
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003871static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003872{
3873 struct hci_ev_role_change *ev = (void *) skb->data;
3874 struct hci_conn *conn;
3875
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003876 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003877
3878 hci_dev_lock(hdev);
3879
3880 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3881 if (conn) {
Johan Hedberg40bef302014-07-16 11:42:27 +03003882 if (!ev->status)
3883 conn->role = ev->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003884
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003885 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003886
3887 hci_role_switch_cfm(conn, ev->status, ev->role);
3888 }
3889
3890 hci_dev_unlock(hdev);
3891}
3892
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003893static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003895 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 int i;
3897
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02003898 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003899 bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02003900 return;
3901 }
3902
Gustavo A. R. Silva16e18342019-02-07 18:40:33 -06003903 if (skb->len < sizeof(*ev) ||
3904 skb->len < struct_size(ev, handles, ev->num_hndl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 BT_DBG("%s bad parameters", hdev->name);
3906 return;
3907 }
3908
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02003909 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
3910
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003911 for (i = 0; i < ev->num_hndl; i++) {
3912 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 struct hci_conn *conn;
3914 __u16 handle, count;
3915
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003916 handle = __le16_to_cpu(info->handle);
3917 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918
3919 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003920 if (!conn)
3921 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003923 conn->sent -= count;
3924
3925 switch (conn->type) {
3926 case ACL_LINK:
3927 hdev->acl_cnt += count;
3928 if (hdev->acl_cnt > hdev->acl_pkts)
3929 hdev->acl_cnt = hdev->acl_pkts;
3930 break;
3931
3932 case LE_LINK:
3933 if (hdev->le_pkts) {
3934 hdev->le_cnt += count;
3935 if (hdev->le_cnt > hdev->le_pkts)
3936 hdev->le_cnt = hdev->le_pkts;
3937 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02003938 hdev->acl_cnt += count;
3939 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 hdev->acl_cnt = hdev->acl_pkts;
3941 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003942 break;
3943
3944 case SCO_LINK:
3945 hdev->sco_cnt += count;
3946 if (hdev->sco_cnt > hdev->sco_pkts)
3947 hdev->sco_cnt = hdev->sco_pkts;
3948 break;
3949
3950 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003951 bt_dev_err(hdev, "unknown type %d conn %p",
3952 conn->type, conn);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003953 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 }
3955 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003956
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02003957 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958}
3959
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003960static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
3961 __u16 handle)
3962{
3963 struct hci_chan *chan;
3964
3965 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02003966 case HCI_PRIMARY:
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003967 return hci_conn_hash_lookup_handle(hdev, handle);
3968 case HCI_AMP:
3969 chan = hci_chan_lookup_handle(hdev, handle);
3970 if (chan)
3971 return chan->conn;
3972 break;
3973 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003974 bt_dev_err(hdev, "unknown dev_type %d", hdev->dev_type);
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003975 break;
3976 }
3977
3978 return NULL;
3979}
3980
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003981static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003982{
3983 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
3984 int i;
3985
3986 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003987 bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003988 return;
3989 }
3990
Gustavo A. R. Silva16e18342019-02-07 18:40:33 -06003991 if (skb->len < sizeof(*ev) ||
3992 skb->len < struct_size(ev, handles, ev->num_hndl)) {
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003993 BT_DBG("%s bad parameters", hdev->name);
3994 return;
3995 }
3996
3997 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003998 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003999
4000 for (i = 0; i < ev->num_hndl; i++) {
4001 struct hci_comp_blocks_info *info = &ev->handles[i];
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03004002 struct hci_conn *conn = NULL;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004003 __u16 handle, block_count;
4004
4005 handle = __le16_to_cpu(info->handle);
4006 block_count = __le16_to_cpu(info->blocks);
4007
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03004008 conn = __hci_conn_lookup_handle(hdev, handle);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004009 if (!conn)
4010 continue;
4011
4012 conn->sent -= block_count;
4013
4014 switch (conn->type) {
4015 case ACL_LINK:
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03004016 case AMP_LINK:
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004017 hdev->block_cnt += block_count;
4018 if (hdev->block_cnt > hdev->num_blocks)
4019 hdev->block_cnt = hdev->num_blocks;
4020 break;
4021
4022 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004023 bt_dev_err(hdev, "unknown type %d conn %p",
4024 conn->type, conn);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004025 break;
4026 }
4027 }
4028
4029 queue_work(hdev->workqueue, &hdev->tx_work);
4030}
4031
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004032static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004034 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004035 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004037 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
4039 hci_dev_lock(hdev);
4040
Marcel Holtmann04837f62006-07-03 10:02:33 +02004041 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4042 if (conn) {
4043 conn->mode = ev->mode;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004044
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03004045 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
4046 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02004047 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02004048 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004049 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02004050 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004051 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04004052
Johan Hedberg51a8efd2012-01-16 06:10:31 +02004053 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04004054 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004055 }
4056
4057 hci_dev_unlock(hdev);
4058}
4059
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004060static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004062 struct hci_ev_pin_code_req *ev = (void *) skb->data;
4063 struct hci_conn *conn;
4064
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004065 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004066
4067 hci_dev_lock(hdev);
4068
4069 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02004070 if (!conn)
4071 goto unlock;
4072
4073 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004074 hci_conn_hold(conn);
4075 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004076 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004077 }
4078
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004079 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedberg2f407f02014-07-17 15:35:40 +03004080 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
Johan Hedberg03b555e2011-01-04 15:40:05 +02004081 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004082 sizeof(ev->bdaddr), &ev->bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004083 } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02004084 u8 secure;
4085
4086 if (conn->pending_sec_level == BT_SECURITY_HIGH)
4087 secure = 1;
4088 else
4089 secure = 0;
4090
Johan Hedberg744cf192011-11-08 20:40:14 +02004091 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02004092 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02004093
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02004094unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004095 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096}
4097
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004098static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
4099{
4100 if (key_type == HCI_LK_CHANGED_COMBINATION)
4101 return;
4102
4103 conn->pin_length = pin_len;
4104 conn->key_type = key_type;
4105
4106 switch (key_type) {
4107 case HCI_LK_LOCAL_UNIT:
4108 case HCI_LK_REMOTE_UNIT:
4109 case HCI_LK_DEBUG_COMBINATION:
4110 return;
4111 case HCI_LK_COMBINATION:
4112 if (pin_len == 16)
4113 conn->pending_sec_level = BT_SECURITY_HIGH;
4114 else
4115 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4116 break;
4117 case HCI_LK_UNAUTH_COMBINATION_P192:
4118 case HCI_LK_UNAUTH_COMBINATION_P256:
4119 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4120 break;
4121 case HCI_LK_AUTH_COMBINATION_P192:
4122 conn->pending_sec_level = BT_SECURITY_HIGH;
4123 break;
4124 case HCI_LK_AUTH_COMBINATION_P256:
4125 conn->pending_sec_level = BT_SECURITY_FIPS;
4126 break;
4127 }
4128}
4129
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004130static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004132 struct hci_ev_link_key_req *ev = (void *) skb->data;
4133 struct hci_cp_link_key_reply cp;
4134 struct hci_conn *conn;
4135 struct link_key *key;
4136
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004137 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004138
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004139 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004140 return;
4141
4142 hci_dev_lock(hdev);
4143
4144 key = hci_find_link_key(hdev, &ev->bdaddr);
4145 if (!key) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03004146 BT_DBG("%s link key not found for %pMR", hdev->name,
4147 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004148 goto not_found;
4149 }
4150
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03004151 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
4152 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004153
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004154 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004155 if (conn) {
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03004156 clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
4157
Marcel Holtmann66138ce2014-01-10 02:07:20 -08004158 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
4159 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03004160 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004161 BT_DBG("%s ignoring unauthenticated key", hdev->name);
4162 goto not_found;
4163 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004164
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004165 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Johan Hedbergf3fb0b52014-06-02 10:12:44 +03004166 (conn->pending_sec_level == BT_SECURITY_HIGH ||
4167 conn->pending_sec_level == BT_SECURITY_FIPS)) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03004168 BT_DBG("%s ignoring key unauthenticated for high security",
4169 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004170 goto not_found;
4171 }
4172
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004173 conn_set_key(conn, key->type, key->pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004174 }
4175
4176 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03004177 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004178
4179 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
4180
4181 hci_dev_unlock(hdev);
4182
4183 return;
4184
4185not_found:
4186 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
4187 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188}
4189
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004190static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004192 struct hci_ev_link_key_notify *ev = (void *) skb->data;
4193 struct hci_conn *conn;
Johan Hedberg7652ff62014-06-24 13:15:49 +03004194 struct link_key *key;
4195 bool persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004196 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004197
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004198 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004199
4200 hci_dev_lock(hdev);
4201
4202 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004203 if (!conn)
4204 goto unlock;
4205
4206 hci_conn_hold(conn);
4207 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
4208 hci_conn_drop(conn);
4209
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03004210 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004211 conn_set_key(conn, ev->key_type, conn->pin_length);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004212
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004213 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg7652ff62014-06-24 13:15:49 +03004214 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004215
Johan Hedberg7652ff62014-06-24 13:15:49 +03004216 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
4217 ev->key_type, pin_len, &persistent);
4218 if (!key)
4219 goto unlock;
4220
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004221 /* Update connection information since adding the key will have
4222 * fixed up the type in the case of changed combination keys.
4223 */
4224 if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
4225 conn_set_key(conn, key->type, key->pin_len);
4226
Johan Hedberg7652ff62014-06-24 13:15:49 +03004227 mgmt_new_link_key(hdev, key, persistent);
4228
Johan Hedberg6d5650c2014-06-24 13:15:51 +03004229 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
4230 * is set. If it's not set simply remove the key from the kernel
4231 * list (we've still notified user space about it but with
4232 * store_hint being 0).
4233 */
4234 if (key->type == HCI_LK_DEBUG_COMBINATION &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004235 !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg0378b592014-11-19 15:22:22 +02004236 list_del_rcu(&key->list);
4237 kfree_rcu(key, rcu);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004238 goto unlock;
Johan Hedberg6d5650c2014-06-24 13:15:51 +03004239 }
Johan Hedberg7652ff62014-06-24 13:15:49 +03004240
Johan Hedberg82c13d42014-12-03 11:03:06 +02004241 if (persistent)
4242 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4243 else
4244 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4245
Johan Hedberg7652ff62014-06-24 13:15:49 +03004246unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004247 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248}
4249
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004250static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02004251{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004252 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004253 struct hci_conn *conn;
4254
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004255 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004256
4257 hci_dev_lock(hdev);
4258
4259 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 if (conn && !ev->status) {
4261 struct inquiry_entry *ie;
4262
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004263 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4264 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 ie->data.clock_offset = ev->clock_offset;
4266 ie->timestamp = jiffies;
4267 }
4268 }
4269
4270 hci_dev_unlock(hdev);
4271}
4272
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004273static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02004274{
4275 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
4276 struct hci_conn *conn;
4277
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004278 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02004279
4280 hci_dev_lock(hdev);
4281
4282 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4283 if (conn && !ev->status)
4284 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
4285
4286 hci_dev_unlock(hdev);
4287}
4288
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004289static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004290{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004291 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004292 struct inquiry_entry *ie;
4293
4294 BT_DBG("%s", hdev->name);
4295
4296 hci_dev_lock(hdev);
4297
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004298 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4299 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004300 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
4301 ie->timestamp = jiffies;
4302 }
4303
4304 hci_dev_unlock(hdev);
4305}
4306
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004307static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
4308 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004309{
4310 struct inquiry_data data;
4311 int num_rsp = *((__u8 *) skb->data);
4312
4313 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
4314
4315 if (!num_rsp)
4316 return;
4317
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004318 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03004319 return;
4320
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004321 hci_dev_lock(hdev);
4322
4323 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01004324 struct inquiry_info_with_rssi_and_pscan_mode *info;
4325 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004326
Peilin Ye629b49c2020-07-10 17:45:26 -04004327 if (skb->len < num_rsp * sizeof(*info) + 1)
4328 goto unlock;
4329
Johan Hedberge17acd42011-03-30 23:57:16 +03004330 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004331 u32 flags;
4332
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004333 bacpy(&data.bdaddr, &info->bdaddr);
4334 data.pscan_rep_mode = info->pscan_rep_mode;
4335 data.pscan_period_mode = info->pscan_period_mode;
4336 data.pscan_mode = info->pscan_mode;
4337 memcpy(data.dev_class, info->dev_class, 3);
4338 data.clock_offset = info->clock_offset;
4339 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004340 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02004341
Marcel Holtmannaf589252014-07-01 14:11:20 +02004342 flags = hci_inquiry_cache_update(hdev, &data, false);
4343
Johan Hedberg48264f02011-11-09 13:58:58 +02004344 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004345 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004346 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004347 }
4348 } else {
4349 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
4350
Peilin Ye629b49c2020-07-10 17:45:26 -04004351 if (skb->len < num_rsp * sizeof(*info) + 1)
4352 goto unlock;
4353
Johan Hedberge17acd42011-03-30 23:57:16 +03004354 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004355 u32 flags;
4356
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004357 bacpy(&data.bdaddr, &info->bdaddr);
4358 data.pscan_rep_mode = info->pscan_rep_mode;
4359 data.pscan_period_mode = info->pscan_period_mode;
4360 data.pscan_mode = 0x00;
4361 memcpy(data.dev_class, info->dev_class, 3);
4362 data.clock_offset = info->clock_offset;
4363 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004364 data.ssp_mode = 0x00;
Marcel Holtmannaf589252014-07-01 14:11:20 +02004365
4366 flags = hci_inquiry_cache_update(hdev, &data, false);
4367
Johan Hedberg48264f02011-11-09 13:58:58 +02004368 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004369 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004370 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004371 }
4372 }
4373
Peilin Ye629b49c2020-07-10 17:45:26 -04004374unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004375 hci_dev_unlock(hdev);
4376}
4377
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004378static void hci_remote_ext_features_evt(struct hci_dev *hdev,
4379 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004380{
Marcel Holtmann41a96212008-07-14 20:13:48 +02004381 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
4382 struct hci_conn *conn;
4383
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004384 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004385
Marcel Holtmann41a96212008-07-14 20:13:48 +02004386 hci_dev_lock(hdev);
4387
4388 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02004389 if (!conn)
4390 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004391
Johan Hedbergcad718e2013-04-17 15:00:51 +03004392 if (ev->page < HCI_MAX_PAGES)
4393 memcpy(conn->features[ev->page], ev->features, 8);
4394
Johan Hedbergccd556f2010-11-10 17:11:51 +02004395 if (!ev->status && ev->page == 0x01) {
4396 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004397
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004398 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4399 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02004400 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02004401
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05304402 if (ev->features[0] & LMP_HOST_SSP) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02004403 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05304404 } else {
4405 /* It is mandatory by the Bluetooth specification that
4406 * Extended Inquiry Results are only used when Secure
4407 * Simple Pairing is enabled, but some devices violate
4408 * this.
4409 *
4410 * To make these devices work, the internal SSP
4411 * enabled flag needs to be cleared if the remote host
4412 * features do not indicate SSP support */
4413 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
4414 }
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -08004415
4416 if (ev->features[0] & LMP_HOST_SC)
4417 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004418 }
4419
Johan Hedbergccd556f2010-11-10 17:11:51 +02004420 if (conn->state != BT_CONFIG)
4421 goto unlock;
4422
Johan Hedberg671267b2012-05-12 16:11:50 -03004423 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02004424 struct hci_cp_remote_name_req cp;
4425 memset(&cp, 0, sizeof(cp));
4426 bacpy(&cp.bdaddr, &conn->dst);
4427 cp.pscan_rep_mode = 0x02;
4428 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02004429 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07004430 mgmt_device_connected(hdev, conn, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02004431
Johan Hedberg127178d2010-11-18 22:22:29 +02004432 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02004433 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02004434 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02004435 hci_conn_drop(conn);
Johan Hedbergccd556f2010-11-10 17:11:51 +02004436 }
4437
4438unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02004439 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004440}
4441
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004442static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
4443 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004444{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004445 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
4446 struct hci_conn *conn;
Chethan T Nf4f9fa02021-09-07 15:42:48 +05304447 unsigned int notify_evt;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004448
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004449 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004450
4451 hci_dev_lock(hdev);
4452
4453 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004454 if (!conn) {
4455 if (ev->link_type == ESCO_LINK)
4456 goto unlock;
4457
Kuba Pawlak618353b2015-08-28 13:05:22 +01004458 /* When the link type in the event indicates SCO connection
4459 * and lookup of the connection object fails, then check
4460 * if an eSCO connection object exists.
4461 *
4462 * The core limits the synchronous connections to either
4463 * SCO or eSCO. The eSCO connection is preferred and tried
4464 * to be setup first and until successfully established,
4465 * the link type will be hinted as eSCO.
4466 */
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004467 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
4468 if (!conn)
4469 goto unlock;
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004470 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004471
Marcel Holtmann732547f2009-04-19 19:14:14 +02004472 switch (ev->status) {
4473 case 0x00:
Desmond Cheong Zhi Xi92fe24a2021-07-28 15:51:04 +08004474 /* The synchronous connection complete event should only be
4475 * sent once per new connection. Receiving a successful
4476 * complete event when the connection status is already
4477 * BT_CONNECTED means that the device is misbehaving and sent
4478 * multiple complete event packets for the same new connection.
4479 *
4480 * Registering the device more than once can corrupt kernel
4481 * memory, hence upon detecting this invalid event, we report
4482 * an error and ignore the packet.
4483 */
4484 if (conn->state == BT_CONNECTED) {
4485 bt_dev_err(hdev, "Ignoring connect complete event for existing connection");
4486 goto unlock;
4487 }
4488
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004489 conn->handle = __le16_to_cpu(ev->handle);
4490 conn->state = BT_CONNECTED;
Kuba Pawlak618353b2015-08-28 13:05:22 +01004491 conn->type = ev->link_type;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02004492
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01004493 hci_debugfs_create_conn(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02004494 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02004495 break;
4496
Nick Pelly81218d22014-06-30 11:25:01 +05304497 case 0x10: /* Connection Accept Timeout */
Frédéric Dalleau1a4c9582013-08-19 14:24:02 +02004498 case 0x0d: /* Connection Rejected due to Limited Resources */
Stephen Coe705e5712010-02-16 11:29:44 -05004499 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02004500 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08004501 case 0x1a: /* Unsupported Remote Feature */
Hsin-Yu Chao56b54532020-05-15 17:27:04 +08004502 case 0x1e: /* Invalid LMP Parameters */
Marcel Holtmann732547f2009-04-19 19:14:14 +02004503 case 0x1f: /* Unspecified error */
Andrew Earl27539bc2014-03-10 10:31:04 +00004504 case 0x20: /* Unsupported LMP Parameter value */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02004505 if (conn->out) {
Marcel Holtmann732547f2009-04-19 19:14:14 +02004506 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
4507 (hdev->esco_type & EDR_ESCO_MASK);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02004508 if (hci_setup_sync(conn, conn->link->handle))
4509 goto unlock;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004510 }
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05004511 fallthrough;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004512
4513 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004514 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004515 break;
4516 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004517
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02004518 bt_dev_dbg(hdev, "SCO connected with air mode: %02x", ev->air_mode);
4519
Kiran K06d213d82021-04-08 22:31:59 +05304520 switch (ev->air_mode) {
4521 case 0x02:
Chethan T Nf4f9fa02021-09-07 15:42:48 +05304522 notify_evt = HCI_NOTIFY_ENABLE_SCO_CVSD;
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02004523 break;
Kiran K06d213d82021-04-08 22:31:59 +05304524 case 0x03:
Chethan T Nf4f9fa02021-09-07 15:42:48 +05304525 notify_evt = HCI_NOTIFY_ENABLE_SCO_TRANSP;
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02004526 break;
4527 }
4528
Chethan T Nf4f9fa02021-09-07 15:42:48 +05304529 /* Notify only in case of SCO over HCI transport data path which
4530 * is zero and non-zero value shall be non-HCI transport data path
4531 */
4532 if (conn->codec.data_path == 0) {
4533 if (hdev->notify)
4534 hdev->notify(hdev, notify_evt);
4535 }
4536
Johan Hedberg539c4962015-02-18 14:53:57 +02004537 hci_connect_cfm(conn, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004538 if (ev->status)
4539 hci_conn_del(conn);
4540
4541unlock:
4542 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004543}
4544
Marcel Holtmannefdcf8e2013-10-15 10:31:12 -07004545static inline size_t eir_get_length(u8 *eir, size_t eir_len)
4546{
4547 size_t parsed = 0;
4548
4549 while (parsed < eir_len) {
4550 u8 field_len = eir[0];
4551
4552 if (field_len == 0)
4553 return parsed;
4554
4555 parsed += field_len + 1;
4556 eir += field_len + 1;
4557 }
4558
4559 return eir_len;
4560}
4561
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004562static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
4563 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004564{
4565 struct inquiry_data data;
4566 struct extended_inquiry_info *info = (void *) (skb->data + 1);
4567 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05304568 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004569
4570 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
4571
Peilin Ye51c19bf2020-07-10 12:09:15 -04004572 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004573 return;
4574
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004575 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03004576 return;
4577
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004578 hci_dev_lock(hdev);
4579
Johan Hedberge17acd42011-03-30 23:57:16 +03004580 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004581 u32 flags;
4582 bool name_known;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004583
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004584 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01004585 data.pscan_rep_mode = info->pscan_rep_mode;
4586 data.pscan_period_mode = info->pscan_period_mode;
4587 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004588 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01004589 data.clock_offset = info->clock_offset;
4590 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004591 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004592
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004593 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg0d3b7f62016-01-05 13:19:31 +02004594 name_known = eir_get_data(info->data,
4595 sizeof(info->data),
4596 EIR_NAME_COMPLETE, NULL);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004597 else
4598 name_known = true;
4599
Marcel Holtmannaf589252014-07-01 14:11:20 +02004600 flags = hci_inquiry_cache_update(hdev, &data, name_known);
4601
Vishal Agarwal9d939d92012-04-26 19:19:56 +05304602 eir_len = eir_get_length(info->data, sizeof(info->data));
Marcel Holtmannaf589252014-07-01 14:11:20 +02004603
Johan Hedberg48264f02011-11-09 13:58:58 +02004604 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004605 info->dev_class, info->rssi,
4606 flags, info->data, eir_len, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004607 }
4608
4609 hci_dev_unlock(hdev);
4610}
4611
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004612static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
4613 struct sk_buff *skb)
4614{
4615 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
4616 struct hci_conn *conn;
4617
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004618 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004619 __le16_to_cpu(ev->handle));
4620
4621 hci_dev_lock(hdev);
4622
4623 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4624 if (!conn)
4625 goto unlock;
4626
Johan Hedberg9eb1fbf2014-04-11 12:02:31 -07004627 /* For BR/EDR the necessary steps are taken through the
4628 * auth_complete event.
4629 */
4630 if (conn->type != LE_LINK)
4631 goto unlock;
4632
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004633 if (!ev->status)
4634 conn->sec_level = conn->pending_sec_level;
4635
4636 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
4637
4638 if (ev->status && conn->state == BT_CONNECTED) {
Andre Guedesbed71742013-01-30 11:50:56 -03004639 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
David Herrmann76a68ba2013-04-06 20:28:37 +02004640 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004641 goto unlock;
4642 }
4643
4644 if (conn->state == BT_CONFIG) {
4645 if (!ev->status)
4646 conn->state = BT_CONNECTED;
4647
Johan Hedberg539c4962015-02-18 14:53:57 +02004648 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02004649 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004650 } else {
4651 hci_auth_cfm(conn, ev->status);
4652
4653 hci_conn_hold(conn);
4654 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004655 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004656 }
4657
4658unlock:
4659 hci_dev_unlock(hdev);
4660}
4661
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004662static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004663{
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004664 /* If remote requests no-bonding follow that lead */
Mikel Astizacabae92013-06-28 10:56:28 +02004665 if (conn->remote_auth == HCI_AT_NO_BONDING ||
4666 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02004667 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004668
Mikel Astizb7f94c82014-04-08 14:21:31 +02004669 /* If both remote and local have enough IO capabilities, require
4670 * MITM protection
4671 */
4672 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
4673 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
4674 return conn->remote_auth | 0x01;
4675
Timo Mueller7e741702014-04-08 14:21:33 +02004676 /* No MITM protection possible so ignore remote requirement */
4677 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004678}
4679
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004680static u8 bredr_oob_data_present(struct hci_conn *conn)
4681{
4682 struct hci_dev *hdev = conn->hdev;
4683 struct oob_data *data;
4684
4685 data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
4686 if (!data)
4687 return 0x00;
4688
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004689 if (bredr_sc_enabled(hdev)) {
4690 /* When Secure Connections is enabled, then just
4691 * return the present value stored with the OOB
4692 * data. The stored value contains the right present
4693 * information. However it can only be trusted when
4694 * not in Secure Connection Only mode.
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004695 */
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004696 if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
4697 return data->present;
4698
4699 /* When Secure Connections Only mode is enabled, then
4700 * the P-256 values are required. If they are not
4701 * available, then do not declare that OOB data is
4702 * present.
4703 */
4704 if (!memcmp(data->rand256, ZERO_KEY, 16) ||
4705 !memcmp(data->hash256, ZERO_KEY, 16))
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004706 return 0x00;
4707
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004708 return 0x02;
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004709 }
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004710
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004711 /* When Secure Connections is not enabled or actually
4712 * not supported by the hardware, then check that if
4713 * P-192 data values are present.
4714 */
4715 if (!memcmp(data->rand192, ZERO_KEY, 16) ||
4716 !memcmp(data->hash192, ZERO_KEY, 16))
4717 return 0x00;
4718
4719 return 0x01;
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004720}
4721
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004722static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02004723{
4724 struct hci_ev_io_capa_request *ev = (void *) skb->data;
4725 struct hci_conn *conn;
4726
4727 BT_DBG("%s", hdev->name);
4728
4729 hci_dev_lock(hdev);
4730
4731 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004732 if (!conn)
4733 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02004734
Johan Hedberg03b555e2011-01-04 15:40:05 +02004735 hci_conn_hold(conn);
4736
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004737 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg03b555e2011-01-04 15:40:05 +02004738 goto unlock;
4739
Johan Hedberg2f407f02014-07-17 15:35:40 +03004740 /* Allow pairing if we're pairable, the initiators of the
4741 * pairing or if the remote is not requesting bonding.
4742 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004743 if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
Johan Hedberg2f407f02014-07-17 15:35:40 +03004744 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03004745 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004746 struct hci_cp_io_capability_reply cp;
4747
4748 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05304749 /* Change the IO capability from KeyboardDisplay
4750 * to DisplayYesNo as it is not supported by BT spec. */
4751 cp.capability = (conn->io_capability == 0x04) ?
Mikel Astiza7676312013-06-28 10:56:29 +02004752 HCI_IO_DISPLAY_YESNO : conn->io_capability;
Mikel Astizb7f94c82014-04-08 14:21:31 +02004753
4754 /* If we are initiators, there is no remote information yet */
4755 if (conn->remote_auth == 0xff) {
Mikel Astizb16c6602014-04-08 14:21:34 +02004756 /* Request MITM protection if our IO caps allow it
Johan Hedberg4ad51a72014-06-09 14:41:25 +03004757 * except for the no-bonding case.
Mikel Astizb16c6602014-04-08 14:21:34 +02004758 */
Mikel Astiz6fd6b912014-04-08 14:21:32 +02004759 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedberg9f743d72014-07-17 11:56:33 +03004760 conn->auth_type != HCI_AT_NO_BONDING)
Johan Hedberg6c538232014-07-11 15:32:23 +03004761 conn->auth_type |= 0x01;
Mikel Astizb7f94c82014-04-08 14:21:31 +02004762 } else {
4763 conn->auth_type = hci_get_auth_req(conn);
Mikel Astizb7f94c82014-04-08 14:21:31 +02004764 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004765
Johan Hedberg82c295b2014-07-30 09:22:24 +03004766 /* If we're not bondable, force one of the non-bondable
4767 * authentication requirement values.
4768 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004769 if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedberg82c295b2014-07-30 09:22:24 +03004770 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
4771
4772 cp.authentication = conn->auth_type;
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004773 cp.oob_data = bredr_oob_data_present(conn);
Szymon Jancce85ee12011-03-22 13:12:23 +01004774
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004775 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004776 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004777 } else {
4778 struct hci_cp_io_capability_neg_reply cp;
4779
4780 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02004781 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02004782
4783 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004784 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004785 }
4786
4787unlock:
4788 hci_dev_unlock(hdev);
4789}
4790
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004791static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02004792{
4793 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
4794 struct hci_conn *conn;
4795
4796 BT_DBG("%s", hdev->name);
4797
4798 hci_dev_lock(hdev);
4799
4800 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4801 if (!conn)
4802 goto unlock;
4803
Johan Hedberg03b555e2011-01-04 15:40:05 +02004804 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02004805 conn->remote_auth = ev->authentication;
4806
4807unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02004808 hci_dev_unlock(hdev);
4809}
4810
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004811static void hci_user_confirm_request_evt(struct hci_dev *hdev,
4812 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03004813{
4814 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004815 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07004816 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03004817
4818 BT_DBG("%s", hdev->name);
4819
4820 hci_dev_lock(hdev);
4821
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004822 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg7a828902011-04-28 11:28:53 -07004823 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03004824
Johan Hedberg7a828902011-04-28 11:28:53 -07004825 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4826 if (!conn)
4827 goto unlock;
4828
4829 loc_mitm = (conn->auth_type & 0x01);
4830 rem_mitm = (conn->remote_auth & 0x01);
4831
4832 /* If we require MITM but the remote device can't provide that
Johan Hedberg6c538232014-07-11 15:32:23 +03004833 * (it has NoInputNoOutput) then reject the confirmation
4834 * request. We check the security level here since it doesn't
4835 * necessarily match conn->auth_type.
Mikel Astiz6fd6b912014-04-08 14:21:32 +02004836 */
Johan Hedberg6c538232014-07-11 15:32:23 +03004837 if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
4838 conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
Johan Hedberg7a828902011-04-28 11:28:53 -07004839 BT_DBG("Rejecting request: remote device can't provide MITM");
4840 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004841 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07004842 goto unlock;
4843 }
4844
4845 /* If no side requires MITM protection; auto-accept */
Mikel Astiza7676312013-06-28 10:56:29 +02004846 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
4847 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004848
4849 /* If we're not the initiators request authorization to
4850 * proceed from user space (mgmt_user_confirm with
Johan Hedbergba15a582014-06-09 13:58:14 +03004851 * confirm_hint set to 1). The exception is if neither
Johan Hedberg02f3e252014-07-16 15:09:13 +03004852 * side had MITM or if the local IO capability is
4853 * NoInputNoOutput, in which case we do auto-accept
Johan Hedbergba15a582014-06-09 13:58:14 +03004854 */
4855 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
Johan Hedberg02f3e252014-07-16 15:09:13 +03004856 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedbergba15a582014-06-09 13:58:14 +03004857 (loc_mitm || rem_mitm)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004858 BT_DBG("Confirming auto-accept as acceptor");
4859 confirm_hint = 1;
4860 goto confirm;
4861 }
4862
Howard Chungcee5f202020-02-14 19:16:41 +08004863 /* If there already exists link key in local host, leave the
4864 * decision to user space since the remote device could be
4865 * legitimate or malicious.
4866 */
4867 if (hci_find_link_key(hdev, &ev->bdaddr)) {
4868 bt_dev_dbg(hdev, "Local host already has link key");
4869 confirm_hint = 1;
4870 goto confirm;
4871 }
4872
Johan Hedberg9f616562011-04-28 11:28:54 -07004873 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03004874 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07004875
4876 if (hdev->auto_accept_delay > 0) {
4877 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03004878 queue_delayed_work(conn->hdev->workqueue,
4879 &conn->auto_accept_work, delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07004880 goto unlock;
4881 }
4882
Johan Hedberg7a828902011-04-28 11:28:53 -07004883 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004884 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07004885 goto unlock;
4886 }
4887
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004888confirm:
Johan Hedberg39adbff2014-03-20 08:18:14 +02004889 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
4890 le32_to_cpu(ev->passkey), confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07004891
4892unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03004893 hci_dev_unlock(hdev);
4894}
4895
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004896static void hci_user_passkey_request_evt(struct hci_dev *hdev,
4897 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08004898{
4899 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
4900
4901 BT_DBG("%s", hdev->name);
4902
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004903 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg272d90d2012-02-09 15:26:12 +02004904 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08004905}
4906
Johan Hedberg92a25252012-09-06 18:39:26 +03004907static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
4908 struct sk_buff *skb)
4909{
4910 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
4911 struct hci_conn *conn;
4912
4913 BT_DBG("%s", hdev->name);
4914
4915 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4916 if (!conn)
4917 return;
4918
4919 conn->passkey_notify = __le32_to_cpu(ev->passkey);
4920 conn->passkey_entered = 0;
4921
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004922 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg92a25252012-09-06 18:39:26 +03004923 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4924 conn->dst_type, conn->passkey_notify,
4925 conn->passkey_entered);
4926}
4927
4928static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
4929{
4930 struct hci_ev_keypress_notify *ev = (void *) skb->data;
4931 struct hci_conn *conn;
4932
4933 BT_DBG("%s", hdev->name);
4934
4935 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4936 if (!conn)
4937 return;
4938
4939 switch (ev->type) {
4940 case HCI_KEYPRESS_STARTED:
4941 conn->passkey_entered = 0;
4942 return;
4943
4944 case HCI_KEYPRESS_ENTERED:
4945 conn->passkey_entered++;
4946 break;
4947
4948 case HCI_KEYPRESS_ERASED:
4949 conn->passkey_entered--;
4950 break;
4951
4952 case HCI_KEYPRESS_CLEARED:
4953 conn->passkey_entered = 0;
4954 break;
4955
4956 case HCI_KEYPRESS_COMPLETED:
4957 return;
4958 }
4959
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004960 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg92a25252012-09-06 18:39:26 +03004961 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4962 conn->dst_type, conn->passkey_notify,
4963 conn->passkey_entered);
4964}
4965
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004966static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
4967 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02004968{
4969 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
4970 struct hci_conn *conn;
4971
4972 BT_DBG("%s", hdev->name);
4973
4974 hci_dev_lock(hdev);
4975
4976 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03004977 if (!conn)
4978 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02004979
Johan Hedbergc1d4fa72014-07-17 15:14:50 +03004980 /* Reset the authentication requirement to unknown */
4981 conn->remote_auth = 0xff;
4982
Johan Hedberg2a611692011-02-19 12:06:00 -03004983 /* To avoid duplicate auth_failed events to user space we check
4984 * the HCI_CONN_AUTH_PEND flag which will be set if we
4985 * initiated the authentication. A traditional auth_complete
4986 * event gets always produced as initiator and is also mapped to
4987 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02004988 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedberge1e930f2014-09-08 17:09:49 -07004989 mgmt_auth_failed(conn, ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03004990
David Herrmann76a68ba2013-04-06 20:28:37 +02004991 hci_conn_drop(conn);
Johan Hedberg2a611692011-02-19 12:06:00 -03004992
4993unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02004994 hci_dev_unlock(hdev);
4995}
4996
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004997static void hci_remote_host_features_evt(struct hci_dev *hdev,
4998 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02004999{
5000 struct hci_ev_remote_host_features *ev = (void *) skb->data;
5001 struct inquiry_entry *ie;
Johan Hedbergcad718e2013-04-17 15:00:51 +03005002 struct hci_conn *conn;
Marcel Holtmann41a96212008-07-14 20:13:48 +02005003
5004 BT_DBG("%s", hdev->name);
5005
5006 hci_dev_lock(hdev);
5007
Johan Hedbergcad718e2013-04-17 15:00:51 +03005008 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5009 if (conn)
5010 memcpy(conn->features[1], ev->features, 8);
5011
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02005012 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
5013 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02005014 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02005015
5016 hci_dev_unlock(hdev);
5017}
5018
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005019static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
5020 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01005021{
5022 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
5023 struct oob_data *data;
5024
5025 BT_DBG("%s", hdev->name);
5026
5027 hci_dev_lock(hdev);
5028
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005029 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Szymon Jance1ba1f12011-04-06 13:01:59 +02005030 goto unlock;
5031
Johan Hedberg6928a922014-10-26 20:46:09 +01005032 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
Marcel Holtmann6665d052015-01-27 16:04:31 -08005033 if (!data) {
Szymon Janc2763eda2011-03-22 13:12:22 +01005034 struct hci_cp_remote_oob_data_neg_reply cp;
5035
5036 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08005037 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
5038 sizeof(cp), &cp);
Marcel Holtmann6665d052015-01-27 16:04:31 -08005039 goto unlock;
5040 }
5041
5042 if (bredr_sc_enabled(hdev)) {
5043 struct hci_cp_remote_oob_ext_data_reply cp;
5044
5045 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005046 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmann6665d052015-01-27 16:04:31 -08005047 memset(cp.hash192, 0, sizeof(cp.hash192));
5048 memset(cp.rand192, 0, sizeof(cp.rand192));
5049 } else {
5050 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
5051 memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
5052 }
5053 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
5054 memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
5055
5056 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
5057 sizeof(cp), &cp);
5058 } else {
5059 struct hci_cp_remote_oob_data_reply cp;
5060
5061 bacpy(&cp.bdaddr, &ev->bdaddr);
5062 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
5063 memcpy(cp.rand, data->rand192, sizeof(cp.rand));
5064
5065 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
5066 sizeof(cp), &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01005067 }
5068
Szymon Jance1ba1f12011-04-06 13:01:59 +02005069unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01005070 hci_dev_unlock(hdev);
5071}
5072
Arron Wanga77a6a12015-07-24 17:13:15 +08005073#if IS_ENABLED(CONFIG_BT_HS)
5074static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
5075{
5076 struct hci_ev_channel_selected *ev = (void *)skb->data;
5077 struct hci_conn *hcon;
5078
5079 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
5080
5081 skb_pull(skb, sizeof(*ev));
5082
5083 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5084 if (!hcon)
5085 return;
5086
5087 amp_read_loc_assoc_final_data(hdev, hcon);
5088}
5089
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005090static void hci_phy_link_complete_evt(struct hci_dev *hdev,
5091 struct sk_buff *skb)
5092{
5093 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
5094 struct hci_conn *hcon, *bredr_hcon;
5095
5096 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
5097 ev->status);
5098
5099 hci_dev_lock(hdev);
5100
5101 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005102 if (!hcon)
5103 goto unlock;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005104
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005105 if (!hcon->amp_mgr)
5106 goto unlock;
Anmol Karn6dfccd12020-09-30 19:48:13 +05305107
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005108 if (ev->status) {
5109 hci_conn_del(hcon);
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005110 goto unlock;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005111 }
5112
5113 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
5114
5115 hcon->state = BT_CONNECTED;
5116 bacpy(&hcon->dst, &bredr_hcon->dst);
5117
5118 hci_conn_hold(hcon);
5119 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02005120 hci_conn_drop(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005121
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01005122 hci_debugfs_create_conn(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005123 hci_conn_add_sysfs(hcon);
5124
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +02005125 amp_physical_cfm(bredr_hcon, hcon);
5126
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005127unlock:
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005128 hci_dev_unlock(hdev);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005129}
5130
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005131static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
5132{
5133 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
5134 struct hci_conn *hcon;
5135 struct hci_chan *hchan;
5136 struct amp_mgr *mgr;
5137
5138 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
5139 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
5140 ev->status);
5141
5142 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5143 if (!hcon)
5144 return;
5145
5146 /* Create AMP hchan */
5147 hchan = hci_chan_create(hcon);
5148 if (!hchan)
5149 return;
5150
5151 hchan->handle = le16_to_cpu(ev->handle);
Archie Pusaka5c4c8c92021-03-22 14:03:11 +08005152 hchan->amp = true;
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005153
5154 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
5155
5156 mgr = hcon->amp_mgr;
5157 if (mgr && mgr->bredr_chan) {
5158 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
5159
5160 l2cap_chan_lock(bredr_chan);
5161
5162 bredr_chan->conn->mtu = hdev->block_mtu;
5163 l2cap_logical_cfm(bredr_chan, hchan, 0);
5164 hci_conn_hold(hcon);
5165
5166 l2cap_chan_unlock(bredr_chan);
5167 }
5168}
5169
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005170static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
5171 struct sk_buff *skb)
5172{
5173 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
5174 struct hci_chan *hchan;
5175
5176 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
5177 le16_to_cpu(ev->handle), ev->status);
5178
5179 if (ev->status)
5180 return;
5181
5182 hci_dev_lock(hdev);
5183
5184 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
Archie Pusaka5c4c8c92021-03-22 14:03:11 +08005185 if (!hchan || !hchan->amp)
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005186 goto unlock;
5187
5188 amp_destroy_logical_link(hchan, ev->reason);
5189
5190unlock:
5191 hci_dev_unlock(hdev);
5192}
5193
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005194static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
5195 struct sk_buff *skb)
5196{
5197 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
5198 struct hci_conn *hcon;
5199
5200 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5201
5202 if (ev->status)
5203 return;
5204
5205 hci_dev_lock(hdev);
5206
5207 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5208 if (hcon) {
5209 hcon->state = BT_CLOSED;
5210 hci_conn_del(hcon);
5211 }
5212
5213 hci_dev_unlock(hdev);
5214}
Arron Wanga77a6a12015-07-24 17:13:15 +08005215#endif
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005216
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005217static void le_conn_update_addr(struct hci_conn *conn, bdaddr_t *bdaddr,
5218 u8 bdaddr_type, bdaddr_t *local_rpa)
5219{
5220 if (conn->out) {
5221 conn->dst_type = bdaddr_type;
5222 conn->resp_addr_type = bdaddr_type;
5223 bacpy(&conn->resp_addr, bdaddr);
5224
5225 /* Check if the controller has set a Local RPA then it must be
5226 * used instead or hdev->rpa.
5227 */
5228 if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5229 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5230 bacpy(&conn->init_addr, local_rpa);
5231 } else if (hci_dev_test_flag(conn->hdev, HCI_PRIVACY)) {
5232 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5233 bacpy(&conn->init_addr, &conn->hdev->rpa);
5234 } else {
5235 hci_copy_identity_address(conn->hdev, &conn->init_addr,
5236 &conn->init_addr_type);
5237 }
5238 } else {
5239 conn->resp_addr_type = conn->hdev->adv_addr_type;
5240 /* Check if the controller has set a Local RPA then it must be
5241 * used instead or hdev->rpa.
5242 */
5243 if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5244 conn->resp_addr_type = ADDR_LE_DEV_RANDOM;
5245 bacpy(&conn->resp_addr, local_rpa);
5246 } else if (conn->hdev->adv_addr_type == ADDR_LE_DEV_RANDOM) {
5247 /* In case of ext adv, resp_addr will be updated in
5248 * Adv Terminated event.
5249 */
5250 if (!ext_adv_capable(conn->hdev))
5251 bacpy(&conn->resp_addr,
5252 &conn->hdev->random_addr);
5253 } else {
5254 bacpy(&conn->resp_addr, &conn->hdev->bdaddr);
5255 }
5256
5257 conn->init_addr_type = bdaddr_type;
5258 bacpy(&conn->init_addr, bdaddr);
5259
5260 /* For incoming connections, set the default minimum
5261 * and maximum connection interval. They will be used
5262 * to check if the parameters are in range and if not
5263 * trigger the connection update procedure.
5264 */
5265 conn->le_conn_min_interval = conn->hdev->le_conn_min_interval;
5266 conn->le_conn_max_interval = conn->hdev->le_conn_max_interval;
5267 }
5268}
5269
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305270static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005271 bdaddr_t *bdaddr, u8 bdaddr_type,
5272 bdaddr_t *local_rpa, u8 role, u16 handle,
5273 u16 interval, u16 latency,
5274 u16 supervision_timeout)
Ville Tervofcd89c02011-02-10 22:38:47 -03005275{
Johan Hedberg912b42ef2014-07-03 19:33:49 +03005276 struct hci_conn_params *params;
Ville Tervofcd89c02011-02-10 22:38:47 -03005277 struct hci_conn *conn;
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02005278 struct smp_irk *irk;
Johan Hedberg837d5022014-07-02 09:36:22 +03005279 u8 addr_type;
Ville Tervofcd89c02011-02-10 22:38:47 -03005280
Ville Tervofcd89c02011-02-10 22:38:47 -03005281 hci_dev_lock(hdev);
5282
Johan Hedbergfbd96c12014-07-08 17:21:51 +03005283 /* All controllers implicitly stop advertising in the event of a
5284 * connection, so ensure that the state bit is cleared.
5285 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005286 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedbergfbd96c12014-07-08 17:21:51 +03005287
Jakub Pawlowskie7d9ab72015-08-07 20:22:52 +02005288 conn = hci_lookup_le_connect(hdev);
Ville Tervob62f3282011-02-10 22:38:50 -03005289 if (!conn) {
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305290 conn = hci_conn_add(hdev, LE_LINK, bdaddr, role);
Ville Tervob62f3282011-02-10 22:38:50 -03005291 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005292 bt_dev_err(hdev, "no memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03005293 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03005294 }
Andre Guedes29b79882011-05-31 14:20:54 -03005295
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305296 conn->dst_type = bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03005297
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005298 /* If we didn't have a hci_conn object previously
Archie Pusaka74be5232021-06-04 16:26:25 +08005299 * but we're in central role this must be something
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005300 * initiated using an accept list. Since accept list based
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005301 * connections are not "first class citizens" we don't
5302 * have full tracking of them. Therefore, we go ahead
5303 * with a "best effort" approach of determining the
5304 * initiator address based on the HCI_PRIVACY flag.
5305 */
5306 if (conn->out) {
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305307 conn->resp_addr_type = bdaddr_type;
5308 bacpy(&conn->resp_addr, bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005309 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005310 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5311 bacpy(&conn->init_addr, &hdev->rpa);
5312 } else {
5313 hci_copy_identity_address(hdev,
5314 &conn->init_addr,
5315 &conn->init_addr_type);
5316 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005317 }
Johan Hedberg9489eca2014-02-28 17:45:46 +02005318 } else {
5319 cancel_delayed_work(&conn->le_conn_timeout);
Ville Tervob62f3282011-02-10 22:38:50 -03005320 }
Ville Tervofcd89c02011-02-10 22:38:47 -03005321
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005322 le_conn_update_addr(conn, bdaddr, bdaddr_type, local_rpa);
Johan Hedberg7be2edb2014-02-23 19:42:17 +02005323
Marcel Holtmannedb4b462014-02-18 15:13:43 -08005324 /* Lookup the identity address from the stored connection
5325 * address and address type.
5326 *
5327 * When establishing connections to an identity address, the
5328 * connection procedure will store the resolvable random
5329 * address first. Now if it can be converted back into the
5330 * identity address, start using the identity address from
5331 * now on.
5332 */
5333 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02005334 if (irk) {
5335 bacpy(&conn->dst, &irk->bdaddr);
5336 conn->dst_type = irk->addr_type;
5337 }
5338
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005339 conn->dst_type = ev_bdaddr_type(hdev, conn->dst_type, NULL);
Sathish Narasimman79699a72021-05-20 17:12:01 +05305340
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305341 if (status) {
5342 hci_le_conn_failed(conn, status);
Johan Hedberg837d5022014-07-02 09:36:22 +03005343 goto unlock;
5344 }
5345
Johan Hedberg08853f12014-08-15 21:06:55 +03005346 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
5347 addr_type = BDADDR_LE_PUBLIC;
5348 else
5349 addr_type = BDADDR_LE_RANDOM;
5350
Johan Hedberg2d3c2262014-07-15 11:51:28 +03005351 /* Drop the connection if the device is blocked */
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005352 if (hci_bdaddr_list_lookup(&hdev->reject_list, &conn->dst, addr_type)) {
Johan Hedberg2d3c2262014-07-15 11:51:28 +03005353 hci_conn_drop(conn);
Andre Guedescd17dec2012-07-27 15:10:16 -03005354 goto unlock;
5355 }
5356
Johan Hedbergb644ba32012-01-17 21:48:47 +02005357 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07005358 mgmt_device_connected(hdev, conn, NULL, 0);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03005359
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03005360 conn->sec_level = BT_SECURITY_LOW;
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305361 conn->handle = handle;
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005362 conn->state = BT_CONFIG;
Ville Tervofcd89c02011-02-10 22:38:47 -03005363
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005364 /* Store current advertising instance as connection advertising instance
5365 * when sotfware rotation is in use so it can be re-enabled when
5366 * disconnected.
5367 */
5368 if (!ext_adv_capable(hdev))
5369 conn->adv_instance = hdev->cur_adv_instance;
5370
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305371 conn->le_conn_interval = interval;
5372 conn->le_conn_latency = latency;
5373 conn->le_supv_timeout = supervision_timeout;
Marcel Holtmanne04fde62014-06-23 11:40:04 +02005374
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01005375 hci_debugfs_create_conn(conn);
Ville Tervofcd89c02011-02-10 22:38:47 -03005376 hci_conn_add_sysfs(conn);
5377
Archie Pusakaef365da2021-05-31 16:37:23 +08005378 /* The remote features procedure is defined for central
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005379 * role only. So only in case of an initiated connection
5380 * request the remote features.
5381 *
Archie Pusakaef365da2021-05-31 16:37:23 +08005382 * If the local controller supports peripheral-initiated features
5383 * exchange, then requesting the remote features in peripheral
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005384 * role is possible. Otherwise just transition into the
5385 * connected state without requesting the remote features.
5386 */
5387 if (conn->out ||
Archie Pusakaef365da2021-05-31 16:37:23 +08005388 (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES)) {
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005389 struct hci_cp_le_read_remote_features cp;
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005390
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005391 cp.handle = __cpu_to_le16(conn->handle);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005392
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005393 hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
5394 sizeof(cp), &cp);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005395
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005396 hci_conn_hold(conn);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005397 } else {
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005398 conn->state = BT_CONNECTED;
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305399 hci_connect_cfm(conn, status);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005400 }
Ville Tervofcd89c02011-02-10 22:38:47 -03005401
Johan Hedberg54776102014-08-15 21:06:56 +03005402 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
5403 conn->dst_type);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005404 if (params) {
Johan Hedberg95305ba2014-07-04 12:37:21 +03005405 list_del_init(&params->action);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005406 if (params->conn) {
5407 hci_conn_drop(params->conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005408 hci_conn_put(params->conn);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005409 params->conn = NULL;
5410 }
5411 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005412
Ville Tervofcd89c02011-02-10 22:38:47 -03005413unlock:
Johan Hedberg223683a52014-07-06 15:44:23 +03005414 hci_update_background_scan(hdev);
Ville Tervofcd89c02011-02-10 22:38:47 -03005415 hci_dev_unlock(hdev);
5416}
5417
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305418static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
5419{
5420 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
5421
5422 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5423
5424 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005425 NULL, ev->role, le16_to_cpu(ev->handle),
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305426 le16_to_cpu(ev->interval),
5427 le16_to_cpu(ev->latency),
5428 le16_to_cpu(ev->supervision_timeout));
5429}
5430
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005431static void hci_le_enh_conn_complete_evt(struct hci_dev *hdev,
5432 struct sk_buff *skb)
5433{
5434 struct hci_ev_le_enh_conn_complete *ev = (void *) skb->data;
5435
5436 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5437
5438 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005439 &ev->local_rpa, ev->role, le16_to_cpu(ev->handle),
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005440 le16_to_cpu(ev->interval),
5441 le16_to_cpu(ev->latency),
5442 le16_to_cpu(ev->supervision_timeout));
Sathish Narasimman5c49bcc2020-07-23 18:09:01 +05305443
5444 if (use_ll_privacy(hdev) &&
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05305445 hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
Sathish Narasimman5c49bcc2020-07-23 18:09:01 +05305446 hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
5447 hci_req_disable_address_resolution(hdev);
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005448}
5449
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305450static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, struct sk_buff *skb)
5451{
5452 struct hci_evt_le_ext_adv_set_term *ev = (void *) skb->data;
5453 struct hci_conn *conn;
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005454 struct adv_info *adv;
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305455
5456 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5457
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005458 adv = hci_find_adv_instance(hdev, ev->handle);
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07005459
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005460 if (ev->status) {
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07005461 if (!adv)
5462 return;
5463
5464 /* Remove advertising as it has been terminated */
5465 hci_remove_adv_instance(hdev, ev->handle);
5466 mgmt_advertising_removed(NULL, hdev, ev->handle);
5467
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305468 return;
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07005469 }
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305470
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005471 if (adv)
5472 adv->enabled = false;
5473
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305474 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle));
5475 if (conn) {
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005476 /* Store handle in the connection so the correct advertising
5477 * instance can be re-enabled when disconnected.
5478 */
5479 conn->adv_instance = ev->handle;
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305480
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005481 if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM ||
5482 bacmp(&conn->resp_addr, BDADDR_ANY))
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305483 return;
5484
Daniel Winkler25e70882021-04-05 16:33:04 -07005485 if (!ev->handle) {
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305486 bacpy(&conn->resp_addr, &hdev->random_addr);
5487 return;
5488 }
5489
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005490 if (adv)
5491 bacpy(&conn->resp_addr, &adv->random_addr);
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305492 }
5493}
5494
Marcel Holtmann1855d922014-06-23 11:40:05 +02005495static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
5496 struct sk_buff *skb)
5497{
5498 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
5499 struct hci_conn *conn;
5500
5501 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5502
5503 if (ev->status)
5504 return;
5505
5506 hci_dev_lock(hdev);
5507
5508 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5509 if (conn) {
5510 conn->le_conn_interval = le16_to_cpu(ev->interval);
5511 conn->le_conn_latency = le16_to_cpu(ev->latency);
5512 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
5513 }
5514
5515 hci_dev_unlock(hdev);
5516}
5517
Andre Guedesa4790db2014-02-26 20:21:47 -03005518/* This function requires the caller holds hdev->lock */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005519static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
5520 bdaddr_t *addr,
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005521 u8 addr_type, bool addr_resolved,
5522 u8 adv_type, bdaddr_t *direct_rpa)
Andre Guedesa4790db2014-02-26 20:21:47 -03005523{
5524 struct hci_conn *conn;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005525 struct hci_conn_params *params;
Andre Guedesa4790db2014-02-26 20:21:47 -03005526
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005527 /* If the event is not connectable don't proceed further */
5528 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005529 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005530
5531 /* Ignore if the device is blocked */
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005532 if (hci_bdaddr_list_lookup(&hdev->reject_list, addr, addr_type))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005533 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005534
Johan Hedbergf99353c2014-07-16 11:56:09 +03005535 /* Most controller will fail if we try to create new connections
Archie Pusaka39bc74c2021-06-04 16:26:26 +08005536 * while we have an existing one in peripheral role.
Johan Hedbergf99353c2014-07-16 11:56:09 +03005537 */
Archie Pusaka39bc74c2021-06-04 16:26:26 +08005538 if (hdev->conn_hash.le_num_peripheral > 0 &&
Alain Michaud4364f2e2020-04-23 14:43:29 +00005539 (!test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) ||
5540 !(hdev->le_states[3] & 0x10)))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005541 return NULL;
Johan Hedbergf99353c2014-07-16 11:56:09 +03005542
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005543 /* If we're not connectable only connect devices that we have in
5544 * our pend_le_conns list.
5545 */
Johan Hedberg49c50922015-10-16 10:07:51 +03005546 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
5547 addr_type);
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005548 if (!params)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005549 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005550
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005551 if (!params->explicit_connect) {
5552 switch (params->auto_connect) {
5553 case HCI_AUTO_CONN_DIRECT:
5554 /* Only devices advertising with ADV_DIRECT_IND are
5555 * triggering a connection attempt. This is allowing
Archie Pusaka67ffb182021-05-31 16:37:28 +08005556 * incoming connections from peripheral devices.
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005557 */
5558 if (adv_type != LE_ADV_DIRECT_IND)
5559 return NULL;
5560 break;
5561 case HCI_AUTO_CONN_ALWAYS:
5562 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
5563 * are triggering a connection attempt. This means
Archie Pusaka67ffb182021-05-31 16:37:28 +08005564 * that incoming connections from peripheral device are
5565 * accepted and also outgoing connections to peripheral
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005566 * devices are established when found.
5567 */
5568 break;
5569 default:
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005570 return NULL;
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005571 }
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005572 }
5573
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005574 conn = hci_connect_le(hdev, addr, addr_type, addr_resolved,
5575 BT_SECURITY_LOW, hdev->def_le_autoconnect_timeout,
5576 HCI_ROLE_MASTER, direct_rpa);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005577 if (!IS_ERR(conn)) {
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005578 /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
5579 * by higher layer that tried to connect, if no then
5580 * store the pointer since we don't really have any
Johan Hedbergf161dd42014-08-15 21:06:54 +03005581 * other owner of the object besides the params that
5582 * triggered it. This way we can abort the connection if
5583 * the parameters get removed and keep the reference
5584 * count consistent once the connection is established.
5585 */
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005586
5587 if (!params->explicit_connect)
5588 params->conn = hci_conn_get(conn);
5589
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005590 return conn;
Johan Hedbergf161dd42014-08-15 21:06:54 +03005591 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005592
5593 switch (PTR_ERR(conn)) {
5594 case -EBUSY:
5595 /* If hci_connect() returns -EBUSY it means there is already
5596 * an LE connection attempt going on. Since controllers don't
5597 * support more than one connection attempt at the time, we
5598 * don't consider this an error case.
5599 */
5600 break;
5601 default:
5602 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005603 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005604 }
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005605
5606 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005607}
5608
Johan Hedberg4af605d2014-03-24 10:48:00 +02005609static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005610 u8 bdaddr_type, bdaddr_t *direct_addr,
Alain Michauda2ec9052020-07-27 20:48:55 +00005611 u8 direct_addr_type, s8 rssi, u8 *data, u8 len,
5612 bool ext_adv)
Johan Hedberg4af605d2014-03-24 10:48:00 +02005613{
Johan Hedbergb9a63282014-03-25 10:51:52 +02005614 struct discovery_state *d = &hdev->discovery;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005615 struct smp_irk *irk;
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005616 struct hci_conn *conn;
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005617 bool match, bdaddr_resolved;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005618 u32 flags;
Szymon Janc1c58e932021-05-18 16:54:36 +02005619 u8 *ptr;
Szymon Janc68183752015-09-16 20:21:54 +02005620
Johan Hedberg56b40fb2016-04-07 21:01:27 +03005621 switch (type) {
5622 case LE_ADV_IND:
5623 case LE_ADV_DIRECT_IND:
5624 case LE_ADV_SCAN_IND:
5625 case LE_ADV_NONCONN_IND:
5626 case LE_ADV_SCAN_RSP:
5627 break;
5628 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005629 bt_dev_err_ratelimited(hdev, "unknown advertising packet "
5630 "type: 0x%02x", type);
Johan Hedberg56b40fb2016-04-07 21:01:27 +03005631 return;
5632 }
5633
Alain Michauda2ec9052020-07-27 20:48:55 +00005634 if (!ext_adv && len > HCI_MAX_AD_LENGTH) {
5635 bt_dev_err_ratelimited(hdev, "legacy adv larger than 31 bytes");
5636 return;
5637 }
5638
Szymon Janc68183752015-09-16 20:21:54 +02005639 /* Find the end of the data in case the report contains padded zero
5640 * bytes at the end causing an invalid length value.
5641 *
5642 * When data is NULL, len is 0 so there is no need for extra ptr
5643 * check as 'ptr < data + 0' is already false in such case.
5644 */
5645 for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
5646 if (ptr + 1 + *ptr > data + len)
5647 break;
5648 }
5649
Szymon Janc1c58e932021-05-18 16:54:36 +02005650 /* Adjust for actual length. This handles the case when remote
5651 * device is advertising with incorrect data length.
5652 */
5653 len = ptr - data;
Johan Hedbergb9a63282014-03-25 10:51:52 +02005654
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005655 /* If the direct address is present, then this report is from
5656 * a LE Direct Advertising Report event. In that case it is
5657 * important to see if the address is matching the local
5658 * controller address.
5659 */
5660 if (direct_addr) {
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005661 direct_addr_type = ev_bdaddr_type(hdev, direct_addr_type,
5662 &bdaddr_resolved);
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07005663
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005664 /* Only resolvable random addresses are valid for these
5665 * kind of reports and others can be ignored.
5666 */
5667 if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
5668 return;
5669
5670 /* If the controller is not using resolvable random
5671 * addresses, then this report can be ignored.
5672 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005673 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005674 return;
5675
5676 /* If the local IRK of the controller does not match
5677 * with the resolvable random address provided, then
5678 * this report can be ignored.
5679 */
5680 if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
5681 return;
5682 }
5683
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005684 /* Check if we need to convert to identity address */
5685 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
5686 if (irk) {
5687 bdaddr = &irk->bdaddr;
5688 bdaddr_type = irk->addr_type;
5689 }
5690
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005691 bdaddr_type = ev_bdaddr_type(hdev, bdaddr_type, &bdaddr_resolved);
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07005692
Szymon Janc082f2302018-04-03 13:40:06 +02005693 /* Check if we have been requested to connect to this device.
5694 *
5695 * direct_addr is set only for directed advertising reports (it is NULL
5696 * for advertising reports) and is already verified to be RPA above.
5697 */
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005698 conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, bdaddr_resolved,
5699 type, direct_addr);
Alain Michauda2ec9052020-07-27 20:48:55 +00005700 if (!ext_adv && conn && type == LE_ADV_IND && len <= HCI_MAX_AD_LENGTH) {
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005701 /* Store report for later inclusion by
5702 * mgmt_device_connected
5703 */
5704 memcpy(conn->le_adv_data, data, len);
5705 conn->le_adv_data_len = len;
5706 }
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005707
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005708 /* Passive scanning shouldn't trigger any device found events,
5709 * except for devices marked as CONN_REPORT for which we do send
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02005710 * device found events, or advertisement monitoring requested.
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005711 */
Johan Hedbergca5c4be2014-03-25 10:30:46 +02005712 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005713 if (type == LE_ADV_DIRECT_IND)
5714 return;
5715
Johan Hedberg3a19b6f2014-07-15 08:07:59 +03005716 if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02005717 bdaddr, bdaddr_type) &&
5718 idr_is_empty(&hdev->adv_monitors_idr))
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005719 return;
5720
5721 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
5722 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
5723 else
5724 flags = 0;
5725 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
5726 rssi, flags, data, len, NULL, 0);
Johan Hedberg97bf2e92014-07-04 12:37:16 +03005727 return;
Johan Hedbergca5c4be2014-03-25 10:30:46 +02005728 }
Johan Hedberg4af605d2014-03-24 10:48:00 +02005729
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005730 /* When receiving non-connectable or scannable undirected
5731 * advertising reports, this means that the remote device is
5732 * not connectable and then clearly indicate this in the
5733 * device found event.
5734 *
5735 * When receiving a scan response, then there is no way to
5736 * know if the remote device is connectable or not. However
5737 * since scan responses are merged with a previously seen
5738 * advertising report, the flags field from that report
5739 * will be used.
5740 *
5741 * In the really unlikely case that a controller get confused
5742 * and just sends a scan response event, then it is marked as
5743 * not connectable as well.
5744 */
5745 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
5746 type == LE_ADV_SCAN_RSP)
5747 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
5748 else
5749 flags = 0;
5750
Johan Hedbergb9a63282014-03-25 10:51:52 +02005751 /* If there's nothing pending either store the data from this
5752 * event or send an immediate device found event if the data
5753 * should not be stored for later.
5754 */
Alain Michauda2ec9052020-07-27 20:48:55 +00005755 if (!ext_adv && !has_pending_adv_report(hdev)) {
Johan Hedbergb9a63282014-03-25 10:51:52 +02005756 /* If the report will trigger a SCAN_REQ store it for
5757 * later merging.
5758 */
5759 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
5760 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005761 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005762 return;
5763 }
5764
5765 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005766 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005767 return;
5768 }
5769
Johan Hedberg474ee062014-03-25 14:34:59 +02005770 /* Check if the pending report is for the same device as the new one */
5771 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
5772 bdaddr_type == d->last_adv_addr_type);
5773
Johan Hedbergb9a63282014-03-25 10:51:52 +02005774 /* If the pending data doesn't match this report or this isn't a
5775 * scan response (e.g. we got a duplicate ADV_IND) then force
5776 * sending of the pending data.
5777 */
Johan Hedberg474ee062014-03-25 14:34:59 +02005778 if (type != LE_ADV_SCAN_RSP || !match) {
5779 /* Send out whatever is in the cache, but skip duplicates */
5780 if (!match)
5781 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergff5cd292014-03-25 14:40:52 +02005782 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005783 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergff5cd292014-03-25 14:40:52 +02005784 d->last_adv_data,
Johan Hedberg474ee062014-03-25 14:34:59 +02005785 d->last_adv_data_len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005786
5787 /* If the new report will trigger a SCAN_REQ store it for
5788 * later merging.
5789 */
Alain Michauda2ec9052020-07-27 20:48:55 +00005790 if (!ext_adv && (type == LE_ADV_IND ||
5791 type == LE_ADV_SCAN_IND)) {
Johan Hedbergb9a63282014-03-25 10:51:52 +02005792 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005793 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005794 return;
5795 }
5796
5797 /* The advertising reports cannot be merged, so clear
5798 * the pending report and send out a device found event.
5799 */
5800 clear_pending_adv_report(hdev);
Johan Hedberg5c5b93e2014-03-29 08:39:53 +02005801 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005802 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005803 return;
5804 }
5805
5806 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
5807 * the new event is a SCAN_RSP. We can therefore proceed with
5808 * sending a merged device found event.
5809 */
5810 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005811 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
Marcel Holtmann42bd6a52014-07-01 14:11:19 +02005812 d->last_adv_data, d->last_adv_data_len, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005813 clear_pending_adv_report(hdev);
Johan Hedberg4af605d2014-03-24 10:48:00 +02005814}
5815
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005816static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03005817{
Andre Guedese95beb42011-09-26 20:48:35 -03005818 u8 num_reports = skb->data[0];
5819 void *ptr = &skb->data[1];
Andre Guedes9aa04c92011-05-26 16:23:51 -03005820
Andre Guedesa4790db2014-02-26 20:21:47 -03005821 hci_dev_lock(hdev);
5822
Andre Guedese95beb42011-09-26 20:48:35 -03005823 while (num_reports--) {
5824 struct hci_ev_le_advertising_info *ev = ptr;
Johan Hedberg4af605d2014-03-24 10:48:00 +02005825 s8 rssi;
Andre Guedesa4790db2014-02-26 20:21:47 -03005826
Chriz Chowee649342018-04-20 15:46:24 +08005827 if (ev->length <= HCI_MAX_AD_LENGTH) {
5828 rssi = ev->data[ev->length];
5829 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
5830 ev->bdaddr_type, NULL, 0, rssi,
Alain Michauda2ec9052020-07-27 20:48:55 +00005831 ev->data, ev->length, false);
Chriz Chowee649342018-04-20 15:46:24 +08005832 } else {
5833 bt_dev_err(hdev, "Dropping invalid advertising data");
5834 }
Andre Guedes3c9e9192012-01-10 18:20:50 -03005835
Andre Guedese95beb42011-09-26 20:48:35 -03005836 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03005837 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005838
5839 hci_dev_unlock(hdev);
Andre Guedes9aa04c92011-05-26 16:23:51 -03005840}
5841
Marcel Holtmann657cc642019-12-11 11:34:36 +01005842static u8 ext_evt_type_to_legacy(struct hci_dev *hdev, u16 evt_type)
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305843{
Jaganath Kanakkasseryb2cc9762018-07-19 17:09:38 +05305844 if (evt_type & LE_EXT_ADV_LEGACY_PDU) {
5845 switch (evt_type) {
5846 case LE_LEGACY_ADV_IND:
5847 return LE_ADV_IND;
5848 case LE_LEGACY_ADV_DIRECT_IND:
5849 return LE_ADV_DIRECT_IND;
5850 case LE_LEGACY_ADV_SCAN_IND:
5851 return LE_ADV_SCAN_IND;
5852 case LE_LEGACY_NONCONN_IND:
5853 return LE_ADV_NONCONN_IND;
5854 case LE_LEGACY_SCAN_RSP_ADV:
5855 case LE_LEGACY_SCAN_RSP_ADV_SCAN:
5856 return LE_ADV_SCAN_RSP;
5857 }
5858
Marcel Holtmann657cc642019-12-11 11:34:36 +01005859 goto invalid;
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305860 }
5861
Jaganath Kanakkasseryb2cc9762018-07-19 17:09:38 +05305862 if (evt_type & LE_EXT_ADV_CONN_IND) {
5863 if (evt_type & LE_EXT_ADV_DIRECT_IND)
5864 return LE_ADV_DIRECT_IND;
5865
5866 return LE_ADV_IND;
5867 }
5868
5869 if (evt_type & LE_EXT_ADV_SCAN_RSP)
5870 return LE_ADV_SCAN_RSP;
5871
5872 if (evt_type & LE_EXT_ADV_SCAN_IND)
5873 return LE_ADV_SCAN_IND;
5874
5875 if (evt_type == LE_EXT_ADV_NON_CONN_IND ||
5876 evt_type & LE_EXT_ADV_DIRECT_IND)
5877 return LE_ADV_NONCONN_IND;
5878
Marcel Holtmann657cc642019-12-11 11:34:36 +01005879invalid:
5880 bt_dev_err_ratelimited(hdev, "Unknown advertising packet type: 0x%02x",
5881 evt_type);
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305882
5883 return LE_ADV_INVALID;
5884}
5885
5886static void hci_le_ext_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
5887{
5888 u8 num_reports = skb->data[0];
5889 void *ptr = &skb->data[1];
5890
5891 hci_dev_lock(hdev);
5892
5893 while (num_reports--) {
5894 struct hci_ev_le_ext_adv_report *ev = ptr;
5895 u8 legacy_evt_type;
5896 u16 evt_type;
5897
5898 evt_type = __le16_to_cpu(ev->evt_type);
Marcel Holtmann657cc642019-12-11 11:34:36 +01005899 legacy_evt_type = ext_evt_type_to_legacy(hdev, evt_type);
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305900 if (legacy_evt_type != LE_ADV_INVALID) {
5901 process_adv_report(hdev, legacy_evt_type, &ev->bdaddr,
5902 ev->bdaddr_type, NULL, 0, ev->rssi,
Alain Michauda2ec9052020-07-27 20:48:55 +00005903 ev->data, ev->length,
5904 !(evt_type & LE_EXT_ADV_LEGACY_PDU));
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305905 }
5906
Jaganath Kanakkasserycd9151b2019-04-03 12:11:44 +05305907 ptr += sizeof(*ev) + ev->length;
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305908 }
5909
5910 hci_dev_unlock(hdev);
5911}
5912
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005913static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev,
5914 struct sk_buff *skb)
5915{
5916 struct hci_ev_le_remote_feat_complete *ev = (void *)skb->data;
5917 struct hci_conn *conn;
5918
5919 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5920
5921 hci_dev_lock(hdev);
5922
5923 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5924 if (conn) {
5925 if (!ev->status)
5926 memcpy(conn->features[0], ev->features, 8);
5927
5928 if (conn->state == BT_CONFIG) {
5929 __u8 status;
5930
Archie Pusakaef365da2021-05-31 16:37:23 +08005931 /* If the local controller supports peripheral-initiated
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005932 * features exchange, but the remote controller does
5933 * not, then it is possible that the error code 0x1a
5934 * for unsupported remote feature gets returned.
5935 *
5936 * In this specific case, allow the connection to
5937 * transition into connected state and mark it as
5938 * successful.
5939 */
Archie Pusakaef365da2021-05-31 16:37:23 +08005940 if (!conn->out && ev->status == 0x1a &&
5941 (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES))
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005942 status = 0x00;
5943 else
5944 status = ev->status;
5945
5946 conn->state = BT_CONNECTED;
5947 hci_connect_cfm(conn, status);
5948 hci_conn_drop(conn);
5949 }
5950 }
5951
5952 hci_dev_unlock(hdev);
5953}
5954
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005955static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005956{
5957 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
5958 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005959 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005960 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03005961 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005962
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03005963 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005964
5965 hci_dev_lock(hdev);
5966
5967 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005968 if (conn == NULL)
5969 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005970
Johan Hedbergf3a73d92014-05-29 15:02:59 +03005971 ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
Johan Hedberg5378bc52014-05-29 14:00:39 +03005972 if (!ltk)
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005973 goto not_found;
5974
Johan Hedberg5378bc52014-05-29 14:00:39 +03005975 if (smp_ltk_is_sc(ltk)) {
5976 /* With SC both EDiv and Rand are set to zero */
5977 if (ev->ediv || ev->rand)
5978 goto not_found;
5979 } else {
5980 /* For non-SC keys check that EDiv and Rand match */
5981 if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
5982 goto not_found;
5983 }
5984
Johan Hedberg8b76ce32015-06-08 18:14:39 +03005985 memcpy(cp.ltk, ltk->val, ltk->enc_size);
5986 memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005987 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03005988
Johan Hedberga6f78332014-09-10 17:37:45 -07005989 conn->pending_sec_level = smp_ltk_sec_level(ltk);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005990
Andre Guedes89cbb4d2013-07-31 16:25:29 -03005991 conn->enc_key_size = ltk->enc_size;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005992
5993 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
5994
Claudio Takahasi5981a882013-07-25 16:34:24 -03005995 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
5996 * temporary key used to encrypt a connection following
5997 * pairing. It is used during the Encrypted Session Setup to
5998 * distribute the keys. Later, security can be re-established
5999 * using a distributed LTK.
6000 */
Johan Hedberg2ceba532014-06-16 19:25:16 +03006001 if (ltk->type == SMP_STK) {
Johan Hedbergfe59a052014-07-01 19:14:12 +03006002 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Johan Hedberg970d0f12014-11-13 14:37:47 +02006003 list_del_rcu(&ltk->list);
6004 kfree_rcu(ltk, rcu);
Johan Hedbergfe59a052014-07-01 19:14:12 +03006005 } else {
6006 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03006007 }
6008
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006009 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03006010
6011 return;
6012
6013not_found:
6014 neg.handle = ev->handle;
6015 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
6016 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006017}
6018
Andre Guedes8e75b462014-07-01 18:10:08 -03006019static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
6020 u8 reason)
6021{
6022 struct hci_cp_le_conn_param_req_neg_reply cp;
6023
6024 cp.handle = cpu_to_le16(handle);
6025 cp.reason = reason;
6026
6027 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
6028 &cp);
6029}
6030
6031static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
6032 struct sk_buff *skb)
6033{
6034 struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
6035 struct hci_cp_le_conn_param_req_reply cp;
6036 struct hci_conn *hcon;
6037 u16 handle, min, max, latency, timeout;
6038
6039 handle = le16_to_cpu(ev->handle);
6040 min = le16_to_cpu(ev->interval_min);
6041 max = le16_to_cpu(ev->interval_max);
6042 latency = le16_to_cpu(ev->latency);
6043 timeout = le16_to_cpu(ev->timeout);
6044
6045 hcon = hci_conn_hash_lookup_handle(hdev, handle);
6046 if (!hcon || hcon->state != BT_CONNECTED)
6047 return send_conn_param_neg_reply(hdev, handle,
6048 HCI_ERROR_UNKNOWN_CONN_ID);
6049
6050 if (hci_check_conn_params(min, max, latency, timeout))
6051 return send_conn_param_neg_reply(hdev, handle,
6052 HCI_ERROR_INVALID_LL_PARAMS);
6053
Johan Hedberg40bef302014-07-16 11:42:27 +03006054 if (hcon->role == HCI_ROLE_MASTER) {
Johan Hedberg348d50b2014-07-02 17:37:30 +03006055 struct hci_conn_params *params;
Johan Hedbergf4869e22014-07-02 17:37:32 +03006056 u8 store_hint;
Johan Hedberg348d50b2014-07-02 17:37:30 +03006057
6058 hci_dev_lock(hdev);
6059
6060 params = hci_conn_params_lookup(hdev, &hcon->dst,
6061 hcon->dst_type);
6062 if (params) {
6063 params->conn_min_interval = min;
6064 params->conn_max_interval = max;
6065 params->conn_latency = latency;
6066 params->supervision_timeout = timeout;
Johan Hedbergf4869e22014-07-02 17:37:32 +03006067 store_hint = 0x01;
Meng Yu149b3f12021-04-01 14:50:39 +08006068 } else {
Johan Hedbergf4869e22014-07-02 17:37:32 +03006069 store_hint = 0x00;
Johan Hedberg348d50b2014-07-02 17:37:30 +03006070 }
6071
6072 hci_dev_unlock(hdev);
6073
Johan Hedbergf4869e22014-07-02 17:37:32 +03006074 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
6075 store_hint, min, max, latency, timeout);
Johan Hedberg348d50b2014-07-02 17:37:30 +03006076 }
Andre Guedesffb5a8272014-07-01 18:10:11 -03006077
Andre Guedes8e75b462014-07-01 18:10:08 -03006078 cp.handle = ev->handle;
6079 cp.interval_min = ev->interval_min;
6080 cp.interval_max = ev->interval_max;
6081 cp.latency = ev->latency;
6082 cp.timeout = ev->timeout;
6083 cp.min_ce_len = 0;
6084 cp.max_ce_len = 0;
6085
6086 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
6087}
6088
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006089static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
6090 struct sk_buff *skb)
6091{
6092 u8 num_reports = skb->data[0];
Peilin Yef7e0e8b2020-09-09 03:17:00 -04006093 struct hci_ev_le_direct_adv_info *ev = (void *)&skb->data[1];
6094
6095 if (!num_reports || skb->len < num_reports * sizeof(*ev) + 1)
6096 return;
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006097
6098 hci_dev_lock(hdev);
6099
Peilin Yef7e0e8b2020-09-09 03:17:00 -04006100 for (; num_reports; num_reports--, ev++)
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006101 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
6102 ev->bdaddr_type, &ev->direct_addr,
Alain Michauda2ec9052020-07-27 20:48:55 +00006103 ev->direct_addr_type, ev->rssi, NULL, 0,
6104 false);
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006105
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006106 hci_dev_unlock(hdev);
6107}
6108
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006109static void hci_le_phy_update_evt(struct hci_dev *hdev, struct sk_buff *skb)
6110{
6111 struct hci_ev_le_phy_update_complete *ev = (void *) skb->data;
6112 struct hci_conn *conn;
6113
6114 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
6115
Ayush Garg87df8bc2021-03-17 16:52:14 +05306116 if (ev->status)
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006117 return;
6118
6119 hci_dev_lock(hdev);
6120
6121 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6122 if (!conn)
6123 goto unlock;
6124
6125 conn->le_tx_phy = ev->tx_phy;
6126 conn->le_rx_phy = ev->rx_phy;
6127
6128unlock:
6129 hci_dev_unlock(hdev);
6130}
6131
Gustavo Padovan6039aa72012-05-23 04:04:18 -03006132static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03006133{
6134 struct hci_ev_le_meta *le_ev = (void *) skb->data;
6135
6136 skb_pull(skb, sizeof(*le_ev));
6137
6138 switch (le_ev->subevent) {
6139 case HCI_EV_LE_CONN_COMPLETE:
6140 hci_le_conn_complete_evt(hdev, skb);
6141 break;
6142
Marcel Holtmann1855d922014-06-23 11:40:05 +02006143 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
6144 hci_le_conn_update_complete_evt(hdev, skb);
6145 break;
6146
Andre Guedes9aa04c92011-05-26 16:23:51 -03006147 case HCI_EV_LE_ADVERTISING_REPORT:
6148 hci_le_adv_report_evt(hdev, skb);
6149 break;
6150
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006151 case HCI_EV_LE_REMOTE_FEAT_COMPLETE:
6152 hci_le_remote_feat_complete_evt(hdev, skb);
6153 break;
6154
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006155 case HCI_EV_LE_LTK_REQ:
6156 hci_le_ltk_request_evt(hdev, skb);
6157 break;
6158
Andre Guedes8e75b462014-07-01 18:10:08 -03006159 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
6160 hci_le_remote_conn_param_req_evt(hdev, skb);
6161 break;
6162
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006163 case HCI_EV_LE_DIRECT_ADV_REPORT:
6164 hci_le_direct_adv_report_evt(hdev, skb);
6165 break;
6166
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006167 case HCI_EV_LE_PHY_UPDATE_COMPLETE:
6168 hci_le_phy_update_evt(hdev, skb);
6169 break;
6170
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05306171 case HCI_EV_LE_EXT_ADV_REPORT:
6172 hci_le_ext_adv_report_evt(hdev, skb);
6173 break;
6174
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02006175 case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
6176 hci_le_enh_conn_complete_evt(hdev, skb);
6177 break;
6178
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306179 case HCI_EV_LE_EXT_ADV_SET_TERM:
6180 hci_le_ext_adv_term_evt(hdev, skb);
6181 break;
6182
Ville Tervofcd89c02011-02-10 22:38:47 -03006183 default:
6184 break;
6185 }
6186}
6187
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006188static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
6189 u8 event, struct sk_buff *skb)
6190{
6191 struct hci_ev_cmd_complete *ev;
6192 struct hci_event_hdr *hdr;
6193
6194 if (!skb)
6195 return false;
6196
6197 if (skb->len < sizeof(*hdr)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006198 bt_dev_err(hdev, "too short HCI event");
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006199 return false;
6200 }
6201
6202 hdr = (void *) skb->data;
6203 skb_pull(skb, HCI_EVENT_HDR_SIZE);
6204
6205 if (event) {
6206 if (hdr->evt != event)
6207 return false;
6208 return true;
6209 }
6210
Zheng Yongjun91641b72021-06-02 14:54:58 +08006211 /* Check if request ended in Command Status - no way to retrieve
Johan Hedberg1629db9c2018-11-27 11:37:46 +02006212 * any extra parameters in this case.
6213 */
6214 if (hdr->evt == HCI_EV_CMD_STATUS)
6215 return false;
6216
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006217 if (hdr->evt != HCI_EV_CMD_COMPLETE) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006218 bt_dev_err(hdev, "last event is not cmd complete (0x%2.2x)",
6219 hdr->evt);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006220 return false;
6221 }
6222
6223 if (skb->len < sizeof(*ev)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006224 bt_dev_err(hdev, "too short cmd_complete event");
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006225 return false;
6226 }
6227
6228 ev = (void *) skb->data;
6229 skb_pull(skb, sizeof(*ev));
6230
6231 if (opcode != __le16_to_cpu(ev->opcode)) {
6232 BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
6233 __le16_to_cpu(ev->opcode));
6234 return false;
6235 }
6236
6237 return true;
6238}
6239
Abhishek Pandit-Subedi2f202162020-09-11 14:07:13 -07006240static void hci_store_wake_reason(struct hci_dev *hdev, u8 event,
6241 struct sk_buff *skb)
6242{
6243 struct hci_ev_le_advertising_info *adv;
6244 struct hci_ev_le_direct_adv_info *direct_adv;
6245 struct hci_ev_le_ext_adv_report *ext_adv;
6246 const struct hci_ev_conn_complete *conn_complete = (void *)skb->data;
6247 const struct hci_ev_conn_request *conn_request = (void *)skb->data;
6248
6249 hci_dev_lock(hdev);
6250
6251 /* If we are currently suspended and this is the first BT event seen,
6252 * save the wake reason associated with the event.
6253 */
6254 if (!hdev->suspended || hdev->wake_reason)
6255 goto unlock;
6256
6257 /* Default to remote wake. Values for wake_reason are documented in the
6258 * Bluez mgmt api docs.
6259 */
6260 hdev->wake_reason = MGMT_WAKE_REASON_REMOTE_WAKE;
6261
6262 /* Once configured for remote wakeup, we should only wake up for
6263 * reconnections. It's useful to see which device is waking us up so
6264 * keep track of the bdaddr of the connection event that woke us up.
6265 */
6266 if (event == HCI_EV_CONN_REQUEST) {
6267 bacpy(&hdev->wake_addr, &conn_complete->bdaddr);
6268 hdev->wake_addr_type = BDADDR_BREDR;
6269 } else if (event == HCI_EV_CONN_COMPLETE) {
6270 bacpy(&hdev->wake_addr, &conn_request->bdaddr);
6271 hdev->wake_addr_type = BDADDR_BREDR;
6272 } else if (event == HCI_EV_LE_META) {
6273 struct hci_ev_le_meta *le_ev = (void *)skb->data;
6274 u8 subevent = le_ev->subevent;
6275 u8 *ptr = &skb->data[sizeof(*le_ev)];
6276 u8 num_reports = *ptr;
6277
6278 if ((subevent == HCI_EV_LE_ADVERTISING_REPORT ||
6279 subevent == HCI_EV_LE_DIRECT_ADV_REPORT ||
6280 subevent == HCI_EV_LE_EXT_ADV_REPORT) &&
6281 num_reports) {
6282 adv = (void *)(ptr + 1);
6283 direct_adv = (void *)(ptr + 1);
6284 ext_adv = (void *)(ptr + 1);
6285
6286 switch (subevent) {
6287 case HCI_EV_LE_ADVERTISING_REPORT:
6288 bacpy(&hdev->wake_addr, &adv->bdaddr);
6289 hdev->wake_addr_type = adv->bdaddr_type;
6290 break;
6291 case HCI_EV_LE_DIRECT_ADV_REPORT:
6292 bacpy(&hdev->wake_addr, &direct_adv->bdaddr);
6293 hdev->wake_addr_type = direct_adv->bdaddr_type;
6294 break;
6295 case HCI_EV_LE_EXT_ADV_REPORT:
6296 bacpy(&hdev->wake_addr, &ext_adv->bdaddr);
6297 hdev->wake_addr_type = ext_adv->bdaddr_type;
6298 break;
6299 }
6300 }
6301 } else {
6302 hdev->wake_reason = MGMT_WAKE_REASON_UNEXPECTED;
6303 }
6304
6305unlock:
6306 hci_dev_unlock(hdev);
6307}
6308
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
6310{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006311 struct hci_event_hdr *hdr = (void *) skb->data;
Johan Hedberge62144872015-04-02 13:41:08 +03006312 hci_req_complete_t req_complete = NULL;
6313 hci_req_complete_skb_t req_complete_skb = NULL;
6314 struct sk_buff *orig_skb = NULL;
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006315 u8 status = 0, event = hdr->evt, req_evt = 0;
Johan Hedberge62144872015-04-02 13:41:08 +03006316 u16 opcode = HCI_OP_NOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317
Alain Michaud08bb4da2020-03-03 15:55:34 +00006318 if (!event) {
6319 bt_dev_warn(hdev, "Received unexpected HCI Event 00000000");
6320 goto done;
6321 }
6322
Marcel Holtmann242c0eb2015-10-25 22:45:53 +01006323 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->hci.req_event == event) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02006324 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
Johan Hedberge62144872015-04-02 13:41:08 +03006325 opcode = __le16_to_cpu(cmd_hdr->opcode);
6326 hci_req_cmd_complete(hdev, opcode, status, &req_complete,
6327 &req_complete_skb);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006328 req_evt = event;
Johan Hedberg02350a72013-04-03 21:50:29 +03006329 }
6330
Johan Hedberge62144872015-04-02 13:41:08 +03006331 /* If it looks like we might end up having to call
6332 * req_complete_skb, store a pristine copy of the skb since the
6333 * various handlers may modify the original one through
6334 * skb_pull() calls, etc.
6335 */
6336 if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
6337 event == HCI_EV_CMD_COMPLETE)
6338 orig_skb = skb_clone(skb, GFP_KERNEL);
6339
6340 skb_pull(skb, HCI_EVENT_HDR_SIZE);
6341
Abhishek Pandit-Subedi2f202162020-09-11 14:07:13 -07006342 /* Store wake reason if we're suspended */
6343 hci_store_wake_reason(hdev, event, skb);
6344
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006345 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 case HCI_EV_INQUIRY_COMPLETE:
6347 hci_inquiry_complete_evt(hdev, skb);
6348 break;
6349
6350 case HCI_EV_INQUIRY_RESULT:
6351 hci_inquiry_result_evt(hdev, skb);
6352 break;
6353
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006354 case HCI_EV_CONN_COMPLETE:
6355 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02006356 break;
6357
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358 case HCI_EV_CONN_REQUEST:
6359 hci_conn_request_evt(hdev, skb);
6360 break;
6361
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362 case HCI_EV_DISCONN_COMPLETE:
6363 hci_disconn_complete_evt(hdev, skb);
6364 break;
6365
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 case HCI_EV_AUTH_COMPLETE:
6367 hci_auth_complete_evt(hdev, skb);
6368 break;
6369
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006370 case HCI_EV_REMOTE_NAME:
6371 hci_remote_name_evt(hdev, skb);
6372 break;
6373
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374 case HCI_EV_ENCRYPT_CHANGE:
6375 hci_encrypt_change_evt(hdev, skb);
6376 break;
6377
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006378 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
6379 hci_change_link_key_complete_evt(hdev, skb);
6380 break;
6381
6382 case HCI_EV_REMOTE_FEATURES:
6383 hci_remote_features_evt(hdev, skb);
6384 break;
6385
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006386 case HCI_EV_CMD_COMPLETE:
Johan Hedberge62144872015-04-02 13:41:08 +03006387 hci_cmd_complete_evt(hdev, skb, &opcode, &status,
6388 &req_complete, &req_complete_skb);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006389 break;
6390
6391 case HCI_EV_CMD_STATUS:
Johan Hedberge62144872015-04-02 13:41:08 +03006392 hci_cmd_status_evt(hdev, skb, &opcode, &status, &req_complete,
6393 &req_complete_skb);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006394 break;
6395
Marcel Holtmann24dfa342014-11-02 02:56:41 +01006396 case HCI_EV_HARDWARE_ERROR:
6397 hci_hardware_error_evt(hdev, skb);
6398 break;
6399
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006400 case HCI_EV_ROLE_CHANGE:
6401 hci_role_change_evt(hdev, skb);
6402 break;
6403
6404 case HCI_EV_NUM_COMP_PKTS:
6405 hci_num_comp_pkts_evt(hdev, skb);
6406 break;
6407
6408 case HCI_EV_MODE_CHANGE:
6409 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006410 break;
6411
6412 case HCI_EV_PIN_CODE_REQ:
6413 hci_pin_code_request_evt(hdev, skb);
6414 break;
6415
6416 case HCI_EV_LINK_KEY_REQ:
6417 hci_link_key_request_evt(hdev, skb);
6418 break;
6419
6420 case HCI_EV_LINK_KEY_NOTIFY:
6421 hci_link_key_notify_evt(hdev, skb);
6422 break;
6423
6424 case HCI_EV_CLOCK_OFFSET:
6425 hci_clock_offset_evt(hdev, skb);
6426 break;
6427
Marcel Holtmanna8746412008-07-14 20:13:46 +02006428 case HCI_EV_PKT_TYPE_CHANGE:
6429 hci_pkt_type_change_evt(hdev, skb);
6430 break;
6431
Marcel Holtmann85a1e932005-08-09 20:28:02 -07006432 case HCI_EV_PSCAN_REP_MODE:
6433 hci_pscan_rep_mode_evt(hdev, skb);
6434 break;
6435
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006436 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
6437 hci_inquiry_result_with_rssi_evt(hdev, skb);
6438 break;
6439
6440 case HCI_EV_REMOTE_EXT_FEATURES:
6441 hci_remote_ext_features_evt(hdev, skb);
6442 break;
6443
6444 case HCI_EV_SYNC_CONN_COMPLETE:
6445 hci_sync_conn_complete_evt(hdev, skb);
6446 break;
6447
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006448 case HCI_EV_EXTENDED_INQUIRY_RESULT:
6449 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006450 break;
6451
Johan Hedberg1c2e0042012-06-08 23:31:13 +08006452 case HCI_EV_KEY_REFRESH_COMPLETE:
6453 hci_key_refresh_complete_evt(hdev, skb);
6454 break;
6455
Marcel Holtmann04936842008-07-14 20:13:48 +02006456 case HCI_EV_IO_CAPA_REQUEST:
6457 hci_io_capa_request_evt(hdev, skb);
6458 break;
6459
Johan Hedberg03b555e2011-01-04 15:40:05 +02006460 case HCI_EV_IO_CAPA_REPLY:
6461 hci_io_capa_reply_evt(hdev, skb);
6462 break;
6463
Johan Hedberga5c29682011-02-19 12:05:57 -03006464 case HCI_EV_USER_CONFIRM_REQUEST:
6465 hci_user_confirm_request_evt(hdev, skb);
6466 break;
6467
Brian Gix1143d452011-11-23 08:28:34 -08006468 case HCI_EV_USER_PASSKEY_REQUEST:
6469 hci_user_passkey_request_evt(hdev, skb);
6470 break;
6471
Johan Hedberg92a25252012-09-06 18:39:26 +03006472 case HCI_EV_USER_PASSKEY_NOTIFY:
6473 hci_user_passkey_notify_evt(hdev, skb);
6474 break;
6475
6476 case HCI_EV_KEYPRESS_NOTIFY:
6477 hci_keypress_notify_evt(hdev, skb);
6478 break;
6479
Marcel Holtmann04936842008-07-14 20:13:48 +02006480 case HCI_EV_SIMPLE_PAIR_COMPLETE:
6481 hci_simple_pair_complete_evt(hdev, skb);
6482 break;
6483
Marcel Holtmann41a96212008-07-14 20:13:48 +02006484 case HCI_EV_REMOTE_HOST_FEATURES:
6485 hci_remote_host_features_evt(hdev, skb);
6486 break;
6487
Ville Tervofcd89c02011-02-10 22:38:47 -03006488 case HCI_EV_LE_META:
6489 hci_le_meta_evt(hdev, skb);
6490 break;
6491
Szymon Janc2763eda2011-03-22 13:12:22 +01006492 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
6493 hci_remote_oob_data_request_evt(hdev, skb);
6494 break;
6495
Arron Wanga77a6a12015-07-24 17:13:15 +08006496#if IS_ENABLED(CONFIG_BT_HS)
6497 case HCI_EV_CHANNEL_SELECTED:
6498 hci_chan_selected_evt(hdev, skb);
6499 break;
6500
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03006501 case HCI_EV_PHY_LINK_COMPLETE:
6502 hci_phy_link_complete_evt(hdev, skb);
6503 break;
6504
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03006505 case HCI_EV_LOGICAL_LINK_COMPLETE:
6506 hci_loglink_complete_evt(hdev, skb);
6507 break;
6508
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02006509 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
6510 hci_disconn_loglink_complete_evt(hdev, skb);
6511 break;
6512
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02006513 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
6514 hci_disconn_phylink_complete_evt(hdev, skb);
6515 break;
Arron Wanga77a6a12015-07-24 17:13:15 +08006516#endif
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02006517
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02006518 case HCI_EV_NUM_COMP_BLOCKS:
6519 hci_num_comp_blocks_evt(hdev, skb);
6520 break;
6521
Miao-chen Chou145373c2020-04-03 21:44:01 +02006522 case HCI_EV_VENDOR:
6523 msft_vendor_evt(hdev, skb);
6524 break;
6525
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006526 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03006527 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006528 break;
6529 }
6530
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006531 if (req_complete) {
Johan Hedberge62144872015-04-02 13:41:08 +03006532 req_complete(hdev, status, opcode);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006533 } else if (req_complete_skb) {
6534 if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
6535 kfree_skb(orig_skb);
6536 orig_skb = NULL;
6537 }
Johan Hedberge62144872015-04-02 13:41:08 +03006538 req_complete_skb(hdev, status, opcode, orig_skb);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006539 }
Johan Hedberge62144872015-04-02 13:41:08 +03006540
Alain Michaud08bb4da2020-03-03 15:55:34 +00006541done:
Johan Hedberge62144872015-04-02 13:41:08 +03006542 kfree_skb(orig_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543 kfree_skb(skb);
6544 hdev->stat.evt_rx++;
6545}