blob: 0bca035bf2dcc20340b228419b6c7d67a43c8952 [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
2281static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
2282{
2283 struct hci_cp_sniff_mode *cp;
2284 struct hci_conn *conn;
2285
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002286 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002287
2288 if (!status)
2289 return;
2290
2291 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
2292 if (!cp)
2293 return;
2294
2295 hci_dev_lock(hdev);
2296
2297 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002298 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002299 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002300
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002301 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002302 hci_sco_setup(conn, status);
2303 }
2304
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002305 hci_dev_unlock(hdev);
2306}
2307
2308static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
2309{
2310 struct hci_cp_exit_sniff_mode *cp;
2311 struct hci_conn *conn;
2312
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002313 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002314
2315 if (!status)
2316 return;
2317
2318 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
2319 if (!cp)
2320 return;
2321
2322 hci_dev_lock(hdev);
2323
2324 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002325 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002326 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002327
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002328 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002329 hci_sco_setup(conn, status);
2330 }
2331
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002332 hci_dev_unlock(hdev);
2333}
2334
Johan Hedberg88c3df12012-02-09 14:27:38 +02002335static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
2336{
2337 struct hci_cp_disconnect *cp;
2338 struct hci_conn *conn;
2339
2340 if (!status)
2341 return;
2342
2343 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
2344 if (!cp)
2345 return;
2346
2347 hci_dev_lock(hdev);
2348
2349 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Joseph Hwangb8d29052020-03-11 19:20:14 -07002350 if (conn) {
Johan Hedberg88c3df12012-02-09 14:27:38 +02002351 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002352 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002353
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07002354 if (conn->type == LE_LINK) {
2355 hdev->cur_adv_instance = conn->adv_instance;
2356 hci_req_reenable_advertising(hdev);
2357 }
2358
Joseph Hwangb8d29052020-03-11 19:20:14 -07002359 /* If the disconnection failed for any reason, the upper layer
2360 * does not retry to disconnect in current implementation.
2361 * Hence, we need to do some basic cleanup here and re-enable
2362 * advertising if necessary.
2363 */
2364 hci_conn_del(conn);
Joseph Hwangb8d29052020-03-11 19:20:14 -07002365 }
2366
Johan Hedberg88c3df12012-02-09 14:27:38 +02002367 hci_dev_unlock(hdev);
2368}
2369
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302370static void cs_le_create_conn(struct hci_dev *hdev, bdaddr_t *peer_addr,
2371 u8 peer_addr_type, u8 own_address_type,
2372 u8 filter_policy)
2373{
2374 struct hci_conn *conn;
2375
2376 conn = hci_conn_hash_lookup_le(hdev, peer_addr,
2377 peer_addr_type);
2378 if (!conn)
2379 return;
2380
Sathish Narasimmanb31bc002020-07-23 18:08:59 +05302381 /* When using controller based address resolution, then the new
2382 * address types 0x02 and 0x03 are used. These types need to be
2383 * converted back into either public address or random address type
2384 */
2385 if (use_ll_privacy(hdev) &&
2386 hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
2387 switch (own_address_type) {
2388 case ADDR_LE_DEV_PUBLIC_RESOLVED:
2389 own_address_type = ADDR_LE_DEV_PUBLIC;
2390 break;
2391 case ADDR_LE_DEV_RANDOM_RESOLVED:
2392 own_address_type = ADDR_LE_DEV_RANDOM;
2393 break;
2394 }
2395 }
2396
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302397 /* Store the initiator and responder address information which
2398 * is needed for SMP. These values will not change during the
2399 * lifetime of the connection.
2400 */
2401 conn->init_addr_type = own_address_type;
2402 if (own_address_type == ADDR_LE_DEV_RANDOM)
2403 bacpy(&conn->init_addr, &hdev->random_addr);
2404 else
2405 bacpy(&conn->init_addr, &hdev->bdaddr);
2406
2407 conn->resp_addr_type = peer_addr_type;
2408 bacpy(&conn->resp_addr, peer_addr);
2409
2410 /* We don't want the connection attempt to stick around
2411 * indefinitely since LE doesn't have a page timeout concept
2412 * like BR/EDR. Set a timer for any connection that doesn't use
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002413 * the accept list for connecting.
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302414 */
2415 if (filter_policy == HCI_LE_USE_PEER_ADDR)
2416 queue_delayed_work(conn->hdev->workqueue,
2417 &conn->le_conn_timeout,
2418 conn->conn_timeout);
2419}
2420
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002421static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
2422{
2423 struct hci_cp_le_create_conn *cp;
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002424
2425 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2426
2427 /* All connection failure handling is taken care of by the
2428 * hci_le_conn_failed function which is triggered by the HCI
2429 * request completion callbacks used for connecting.
2430 */
2431 if (status)
2432 return;
2433
2434 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
2435 if (!cp)
2436 return;
2437
2438 hci_dev_lock(hdev);
2439
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302440 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2441 cp->own_address_type, cp->filter_policy);
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002442
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002443 hci_dev_unlock(hdev);
2444}
2445
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02002446static void hci_cs_le_ext_create_conn(struct hci_dev *hdev, u8 status)
2447{
2448 struct hci_cp_le_ext_create_conn *cp;
2449
2450 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2451
2452 /* All connection failure handling is taken care of by the
2453 * hci_le_conn_failed function which is triggered by the HCI
2454 * request completion callbacks used for connecting.
2455 */
2456 if (status)
2457 return;
2458
2459 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_EXT_CREATE_CONN);
2460 if (!cp)
2461 return;
2462
2463 hci_dev_lock(hdev);
2464
2465 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2466 cp->own_addr_type, cp->filter_policy);
2467
2468 hci_dev_unlock(hdev);
2469}
2470
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07002471static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
2472{
2473 struct hci_cp_le_read_remote_features *cp;
2474 struct hci_conn *conn;
2475
2476 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2477
2478 if (!status)
2479 return;
2480
2481 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
2482 if (!cp)
2483 return;
2484
2485 hci_dev_lock(hdev);
2486
2487 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2488 if (conn) {
2489 if (conn->state == BT_CONFIG) {
2490 hci_connect_cfm(conn, status);
2491 hci_conn_drop(conn);
2492 }
2493 }
2494
2495 hci_dev_unlock(hdev);
2496}
2497
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02002498static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
2499{
2500 struct hci_cp_le_start_enc *cp;
2501 struct hci_conn *conn;
2502
2503 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2504
2505 if (!status)
2506 return;
2507
2508 hci_dev_lock(hdev);
2509
2510 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
2511 if (!cp)
2512 goto unlock;
2513
2514 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2515 if (!conn)
2516 goto unlock;
2517
2518 if (conn->state != BT_CONNECTED)
2519 goto unlock;
2520
2521 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2522 hci_conn_drop(conn);
2523
2524unlock:
2525 hci_dev_unlock(hdev);
2526}
2527
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01002528static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
2529{
2530 struct hci_cp_switch_role *cp;
2531 struct hci_conn *conn;
2532
2533 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2534
2535 if (!status)
2536 return;
2537
2538 cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
2539 if (!cp)
2540 return;
2541
2542 hci_dev_lock(hdev);
2543
2544 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2545 if (conn)
2546 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
2547
2548 hci_dev_unlock(hdev);
2549}
2550
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002551static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002552{
2553 __u8 status = *((__u8 *) skb->data);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002554 struct discovery_state *discov = &hdev->discovery;
2555 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002556
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002557 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002558
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002559 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03002560
2561 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
2562 return;
2563
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002564 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -03002565 wake_up_bit(&hdev->flags, HCI_INQUIRY);
2566
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002567 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002568 return;
2569
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002570 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002571
Andre Guedes343f9352012-02-17 20:39:37 -03002572 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002573 goto unlock;
2574
2575 if (list_empty(&discov->resolve)) {
Jakub Pawlowski07d23342015-03-17 09:04:14 -07002576 /* When BR/EDR inquiry is active and no LE scanning is in
2577 * progress, then change discovery state to indicate completion.
2578 *
2579 * When running LE scanning and BR/EDR inquiry simultaneously
2580 * and the LE scan already finished, then change the discovery
2581 * state to indicate completion.
2582 */
2583 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2584 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2585 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002586 goto unlock;
2587 }
2588
2589 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
2590 if (e && hci_resolve_name(hdev, e) == 0) {
2591 e->name_state = NAME_PENDING;
2592 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
2593 } else {
Jakub Pawlowski07d23342015-03-17 09:04:14 -07002594 /* When BR/EDR inquiry is active and no LE scanning is in
2595 * progress, then change discovery state to indicate completion.
2596 *
2597 * When running LE scanning and BR/EDR inquiry simultaneously
2598 * and the LE scan already finished, then change the discovery
2599 * state to indicate completion.
2600 */
2601 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2602 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2603 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002604 }
2605
2606unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002607 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002608}
2609
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002610static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002612 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002613 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 int num_rsp = *((__u8 *) skb->data);
2615
2616 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2617
Peilin Ye75bbd2e2020-07-10 17:39:18 -04002618 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002619 return;
2620
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002621 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03002622 return;
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002625
Johan Hedberge17acd42011-03-30 23:57:16 +03002626 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02002627 u32 flags;
Johan Hedberg31754052012-01-04 13:39:52 +02002628
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 bacpy(&data.bdaddr, &info->bdaddr);
2630 data.pscan_rep_mode = info->pscan_rep_mode;
2631 data.pscan_period_mode = info->pscan_period_mode;
2632 data.pscan_mode = info->pscan_mode;
2633 memcpy(data.dev_class, info->dev_class, 3);
2634 data.clock_offset = info->clock_offset;
Marcel Holtmannefb25132014-12-05 13:03:34 +01002635 data.rssi = HCI_RSSI_INVALID;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002636 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02002637
Marcel Holtmannaf589252014-07-01 14:11:20 +02002638 flags = hci_inquiry_cache_update(hdev, &data, false);
2639
Johan Hedberg48264f02011-11-09 13:58:58 +02002640 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannefb25132014-12-05 13:03:34 +01002641 info->dev_class, HCI_RSSI_INVALID,
2642 flags, NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002644
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 hci_dev_unlock(hdev);
2646}
2647
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002648static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002650 struct hci_ev_conn_complete *ev = (void *) skb->data;
2651 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002653 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002656
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002657 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02002658 if (!conn) {
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002659 /* Connection may not exist if auto-connected. Check the bredr
2660 * allowlist to see if this device is allowed to auto connect.
2661 * If link is an ACL type, create a connection class
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002662 * automatically.
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002663 *
2664 * Auto-connect will only occur if the event filter is
2665 * programmed with a given address. Right now, event filter is
2666 * only used during suspend.
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002667 */
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002668 if (ev->link_type == ACL_LINK &&
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002669 hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002670 &ev->bdaddr,
2671 BDADDR_BREDR)) {
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002672 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2673 HCI_ROLE_SLAVE);
2674 if (!conn) {
2675 bt_dev_err(hdev, "no memory for new conn");
2676 goto unlock;
2677 }
Abhishek Pandit-Subedi2d186fc2020-03-19 17:07:13 -07002678 } else {
2679 if (ev->link_type != SCO_LINK)
2680 goto unlock;
2681
2682 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK,
2683 &ev->bdaddr);
2684 if (!conn)
2685 goto unlock;
2686
2687 conn->type = SCO_LINK;
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002688 }
Marcel Holtmann94992372009-04-19 19:30:03 +02002689 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002690
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002691 if (!ev->status) {
2692 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02002693
2694 if (conn->type == ACL_LINK) {
2695 conn->state = BT_CONFIG;
2696 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02002697
2698 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
2699 !hci_find_link_key(hdev, &ev->bdaddr))
2700 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2701 else
2702 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02002703 } else
2704 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002705
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01002706 hci_debugfs_create_conn(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002707 hci_conn_add_sysfs(conn);
2708
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002709 if (test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002710 set_bit(HCI_CONN_AUTH, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002711
2712 if (test_bit(HCI_ENCRYPT, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002713 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002714
2715 /* Get remote features */
2716 if (conn->type == ACL_LINK) {
2717 struct hci_cp_read_remote_features cp;
2718 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02002719 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002720 sizeof(cp), &cp);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002721
Johan Hedberg01b1cb82015-11-16 12:52:21 +02002722 hci_req_update_scan(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002723 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002724
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002725 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02002726 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002727 struct hci_cp_change_conn_ptype cp;
2728 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02002729 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002730 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
2731 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002732 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02002733 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002734 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02002735 if (conn->type == ACL_LINK)
Marcel Holtmann64c7b772014-02-18 14:22:20 -08002736 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002737 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02002738 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002739
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002740 if (conn->type == ACL_LINK)
2741 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002742
Marcel Holtmann769be972008-07-14 20:13:49 +02002743 if (ev->status) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002744 hci_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002745 hci_conn_del(conn);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02002746 } else if (ev->link_type == SCO_LINK) {
2747 switch (conn->setting & SCO_AIRMODE_MASK) {
2748 case SCO_AIRMODE_CVSD:
2749 if (hdev->notify)
2750 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
2751 break;
2752 }
2753
Johan Hedberg539c4962015-02-18 14:53:57 +02002754 hci_connect_cfm(conn, ev->status);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02002755 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002756
2757unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002759
2760 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
Johan Hedberg70c46422014-07-09 12:59:17 +03002763static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
2764{
2765 struct hci_cp_reject_conn_req cp;
2766
2767 bacpy(&cp.bdaddr, bdaddr);
2768 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
2769 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
2770}
2771
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002772static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002774 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 int mask = hdev->link_mode;
Johan Hedberg70c46422014-07-09 12:59:17 +03002776 struct inquiry_entry *ie;
2777 struct hci_conn *conn;
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01002778 __u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002780 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002781 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01002783 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
2784 &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Johan Hedberg70c46422014-07-09 12:59:17 +03002786 if (!(mask & HCI_LM_ACCEPT)) {
2787 hci_reject_conn(hdev, &ev->bdaddr);
2788 return;
2789 }
2790
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002791 if (hci_bdaddr_list_lookup(&hdev->reject_list, &ev->bdaddr,
Johan Hedberg46c4c942014-07-16 16:19:21 +03002792 BDADDR_BREDR)) {
2793 hci_reject_conn(hdev, &ev->bdaddr);
2794 return;
2795 }
2796
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002797 /* Require HCI_CONNECTABLE or an accept list entry to accept the
Johan Hedberg6a8fc952014-12-24 20:43:11 +02002798 * connection. These features are only touched through mgmt so
2799 * only do the checks if HCI_MGMT is set.
2800 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002801 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
2802 !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002803 !hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, &ev->bdaddr,
Abhishek Pandit-Subedi8baaa402020-06-17 16:39:08 +02002804 BDADDR_BREDR)) {
2805 hci_reject_conn(hdev, &ev->bdaddr);
2806 return;
Johan Hedberg70c46422014-07-09 12:59:17 +03002807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Johan Hedberg70c46422014-07-09 12:59:17 +03002809 /* Connection accepted */
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002810
Johan Hedberg70c46422014-07-09 12:59:17 +03002811 hci_dev_lock(hdev);
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02002812
Johan Hedberg70c46422014-07-09 12:59:17 +03002813 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2814 if (ie)
2815 memcpy(ie->data.dev_class, ev->dev_class, 3);
2816
2817 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
2818 &ev->bdaddr);
2819 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03002820 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2821 HCI_ROLE_SLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002823 bt_dev_err(hdev, "no memory for new connection");
Johan Hedberg70c46422014-07-09 12:59:17 +03002824 hci_dev_unlock(hdev);
2825 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 }
Johan Hedberg70c46422014-07-09 12:59:17 +03002827 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002828
Johan Hedberg70c46422014-07-09 12:59:17 +03002829 memcpy(conn->dev_class, ev->dev_class, 3);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002830
Johan Hedberg70c46422014-07-09 12:59:17 +03002831 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
Johan Hedberg70c46422014-07-09 12:59:17 +03002833 if (ev->link_type == ACL_LINK ||
2834 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
2835 struct hci_cp_accept_conn_req cp;
2836 conn->state = BT_CONNECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837
2838 bacpy(&cp.bdaddr, &ev->bdaddr);
Johan Hedberg70c46422014-07-09 12:59:17 +03002839
2840 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
Archie Pusaka74be5232021-06-04 16:26:25 +08002841 cp.role = 0x00; /* Become central */
Johan Hedberg70c46422014-07-09 12:59:17 +03002842 else
Archie Pusaka74be5232021-06-04 16:26:25 +08002843 cp.role = 0x01; /* Remain peripheral */
Johan Hedberg70c46422014-07-09 12:59:17 +03002844
2845 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
2846 } else if (!(flags & HCI_PROTO_DEFER)) {
2847 struct hci_cp_accept_sync_conn_req cp;
2848 conn->state = BT_CONNECT;
2849
2850 bacpy(&cp.bdaddr, &ev->bdaddr);
2851 cp.pkt_type = cpu_to_le16(conn->pkt_type);
2852
2853 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
2854 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
2855 cp.max_latency = cpu_to_le16(0xffff);
2856 cp.content_format = cpu_to_le16(hdev->voice_setting);
2857 cp.retrans_effort = 0xff;
2858
2859 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
2860 &cp);
2861 } else {
2862 conn->state = BT_CONNECT2;
Johan Hedberg539c4962015-02-18 14:53:57 +02002863 hci_connect_cfm(conn, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865}
2866
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02002867static u8 hci_to_mgmt_reason(u8 err)
2868{
2869 switch (err) {
2870 case HCI_ERROR_CONNECTION_TIMEOUT:
2871 return MGMT_DEV_DISCONN_TIMEOUT;
2872 case HCI_ERROR_REMOTE_USER_TERM:
2873 case HCI_ERROR_REMOTE_LOW_RESOURCES:
2874 case HCI_ERROR_REMOTE_POWER_OFF:
2875 return MGMT_DEV_DISCONN_REMOTE;
2876 case HCI_ERROR_LOCAL_HOST_TERM:
2877 return MGMT_DEV_DISCONN_LOCAL_HOST;
2878 default:
2879 return MGMT_DEV_DISCONN_UNKNOWN;
2880 }
2881}
2882
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002883static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002885 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Szymon Janc160b9252016-07-12 02:12:16 +02002886 u8 reason;
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002887 struct hci_conn_params *params;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002888 struct hci_conn *conn;
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002889 bool mgmt_connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002891 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 hci_dev_lock(hdev);
2894
Marcel Holtmann04837f62006-07-03 10:02:33 +02002895 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02002896 if (!conn)
2897 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002898
Andre Guedesabf54a52013-11-07 17:36:09 -03002899 if (ev->status) {
2900 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2901 conn->dst_type, ev->status);
2902 goto unlock;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02002903 }
Johan Hedbergf7520542011-01-20 12:34:39 +02002904
Andre Guedes38462202013-11-07 17:36:10 -03002905 conn->state = BT_CLOSED;
2906
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002907 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
Szymon Janc160b9252016-07-12 02:12:16 +02002908
2909 if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
2910 reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
2911 else
2912 reason = hci_to_mgmt_reason(ev->reason);
2913
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002914 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2915 reason, mgmt_connected);
Andre Guedesabf54a52013-11-07 17:36:09 -03002916
Johan Hedberg22f433d2014-08-01 11:13:32 +03002917 if (conn->type == ACL_LINK) {
2918 if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2919 hci_remove_link_key(hdev, &conn->dst);
2920
Johan Hedberg01b1cb82015-11-16 12:52:21 +02002921 hci_req_update_scan(hdev);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002922 }
Johan Hedberg22102462013-10-05 12:01:06 +02002923
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002924 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2925 if (params) {
2926 switch (params->auto_connect) {
2927 case HCI_AUTO_CONN_LINK_LOSS:
2928 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2929 break;
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05002930 fallthrough;
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002931
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02002932 case HCI_AUTO_CONN_DIRECT:
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002933 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg418025d2014-07-04 12:37:24 +03002934 list_del_init(&params->action);
2935 list_add(&params->action, &hdev->pend_le_conns);
2936 hci_update_background_scan(hdev);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002937 break;
2938
2939 default:
2940 break;
2941 }
2942 }
2943
Johan Hedberg3a6d5762015-02-18 14:53:58 +02002944 hci_disconn_cfm(conn, ev->reason);
Andre Guedes38462202013-11-07 17:36:10 -03002945
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002946 /* The suspend notifier is waiting for all devices to disconnect so
2947 * clear the bit from pending tasks and inform the wait queue.
2948 */
2949 if (list_empty(&hdev->conn_hash.list) &&
2950 test_and_clear_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks)) {
2951 wake_up(&hdev->suspend_wait_q);
2952 }
2953
Andre Guedes38462202013-11-07 17:36:10 -03002954 /* Re-enable advertising if necessary, since it might
2955 * have been disabled by the connection. From the
2956 * HCI_LE_Set_Advertise_Enable command description in
2957 * the core specification (v4.0):
2958 * "The Controller shall continue advertising until the Host
2959 * issues an LE_Set_Advertise_Enable command with
2960 * Advertising_Enable set to 0x00 (Advertising is disabled)
2961 * or until a connection is created or until the Advertising
2962 * is timed out due to Directed Advertising."
2963 */
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07002964 if (conn->type == LE_LINK) {
2965 hdev->cur_adv_instance = conn->adv_instance;
Johan Hedbergf2252572015-11-18 12:49:20 +02002966 hci_req_reenable_advertising(hdev);
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07002967 }
2968
2969 hci_conn_del(conn);
Johan Hedbergf7520542011-01-20 12:34:39 +02002970
2971unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 hci_dev_unlock(hdev);
2973}
2974
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002975static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002976{
2977 struct hci_ev_auth_complete *ev = (void *) skb->data;
2978 struct hci_conn *conn;
2979
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002980 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002981
2982 hci_dev_lock(hdev);
2983
2984 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002985 if (!conn)
2986 goto unlock;
2987
2988 if (!ev->status) {
Szymon Janc160b9252016-07-12 02:12:16 +02002989 clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
2990
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002991 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03002992 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002993 bt_dev_info(hdev, "re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03002994 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03002995 set_bit(HCI_CONN_AUTH, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002996 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03002997 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002998 } else {
Szymon Janc160b9252016-07-12 02:12:16 +02002999 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3000 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3001
Johan Hedberge1e930f2014-09-08 17:09:49 -07003002 mgmt_auth_failed(conn, ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003003 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003004
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003005 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
3006 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003007
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003008 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02003009 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003010 struct hci_cp_set_conn_encrypt cp;
3011 cp.handle = ev->handle;
3012 cp.encrypt = 0x01;
3013 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03003014 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003015 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003016 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02003017 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003018 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003019 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003020 } else {
3021 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003022
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003023 hci_conn_hold(conn);
3024 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003025 hci_conn_drop(conn);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003026 }
3027
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003028 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003029 if (!ev->status) {
3030 struct hci_cp_set_conn_encrypt cp;
3031 cp.handle = ev->handle;
3032 cp.encrypt = 0x01;
3033 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03003034 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003035 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003036 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003037 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003038 }
3039 }
3040
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003041unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003042 hci_dev_unlock(hdev);
3043}
3044
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003045static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003046{
Johan Hedberg127178d2010-11-18 22:22:29 +02003047 struct hci_ev_remote_name *ev = (void *) skb->data;
3048 struct hci_conn *conn;
3049
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003050 BT_DBG("%s", hdev->name);
3051
3052 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02003053
3054 hci_dev_lock(hdev);
3055
3056 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003057
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003058 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergb644ba32012-01-17 21:48:47 +02003059 goto check_auth;
3060
3061 if (ev->status == 0)
3062 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003063 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02003064 else
3065 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
3066
3067check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07003068 if (!conn)
3069 goto unlock;
3070
3071 if (!hci_outgoing_auth_needed(hdev, conn))
3072 goto unlock;
3073
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003074 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003075 struct hci_cp_auth_requested cp;
Johan Hedberg977f8fc2014-07-17 15:35:39 +03003076
3077 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
3078
Johan Hedberg127178d2010-11-18 22:22:29 +02003079 cp.handle = __cpu_to_le16(conn->handle);
3080 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
3081 }
3082
Johan Hedberg79c6c702011-04-28 11:28:55 -07003083unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02003084 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003085}
3086
Johan Hedberg821f3762015-06-11 13:52:29 +03003087static void read_enc_key_size_complete(struct hci_dev *hdev, u8 status,
3088 u16 opcode, struct sk_buff *skb)
3089{
3090 const struct hci_rp_read_enc_key_size *rp;
3091 struct hci_conn *conn;
3092 u16 handle;
3093
3094 BT_DBG("%s status 0x%02x", hdev->name, status);
3095
3096 if (!skb || skb->len < sizeof(*rp)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003097 bt_dev_err(hdev, "invalid read key size response");
Johan Hedberg821f3762015-06-11 13:52:29 +03003098 return;
3099 }
3100
3101 rp = (void *)skb->data;
3102 handle = le16_to_cpu(rp->handle);
3103
3104 hci_dev_lock(hdev);
3105
3106 conn = hci_conn_hash_lookup_handle(hdev, handle);
3107 if (!conn)
3108 goto unlock;
3109
Alain Michaud32b50722020-03-25 14:48:34 +00003110 /* While unexpected, the read_enc_key_size command may fail. The most
3111 * secure approach is to then assume the key size is 0 to force a
3112 * disconnection.
Johan Hedberg821f3762015-06-11 13:52:29 +03003113 */
3114 if (rp->status) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003115 bt_dev_err(hdev, "failed to read key size for handle %u",
3116 handle);
Alain Michaud32b50722020-03-25 14:48:34 +00003117 conn->enc_key_size = 0;
Johan Hedberg821f3762015-06-11 13:52:29 +03003118 } else {
3119 conn->enc_key_size = rp->key_size;
3120 }
3121
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003122 hci_encrypt_cfm(conn, 0);
Johan Hedberg821f3762015-06-11 13:52:29 +03003123
3124unlock:
3125 hci_dev_unlock(hdev);
3126}
3127
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003128static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003129{
3130 struct hci_ev_encrypt_change *ev = (void *) skb->data;
3131 struct hci_conn *conn;
3132
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003133 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003134
3135 hci_dev_lock(hdev);
3136
3137 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003138 if (!conn)
3139 goto unlock;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003140
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003141 if (!ev->status) {
3142 if (ev->encrypt) {
3143 /* Encryption implies authentication */
Johan Hedberg4dae2792014-06-24 17:03:50 +03003144 set_bit(HCI_CONN_AUTH, &conn->flags);
3145 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003146 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003147
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08003148 /* P-256 authentication key implies FIPS */
3149 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
Johan Hedberg4dae2792014-06-24 17:03:50 +03003150 set_bit(HCI_CONN_FIPS, &conn->flags);
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08003151
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003152 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
3153 conn->type == LE_LINK)
3154 set_bit(HCI_CONN_AES_CCM, &conn->flags);
3155 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03003156 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003157 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
3158 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003159 }
3160
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07003161 /* We should disregard the current RPA and generate a new one
3162 * whenever the encryption procedure fails.
3163 */
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303164 if (ev->status && conn->type == LE_LINK) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07003165 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303166 hci_adv_instances_set_rpa_expired(hdev, true);
3167 }
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07003168
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003169 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3170
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07003171 /* Check link security requirements are met */
3172 if (!hci_conn_check_link_mode(conn))
3173 ev->status = HCI_ERROR_AUTH_FAILURE;
3174
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003175 if (ev->status && conn->state == BT_CONNECTED) {
Szymon Janc160b9252016-07-12 02:12:16 +02003176 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3177 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3178
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07003179 /* Notify upper layers so they can cleanup before
3180 * disconnecting.
3181 */
3182 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003183 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
3184 hci_conn_drop(conn);
3185 goto unlock;
3186 }
3187
Johan Hedberg821f3762015-06-11 13:52:29 +03003188 /* Try reading the encryption key size for encrypted ACL links */
3189 if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
3190 struct hci_cp_read_enc_key_size cp;
3191 struct hci_request req;
3192
3193 /* Only send HCI_Read_Encryption_Key_Size if the
3194 * controller really supports it. If it doesn't, assume
3195 * the default size (16).
3196 */
3197 if (!(hdev->commands[20] & 0x10)) {
3198 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3199 goto notify;
3200 }
3201
3202 hci_req_init(&req, hdev);
3203
3204 cp.handle = cpu_to_le16(conn->handle);
3205 hci_req_add(&req, HCI_OP_READ_ENC_KEY_SIZE, sizeof(cp), &cp);
3206
3207 if (hci_req_run_skb(&req, read_enc_key_size_complete)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003208 bt_dev_err(hdev, "sending read key size failed");
Johan Hedberg821f3762015-06-11 13:52:29 +03003209 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3210 goto notify;
3211 }
3212
3213 goto unlock;
3214 }
3215
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +05303216 /* Set the default Authenticated Payload Timeout after
3217 * an LE Link is established. As per Core Spec v5.0, Vol 2, Part B
3218 * Section 3.3, the HCI command WRITE_AUTH_PAYLOAD_TIMEOUT should be
3219 * sent when the link is active and Encryption is enabled, the conn
3220 * type can be either LE or ACL and controller must support LMP Ping.
3221 * Ensure for AES-CCM encryption as well.
3222 */
3223 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
3224 test_bit(HCI_CONN_AES_CCM, &conn->flags) &&
3225 ((conn->type == ACL_LINK && lmp_ping_capable(hdev)) ||
3226 (conn->type == LE_LINK && (hdev->le_features[0] & HCI_LE_PING)))) {
3227 struct hci_cp_write_auth_payload_to cp;
3228
3229 cp.handle = cpu_to_le16(conn->handle);
3230 cp.timeout = cpu_to_le16(hdev->auth_payload_timeout);
3231 hci_send_cmd(conn->hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO,
3232 sizeof(cp), &cp);
3233 }
3234
Johan Hedberg821f3762015-06-11 13:52:29 +03003235notify:
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003236 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003237
Gustavo Padovana7d77232012-05-13 03:20:07 -03003238unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003239 hci_dev_unlock(hdev);
3240}
3241
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003242static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
3243 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003244{
3245 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
3246 struct hci_conn *conn;
3247
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003248 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003249
3250 hci_dev_lock(hdev);
3251
3252 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3253 if (conn) {
3254 if (!ev->status)
Johan Hedberg4dae2792014-06-24 17:03:50 +03003255 set_bit(HCI_CONN_SECURE, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003256
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003257 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003258
3259 hci_key_change_cfm(conn, ev->status);
3260 }
3261
3262 hci_dev_unlock(hdev);
3263}
3264
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003265static void hci_remote_features_evt(struct hci_dev *hdev,
3266 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003267{
3268 struct hci_ev_remote_features *ev = (void *) skb->data;
3269 struct hci_conn *conn;
3270
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003271 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003272
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003273 hci_dev_lock(hdev);
3274
3275 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02003276 if (!conn)
3277 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02003278
Johan Hedbergccd556f2010-11-10 17:11:51 +02003279 if (!ev->status)
Johan Hedbergcad718e2013-04-17 15:00:51 +03003280 memcpy(conn->features[0], ev->features, 8);
Johan Hedbergccd556f2010-11-10 17:11:51 +02003281
3282 if (conn->state != BT_CONFIG)
3283 goto unlock;
3284
Szymon Jancac363cf2015-01-29 16:36:59 +01003285 if (!ev->status && lmp_ext_feat_capable(hdev) &&
3286 lmp_ext_feat_capable(conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003287 struct hci_cp_read_remote_ext_features cp;
3288 cp.handle = ev->handle;
3289 cp.page = 0x01;
3290 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003291 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02003292 goto unlock;
3293 }
3294
Johan Hedberg671267b2012-05-12 16:11:50 -03003295 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003296 struct hci_cp_remote_name_req cp;
3297 memset(&cp, 0, sizeof(cp));
3298 bacpy(&cp.bdaddr, &conn->dst);
3299 cp.pscan_rep_mode = 0x02;
3300 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003301 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07003302 mgmt_device_connected(hdev, conn, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02003303
Johan Hedberg127178d2010-11-18 22:22:29 +02003304 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003305 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02003306 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003307 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02003308 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003309
Johan Hedbergccd556f2010-11-10 17:11:51 +02003310unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003311 hci_dev_unlock(hdev);
3312}
3313
Kiran Kecb71f252021-08-16 05:07:47 +05303314static inline void handle_cmd_cnt_and_timer(struct hci_dev *hdev, u8 ncmd)
Manish Mandlikde75cd02021-04-29 10:24:22 -07003315{
Kiran Kecb71f252021-08-16 05:07:47 +05303316 cancel_delayed_work(&hdev->cmd_timer);
Manish Mandlikde75cd02021-04-29 10:24:22 -07003317
3318 if (!test_bit(HCI_RESET, &hdev->flags)) {
3319 if (ncmd) {
3320 cancel_delayed_work(&hdev->ncmd_timer);
3321 atomic_set(&hdev->cmd_cnt, 1);
3322 } else {
3323 schedule_delayed_work(&hdev->ncmd_timer,
3324 HCI_NCMD_TIMEOUT);
3325 }
3326 }
3327}
3328
Johan Hedberge62144872015-04-02 13:41:08 +03003329static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
3330 u16 *opcode, u8 *status,
3331 hci_req_complete_t *req_complete,
3332 hci_req_complete_skb_t *req_complete_skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003333{
3334 struct hci_ev_cmd_complete *ev = (void *) skb->data;
Johan Hedberge62144872015-04-02 13:41:08 +03003335
3336 *opcode = __le16_to_cpu(ev->opcode);
3337 *status = skb->data[sizeof(*ev)];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003338
3339 skb_pull(skb, sizeof(*ev));
3340
Johan Hedberge62144872015-04-02 13:41:08 +03003341 switch (*opcode) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003342 case HCI_OP_INQUIRY_CANCEL:
Sonny Sasakaadf1d692020-05-06 12:55:03 -07003343 hci_cc_inquiry_cancel(hdev, skb, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003344 break;
3345
Andre Guedes4d934832012-03-21 00:03:35 -03003346 case HCI_OP_PERIODIC_INQ:
3347 hci_cc_periodic_inq(hdev, skb);
3348 break;
3349
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003350 case HCI_OP_EXIT_PERIODIC_INQ:
3351 hci_cc_exit_periodic_inq(hdev, skb);
3352 break;
3353
3354 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
3355 hci_cc_remote_name_req_cancel(hdev, skb);
3356 break;
3357
3358 case HCI_OP_ROLE_DISCOVERY:
3359 hci_cc_role_discovery(hdev, skb);
3360 break;
3361
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02003362 case HCI_OP_READ_LINK_POLICY:
3363 hci_cc_read_link_policy(hdev, skb);
3364 break;
3365
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003366 case HCI_OP_WRITE_LINK_POLICY:
3367 hci_cc_write_link_policy(hdev, skb);
3368 break;
3369
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02003370 case HCI_OP_READ_DEF_LINK_POLICY:
3371 hci_cc_read_def_link_policy(hdev, skb);
3372 break;
3373
3374 case HCI_OP_WRITE_DEF_LINK_POLICY:
3375 hci_cc_write_def_link_policy(hdev, skb);
3376 break;
3377
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003378 case HCI_OP_RESET:
3379 hci_cc_reset(hdev, skb);
3380 break;
3381
Marcel Holtmannc2f0f972015-01-12 09:21:25 -08003382 case HCI_OP_READ_STORED_LINK_KEY:
3383 hci_cc_read_stored_link_key(hdev, skb);
3384 break;
3385
Marcel Holtmanna93661202015-01-12 09:21:28 -08003386 case HCI_OP_DELETE_STORED_LINK_KEY:
3387 hci_cc_delete_stored_link_key(hdev, skb);
3388 break;
3389
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003390 case HCI_OP_WRITE_LOCAL_NAME:
3391 hci_cc_write_local_name(hdev, skb);
3392 break;
3393
3394 case HCI_OP_READ_LOCAL_NAME:
3395 hci_cc_read_local_name(hdev, skb);
3396 break;
3397
3398 case HCI_OP_WRITE_AUTH_ENABLE:
3399 hci_cc_write_auth_enable(hdev, skb);
3400 break;
3401
3402 case HCI_OP_WRITE_ENCRYPT_MODE:
3403 hci_cc_write_encrypt_mode(hdev, skb);
3404 break;
3405
3406 case HCI_OP_WRITE_SCAN_ENABLE:
3407 hci_cc_write_scan_enable(hdev, skb);
3408 break;
3409
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -08003410 case HCI_OP_SET_EVENT_FLT:
3411 hci_cc_set_event_filter(hdev, skb);
3412 break;
3413
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003414 case HCI_OP_READ_CLASS_OF_DEV:
3415 hci_cc_read_class_of_dev(hdev, skb);
3416 break;
3417
3418 case HCI_OP_WRITE_CLASS_OF_DEV:
3419 hci_cc_write_class_of_dev(hdev, skb);
3420 break;
3421
3422 case HCI_OP_READ_VOICE_SETTING:
3423 hci_cc_read_voice_setting(hdev, skb);
3424 break;
3425
3426 case HCI_OP_WRITE_VOICE_SETTING:
3427 hci_cc_write_voice_setting(hdev, skb);
3428 break;
3429
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -07003430 case HCI_OP_READ_NUM_SUPPORTED_IAC:
3431 hci_cc_read_num_supported_iac(hdev, skb);
3432 break;
3433
Marcel Holtmann333140b2008-07-14 20:13:48 +02003434 case HCI_OP_WRITE_SSP_MODE:
3435 hci_cc_write_ssp_mode(hdev, skb);
3436 break;
3437
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08003438 case HCI_OP_WRITE_SC_SUPPORT:
3439 hci_cc_write_sc_support(hdev, skb);
3440 break;
3441
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +05303442 case HCI_OP_READ_AUTH_PAYLOAD_TO:
3443 hci_cc_read_auth_payload_timeout(hdev, skb);
3444 break;
3445
3446 case HCI_OP_WRITE_AUTH_PAYLOAD_TO:
3447 hci_cc_write_auth_payload_timeout(hdev, skb);
3448 break;
3449
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003450 case HCI_OP_READ_LOCAL_VERSION:
3451 hci_cc_read_local_version(hdev, skb);
3452 break;
3453
3454 case HCI_OP_READ_LOCAL_COMMANDS:
3455 hci_cc_read_local_commands(hdev, skb);
3456 break;
3457
3458 case HCI_OP_READ_LOCAL_FEATURES:
3459 hci_cc_read_local_features(hdev, skb);
3460 break;
3461
Andre Guedes971e3a42011-06-30 19:20:52 -03003462 case HCI_OP_READ_LOCAL_EXT_FEATURES:
3463 hci_cc_read_local_ext_features(hdev, skb);
3464 break;
3465
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003466 case HCI_OP_READ_BUFFER_SIZE:
3467 hci_cc_read_buffer_size(hdev, skb);
3468 break;
3469
3470 case HCI_OP_READ_BD_ADDR:
3471 hci_cc_read_bd_addr(hdev, skb);
3472 break;
3473
Marcel Holtmanna4790362020-04-03 21:44:04 +02003474 case HCI_OP_READ_LOCAL_PAIRING_OPTS:
3475 hci_cc_read_local_pairing_opts(hdev, skb);
3476 break;
3477
Johan Hedbergf332ec62013-03-15 17:07:11 -05003478 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
3479 hci_cc_read_page_scan_activity(hdev, skb);
3480 break;
3481
Johan Hedberg4a3ee762013-03-15 17:07:12 -05003482 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
3483 hci_cc_write_page_scan_activity(hdev, skb);
3484 break;
3485
Johan Hedbergf332ec62013-03-15 17:07:11 -05003486 case HCI_OP_READ_PAGE_SCAN_TYPE:
3487 hci_cc_read_page_scan_type(hdev, skb);
3488 break;
3489
Johan Hedberg4a3ee762013-03-15 17:07:12 -05003490 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
3491 hci_cc_write_page_scan_type(hdev, skb);
3492 break;
3493
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02003494 case HCI_OP_READ_DATA_BLOCK_SIZE:
3495 hci_cc_read_data_block_size(hdev, skb);
3496 break;
3497
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02003498 case HCI_OP_READ_FLOW_CONTROL_MODE:
3499 hci_cc_read_flow_control_mode(hdev, skb);
3500 break;
3501
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03003502 case HCI_OP_READ_LOCAL_AMP_INFO:
3503 hci_cc_read_local_amp_info(hdev, skb);
3504 break;
3505
Johan Hedberg33f35722014-06-28 17:54:06 +03003506 case HCI_OP_READ_CLOCK:
3507 hci_cc_read_clock(hdev, skb);
3508 break;
3509
Johan Hedbergd5859e22011-01-25 01:19:58 +02003510 case HCI_OP_READ_INQ_RSP_TX_POWER:
3511 hci_cc_read_inq_rsp_tx_power(hdev, skb);
3512 break;
3513
Alain Michaud00bce3f2020-03-05 16:14:59 +00003514 case HCI_OP_READ_DEF_ERR_DATA_REPORTING:
3515 hci_cc_read_def_err_data_reporting(hdev, skb);
3516 break;
3517
3518 case HCI_OP_WRITE_DEF_ERR_DATA_REPORTING:
3519 hci_cc_write_def_err_data_reporting(hdev, skb);
3520 break;
3521
Johan Hedberg980e1a52011-01-22 06:10:07 +02003522 case HCI_OP_PIN_CODE_REPLY:
3523 hci_cc_pin_code_reply(hdev, skb);
3524 break;
3525
3526 case HCI_OP_PIN_CODE_NEG_REPLY:
3527 hci_cc_pin_code_neg_reply(hdev, skb);
3528 break;
3529
Szymon Jancc35938b2011-03-22 13:12:21 +01003530 case HCI_OP_READ_LOCAL_OOB_DATA:
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003531 hci_cc_read_local_oob_data(hdev, skb);
3532 break;
3533
3534 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
3535 hci_cc_read_local_oob_ext_data(hdev, skb);
Szymon Jancc35938b2011-03-22 13:12:21 +01003536 break;
3537
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003538 case HCI_OP_LE_READ_BUFFER_SIZE:
3539 hci_cc_le_read_buffer_size(hdev, skb);
3540 break;
3541
Johan Hedberg60e77322013-01-22 14:01:59 +02003542 case HCI_OP_LE_READ_LOCAL_FEATURES:
3543 hci_cc_le_read_local_features(hdev, skb);
3544 break;
3545
Johan Hedberg8fa19092012-10-19 20:57:49 +03003546 case HCI_OP_LE_READ_ADV_TX_POWER:
3547 hci_cc_le_read_adv_tx_power(hdev, skb);
3548 break;
3549
Johan Hedberga5c29682011-02-19 12:05:57 -03003550 case HCI_OP_USER_CONFIRM_REPLY:
3551 hci_cc_user_confirm_reply(hdev, skb);
3552 break;
3553
3554 case HCI_OP_USER_CONFIRM_NEG_REPLY:
3555 hci_cc_user_confirm_neg_reply(hdev, skb);
3556 break;
3557
Brian Gix1143d452011-11-23 08:28:34 -08003558 case HCI_OP_USER_PASSKEY_REPLY:
3559 hci_cc_user_passkey_reply(hdev, skb);
3560 break;
3561
3562 case HCI_OP_USER_PASSKEY_NEG_REPLY:
3563 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02003564 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09003565
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08003566 case HCI_OP_LE_SET_RANDOM_ADDR:
3567 hci_cc_le_set_random_addr(hdev, skb);
3568 break;
3569
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01003570 case HCI_OP_LE_SET_ADV_ENABLE:
3571 hci_cc_le_set_adv_enable(hdev, skb);
3572 break;
3573
Marcel Holtmann533553f2014-03-21 12:18:10 -07003574 case HCI_OP_LE_SET_SCAN_PARAM:
3575 hci_cc_le_set_scan_param(hdev, skb);
3576 break;
3577
Andre Guedeseb9d91f2011-05-26 16:23:52 -03003578 case HCI_OP_LE_SET_SCAN_ENABLE:
3579 hci_cc_le_set_scan_enable(hdev, skb);
3580 break;
3581
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003582 case HCI_OP_LE_READ_ACCEPT_LIST_SIZE:
3583 hci_cc_le_read_accept_list_size(hdev, skb);
Johan Hedbergcf1d0812013-01-22 14:02:00 +02003584 break;
3585
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003586 case HCI_OP_LE_CLEAR_ACCEPT_LIST:
3587 hci_cc_le_clear_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08003588 break;
3589
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003590 case HCI_OP_LE_ADD_TO_ACCEPT_LIST:
3591 hci_cc_le_add_to_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08003592 break;
3593
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003594 case HCI_OP_LE_DEL_FROM_ACCEPT_LIST:
3595 hci_cc_le_del_from_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08003596 break;
3597
Johan Hedberg9b008c02013-01-22 14:02:01 +02003598 case HCI_OP_LE_READ_SUPPORTED_STATES:
3599 hci_cc_le_read_supported_states(hdev, skb);
3600 break;
3601
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01003602 case HCI_OP_LE_READ_DEF_DATA_LEN:
3603 hci_cc_le_read_def_data_len(hdev, skb);
3604 break;
3605
3606 case HCI_OP_LE_WRITE_DEF_DATA_LEN:
3607 hci_cc_le_write_def_data_len(hdev, skb);
3608 break;
3609
Ankit Navikb950aa82018-08-17 07:29:19 +05303610 case HCI_OP_LE_ADD_TO_RESOLV_LIST:
3611 hci_cc_le_add_to_resolv_list(hdev, skb);
3612 break;
3613
3614 case HCI_OP_LE_DEL_FROM_RESOLV_LIST:
3615 hci_cc_le_del_from_resolv_list(hdev, skb);
3616 break;
3617
Ankit Navik545f2592018-06-29 12:13:20 +05303618 case HCI_OP_LE_CLEAR_RESOLV_LIST:
3619 hci_cc_le_clear_resolv_list(hdev, skb);
3620 break;
3621
Ankit Navikcfdb0c22018-06-29 12:12:50 +05303622 case HCI_OP_LE_READ_RESOLV_LIST_SIZE:
3623 hci_cc_le_read_resolv_list_size(hdev, skb);
3624 break;
3625
Ankit Navikaa12af72018-08-07 13:16:35 +05303626 case HCI_OP_LE_SET_ADDR_RESOLV_ENABLE:
3627 hci_cc_le_set_addr_resolution_enable(hdev, skb);
3628 break;
3629
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01003630 case HCI_OP_LE_READ_MAX_DATA_LEN:
3631 hci_cc_le_read_max_data_len(hdev, skb);
3632 break;
3633
Andre Guedesf9b49302011-06-30 19:20:53 -03003634 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
3635 hci_cc_write_le_host_supported(hdev, skb);
3636 break;
3637
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02003638 case HCI_OP_LE_SET_ADV_PARAM:
3639 hci_cc_set_adv_param(hdev, skb);
3640 break;
3641
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02003642 case HCI_OP_READ_RSSI:
3643 hci_cc_read_rssi(hdev, skb);
3644 break;
3645
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02003646 case HCI_OP_READ_TX_POWER:
3647 hci_cc_read_tx_power(hdev, skb);
3648 break;
3649
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08003650 case HCI_OP_WRITE_SSP_DEBUG_MODE:
3651 hci_cc_write_ssp_debug_mode(hdev, skb);
3652 break;
3653
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05303654 case HCI_OP_LE_SET_EXT_SCAN_PARAMS:
3655 hci_cc_le_set_ext_scan_param(hdev, skb);
3656 break;
3657
3658 case HCI_OP_LE_SET_EXT_SCAN_ENABLE:
3659 hci_cc_le_set_ext_scan_enable(hdev, skb);
3660 break;
3661
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303662 case HCI_OP_LE_SET_DEFAULT_PHY:
3663 hci_cc_le_set_default_phy(hdev, skb);
3664 break;
3665
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05303666 case HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS:
3667 hci_cc_le_read_num_adv_sets(hdev, skb);
3668 break;
3669
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05303670 case HCI_OP_LE_SET_EXT_ADV_PARAMS:
3671 hci_cc_set_ext_adv_param(hdev, skb);
3672 break;
3673
3674 case HCI_OP_LE_SET_EXT_ADV_ENABLE:
3675 hci_cc_le_set_ext_adv_enable(hdev, skb);
3676 break;
3677
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303678 case HCI_OP_LE_SET_ADV_SET_RAND_ADDR:
3679 hci_cc_le_set_adv_set_random_addr(hdev, skb);
3680 break;
3681
Daniel Winkler7c395ea2020-12-03 12:12:51 -08003682 case HCI_OP_LE_READ_TRANSMIT_POWER:
3683 hci_cc_le_read_transmit_power(hdev, skb);
3684 break;
3685
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003686 default:
Johan Hedberge62144872015-04-02 13:41:08 +03003687 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003688 break;
3689 }
3690
Kiran Kecb71f252021-08-16 05:07:47 +05303691 handle_cmd_cnt_and_timer(hdev, ev->ncmd);
Johan Hedberg600b2152015-03-28 11:17:36 +02003692
Johan Hedberge62144872015-04-02 13:41:08 +03003693 hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
3694 req_complete_skb);
Johan Hedberg9238f362013-03-05 20:37:48 +02003695
João Paulo Rechi Vitaf80c5da2019-05-02 10:01:52 +08003696 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
3697 bt_dev_err(hdev,
3698 "unexpected event for opcode 0x%4.4x", *opcode);
3699 return;
3700 }
3701
Johan Hedberg600b2152015-03-28 11:17:36 +02003702 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3703 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003704}
3705
Johan Hedberge62144872015-04-02 13:41:08 +03003706static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb,
3707 u16 *opcode, u8 *status,
3708 hci_req_complete_t *req_complete,
3709 hci_req_complete_skb_t *req_complete_skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003710{
3711 struct hci_ev_cmd_status *ev = (void *) skb->data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003712
3713 skb_pull(skb, sizeof(*ev));
3714
Johan Hedberge62144872015-04-02 13:41:08 +03003715 *opcode = __le16_to_cpu(ev->opcode);
3716 *status = ev->status;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003717
Johan Hedberge62144872015-04-02 13:41:08 +03003718 switch (*opcode) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003719 case HCI_OP_INQUIRY:
3720 hci_cs_inquiry(hdev, ev->status);
3721 break;
3722
3723 case HCI_OP_CREATE_CONN:
3724 hci_cs_create_conn(hdev, ev->status);
3725 break;
3726
Kuba Pawlak9645c762014-11-06 19:36:53 +01003727 case HCI_OP_DISCONNECT:
3728 hci_cs_disconnect(hdev, ev->status);
3729 break;
3730
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003731 case HCI_OP_ADD_SCO:
3732 hci_cs_add_sco(hdev, ev->status);
3733 break;
3734
Marcel Holtmannf8558552008-07-14 20:13:49 +02003735 case HCI_OP_AUTH_REQUESTED:
3736 hci_cs_auth_requested(hdev, ev->status);
3737 break;
3738
3739 case HCI_OP_SET_CONN_ENCRYPT:
3740 hci_cs_set_conn_encrypt(hdev, ev->status);
3741 break;
3742
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003743 case HCI_OP_REMOTE_NAME_REQ:
3744 hci_cs_remote_name_req(hdev, ev->status);
3745 break;
3746
Marcel Holtmann769be972008-07-14 20:13:49 +02003747 case HCI_OP_READ_REMOTE_FEATURES:
3748 hci_cs_read_remote_features(hdev, ev->status);
3749 break;
3750
3751 case HCI_OP_READ_REMOTE_EXT_FEATURES:
3752 hci_cs_read_remote_ext_features(hdev, ev->status);
3753 break;
3754
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003755 case HCI_OP_SETUP_SYNC_CONN:
3756 hci_cs_setup_sync_conn(hdev, ev->status);
3757 break;
3758
3759 case HCI_OP_SNIFF_MODE:
3760 hci_cs_sniff_mode(hdev, ev->status);
3761 break;
3762
3763 case HCI_OP_EXIT_SNIFF_MODE:
3764 hci_cs_exit_sniff_mode(hdev, ev->status);
3765 break;
3766
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01003767 case HCI_OP_SWITCH_ROLE:
3768 hci_cs_switch_role(hdev, ev->status);
3769 break;
3770
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02003771 case HCI_OP_LE_CREATE_CONN:
3772 hci_cs_le_create_conn(hdev, ev->status);
3773 break;
3774
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07003775 case HCI_OP_LE_READ_REMOTE_FEATURES:
3776 hci_cs_le_read_remote_features(hdev, ev->status);
3777 break;
3778
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02003779 case HCI_OP_LE_START_ENC:
3780 hci_cs_le_start_enc(hdev, ev->status);
3781 break;
3782
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02003783 case HCI_OP_LE_EXT_CREATE_CONN:
3784 hci_cs_le_ext_create_conn(hdev, ev->status);
3785 break;
3786
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003787 default:
Johan Hedberge62144872015-04-02 13:41:08 +03003788 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003789 break;
3790 }
3791
Kiran Kecb71f252021-08-16 05:07:47 +05303792 handle_cmd_cnt_and_timer(hdev, ev->ncmd);
Johan Hedberg600b2152015-03-28 11:17:36 +02003793
Johan Hedberg444c6dd2015-04-02 13:41:07 +03003794 /* Indicate request completion if the command failed. Also, if
3795 * we're not waiting for a special event and we get a success
3796 * command status we should try to flag the request as completed
3797 * (since for this kind of commands there will not be a command
3798 * complete event).
3799 */
Johan Hedberg02350a72013-04-03 21:50:29 +03003800 if (ev->status ||
Marcel Holtmann242c0eb2015-10-25 22:45:53 +01003801 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->hci.req_event))
Johan Hedberge62144872015-04-02 13:41:08 +03003802 hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
3803 req_complete_skb);
Johan Hedberg9238f362013-03-05 20:37:48 +02003804
João Paulo Rechi Vitaf80c5da2019-05-02 10:01:52 +08003805 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
3806 bt_dev_err(hdev,
3807 "unexpected event for opcode 0x%4.4x", *opcode);
3808 return;
3809 }
3810
Johan Hedberg600b2152015-03-28 11:17:36 +02003811 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3812 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003813}
3814
Marcel Holtmann24dfa342014-11-02 02:56:41 +01003815static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
3816{
3817 struct hci_ev_hardware_error *ev = (void *) skb->data;
3818
Marcel Holtmannc7741d12015-01-28 11:09:55 -08003819 hdev->hw_error_code = ev->code;
3820
3821 queue_work(hdev->req_workqueue, &hdev->error_reset);
Marcel Holtmann24dfa342014-11-02 02:56:41 +01003822}
3823
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003824static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003825{
3826 struct hci_ev_role_change *ev = (void *) skb->data;
3827 struct hci_conn *conn;
3828
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003829 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003830
3831 hci_dev_lock(hdev);
3832
3833 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3834 if (conn) {
Johan Hedberg40bef302014-07-16 11:42:27 +03003835 if (!ev->status)
3836 conn->role = ev->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003837
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003838 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003839
3840 hci_role_switch_cfm(conn, ev->status, ev->role);
3841 }
3842
3843 hci_dev_unlock(hdev);
3844}
3845
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003846static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003848 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 int i;
3850
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02003851 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003852 bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02003853 return;
3854 }
3855
Gustavo A. R. Silva16e18342019-02-07 18:40:33 -06003856 if (skb->len < sizeof(*ev) ||
3857 skb->len < struct_size(ev, handles, ev->num_hndl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 BT_DBG("%s bad parameters", hdev->name);
3859 return;
3860 }
3861
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02003862 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
3863
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003864 for (i = 0; i < ev->num_hndl; i++) {
3865 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 struct hci_conn *conn;
3867 __u16 handle, count;
3868
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003869 handle = __le16_to_cpu(info->handle);
3870 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
3872 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003873 if (!conn)
3874 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003876 conn->sent -= count;
3877
3878 switch (conn->type) {
3879 case ACL_LINK:
3880 hdev->acl_cnt += count;
3881 if (hdev->acl_cnt > hdev->acl_pkts)
3882 hdev->acl_cnt = hdev->acl_pkts;
3883 break;
3884
3885 case LE_LINK:
3886 if (hdev->le_pkts) {
3887 hdev->le_cnt += count;
3888 if (hdev->le_cnt > hdev->le_pkts)
3889 hdev->le_cnt = hdev->le_pkts;
3890 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02003891 hdev->acl_cnt += count;
3892 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 hdev->acl_cnt = hdev->acl_pkts;
3894 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003895 break;
3896
3897 case SCO_LINK:
3898 hdev->sco_cnt += count;
3899 if (hdev->sco_cnt > hdev->sco_pkts)
3900 hdev->sco_cnt = hdev->sco_pkts;
3901 break;
3902
3903 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003904 bt_dev_err(hdev, "unknown type %d conn %p",
3905 conn->type, conn);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003906 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 }
3908 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003909
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02003910 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911}
3912
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003913static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
3914 __u16 handle)
3915{
3916 struct hci_chan *chan;
3917
3918 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02003919 case HCI_PRIMARY:
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003920 return hci_conn_hash_lookup_handle(hdev, handle);
3921 case HCI_AMP:
3922 chan = hci_chan_lookup_handle(hdev, handle);
3923 if (chan)
3924 return chan->conn;
3925 break;
3926 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003927 bt_dev_err(hdev, "unknown dev_type %d", hdev->dev_type);
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003928 break;
3929 }
3930
3931 return NULL;
3932}
3933
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003934static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003935{
3936 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
3937 int i;
3938
3939 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003940 bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003941 return;
3942 }
3943
Gustavo A. R. Silva16e18342019-02-07 18:40:33 -06003944 if (skb->len < sizeof(*ev) ||
3945 skb->len < struct_size(ev, handles, ev->num_hndl)) {
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003946 BT_DBG("%s bad parameters", hdev->name);
3947 return;
3948 }
3949
3950 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003951 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003952
3953 for (i = 0; i < ev->num_hndl; i++) {
3954 struct hci_comp_blocks_info *info = &ev->handles[i];
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003955 struct hci_conn *conn = NULL;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003956 __u16 handle, block_count;
3957
3958 handle = __le16_to_cpu(info->handle);
3959 block_count = __le16_to_cpu(info->blocks);
3960
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003961 conn = __hci_conn_lookup_handle(hdev, handle);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003962 if (!conn)
3963 continue;
3964
3965 conn->sent -= block_count;
3966
3967 switch (conn->type) {
3968 case ACL_LINK:
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03003969 case AMP_LINK:
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003970 hdev->block_cnt += block_count;
3971 if (hdev->block_cnt > hdev->num_blocks)
3972 hdev->block_cnt = hdev->num_blocks;
3973 break;
3974
3975 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003976 bt_dev_err(hdev, "unknown type %d conn %p",
3977 conn->type, conn);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003978 break;
3979 }
3980 }
3981
3982 queue_work(hdev->workqueue, &hdev->tx_work);
3983}
3984
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003985static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003987 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003988 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003990 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
3992 hci_dev_lock(hdev);
3993
Marcel Holtmann04837f62006-07-03 10:02:33 +02003994 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3995 if (conn) {
3996 conn->mode = ev->mode;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003997
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003998 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
3999 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02004000 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02004001 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004002 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02004003 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004004 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04004005
Johan Hedberg51a8efd2012-01-16 06:10:31 +02004006 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04004007 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004008 }
4009
4010 hci_dev_unlock(hdev);
4011}
4012
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004013static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004015 struct hci_ev_pin_code_req *ev = (void *) skb->data;
4016 struct hci_conn *conn;
4017
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004018 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004019
4020 hci_dev_lock(hdev);
4021
4022 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02004023 if (!conn)
4024 goto unlock;
4025
4026 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004027 hci_conn_hold(conn);
4028 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004029 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004030 }
4031
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004032 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedberg2f407f02014-07-17 15:35:40 +03004033 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
Johan Hedberg03b555e2011-01-04 15:40:05 +02004034 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004035 sizeof(ev->bdaddr), &ev->bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004036 } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02004037 u8 secure;
4038
4039 if (conn->pending_sec_level == BT_SECURITY_HIGH)
4040 secure = 1;
4041 else
4042 secure = 0;
4043
Johan Hedberg744cf192011-11-08 20:40:14 +02004044 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02004045 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02004046
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02004047unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004048 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049}
4050
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004051static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
4052{
4053 if (key_type == HCI_LK_CHANGED_COMBINATION)
4054 return;
4055
4056 conn->pin_length = pin_len;
4057 conn->key_type = key_type;
4058
4059 switch (key_type) {
4060 case HCI_LK_LOCAL_UNIT:
4061 case HCI_LK_REMOTE_UNIT:
4062 case HCI_LK_DEBUG_COMBINATION:
4063 return;
4064 case HCI_LK_COMBINATION:
4065 if (pin_len == 16)
4066 conn->pending_sec_level = BT_SECURITY_HIGH;
4067 else
4068 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4069 break;
4070 case HCI_LK_UNAUTH_COMBINATION_P192:
4071 case HCI_LK_UNAUTH_COMBINATION_P256:
4072 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4073 break;
4074 case HCI_LK_AUTH_COMBINATION_P192:
4075 conn->pending_sec_level = BT_SECURITY_HIGH;
4076 break;
4077 case HCI_LK_AUTH_COMBINATION_P256:
4078 conn->pending_sec_level = BT_SECURITY_FIPS;
4079 break;
4080 }
4081}
4082
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004083static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004085 struct hci_ev_link_key_req *ev = (void *) skb->data;
4086 struct hci_cp_link_key_reply cp;
4087 struct hci_conn *conn;
4088 struct link_key *key;
4089
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004090 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004091
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004092 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004093 return;
4094
4095 hci_dev_lock(hdev);
4096
4097 key = hci_find_link_key(hdev, &ev->bdaddr);
4098 if (!key) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03004099 BT_DBG("%s link key not found for %pMR", hdev->name,
4100 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004101 goto not_found;
4102 }
4103
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03004104 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
4105 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004106
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004107 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004108 if (conn) {
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03004109 clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
4110
Marcel Holtmann66138ce2014-01-10 02:07:20 -08004111 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
4112 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03004113 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004114 BT_DBG("%s ignoring unauthenticated key", hdev->name);
4115 goto not_found;
4116 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004117
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004118 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Johan Hedbergf3fb0b52014-06-02 10:12:44 +03004119 (conn->pending_sec_level == BT_SECURITY_HIGH ||
4120 conn->pending_sec_level == BT_SECURITY_FIPS)) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03004121 BT_DBG("%s ignoring key unauthenticated for high security",
4122 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004123 goto not_found;
4124 }
4125
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004126 conn_set_key(conn, key->type, key->pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004127 }
4128
4129 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03004130 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004131
4132 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
4133
4134 hci_dev_unlock(hdev);
4135
4136 return;
4137
4138not_found:
4139 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
4140 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004143static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004145 struct hci_ev_link_key_notify *ev = (void *) skb->data;
4146 struct hci_conn *conn;
Johan Hedberg7652ff62014-06-24 13:15:49 +03004147 struct link_key *key;
4148 bool persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004149 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004150
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004151 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004152
4153 hci_dev_lock(hdev);
4154
4155 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004156 if (!conn)
4157 goto unlock;
4158
4159 hci_conn_hold(conn);
4160 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
4161 hci_conn_drop(conn);
4162
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03004163 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004164 conn_set_key(conn, ev->key_type, conn->pin_length);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004165
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004166 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg7652ff62014-06-24 13:15:49 +03004167 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004168
Johan Hedberg7652ff62014-06-24 13:15:49 +03004169 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
4170 ev->key_type, pin_len, &persistent);
4171 if (!key)
4172 goto unlock;
4173
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004174 /* Update connection information since adding the key will have
4175 * fixed up the type in the case of changed combination keys.
4176 */
4177 if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
4178 conn_set_key(conn, key->type, key->pin_len);
4179
Johan Hedberg7652ff62014-06-24 13:15:49 +03004180 mgmt_new_link_key(hdev, key, persistent);
4181
Johan Hedberg6d5650c2014-06-24 13:15:51 +03004182 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
4183 * is set. If it's not set simply remove the key from the kernel
4184 * list (we've still notified user space about it but with
4185 * store_hint being 0).
4186 */
4187 if (key->type == HCI_LK_DEBUG_COMBINATION &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004188 !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg0378b592014-11-19 15:22:22 +02004189 list_del_rcu(&key->list);
4190 kfree_rcu(key, rcu);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004191 goto unlock;
Johan Hedberg6d5650c2014-06-24 13:15:51 +03004192 }
Johan Hedberg7652ff62014-06-24 13:15:49 +03004193
Johan Hedberg82c13d42014-12-03 11:03:06 +02004194 if (persistent)
4195 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4196 else
4197 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4198
Johan Hedberg7652ff62014-06-24 13:15:49 +03004199unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004200 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201}
4202
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004203static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02004204{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004205 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004206 struct hci_conn *conn;
4207
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004208 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004209
4210 hci_dev_lock(hdev);
4211
4212 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 if (conn && !ev->status) {
4214 struct inquiry_entry *ie;
4215
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004216 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4217 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 ie->data.clock_offset = ev->clock_offset;
4219 ie->timestamp = jiffies;
4220 }
4221 }
4222
4223 hci_dev_unlock(hdev);
4224}
4225
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004226static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02004227{
4228 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
4229 struct hci_conn *conn;
4230
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004231 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02004232
4233 hci_dev_lock(hdev);
4234
4235 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4236 if (conn && !ev->status)
4237 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
4238
4239 hci_dev_unlock(hdev);
4240}
4241
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004242static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004243{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004244 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004245 struct inquiry_entry *ie;
4246
4247 BT_DBG("%s", hdev->name);
4248
4249 hci_dev_lock(hdev);
4250
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004251 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4252 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004253 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
4254 ie->timestamp = jiffies;
4255 }
4256
4257 hci_dev_unlock(hdev);
4258}
4259
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004260static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
4261 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004262{
4263 struct inquiry_data data;
4264 int num_rsp = *((__u8 *) skb->data);
4265
4266 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
4267
4268 if (!num_rsp)
4269 return;
4270
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004271 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03004272 return;
4273
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004274 hci_dev_lock(hdev);
4275
4276 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01004277 struct inquiry_info_with_rssi_and_pscan_mode *info;
4278 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004279
Peilin Ye629b49c2020-07-10 17:45:26 -04004280 if (skb->len < num_rsp * sizeof(*info) + 1)
4281 goto unlock;
4282
Johan Hedberge17acd42011-03-30 23:57:16 +03004283 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004284 u32 flags;
4285
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004286 bacpy(&data.bdaddr, &info->bdaddr);
4287 data.pscan_rep_mode = info->pscan_rep_mode;
4288 data.pscan_period_mode = info->pscan_period_mode;
4289 data.pscan_mode = info->pscan_mode;
4290 memcpy(data.dev_class, info->dev_class, 3);
4291 data.clock_offset = info->clock_offset;
4292 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004293 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02004294
Marcel Holtmannaf589252014-07-01 14:11:20 +02004295 flags = hci_inquiry_cache_update(hdev, &data, false);
4296
Johan Hedberg48264f02011-11-09 13:58:58 +02004297 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004298 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004299 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004300 }
4301 } else {
4302 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
4303
Peilin Ye629b49c2020-07-10 17:45:26 -04004304 if (skb->len < num_rsp * sizeof(*info) + 1)
4305 goto unlock;
4306
Johan Hedberge17acd42011-03-30 23:57:16 +03004307 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004308 u32 flags;
4309
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004310 bacpy(&data.bdaddr, &info->bdaddr);
4311 data.pscan_rep_mode = info->pscan_rep_mode;
4312 data.pscan_period_mode = info->pscan_period_mode;
4313 data.pscan_mode = 0x00;
4314 memcpy(data.dev_class, info->dev_class, 3);
4315 data.clock_offset = info->clock_offset;
4316 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004317 data.ssp_mode = 0x00;
Marcel Holtmannaf589252014-07-01 14:11:20 +02004318
4319 flags = hci_inquiry_cache_update(hdev, &data, false);
4320
Johan Hedberg48264f02011-11-09 13:58:58 +02004321 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004322 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004323 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004324 }
4325 }
4326
Peilin Ye629b49c2020-07-10 17:45:26 -04004327unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004328 hci_dev_unlock(hdev);
4329}
4330
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004331static void hci_remote_ext_features_evt(struct hci_dev *hdev,
4332 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004333{
Marcel Holtmann41a96212008-07-14 20:13:48 +02004334 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
4335 struct hci_conn *conn;
4336
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004337 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004338
Marcel Holtmann41a96212008-07-14 20:13:48 +02004339 hci_dev_lock(hdev);
4340
4341 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02004342 if (!conn)
4343 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004344
Johan Hedbergcad718e2013-04-17 15:00:51 +03004345 if (ev->page < HCI_MAX_PAGES)
4346 memcpy(conn->features[ev->page], ev->features, 8);
4347
Johan Hedbergccd556f2010-11-10 17:11:51 +02004348 if (!ev->status && ev->page == 0x01) {
4349 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004350
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004351 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4352 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02004353 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02004354
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05304355 if (ev->features[0] & LMP_HOST_SSP) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02004356 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05304357 } else {
4358 /* It is mandatory by the Bluetooth specification that
4359 * Extended Inquiry Results are only used when Secure
4360 * Simple Pairing is enabled, but some devices violate
4361 * this.
4362 *
4363 * To make these devices work, the internal SSP
4364 * enabled flag needs to be cleared if the remote host
4365 * features do not indicate SSP support */
4366 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
4367 }
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -08004368
4369 if (ev->features[0] & LMP_HOST_SC)
4370 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004371 }
4372
Johan Hedbergccd556f2010-11-10 17:11:51 +02004373 if (conn->state != BT_CONFIG)
4374 goto unlock;
4375
Johan Hedberg671267b2012-05-12 16:11:50 -03004376 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02004377 struct hci_cp_remote_name_req cp;
4378 memset(&cp, 0, sizeof(cp));
4379 bacpy(&cp.bdaddr, &conn->dst);
4380 cp.pscan_rep_mode = 0x02;
4381 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02004382 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07004383 mgmt_device_connected(hdev, conn, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02004384
Johan Hedberg127178d2010-11-18 22:22:29 +02004385 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02004386 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02004387 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02004388 hci_conn_drop(conn);
Johan Hedbergccd556f2010-11-10 17:11:51 +02004389 }
4390
4391unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02004392 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004393}
4394
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004395static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
4396 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004397{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004398 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
4399 struct hci_conn *conn;
4400
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004401 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004402
4403 hci_dev_lock(hdev);
4404
4405 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004406 if (!conn) {
4407 if (ev->link_type == ESCO_LINK)
4408 goto unlock;
4409
Kuba Pawlak618353b2015-08-28 13:05:22 +01004410 /* When the link type in the event indicates SCO connection
4411 * and lookup of the connection object fails, then check
4412 * if an eSCO connection object exists.
4413 *
4414 * The core limits the synchronous connections to either
4415 * SCO or eSCO. The eSCO connection is preferred and tried
4416 * to be setup first and until successfully established,
4417 * the link type will be hinted as eSCO.
4418 */
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004419 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
4420 if (!conn)
4421 goto unlock;
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004422 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004423
Marcel Holtmann732547f2009-04-19 19:14:14 +02004424 switch (ev->status) {
4425 case 0x00:
Desmond Cheong Zhi Xi92fe24a2021-07-28 15:51:04 +08004426 /* The synchronous connection complete event should only be
4427 * sent once per new connection. Receiving a successful
4428 * complete event when the connection status is already
4429 * BT_CONNECTED means that the device is misbehaving and sent
4430 * multiple complete event packets for the same new connection.
4431 *
4432 * Registering the device more than once can corrupt kernel
4433 * memory, hence upon detecting this invalid event, we report
4434 * an error and ignore the packet.
4435 */
4436 if (conn->state == BT_CONNECTED) {
4437 bt_dev_err(hdev, "Ignoring connect complete event for existing connection");
4438 goto unlock;
4439 }
4440
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004441 conn->handle = __le16_to_cpu(ev->handle);
4442 conn->state = BT_CONNECTED;
Kuba Pawlak618353b2015-08-28 13:05:22 +01004443 conn->type = ev->link_type;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02004444
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01004445 hci_debugfs_create_conn(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02004446 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02004447 break;
4448
Nick Pelly81218d22014-06-30 11:25:01 +05304449 case 0x10: /* Connection Accept Timeout */
Frédéric Dalleau1a4c9582013-08-19 14:24:02 +02004450 case 0x0d: /* Connection Rejected due to Limited Resources */
Stephen Coe705e5712010-02-16 11:29:44 -05004451 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02004452 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08004453 case 0x1a: /* Unsupported Remote Feature */
Hsin-Yu Chao56b54532020-05-15 17:27:04 +08004454 case 0x1e: /* Invalid LMP Parameters */
Marcel Holtmann732547f2009-04-19 19:14:14 +02004455 case 0x1f: /* Unspecified error */
Andrew Earl27539bc2014-03-10 10:31:04 +00004456 case 0x20: /* Unsupported LMP Parameter value */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02004457 if (conn->out) {
Marcel Holtmann732547f2009-04-19 19:14:14 +02004458 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
4459 (hdev->esco_type & EDR_ESCO_MASK);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02004460 if (hci_setup_sync(conn, conn->link->handle))
4461 goto unlock;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004462 }
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05004463 fallthrough;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004464
4465 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004466 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004467 break;
4468 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004469
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02004470 bt_dev_dbg(hdev, "SCO connected with air mode: %02x", ev->air_mode);
4471
Kiran K06d213d82021-04-08 22:31:59 +05304472 switch (ev->air_mode) {
4473 case 0x02:
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02004474 if (hdev->notify)
4475 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
4476 break;
Kiran K06d213d82021-04-08 22:31:59 +05304477 case 0x03:
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02004478 if (hdev->notify)
4479 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_TRANSP);
4480 break;
4481 }
4482
Johan Hedberg539c4962015-02-18 14:53:57 +02004483 hci_connect_cfm(conn, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004484 if (ev->status)
4485 hci_conn_del(conn);
4486
4487unlock:
4488 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004489}
4490
Marcel Holtmannefdcf8e2013-10-15 10:31:12 -07004491static inline size_t eir_get_length(u8 *eir, size_t eir_len)
4492{
4493 size_t parsed = 0;
4494
4495 while (parsed < eir_len) {
4496 u8 field_len = eir[0];
4497
4498 if (field_len == 0)
4499 return parsed;
4500
4501 parsed += field_len + 1;
4502 eir += field_len + 1;
4503 }
4504
4505 return eir_len;
4506}
4507
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004508static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
4509 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004510{
4511 struct inquiry_data data;
4512 struct extended_inquiry_info *info = (void *) (skb->data + 1);
4513 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05304514 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004515
4516 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
4517
Peilin Ye51c19bf2020-07-10 12:09:15 -04004518 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004519 return;
4520
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004521 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03004522 return;
4523
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004524 hci_dev_lock(hdev);
4525
Johan Hedberge17acd42011-03-30 23:57:16 +03004526 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004527 u32 flags;
4528 bool name_known;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004529
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004530 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01004531 data.pscan_rep_mode = info->pscan_rep_mode;
4532 data.pscan_period_mode = info->pscan_period_mode;
4533 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004534 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01004535 data.clock_offset = info->clock_offset;
4536 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004537 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004538
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004539 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg0d3b7f62016-01-05 13:19:31 +02004540 name_known = eir_get_data(info->data,
4541 sizeof(info->data),
4542 EIR_NAME_COMPLETE, NULL);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004543 else
4544 name_known = true;
4545
Marcel Holtmannaf589252014-07-01 14:11:20 +02004546 flags = hci_inquiry_cache_update(hdev, &data, name_known);
4547
Vishal Agarwal9d939d92012-04-26 19:19:56 +05304548 eir_len = eir_get_length(info->data, sizeof(info->data));
Marcel Holtmannaf589252014-07-01 14:11:20 +02004549
Johan Hedberg48264f02011-11-09 13:58:58 +02004550 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004551 info->dev_class, info->rssi,
4552 flags, info->data, eir_len, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004553 }
4554
4555 hci_dev_unlock(hdev);
4556}
4557
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004558static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
4559 struct sk_buff *skb)
4560{
4561 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
4562 struct hci_conn *conn;
4563
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004564 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004565 __le16_to_cpu(ev->handle));
4566
4567 hci_dev_lock(hdev);
4568
4569 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4570 if (!conn)
4571 goto unlock;
4572
Johan Hedberg9eb1fbf2014-04-11 12:02:31 -07004573 /* For BR/EDR the necessary steps are taken through the
4574 * auth_complete event.
4575 */
4576 if (conn->type != LE_LINK)
4577 goto unlock;
4578
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004579 if (!ev->status)
4580 conn->sec_level = conn->pending_sec_level;
4581
4582 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
4583
4584 if (ev->status && conn->state == BT_CONNECTED) {
Andre Guedesbed71742013-01-30 11:50:56 -03004585 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
David Herrmann76a68ba2013-04-06 20:28:37 +02004586 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004587 goto unlock;
4588 }
4589
4590 if (conn->state == BT_CONFIG) {
4591 if (!ev->status)
4592 conn->state = BT_CONNECTED;
4593
Johan Hedberg539c4962015-02-18 14:53:57 +02004594 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02004595 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004596 } else {
4597 hci_auth_cfm(conn, ev->status);
4598
4599 hci_conn_hold(conn);
4600 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004601 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004602 }
4603
4604unlock:
4605 hci_dev_unlock(hdev);
4606}
4607
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004608static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004609{
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004610 /* If remote requests no-bonding follow that lead */
Mikel Astizacabae92013-06-28 10:56:28 +02004611 if (conn->remote_auth == HCI_AT_NO_BONDING ||
4612 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02004613 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004614
Mikel Astizb7f94c82014-04-08 14:21:31 +02004615 /* If both remote and local have enough IO capabilities, require
4616 * MITM protection
4617 */
4618 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
4619 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
4620 return conn->remote_auth | 0x01;
4621
Timo Mueller7e741702014-04-08 14:21:33 +02004622 /* No MITM protection possible so ignore remote requirement */
4623 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004624}
4625
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004626static u8 bredr_oob_data_present(struct hci_conn *conn)
4627{
4628 struct hci_dev *hdev = conn->hdev;
4629 struct oob_data *data;
4630
4631 data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
4632 if (!data)
4633 return 0x00;
4634
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004635 if (bredr_sc_enabled(hdev)) {
4636 /* When Secure Connections is enabled, then just
4637 * return the present value stored with the OOB
4638 * data. The stored value contains the right present
4639 * information. However it can only be trusted when
4640 * not in Secure Connection Only mode.
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004641 */
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004642 if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
4643 return data->present;
4644
4645 /* When Secure Connections Only mode is enabled, then
4646 * the P-256 values are required. If they are not
4647 * available, then do not declare that OOB data is
4648 * present.
4649 */
4650 if (!memcmp(data->rand256, ZERO_KEY, 16) ||
4651 !memcmp(data->hash256, ZERO_KEY, 16))
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004652 return 0x00;
4653
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004654 return 0x02;
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004655 }
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004656
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004657 /* When Secure Connections is not enabled or actually
4658 * not supported by the hardware, then check that if
4659 * P-192 data values are present.
4660 */
4661 if (!memcmp(data->rand192, ZERO_KEY, 16) ||
4662 !memcmp(data->hash192, ZERO_KEY, 16))
4663 return 0x00;
4664
4665 return 0x01;
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004666}
4667
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004668static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02004669{
4670 struct hci_ev_io_capa_request *ev = (void *) skb->data;
4671 struct hci_conn *conn;
4672
4673 BT_DBG("%s", hdev->name);
4674
4675 hci_dev_lock(hdev);
4676
4677 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004678 if (!conn)
4679 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02004680
Johan Hedberg03b555e2011-01-04 15:40:05 +02004681 hci_conn_hold(conn);
4682
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004683 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg03b555e2011-01-04 15:40:05 +02004684 goto unlock;
4685
Johan Hedberg2f407f02014-07-17 15:35:40 +03004686 /* Allow pairing if we're pairable, the initiators of the
4687 * pairing or if the remote is not requesting bonding.
4688 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004689 if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
Johan Hedberg2f407f02014-07-17 15:35:40 +03004690 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03004691 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004692 struct hci_cp_io_capability_reply cp;
4693
4694 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05304695 /* Change the IO capability from KeyboardDisplay
4696 * to DisplayYesNo as it is not supported by BT spec. */
4697 cp.capability = (conn->io_capability == 0x04) ?
Mikel Astiza7676312013-06-28 10:56:29 +02004698 HCI_IO_DISPLAY_YESNO : conn->io_capability;
Mikel Astizb7f94c82014-04-08 14:21:31 +02004699
4700 /* If we are initiators, there is no remote information yet */
4701 if (conn->remote_auth == 0xff) {
Mikel Astizb16c6602014-04-08 14:21:34 +02004702 /* Request MITM protection if our IO caps allow it
Johan Hedberg4ad51a72014-06-09 14:41:25 +03004703 * except for the no-bonding case.
Mikel Astizb16c6602014-04-08 14:21:34 +02004704 */
Mikel Astiz6fd6b912014-04-08 14:21:32 +02004705 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedberg9f743d72014-07-17 11:56:33 +03004706 conn->auth_type != HCI_AT_NO_BONDING)
Johan Hedberg6c538232014-07-11 15:32:23 +03004707 conn->auth_type |= 0x01;
Mikel Astizb7f94c82014-04-08 14:21:31 +02004708 } else {
4709 conn->auth_type = hci_get_auth_req(conn);
Mikel Astizb7f94c82014-04-08 14:21:31 +02004710 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004711
Johan Hedberg82c295b2014-07-30 09:22:24 +03004712 /* If we're not bondable, force one of the non-bondable
4713 * authentication requirement values.
4714 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004715 if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedberg82c295b2014-07-30 09:22:24 +03004716 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
4717
4718 cp.authentication = conn->auth_type;
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004719 cp.oob_data = bredr_oob_data_present(conn);
Szymon Jancce85ee12011-03-22 13:12:23 +01004720
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004721 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004722 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004723 } else {
4724 struct hci_cp_io_capability_neg_reply cp;
4725
4726 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02004727 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02004728
4729 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004730 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004731 }
4732
4733unlock:
4734 hci_dev_unlock(hdev);
4735}
4736
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004737static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02004738{
4739 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
4740 struct hci_conn *conn;
4741
4742 BT_DBG("%s", hdev->name);
4743
4744 hci_dev_lock(hdev);
4745
4746 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4747 if (!conn)
4748 goto unlock;
4749
Johan Hedberg03b555e2011-01-04 15:40:05 +02004750 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02004751 conn->remote_auth = ev->authentication;
4752
4753unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02004754 hci_dev_unlock(hdev);
4755}
4756
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004757static void hci_user_confirm_request_evt(struct hci_dev *hdev,
4758 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03004759{
4760 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004761 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07004762 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03004763
4764 BT_DBG("%s", hdev->name);
4765
4766 hci_dev_lock(hdev);
4767
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004768 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg7a828902011-04-28 11:28:53 -07004769 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03004770
Johan Hedberg7a828902011-04-28 11:28:53 -07004771 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4772 if (!conn)
4773 goto unlock;
4774
4775 loc_mitm = (conn->auth_type & 0x01);
4776 rem_mitm = (conn->remote_auth & 0x01);
4777
4778 /* If we require MITM but the remote device can't provide that
Johan Hedberg6c538232014-07-11 15:32:23 +03004779 * (it has NoInputNoOutput) then reject the confirmation
4780 * request. We check the security level here since it doesn't
4781 * necessarily match conn->auth_type.
Mikel Astiz6fd6b912014-04-08 14:21:32 +02004782 */
Johan Hedberg6c538232014-07-11 15:32:23 +03004783 if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
4784 conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
Johan Hedberg7a828902011-04-28 11:28:53 -07004785 BT_DBG("Rejecting request: remote device can't provide MITM");
4786 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004787 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07004788 goto unlock;
4789 }
4790
4791 /* If no side requires MITM protection; auto-accept */
Mikel Astiza7676312013-06-28 10:56:29 +02004792 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
4793 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004794
4795 /* If we're not the initiators request authorization to
4796 * proceed from user space (mgmt_user_confirm with
Johan Hedbergba15a582014-06-09 13:58:14 +03004797 * confirm_hint set to 1). The exception is if neither
Johan Hedberg02f3e252014-07-16 15:09:13 +03004798 * side had MITM or if the local IO capability is
4799 * NoInputNoOutput, in which case we do auto-accept
Johan Hedbergba15a582014-06-09 13:58:14 +03004800 */
4801 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
Johan Hedberg02f3e252014-07-16 15:09:13 +03004802 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedbergba15a582014-06-09 13:58:14 +03004803 (loc_mitm || rem_mitm)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004804 BT_DBG("Confirming auto-accept as acceptor");
4805 confirm_hint = 1;
4806 goto confirm;
4807 }
4808
Howard Chungcee5f202020-02-14 19:16:41 +08004809 /* If there already exists link key in local host, leave the
4810 * decision to user space since the remote device could be
4811 * legitimate or malicious.
4812 */
4813 if (hci_find_link_key(hdev, &ev->bdaddr)) {
4814 bt_dev_dbg(hdev, "Local host already has link key");
4815 confirm_hint = 1;
4816 goto confirm;
4817 }
4818
Johan Hedberg9f616562011-04-28 11:28:54 -07004819 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03004820 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07004821
4822 if (hdev->auto_accept_delay > 0) {
4823 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03004824 queue_delayed_work(conn->hdev->workqueue,
4825 &conn->auto_accept_work, delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07004826 goto unlock;
4827 }
4828
Johan Hedberg7a828902011-04-28 11:28:53 -07004829 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004830 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07004831 goto unlock;
4832 }
4833
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004834confirm:
Johan Hedberg39adbff2014-03-20 08:18:14 +02004835 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
4836 le32_to_cpu(ev->passkey), confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07004837
4838unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03004839 hci_dev_unlock(hdev);
4840}
4841
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004842static void hci_user_passkey_request_evt(struct hci_dev *hdev,
4843 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08004844{
4845 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
4846
4847 BT_DBG("%s", hdev->name);
4848
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004849 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg272d90d2012-02-09 15:26:12 +02004850 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08004851}
4852
Johan Hedberg92a25252012-09-06 18:39:26 +03004853static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
4854 struct sk_buff *skb)
4855{
4856 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
4857 struct hci_conn *conn;
4858
4859 BT_DBG("%s", hdev->name);
4860
4861 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4862 if (!conn)
4863 return;
4864
4865 conn->passkey_notify = __le32_to_cpu(ev->passkey);
4866 conn->passkey_entered = 0;
4867
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004868 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg92a25252012-09-06 18:39:26 +03004869 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4870 conn->dst_type, conn->passkey_notify,
4871 conn->passkey_entered);
4872}
4873
4874static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
4875{
4876 struct hci_ev_keypress_notify *ev = (void *) skb->data;
4877 struct hci_conn *conn;
4878
4879 BT_DBG("%s", hdev->name);
4880
4881 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4882 if (!conn)
4883 return;
4884
4885 switch (ev->type) {
4886 case HCI_KEYPRESS_STARTED:
4887 conn->passkey_entered = 0;
4888 return;
4889
4890 case HCI_KEYPRESS_ENTERED:
4891 conn->passkey_entered++;
4892 break;
4893
4894 case HCI_KEYPRESS_ERASED:
4895 conn->passkey_entered--;
4896 break;
4897
4898 case HCI_KEYPRESS_CLEARED:
4899 conn->passkey_entered = 0;
4900 break;
4901
4902 case HCI_KEYPRESS_COMPLETED:
4903 return;
4904 }
4905
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004906 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg92a25252012-09-06 18:39:26 +03004907 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4908 conn->dst_type, conn->passkey_notify,
4909 conn->passkey_entered);
4910}
4911
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004912static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
4913 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02004914{
4915 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
4916 struct hci_conn *conn;
4917
4918 BT_DBG("%s", hdev->name);
4919
4920 hci_dev_lock(hdev);
4921
4922 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03004923 if (!conn)
4924 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02004925
Johan Hedbergc1d4fa72014-07-17 15:14:50 +03004926 /* Reset the authentication requirement to unknown */
4927 conn->remote_auth = 0xff;
4928
Johan Hedberg2a611692011-02-19 12:06:00 -03004929 /* To avoid duplicate auth_failed events to user space we check
4930 * the HCI_CONN_AUTH_PEND flag which will be set if we
4931 * initiated the authentication. A traditional auth_complete
4932 * event gets always produced as initiator and is also mapped to
4933 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02004934 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedberge1e930f2014-09-08 17:09:49 -07004935 mgmt_auth_failed(conn, ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03004936
David Herrmann76a68ba2013-04-06 20:28:37 +02004937 hci_conn_drop(conn);
Johan Hedberg2a611692011-02-19 12:06:00 -03004938
4939unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02004940 hci_dev_unlock(hdev);
4941}
4942
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004943static void hci_remote_host_features_evt(struct hci_dev *hdev,
4944 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02004945{
4946 struct hci_ev_remote_host_features *ev = (void *) skb->data;
4947 struct inquiry_entry *ie;
Johan Hedbergcad718e2013-04-17 15:00:51 +03004948 struct hci_conn *conn;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004949
4950 BT_DBG("%s", hdev->name);
4951
4952 hci_dev_lock(hdev);
4953
Johan Hedbergcad718e2013-04-17 15:00:51 +03004954 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4955 if (conn)
4956 memcpy(conn->features[1], ev->features, 8);
4957
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004958 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4959 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02004960 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004961
4962 hci_dev_unlock(hdev);
4963}
4964
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004965static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
4966 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01004967{
4968 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
4969 struct oob_data *data;
4970
4971 BT_DBG("%s", hdev->name);
4972
4973 hci_dev_lock(hdev);
4974
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004975 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Szymon Jance1ba1f12011-04-06 13:01:59 +02004976 goto unlock;
4977
Johan Hedberg6928a922014-10-26 20:46:09 +01004978 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
Marcel Holtmann6665d052015-01-27 16:04:31 -08004979 if (!data) {
Szymon Janc2763eda2011-03-22 13:12:22 +01004980 struct hci_cp_remote_oob_data_neg_reply cp;
4981
4982 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004983 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
4984 sizeof(cp), &cp);
Marcel Holtmann6665d052015-01-27 16:04:31 -08004985 goto unlock;
4986 }
4987
4988 if (bredr_sc_enabled(hdev)) {
4989 struct hci_cp_remote_oob_ext_data_reply cp;
4990
4991 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004992 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmann6665d052015-01-27 16:04:31 -08004993 memset(cp.hash192, 0, sizeof(cp.hash192));
4994 memset(cp.rand192, 0, sizeof(cp.rand192));
4995 } else {
4996 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
4997 memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
4998 }
4999 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
5000 memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
5001
5002 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
5003 sizeof(cp), &cp);
5004 } else {
5005 struct hci_cp_remote_oob_data_reply cp;
5006
5007 bacpy(&cp.bdaddr, &ev->bdaddr);
5008 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
5009 memcpy(cp.rand, data->rand192, sizeof(cp.rand));
5010
5011 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
5012 sizeof(cp), &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01005013 }
5014
Szymon Jance1ba1f12011-04-06 13:01:59 +02005015unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01005016 hci_dev_unlock(hdev);
5017}
5018
Arron Wanga77a6a12015-07-24 17:13:15 +08005019#if IS_ENABLED(CONFIG_BT_HS)
5020static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
5021{
5022 struct hci_ev_channel_selected *ev = (void *)skb->data;
5023 struct hci_conn *hcon;
5024
5025 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
5026
5027 skb_pull(skb, sizeof(*ev));
5028
5029 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5030 if (!hcon)
5031 return;
5032
5033 amp_read_loc_assoc_final_data(hdev, hcon);
5034}
5035
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005036static void hci_phy_link_complete_evt(struct hci_dev *hdev,
5037 struct sk_buff *skb)
5038{
5039 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
5040 struct hci_conn *hcon, *bredr_hcon;
5041
5042 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
5043 ev->status);
5044
5045 hci_dev_lock(hdev);
5046
5047 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005048 if (!hcon)
5049 goto unlock;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005050
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005051 if (!hcon->amp_mgr)
5052 goto unlock;
Anmol Karn6dfccd12020-09-30 19:48:13 +05305053
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005054 if (ev->status) {
5055 hci_conn_del(hcon);
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005056 goto unlock;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005057 }
5058
5059 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
5060
5061 hcon->state = BT_CONNECTED;
5062 bacpy(&hcon->dst, &bredr_hcon->dst);
5063
5064 hci_conn_hold(hcon);
5065 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02005066 hci_conn_drop(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005067
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01005068 hci_debugfs_create_conn(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005069 hci_conn_add_sysfs(hcon);
5070
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +02005071 amp_physical_cfm(bredr_hcon, hcon);
5072
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005073unlock:
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005074 hci_dev_unlock(hdev);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005075}
5076
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005077static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
5078{
5079 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
5080 struct hci_conn *hcon;
5081 struct hci_chan *hchan;
5082 struct amp_mgr *mgr;
5083
5084 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
5085 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
5086 ev->status);
5087
5088 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5089 if (!hcon)
5090 return;
5091
5092 /* Create AMP hchan */
5093 hchan = hci_chan_create(hcon);
5094 if (!hchan)
5095 return;
5096
5097 hchan->handle = le16_to_cpu(ev->handle);
Archie Pusaka5c4c8c92021-03-22 14:03:11 +08005098 hchan->amp = true;
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005099
5100 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
5101
5102 mgr = hcon->amp_mgr;
5103 if (mgr && mgr->bredr_chan) {
5104 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
5105
5106 l2cap_chan_lock(bredr_chan);
5107
5108 bredr_chan->conn->mtu = hdev->block_mtu;
5109 l2cap_logical_cfm(bredr_chan, hchan, 0);
5110 hci_conn_hold(hcon);
5111
5112 l2cap_chan_unlock(bredr_chan);
5113 }
5114}
5115
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005116static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
5117 struct sk_buff *skb)
5118{
5119 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
5120 struct hci_chan *hchan;
5121
5122 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
5123 le16_to_cpu(ev->handle), ev->status);
5124
5125 if (ev->status)
5126 return;
5127
5128 hci_dev_lock(hdev);
5129
5130 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
Archie Pusaka5c4c8c92021-03-22 14:03:11 +08005131 if (!hchan || !hchan->amp)
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005132 goto unlock;
5133
5134 amp_destroy_logical_link(hchan, ev->reason);
5135
5136unlock:
5137 hci_dev_unlock(hdev);
5138}
5139
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005140static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
5141 struct sk_buff *skb)
5142{
5143 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
5144 struct hci_conn *hcon;
5145
5146 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5147
5148 if (ev->status)
5149 return;
5150
5151 hci_dev_lock(hdev);
5152
5153 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5154 if (hcon) {
5155 hcon->state = BT_CLOSED;
5156 hci_conn_del(hcon);
5157 }
5158
5159 hci_dev_unlock(hdev);
5160}
Arron Wanga77a6a12015-07-24 17:13:15 +08005161#endif
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005162
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005163static void le_conn_update_addr(struct hci_conn *conn, bdaddr_t *bdaddr,
5164 u8 bdaddr_type, bdaddr_t *local_rpa)
5165{
5166 if (conn->out) {
5167 conn->dst_type = bdaddr_type;
5168 conn->resp_addr_type = bdaddr_type;
5169 bacpy(&conn->resp_addr, bdaddr);
5170
5171 /* Check if the controller has set a Local RPA then it must be
5172 * used instead or hdev->rpa.
5173 */
5174 if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5175 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5176 bacpy(&conn->init_addr, local_rpa);
5177 } else if (hci_dev_test_flag(conn->hdev, HCI_PRIVACY)) {
5178 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5179 bacpy(&conn->init_addr, &conn->hdev->rpa);
5180 } else {
5181 hci_copy_identity_address(conn->hdev, &conn->init_addr,
5182 &conn->init_addr_type);
5183 }
5184 } else {
5185 conn->resp_addr_type = conn->hdev->adv_addr_type;
5186 /* Check if the controller has set a Local RPA then it must be
5187 * used instead or hdev->rpa.
5188 */
5189 if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5190 conn->resp_addr_type = ADDR_LE_DEV_RANDOM;
5191 bacpy(&conn->resp_addr, local_rpa);
5192 } else if (conn->hdev->adv_addr_type == ADDR_LE_DEV_RANDOM) {
5193 /* In case of ext adv, resp_addr will be updated in
5194 * Adv Terminated event.
5195 */
5196 if (!ext_adv_capable(conn->hdev))
5197 bacpy(&conn->resp_addr,
5198 &conn->hdev->random_addr);
5199 } else {
5200 bacpy(&conn->resp_addr, &conn->hdev->bdaddr);
5201 }
5202
5203 conn->init_addr_type = bdaddr_type;
5204 bacpy(&conn->init_addr, bdaddr);
5205
5206 /* For incoming connections, set the default minimum
5207 * and maximum connection interval. They will be used
5208 * to check if the parameters are in range and if not
5209 * trigger the connection update procedure.
5210 */
5211 conn->le_conn_min_interval = conn->hdev->le_conn_min_interval;
5212 conn->le_conn_max_interval = conn->hdev->le_conn_max_interval;
5213 }
5214}
5215
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305216static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005217 bdaddr_t *bdaddr, u8 bdaddr_type,
5218 bdaddr_t *local_rpa, u8 role, u16 handle,
5219 u16 interval, u16 latency,
5220 u16 supervision_timeout)
Ville Tervofcd89c02011-02-10 22:38:47 -03005221{
Johan Hedberg912b42ef2014-07-03 19:33:49 +03005222 struct hci_conn_params *params;
Ville Tervofcd89c02011-02-10 22:38:47 -03005223 struct hci_conn *conn;
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02005224 struct smp_irk *irk;
Johan Hedberg837d5022014-07-02 09:36:22 +03005225 u8 addr_type;
Ville Tervofcd89c02011-02-10 22:38:47 -03005226
Ville Tervofcd89c02011-02-10 22:38:47 -03005227 hci_dev_lock(hdev);
5228
Johan Hedbergfbd96c12014-07-08 17:21:51 +03005229 /* All controllers implicitly stop advertising in the event of a
5230 * connection, so ensure that the state bit is cleared.
5231 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005232 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedbergfbd96c12014-07-08 17:21:51 +03005233
Jakub Pawlowskie7d9ab72015-08-07 20:22:52 +02005234 conn = hci_lookup_le_connect(hdev);
Ville Tervob62f3282011-02-10 22:38:50 -03005235 if (!conn) {
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305236 conn = hci_conn_add(hdev, LE_LINK, bdaddr, role);
Ville Tervob62f3282011-02-10 22:38:50 -03005237 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005238 bt_dev_err(hdev, "no memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03005239 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03005240 }
Andre Guedes29b79882011-05-31 14:20:54 -03005241
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305242 conn->dst_type = bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03005243
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005244 /* If we didn't have a hci_conn object previously
Archie Pusaka74be5232021-06-04 16:26:25 +08005245 * but we're in central role this must be something
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005246 * initiated using an accept list. Since accept list based
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005247 * connections are not "first class citizens" we don't
5248 * have full tracking of them. Therefore, we go ahead
5249 * with a "best effort" approach of determining the
5250 * initiator address based on the HCI_PRIVACY flag.
5251 */
5252 if (conn->out) {
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305253 conn->resp_addr_type = bdaddr_type;
5254 bacpy(&conn->resp_addr, bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005255 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005256 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5257 bacpy(&conn->init_addr, &hdev->rpa);
5258 } else {
5259 hci_copy_identity_address(hdev,
5260 &conn->init_addr,
5261 &conn->init_addr_type);
5262 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005263 }
Johan Hedberg9489eca2014-02-28 17:45:46 +02005264 } else {
5265 cancel_delayed_work(&conn->le_conn_timeout);
Ville Tervob62f3282011-02-10 22:38:50 -03005266 }
Ville Tervofcd89c02011-02-10 22:38:47 -03005267
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005268 le_conn_update_addr(conn, bdaddr, bdaddr_type, local_rpa);
Johan Hedberg7be2edb2014-02-23 19:42:17 +02005269
Marcel Holtmannedb4b462014-02-18 15:13:43 -08005270 /* Lookup the identity address from the stored connection
5271 * address and address type.
5272 *
5273 * When establishing connections to an identity address, the
5274 * connection procedure will store the resolvable random
5275 * address first. Now if it can be converted back into the
5276 * identity address, start using the identity address from
5277 * now on.
5278 */
5279 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02005280 if (irk) {
5281 bacpy(&conn->dst, &irk->bdaddr);
5282 conn->dst_type = irk->addr_type;
5283 }
5284
Sathish Narasimman79699a72021-05-20 17:12:01 +05305285 /* When using controller based address resolution, then the new
5286 * address types 0x02 and 0x03 are used. These types need to be
5287 * converted back into either public address or random address type
5288 */
5289 if (use_ll_privacy(hdev) &&
5290 hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
5291 hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
5292 switch (conn->dst_type) {
5293 case ADDR_LE_DEV_PUBLIC_RESOLVED:
5294 conn->dst_type = ADDR_LE_DEV_PUBLIC;
5295 break;
5296 case ADDR_LE_DEV_RANDOM_RESOLVED:
5297 conn->dst_type = ADDR_LE_DEV_RANDOM;
5298 break;
5299 }
5300 }
5301
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305302 if (status) {
5303 hci_le_conn_failed(conn, status);
Johan Hedberg837d5022014-07-02 09:36:22 +03005304 goto unlock;
5305 }
5306
Johan Hedberg08853f12014-08-15 21:06:55 +03005307 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
5308 addr_type = BDADDR_LE_PUBLIC;
5309 else
5310 addr_type = BDADDR_LE_RANDOM;
5311
Johan Hedberg2d3c2262014-07-15 11:51:28 +03005312 /* Drop the connection if the device is blocked */
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005313 if (hci_bdaddr_list_lookup(&hdev->reject_list, &conn->dst, addr_type)) {
Johan Hedberg2d3c2262014-07-15 11:51:28 +03005314 hci_conn_drop(conn);
Andre Guedescd17dec2012-07-27 15:10:16 -03005315 goto unlock;
5316 }
5317
Johan Hedbergb644ba32012-01-17 21:48:47 +02005318 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07005319 mgmt_device_connected(hdev, conn, NULL, 0);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03005320
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03005321 conn->sec_level = BT_SECURITY_LOW;
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305322 conn->handle = handle;
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005323 conn->state = BT_CONFIG;
Ville Tervofcd89c02011-02-10 22:38:47 -03005324
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005325 /* Store current advertising instance as connection advertising instance
5326 * when sotfware rotation is in use so it can be re-enabled when
5327 * disconnected.
5328 */
5329 if (!ext_adv_capable(hdev))
5330 conn->adv_instance = hdev->cur_adv_instance;
5331
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305332 conn->le_conn_interval = interval;
5333 conn->le_conn_latency = latency;
5334 conn->le_supv_timeout = supervision_timeout;
Marcel Holtmanne04fde62014-06-23 11:40:04 +02005335
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01005336 hci_debugfs_create_conn(conn);
Ville Tervofcd89c02011-02-10 22:38:47 -03005337 hci_conn_add_sysfs(conn);
5338
Archie Pusakaef365da2021-05-31 16:37:23 +08005339 /* The remote features procedure is defined for central
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005340 * role only. So only in case of an initiated connection
5341 * request the remote features.
5342 *
Archie Pusakaef365da2021-05-31 16:37:23 +08005343 * If the local controller supports peripheral-initiated features
5344 * exchange, then requesting the remote features in peripheral
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005345 * role is possible. Otherwise just transition into the
5346 * connected state without requesting the remote features.
5347 */
5348 if (conn->out ||
Archie Pusakaef365da2021-05-31 16:37:23 +08005349 (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES)) {
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005350 struct hci_cp_le_read_remote_features cp;
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005351
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005352 cp.handle = __cpu_to_le16(conn->handle);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005353
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005354 hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
5355 sizeof(cp), &cp);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005356
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005357 hci_conn_hold(conn);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005358 } else {
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005359 conn->state = BT_CONNECTED;
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305360 hci_connect_cfm(conn, status);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005361 }
Ville Tervofcd89c02011-02-10 22:38:47 -03005362
Johan Hedberg54776102014-08-15 21:06:56 +03005363 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
5364 conn->dst_type);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005365 if (params) {
Johan Hedberg95305ba2014-07-04 12:37:21 +03005366 list_del_init(&params->action);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005367 if (params->conn) {
5368 hci_conn_drop(params->conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005369 hci_conn_put(params->conn);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005370 params->conn = NULL;
5371 }
5372 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005373
Ville Tervofcd89c02011-02-10 22:38:47 -03005374unlock:
Johan Hedberg223683a52014-07-06 15:44:23 +03005375 hci_update_background_scan(hdev);
Ville Tervofcd89c02011-02-10 22:38:47 -03005376 hci_dev_unlock(hdev);
5377}
5378
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305379static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
5380{
5381 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
5382
5383 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5384
5385 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005386 NULL, ev->role, le16_to_cpu(ev->handle),
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305387 le16_to_cpu(ev->interval),
5388 le16_to_cpu(ev->latency),
5389 le16_to_cpu(ev->supervision_timeout));
5390}
5391
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005392static void hci_le_enh_conn_complete_evt(struct hci_dev *hdev,
5393 struct sk_buff *skb)
5394{
5395 struct hci_ev_le_enh_conn_complete *ev = (void *) skb->data;
5396
5397 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5398
5399 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005400 &ev->local_rpa, ev->role, le16_to_cpu(ev->handle),
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005401 le16_to_cpu(ev->interval),
5402 le16_to_cpu(ev->latency),
5403 le16_to_cpu(ev->supervision_timeout));
Sathish Narasimman5c49bcc2020-07-23 18:09:01 +05305404
5405 if (use_ll_privacy(hdev) &&
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05305406 hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
Sathish Narasimman5c49bcc2020-07-23 18:09:01 +05305407 hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
5408 hci_req_disable_address_resolution(hdev);
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005409}
5410
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305411static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, struct sk_buff *skb)
5412{
5413 struct hci_evt_le_ext_adv_set_term *ev = (void *) skb->data;
5414 struct hci_conn *conn;
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005415 struct adv_info *adv;
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305416
5417 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5418
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005419 adv = hci_find_adv_instance(hdev, ev->handle);
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07005420
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005421 if (ev->status) {
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07005422 if (!adv)
5423 return;
5424
5425 /* Remove advertising as it has been terminated */
5426 hci_remove_adv_instance(hdev, ev->handle);
5427 mgmt_advertising_removed(NULL, hdev, ev->handle);
5428
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305429 return;
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07005430 }
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305431
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005432 if (adv)
5433 adv->enabled = false;
5434
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305435 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle));
5436 if (conn) {
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005437 /* Store handle in the connection so the correct advertising
5438 * instance can be re-enabled when disconnected.
5439 */
5440 conn->adv_instance = ev->handle;
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305441
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005442 if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM ||
5443 bacmp(&conn->resp_addr, BDADDR_ANY))
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305444 return;
5445
Daniel Winkler25e70882021-04-05 16:33:04 -07005446 if (!ev->handle) {
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305447 bacpy(&conn->resp_addr, &hdev->random_addr);
5448 return;
5449 }
5450
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005451 if (adv)
5452 bacpy(&conn->resp_addr, &adv->random_addr);
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305453 }
5454}
5455
Marcel Holtmann1855d922014-06-23 11:40:05 +02005456static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
5457 struct sk_buff *skb)
5458{
5459 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
5460 struct hci_conn *conn;
5461
5462 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5463
5464 if (ev->status)
5465 return;
5466
5467 hci_dev_lock(hdev);
5468
5469 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5470 if (conn) {
5471 conn->le_conn_interval = le16_to_cpu(ev->interval);
5472 conn->le_conn_latency = le16_to_cpu(ev->latency);
5473 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
5474 }
5475
5476 hci_dev_unlock(hdev);
5477}
5478
Andre Guedesa4790db2014-02-26 20:21:47 -03005479/* This function requires the caller holds hdev->lock */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005480static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
5481 bdaddr_t *addr,
Szymon Janc082f2302018-04-03 13:40:06 +02005482 u8 addr_type, u8 adv_type,
5483 bdaddr_t *direct_rpa)
Andre Guedesa4790db2014-02-26 20:21:47 -03005484{
5485 struct hci_conn *conn;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005486 struct hci_conn_params *params;
Andre Guedesa4790db2014-02-26 20:21:47 -03005487
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005488 /* If the event is not connectable don't proceed further */
5489 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005490 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005491
5492 /* Ignore if the device is blocked */
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005493 if (hci_bdaddr_list_lookup(&hdev->reject_list, addr, addr_type))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005494 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005495
Johan Hedbergf99353c2014-07-16 11:56:09 +03005496 /* Most controller will fail if we try to create new connections
Archie Pusaka39bc74c2021-06-04 16:26:26 +08005497 * while we have an existing one in peripheral role.
Johan Hedbergf99353c2014-07-16 11:56:09 +03005498 */
Archie Pusaka39bc74c2021-06-04 16:26:26 +08005499 if (hdev->conn_hash.le_num_peripheral > 0 &&
Alain Michaud4364f2e2020-04-23 14:43:29 +00005500 (!test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) ||
5501 !(hdev->le_states[3] & 0x10)))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005502 return NULL;
Johan Hedbergf99353c2014-07-16 11:56:09 +03005503
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005504 /* If we're not connectable only connect devices that we have in
5505 * our pend_le_conns list.
5506 */
Johan Hedberg49c50922015-10-16 10:07:51 +03005507 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
5508 addr_type);
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005509 if (!params)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005510 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005511
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005512 if (!params->explicit_connect) {
5513 switch (params->auto_connect) {
5514 case HCI_AUTO_CONN_DIRECT:
5515 /* Only devices advertising with ADV_DIRECT_IND are
5516 * triggering a connection attempt. This is allowing
Archie Pusaka67ffb182021-05-31 16:37:28 +08005517 * incoming connections from peripheral devices.
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005518 */
5519 if (adv_type != LE_ADV_DIRECT_IND)
5520 return NULL;
5521 break;
5522 case HCI_AUTO_CONN_ALWAYS:
5523 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
5524 * are triggering a connection attempt. This means
Archie Pusaka67ffb182021-05-31 16:37:28 +08005525 * that incoming connections from peripheral device are
5526 * accepted and also outgoing connections to peripheral
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005527 * devices are established when found.
5528 */
5529 break;
5530 default:
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005531 return NULL;
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005532 }
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005533 }
5534
Andre Guedesa4790db2014-02-26 20:21:47 -03005535 conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
Alain Michaud49b020c2020-06-29 16:11:00 +00005536 hdev->def_le_autoconnect_timeout, HCI_ROLE_MASTER,
Szymon Janc082f2302018-04-03 13:40:06 +02005537 direct_rpa);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005538 if (!IS_ERR(conn)) {
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005539 /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
5540 * by higher layer that tried to connect, if no then
5541 * store the pointer since we don't really have any
Johan Hedbergf161dd42014-08-15 21:06:54 +03005542 * other owner of the object besides the params that
5543 * triggered it. This way we can abort the connection if
5544 * the parameters get removed and keep the reference
5545 * count consistent once the connection is established.
5546 */
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005547
5548 if (!params->explicit_connect)
5549 params->conn = hci_conn_get(conn);
5550
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005551 return conn;
Johan Hedbergf161dd42014-08-15 21:06:54 +03005552 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005553
5554 switch (PTR_ERR(conn)) {
5555 case -EBUSY:
5556 /* If hci_connect() returns -EBUSY it means there is already
5557 * an LE connection attempt going on. Since controllers don't
5558 * support more than one connection attempt at the time, we
5559 * don't consider this an error case.
5560 */
5561 break;
5562 default:
5563 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005564 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005565 }
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005566
5567 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005568}
5569
Johan Hedberg4af605d2014-03-24 10:48:00 +02005570static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005571 u8 bdaddr_type, bdaddr_t *direct_addr,
Alain Michauda2ec9052020-07-27 20:48:55 +00005572 u8 direct_addr_type, s8 rssi, u8 *data, u8 len,
5573 bool ext_adv)
Johan Hedberg4af605d2014-03-24 10:48:00 +02005574{
Johan Hedbergb9a63282014-03-25 10:51:52 +02005575 struct discovery_state *d = &hdev->discovery;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005576 struct smp_irk *irk;
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005577 struct hci_conn *conn;
Johan Hedberg474ee062014-03-25 14:34:59 +02005578 bool match;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005579 u32 flags;
Szymon Janc1c58e932021-05-18 16:54:36 +02005580 u8 *ptr;
Szymon Janc68183752015-09-16 20:21:54 +02005581
Johan Hedberg56b40fb2016-04-07 21:01:27 +03005582 switch (type) {
5583 case LE_ADV_IND:
5584 case LE_ADV_DIRECT_IND:
5585 case LE_ADV_SCAN_IND:
5586 case LE_ADV_NONCONN_IND:
5587 case LE_ADV_SCAN_RSP:
5588 break;
5589 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005590 bt_dev_err_ratelimited(hdev, "unknown advertising packet "
5591 "type: 0x%02x", type);
Johan Hedberg56b40fb2016-04-07 21:01:27 +03005592 return;
5593 }
5594
Alain Michauda2ec9052020-07-27 20:48:55 +00005595 if (!ext_adv && len > HCI_MAX_AD_LENGTH) {
5596 bt_dev_err_ratelimited(hdev, "legacy adv larger than 31 bytes");
5597 return;
5598 }
5599
Szymon Janc68183752015-09-16 20:21:54 +02005600 /* Find the end of the data in case the report contains padded zero
5601 * bytes at the end causing an invalid length value.
5602 *
5603 * When data is NULL, len is 0 so there is no need for extra ptr
5604 * check as 'ptr < data + 0' is already false in such case.
5605 */
5606 for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
5607 if (ptr + 1 + *ptr > data + len)
5608 break;
5609 }
5610
Szymon Janc1c58e932021-05-18 16:54:36 +02005611 /* Adjust for actual length. This handles the case when remote
5612 * device is advertising with incorrect data length.
5613 */
5614 len = ptr - data;
Johan Hedbergb9a63282014-03-25 10:51:52 +02005615
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005616 /* If the direct address is present, then this report is from
5617 * a LE Direct Advertising Report event. In that case it is
5618 * important to see if the address is matching the local
5619 * controller address.
5620 */
5621 if (direct_addr) {
5622 /* Only resolvable random addresses are valid for these
5623 * kind of reports and others can be ignored.
5624 */
5625 if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
5626 return;
5627
5628 /* If the controller is not using resolvable random
5629 * addresses, then this report can be ignored.
5630 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005631 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005632 return;
5633
5634 /* If the local IRK of the controller does not match
5635 * with the resolvable random address provided, then
5636 * this report can be ignored.
5637 */
5638 if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
5639 return;
5640 }
5641
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005642 /* Check if we need to convert to identity address */
5643 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
5644 if (irk) {
5645 bdaddr = &irk->bdaddr;
5646 bdaddr_type = irk->addr_type;
5647 }
5648
Szymon Janc082f2302018-04-03 13:40:06 +02005649 /* Check if we have been requested to connect to this device.
5650 *
5651 * direct_addr is set only for directed advertising reports (it is NULL
5652 * for advertising reports) and is already verified to be RPA above.
5653 */
5654 conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, type,
5655 direct_addr);
Alain Michauda2ec9052020-07-27 20:48:55 +00005656 if (!ext_adv && conn && type == LE_ADV_IND && len <= HCI_MAX_AD_LENGTH) {
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005657 /* Store report for later inclusion by
5658 * mgmt_device_connected
5659 */
5660 memcpy(conn->le_adv_data, data, len);
5661 conn->le_adv_data_len = len;
5662 }
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005663
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005664 /* Passive scanning shouldn't trigger any device found events,
5665 * except for devices marked as CONN_REPORT for which we do send
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02005666 * device found events, or advertisement monitoring requested.
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005667 */
Johan Hedbergca5c4be2014-03-25 10:30:46 +02005668 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005669 if (type == LE_ADV_DIRECT_IND)
5670 return;
5671
Johan Hedberg3a19b6f2014-07-15 08:07:59 +03005672 if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02005673 bdaddr, bdaddr_type) &&
5674 idr_is_empty(&hdev->adv_monitors_idr))
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005675 return;
5676
5677 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
5678 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
5679 else
5680 flags = 0;
5681 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
5682 rssi, flags, data, len, NULL, 0);
Johan Hedberg97bf2e92014-07-04 12:37:16 +03005683 return;
Johan Hedbergca5c4be2014-03-25 10:30:46 +02005684 }
Johan Hedberg4af605d2014-03-24 10:48:00 +02005685
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005686 /* When receiving non-connectable or scannable undirected
5687 * advertising reports, this means that the remote device is
5688 * not connectable and then clearly indicate this in the
5689 * device found event.
5690 *
5691 * When receiving a scan response, then there is no way to
5692 * know if the remote device is connectable or not. However
5693 * since scan responses are merged with a previously seen
5694 * advertising report, the flags field from that report
5695 * will be used.
5696 *
5697 * In the really unlikely case that a controller get confused
5698 * and just sends a scan response event, then it is marked as
5699 * not connectable as well.
5700 */
5701 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
5702 type == LE_ADV_SCAN_RSP)
5703 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
5704 else
5705 flags = 0;
5706
Johan Hedbergb9a63282014-03-25 10:51:52 +02005707 /* If there's nothing pending either store the data from this
5708 * event or send an immediate device found event if the data
5709 * should not be stored for later.
5710 */
Alain Michauda2ec9052020-07-27 20:48:55 +00005711 if (!ext_adv && !has_pending_adv_report(hdev)) {
Johan Hedbergb9a63282014-03-25 10:51:52 +02005712 /* If the report will trigger a SCAN_REQ store it for
5713 * later merging.
5714 */
5715 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
5716 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005717 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005718 return;
5719 }
5720
5721 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005722 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005723 return;
5724 }
5725
Johan Hedberg474ee062014-03-25 14:34:59 +02005726 /* Check if the pending report is for the same device as the new one */
5727 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
5728 bdaddr_type == d->last_adv_addr_type);
5729
Johan Hedbergb9a63282014-03-25 10:51:52 +02005730 /* If the pending data doesn't match this report or this isn't a
5731 * scan response (e.g. we got a duplicate ADV_IND) then force
5732 * sending of the pending data.
5733 */
Johan Hedberg474ee062014-03-25 14:34:59 +02005734 if (type != LE_ADV_SCAN_RSP || !match) {
5735 /* Send out whatever is in the cache, but skip duplicates */
5736 if (!match)
5737 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergff5cd292014-03-25 14:40:52 +02005738 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005739 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergff5cd292014-03-25 14:40:52 +02005740 d->last_adv_data,
Johan Hedberg474ee062014-03-25 14:34:59 +02005741 d->last_adv_data_len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005742
5743 /* If the new report will trigger a SCAN_REQ store it for
5744 * later merging.
5745 */
Alain Michauda2ec9052020-07-27 20:48:55 +00005746 if (!ext_adv && (type == LE_ADV_IND ||
5747 type == LE_ADV_SCAN_IND)) {
Johan Hedbergb9a63282014-03-25 10:51:52 +02005748 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005749 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005750 return;
5751 }
5752
5753 /* The advertising reports cannot be merged, so clear
5754 * the pending report and send out a device found event.
5755 */
5756 clear_pending_adv_report(hdev);
Johan Hedberg5c5b93e2014-03-29 08:39:53 +02005757 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005758 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005759 return;
5760 }
5761
5762 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
5763 * the new event is a SCAN_RSP. We can therefore proceed with
5764 * sending a merged device found event.
5765 */
5766 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005767 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
Marcel Holtmann42bd6a52014-07-01 14:11:19 +02005768 d->last_adv_data, d->last_adv_data_len, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005769 clear_pending_adv_report(hdev);
Johan Hedberg4af605d2014-03-24 10:48:00 +02005770}
5771
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005772static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03005773{
Andre Guedese95beb42011-09-26 20:48:35 -03005774 u8 num_reports = skb->data[0];
5775 void *ptr = &skb->data[1];
Andre Guedes9aa04c92011-05-26 16:23:51 -03005776
Andre Guedesa4790db2014-02-26 20:21:47 -03005777 hci_dev_lock(hdev);
5778
Andre Guedese95beb42011-09-26 20:48:35 -03005779 while (num_reports--) {
5780 struct hci_ev_le_advertising_info *ev = ptr;
Johan Hedberg4af605d2014-03-24 10:48:00 +02005781 s8 rssi;
Andre Guedesa4790db2014-02-26 20:21:47 -03005782
Chriz Chowee649342018-04-20 15:46:24 +08005783 if (ev->length <= HCI_MAX_AD_LENGTH) {
5784 rssi = ev->data[ev->length];
5785 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
5786 ev->bdaddr_type, NULL, 0, rssi,
Alain Michauda2ec9052020-07-27 20:48:55 +00005787 ev->data, ev->length, false);
Chriz Chowee649342018-04-20 15:46:24 +08005788 } else {
5789 bt_dev_err(hdev, "Dropping invalid advertising data");
5790 }
Andre Guedes3c9e9192012-01-10 18:20:50 -03005791
Andre Guedese95beb42011-09-26 20:48:35 -03005792 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03005793 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005794
5795 hci_dev_unlock(hdev);
Andre Guedes9aa04c92011-05-26 16:23:51 -03005796}
5797
Marcel Holtmann657cc642019-12-11 11:34:36 +01005798static u8 ext_evt_type_to_legacy(struct hci_dev *hdev, u16 evt_type)
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305799{
Jaganath Kanakkasseryb2cc9762018-07-19 17:09:38 +05305800 if (evt_type & LE_EXT_ADV_LEGACY_PDU) {
5801 switch (evt_type) {
5802 case LE_LEGACY_ADV_IND:
5803 return LE_ADV_IND;
5804 case LE_LEGACY_ADV_DIRECT_IND:
5805 return LE_ADV_DIRECT_IND;
5806 case LE_LEGACY_ADV_SCAN_IND:
5807 return LE_ADV_SCAN_IND;
5808 case LE_LEGACY_NONCONN_IND:
5809 return LE_ADV_NONCONN_IND;
5810 case LE_LEGACY_SCAN_RSP_ADV:
5811 case LE_LEGACY_SCAN_RSP_ADV_SCAN:
5812 return LE_ADV_SCAN_RSP;
5813 }
5814
Marcel Holtmann657cc642019-12-11 11:34:36 +01005815 goto invalid;
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305816 }
5817
Jaganath Kanakkasseryb2cc9762018-07-19 17:09:38 +05305818 if (evt_type & LE_EXT_ADV_CONN_IND) {
5819 if (evt_type & LE_EXT_ADV_DIRECT_IND)
5820 return LE_ADV_DIRECT_IND;
5821
5822 return LE_ADV_IND;
5823 }
5824
5825 if (evt_type & LE_EXT_ADV_SCAN_RSP)
5826 return LE_ADV_SCAN_RSP;
5827
5828 if (evt_type & LE_EXT_ADV_SCAN_IND)
5829 return LE_ADV_SCAN_IND;
5830
5831 if (evt_type == LE_EXT_ADV_NON_CONN_IND ||
5832 evt_type & LE_EXT_ADV_DIRECT_IND)
5833 return LE_ADV_NONCONN_IND;
5834
Marcel Holtmann657cc642019-12-11 11:34:36 +01005835invalid:
5836 bt_dev_err_ratelimited(hdev, "Unknown advertising packet type: 0x%02x",
5837 evt_type);
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305838
5839 return LE_ADV_INVALID;
5840}
5841
5842static void hci_le_ext_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
5843{
5844 u8 num_reports = skb->data[0];
5845 void *ptr = &skb->data[1];
5846
5847 hci_dev_lock(hdev);
5848
5849 while (num_reports--) {
5850 struct hci_ev_le_ext_adv_report *ev = ptr;
5851 u8 legacy_evt_type;
5852 u16 evt_type;
5853
5854 evt_type = __le16_to_cpu(ev->evt_type);
Marcel Holtmann657cc642019-12-11 11:34:36 +01005855 legacy_evt_type = ext_evt_type_to_legacy(hdev, evt_type);
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305856 if (legacy_evt_type != LE_ADV_INVALID) {
5857 process_adv_report(hdev, legacy_evt_type, &ev->bdaddr,
5858 ev->bdaddr_type, NULL, 0, ev->rssi,
Alain Michauda2ec9052020-07-27 20:48:55 +00005859 ev->data, ev->length,
5860 !(evt_type & LE_EXT_ADV_LEGACY_PDU));
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305861 }
5862
Jaganath Kanakkasserycd9151b2019-04-03 12:11:44 +05305863 ptr += sizeof(*ev) + ev->length;
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305864 }
5865
5866 hci_dev_unlock(hdev);
5867}
5868
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005869static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev,
5870 struct sk_buff *skb)
5871{
5872 struct hci_ev_le_remote_feat_complete *ev = (void *)skb->data;
5873 struct hci_conn *conn;
5874
5875 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5876
5877 hci_dev_lock(hdev);
5878
5879 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5880 if (conn) {
5881 if (!ev->status)
5882 memcpy(conn->features[0], ev->features, 8);
5883
5884 if (conn->state == BT_CONFIG) {
5885 __u8 status;
5886
Archie Pusakaef365da2021-05-31 16:37:23 +08005887 /* If the local controller supports peripheral-initiated
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005888 * features exchange, but the remote controller does
5889 * not, then it is possible that the error code 0x1a
5890 * for unsupported remote feature gets returned.
5891 *
5892 * In this specific case, allow the connection to
5893 * transition into connected state and mark it as
5894 * successful.
5895 */
Archie Pusakaef365da2021-05-31 16:37:23 +08005896 if (!conn->out && ev->status == 0x1a &&
5897 (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES))
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005898 status = 0x00;
5899 else
5900 status = ev->status;
5901
5902 conn->state = BT_CONNECTED;
5903 hci_connect_cfm(conn, status);
5904 hci_conn_drop(conn);
5905 }
5906 }
5907
5908 hci_dev_unlock(hdev);
5909}
5910
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005911static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005912{
5913 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
5914 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005915 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005916 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03005917 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005918
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03005919 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005920
5921 hci_dev_lock(hdev);
5922
5923 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005924 if (conn == NULL)
5925 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005926
Johan Hedbergf3a73d92014-05-29 15:02:59 +03005927 ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
Johan Hedberg5378bc52014-05-29 14:00:39 +03005928 if (!ltk)
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005929 goto not_found;
5930
Johan Hedberg5378bc52014-05-29 14:00:39 +03005931 if (smp_ltk_is_sc(ltk)) {
5932 /* With SC both EDiv and Rand are set to zero */
5933 if (ev->ediv || ev->rand)
5934 goto not_found;
5935 } else {
5936 /* For non-SC keys check that EDiv and Rand match */
5937 if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
5938 goto not_found;
5939 }
5940
Johan Hedberg8b76ce32015-06-08 18:14:39 +03005941 memcpy(cp.ltk, ltk->val, ltk->enc_size);
5942 memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005943 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03005944
Johan Hedberga6f78332014-09-10 17:37:45 -07005945 conn->pending_sec_level = smp_ltk_sec_level(ltk);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005946
Andre Guedes89cbb4d2013-07-31 16:25:29 -03005947 conn->enc_key_size = ltk->enc_size;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005948
5949 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
5950
Claudio Takahasi5981a882013-07-25 16:34:24 -03005951 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
5952 * temporary key used to encrypt a connection following
5953 * pairing. It is used during the Encrypted Session Setup to
5954 * distribute the keys. Later, security can be re-established
5955 * using a distributed LTK.
5956 */
Johan Hedberg2ceba532014-06-16 19:25:16 +03005957 if (ltk->type == SMP_STK) {
Johan Hedbergfe59a052014-07-01 19:14:12 +03005958 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Johan Hedberg970d0f12014-11-13 14:37:47 +02005959 list_del_rcu(&ltk->list);
5960 kfree_rcu(ltk, rcu);
Johan Hedbergfe59a052014-07-01 19:14:12 +03005961 } else {
5962 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03005963 }
5964
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005965 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005966
5967 return;
5968
5969not_found:
5970 neg.handle = ev->handle;
5971 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
5972 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005973}
5974
Andre Guedes8e75b462014-07-01 18:10:08 -03005975static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
5976 u8 reason)
5977{
5978 struct hci_cp_le_conn_param_req_neg_reply cp;
5979
5980 cp.handle = cpu_to_le16(handle);
5981 cp.reason = reason;
5982
5983 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
5984 &cp);
5985}
5986
5987static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
5988 struct sk_buff *skb)
5989{
5990 struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
5991 struct hci_cp_le_conn_param_req_reply cp;
5992 struct hci_conn *hcon;
5993 u16 handle, min, max, latency, timeout;
5994
5995 handle = le16_to_cpu(ev->handle);
5996 min = le16_to_cpu(ev->interval_min);
5997 max = le16_to_cpu(ev->interval_max);
5998 latency = le16_to_cpu(ev->latency);
5999 timeout = le16_to_cpu(ev->timeout);
6000
6001 hcon = hci_conn_hash_lookup_handle(hdev, handle);
6002 if (!hcon || hcon->state != BT_CONNECTED)
6003 return send_conn_param_neg_reply(hdev, handle,
6004 HCI_ERROR_UNKNOWN_CONN_ID);
6005
6006 if (hci_check_conn_params(min, max, latency, timeout))
6007 return send_conn_param_neg_reply(hdev, handle,
6008 HCI_ERROR_INVALID_LL_PARAMS);
6009
Johan Hedberg40bef302014-07-16 11:42:27 +03006010 if (hcon->role == HCI_ROLE_MASTER) {
Johan Hedberg348d50b2014-07-02 17:37:30 +03006011 struct hci_conn_params *params;
Johan Hedbergf4869e22014-07-02 17:37:32 +03006012 u8 store_hint;
Johan Hedberg348d50b2014-07-02 17:37:30 +03006013
6014 hci_dev_lock(hdev);
6015
6016 params = hci_conn_params_lookup(hdev, &hcon->dst,
6017 hcon->dst_type);
6018 if (params) {
6019 params->conn_min_interval = min;
6020 params->conn_max_interval = max;
6021 params->conn_latency = latency;
6022 params->supervision_timeout = timeout;
Johan Hedbergf4869e22014-07-02 17:37:32 +03006023 store_hint = 0x01;
Meng Yu149b3f12021-04-01 14:50:39 +08006024 } else {
Johan Hedbergf4869e22014-07-02 17:37:32 +03006025 store_hint = 0x00;
Johan Hedberg348d50b2014-07-02 17:37:30 +03006026 }
6027
6028 hci_dev_unlock(hdev);
6029
Johan Hedbergf4869e22014-07-02 17:37:32 +03006030 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
6031 store_hint, min, max, latency, timeout);
Johan Hedberg348d50b2014-07-02 17:37:30 +03006032 }
Andre Guedesffb5a8272014-07-01 18:10:11 -03006033
Andre Guedes8e75b462014-07-01 18:10:08 -03006034 cp.handle = ev->handle;
6035 cp.interval_min = ev->interval_min;
6036 cp.interval_max = ev->interval_max;
6037 cp.latency = ev->latency;
6038 cp.timeout = ev->timeout;
6039 cp.min_ce_len = 0;
6040 cp.max_ce_len = 0;
6041
6042 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
6043}
6044
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006045static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
6046 struct sk_buff *skb)
6047{
6048 u8 num_reports = skb->data[0];
Peilin Yef7e0e8b2020-09-09 03:17:00 -04006049 struct hci_ev_le_direct_adv_info *ev = (void *)&skb->data[1];
6050
6051 if (!num_reports || skb->len < num_reports * sizeof(*ev) + 1)
6052 return;
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006053
6054 hci_dev_lock(hdev);
6055
Peilin Yef7e0e8b2020-09-09 03:17:00 -04006056 for (; num_reports; num_reports--, ev++)
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006057 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
6058 ev->bdaddr_type, &ev->direct_addr,
Alain Michauda2ec9052020-07-27 20:48:55 +00006059 ev->direct_addr_type, ev->rssi, NULL, 0,
6060 false);
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006061
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006062 hci_dev_unlock(hdev);
6063}
6064
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006065static void hci_le_phy_update_evt(struct hci_dev *hdev, struct sk_buff *skb)
6066{
6067 struct hci_ev_le_phy_update_complete *ev = (void *) skb->data;
6068 struct hci_conn *conn;
6069
6070 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
6071
Ayush Garg87df8bc2021-03-17 16:52:14 +05306072 if (ev->status)
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006073 return;
6074
6075 hci_dev_lock(hdev);
6076
6077 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6078 if (!conn)
6079 goto unlock;
6080
6081 conn->le_tx_phy = ev->tx_phy;
6082 conn->le_rx_phy = ev->rx_phy;
6083
6084unlock:
6085 hci_dev_unlock(hdev);
6086}
6087
Gustavo Padovan6039aa72012-05-23 04:04:18 -03006088static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03006089{
6090 struct hci_ev_le_meta *le_ev = (void *) skb->data;
6091
6092 skb_pull(skb, sizeof(*le_ev));
6093
6094 switch (le_ev->subevent) {
6095 case HCI_EV_LE_CONN_COMPLETE:
6096 hci_le_conn_complete_evt(hdev, skb);
6097 break;
6098
Marcel Holtmann1855d922014-06-23 11:40:05 +02006099 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
6100 hci_le_conn_update_complete_evt(hdev, skb);
6101 break;
6102
Andre Guedes9aa04c92011-05-26 16:23:51 -03006103 case HCI_EV_LE_ADVERTISING_REPORT:
6104 hci_le_adv_report_evt(hdev, skb);
6105 break;
6106
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006107 case HCI_EV_LE_REMOTE_FEAT_COMPLETE:
6108 hci_le_remote_feat_complete_evt(hdev, skb);
6109 break;
6110
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006111 case HCI_EV_LE_LTK_REQ:
6112 hci_le_ltk_request_evt(hdev, skb);
6113 break;
6114
Andre Guedes8e75b462014-07-01 18:10:08 -03006115 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
6116 hci_le_remote_conn_param_req_evt(hdev, skb);
6117 break;
6118
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006119 case HCI_EV_LE_DIRECT_ADV_REPORT:
6120 hci_le_direct_adv_report_evt(hdev, skb);
6121 break;
6122
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006123 case HCI_EV_LE_PHY_UPDATE_COMPLETE:
6124 hci_le_phy_update_evt(hdev, skb);
6125 break;
6126
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05306127 case HCI_EV_LE_EXT_ADV_REPORT:
6128 hci_le_ext_adv_report_evt(hdev, skb);
6129 break;
6130
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02006131 case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
6132 hci_le_enh_conn_complete_evt(hdev, skb);
6133 break;
6134
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306135 case HCI_EV_LE_EXT_ADV_SET_TERM:
6136 hci_le_ext_adv_term_evt(hdev, skb);
6137 break;
6138
Ville Tervofcd89c02011-02-10 22:38:47 -03006139 default:
6140 break;
6141 }
6142}
6143
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006144static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
6145 u8 event, struct sk_buff *skb)
6146{
6147 struct hci_ev_cmd_complete *ev;
6148 struct hci_event_hdr *hdr;
6149
6150 if (!skb)
6151 return false;
6152
6153 if (skb->len < sizeof(*hdr)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006154 bt_dev_err(hdev, "too short HCI event");
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006155 return false;
6156 }
6157
6158 hdr = (void *) skb->data;
6159 skb_pull(skb, HCI_EVENT_HDR_SIZE);
6160
6161 if (event) {
6162 if (hdr->evt != event)
6163 return false;
6164 return true;
6165 }
6166
Zheng Yongjun91641b72021-06-02 14:54:58 +08006167 /* Check if request ended in Command Status - no way to retrieve
Johan Hedberg1629db9c2018-11-27 11:37:46 +02006168 * any extra parameters in this case.
6169 */
6170 if (hdr->evt == HCI_EV_CMD_STATUS)
6171 return false;
6172
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006173 if (hdr->evt != HCI_EV_CMD_COMPLETE) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006174 bt_dev_err(hdev, "last event is not cmd complete (0x%2.2x)",
6175 hdr->evt);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006176 return false;
6177 }
6178
6179 if (skb->len < sizeof(*ev)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006180 bt_dev_err(hdev, "too short cmd_complete event");
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006181 return false;
6182 }
6183
6184 ev = (void *) skb->data;
6185 skb_pull(skb, sizeof(*ev));
6186
6187 if (opcode != __le16_to_cpu(ev->opcode)) {
6188 BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
6189 __le16_to_cpu(ev->opcode));
6190 return false;
6191 }
6192
6193 return true;
6194}
6195
Abhishek Pandit-Subedi2f202162020-09-11 14:07:13 -07006196static void hci_store_wake_reason(struct hci_dev *hdev, u8 event,
6197 struct sk_buff *skb)
6198{
6199 struct hci_ev_le_advertising_info *adv;
6200 struct hci_ev_le_direct_adv_info *direct_adv;
6201 struct hci_ev_le_ext_adv_report *ext_adv;
6202 const struct hci_ev_conn_complete *conn_complete = (void *)skb->data;
6203 const struct hci_ev_conn_request *conn_request = (void *)skb->data;
6204
6205 hci_dev_lock(hdev);
6206
6207 /* If we are currently suspended and this is the first BT event seen,
6208 * save the wake reason associated with the event.
6209 */
6210 if (!hdev->suspended || hdev->wake_reason)
6211 goto unlock;
6212
6213 /* Default to remote wake. Values for wake_reason are documented in the
6214 * Bluez mgmt api docs.
6215 */
6216 hdev->wake_reason = MGMT_WAKE_REASON_REMOTE_WAKE;
6217
6218 /* Once configured for remote wakeup, we should only wake up for
6219 * reconnections. It's useful to see which device is waking us up so
6220 * keep track of the bdaddr of the connection event that woke us up.
6221 */
6222 if (event == HCI_EV_CONN_REQUEST) {
6223 bacpy(&hdev->wake_addr, &conn_complete->bdaddr);
6224 hdev->wake_addr_type = BDADDR_BREDR;
6225 } else if (event == HCI_EV_CONN_COMPLETE) {
6226 bacpy(&hdev->wake_addr, &conn_request->bdaddr);
6227 hdev->wake_addr_type = BDADDR_BREDR;
6228 } else if (event == HCI_EV_LE_META) {
6229 struct hci_ev_le_meta *le_ev = (void *)skb->data;
6230 u8 subevent = le_ev->subevent;
6231 u8 *ptr = &skb->data[sizeof(*le_ev)];
6232 u8 num_reports = *ptr;
6233
6234 if ((subevent == HCI_EV_LE_ADVERTISING_REPORT ||
6235 subevent == HCI_EV_LE_DIRECT_ADV_REPORT ||
6236 subevent == HCI_EV_LE_EXT_ADV_REPORT) &&
6237 num_reports) {
6238 adv = (void *)(ptr + 1);
6239 direct_adv = (void *)(ptr + 1);
6240 ext_adv = (void *)(ptr + 1);
6241
6242 switch (subevent) {
6243 case HCI_EV_LE_ADVERTISING_REPORT:
6244 bacpy(&hdev->wake_addr, &adv->bdaddr);
6245 hdev->wake_addr_type = adv->bdaddr_type;
6246 break;
6247 case HCI_EV_LE_DIRECT_ADV_REPORT:
6248 bacpy(&hdev->wake_addr, &direct_adv->bdaddr);
6249 hdev->wake_addr_type = direct_adv->bdaddr_type;
6250 break;
6251 case HCI_EV_LE_EXT_ADV_REPORT:
6252 bacpy(&hdev->wake_addr, &ext_adv->bdaddr);
6253 hdev->wake_addr_type = ext_adv->bdaddr_type;
6254 break;
6255 }
6256 }
6257 } else {
6258 hdev->wake_reason = MGMT_WAKE_REASON_UNEXPECTED;
6259 }
6260
6261unlock:
6262 hci_dev_unlock(hdev);
6263}
6264
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
6266{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006267 struct hci_event_hdr *hdr = (void *) skb->data;
Johan Hedberge62144872015-04-02 13:41:08 +03006268 hci_req_complete_t req_complete = NULL;
6269 hci_req_complete_skb_t req_complete_skb = NULL;
6270 struct sk_buff *orig_skb = NULL;
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006271 u8 status = 0, event = hdr->evt, req_evt = 0;
Johan Hedberge62144872015-04-02 13:41:08 +03006272 u16 opcode = HCI_OP_NOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273
Alain Michaud08bb4da2020-03-03 15:55:34 +00006274 if (!event) {
6275 bt_dev_warn(hdev, "Received unexpected HCI Event 00000000");
6276 goto done;
6277 }
6278
Marcel Holtmann242c0eb2015-10-25 22:45:53 +01006279 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->hci.req_event == event) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02006280 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
Johan Hedberge62144872015-04-02 13:41:08 +03006281 opcode = __le16_to_cpu(cmd_hdr->opcode);
6282 hci_req_cmd_complete(hdev, opcode, status, &req_complete,
6283 &req_complete_skb);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006284 req_evt = event;
Johan Hedberg02350a72013-04-03 21:50:29 +03006285 }
6286
Johan Hedberge62144872015-04-02 13:41:08 +03006287 /* If it looks like we might end up having to call
6288 * req_complete_skb, store a pristine copy of the skb since the
6289 * various handlers may modify the original one through
6290 * skb_pull() calls, etc.
6291 */
6292 if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
6293 event == HCI_EV_CMD_COMPLETE)
6294 orig_skb = skb_clone(skb, GFP_KERNEL);
6295
6296 skb_pull(skb, HCI_EVENT_HDR_SIZE);
6297
Abhishek Pandit-Subedi2f202162020-09-11 14:07:13 -07006298 /* Store wake reason if we're suspended */
6299 hci_store_wake_reason(hdev, event, skb);
6300
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006301 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006302 case HCI_EV_INQUIRY_COMPLETE:
6303 hci_inquiry_complete_evt(hdev, skb);
6304 break;
6305
6306 case HCI_EV_INQUIRY_RESULT:
6307 hci_inquiry_result_evt(hdev, skb);
6308 break;
6309
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006310 case HCI_EV_CONN_COMPLETE:
6311 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02006312 break;
6313
Linus Torvalds1da177e2005-04-16 15:20:36 -07006314 case HCI_EV_CONN_REQUEST:
6315 hci_conn_request_evt(hdev, skb);
6316 break;
6317
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318 case HCI_EV_DISCONN_COMPLETE:
6319 hci_disconn_complete_evt(hdev, skb);
6320 break;
6321
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 case HCI_EV_AUTH_COMPLETE:
6323 hci_auth_complete_evt(hdev, skb);
6324 break;
6325
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006326 case HCI_EV_REMOTE_NAME:
6327 hci_remote_name_evt(hdev, skb);
6328 break;
6329
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 case HCI_EV_ENCRYPT_CHANGE:
6331 hci_encrypt_change_evt(hdev, skb);
6332 break;
6333
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006334 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
6335 hci_change_link_key_complete_evt(hdev, skb);
6336 break;
6337
6338 case HCI_EV_REMOTE_FEATURES:
6339 hci_remote_features_evt(hdev, skb);
6340 break;
6341
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006342 case HCI_EV_CMD_COMPLETE:
Johan Hedberge62144872015-04-02 13:41:08 +03006343 hci_cmd_complete_evt(hdev, skb, &opcode, &status,
6344 &req_complete, &req_complete_skb);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006345 break;
6346
6347 case HCI_EV_CMD_STATUS:
Johan Hedberge62144872015-04-02 13:41:08 +03006348 hci_cmd_status_evt(hdev, skb, &opcode, &status, &req_complete,
6349 &req_complete_skb);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006350 break;
6351
Marcel Holtmann24dfa342014-11-02 02:56:41 +01006352 case HCI_EV_HARDWARE_ERROR:
6353 hci_hardware_error_evt(hdev, skb);
6354 break;
6355
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006356 case HCI_EV_ROLE_CHANGE:
6357 hci_role_change_evt(hdev, skb);
6358 break;
6359
6360 case HCI_EV_NUM_COMP_PKTS:
6361 hci_num_comp_pkts_evt(hdev, skb);
6362 break;
6363
6364 case HCI_EV_MODE_CHANGE:
6365 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 break;
6367
6368 case HCI_EV_PIN_CODE_REQ:
6369 hci_pin_code_request_evt(hdev, skb);
6370 break;
6371
6372 case HCI_EV_LINK_KEY_REQ:
6373 hci_link_key_request_evt(hdev, skb);
6374 break;
6375
6376 case HCI_EV_LINK_KEY_NOTIFY:
6377 hci_link_key_notify_evt(hdev, skb);
6378 break;
6379
6380 case HCI_EV_CLOCK_OFFSET:
6381 hci_clock_offset_evt(hdev, skb);
6382 break;
6383
Marcel Holtmanna8746412008-07-14 20:13:46 +02006384 case HCI_EV_PKT_TYPE_CHANGE:
6385 hci_pkt_type_change_evt(hdev, skb);
6386 break;
6387
Marcel Holtmann85a1e932005-08-09 20:28:02 -07006388 case HCI_EV_PSCAN_REP_MODE:
6389 hci_pscan_rep_mode_evt(hdev, skb);
6390 break;
6391
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006392 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
6393 hci_inquiry_result_with_rssi_evt(hdev, skb);
6394 break;
6395
6396 case HCI_EV_REMOTE_EXT_FEATURES:
6397 hci_remote_ext_features_evt(hdev, skb);
6398 break;
6399
6400 case HCI_EV_SYNC_CONN_COMPLETE:
6401 hci_sync_conn_complete_evt(hdev, skb);
6402 break;
6403
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006404 case HCI_EV_EXTENDED_INQUIRY_RESULT:
6405 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006406 break;
6407
Johan Hedberg1c2e0042012-06-08 23:31:13 +08006408 case HCI_EV_KEY_REFRESH_COMPLETE:
6409 hci_key_refresh_complete_evt(hdev, skb);
6410 break;
6411
Marcel Holtmann04936842008-07-14 20:13:48 +02006412 case HCI_EV_IO_CAPA_REQUEST:
6413 hci_io_capa_request_evt(hdev, skb);
6414 break;
6415
Johan Hedberg03b555e2011-01-04 15:40:05 +02006416 case HCI_EV_IO_CAPA_REPLY:
6417 hci_io_capa_reply_evt(hdev, skb);
6418 break;
6419
Johan Hedberga5c29682011-02-19 12:05:57 -03006420 case HCI_EV_USER_CONFIRM_REQUEST:
6421 hci_user_confirm_request_evt(hdev, skb);
6422 break;
6423
Brian Gix1143d452011-11-23 08:28:34 -08006424 case HCI_EV_USER_PASSKEY_REQUEST:
6425 hci_user_passkey_request_evt(hdev, skb);
6426 break;
6427
Johan Hedberg92a25252012-09-06 18:39:26 +03006428 case HCI_EV_USER_PASSKEY_NOTIFY:
6429 hci_user_passkey_notify_evt(hdev, skb);
6430 break;
6431
6432 case HCI_EV_KEYPRESS_NOTIFY:
6433 hci_keypress_notify_evt(hdev, skb);
6434 break;
6435
Marcel Holtmann04936842008-07-14 20:13:48 +02006436 case HCI_EV_SIMPLE_PAIR_COMPLETE:
6437 hci_simple_pair_complete_evt(hdev, skb);
6438 break;
6439
Marcel Holtmann41a96212008-07-14 20:13:48 +02006440 case HCI_EV_REMOTE_HOST_FEATURES:
6441 hci_remote_host_features_evt(hdev, skb);
6442 break;
6443
Ville Tervofcd89c02011-02-10 22:38:47 -03006444 case HCI_EV_LE_META:
6445 hci_le_meta_evt(hdev, skb);
6446 break;
6447
Szymon Janc2763eda2011-03-22 13:12:22 +01006448 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
6449 hci_remote_oob_data_request_evt(hdev, skb);
6450 break;
6451
Arron Wanga77a6a12015-07-24 17:13:15 +08006452#if IS_ENABLED(CONFIG_BT_HS)
6453 case HCI_EV_CHANNEL_SELECTED:
6454 hci_chan_selected_evt(hdev, skb);
6455 break;
6456
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03006457 case HCI_EV_PHY_LINK_COMPLETE:
6458 hci_phy_link_complete_evt(hdev, skb);
6459 break;
6460
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03006461 case HCI_EV_LOGICAL_LINK_COMPLETE:
6462 hci_loglink_complete_evt(hdev, skb);
6463 break;
6464
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02006465 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
6466 hci_disconn_loglink_complete_evt(hdev, skb);
6467 break;
6468
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02006469 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
6470 hci_disconn_phylink_complete_evt(hdev, skb);
6471 break;
Arron Wanga77a6a12015-07-24 17:13:15 +08006472#endif
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02006473
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02006474 case HCI_EV_NUM_COMP_BLOCKS:
6475 hci_num_comp_blocks_evt(hdev, skb);
6476 break;
6477
Miao-chen Chou145373c2020-04-03 21:44:01 +02006478 case HCI_EV_VENDOR:
6479 msft_vendor_evt(hdev, skb);
6480 break;
6481
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006482 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03006483 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006484 break;
6485 }
6486
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006487 if (req_complete) {
Johan Hedberge62144872015-04-02 13:41:08 +03006488 req_complete(hdev, status, opcode);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006489 } else if (req_complete_skb) {
6490 if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
6491 kfree_skb(orig_skb);
6492 orig_skb = NULL;
6493 }
Johan Hedberge62144872015-04-02 13:41:08 +03006494 req_complete_skb(hdev, status, opcode, orig_skb);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006495 }
Johan Hedberge62144872015-04-02 13:41:08 +03006496
Alain Michaud08bb4da2020-03-03 15:55:34 +00006497done:
Johan Hedberge62144872015-04-02 13:41:08 +03006498 kfree_skb(orig_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 kfree_skb(skb);
6500 hdev->stat.evt_rx++;
6501}