blob: 7d0db1ca1248294577c897485ca4a3994a70d302 [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"
Luiz Augusto von Dentz01ce70b2021-09-20 15:59:37 -070039#include "eir.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Marcel Holtmannaa5b0342015-01-27 16:04:33 -080041#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
42 "\x00\x00\x00\x00\x00\x00\x00\x00"
43
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -070044#define secs_to_jiffies(_secs) msecs_to_jiffies((_secs) * 1000)
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* Handle HCI Event packets */
47
Sonny Sasakaadf1d692020-05-06 12:55:03 -070048static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb,
49 u8 *new_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020051 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030053 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Sonny Sasakaadf1d692020-05-06 12:55:03 -070055 /* It is possible that we receive Inquiry Complete event right
56 * before we receive Inquiry Cancel Command Complete event, in
57 * which case the latter event should have status of Command
58 * Disallowed (0x0c). This should not be treated as error, since
59 * we actually achieve what Inquiry Cancel wants to achieve,
60 * which is to end the last Inquiry session.
61 */
62 if (status == 0x0c && !test_bit(HCI_INQUIRY, &hdev->flags)) {
63 bt_dev_warn(hdev, "Ignoring error of Inquiry Cancel command");
64 status = 0x00;
65 }
66
67 *new_status = status;
68
Andre Guedes82f47852013-04-30 15:29:34 -030069 if (status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020070 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Andre Guedes89352e72011-11-04 14:16:53 -030072 clear_bit(HCI_INQUIRY, &hdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010073 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -030074 wake_up_bit(&hdev->flags, HCI_INQUIRY);
Andre Guedes89352e72011-11-04 14:16:53 -030075
Johan Hedberg50143a42014-06-10 14:05:57 +030076 hci_dev_lock(hdev);
Jakub Pawlowski168b8a22015-10-16 10:07:49 +030077 /* Set discovery state to stopped if we're not doing LE active
78 * scanning.
79 */
80 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
81 hdev->le_scan_type != LE_SCAN_ACTIVE)
82 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg50143a42014-06-10 14:05:57 +030083 hci_dev_unlock(hdev);
84
Marcel Holtmanna9de9242007-10-20 13:33:56 +020085 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Andre Guedes4d934832012-03-21 00:03:35 -030088static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
89{
90 __u8 status = *((__u8 *) skb->data);
91
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030092 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesae854a72012-03-21 00:03:36 -030093
94 if (status)
95 return;
96
Marcel Holtmanna1536da2015-03-13 02:11:01 -070097 hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
Andre Guedes4d934832012-03-21 00:03:35 -030098}
99
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200100static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200102 __u8 status = *((__u8 *) skb->data);
103
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300104 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200105
106 if (status)
107 return;
108
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700109 hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
Andre Guedesae854a72012-03-21 00:03:36 -0300110
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200111 hci_conn_check_pending(hdev);
112}
113
Gustavo Padovan807deac2012-05-17 00:36:24 -0300114static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
115 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200116{
117 BT_DBG("%s", hdev->name);
118}
119
120static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
121{
122 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300125 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200127 if (rp->status)
128 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200130 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200132 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Johan Hedberg40bef302014-07-16 11:42:27 +0300133 if (conn)
134 conn->role = rp->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200135
136 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200139static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
140{
141 struct hci_rp_read_link_policy *rp = (void *) skb->data;
142 struct hci_conn *conn;
143
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300144 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200145
146 if (rp->status)
147 return;
148
149 hci_dev_lock(hdev);
150
151 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
152 if (conn)
153 conn->link_policy = __le16_to_cpu(rp->policy);
154
155 hci_dev_unlock(hdev);
156}
157
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200158static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200160 struct hci_rp_write_link_policy *rp = (void *) skb->data;
161 struct hci_conn *conn;
162 void *sent;
163
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300164 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200165
166 if (rp->status)
167 return;
168
169 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
170 if (!sent)
171 return;
172
173 hci_dev_lock(hdev);
174
175 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200176 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700177 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200178
179 hci_dev_unlock(hdev);
180}
181
Gustavo Padovan807deac2012-05-17 00:36:24 -0300182static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
183 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200184{
185 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
186
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300187 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200188
189 if (rp->status)
190 return;
191
192 hdev->link_policy = __le16_to_cpu(rp->policy);
193}
194
Gustavo Padovan807deac2012-05-17 00:36:24 -0300195static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
196 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200197{
198 __u8 status = *((__u8 *) skb->data);
199 void *sent;
200
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300201 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200202
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200203 if (status)
204 return;
205
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200206 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
207 if (!sent)
208 return;
209
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200210 hdev->link_policy = get_unaligned_le16(sent);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200211}
212
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200213static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
214{
215 __u8 status = *((__u8 *) skb->data);
216
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300217 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200218
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300219 clear_bit(HCI_RESET, &hdev->flags);
220
Marcel Holtmann8761f9d2014-11-02 02:45:58 +0100221 if (status)
222 return;
223
Johan Hedberga297e972012-02-21 17:55:47 +0200224 /* Reset all non-persistent flags */
Marcel Holtmanneacb44d2015-03-13 09:04:17 -0700225 hci_dev_clear_volatile_flags(hdev);
Andre Guedes69775ff2012-02-23 16:50:05 +0200226
Johan Hedberg39c5d972015-01-28 19:56:01 +0200227 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
228
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100229 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
230 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
Johan Hedberg3f0f5242012-11-08 01:23:00 +0100231
232 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
233 hdev->adv_data_len = 0;
Marcel Holtmannf8e808b2013-10-16 00:16:47 -0700234
235 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
236 hdev->scan_rsp_data_len = 0;
Marcel Holtmann06f5b7782013-10-19 07:09:11 -0700237
Marcel Holtmann533553f2014-03-21 12:18:10 -0700238 hdev->le_scan_type = LE_SCAN_PASSIVE;
239
Marcel Holtmann06f5b7782013-10-19 07:09:11 -0700240 hdev->ssp_debug_mode = 0;
Marcel Holtmanna4d55042014-10-29 23:37:53 +0100241
Archie Pusaka3d4f9c02021-06-04 16:26:27 +0800242 hci_bdaddr_list_clear(&hdev->le_accept_list);
Ankit Navikcfdb0c22018-06-29 12:12:50 +0530243 hci_bdaddr_list_clear(&hdev->le_resolv_list);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200244}
245
Marcel Holtmannc2f0f972015-01-12 09:21:25 -0800246static void hci_cc_read_stored_link_key(struct hci_dev *hdev,
247 struct sk_buff *skb)
248{
249 struct hci_rp_read_stored_link_key *rp = (void *)skb->data;
250 struct hci_cp_read_stored_link_key *sent;
251
252 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
253
254 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_STORED_LINK_KEY);
255 if (!sent)
256 return;
257
258 if (!rp->status && sent->read_all == 0x01) {
259 hdev->stored_max_keys = rp->max_keys;
260 hdev->stored_num_keys = rp->num_keys;
261 }
262}
263
Marcel Holtmanna93661202015-01-12 09:21:28 -0800264static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
265 struct sk_buff *skb)
266{
267 struct hci_rp_delete_stored_link_key *rp = (void *)skb->data;
268
269 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
270
271 if (rp->status)
272 return;
273
274 if (rp->num_keys <= hdev->stored_num_keys)
275 hdev->stored_num_keys -= rp->num_keys;
276 else
277 hdev->stored_num_keys = 0;
278}
279
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200280static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
281{
282 __u8 status = *((__u8 *) skb->data);
283 void *sent;
284
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300285 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200286
287 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
288 if (!sent)
289 return;
290
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200291 hci_dev_lock(hdev);
292
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700293 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200294 mgmt_set_local_name_complete(hdev, sent, status);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +0200295 else if (!status)
296 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200297
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200298 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200299}
300
301static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
302{
303 struct hci_rp_read_local_name *rp = (void *) skb->data;
304
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300305 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200306
307 if (rp->status)
308 return;
309
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700310 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
311 hci_dev_test_flag(hdev, HCI_CONFIG))
Johan Hedbergdb99b5f2012-02-22 20:14:22 +0200312 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200313}
314
315static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
316{
317 __u8 status = *((__u8 *) skb->data);
318 void *sent;
319
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300320 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200321
322 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
323 if (!sent)
324 return;
325
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530326 hci_dev_lock(hdev);
327
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200328 if (!status) {
329 __u8 param = *((__u8 *) sent);
330
331 if (param == AUTH_ENABLED)
332 set_bit(HCI_AUTH, &hdev->flags);
333 else
334 clear_bit(HCI_AUTH, &hdev->flags);
335 }
336
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700337 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg33ef95e2012-02-16 23:56:27 +0200338 mgmt_auth_enable_complete(hdev, status);
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530339
340 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200341}
342
343static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
344{
345 __u8 status = *((__u8 *) skb->data);
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200346 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200347 void *sent;
348
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300349 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200350
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200351 if (status)
352 return;
353
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200354 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
355 if (!sent)
356 return;
357
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200358 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200359
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200360 if (param)
361 set_bit(HCI_ENCRYPT, &hdev->flags);
362 else
363 clear_bit(HCI_ENCRYPT, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200364}
365
366static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
367{
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200368 __u8 status = *((__u8 *) skb->data);
369 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200370 void *sent;
371
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300372 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200373
374 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
375 if (!sent)
376 return;
377
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200378 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200379
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200380 hci_dev_lock(hdev);
381
Mikel Astizfa1bd912012-08-09 09:52:29 +0200382 if (status) {
Johan Hedberg2d7cee52011-11-07 22:16:03 +0200383 hdev->discov_timeout = 0;
384 goto done;
385 }
386
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300387 if (param & SCAN_INQUIRY)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200388 set_bit(HCI_ISCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300389 else
390 clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200391
Johan Hedberg031547d2014-07-10 12:09:06 +0300392 if (param & SCAN_PAGE)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200393 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300394 else
Johan Hedberg204e3992014-07-28 15:45:31 +0300395 clear_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200396
397done:
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200398 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200399}
400
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -0800401static void hci_cc_set_event_filter(struct hci_dev *hdev, struct sk_buff *skb)
402{
403 __u8 status = *((__u8 *)skb->data);
404 struct hci_cp_set_event_filter *cp;
405 void *sent;
406
407 BT_DBG("%s status 0x%2.2x", hdev->name, status);
408
409 if (status)
410 return;
411
412 sent = hci_sent_cmd_data(hdev, HCI_OP_SET_EVENT_FLT);
413 if (!sent)
414 return;
415
416 cp = (struct hci_cp_set_event_filter *)sent;
417
418 if (cp->flt_type == HCI_FLT_CLEAR_ALL)
419 hci_dev_clear_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
420 else
421 hci_dev_set_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
422}
423
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200424static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
425{
426 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
427
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300428 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200429
430 if (rp->status)
431 return;
432
433 memcpy(hdev->dev_class, rp->dev_class, 3);
434
435 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300436 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200437}
438
439static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
440{
441 __u8 status = *((__u8 *) skb->data);
442 void *sent;
443
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300444 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200445
446 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
447 if (!sent)
448 return;
449
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100450 hci_dev_lock(hdev);
451
452 if (status == 0)
453 memcpy(hdev->dev_class, sent, 3);
454
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700455 if (hci_dev_test_flag(hdev, HCI_MGMT))
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100456 mgmt_set_class_of_dev_complete(hdev, sent, status);
457
458 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200459}
460
461static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
462{
463 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200465
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300466 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200467
468 if (rp->status)
469 return;
470
471 setting = __le16_to_cpu(rp->voice_setting);
472
Marcel Holtmannf383f272008-07-14 20:13:47 +0200473 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200474 return;
475
476 hdev->voice_setting = setting;
477
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300478 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200479
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200480 if (hdev->notify)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200481 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200482}
483
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300484static void hci_cc_write_voice_setting(struct hci_dev *hdev,
485 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200486{
487 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200488 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 void *sent;
490
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300491 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Marcel Holtmannf383f272008-07-14 20:13:47 +0200493 if (status)
494 return;
495
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200496 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
497 if (!sent)
498 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Marcel Holtmannf383f272008-07-14 20:13:47 +0200500 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Marcel Holtmannf383f272008-07-14 20:13:47 +0200502 if (hdev->voice_setting == setting)
503 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Marcel Holtmannf383f272008-07-14 20:13:47 +0200505 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300507 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200508
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200509 if (hdev->notify)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200510 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700513static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
514 struct sk_buff *skb)
515{
516 struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
517
518 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
519
520 if (rp->status)
521 return;
522
523 hdev->num_iac = rp->num_iac;
524
525 BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
526}
527
Marcel Holtmann333140b2008-07-14 20:13:48 +0200528static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
529{
530 __u8 status = *((__u8 *) skb->data);
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300531 struct hci_cp_write_ssp_mode *sent;
Marcel Holtmann333140b2008-07-14 20:13:48 +0200532
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300533 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200534
Marcel Holtmann333140b2008-07-14 20:13:48 +0200535 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
536 if (!sent)
537 return;
538
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530539 hci_dev_lock(hdev);
540
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300541 if (!status) {
542 if (sent->mode)
Johan Hedbergcad718e2013-04-17 15:00:51 +0300543 hdev->features[1][0] |= LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300544 else
Johan Hedbergcad718e2013-04-17 15:00:51 +0300545 hdev->features[1][0] &= ~LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300546 }
547
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700548 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300549 mgmt_ssp_enable_complete(hdev, sent->mode, status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200550 else if (!status) {
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300551 if (sent->mode)
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700552 hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200553 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700554 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200555 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530556
557 hci_dev_unlock(hdev);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200558}
559
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800560static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
561{
562 u8 status = *((u8 *) skb->data);
563 struct hci_cp_write_sc_support *sent;
564
565 BT_DBG("%s status 0x%2.2x", hdev->name, status);
566
567 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
568 if (!sent)
569 return;
570
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530571 hci_dev_lock(hdev);
572
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800573 if (!status) {
574 if (sent->support)
575 hdev->features[1][0] |= LMP_HOST_SC;
576 else
577 hdev->features[1][0] &= ~LMP_HOST_SC;
578 }
579
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700580 if (!hci_dev_test_flag(hdev, HCI_MGMT) && !status) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800581 if (sent->support)
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700582 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800583 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700584 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800585 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530586
587 hci_dev_unlock(hdev);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800588}
589
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200590static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
591{
592 struct hci_rp_read_local_version *rp = (void *) skb->data;
593
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300594 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200595
596 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200597 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200598
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700599 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
600 hci_dev_test_flag(hdev, HCI_CONFIG)) {
Marcel Holtmann0d5551f2013-10-18 12:04:50 -0700601 hdev->hci_ver = rp->hci_ver;
602 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
603 hdev->lmp_ver = rp->lmp_ver;
604 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
605 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
606 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200607}
608
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300609static void hci_cc_read_local_commands(struct hci_dev *hdev,
610 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200611{
612 struct hci_rp_read_local_commands *rp = (void *) skb->data;
613
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300614 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200615
Marcel Holtmann6a070e62013-10-31 04:54:33 -0700616 if (rp->status)
617 return;
618
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700619 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
620 hci_dev_test_flag(hdev, HCI_CONFIG))
Johan Hedberg2177bab2013-03-05 20:37:43 +0200621 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200622}
623
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +0530624static void hci_cc_read_auth_payload_timeout(struct hci_dev *hdev,
625 struct sk_buff *skb)
626{
627 struct hci_rp_read_auth_payload_to *rp = (void *)skb->data;
628 struct hci_conn *conn;
629
630 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
631
632 if (rp->status)
633 return;
634
635 hci_dev_lock(hdev);
636
637 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
638 if (conn)
639 conn->auth_payload_timeout = __le16_to_cpu(rp->timeout);
640
641 hci_dev_unlock(hdev);
642}
643
644static void hci_cc_write_auth_payload_timeout(struct hci_dev *hdev,
645 struct sk_buff *skb)
646{
647 struct hci_rp_write_auth_payload_to *rp = (void *)skb->data;
648 struct hci_conn *conn;
649 void *sent;
650
651 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
652
653 if (rp->status)
654 return;
655
656 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO);
657 if (!sent)
658 return;
659
660 hci_dev_lock(hdev);
661
662 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
663 if (conn)
664 conn->auth_payload_timeout = get_unaligned_le16(sent + 2);
665
666 hci_dev_unlock(hdev);
667}
668
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300669static void hci_cc_read_local_features(struct hci_dev *hdev,
670 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200671{
672 struct hci_rp_read_local_features *rp = (void *) skb->data;
673
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300674 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200675
676 if (rp->status)
677 return;
678
679 memcpy(hdev->features, rp->features, 8);
680
681 /* Adjust default settings according to features
682 * supported by device. */
683
Johan Hedbergcad718e2013-04-17 15:00:51 +0300684 if (hdev->features[0][0] & LMP_3SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200685 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
686
Johan Hedbergcad718e2013-04-17 15:00:51 +0300687 if (hdev->features[0][0] & LMP_5SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200688 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
689
Johan Hedbergcad718e2013-04-17 15:00:51 +0300690 if (hdev->features[0][1] & LMP_HV2) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200691 hdev->pkt_type |= (HCI_HV2);
692 hdev->esco_type |= (ESCO_HV2);
693 }
694
Johan Hedbergcad718e2013-04-17 15:00:51 +0300695 if (hdev->features[0][1] & LMP_HV3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200696 hdev->pkt_type |= (HCI_HV3);
697 hdev->esco_type |= (ESCO_HV3);
698 }
699
Andre Guedes45db810f2012-07-24 15:03:49 -0300700 if (lmp_esco_capable(hdev))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200701 hdev->esco_type |= (ESCO_EV3);
702
Johan Hedbergcad718e2013-04-17 15:00:51 +0300703 if (hdev->features[0][4] & LMP_EV4)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200704 hdev->esco_type |= (ESCO_EV4);
705
Johan Hedbergcad718e2013-04-17 15:00:51 +0300706 if (hdev->features[0][4] & LMP_EV5)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200707 hdev->esco_type |= (ESCO_EV5);
708
Johan Hedbergcad718e2013-04-17 15:00:51 +0300709 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100710 hdev->esco_type |= (ESCO_2EV3);
711
Johan Hedbergcad718e2013-04-17 15:00:51 +0300712 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100713 hdev->esco_type |= (ESCO_3EV3);
714
Johan Hedbergcad718e2013-04-17 15:00:51 +0300715 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100716 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200717}
718
Andre Guedes971e3a42011-06-30 19:20:52 -0300719static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300720 struct sk_buff *skb)
Andre Guedes971e3a42011-06-30 19:20:52 -0300721{
722 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
723
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300724 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andre Guedes971e3a42011-06-30 19:20:52 -0300725
726 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200727 return;
Andre Guedes971e3a42011-06-30 19:20:52 -0300728
Marcel Holtmann57af75a2013-10-18 12:04:47 -0700729 if (hdev->max_page < rp->max_page)
730 hdev->max_page = rp->max_page;
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300731
Johan Hedbergcad718e2013-04-17 15:00:51 +0300732 if (rp->page < HCI_MAX_PAGES)
733 memcpy(hdev->features[rp->page], rp->features, 8);
Andre Guedes971e3a42011-06-30 19:20:52 -0300734}
735
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200736static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300737 struct sk_buff *skb)
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200738{
739 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
740
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300741 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200742
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200743 if (rp->status)
744 return;
745
746 hdev->flow_ctl_mode = rp->mode;
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200747}
748
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200749static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
750{
751 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
752
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300753 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200754
755 if (rp->status)
756 return;
757
758 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
759 hdev->sco_mtu = rp->sco_mtu;
760 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
761 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
762
763 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
764 hdev->sco_mtu = 64;
765 hdev->sco_pkts = 8;
766 }
767
768 hdev->acl_cnt = hdev->acl_pkts;
769 hdev->sco_cnt = hdev->sco_pkts;
770
Gustavo Padovan807deac2012-05-17 00:36:24 -0300771 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
772 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200773}
774
775static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
776{
777 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
778
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300779 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200780
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200781 if (rp->status)
782 return;
783
784 if (test_bit(HCI_INIT, &hdev->flags))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200785 bacpy(&hdev->bdaddr, &rp->bdaddr);
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200786
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700787 if (hci_dev_test_flag(hdev, HCI_SETUP))
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200788 bacpy(&hdev->setup_addr, &rp->bdaddr);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200789}
790
Marcel Holtmanna47903602020-04-03 21:44:04 +0200791static void hci_cc_read_local_pairing_opts(struct hci_dev *hdev,
792 struct sk_buff *skb)
793{
794 struct hci_rp_read_local_pairing_opts *rp = (void *) skb->data;
795
796 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
797
798 if (rp->status)
799 return;
800
801 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
802 hci_dev_test_flag(hdev, HCI_CONFIG)) {
803 hdev->pairing_opts = rp->pairing_opts;
804 hdev->max_enc_key_size = rp->max_key_size;
805 }
806}
807
Johan Hedbergf332ec62013-03-15 17:07:11 -0500808static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
809 struct sk_buff *skb)
810{
811 struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
812
813 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
814
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200815 if (rp->status)
816 return;
817
818 if (test_bit(HCI_INIT, &hdev->flags)) {
Johan Hedbergf332ec62013-03-15 17:07:11 -0500819 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
820 hdev->page_scan_window = __le16_to_cpu(rp->window);
821 }
822}
823
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500824static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
825 struct sk_buff *skb)
826{
827 u8 status = *((u8 *) skb->data);
828 struct hci_cp_write_page_scan_activity *sent;
829
830 BT_DBG("%s status 0x%2.2x", hdev->name, status);
831
832 if (status)
833 return;
834
835 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
836 if (!sent)
837 return;
838
839 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
840 hdev->page_scan_window = __le16_to_cpu(sent->window);
841}
842
Johan Hedbergf332ec62013-03-15 17:07:11 -0500843static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
844 struct sk_buff *skb)
845{
846 struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
847
848 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
849
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200850 if (rp->status)
851 return;
852
853 if (test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergf332ec62013-03-15 17:07:11 -0500854 hdev->page_scan_type = rp->type;
855}
856
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500857static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
858 struct sk_buff *skb)
859{
860 u8 status = *((u8 *) skb->data);
861 u8 *type;
862
863 BT_DBG("%s status 0x%2.2x", hdev->name, status);
864
865 if (status)
866 return;
867
868 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
869 if (type)
870 hdev->page_scan_type = *type;
871}
872
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200873static void hci_cc_read_data_block_size(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300874 struct sk_buff *skb)
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200875{
876 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
877
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300878 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200879
880 if (rp->status)
881 return;
882
883 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
884 hdev->block_len = __le16_to_cpu(rp->block_len);
885 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
886
887 hdev->block_cnt = hdev->num_blocks;
888
889 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300890 hdev->block_cnt, hdev->block_len);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200891}
892
Johan Hedberg33f35722014-06-28 17:54:06 +0300893static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
894{
895 struct hci_rp_read_clock *rp = (void *) skb->data;
896 struct hci_cp_read_clock *cp;
897 struct hci_conn *conn;
898
899 BT_DBG("%s", hdev->name);
900
901 if (skb->len < sizeof(*rp))
902 return;
903
904 if (rp->status)
905 return;
906
907 hci_dev_lock(hdev);
908
909 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
910 if (!cp)
911 goto unlock;
912
913 if (cp->which == 0x00) {
914 hdev->clock = le32_to_cpu(rp->clock);
915 goto unlock;
916 }
917
918 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
919 if (conn) {
920 conn->clock = le32_to_cpu(rp->clock);
921 conn->clock_accuracy = le16_to_cpu(rp->accuracy);
922 }
923
924unlock:
925 hci_dev_unlock(hdev);
926}
927
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300928static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300929 struct sk_buff *skb)
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300930{
931 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
932
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300933 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300934
935 if (rp->status)
Arron Wang83927882015-07-24 17:10:16 +0800936 return;
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300937
938 hdev->amp_status = rp->amp_status;
939 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
940 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
941 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
942 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
943 hdev->amp_type = rp->amp_type;
944 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
945 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
946 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
947 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300948}
949
Johan Hedbergd5859e22011-01-25 01:19:58 +0200950static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300951 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200952{
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700953 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200954
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300955 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200956
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200957 if (rp->status)
958 return;
959
960 hdev->inq_tx_power = rp->tx_power;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200961}
962
Alain Michaud00bce3f2020-03-05 16:14:59 +0000963static void hci_cc_read_def_err_data_reporting(struct hci_dev *hdev,
964 struct sk_buff *skb)
965{
966 struct hci_rp_read_def_err_data_reporting *rp = (void *)skb->data;
967
968 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
969
970 if (rp->status)
971 return;
972
973 hdev->err_data_reporting = rp->err_data_reporting;
974}
975
976static void hci_cc_write_def_err_data_reporting(struct hci_dev *hdev,
977 struct sk_buff *skb)
978{
979 __u8 status = *((__u8 *)skb->data);
980 struct hci_cp_write_def_err_data_reporting *cp;
981
982 BT_DBG("%s status 0x%2.2x", hdev->name, status);
983
984 if (status)
985 return;
986
987 cp = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING);
988 if (!cp)
989 return;
990
991 hdev->err_data_reporting = cp->err_data_reporting;
992}
993
Johan Hedberg980e1a52011-01-22 06:10:07 +0200994static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
995{
996 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
997 struct hci_cp_pin_code_reply *cp;
998 struct hci_conn *conn;
999
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001000 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001001
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001002 hci_dev_lock(hdev);
1003
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001004 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001005 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001006
Mikel Astizfa1bd912012-08-09 09:52:29 +02001007 if (rp->status)
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001008 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001009
1010 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
1011 if (!cp)
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001012 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001013
1014 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1015 if (conn)
1016 conn->pin_length = cp->pin_len;
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001017
1018unlock:
1019 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001020}
1021
1022static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
1023{
1024 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
1025
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001026 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001027
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001028 hci_dev_lock(hdev);
1029
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001030 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001031 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001032 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001033
1034 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001035}
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001036
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001037static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
1038 struct sk_buff *skb)
1039{
1040 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
1041
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001042 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001043
1044 if (rp->status)
1045 return;
1046
1047 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
1048 hdev->le_pkts = rp->le_max_pkt;
1049
1050 hdev->le_cnt = hdev->le_pkts;
1051
1052 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001053}
Johan Hedberg980e1a52011-01-22 06:10:07 +02001054
Johan Hedberg60e77322013-01-22 14:01:59 +02001055static void hci_cc_le_read_local_features(struct hci_dev *hdev,
1056 struct sk_buff *skb)
1057{
1058 struct hci_rp_le_read_local_features *rp = (void *) skb->data;
1059
1060 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1061
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001062 if (rp->status)
1063 return;
1064
1065 memcpy(hdev->le_features, rp->features, 8);
Johan Hedberg60e77322013-01-22 14:01:59 +02001066}
1067
Johan Hedberg8fa19092012-10-19 20:57:49 +03001068static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
1069 struct sk_buff *skb)
1070{
1071 struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
1072
1073 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1074
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001075 if (rp->status)
1076 return;
1077
1078 hdev->adv_tx_power = rp->tx_power;
Johan Hedberg8fa19092012-10-19 20:57:49 +03001079}
1080
Johan Hedberga5c29682011-02-19 12:05:57 -03001081static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
1082{
1083 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1084
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001085 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -03001086
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001087 hci_dev_lock(hdev);
1088
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001089 if (hci_dev_test_flag(hdev, HCI_MGMT))
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001090 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
1091 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001092
1093 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03001094}
1095
1096static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001097 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03001098{
1099 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1100
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001101 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -03001102
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001103 hci_dev_lock(hdev);
1104
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001105 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001106 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001107 ACL_LINK, 0, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001108
1109 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03001110}
1111
Brian Gix1143d452011-11-23 08:28:34 -08001112static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
1113{
1114 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1115
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001116 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001117
1118 hci_dev_lock(hdev);
1119
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001120 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg272d90d2012-02-09 15:26:12 +02001121 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001122 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001123
1124 hci_dev_unlock(hdev);
1125}
1126
1127static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001128 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08001129{
1130 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1131
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001132 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001133
1134 hci_dev_lock(hdev);
1135
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001136 if (hci_dev_test_flag(hdev, HCI_MGMT))
Brian Gix1143d452011-11-23 08:28:34 -08001137 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001138 ACL_LINK, 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001139
1140 hci_dev_unlock(hdev);
1141}
1142
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001143static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
1144 struct sk_buff *skb)
Szymon Jancc35938b2011-03-22 13:12:21 +01001145{
1146 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
1147
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001148 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001149}
1150
1151static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
1152 struct sk_buff *skb)
1153{
1154 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
1155
1156 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Szymon Jancc35938b2011-03-22 13:12:21 +01001157}
1158
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001159static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
1160{
1161 __u8 status = *((__u8 *) skb->data);
1162 bdaddr_t *sent;
1163
1164 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1165
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001166 if (status)
1167 return;
1168
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001169 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1170 if (!sent)
1171 return;
1172
1173 hci_dev_lock(hdev);
1174
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001175 bacpy(&hdev->random_addr, sent);
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001176
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001177 if (!bacmp(&hdev->rpa, sent)) {
1178 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
1179 queue_delayed_work(hdev->workqueue, &hdev->rpa_expired,
1180 secs_to_jiffies(hdev->rpa_timeout));
1181 }
1182
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001183 hci_dev_unlock(hdev);
1184}
1185
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05301186static void hci_cc_le_set_default_phy(struct hci_dev *hdev, struct sk_buff *skb)
1187{
1188 __u8 status = *((__u8 *) skb->data);
1189 struct hci_cp_le_set_default_phy *cp;
1190
1191 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1192
1193 if (status)
1194 return;
1195
1196 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_DEFAULT_PHY);
1197 if (!cp)
1198 return;
1199
1200 hci_dev_lock(hdev);
1201
1202 hdev->le_tx_def_phys = cp->tx_phys;
1203 hdev->le_rx_def_phys = cp->rx_phys;
1204
1205 hci_dev_unlock(hdev);
1206}
1207
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301208static void hci_cc_le_set_adv_set_random_addr(struct hci_dev *hdev,
1209 struct sk_buff *skb)
1210{
1211 __u8 status = *((__u8 *) skb->data);
1212 struct hci_cp_le_set_adv_set_rand_addr *cp;
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001213 struct adv_info *adv;
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301214
1215 if (status)
1216 return;
1217
1218 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR);
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001219 /* Update only in case the adv instance since handle 0x00 shall be using
1220 * HCI_OP_LE_SET_RANDOM_ADDR since that allows both extended and
1221 * non-extended adverting.
1222 */
1223 if (!cp || !cp->handle)
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301224 return;
1225
1226 hci_dev_lock(hdev);
1227
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001228 adv = hci_find_adv_instance(hdev, cp->handle);
1229 if (adv) {
1230 bacpy(&adv->random_addr, &cp->bdaddr);
1231 if (!bacmp(&hdev->rpa, &cp->bdaddr)) {
1232 adv->rpa_expired = false;
1233 queue_delayed_work(hdev->workqueue,
1234 &adv->rpa_expired_cb,
1235 secs_to_jiffies(hdev->rpa_timeout));
1236 }
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301237 }
1238
1239 hci_dev_unlock(hdev);
1240}
1241
Daniel Winkler7c395ea2020-12-03 12:12:51 -08001242static void hci_cc_le_read_transmit_power(struct hci_dev *hdev,
1243 struct sk_buff *skb)
1244{
1245 struct hci_rp_le_read_transmit_power *rp = (void *)skb->data;
1246
1247 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1248
1249 if (rp->status)
1250 return;
1251
1252 hdev->min_le_tx_power = rp->min_le_tx_power;
1253 hdev->max_le_tx_power = rp->max_le_tx_power;
1254}
1255
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001256static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1257{
1258 __u8 *sent, status = *((__u8 *) skb->data);
1259
1260 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1261
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001262 if (status)
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001263 return;
1264
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001265 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1266 if (!sent)
Johan Hedberg3c857752014-03-25 10:30:49 +02001267 return;
1268
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001269 hci_dev_lock(hdev);
1270
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001271 /* If we're doing connection initiation as peripheral. Set a
Johan Hedberg3c857752014-03-25 10:30:49 +02001272 * timeout in case something goes wrong.
1273 */
1274 if (*sent) {
1275 struct hci_conn *conn;
1276
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001277 hci_dev_set_flag(hdev, HCI_LE_ADV);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001278
Jakub Pawlowskie7d9ab72015-08-07 20:22:52 +02001279 conn = hci_lookup_le_connect(hdev);
Johan Hedberg3c857752014-03-25 10:30:49 +02001280 if (conn)
1281 queue_delayed_work(hdev->workqueue,
1282 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001283 conn->conn_timeout);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001284 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001285 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedberg3c857752014-03-25 10:30:49 +02001286 }
1287
Johan Hedberg04b4edc2013-03-15 17:07:01 -05001288 hci_dev_unlock(hdev);
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001289}
1290
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301291static void hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev,
1292 struct sk_buff *skb)
1293{
1294 struct hci_cp_le_set_ext_adv_enable *cp;
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001295 struct hci_cp_ext_adv_set *set;
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301296 __u8 status = *((__u8 *) skb->data);
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001297 struct adv_info *adv = NULL, *n;
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301298
1299 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1300
1301 if (status)
1302 return;
1303
1304 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE);
1305 if (!cp)
1306 return;
1307
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001308 set = (void *)cp->data;
1309
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301310 hci_dev_lock(hdev);
1311
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001312 if (cp->num_of_sets)
1313 adv = hci_find_adv_instance(hdev, set->handle);
1314
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301315 if (cp->enable) {
1316 struct hci_conn *conn;
1317
1318 hci_dev_set_flag(hdev, HCI_LE_ADV);
1319
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001320 if (adv)
1321 adv->enabled = true;
1322
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301323 conn = hci_lookup_le_connect(hdev);
1324 if (conn)
1325 queue_delayed_work(hdev->workqueue,
1326 &conn->le_conn_timeout,
1327 conn->conn_timeout);
Jaganath Kanakkassery45b77492018-07-19 17:09:43 +05301328 } else {
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001329 if (adv) {
1330 adv->enabled = false;
1331 /* If just one instance was disabled check if there are
1332 * any other instance enabled before clearing HCI_LE_ADV
1333 */
1334 list_for_each_entry_safe(adv, n, &hdev->adv_instances,
1335 list) {
1336 if (adv->enabled)
1337 goto unlock;
1338 }
1339 } else {
1340 /* All instances shall be considered disabled */
1341 list_for_each_entry_safe(adv, n, &hdev->adv_instances,
1342 list)
1343 adv->enabled = false;
1344 }
1345
Jaganath Kanakkassery45b77492018-07-19 17:09:43 +05301346 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301347 }
1348
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001349unlock:
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301350 hci_dev_unlock(hdev);
1351}
1352
Marcel Holtmann533553f2014-03-21 12:18:10 -07001353static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1354{
1355 struct hci_cp_le_set_scan_param *cp;
1356 __u8 status = *((__u8 *) skb->data);
1357
1358 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1359
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001360 if (status)
1361 return;
1362
Marcel Holtmann533553f2014-03-21 12:18:10 -07001363 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1364 if (!cp)
1365 return;
1366
1367 hci_dev_lock(hdev);
1368
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001369 hdev->le_scan_type = cp->type;
Marcel Holtmann533553f2014-03-21 12:18:10 -07001370
1371 hci_dev_unlock(hdev);
1372}
1373
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301374static void hci_cc_le_set_ext_scan_param(struct hci_dev *hdev,
1375 struct sk_buff *skb)
1376{
1377 struct hci_cp_le_set_ext_scan_params *cp;
1378 __u8 status = *((__u8 *) skb->data);
1379 struct hci_cp_le_scan_phy_params *phy_param;
1380
1381 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1382
1383 if (status)
1384 return;
1385
1386 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS);
1387 if (!cp)
1388 return;
1389
1390 phy_param = (void *)cp->data;
1391
1392 hci_dev_lock(hdev);
1393
1394 hdev->le_scan_type = phy_param->type;
1395
1396 hci_dev_unlock(hdev);
1397}
1398
Johan Hedbergb9a63282014-03-25 10:51:52 +02001399static bool has_pending_adv_report(struct hci_dev *hdev)
1400{
1401 struct discovery_state *d = &hdev->discovery;
1402
1403 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1404}
1405
1406static void clear_pending_adv_report(struct hci_dev *hdev)
1407{
1408 struct discovery_state *d = &hdev->discovery;
1409
1410 bacpy(&d->last_adv_addr, BDADDR_ANY);
1411 d->last_adv_data_len = 0;
1412}
1413
1414static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001415 u8 bdaddr_type, s8 rssi, u32 flags,
1416 u8 *data, u8 len)
Johan Hedbergb9a63282014-03-25 10:51:52 +02001417{
1418 struct discovery_state *d = &hdev->discovery;
1419
Alain Michauda2ec9052020-07-27 20:48:55 +00001420 if (len > HCI_MAX_AD_LENGTH)
1421 return;
1422
Johan Hedbergb9a63282014-03-25 10:51:52 +02001423 bacpy(&d->last_adv_addr, bdaddr);
1424 d->last_adv_addr_type = bdaddr_type;
Johan Hedbergff5cd292014-03-25 14:40:52 +02001425 d->last_adv_rssi = rssi;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001426 d->last_adv_flags = flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02001427 memcpy(d->last_adv_data, data, len);
1428 d->last_adv_data_len = len;
1429}
1430
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301431static void le_set_scan_enable_complete(struct hci_dev *hdev, u8 enable)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001432{
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301433 hci_dev_lock(hdev);
1434
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301435 switch (enable) {
Andre Guedes76a388be2013-04-04 20:21:02 -03001436 case LE_SCAN_ENABLE:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001437 hci_dev_set_flag(hdev, HCI_LE_SCAN);
Johan Hedbergb9a63282014-03-25 10:51:52 +02001438 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1439 clear_pending_adv_report(hdev);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001440 break;
1441
Andre Guedes76a388be2013-04-04 20:21:02 -03001442 case LE_SCAN_DISABLE:
Johan Hedbergb9a63282014-03-25 10:51:52 +02001443 /* We do this here instead of when setting DISCOVERY_STOPPED
1444 * since the latter would potentially require waiting for
1445 * inquiry to stop too.
1446 */
1447 if (has_pending_adv_report(hdev)) {
1448 struct discovery_state *d = &hdev->discovery;
1449
1450 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001451 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001452 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001453 d->last_adv_data,
Johan Hedbergb9a63282014-03-25 10:51:52 +02001454 d->last_adv_data_len, NULL, 0);
1455 }
1456
Johan Hedberg317ac8c2014-02-28 20:26:12 +02001457 /* Cancel this timer so that we don't try to disable scanning
1458 * when it's already disabled.
1459 */
1460 cancel_delayed_work(&hdev->le_scan_disable);
1461
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001462 hci_dev_clear_flag(hdev, HCI_LE_SCAN);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001463
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001464 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1465 * interrupted scanning due to a connect request. Mark
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001466 * therefore discovery as stopped. If this was not
1467 * because of a connect request advertising might have
1468 * been disabled because of active scanning, so
1469 * re-enable it again if necessary.
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001470 */
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001471 if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001472 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001473 else if (!hci_dev_test_flag(hdev, HCI_LE_ADV) &&
Johan Hedberg34722272014-07-08 16:05:05 +03001474 hdev->discovery.state == DISCOVERY_FINDING)
Johan Hedbergf2252572015-11-18 12:49:20 +02001475 hci_req_reenable_advertising(hdev);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001476
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001477 break;
1478
1479 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001480 bt_dev_err(hdev, "use of reserved LE_Scan_Enable param %d",
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301481 enable);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001482 break;
Andre Guedes35815082011-05-26 16:23:53 -03001483 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301484
1485 hci_dev_unlock(hdev);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001486}
1487
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301488static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
1489 struct sk_buff *skb)
1490{
1491 struct hci_cp_le_set_scan_enable *cp;
1492 __u8 status = *((__u8 *) skb->data);
1493
1494 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1495
1496 if (status)
1497 return;
1498
1499 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1500 if (!cp)
1501 return;
1502
1503 le_set_scan_enable_complete(hdev, cp->enable);
1504}
1505
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301506static void hci_cc_le_set_ext_scan_enable(struct hci_dev *hdev,
1507 struct sk_buff *skb)
1508{
1509 struct hci_cp_le_set_ext_scan_enable *cp;
1510 __u8 status = *((__u8 *) skb->data);
1511
1512 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1513
1514 if (status)
1515 return;
1516
1517 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE);
1518 if (!cp)
1519 return;
1520
1521 le_set_scan_enable_complete(hdev, cp->enable);
1522}
1523
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05301524static void hci_cc_le_read_num_adv_sets(struct hci_dev *hdev,
1525 struct sk_buff *skb)
1526{
1527 struct hci_rp_le_read_num_supported_adv_sets *rp = (void *) skb->data;
1528
1529 BT_DBG("%s status 0x%2.2x No of Adv sets %u", hdev->name, rp->status,
1530 rp->num_of_sets);
1531
1532 if (rp->status)
1533 return;
1534
1535 hdev->le_num_of_adv_sets = rp->num_of_sets;
1536}
1537
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001538static void hci_cc_le_read_accept_list_size(struct hci_dev *hdev,
1539 struct sk_buff *skb)
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001540{
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001541 struct hci_rp_le_read_accept_list_size *rp = (void *)skb->data;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001542
1543 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1544
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001545 if (rp->status)
1546 return;
1547
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001548 hdev->le_accept_list_size = rp->size;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001549}
1550
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001551static void hci_cc_le_clear_accept_list(struct hci_dev *hdev,
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001552 struct sk_buff *skb)
1553{
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001554 __u8 status = *((__u8 *) skb->data);
1555
1556 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1557
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001558 if (status)
1559 return;
1560
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001561 hci_bdaddr_list_clear(&hdev->le_accept_list);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001562}
1563
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001564static void hci_cc_le_add_to_accept_list(struct hci_dev *hdev,
1565 struct sk_buff *skb)
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001566{
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001567 struct hci_cp_le_add_to_accept_list *sent;
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001568 __u8 status = *((__u8 *) skb->data);
1569
1570 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1571
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001572 if (status)
1573 return;
1574
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001575 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001576 if (!sent)
1577 return;
1578
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001579 hci_bdaddr_list_add(&hdev->le_accept_list, &sent->bdaddr,
1580 sent->bdaddr_type);
1581}
1582
1583static void hci_cc_le_del_from_accept_list(struct hci_dev *hdev,
1584 struct sk_buff *skb)
1585{
1586 struct hci_cp_le_del_from_accept_list *sent;
1587 __u8 status = *((__u8 *) skb->data);
1588
1589 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1590
1591 if (status)
1592 return;
1593
1594 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST);
1595 if (!sent)
1596 return;
1597
1598 hci_bdaddr_list_del(&hdev->le_accept_list, &sent->bdaddr,
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001599 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001600}
1601
Johan Hedberg9b008c02013-01-22 14:02:01 +02001602static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
1603 struct sk_buff *skb)
1604{
1605 struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
1606
1607 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1608
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001609 if (rp->status)
1610 return;
1611
1612 memcpy(hdev->le_states, rp->le_states, 8);
Johan Hedberg9b008c02013-01-22 14:02:01 +02001613}
1614
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01001615static void hci_cc_le_read_def_data_len(struct hci_dev *hdev,
1616 struct sk_buff *skb)
1617{
1618 struct hci_rp_le_read_def_data_len *rp = (void *) skb->data;
1619
1620 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1621
1622 if (rp->status)
1623 return;
1624
1625 hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
1626 hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
1627}
1628
1629static void hci_cc_le_write_def_data_len(struct hci_dev *hdev,
1630 struct sk_buff *skb)
1631{
1632 struct hci_cp_le_write_def_data_len *sent;
1633 __u8 status = *((__u8 *) skb->data);
1634
1635 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1636
1637 if (status)
1638 return;
1639
1640 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
1641 if (!sent)
1642 return;
1643
1644 hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
1645 hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
1646}
1647
Ankit Navikb950aa82018-08-17 07:29:19 +05301648static void hci_cc_le_add_to_resolv_list(struct hci_dev *hdev,
1649 struct sk_buff *skb)
1650{
1651 struct hci_cp_le_add_to_resolv_list *sent;
1652 __u8 status = *((__u8 *) skb->data);
1653
1654 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1655
1656 if (status)
1657 return;
1658
1659 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST);
1660 if (!sent)
1661 return;
1662
1663 hci_bdaddr_list_add_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
1664 sent->bdaddr_type, sent->peer_irk,
1665 sent->local_irk);
1666}
1667
1668static void hci_cc_le_del_from_resolv_list(struct hci_dev *hdev,
1669 struct sk_buff *skb)
1670{
1671 struct hci_cp_le_del_from_resolv_list *sent;
1672 __u8 status = *((__u8 *) skb->data);
1673
1674 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1675
1676 if (status)
1677 return;
1678
1679 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST);
1680 if (!sent)
1681 return;
1682
1683 hci_bdaddr_list_del_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
1684 sent->bdaddr_type);
1685}
1686
Ankit Navik545f2592018-06-29 12:13:20 +05301687static void hci_cc_le_clear_resolv_list(struct hci_dev *hdev,
1688 struct sk_buff *skb)
1689{
1690 __u8 status = *((__u8 *) skb->data);
1691
1692 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1693
1694 if (status)
1695 return;
1696
1697 hci_bdaddr_list_clear(&hdev->le_resolv_list);
1698}
1699
Ankit Navikcfdb0c22018-06-29 12:12:50 +05301700static void hci_cc_le_read_resolv_list_size(struct hci_dev *hdev,
1701 struct sk_buff *skb)
1702{
1703 struct hci_rp_le_read_resolv_list_size *rp = (void *) skb->data;
1704
1705 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1706
1707 if (rp->status)
1708 return;
1709
1710 hdev->le_resolv_list_size = rp->size;
1711}
1712
Ankit Navikaa12af72018-08-07 13:16:35 +05301713static void hci_cc_le_set_addr_resolution_enable(struct hci_dev *hdev,
1714 struct sk_buff *skb)
1715{
1716 __u8 *sent, status = *((__u8 *) skb->data);
1717
1718 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1719
1720 if (status)
1721 return;
1722
1723 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE);
1724 if (!sent)
1725 return;
1726
1727 hci_dev_lock(hdev);
1728
1729 if (*sent)
1730 hci_dev_set_flag(hdev, HCI_LL_RPA_RESOLUTION);
1731 else
1732 hci_dev_clear_flag(hdev, HCI_LL_RPA_RESOLUTION);
1733
1734 hci_dev_unlock(hdev);
1735}
1736
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01001737static void hci_cc_le_read_max_data_len(struct hci_dev *hdev,
1738 struct sk_buff *skb)
1739{
1740 struct hci_rp_le_read_max_data_len *rp = (void *) skb->data;
1741
1742 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1743
1744 if (rp->status)
1745 return;
1746
1747 hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
1748 hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
1749 hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
1750 hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
1751}
1752
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001753static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1754 struct sk_buff *skb)
Andre Guedesf9b49302011-06-30 19:20:53 -03001755{
Johan Hedberg06199cf2012-02-22 16:37:11 +02001756 struct hci_cp_write_le_host_supported *sent;
Andre Guedesf9b49302011-06-30 19:20:53 -03001757 __u8 status = *((__u8 *) skb->data);
1758
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001759 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesf9b49302011-06-30 19:20:53 -03001760
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001761 if (status)
1762 return;
1763
Johan Hedberg06199cf2012-02-22 16:37:11 +02001764 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001765 if (!sent)
Andre Guedesf9b49302011-06-30 19:20:53 -03001766 return;
1767
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301768 hci_dev_lock(hdev);
1769
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001770 if (sent->le) {
1771 hdev->features[1][0] |= LMP_HOST_LE;
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001772 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001773 } else {
1774 hdev->features[1][0] &= ~LMP_HOST_LE;
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001775 hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
1776 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001777 }
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001778
1779 if (sent->simul)
1780 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
1781 else
1782 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301783
1784 hci_dev_unlock(hdev);
Andre Guedesf9b49302011-06-30 19:20:53 -03001785}
1786
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02001787static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1788{
1789 struct hci_cp_le_set_adv_param *cp;
1790 u8 status = *((u8 *) skb->data);
1791
1792 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1793
1794 if (status)
1795 return;
1796
1797 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
1798 if (!cp)
1799 return;
1800
1801 hci_dev_lock(hdev);
1802 hdev->adv_addr_type = cp->own_address_type;
1803 hci_dev_unlock(hdev);
1804}
1805
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301806static void hci_cc_set_ext_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1807{
1808 struct hci_rp_le_set_ext_adv_params *rp = (void *) skb->data;
1809 struct hci_cp_le_set_ext_adv_params *cp;
1810 struct adv_info *adv_instance;
1811
1812 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1813
1814 if (rp->status)
1815 return;
1816
1817 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS);
1818 if (!cp)
1819 return;
1820
1821 hci_dev_lock(hdev);
1822 hdev->adv_addr_type = cp->own_addr_type;
Daniel Winkler25e70882021-04-05 16:33:04 -07001823 if (!cp->handle) {
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301824 /* Store in hdev for instance 0 */
1825 hdev->adv_tx_power = rp->tx_power;
1826 } else {
Daniel Winkler25e70882021-04-05 16:33:04 -07001827 adv_instance = hci_find_adv_instance(hdev, cp->handle);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301828 if (adv_instance)
1829 adv_instance->tx_power = rp->tx_power;
1830 }
Jaganath Kanakkasserya0fb3722018-07-19 17:09:42 +05301831 /* Update adv data as tx power is known now */
Daniel Winkler25e70882021-04-05 16:33:04 -07001832 hci_req_update_adv_data(hdev, cp->handle);
Daniel Winkler12410572020-12-03 12:12:49 -08001833
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301834 hci_dev_unlock(hdev);
1835}
1836
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02001837static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
1838{
1839 struct hci_rp_read_rssi *rp = (void *) skb->data;
1840 struct hci_conn *conn;
1841
1842 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1843
1844 if (rp->status)
1845 return;
1846
1847 hci_dev_lock(hdev);
1848
1849 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1850 if (conn)
1851 conn->rssi = rp->rssi;
1852
1853 hci_dev_unlock(hdev);
1854}
1855
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001856static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
1857{
1858 struct hci_cp_read_tx_power *sent;
1859 struct hci_rp_read_tx_power *rp = (void *) skb->data;
1860 struct hci_conn *conn;
1861
1862 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1863
1864 if (rp->status)
1865 return;
1866
1867 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
1868 if (!sent)
1869 return;
1870
1871 hci_dev_lock(hdev);
1872
1873 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001874 if (!conn)
1875 goto unlock;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001876
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001877 switch (sent->type) {
1878 case 0x00:
1879 conn->tx_power = rp->tx_power;
1880 break;
1881 case 0x01:
1882 conn->max_tx_power = rp->tx_power;
1883 break;
1884 }
1885
1886unlock:
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001887 hci_dev_unlock(hdev);
1888}
1889
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08001890static void hci_cc_write_ssp_debug_mode(struct hci_dev *hdev, struct sk_buff *skb)
1891{
1892 u8 status = *((u8 *) skb->data);
1893 u8 *mode;
1894
1895 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1896
1897 if (status)
1898 return;
1899
1900 mode = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE);
1901 if (mode)
1902 hdev->ssp_debug_mode = *mode;
1903}
1904
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001905static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001906{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001907 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001908
1909 if (status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001910 hci_conn_check_pending(hdev);
Johan Hedberg314b2382011-04-27 10:29:57 -04001911 return;
1912 }
1913
Andre Guedes89352e72011-11-04 14:16:53 -03001914 set_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001915}
1916
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001917static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001919 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001922 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001923
1924 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 if (!cp)
1926 return;
1927
1928 hci_dev_lock(hdev);
1929
1930 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1931
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001932 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 if (status) {
1935 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001936 if (status != 0x0c || conn->attempt > 2) {
1937 conn->state = BT_CLOSED;
Johan Hedberg539c4962015-02-18 14:53:57 +02001938 hci_connect_cfm(conn, status);
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001939 hci_conn_del(conn);
1940 } else
1941 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 }
1943 } else {
1944 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03001945 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr,
1946 HCI_ROLE_MASTER);
1947 if (!conn)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001948 bt_dev_err(hdev, "no memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 }
1950 }
1951
1952 hci_dev_unlock(hdev);
1953}
1954
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001955static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001957 struct hci_cp_add_sco *cp;
1958 struct hci_conn *acl, *sco;
1959 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001961 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001962
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001963 if (!status)
1964 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001966 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1967 if (!cp)
1968 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001970 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001972 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001973
1974 hci_dev_lock(hdev);
1975
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001976 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001977 if (acl) {
1978 sco = acl->link;
1979 if (sco) {
1980 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001981
Johan Hedberg539c4962015-02-18 14:53:57 +02001982 hci_connect_cfm(sco, status);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001983 hci_conn_del(sco);
1984 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001985 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001986
1987 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988}
1989
Marcel Holtmannf85585552008-07-14 20:13:49 +02001990static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1991{
1992 struct hci_cp_auth_requested *cp;
1993 struct hci_conn *conn;
1994
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001995 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf85585552008-07-14 20:13:49 +02001996
1997 if (!status)
1998 return;
1999
2000 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
2001 if (!cp)
2002 return;
2003
2004 hci_dev_lock(hdev);
2005
2006 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2007 if (conn) {
2008 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002009 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002010 hci_conn_drop(conn);
Marcel Holtmannf85585552008-07-14 20:13:49 +02002011 }
2012 }
2013
2014 hci_dev_unlock(hdev);
2015}
2016
2017static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
2018{
2019 struct hci_cp_set_conn_encrypt *cp;
2020 struct hci_conn *conn;
2021
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002022 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf85585552008-07-14 20:13:49 +02002023
2024 if (!status)
2025 return;
2026
2027 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
2028 if (!cp)
2029 return;
2030
2031 hci_dev_lock(hdev);
2032
2033 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2034 if (conn) {
2035 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002036 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002037 hci_conn_drop(conn);
Marcel Holtmannf85585552008-07-14 20:13:49 +02002038 }
2039 }
2040
2041 hci_dev_unlock(hdev);
2042}
2043
Johan Hedberg127178d2010-11-18 22:22:29 +02002044static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002045 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02002046{
Johan Hedberg392599b2010-11-18 22:22:28 +02002047 if (conn->state != BT_CONFIG || !conn->out)
2048 return 0;
2049
Johan Hedberg765c2a92011-01-19 12:06:52 +05302050 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02002051 return 0;
2052
2053 /* Only request authentication for SSP connections or non-SSP
Johan Hedberg264b8b42014-01-08 16:40:39 +02002054 * devices with sec_level MEDIUM or HIGH or if MITM protection
2055 * is requested.
2056 */
Gustavo Padovan807deac2012-05-17 00:36:24 -03002057 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
Johan Hedberg7e3691e2014-05-30 14:45:19 +03002058 conn->pending_sec_level != BT_SECURITY_FIPS &&
Johan Hedberg264b8b42014-01-08 16:40:39 +02002059 conn->pending_sec_level != BT_SECURITY_HIGH &&
2060 conn->pending_sec_level != BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02002061 return 0;
2062
Johan Hedberg392599b2010-11-18 22:22:28 +02002063 return 1;
2064}
2065
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002066static int hci_resolve_name(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002067 struct inquiry_entry *e)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002068{
2069 struct hci_cp_remote_name_req cp;
2070
2071 memset(&cp, 0, sizeof(cp));
2072
2073 bacpy(&cp.bdaddr, &e->data.bdaddr);
2074 cp.pscan_rep_mode = e->data.pscan_rep_mode;
2075 cp.pscan_mode = e->data.pscan_mode;
2076 cp.clock_offset = e->data.clock_offset;
2077
2078 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2079}
2080
Johan Hedbergb644ba32012-01-17 21:48:47 +02002081static bool hci_resolve_next_name(struct hci_dev *hdev)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002082{
2083 struct discovery_state *discov = &hdev->discovery;
2084 struct inquiry_entry *e;
2085
Johan Hedbergb644ba32012-01-17 21:48:47 +02002086 if (list_empty(&discov->resolve))
2087 return false;
2088
2089 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
Ram Malovanyc8100892012-07-19 10:26:09 +03002090 if (!e)
2091 return false;
2092
Johan Hedbergb644ba32012-01-17 21:48:47 +02002093 if (hci_resolve_name(hdev, e) == 0) {
2094 e->name_state = NAME_PENDING;
2095 return true;
2096 }
2097
2098 return false;
2099}
2100
2101static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002102 bdaddr_t *bdaddr, u8 *name, u8 name_len)
Johan Hedbergb644ba32012-01-17 21:48:47 +02002103{
2104 struct discovery_state *discov = &hdev->discovery;
2105 struct inquiry_entry *e;
2106
Johan Hedberg60cb49d2014-11-11 11:33:24 +02002107 /* Update the mgmt connected state if necessary. Be careful with
2108 * conn objects that exist but are not (yet) connected however.
2109 * Only those in BT_CONFIG or BT_CONNECTED states can be
2110 * considered connected.
2111 */
2112 if (conn &&
2113 (conn->state == BT_CONFIG || conn->state == BT_CONNECTED) &&
Jaganath Kanakkasserycb77c3e2014-11-07 16:39:09 +05302114 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07002115 mgmt_device_connected(hdev, conn, name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002116
2117 if (discov->state == DISCOVERY_STOPPED)
2118 return;
2119
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002120 if (discov->state == DISCOVERY_STOPPING)
2121 goto discov_complete;
2122
2123 if (discov->state != DISCOVERY_RESOLVING)
2124 return;
2125
2126 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
Ram Malovany7cc83802012-07-19 10:26:10 +03002127 /* If the device was not found in a list of found devices names of which
2128 * are pending. there is no need to continue resolving a next name as it
2129 * will be done upon receiving another Remote Name Request Complete
2130 * Event */
2131 if (!e)
2132 return;
2133
2134 list_del(&e->list);
2135 if (name) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002136 e->name_state = NAME_KNOWN;
Ram Malovany7cc83802012-07-19 10:26:10 +03002137 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
2138 e->data.rssi, name, name_len);
Ram Malovanyc3e7c0d2012-07-19 10:26:11 +03002139 } else {
2140 e->name_state = NAME_NOT_KNOWN;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002141 }
2142
Johan Hedbergb644ba32012-01-17 21:48:47 +02002143 if (hci_resolve_next_name(hdev))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002144 return;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002145
2146discov_complete:
2147 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2148}
2149
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002150static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
2151{
Johan Hedberg127178d2010-11-18 22:22:29 +02002152 struct hci_cp_remote_name_req *cp;
2153 struct hci_conn *conn;
2154
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002155 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02002156
2157 /* If successful wait for the name req complete event before
2158 * checking for the need to do authentication */
2159 if (!status)
2160 return;
2161
2162 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
2163 if (!cp)
2164 return;
2165
2166 hci_dev_lock(hdev);
2167
2168 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002169
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002170 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergb644ba32012-01-17 21:48:47 +02002171 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
2172
Johan Hedberg79c6c702011-04-28 11:28:55 -07002173 if (!conn)
2174 goto unlock;
2175
2176 if (!hci_outgoing_auth_needed(hdev, conn))
2177 goto unlock;
2178
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002179 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02002180 struct hci_cp_auth_requested auth_cp;
2181
Johan Hedberg977f8fce2014-07-17 15:35:39 +03002182 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2183
Johannes Bergc1f23a22013-10-07 18:19:16 +02002184 auth_cp.handle = __cpu_to_le16(conn->handle);
2185 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
2186 sizeof(auth_cp), &auth_cp);
Johan Hedberg127178d2010-11-18 22:22:29 +02002187 }
2188
Johan Hedberg79c6c702011-04-28 11:28:55 -07002189unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02002190 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002191}
2192
Marcel Holtmann769be972008-07-14 20:13:49 +02002193static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
2194{
2195 struct hci_cp_read_remote_features *cp;
2196 struct hci_conn *conn;
2197
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002198 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02002199
2200 if (!status)
2201 return;
2202
2203 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
2204 if (!cp)
2205 return;
2206
2207 hci_dev_lock(hdev);
2208
2209 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2210 if (conn) {
2211 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002212 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002213 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002214 }
2215 }
2216
2217 hci_dev_unlock(hdev);
2218}
2219
2220static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
2221{
2222 struct hci_cp_read_remote_ext_features *cp;
2223 struct hci_conn *conn;
2224
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002225 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02002226
2227 if (!status)
2228 return;
2229
2230 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
2231 if (!cp)
2232 return;
2233
2234 hci_dev_lock(hdev);
2235
2236 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2237 if (conn) {
2238 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002239 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002240 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002241 }
2242 }
2243
2244 hci_dev_unlock(hdev);
2245}
2246
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002247static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2248{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002249 struct hci_cp_setup_sync_conn *cp;
2250 struct hci_conn *acl, *sco;
2251 __u16 handle;
2252
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002253 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002254
2255 if (!status)
2256 return;
2257
2258 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
2259 if (!cp)
2260 return;
2261
2262 handle = __le16_to_cpu(cp->handle);
2263
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002264 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002265
2266 hci_dev_lock(hdev);
2267
2268 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002269 if (acl) {
2270 sco = acl->link;
2271 if (sco) {
2272 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002273
Johan Hedberg539c4962015-02-18 14:53:57 +02002274 hci_connect_cfm(sco, status);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002275 hci_conn_del(sco);
2276 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002277 }
2278
2279 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002280}
2281
Kiran Kb2af2642021-09-07 15:42:43 +05302282static void hci_cs_enhanced_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2283{
2284 struct hci_cp_enhanced_setup_sync_conn *cp;
2285 struct hci_conn *acl, *sco;
2286 __u16 handle;
2287
2288 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2289
2290 if (!status)
2291 return;
2292
2293 cp = hci_sent_cmd_data(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN);
2294 if (!cp)
2295 return;
2296
2297 handle = __le16_to_cpu(cp->handle);
2298
2299 bt_dev_dbg(hdev, "handle 0x%4.4x", handle);
2300
2301 hci_dev_lock(hdev);
2302
2303 acl = hci_conn_hash_lookup_handle(hdev, handle);
2304 if (acl) {
2305 sco = acl->link;
2306 if (sco) {
2307 sco->state = BT_CLOSED;
2308
2309 hci_connect_cfm(sco, status);
2310 hci_conn_del(sco);
2311 }
2312 }
2313
2314 hci_dev_unlock(hdev);
2315}
2316
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002317static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
2318{
2319 struct hci_cp_sniff_mode *cp;
2320 struct hci_conn *conn;
2321
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002322 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002323
2324 if (!status)
2325 return;
2326
2327 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
2328 if (!cp)
2329 return;
2330
2331 hci_dev_lock(hdev);
2332
2333 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002334 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002335 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002336
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002337 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002338 hci_sco_setup(conn, status);
2339 }
2340
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002341 hci_dev_unlock(hdev);
2342}
2343
2344static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
2345{
2346 struct hci_cp_exit_sniff_mode *cp;
2347 struct hci_conn *conn;
2348
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002349 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002350
2351 if (!status)
2352 return;
2353
2354 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
2355 if (!cp)
2356 return;
2357
2358 hci_dev_lock(hdev);
2359
2360 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002361 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002362 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002363
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002364 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002365 hci_sco_setup(conn, status);
2366 }
2367
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002368 hci_dev_unlock(hdev);
2369}
2370
Johan Hedberg88c3df12012-02-09 14:27:38 +02002371static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
2372{
2373 struct hci_cp_disconnect *cp;
2374 struct hci_conn *conn;
2375
2376 if (!status)
2377 return;
2378
2379 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
2380 if (!cp)
2381 return;
2382
2383 hci_dev_lock(hdev);
2384
2385 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Joseph Hwangb8d290522020-03-11 19:20:14 -07002386 if (conn) {
Johan Hedberg88c3df12012-02-09 14:27:38 +02002387 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002388 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002389
Luiz Augusto von Dentz1eeaa1a2021-08-20 16:40:38 -07002390 if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07002391 hdev->cur_adv_instance = conn->adv_instance;
2392 hci_req_reenable_advertising(hdev);
2393 }
2394
Joseph Hwangb8d290522020-03-11 19:20:14 -07002395 /* If the disconnection failed for any reason, the upper layer
2396 * does not retry to disconnect in current implementation.
2397 * Hence, we need to do some basic cleanup here and re-enable
2398 * advertising if necessary.
2399 */
2400 hci_conn_del(conn);
Joseph Hwangb8d290522020-03-11 19:20:14 -07002401 }
2402
Johan Hedberg88c3df12012-02-09 14:27:38 +02002403 hci_dev_unlock(hdev);
2404}
2405
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002406static u8 ev_bdaddr_type(struct hci_dev *hdev, u8 type, bool *resolved)
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002407{
2408 /* When using controller based address resolution, then the new
2409 * address types 0x02 and 0x03 are used. These types need to be
2410 * converted back into either public address or random address type
2411 */
2412 switch (type) {
2413 case ADDR_LE_DEV_PUBLIC_RESOLVED:
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002414 if (resolved)
2415 *resolved = true;
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002416 return ADDR_LE_DEV_PUBLIC;
2417 case ADDR_LE_DEV_RANDOM_RESOLVED:
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002418 if (resolved)
2419 *resolved = true;
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002420 return ADDR_LE_DEV_RANDOM;
2421 }
2422
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002423 if (resolved)
2424 *resolved = false;
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002425 return type;
2426}
2427
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302428static void cs_le_create_conn(struct hci_dev *hdev, bdaddr_t *peer_addr,
2429 u8 peer_addr_type, u8 own_address_type,
2430 u8 filter_policy)
2431{
2432 struct hci_conn *conn;
2433
2434 conn = hci_conn_hash_lookup_le(hdev, peer_addr,
2435 peer_addr_type);
2436 if (!conn)
2437 return;
2438
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002439 own_address_type = ev_bdaddr_type(hdev, own_address_type, NULL);
Sathish Narasimmanb31bc002020-07-23 18:08:59 +05302440
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302441 /* Store the initiator and responder address information which
2442 * is needed for SMP. These values will not change during the
2443 * lifetime of the connection.
2444 */
2445 conn->init_addr_type = own_address_type;
2446 if (own_address_type == ADDR_LE_DEV_RANDOM)
2447 bacpy(&conn->init_addr, &hdev->random_addr);
2448 else
2449 bacpy(&conn->init_addr, &hdev->bdaddr);
2450
2451 conn->resp_addr_type = peer_addr_type;
2452 bacpy(&conn->resp_addr, peer_addr);
2453
2454 /* We don't want the connection attempt to stick around
2455 * indefinitely since LE doesn't have a page timeout concept
2456 * like BR/EDR. Set a timer for any connection that doesn't use
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002457 * the accept list for connecting.
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302458 */
2459 if (filter_policy == HCI_LE_USE_PEER_ADDR)
2460 queue_delayed_work(conn->hdev->workqueue,
2461 &conn->le_conn_timeout,
2462 conn->conn_timeout);
2463}
2464
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002465static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
2466{
2467 struct hci_cp_le_create_conn *cp;
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002468
2469 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2470
2471 /* All connection failure handling is taken care of by the
2472 * hci_le_conn_failed function which is triggered by the HCI
2473 * request completion callbacks used for connecting.
2474 */
2475 if (status)
2476 return;
2477
2478 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
2479 if (!cp)
2480 return;
2481
2482 hci_dev_lock(hdev);
2483
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302484 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2485 cp->own_address_type, cp->filter_policy);
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002486
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002487 hci_dev_unlock(hdev);
2488}
2489
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02002490static void hci_cs_le_ext_create_conn(struct hci_dev *hdev, u8 status)
2491{
2492 struct hci_cp_le_ext_create_conn *cp;
2493
2494 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2495
2496 /* All connection failure handling is taken care of by the
2497 * hci_le_conn_failed function which is triggered by the HCI
2498 * request completion callbacks used for connecting.
2499 */
2500 if (status)
2501 return;
2502
2503 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_EXT_CREATE_CONN);
2504 if (!cp)
2505 return;
2506
2507 hci_dev_lock(hdev);
2508
2509 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2510 cp->own_addr_type, cp->filter_policy);
2511
2512 hci_dev_unlock(hdev);
2513}
2514
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07002515static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
2516{
2517 struct hci_cp_le_read_remote_features *cp;
2518 struct hci_conn *conn;
2519
2520 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2521
2522 if (!status)
2523 return;
2524
2525 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
2526 if (!cp)
2527 return;
2528
2529 hci_dev_lock(hdev);
2530
2531 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2532 if (conn) {
2533 if (conn->state == BT_CONFIG) {
2534 hci_connect_cfm(conn, status);
2535 hci_conn_drop(conn);
2536 }
2537 }
2538
2539 hci_dev_unlock(hdev);
2540}
2541
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02002542static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
2543{
2544 struct hci_cp_le_start_enc *cp;
2545 struct hci_conn *conn;
2546
2547 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2548
2549 if (!status)
2550 return;
2551
2552 hci_dev_lock(hdev);
2553
2554 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
2555 if (!cp)
2556 goto unlock;
2557
2558 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2559 if (!conn)
2560 goto unlock;
2561
2562 if (conn->state != BT_CONNECTED)
2563 goto unlock;
2564
2565 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2566 hci_conn_drop(conn);
2567
2568unlock:
2569 hci_dev_unlock(hdev);
2570}
2571
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01002572static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
2573{
2574 struct hci_cp_switch_role *cp;
2575 struct hci_conn *conn;
2576
2577 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2578
2579 if (!status)
2580 return;
2581
2582 cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
2583 if (!cp)
2584 return;
2585
2586 hci_dev_lock(hdev);
2587
2588 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2589 if (conn)
2590 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
2591
2592 hci_dev_unlock(hdev);
2593}
2594
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002595static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002596{
2597 __u8 status = *((__u8 *) skb->data);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002598 struct discovery_state *discov = &hdev->discovery;
2599 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002600
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002601 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002602
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002603 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03002604
2605 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
2606 return;
2607
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002608 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -03002609 wake_up_bit(&hdev->flags, HCI_INQUIRY);
2610
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002611 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002612 return;
2613
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002614 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002615
Andre Guedes343f9352012-02-17 20:39:37 -03002616 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002617 goto unlock;
2618
2619 if (list_empty(&discov->resolve)) {
Jakub Pawlowski07d23342015-03-17 09:04:14 -07002620 /* When BR/EDR inquiry is active and no LE scanning is in
2621 * progress, then change discovery state to indicate completion.
2622 *
2623 * When running LE scanning and BR/EDR inquiry simultaneously
2624 * and the LE scan already finished, then change the discovery
2625 * state to indicate completion.
2626 */
2627 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2628 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2629 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002630 goto unlock;
2631 }
2632
2633 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
2634 if (e && hci_resolve_name(hdev, e) == 0) {
2635 e->name_state = NAME_PENDING;
2636 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
2637 } else {
Jakub Pawlowski07d23342015-03-17 09:04:14 -07002638 /* When BR/EDR inquiry is active and no LE scanning is in
2639 * progress, then change discovery state to indicate completion.
2640 *
2641 * When running LE scanning and BR/EDR inquiry simultaneously
2642 * and the LE scan already finished, then change the discovery
2643 * state to indicate completion.
2644 */
2645 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2646 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2647 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002648 }
2649
2650unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002651 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002652}
2653
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002654static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002656 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002657 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 int num_rsp = *((__u8 *) skb->data);
2659
2660 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2661
Peilin Ye75bbd2e2020-07-10 17:39:18 -04002662 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002663 return;
2664
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002665 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03002666 return;
2667
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002669
Johan Hedberge17acd42011-03-30 23:57:16 +03002670 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02002671 u32 flags;
Johan Hedberg31754052012-01-04 13:39:52 +02002672
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 bacpy(&data.bdaddr, &info->bdaddr);
2674 data.pscan_rep_mode = info->pscan_rep_mode;
2675 data.pscan_period_mode = info->pscan_period_mode;
2676 data.pscan_mode = info->pscan_mode;
2677 memcpy(data.dev_class, info->dev_class, 3);
2678 data.clock_offset = info->clock_offset;
Marcel Holtmannefb25132014-12-05 13:03:34 +01002679 data.rssi = HCI_RSSI_INVALID;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002680 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02002681
Marcel Holtmannaf589252014-07-01 14:11:20 +02002682 flags = hci_inquiry_cache_update(hdev, &data, false);
2683
Johan Hedberg48264f02011-11-09 13:58:58 +02002684 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannefb25132014-12-05 13:03:34 +01002685 info->dev_class, HCI_RSSI_INVALID,
2686 flags, NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002688
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 hci_dev_unlock(hdev);
2690}
2691
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002692static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002694 struct hci_ev_conn_complete *ev = (void *) skb->data;
2695 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002697 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002698
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002700
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002701 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02002702 if (!conn) {
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002703 /* Connection may not exist if auto-connected. Check the bredr
2704 * allowlist to see if this device is allowed to auto connect.
2705 * If link is an ACL type, create a connection class
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002706 * automatically.
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002707 *
2708 * Auto-connect will only occur if the event filter is
2709 * programmed with a given address. Right now, event filter is
2710 * only used during suspend.
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002711 */
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002712 if (ev->link_type == ACL_LINK &&
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002713 hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002714 &ev->bdaddr,
2715 BDADDR_BREDR)) {
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002716 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2717 HCI_ROLE_SLAVE);
2718 if (!conn) {
2719 bt_dev_err(hdev, "no memory for new conn");
2720 goto unlock;
2721 }
Abhishek Pandit-Subedi2d186fc2020-03-19 17:07:13 -07002722 } else {
2723 if (ev->link_type != SCO_LINK)
2724 goto unlock;
2725
2726 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK,
2727 &ev->bdaddr);
2728 if (!conn)
2729 goto unlock;
2730
2731 conn->type = SCO_LINK;
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002732 }
Marcel Holtmann94992372009-04-19 19:30:03 +02002733 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002734
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002735 if (!ev->status) {
2736 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02002737
2738 if (conn->type == ACL_LINK) {
2739 conn->state = BT_CONFIG;
2740 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02002741
2742 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
2743 !hci_find_link_key(hdev, &ev->bdaddr))
2744 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2745 else
2746 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02002747 } else
2748 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002749
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01002750 hci_debugfs_create_conn(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002751 hci_conn_add_sysfs(conn);
2752
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002753 if (test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002754 set_bit(HCI_CONN_AUTH, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002755
2756 if (test_bit(HCI_ENCRYPT, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002757 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002758
2759 /* Get remote features */
2760 if (conn->type == ACL_LINK) {
2761 struct hci_cp_read_remote_features cp;
2762 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02002763 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002764 sizeof(cp), &cp);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002765
Johan Hedberg01b1cb82015-11-16 12:52:21 +02002766 hci_req_update_scan(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002767 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002768
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002769 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02002770 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002771 struct hci_cp_change_conn_ptype cp;
2772 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02002773 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002774 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
2775 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002776 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02002777 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002778 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02002779 if (conn->type == ACL_LINK)
Marcel Holtmann64c7b772014-02-18 14:22:20 -08002780 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002781 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02002782 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002783
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002784 if (conn->type == ACL_LINK)
2785 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002786
Marcel Holtmann769be972008-07-14 20:13:49 +02002787 if (ev->status) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002788 hci_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002789 hci_conn_del(conn);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02002790 } else if (ev->link_type == SCO_LINK) {
2791 switch (conn->setting & SCO_AIRMODE_MASK) {
2792 case SCO_AIRMODE_CVSD:
2793 if (hdev->notify)
2794 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
2795 break;
2796 }
2797
Johan Hedberg539c4962015-02-18 14:53:57 +02002798 hci_connect_cfm(conn, ev->status);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02002799 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002800
2801unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002803
2804 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805}
2806
Johan Hedberg70c46422014-07-09 12:59:17 +03002807static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
2808{
2809 struct hci_cp_reject_conn_req cp;
2810
2811 bacpy(&cp.bdaddr, bdaddr);
2812 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
2813 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
2814}
2815
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002816static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002818 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 int mask = hdev->link_mode;
Johan Hedberg70c46422014-07-09 12:59:17 +03002820 struct inquiry_entry *ie;
2821 struct hci_conn *conn;
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01002822 __u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002824 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002825 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01002827 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
2828 &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
Johan Hedberg70c46422014-07-09 12:59:17 +03002830 if (!(mask & HCI_LM_ACCEPT)) {
2831 hci_reject_conn(hdev, &ev->bdaddr);
2832 return;
2833 }
2834
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002835 if (hci_bdaddr_list_lookup(&hdev->reject_list, &ev->bdaddr,
Johan Hedberg46c4c942014-07-16 16:19:21 +03002836 BDADDR_BREDR)) {
2837 hci_reject_conn(hdev, &ev->bdaddr);
2838 return;
2839 }
2840
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002841 /* Require HCI_CONNECTABLE or an accept list entry to accept the
Johan Hedberg6a8fc952014-12-24 20:43:11 +02002842 * connection. These features are only touched through mgmt so
2843 * only do the checks if HCI_MGMT is set.
2844 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002845 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
2846 !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002847 !hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, &ev->bdaddr,
Abhishek Pandit-Subedi8baaa402020-06-17 16:39:08 +02002848 BDADDR_BREDR)) {
2849 hci_reject_conn(hdev, &ev->bdaddr);
2850 return;
Johan Hedberg70c46422014-07-09 12:59:17 +03002851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Johan Hedberg70c46422014-07-09 12:59:17 +03002853 /* Connection accepted */
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002854
Johan Hedberg70c46422014-07-09 12:59:17 +03002855 hci_dev_lock(hdev);
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02002856
Johan Hedberg70c46422014-07-09 12:59:17 +03002857 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2858 if (ie)
2859 memcpy(ie->data.dev_class, ev->dev_class, 3);
2860
2861 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
2862 &ev->bdaddr);
2863 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03002864 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2865 HCI_ROLE_SLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002867 bt_dev_err(hdev, "no memory for new connection");
Johan Hedberg70c46422014-07-09 12:59:17 +03002868 hci_dev_unlock(hdev);
2869 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 }
Johan Hedberg70c46422014-07-09 12:59:17 +03002871 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002872
Johan Hedberg70c46422014-07-09 12:59:17 +03002873 memcpy(conn->dev_class, ev->dev_class, 3);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002874
Johan Hedberg70c46422014-07-09 12:59:17 +03002875 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Johan Hedberg70c46422014-07-09 12:59:17 +03002877 if (ev->link_type == ACL_LINK ||
2878 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
2879 struct hci_cp_accept_conn_req cp;
2880 conn->state = BT_CONNECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 bacpy(&cp.bdaddr, &ev->bdaddr);
Johan Hedberg70c46422014-07-09 12:59:17 +03002883
2884 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
Archie Pusaka74be5232021-06-04 16:26:25 +08002885 cp.role = 0x00; /* Become central */
Johan Hedberg70c46422014-07-09 12:59:17 +03002886 else
Archie Pusaka74be5232021-06-04 16:26:25 +08002887 cp.role = 0x01; /* Remain peripheral */
Johan Hedberg70c46422014-07-09 12:59:17 +03002888
2889 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
2890 } else if (!(flags & HCI_PROTO_DEFER)) {
2891 struct hci_cp_accept_sync_conn_req cp;
2892 conn->state = BT_CONNECT;
2893
2894 bacpy(&cp.bdaddr, &ev->bdaddr);
2895 cp.pkt_type = cpu_to_le16(conn->pkt_type);
2896
2897 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
2898 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
2899 cp.max_latency = cpu_to_le16(0xffff);
2900 cp.content_format = cpu_to_le16(hdev->voice_setting);
2901 cp.retrans_effort = 0xff;
2902
2903 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
2904 &cp);
2905 } else {
2906 conn->state = BT_CONNECT2;
Johan Hedberg539c4962015-02-18 14:53:57 +02002907 hci_connect_cfm(conn, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 }
2909}
2910
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02002911static u8 hci_to_mgmt_reason(u8 err)
2912{
2913 switch (err) {
2914 case HCI_ERROR_CONNECTION_TIMEOUT:
2915 return MGMT_DEV_DISCONN_TIMEOUT;
2916 case HCI_ERROR_REMOTE_USER_TERM:
2917 case HCI_ERROR_REMOTE_LOW_RESOURCES:
2918 case HCI_ERROR_REMOTE_POWER_OFF:
2919 return MGMT_DEV_DISCONN_REMOTE;
2920 case HCI_ERROR_LOCAL_HOST_TERM:
2921 return MGMT_DEV_DISCONN_LOCAL_HOST;
2922 default:
2923 return MGMT_DEV_DISCONN_UNKNOWN;
2924 }
2925}
2926
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002927static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002929 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Szymon Janc160b9252016-07-12 02:12:16 +02002930 u8 reason;
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002931 struct hci_conn_params *params;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002932 struct hci_conn *conn;
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002933 bool mgmt_connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002935 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 hci_dev_lock(hdev);
2938
Marcel Holtmann04837f62006-07-03 10:02:33 +02002939 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02002940 if (!conn)
2941 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002942
Andre Guedesabf54a52013-11-07 17:36:09 -03002943 if (ev->status) {
2944 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2945 conn->dst_type, ev->status);
2946 goto unlock;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02002947 }
Johan Hedbergf7520542011-01-20 12:34:39 +02002948
Andre Guedes38462202013-11-07 17:36:10 -03002949 conn->state = BT_CLOSED;
2950
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002951 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
Szymon Janc160b9252016-07-12 02:12:16 +02002952
2953 if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
2954 reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
2955 else
2956 reason = hci_to_mgmt_reason(ev->reason);
2957
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002958 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2959 reason, mgmt_connected);
Andre Guedesabf54a52013-11-07 17:36:09 -03002960
Johan Hedberg22f433d2014-08-01 11:13:32 +03002961 if (conn->type == ACL_LINK) {
2962 if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2963 hci_remove_link_key(hdev, &conn->dst);
2964
Johan Hedberg01b1cb82015-11-16 12:52:21 +02002965 hci_req_update_scan(hdev);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002966 }
Johan Hedberg22102462013-10-05 12:01:06 +02002967
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002968 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2969 if (params) {
2970 switch (params->auto_connect) {
2971 case HCI_AUTO_CONN_LINK_LOSS:
2972 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2973 break;
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05002974 fallthrough;
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002975
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02002976 case HCI_AUTO_CONN_DIRECT:
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002977 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg418025d2014-07-04 12:37:24 +03002978 list_del_init(&params->action);
2979 list_add(&params->action, &hdev->pend_le_conns);
2980 hci_update_background_scan(hdev);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002981 break;
2982
2983 default:
2984 break;
2985 }
2986 }
2987
Johan Hedberg3a6d5762015-02-18 14:53:58 +02002988 hci_disconn_cfm(conn, ev->reason);
Andre Guedes38462202013-11-07 17:36:10 -03002989
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002990 /* The suspend notifier is waiting for all devices to disconnect so
2991 * clear the bit from pending tasks and inform the wait queue.
2992 */
2993 if (list_empty(&hdev->conn_hash.list) &&
2994 test_and_clear_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks)) {
2995 wake_up(&hdev->suspend_wait_q);
2996 }
2997
Andre Guedes38462202013-11-07 17:36:10 -03002998 /* Re-enable advertising if necessary, since it might
2999 * have been disabled by the connection. From the
3000 * HCI_LE_Set_Advertise_Enable command description in
3001 * the core specification (v4.0):
3002 * "The Controller shall continue advertising until the Host
3003 * issues an LE_Set_Advertise_Enable command with
3004 * Advertising_Enable set to 0x00 (Advertising is disabled)
3005 * or until a connection is created or until the Advertising
3006 * is timed out due to Directed Advertising."
3007 */
Luiz Augusto von Dentz1eeaa1a2021-08-20 16:40:38 -07003008 if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07003009 hdev->cur_adv_instance = conn->adv_instance;
Johan Hedbergf2252572015-11-18 12:49:20 +02003010 hci_req_reenable_advertising(hdev);
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07003011 }
3012
3013 hci_conn_del(conn);
Johan Hedbergf7520542011-01-20 12:34:39 +02003014
3015unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 hci_dev_unlock(hdev);
3017}
3018
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003019static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003020{
3021 struct hci_ev_auth_complete *ev = (void *) skb->data;
3022 struct hci_conn *conn;
3023
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003024 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003025
3026 hci_dev_lock(hdev);
3027
3028 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003029 if (!conn)
3030 goto unlock;
3031
3032 if (!ev->status) {
Szymon Janc160b9252016-07-12 02:12:16 +02003033 clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3034
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02003035 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03003036 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003037 bt_dev_info(hdev, "re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03003038 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03003039 set_bit(HCI_CONN_AUTH, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003040 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03003041 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003042 } else {
Szymon Janc160b9252016-07-12 02:12:16 +02003043 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3044 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3045
Johan Hedberge1e930f2014-09-08 17:09:49 -07003046 mgmt_auth_failed(conn, ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003047 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003048
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003049 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
3050 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003051
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003052 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02003053 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003054 struct hci_cp_set_conn_encrypt cp;
3055 cp.handle = ev->handle;
3056 cp.encrypt = 0x01;
3057 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03003058 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003059 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003060 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02003061 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003062 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003063 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003064 } else {
3065 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003066
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003067 hci_conn_hold(conn);
3068 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003069 hci_conn_drop(conn);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003070 }
3071
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003072 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003073 if (!ev->status) {
3074 struct hci_cp_set_conn_encrypt cp;
3075 cp.handle = ev->handle;
3076 cp.encrypt = 0x01;
3077 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03003078 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003079 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003080 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003081 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003082 }
3083 }
3084
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003085unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003086 hci_dev_unlock(hdev);
3087}
3088
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003089static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003090{
Johan Hedberg127178d2010-11-18 22:22:29 +02003091 struct hci_ev_remote_name *ev = (void *) skb->data;
3092 struct hci_conn *conn;
3093
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003094 BT_DBG("%s", hdev->name);
3095
3096 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02003097
3098 hci_dev_lock(hdev);
3099
3100 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003101
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003102 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergb644ba32012-01-17 21:48:47 +02003103 goto check_auth;
3104
3105 if (ev->status == 0)
3106 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003107 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02003108 else
3109 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
3110
3111check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07003112 if (!conn)
3113 goto unlock;
3114
3115 if (!hci_outgoing_auth_needed(hdev, conn))
3116 goto unlock;
3117
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003118 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003119 struct hci_cp_auth_requested cp;
Johan Hedberg977f8fce2014-07-17 15:35:39 +03003120
3121 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
3122
Johan Hedberg127178d2010-11-18 22:22:29 +02003123 cp.handle = __cpu_to_le16(conn->handle);
3124 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
3125 }
3126
Johan Hedberg79c6c702011-04-28 11:28:55 -07003127unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02003128 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003129}
3130
Johan Hedberg821f3762015-06-11 13:52:29 +03003131static void read_enc_key_size_complete(struct hci_dev *hdev, u8 status,
3132 u16 opcode, struct sk_buff *skb)
3133{
3134 const struct hci_rp_read_enc_key_size *rp;
3135 struct hci_conn *conn;
3136 u16 handle;
3137
3138 BT_DBG("%s status 0x%02x", hdev->name, status);
3139
3140 if (!skb || skb->len < sizeof(*rp)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003141 bt_dev_err(hdev, "invalid read key size response");
Johan Hedberg821f3762015-06-11 13:52:29 +03003142 return;
3143 }
3144
3145 rp = (void *)skb->data;
3146 handle = le16_to_cpu(rp->handle);
3147
3148 hci_dev_lock(hdev);
3149
3150 conn = hci_conn_hash_lookup_handle(hdev, handle);
3151 if (!conn)
3152 goto unlock;
3153
Alain Michaud32b50722020-03-25 14:48:34 +00003154 /* While unexpected, the read_enc_key_size command may fail. The most
3155 * secure approach is to then assume the key size is 0 to force a
3156 * disconnection.
Johan Hedberg821f3762015-06-11 13:52:29 +03003157 */
3158 if (rp->status) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003159 bt_dev_err(hdev, "failed to read key size for handle %u",
3160 handle);
Alain Michaud32b50722020-03-25 14:48:34 +00003161 conn->enc_key_size = 0;
Johan Hedberg821f3762015-06-11 13:52:29 +03003162 } else {
3163 conn->enc_key_size = rp->key_size;
3164 }
3165
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003166 hci_encrypt_cfm(conn, 0);
Johan Hedberg821f3762015-06-11 13:52:29 +03003167
3168unlock:
3169 hci_dev_unlock(hdev);
3170}
3171
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003172static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003173{
3174 struct hci_ev_encrypt_change *ev = (void *) skb->data;
3175 struct hci_conn *conn;
3176
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003177 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003178
3179 hci_dev_lock(hdev);
3180
3181 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003182 if (!conn)
3183 goto unlock;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003184
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003185 if (!ev->status) {
3186 if (ev->encrypt) {
3187 /* Encryption implies authentication */
Johan Hedberg4dae2792014-06-24 17:03:50 +03003188 set_bit(HCI_CONN_AUTH, &conn->flags);
3189 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003190 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003191
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08003192 /* P-256 authentication key implies FIPS */
3193 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
Johan Hedberg4dae2792014-06-24 17:03:50 +03003194 set_bit(HCI_CONN_FIPS, &conn->flags);
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08003195
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003196 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
3197 conn->type == LE_LINK)
3198 set_bit(HCI_CONN_AES_CCM, &conn->flags);
3199 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03003200 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003201 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
3202 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003203 }
3204
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07003205 /* We should disregard the current RPA and generate a new one
3206 * whenever the encryption procedure fails.
3207 */
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303208 if (ev->status && conn->type == LE_LINK) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07003209 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303210 hci_adv_instances_set_rpa_expired(hdev, true);
3211 }
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07003212
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003213 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3214
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07003215 /* Check link security requirements are met */
3216 if (!hci_conn_check_link_mode(conn))
3217 ev->status = HCI_ERROR_AUTH_FAILURE;
3218
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003219 if (ev->status && conn->state == BT_CONNECTED) {
Szymon Janc160b9252016-07-12 02:12:16 +02003220 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3221 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3222
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07003223 /* Notify upper layers so they can cleanup before
3224 * disconnecting.
3225 */
3226 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003227 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
3228 hci_conn_drop(conn);
3229 goto unlock;
3230 }
3231
Johan Hedberg821f3762015-06-11 13:52:29 +03003232 /* Try reading the encryption key size for encrypted ACL links */
3233 if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
3234 struct hci_cp_read_enc_key_size cp;
3235 struct hci_request req;
3236
3237 /* Only send HCI_Read_Encryption_Key_Size if the
3238 * controller really supports it. If it doesn't, assume
3239 * the default size (16).
3240 */
3241 if (!(hdev->commands[20] & 0x10)) {
3242 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3243 goto notify;
3244 }
3245
3246 hci_req_init(&req, hdev);
3247
3248 cp.handle = cpu_to_le16(conn->handle);
3249 hci_req_add(&req, HCI_OP_READ_ENC_KEY_SIZE, sizeof(cp), &cp);
3250
3251 if (hci_req_run_skb(&req, read_enc_key_size_complete)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003252 bt_dev_err(hdev, "sending read key size failed");
Johan Hedberg821f3762015-06-11 13:52:29 +03003253 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3254 goto notify;
3255 }
3256
3257 goto unlock;
3258 }
3259
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +05303260 /* Set the default Authenticated Payload Timeout after
3261 * an LE Link is established. As per Core Spec v5.0, Vol 2, Part B
3262 * Section 3.3, the HCI command WRITE_AUTH_PAYLOAD_TIMEOUT should be
3263 * sent when the link is active and Encryption is enabled, the conn
3264 * type can be either LE or ACL and controller must support LMP Ping.
3265 * Ensure for AES-CCM encryption as well.
3266 */
3267 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
3268 test_bit(HCI_CONN_AES_CCM, &conn->flags) &&
3269 ((conn->type == ACL_LINK && lmp_ping_capable(hdev)) ||
3270 (conn->type == LE_LINK && (hdev->le_features[0] & HCI_LE_PING)))) {
3271 struct hci_cp_write_auth_payload_to cp;
3272
3273 cp.handle = cpu_to_le16(conn->handle);
3274 cp.timeout = cpu_to_le16(hdev->auth_payload_timeout);
3275 hci_send_cmd(conn->hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO,
3276 sizeof(cp), &cp);
3277 }
3278
Johan Hedberg821f3762015-06-11 13:52:29 +03003279notify:
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003280 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003281
Gustavo Padovana7d77232012-05-13 03:20:07 -03003282unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003283 hci_dev_unlock(hdev);
3284}
3285
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003286static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
3287 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003288{
3289 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
3290 struct hci_conn *conn;
3291
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003292 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003293
3294 hci_dev_lock(hdev);
3295
3296 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3297 if (conn) {
3298 if (!ev->status)
Johan Hedberg4dae2792014-06-24 17:03:50 +03003299 set_bit(HCI_CONN_SECURE, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003300
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003301 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003302
3303 hci_key_change_cfm(conn, ev->status);
3304 }
3305
3306 hci_dev_unlock(hdev);
3307}
3308
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003309static void hci_remote_features_evt(struct hci_dev *hdev,
3310 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003311{
3312 struct hci_ev_remote_features *ev = (void *) skb->data;
3313 struct hci_conn *conn;
3314
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003315 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003316
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003317 hci_dev_lock(hdev);
3318
3319 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02003320 if (!conn)
3321 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02003322
Johan Hedbergccd556f2010-11-10 17:11:51 +02003323 if (!ev->status)
Johan Hedbergcad718e2013-04-17 15:00:51 +03003324 memcpy(conn->features[0], ev->features, 8);
Johan Hedbergccd556f2010-11-10 17:11:51 +02003325
3326 if (conn->state != BT_CONFIG)
3327 goto unlock;
3328
Szymon Jancac363cf2015-01-29 16:36:59 +01003329 if (!ev->status && lmp_ext_feat_capable(hdev) &&
3330 lmp_ext_feat_capable(conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003331 struct hci_cp_read_remote_ext_features cp;
3332 cp.handle = ev->handle;
3333 cp.page = 0x01;
3334 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003335 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02003336 goto unlock;
3337 }
3338
Johan Hedberg671267b2012-05-12 16:11:50 -03003339 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003340 struct hci_cp_remote_name_req cp;
3341 memset(&cp, 0, sizeof(cp));
3342 bacpy(&cp.bdaddr, &conn->dst);
3343 cp.pscan_rep_mode = 0x02;
3344 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003345 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07003346 mgmt_device_connected(hdev, conn, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02003347
Johan Hedberg127178d2010-11-18 22:22:29 +02003348 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003349 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02003350 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003351 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02003352 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003353
Johan Hedbergccd556f2010-11-10 17:11:51 +02003354unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003355 hci_dev_unlock(hdev);
3356}
3357
Kiran Kecb71f252021-08-16 05:07:47 +05303358static inline void handle_cmd_cnt_and_timer(struct hci_dev *hdev, u8 ncmd)
Manish Mandlikde75cd02021-04-29 10:24:22 -07003359{
Kiran Kecb71f252021-08-16 05:07:47 +05303360 cancel_delayed_work(&hdev->cmd_timer);
Manish Mandlikde75cd02021-04-29 10:24:22 -07003361
3362 if (!test_bit(HCI_RESET, &hdev->flags)) {
3363 if (ncmd) {
3364 cancel_delayed_work(&hdev->ncmd_timer);
3365 atomic_set(&hdev->cmd_cnt, 1);
3366 } else {
3367 schedule_delayed_work(&hdev->ncmd_timer,
3368 HCI_NCMD_TIMEOUT);
3369 }
3370 }
3371}
3372
Johan Hedberge62144872015-04-02 13:41:08 +03003373static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
3374 u16 *opcode, u8 *status,
3375 hci_req_complete_t *req_complete,
3376 hci_req_complete_skb_t *req_complete_skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003377{
3378 struct hci_ev_cmd_complete *ev = (void *) skb->data;
Johan Hedberge62144872015-04-02 13:41:08 +03003379
3380 *opcode = __le16_to_cpu(ev->opcode);
3381 *status = skb->data[sizeof(*ev)];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003382
3383 skb_pull(skb, sizeof(*ev));
3384
Johan Hedberge62144872015-04-02 13:41:08 +03003385 switch (*opcode) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003386 case HCI_OP_INQUIRY_CANCEL:
Sonny Sasakaadf1d692020-05-06 12:55:03 -07003387 hci_cc_inquiry_cancel(hdev, skb, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003388 break;
3389
Andre Guedes4d934832012-03-21 00:03:35 -03003390 case HCI_OP_PERIODIC_INQ:
3391 hci_cc_periodic_inq(hdev, skb);
3392 break;
3393
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003394 case HCI_OP_EXIT_PERIODIC_INQ:
3395 hci_cc_exit_periodic_inq(hdev, skb);
3396 break;
3397
3398 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
3399 hci_cc_remote_name_req_cancel(hdev, skb);
3400 break;
3401
3402 case HCI_OP_ROLE_DISCOVERY:
3403 hci_cc_role_discovery(hdev, skb);
3404 break;
3405
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02003406 case HCI_OP_READ_LINK_POLICY:
3407 hci_cc_read_link_policy(hdev, skb);
3408 break;
3409
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003410 case HCI_OP_WRITE_LINK_POLICY:
3411 hci_cc_write_link_policy(hdev, skb);
3412 break;
3413
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02003414 case HCI_OP_READ_DEF_LINK_POLICY:
3415 hci_cc_read_def_link_policy(hdev, skb);
3416 break;
3417
3418 case HCI_OP_WRITE_DEF_LINK_POLICY:
3419 hci_cc_write_def_link_policy(hdev, skb);
3420 break;
3421
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003422 case HCI_OP_RESET:
3423 hci_cc_reset(hdev, skb);
3424 break;
3425
Marcel Holtmannc2f0f972015-01-12 09:21:25 -08003426 case HCI_OP_READ_STORED_LINK_KEY:
3427 hci_cc_read_stored_link_key(hdev, skb);
3428 break;
3429
Marcel Holtmanna93661202015-01-12 09:21:28 -08003430 case HCI_OP_DELETE_STORED_LINK_KEY:
3431 hci_cc_delete_stored_link_key(hdev, skb);
3432 break;
3433
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003434 case HCI_OP_WRITE_LOCAL_NAME:
3435 hci_cc_write_local_name(hdev, skb);
3436 break;
3437
3438 case HCI_OP_READ_LOCAL_NAME:
3439 hci_cc_read_local_name(hdev, skb);
3440 break;
3441
3442 case HCI_OP_WRITE_AUTH_ENABLE:
3443 hci_cc_write_auth_enable(hdev, skb);
3444 break;
3445
3446 case HCI_OP_WRITE_ENCRYPT_MODE:
3447 hci_cc_write_encrypt_mode(hdev, skb);
3448 break;
3449
3450 case HCI_OP_WRITE_SCAN_ENABLE:
3451 hci_cc_write_scan_enable(hdev, skb);
3452 break;
3453
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -08003454 case HCI_OP_SET_EVENT_FLT:
3455 hci_cc_set_event_filter(hdev, skb);
3456 break;
3457
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003458 case HCI_OP_READ_CLASS_OF_DEV:
3459 hci_cc_read_class_of_dev(hdev, skb);
3460 break;
3461
3462 case HCI_OP_WRITE_CLASS_OF_DEV:
3463 hci_cc_write_class_of_dev(hdev, skb);
3464 break;
3465
3466 case HCI_OP_READ_VOICE_SETTING:
3467 hci_cc_read_voice_setting(hdev, skb);
3468 break;
3469
3470 case HCI_OP_WRITE_VOICE_SETTING:
3471 hci_cc_write_voice_setting(hdev, skb);
3472 break;
3473
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -07003474 case HCI_OP_READ_NUM_SUPPORTED_IAC:
3475 hci_cc_read_num_supported_iac(hdev, skb);
3476 break;
3477
Marcel Holtmann333140b2008-07-14 20:13:48 +02003478 case HCI_OP_WRITE_SSP_MODE:
3479 hci_cc_write_ssp_mode(hdev, skb);
3480 break;
3481
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08003482 case HCI_OP_WRITE_SC_SUPPORT:
3483 hci_cc_write_sc_support(hdev, skb);
3484 break;
3485
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +05303486 case HCI_OP_READ_AUTH_PAYLOAD_TO:
3487 hci_cc_read_auth_payload_timeout(hdev, skb);
3488 break;
3489
3490 case HCI_OP_WRITE_AUTH_PAYLOAD_TO:
3491 hci_cc_write_auth_payload_timeout(hdev, skb);
3492 break;
3493
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003494 case HCI_OP_READ_LOCAL_VERSION:
3495 hci_cc_read_local_version(hdev, skb);
3496 break;
3497
3498 case HCI_OP_READ_LOCAL_COMMANDS:
3499 hci_cc_read_local_commands(hdev, skb);
3500 break;
3501
3502 case HCI_OP_READ_LOCAL_FEATURES:
3503 hci_cc_read_local_features(hdev, skb);
3504 break;
3505
Andre Guedes971e3a42011-06-30 19:20:52 -03003506 case HCI_OP_READ_LOCAL_EXT_FEATURES:
3507 hci_cc_read_local_ext_features(hdev, skb);
3508 break;
3509
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003510 case HCI_OP_READ_BUFFER_SIZE:
3511 hci_cc_read_buffer_size(hdev, skb);
3512 break;
3513
3514 case HCI_OP_READ_BD_ADDR:
3515 hci_cc_read_bd_addr(hdev, skb);
3516 break;
3517
Marcel Holtmanna47903602020-04-03 21:44:04 +02003518 case HCI_OP_READ_LOCAL_PAIRING_OPTS:
3519 hci_cc_read_local_pairing_opts(hdev, skb);
3520 break;
3521
Johan Hedbergf332ec62013-03-15 17:07:11 -05003522 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
3523 hci_cc_read_page_scan_activity(hdev, skb);
3524 break;
3525
Johan Hedberg4a3ee762013-03-15 17:07:12 -05003526 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
3527 hci_cc_write_page_scan_activity(hdev, skb);
3528 break;
3529
Johan Hedbergf332ec62013-03-15 17:07:11 -05003530 case HCI_OP_READ_PAGE_SCAN_TYPE:
3531 hci_cc_read_page_scan_type(hdev, skb);
3532 break;
3533
Johan Hedberg4a3ee762013-03-15 17:07:12 -05003534 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
3535 hci_cc_write_page_scan_type(hdev, skb);
3536 break;
3537
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02003538 case HCI_OP_READ_DATA_BLOCK_SIZE:
3539 hci_cc_read_data_block_size(hdev, skb);
3540 break;
3541
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02003542 case HCI_OP_READ_FLOW_CONTROL_MODE:
3543 hci_cc_read_flow_control_mode(hdev, skb);
3544 break;
3545
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03003546 case HCI_OP_READ_LOCAL_AMP_INFO:
3547 hci_cc_read_local_amp_info(hdev, skb);
3548 break;
3549
Johan Hedberg33f35722014-06-28 17:54:06 +03003550 case HCI_OP_READ_CLOCK:
3551 hci_cc_read_clock(hdev, skb);
3552 break;
3553
Johan Hedbergd5859e22011-01-25 01:19:58 +02003554 case HCI_OP_READ_INQ_RSP_TX_POWER:
3555 hci_cc_read_inq_rsp_tx_power(hdev, skb);
3556 break;
3557
Alain Michaud00bce3f2020-03-05 16:14:59 +00003558 case HCI_OP_READ_DEF_ERR_DATA_REPORTING:
3559 hci_cc_read_def_err_data_reporting(hdev, skb);
3560 break;
3561
3562 case HCI_OP_WRITE_DEF_ERR_DATA_REPORTING:
3563 hci_cc_write_def_err_data_reporting(hdev, skb);
3564 break;
3565
Johan Hedberg980e1a52011-01-22 06:10:07 +02003566 case HCI_OP_PIN_CODE_REPLY:
3567 hci_cc_pin_code_reply(hdev, skb);
3568 break;
3569
3570 case HCI_OP_PIN_CODE_NEG_REPLY:
3571 hci_cc_pin_code_neg_reply(hdev, skb);
3572 break;
3573
Szymon Jancc35938b2011-03-22 13:12:21 +01003574 case HCI_OP_READ_LOCAL_OOB_DATA:
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003575 hci_cc_read_local_oob_data(hdev, skb);
3576 break;
3577
3578 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
3579 hci_cc_read_local_oob_ext_data(hdev, skb);
Szymon Jancc35938b2011-03-22 13:12:21 +01003580 break;
3581
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003582 case HCI_OP_LE_READ_BUFFER_SIZE:
3583 hci_cc_le_read_buffer_size(hdev, skb);
3584 break;
3585
Johan Hedberg60e77322013-01-22 14:01:59 +02003586 case HCI_OP_LE_READ_LOCAL_FEATURES:
3587 hci_cc_le_read_local_features(hdev, skb);
3588 break;
3589
Johan Hedberg8fa19092012-10-19 20:57:49 +03003590 case HCI_OP_LE_READ_ADV_TX_POWER:
3591 hci_cc_le_read_adv_tx_power(hdev, skb);
3592 break;
3593
Johan Hedberga5c29682011-02-19 12:05:57 -03003594 case HCI_OP_USER_CONFIRM_REPLY:
3595 hci_cc_user_confirm_reply(hdev, skb);
3596 break;
3597
3598 case HCI_OP_USER_CONFIRM_NEG_REPLY:
3599 hci_cc_user_confirm_neg_reply(hdev, skb);
3600 break;
3601
Brian Gix1143d452011-11-23 08:28:34 -08003602 case HCI_OP_USER_PASSKEY_REPLY:
3603 hci_cc_user_passkey_reply(hdev, skb);
3604 break;
3605
3606 case HCI_OP_USER_PASSKEY_NEG_REPLY:
3607 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02003608 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09003609
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08003610 case HCI_OP_LE_SET_RANDOM_ADDR:
3611 hci_cc_le_set_random_addr(hdev, skb);
3612 break;
3613
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01003614 case HCI_OP_LE_SET_ADV_ENABLE:
3615 hci_cc_le_set_adv_enable(hdev, skb);
3616 break;
3617
Marcel Holtmann533553f2014-03-21 12:18:10 -07003618 case HCI_OP_LE_SET_SCAN_PARAM:
3619 hci_cc_le_set_scan_param(hdev, skb);
3620 break;
3621
Andre Guedeseb9d91f2011-05-26 16:23:52 -03003622 case HCI_OP_LE_SET_SCAN_ENABLE:
3623 hci_cc_le_set_scan_enable(hdev, skb);
3624 break;
3625
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003626 case HCI_OP_LE_READ_ACCEPT_LIST_SIZE:
3627 hci_cc_le_read_accept_list_size(hdev, skb);
Johan Hedbergcf1d0812013-01-22 14:02:00 +02003628 break;
3629
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003630 case HCI_OP_LE_CLEAR_ACCEPT_LIST:
3631 hci_cc_le_clear_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08003632 break;
3633
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003634 case HCI_OP_LE_ADD_TO_ACCEPT_LIST:
3635 hci_cc_le_add_to_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08003636 break;
3637
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003638 case HCI_OP_LE_DEL_FROM_ACCEPT_LIST:
3639 hci_cc_le_del_from_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08003640 break;
3641
Johan Hedberg9b008c02013-01-22 14:02:01 +02003642 case HCI_OP_LE_READ_SUPPORTED_STATES:
3643 hci_cc_le_read_supported_states(hdev, skb);
3644 break;
3645
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01003646 case HCI_OP_LE_READ_DEF_DATA_LEN:
3647 hci_cc_le_read_def_data_len(hdev, skb);
3648 break;
3649
3650 case HCI_OP_LE_WRITE_DEF_DATA_LEN:
3651 hci_cc_le_write_def_data_len(hdev, skb);
3652 break;
3653
Ankit Navikb950aa82018-08-17 07:29:19 +05303654 case HCI_OP_LE_ADD_TO_RESOLV_LIST:
3655 hci_cc_le_add_to_resolv_list(hdev, skb);
3656 break;
3657
3658 case HCI_OP_LE_DEL_FROM_RESOLV_LIST:
3659 hci_cc_le_del_from_resolv_list(hdev, skb);
3660 break;
3661
Ankit Navik545f2592018-06-29 12:13:20 +05303662 case HCI_OP_LE_CLEAR_RESOLV_LIST:
3663 hci_cc_le_clear_resolv_list(hdev, skb);
3664 break;
3665
Ankit Navikcfdb0c22018-06-29 12:12:50 +05303666 case HCI_OP_LE_READ_RESOLV_LIST_SIZE:
3667 hci_cc_le_read_resolv_list_size(hdev, skb);
3668 break;
3669
Ankit Navikaa12af72018-08-07 13:16:35 +05303670 case HCI_OP_LE_SET_ADDR_RESOLV_ENABLE:
3671 hci_cc_le_set_addr_resolution_enable(hdev, skb);
3672 break;
3673
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01003674 case HCI_OP_LE_READ_MAX_DATA_LEN:
3675 hci_cc_le_read_max_data_len(hdev, skb);
3676 break;
3677
Andre Guedesf9b49302011-06-30 19:20:53 -03003678 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
3679 hci_cc_write_le_host_supported(hdev, skb);
3680 break;
3681
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02003682 case HCI_OP_LE_SET_ADV_PARAM:
3683 hci_cc_set_adv_param(hdev, skb);
3684 break;
3685
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02003686 case HCI_OP_READ_RSSI:
3687 hci_cc_read_rssi(hdev, skb);
3688 break;
3689
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02003690 case HCI_OP_READ_TX_POWER:
3691 hci_cc_read_tx_power(hdev, skb);
3692 break;
3693
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08003694 case HCI_OP_WRITE_SSP_DEBUG_MODE:
3695 hci_cc_write_ssp_debug_mode(hdev, skb);
3696 break;
3697
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05303698 case HCI_OP_LE_SET_EXT_SCAN_PARAMS:
3699 hci_cc_le_set_ext_scan_param(hdev, skb);
3700 break;
3701
3702 case HCI_OP_LE_SET_EXT_SCAN_ENABLE:
3703 hci_cc_le_set_ext_scan_enable(hdev, skb);
3704 break;
3705
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303706 case HCI_OP_LE_SET_DEFAULT_PHY:
3707 hci_cc_le_set_default_phy(hdev, skb);
3708 break;
3709
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05303710 case HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS:
3711 hci_cc_le_read_num_adv_sets(hdev, skb);
3712 break;
3713
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05303714 case HCI_OP_LE_SET_EXT_ADV_PARAMS:
3715 hci_cc_set_ext_adv_param(hdev, skb);
3716 break;
3717
3718 case HCI_OP_LE_SET_EXT_ADV_ENABLE:
3719 hci_cc_le_set_ext_adv_enable(hdev, skb);
3720 break;
3721
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303722 case HCI_OP_LE_SET_ADV_SET_RAND_ADDR:
3723 hci_cc_le_set_adv_set_random_addr(hdev, skb);
3724 break;
3725
Daniel Winkler7c395ea2020-12-03 12:12:51 -08003726 case HCI_OP_LE_READ_TRANSMIT_POWER:
3727 hci_cc_le_read_transmit_power(hdev, skb);
3728 break;
3729
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003730 default:
Johan Hedberge62144872015-04-02 13:41:08 +03003731 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003732 break;
3733 }
3734
Kiran Kecb71f252021-08-16 05:07:47 +05303735 handle_cmd_cnt_and_timer(hdev, ev->ncmd);
Johan Hedberg600b2152015-03-28 11:17:36 +02003736
Johan Hedberge62144872015-04-02 13:41:08 +03003737 hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
3738 req_complete_skb);
Johan Hedberg9238f362013-03-05 20:37:48 +02003739
João Paulo Rechi Vitaf80c5da2019-05-02 10:01:52 +08003740 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
3741 bt_dev_err(hdev,
3742 "unexpected event for opcode 0x%4.4x", *opcode);
3743 return;
3744 }
3745
Johan Hedberg600b2152015-03-28 11:17:36 +02003746 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3747 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003748}
3749
Johan Hedberge62144872015-04-02 13:41:08 +03003750static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb,
3751 u16 *opcode, u8 *status,
3752 hci_req_complete_t *req_complete,
3753 hci_req_complete_skb_t *req_complete_skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003754{
3755 struct hci_ev_cmd_status *ev = (void *) skb->data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003756
3757 skb_pull(skb, sizeof(*ev));
3758
Johan Hedberge62144872015-04-02 13:41:08 +03003759 *opcode = __le16_to_cpu(ev->opcode);
3760 *status = ev->status;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003761
Johan Hedberge62144872015-04-02 13:41:08 +03003762 switch (*opcode) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003763 case HCI_OP_INQUIRY:
3764 hci_cs_inquiry(hdev, ev->status);
3765 break;
3766
3767 case HCI_OP_CREATE_CONN:
3768 hci_cs_create_conn(hdev, ev->status);
3769 break;
3770
Kuba Pawlak9645c762014-11-06 19:36:53 +01003771 case HCI_OP_DISCONNECT:
3772 hci_cs_disconnect(hdev, ev->status);
3773 break;
3774
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003775 case HCI_OP_ADD_SCO:
3776 hci_cs_add_sco(hdev, ev->status);
3777 break;
3778
Marcel Holtmannf85585552008-07-14 20:13:49 +02003779 case HCI_OP_AUTH_REQUESTED:
3780 hci_cs_auth_requested(hdev, ev->status);
3781 break;
3782
3783 case HCI_OP_SET_CONN_ENCRYPT:
3784 hci_cs_set_conn_encrypt(hdev, ev->status);
3785 break;
3786
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003787 case HCI_OP_REMOTE_NAME_REQ:
3788 hci_cs_remote_name_req(hdev, ev->status);
3789 break;
3790
Marcel Holtmann769be972008-07-14 20:13:49 +02003791 case HCI_OP_READ_REMOTE_FEATURES:
3792 hci_cs_read_remote_features(hdev, ev->status);
3793 break;
3794
3795 case HCI_OP_READ_REMOTE_EXT_FEATURES:
3796 hci_cs_read_remote_ext_features(hdev, ev->status);
3797 break;
3798
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003799 case HCI_OP_SETUP_SYNC_CONN:
3800 hci_cs_setup_sync_conn(hdev, ev->status);
3801 break;
3802
Kiran Kb2af2642021-09-07 15:42:43 +05303803 case HCI_OP_ENHANCED_SETUP_SYNC_CONN:
3804 hci_cs_enhanced_setup_sync_conn(hdev, ev->status);
3805 break;
3806
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003807 case HCI_OP_SNIFF_MODE:
3808 hci_cs_sniff_mode(hdev, ev->status);
3809 break;
3810
3811 case HCI_OP_EXIT_SNIFF_MODE:
3812 hci_cs_exit_sniff_mode(hdev, ev->status);
3813 break;
3814
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01003815 case HCI_OP_SWITCH_ROLE:
3816 hci_cs_switch_role(hdev, ev->status);
3817 break;
3818
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02003819 case HCI_OP_LE_CREATE_CONN:
3820 hci_cs_le_create_conn(hdev, ev->status);
3821 break;
3822
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07003823 case HCI_OP_LE_READ_REMOTE_FEATURES:
3824 hci_cs_le_read_remote_features(hdev, ev->status);
3825 break;
3826
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02003827 case HCI_OP_LE_START_ENC:
3828 hci_cs_le_start_enc(hdev, ev->status);
3829 break;
3830
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02003831 case HCI_OP_LE_EXT_CREATE_CONN:
3832 hci_cs_le_ext_create_conn(hdev, ev->status);
3833 break;
3834
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003835 default:
Johan Hedberge62144872015-04-02 13:41:08 +03003836 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003837 break;
3838 }
3839
Kiran Kecb71f252021-08-16 05:07:47 +05303840 handle_cmd_cnt_and_timer(hdev, ev->ncmd);
Johan Hedberg600b2152015-03-28 11:17:36 +02003841
Johan Hedberg444c6dd2015-04-02 13:41:07 +03003842 /* Indicate request completion if the command failed. Also, if
3843 * we're not waiting for a special event and we get a success
3844 * command status we should try to flag the request as completed
3845 * (since for this kind of commands there will not be a command
3846 * complete event).
3847 */
Johan Hedberg02350a72013-04-03 21:50:29 +03003848 if (ev->status ||
Marcel Holtmann242c0eb2015-10-25 22:45:53 +01003849 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->hci.req_event))
Johan Hedberge62144872015-04-02 13:41:08 +03003850 hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
3851 req_complete_skb);
Johan Hedberg9238f362013-03-05 20:37:48 +02003852
João Paulo Rechi Vitaf80c5da2019-05-02 10:01:52 +08003853 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
3854 bt_dev_err(hdev,
3855 "unexpected event for opcode 0x%4.4x", *opcode);
3856 return;
3857 }
3858
Johan Hedberg600b2152015-03-28 11:17:36 +02003859 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3860 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003861}
3862
Marcel Holtmann24dfa342014-11-02 02:56:41 +01003863static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
3864{
3865 struct hci_ev_hardware_error *ev = (void *) skb->data;
3866
Marcel Holtmannc7741d12015-01-28 11:09:55 -08003867 hdev->hw_error_code = ev->code;
3868
3869 queue_work(hdev->req_workqueue, &hdev->error_reset);
Marcel Holtmann24dfa342014-11-02 02:56:41 +01003870}
3871
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003872static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003873{
3874 struct hci_ev_role_change *ev = (void *) skb->data;
3875 struct hci_conn *conn;
3876
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003877 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003878
3879 hci_dev_lock(hdev);
3880
3881 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3882 if (conn) {
Johan Hedberg40bef302014-07-16 11:42:27 +03003883 if (!ev->status)
3884 conn->role = ev->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003885
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003886 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003887
3888 hci_role_switch_cfm(conn, ev->status, ev->role);
3889 }
3890
3891 hci_dev_unlock(hdev);
3892}
3893
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003894static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003896 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 int i;
3898
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02003899 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003900 bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02003901 return;
3902 }
3903
Gustavo A. R. Silva16e18342019-02-07 18:40:33 -06003904 if (skb->len < sizeof(*ev) ||
3905 skb->len < struct_size(ev, handles, ev->num_hndl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 BT_DBG("%s bad parameters", hdev->name);
3907 return;
3908 }
3909
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02003910 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
3911
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003912 for (i = 0; i < ev->num_hndl; i++) {
3913 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 struct hci_conn *conn;
3915 __u16 handle, count;
3916
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003917 handle = __le16_to_cpu(info->handle);
3918 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919
3920 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003921 if (!conn)
3922 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003924 conn->sent -= count;
3925
3926 switch (conn->type) {
3927 case ACL_LINK:
3928 hdev->acl_cnt += count;
3929 if (hdev->acl_cnt > hdev->acl_pkts)
3930 hdev->acl_cnt = hdev->acl_pkts;
3931 break;
3932
3933 case LE_LINK:
3934 if (hdev->le_pkts) {
3935 hdev->le_cnt += count;
3936 if (hdev->le_cnt > hdev->le_pkts)
3937 hdev->le_cnt = hdev->le_pkts;
3938 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02003939 hdev->acl_cnt += count;
3940 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 hdev->acl_cnt = hdev->acl_pkts;
3942 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003943 break;
3944
3945 case SCO_LINK:
3946 hdev->sco_cnt += count;
3947 if (hdev->sco_cnt > hdev->sco_pkts)
3948 hdev->sco_cnt = hdev->sco_pkts;
3949 break;
3950
3951 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003952 bt_dev_err(hdev, "unknown type %d conn %p",
3953 conn->type, conn);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003954 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 }
3956 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003957
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02003958 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959}
3960
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003961static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
3962 __u16 handle)
3963{
3964 struct hci_chan *chan;
3965
3966 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02003967 case HCI_PRIMARY:
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003968 return hci_conn_hash_lookup_handle(hdev, handle);
3969 case HCI_AMP:
3970 chan = hci_chan_lookup_handle(hdev, handle);
3971 if (chan)
3972 return chan->conn;
3973 break;
3974 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003975 bt_dev_err(hdev, "unknown dev_type %d", hdev->dev_type);
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003976 break;
3977 }
3978
3979 return NULL;
3980}
3981
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003982static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003983{
3984 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
3985 int i;
3986
3987 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003988 bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003989 return;
3990 }
3991
Gustavo A. R. Silva16e18342019-02-07 18:40:33 -06003992 if (skb->len < sizeof(*ev) ||
3993 skb->len < struct_size(ev, handles, ev->num_hndl)) {
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003994 BT_DBG("%s bad parameters", hdev->name);
3995 return;
3996 }
3997
3998 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003999 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004000
4001 for (i = 0; i < ev->num_hndl; i++) {
4002 struct hci_comp_blocks_info *info = &ev->handles[i];
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03004003 struct hci_conn *conn = NULL;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004004 __u16 handle, block_count;
4005
4006 handle = __le16_to_cpu(info->handle);
4007 block_count = __le16_to_cpu(info->blocks);
4008
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03004009 conn = __hci_conn_lookup_handle(hdev, handle);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004010 if (!conn)
4011 continue;
4012
4013 conn->sent -= block_count;
4014
4015 switch (conn->type) {
4016 case ACL_LINK:
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03004017 case AMP_LINK:
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004018 hdev->block_cnt += block_count;
4019 if (hdev->block_cnt > hdev->num_blocks)
4020 hdev->block_cnt = hdev->num_blocks;
4021 break;
4022
4023 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004024 bt_dev_err(hdev, "unknown type %d conn %p",
4025 conn->type, conn);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004026 break;
4027 }
4028 }
4029
4030 queue_work(hdev->workqueue, &hdev->tx_work);
4031}
4032
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004033static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004035 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004036 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004038 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039
4040 hci_dev_lock(hdev);
4041
Marcel Holtmann04837f62006-07-03 10:02:33 +02004042 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4043 if (conn) {
4044 conn->mode = ev->mode;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004045
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03004046 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
4047 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02004048 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02004049 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004050 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02004051 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004052 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04004053
Johan Hedberg51a8efd2012-01-16 06:10:31 +02004054 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04004055 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004056 }
4057
4058 hci_dev_unlock(hdev);
4059}
4060
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004061static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004063 struct hci_ev_pin_code_req *ev = (void *) skb->data;
4064 struct hci_conn *conn;
4065
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004066 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004067
4068 hci_dev_lock(hdev);
4069
4070 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02004071 if (!conn)
4072 goto unlock;
4073
4074 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004075 hci_conn_hold(conn);
4076 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004077 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004078 }
4079
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004080 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedberg2f407f02014-07-17 15:35:40 +03004081 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
Johan Hedberg03b555e2011-01-04 15:40:05 +02004082 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004083 sizeof(ev->bdaddr), &ev->bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004084 } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02004085 u8 secure;
4086
4087 if (conn->pending_sec_level == BT_SECURITY_HIGH)
4088 secure = 1;
4089 else
4090 secure = 0;
4091
Johan Hedberg744cf192011-11-08 20:40:14 +02004092 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02004093 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02004094
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02004095unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004096 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097}
4098
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004099static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
4100{
4101 if (key_type == HCI_LK_CHANGED_COMBINATION)
4102 return;
4103
4104 conn->pin_length = pin_len;
4105 conn->key_type = key_type;
4106
4107 switch (key_type) {
4108 case HCI_LK_LOCAL_UNIT:
4109 case HCI_LK_REMOTE_UNIT:
4110 case HCI_LK_DEBUG_COMBINATION:
4111 return;
4112 case HCI_LK_COMBINATION:
4113 if (pin_len == 16)
4114 conn->pending_sec_level = BT_SECURITY_HIGH;
4115 else
4116 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4117 break;
4118 case HCI_LK_UNAUTH_COMBINATION_P192:
4119 case HCI_LK_UNAUTH_COMBINATION_P256:
4120 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4121 break;
4122 case HCI_LK_AUTH_COMBINATION_P192:
4123 conn->pending_sec_level = BT_SECURITY_HIGH;
4124 break;
4125 case HCI_LK_AUTH_COMBINATION_P256:
4126 conn->pending_sec_level = BT_SECURITY_FIPS;
4127 break;
4128 }
4129}
4130
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004131static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004133 struct hci_ev_link_key_req *ev = (void *) skb->data;
4134 struct hci_cp_link_key_reply cp;
4135 struct hci_conn *conn;
4136 struct link_key *key;
4137
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004138 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004139
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004140 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004141 return;
4142
4143 hci_dev_lock(hdev);
4144
4145 key = hci_find_link_key(hdev, &ev->bdaddr);
4146 if (!key) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03004147 BT_DBG("%s link key not found for %pMR", hdev->name,
4148 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004149 goto not_found;
4150 }
4151
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03004152 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
4153 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004154
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004155 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004156 if (conn) {
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03004157 clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
4158
Marcel Holtmann66138ce2014-01-10 02:07:20 -08004159 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
4160 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03004161 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004162 BT_DBG("%s ignoring unauthenticated key", hdev->name);
4163 goto not_found;
4164 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004165
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004166 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Johan Hedbergf3fb0b52014-06-02 10:12:44 +03004167 (conn->pending_sec_level == BT_SECURITY_HIGH ||
4168 conn->pending_sec_level == BT_SECURITY_FIPS)) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03004169 BT_DBG("%s ignoring key unauthenticated for high security",
4170 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004171 goto not_found;
4172 }
4173
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004174 conn_set_key(conn, key->type, key->pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004175 }
4176
4177 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03004178 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004179
4180 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
4181
4182 hci_dev_unlock(hdev);
4183
4184 return;
4185
4186not_found:
4187 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
4188 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189}
4190
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004191static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004193 struct hci_ev_link_key_notify *ev = (void *) skb->data;
4194 struct hci_conn *conn;
Johan Hedberg7652ff62014-06-24 13:15:49 +03004195 struct link_key *key;
4196 bool persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004197 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004198
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004199 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004200
4201 hci_dev_lock(hdev);
4202
4203 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004204 if (!conn)
4205 goto unlock;
4206
4207 hci_conn_hold(conn);
4208 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
4209 hci_conn_drop(conn);
4210
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03004211 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004212 conn_set_key(conn, ev->key_type, conn->pin_length);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004213
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004214 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg7652ff62014-06-24 13:15:49 +03004215 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004216
Johan Hedberg7652ff62014-06-24 13:15:49 +03004217 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
4218 ev->key_type, pin_len, &persistent);
4219 if (!key)
4220 goto unlock;
4221
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004222 /* Update connection information since adding the key will have
4223 * fixed up the type in the case of changed combination keys.
4224 */
4225 if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
4226 conn_set_key(conn, key->type, key->pin_len);
4227
Johan Hedberg7652ff62014-06-24 13:15:49 +03004228 mgmt_new_link_key(hdev, key, persistent);
4229
Johan Hedberg6d5650c2014-06-24 13:15:51 +03004230 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
4231 * is set. If it's not set simply remove the key from the kernel
4232 * list (we've still notified user space about it but with
4233 * store_hint being 0).
4234 */
4235 if (key->type == HCI_LK_DEBUG_COMBINATION &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004236 !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg0378b592014-11-19 15:22:22 +02004237 list_del_rcu(&key->list);
4238 kfree_rcu(key, rcu);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004239 goto unlock;
Johan Hedberg6d5650c2014-06-24 13:15:51 +03004240 }
Johan Hedberg7652ff62014-06-24 13:15:49 +03004241
Johan Hedberg82c13d42014-12-03 11:03:06 +02004242 if (persistent)
4243 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4244 else
4245 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4246
Johan Hedberg7652ff62014-06-24 13:15:49 +03004247unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004248 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249}
4250
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004251static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02004252{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004253 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004254 struct hci_conn *conn;
4255
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004256 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004257
4258 hci_dev_lock(hdev);
4259
4260 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 if (conn && !ev->status) {
4262 struct inquiry_entry *ie;
4263
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004264 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4265 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 ie->data.clock_offset = ev->clock_offset;
4267 ie->timestamp = jiffies;
4268 }
4269 }
4270
4271 hci_dev_unlock(hdev);
4272}
4273
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004274static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02004275{
4276 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
4277 struct hci_conn *conn;
4278
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004279 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02004280
4281 hci_dev_lock(hdev);
4282
4283 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4284 if (conn && !ev->status)
4285 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
4286
4287 hci_dev_unlock(hdev);
4288}
4289
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004290static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004291{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004292 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004293 struct inquiry_entry *ie;
4294
4295 BT_DBG("%s", hdev->name);
4296
4297 hci_dev_lock(hdev);
4298
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004299 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4300 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004301 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
4302 ie->timestamp = jiffies;
4303 }
4304
4305 hci_dev_unlock(hdev);
4306}
4307
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004308static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
4309 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004310{
4311 struct inquiry_data data;
4312 int num_rsp = *((__u8 *) skb->data);
4313
4314 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
4315
4316 if (!num_rsp)
4317 return;
4318
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004319 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03004320 return;
4321
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004322 hci_dev_lock(hdev);
4323
4324 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01004325 struct inquiry_info_with_rssi_and_pscan_mode *info;
4326 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004327
Peilin Ye629b49c2020-07-10 17:45:26 -04004328 if (skb->len < num_rsp * sizeof(*info) + 1)
4329 goto unlock;
4330
Johan Hedberge17acd42011-03-30 23:57:16 +03004331 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004332 u32 flags;
4333
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004334 bacpy(&data.bdaddr, &info->bdaddr);
4335 data.pscan_rep_mode = info->pscan_rep_mode;
4336 data.pscan_period_mode = info->pscan_period_mode;
4337 data.pscan_mode = info->pscan_mode;
4338 memcpy(data.dev_class, info->dev_class, 3);
4339 data.clock_offset = info->clock_offset;
4340 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004341 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02004342
Marcel Holtmannaf589252014-07-01 14:11:20 +02004343 flags = hci_inquiry_cache_update(hdev, &data, false);
4344
Johan Hedberg48264f02011-11-09 13:58:58 +02004345 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004346 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004347 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004348 }
4349 } else {
4350 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
4351
Peilin Ye629b49c2020-07-10 17:45:26 -04004352 if (skb->len < num_rsp * sizeof(*info) + 1)
4353 goto unlock;
4354
Johan Hedberge17acd42011-03-30 23:57:16 +03004355 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004356 u32 flags;
4357
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004358 bacpy(&data.bdaddr, &info->bdaddr);
4359 data.pscan_rep_mode = info->pscan_rep_mode;
4360 data.pscan_period_mode = info->pscan_period_mode;
4361 data.pscan_mode = 0x00;
4362 memcpy(data.dev_class, info->dev_class, 3);
4363 data.clock_offset = info->clock_offset;
4364 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004365 data.ssp_mode = 0x00;
Marcel Holtmannaf589252014-07-01 14:11:20 +02004366
4367 flags = hci_inquiry_cache_update(hdev, &data, false);
4368
Johan Hedberg48264f02011-11-09 13:58:58 +02004369 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004370 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004371 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004372 }
4373 }
4374
Peilin Ye629b49c2020-07-10 17:45:26 -04004375unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004376 hci_dev_unlock(hdev);
4377}
4378
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004379static void hci_remote_ext_features_evt(struct hci_dev *hdev,
4380 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004381{
Marcel Holtmann41a96212008-07-14 20:13:48 +02004382 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
4383 struct hci_conn *conn;
4384
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004385 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004386
Marcel Holtmann41a96212008-07-14 20:13:48 +02004387 hci_dev_lock(hdev);
4388
4389 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02004390 if (!conn)
4391 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004392
Johan Hedbergcad718e2013-04-17 15:00:51 +03004393 if (ev->page < HCI_MAX_PAGES)
4394 memcpy(conn->features[ev->page], ev->features, 8);
4395
Johan Hedbergccd556f2010-11-10 17:11:51 +02004396 if (!ev->status && ev->page == 0x01) {
4397 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004398
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004399 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4400 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02004401 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02004402
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05304403 if (ev->features[0] & LMP_HOST_SSP) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02004404 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05304405 } else {
4406 /* It is mandatory by the Bluetooth specification that
4407 * Extended Inquiry Results are only used when Secure
4408 * Simple Pairing is enabled, but some devices violate
4409 * this.
4410 *
4411 * To make these devices work, the internal SSP
4412 * enabled flag needs to be cleared if the remote host
4413 * features do not indicate SSP support */
4414 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
4415 }
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -08004416
4417 if (ev->features[0] & LMP_HOST_SC)
4418 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004419 }
4420
Johan Hedbergccd556f2010-11-10 17:11:51 +02004421 if (conn->state != BT_CONFIG)
4422 goto unlock;
4423
Johan Hedberg671267b2012-05-12 16:11:50 -03004424 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02004425 struct hci_cp_remote_name_req cp;
4426 memset(&cp, 0, sizeof(cp));
4427 bacpy(&cp.bdaddr, &conn->dst);
4428 cp.pscan_rep_mode = 0x02;
4429 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02004430 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07004431 mgmt_device_connected(hdev, conn, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02004432
Johan Hedberg127178d2010-11-18 22:22:29 +02004433 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02004434 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02004435 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02004436 hci_conn_drop(conn);
Johan Hedbergccd556f2010-11-10 17:11:51 +02004437 }
4438
4439unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02004440 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004441}
4442
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004443static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
4444 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004445{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004446 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
4447 struct hci_conn *conn;
Chethan T Nf4f9fa02021-09-07 15:42:48 +05304448 unsigned int notify_evt;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004449
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004450 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004451
4452 hci_dev_lock(hdev);
4453
4454 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004455 if (!conn) {
4456 if (ev->link_type == ESCO_LINK)
4457 goto unlock;
4458
Kuba Pawlak618353b2015-08-28 13:05:22 +01004459 /* When the link type in the event indicates SCO connection
4460 * and lookup of the connection object fails, then check
4461 * if an eSCO connection object exists.
4462 *
4463 * The core limits the synchronous connections to either
4464 * SCO or eSCO. The eSCO connection is preferred and tried
4465 * to be setup first and until successfully established,
4466 * the link type will be hinted as eSCO.
4467 */
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004468 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
4469 if (!conn)
4470 goto unlock;
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004471 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004472
Marcel Holtmann732547f2009-04-19 19:14:14 +02004473 switch (ev->status) {
4474 case 0x00:
Desmond Cheong Zhi Xi92fe24a2021-07-28 15:51:04 +08004475 /* The synchronous connection complete event should only be
4476 * sent once per new connection. Receiving a successful
4477 * complete event when the connection status is already
4478 * BT_CONNECTED means that the device is misbehaving and sent
4479 * multiple complete event packets for the same new connection.
4480 *
4481 * Registering the device more than once can corrupt kernel
4482 * memory, hence upon detecting this invalid event, we report
4483 * an error and ignore the packet.
4484 */
4485 if (conn->state == BT_CONNECTED) {
4486 bt_dev_err(hdev, "Ignoring connect complete event for existing connection");
4487 goto unlock;
4488 }
4489
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004490 conn->handle = __le16_to_cpu(ev->handle);
4491 conn->state = BT_CONNECTED;
Kuba Pawlak618353b2015-08-28 13:05:22 +01004492 conn->type = ev->link_type;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02004493
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01004494 hci_debugfs_create_conn(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02004495 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02004496 break;
4497
Nick Pelly81218d22014-06-30 11:25:01 +05304498 case 0x10: /* Connection Accept Timeout */
Frédéric Dalleau1a4c9582013-08-19 14:24:02 +02004499 case 0x0d: /* Connection Rejected due to Limited Resources */
Stephen Coe705e5712010-02-16 11:29:44 -05004500 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02004501 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08004502 case 0x1a: /* Unsupported Remote Feature */
Hsin-Yu Chao56b54532020-05-15 17:27:04 +08004503 case 0x1e: /* Invalid LMP Parameters */
Marcel Holtmann732547f2009-04-19 19:14:14 +02004504 case 0x1f: /* Unspecified error */
Andrew Earl27539bc2014-03-10 10:31:04 +00004505 case 0x20: /* Unsupported LMP Parameter value */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02004506 if (conn->out) {
Marcel Holtmann732547f2009-04-19 19:14:14 +02004507 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
4508 (hdev->esco_type & EDR_ESCO_MASK);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02004509 if (hci_setup_sync(conn, conn->link->handle))
4510 goto unlock;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004511 }
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05004512 fallthrough;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004513
4514 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004515 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004516 break;
4517 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004518
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02004519 bt_dev_dbg(hdev, "SCO connected with air mode: %02x", ev->air_mode);
4520
Kiran K06d213d82021-04-08 22:31:59 +05304521 switch (ev->air_mode) {
4522 case 0x02:
Chethan T Nf4f9fa02021-09-07 15:42:48 +05304523 notify_evt = HCI_NOTIFY_ENABLE_SCO_CVSD;
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02004524 break;
Kiran K06d213d82021-04-08 22:31:59 +05304525 case 0x03:
Chethan T Nf4f9fa02021-09-07 15:42:48 +05304526 notify_evt = HCI_NOTIFY_ENABLE_SCO_TRANSP;
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02004527 break;
4528 }
4529
Chethan T Nf4f9fa02021-09-07 15:42:48 +05304530 /* Notify only in case of SCO over HCI transport data path which
4531 * is zero and non-zero value shall be non-HCI transport data path
4532 */
4533 if (conn->codec.data_path == 0) {
4534 if (hdev->notify)
4535 hdev->notify(hdev, notify_evt);
4536 }
4537
Johan Hedberg539c4962015-02-18 14:53:57 +02004538 hci_connect_cfm(conn, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004539 if (ev->status)
4540 hci_conn_del(conn);
4541
4542unlock:
4543 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004544}
4545
Marcel Holtmannefdcf8e2013-10-15 10:31:12 -07004546static inline size_t eir_get_length(u8 *eir, size_t eir_len)
4547{
4548 size_t parsed = 0;
4549
4550 while (parsed < eir_len) {
4551 u8 field_len = eir[0];
4552
4553 if (field_len == 0)
4554 return parsed;
4555
4556 parsed += field_len + 1;
4557 eir += field_len + 1;
4558 }
4559
4560 return eir_len;
4561}
4562
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004563static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
4564 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004565{
4566 struct inquiry_data data;
4567 struct extended_inquiry_info *info = (void *) (skb->data + 1);
4568 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05304569 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004570
4571 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
4572
Peilin Ye51c19bf2020-07-10 12:09:15 -04004573 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004574 return;
4575
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004576 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03004577 return;
4578
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004579 hci_dev_lock(hdev);
4580
Johan Hedberge17acd42011-03-30 23:57:16 +03004581 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004582 u32 flags;
4583 bool name_known;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004584
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004585 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01004586 data.pscan_rep_mode = info->pscan_rep_mode;
4587 data.pscan_period_mode = info->pscan_period_mode;
4588 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004589 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01004590 data.clock_offset = info->clock_offset;
4591 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004592 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004593
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004594 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg0d3b7f62016-01-05 13:19:31 +02004595 name_known = eir_get_data(info->data,
4596 sizeof(info->data),
4597 EIR_NAME_COMPLETE, NULL);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004598 else
4599 name_known = true;
4600
Marcel Holtmannaf589252014-07-01 14:11:20 +02004601 flags = hci_inquiry_cache_update(hdev, &data, name_known);
4602
Vishal Agarwal9d939d92012-04-26 19:19:56 +05304603 eir_len = eir_get_length(info->data, sizeof(info->data));
Marcel Holtmannaf589252014-07-01 14:11:20 +02004604
Johan Hedberg48264f02011-11-09 13:58:58 +02004605 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004606 info->dev_class, info->rssi,
4607 flags, info->data, eir_len, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004608 }
4609
4610 hci_dev_unlock(hdev);
4611}
4612
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004613static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
4614 struct sk_buff *skb)
4615{
4616 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
4617 struct hci_conn *conn;
4618
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004619 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004620 __le16_to_cpu(ev->handle));
4621
4622 hci_dev_lock(hdev);
4623
4624 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4625 if (!conn)
4626 goto unlock;
4627
Johan Hedberg9eb1fbf2014-04-11 12:02:31 -07004628 /* For BR/EDR the necessary steps are taken through the
4629 * auth_complete event.
4630 */
4631 if (conn->type != LE_LINK)
4632 goto unlock;
4633
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004634 if (!ev->status)
4635 conn->sec_level = conn->pending_sec_level;
4636
4637 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
4638
4639 if (ev->status && conn->state == BT_CONNECTED) {
Andre Guedesbed71742013-01-30 11:50:56 -03004640 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
David Herrmann76a68ba2013-04-06 20:28:37 +02004641 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004642 goto unlock;
4643 }
4644
4645 if (conn->state == BT_CONFIG) {
4646 if (!ev->status)
4647 conn->state = BT_CONNECTED;
4648
Johan Hedberg539c4962015-02-18 14:53:57 +02004649 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02004650 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004651 } else {
4652 hci_auth_cfm(conn, ev->status);
4653
4654 hci_conn_hold(conn);
4655 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004656 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004657 }
4658
4659unlock:
4660 hci_dev_unlock(hdev);
4661}
4662
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004663static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004664{
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004665 /* If remote requests no-bonding follow that lead */
Mikel Astizacabae92013-06-28 10:56:28 +02004666 if (conn->remote_auth == HCI_AT_NO_BONDING ||
4667 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02004668 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004669
Mikel Astizb7f94c82014-04-08 14:21:31 +02004670 /* If both remote and local have enough IO capabilities, require
4671 * MITM protection
4672 */
4673 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
4674 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
4675 return conn->remote_auth | 0x01;
4676
Timo Mueller7e741702014-04-08 14:21:33 +02004677 /* No MITM protection possible so ignore remote requirement */
4678 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004679}
4680
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004681static u8 bredr_oob_data_present(struct hci_conn *conn)
4682{
4683 struct hci_dev *hdev = conn->hdev;
4684 struct oob_data *data;
4685
4686 data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
4687 if (!data)
4688 return 0x00;
4689
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004690 if (bredr_sc_enabled(hdev)) {
4691 /* When Secure Connections is enabled, then just
4692 * return the present value stored with the OOB
4693 * data. The stored value contains the right present
4694 * information. However it can only be trusted when
4695 * not in Secure Connection Only mode.
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004696 */
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004697 if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
4698 return data->present;
4699
4700 /* When Secure Connections Only mode is enabled, then
4701 * the P-256 values are required. If they are not
4702 * available, then do not declare that OOB data is
4703 * present.
4704 */
4705 if (!memcmp(data->rand256, ZERO_KEY, 16) ||
4706 !memcmp(data->hash256, ZERO_KEY, 16))
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004707 return 0x00;
4708
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004709 return 0x02;
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004710 }
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004711
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004712 /* When Secure Connections is not enabled or actually
4713 * not supported by the hardware, then check that if
4714 * P-192 data values are present.
4715 */
4716 if (!memcmp(data->rand192, ZERO_KEY, 16) ||
4717 !memcmp(data->hash192, ZERO_KEY, 16))
4718 return 0x00;
4719
4720 return 0x01;
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004721}
4722
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004723static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02004724{
4725 struct hci_ev_io_capa_request *ev = (void *) skb->data;
4726 struct hci_conn *conn;
4727
4728 BT_DBG("%s", hdev->name);
4729
4730 hci_dev_lock(hdev);
4731
4732 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004733 if (!conn)
4734 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02004735
Johan Hedberg03b555e2011-01-04 15:40:05 +02004736 hci_conn_hold(conn);
4737
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004738 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg03b555e2011-01-04 15:40:05 +02004739 goto unlock;
4740
Johan Hedberg2f407f02014-07-17 15:35:40 +03004741 /* Allow pairing if we're pairable, the initiators of the
4742 * pairing or if the remote is not requesting bonding.
4743 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004744 if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
Johan Hedberg2f407f02014-07-17 15:35:40 +03004745 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03004746 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004747 struct hci_cp_io_capability_reply cp;
4748
4749 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05304750 /* Change the IO capability from KeyboardDisplay
4751 * to DisplayYesNo as it is not supported by BT spec. */
4752 cp.capability = (conn->io_capability == 0x04) ?
Mikel Astiza7676312013-06-28 10:56:29 +02004753 HCI_IO_DISPLAY_YESNO : conn->io_capability;
Mikel Astizb7f94c82014-04-08 14:21:31 +02004754
4755 /* If we are initiators, there is no remote information yet */
4756 if (conn->remote_auth == 0xff) {
Mikel Astizb16c6602014-04-08 14:21:34 +02004757 /* Request MITM protection if our IO caps allow it
Johan Hedberg4ad51a72014-06-09 14:41:25 +03004758 * except for the no-bonding case.
Mikel Astizb16c6602014-04-08 14:21:34 +02004759 */
Mikel Astiz6fd6b912014-04-08 14:21:32 +02004760 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedberg9f743d72014-07-17 11:56:33 +03004761 conn->auth_type != HCI_AT_NO_BONDING)
Johan Hedberg6c538232014-07-11 15:32:23 +03004762 conn->auth_type |= 0x01;
Mikel Astizb7f94c82014-04-08 14:21:31 +02004763 } else {
4764 conn->auth_type = hci_get_auth_req(conn);
Mikel Astizb7f94c82014-04-08 14:21:31 +02004765 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004766
Johan Hedberg82c295b2014-07-30 09:22:24 +03004767 /* If we're not bondable, force one of the non-bondable
4768 * authentication requirement values.
4769 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004770 if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedberg82c295b2014-07-30 09:22:24 +03004771 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
4772
4773 cp.authentication = conn->auth_type;
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004774 cp.oob_data = bredr_oob_data_present(conn);
Szymon Jancce85ee12011-03-22 13:12:23 +01004775
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004776 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004777 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004778 } else {
4779 struct hci_cp_io_capability_neg_reply cp;
4780
4781 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02004782 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02004783
4784 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004785 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004786 }
4787
4788unlock:
4789 hci_dev_unlock(hdev);
4790}
4791
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004792static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02004793{
4794 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
4795 struct hci_conn *conn;
4796
4797 BT_DBG("%s", hdev->name);
4798
4799 hci_dev_lock(hdev);
4800
4801 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4802 if (!conn)
4803 goto unlock;
4804
Johan Hedberg03b555e2011-01-04 15:40:05 +02004805 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02004806 conn->remote_auth = ev->authentication;
4807
4808unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02004809 hci_dev_unlock(hdev);
4810}
4811
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004812static void hci_user_confirm_request_evt(struct hci_dev *hdev,
4813 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03004814{
4815 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004816 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07004817 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03004818
4819 BT_DBG("%s", hdev->name);
4820
4821 hci_dev_lock(hdev);
4822
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004823 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg7a828902011-04-28 11:28:53 -07004824 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03004825
Johan Hedberg7a828902011-04-28 11:28:53 -07004826 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4827 if (!conn)
4828 goto unlock;
4829
4830 loc_mitm = (conn->auth_type & 0x01);
4831 rem_mitm = (conn->remote_auth & 0x01);
4832
4833 /* If we require MITM but the remote device can't provide that
Johan Hedberg6c538232014-07-11 15:32:23 +03004834 * (it has NoInputNoOutput) then reject the confirmation
4835 * request. We check the security level here since it doesn't
4836 * necessarily match conn->auth_type.
Mikel Astiz6fd6b912014-04-08 14:21:32 +02004837 */
Johan Hedberg6c538232014-07-11 15:32:23 +03004838 if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
4839 conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
Johan Hedberg7a828902011-04-28 11:28:53 -07004840 BT_DBG("Rejecting request: remote device can't provide MITM");
4841 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004842 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07004843 goto unlock;
4844 }
4845
4846 /* If no side requires MITM protection; auto-accept */
Mikel Astiza7676312013-06-28 10:56:29 +02004847 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
4848 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004849
4850 /* If we're not the initiators request authorization to
4851 * proceed from user space (mgmt_user_confirm with
Johan Hedbergba15a582014-06-09 13:58:14 +03004852 * confirm_hint set to 1). The exception is if neither
Johan Hedberg02f3e252014-07-16 15:09:13 +03004853 * side had MITM or if the local IO capability is
4854 * NoInputNoOutput, in which case we do auto-accept
Johan Hedbergba15a582014-06-09 13:58:14 +03004855 */
4856 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
Johan Hedberg02f3e252014-07-16 15:09:13 +03004857 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedbergba15a582014-06-09 13:58:14 +03004858 (loc_mitm || rem_mitm)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004859 BT_DBG("Confirming auto-accept as acceptor");
4860 confirm_hint = 1;
4861 goto confirm;
4862 }
4863
Howard Chungcee5f202020-02-14 19:16:41 +08004864 /* If there already exists link key in local host, leave the
4865 * decision to user space since the remote device could be
4866 * legitimate or malicious.
4867 */
4868 if (hci_find_link_key(hdev, &ev->bdaddr)) {
4869 bt_dev_dbg(hdev, "Local host already has link key");
4870 confirm_hint = 1;
4871 goto confirm;
4872 }
4873
Johan Hedberg9f616562011-04-28 11:28:54 -07004874 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03004875 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07004876
4877 if (hdev->auto_accept_delay > 0) {
4878 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03004879 queue_delayed_work(conn->hdev->workqueue,
4880 &conn->auto_accept_work, delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07004881 goto unlock;
4882 }
4883
Johan Hedberg7a828902011-04-28 11:28:53 -07004884 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004885 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07004886 goto unlock;
4887 }
4888
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004889confirm:
Johan Hedberg39adbff2014-03-20 08:18:14 +02004890 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
4891 le32_to_cpu(ev->passkey), confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07004892
4893unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03004894 hci_dev_unlock(hdev);
4895}
4896
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004897static void hci_user_passkey_request_evt(struct hci_dev *hdev,
4898 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08004899{
4900 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
4901
4902 BT_DBG("%s", hdev->name);
4903
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004904 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg272d90d2012-02-09 15:26:12 +02004905 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08004906}
4907
Johan Hedberg92a25252012-09-06 18:39:26 +03004908static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
4909 struct sk_buff *skb)
4910{
4911 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
4912 struct hci_conn *conn;
4913
4914 BT_DBG("%s", hdev->name);
4915
4916 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4917 if (!conn)
4918 return;
4919
4920 conn->passkey_notify = __le32_to_cpu(ev->passkey);
4921 conn->passkey_entered = 0;
4922
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004923 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg92a25252012-09-06 18:39:26 +03004924 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4925 conn->dst_type, conn->passkey_notify,
4926 conn->passkey_entered);
4927}
4928
4929static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
4930{
4931 struct hci_ev_keypress_notify *ev = (void *) skb->data;
4932 struct hci_conn *conn;
4933
4934 BT_DBG("%s", hdev->name);
4935
4936 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4937 if (!conn)
4938 return;
4939
4940 switch (ev->type) {
4941 case HCI_KEYPRESS_STARTED:
4942 conn->passkey_entered = 0;
4943 return;
4944
4945 case HCI_KEYPRESS_ENTERED:
4946 conn->passkey_entered++;
4947 break;
4948
4949 case HCI_KEYPRESS_ERASED:
4950 conn->passkey_entered--;
4951 break;
4952
4953 case HCI_KEYPRESS_CLEARED:
4954 conn->passkey_entered = 0;
4955 break;
4956
4957 case HCI_KEYPRESS_COMPLETED:
4958 return;
4959 }
4960
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004961 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg92a25252012-09-06 18:39:26 +03004962 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4963 conn->dst_type, conn->passkey_notify,
4964 conn->passkey_entered);
4965}
4966
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004967static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
4968 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02004969{
4970 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
4971 struct hci_conn *conn;
4972
4973 BT_DBG("%s", hdev->name);
4974
4975 hci_dev_lock(hdev);
4976
4977 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03004978 if (!conn)
4979 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02004980
Johan Hedbergc1d4fa72014-07-17 15:14:50 +03004981 /* Reset the authentication requirement to unknown */
4982 conn->remote_auth = 0xff;
4983
Johan Hedberg2a611692011-02-19 12:06:00 -03004984 /* To avoid duplicate auth_failed events to user space we check
4985 * the HCI_CONN_AUTH_PEND flag which will be set if we
4986 * initiated the authentication. A traditional auth_complete
4987 * event gets always produced as initiator and is also mapped to
4988 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02004989 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedberge1e930f2014-09-08 17:09:49 -07004990 mgmt_auth_failed(conn, ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03004991
David Herrmann76a68ba2013-04-06 20:28:37 +02004992 hci_conn_drop(conn);
Johan Hedberg2a611692011-02-19 12:06:00 -03004993
4994unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02004995 hci_dev_unlock(hdev);
4996}
4997
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004998static void hci_remote_host_features_evt(struct hci_dev *hdev,
4999 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02005000{
5001 struct hci_ev_remote_host_features *ev = (void *) skb->data;
5002 struct inquiry_entry *ie;
Johan Hedbergcad718e2013-04-17 15:00:51 +03005003 struct hci_conn *conn;
Marcel Holtmann41a96212008-07-14 20:13:48 +02005004
5005 BT_DBG("%s", hdev->name);
5006
5007 hci_dev_lock(hdev);
5008
Johan Hedbergcad718e2013-04-17 15:00:51 +03005009 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5010 if (conn)
5011 memcpy(conn->features[1], ev->features, 8);
5012
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02005013 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
5014 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02005015 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02005016
5017 hci_dev_unlock(hdev);
5018}
5019
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005020static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
5021 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01005022{
5023 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
5024 struct oob_data *data;
5025
5026 BT_DBG("%s", hdev->name);
5027
5028 hci_dev_lock(hdev);
5029
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005030 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Szymon Jance1ba1f12011-04-06 13:01:59 +02005031 goto unlock;
5032
Johan Hedberg6928a922014-10-26 20:46:09 +01005033 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
Marcel Holtmann6665d052015-01-27 16:04:31 -08005034 if (!data) {
Szymon Janc2763eda2011-03-22 13:12:22 +01005035 struct hci_cp_remote_oob_data_neg_reply cp;
5036
5037 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08005038 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
5039 sizeof(cp), &cp);
Marcel Holtmann6665d052015-01-27 16:04:31 -08005040 goto unlock;
5041 }
5042
5043 if (bredr_sc_enabled(hdev)) {
5044 struct hci_cp_remote_oob_ext_data_reply cp;
5045
5046 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005047 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmann6665d052015-01-27 16:04:31 -08005048 memset(cp.hash192, 0, sizeof(cp.hash192));
5049 memset(cp.rand192, 0, sizeof(cp.rand192));
5050 } else {
5051 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
5052 memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
5053 }
5054 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
5055 memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
5056
5057 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
5058 sizeof(cp), &cp);
5059 } else {
5060 struct hci_cp_remote_oob_data_reply cp;
5061
5062 bacpy(&cp.bdaddr, &ev->bdaddr);
5063 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
5064 memcpy(cp.rand, data->rand192, sizeof(cp.rand));
5065
5066 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
5067 sizeof(cp), &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01005068 }
5069
Szymon Jance1ba1f12011-04-06 13:01:59 +02005070unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01005071 hci_dev_unlock(hdev);
5072}
5073
Arron Wanga77a6a12015-07-24 17:13:15 +08005074#if IS_ENABLED(CONFIG_BT_HS)
5075static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
5076{
5077 struct hci_ev_channel_selected *ev = (void *)skb->data;
5078 struct hci_conn *hcon;
5079
5080 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
5081
5082 skb_pull(skb, sizeof(*ev));
5083
5084 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5085 if (!hcon)
5086 return;
5087
5088 amp_read_loc_assoc_final_data(hdev, hcon);
5089}
5090
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005091static void hci_phy_link_complete_evt(struct hci_dev *hdev,
5092 struct sk_buff *skb)
5093{
5094 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
5095 struct hci_conn *hcon, *bredr_hcon;
5096
5097 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
5098 ev->status);
5099
5100 hci_dev_lock(hdev);
5101
5102 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005103 if (!hcon)
5104 goto unlock;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005105
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005106 if (!hcon->amp_mgr)
5107 goto unlock;
Anmol Karn6dfccd12020-09-30 19:48:13 +05305108
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005109 if (ev->status) {
5110 hci_conn_del(hcon);
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005111 goto unlock;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005112 }
5113
5114 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
5115
5116 hcon->state = BT_CONNECTED;
5117 bacpy(&hcon->dst, &bredr_hcon->dst);
5118
5119 hci_conn_hold(hcon);
5120 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02005121 hci_conn_drop(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005122
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01005123 hci_debugfs_create_conn(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005124 hci_conn_add_sysfs(hcon);
5125
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +02005126 amp_physical_cfm(bredr_hcon, hcon);
5127
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005128unlock:
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005129 hci_dev_unlock(hdev);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005130}
5131
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005132static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
5133{
5134 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
5135 struct hci_conn *hcon;
5136 struct hci_chan *hchan;
5137 struct amp_mgr *mgr;
5138
5139 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
5140 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
5141 ev->status);
5142
5143 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5144 if (!hcon)
5145 return;
5146
5147 /* Create AMP hchan */
5148 hchan = hci_chan_create(hcon);
5149 if (!hchan)
5150 return;
5151
5152 hchan->handle = le16_to_cpu(ev->handle);
Archie Pusaka5c4c8c92021-03-22 14:03:11 +08005153 hchan->amp = true;
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005154
5155 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
5156
5157 mgr = hcon->amp_mgr;
5158 if (mgr && mgr->bredr_chan) {
5159 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
5160
5161 l2cap_chan_lock(bredr_chan);
5162
5163 bredr_chan->conn->mtu = hdev->block_mtu;
5164 l2cap_logical_cfm(bredr_chan, hchan, 0);
5165 hci_conn_hold(hcon);
5166
5167 l2cap_chan_unlock(bredr_chan);
5168 }
5169}
5170
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005171static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
5172 struct sk_buff *skb)
5173{
5174 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
5175 struct hci_chan *hchan;
5176
5177 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
5178 le16_to_cpu(ev->handle), ev->status);
5179
5180 if (ev->status)
5181 return;
5182
5183 hci_dev_lock(hdev);
5184
5185 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
Archie Pusaka5c4c8c92021-03-22 14:03:11 +08005186 if (!hchan || !hchan->amp)
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005187 goto unlock;
5188
5189 amp_destroy_logical_link(hchan, ev->reason);
5190
5191unlock:
5192 hci_dev_unlock(hdev);
5193}
5194
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005195static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
5196 struct sk_buff *skb)
5197{
5198 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
5199 struct hci_conn *hcon;
5200
5201 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5202
5203 if (ev->status)
5204 return;
5205
5206 hci_dev_lock(hdev);
5207
5208 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5209 if (hcon) {
5210 hcon->state = BT_CLOSED;
5211 hci_conn_del(hcon);
5212 }
5213
5214 hci_dev_unlock(hdev);
5215}
Arron Wanga77a6a12015-07-24 17:13:15 +08005216#endif
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005217
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005218static void le_conn_update_addr(struct hci_conn *conn, bdaddr_t *bdaddr,
5219 u8 bdaddr_type, bdaddr_t *local_rpa)
5220{
5221 if (conn->out) {
5222 conn->dst_type = bdaddr_type;
5223 conn->resp_addr_type = bdaddr_type;
5224 bacpy(&conn->resp_addr, bdaddr);
5225
5226 /* Check if the controller has set a Local RPA then it must be
5227 * used instead or hdev->rpa.
5228 */
5229 if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5230 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5231 bacpy(&conn->init_addr, local_rpa);
5232 } else if (hci_dev_test_flag(conn->hdev, HCI_PRIVACY)) {
5233 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5234 bacpy(&conn->init_addr, &conn->hdev->rpa);
5235 } else {
5236 hci_copy_identity_address(conn->hdev, &conn->init_addr,
5237 &conn->init_addr_type);
5238 }
5239 } else {
5240 conn->resp_addr_type = conn->hdev->adv_addr_type;
5241 /* Check if the controller has set a Local RPA then it must be
5242 * used instead or hdev->rpa.
5243 */
5244 if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5245 conn->resp_addr_type = ADDR_LE_DEV_RANDOM;
5246 bacpy(&conn->resp_addr, local_rpa);
5247 } else if (conn->hdev->adv_addr_type == ADDR_LE_DEV_RANDOM) {
5248 /* In case of ext adv, resp_addr will be updated in
5249 * Adv Terminated event.
5250 */
5251 if (!ext_adv_capable(conn->hdev))
5252 bacpy(&conn->resp_addr,
5253 &conn->hdev->random_addr);
5254 } else {
5255 bacpy(&conn->resp_addr, &conn->hdev->bdaddr);
5256 }
5257
5258 conn->init_addr_type = bdaddr_type;
5259 bacpy(&conn->init_addr, bdaddr);
5260
5261 /* For incoming connections, set the default minimum
5262 * and maximum connection interval. They will be used
5263 * to check if the parameters are in range and if not
5264 * trigger the connection update procedure.
5265 */
5266 conn->le_conn_min_interval = conn->hdev->le_conn_min_interval;
5267 conn->le_conn_max_interval = conn->hdev->le_conn_max_interval;
5268 }
5269}
5270
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305271static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005272 bdaddr_t *bdaddr, u8 bdaddr_type,
5273 bdaddr_t *local_rpa, u8 role, u16 handle,
5274 u16 interval, u16 latency,
5275 u16 supervision_timeout)
Ville Tervofcd89c02011-02-10 22:38:47 -03005276{
Johan Hedberg912b42ef2014-07-03 19:33:49 +03005277 struct hci_conn_params *params;
Ville Tervofcd89c02011-02-10 22:38:47 -03005278 struct hci_conn *conn;
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02005279 struct smp_irk *irk;
Johan Hedberg837d5022014-07-02 09:36:22 +03005280 u8 addr_type;
Ville Tervofcd89c02011-02-10 22:38:47 -03005281
Ville Tervofcd89c02011-02-10 22:38:47 -03005282 hci_dev_lock(hdev);
5283
Johan Hedbergfbd96c12014-07-08 17:21:51 +03005284 /* All controllers implicitly stop advertising in the event of a
5285 * connection, so ensure that the state bit is cleared.
5286 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005287 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedbergfbd96c12014-07-08 17:21:51 +03005288
Jakub Pawlowskie7d9ab72015-08-07 20:22:52 +02005289 conn = hci_lookup_le_connect(hdev);
Ville Tervob62f3282011-02-10 22:38:50 -03005290 if (!conn) {
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305291 conn = hci_conn_add(hdev, LE_LINK, bdaddr, role);
Ville Tervob62f3282011-02-10 22:38:50 -03005292 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005293 bt_dev_err(hdev, "no memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03005294 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03005295 }
Andre Guedes29b79882011-05-31 14:20:54 -03005296
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305297 conn->dst_type = bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03005298
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005299 /* If we didn't have a hci_conn object previously
Archie Pusaka74be5232021-06-04 16:26:25 +08005300 * but we're in central role this must be something
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005301 * initiated using an accept list. Since accept list based
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005302 * connections are not "first class citizens" we don't
5303 * have full tracking of them. Therefore, we go ahead
5304 * with a "best effort" approach of determining the
5305 * initiator address based on the HCI_PRIVACY flag.
5306 */
5307 if (conn->out) {
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305308 conn->resp_addr_type = bdaddr_type;
5309 bacpy(&conn->resp_addr, bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005310 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005311 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5312 bacpy(&conn->init_addr, &hdev->rpa);
5313 } else {
5314 hci_copy_identity_address(hdev,
5315 &conn->init_addr,
5316 &conn->init_addr_type);
5317 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005318 }
Johan Hedberg9489eca2014-02-28 17:45:46 +02005319 } else {
5320 cancel_delayed_work(&conn->le_conn_timeout);
Ville Tervob62f3282011-02-10 22:38:50 -03005321 }
Ville Tervofcd89c02011-02-10 22:38:47 -03005322
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005323 le_conn_update_addr(conn, bdaddr, bdaddr_type, local_rpa);
Johan Hedberg7be2edb2014-02-23 19:42:17 +02005324
Marcel Holtmannedb4b462014-02-18 15:13:43 -08005325 /* Lookup the identity address from the stored connection
5326 * address and address type.
5327 *
5328 * When establishing connections to an identity address, the
5329 * connection procedure will store the resolvable random
5330 * address first. Now if it can be converted back into the
5331 * identity address, start using the identity address from
5332 * now on.
5333 */
5334 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02005335 if (irk) {
5336 bacpy(&conn->dst, &irk->bdaddr);
5337 conn->dst_type = irk->addr_type;
5338 }
5339
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005340 conn->dst_type = ev_bdaddr_type(hdev, conn->dst_type, NULL);
Sathish Narasimman79699a72021-05-20 17:12:01 +05305341
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305342 if (status) {
5343 hci_le_conn_failed(conn, status);
Johan Hedberg837d5022014-07-02 09:36:22 +03005344 goto unlock;
5345 }
5346
Johan Hedberg08853f12014-08-15 21:06:55 +03005347 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
5348 addr_type = BDADDR_LE_PUBLIC;
5349 else
5350 addr_type = BDADDR_LE_RANDOM;
5351
Johan Hedberg2d3c22602014-07-15 11:51:28 +03005352 /* Drop the connection if the device is blocked */
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005353 if (hci_bdaddr_list_lookup(&hdev->reject_list, &conn->dst, addr_type)) {
Johan Hedberg2d3c22602014-07-15 11:51:28 +03005354 hci_conn_drop(conn);
Andre Guedescd17dec2012-07-27 15:10:16 -03005355 goto unlock;
5356 }
5357
Johan Hedbergb644ba32012-01-17 21:48:47 +02005358 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07005359 mgmt_device_connected(hdev, conn, NULL, 0);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03005360
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03005361 conn->sec_level = BT_SECURITY_LOW;
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305362 conn->handle = handle;
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005363 conn->state = BT_CONFIG;
Ville Tervofcd89c02011-02-10 22:38:47 -03005364
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005365 /* Store current advertising instance as connection advertising instance
5366 * when sotfware rotation is in use so it can be re-enabled when
5367 * disconnected.
5368 */
5369 if (!ext_adv_capable(hdev))
5370 conn->adv_instance = hdev->cur_adv_instance;
5371
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305372 conn->le_conn_interval = interval;
5373 conn->le_conn_latency = latency;
5374 conn->le_supv_timeout = supervision_timeout;
Marcel Holtmanne04fde62014-06-23 11:40:04 +02005375
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01005376 hci_debugfs_create_conn(conn);
Ville Tervofcd89c02011-02-10 22:38:47 -03005377 hci_conn_add_sysfs(conn);
5378
Archie Pusakaef365da2021-05-31 16:37:23 +08005379 /* The remote features procedure is defined for central
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005380 * role only. So only in case of an initiated connection
5381 * request the remote features.
5382 *
Archie Pusakaef365da2021-05-31 16:37:23 +08005383 * If the local controller supports peripheral-initiated features
5384 * exchange, then requesting the remote features in peripheral
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005385 * role is possible. Otherwise just transition into the
5386 * connected state without requesting the remote features.
5387 */
5388 if (conn->out ||
Archie Pusakaef365da2021-05-31 16:37:23 +08005389 (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES)) {
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005390 struct hci_cp_le_read_remote_features cp;
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005391
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005392 cp.handle = __cpu_to_le16(conn->handle);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005393
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005394 hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
5395 sizeof(cp), &cp);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005396
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005397 hci_conn_hold(conn);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005398 } else {
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005399 conn->state = BT_CONNECTED;
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305400 hci_connect_cfm(conn, status);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005401 }
Ville Tervofcd89c02011-02-10 22:38:47 -03005402
Johan Hedberg54776102014-08-15 21:06:56 +03005403 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
5404 conn->dst_type);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005405 if (params) {
Johan Hedberg95305ba2014-07-04 12:37:21 +03005406 list_del_init(&params->action);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005407 if (params->conn) {
5408 hci_conn_drop(params->conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005409 hci_conn_put(params->conn);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005410 params->conn = NULL;
5411 }
5412 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005413
Ville Tervofcd89c02011-02-10 22:38:47 -03005414unlock:
Johan Hedberg223683a52014-07-06 15:44:23 +03005415 hci_update_background_scan(hdev);
Ville Tervofcd89c02011-02-10 22:38:47 -03005416 hci_dev_unlock(hdev);
5417}
5418
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305419static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
5420{
5421 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
5422
5423 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5424
5425 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005426 NULL, ev->role, le16_to_cpu(ev->handle),
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305427 le16_to_cpu(ev->interval),
5428 le16_to_cpu(ev->latency),
5429 le16_to_cpu(ev->supervision_timeout));
5430}
5431
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005432static void hci_le_enh_conn_complete_evt(struct hci_dev *hdev,
5433 struct sk_buff *skb)
5434{
5435 struct hci_ev_le_enh_conn_complete *ev = (void *) skb->data;
5436
5437 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5438
5439 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005440 &ev->local_rpa, ev->role, le16_to_cpu(ev->handle),
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005441 le16_to_cpu(ev->interval),
5442 le16_to_cpu(ev->latency),
5443 le16_to_cpu(ev->supervision_timeout));
Sathish Narasimman5c49bcc2020-07-23 18:09:01 +05305444
5445 if (use_ll_privacy(hdev) &&
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05305446 hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
Sathish Narasimman5c49bcc2020-07-23 18:09:01 +05305447 hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
5448 hci_req_disable_address_resolution(hdev);
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005449}
5450
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305451static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, struct sk_buff *skb)
5452{
5453 struct hci_evt_le_ext_adv_set_term *ev = (void *) skb->data;
5454 struct hci_conn *conn;
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005455 struct adv_info *adv;
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305456
5457 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5458
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005459 adv = hci_find_adv_instance(hdev, ev->handle);
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07005460
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005461 if (ev->status) {
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07005462 if (!adv)
5463 return;
5464
5465 /* Remove advertising as it has been terminated */
5466 hci_remove_adv_instance(hdev, ev->handle);
5467 mgmt_advertising_removed(NULL, hdev, ev->handle);
5468
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305469 return;
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07005470 }
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305471
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005472 if (adv)
5473 adv->enabled = false;
5474
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305475 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle));
5476 if (conn) {
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005477 /* Store handle in the connection so the correct advertising
5478 * instance can be re-enabled when disconnected.
5479 */
5480 conn->adv_instance = ev->handle;
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305481
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005482 if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM ||
5483 bacmp(&conn->resp_addr, BDADDR_ANY))
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305484 return;
5485
Daniel Winkler25e70882021-04-05 16:33:04 -07005486 if (!ev->handle) {
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305487 bacpy(&conn->resp_addr, &hdev->random_addr);
5488 return;
5489 }
5490
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07005491 if (adv)
5492 bacpy(&conn->resp_addr, &adv->random_addr);
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305493 }
5494}
5495
Marcel Holtmann1855d922014-06-23 11:40:05 +02005496static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
5497 struct sk_buff *skb)
5498{
5499 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
5500 struct hci_conn *conn;
5501
5502 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5503
5504 if (ev->status)
5505 return;
5506
5507 hci_dev_lock(hdev);
5508
5509 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5510 if (conn) {
5511 conn->le_conn_interval = le16_to_cpu(ev->interval);
5512 conn->le_conn_latency = le16_to_cpu(ev->latency);
5513 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
5514 }
5515
5516 hci_dev_unlock(hdev);
5517}
5518
Andre Guedesa4790db2014-02-26 20:21:47 -03005519/* This function requires the caller holds hdev->lock */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005520static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
5521 bdaddr_t *addr,
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005522 u8 addr_type, bool addr_resolved,
5523 u8 adv_type, bdaddr_t *direct_rpa)
Andre Guedesa4790db2014-02-26 20:21:47 -03005524{
5525 struct hci_conn *conn;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005526 struct hci_conn_params *params;
Andre Guedesa4790db2014-02-26 20:21:47 -03005527
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005528 /* If the event is not connectable don't proceed further */
5529 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005530 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005531
5532 /* Ignore if the device is blocked */
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005533 if (hci_bdaddr_list_lookup(&hdev->reject_list, addr, addr_type))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005534 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005535
Johan Hedbergf99353c2014-07-16 11:56:09 +03005536 /* Most controller will fail if we try to create new connections
Archie Pusaka39bc74c2021-06-04 16:26:26 +08005537 * while we have an existing one in peripheral role.
Johan Hedbergf99353c2014-07-16 11:56:09 +03005538 */
Archie Pusaka39bc74c2021-06-04 16:26:26 +08005539 if (hdev->conn_hash.le_num_peripheral > 0 &&
Alain Michaud4364f2e2020-04-23 14:43:29 +00005540 (!test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) ||
5541 !(hdev->le_states[3] & 0x10)))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005542 return NULL;
Johan Hedbergf99353c2014-07-16 11:56:09 +03005543
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005544 /* If we're not connectable only connect devices that we have in
5545 * our pend_le_conns list.
5546 */
Johan Hedberg49c50922015-10-16 10:07:51 +03005547 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
5548 addr_type);
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005549 if (!params)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005550 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005551
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005552 if (!params->explicit_connect) {
5553 switch (params->auto_connect) {
5554 case HCI_AUTO_CONN_DIRECT:
5555 /* Only devices advertising with ADV_DIRECT_IND are
5556 * triggering a connection attempt. This is allowing
Archie Pusaka67ffb182021-05-31 16:37:28 +08005557 * incoming connections from peripheral devices.
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005558 */
5559 if (adv_type != LE_ADV_DIRECT_IND)
5560 return NULL;
5561 break;
5562 case HCI_AUTO_CONN_ALWAYS:
5563 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
5564 * are triggering a connection attempt. This means
Archie Pusaka67ffb182021-05-31 16:37:28 +08005565 * that incoming connections from peripheral device are
5566 * accepted and also outgoing connections to peripheral
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005567 * devices are established when found.
5568 */
5569 break;
5570 default:
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005571 return NULL;
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005572 }
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005573 }
5574
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005575 conn = hci_connect_le(hdev, addr, addr_type, addr_resolved,
5576 BT_SECURITY_LOW, hdev->def_le_autoconnect_timeout,
5577 HCI_ROLE_MASTER, direct_rpa);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005578 if (!IS_ERR(conn)) {
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005579 /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
5580 * by higher layer that tried to connect, if no then
5581 * store the pointer since we don't really have any
Johan Hedbergf161dd42014-08-15 21:06:54 +03005582 * other owner of the object besides the params that
5583 * triggered it. This way we can abort the connection if
5584 * the parameters get removed and keep the reference
5585 * count consistent once the connection is established.
5586 */
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005587
5588 if (!params->explicit_connect)
5589 params->conn = hci_conn_get(conn);
5590
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005591 return conn;
Johan Hedbergf161dd42014-08-15 21:06:54 +03005592 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005593
5594 switch (PTR_ERR(conn)) {
5595 case -EBUSY:
5596 /* If hci_connect() returns -EBUSY it means there is already
5597 * an LE connection attempt going on. Since controllers don't
5598 * support more than one connection attempt at the time, we
5599 * don't consider this an error case.
5600 */
5601 break;
5602 default:
5603 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005604 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005605 }
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005606
5607 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005608}
5609
Johan Hedberg4af605d2014-03-24 10:48:00 +02005610static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005611 u8 bdaddr_type, bdaddr_t *direct_addr,
Alain Michauda2ec9052020-07-27 20:48:55 +00005612 u8 direct_addr_type, s8 rssi, u8 *data, u8 len,
5613 bool ext_adv)
Johan Hedberg4af605d2014-03-24 10:48:00 +02005614{
Johan Hedbergb9a63282014-03-25 10:51:52 +02005615 struct discovery_state *d = &hdev->discovery;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005616 struct smp_irk *irk;
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005617 struct hci_conn *conn;
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005618 bool match, bdaddr_resolved;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005619 u32 flags;
Szymon Janc1c58e932021-05-18 16:54:36 +02005620 u8 *ptr;
Szymon Janc68183752015-09-16 20:21:54 +02005621
Johan Hedberg56b40fb2016-04-07 21:01:27 +03005622 switch (type) {
5623 case LE_ADV_IND:
5624 case LE_ADV_DIRECT_IND:
5625 case LE_ADV_SCAN_IND:
5626 case LE_ADV_NONCONN_IND:
5627 case LE_ADV_SCAN_RSP:
5628 break;
5629 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005630 bt_dev_err_ratelimited(hdev, "unknown advertising packet "
5631 "type: 0x%02x", type);
Johan Hedberg56b40fb2016-04-07 21:01:27 +03005632 return;
5633 }
5634
Alain Michauda2ec9052020-07-27 20:48:55 +00005635 if (!ext_adv && len > HCI_MAX_AD_LENGTH) {
5636 bt_dev_err_ratelimited(hdev, "legacy adv larger than 31 bytes");
5637 return;
5638 }
5639
Szymon Janc68183752015-09-16 20:21:54 +02005640 /* Find the end of the data in case the report contains padded zero
5641 * bytes at the end causing an invalid length value.
5642 *
5643 * When data is NULL, len is 0 so there is no need for extra ptr
5644 * check as 'ptr < data + 0' is already false in such case.
5645 */
5646 for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
5647 if (ptr + 1 + *ptr > data + len)
5648 break;
5649 }
5650
Szymon Janc1c58e932021-05-18 16:54:36 +02005651 /* Adjust for actual length. This handles the case when remote
5652 * device is advertising with incorrect data length.
5653 */
5654 len = ptr - data;
Johan Hedbergb9a63282014-03-25 10:51:52 +02005655
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005656 /* If the direct address is present, then this report is from
5657 * a LE Direct Advertising Report event. In that case it is
5658 * important to see if the address is matching the local
5659 * controller address.
5660 */
5661 if (direct_addr) {
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005662 direct_addr_type = ev_bdaddr_type(hdev, direct_addr_type,
5663 &bdaddr_resolved);
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07005664
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005665 /* Only resolvable random addresses are valid for these
5666 * kind of reports and others can be ignored.
5667 */
5668 if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
5669 return;
5670
5671 /* If the controller is not using resolvable random
5672 * addresses, then this report can be ignored.
5673 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005674 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005675 return;
5676
5677 /* If the local IRK of the controller does not match
5678 * with the resolvable random address provided, then
5679 * this report can be ignored.
5680 */
5681 if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
5682 return;
5683 }
5684
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005685 /* Check if we need to convert to identity address */
5686 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
5687 if (irk) {
5688 bdaddr = &irk->bdaddr;
5689 bdaddr_type = irk->addr_type;
5690 }
5691
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005692 bdaddr_type = ev_bdaddr_type(hdev, bdaddr_type, &bdaddr_resolved);
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07005693
Szymon Janc082f2302018-04-03 13:40:06 +02005694 /* Check if we have been requested to connect to this device.
5695 *
5696 * direct_addr is set only for directed advertising reports (it is NULL
5697 * for advertising reports) and is already verified to be RPA above.
5698 */
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07005699 conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, bdaddr_resolved,
5700 type, direct_addr);
Alain Michauda2ec9052020-07-27 20:48:55 +00005701 if (!ext_adv && conn && type == LE_ADV_IND && len <= HCI_MAX_AD_LENGTH) {
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005702 /* Store report for later inclusion by
5703 * mgmt_device_connected
5704 */
5705 memcpy(conn->le_adv_data, data, len);
5706 conn->le_adv_data_len = len;
5707 }
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005708
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005709 /* Passive scanning shouldn't trigger any device found events,
5710 * except for devices marked as CONN_REPORT for which we do send
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02005711 * device found events, or advertisement monitoring requested.
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005712 */
Johan Hedbergca5c4be2014-03-25 10:30:46 +02005713 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005714 if (type == LE_ADV_DIRECT_IND)
5715 return;
5716
Johan Hedberg3a19b6f2014-07-15 08:07:59 +03005717 if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02005718 bdaddr, bdaddr_type) &&
5719 idr_is_empty(&hdev->adv_monitors_idr))
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005720 return;
5721
5722 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
5723 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
5724 else
5725 flags = 0;
5726 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
5727 rssi, flags, data, len, NULL, 0);
Johan Hedberg97bf2e92014-07-04 12:37:16 +03005728 return;
Johan Hedbergca5c4be2014-03-25 10:30:46 +02005729 }
Johan Hedberg4af605d2014-03-24 10:48:00 +02005730
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005731 /* When receiving non-connectable or scannable undirected
5732 * advertising reports, this means that the remote device is
5733 * not connectable and then clearly indicate this in the
5734 * device found event.
5735 *
5736 * When receiving a scan response, then there is no way to
5737 * know if the remote device is connectable or not. However
5738 * since scan responses are merged with a previously seen
5739 * advertising report, the flags field from that report
5740 * will be used.
5741 *
5742 * In the really unlikely case that a controller get confused
5743 * and just sends a scan response event, then it is marked as
5744 * not connectable as well.
5745 */
5746 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
5747 type == LE_ADV_SCAN_RSP)
5748 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
5749 else
5750 flags = 0;
5751
Johan Hedbergb9a63282014-03-25 10:51:52 +02005752 /* If there's nothing pending either store the data from this
5753 * event or send an immediate device found event if the data
5754 * should not be stored for later.
5755 */
Alain Michauda2ec9052020-07-27 20:48:55 +00005756 if (!ext_adv && !has_pending_adv_report(hdev)) {
Johan Hedbergb9a63282014-03-25 10:51:52 +02005757 /* If the report will trigger a SCAN_REQ store it for
5758 * later merging.
5759 */
5760 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
5761 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005762 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005763 return;
5764 }
5765
5766 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005767 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005768 return;
5769 }
5770
Johan Hedberg474ee062014-03-25 14:34:59 +02005771 /* Check if the pending report is for the same device as the new one */
5772 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
5773 bdaddr_type == d->last_adv_addr_type);
5774
Johan Hedbergb9a63282014-03-25 10:51:52 +02005775 /* If the pending data doesn't match this report or this isn't a
5776 * scan response (e.g. we got a duplicate ADV_IND) then force
5777 * sending of the pending data.
5778 */
Johan Hedberg474ee062014-03-25 14:34:59 +02005779 if (type != LE_ADV_SCAN_RSP || !match) {
5780 /* Send out whatever is in the cache, but skip duplicates */
5781 if (!match)
5782 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergff5cd292014-03-25 14:40:52 +02005783 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005784 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergff5cd292014-03-25 14:40:52 +02005785 d->last_adv_data,
Johan Hedberg474ee062014-03-25 14:34:59 +02005786 d->last_adv_data_len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005787
5788 /* If the new report will trigger a SCAN_REQ store it for
5789 * later merging.
5790 */
Alain Michauda2ec9052020-07-27 20:48:55 +00005791 if (!ext_adv && (type == LE_ADV_IND ||
5792 type == LE_ADV_SCAN_IND)) {
Johan Hedbergb9a63282014-03-25 10:51:52 +02005793 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005794 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005795 return;
5796 }
5797
5798 /* The advertising reports cannot be merged, so clear
5799 * the pending report and send out a device found event.
5800 */
5801 clear_pending_adv_report(hdev);
Johan Hedberg5c5b93e2014-03-29 08:39:53 +02005802 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005803 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005804 return;
5805 }
5806
5807 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
5808 * the new event is a SCAN_RSP. We can therefore proceed with
5809 * sending a merged device found event.
5810 */
5811 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005812 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
Marcel Holtmann42bd6a52014-07-01 14:11:19 +02005813 d->last_adv_data, d->last_adv_data_len, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005814 clear_pending_adv_report(hdev);
Johan Hedberg4af605d2014-03-24 10:48:00 +02005815}
5816
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005817static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03005818{
Andre Guedese95beb42011-09-26 20:48:35 -03005819 u8 num_reports = skb->data[0];
5820 void *ptr = &skb->data[1];
Andre Guedes9aa04c92011-05-26 16:23:51 -03005821
Andre Guedesa4790db2014-02-26 20:21:47 -03005822 hci_dev_lock(hdev);
5823
Andre Guedese95beb42011-09-26 20:48:35 -03005824 while (num_reports--) {
5825 struct hci_ev_le_advertising_info *ev = ptr;
Johan Hedberg4af605d2014-03-24 10:48:00 +02005826 s8 rssi;
Andre Guedesa4790db2014-02-26 20:21:47 -03005827
Chriz Chowee649342018-04-20 15:46:24 +08005828 if (ev->length <= HCI_MAX_AD_LENGTH) {
5829 rssi = ev->data[ev->length];
5830 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
5831 ev->bdaddr_type, NULL, 0, rssi,
Alain Michauda2ec9052020-07-27 20:48:55 +00005832 ev->data, ev->length, false);
Chriz Chowee649342018-04-20 15:46:24 +08005833 } else {
5834 bt_dev_err(hdev, "Dropping invalid advertising data");
5835 }
Andre Guedes3c9e9192012-01-10 18:20:50 -03005836
Andre Guedese95beb42011-09-26 20:48:35 -03005837 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03005838 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005839
5840 hci_dev_unlock(hdev);
Andre Guedes9aa04c92011-05-26 16:23:51 -03005841}
5842
Marcel Holtmann657cc642019-12-11 11:34:36 +01005843static u8 ext_evt_type_to_legacy(struct hci_dev *hdev, u16 evt_type)
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305844{
Jaganath Kanakkasseryb2cc9762018-07-19 17:09:38 +05305845 if (evt_type & LE_EXT_ADV_LEGACY_PDU) {
5846 switch (evt_type) {
5847 case LE_LEGACY_ADV_IND:
5848 return LE_ADV_IND;
5849 case LE_LEGACY_ADV_DIRECT_IND:
5850 return LE_ADV_DIRECT_IND;
5851 case LE_LEGACY_ADV_SCAN_IND:
5852 return LE_ADV_SCAN_IND;
5853 case LE_LEGACY_NONCONN_IND:
5854 return LE_ADV_NONCONN_IND;
5855 case LE_LEGACY_SCAN_RSP_ADV:
5856 case LE_LEGACY_SCAN_RSP_ADV_SCAN:
5857 return LE_ADV_SCAN_RSP;
5858 }
5859
Marcel Holtmann657cc642019-12-11 11:34:36 +01005860 goto invalid;
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305861 }
5862
Jaganath Kanakkasseryb2cc9762018-07-19 17:09:38 +05305863 if (evt_type & LE_EXT_ADV_CONN_IND) {
5864 if (evt_type & LE_EXT_ADV_DIRECT_IND)
5865 return LE_ADV_DIRECT_IND;
5866
5867 return LE_ADV_IND;
5868 }
5869
5870 if (evt_type & LE_EXT_ADV_SCAN_RSP)
5871 return LE_ADV_SCAN_RSP;
5872
5873 if (evt_type & LE_EXT_ADV_SCAN_IND)
5874 return LE_ADV_SCAN_IND;
5875
5876 if (evt_type == LE_EXT_ADV_NON_CONN_IND ||
5877 evt_type & LE_EXT_ADV_DIRECT_IND)
5878 return LE_ADV_NONCONN_IND;
5879
Marcel Holtmann657cc642019-12-11 11:34:36 +01005880invalid:
5881 bt_dev_err_ratelimited(hdev, "Unknown advertising packet type: 0x%02x",
5882 evt_type);
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305883
5884 return LE_ADV_INVALID;
5885}
5886
5887static void hci_le_ext_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
5888{
5889 u8 num_reports = skb->data[0];
5890 void *ptr = &skb->data[1];
5891
5892 hci_dev_lock(hdev);
5893
5894 while (num_reports--) {
5895 struct hci_ev_le_ext_adv_report *ev = ptr;
5896 u8 legacy_evt_type;
5897 u16 evt_type;
5898
5899 evt_type = __le16_to_cpu(ev->evt_type);
Marcel Holtmann657cc642019-12-11 11:34:36 +01005900 legacy_evt_type = ext_evt_type_to_legacy(hdev, evt_type);
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305901 if (legacy_evt_type != LE_ADV_INVALID) {
5902 process_adv_report(hdev, legacy_evt_type, &ev->bdaddr,
5903 ev->bdaddr_type, NULL, 0, ev->rssi,
Alain Michauda2ec9052020-07-27 20:48:55 +00005904 ev->data, ev->length,
5905 !(evt_type & LE_EXT_ADV_LEGACY_PDU));
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305906 }
5907
Jaganath Kanakkasserycd9151b2019-04-03 12:11:44 +05305908 ptr += sizeof(*ev) + ev->length;
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305909 }
5910
5911 hci_dev_unlock(hdev);
5912}
5913
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005914static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev,
5915 struct sk_buff *skb)
5916{
5917 struct hci_ev_le_remote_feat_complete *ev = (void *)skb->data;
5918 struct hci_conn *conn;
5919
5920 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5921
5922 hci_dev_lock(hdev);
5923
5924 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5925 if (conn) {
5926 if (!ev->status)
5927 memcpy(conn->features[0], ev->features, 8);
5928
5929 if (conn->state == BT_CONFIG) {
5930 __u8 status;
5931
Archie Pusakaef365da2021-05-31 16:37:23 +08005932 /* If the local controller supports peripheral-initiated
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005933 * features exchange, but the remote controller does
5934 * not, then it is possible that the error code 0x1a
5935 * for unsupported remote feature gets returned.
5936 *
5937 * In this specific case, allow the connection to
5938 * transition into connected state and mark it as
5939 * successful.
5940 */
Archie Pusakaef365da2021-05-31 16:37:23 +08005941 if (!conn->out && ev->status == 0x1a &&
5942 (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES))
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005943 status = 0x00;
5944 else
5945 status = ev->status;
5946
5947 conn->state = BT_CONNECTED;
5948 hci_connect_cfm(conn, status);
5949 hci_conn_drop(conn);
5950 }
5951 }
5952
5953 hci_dev_unlock(hdev);
5954}
5955
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005956static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005957{
5958 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
5959 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005960 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005961 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03005962 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005963
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03005964 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005965
5966 hci_dev_lock(hdev);
5967
5968 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005969 if (conn == NULL)
5970 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005971
Johan Hedbergf3a73d92014-05-29 15:02:59 +03005972 ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
Johan Hedberg5378bc52014-05-29 14:00:39 +03005973 if (!ltk)
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005974 goto not_found;
5975
Johan Hedberg5378bc52014-05-29 14:00:39 +03005976 if (smp_ltk_is_sc(ltk)) {
5977 /* With SC both EDiv and Rand are set to zero */
5978 if (ev->ediv || ev->rand)
5979 goto not_found;
5980 } else {
5981 /* For non-SC keys check that EDiv and Rand match */
5982 if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
5983 goto not_found;
5984 }
5985
Johan Hedberg8b76ce32015-06-08 18:14:39 +03005986 memcpy(cp.ltk, ltk->val, ltk->enc_size);
5987 memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005988 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03005989
Johan Hedberga6f78332014-09-10 17:37:45 -07005990 conn->pending_sec_level = smp_ltk_sec_level(ltk);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005991
Andre Guedes89cbb4d2013-07-31 16:25:29 -03005992 conn->enc_key_size = ltk->enc_size;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005993
5994 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
5995
Claudio Takahasi5981a882013-07-25 16:34:24 -03005996 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
5997 * temporary key used to encrypt a connection following
5998 * pairing. It is used during the Encrypted Session Setup to
5999 * distribute the keys. Later, security can be re-established
6000 * using a distributed LTK.
6001 */
Johan Hedberg2ceba532014-06-16 19:25:16 +03006002 if (ltk->type == SMP_STK) {
Johan Hedbergfe59a052014-07-01 19:14:12 +03006003 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Johan Hedberg970d0f12014-11-13 14:37:47 +02006004 list_del_rcu(&ltk->list);
6005 kfree_rcu(ltk, rcu);
Johan Hedbergfe59a052014-07-01 19:14:12 +03006006 } else {
6007 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03006008 }
6009
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006010 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03006011
6012 return;
6013
6014not_found:
6015 neg.handle = ev->handle;
6016 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
6017 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006018}
6019
Andre Guedes8e75b462014-07-01 18:10:08 -03006020static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
6021 u8 reason)
6022{
6023 struct hci_cp_le_conn_param_req_neg_reply cp;
6024
6025 cp.handle = cpu_to_le16(handle);
6026 cp.reason = reason;
6027
6028 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
6029 &cp);
6030}
6031
6032static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
6033 struct sk_buff *skb)
6034{
6035 struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
6036 struct hci_cp_le_conn_param_req_reply cp;
6037 struct hci_conn *hcon;
6038 u16 handle, min, max, latency, timeout;
6039
6040 handle = le16_to_cpu(ev->handle);
6041 min = le16_to_cpu(ev->interval_min);
6042 max = le16_to_cpu(ev->interval_max);
6043 latency = le16_to_cpu(ev->latency);
6044 timeout = le16_to_cpu(ev->timeout);
6045
6046 hcon = hci_conn_hash_lookup_handle(hdev, handle);
6047 if (!hcon || hcon->state != BT_CONNECTED)
6048 return send_conn_param_neg_reply(hdev, handle,
6049 HCI_ERROR_UNKNOWN_CONN_ID);
6050
6051 if (hci_check_conn_params(min, max, latency, timeout))
6052 return send_conn_param_neg_reply(hdev, handle,
6053 HCI_ERROR_INVALID_LL_PARAMS);
6054
Johan Hedberg40bef302014-07-16 11:42:27 +03006055 if (hcon->role == HCI_ROLE_MASTER) {
Johan Hedberg348d50b2014-07-02 17:37:30 +03006056 struct hci_conn_params *params;
Johan Hedbergf4869e22014-07-02 17:37:32 +03006057 u8 store_hint;
Johan Hedberg348d50b2014-07-02 17:37:30 +03006058
6059 hci_dev_lock(hdev);
6060
6061 params = hci_conn_params_lookup(hdev, &hcon->dst,
6062 hcon->dst_type);
6063 if (params) {
6064 params->conn_min_interval = min;
6065 params->conn_max_interval = max;
6066 params->conn_latency = latency;
6067 params->supervision_timeout = timeout;
Johan Hedbergf4869e22014-07-02 17:37:32 +03006068 store_hint = 0x01;
Meng Yu149b3f12021-04-01 14:50:39 +08006069 } else {
Johan Hedbergf4869e22014-07-02 17:37:32 +03006070 store_hint = 0x00;
Johan Hedberg348d50b2014-07-02 17:37:30 +03006071 }
6072
6073 hci_dev_unlock(hdev);
6074
Johan Hedbergf4869e22014-07-02 17:37:32 +03006075 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
6076 store_hint, min, max, latency, timeout);
Johan Hedberg348d50b2014-07-02 17:37:30 +03006077 }
Andre Guedesffb5a8272014-07-01 18:10:11 -03006078
Andre Guedes8e75b462014-07-01 18:10:08 -03006079 cp.handle = ev->handle;
6080 cp.interval_min = ev->interval_min;
6081 cp.interval_max = ev->interval_max;
6082 cp.latency = ev->latency;
6083 cp.timeout = ev->timeout;
6084 cp.min_ce_len = 0;
6085 cp.max_ce_len = 0;
6086
6087 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
6088}
6089
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006090static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
6091 struct sk_buff *skb)
6092{
6093 u8 num_reports = skb->data[0];
Peilin Yef7e0e8b2020-09-09 03:17:00 -04006094 struct hci_ev_le_direct_adv_info *ev = (void *)&skb->data[1];
6095
6096 if (!num_reports || skb->len < num_reports * sizeof(*ev) + 1)
6097 return;
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006098
6099 hci_dev_lock(hdev);
6100
Peilin Yef7e0e8b2020-09-09 03:17:00 -04006101 for (; num_reports; num_reports--, ev++)
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006102 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
6103 ev->bdaddr_type, &ev->direct_addr,
Alain Michauda2ec9052020-07-27 20:48:55 +00006104 ev->direct_addr_type, ev->rssi, NULL, 0,
6105 false);
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006106
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006107 hci_dev_unlock(hdev);
6108}
6109
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006110static void hci_le_phy_update_evt(struct hci_dev *hdev, struct sk_buff *skb)
6111{
6112 struct hci_ev_le_phy_update_complete *ev = (void *) skb->data;
6113 struct hci_conn *conn;
6114
6115 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
6116
Ayush Garg87df8bc2021-03-17 16:52:14 +05306117 if (ev->status)
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006118 return;
6119
6120 hci_dev_lock(hdev);
6121
6122 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6123 if (!conn)
6124 goto unlock;
6125
6126 conn->le_tx_phy = ev->tx_phy;
6127 conn->le_rx_phy = ev->rx_phy;
6128
6129unlock:
6130 hci_dev_unlock(hdev);
6131}
6132
Gustavo Padovan6039aa72012-05-23 04:04:18 -03006133static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03006134{
6135 struct hci_ev_le_meta *le_ev = (void *) skb->data;
6136
6137 skb_pull(skb, sizeof(*le_ev));
6138
6139 switch (le_ev->subevent) {
6140 case HCI_EV_LE_CONN_COMPLETE:
6141 hci_le_conn_complete_evt(hdev, skb);
6142 break;
6143
Marcel Holtmann1855d922014-06-23 11:40:05 +02006144 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
6145 hci_le_conn_update_complete_evt(hdev, skb);
6146 break;
6147
Andre Guedes9aa04c92011-05-26 16:23:51 -03006148 case HCI_EV_LE_ADVERTISING_REPORT:
6149 hci_le_adv_report_evt(hdev, skb);
6150 break;
6151
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006152 case HCI_EV_LE_REMOTE_FEAT_COMPLETE:
6153 hci_le_remote_feat_complete_evt(hdev, skb);
6154 break;
6155
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006156 case HCI_EV_LE_LTK_REQ:
6157 hci_le_ltk_request_evt(hdev, skb);
6158 break;
6159
Andre Guedes8e75b462014-07-01 18:10:08 -03006160 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
6161 hci_le_remote_conn_param_req_evt(hdev, skb);
6162 break;
6163
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006164 case HCI_EV_LE_DIRECT_ADV_REPORT:
6165 hci_le_direct_adv_report_evt(hdev, skb);
6166 break;
6167
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006168 case HCI_EV_LE_PHY_UPDATE_COMPLETE:
6169 hci_le_phy_update_evt(hdev, skb);
6170 break;
6171
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05306172 case HCI_EV_LE_EXT_ADV_REPORT:
6173 hci_le_ext_adv_report_evt(hdev, skb);
6174 break;
6175
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02006176 case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
6177 hci_le_enh_conn_complete_evt(hdev, skb);
6178 break;
6179
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306180 case HCI_EV_LE_EXT_ADV_SET_TERM:
6181 hci_le_ext_adv_term_evt(hdev, skb);
6182 break;
6183
Ville Tervofcd89c02011-02-10 22:38:47 -03006184 default:
6185 break;
6186 }
6187}
6188
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006189static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
6190 u8 event, struct sk_buff *skb)
6191{
6192 struct hci_ev_cmd_complete *ev;
6193 struct hci_event_hdr *hdr;
6194
6195 if (!skb)
6196 return false;
6197
6198 if (skb->len < sizeof(*hdr)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006199 bt_dev_err(hdev, "too short HCI event");
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006200 return false;
6201 }
6202
6203 hdr = (void *) skb->data;
6204 skb_pull(skb, HCI_EVENT_HDR_SIZE);
6205
6206 if (event) {
6207 if (hdr->evt != event)
6208 return false;
6209 return true;
6210 }
6211
Zheng Yongjun91641b72021-06-02 14:54:58 +08006212 /* Check if request ended in Command Status - no way to retrieve
Johan Hedberg1629db9c2018-11-27 11:37:46 +02006213 * any extra parameters in this case.
6214 */
6215 if (hdr->evt == HCI_EV_CMD_STATUS)
6216 return false;
6217
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006218 if (hdr->evt != HCI_EV_CMD_COMPLETE) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006219 bt_dev_err(hdev, "last event is not cmd complete (0x%2.2x)",
6220 hdr->evt);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006221 return false;
6222 }
6223
6224 if (skb->len < sizeof(*ev)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006225 bt_dev_err(hdev, "too short cmd_complete event");
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006226 return false;
6227 }
6228
6229 ev = (void *) skb->data;
6230 skb_pull(skb, sizeof(*ev));
6231
6232 if (opcode != __le16_to_cpu(ev->opcode)) {
6233 BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
6234 __le16_to_cpu(ev->opcode));
6235 return false;
6236 }
6237
6238 return true;
6239}
6240
Abhishek Pandit-Subedi2f202162020-09-11 14:07:13 -07006241static void hci_store_wake_reason(struct hci_dev *hdev, u8 event,
6242 struct sk_buff *skb)
6243{
6244 struct hci_ev_le_advertising_info *adv;
6245 struct hci_ev_le_direct_adv_info *direct_adv;
6246 struct hci_ev_le_ext_adv_report *ext_adv;
6247 const struct hci_ev_conn_complete *conn_complete = (void *)skb->data;
6248 const struct hci_ev_conn_request *conn_request = (void *)skb->data;
6249
6250 hci_dev_lock(hdev);
6251
6252 /* If we are currently suspended and this is the first BT event seen,
6253 * save the wake reason associated with the event.
6254 */
6255 if (!hdev->suspended || hdev->wake_reason)
6256 goto unlock;
6257
6258 /* Default to remote wake. Values for wake_reason are documented in the
6259 * Bluez mgmt api docs.
6260 */
6261 hdev->wake_reason = MGMT_WAKE_REASON_REMOTE_WAKE;
6262
6263 /* Once configured for remote wakeup, we should only wake up for
6264 * reconnections. It's useful to see which device is waking us up so
6265 * keep track of the bdaddr of the connection event that woke us up.
6266 */
6267 if (event == HCI_EV_CONN_REQUEST) {
6268 bacpy(&hdev->wake_addr, &conn_complete->bdaddr);
6269 hdev->wake_addr_type = BDADDR_BREDR;
6270 } else if (event == HCI_EV_CONN_COMPLETE) {
6271 bacpy(&hdev->wake_addr, &conn_request->bdaddr);
6272 hdev->wake_addr_type = BDADDR_BREDR;
6273 } else if (event == HCI_EV_LE_META) {
6274 struct hci_ev_le_meta *le_ev = (void *)skb->data;
6275 u8 subevent = le_ev->subevent;
6276 u8 *ptr = &skb->data[sizeof(*le_ev)];
6277 u8 num_reports = *ptr;
6278
6279 if ((subevent == HCI_EV_LE_ADVERTISING_REPORT ||
6280 subevent == HCI_EV_LE_DIRECT_ADV_REPORT ||
6281 subevent == HCI_EV_LE_EXT_ADV_REPORT) &&
6282 num_reports) {
6283 adv = (void *)(ptr + 1);
6284 direct_adv = (void *)(ptr + 1);
6285 ext_adv = (void *)(ptr + 1);
6286
6287 switch (subevent) {
6288 case HCI_EV_LE_ADVERTISING_REPORT:
6289 bacpy(&hdev->wake_addr, &adv->bdaddr);
6290 hdev->wake_addr_type = adv->bdaddr_type;
6291 break;
6292 case HCI_EV_LE_DIRECT_ADV_REPORT:
6293 bacpy(&hdev->wake_addr, &direct_adv->bdaddr);
6294 hdev->wake_addr_type = direct_adv->bdaddr_type;
6295 break;
6296 case HCI_EV_LE_EXT_ADV_REPORT:
6297 bacpy(&hdev->wake_addr, &ext_adv->bdaddr);
6298 hdev->wake_addr_type = ext_adv->bdaddr_type;
6299 break;
6300 }
6301 }
6302 } else {
6303 hdev->wake_reason = MGMT_WAKE_REASON_UNEXPECTED;
6304 }
6305
6306unlock:
6307 hci_dev_unlock(hdev);
6308}
6309
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
6311{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006312 struct hci_event_hdr *hdr = (void *) skb->data;
Johan Hedberge62144872015-04-02 13:41:08 +03006313 hci_req_complete_t req_complete = NULL;
6314 hci_req_complete_skb_t req_complete_skb = NULL;
6315 struct sk_buff *orig_skb = NULL;
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006316 u8 status = 0, event = hdr->evt, req_evt = 0;
Johan Hedberge62144872015-04-02 13:41:08 +03006317 u16 opcode = HCI_OP_NOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006318
Alain Michaud08bb4da2020-03-03 15:55:34 +00006319 if (!event) {
6320 bt_dev_warn(hdev, "Received unexpected HCI Event 00000000");
6321 goto done;
6322 }
6323
Marcel Holtmann242c0eb2015-10-25 22:45:53 +01006324 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->hci.req_event == event) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02006325 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
Johan Hedberge62144872015-04-02 13:41:08 +03006326 opcode = __le16_to_cpu(cmd_hdr->opcode);
6327 hci_req_cmd_complete(hdev, opcode, status, &req_complete,
6328 &req_complete_skb);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006329 req_evt = event;
Johan Hedberg02350a72013-04-03 21:50:29 +03006330 }
6331
Johan Hedberge62144872015-04-02 13:41:08 +03006332 /* If it looks like we might end up having to call
6333 * req_complete_skb, store a pristine copy of the skb since the
6334 * various handlers may modify the original one through
6335 * skb_pull() calls, etc.
6336 */
6337 if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
6338 event == HCI_EV_CMD_COMPLETE)
6339 orig_skb = skb_clone(skb, GFP_KERNEL);
6340
6341 skb_pull(skb, HCI_EVENT_HDR_SIZE);
6342
Abhishek Pandit-Subedi2f202162020-09-11 14:07:13 -07006343 /* Store wake reason if we're suspended */
6344 hci_store_wake_reason(hdev, event, skb);
6345
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006346 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006347 case HCI_EV_INQUIRY_COMPLETE:
6348 hci_inquiry_complete_evt(hdev, skb);
6349 break;
6350
6351 case HCI_EV_INQUIRY_RESULT:
6352 hci_inquiry_result_evt(hdev, skb);
6353 break;
6354
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006355 case HCI_EV_CONN_COMPLETE:
6356 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02006357 break;
6358
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 case HCI_EV_CONN_REQUEST:
6360 hci_conn_request_evt(hdev, skb);
6361 break;
6362
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 case HCI_EV_DISCONN_COMPLETE:
6364 hci_disconn_complete_evt(hdev, skb);
6365 break;
6366
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 case HCI_EV_AUTH_COMPLETE:
6368 hci_auth_complete_evt(hdev, skb);
6369 break;
6370
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006371 case HCI_EV_REMOTE_NAME:
6372 hci_remote_name_evt(hdev, skb);
6373 break;
6374
Linus Torvalds1da177e2005-04-16 15:20:36 -07006375 case HCI_EV_ENCRYPT_CHANGE:
6376 hci_encrypt_change_evt(hdev, skb);
6377 break;
6378
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006379 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
6380 hci_change_link_key_complete_evt(hdev, skb);
6381 break;
6382
6383 case HCI_EV_REMOTE_FEATURES:
6384 hci_remote_features_evt(hdev, skb);
6385 break;
6386
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006387 case HCI_EV_CMD_COMPLETE:
Johan Hedberge62144872015-04-02 13:41:08 +03006388 hci_cmd_complete_evt(hdev, skb, &opcode, &status,
6389 &req_complete, &req_complete_skb);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006390 break;
6391
6392 case HCI_EV_CMD_STATUS:
Johan Hedberge62144872015-04-02 13:41:08 +03006393 hci_cmd_status_evt(hdev, skb, &opcode, &status, &req_complete,
6394 &req_complete_skb);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006395 break;
6396
Marcel Holtmann24dfa342014-11-02 02:56:41 +01006397 case HCI_EV_HARDWARE_ERROR:
6398 hci_hardware_error_evt(hdev, skb);
6399 break;
6400
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006401 case HCI_EV_ROLE_CHANGE:
6402 hci_role_change_evt(hdev, skb);
6403 break;
6404
6405 case HCI_EV_NUM_COMP_PKTS:
6406 hci_num_comp_pkts_evt(hdev, skb);
6407 break;
6408
6409 case HCI_EV_MODE_CHANGE:
6410 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006411 break;
6412
6413 case HCI_EV_PIN_CODE_REQ:
6414 hci_pin_code_request_evt(hdev, skb);
6415 break;
6416
6417 case HCI_EV_LINK_KEY_REQ:
6418 hci_link_key_request_evt(hdev, skb);
6419 break;
6420
6421 case HCI_EV_LINK_KEY_NOTIFY:
6422 hci_link_key_notify_evt(hdev, skb);
6423 break;
6424
6425 case HCI_EV_CLOCK_OFFSET:
6426 hci_clock_offset_evt(hdev, skb);
6427 break;
6428
Marcel Holtmanna8746412008-07-14 20:13:46 +02006429 case HCI_EV_PKT_TYPE_CHANGE:
6430 hci_pkt_type_change_evt(hdev, skb);
6431 break;
6432
Marcel Holtmann85a1e932005-08-09 20:28:02 -07006433 case HCI_EV_PSCAN_REP_MODE:
6434 hci_pscan_rep_mode_evt(hdev, skb);
6435 break;
6436
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006437 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
6438 hci_inquiry_result_with_rssi_evt(hdev, skb);
6439 break;
6440
6441 case HCI_EV_REMOTE_EXT_FEATURES:
6442 hci_remote_ext_features_evt(hdev, skb);
6443 break;
6444
6445 case HCI_EV_SYNC_CONN_COMPLETE:
6446 hci_sync_conn_complete_evt(hdev, skb);
6447 break;
6448
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006449 case HCI_EV_EXTENDED_INQUIRY_RESULT:
6450 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006451 break;
6452
Johan Hedberg1c2e0042012-06-08 23:31:13 +08006453 case HCI_EV_KEY_REFRESH_COMPLETE:
6454 hci_key_refresh_complete_evt(hdev, skb);
6455 break;
6456
Marcel Holtmann04936842008-07-14 20:13:48 +02006457 case HCI_EV_IO_CAPA_REQUEST:
6458 hci_io_capa_request_evt(hdev, skb);
6459 break;
6460
Johan Hedberg03b555e2011-01-04 15:40:05 +02006461 case HCI_EV_IO_CAPA_REPLY:
6462 hci_io_capa_reply_evt(hdev, skb);
6463 break;
6464
Johan Hedberga5c29682011-02-19 12:05:57 -03006465 case HCI_EV_USER_CONFIRM_REQUEST:
6466 hci_user_confirm_request_evt(hdev, skb);
6467 break;
6468
Brian Gix1143d452011-11-23 08:28:34 -08006469 case HCI_EV_USER_PASSKEY_REQUEST:
6470 hci_user_passkey_request_evt(hdev, skb);
6471 break;
6472
Johan Hedberg92a25252012-09-06 18:39:26 +03006473 case HCI_EV_USER_PASSKEY_NOTIFY:
6474 hci_user_passkey_notify_evt(hdev, skb);
6475 break;
6476
6477 case HCI_EV_KEYPRESS_NOTIFY:
6478 hci_keypress_notify_evt(hdev, skb);
6479 break;
6480
Marcel Holtmann04936842008-07-14 20:13:48 +02006481 case HCI_EV_SIMPLE_PAIR_COMPLETE:
6482 hci_simple_pair_complete_evt(hdev, skb);
6483 break;
6484
Marcel Holtmann41a96212008-07-14 20:13:48 +02006485 case HCI_EV_REMOTE_HOST_FEATURES:
6486 hci_remote_host_features_evt(hdev, skb);
6487 break;
6488
Ville Tervofcd89c02011-02-10 22:38:47 -03006489 case HCI_EV_LE_META:
6490 hci_le_meta_evt(hdev, skb);
6491 break;
6492
Szymon Janc2763eda2011-03-22 13:12:22 +01006493 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
6494 hci_remote_oob_data_request_evt(hdev, skb);
6495 break;
6496
Arron Wanga77a6a12015-07-24 17:13:15 +08006497#if IS_ENABLED(CONFIG_BT_HS)
6498 case HCI_EV_CHANNEL_SELECTED:
6499 hci_chan_selected_evt(hdev, skb);
6500 break;
6501
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03006502 case HCI_EV_PHY_LINK_COMPLETE:
6503 hci_phy_link_complete_evt(hdev, skb);
6504 break;
6505
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03006506 case HCI_EV_LOGICAL_LINK_COMPLETE:
6507 hci_loglink_complete_evt(hdev, skb);
6508 break;
6509
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02006510 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
6511 hci_disconn_loglink_complete_evt(hdev, skb);
6512 break;
6513
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02006514 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
6515 hci_disconn_phylink_complete_evt(hdev, skb);
6516 break;
Arron Wanga77a6a12015-07-24 17:13:15 +08006517#endif
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02006518
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02006519 case HCI_EV_NUM_COMP_BLOCKS:
6520 hci_num_comp_blocks_evt(hdev, skb);
6521 break;
6522
Miao-chen Chou145373c2020-04-03 21:44:01 +02006523 case HCI_EV_VENDOR:
6524 msft_vendor_evt(hdev, skb);
6525 break;
6526
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006527 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03006528 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006529 break;
6530 }
6531
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006532 if (req_complete) {
Johan Hedberge62144872015-04-02 13:41:08 +03006533 req_complete(hdev, status, opcode);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006534 } else if (req_complete_skb) {
6535 if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
6536 kfree_skb(orig_skb);
6537 orig_skb = NULL;
6538 }
Johan Hedberge62144872015-04-02 13:41:08 +03006539 req_complete_skb(hdev, status, opcode, orig_skb);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006540 }
Johan Hedberge62144872015-04-02 13:41:08 +03006541
Alain Michaud08bb4da2020-03-03 15:55:34 +00006542done:
Johan Hedberge62144872015-04-02 13:41:08 +03006543 kfree_skb(orig_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006544 kfree_skb(skb);
6545 hdev->stat.evt_rx++;
6546}