blob: 016b2999f21957656f5e1bea5870a0e95944ab6c [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI event handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/unaligned.h>
28
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
Mikel Astizf0d6a0e2012-08-09 09:52:30 +020031#include <net/bluetooth/mgmt.h>
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070032
Johan Hedberg0857dd32014-12-19 13:40:20 +020033#include "hci_request.h"
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +010034#include "hci_debugfs.h"
Marcel Holtmann70247282013-10-10 14:54:15 -070035#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070036#include "amp.h"
Johan Hedberg2ceba532014-06-16 19:25:16 +030037#include "smp.h"
Miao-chen Chou145373c2020-04-03 21:44:01 +020038#include "msft.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Marcel Holtmannaa5b0342015-01-27 16:04:33 -080040#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
41 "\x00\x00\x00\x00\x00\x00\x00\x00"
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/* Handle HCI Event packets */
44
Sonny Sasakaadf1d692020-05-06 12:55:03 -070045static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb,
46 u8 *new_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020048 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030050 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Sonny Sasakaadf1d692020-05-06 12:55:03 -070052 /* It is possible that we receive Inquiry Complete event right
53 * before we receive Inquiry Cancel Command Complete event, in
54 * which case the latter event should have status of Command
55 * Disallowed (0x0c). This should not be treated as error, since
56 * we actually achieve what Inquiry Cancel wants to achieve,
57 * which is to end the last Inquiry session.
58 */
59 if (status == 0x0c && !test_bit(HCI_INQUIRY, &hdev->flags)) {
60 bt_dev_warn(hdev, "Ignoring error of Inquiry Cancel command");
61 status = 0x00;
62 }
63
64 *new_status = status;
65
Andre Guedes82f47852013-04-30 15:29:34 -030066 if (status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Andre Guedes89352e72011-11-04 14:16:53 -030069 clear_bit(HCI_INQUIRY, &hdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010070 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -030071 wake_up_bit(&hdev->flags, HCI_INQUIRY);
Andre Guedes89352e72011-11-04 14:16:53 -030072
Johan Hedberg50143a42014-06-10 14:05:57 +030073 hci_dev_lock(hdev);
Jakub Pawlowski168b8a22015-10-16 10:07:49 +030074 /* Set discovery state to stopped if we're not doing LE active
75 * scanning.
76 */
77 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
78 hdev->le_scan_type != LE_SCAN_ACTIVE)
79 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg50143a42014-06-10 14:05:57 +030080 hci_dev_unlock(hdev);
81
Marcel Holtmanna9de9242007-10-20 13:33:56 +020082 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Andre Guedes4d934832012-03-21 00:03:35 -030085static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
86{
87 __u8 status = *((__u8 *) skb->data);
88
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030089 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesae854a72012-03-21 00:03:36 -030090
91 if (status)
92 return;
93
Marcel Holtmanna1536da2015-03-13 02:11:01 -070094 hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
Andre Guedes4d934832012-03-21 00:03:35 -030095}
96
Marcel Holtmanna9de9242007-10-20 13:33:56 +020097static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020099 __u8 status = *((__u8 *) skb->data);
100
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300101 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200102
103 if (status)
104 return;
105
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700106 hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
Andre Guedesae854a72012-03-21 00:03:36 -0300107
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200108 hci_conn_check_pending(hdev);
109}
110
Gustavo Padovan807deac2012-05-17 00:36:24 -0300111static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
112 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200113{
114 BT_DBG("%s", hdev->name);
115}
116
117static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
118{
119 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300122 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200124 if (rp->status)
125 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200127 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200129 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Johan Hedberg40bef302014-07-16 11:42:27 +0300130 if (conn)
131 conn->role = rp->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200132
133 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200136static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
137{
138 struct hci_rp_read_link_policy *rp = (void *) skb->data;
139 struct hci_conn *conn;
140
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300141 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200142
143 if (rp->status)
144 return;
145
146 hci_dev_lock(hdev);
147
148 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
149 if (conn)
150 conn->link_policy = __le16_to_cpu(rp->policy);
151
152 hci_dev_unlock(hdev);
153}
154
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200155static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200157 struct hci_rp_write_link_policy *rp = (void *) skb->data;
158 struct hci_conn *conn;
159 void *sent;
160
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300161 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200162
163 if (rp->status)
164 return;
165
166 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
167 if (!sent)
168 return;
169
170 hci_dev_lock(hdev);
171
172 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200173 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700174 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200175
176 hci_dev_unlock(hdev);
177}
178
Gustavo Padovan807deac2012-05-17 00:36:24 -0300179static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
180 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200181{
182 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
183
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300184 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200185
186 if (rp->status)
187 return;
188
189 hdev->link_policy = __le16_to_cpu(rp->policy);
190}
191
Gustavo Padovan807deac2012-05-17 00:36:24 -0300192static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
193 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200194{
195 __u8 status = *((__u8 *) skb->data);
196 void *sent;
197
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300198 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200199
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200200 if (status)
201 return;
202
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200203 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
204 if (!sent)
205 return;
206
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200207 hdev->link_policy = get_unaligned_le16(sent);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200208}
209
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200210static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
211{
212 __u8 status = *((__u8 *) skb->data);
213
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300214 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200215
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300216 clear_bit(HCI_RESET, &hdev->flags);
217
Marcel Holtmann8761f9d2014-11-02 02:45:58 +0100218 if (status)
219 return;
220
Johan Hedberga297e972012-02-21 17:55:47 +0200221 /* Reset all non-persistent flags */
Marcel Holtmanneacb44d2015-03-13 09:04:17 -0700222 hci_dev_clear_volatile_flags(hdev);
Andre Guedes69775ff2012-02-23 16:50:05 +0200223
Johan Hedberg39c5d972015-01-28 19:56:01 +0200224 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
225
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100226 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
227 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
Johan Hedberg3f0f5242012-11-08 01:23:00 +0100228
229 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
230 hdev->adv_data_len = 0;
Marcel Holtmannf8e808b2013-10-16 00:16:47 -0700231
232 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
233 hdev->scan_rsp_data_len = 0;
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700234
Marcel Holtmann533553f2014-03-21 12:18:10 -0700235 hdev->le_scan_type = LE_SCAN_PASSIVE;
236
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700237 hdev->ssp_debug_mode = 0;
Marcel Holtmanna4d55042014-10-29 23:37:53 +0100238
239 hci_bdaddr_list_clear(&hdev->le_white_list);
Ankit Navikcfdb0c22018-06-29 12:12:50 +0530240 hci_bdaddr_list_clear(&hdev->le_resolv_list);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200241}
242
Marcel Holtmannc2f0f972015-01-12 09:21:25 -0800243static void hci_cc_read_stored_link_key(struct hci_dev *hdev,
244 struct sk_buff *skb)
245{
246 struct hci_rp_read_stored_link_key *rp = (void *)skb->data;
247 struct hci_cp_read_stored_link_key *sent;
248
249 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
250
251 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_STORED_LINK_KEY);
252 if (!sent)
253 return;
254
255 if (!rp->status && sent->read_all == 0x01) {
256 hdev->stored_max_keys = rp->max_keys;
257 hdev->stored_num_keys = rp->num_keys;
258 }
259}
260
Marcel Holtmanna93661202015-01-12 09:21:28 -0800261static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
262 struct sk_buff *skb)
263{
264 struct hci_rp_delete_stored_link_key *rp = (void *)skb->data;
265
266 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
267
268 if (rp->status)
269 return;
270
271 if (rp->num_keys <= hdev->stored_num_keys)
272 hdev->stored_num_keys -= rp->num_keys;
273 else
274 hdev->stored_num_keys = 0;
275}
276
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200277static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
278{
279 __u8 status = *((__u8 *) skb->data);
280 void *sent;
281
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300282 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200283
284 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
285 if (!sent)
286 return;
287
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200288 hci_dev_lock(hdev);
289
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700290 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200291 mgmt_set_local_name_complete(hdev, sent, status);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +0200292 else if (!status)
293 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200294
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200295 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200296}
297
298static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
299{
300 struct hci_rp_read_local_name *rp = (void *) skb->data;
301
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300302 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200303
304 if (rp->status)
305 return;
306
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700307 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
308 hci_dev_test_flag(hdev, HCI_CONFIG))
Johan Hedbergdb99b5f2012-02-22 20:14:22 +0200309 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200310}
311
312static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
313{
314 __u8 status = *((__u8 *) skb->data);
315 void *sent;
316
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300317 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200318
319 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
320 if (!sent)
321 return;
322
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530323 hci_dev_lock(hdev);
324
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200325 if (!status) {
326 __u8 param = *((__u8 *) sent);
327
328 if (param == AUTH_ENABLED)
329 set_bit(HCI_AUTH, &hdev->flags);
330 else
331 clear_bit(HCI_AUTH, &hdev->flags);
332 }
333
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700334 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg33ef95e2012-02-16 23:56:27 +0200335 mgmt_auth_enable_complete(hdev, status);
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530336
337 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200338}
339
340static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
341{
342 __u8 status = *((__u8 *) skb->data);
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200343 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200344 void *sent;
345
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300346 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200347
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200348 if (status)
349 return;
350
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200351 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
352 if (!sent)
353 return;
354
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200355 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200356
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200357 if (param)
358 set_bit(HCI_ENCRYPT, &hdev->flags);
359 else
360 clear_bit(HCI_ENCRYPT, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200361}
362
363static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
364{
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200365 __u8 status = *((__u8 *) skb->data);
366 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200367 void *sent;
368
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300369 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200370
371 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
372 if (!sent)
373 return;
374
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200375 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200376
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200377 hci_dev_lock(hdev);
378
Mikel Astizfa1bd912012-08-09 09:52:29 +0200379 if (status) {
Johan Hedberg2d7cee52011-11-07 22:16:03 +0200380 hdev->discov_timeout = 0;
381 goto done;
382 }
383
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300384 if (param & SCAN_INQUIRY)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200385 set_bit(HCI_ISCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300386 else
387 clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200388
Johan Hedberg031547d2014-07-10 12:09:06 +0300389 if (param & SCAN_PAGE)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200390 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300391 else
Johan Hedberg204e3992014-07-28 15:45:31 +0300392 clear_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200393
394done:
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200395 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200396}
397
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -0800398static void hci_cc_set_event_filter(struct hci_dev *hdev, struct sk_buff *skb)
399{
400 __u8 status = *((__u8 *)skb->data);
401 struct hci_cp_set_event_filter *cp;
402 void *sent;
403
404 BT_DBG("%s status 0x%2.2x", hdev->name, status);
405
406 if (status)
407 return;
408
409 sent = hci_sent_cmd_data(hdev, HCI_OP_SET_EVENT_FLT);
410 if (!sent)
411 return;
412
413 cp = (struct hci_cp_set_event_filter *)sent;
414
415 if (cp->flt_type == HCI_FLT_CLEAR_ALL)
416 hci_dev_clear_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
417 else
418 hci_dev_set_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
419}
420
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200421static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
422{
423 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
424
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300425 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200426
427 if (rp->status)
428 return;
429
430 memcpy(hdev->dev_class, rp->dev_class, 3);
431
432 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300433 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200434}
435
436static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
437{
438 __u8 status = *((__u8 *) skb->data);
439 void *sent;
440
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300441 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200442
443 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
444 if (!sent)
445 return;
446
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100447 hci_dev_lock(hdev);
448
449 if (status == 0)
450 memcpy(hdev->dev_class, sent, 3);
451
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700452 if (hci_dev_test_flag(hdev, HCI_MGMT))
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100453 mgmt_set_class_of_dev_complete(hdev, sent, status);
454
455 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200456}
457
458static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
459{
460 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200462
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300463 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200464
465 if (rp->status)
466 return;
467
468 setting = __le16_to_cpu(rp->voice_setting);
469
Marcel Holtmannf383f272008-07-14 20:13:47 +0200470 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200471 return;
472
473 hdev->voice_setting = setting;
474
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300475 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200476
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200477 if (hdev->notify)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200478 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200479}
480
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300481static void hci_cc_write_voice_setting(struct hci_dev *hdev,
482 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200483{
484 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200485 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 void *sent;
487
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300488 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Marcel Holtmannf383f272008-07-14 20:13:47 +0200490 if (status)
491 return;
492
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200493 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
494 if (!sent)
495 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Marcel Holtmannf383f272008-07-14 20:13:47 +0200497 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Marcel Holtmannf383f272008-07-14 20:13:47 +0200499 if (hdev->voice_setting == setting)
500 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Marcel Holtmannf383f272008-07-14 20:13:47 +0200502 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300504 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200505
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200506 if (hdev->notify)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200507 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700510static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
511 struct sk_buff *skb)
512{
513 struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
514
515 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
516
517 if (rp->status)
518 return;
519
520 hdev->num_iac = rp->num_iac;
521
522 BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
523}
524
Marcel Holtmann333140b2008-07-14 20:13:48 +0200525static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
526{
527 __u8 status = *((__u8 *) skb->data);
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300528 struct hci_cp_write_ssp_mode *sent;
Marcel Holtmann333140b2008-07-14 20:13:48 +0200529
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300530 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200531
Marcel Holtmann333140b2008-07-14 20:13:48 +0200532 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
533 if (!sent)
534 return;
535
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530536 hci_dev_lock(hdev);
537
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300538 if (!status) {
539 if (sent->mode)
Johan Hedbergcad718e2013-04-17 15:00:51 +0300540 hdev->features[1][0] |= LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300541 else
Johan Hedbergcad718e2013-04-17 15:00:51 +0300542 hdev->features[1][0] &= ~LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300543 }
544
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700545 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300546 mgmt_ssp_enable_complete(hdev, sent->mode, status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200547 else if (!status) {
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300548 if (sent->mode)
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700549 hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200550 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700551 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200552 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530553
554 hci_dev_unlock(hdev);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200555}
556
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800557static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
558{
559 u8 status = *((u8 *) skb->data);
560 struct hci_cp_write_sc_support *sent;
561
562 BT_DBG("%s status 0x%2.2x", hdev->name, status);
563
564 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
565 if (!sent)
566 return;
567
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530568 hci_dev_lock(hdev);
569
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800570 if (!status) {
571 if (sent->support)
572 hdev->features[1][0] |= LMP_HOST_SC;
573 else
574 hdev->features[1][0] &= ~LMP_HOST_SC;
575 }
576
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700577 if (!hci_dev_test_flag(hdev, HCI_MGMT) && !status) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800578 if (sent->support)
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700579 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800580 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700581 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800582 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530583
584 hci_dev_unlock(hdev);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800585}
586
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200587static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
588{
589 struct hci_rp_read_local_version *rp = (void *) skb->data;
590
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300591 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200592
593 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200594 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200595
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700596 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
597 hci_dev_test_flag(hdev, HCI_CONFIG)) {
Marcel Holtmann0d5551f2013-10-18 12:04:50 -0700598 hdev->hci_ver = rp->hci_ver;
599 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
600 hdev->lmp_ver = rp->lmp_ver;
601 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
602 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
603 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200604}
605
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300606static void hci_cc_read_local_commands(struct hci_dev *hdev,
607 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200608{
609 struct hci_rp_read_local_commands *rp = (void *) skb->data;
610
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300611 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200612
Marcel Holtmann6a070e62013-10-31 04:54:33 -0700613 if (rp->status)
614 return;
615
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700616 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
617 hci_dev_test_flag(hdev, HCI_CONFIG))
Johan Hedberg2177bab2013-03-05 20:37:43 +0200618 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200619}
620
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +0530621static void hci_cc_read_auth_payload_timeout(struct hci_dev *hdev,
622 struct sk_buff *skb)
623{
624 struct hci_rp_read_auth_payload_to *rp = (void *)skb->data;
625 struct hci_conn *conn;
626
627 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
628
629 if (rp->status)
630 return;
631
632 hci_dev_lock(hdev);
633
634 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
635 if (conn)
636 conn->auth_payload_timeout = __le16_to_cpu(rp->timeout);
637
638 hci_dev_unlock(hdev);
639}
640
641static void hci_cc_write_auth_payload_timeout(struct hci_dev *hdev,
642 struct sk_buff *skb)
643{
644 struct hci_rp_write_auth_payload_to *rp = (void *)skb->data;
645 struct hci_conn *conn;
646 void *sent;
647
648 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
649
650 if (rp->status)
651 return;
652
653 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO);
654 if (!sent)
655 return;
656
657 hci_dev_lock(hdev);
658
659 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
660 if (conn)
661 conn->auth_payload_timeout = get_unaligned_le16(sent + 2);
662
663 hci_dev_unlock(hdev);
664}
665
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300666static void hci_cc_read_local_features(struct hci_dev *hdev,
667 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200668{
669 struct hci_rp_read_local_features *rp = (void *) skb->data;
670
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300671 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200672
673 if (rp->status)
674 return;
675
676 memcpy(hdev->features, rp->features, 8);
677
678 /* Adjust default settings according to features
679 * supported by device. */
680
Johan Hedbergcad718e2013-04-17 15:00:51 +0300681 if (hdev->features[0][0] & LMP_3SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200682 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
683
Johan Hedbergcad718e2013-04-17 15:00:51 +0300684 if (hdev->features[0][0] & LMP_5SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200685 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
686
Johan Hedbergcad718e2013-04-17 15:00:51 +0300687 if (hdev->features[0][1] & LMP_HV2) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200688 hdev->pkt_type |= (HCI_HV2);
689 hdev->esco_type |= (ESCO_HV2);
690 }
691
Johan Hedbergcad718e2013-04-17 15:00:51 +0300692 if (hdev->features[0][1] & LMP_HV3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200693 hdev->pkt_type |= (HCI_HV3);
694 hdev->esco_type |= (ESCO_HV3);
695 }
696
Andre Guedes45db810f2012-07-24 15:03:49 -0300697 if (lmp_esco_capable(hdev))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200698 hdev->esco_type |= (ESCO_EV3);
699
Johan Hedbergcad718e2013-04-17 15:00:51 +0300700 if (hdev->features[0][4] & LMP_EV4)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200701 hdev->esco_type |= (ESCO_EV4);
702
Johan Hedbergcad718e2013-04-17 15:00:51 +0300703 if (hdev->features[0][4] & LMP_EV5)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200704 hdev->esco_type |= (ESCO_EV5);
705
Johan Hedbergcad718e2013-04-17 15:00:51 +0300706 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100707 hdev->esco_type |= (ESCO_2EV3);
708
Johan Hedbergcad718e2013-04-17 15:00:51 +0300709 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100710 hdev->esco_type |= (ESCO_3EV3);
711
Johan Hedbergcad718e2013-04-17 15:00:51 +0300712 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100713 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200714}
715
Andre Guedes971e3a42011-06-30 19:20:52 -0300716static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300717 struct sk_buff *skb)
Andre Guedes971e3a42011-06-30 19:20:52 -0300718{
719 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
720
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300721 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andre Guedes971e3a42011-06-30 19:20:52 -0300722
723 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200724 return;
Andre Guedes971e3a42011-06-30 19:20:52 -0300725
Marcel Holtmann57af75a2013-10-18 12:04:47 -0700726 if (hdev->max_page < rp->max_page)
727 hdev->max_page = rp->max_page;
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300728
Johan Hedbergcad718e2013-04-17 15:00:51 +0300729 if (rp->page < HCI_MAX_PAGES)
730 memcpy(hdev->features[rp->page], rp->features, 8);
Andre Guedes971e3a42011-06-30 19:20:52 -0300731}
732
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200733static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300734 struct sk_buff *skb)
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200735{
736 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
737
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300738 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200739
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200740 if (rp->status)
741 return;
742
743 hdev->flow_ctl_mode = rp->mode;
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200744}
745
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200746static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
747{
748 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
749
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300750 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200751
752 if (rp->status)
753 return;
754
755 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
756 hdev->sco_mtu = rp->sco_mtu;
757 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
758 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
759
760 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
761 hdev->sco_mtu = 64;
762 hdev->sco_pkts = 8;
763 }
764
765 hdev->acl_cnt = hdev->acl_pkts;
766 hdev->sco_cnt = hdev->sco_pkts;
767
Gustavo Padovan807deac2012-05-17 00:36:24 -0300768 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
769 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200770}
771
772static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
773{
774 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
775
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300776 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200777
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200778 if (rp->status)
779 return;
780
781 if (test_bit(HCI_INIT, &hdev->flags))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200782 bacpy(&hdev->bdaddr, &rp->bdaddr);
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200783
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700784 if (hci_dev_test_flag(hdev, HCI_SETUP))
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200785 bacpy(&hdev->setup_addr, &rp->bdaddr);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200786}
787
Marcel Holtmanna4790362020-04-03 21:44:04 +0200788static void hci_cc_read_local_pairing_opts(struct hci_dev *hdev,
789 struct sk_buff *skb)
790{
791 struct hci_rp_read_local_pairing_opts *rp = (void *) skb->data;
792
793 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
794
795 if (rp->status)
796 return;
797
798 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
799 hci_dev_test_flag(hdev, HCI_CONFIG)) {
800 hdev->pairing_opts = rp->pairing_opts;
801 hdev->max_enc_key_size = rp->max_key_size;
802 }
803}
804
Johan Hedbergf332ec62013-03-15 17:07:11 -0500805static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
806 struct sk_buff *skb)
807{
808 struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
809
810 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
811
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200812 if (rp->status)
813 return;
814
815 if (test_bit(HCI_INIT, &hdev->flags)) {
Johan Hedbergf332ec62013-03-15 17:07:11 -0500816 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
817 hdev->page_scan_window = __le16_to_cpu(rp->window);
818 }
819}
820
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500821static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
822 struct sk_buff *skb)
823{
824 u8 status = *((u8 *) skb->data);
825 struct hci_cp_write_page_scan_activity *sent;
826
827 BT_DBG("%s status 0x%2.2x", hdev->name, status);
828
829 if (status)
830 return;
831
832 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
833 if (!sent)
834 return;
835
836 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
837 hdev->page_scan_window = __le16_to_cpu(sent->window);
838}
839
Johan Hedbergf332ec62013-03-15 17:07:11 -0500840static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
841 struct sk_buff *skb)
842{
843 struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
844
845 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
846
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200847 if (rp->status)
848 return;
849
850 if (test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergf332ec62013-03-15 17:07:11 -0500851 hdev->page_scan_type = rp->type;
852}
853
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500854static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
855 struct sk_buff *skb)
856{
857 u8 status = *((u8 *) skb->data);
858 u8 *type;
859
860 BT_DBG("%s status 0x%2.2x", hdev->name, status);
861
862 if (status)
863 return;
864
865 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
866 if (type)
867 hdev->page_scan_type = *type;
868}
869
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200870static void hci_cc_read_data_block_size(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300871 struct sk_buff *skb)
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200872{
873 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
874
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300875 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200876
877 if (rp->status)
878 return;
879
880 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
881 hdev->block_len = __le16_to_cpu(rp->block_len);
882 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
883
884 hdev->block_cnt = hdev->num_blocks;
885
886 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300887 hdev->block_cnt, hdev->block_len);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200888}
889
Johan Hedberg33f35722014-06-28 17:54:06 +0300890static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
891{
892 struct hci_rp_read_clock *rp = (void *) skb->data;
893 struct hci_cp_read_clock *cp;
894 struct hci_conn *conn;
895
896 BT_DBG("%s", hdev->name);
897
898 if (skb->len < sizeof(*rp))
899 return;
900
901 if (rp->status)
902 return;
903
904 hci_dev_lock(hdev);
905
906 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
907 if (!cp)
908 goto unlock;
909
910 if (cp->which == 0x00) {
911 hdev->clock = le32_to_cpu(rp->clock);
912 goto unlock;
913 }
914
915 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
916 if (conn) {
917 conn->clock = le32_to_cpu(rp->clock);
918 conn->clock_accuracy = le16_to_cpu(rp->accuracy);
919 }
920
921unlock:
922 hci_dev_unlock(hdev);
923}
924
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300925static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300926 struct sk_buff *skb)
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300927{
928 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
929
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300930 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300931
932 if (rp->status)
Arron Wang83927882015-07-24 17:10:16 +0800933 return;
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300934
935 hdev->amp_status = rp->amp_status;
936 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
937 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
938 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
939 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
940 hdev->amp_type = rp->amp_type;
941 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
942 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
943 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
944 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300945}
946
Johan Hedbergd5859e22011-01-25 01:19:58 +0200947static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300948 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200949{
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700950 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200951
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300952 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200953
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200954 if (rp->status)
955 return;
956
957 hdev->inq_tx_power = rp->tx_power;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200958}
959
Alain Michaud00bce3f2020-03-05 16:14:59 +0000960static void hci_cc_read_def_err_data_reporting(struct hci_dev *hdev,
961 struct sk_buff *skb)
962{
963 struct hci_rp_read_def_err_data_reporting *rp = (void *)skb->data;
964
965 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
966
967 if (rp->status)
968 return;
969
970 hdev->err_data_reporting = rp->err_data_reporting;
971}
972
973static void hci_cc_write_def_err_data_reporting(struct hci_dev *hdev,
974 struct sk_buff *skb)
975{
976 __u8 status = *((__u8 *)skb->data);
977 struct hci_cp_write_def_err_data_reporting *cp;
978
979 BT_DBG("%s status 0x%2.2x", hdev->name, status);
980
981 if (status)
982 return;
983
984 cp = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING);
985 if (!cp)
986 return;
987
988 hdev->err_data_reporting = cp->err_data_reporting;
989}
990
Johan Hedberg980e1a52011-01-22 06:10:07 +0200991static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
992{
993 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
994 struct hci_cp_pin_code_reply *cp;
995 struct hci_conn *conn;
996
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300997 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200998
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200999 hci_dev_lock(hdev);
1000
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001001 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001002 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001003
Mikel Astizfa1bd912012-08-09 09:52:29 +02001004 if (rp->status)
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001005 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001006
1007 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
1008 if (!cp)
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001009 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001010
1011 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1012 if (conn)
1013 conn->pin_length = cp->pin_len;
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001014
1015unlock:
1016 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001017}
1018
1019static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
1020{
1021 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
1022
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001023 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001024
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001025 hci_dev_lock(hdev);
1026
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001027 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001028 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001029 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001030
1031 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001032}
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001033
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001034static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
1035 struct sk_buff *skb)
1036{
1037 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
1038
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001039 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001040
1041 if (rp->status)
1042 return;
1043
1044 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
1045 hdev->le_pkts = rp->le_max_pkt;
1046
1047 hdev->le_cnt = hdev->le_pkts;
1048
1049 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001050}
Johan Hedberg980e1a52011-01-22 06:10:07 +02001051
Johan Hedberg60e77322013-01-22 14:01:59 +02001052static void hci_cc_le_read_local_features(struct hci_dev *hdev,
1053 struct sk_buff *skb)
1054{
1055 struct hci_rp_le_read_local_features *rp = (void *) skb->data;
1056
1057 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1058
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001059 if (rp->status)
1060 return;
1061
1062 memcpy(hdev->le_features, rp->features, 8);
Johan Hedberg60e77322013-01-22 14:01:59 +02001063}
1064
Johan Hedberg8fa19092012-10-19 20:57:49 +03001065static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
1066 struct sk_buff *skb)
1067{
1068 struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
1069
1070 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1071
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001072 if (rp->status)
1073 return;
1074
1075 hdev->adv_tx_power = rp->tx_power;
Johan Hedberg8fa19092012-10-19 20:57:49 +03001076}
1077
Johan Hedberga5c29682011-02-19 12:05:57 -03001078static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
1079{
1080 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1081
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001082 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -03001083
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001084 hci_dev_lock(hdev);
1085
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001086 if (hci_dev_test_flag(hdev, HCI_MGMT))
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001087 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
1088 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001089
1090 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03001091}
1092
1093static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001094 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03001095{
1096 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1097
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001098 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -03001099
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001100 hci_dev_lock(hdev);
1101
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001102 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001103 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001104 ACL_LINK, 0, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001105
1106 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03001107}
1108
Brian Gix1143d452011-11-23 08:28:34 -08001109static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
1110{
1111 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1112
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001113 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001114
1115 hci_dev_lock(hdev);
1116
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001117 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg272d90d2012-02-09 15:26:12 +02001118 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001119 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001120
1121 hci_dev_unlock(hdev);
1122}
1123
1124static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001125 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08001126{
1127 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
1128
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001129 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001130
1131 hci_dev_lock(hdev);
1132
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001133 if (hci_dev_test_flag(hdev, HCI_MGMT))
Brian Gix1143d452011-11-23 08:28:34 -08001134 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001135 ACL_LINK, 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001136
1137 hci_dev_unlock(hdev);
1138}
1139
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001140static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
1141 struct sk_buff *skb)
Szymon Jancc35938b2011-03-22 13:12:21 +01001142{
1143 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
1144
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001145 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001146}
1147
1148static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
1149 struct sk_buff *skb)
1150{
1151 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
1152
1153 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Szymon Jancc35938b2011-03-22 13:12:21 +01001154}
1155
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001156static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
1157{
1158 __u8 status = *((__u8 *) skb->data);
1159 bdaddr_t *sent;
1160
1161 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1162
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001163 if (status)
1164 return;
1165
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001166 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1167 if (!sent)
1168 return;
1169
1170 hci_dev_lock(hdev);
1171
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001172 bacpy(&hdev->random_addr, sent);
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001173
1174 hci_dev_unlock(hdev);
1175}
1176
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05301177static void hci_cc_le_set_default_phy(struct hci_dev *hdev, struct sk_buff *skb)
1178{
1179 __u8 status = *((__u8 *) skb->data);
1180 struct hci_cp_le_set_default_phy *cp;
1181
1182 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1183
1184 if (status)
1185 return;
1186
1187 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_DEFAULT_PHY);
1188 if (!cp)
1189 return;
1190
1191 hci_dev_lock(hdev);
1192
1193 hdev->le_tx_def_phys = cp->tx_phys;
1194 hdev->le_rx_def_phys = cp->rx_phys;
1195
1196 hci_dev_unlock(hdev);
1197}
1198
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301199static void hci_cc_le_set_adv_set_random_addr(struct hci_dev *hdev,
1200 struct sk_buff *skb)
1201{
1202 __u8 status = *((__u8 *) skb->data);
1203 struct hci_cp_le_set_adv_set_rand_addr *cp;
1204 struct adv_info *adv_instance;
1205
1206 if (status)
1207 return;
1208
1209 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR);
1210 if (!cp)
1211 return;
1212
1213 hci_dev_lock(hdev);
1214
Daniel Winkler25e70882021-04-05 16:33:04 -07001215 if (!cp->handle) {
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301216 /* Store in hdev for instance 0 (Set adv and Directed advs) */
1217 bacpy(&hdev->random_addr, &cp->bdaddr);
1218 } else {
Daniel Winkler25e70882021-04-05 16:33:04 -07001219 adv_instance = hci_find_adv_instance(hdev, cp->handle);
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301220 if (adv_instance)
1221 bacpy(&adv_instance->random_addr, &cp->bdaddr);
1222 }
1223
1224 hci_dev_unlock(hdev);
1225}
1226
Daniel Winkler7c395ea2020-12-03 12:12:51 -08001227static void hci_cc_le_read_transmit_power(struct hci_dev *hdev,
1228 struct sk_buff *skb)
1229{
1230 struct hci_rp_le_read_transmit_power *rp = (void *)skb->data;
1231
1232 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1233
1234 if (rp->status)
1235 return;
1236
1237 hdev->min_le_tx_power = rp->min_le_tx_power;
1238 hdev->max_le_tx_power = rp->max_le_tx_power;
1239}
1240
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001241static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1242{
1243 __u8 *sent, status = *((__u8 *) skb->data);
1244
1245 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1246
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001247 if (status)
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001248 return;
1249
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001250 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1251 if (!sent)
Johan Hedberg3c857752014-03-25 10:30:49 +02001252 return;
1253
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001254 hci_dev_lock(hdev);
1255
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001256 /* If we're doing connection initiation as peripheral. Set a
Johan Hedberg3c857752014-03-25 10:30:49 +02001257 * timeout in case something goes wrong.
1258 */
1259 if (*sent) {
1260 struct hci_conn *conn;
1261
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001262 hci_dev_set_flag(hdev, HCI_LE_ADV);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001263
Jakub Pawlowskie7d9ab72015-08-07 20:22:52 +02001264 conn = hci_lookup_le_connect(hdev);
Johan Hedberg3c857752014-03-25 10:30:49 +02001265 if (conn)
1266 queue_delayed_work(hdev->workqueue,
1267 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001268 conn->conn_timeout);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001269 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001270 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedberg3c857752014-03-25 10:30:49 +02001271 }
1272
Johan Hedberg04b4edc2013-03-15 17:07:01 -05001273 hci_dev_unlock(hdev);
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001274}
1275
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301276static void hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev,
1277 struct sk_buff *skb)
1278{
1279 struct hci_cp_le_set_ext_adv_enable *cp;
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301280 __u8 status = *((__u8 *) skb->data);
1281
1282 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1283
1284 if (status)
1285 return;
1286
1287 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE);
1288 if (!cp)
1289 return;
1290
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301291 hci_dev_lock(hdev);
1292
1293 if (cp->enable) {
1294 struct hci_conn *conn;
1295
1296 hci_dev_set_flag(hdev, HCI_LE_ADV);
1297
1298 conn = hci_lookup_le_connect(hdev);
1299 if (conn)
1300 queue_delayed_work(hdev->workqueue,
1301 &conn->le_conn_timeout,
1302 conn->conn_timeout);
Jaganath Kanakkassery45b77492018-07-19 17:09:43 +05301303 } else {
1304 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301305 }
1306
1307 hci_dev_unlock(hdev);
1308}
1309
Marcel Holtmann533553f2014-03-21 12:18:10 -07001310static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1311{
1312 struct hci_cp_le_set_scan_param *cp;
1313 __u8 status = *((__u8 *) skb->data);
1314
1315 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1316
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001317 if (status)
1318 return;
1319
Marcel Holtmann533553f2014-03-21 12:18:10 -07001320 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1321 if (!cp)
1322 return;
1323
1324 hci_dev_lock(hdev);
1325
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001326 hdev->le_scan_type = cp->type;
Marcel Holtmann533553f2014-03-21 12:18:10 -07001327
1328 hci_dev_unlock(hdev);
1329}
1330
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301331static void hci_cc_le_set_ext_scan_param(struct hci_dev *hdev,
1332 struct sk_buff *skb)
1333{
1334 struct hci_cp_le_set_ext_scan_params *cp;
1335 __u8 status = *((__u8 *) skb->data);
1336 struct hci_cp_le_scan_phy_params *phy_param;
1337
1338 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1339
1340 if (status)
1341 return;
1342
1343 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS);
1344 if (!cp)
1345 return;
1346
1347 phy_param = (void *)cp->data;
1348
1349 hci_dev_lock(hdev);
1350
1351 hdev->le_scan_type = phy_param->type;
1352
1353 hci_dev_unlock(hdev);
1354}
1355
Johan Hedbergb9a63282014-03-25 10:51:52 +02001356static bool has_pending_adv_report(struct hci_dev *hdev)
1357{
1358 struct discovery_state *d = &hdev->discovery;
1359
1360 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1361}
1362
1363static void clear_pending_adv_report(struct hci_dev *hdev)
1364{
1365 struct discovery_state *d = &hdev->discovery;
1366
1367 bacpy(&d->last_adv_addr, BDADDR_ANY);
1368 d->last_adv_data_len = 0;
1369}
1370
1371static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001372 u8 bdaddr_type, s8 rssi, u32 flags,
1373 u8 *data, u8 len)
Johan Hedbergb9a63282014-03-25 10:51:52 +02001374{
1375 struct discovery_state *d = &hdev->discovery;
1376
Alain Michauda2ec9052020-07-27 20:48:55 +00001377 if (len > HCI_MAX_AD_LENGTH)
1378 return;
1379
Johan Hedbergb9a63282014-03-25 10:51:52 +02001380 bacpy(&d->last_adv_addr, bdaddr);
1381 d->last_adv_addr_type = bdaddr_type;
Johan Hedbergff5cd292014-03-25 14:40:52 +02001382 d->last_adv_rssi = rssi;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001383 d->last_adv_flags = flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02001384 memcpy(d->last_adv_data, data, len);
1385 d->last_adv_data_len = len;
1386}
1387
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301388static void le_set_scan_enable_complete(struct hci_dev *hdev, u8 enable)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001389{
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301390 hci_dev_lock(hdev);
1391
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301392 switch (enable) {
Andre Guedes76a388be2013-04-04 20:21:02 -03001393 case LE_SCAN_ENABLE:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001394 hci_dev_set_flag(hdev, HCI_LE_SCAN);
Johan Hedbergb9a63282014-03-25 10:51:52 +02001395 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1396 clear_pending_adv_report(hdev);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001397 break;
1398
Andre Guedes76a388be2013-04-04 20:21:02 -03001399 case LE_SCAN_DISABLE:
Johan Hedbergb9a63282014-03-25 10:51:52 +02001400 /* We do this here instead of when setting DISCOVERY_STOPPED
1401 * since the latter would potentially require waiting for
1402 * inquiry to stop too.
1403 */
1404 if (has_pending_adv_report(hdev)) {
1405 struct discovery_state *d = &hdev->discovery;
1406
1407 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001408 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001409 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001410 d->last_adv_data,
Johan Hedbergb9a63282014-03-25 10:51:52 +02001411 d->last_adv_data_len, NULL, 0);
1412 }
1413
Johan Hedberg317ac8c2014-02-28 20:26:12 +02001414 /* Cancel this timer so that we don't try to disable scanning
1415 * when it's already disabled.
1416 */
1417 cancel_delayed_work(&hdev->le_scan_disable);
1418
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001419 hci_dev_clear_flag(hdev, HCI_LE_SCAN);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001420
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001421 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1422 * interrupted scanning due to a connect request. Mark
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001423 * therefore discovery as stopped. If this was not
1424 * because of a connect request advertising might have
1425 * been disabled because of active scanning, so
1426 * re-enable it again if necessary.
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001427 */
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001428 if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001429 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001430 else if (!hci_dev_test_flag(hdev, HCI_LE_ADV) &&
Johan Hedberg34722272014-07-08 16:05:05 +03001431 hdev->discovery.state == DISCOVERY_FINDING)
Johan Hedbergf2252572015-11-18 12:49:20 +02001432 hci_req_reenable_advertising(hdev);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001433
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001434 break;
1435
1436 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001437 bt_dev_err(hdev, "use of reserved LE_Scan_Enable param %d",
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301438 enable);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001439 break;
Andre Guedes35815082011-05-26 16:23:53 -03001440 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301441
1442 hci_dev_unlock(hdev);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001443}
1444
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301445static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
1446 struct sk_buff *skb)
1447{
1448 struct hci_cp_le_set_scan_enable *cp;
1449 __u8 status = *((__u8 *) skb->data);
1450
1451 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1452
1453 if (status)
1454 return;
1455
1456 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1457 if (!cp)
1458 return;
1459
1460 le_set_scan_enable_complete(hdev, cp->enable);
1461}
1462
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301463static void hci_cc_le_set_ext_scan_enable(struct hci_dev *hdev,
1464 struct sk_buff *skb)
1465{
1466 struct hci_cp_le_set_ext_scan_enable *cp;
1467 __u8 status = *((__u8 *) skb->data);
1468
1469 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1470
1471 if (status)
1472 return;
1473
1474 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE);
1475 if (!cp)
1476 return;
1477
1478 le_set_scan_enable_complete(hdev, cp->enable);
1479}
1480
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05301481static void hci_cc_le_read_num_adv_sets(struct hci_dev *hdev,
1482 struct sk_buff *skb)
1483{
1484 struct hci_rp_le_read_num_supported_adv_sets *rp = (void *) skb->data;
1485
1486 BT_DBG("%s status 0x%2.2x No of Adv sets %u", hdev->name, rp->status,
1487 rp->num_of_sets);
1488
1489 if (rp->status)
1490 return;
1491
1492 hdev->le_num_of_adv_sets = rp->num_of_sets;
1493}
1494
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001495static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
1496 struct sk_buff *skb)
1497{
1498 struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
1499
1500 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1501
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001502 if (rp->status)
1503 return;
1504
1505 hdev->le_white_list_size = rp->size;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001506}
1507
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001508static void hci_cc_le_clear_white_list(struct hci_dev *hdev,
1509 struct sk_buff *skb)
1510{
1511 __u8 status = *((__u8 *) skb->data);
1512
1513 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1514
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001515 if (status)
1516 return;
1517
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001518 hci_bdaddr_list_clear(&hdev->le_white_list);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001519}
1520
1521static void hci_cc_le_add_to_white_list(struct hci_dev *hdev,
1522 struct sk_buff *skb)
1523{
1524 struct hci_cp_le_add_to_white_list *sent;
1525 __u8 status = *((__u8 *) skb->data);
1526
1527 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1528
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001529 if (status)
1530 return;
1531
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001532 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_WHITE_LIST);
1533 if (!sent)
1534 return;
1535
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001536 hci_bdaddr_list_add(&hdev->le_white_list, &sent->bdaddr,
1537 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001538}
1539
1540static void hci_cc_le_del_from_white_list(struct hci_dev *hdev,
1541 struct sk_buff *skb)
1542{
1543 struct hci_cp_le_del_from_white_list *sent;
1544 __u8 status = *((__u8 *) skb->data);
1545
1546 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1547
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001548 if (status)
1549 return;
1550
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001551 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_WHITE_LIST);
1552 if (!sent)
1553 return;
1554
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001555 hci_bdaddr_list_del(&hdev->le_white_list, &sent->bdaddr,
1556 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001557}
1558
Johan Hedberg9b008c02013-01-22 14:02:01 +02001559static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
1560 struct sk_buff *skb)
1561{
1562 struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
1563
1564 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1565
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001566 if (rp->status)
1567 return;
1568
1569 memcpy(hdev->le_states, rp->le_states, 8);
Johan Hedberg9b008c02013-01-22 14:02:01 +02001570}
1571
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01001572static void hci_cc_le_read_def_data_len(struct hci_dev *hdev,
1573 struct sk_buff *skb)
1574{
1575 struct hci_rp_le_read_def_data_len *rp = (void *) skb->data;
1576
1577 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1578
1579 if (rp->status)
1580 return;
1581
1582 hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
1583 hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
1584}
1585
1586static void hci_cc_le_write_def_data_len(struct hci_dev *hdev,
1587 struct sk_buff *skb)
1588{
1589 struct hci_cp_le_write_def_data_len *sent;
1590 __u8 status = *((__u8 *) skb->data);
1591
1592 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1593
1594 if (status)
1595 return;
1596
1597 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
1598 if (!sent)
1599 return;
1600
1601 hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
1602 hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
1603}
1604
Ankit Navikb950aa82018-08-17 07:29:19 +05301605static void hci_cc_le_add_to_resolv_list(struct hci_dev *hdev,
1606 struct sk_buff *skb)
1607{
1608 struct hci_cp_le_add_to_resolv_list *sent;
1609 __u8 status = *((__u8 *) skb->data);
1610
1611 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1612
1613 if (status)
1614 return;
1615
1616 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST);
1617 if (!sent)
1618 return;
1619
1620 hci_bdaddr_list_add_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
1621 sent->bdaddr_type, sent->peer_irk,
1622 sent->local_irk);
1623}
1624
1625static void hci_cc_le_del_from_resolv_list(struct hci_dev *hdev,
1626 struct sk_buff *skb)
1627{
1628 struct hci_cp_le_del_from_resolv_list *sent;
1629 __u8 status = *((__u8 *) skb->data);
1630
1631 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1632
1633 if (status)
1634 return;
1635
1636 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST);
1637 if (!sent)
1638 return;
1639
1640 hci_bdaddr_list_del_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
1641 sent->bdaddr_type);
1642}
1643
Ankit Navik545f2592018-06-29 12:13:20 +05301644static void hci_cc_le_clear_resolv_list(struct hci_dev *hdev,
1645 struct sk_buff *skb)
1646{
1647 __u8 status = *((__u8 *) skb->data);
1648
1649 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1650
1651 if (status)
1652 return;
1653
1654 hci_bdaddr_list_clear(&hdev->le_resolv_list);
1655}
1656
Ankit Navikcfdb0c22018-06-29 12:12:50 +05301657static void hci_cc_le_read_resolv_list_size(struct hci_dev *hdev,
1658 struct sk_buff *skb)
1659{
1660 struct hci_rp_le_read_resolv_list_size *rp = (void *) skb->data;
1661
1662 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1663
1664 if (rp->status)
1665 return;
1666
1667 hdev->le_resolv_list_size = rp->size;
1668}
1669
Ankit Navikaa12af72018-08-07 13:16:35 +05301670static void hci_cc_le_set_addr_resolution_enable(struct hci_dev *hdev,
1671 struct sk_buff *skb)
1672{
1673 __u8 *sent, status = *((__u8 *) skb->data);
1674
1675 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1676
1677 if (status)
1678 return;
1679
1680 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE);
1681 if (!sent)
1682 return;
1683
1684 hci_dev_lock(hdev);
1685
1686 if (*sent)
1687 hci_dev_set_flag(hdev, HCI_LL_RPA_RESOLUTION);
1688 else
1689 hci_dev_clear_flag(hdev, HCI_LL_RPA_RESOLUTION);
1690
1691 hci_dev_unlock(hdev);
1692}
1693
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01001694static void hci_cc_le_read_max_data_len(struct hci_dev *hdev,
1695 struct sk_buff *skb)
1696{
1697 struct hci_rp_le_read_max_data_len *rp = (void *) skb->data;
1698
1699 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1700
1701 if (rp->status)
1702 return;
1703
1704 hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
1705 hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
1706 hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
1707 hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
1708}
1709
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001710static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1711 struct sk_buff *skb)
Andre Guedesf9b49302011-06-30 19:20:53 -03001712{
Johan Hedberg06199cf2012-02-22 16:37:11 +02001713 struct hci_cp_write_le_host_supported *sent;
Andre Guedesf9b49302011-06-30 19:20:53 -03001714 __u8 status = *((__u8 *) skb->data);
1715
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001716 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesf9b49302011-06-30 19:20:53 -03001717
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001718 if (status)
1719 return;
1720
Johan Hedberg06199cf2012-02-22 16:37:11 +02001721 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001722 if (!sent)
Andre Guedesf9b49302011-06-30 19:20:53 -03001723 return;
1724
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301725 hci_dev_lock(hdev);
1726
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001727 if (sent->le) {
1728 hdev->features[1][0] |= LMP_HOST_LE;
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001729 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001730 } else {
1731 hdev->features[1][0] &= ~LMP_HOST_LE;
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001732 hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
1733 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001734 }
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001735
1736 if (sent->simul)
1737 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
1738 else
1739 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301740
1741 hci_dev_unlock(hdev);
Andre Guedesf9b49302011-06-30 19:20:53 -03001742}
1743
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02001744static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1745{
1746 struct hci_cp_le_set_adv_param *cp;
1747 u8 status = *((u8 *) skb->data);
1748
1749 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1750
1751 if (status)
1752 return;
1753
1754 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
1755 if (!cp)
1756 return;
1757
1758 hci_dev_lock(hdev);
1759 hdev->adv_addr_type = cp->own_address_type;
1760 hci_dev_unlock(hdev);
1761}
1762
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301763static void hci_cc_set_ext_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1764{
1765 struct hci_rp_le_set_ext_adv_params *rp = (void *) skb->data;
1766 struct hci_cp_le_set_ext_adv_params *cp;
1767 struct adv_info *adv_instance;
1768
1769 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1770
1771 if (rp->status)
1772 return;
1773
1774 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS);
1775 if (!cp)
1776 return;
1777
1778 hci_dev_lock(hdev);
1779 hdev->adv_addr_type = cp->own_addr_type;
Daniel Winkler25e70882021-04-05 16:33:04 -07001780 if (!cp->handle) {
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301781 /* Store in hdev for instance 0 */
1782 hdev->adv_tx_power = rp->tx_power;
1783 } else {
Daniel Winkler25e70882021-04-05 16:33:04 -07001784 adv_instance = hci_find_adv_instance(hdev, cp->handle);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301785 if (adv_instance)
1786 adv_instance->tx_power = rp->tx_power;
1787 }
Jaganath Kanakkasserya0fb3722018-07-19 17:09:42 +05301788 /* Update adv data as tx power is known now */
Daniel Winkler25e70882021-04-05 16:33:04 -07001789 hci_req_update_adv_data(hdev, cp->handle);
Daniel Winkler12410572020-12-03 12:12:49 -08001790
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301791 hci_dev_unlock(hdev);
1792}
1793
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02001794static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
1795{
1796 struct hci_rp_read_rssi *rp = (void *) skb->data;
1797 struct hci_conn *conn;
1798
1799 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1800
1801 if (rp->status)
1802 return;
1803
1804 hci_dev_lock(hdev);
1805
1806 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1807 if (conn)
1808 conn->rssi = rp->rssi;
1809
1810 hci_dev_unlock(hdev);
1811}
1812
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001813static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
1814{
1815 struct hci_cp_read_tx_power *sent;
1816 struct hci_rp_read_tx_power *rp = (void *) skb->data;
1817 struct hci_conn *conn;
1818
1819 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1820
1821 if (rp->status)
1822 return;
1823
1824 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
1825 if (!sent)
1826 return;
1827
1828 hci_dev_lock(hdev);
1829
1830 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001831 if (!conn)
1832 goto unlock;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001833
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001834 switch (sent->type) {
1835 case 0x00:
1836 conn->tx_power = rp->tx_power;
1837 break;
1838 case 0x01:
1839 conn->max_tx_power = rp->tx_power;
1840 break;
1841 }
1842
1843unlock:
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001844 hci_dev_unlock(hdev);
1845}
1846
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08001847static void hci_cc_write_ssp_debug_mode(struct hci_dev *hdev, struct sk_buff *skb)
1848{
1849 u8 status = *((u8 *) skb->data);
1850 u8 *mode;
1851
1852 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1853
1854 if (status)
1855 return;
1856
1857 mode = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE);
1858 if (mode)
1859 hdev->ssp_debug_mode = *mode;
1860}
1861
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001862static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001863{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001864 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001865
1866 if (status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001867 hci_conn_check_pending(hdev);
Johan Hedberg314b2382011-04-27 10:29:57 -04001868 return;
1869 }
1870
Andre Guedes89352e72011-11-04 14:16:53 -03001871 set_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001872}
1873
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001874static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001876 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001879 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001880
1881 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (!cp)
1883 return;
1884
1885 hci_dev_lock(hdev);
1886
1887 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1888
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001889 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 if (status) {
1892 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001893 if (status != 0x0c || conn->attempt > 2) {
1894 conn->state = BT_CLOSED;
Johan Hedberg539c4962015-02-18 14:53:57 +02001895 hci_connect_cfm(conn, status);
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001896 hci_conn_del(conn);
1897 } else
1898 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 }
1900 } else {
1901 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03001902 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr,
1903 HCI_ROLE_MASTER);
1904 if (!conn)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001905 bt_dev_err(hdev, "no memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907 }
1908
1909 hci_dev_unlock(hdev);
1910}
1911
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001912static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001914 struct hci_cp_add_sco *cp;
1915 struct hci_conn *acl, *sco;
1916 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001918 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001919
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001920 if (!status)
1921 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001923 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1924 if (!cp)
1925 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001927 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001929 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001930
1931 hci_dev_lock(hdev);
1932
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001933 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001934 if (acl) {
1935 sco = acl->link;
1936 if (sco) {
1937 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001938
Johan Hedberg539c4962015-02-18 14:53:57 +02001939 hci_connect_cfm(sco, status);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001940 hci_conn_del(sco);
1941 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001942 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001943
1944 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945}
1946
Marcel Holtmannf8558552008-07-14 20:13:49 +02001947static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1948{
1949 struct hci_cp_auth_requested *cp;
1950 struct hci_conn *conn;
1951
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001952 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001953
1954 if (!status)
1955 return;
1956
1957 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1958 if (!cp)
1959 return;
1960
1961 hci_dev_lock(hdev);
1962
1963 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1964 if (conn) {
1965 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02001966 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001967 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001968 }
1969 }
1970
1971 hci_dev_unlock(hdev);
1972}
1973
1974static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1975{
1976 struct hci_cp_set_conn_encrypt *cp;
1977 struct hci_conn *conn;
1978
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001979 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001980
1981 if (!status)
1982 return;
1983
1984 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1985 if (!cp)
1986 return;
1987
1988 hci_dev_lock(hdev);
1989
1990 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1991 if (conn) {
1992 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02001993 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001994 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001995 }
1996 }
1997
1998 hci_dev_unlock(hdev);
1999}
2000
Johan Hedberg127178d2010-11-18 22:22:29 +02002001static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002002 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02002003{
Johan Hedberg392599b2010-11-18 22:22:28 +02002004 if (conn->state != BT_CONFIG || !conn->out)
2005 return 0;
2006
Johan Hedberg765c2a92011-01-19 12:06:52 +05302007 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02002008 return 0;
2009
2010 /* Only request authentication for SSP connections or non-SSP
Johan Hedberg264b8b42014-01-08 16:40:39 +02002011 * devices with sec_level MEDIUM or HIGH or if MITM protection
2012 * is requested.
2013 */
Gustavo Padovan807deac2012-05-17 00:36:24 -03002014 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
Johan Hedberg7e3691e2014-05-30 14:45:19 +03002015 conn->pending_sec_level != BT_SECURITY_FIPS &&
Johan Hedberg264b8b42014-01-08 16:40:39 +02002016 conn->pending_sec_level != BT_SECURITY_HIGH &&
2017 conn->pending_sec_level != BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02002018 return 0;
2019
Johan Hedberg392599b2010-11-18 22:22:28 +02002020 return 1;
2021}
2022
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002023static int hci_resolve_name(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002024 struct inquiry_entry *e)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002025{
2026 struct hci_cp_remote_name_req cp;
2027
2028 memset(&cp, 0, sizeof(cp));
2029
2030 bacpy(&cp.bdaddr, &e->data.bdaddr);
2031 cp.pscan_rep_mode = e->data.pscan_rep_mode;
2032 cp.pscan_mode = e->data.pscan_mode;
2033 cp.clock_offset = e->data.clock_offset;
2034
2035 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2036}
2037
Johan Hedbergb644ba32012-01-17 21:48:47 +02002038static bool hci_resolve_next_name(struct hci_dev *hdev)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002039{
2040 struct discovery_state *discov = &hdev->discovery;
2041 struct inquiry_entry *e;
2042
Johan Hedbergb644ba32012-01-17 21:48:47 +02002043 if (list_empty(&discov->resolve))
2044 return false;
2045
2046 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
Ram Malovanyc8100892012-07-19 10:26:09 +03002047 if (!e)
2048 return false;
2049
Johan Hedbergb644ba32012-01-17 21:48:47 +02002050 if (hci_resolve_name(hdev, e) == 0) {
2051 e->name_state = NAME_PENDING;
2052 return true;
2053 }
2054
2055 return false;
2056}
2057
2058static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002059 bdaddr_t *bdaddr, u8 *name, u8 name_len)
Johan Hedbergb644ba32012-01-17 21:48:47 +02002060{
2061 struct discovery_state *discov = &hdev->discovery;
2062 struct inquiry_entry *e;
2063
Johan Hedberg60cb49d2014-11-11 11:33:24 +02002064 /* Update the mgmt connected state if necessary. Be careful with
2065 * conn objects that exist but are not (yet) connected however.
2066 * Only those in BT_CONFIG or BT_CONNECTED states can be
2067 * considered connected.
2068 */
2069 if (conn &&
2070 (conn->state == BT_CONFIG || conn->state == BT_CONNECTED) &&
Jaganath Kanakkasserycb77c3e2014-11-07 16:39:09 +05302071 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00002072 mgmt_device_connected(hdev, conn, 0, name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002073
2074 if (discov->state == DISCOVERY_STOPPED)
2075 return;
2076
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002077 if (discov->state == DISCOVERY_STOPPING)
2078 goto discov_complete;
2079
2080 if (discov->state != DISCOVERY_RESOLVING)
2081 return;
2082
2083 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
Ram Malovany7cc83802012-07-19 10:26:10 +03002084 /* If the device was not found in a list of found devices names of which
2085 * are pending. there is no need to continue resolving a next name as it
2086 * will be done upon receiving another Remote Name Request Complete
2087 * Event */
2088 if (!e)
2089 return;
2090
2091 list_del(&e->list);
2092 if (name) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002093 e->name_state = NAME_KNOWN;
Ram Malovany7cc83802012-07-19 10:26:10 +03002094 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
2095 e->data.rssi, name, name_len);
Ram Malovanyc3e7c0d2012-07-19 10:26:11 +03002096 } else {
2097 e->name_state = NAME_NOT_KNOWN;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002098 }
2099
Johan Hedbergb644ba32012-01-17 21:48:47 +02002100 if (hci_resolve_next_name(hdev))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002101 return;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002102
2103discov_complete:
2104 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2105}
2106
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002107static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
2108{
Johan Hedberg127178d2010-11-18 22:22:29 +02002109 struct hci_cp_remote_name_req *cp;
2110 struct hci_conn *conn;
2111
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002112 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02002113
2114 /* If successful wait for the name req complete event before
2115 * checking for the need to do authentication */
2116 if (!status)
2117 return;
2118
2119 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
2120 if (!cp)
2121 return;
2122
2123 hci_dev_lock(hdev);
2124
2125 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002126
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002127 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergb644ba32012-01-17 21:48:47 +02002128 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
2129
Johan Hedberg79c6c702011-04-28 11:28:55 -07002130 if (!conn)
2131 goto unlock;
2132
2133 if (!hci_outgoing_auth_needed(hdev, conn))
2134 goto unlock;
2135
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002136 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02002137 struct hci_cp_auth_requested auth_cp;
2138
Johan Hedberg977f8fc2014-07-17 15:35:39 +03002139 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2140
Johannes Bergc1f23a22013-10-07 18:19:16 +02002141 auth_cp.handle = __cpu_to_le16(conn->handle);
2142 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
2143 sizeof(auth_cp), &auth_cp);
Johan Hedberg127178d2010-11-18 22:22:29 +02002144 }
2145
Johan Hedberg79c6c702011-04-28 11:28:55 -07002146unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02002147 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002148}
2149
Marcel Holtmann769be972008-07-14 20:13:49 +02002150static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
2151{
2152 struct hci_cp_read_remote_features *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);
Marcel Holtmann769be972008-07-14 20:13:49 +02002156
2157 if (!status)
2158 return;
2159
2160 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
2161 if (!cp)
2162 return;
2163
2164 hci_dev_lock(hdev);
2165
2166 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2167 if (conn) {
2168 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002169 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002170 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002171 }
2172 }
2173
2174 hci_dev_unlock(hdev);
2175}
2176
2177static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
2178{
2179 struct hci_cp_read_remote_ext_features *cp;
2180 struct hci_conn *conn;
2181
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002182 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02002183
2184 if (!status)
2185 return;
2186
2187 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
2188 if (!cp)
2189 return;
2190
2191 hci_dev_lock(hdev);
2192
2193 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2194 if (conn) {
2195 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002196 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002197 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002198 }
2199 }
2200
2201 hci_dev_unlock(hdev);
2202}
2203
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002204static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2205{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002206 struct hci_cp_setup_sync_conn *cp;
2207 struct hci_conn *acl, *sco;
2208 __u16 handle;
2209
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002210 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002211
2212 if (!status)
2213 return;
2214
2215 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
2216 if (!cp)
2217 return;
2218
2219 handle = __le16_to_cpu(cp->handle);
2220
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002221 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002222
2223 hci_dev_lock(hdev);
2224
2225 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002226 if (acl) {
2227 sco = acl->link;
2228 if (sco) {
2229 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002230
Johan Hedberg539c4962015-02-18 14:53:57 +02002231 hci_connect_cfm(sco, status);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002232 hci_conn_del(sco);
2233 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002234 }
2235
2236 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002237}
2238
2239static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
2240{
2241 struct hci_cp_sniff_mode *cp;
2242 struct hci_conn *conn;
2243
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002244 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002245
2246 if (!status)
2247 return;
2248
2249 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
2250 if (!cp)
2251 return;
2252
2253 hci_dev_lock(hdev);
2254
2255 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002256 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002257 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002258
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002259 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002260 hci_sco_setup(conn, status);
2261 }
2262
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002263 hci_dev_unlock(hdev);
2264}
2265
2266static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
2267{
2268 struct hci_cp_exit_sniff_mode *cp;
2269 struct hci_conn *conn;
2270
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002271 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002272
2273 if (!status)
2274 return;
2275
2276 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
2277 if (!cp)
2278 return;
2279
2280 hci_dev_lock(hdev);
2281
2282 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002283 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002284 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002285
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002286 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002287 hci_sco_setup(conn, status);
2288 }
2289
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002290 hci_dev_unlock(hdev);
2291}
2292
Johan Hedberg88c3df12012-02-09 14:27:38 +02002293static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
2294{
2295 struct hci_cp_disconnect *cp;
2296 struct hci_conn *conn;
2297
2298 if (!status)
2299 return;
2300
2301 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
2302 if (!cp)
2303 return;
2304
2305 hci_dev_lock(hdev);
2306
2307 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Joseph Hwangb8d29052020-03-11 19:20:14 -07002308 if (conn) {
2309 u8 type = conn->type;
2310
Johan Hedberg88c3df12012-02-09 14:27:38 +02002311 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002312 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002313
Joseph Hwangb8d29052020-03-11 19:20:14 -07002314 /* If the disconnection failed for any reason, the upper layer
2315 * does not retry to disconnect in current implementation.
2316 * Hence, we need to do some basic cleanup here and re-enable
2317 * advertising if necessary.
2318 */
2319 hci_conn_del(conn);
2320 if (type == LE_LINK)
2321 hci_req_reenable_advertising(hdev);
2322 }
2323
Johan Hedberg88c3df12012-02-09 14:27:38 +02002324 hci_dev_unlock(hdev);
2325}
2326
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302327static void cs_le_create_conn(struct hci_dev *hdev, bdaddr_t *peer_addr,
2328 u8 peer_addr_type, u8 own_address_type,
2329 u8 filter_policy)
2330{
2331 struct hci_conn *conn;
2332
2333 conn = hci_conn_hash_lookup_le(hdev, peer_addr,
2334 peer_addr_type);
2335 if (!conn)
2336 return;
2337
Sathish Narasimmanb31bc002020-07-23 18:08:59 +05302338 /* When using controller based address resolution, then the new
2339 * address types 0x02 and 0x03 are used. These types need to be
2340 * converted back into either public address or random address type
2341 */
2342 if (use_ll_privacy(hdev) &&
2343 hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION)) {
2344 switch (own_address_type) {
2345 case ADDR_LE_DEV_PUBLIC_RESOLVED:
2346 own_address_type = ADDR_LE_DEV_PUBLIC;
2347 break;
2348 case ADDR_LE_DEV_RANDOM_RESOLVED:
2349 own_address_type = ADDR_LE_DEV_RANDOM;
2350 break;
2351 }
2352 }
2353
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302354 /* Store the initiator and responder address information which
2355 * is needed for SMP. These values will not change during the
2356 * lifetime of the connection.
2357 */
2358 conn->init_addr_type = own_address_type;
2359 if (own_address_type == ADDR_LE_DEV_RANDOM)
2360 bacpy(&conn->init_addr, &hdev->random_addr);
2361 else
2362 bacpy(&conn->init_addr, &hdev->bdaddr);
2363
2364 conn->resp_addr_type = peer_addr_type;
2365 bacpy(&conn->resp_addr, peer_addr);
2366
2367 /* We don't want the connection attempt to stick around
2368 * indefinitely since LE doesn't have a page timeout concept
2369 * like BR/EDR. Set a timer for any connection that doesn't use
2370 * the white list for connecting.
2371 */
2372 if (filter_policy == HCI_LE_USE_PEER_ADDR)
2373 queue_delayed_work(conn->hdev->workqueue,
2374 &conn->le_conn_timeout,
2375 conn->conn_timeout);
2376}
2377
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002378static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
2379{
2380 struct hci_cp_le_create_conn *cp;
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002381
2382 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2383
2384 /* All connection failure handling is taken care of by the
2385 * hci_le_conn_failed function which is triggered by the HCI
2386 * request completion callbacks used for connecting.
2387 */
2388 if (status)
2389 return;
2390
2391 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
2392 if (!cp)
2393 return;
2394
2395 hci_dev_lock(hdev);
2396
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302397 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2398 cp->own_address_type, cp->filter_policy);
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002399
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002400 hci_dev_unlock(hdev);
2401}
2402
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02002403static void hci_cs_le_ext_create_conn(struct hci_dev *hdev, u8 status)
2404{
2405 struct hci_cp_le_ext_create_conn *cp;
2406
2407 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2408
2409 /* All connection failure handling is taken care of by the
2410 * hci_le_conn_failed function which is triggered by the HCI
2411 * request completion callbacks used for connecting.
2412 */
2413 if (status)
2414 return;
2415
2416 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_EXT_CREATE_CONN);
2417 if (!cp)
2418 return;
2419
2420 hci_dev_lock(hdev);
2421
2422 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
2423 cp->own_addr_type, cp->filter_policy);
2424
2425 hci_dev_unlock(hdev);
2426}
2427
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07002428static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
2429{
2430 struct hci_cp_le_read_remote_features *cp;
2431 struct hci_conn *conn;
2432
2433 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2434
2435 if (!status)
2436 return;
2437
2438 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
2439 if (!cp)
2440 return;
2441
2442 hci_dev_lock(hdev);
2443
2444 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2445 if (conn) {
2446 if (conn->state == BT_CONFIG) {
2447 hci_connect_cfm(conn, status);
2448 hci_conn_drop(conn);
2449 }
2450 }
2451
2452 hci_dev_unlock(hdev);
2453}
2454
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02002455static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
2456{
2457 struct hci_cp_le_start_enc *cp;
2458 struct hci_conn *conn;
2459
2460 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2461
2462 if (!status)
2463 return;
2464
2465 hci_dev_lock(hdev);
2466
2467 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
2468 if (!cp)
2469 goto unlock;
2470
2471 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2472 if (!conn)
2473 goto unlock;
2474
2475 if (conn->state != BT_CONNECTED)
2476 goto unlock;
2477
2478 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2479 hci_conn_drop(conn);
2480
2481unlock:
2482 hci_dev_unlock(hdev);
2483}
2484
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01002485static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
2486{
2487 struct hci_cp_switch_role *cp;
2488 struct hci_conn *conn;
2489
2490 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2491
2492 if (!status)
2493 return;
2494
2495 cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
2496 if (!cp)
2497 return;
2498
2499 hci_dev_lock(hdev);
2500
2501 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2502 if (conn)
2503 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
2504
2505 hci_dev_unlock(hdev);
2506}
2507
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002508static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002509{
2510 __u8 status = *((__u8 *) skb->data);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002511 struct discovery_state *discov = &hdev->discovery;
2512 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002513
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002514 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002515
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002516 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03002517
2518 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
2519 return;
2520
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002521 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -03002522 wake_up_bit(&hdev->flags, HCI_INQUIRY);
2523
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002524 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002525 return;
2526
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002527 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002528
Andre Guedes343f9352012-02-17 20:39:37 -03002529 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002530 goto unlock;
2531
2532 if (list_empty(&discov->resolve)) {
Jakub Pawlowski07d23342015-03-17 09:04:14 -07002533 /* When BR/EDR inquiry is active and no LE scanning is in
2534 * progress, then change discovery state to indicate completion.
2535 *
2536 * When running LE scanning and BR/EDR inquiry simultaneously
2537 * and the LE scan already finished, then change the discovery
2538 * state to indicate completion.
2539 */
2540 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2541 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2542 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002543 goto unlock;
2544 }
2545
2546 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
2547 if (e && hci_resolve_name(hdev, e) == 0) {
2548 e->name_state = NAME_PENDING;
2549 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
2550 } else {
Jakub Pawlowski07d23342015-03-17 09:04:14 -07002551 /* When BR/EDR inquiry is active and no LE scanning is in
2552 * progress, then change discovery state to indicate completion.
2553 *
2554 * When running LE scanning and BR/EDR inquiry simultaneously
2555 * and the LE scan already finished, then change the discovery
2556 * state to indicate completion.
2557 */
2558 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
2559 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
2560 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002561 }
2562
2563unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002564 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002565}
2566
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002567static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002569 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002570 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 int num_rsp = *((__u8 *) skb->data);
2572
2573 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2574
Peilin Ye75bbd2e2020-07-10 17:39:18 -04002575 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002576 return;
2577
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002578 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03002579 return;
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002582
Johan Hedberge17acd42011-03-30 23:57:16 +03002583 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02002584 u32 flags;
Johan Hedberg31754052012-01-04 13:39:52 +02002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 bacpy(&data.bdaddr, &info->bdaddr);
2587 data.pscan_rep_mode = info->pscan_rep_mode;
2588 data.pscan_period_mode = info->pscan_period_mode;
2589 data.pscan_mode = info->pscan_mode;
2590 memcpy(data.dev_class, info->dev_class, 3);
2591 data.clock_offset = info->clock_offset;
Marcel Holtmannefb25132014-12-05 13:03:34 +01002592 data.rssi = HCI_RSSI_INVALID;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002593 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02002594
Marcel Holtmannaf589252014-07-01 14:11:20 +02002595 flags = hci_inquiry_cache_update(hdev, &data, false);
2596
Johan Hedberg48264f02011-11-09 13:58:58 +02002597 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannefb25132014-12-05 13:03:34 +01002598 info->dev_class, HCI_RSSI_INVALID,
2599 flags, NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002601
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 hci_dev_unlock(hdev);
2603}
2604
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002605static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002607 struct hci_ev_conn_complete *ev = (void *) skb->data;
2608 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002610 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002613
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002614 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02002615 if (!conn) {
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002616 /* Connection may not exist if auto-connected. Check the bredr
2617 * allowlist to see if this device is allowed to auto connect.
2618 * If link is an ACL type, create a connection class
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002619 * automatically.
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002620 *
2621 * Auto-connect will only occur if the event filter is
2622 * programmed with a given address. Right now, event filter is
2623 * only used during suspend.
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002624 */
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07002625 if (ev->link_type == ACL_LINK &&
2626 hci_bdaddr_list_lookup_with_flags(&hdev->whitelist,
2627 &ev->bdaddr,
2628 BDADDR_BREDR)) {
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002629 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2630 HCI_ROLE_SLAVE);
2631 if (!conn) {
2632 bt_dev_err(hdev, "no memory for new conn");
2633 goto unlock;
2634 }
Abhishek Pandit-Subedi2d186fc2020-03-19 17:07:13 -07002635 } else {
2636 if (ev->link_type != SCO_LINK)
2637 goto unlock;
2638
2639 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK,
2640 &ev->bdaddr);
2641 if (!conn)
2642 goto unlock;
2643
2644 conn->type = SCO_LINK;
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002645 }
Marcel Holtmann94992372009-04-19 19:30:03 +02002646 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002647
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002648 if (!ev->status) {
2649 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02002650
2651 if (conn->type == ACL_LINK) {
2652 conn->state = BT_CONFIG;
2653 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02002654
2655 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
2656 !hci_find_link_key(hdev, &ev->bdaddr))
2657 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2658 else
2659 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02002660 } else
2661 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002662
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01002663 hci_debugfs_create_conn(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002664 hci_conn_add_sysfs(conn);
2665
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002666 if (test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002667 set_bit(HCI_CONN_AUTH, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002668
2669 if (test_bit(HCI_ENCRYPT, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002670 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002671
2672 /* Get remote features */
2673 if (conn->type == ACL_LINK) {
2674 struct hci_cp_read_remote_features cp;
2675 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02002676 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002677 sizeof(cp), &cp);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002678
Johan Hedberg01b1cb82015-11-16 12:52:21 +02002679 hci_req_update_scan(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002680 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002681
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002682 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02002683 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002684 struct hci_cp_change_conn_ptype cp;
2685 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02002686 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002687 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
2688 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002689 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02002690 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002691 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02002692 if (conn->type == ACL_LINK)
Marcel Holtmann64c7b772014-02-18 14:22:20 -08002693 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002694 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02002695 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002696
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002697 if (conn->type == ACL_LINK)
2698 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002699
Marcel Holtmann769be972008-07-14 20:13:49 +02002700 if (ev->status) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002701 hci_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002702 hci_conn_del(conn);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02002703 } else if (ev->link_type == SCO_LINK) {
2704 switch (conn->setting & SCO_AIRMODE_MASK) {
2705 case SCO_AIRMODE_CVSD:
2706 if (hdev->notify)
2707 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
2708 break;
2709 }
2710
Johan Hedberg539c4962015-02-18 14:53:57 +02002711 hci_connect_cfm(conn, ev->status);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02002712 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002713
2714unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002716
2717 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
Johan Hedberg70c46422014-07-09 12:59:17 +03002720static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
2721{
2722 struct hci_cp_reject_conn_req cp;
2723
2724 bacpy(&cp.bdaddr, bdaddr);
2725 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
2726 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
2727}
2728
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002729static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002731 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 int mask = hdev->link_mode;
Johan Hedberg70c46422014-07-09 12:59:17 +03002733 struct inquiry_entry *ie;
2734 struct hci_conn *conn;
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01002735 __u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002737 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002738 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01002740 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
2741 &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Johan Hedberg70c46422014-07-09 12:59:17 +03002743 if (!(mask & HCI_LM_ACCEPT)) {
2744 hci_reject_conn(hdev, &ev->bdaddr);
2745 return;
2746 }
2747
Johan Hedberg46c4c942014-07-16 16:19:21 +03002748 if (hci_bdaddr_list_lookup(&hdev->blacklist, &ev->bdaddr,
2749 BDADDR_BREDR)) {
2750 hci_reject_conn(hdev, &ev->bdaddr);
2751 return;
2752 }
2753
Johan Hedberg6a8fc952014-12-24 20:43:11 +02002754 /* Require HCI_CONNECTABLE or a whitelist entry to accept the
2755 * connection. These features are only touched through mgmt so
2756 * only do the checks if HCI_MGMT is set.
2757 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002758 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
2759 !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
Abhishek Pandit-Subedi8baaa402020-06-17 16:39:08 +02002760 !hci_bdaddr_list_lookup_with_flags(&hdev->whitelist, &ev->bdaddr,
2761 BDADDR_BREDR)) {
2762 hci_reject_conn(hdev, &ev->bdaddr);
2763 return;
Johan Hedberg70c46422014-07-09 12:59:17 +03002764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
Johan Hedberg70c46422014-07-09 12:59:17 +03002766 /* Connection accepted */
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002767
Johan Hedberg70c46422014-07-09 12:59:17 +03002768 hci_dev_lock(hdev);
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02002769
Johan Hedberg70c46422014-07-09 12:59:17 +03002770 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2771 if (ie)
2772 memcpy(ie->data.dev_class, ev->dev_class, 3);
2773
2774 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
2775 &ev->bdaddr);
2776 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03002777 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2778 HCI_ROLE_SLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002780 bt_dev_err(hdev, "no memory for new connection");
Johan Hedberg70c46422014-07-09 12:59:17 +03002781 hci_dev_unlock(hdev);
2782 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 }
Johan Hedberg70c46422014-07-09 12:59:17 +03002784 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002785
Johan Hedberg70c46422014-07-09 12:59:17 +03002786 memcpy(conn->dev_class, ev->dev_class, 3);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002787
Johan Hedberg70c46422014-07-09 12:59:17 +03002788 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Johan Hedberg70c46422014-07-09 12:59:17 +03002790 if (ev->link_type == ACL_LINK ||
2791 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
2792 struct hci_cp_accept_conn_req cp;
2793 conn->state = BT_CONNECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 bacpy(&cp.bdaddr, &ev->bdaddr);
Johan Hedberg70c46422014-07-09 12:59:17 +03002796
2797 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
2798 cp.role = 0x00; /* Become master */
2799 else
2800 cp.role = 0x01; /* Remain slave */
2801
2802 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
2803 } else if (!(flags & HCI_PROTO_DEFER)) {
2804 struct hci_cp_accept_sync_conn_req cp;
2805 conn->state = BT_CONNECT;
2806
2807 bacpy(&cp.bdaddr, &ev->bdaddr);
2808 cp.pkt_type = cpu_to_le16(conn->pkt_type);
2809
2810 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
2811 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
2812 cp.max_latency = cpu_to_le16(0xffff);
2813 cp.content_format = cpu_to_le16(hdev->voice_setting);
2814 cp.retrans_effort = 0xff;
2815
2816 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
2817 &cp);
2818 } else {
2819 conn->state = BT_CONNECT2;
Johan Hedberg539c4962015-02-18 14:53:57 +02002820 hci_connect_cfm(conn, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 }
2822}
2823
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02002824static u8 hci_to_mgmt_reason(u8 err)
2825{
2826 switch (err) {
2827 case HCI_ERROR_CONNECTION_TIMEOUT:
2828 return MGMT_DEV_DISCONN_TIMEOUT;
2829 case HCI_ERROR_REMOTE_USER_TERM:
2830 case HCI_ERROR_REMOTE_LOW_RESOURCES:
2831 case HCI_ERROR_REMOTE_POWER_OFF:
2832 return MGMT_DEV_DISCONN_REMOTE;
2833 case HCI_ERROR_LOCAL_HOST_TERM:
2834 return MGMT_DEV_DISCONN_LOCAL_HOST;
2835 default:
2836 return MGMT_DEV_DISCONN_UNKNOWN;
2837 }
2838}
2839
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002840static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002842 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Szymon Janc160b9252016-07-12 02:12:16 +02002843 u8 reason;
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002844 struct hci_conn_params *params;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002845 struct hci_conn *conn;
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002846 bool mgmt_connected;
Andre Guedes38462202013-11-07 17:36:10 -03002847 u8 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002849 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 hci_dev_lock(hdev);
2852
Marcel Holtmann04837f62006-07-03 10:02:33 +02002853 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02002854 if (!conn)
2855 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002856
Andre Guedesabf54a52013-11-07 17:36:09 -03002857 if (ev->status) {
2858 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2859 conn->dst_type, ev->status);
2860 goto unlock;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02002861 }
Johan Hedbergf7520542011-01-20 12:34:39 +02002862
Andre Guedes38462202013-11-07 17:36:10 -03002863 conn->state = BT_CLOSED;
2864
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002865 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
Szymon Janc160b9252016-07-12 02:12:16 +02002866
2867 if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
2868 reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
2869 else
2870 reason = hci_to_mgmt_reason(ev->reason);
2871
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002872 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2873 reason, mgmt_connected);
Andre Guedesabf54a52013-11-07 17:36:09 -03002874
Johan Hedberg22f433d2014-08-01 11:13:32 +03002875 if (conn->type == ACL_LINK) {
2876 if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2877 hci_remove_link_key(hdev, &conn->dst);
2878
Johan Hedberg01b1cb82015-11-16 12:52:21 +02002879 hci_req_update_scan(hdev);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002880 }
Johan Hedberg22102462013-10-05 12:01:06 +02002881
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002882 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2883 if (params) {
2884 switch (params->auto_connect) {
2885 case HCI_AUTO_CONN_LINK_LOSS:
2886 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2887 break;
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05002888 fallthrough;
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002889
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02002890 case HCI_AUTO_CONN_DIRECT:
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002891 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg418025d2014-07-04 12:37:24 +03002892 list_del_init(&params->action);
2893 list_add(&params->action, &hdev->pend_le_conns);
2894 hci_update_background_scan(hdev);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002895 break;
2896
2897 default:
2898 break;
2899 }
2900 }
2901
Andre Guedes38462202013-11-07 17:36:10 -03002902 type = conn->type;
Johan Hedberg22102462013-10-05 12:01:06 +02002903
Johan Hedberg3a6d5762015-02-18 14:53:58 +02002904 hci_disconn_cfm(conn, ev->reason);
Andre Guedes38462202013-11-07 17:36:10 -03002905 hci_conn_del(conn);
2906
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07002907 /* The suspend notifier is waiting for all devices to disconnect so
2908 * clear the bit from pending tasks and inform the wait queue.
2909 */
2910 if (list_empty(&hdev->conn_hash.list) &&
2911 test_and_clear_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks)) {
2912 wake_up(&hdev->suspend_wait_q);
2913 }
2914
Andre Guedes38462202013-11-07 17:36:10 -03002915 /* Re-enable advertising if necessary, since it might
2916 * have been disabled by the connection. From the
2917 * HCI_LE_Set_Advertise_Enable command description in
2918 * the core specification (v4.0):
2919 * "The Controller shall continue advertising until the Host
2920 * issues an LE_Set_Advertise_Enable command with
2921 * Advertising_Enable set to 0x00 (Advertising is disabled)
2922 * or until a connection is created or until the Advertising
2923 * is timed out due to Directed Advertising."
2924 */
2925 if (type == LE_LINK)
Johan Hedbergf2252572015-11-18 12:49:20 +02002926 hci_req_reenable_advertising(hdev);
Johan Hedbergf7520542011-01-20 12:34:39 +02002927
2928unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 hci_dev_unlock(hdev);
2930}
2931
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002932static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002933{
2934 struct hci_ev_auth_complete *ev = (void *) skb->data;
2935 struct hci_conn *conn;
2936
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002937 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002938
2939 hci_dev_lock(hdev);
2940
2941 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002942 if (!conn)
2943 goto unlock;
2944
2945 if (!ev->status) {
Szymon Janc160b9252016-07-12 02:12:16 +02002946 clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
2947
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002948 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03002949 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002950 bt_dev_info(hdev, "re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03002951 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03002952 set_bit(HCI_CONN_AUTH, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002953 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03002954 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002955 } else {
Szymon Janc160b9252016-07-12 02:12:16 +02002956 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
2957 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
2958
Johan Hedberge1e930f2014-09-08 17:09:49 -07002959 mgmt_auth_failed(conn, ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002960 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002961
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002962 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
2963 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002964
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002965 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002966 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002967 struct hci_cp_set_conn_encrypt cp;
2968 cp.handle = ev->handle;
2969 cp.encrypt = 0x01;
2970 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002971 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002972 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002973 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02002974 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002975 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002976 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002977 } else {
2978 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002979
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002980 hci_conn_hold(conn);
2981 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02002982 hci_conn_drop(conn);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002983 }
2984
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002985 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002986 if (!ev->status) {
2987 struct hci_cp_set_conn_encrypt cp;
2988 cp.handle = ev->handle;
2989 cp.encrypt = 0x01;
2990 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002991 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002992 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002993 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07002994 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002995 }
2996 }
2997
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002998unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002999 hci_dev_unlock(hdev);
3000}
3001
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003002static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003003{
Johan Hedberg127178d2010-11-18 22:22:29 +02003004 struct hci_ev_remote_name *ev = (void *) skb->data;
3005 struct hci_conn *conn;
3006
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003007 BT_DBG("%s", hdev->name);
3008
3009 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02003010
3011 hci_dev_lock(hdev);
3012
3013 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003014
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003015 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergb644ba32012-01-17 21:48:47 +02003016 goto check_auth;
3017
3018 if (ev->status == 0)
3019 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003020 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02003021 else
3022 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
3023
3024check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07003025 if (!conn)
3026 goto unlock;
3027
3028 if (!hci_outgoing_auth_needed(hdev, conn))
3029 goto unlock;
3030
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003031 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003032 struct hci_cp_auth_requested cp;
Johan Hedberg977f8fc2014-07-17 15:35:39 +03003033
3034 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
3035
Johan Hedberg127178d2010-11-18 22:22:29 +02003036 cp.handle = __cpu_to_le16(conn->handle);
3037 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
3038 }
3039
Johan Hedberg79c6c702011-04-28 11:28:55 -07003040unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02003041 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003042}
3043
Johan Hedberg821f3762015-06-11 13:52:29 +03003044static void read_enc_key_size_complete(struct hci_dev *hdev, u8 status,
3045 u16 opcode, struct sk_buff *skb)
3046{
3047 const struct hci_rp_read_enc_key_size *rp;
3048 struct hci_conn *conn;
3049 u16 handle;
3050
3051 BT_DBG("%s status 0x%02x", hdev->name, status);
3052
3053 if (!skb || skb->len < sizeof(*rp)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003054 bt_dev_err(hdev, "invalid read key size response");
Johan Hedberg821f3762015-06-11 13:52:29 +03003055 return;
3056 }
3057
3058 rp = (void *)skb->data;
3059 handle = le16_to_cpu(rp->handle);
3060
3061 hci_dev_lock(hdev);
3062
3063 conn = hci_conn_hash_lookup_handle(hdev, handle);
3064 if (!conn)
3065 goto unlock;
3066
Alain Michaud32b50722020-03-25 14:48:34 +00003067 /* While unexpected, the read_enc_key_size command may fail. The most
3068 * secure approach is to then assume the key size is 0 to force a
3069 * disconnection.
Johan Hedberg821f3762015-06-11 13:52:29 +03003070 */
3071 if (rp->status) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003072 bt_dev_err(hdev, "failed to read key size for handle %u",
3073 handle);
Alain Michaud32b50722020-03-25 14:48:34 +00003074 conn->enc_key_size = 0;
Johan Hedberg821f3762015-06-11 13:52:29 +03003075 } else {
3076 conn->enc_key_size = rp->key_size;
3077 }
3078
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003079 hci_encrypt_cfm(conn, 0);
Johan Hedberg821f3762015-06-11 13:52:29 +03003080
3081unlock:
3082 hci_dev_unlock(hdev);
3083}
3084
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003085static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003086{
3087 struct hci_ev_encrypt_change *ev = (void *) skb->data;
3088 struct hci_conn *conn;
3089
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003090 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003091
3092 hci_dev_lock(hdev);
3093
3094 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003095 if (!conn)
3096 goto unlock;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003097
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003098 if (!ev->status) {
3099 if (ev->encrypt) {
3100 /* Encryption implies authentication */
Johan Hedberg4dae2792014-06-24 17:03:50 +03003101 set_bit(HCI_CONN_AUTH, &conn->flags);
3102 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003103 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003104
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08003105 /* P-256 authentication key implies FIPS */
3106 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
Johan Hedberg4dae2792014-06-24 17:03:50 +03003107 set_bit(HCI_CONN_FIPS, &conn->flags);
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08003108
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003109 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
3110 conn->type == LE_LINK)
3111 set_bit(HCI_CONN_AES_CCM, &conn->flags);
3112 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03003113 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003114 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
3115 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003116 }
3117
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07003118 /* We should disregard the current RPA and generate a new one
3119 * whenever the encryption procedure fails.
3120 */
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303121 if (ev->status && conn->type == LE_LINK) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07003122 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303123 hci_adv_instances_set_rpa_expired(hdev, true);
3124 }
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07003125
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003126 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3127
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07003128 /* Check link security requirements are met */
3129 if (!hci_conn_check_link_mode(conn))
3130 ev->status = HCI_ERROR_AUTH_FAILURE;
3131
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003132 if (ev->status && conn->state == BT_CONNECTED) {
Szymon Janc160b9252016-07-12 02:12:16 +02003133 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3134 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3135
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07003136 /* Notify upper layers so they can cleanup before
3137 * disconnecting.
3138 */
3139 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003140 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
3141 hci_conn_drop(conn);
3142 goto unlock;
3143 }
3144
Johan Hedberg821f3762015-06-11 13:52:29 +03003145 /* Try reading the encryption key size for encrypted ACL links */
3146 if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
3147 struct hci_cp_read_enc_key_size cp;
3148 struct hci_request req;
3149
3150 /* Only send HCI_Read_Encryption_Key_Size if the
3151 * controller really supports it. If it doesn't, assume
3152 * the default size (16).
3153 */
3154 if (!(hdev->commands[20] & 0x10)) {
3155 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3156 goto notify;
3157 }
3158
3159 hci_req_init(&req, hdev);
3160
3161 cp.handle = cpu_to_le16(conn->handle);
3162 hci_req_add(&req, HCI_OP_READ_ENC_KEY_SIZE, sizeof(cp), &cp);
3163
3164 if (hci_req_run_skb(&req, read_enc_key_size_complete)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003165 bt_dev_err(hdev, "sending read key size failed");
Johan Hedberg821f3762015-06-11 13:52:29 +03003166 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3167 goto notify;
3168 }
3169
3170 goto unlock;
3171 }
3172
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +05303173 /* Set the default Authenticated Payload Timeout after
3174 * an LE Link is established. As per Core Spec v5.0, Vol 2, Part B
3175 * Section 3.3, the HCI command WRITE_AUTH_PAYLOAD_TIMEOUT should be
3176 * sent when the link is active and Encryption is enabled, the conn
3177 * type can be either LE or ACL and controller must support LMP Ping.
3178 * Ensure for AES-CCM encryption as well.
3179 */
3180 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
3181 test_bit(HCI_CONN_AES_CCM, &conn->flags) &&
3182 ((conn->type == ACL_LINK && lmp_ping_capable(hdev)) ||
3183 (conn->type == LE_LINK && (hdev->le_features[0] & HCI_LE_PING)))) {
3184 struct hci_cp_write_auth_payload_to cp;
3185
3186 cp.handle = cpu_to_le16(conn->handle);
3187 cp.timeout = cpu_to_le16(hdev->auth_payload_timeout);
3188 hci_send_cmd(conn->hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO,
3189 sizeof(cp), &cp);
3190 }
3191
Johan Hedberg821f3762015-06-11 13:52:29 +03003192notify:
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003193 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003194
Gustavo Padovana7d77232012-05-13 03:20:07 -03003195unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003196 hci_dev_unlock(hdev);
3197}
3198
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003199static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
3200 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003201{
3202 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
3203 struct hci_conn *conn;
3204
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003205 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003206
3207 hci_dev_lock(hdev);
3208
3209 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3210 if (conn) {
3211 if (!ev->status)
Johan Hedberg4dae2792014-06-24 17:03:50 +03003212 set_bit(HCI_CONN_SECURE, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003213
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003214 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003215
3216 hci_key_change_cfm(conn, ev->status);
3217 }
3218
3219 hci_dev_unlock(hdev);
3220}
3221
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003222static void hci_remote_features_evt(struct hci_dev *hdev,
3223 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003224{
3225 struct hci_ev_remote_features *ev = (void *) skb->data;
3226 struct hci_conn *conn;
3227
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003228 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003229
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003230 hci_dev_lock(hdev);
3231
3232 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02003233 if (!conn)
3234 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02003235
Johan Hedbergccd556f2010-11-10 17:11:51 +02003236 if (!ev->status)
Johan Hedbergcad718e2013-04-17 15:00:51 +03003237 memcpy(conn->features[0], ev->features, 8);
Johan Hedbergccd556f2010-11-10 17:11:51 +02003238
3239 if (conn->state != BT_CONFIG)
3240 goto unlock;
3241
Szymon Jancac363cf2015-01-29 16:36:59 +01003242 if (!ev->status && lmp_ext_feat_capable(hdev) &&
3243 lmp_ext_feat_capable(conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003244 struct hci_cp_read_remote_ext_features cp;
3245 cp.handle = ev->handle;
3246 cp.page = 0x01;
3247 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003248 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02003249 goto unlock;
3250 }
3251
Johan Hedberg671267b2012-05-12 16:11:50 -03003252 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003253 struct hci_cp_remote_name_req cp;
3254 memset(&cp, 0, sizeof(cp));
3255 bacpy(&cp.bdaddr, &conn->dst);
3256 cp.pscan_rep_mode = 0x02;
3257 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003258 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00003259 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02003260
Johan Hedberg127178d2010-11-18 22:22:29 +02003261 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003262 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02003263 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003264 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02003265 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003266
Johan Hedbergccd556f2010-11-10 17:11:51 +02003267unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003268 hci_dev_unlock(hdev);
3269}
3270
Johan Hedberge62144872015-04-02 13:41:08 +03003271static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
3272 u16 *opcode, u8 *status,
3273 hci_req_complete_t *req_complete,
3274 hci_req_complete_skb_t *req_complete_skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003275{
3276 struct hci_ev_cmd_complete *ev = (void *) skb->data;
Johan Hedberge62144872015-04-02 13:41:08 +03003277
3278 *opcode = __le16_to_cpu(ev->opcode);
3279 *status = skb->data[sizeof(*ev)];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003280
3281 skb_pull(skb, sizeof(*ev));
3282
Johan Hedberge62144872015-04-02 13:41:08 +03003283 switch (*opcode) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003284 case HCI_OP_INQUIRY_CANCEL:
Sonny Sasakaadf1d692020-05-06 12:55:03 -07003285 hci_cc_inquiry_cancel(hdev, skb, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003286 break;
3287
Andre Guedes4d934832012-03-21 00:03:35 -03003288 case HCI_OP_PERIODIC_INQ:
3289 hci_cc_periodic_inq(hdev, skb);
3290 break;
3291
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003292 case HCI_OP_EXIT_PERIODIC_INQ:
3293 hci_cc_exit_periodic_inq(hdev, skb);
3294 break;
3295
3296 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
3297 hci_cc_remote_name_req_cancel(hdev, skb);
3298 break;
3299
3300 case HCI_OP_ROLE_DISCOVERY:
3301 hci_cc_role_discovery(hdev, skb);
3302 break;
3303
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02003304 case HCI_OP_READ_LINK_POLICY:
3305 hci_cc_read_link_policy(hdev, skb);
3306 break;
3307
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003308 case HCI_OP_WRITE_LINK_POLICY:
3309 hci_cc_write_link_policy(hdev, skb);
3310 break;
3311
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02003312 case HCI_OP_READ_DEF_LINK_POLICY:
3313 hci_cc_read_def_link_policy(hdev, skb);
3314 break;
3315
3316 case HCI_OP_WRITE_DEF_LINK_POLICY:
3317 hci_cc_write_def_link_policy(hdev, skb);
3318 break;
3319
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003320 case HCI_OP_RESET:
3321 hci_cc_reset(hdev, skb);
3322 break;
3323
Marcel Holtmannc2f0f972015-01-12 09:21:25 -08003324 case HCI_OP_READ_STORED_LINK_KEY:
3325 hci_cc_read_stored_link_key(hdev, skb);
3326 break;
3327
Marcel Holtmanna93661202015-01-12 09:21:28 -08003328 case HCI_OP_DELETE_STORED_LINK_KEY:
3329 hci_cc_delete_stored_link_key(hdev, skb);
3330 break;
3331
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003332 case HCI_OP_WRITE_LOCAL_NAME:
3333 hci_cc_write_local_name(hdev, skb);
3334 break;
3335
3336 case HCI_OP_READ_LOCAL_NAME:
3337 hci_cc_read_local_name(hdev, skb);
3338 break;
3339
3340 case HCI_OP_WRITE_AUTH_ENABLE:
3341 hci_cc_write_auth_enable(hdev, skb);
3342 break;
3343
3344 case HCI_OP_WRITE_ENCRYPT_MODE:
3345 hci_cc_write_encrypt_mode(hdev, skb);
3346 break;
3347
3348 case HCI_OP_WRITE_SCAN_ENABLE:
3349 hci_cc_write_scan_enable(hdev, skb);
3350 break;
3351
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -08003352 case HCI_OP_SET_EVENT_FLT:
3353 hci_cc_set_event_filter(hdev, skb);
3354 break;
3355
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003356 case HCI_OP_READ_CLASS_OF_DEV:
3357 hci_cc_read_class_of_dev(hdev, skb);
3358 break;
3359
3360 case HCI_OP_WRITE_CLASS_OF_DEV:
3361 hci_cc_write_class_of_dev(hdev, skb);
3362 break;
3363
3364 case HCI_OP_READ_VOICE_SETTING:
3365 hci_cc_read_voice_setting(hdev, skb);
3366 break;
3367
3368 case HCI_OP_WRITE_VOICE_SETTING:
3369 hci_cc_write_voice_setting(hdev, skb);
3370 break;
3371
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -07003372 case HCI_OP_READ_NUM_SUPPORTED_IAC:
3373 hci_cc_read_num_supported_iac(hdev, skb);
3374 break;
3375
Marcel Holtmann333140b2008-07-14 20:13:48 +02003376 case HCI_OP_WRITE_SSP_MODE:
3377 hci_cc_write_ssp_mode(hdev, skb);
3378 break;
3379
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08003380 case HCI_OP_WRITE_SC_SUPPORT:
3381 hci_cc_write_sc_support(hdev, skb);
3382 break;
3383
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +05303384 case HCI_OP_READ_AUTH_PAYLOAD_TO:
3385 hci_cc_read_auth_payload_timeout(hdev, skb);
3386 break;
3387
3388 case HCI_OP_WRITE_AUTH_PAYLOAD_TO:
3389 hci_cc_write_auth_payload_timeout(hdev, skb);
3390 break;
3391
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003392 case HCI_OP_READ_LOCAL_VERSION:
3393 hci_cc_read_local_version(hdev, skb);
3394 break;
3395
3396 case HCI_OP_READ_LOCAL_COMMANDS:
3397 hci_cc_read_local_commands(hdev, skb);
3398 break;
3399
3400 case HCI_OP_READ_LOCAL_FEATURES:
3401 hci_cc_read_local_features(hdev, skb);
3402 break;
3403
Andre Guedes971e3a42011-06-30 19:20:52 -03003404 case HCI_OP_READ_LOCAL_EXT_FEATURES:
3405 hci_cc_read_local_ext_features(hdev, skb);
3406 break;
3407
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003408 case HCI_OP_READ_BUFFER_SIZE:
3409 hci_cc_read_buffer_size(hdev, skb);
3410 break;
3411
3412 case HCI_OP_READ_BD_ADDR:
3413 hci_cc_read_bd_addr(hdev, skb);
3414 break;
3415
Marcel Holtmanna4790362020-04-03 21:44:04 +02003416 case HCI_OP_READ_LOCAL_PAIRING_OPTS:
3417 hci_cc_read_local_pairing_opts(hdev, skb);
3418 break;
3419
Johan Hedbergf332ec62013-03-15 17:07:11 -05003420 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
3421 hci_cc_read_page_scan_activity(hdev, skb);
3422 break;
3423
Johan Hedberg4a3ee762013-03-15 17:07:12 -05003424 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
3425 hci_cc_write_page_scan_activity(hdev, skb);
3426 break;
3427
Johan Hedbergf332ec62013-03-15 17:07:11 -05003428 case HCI_OP_READ_PAGE_SCAN_TYPE:
3429 hci_cc_read_page_scan_type(hdev, skb);
3430 break;
3431
Johan Hedberg4a3ee762013-03-15 17:07:12 -05003432 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
3433 hci_cc_write_page_scan_type(hdev, skb);
3434 break;
3435
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02003436 case HCI_OP_READ_DATA_BLOCK_SIZE:
3437 hci_cc_read_data_block_size(hdev, skb);
3438 break;
3439
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02003440 case HCI_OP_READ_FLOW_CONTROL_MODE:
3441 hci_cc_read_flow_control_mode(hdev, skb);
3442 break;
3443
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03003444 case HCI_OP_READ_LOCAL_AMP_INFO:
3445 hci_cc_read_local_amp_info(hdev, skb);
3446 break;
3447
Johan Hedberg33f35722014-06-28 17:54:06 +03003448 case HCI_OP_READ_CLOCK:
3449 hci_cc_read_clock(hdev, skb);
3450 break;
3451
Johan Hedbergd5859e22011-01-25 01:19:58 +02003452 case HCI_OP_READ_INQ_RSP_TX_POWER:
3453 hci_cc_read_inq_rsp_tx_power(hdev, skb);
3454 break;
3455
Alain Michaud00bce3f2020-03-05 16:14:59 +00003456 case HCI_OP_READ_DEF_ERR_DATA_REPORTING:
3457 hci_cc_read_def_err_data_reporting(hdev, skb);
3458 break;
3459
3460 case HCI_OP_WRITE_DEF_ERR_DATA_REPORTING:
3461 hci_cc_write_def_err_data_reporting(hdev, skb);
3462 break;
3463
Johan Hedberg980e1a52011-01-22 06:10:07 +02003464 case HCI_OP_PIN_CODE_REPLY:
3465 hci_cc_pin_code_reply(hdev, skb);
3466 break;
3467
3468 case HCI_OP_PIN_CODE_NEG_REPLY:
3469 hci_cc_pin_code_neg_reply(hdev, skb);
3470 break;
3471
Szymon Jancc35938b2011-03-22 13:12:21 +01003472 case HCI_OP_READ_LOCAL_OOB_DATA:
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003473 hci_cc_read_local_oob_data(hdev, skb);
3474 break;
3475
3476 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
3477 hci_cc_read_local_oob_ext_data(hdev, skb);
Szymon Jancc35938b2011-03-22 13:12:21 +01003478 break;
3479
Ville Tervo6ed58ec2011-02-10 22:38:48 -03003480 case HCI_OP_LE_READ_BUFFER_SIZE:
3481 hci_cc_le_read_buffer_size(hdev, skb);
3482 break;
3483
Johan Hedberg60e77322013-01-22 14:01:59 +02003484 case HCI_OP_LE_READ_LOCAL_FEATURES:
3485 hci_cc_le_read_local_features(hdev, skb);
3486 break;
3487
Johan Hedberg8fa19092012-10-19 20:57:49 +03003488 case HCI_OP_LE_READ_ADV_TX_POWER:
3489 hci_cc_le_read_adv_tx_power(hdev, skb);
3490 break;
3491
Johan Hedberga5c29682011-02-19 12:05:57 -03003492 case HCI_OP_USER_CONFIRM_REPLY:
3493 hci_cc_user_confirm_reply(hdev, skb);
3494 break;
3495
3496 case HCI_OP_USER_CONFIRM_NEG_REPLY:
3497 hci_cc_user_confirm_neg_reply(hdev, skb);
3498 break;
3499
Brian Gix1143d452011-11-23 08:28:34 -08003500 case HCI_OP_USER_PASSKEY_REPLY:
3501 hci_cc_user_passkey_reply(hdev, skb);
3502 break;
3503
3504 case HCI_OP_USER_PASSKEY_NEG_REPLY:
3505 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02003506 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09003507
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08003508 case HCI_OP_LE_SET_RANDOM_ADDR:
3509 hci_cc_le_set_random_addr(hdev, skb);
3510 break;
3511
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01003512 case HCI_OP_LE_SET_ADV_ENABLE:
3513 hci_cc_le_set_adv_enable(hdev, skb);
3514 break;
3515
Marcel Holtmann533553f2014-03-21 12:18:10 -07003516 case HCI_OP_LE_SET_SCAN_PARAM:
3517 hci_cc_le_set_scan_param(hdev, skb);
3518 break;
3519
Andre Guedeseb9d91f2011-05-26 16:23:52 -03003520 case HCI_OP_LE_SET_SCAN_ENABLE:
3521 hci_cc_le_set_scan_enable(hdev, skb);
3522 break;
3523
Johan Hedbergcf1d0812013-01-22 14:02:00 +02003524 case HCI_OP_LE_READ_WHITE_LIST_SIZE:
3525 hci_cc_le_read_white_list_size(hdev, skb);
3526 break;
3527
Marcel Holtmann0f36b582014-02-27 20:37:31 -08003528 case HCI_OP_LE_CLEAR_WHITE_LIST:
3529 hci_cc_le_clear_white_list(hdev, skb);
3530 break;
3531
3532 case HCI_OP_LE_ADD_TO_WHITE_LIST:
3533 hci_cc_le_add_to_white_list(hdev, skb);
3534 break;
3535
3536 case HCI_OP_LE_DEL_FROM_WHITE_LIST:
3537 hci_cc_le_del_from_white_list(hdev, skb);
3538 break;
3539
Johan Hedberg9b008c02013-01-22 14:02:01 +02003540 case HCI_OP_LE_READ_SUPPORTED_STATES:
3541 hci_cc_le_read_supported_states(hdev, skb);
3542 break;
3543
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01003544 case HCI_OP_LE_READ_DEF_DATA_LEN:
3545 hci_cc_le_read_def_data_len(hdev, skb);
3546 break;
3547
3548 case HCI_OP_LE_WRITE_DEF_DATA_LEN:
3549 hci_cc_le_write_def_data_len(hdev, skb);
3550 break;
3551
Ankit Navikb950aa82018-08-17 07:29:19 +05303552 case HCI_OP_LE_ADD_TO_RESOLV_LIST:
3553 hci_cc_le_add_to_resolv_list(hdev, skb);
3554 break;
3555
3556 case HCI_OP_LE_DEL_FROM_RESOLV_LIST:
3557 hci_cc_le_del_from_resolv_list(hdev, skb);
3558 break;
3559
Ankit Navik545f2592018-06-29 12:13:20 +05303560 case HCI_OP_LE_CLEAR_RESOLV_LIST:
3561 hci_cc_le_clear_resolv_list(hdev, skb);
3562 break;
3563
Ankit Navikcfdb0c22018-06-29 12:12:50 +05303564 case HCI_OP_LE_READ_RESOLV_LIST_SIZE:
3565 hci_cc_le_read_resolv_list_size(hdev, skb);
3566 break;
3567
Ankit Navikaa12af72018-08-07 13:16:35 +05303568 case HCI_OP_LE_SET_ADDR_RESOLV_ENABLE:
3569 hci_cc_le_set_addr_resolution_enable(hdev, skb);
3570 break;
3571
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01003572 case HCI_OP_LE_READ_MAX_DATA_LEN:
3573 hci_cc_le_read_max_data_len(hdev, skb);
3574 break;
3575
Andre Guedesf9b49302011-06-30 19:20:53 -03003576 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
3577 hci_cc_write_le_host_supported(hdev, skb);
3578 break;
3579
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02003580 case HCI_OP_LE_SET_ADV_PARAM:
3581 hci_cc_set_adv_param(hdev, skb);
3582 break;
3583
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02003584 case HCI_OP_READ_RSSI:
3585 hci_cc_read_rssi(hdev, skb);
3586 break;
3587
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02003588 case HCI_OP_READ_TX_POWER:
3589 hci_cc_read_tx_power(hdev, skb);
3590 break;
3591
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08003592 case HCI_OP_WRITE_SSP_DEBUG_MODE:
3593 hci_cc_write_ssp_debug_mode(hdev, skb);
3594 break;
3595
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05303596 case HCI_OP_LE_SET_EXT_SCAN_PARAMS:
3597 hci_cc_le_set_ext_scan_param(hdev, skb);
3598 break;
3599
3600 case HCI_OP_LE_SET_EXT_SCAN_ENABLE:
3601 hci_cc_le_set_ext_scan_enable(hdev, skb);
3602 break;
3603
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05303604 case HCI_OP_LE_SET_DEFAULT_PHY:
3605 hci_cc_le_set_default_phy(hdev, skb);
3606 break;
3607
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05303608 case HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS:
3609 hci_cc_le_read_num_adv_sets(hdev, skb);
3610 break;
3611
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05303612 case HCI_OP_LE_SET_EXT_ADV_PARAMS:
3613 hci_cc_set_ext_adv_param(hdev, skb);
3614 break;
3615
3616 case HCI_OP_LE_SET_EXT_ADV_ENABLE:
3617 hci_cc_le_set_ext_adv_enable(hdev, skb);
3618 break;
3619
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303620 case HCI_OP_LE_SET_ADV_SET_RAND_ADDR:
3621 hci_cc_le_set_adv_set_random_addr(hdev, skb);
3622 break;
3623
Daniel Winkler7c395ea2020-12-03 12:12:51 -08003624 case HCI_OP_LE_READ_TRANSMIT_POWER:
3625 hci_cc_le_read_transmit_power(hdev, skb);
3626 break;
3627
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003628 default:
Johan Hedberge62144872015-04-02 13:41:08 +03003629 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003630 break;
3631 }
3632
Johan Hedberge62144872015-04-02 13:41:08 +03003633 if (*opcode != HCI_OP_NOP)
Marcel Holtmann65cc2b42014-06-16 12:30:56 +02003634 cancel_delayed_work(&hdev->cmd_timer);
Ville Tervo6bd32322011-02-16 16:32:41 +02003635
Johan Hedberg600b2152015-03-28 11:17:36 +02003636 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags))
3637 atomic_set(&hdev->cmd_cnt, 1);
3638
Johan Hedberge62144872015-04-02 13:41:08 +03003639 hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
3640 req_complete_skb);
Johan Hedberg9238f362013-03-05 20:37:48 +02003641
João Paulo Rechi Vitaf80c5da2019-05-02 10:01:52 +08003642 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
3643 bt_dev_err(hdev,
3644 "unexpected event for opcode 0x%4.4x", *opcode);
3645 return;
3646 }
3647
Johan Hedberg600b2152015-03-28 11:17:36 +02003648 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3649 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003650}
3651
Johan Hedberge62144872015-04-02 13:41:08 +03003652static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb,
3653 u16 *opcode, u8 *status,
3654 hci_req_complete_t *req_complete,
3655 hci_req_complete_skb_t *req_complete_skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003656{
3657 struct hci_ev_cmd_status *ev = (void *) skb->data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003658
3659 skb_pull(skb, sizeof(*ev));
3660
Johan Hedberge62144872015-04-02 13:41:08 +03003661 *opcode = __le16_to_cpu(ev->opcode);
3662 *status = ev->status;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003663
Johan Hedberge62144872015-04-02 13:41:08 +03003664 switch (*opcode) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003665 case HCI_OP_INQUIRY:
3666 hci_cs_inquiry(hdev, ev->status);
3667 break;
3668
3669 case HCI_OP_CREATE_CONN:
3670 hci_cs_create_conn(hdev, ev->status);
3671 break;
3672
Kuba Pawlak9645c762014-11-06 19:36:53 +01003673 case HCI_OP_DISCONNECT:
3674 hci_cs_disconnect(hdev, ev->status);
3675 break;
3676
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003677 case HCI_OP_ADD_SCO:
3678 hci_cs_add_sco(hdev, ev->status);
3679 break;
3680
Marcel Holtmannf8558552008-07-14 20:13:49 +02003681 case HCI_OP_AUTH_REQUESTED:
3682 hci_cs_auth_requested(hdev, ev->status);
3683 break;
3684
3685 case HCI_OP_SET_CONN_ENCRYPT:
3686 hci_cs_set_conn_encrypt(hdev, ev->status);
3687 break;
3688
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003689 case HCI_OP_REMOTE_NAME_REQ:
3690 hci_cs_remote_name_req(hdev, ev->status);
3691 break;
3692
Marcel Holtmann769be972008-07-14 20:13:49 +02003693 case HCI_OP_READ_REMOTE_FEATURES:
3694 hci_cs_read_remote_features(hdev, ev->status);
3695 break;
3696
3697 case HCI_OP_READ_REMOTE_EXT_FEATURES:
3698 hci_cs_read_remote_ext_features(hdev, ev->status);
3699 break;
3700
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003701 case HCI_OP_SETUP_SYNC_CONN:
3702 hci_cs_setup_sync_conn(hdev, ev->status);
3703 break;
3704
3705 case HCI_OP_SNIFF_MODE:
3706 hci_cs_sniff_mode(hdev, ev->status);
3707 break;
3708
3709 case HCI_OP_EXIT_SNIFF_MODE:
3710 hci_cs_exit_sniff_mode(hdev, ev->status);
3711 break;
3712
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01003713 case HCI_OP_SWITCH_ROLE:
3714 hci_cs_switch_role(hdev, ev->status);
3715 break;
3716
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02003717 case HCI_OP_LE_CREATE_CONN:
3718 hci_cs_le_create_conn(hdev, ev->status);
3719 break;
3720
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07003721 case HCI_OP_LE_READ_REMOTE_FEATURES:
3722 hci_cs_le_read_remote_features(hdev, ev->status);
3723 break;
3724
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02003725 case HCI_OP_LE_START_ENC:
3726 hci_cs_le_start_enc(hdev, ev->status);
3727 break;
3728
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02003729 case HCI_OP_LE_EXT_CREATE_CONN:
3730 hci_cs_le_ext_create_conn(hdev, ev->status);
3731 break;
3732
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003733 default:
Johan Hedberge62144872015-04-02 13:41:08 +03003734 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003735 break;
3736 }
3737
Johan Hedberge62144872015-04-02 13:41:08 +03003738 if (*opcode != HCI_OP_NOP)
Marcel Holtmann65cc2b42014-06-16 12:30:56 +02003739 cancel_delayed_work(&hdev->cmd_timer);
Ville Tervo6bd32322011-02-16 16:32:41 +02003740
Johan Hedberg600b2152015-03-28 11:17:36 +02003741 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags))
3742 atomic_set(&hdev->cmd_cnt, 1);
3743
Johan Hedberg444c6dd2015-04-02 13:41:07 +03003744 /* Indicate request completion if the command failed. Also, if
3745 * we're not waiting for a special event and we get a success
3746 * command status we should try to flag the request as completed
3747 * (since for this kind of commands there will not be a command
3748 * complete event).
3749 */
Johan Hedberg02350a72013-04-03 21:50:29 +03003750 if (ev->status ||
Marcel Holtmann242c0eb2015-10-25 22:45:53 +01003751 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->hci.req_event))
Johan Hedberge62144872015-04-02 13:41:08 +03003752 hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
3753 req_complete_skb);
Johan Hedberg9238f362013-03-05 20:37:48 +02003754
João Paulo Rechi Vitaf80c5da2019-05-02 10:01:52 +08003755 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
3756 bt_dev_err(hdev,
3757 "unexpected event for opcode 0x%4.4x", *opcode);
3758 return;
3759 }
3760
Johan Hedberg600b2152015-03-28 11:17:36 +02003761 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
3762 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003763}
3764
Marcel Holtmann24dfa342014-11-02 02:56:41 +01003765static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
3766{
3767 struct hci_ev_hardware_error *ev = (void *) skb->data;
3768
Marcel Holtmannc7741d12015-01-28 11:09:55 -08003769 hdev->hw_error_code = ev->code;
3770
3771 queue_work(hdev->req_workqueue, &hdev->error_reset);
Marcel Holtmann24dfa342014-11-02 02:56:41 +01003772}
3773
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003774static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003775{
3776 struct hci_ev_role_change *ev = (void *) skb->data;
3777 struct hci_conn *conn;
3778
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003779 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003780
3781 hci_dev_lock(hdev);
3782
3783 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3784 if (conn) {
Johan Hedberg40bef302014-07-16 11:42:27 +03003785 if (!ev->status)
3786 conn->role = ev->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003787
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003788 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003789
3790 hci_role_switch_cfm(conn, ev->status, ev->role);
3791 }
3792
3793 hci_dev_unlock(hdev);
3794}
3795
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003796static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003798 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 int i;
3800
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02003801 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003802 bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02003803 return;
3804 }
3805
Gustavo A. R. Silva16e18342019-02-07 18:40:33 -06003806 if (skb->len < sizeof(*ev) ||
3807 skb->len < struct_size(ev, handles, ev->num_hndl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 BT_DBG("%s bad parameters", hdev->name);
3809 return;
3810 }
3811
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02003812 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
3813
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003814 for (i = 0; i < ev->num_hndl; i++) {
3815 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 struct hci_conn *conn;
3817 __u16 handle, count;
3818
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003819 handle = __le16_to_cpu(info->handle);
3820 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
3822 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003823 if (!conn)
3824 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003826 conn->sent -= count;
3827
3828 switch (conn->type) {
3829 case ACL_LINK:
3830 hdev->acl_cnt += count;
3831 if (hdev->acl_cnt > hdev->acl_pkts)
3832 hdev->acl_cnt = hdev->acl_pkts;
3833 break;
3834
3835 case LE_LINK:
3836 if (hdev->le_pkts) {
3837 hdev->le_cnt += count;
3838 if (hdev->le_cnt > hdev->le_pkts)
3839 hdev->le_cnt = hdev->le_pkts;
3840 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02003841 hdev->acl_cnt += count;
3842 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 hdev->acl_cnt = hdev->acl_pkts;
3844 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003845 break;
3846
3847 case SCO_LINK:
3848 hdev->sco_cnt += count;
3849 if (hdev->sco_cnt > hdev->sco_pkts)
3850 hdev->sco_cnt = hdev->sco_pkts;
3851 break;
3852
3853 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003854 bt_dev_err(hdev, "unknown type %d conn %p",
3855 conn->type, conn);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003856 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 }
3858 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003859
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02003860 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861}
3862
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003863static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
3864 __u16 handle)
3865{
3866 struct hci_chan *chan;
3867
3868 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02003869 case HCI_PRIMARY:
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003870 return hci_conn_hash_lookup_handle(hdev, handle);
3871 case HCI_AMP:
3872 chan = hci_chan_lookup_handle(hdev, handle);
3873 if (chan)
3874 return chan->conn;
3875 break;
3876 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003877 bt_dev_err(hdev, "unknown dev_type %d", hdev->dev_type);
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003878 break;
3879 }
3880
3881 return NULL;
3882}
3883
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003884static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003885{
3886 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
3887 int i;
3888
3889 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003890 bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003891 return;
3892 }
3893
Gustavo A. R. Silva16e18342019-02-07 18:40:33 -06003894 if (skb->len < sizeof(*ev) ||
3895 skb->len < struct_size(ev, handles, ev->num_hndl)) {
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003896 BT_DBG("%s bad parameters", hdev->name);
3897 return;
3898 }
3899
3900 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003901 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003902
3903 for (i = 0; i < ev->num_hndl; i++) {
3904 struct hci_comp_blocks_info *info = &ev->handles[i];
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003905 struct hci_conn *conn = NULL;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003906 __u16 handle, block_count;
3907
3908 handle = __le16_to_cpu(info->handle);
3909 block_count = __le16_to_cpu(info->blocks);
3910
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003911 conn = __hci_conn_lookup_handle(hdev, handle);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003912 if (!conn)
3913 continue;
3914
3915 conn->sent -= block_count;
3916
3917 switch (conn->type) {
3918 case ACL_LINK:
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03003919 case AMP_LINK:
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003920 hdev->block_cnt += block_count;
3921 if (hdev->block_cnt > hdev->num_blocks)
3922 hdev->block_cnt = hdev->num_blocks;
3923 break;
3924
3925 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003926 bt_dev_err(hdev, "unknown type %d conn %p",
3927 conn->type, conn);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003928 break;
3929 }
3930 }
3931
3932 queue_work(hdev->workqueue, &hdev->tx_work);
3933}
3934
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003935static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003937 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003938 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003940 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941
3942 hci_dev_lock(hdev);
3943
Marcel Holtmann04837f62006-07-03 10:02:33 +02003944 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3945 if (conn) {
3946 conn->mode = ev->mode;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003947
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003948 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
3949 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02003950 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02003951 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003952 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02003953 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003954 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003955
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003956 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003957 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003958 }
3959
3960 hci_dev_unlock(hdev);
3961}
3962
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003963static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003965 struct hci_ev_pin_code_req *ev = (void *) skb->data;
3966 struct hci_conn *conn;
3967
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003968 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003969
3970 hci_dev_lock(hdev);
3971
3972 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003973 if (!conn)
3974 goto unlock;
3975
3976 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003977 hci_conn_hold(conn);
3978 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003979 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003980 }
3981
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003982 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedberg2f407f02014-07-17 15:35:40 +03003983 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
Johan Hedberg03b555e2011-01-04 15:40:05 +02003984 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003985 sizeof(ev->bdaddr), &ev->bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003986 } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003987 u8 secure;
3988
3989 if (conn->pending_sec_level == BT_SECURITY_HIGH)
3990 secure = 1;
3991 else
3992 secure = 0;
3993
Johan Hedberg744cf192011-11-08 20:40:14 +02003994 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003995 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003996
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003997unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003998 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999}
4000
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004001static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
4002{
4003 if (key_type == HCI_LK_CHANGED_COMBINATION)
4004 return;
4005
4006 conn->pin_length = pin_len;
4007 conn->key_type = key_type;
4008
4009 switch (key_type) {
4010 case HCI_LK_LOCAL_UNIT:
4011 case HCI_LK_REMOTE_UNIT:
4012 case HCI_LK_DEBUG_COMBINATION:
4013 return;
4014 case HCI_LK_COMBINATION:
4015 if (pin_len == 16)
4016 conn->pending_sec_level = BT_SECURITY_HIGH;
4017 else
4018 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4019 break;
4020 case HCI_LK_UNAUTH_COMBINATION_P192:
4021 case HCI_LK_UNAUTH_COMBINATION_P256:
4022 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4023 break;
4024 case HCI_LK_AUTH_COMBINATION_P192:
4025 conn->pending_sec_level = BT_SECURITY_HIGH;
4026 break;
4027 case HCI_LK_AUTH_COMBINATION_P256:
4028 conn->pending_sec_level = BT_SECURITY_FIPS;
4029 break;
4030 }
4031}
4032
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004033static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004035 struct hci_ev_link_key_req *ev = (void *) skb->data;
4036 struct hci_cp_link_key_reply cp;
4037 struct hci_conn *conn;
4038 struct link_key *key;
4039
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004040 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004041
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004042 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004043 return;
4044
4045 hci_dev_lock(hdev);
4046
4047 key = hci_find_link_key(hdev, &ev->bdaddr);
4048 if (!key) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03004049 BT_DBG("%s link key not found for %pMR", hdev->name,
4050 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004051 goto not_found;
4052 }
4053
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03004054 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
4055 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004056
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004057 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004058 if (conn) {
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03004059 clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
4060
Marcel Holtmann66138ce2014-01-10 02:07:20 -08004061 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
4062 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03004063 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004064 BT_DBG("%s ignoring unauthenticated key", hdev->name);
4065 goto not_found;
4066 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004067
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004068 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Johan Hedbergf3fb0b52014-06-02 10:12:44 +03004069 (conn->pending_sec_level == BT_SECURITY_HIGH ||
4070 conn->pending_sec_level == BT_SECURITY_FIPS)) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03004071 BT_DBG("%s ignoring key unauthenticated for high security",
4072 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004073 goto not_found;
4074 }
4075
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004076 conn_set_key(conn, key->type, key->pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004077 }
4078
4079 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03004080 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004081
4082 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
4083
4084 hci_dev_unlock(hdev);
4085
4086 return;
4087
4088not_found:
4089 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
4090 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091}
4092
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004093static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004095 struct hci_ev_link_key_notify *ev = (void *) skb->data;
4096 struct hci_conn *conn;
Johan Hedberg7652ff62014-06-24 13:15:49 +03004097 struct link_key *key;
4098 bool persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004099 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004100
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004101 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004102
4103 hci_dev_lock(hdev);
4104
4105 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004106 if (!conn)
4107 goto unlock;
4108
4109 hci_conn_hold(conn);
4110 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
4111 hci_conn_drop(conn);
4112
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03004113 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004114 conn_set_key(conn, ev->key_type, conn->pin_length);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004115
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004116 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg7652ff62014-06-24 13:15:49 +03004117 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004118
Johan Hedberg7652ff62014-06-24 13:15:49 +03004119 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
4120 ev->key_type, pin_len, &persistent);
4121 if (!key)
4122 goto unlock;
4123
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004124 /* Update connection information since adding the key will have
4125 * fixed up the type in the case of changed combination keys.
4126 */
4127 if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
4128 conn_set_key(conn, key->type, key->pin_len);
4129
Johan Hedberg7652ff62014-06-24 13:15:49 +03004130 mgmt_new_link_key(hdev, key, persistent);
4131
Johan Hedberg6d5650c2014-06-24 13:15:51 +03004132 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
4133 * is set. If it's not set simply remove the key from the kernel
4134 * list (we've still notified user space about it but with
4135 * store_hint being 0).
4136 */
4137 if (key->type == HCI_LK_DEBUG_COMBINATION &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004138 !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg0378b592014-11-19 15:22:22 +02004139 list_del_rcu(&key->list);
4140 kfree_rcu(key, rcu);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004141 goto unlock;
Johan Hedberg6d5650c2014-06-24 13:15:51 +03004142 }
Johan Hedberg7652ff62014-06-24 13:15:49 +03004143
Johan Hedberg82c13d42014-12-03 11:03:06 +02004144 if (persistent)
4145 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4146 else
4147 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4148
Johan Hedberg7652ff62014-06-24 13:15:49 +03004149unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004150 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151}
4152
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004153static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02004154{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004155 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004156 struct hci_conn *conn;
4157
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004158 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004159
4160 hci_dev_lock(hdev);
4161
4162 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 if (conn && !ev->status) {
4164 struct inquiry_entry *ie;
4165
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004166 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4167 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 ie->data.clock_offset = ev->clock_offset;
4169 ie->timestamp = jiffies;
4170 }
4171 }
4172
4173 hci_dev_unlock(hdev);
4174}
4175
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004176static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02004177{
4178 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
4179 struct hci_conn *conn;
4180
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004181 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02004182
4183 hci_dev_lock(hdev);
4184
4185 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4186 if (conn && !ev->status)
4187 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
4188
4189 hci_dev_unlock(hdev);
4190}
4191
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004192static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004193{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004194 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004195 struct inquiry_entry *ie;
4196
4197 BT_DBG("%s", hdev->name);
4198
4199 hci_dev_lock(hdev);
4200
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004201 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4202 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004203 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
4204 ie->timestamp = jiffies;
4205 }
4206
4207 hci_dev_unlock(hdev);
4208}
4209
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004210static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
4211 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004212{
4213 struct inquiry_data data;
4214 int num_rsp = *((__u8 *) skb->data);
4215
4216 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
4217
4218 if (!num_rsp)
4219 return;
4220
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004221 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03004222 return;
4223
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004224 hci_dev_lock(hdev);
4225
4226 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01004227 struct inquiry_info_with_rssi_and_pscan_mode *info;
4228 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004229
Peilin Ye629b49c2020-07-10 17:45:26 -04004230 if (skb->len < num_rsp * sizeof(*info) + 1)
4231 goto unlock;
4232
Johan Hedberge17acd42011-03-30 23:57:16 +03004233 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004234 u32 flags;
4235
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004236 bacpy(&data.bdaddr, &info->bdaddr);
4237 data.pscan_rep_mode = info->pscan_rep_mode;
4238 data.pscan_period_mode = info->pscan_period_mode;
4239 data.pscan_mode = info->pscan_mode;
4240 memcpy(data.dev_class, info->dev_class, 3);
4241 data.clock_offset = info->clock_offset;
4242 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004243 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02004244
Marcel Holtmannaf589252014-07-01 14:11:20 +02004245 flags = hci_inquiry_cache_update(hdev, &data, false);
4246
Johan Hedberg48264f02011-11-09 13:58:58 +02004247 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004248 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004249 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004250 }
4251 } else {
4252 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
4253
Peilin Ye629b49c2020-07-10 17:45:26 -04004254 if (skb->len < num_rsp * sizeof(*info) + 1)
4255 goto unlock;
4256
Johan Hedberge17acd42011-03-30 23:57:16 +03004257 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004258 u32 flags;
4259
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004260 bacpy(&data.bdaddr, &info->bdaddr);
4261 data.pscan_rep_mode = info->pscan_rep_mode;
4262 data.pscan_period_mode = info->pscan_period_mode;
4263 data.pscan_mode = 0x00;
4264 memcpy(data.dev_class, info->dev_class, 3);
4265 data.clock_offset = info->clock_offset;
4266 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004267 data.ssp_mode = 0x00;
Marcel Holtmannaf589252014-07-01 14:11:20 +02004268
4269 flags = hci_inquiry_cache_update(hdev, &data, false);
4270
Johan Hedberg48264f02011-11-09 13:58:58 +02004271 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004272 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004273 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004274 }
4275 }
4276
Peilin Ye629b49c2020-07-10 17:45:26 -04004277unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004278 hci_dev_unlock(hdev);
4279}
4280
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004281static void hci_remote_ext_features_evt(struct hci_dev *hdev,
4282 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004283{
Marcel Holtmann41a96212008-07-14 20:13:48 +02004284 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
4285 struct hci_conn *conn;
4286
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004287 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004288
Marcel Holtmann41a96212008-07-14 20:13:48 +02004289 hci_dev_lock(hdev);
4290
4291 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02004292 if (!conn)
4293 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004294
Johan Hedbergcad718e2013-04-17 15:00:51 +03004295 if (ev->page < HCI_MAX_PAGES)
4296 memcpy(conn->features[ev->page], ev->features, 8);
4297
Johan Hedbergccd556f2010-11-10 17:11:51 +02004298 if (!ev->status && ev->page == 0x01) {
4299 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004300
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004301 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4302 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02004303 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02004304
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05304305 if (ev->features[0] & LMP_HOST_SSP) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02004306 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05304307 } else {
4308 /* It is mandatory by the Bluetooth specification that
4309 * Extended Inquiry Results are only used when Secure
4310 * Simple Pairing is enabled, but some devices violate
4311 * this.
4312 *
4313 * To make these devices work, the internal SSP
4314 * enabled flag needs to be cleared if the remote host
4315 * features do not indicate SSP support */
4316 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
4317 }
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -08004318
4319 if (ev->features[0] & LMP_HOST_SC)
4320 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004321 }
4322
Johan Hedbergccd556f2010-11-10 17:11:51 +02004323 if (conn->state != BT_CONFIG)
4324 goto unlock;
4325
Johan Hedberg671267b2012-05-12 16:11:50 -03004326 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02004327 struct hci_cp_remote_name_req cp;
4328 memset(&cp, 0, sizeof(cp));
4329 bacpy(&cp.bdaddr, &conn->dst);
4330 cp.pscan_rep_mode = 0x02;
4331 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02004332 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00004333 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02004334
Johan Hedberg127178d2010-11-18 22:22:29 +02004335 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02004336 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02004337 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02004338 hci_conn_drop(conn);
Johan Hedbergccd556f2010-11-10 17:11:51 +02004339 }
4340
4341unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02004342 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004343}
4344
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004345static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
4346 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004347{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004348 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
4349 struct hci_conn *conn;
4350
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004351 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004352
4353 hci_dev_lock(hdev);
4354
4355 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004356 if (!conn) {
4357 if (ev->link_type == ESCO_LINK)
4358 goto unlock;
4359
Kuba Pawlak618353b2015-08-28 13:05:22 +01004360 /* When the link type in the event indicates SCO connection
4361 * and lookup of the connection object fails, then check
4362 * if an eSCO connection object exists.
4363 *
4364 * The core limits the synchronous connections to either
4365 * SCO or eSCO. The eSCO connection is preferred and tried
4366 * to be setup first and until successfully established,
4367 * the link type will be hinted as eSCO.
4368 */
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004369 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
4370 if (!conn)
4371 goto unlock;
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02004372 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004373
Marcel Holtmann732547f2009-04-19 19:14:14 +02004374 switch (ev->status) {
4375 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004376 conn->handle = __le16_to_cpu(ev->handle);
4377 conn->state = BT_CONNECTED;
Kuba Pawlak618353b2015-08-28 13:05:22 +01004378 conn->type = ev->link_type;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02004379
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01004380 hci_debugfs_create_conn(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02004381 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02004382 break;
4383
Nick Pelly81218d22014-06-30 11:25:01 +05304384 case 0x10: /* Connection Accept Timeout */
Frédéric Dalleau1a4c9582013-08-19 14:24:02 +02004385 case 0x0d: /* Connection Rejected due to Limited Resources */
Stephen Coe705e5712010-02-16 11:29:44 -05004386 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02004387 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08004388 case 0x1a: /* Unsupported Remote Feature */
Hsin-Yu Chao56b54532020-05-15 17:27:04 +08004389 case 0x1e: /* Invalid LMP Parameters */
Marcel Holtmann732547f2009-04-19 19:14:14 +02004390 case 0x1f: /* Unspecified error */
Andrew Earl27539bc2014-03-10 10:31:04 +00004391 case 0x20: /* Unsupported LMP Parameter value */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02004392 if (conn->out) {
Marcel Holtmann732547f2009-04-19 19:14:14 +02004393 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
4394 (hdev->esco_type & EDR_ESCO_MASK);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02004395 if (hci_setup_sync(conn, conn->link->handle))
4396 goto unlock;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004397 }
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05004398 fallthrough;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004399
4400 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004401 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02004402 break;
4403 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004404
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02004405 bt_dev_dbg(hdev, "SCO connected with air mode: %02x", ev->air_mode);
4406
4407 switch (conn->setting & SCO_AIRMODE_MASK) {
4408 case SCO_AIRMODE_CVSD:
4409 if (hdev->notify)
4410 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
4411 break;
4412 case SCO_AIRMODE_TRANSP:
4413 if (hdev->notify)
4414 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_TRANSP);
4415 break;
4416 }
4417
Johan Hedberg539c4962015-02-18 14:53:57 +02004418 hci_connect_cfm(conn, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02004419 if (ev->status)
4420 hci_conn_del(conn);
4421
4422unlock:
4423 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004424}
4425
Marcel Holtmannefdcf8e2013-10-15 10:31:12 -07004426static inline size_t eir_get_length(u8 *eir, size_t eir_len)
4427{
4428 size_t parsed = 0;
4429
4430 while (parsed < eir_len) {
4431 u8 field_len = eir[0];
4432
4433 if (field_len == 0)
4434 return parsed;
4435
4436 parsed += field_len + 1;
4437 eir += field_len + 1;
4438 }
4439
4440 return eir_len;
4441}
4442
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004443static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
4444 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004445{
4446 struct inquiry_data data;
4447 struct extended_inquiry_info *info = (void *) (skb->data + 1);
4448 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05304449 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004450
4451 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
4452
Peilin Ye51c19bf2020-07-10 12:09:15 -04004453 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004454 return;
4455
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004456 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03004457 return;
4458
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004459 hci_dev_lock(hdev);
4460
Johan Hedberge17acd42011-03-30 23:57:16 +03004461 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004462 u32 flags;
4463 bool name_known;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004464
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004465 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01004466 data.pscan_rep_mode = info->pscan_rep_mode;
4467 data.pscan_period_mode = info->pscan_period_mode;
4468 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004469 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01004470 data.clock_offset = info->clock_offset;
4471 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004472 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004473
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004474 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg0d3b7f62016-01-05 13:19:31 +02004475 name_known = eir_get_data(info->data,
4476 sizeof(info->data),
4477 EIR_NAME_COMPLETE, NULL);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004478 else
4479 name_known = true;
4480
Marcel Holtmannaf589252014-07-01 14:11:20 +02004481 flags = hci_inquiry_cache_update(hdev, &data, name_known);
4482
Vishal Agarwal9d939d92012-04-26 19:19:56 +05304483 eir_len = eir_get_length(info->data, sizeof(info->data));
Marcel Holtmannaf589252014-07-01 14:11:20 +02004484
Johan Hedberg48264f02011-11-09 13:58:58 +02004485 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004486 info->dev_class, info->rssi,
4487 flags, info->data, eir_len, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004488 }
4489
4490 hci_dev_unlock(hdev);
4491}
4492
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004493static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
4494 struct sk_buff *skb)
4495{
4496 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
4497 struct hci_conn *conn;
4498
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004499 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004500 __le16_to_cpu(ev->handle));
4501
4502 hci_dev_lock(hdev);
4503
4504 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4505 if (!conn)
4506 goto unlock;
4507
Johan Hedberg9eb1fbf2014-04-11 12:02:31 -07004508 /* For BR/EDR the necessary steps are taken through the
4509 * auth_complete event.
4510 */
4511 if (conn->type != LE_LINK)
4512 goto unlock;
4513
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004514 if (!ev->status)
4515 conn->sec_level = conn->pending_sec_level;
4516
4517 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
4518
4519 if (ev->status && conn->state == BT_CONNECTED) {
Andre Guedesbed71742013-01-30 11:50:56 -03004520 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
David Herrmann76a68ba2013-04-06 20:28:37 +02004521 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004522 goto unlock;
4523 }
4524
4525 if (conn->state == BT_CONFIG) {
4526 if (!ev->status)
4527 conn->state = BT_CONNECTED;
4528
Johan Hedberg539c4962015-02-18 14:53:57 +02004529 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02004530 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004531 } else {
4532 hci_auth_cfm(conn, ev->status);
4533
4534 hci_conn_hold(conn);
4535 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004536 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004537 }
4538
4539unlock:
4540 hci_dev_unlock(hdev);
4541}
4542
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004543static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004544{
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004545 /* If remote requests no-bonding follow that lead */
Mikel Astizacabae92013-06-28 10:56:28 +02004546 if (conn->remote_auth == HCI_AT_NO_BONDING ||
4547 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02004548 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004549
Mikel Astizb7f94c82014-04-08 14:21:31 +02004550 /* If both remote and local have enough IO capabilities, require
4551 * MITM protection
4552 */
4553 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
4554 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
4555 return conn->remote_auth | 0x01;
4556
Timo Mueller7e741702014-04-08 14:21:33 +02004557 /* No MITM protection possible so ignore remote requirement */
4558 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004559}
4560
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004561static u8 bredr_oob_data_present(struct hci_conn *conn)
4562{
4563 struct hci_dev *hdev = conn->hdev;
4564 struct oob_data *data;
4565
4566 data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
4567 if (!data)
4568 return 0x00;
4569
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004570 if (bredr_sc_enabled(hdev)) {
4571 /* When Secure Connections is enabled, then just
4572 * return the present value stored with the OOB
4573 * data. The stored value contains the right present
4574 * information. However it can only be trusted when
4575 * not in Secure Connection Only mode.
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004576 */
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004577 if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
4578 return data->present;
4579
4580 /* When Secure Connections Only mode is enabled, then
4581 * the P-256 values are required. If they are not
4582 * available, then do not declare that OOB data is
4583 * present.
4584 */
4585 if (!memcmp(data->rand256, ZERO_KEY, 16) ||
4586 !memcmp(data->hash256, ZERO_KEY, 16))
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004587 return 0x00;
4588
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004589 return 0x02;
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08004590 }
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004591
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07004592 /* When Secure Connections is not enabled or actually
4593 * not supported by the hardware, then check that if
4594 * P-192 data values are present.
4595 */
4596 if (!memcmp(data->rand192, ZERO_KEY, 16) ||
4597 !memcmp(data->hash192, ZERO_KEY, 16))
4598 return 0x00;
4599
4600 return 0x01;
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004601}
4602
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004603static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02004604{
4605 struct hci_ev_io_capa_request *ev = (void *) skb->data;
4606 struct hci_conn *conn;
4607
4608 BT_DBG("%s", hdev->name);
4609
4610 hci_dev_lock(hdev);
4611
4612 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004613 if (!conn)
4614 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02004615
Johan Hedberg03b555e2011-01-04 15:40:05 +02004616 hci_conn_hold(conn);
4617
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004618 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg03b555e2011-01-04 15:40:05 +02004619 goto unlock;
4620
Johan Hedberg2f407f02014-07-17 15:35:40 +03004621 /* Allow pairing if we're pairable, the initiators of the
4622 * pairing or if the remote is not requesting bonding.
4623 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004624 if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
Johan Hedberg2f407f02014-07-17 15:35:40 +03004625 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03004626 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004627 struct hci_cp_io_capability_reply cp;
4628
4629 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05304630 /* Change the IO capability from KeyboardDisplay
4631 * to DisplayYesNo as it is not supported by BT spec. */
4632 cp.capability = (conn->io_capability == 0x04) ?
Mikel Astiza7676312013-06-28 10:56:29 +02004633 HCI_IO_DISPLAY_YESNO : conn->io_capability;
Mikel Astizb7f94c82014-04-08 14:21:31 +02004634
4635 /* If we are initiators, there is no remote information yet */
4636 if (conn->remote_auth == 0xff) {
Mikel Astizb16c6602014-04-08 14:21:34 +02004637 /* Request MITM protection if our IO caps allow it
Johan Hedberg4ad51a72014-06-09 14:41:25 +03004638 * except for the no-bonding case.
Mikel Astizb16c6602014-04-08 14:21:34 +02004639 */
Mikel Astiz6fd6b912014-04-08 14:21:32 +02004640 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedberg9f743d72014-07-17 11:56:33 +03004641 conn->auth_type != HCI_AT_NO_BONDING)
Johan Hedberg6c538232014-07-11 15:32:23 +03004642 conn->auth_type |= 0x01;
Mikel Astizb7f94c82014-04-08 14:21:31 +02004643 } else {
4644 conn->auth_type = hci_get_auth_req(conn);
Mikel Astizb7f94c82014-04-08 14:21:31 +02004645 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004646
Johan Hedberg82c295b2014-07-30 09:22:24 +03004647 /* If we're not bondable, force one of the non-bondable
4648 * authentication requirement values.
4649 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004650 if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedberg82c295b2014-07-30 09:22:24 +03004651 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
4652
4653 cp.authentication = conn->auth_type;
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08004654 cp.oob_data = bredr_oob_data_present(conn);
Szymon Jancce85ee12011-03-22 13:12:23 +01004655
Johan Hedberg17fa4b92011-01-25 13:28:33 +02004656 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004657 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004658 } else {
4659 struct hci_cp_io_capability_neg_reply cp;
4660
4661 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02004662 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02004663
4664 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004665 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02004666 }
4667
4668unlock:
4669 hci_dev_unlock(hdev);
4670}
4671
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004672static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02004673{
4674 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
4675 struct hci_conn *conn;
4676
4677 BT_DBG("%s", hdev->name);
4678
4679 hci_dev_lock(hdev);
4680
4681 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4682 if (!conn)
4683 goto unlock;
4684
Johan Hedberg03b555e2011-01-04 15:40:05 +02004685 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02004686 conn->remote_auth = ev->authentication;
4687
4688unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02004689 hci_dev_unlock(hdev);
4690}
4691
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004692static void hci_user_confirm_request_evt(struct hci_dev *hdev,
4693 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03004694{
4695 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004696 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07004697 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03004698
4699 BT_DBG("%s", hdev->name);
4700
4701 hci_dev_lock(hdev);
4702
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004703 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg7a828902011-04-28 11:28:53 -07004704 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03004705
Johan Hedberg7a828902011-04-28 11:28:53 -07004706 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4707 if (!conn)
4708 goto unlock;
4709
4710 loc_mitm = (conn->auth_type & 0x01);
4711 rem_mitm = (conn->remote_auth & 0x01);
4712
4713 /* If we require MITM but the remote device can't provide that
Johan Hedberg6c538232014-07-11 15:32:23 +03004714 * (it has NoInputNoOutput) then reject the confirmation
4715 * request. We check the security level here since it doesn't
4716 * necessarily match conn->auth_type.
Mikel Astiz6fd6b912014-04-08 14:21:32 +02004717 */
Johan Hedberg6c538232014-07-11 15:32:23 +03004718 if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
4719 conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
Johan Hedberg7a828902011-04-28 11:28:53 -07004720 BT_DBG("Rejecting request: remote device can't provide MITM");
4721 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004722 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07004723 goto unlock;
4724 }
4725
4726 /* If no side requires MITM protection; auto-accept */
Mikel Astiza7676312013-06-28 10:56:29 +02004727 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
4728 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004729
4730 /* If we're not the initiators request authorization to
4731 * proceed from user space (mgmt_user_confirm with
Johan Hedbergba15a582014-06-09 13:58:14 +03004732 * confirm_hint set to 1). The exception is if neither
Johan Hedberg02f3e252014-07-16 15:09:13 +03004733 * side had MITM or if the local IO capability is
4734 * NoInputNoOutput, in which case we do auto-accept
Johan Hedbergba15a582014-06-09 13:58:14 +03004735 */
4736 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
Johan Hedberg02f3e252014-07-16 15:09:13 +03004737 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedbergba15a582014-06-09 13:58:14 +03004738 (loc_mitm || rem_mitm)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004739 BT_DBG("Confirming auto-accept as acceptor");
4740 confirm_hint = 1;
4741 goto confirm;
4742 }
4743
Howard Chungcee5f202020-02-14 19:16:41 +08004744 /* If there already exists link key in local host, leave the
4745 * decision to user space since the remote device could be
4746 * legitimate or malicious.
4747 */
4748 if (hci_find_link_key(hdev, &ev->bdaddr)) {
4749 bt_dev_dbg(hdev, "Local host already has link key");
4750 confirm_hint = 1;
4751 goto confirm;
4752 }
4753
Johan Hedberg9f616562011-04-28 11:28:54 -07004754 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03004755 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07004756
4757 if (hdev->auto_accept_delay > 0) {
4758 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03004759 queue_delayed_work(conn->hdev->workqueue,
4760 &conn->auto_accept_work, delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07004761 goto unlock;
4762 }
4763
Johan Hedberg7a828902011-04-28 11:28:53 -07004764 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004765 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07004766 goto unlock;
4767 }
4768
Johan Hedberg55bc1a32011-04-28 11:28:56 -07004769confirm:
Johan Hedberg39adbff2014-03-20 08:18:14 +02004770 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
4771 le32_to_cpu(ev->passkey), confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07004772
4773unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03004774 hci_dev_unlock(hdev);
4775}
4776
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004777static void hci_user_passkey_request_evt(struct hci_dev *hdev,
4778 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08004779{
4780 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
4781
4782 BT_DBG("%s", hdev->name);
4783
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004784 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg272d90d2012-02-09 15:26:12 +02004785 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08004786}
4787
Johan Hedberg92a25252012-09-06 18:39:26 +03004788static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
4789 struct sk_buff *skb)
4790{
4791 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
4792 struct hci_conn *conn;
4793
4794 BT_DBG("%s", hdev->name);
4795
4796 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4797 if (!conn)
4798 return;
4799
4800 conn->passkey_notify = __le32_to_cpu(ev->passkey);
4801 conn->passkey_entered = 0;
4802
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004803 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg92a25252012-09-06 18:39:26 +03004804 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4805 conn->dst_type, conn->passkey_notify,
4806 conn->passkey_entered);
4807}
4808
4809static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
4810{
4811 struct hci_ev_keypress_notify *ev = (void *) skb->data;
4812 struct hci_conn *conn;
4813
4814 BT_DBG("%s", hdev->name);
4815
4816 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4817 if (!conn)
4818 return;
4819
4820 switch (ev->type) {
4821 case HCI_KEYPRESS_STARTED:
4822 conn->passkey_entered = 0;
4823 return;
4824
4825 case HCI_KEYPRESS_ENTERED:
4826 conn->passkey_entered++;
4827 break;
4828
4829 case HCI_KEYPRESS_ERASED:
4830 conn->passkey_entered--;
4831 break;
4832
4833 case HCI_KEYPRESS_CLEARED:
4834 conn->passkey_entered = 0;
4835 break;
4836
4837 case HCI_KEYPRESS_COMPLETED:
4838 return;
4839 }
4840
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004841 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg92a25252012-09-06 18:39:26 +03004842 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
4843 conn->dst_type, conn->passkey_notify,
4844 conn->passkey_entered);
4845}
4846
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004847static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
4848 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02004849{
4850 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
4851 struct hci_conn *conn;
4852
4853 BT_DBG("%s", hdev->name);
4854
4855 hci_dev_lock(hdev);
4856
4857 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03004858 if (!conn)
4859 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02004860
Johan Hedbergc1d4fa72014-07-17 15:14:50 +03004861 /* Reset the authentication requirement to unknown */
4862 conn->remote_auth = 0xff;
4863
Johan Hedberg2a611692011-02-19 12:06:00 -03004864 /* To avoid duplicate auth_failed events to user space we check
4865 * the HCI_CONN_AUTH_PEND flag which will be set if we
4866 * initiated the authentication. A traditional auth_complete
4867 * event gets always produced as initiator and is also mapped to
4868 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02004869 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedberge1e930f2014-09-08 17:09:49 -07004870 mgmt_auth_failed(conn, ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03004871
David Herrmann76a68ba2013-04-06 20:28:37 +02004872 hci_conn_drop(conn);
Johan Hedberg2a611692011-02-19 12:06:00 -03004873
4874unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02004875 hci_dev_unlock(hdev);
4876}
4877
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004878static void hci_remote_host_features_evt(struct hci_dev *hdev,
4879 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02004880{
4881 struct hci_ev_remote_host_features *ev = (void *) skb->data;
4882 struct inquiry_entry *ie;
Johan Hedbergcad718e2013-04-17 15:00:51 +03004883 struct hci_conn *conn;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004884
4885 BT_DBG("%s", hdev->name);
4886
4887 hci_dev_lock(hdev);
4888
Johan Hedbergcad718e2013-04-17 15:00:51 +03004889 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4890 if (conn)
4891 memcpy(conn->features[1], ev->features, 8);
4892
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004893 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4894 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02004895 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004896
4897 hci_dev_unlock(hdev);
4898}
4899
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004900static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
4901 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01004902{
4903 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
4904 struct oob_data *data;
4905
4906 BT_DBG("%s", hdev->name);
4907
4908 hci_dev_lock(hdev);
4909
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004910 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Szymon Jance1ba1f12011-04-06 13:01:59 +02004911 goto unlock;
4912
Johan Hedberg6928a922014-10-26 20:46:09 +01004913 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
Marcel Holtmann6665d052015-01-27 16:04:31 -08004914 if (!data) {
Szymon Janc2763eda2011-03-22 13:12:22 +01004915 struct hci_cp_remote_oob_data_neg_reply cp;
4916
4917 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004918 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
4919 sizeof(cp), &cp);
Marcel Holtmann6665d052015-01-27 16:04:31 -08004920 goto unlock;
4921 }
4922
4923 if (bredr_sc_enabled(hdev)) {
4924 struct hci_cp_remote_oob_ext_data_reply cp;
4925
4926 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004927 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmann6665d052015-01-27 16:04:31 -08004928 memset(cp.hash192, 0, sizeof(cp.hash192));
4929 memset(cp.rand192, 0, sizeof(cp.rand192));
4930 } else {
4931 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
4932 memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
4933 }
4934 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
4935 memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
4936
4937 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
4938 sizeof(cp), &cp);
4939 } else {
4940 struct hci_cp_remote_oob_data_reply cp;
4941
4942 bacpy(&cp.bdaddr, &ev->bdaddr);
4943 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
4944 memcpy(cp.rand, data->rand192, sizeof(cp.rand));
4945
4946 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
4947 sizeof(cp), &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01004948 }
4949
Szymon Jance1ba1f12011-04-06 13:01:59 +02004950unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01004951 hci_dev_unlock(hdev);
4952}
4953
Arron Wanga77a6a12015-07-24 17:13:15 +08004954#if IS_ENABLED(CONFIG_BT_HS)
4955static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
4956{
4957 struct hci_ev_channel_selected *ev = (void *)skb->data;
4958 struct hci_conn *hcon;
4959
4960 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
4961
4962 skb_pull(skb, sizeof(*ev));
4963
4964 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4965 if (!hcon)
4966 return;
4967
4968 amp_read_loc_assoc_final_data(hdev, hcon);
4969}
4970
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004971static void hci_phy_link_complete_evt(struct hci_dev *hdev,
4972 struct sk_buff *skb)
4973{
4974 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
4975 struct hci_conn *hcon, *bredr_hcon;
4976
4977 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
4978 ev->status);
4979
4980 hci_dev_lock(hdev);
4981
4982 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03004983 if (!hcon)
4984 goto unlock;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004985
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03004986 if (!hcon->amp_mgr)
4987 goto unlock;
Anmol Karn6dfccd12020-09-30 19:48:13 +05304988
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004989 if (ev->status) {
4990 hci_conn_del(hcon);
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03004991 goto unlock;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004992 }
4993
4994 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
4995
4996 hcon->state = BT_CONNECTED;
4997 bacpy(&hcon->dst, &bredr_hcon->dst);
4998
4999 hci_conn_hold(hcon);
5000 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02005001 hci_conn_drop(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005002
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01005003 hci_debugfs_create_conn(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005004 hci_conn_add_sysfs(hcon);
5005
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +02005006 amp_physical_cfm(bredr_hcon, hcon);
5007
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005008unlock:
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005009 hci_dev_unlock(hdev);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005010}
5011
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005012static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
5013{
5014 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
5015 struct hci_conn *hcon;
5016 struct hci_chan *hchan;
5017 struct amp_mgr *mgr;
5018
5019 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
5020 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
5021 ev->status);
5022
5023 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5024 if (!hcon)
5025 return;
5026
5027 /* Create AMP hchan */
5028 hchan = hci_chan_create(hcon);
5029 if (!hchan)
5030 return;
5031
5032 hchan->handle = le16_to_cpu(ev->handle);
Archie Pusaka5c4c8c92021-03-22 14:03:11 +08005033 hchan->amp = true;
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005034
5035 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
5036
5037 mgr = hcon->amp_mgr;
5038 if (mgr && mgr->bredr_chan) {
5039 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
5040
5041 l2cap_chan_lock(bredr_chan);
5042
5043 bredr_chan->conn->mtu = hdev->block_mtu;
5044 l2cap_logical_cfm(bredr_chan, hchan, 0);
5045 hci_conn_hold(hcon);
5046
5047 l2cap_chan_unlock(bredr_chan);
5048 }
5049}
5050
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005051static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
5052 struct sk_buff *skb)
5053{
5054 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
5055 struct hci_chan *hchan;
5056
5057 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
5058 le16_to_cpu(ev->handle), ev->status);
5059
5060 if (ev->status)
5061 return;
5062
5063 hci_dev_lock(hdev);
5064
5065 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
Archie Pusaka5c4c8c92021-03-22 14:03:11 +08005066 if (!hchan || !hchan->amp)
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005067 goto unlock;
5068
5069 amp_destroy_logical_link(hchan, ev->reason);
5070
5071unlock:
5072 hci_dev_unlock(hdev);
5073}
5074
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005075static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
5076 struct sk_buff *skb)
5077{
5078 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
5079 struct hci_conn *hcon;
5080
5081 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5082
5083 if (ev->status)
5084 return;
5085
5086 hci_dev_lock(hdev);
5087
5088 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5089 if (hcon) {
5090 hcon->state = BT_CLOSED;
5091 hci_conn_del(hcon);
5092 }
5093
5094 hci_dev_unlock(hdev);
5095}
Arron Wanga77a6a12015-07-24 17:13:15 +08005096#endif
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005097
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305098static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,
5099 bdaddr_t *bdaddr, u8 bdaddr_type, u8 role, u16 handle,
5100 u16 interval, u16 latency, u16 supervision_timeout)
Ville Tervofcd89c02011-02-10 22:38:47 -03005101{
Johan Hedberg912b42ef2014-07-03 19:33:49 +03005102 struct hci_conn_params *params;
Ville Tervofcd89c02011-02-10 22:38:47 -03005103 struct hci_conn *conn;
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02005104 struct smp_irk *irk;
Johan Hedberg837d5022014-07-02 09:36:22 +03005105 u8 addr_type;
Ville Tervofcd89c02011-02-10 22:38:47 -03005106
Ville Tervofcd89c02011-02-10 22:38:47 -03005107 hci_dev_lock(hdev);
5108
Johan Hedbergfbd96c12014-07-08 17:21:51 +03005109 /* All controllers implicitly stop advertising in the event of a
5110 * connection, so ensure that the state bit is cleared.
5111 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005112 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedbergfbd96c12014-07-08 17:21:51 +03005113
Jakub Pawlowskie7d9ab72015-08-07 20:22:52 +02005114 conn = hci_lookup_le_connect(hdev);
Ville Tervob62f3282011-02-10 22:38:50 -03005115 if (!conn) {
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305116 conn = hci_conn_add(hdev, LE_LINK, bdaddr, role);
Ville Tervob62f3282011-02-10 22:38:50 -03005117 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005118 bt_dev_err(hdev, "no memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03005119 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03005120 }
Andre Guedes29b79882011-05-31 14:20:54 -03005121
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305122 conn->dst_type = bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03005123
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005124 /* If we didn't have a hci_conn object previously
5125 * but we're in master role this must be something
5126 * initiated using a white list. Since white list based
5127 * connections are not "first class citizens" we don't
5128 * have full tracking of them. Therefore, we go ahead
5129 * with a "best effort" approach of determining the
5130 * initiator address based on the HCI_PRIVACY flag.
5131 */
5132 if (conn->out) {
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305133 conn->resp_addr_type = bdaddr_type;
5134 bacpy(&conn->resp_addr, bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005135 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005136 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5137 bacpy(&conn->init_addr, &hdev->rpa);
5138 } else {
5139 hci_copy_identity_address(hdev,
5140 &conn->init_addr,
5141 &conn->init_addr_type);
5142 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005143 }
Johan Hedberg9489eca2014-02-28 17:45:46 +02005144 } else {
5145 cancel_delayed_work(&conn->le_conn_timeout);
Ville Tervob62f3282011-02-10 22:38:50 -03005146 }
Ville Tervofcd89c02011-02-10 22:38:47 -03005147
Johan Hedberg80c24ab2014-03-24 20:21:51 +02005148 if (!conn->out) {
5149 /* Set the responder (our side) address type based on
5150 * the advertising address type.
5151 */
5152 conn->resp_addr_type = hdev->adv_addr_type;
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305153 if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM) {
5154 /* In case of ext adv, resp_addr will be updated in
5155 * Adv Terminated event.
5156 */
5157 if (!ext_adv_capable(hdev))
5158 bacpy(&conn->resp_addr, &hdev->random_addr);
5159 } else {
Johan Hedberg80c24ab2014-03-24 20:21:51 +02005160 bacpy(&conn->resp_addr, &hdev->bdaddr);
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305161 }
Johan Hedberg80c24ab2014-03-24 20:21:51 +02005162
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305163 conn->init_addr_type = bdaddr_type;
5164 bacpy(&conn->init_addr, bdaddr);
Marcel Holtmanna720d732014-06-23 12:14:51 +02005165
5166 /* For incoming connections, set the default minimum
5167 * and maximum connection interval. They will be used
5168 * to check if the parameters are in range and if not
5169 * trigger the connection update procedure.
5170 */
5171 conn->le_conn_min_interval = hdev->le_conn_min_interval;
5172 conn->le_conn_max_interval = hdev->le_conn_max_interval;
Johan Hedberg80c24ab2014-03-24 20:21:51 +02005173 }
Johan Hedberg7be2edb2014-02-23 19:42:17 +02005174
Marcel Holtmannedb4b462014-02-18 15:13:43 -08005175 /* Lookup the identity address from the stored connection
5176 * address and address type.
5177 *
5178 * When establishing connections to an identity address, the
5179 * connection procedure will store the resolvable random
5180 * address first. Now if it can be converted back into the
5181 * identity address, start using the identity address from
5182 * now on.
5183 */
5184 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02005185 if (irk) {
5186 bacpy(&conn->dst, &irk->bdaddr);
5187 conn->dst_type = irk->addr_type;
5188 }
5189
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305190 if (status) {
5191 hci_le_conn_failed(conn, status);
Johan Hedberg837d5022014-07-02 09:36:22 +03005192 goto unlock;
5193 }
5194
Johan Hedberg08853f12014-08-15 21:06:55 +03005195 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
5196 addr_type = BDADDR_LE_PUBLIC;
5197 else
5198 addr_type = BDADDR_LE_RANDOM;
5199
Johan Hedberg2d3c2262014-07-15 11:51:28 +03005200 /* Drop the connection if the device is blocked */
5201 if (hci_bdaddr_list_lookup(&hdev->blacklist, &conn->dst, addr_type)) {
5202 hci_conn_drop(conn);
Andre Guedescd17dec2012-07-27 15:10:16 -03005203 goto unlock;
5204 }
5205
Johan Hedbergb644ba32012-01-17 21:48:47 +02005206 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00005207 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03005208
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03005209 conn->sec_level = BT_SECURITY_LOW;
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305210 conn->handle = handle;
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005211 conn->state = BT_CONFIG;
Ville Tervofcd89c02011-02-10 22:38:47 -03005212
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305213 conn->le_conn_interval = interval;
5214 conn->le_conn_latency = latency;
5215 conn->le_supv_timeout = supervision_timeout;
Marcel Holtmanne04fde62014-06-23 11:40:04 +02005216
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01005217 hci_debugfs_create_conn(conn);
Ville Tervofcd89c02011-02-10 22:38:47 -03005218 hci_conn_add_sysfs(conn);
5219
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005220 /* The remote features procedure is defined for master
5221 * role only. So only in case of an initiated connection
5222 * request the remote features.
5223 *
5224 * If the local controller supports slave-initiated features
5225 * exchange, then requesting the remote features in slave
5226 * role is possible. Otherwise just transition into the
5227 * connected state without requesting the remote features.
5228 */
5229 if (conn->out ||
5230 (hdev->le_features[0] & HCI_LE_SLAVE_FEATURES)) {
5231 struct hci_cp_le_read_remote_features cp;
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005232
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005233 cp.handle = __cpu_to_le16(conn->handle);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005234
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005235 hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
5236 sizeof(cp), &cp);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005237
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005238 hci_conn_hold(conn);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005239 } else {
Colin Ian Kingd17010b2018-10-10 15:37:31 +01005240 conn->state = BT_CONNECTED;
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305241 hci_connect_cfm(conn, status);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005242 }
Ville Tervofcd89c02011-02-10 22:38:47 -03005243
Johan Hedberg54776102014-08-15 21:06:56 +03005244 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
5245 conn->dst_type);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005246 if (params) {
Johan Hedberg95305ba2014-07-04 12:37:21 +03005247 list_del_init(&params->action);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005248 if (params->conn) {
5249 hci_conn_drop(params->conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005250 hci_conn_put(params->conn);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005251 params->conn = NULL;
5252 }
5253 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005254
Ville Tervofcd89c02011-02-10 22:38:47 -03005255unlock:
Johan Hedberg223683a52014-07-06 15:44:23 +03005256 hci_update_background_scan(hdev);
Ville Tervofcd89c02011-02-10 22:38:47 -03005257 hci_dev_unlock(hdev);
5258}
5259
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305260static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
5261{
5262 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
5263
5264 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5265
5266 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
5267 ev->role, le16_to_cpu(ev->handle),
5268 le16_to_cpu(ev->interval),
5269 le16_to_cpu(ev->latency),
5270 le16_to_cpu(ev->supervision_timeout));
5271}
5272
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005273static void hci_le_enh_conn_complete_evt(struct hci_dev *hdev,
5274 struct sk_buff *skb)
5275{
5276 struct hci_ev_le_enh_conn_complete *ev = (void *) skb->data;
5277
5278 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5279
5280 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
5281 ev->role, le16_to_cpu(ev->handle),
5282 le16_to_cpu(ev->interval),
5283 le16_to_cpu(ev->latency),
5284 le16_to_cpu(ev->supervision_timeout));
Sathish Narasimman5c49bcc2020-07-23 18:09:01 +05305285
5286 if (use_ll_privacy(hdev) &&
Sathish Narasimmancbbdfa62020-07-23 18:09:03 +05305287 hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
Sathish Narasimman5c49bcc2020-07-23 18:09:01 +05305288 hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION))
5289 hci_req_disable_address_resolution(hdev);
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005290}
5291
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305292static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, struct sk_buff *skb)
5293{
5294 struct hci_evt_le_ext_adv_set_term *ev = (void *) skb->data;
5295 struct hci_conn *conn;
5296
5297 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5298
5299 if (ev->status)
5300 return;
5301
5302 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle));
5303 if (conn) {
5304 struct adv_info *adv_instance;
5305
5306 if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM)
5307 return;
5308
Daniel Winkler25e70882021-04-05 16:33:04 -07005309 if (!ev->handle) {
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305310 bacpy(&conn->resp_addr, &hdev->random_addr);
5311 return;
5312 }
5313
Daniel Winkler25e70882021-04-05 16:33:04 -07005314 adv_instance = hci_find_adv_instance(hdev, ev->handle);
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05305315 if (adv_instance)
5316 bacpy(&conn->resp_addr, &adv_instance->random_addr);
5317 }
5318}
5319
Marcel Holtmann1855d922014-06-23 11:40:05 +02005320static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
5321 struct sk_buff *skb)
5322{
5323 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
5324 struct hci_conn *conn;
5325
5326 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5327
5328 if (ev->status)
5329 return;
5330
5331 hci_dev_lock(hdev);
5332
5333 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5334 if (conn) {
5335 conn->le_conn_interval = le16_to_cpu(ev->interval);
5336 conn->le_conn_latency = le16_to_cpu(ev->latency);
5337 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
5338 }
5339
5340 hci_dev_unlock(hdev);
5341}
5342
Andre Guedesa4790db2014-02-26 20:21:47 -03005343/* This function requires the caller holds hdev->lock */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005344static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
5345 bdaddr_t *addr,
Szymon Janc082f2302018-04-03 13:40:06 +02005346 u8 addr_type, u8 adv_type,
5347 bdaddr_t *direct_rpa)
Andre Guedesa4790db2014-02-26 20:21:47 -03005348{
5349 struct hci_conn *conn;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005350 struct hci_conn_params *params;
Andre Guedesa4790db2014-02-26 20:21:47 -03005351
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005352 /* If the event is not connectable don't proceed further */
5353 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005354 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005355
5356 /* Ignore if the device is blocked */
Johan Hedbergdcc36c12014-07-09 12:59:13 +03005357 if (hci_bdaddr_list_lookup(&hdev->blacklist, addr, addr_type))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005358 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005359
Johan Hedbergf99353c2014-07-16 11:56:09 +03005360 /* Most controller will fail if we try to create new connections
5361 * while we have an existing one in slave role.
5362 */
Alain Michaud4364f2e2020-04-23 14:43:29 +00005363 if (hdev->conn_hash.le_num_slave > 0 &&
5364 (!test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) ||
5365 !(hdev->le_states[3] & 0x10)))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005366 return NULL;
Johan Hedbergf99353c2014-07-16 11:56:09 +03005367
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005368 /* If we're not connectable only connect devices that we have in
5369 * our pend_le_conns list.
5370 */
Johan Hedberg49c50922015-10-16 10:07:51 +03005371 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
5372 addr_type);
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005373 if (!params)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005374 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005375
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005376 if (!params->explicit_connect) {
5377 switch (params->auto_connect) {
5378 case HCI_AUTO_CONN_DIRECT:
5379 /* Only devices advertising with ADV_DIRECT_IND are
5380 * triggering a connection attempt. This is allowing
5381 * incoming connections from slave devices.
5382 */
5383 if (adv_type != LE_ADV_DIRECT_IND)
5384 return NULL;
5385 break;
5386 case HCI_AUTO_CONN_ALWAYS:
5387 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
5388 * are triggering a connection attempt. This means
Alain Michaud1e5479b2020-04-03 13:49:05 +00005389 * that incoming connections from slave device are
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005390 * accepted and also outgoing connections to slave
5391 * devices are established when found.
5392 */
5393 break;
5394 default:
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005395 return NULL;
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005396 }
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005397 }
5398
Andre Guedesa4790db2014-02-26 20:21:47 -03005399 conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
Alain Michaud49b020c2020-06-29 16:11:00 +00005400 hdev->def_le_autoconnect_timeout, HCI_ROLE_MASTER,
Szymon Janc082f2302018-04-03 13:40:06 +02005401 direct_rpa);
Johan Hedbergf161dd42014-08-15 21:06:54 +03005402 if (!IS_ERR(conn)) {
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005403 /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
5404 * by higher layer that tried to connect, if no then
5405 * store the pointer since we don't really have any
Johan Hedbergf161dd42014-08-15 21:06:54 +03005406 * other owner of the object besides the params that
5407 * triggered it. This way we can abort the connection if
5408 * the parameters get removed and keep the reference
5409 * count consistent once the connection is established.
5410 */
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02005411
5412 if (!params->explicit_connect)
5413 params->conn = hci_conn_get(conn);
5414
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005415 return conn;
Johan Hedbergf161dd42014-08-15 21:06:54 +03005416 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005417
5418 switch (PTR_ERR(conn)) {
5419 case -EBUSY:
5420 /* If hci_connect() returns -EBUSY it means there is already
5421 * an LE connection attempt going on. Since controllers don't
5422 * support more than one connection attempt at the time, we
5423 * don't consider this an error case.
5424 */
5425 break;
5426 default:
5427 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005428 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005429 }
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005430
5431 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03005432}
5433
Johan Hedberg4af605d2014-03-24 10:48:00 +02005434static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005435 u8 bdaddr_type, bdaddr_t *direct_addr,
Alain Michauda2ec9052020-07-27 20:48:55 +00005436 u8 direct_addr_type, s8 rssi, u8 *data, u8 len,
5437 bool ext_adv)
Johan Hedberg4af605d2014-03-24 10:48:00 +02005438{
Johan Hedbergb9a63282014-03-25 10:51:52 +02005439 struct discovery_state *d = &hdev->discovery;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005440 struct smp_irk *irk;
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005441 struct hci_conn *conn;
Johan Hedberg474ee062014-03-25 14:34:59 +02005442 bool match;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005443 u32 flags;
Szymon Janc68183752015-09-16 20:21:54 +02005444 u8 *ptr, real_len;
5445
Johan Hedberg56b40fb2016-04-07 21:01:27 +03005446 switch (type) {
5447 case LE_ADV_IND:
5448 case LE_ADV_DIRECT_IND:
5449 case LE_ADV_SCAN_IND:
5450 case LE_ADV_NONCONN_IND:
5451 case LE_ADV_SCAN_RSP:
5452 break;
5453 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005454 bt_dev_err_ratelimited(hdev, "unknown advertising packet "
5455 "type: 0x%02x", type);
Johan Hedberg56b40fb2016-04-07 21:01:27 +03005456 return;
5457 }
5458
Alain Michauda2ec9052020-07-27 20:48:55 +00005459 if (!ext_adv && len > HCI_MAX_AD_LENGTH) {
5460 bt_dev_err_ratelimited(hdev, "legacy adv larger than 31 bytes");
5461 return;
5462 }
5463
Szymon Janc68183752015-09-16 20:21:54 +02005464 /* Find the end of the data in case the report contains padded zero
5465 * bytes at the end causing an invalid length value.
5466 *
5467 * When data is NULL, len is 0 so there is no need for extra ptr
5468 * check as 'ptr < data + 0' is already false in such case.
5469 */
5470 for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
5471 if (ptr + 1 + *ptr > data + len)
5472 break;
5473 }
5474
5475 real_len = ptr - data;
5476
5477 /* Adjust for actual length */
5478 if (len != real_len) {
Daniels Umanovskis943d5d922020-04-09 13:18:29 +02005479 bt_dev_err_ratelimited(hdev, "advertising data len corrected %u -> %u",
5480 len, real_len);
Szymon Janc68183752015-09-16 20:21:54 +02005481 len = real_len;
5482 }
Johan Hedbergb9a63282014-03-25 10:51:52 +02005483
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005484 /* If the direct address is present, then this report is from
5485 * a LE Direct Advertising Report event. In that case it is
5486 * important to see if the address is matching the local
5487 * controller address.
5488 */
5489 if (direct_addr) {
5490 /* Only resolvable random addresses are valid for these
5491 * kind of reports and others can be ignored.
5492 */
5493 if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
5494 return;
5495
5496 /* If the controller is not using resolvable random
5497 * addresses, then this report can be ignored.
5498 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005499 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005500 return;
5501
5502 /* If the local IRK of the controller does not match
5503 * with the resolvable random address provided, then
5504 * this report can be ignored.
5505 */
5506 if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
5507 return;
5508 }
5509
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005510 /* Check if we need to convert to identity address */
5511 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
5512 if (irk) {
5513 bdaddr = &irk->bdaddr;
5514 bdaddr_type = irk->addr_type;
5515 }
5516
Szymon Janc082f2302018-04-03 13:40:06 +02005517 /* Check if we have been requested to connect to this device.
5518 *
5519 * direct_addr is set only for directed advertising reports (it is NULL
5520 * for advertising reports) and is already verified to be RPA above.
5521 */
5522 conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, type,
5523 direct_addr);
Alain Michauda2ec9052020-07-27 20:48:55 +00005524 if (!ext_adv && conn && type == LE_ADV_IND && len <= HCI_MAX_AD_LENGTH) {
Alfonso Acostafd45ada2014-10-07 08:44:11 +00005525 /* Store report for later inclusion by
5526 * mgmt_device_connected
5527 */
5528 memcpy(conn->le_adv_data, data, len);
5529 conn->le_adv_data_len = len;
5530 }
Johan Hedberg1c1abca2014-07-07 12:45:53 +03005531
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005532 /* Passive scanning shouldn't trigger any device found events,
5533 * except for devices marked as CONN_REPORT for which we do send
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02005534 * device found events, or advertisement monitoring requested.
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005535 */
Johan Hedbergca5c4be2014-03-25 10:30:46 +02005536 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005537 if (type == LE_ADV_DIRECT_IND)
5538 return;
5539
Johan Hedberg3a19b6f2014-07-15 08:07:59 +03005540 if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02005541 bdaddr, bdaddr_type) &&
5542 idr_is_empty(&hdev->adv_monitors_idr))
Johan Hedberg0d2bf132014-07-02 22:42:02 +03005543 return;
5544
5545 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
5546 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
5547 else
5548 flags = 0;
5549 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
5550 rssi, flags, data, len, NULL, 0);
Johan Hedberg97bf2e92014-07-04 12:37:16 +03005551 return;
Johan Hedbergca5c4be2014-03-25 10:30:46 +02005552 }
Johan Hedberg4af605d2014-03-24 10:48:00 +02005553
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005554 /* When receiving non-connectable or scannable undirected
5555 * advertising reports, this means that the remote device is
5556 * not connectable and then clearly indicate this in the
5557 * device found event.
5558 *
5559 * When receiving a scan response, then there is no way to
5560 * know if the remote device is connectable or not. However
5561 * since scan responses are merged with a previously seen
5562 * advertising report, the flags field from that report
5563 * will be used.
5564 *
5565 * In the really unlikely case that a controller get confused
5566 * and just sends a scan response event, then it is marked as
5567 * not connectable as well.
5568 */
5569 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
5570 type == LE_ADV_SCAN_RSP)
5571 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
5572 else
5573 flags = 0;
5574
Johan Hedbergb9a63282014-03-25 10:51:52 +02005575 /* If there's nothing pending either store the data from this
5576 * event or send an immediate device found event if the data
5577 * should not be stored for later.
5578 */
Alain Michauda2ec9052020-07-27 20:48:55 +00005579 if (!ext_adv && !has_pending_adv_report(hdev)) {
Johan Hedbergb9a63282014-03-25 10:51:52 +02005580 /* If the report will trigger a SCAN_REQ store it for
5581 * later merging.
5582 */
5583 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
5584 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005585 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005586 return;
5587 }
5588
5589 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005590 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005591 return;
5592 }
5593
Johan Hedberg474ee062014-03-25 14:34:59 +02005594 /* Check if the pending report is for the same device as the new one */
5595 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
5596 bdaddr_type == d->last_adv_addr_type);
5597
Johan Hedbergb9a63282014-03-25 10:51:52 +02005598 /* If the pending data doesn't match this report or this isn't a
5599 * scan response (e.g. we got a duplicate ADV_IND) then force
5600 * sending of the pending data.
5601 */
Johan Hedberg474ee062014-03-25 14:34:59 +02005602 if (type != LE_ADV_SCAN_RSP || !match) {
5603 /* Send out whatever is in the cache, but skip duplicates */
5604 if (!match)
5605 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergff5cd292014-03-25 14:40:52 +02005606 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005607 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergff5cd292014-03-25 14:40:52 +02005608 d->last_adv_data,
Johan Hedberg474ee062014-03-25 14:34:59 +02005609 d->last_adv_data_len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005610
5611 /* If the new report will trigger a SCAN_REQ store it for
5612 * later merging.
5613 */
Alain Michauda2ec9052020-07-27 20:48:55 +00005614 if (!ext_adv && (type == LE_ADV_IND ||
5615 type == LE_ADV_SCAN_IND)) {
Johan Hedbergb9a63282014-03-25 10:51:52 +02005616 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005617 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005618 return;
5619 }
5620
5621 /* The advertising reports cannot be merged, so clear
5622 * the pending report and send out a device found event.
5623 */
5624 clear_pending_adv_report(hdev);
Johan Hedberg5c5b93e2014-03-29 08:39:53 +02005625 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005626 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005627 return;
5628 }
5629
5630 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
5631 * the new event is a SCAN_RSP. We can therefore proceed with
5632 * sending a merged device found event.
5633 */
5634 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02005635 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
Marcel Holtmann42bd6a52014-07-01 14:11:19 +02005636 d->last_adv_data, d->last_adv_data_len, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02005637 clear_pending_adv_report(hdev);
Johan Hedberg4af605d2014-03-24 10:48:00 +02005638}
5639
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005640static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03005641{
Andre Guedese95beb42011-09-26 20:48:35 -03005642 u8 num_reports = skb->data[0];
5643 void *ptr = &skb->data[1];
Andre Guedes9aa04c92011-05-26 16:23:51 -03005644
Andre Guedesa4790db2014-02-26 20:21:47 -03005645 hci_dev_lock(hdev);
5646
Andre Guedese95beb42011-09-26 20:48:35 -03005647 while (num_reports--) {
5648 struct hci_ev_le_advertising_info *ev = ptr;
Johan Hedberg4af605d2014-03-24 10:48:00 +02005649 s8 rssi;
Andre Guedesa4790db2014-02-26 20:21:47 -03005650
Chriz Chowee649342018-04-20 15:46:24 +08005651 if (ev->length <= HCI_MAX_AD_LENGTH) {
5652 rssi = ev->data[ev->length];
5653 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
5654 ev->bdaddr_type, NULL, 0, rssi,
Alain Michauda2ec9052020-07-27 20:48:55 +00005655 ev->data, ev->length, false);
Chriz Chowee649342018-04-20 15:46:24 +08005656 } else {
5657 bt_dev_err(hdev, "Dropping invalid advertising data");
5658 }
Andre Guedes3c9e9192012-01-10 18:20:50 -03005659
Andre Guedese95beb42011-09-26 20:48:35 -03005660 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03005661 }
Andre Guedesa4790db2014-02-26 20:21:47 -03005662
5663 hci_dev_unlock(hdev);
Andre Guedes9aa04c92011-05-26 16:23:51 -03005664}
5665
Marcel Holtmann657cc642019-12-11 11:34:36 +01005666static u8 ext_evt_type_to_legacy(struct hci_dev *hdev, u16 evt_type)
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305667{
Jaganath Kanakkasseryb2cc9762018-07-19 17:09:38 +05305668 if (evt_type & LE_EXT_ADV_LEGACY_PDU) {
5669 switch (evt_type) {
5670 case LE_LEGACY_ADV_IND:
5671 return LE_ADV_IND;
5672 case LE_LEGACY_ADV_DIRECT_IND:
5673 return LE_ADV_DIRECT_IND;
5674 case LE_LEGACY_ADV_SCAN_IND:
5675 return LE_ADV_SCAN_IND;
5676 case LE_LEGACY_NONCONN_IND:
5677 return LE_ADV_NONCONN_IND;
5678 case LE_LEGACY_SCAN_RSP_ADV:
5679 case LE_LEGACY_SCAN_RSP_ADV_SCAN:
5680 return LE_ADV_SCAN_RSP;
5681 }
5682
Marcel Holtmann657cc642019-12-11 11:34:36 +01005683 goto invalid;
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305684 }
5685
Jaganath Kanakkasseryb2cc9762018-07-19 17:09:38 +05305686 if (evt_type & LE_EXT_ADV_CONN_IND) {
5687 if (evt_type & LE_EXT_ADV_DIRECT_IND)
5688 return LE_ADV_DIRECT_IND;
5689
5690 return LE_ADV_IND;
5691 }
5692
5693 if (evt_type & LE_EXT_ADV_SCAN_RSP)
5694 return LE_ADV_SCAN_RSP;
5695
5696 if (evt_type & LE_EXT_ADV_SCAN_IND)
5697 return LE_ADV_SCAN_IND;
5698
5699 if (evt_type == LE_EXT_ADV_NON_CONN_IND ||
5700 evt_type & LE_EXT_ADV_DIRECT_IND)
5701 return LE_ADV_NONCONN_IND;
5702
Marcel Holtmann657cc642019-12-11 11:34:36 +01005703invalid:
5704 bt_dev_err_ratelimited(hdev, "Unknown advertising packet type: 0x%02x",
5705 evt_type);
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305706
5707 return LE_ADV_INVALID;
5708}
5709
5710static void hci_le_ext_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
5711{
5712 u8 num_reports = skb->data[0];
5713 void *ptr = &skb->data[1];
5714
5715 hci_dev_lock(hdev);
5716
5717 while (num_reports--) {
5718 struct hci_ev_le_ext_adv_report *ev = ptr;
5719 u8 legacy_evt_type;
5720 u16 evt_type;
5721
5722 evt_type = __le16_to_cpu(ev->evt_type);
Marcel Holtmann657cc642019-12-11 11:34:36 +01005723 legacy_evt_type = ext_evt_type_to_legacy(hdev, evt_type);
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305724 if (legacy_evt_type != LE_ADV_INVALID) {
5725 process_adv_report(hdev, legacy_evt_type, &ev->bdaddr,
5726 ev->bdaddr_type, NULL, 0, ev->rssi,
Alain Michauda2ec9052020-07-27 20:48:55 +00005727 ev->data, ev->length,
5728 !(evt_type & LE_EXT_ADV_LEGACY_PDU));
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305729 }
5730
Jaganath Kanakkasserycd9151b2019-04-03 12:11:44 +05305731 ptr += sizeof(*ev) + ev->length;
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305732 }
5733
5734 hci_dev_unlock(hdev);
5735}
5736
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005737static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev,
5738 struct sk_buff *skb)
5739{
5740 struct hci_ev_le_remote_feat_complete *ev = (void *)skb->data;
5741 struct hci_conn *conn;
5742
5743 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5744
5745 hci_dev_lock(hdev);
5746
5747 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5748 if (conn) {
5749 if (!ev->status)
5750 memcpy(conn->features[0], ev->features, 8);
5751
5752 if (conn->state == BT_CONFIG) {
5753 __u8 status;
5754
5755 /* If the local controller supports slave-initiated
5756 * features exchange, but the remote controller does
5757 * not, then it is possible that the error code 0x1a
5758 * for unsupported remote feature gets returned.
5759 *
5760 * In this specific case, allow the connection to
5761 * transition into connected state and mark it as
5762 * successful.
5763 */
5764 if ((hdev->le_features[0] & HCI_LE_SLAVE_FEATURES) &&
5765 !conn->out && ev->status == 0x1a)
5766 status = 0x00;
5767 else
5768 status = ev->status;
5769
5770 conn->state = BT_CONNECTED;
5771 hci_connect_cfm(conn, status);
5772 hci_conn_drop(conn);
5773 }
5774 }
5775
5776 hci_dev_unlock(hdev);
5777}
5778
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005779static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005780{
5781 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
5782 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005783 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005784 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03005785 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005786
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03005787 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005788
5789 hci_dev_lock(hdev);
5790
5791 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005792 if (conn == NULL)
5793 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005794
Johan Hedbergf3a73d92014-05-29 15:02:59 +03005795 ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
Johan Hedberg5378bc52014-05-29 14:00:39 +03005796 if (!ltk)
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005797 goto not_found;
5798
Johan Hedberg5378bc52014-05-29 14:00:39 +03005799 if (smp_ltk_is_sc(ltk)) {
5800 /* With SC both EDiv and Rand are set to zero */
5801 if (ev->ediv || ev->rand)
5802 goto not_found;
5803 } else {
5804 /* For non-SC keys check that EDiv and Rand match */
5805 if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
5806 goto not_found;
5807 }
5808
Johan Hedberg8b76ce32015-06-08 18:14:39 +03005809 memcpy(cp.ltk, ltk->val, ltk->enc_size);
5810 memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005811 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03005812
Johan Hedberga6f78332014-09-10 17:37:45 -07005813 conn->pending_sec_level = smp_ltk_sec_level(ltk);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005814
Andre Guedes89cbb4d2013-07-31 16:25:29 -03005815 conn->enc_key_size = ltk->enc_size;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005816
5817 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
5818
Claudio Takahasi5981a882013-07-25 16:34:24 -03005819 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
5820 * temporary key used to encrypt a connection following
5821 * pairing. It is used during the Encrypted Session Setup to
5822 * distribute the keys. Later, security can be re-established
5823 * using a distributed LTK.
5824 */
Johan Hedberg2ceba532014-06-16 19:25:16 +03005825 if (ltk->type == SMP_STK) {
Johan Hedbergfe59a052014-07-01 19:14:12 +03005826 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Johan Hedberg970d0f12014-11-13 14:37:47 +02005827 list_del_rcu(&ltk->list);
5828 kfree_rcu(ltk, rcu);
Johan Hedbergfe59a052014-07-01 19:14:12 +03005829 } else {
5830 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03005831 }
5832
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005833 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03005834
5835 return;
5836
5837not_found:
5838 neg.handle = ev->handle;
5839 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
5840 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005841}
5842
Andre Guedes8e75b462014-07-01 18:10:08 -03005843static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
5844 u8 reason)
5845{
5846 struct hci_cp_le_conn_param_req_neg_reply cp;
5847
5848 cp.handle = cpu_to_le16(handle);
5849 cp.reason = reason;
5850
5851 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
5852 &cp);
5853}
5854
5855static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
5856 struct sk_buff *skb)
5857{
5858 struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
5859 struct hci_cp_le_conn_param_req_reply cp;
5860 struct hci_conn *hcon;
5861 u16 handle, min, max, latency, timeout;
5862
5863 handle = le16_to_cpu(ev->handle);
5864 min = le16_to_cpu(ev->interval_min);
5865 max = le16_to_cpu(ev->interval_max);
5866 latency = le16_to_cpu(ev->latency);
5867 timeout = le16_to_cpu(ev->timeout);
5868
5869 hcon = hci_conn_hash_lookup_handle(hdev, handle);
5870 if (!hcon || hcon->state != BT_CONNECTED)
5871 return send_conn_param_neg_reply(hdev, handle,
5872 HCI_ERROR_UNKNOWN_CONN_ID);
5873
5874 if (hci_check_conn_params(min, max, latency, timeout))
5875 return send_conn_param_neg_reply(hdev, handle,
5876 HCI_ERROR_INVALID_LL_PARAMS);
5877
Johan Hedberg40bef302014-07-16 11:42:27 +03005878 if (hcon->role == HCI_ROLE_MASTER) {
Johan Hedberg348d50b2014-07-02 17:37:30 +03005879 struct hci_conn_params *params;
Johan Hedbergf4869e22014-07-02 17:37:32 +03005880 u8 store_hint;
Johan Hedberg348d50b2014-07-02 17:37:30 +03005881
5882 hci_dev_lock(hdev);
5883
5884 params = hci_conn_params_lookup(hdev, &hcon->dst,
5885 hcon->dst_type);
5886 if (params) {
5887 params->conn_min_interval = min;
5888 params->conn_max_interval = max;
5889 params->conn_latency = latency;
5890 params->supervision_timeout = timeout;
Johan Hedbergf4869e22014-07-02 17:37:32 +03005891 store_hint = 0x01;
Meng Yu149b3f12021-04-01 14:50:39 +08005892 } else {
Johan Hedbergf4869e22014-07-02 17:37:32 +03005893 store_hint = 0x00;
Johan Hedberg348d50b2014-07-02 17:37:30 +03005894 }
5895
5896 hci_dev_unlock(hdev);
5897
Johan Hedbergf4869e22014-07-02 17:37:32 +03005898 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
5899 store_hint, min, max, latency, timeout);
Johan Hedberg348d50b2014-07-02 17:37:30 +03005900 }
Andre Guedesffb5a8272014-07-01 18:10:11 -03005901
Andre Guedes8e75b462014-07-01 18:10:08 -03005902 cp.handle = ev->handle;
5903 cp.interval_min = ev->interval_min;
5904 cp.interval_max = ev->interval_max;
5905 cp.latency = ev->latency;
5906 cp.timeout = ev->timeout;
5907 cp.min_ce_len = 0;
5908 cp.max_ce_len = 0;
5909
5910 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
5911}
5912
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005913static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
5914 struct sk_buff *skb)
5915{
5916 u8 num_reports = skb->data[0];
Peilin Yef7e0e8b2020-09-09 03:17:00 -04005917 struct hci_ev_le_direct_adv_info *ev = (void *)&skb->data[1];
5918
5919 if (!num_reports || skb->len < num_reports * sizeof(*ev) + 1)
5920 return;
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005921
5922 hci_dev_lock(hdev);
5923
Peilin Yef7e0e8b2020-09-09 03:17:00 -04005924 for (; num_reports; num_reports--, ev++)
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005925 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
5926 ev->bdaddr_type, &ev->direct_addr,
Alain Michauda2ec9052020-07-27 20:48:55 +00005927 ev->direct_addr_type, ev->rssi, NULL, 0,
5928 false);
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005929
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005930 hci_dev_unlock(hdev);
5931}
5932
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08005933static void hci_le_phy_update_evt(struct hci_dev *hdev, struct sk_buff *skb)
5934{
5935 struct hci_ev_le_phy_update_complete *ev = (void *) skb->data;
5936 struct hci_conn *conn;
5937
5938 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5939
Ayush Garg87df8bc2021-03-17 16:52:14 +05305940 if (ev->status)
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08005941 return;
5942
5943 hci_dev_lock(hdev);
5944
5945 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5946 if (!conn)
5947 goto unlock;
5948
5949 conn->le_tx_phy = ev->tx_phy;
5950 conn->le_rx_phy = ev->rx_phy;
5951
5952unlock:
5953 hci_dev_unlock(hdev);
5954}
5955
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005956static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03005957{
5958 struct hci_ev_le_meta *le_ev = (void *) skb->data;
5959
5960 skb_pull(skb, sizeof(*le_ev));
5961
5962 switch (le_ev->subevent) {
5963 case HCI_EV_LE_CONN_COMPLETE:
5964 hci_le_conn_complete_evt(hdev, skb);
5965 break;
5966
Marcel Holtmann1855d922014-06-23 11:40:05 +02005967 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
5968 hci_le_conn_update_complete_evt(hdev, skb);
5969 break;
5970
Andre Guedes9aa04c92011-05-26 16:23:51 -03005971 case HCI_EV_LE_ADVERTISING_REPORT:
5972 hci_le_adv_report_evt(hdev, skb);
5973 break;
5974
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07005975 case HCI_EV_LE_REMOTE_FEAT_COMPLETE:
5976 hci_le_remote_feat_complete_evt(hdev, skb);
5977 break;
5978
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03005979 case HCI_EV_LE_LTK_REQ:
5980 hci_le_ltk_request_evt(hdev, skb);
5981 break;
5982
Andre Guedes8e75b462014-07-01 18:10:08 -03005983 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
5984 hci_le_remote_conn_param_req_evt(hdev, skb);
5985 break;
5986
Marcel Holtmann2f010b52014-12-05 16:20:13 +01005987 case HCI_EV_LE_DIRECT_ADV_REPORT:
5988 hci_le_direct_adv_report_evt(hdev, skb);
5989 break;
5990
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08005991 case HCI_EV_LE_PHY_UPDATE_COMPLETE:
5992 hci_le_phy_update_evt(hdev, skb);
5993 break;
5994
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05305995 case HCI_EV_LE_EXT_ADV_REPORT:
5996 hci_le_ext_adv_report_evt(hdev, skb);
5997 break;
5998
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02005999 case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
6000 hci_le_enh_conn_complete_evt(hdev, skb);
6001 break;
6002
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306003 case HCI_EV_LE_EXT_ADV_SET_TERM:
6004 hci_le_ext_adv_term_evt(hdev, skb);
6005 break;
6006
Ville Tervofcd89c02011-02-10 22:38:47 -03006007 default:
6008 break;
6009 }
6010}
6011
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006012static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
6013 u8 event, struct sk_buff *skb)
6014{
6015 struct hci_ev_cmd_complete *ev;
6016 struct hci_event_hdr *hdr;
6017
6018 if (!skb)
6019 return false;
6020
6021 if (skb->len < sizeof(*hdr)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006022 bt_dev_err(hdev, "too short HCI event");
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006023 return false;
6024 }
6025
6026 hdr = (void *) skb->data;
6027 skb_pull(skb, HCI_EVENT_HDR_SIZE);
6028
6029 if (event) {
6030 if (hdr->evt != event)
6031 return false;
6032 return true;
6033 }
6034
Johan Hedberg1629db9c2018-11-27 11:37:46 +02006035 /* Check if request ended in Command Status - no way to retreive
6036 * any extra parameters in this case.
6037 */
6038 if (hdr->evt == HCI_EV_CMD_STATUS)
6039 return false;
6040
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006041 if (hdr->evt != HCI_EV_CMD_COMPLETE) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006042 bt_dev_err(hdev, "last event is not cmd complete (0x%2.2x)",
6043 hdr->evt);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006044 return false;
6045 }
6046
6047 if (skb->len < sizeof(*ev)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006048 bt_dev_err(hdev, "too short cmd_complete event");
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006049 return false;
6050 }
6051
6052 ev = (void *) skb->data;
6053 skb_pull(skb, sizeof(*ev));
6054
6055 if (opcode != __le16_to_cpu(ev->opcode)) {
6056 BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
6057 __le16_to_cpu(ev->opcode));
6058 return false;
6059 }
6060
6061 return true;
6062}
6063
Abhishek Pandit-Subedi2f202162020-09-11 14:07:13 -07006064static void hci_store_wake_reason(struct hci_dev *hdev, u8 event,
6065 struct sk_buff *skb)
6066{
6067 struct hci_ev_le_advertising_info *adv;
6068 struct hci_ev_le_direct_adv_info *direct_adv;
6069 struct hci_ev_le_ext_adv_report *ext_adv;
6070 const struct hci_ev_conn_complete *conn_complete = (void *)skb->data;
6071 const struct hci_ev_conn_request *conn_request = (void *)skb->data;
6072
6073 hci_dev_lock(hdev);
6074
6075 /* If we are currently suspended and this is the first BT event seen,
6076 * save the wake reason associated with the event.
6077 */
6078 if (!hdev->suspended || hdev->wake_reason)
6079 goto unlock;
6080
6081 /* Default to remote wake. Values for wake_reason are documented in the
6082 * Bluez mgmt api docs.
6083 */
6084 hdev->wake_reason = MGMT_WAKE_REASON_REMOTE_WAKE;
6085
6086 /* Once configured for remote wakeup, we should only wake up for
6087 * reconnections. It's useful to see which device is waking us up so
6088 * keep track of the bdaddr of the connection event that woke us up.
6089 */
6090 if (event == HCI_EV_CONN_REQUEST) {
6091 bacpy(&hdev->wake_addr, &conn_complete->bdaddr);
6092 hdev->wake_addr_type = BDADDR_BREDR;
6093 } else if (event == HCI_EV_CONN_COMPLETE) {
6094 bacpy(&hdev->wake_addr, &conn_request->bdaddr);
6095 hdev->wake_addr_type = BDADDR_BREDR;
6096 } else if (event == HCI_EV_LE_META) {
6097 struct hci_ev_le_meta *le_ev = (void *)skb->data;
6098 u8 subevent = le_ev->subevent;
6099 u8 *ptr = &skb->data[sizeof(*le_ev)];
6100 u8 num_reports = *ptr;
6101
6102 if ((subevent == HCI_EV_LE_ADVERTISING_REPORT ||
6103 subevent == HCI_EV_LE_DIRECT_ADV_REPORT ||
6104 subevent == HCI_EV_LE_EXT_ADV_REPORT) &&
6105 num_reports) {
6106 adv = (void *)(ptr + 1);
6107 direct_adv = (void *)(ptr + 1);
6108 ext_adv = (void *)(ptr + 1);
6109
6110 switch (subevent) {
6111 case HCI_EV_LE_ADVERTISING_REPORT:
6112 bacpy(&hdev->wake_addr, &adv->bdaddr);
6113 hdev->wake_addr_type = adv->bdaddr_type;
6114 break;
6115 case HCI_EV_LE_DIRECT_ADV_REPORT:
6116 bacpy(&hdev->wake_addr, &direct_adv->bdaddr);
6117 hdev->wake_addr_type = direct_adv->bdaddr_type;
6118 break;
6119 case HCI_EV_LE_EXT_ADV_REPORT:
6120 bacpy(&hdev->wake_addr, &ext_adv->bdaddr);
6121 hdev->wake_addr_type = ext_adv->bdaddr_type;
6122 break;
6123 }
6124 }
6125 } else {
6126 hdev->wake_reason = MGMT_WAKE_REASON_UNEXPECTED;
6127 }
6128
6129unlock:
6130 hci_dev_unlock(hdev);
6131}
6132
Linus Torvalds1da177e2005-04-16 15:20:36 -07006133void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
6134{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006135 struct hci_event_hdr *hdr = (void *) skb->data;
Johan Hedberge62144872015-04-02 13:41:08 +03006136 hci_req_complete_t req_complete = NULL;
6137 hci_req_complete_skb_t req_complete_skb = NULL;
6138 struct sk_buff *orig_skb = NULL;
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006139 u8 status = 0, event = hdr->evt, req_evt = 0;
Johan Hedberge62144872015-04-02 13:41:08 +03006140 u16 opcode = HCI_OP_NOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006141
Alain Michaud08bb4da2020-03-03 15:55:34 +00006142 if (!event) {
6143 bt_dev_warn(hdev, "Received unexpected HCI Event 00000000");
6144 goto done;
6145 }
6146
Marcel Holtmann242c0eb2015-10-25 22:45:53 +01006147 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->hci.req_event == event) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02006148 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
Johan Hedberge62144872015-04-02 13:41:08 +03006149 opcode = __le16_to_cpu(cmd_hdr->opcode);
6150 hci_req_cmd_complete(hdev, opcode, status, &req_complete,
6151 &req_complete_skb);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006152 req_evt = event;
Johan Hedberg02350a72013-04-03 21:50:29 +03006153 }
6154
Johan Hedberge62144872015-04-02 13:41:08 +03006155 /* If it looks like we might end up having to call
6156 * req_complete_skb, store a pristine copy of the skb since the
6157 * various handlers may modify the original one through
6158 * skb_pull() calls, etc.
6159 */
6160 if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
6161 event == HCI_EV_CMD_COMPLETE)
6162 orig_skb = skb_clone(skb, GFP_KERNEL);
6163
6164 skb_pull(skb, HCI_EVENT_HDR_SIZE);
6165
Abhishek Pandit-Subedi2f202162020-09-11 14:07:13 -07006166 /* Store wake reason if we're suspended */
6167 hci_store_wake_reason(hdev, event, skb);
6168
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006169 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 case HCI_EV_INQUIRY_COMPLETE:
6171 hci_inquiry_complete_evt(hdev, skb);
6172 break;
6173
6174 case HCI_EV_INQUIRY_RESULT:
6175 hci_inquiry_result_evt(hdev, skb);
6176 break;
6177
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006178 case HCI_EV_CONN_COMPLETE:
6179 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02006180 break;
6181
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182 case HCI_EV_CONN_REQUEST:
6183 hci_conn_request_evt(hdev, skb);
6184 break;
6185
Linus Torvalds1da177e2005-04-16 15:20:36 -07006186 case HCI_EV_DISCONN_COMPLETE:
6187 hci_disconn_complete_evt(hdev, skb);
6188 break;
6189
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190 case HCI_EV_AUTH_COMPLETE:
6191 hci_auth_complete_evt(hdev, skb);
6192 break;
6193
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006194 case HCI_EV_REMOTE_NAME:
6195 hci_remote_name_evt(hdev, skb);
6196 break;
6197
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 case HCI_EV_ENCRYPT_CHANGE:
6199 hci_encrypt_change_evt(hdev, skb);
6200 break;
6201
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006202 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
6203 hci_change_link_key_complete_evt(hdev, skb);
6204 break;
6205
6206 case HCI_EV_REMOTE_FEATURES:
6207 hci_remote_features_evt(hdev, skb);
6208 break;
6209
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006210 case HCI_EV_CMD_COMPLETE:
Johan Hedberge62144872015-04-02 13:41:08 +03006211 hci_cmd_complete_evt(hdev, skb, &opcode, &status,
6212 &req_complete, &req_complete_skb);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006213 break;
6214
6215 case HCI_EV_CMD_STATUS:
Johan Hedberge62144872015-04-02 13:41:08 +03006216 hci_cmd_status_evt(hdev, skb, &opcode, &status, &req_complete,
6217 &req_complete_skb);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006218 break;
6219
Marcel Holtmann24dfa342014-11-02 02:56:41 +01006220 case HCI_EV_HARDWARE_ERROR:
6221 hci_hardware_error_evt(hdev, skb);
6222 break;
6223
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006224 case HCI_EV_ROLE_CHANGE:
6225 hci_role_change_evt(hdev, skb);
6226 break;
6227
6228 case HCI_EV_NUM_COMP_PKTS:
6229 hci_num_comp_pkts_evt(hdev, skb);
6230 break;
6231
6232 case HCI_EV_MODE_CHANGE:
6233 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 break;
6235
6236 case HCI_EV_PIN_CODE_REQ:
6237 hci_pin_code_request_evt(hdev, skb);
6238 break;
6239
6240 case HCI_EV_LINK_KEY_REQ:
6241 hci_link_key_request_evt(hdev, skb);
6242 break;
6243
6244 case HCI_EV_LINK_KEY_NOTIFY:
6245 hci_link_key_notify_evt(hdev, skb);
6246 break;
6247
6248 case HCI_EV_CLOCK_OFFSET:
6249 hci_clock_offset_evt(hdev, skb);
6250 break;
6251
Marcel Holtmanna8746412008-07-14 20:13:46 +02006252 case HCI_EV_PKT_TYPE_CHANGE:
6253 hci_pkt_type_change_evt(hdev, skb);
6254 break;
6255
Marcel Holtmann85a1e932005-08-09 20:28:02 -07006256 case HCI_EV_PSCAN_REP_MODE:
6257 hci_pscan_rep_mode_evt(hdev, skb);
6258 break;
6259
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006260 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
6261 hci_inquiry_result_with_rssi_evt(hdev, skb);
6262 break;
6263
6264 case HCI_EV_REMOTE_EXT_FEATURES:
6265 hci_remote_ext_features_evt(hdev, skb);
6266 break;
6267
6268 case HCI_EV_SYNC_CONN_COMPLETE:
6269 hci_sync_conn_complete_evt(hdev, skb);
6270 break;
6271
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006272 case HCI_EV_EXTENDED_INQUIRY_RESULT:
6273 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006274 break;
6275
Johan Hedberg1c2e0042012-06-08 23:31:13 +08006276 case HCI_EV_KEY_REFRESH_COMPLETE:
6277 hci_key_refresh_complete_evt(hdev, skb);
6278 break;
6279
Marcel Holtmann04936842008-07-14 20:13:48 +02006280 case HCI_EV_IO_CAPA_REQUEST:
6281 hci_io_capa_request_evt(hdev, skb);
6282 break;
6283
Johan Hedberg03b555e2011-01-04 15:40:05 +02006284 case HCI_EV_IO_CAPA_REPLY:
6285 hci_io_capa_reply_evt(hdev, skb);
6286 break;
6287
Johan Hedberga5c29682011-02-19 12:05:57 -03006288 case HCI_EV_USER_CONFIRM_REQUEST:
6289 hci_user_confirm_request_evt(hdev, skb);
6290 break;
6291
Brian Gix1143d452011-11-23 08:28:34 -08006292 case HCI_EV_USER_PASSKEY_REQUEST:
6293 hci_user_passkey_request_evt(hdev, skb);
6294 break;
6295
Johan Hedberg92a25252012-09-06 18:39:26 +03006296 case HCI_EV_USER_PASSKEY_NOTIFY:
6297 hci_user_passkey_notify_evt(hdev, skb);
6298 break;
6299
6300 case HCI_EV_KEYPRESS_NOTIFY:
6301 hci_keypress_notify_evt(hdev, skb);
6302 break;
6303
Marcel Holtmann04936842008-07-14 20:13:48 +02006304 case HCI_EV_SIMPLE_PAIR_COMPLETE:
6305 hci_simple_pair_complete_evt(hdev, skb);
6306 break;
6307
Marcel Holtmann41a96212008-07-14 20:13:48 +02006308 case HCI_EV_REMOTE_HOST_FEATURES:
6309 hci_remote_host_features_evt(hdev, skb);
6310 break;
6311
Ville Tervofcd89c02011-02-10 22:38:47 -03006312 case HCI_EV_LE_META:
6313 hci_le_meta_evt(hdev, skb);
6314 break;
6315
Szymon Janc2763eda2011-03-22 13:12:22 +01006316 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
6317 hci_remote_oob_data_request_evt(hdev, skb);
6318 break;
6319
Arron Wanga77a6a12015-07-24 17:13:15 +08006320#if IS_ENABLED(CONFIG_BT_HS)
6321 case HCI_EV_CHANNEL_SELECTED:
6322 hci_chan_selected_evt(hdev, skb);
6323 break;
6324
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03006325 case HCI_EV_PHY_LINK_COMPLETE:
6326 hci_phy_link_complete_evt(hdev, skb);
6327 break;
6328
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03006329 case HCI_EV_LOGICAL_LINK_COMPLETE:
6330 hci_loglink_complete_evt(hdev, skb);
6331 break;
6332
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02006333 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
6334 hci_disconn_loglink_complete_evt(hdev, skb);
6335 break;
6336
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02006337 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
6338 hci_disconn_phylink_complete_evt(hdev, skb);
6339 break;
Arron Wanga77a6a12015-07-24 17:13:15 +08006340#endif
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02006341
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02006342 case HCI_EV_NUM_COMP_BLOCKS:
6343 hci_num_comp_blocks_evt(hdev, skb);
6344 break;
6345
Miao-chen Chou145373c2020-04-03 21:44:01 +02006346 case HCI_EV_VENDOR:
6347 msft_vendor_evt(hdev, skb);
6348 break;
6349
Marcel Holtmanna9de9242007-10-20 13:33:56 +02006350 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03006351 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 break;
6353 }
6354
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006355 if (req_complete) {
Johan Hedberge62144872015-04-02 13:41:08 +03006356 req_complete(hdev, status, opcode);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006357 } else if (req_complete_skb) {
6358 if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
6359 kfree_skb(orig_skb);
6360 orig_skb = NULL;
6361 }
Johan Hedberge62144872015-04-02 13:41:08 +03006362 req_complete_skb(hdev, status, opcode, orig_skb);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006363 }
Johan Hedberge62144872015-04-02 13:41:08 +03006364
Alain Michaud08bb4da2020-03-03 15:55:34 +00006365done:
Johan Hedberge62144872015-04-02 13:41:08 +03006366 kfree_skb(orig_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367 kfree_skb(skb);
6368 hdev->stat.evt_rx++;
6369}