blob: 6560dca8c5ce5e7b93eec6b7eddbc89bd7268637 [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI event handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/unaligned.h>
28
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
Mikel Astizf0d6a0e2012-08-09 09:52:30 +020031#include <net/bluetooth/mgmt.h>
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070032
Johan Hedberg0857dd32014-12-19 13:40:20 +020033#include "hci_request.h"
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +010034#include "hci_debugfs.h"
Marcel Holtmann70247282013-10-10 14:54:15 -070035#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070036#include "amp.h"
Johan Hedberg2ceba532014-06-16 19:25:16 +030037#include "smp.h"
Miao-chen Chou145373c2020-04-03 21:44:01 +020038#include "msft.h"
Luiz Augusto von Dentz01ce70b2021-09-20 15:59:37 -070039#include "eir.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Marcel Holtmannaa5b0342015-01-27 16:04:33 -080041#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
42 "\x00\x00\x00\x00\x00\x00\x00\x00"
43
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -070044#define secs_to_jiffies(_secs) msecs_to_jiffies((_secs) * 1000)
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* Handle HCI Event packets */
47
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -080048static void *hci_ev_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
49 u8 ev, size_t len)
50{
51 void *data;
52
53 data = skb_pull_data(skb, len);
54 if (!data)
55 bt_dev_err(hdev, "Malformed Event: 0x%2.2x", ev);
56
57 return data;
58}
59
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -080060static void *hci_cc_skb_pull(struct hci_dev *hdev, struct sk_buff *skb,
61 u16 op, size_t len)
62{
63 void *data;
64
65 data = skb_pull_data(skb, len);
66 if (!data)
67 bt_dev_err(hdev, "Malformed Command Complete: 0x%4.4x", op);
68
69 return data;
70}
71
Sonny Sasakaadf1d692020-05-06 12:55:03 -070072static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb,
73 u8 *new_status)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -080075 struct hci_ev_status *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -080077 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_INQUIRY_CANCEL, sizeof(*rp));
78 if (!rp)
79 return;
80
81 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Sonny Sasakaadf1d692020-05-06 12:55:03 -070083 /* It is possible that we receive Inquiry Complete event right
84 * before we receive Inquiry Cancel Command Complete event, in
85 * which case the latter event should have status of Command
86 * Disallowed (0x0c). This should not be treated as error, since
87 * we actually achieve what Inquiry Cancel wants to achieve,
88 * which is to end the last Inquiry session.
89 */
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -080090 if (rp->status == 0x0c && !test_bit(HCI_INQUIRY, &hdev->flags)) {
Sonny Sasakaadf1d692020-05-06 12:55:03 -070091 bt_dev_warn(hdev, "Ignoring error of Inquiry Cancel command");
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -080092 rp->status = 0x00;
Sonny Sasakaadf1d692020-05-06 12:55:03 -070093 }
94
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -080095 *new_status = rp->status;
Sonny Sasakaadf1d692020-05-06 12:55:03 -070096
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -080097 if (rp->status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020098 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Andre Guedes89352e72011-11-04 14:16:53 -0300100 clear_bit(HCI_INQUIRY, &hdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100101 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -0300102 wake_up_bit(&hdev->flags, HCI_INQUIRY);
Andre Guedes89352e72011-11-04 14:16:53 -0300103
Johan Hedberg50143a42014-06-10 14:05:57 +0300104 hci_dev_lock(hdev);
Jakub Pawlowski168b8a22015-10-16 10:07:49 +0300105 /* Set discovery state to stopped if we're not doing LE active
106 * scanning.
107 */
108 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
109 hdev->le_scan_type != LE_SCAN_ACTIVE)
110 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg50143a42014-06-10 14:05:57 +0300111 hci_dev_unlock(hdev);
112
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200113 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Andre Guedes4d934832012-03-21 00:03:35 -0300116static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
117{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800118 struct hci_ev_status *rp;
Andre Guedes4d934832012-03-21 00:03:35 -0300119
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800120 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_PERIODIC_INQ, sizeof(*rp));
121 if (!rp)
122 return;
Andre Guedesae854a72012-03-21 00:03:36 -0300123
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800124 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
125
126 if (rp->status)
Andre Guedesae854a72012-03-21 00:03:36 -0300127 return;
128
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700129 hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
Andre Guedes4d934832012-03-21 00:03:35 -0300130}
131
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200132static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800134 struct hci_ev_status *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200135
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800136 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_EXIT_PERIODIC_INQ, sizeof(*rp));
137 if (!rp)
138 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200139
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800140 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
141
142 if (rp->status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200143 return;
144
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700145 hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
Andre Guedesae854a72012-03-21 00:03:36 -0300146
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200147 hci_conn_check_pending(hdev);
148}
149
Gustavo Padovan807deac2012-05-17 00:36:24 -0300150static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
151 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200152{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800153 struct hci_ev_status *rp;
154
155 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_REMOTE_NAME_REQ_CANCEL,
156 sizeof(*rp));
157 if (!rp)
158 return;
159
160 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200161}
162
163static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
164{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800165 struct hci_rp_role_discovery *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800168 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_ROLE_DISCOVERY, sizeof(*rp));
169 if (!rp)
170 return;
171
172 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200174 if (rp->status)
175 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200177 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200179 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Johan Hedberg40bef302014-07-16 11:42:27 +0300180 if (conn)
181 conn->role = rp->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200182
183 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200186static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
187{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800188 struct hci_rp_read_link_policy *rp;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200189 struct hci_conn *conn;
190
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800191 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_LINK_POLICY, sizeof(*rp));
192 if (!rp)
193 return;
194
195 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200196
197 if (rp->status)
198 return;
199
200 hci_dev_lock(hdev);
201
202 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
203 if (conn)
204 conn->link_policy = __le16_to_cpu(rp->policy);
205
206 hci_dev_unlock(hdev);
207}
208
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200209static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800211 struct hci_rp_write_link_policy *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200212 struct hci_conn *conn;
213 void *sent;
214
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800215 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_LINK_POLICY, sizeof(*rp));
216 if (!rp)
217 return;
218
219 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200220
221 if (rp->status)
222 return;
223
224 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
225 if (!sent)
226 return;
227
228 hci_dev_lock(hdev);
229
230 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200231 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700232 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200233
234 hci_dev_unlock(hdev);
235}
236
Gustavo Padovan807deac2012-05-17 00:36:24 -0300237static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
238 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200239{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800240 struct hci_rp_read_def_link_policy *rp;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200241
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800242 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_DEF_LINK_POLICY,
243 sizeof(*rp));
244 if (!rp)
245 return;
246
247 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200248
249 if (rp->status)
250 return;
251
252 hdev->link_policy = __le16_to_cpu(rp->policy);
253}
254
Gustavo Padovan807deac2012-05-17 00:36:24 -0300255static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
256 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200257{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800258 struct hci_ev_status *rp;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200259 void *sent;
260
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800261 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_DEF_LINK_POLICY,
262 sizeof(*rp));
263 if (!rp)
264 return;
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200265
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800266 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
267
268 if (rp->status)
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200269 return;
270
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200271 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
272 if (!sent)
273 return;
274
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200275 hdev->link_policy = get_unaligned_le16(sent);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200276}
277
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200278static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
279{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800280 struct hci_ev_status *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200281
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800282 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_RESET, sizeof(*rp));
283 if (!rp)
284 return;
285
286 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200287
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300288 clear_bit(HCI_RESET, &hdev->flags);
289
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800290 if (rp->status)
Marcel Holtmann8761f9d2014-11-02 02:45:58 +0100291 return;
292
Johan Hedberga297e972012-02-21 17:55:47 +0200293 /* Reset all non-persistent flags */
Marcel Holtmanneacb44d2015-03-13 09:04:17 -0700294 hci_dev_clear_volatile_flags(hdev);
Andre Guedes69775ff2012-02-23 16:50:05 +0200295
Johan Hedberg39c5d972015-01-28 19:56:01 +0200296 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
297
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100298 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
299 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
Johan Hedberg3f0f5242012-11-08 01:23:00 +0100300
301 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
302 hdev->adv_data_len = 0;
Marcel Holtmannf8e808b2013-10-16 00:16:47 -0700303
304 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
305 hdev->scan_rsp_data_len = 0;
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700306
Marcel Holtmann533553f2014-03-21 12:18:10 -0700307 hdev->le_scan_type = LE_SCAN_PASSIVE;
308
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700309 hdev->ssp_debug_mode = 0;
Marcel Holtmanna4d55042014-10-29 23:37:53 +0100310
Archie Pusaka3d4f9c02021-06-04 16:26:27 +0800311 hci_bdaddr_list_clear(&hdev->le_accept_list);
Ankit Navikcfdb0c22018-06-29 12:12:50 +0530312 hci_bdaddr_list_clear(&hdev->le_resolv_list);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200313}
314
Marcel Holtmannc2f0f972015-01-12 09:21:25 -0800315static void hci_cc_read_stored_link_key(struct hci_dev *hdev,
316 struct sk_buff *skb)
317{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800318 struct hci_rp_read_stored_link_key *rp;
Marcel Holtmannc2f0f972015-01-12 09:21:25 -0800319 struct hci_cp_read_stored_link_key *sent;
320
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800321 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_STORED_LINK_KEY,
322 sizeof(*rp));
323 if (!rp)
324 return;
325
326 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmannc2f0f972015-01-12 09:21:25 -0800327
328 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_STORED_LINK_KEY);
329 if (!sent)
330 return;
331
332 if (!rp->status && sent->read_all == 0x01) {
Luiz Augusto von Dentze88422b2021-11-24 16:16:12 -0800333 hdev->stored_max_keys = le16_to_cpu(rp->max_keys);
334 hdev->stored_num_keys = le16_to_cpu(rp->num_keys);
Marcel Holtmannc2f0f972015-01-12 09:21:25 -0800335 }
336}
337
Marcel Holtmanna93661202015-01-12 09:21:28 -0800338static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
339 struct sk_buff *skb)
340{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800341 struct hci_rp_delete_stored_link_key *rp;
Marcel Holtmanna93661202015-01-12 09:21:28 -0800342
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800343 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_DELETE_STORED_LINK_KEY,
344 sizeof(*rp));
345 if (!rp)
346 return;
347
348 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna93661202015-01-12 09:21:28 -0800349
350 if (rp->status)
351 return;
352
353 if (rp->num_keys <= hdev->stored_num_keys)
Luiz Augusto von Dentz79786562021-11-24 16:16:13 -0800354 hdev->stored_num_keys -= le16_to_cpu(rp->num_keys);
Marcel Holtmanna93661202015-01-12 09:21:28 -0800355 else
356 hdev->stored_num_keys = 0;
357}
358
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200359static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
360{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800361 struct hci_ev_status *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200362 void *sent;
363
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800364 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_LOCAL_NAME, sizeof(*rp));
365 if (!rp)
366 return;
367
368 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200369
370 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
371 if (!sent)
372 return;
373
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200374 hci_dev_lock(hdev);
375
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700376 if (hci_dev_test_flag(hdev, HCI_MGMT))
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800377 mgmt_set_local_name_complete(hdev, sent, rp->status);
378 else if (!rp->status)
Johan Hedberg28cc7bd2012-02-22 21:06:55 +0200379 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200380
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200381 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200382}
383
384static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
385{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800386 struct hci_rp_read_local_name *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200387
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800388 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_LOCAL_NAME, sizeof(*rp));
389 if (!rp)
390 return;
391
392 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200393
394 if (rp->status)
395 return;
396
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700397 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
398 hci_dev_test_flag(hdev, HCI_CONFIG))
Johan Hedbergdb99b5f2012-02-22 20:14:22 +0200399 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200400}
401
402static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
403{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800404 struct hci_ev_status *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200405 void *sent;
406
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800407 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_AUTH_ENABLE, sizeof(*rp));
408 if (!rp)
409 return;
410
411 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200412
413 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
414 if (!sent)
415 return;
416
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530417 hci_dev_lock(hdev);
418
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800419 if (!rp->status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200420 __u8 param = *((__u8 *) sent);
421
422 if (param == AUTH_ENABLED)
423 set_bit(HCI_AUTH, &hdev->flags);
424 else
425 clear_bit(HCI_AUTH, &hdev->flags);
426 }
427
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700428 if (hci_dev_test_flag(hdev, HCI_MGMT))
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800429 mgmt_auth_enable_complete(hdev, rp->status);
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530430
431 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200432}
433
434static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
435{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800436 struct hci_ev_status *rp;
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200437 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200438 void *sent;
439
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800440 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_ENCRYPT_MODE, sizeof(*rp));
441 if (!rp)
442 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200443
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800444 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
445
446 if (rp->status)
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200447 return;
448
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200449 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
450 if (!sent)
451 return;
452
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200453 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200454
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200455 if (param)
456 set_bit(HCI_ENCRYPT, &hdev->flags);
457 else
458 clear_bit(HCI_ENCRYPT, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200459}
460
461static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
462{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800463 struct hci_ev_status *rp;
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200464 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200465 void *sent;
466
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800467 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_SCAN_ENABLE, sizeof(*rp));
468 if (!rp)
469 return;
470
471 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200472
473 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
474 if (!sent)
475 return;
476
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200477 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200478
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200479 hci_dev_lock(hdev);
480
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800481 if (rp->status) {
Johan Hedberg2d7cee52011-11-07 22:16:03 +0200482 hdev->discov_timeout = 0;
483 goto done;
484 }
485
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300486 if (param & SCAN_INQUIRY)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200487 set_bit(HCI_ISCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300488 else
489 clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200490
Johan Hedberg031547d2014-07-10 12:09:06 +0300491 if (param & SCAN_PAGE)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200492 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300493 else
Johan Hedberg204e3992014-07-28 15:45:31 +0300494 clear_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200495
496done:
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200497 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200498}
499
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -0800500static void hci_cc_set_event_filter(struct hci_dev *hdev, struct sk_buff *skb)
501{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800502 struct hci_ev_status *rp;
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -0800503 struct hci_cp_set_event_filter *cp;
504 void *sent;
505
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800506 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_SCAN_ENABLE, sizeof(*rp));
507 if (!rp)
508 return;
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -0800509
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800510 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
511
512 if (rp->status)
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -0800513 return;
514
515 sent = hci_sent_cmd_data(hdev, HCI_OP_SET_EVENT_FLT);
516 if (!sent)
517 return;
518
519 cp = (struct hci_cp_set_event_filter *)sent;
520
521 if (cp->flt_type == HCI_FLT_CLEAR_ALL)
522 hci_dev_clear_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
523 else
524 hci_dev_set_flag(hdev, HCI_EVENT_FILTER_CONFIGURED);
525}
526
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200527static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
528{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800529 struct hci_rp_read_class_of_dev *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200530
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800531 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_CLASS_OF_DEV, sizeof(*rp));
532 if (!rp)
533 return;
534
535 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200536
537 if (rp->status)
538 return;
539
540 memcpy(hdev->dev_class, rp->dev_class, 3);
541
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800542 bt_dev_dbg(hdev, "class 0x%.2x%.2x%.2x", hdev->dev_class[2],
543 hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200544}
545
546static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
547{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800548 struct hci_ev_status *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200549 void *sent;
550
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800551 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_CLASS_OF_DEV, sizeof(*rp));
552 if (!rp)
553 return;
554
555 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200556
557 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
558 if (!sent)
559 return;
560
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100561 hci_dev_lock(hdev);
562
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800563 if (!rp->status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100564 memcpy(hdev->dev_class, sent, 3);
565
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700566 if (hci_dev_test_flag(hdev, HCI_MGMT))
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800567 mgmt_set_class_of_dev_complete(hdev, sent, rp->status);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100568
569 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200570}
571
572static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
573{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800574 struct hci_rp_read_voice_setting *rp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200576
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800577 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_VOICE_SETTING, sizeof(*rp));
578 if (!rp)
579 return;
580
581 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200582
583 if (rp->status)
584 return;
585
586 setting = __le16_to_cpu(rp->voice_setting);
587
Marcel Holtmannf383f272008-07-14 20:13:47 +0200588 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200589 return;
590
591 hdev->voice_setting = setting;
592
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800593 bt_dev_dbg(hdev, "voice setting 0x%4.4x", setting);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200594
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200595 if (hdev->notify)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200596 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200597}
598
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300599static void hci_cc_write_voice_setting(struct hci_dev *hdev,
600 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200601{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800602 struct hci_ev_status *rp;
Marcel Holtmannf383f272008-07-14 20:13:47 +0200603 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 void *sent;
605
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800606 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_VOICE_SETTING,
607 sizeof(*rp));
608 if (!rp)
609 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800611 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
612
613 if (rp->status)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200614 return;
615
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200616 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
617 if (!sent)
618 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Marcel Holtmannf383f272008-07-14 20:13:47 +0200620 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Marcel Holtmannf383f272008-07-14 20:13:47 +0200622 if (hdev->voice_setting == setting)
623 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
Marcel Holtmannf383f272008-07-14 20:13:47 +0200625 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800627 bt_dev_dbg(hdev, "voice setting 0x%4.4x", setting);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200628
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200629 if (hdev->notify)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200630 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700633static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
634 struct sk_buff *skb)
635{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800636 struct hci_rp_read_num_supported_iac *rp;
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700637
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800638 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_NUM_SUPPORTED_IAC,
639 sizeof(*rp));
640 if (!rp)
641 return;
642
643 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700644
645 if (rp->status)
646 return;
647
648 hdev->num_iac = rp->num_iac;
649
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800650 bt_dev_dbg(hdev, "num iac %d", hdev->num_iac);
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700651}
652
Marcel Holtmann333140b2008-07-14 20:13:48 +0200653static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
654{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800655 struct hci_ev_status *rp;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300656 struct hci_cp_write_ssp_mode *sent;
Marcel Holtmann333140b2008-07-14 20:13:48 +0200657
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800658 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_SSP_MODE, sizeof(*rp));
659 if (!rp)
660 return;
661
662 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200663
Marcel Holtmann333140b2008-07-14 20:13:48 +0200664 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
665 if (!sent)
666 return;
667
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530668 hci_dev_lock(hdev);
669
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800670 if (!rp->status) {
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300671 if (sent->mode)
Johan Hedbergcad718e2013-04-17 15:00:51 +0300672 hdev->features[1][0] |= LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300673 else
Johan Hedbergcad718e2013-04-17 15:00:51 +0300674 hdev->features[1][0] &= ~LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300675 }
676
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800677 if (!rp->status) {
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300678 if (sent->mode)
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700679 hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200680 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700681 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200682 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530683
684 hci_dev_unlock(hdev);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200685}
686
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800687static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
688{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800689 struct hci_ev_status *rp;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800690 struct hci_cp_write_sc_support *sent;
691
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800692 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_SC_SUPPORT, sizeof(*rp));
693 if (!rp)
694 return;
695
696 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800697
698 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
699 if (!sent)
700 return;
701
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530702 hci_dev_lock(hdev);
703
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800704 if (!rp->status) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800705 if (sent->support)
706 hdev->features[1][0] |= LMP_HOST_SC;
707 else
708 hdev->features[1][0] &= ~LMP_HOST_SC;
709 }
710
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800711 if (!hci_dev_test_flag(hdev, HCI_MGMT) && !rp->status) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800712 if (sent->support)
Marcel Holtmanna1536da2015-03-13 02:11:01 -0700713 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800714 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -0700715 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800716 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530717
718 hci_dev_unlock(hdev);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800719}
720
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200721static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
722{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800723 struct hci_rp_read_local_version *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200724
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800725 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_LOCAL_VERSION, sizeof(*rp));
726 if (!rp)
727 return;
728
729 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200730
731 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200732 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200733
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700734 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
735 hci_dev_test_flag(hdev, HCI_CONFIG)) {
Marcel Holtmann0d5551f2013-10-18 12:04:50 -0700736 hdev->hci_ver = rp->hci_ver;
737 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
738 hdev->lmp_ver = rp->lmp_ver;
739 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
740 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
741 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200742}
743
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300744static void hci_cc_read_local_commands(struct hci_dev *hdev,
745 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200746{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800747 struct hci_rp_read_local_commands *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200748
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800749 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_LOCAL_COMMANDS,
750 sizeof(*rp));
751 if (!rp)
752 return;
753
754 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200755
Marcel Holtmann6a070e62013-10-31 04:54:33 -0700756 if (rp->status)
757 return;
758
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700759 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
760 hci_dev_test_flag(hdev, HCI_CONFIG))
Johan Hedberg2177bab2013-03-05 20:37:43 +0200761 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200762}
763
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +0530764static void hci_cc_read_auth_payload_timeout(struct hci_dev *hdev,
765 struct sk_buff *skb)
766{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800767 struct hci_rp_read_auth_payload_to *rp;
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +0530768 struct hci_conn *conn;
769
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800770 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_AUTH_PAYLOAD_TO,
771 sizeof(*rp));
772 if (!rp)
773 return;
774
775 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +0530776
777 if (rp->status)
778 return;
779
780 hci_dev_lock(hdev);
781
782 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
783 if (conn)
784 conn->auth_payload_timeout = __le16_to_cpu(rp->timeout);
785
786 hci_dev_unlock(hdev);
787}
788
789static void hci_cc_write_auth_payload_timeout(struct hci_dev *hdev,
790 struct sk_buff *skb)
791{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800792 struct hci_rp_write_auth_payload_to *rp;
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +0530793 struct hci_conn *conn;
794 void *sent;
795
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800796 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_AUTH_PAYLOAD_TO, sizeof(*rp));
797 if (!rp)
798 return;
799
800 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +0530801
802 if (rp->status)
803 return;
804
805 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO);
806 if (!sent)
807 return;
808
809 hci_dev_lock(hdev);
810
811 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
812 if (conn)
813 conn->auth_payload_timeout = get_unaligned_le16(sent + 2);
814
815 hci_dev_unlock(hdev);
816}
817
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300818static void hci_cc_read_local_features(struct hci_dev *hdev,
819 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200820{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800821 struct hci_rp_read_local_features *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200822
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800823 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_LOCAL_FEATURES,
824 sizeof(*rp));
825 if (!rp)
826 return;
827
828 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200829
830 if (rp->status)
831 return;
832
833 memcpy(hdev->features, rp->features, 8);
834
835 /* Adjust default settings according to features
836 * supported by device. */
837
Johan Hedbergcad718e2013-04-17 15:00:51 +0300838 if (hdev->features[0][0] & LMP_3SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200839 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
840
Johan Hedbergcad718e2013-04-17 15:00:51 +0300841 if (hdev->features[0][0] & LMP_5SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200842 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
843
Johan Hedbergcad718e2013-04-17 15:00:51 +0300844 if (hdev->features[0][1] & LMP_HV2) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200845 hdev->pkt_type |= (HCI_HV2);
846 hdev->esco_type |= (ESCO_HV2);
847 }
848
Johan Hedbergcad718e2013-04-17 15:00:51 +0300849 if (hdev->features[0][1] & LMP_HV3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200850 hdev->pkt_type |= (HCI_HV3);
851 hdev->esco_type |= (ESCO_HV3);
852 }
853
Andre Guedes45db810f2012-07-24 15:03:49 -0300854 if (lmp_esco_capable(hdev))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200855 hdev->esco_type |= (ESCO_EV3);
856
Johan Hedbergcad718e2013-04-17 15:00:51 +0300857 if (hdev->features[0][4] & LMP_EV4)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200858 hdev->esco_type |= (ESCO_EV4);
859
Johan Hedbergcad718e2013-04-17 15:00:51 +0300860 if (hdev->features[0][4] & LMP_EV5)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200861 hdev->esco_type |= (ESCO_EV5);
862
Johan Hedbergcad718e2013-04-17 15:00:51 +0300863 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100864 hdev->esco_type |= (ESCO_2EV3);
865
Johan Hedbergcad718e2013-04-17 15:00:51 +0300866 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100867 hdev->esco_type |= (ESCO_3EV3);
868
Johan Hedbergcad718e2013-04-17 15:00:51 +0300869 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100870 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200871}
872
Andre Guedes971e3a42011-06-30 19:20:52 -0300873static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300874 struct sk_buff *skb)
Andre Guedes971e3a42011-06-30 19:20:52 -0300875{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800876 struct hci_rp_read_local_ext_features *rp;
Andre Guedes971e3a42011-06-30 19:20:52 -0300877
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800878 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_LOCAL_EXT_FEATURES,
879 sizeof(*rp));
880 if (!rp)
881 return;
882
883 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Andre Guedes971e3a42011-06-30 19:20:52 -0300884
885 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200886 return;
Andre Guedes971e3a42011-06-30 19:20:52 -0300887
Marcel Holtmann57af75a2013-10-18 12:04:47 -0700888 if (hdev->max_page < rp->max_page)
889 hdev->max_page = rp->max_page;
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300890
Johan Hedbergcad718e2013-04-17 15:00:51 +0300891 if (rp->page < HCI_MAX_PAGES)
892 memcpy(hdev->features[rp->page], rp->features, 8);
Andre Guedes971e3a42011-06-30 19:20:52 -0300893}
894
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200895static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300896 struct sk_buff *skb)
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200897{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800898 struct hci_rp_read_flow_control_mode *rp;
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200899
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800900 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_FLOW_CONTROL_MODE,
901 sizeof(*rp));
902 if (!rp)
903 return;
904
905 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200906
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200907 if (rp->status)
908 return;
909
910 hdev->flow_ctl_mode = rp->mode;
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200911}
912
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200913static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
914{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800915 struct hci_rp_read_buffer_size *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200916
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800917 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_BUFFER_SIZE, sizeof(*rp));
918 if (!rp)
919 return;
920
921 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200922
923 if (rp->status)
924 return;
925
926 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
927 hdev->sco_mtu = rp->sco_mtu;
928 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
929 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
930
931 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
932 hdev->sco_mtu = 64;
933 hdev->sco_pkts = 8;
934 }
935
936 hdev->acl_cnt = hdev->acl_pkts;
937 hdev->sco_cnt = hdev->sco_pkts;
938
Gustavo Padovan807deac2012-05-17 00:36:24 -0300939 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
940 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200941}
942
943static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
944{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800945 struct hci_rp_read_bd_addr *rp;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200946
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800947 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_BD_ADDR, sizeof(*rp));
948 if (!rp)
949 return;
950
951 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200952
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200953 if (rp->status)
954 return;
955
956 if (test_bit(HCI_INIT, &hdev->flags))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200957 bacpy(&hdev->bdaddr, &rp->bdaddr);
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200958
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700959 if (hci_dev_test_flag(hdev, HCI_SETUP))
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200960 bacpy(&hdev->setup_addr, &rp->bdaddr);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200961}
962
Marcel Holtmanna4790362020-04-03 21:44:04 +0200963static void hci_cc_read_local_pairing_opts(struct hci_dev *hdev,
964 struct sk_buff *skb)
965{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800966 struct hci_rp_read_local_pairing_opts *rp;
Marcel Holtmanna4790362020-04-03 21:44:04 +0200967
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800968 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_LOCAL_PAIRING_OPTS,
969 sizeof(*rp));
970 if (!rp)
971 return;
972
973 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna4790362020-04-03 21:44:04 +0200974
975 if (rp->status)
976 return;
977
978 if (hci_dev_test_flag(hdev, HCI_SETUP) ||
979 hci_dev_test_flag(hdev, HCI_CONFIG)) {
980 hdev->pairing_opts = rp->pairing_opts;
981 hdev->max_enc_key_size = rp->max_key_size;
982 }
983}
984
Johan Hedbergf332ec62013-03-15 17:07:11 -0500985static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
986 struct sk_buff *skb)
987{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800988 struct hci_rp_read_page_scan_activity *rp;
Johan Hedbergf332ec62013-03-15 17:07:11 -0500989
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -0800990 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
991 sizeof(*rp));
992 if (!rp)
993 return;
994
995 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Johan Hedbergf332ec62013-03-15 17:07:11 -0500996
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200997 if (rp->status)
998 return;
999
1000 if (test_bit(HCI_INIT, &hdev->flags)) {
Johan Hedbergf332ec62013-03-15 17:07:11 -05001001 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
1002 hdev->page_scan_window = __le16_to_cpu(rp->window);
1003 }
1004}
1005
Johan Hedberg4a3ee762013-03-15 17:07:12 -05001006static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
1007 struct sk_buff *skb)
1008{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001009 struct hci_ev_status *rp;
Johan Hedberg4a3ee762013-03-15 17:07:12 -05001010 struct hci_cp_write_page_scan_activity *sent;
1011
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001012 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_PAGE_SCAN_ACTIVITY,
1013 sizeof(*rp));
1014 if (!rp)
1015 return;
Johan Hedberg4a3ee762013-03-15 17:07:12 -05001016
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001017 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1018
1019 if (rp->status)
Johan Hedberg4a3ee762013-03-15 17:07:12 -05001020 return;
1021
1022 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
1023 if (!sent)
1024 return;
1025
1026 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
1027 hdev->page_scan_window = __le16_to_cpu(sent->window);
1028}
1029
Johan Hedbergf332ec62013-03-15 17:07:11 -05001030static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
1031 struct sk_buff *skb)
1032{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001033 struct hci_rp_read_page_scan_type *rp;
Johan Hedbergf332ec62013-03-15 17:07:11 -05001034
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001035 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_PAGE_SCAN_TYPE,
1036 sizeof(*rp));
1037 if (!rp)
1038 return;
1039
1040 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Johan Hedbergf332ec62013-03-15 17:07:11 -05001041
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001042 if (rp->status)
1043 return;
1044
1045 if (test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergf332ec62013-03-15 17:07:11 -05001046 hdev->page_scan_type = rp->type;
1047}
1048
Johan Hedberg4a3ee762013-03-15 17:07:12 -05001049static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
1050 struct sk_buff *skb)
1051{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001052 struct hci_ev_status *rp;
Johan Hedberg4a3ee762013-03-15 17:07:12 -05001053 u8 *type;
1054
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001055 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_PAGE_SCAN_TYPE,
1056 sizeof(*rp));
1057 if (!rp)
1058 return;
Johan Hedberg4a3ee762013-03-15 17:07:12 -05001059
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001060 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1061
1062 if (rp->status)
Johan Hedberg4a3ee762013-03-15 17:07:12 -05001063 return;
1064
1065 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
1066 if (type)
1067 hdev->page_scan_type = *type;
1068}
1069
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02001070static void hci_cc_read_data_block_size(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001071 struct sk_buff *skb)
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02001072{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001073 struct hci_rp_read_data_block_size *rp;
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02001074
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001075 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_DATA_BLOCK_SIZE,
1076 sizeof(*rp));
1077 if (!rp)
1078 return;
1079
1080 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02001081
1082 if (rp->status)
1083 return;
1084
1085 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
1086 hdev->block_len = __le16_to_cpu(rp->block_len);
1087 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
1088
1089 hdev->block_cnt = hdev->num_blocks;
1090
1091 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001092 hdev->block_cnt, hdev->block_len);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02001093}
1094
Johan Hedberg33f35722014-06-28 17:54:06 +03001095static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
1096{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001097 struct hci_rp_read_clock *rp;
Johan Hedberg33f35722014-06-28 17:54:06 +03001098 struct hci_cp_read_clock *cp;
1099 struct hci_conn *conn;
1100
1101 BT_DBG("%s", hdev->name);
1102
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001103 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_CLOCK, sizeof(*rp));
1104 if (!rp)
Johan Hedberg33f35722014-06-28 17:54:06 +03001105 return;
1106
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001107 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1108
Johan Hedberg33f35722014-06-28 17:54:06 +03001109 if (rp->status)
1110 return;
1111
1112 hci_dev_lock(hdev);
1113
1114 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
1115 if (!cp)
1116 goto unlock;
1117
1118 if (cp->which == 0x00) {
1119 hdev->clock = le32_to_cpu(rp->clock);
1120 goto unlock;
1121 }
1122
1123 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1124 if (conn) {
1125 conn->clock = le32_to_cpu(rp->clock);
1126 conn->clock_accuracy = le16_to_cpu(rp->accuracy);
1127 }
1128
1129unlock:
1130 hci_dev_unlock(hdev);
1131}
1132
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03001133static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001134 struct sk_buff *skb)
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03001135{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001136 struct hci_rp_read_local_amp_info *rp;
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03001137
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001138 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_LOCAL_AMP_INFO,
1139 sizeof(*rp));
1140 if (!rp)
1141 return;
1142
1143 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03001144
1145 if (rp->status)
Arron Wang83927882015-07-24 17:10:16 +08001146 return;
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03001147
1148 hdev->amp_status = rp->amp_status;
1149 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
1150 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
1151 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
1152 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
1153 hdev->amp_type = rp->amp_type;
1154 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
1155 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
1156 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
1157 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03001158}
1159
Johan Hedbergd5859e22011-01-25 01:19:58 +02001160static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001161 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +02001162{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001163 struct hci_rp_read_inq_rsp_tx_power *rp;
Johan Hedbergd5859e22011-01-25 01:19:58 +02001164
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001165 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_INQ_RSP_TX_POWER,
1166 sizeof(*rp));
1167 if (!rp)
1168 return;
1169
1170 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +02001171
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001172 if (rp->status)
1173 return;
1174
1175 hdev->inq_tx_power = rp->tx_power;
Johan Hedbergd5859e22011-01-25 01:19:58 +02001176}
1177
Alain Michaud00bce3f2020-03-05 16:14:59 +00001178static void hci_cc_read_def_err_data_reporting(struct hci_dev *hdev,
1179 struct sk_buff *skb)
1180{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001181 struct hci_rp_read_def_err_data_reporting *rp;
Alain Michaud00bce3f2020-03-05 16:14:59 +00001182
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001183 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_DEF_ERR_DATA_REPORTING,
1184 sizeof(*rp));
1185 if (!rp)
1186 return;
1187
1188 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Alain Michaud00bce3f2020-03-05 16:14:59 +00001189
1190 if (rp->status)
1191 return;
1192
1193 hdev->err_data_reporting = rp->err_data_reporting;
1194}
1195
1196static void hci_cc_write_def_err_data_reporting(struct hci_dev *hdev,
1197 struct sk_buff *skb)
1198{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001199 struct hci_ev_status *rp;
Alain Michaud00bce3f2020-03-05 16:14:59 +00001200 struct hci_cp_write_def_err_data_reporting *cp;
1201
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001202 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING,
1203 sizeof(*rp));
1204 if (!rp)
1205 return;
Alain Michaud00bce3f2020-03-05 16:14:59 +00001206
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001207 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1208
1209 if (rp->status)
Alain Michaud00bce3f2020-03-05 16:14:59 +00001210 return;
1211
1212 cp = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING);
1213 if (!cp)
1214 return;
1215
1216 hdev->err_data_reporting = cp->err_data_reporting;
1217}
1218
Johan Hedberg980e1a52011-01-22 06:10:07 +02001219static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
1220{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001221 struct hci_rp_pin_code_reply *rp;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001222 struct hci_cp_pin_code_reply *cp;
1223 struct hci_conn *conn;
1224
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001225 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_PIN_CODE_REPLY, sizeof(*rp));
1226 if (!rp)
1227 return;
1228
1229 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001230
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001231 hci_dev_lock(hdev);
1232
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001233 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001234 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001235
Mikel Astizfa1bd912012-08-09 09:52:29 +02001236 if (rp->status)
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001237 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001238
1239 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
1240 if (!cp)
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001241 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001242
1243 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1244 if (conn)
1245 conn->pin_length = cp->pin_len;
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001246
1247unlock:
1248 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001249}
1250
1251static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
1252{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001253 struct hci_rp_pin_code_neg_reply *rp;
Johan Hedberg980e1a52011-01-22 06:10:07 +02001254
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001255 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_PIN_CODE_NEG_REPLY, sizeof(*rp));
1256 if (!rp)
1257 return;
1258
1259 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001260
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001261 hci_dev_lock(hdev);
1262
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001263 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001264 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001265 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001266
1267 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02001268}
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001269
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001270static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
1271 struct sk_buff *skb)
1272{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001273 struct hci_rp_le_read_buffer_size *rp;
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001274
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001275 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_READ_BUFFER_SIZE,
1276 sizeof(*rp));
1277 if (!rp)
1278 return;
1279
1280 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001281
1282 if (rp->status)
1283 return;
1284
1285 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
1286 hdev->le_pkts = rp->le_max_pkt;
1287
1288 hdev->le_cnt = hdev->le_pkts;
1289
1290 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
Ville Tervo6ed58ec2011-02-10 22:38:48 -03001291}
Johan Hedberg980e1a52011-01-22 06:10:07 +02001292
Johan Hedberg60e77322013-01-22 14:01:59 +02001293static void hci_cc_le_read_local_features(struct hci_dev *hdev,
1294 struct sk_buff *skb)
1295{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001296 struct hci_rp_le_read_local_features *rp;
1297
1298 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_READ_LOCAL_FEATURES,
1299 sizeof(*rp));
1300 if (!rp)
1301 return;
Johan Hedberg60e77322013-01-22 14:01:59 +02001302
1303 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1304
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001305 if (rp->status)
1306 return;
1307
1308 memcpy(hdev->le_features, rp->features, 8);
Johan Hedberg60e77322013-01-22 14:01:59 +02001309}
1310
Johan Hedberg8fa19092012-10-19 20:57:49 +03001311static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
1312 struct sk_buff *skb)
1313{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001314 struct hci_rp_le_read_adv_tx_power *rp;
Johan Hedberg8fa19092012-10-19 20:57:49 +03001315
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001316 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_READ_ADV_TX_POWER,
1317 sizeof(*rp));
1318 if (!rp)
1319 return;
1320
1321 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Johan Hedberg8fa19092012-10-19 20:57:49 +03001322
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001323 if (rp->status)
1324 return;
1325
1326 hdev->adv_tx_power = rp->tx_power;
Johan Hedberg8fa19092012-10-19 20:57:49 +03001327}
1328
Johan Hedberga5c29682011-02-19 12:05:57 -03001329static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
1330{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001331 struct hci_rp_user_confirm_reply *rp;
Johan Hedberga5c29682011-02-19 12:05:57 -03001332
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001333 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_USER_CONFIRM_REPLY, sizeof(*rp));
1334 if (!rp)
1335 return;
1336
1337 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -03001338
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001339 hci_dev_lock(hdev);
1340
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001341 if (hci_dev_test_flag(hdev, HCI_MGMT))
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001342 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
1343 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001344
1345 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03001346}
1347
1348static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001349 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03001350{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001351 struct hci_rp_user_confirm_reply *rp;
Johan Hedberga5c29682011-02-19 12:05:57 -03001352
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001353 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_USER_CONFIRM_NEG_REPLY,
1354 sizeof(*rp));
1355 if (!rp)
1356 return;
1357
1358 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -03001359
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001360 hci_dev_lock(hdev);
1361
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001362 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg744cf192011-11-08 20:40:14 +02001363 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001364 ACL_LINK, 0, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001365
1366 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03001367}
1368
Brian Gix1143d452011-11-23 08:28:34 -08001369static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
1370{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001371 struct hci_rp_user_confirm_reply *rp;
Brian Gix1143d452011-11-23 08:28:34 -08001372
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001373 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_USER_PASSKEY_REPLY, sizeof(*rp));
1374 if (!rp)
1375 return;
1376
1377 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001378
1379 hci_dev_lock(hdev);
1380
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001381 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg272d90d2012-02-09 15:26:12 +02001382 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001383 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001384
1385 hci_dev_unlock(hdev);
1386}
1387
1388static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001389 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08001390{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001391 struct hci_rp_user_confirm_reply *rp;
Brian Gix1143d452011-11-23 08:28:34 -08001392
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001393 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_USER_PASSKEY_NEG_REPLY, sizeof(*rp));
1394 if (!rp)
1395 return;
1396
1397 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001398
1399 hci_dev_lock(hdev);
1400
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001401 if (hci_dev_test_flag(hdev, HCI_MGMT))
Brian Gix1143d452011-11-23 08:28:34 -08001402 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001403 ACL_LINK, 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -08001404
1405 hci_dev_unlock(hdev);
1406}
1407
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001408static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
1409 struct sk_buff *skb)
Szymon Jancc35938b2011-03-22 13:12:21 +01001410{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001411 struct hci_rp_read_local_oob_data *rp;
Szymon Jancc35938b2011-03-22 13:12:21 +01001412
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001413 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_LOCAL_OOB_DATA, sizeof(*rp));
1414 if (!rp)
1415 return;
1416
1417 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001418}
1419
1420static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
1421 struct sk_buff *skb)
1422{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001423 struct hci_rp_read_local_oob_ext_data *rp;
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001424
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001425 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_LOCAL_OOB_EXT_DATA, sizeof(*rp));
1426 if (!rp)
1427 return;
1428
1429 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Szymon Jancc35938b2011-03-22 13:12:21 +01001430}
1431
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001432static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
1433{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001434 struct hci_ev_status *rp;
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001435 bdaddr_t *sent;
1436
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001437 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_RANDOM_ADDR, sizeof(*rp));
1438 if (!rp)
1439 return;
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001440
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001441 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1442
1443 if (rp->status)
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001444 return;
1445
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001446 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1447 if (!sent)
1448 return;
1449
1450 hci_dev_lock(hdev);
1451
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001452 bacpy(&hdev->random_addr, sent);
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001453
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001454 if (!bacmp(&hdev->rpa, sent)) {
1455 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
1456 queue_delayed_work(hdev->workqueue, &hdev->rpa_expired,
1457 secs_to_jiffies(hdev->rpa_timeout));
1458 }
1459
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001460 hci_dev_unlock(hdev);
1461}
1462
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05301463static void hci_cc_le_set_default_phy(struct hci_dev *hdev, struct sk_buff *skb)
1464{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001465 struct hci_ev_status *rp;
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05301466 struct hci_cp_le_set_default_phy *cp;
1467
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001468 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_DEFAULT_PHY, sizeof(*rp));
1469 if (!rp)
1470 return;
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05301471
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001472 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1473
1474 if (rp->status)
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05301475 return;
1476
1477 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_DEFAULT_PHY);
1478 if (!cp)
1479 return;
1480
1481 hci_dev_lock(hdev);
1482
1483 hdev->le_tx_def_phys = cp->tx_phys;
1484 hdev->le_rx_def_phys = cp->rx_phys;
1485
1486 hci_dev_unlock(hdev);
1487}
1488
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301489static void hci_cc_le_set_adv_set_random_addr(struct hci_dev *hdev,
1490 struct sk_buff *skb)
1491{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001492 struct hci_ev_status *rp;
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301493 struct hci_cp_le_set_adv_set_rand_addr *cp;
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001494 struct adv_info *adv;
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301495
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001496 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_ADV_SET_RAND_ADDR,
1497 sizeof(*rp));
1498 if (!rp)
1499 return;
1500
1501 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1502
1503 if (rp->status)
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301504 return;
1505
1506 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR);
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001507 /* Update only in case the adv instance since handle 0x00 shall be using
1508 * HCI_OP_LE_SET_RANDOM_ADDR since that allows both extended and
1509 * non-extended adverting.
1510 */
1511 if (!cp || !cp->handle)
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301512 return;
1513
1514 hci_dev_lock(hdev);
1515
Luiz Augusto von Dentzc45074d2021-08-02 16:56:19 -07001516 adv = hci_find_adv_instance(hdev, cp->handle);
1517 if (adv) {
1518 bacpy(&adv->random_addr, &cp->bdaddr);
1519 if (!bacmp(&hdev->rpa, &cp->bdaddr)) {
1520 adv->rpa_expired = false;
1521 queue_delayed_work(hdev->workqueue,
1522 &adv->rpa_expired_cb,
1523 secs_to_jiffies(hdev->rpa_timeout));
1524 }
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05301525 }
1526
1527 hci_dev_unlock(hdev);
1528}
1529
Luiz Augusto von Dentzcba6b752021-10-27 16:58:40 -07001530static void hci_cc_le_remove_adv_set(struct hci_dev *hdev, struct sk_buff *skb)
1531{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001532 struct hci_ev_status *rp;
Luiz Augusto von Dentzcba6b752021-10-27 16:58:40 -07001533 u8 *instance;
1534 int err;
1535
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001536 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_REMOVE_ADV_SET, sizeof(*rp));
1537 if (!rp)
1538 return;
1539
1540 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1541
1542 if (rp->status)
Luiz Augusto von Dentzcba6b752021-10-27 16:58:40 -07001543 return;
1544
1545 instance = hci_sent_cmd_data(hdev, HCI_OP_LE_REMOVE_ADV_SET);
1546 if (!instance)
1547 return;
1548
1549 hci_dev_lock(hdev);
1550
1551 err = hci_remove_adv_instance(hdev, *instance);
1552 if (!err)
1553 mgmt_advertising_removed(hci_skb_sk(hdev->sent_cmd), hdev,
1554 *instance);
1555
1556 hci_dev_unlock(hdev);
1557}
1558
1559static void hci_cc_le_clear_adv_sets(struct hci_dev *hdev, struct sk_buff *skb)
1560{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001561 struct hci_ev_status *rp;
Luiz Augusto von Dentzcba6b752021-10-27 16:58:40 -07001562 struct adv_info *adv, *n;
1563 int err;
1564
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001565 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_CLEAR_ADV_SETS, sizeof(*rp));
1566 if (!rp)
1567 return;
1568
1569 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1570
1571 if (rp->status)
Luiz Augusto von Dentzcba6b752021-10-27 16:58:40 -07001572 return;
1573
1574 if (!hci_sent_cmd_data(hdev, HCI_OP_LE_CLEAR_ADV_SETS))
1575 return;
1576
1577 hci_dev_lock(hdev);
1578
1579 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
1580 u8 instance = adv->instance;
1581
1582 err = hci_remove_adv_instance(hdev, instance);
1583 if (!err)
1584 mgmt_advertising_removed(hci_skb_sk(hdev->sent_cmd),
1585 hdev, instance);
1586 }
1587
1588 hci_dev_unlock(hdev);
1589}
1590
Daniel Winkler7c395ea2020-12-03 12:12:51 -08001591static void hci_cc_le_read_transmit_power(struct hci_dev *hdev,
1592 struct sk_buff *skb)
1593{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001594 struct hci_rp_le_read_transmit_power *rp;
Daniel Winkler7c395ea2020-12-03 12:12:51 -08001595
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001596 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_READ_TRANSMIT_POWER,
1597 sizeof(*rp));
1598 if (!rp)
1599 return;
1600
1601 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Daniel Winkler7c395ea2020-12-03 12:12:51 -08001602
1603 if (rp->status)
1604 return;
1605
1606 hdev->min_le_tx_power = rp->min_le_tx_power;
1607 hdev->max_le_tx_power = rp->max_le_tx_power;
1608}
1609
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001610static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1611{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001612 struct hci_ev_status *rp;
1613 __u8 *sent;
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001614
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001615 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_ADV_ENABLE, sizeof(*rp));
1616 if (!rp)
1617 return;
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001618
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001619 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1620
1621 if (rp->status)
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001622 return;
1623
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001624 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1625 if (!sent)
Johan Hedberg3c857752014-03-25 10:30:49 +02001626 return;
1627
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001628 hci_dev_lock(hdev);
1629
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001630 /* If we're doing connection initiation as peripheral. Set a
Johan Hedberg3c857752014-03-25 10:30:49 +02001631 * timeout in case something goes wrong.
1632 */
1633 if (*sent) {
1634 struct hci_conn *conn;
1635
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001636 hci_dev_set_flag(hdev, HCI_LE_ADV);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001637
Jakub Pawlowskie7d9ab72015-08-07 20:22:52 +02001638 conn = hci_lookup_le_connect(hdev);
Johan Hedberg3c857752014-03-25 10:30:49 +02001639 if (conn)
1640 queue_delayed_work(hdev->workqueue,
1641 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001642 conn->conn_timeout);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001643 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001644 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedberg3c857752014-03-25 10:30:49 +02001645 }
1646
Johan Hedberg04b4edc2013-03-15 17:07:01 -05001647 hci_dev_unlock(hdev);
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001648}
1649
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301650static void hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev,
1651 struct sk_buff *skb)
1652{
1653 struct hci_cp_le_set_ext_adv_enable *cp;
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001654 struct hci_cp_ext_adv_set *set;
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001655 struct adv_info *adv = NULL, *n;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001656 struct hci_ev_status *rp;
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301657
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001658 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_EXT_ADV_ENABLE,
1659 sizeof(*rp));
1660 if (!rp)
1661 return;
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301662
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001663 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1664
1665 if (rp->status)
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301666 return;
1667
1668 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE);
1669 if (!cp)
1670 return;
1671
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001672 set = (void *)cp->data;
1673
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301674 hci_dev_lock(hdev);
1675
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001676 if (cp->num_of_sets)
1677 adv = hci_find_adv_instance(hdev, set->handle);
1678
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301679 if (cp->enable) {
1680 struct hci_conn *conn;
1681
1682 hci_dev_set_flag(hdev, HCI_LE_ADV);
1683
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001684 if (adv)
1685 adv->enabled = true;
1686
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301687 conn = hci_lookup_le_connect(hdev);
1688 if (conn)
1689 queue_delayed_work(hdev->workqueue,
1690 &conn->le_conn_timeout,
1691 conn->conn_timeout);
Jaganath Kanakkassery45b77492018-07-19 17:09:43 +05301692 } else {
Archie Pusaka21289392021-10-28 19:17:25 +08001693 if (cp->num_of_sets) {
1694 if (adv)
1695 adv->enabled = false;
1696
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001697 /* If just one instance was disabled check if there are
1698 * any other instance enabled before clearing HCI_LE_ADV
1699 */
1700 list_for_each_entry_safe(adv, n, &hdev->adv_instances,
1701 list) {
1702 if (adv->enabled)
1703 goto unlock;
1704 }
1705 } else {
1706 /* All instances shall be considered disabled */
1707 list_for_each_entry_safe(adv, n, &hdev->adv_instances,
1708 list)
1709 adv->enabled = false;
1710 }
1711
Jaganath Kanakkassery45b77492018-07-19 17:09:43 +05301712 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301713 }
1714
Luiz Augusto von Dentz10279312021-08-02 16:56:18 -07001715unlock:
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05301716 hci_dev_unlock(hdev);
1717}
1718
Marcel Holtmann533553f2014-03-21 12:18:10 -07001719static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1720{
1721 struct hci_cp_le_set_scan_param *cp;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001722 struct hci_ev_status *rp;
Marcel Holtmann533553f2014-03-21 12:18:10 -07001723
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001724 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_SCAN_PARAM, sizeof(*rp));
1725 if (!rp)
1726 return;
Marcel Holtmann533553f2014-03-21 12:18:10 -07001727
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001728 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1729
1730 if (rp->status)
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001731 return;
1732
Marcel Holtmann533553f2014-03-21 12:18:10 -07001733 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1734 if (!cp)
1735 return;
1736
1737 hci_dev_lock(hdev);
1738
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001739 hdev->le_scan_type = cp->type;
Marcel Holtmann533553f2014-03-21 12:18:10 -07001740
1741 hci_dev_unlock(hdev);
1742}
1743
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301744static void hci_cc_le_set_ext_scan_param(struct hci_dev *hdev,
1745 struct sk_buff *skb)
1746{
1747 struct hci_cp_le_set_ext_scan_params *cp;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001748 struct hci_ev_status *rp;
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301749 struct hci_cp_le_scan_phy_params *phy_param;
1750
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001751 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
1752 sizeof(*rp));
1753 if (!rp)
1754 return;
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301755
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001756 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1757
1758 if (rp->status)
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301759 return;
1760
1761 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS);
1762 if (!cp)
1763 return;
1764
1765 phy_param = (void *)cp->data;
1766
1767 hci_dev_lock(hdev);
1768
1769 hdev->le_scan_type = phy_param->type;
1770
1771 hci_dev_unlock(hdev);
1772}
1773
Johan Hedbergb9a63282014-03-25 10:51:52 +02001774static bool has_pending_adv_report(struct hci_dev *hdev)
1775{
1776 struct discovery_state *d = &hdev->discovery;
1777
1778 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1779}
1780
1781static void clear_pending_adv_report(struct hci_dev *hdev)
1782{
1783 struct discovery_state *d = &hdev->discovery;
1784
1785 bacpy(&d->last_adv_addr, BDADDR_ANY);
1786 d->last_adv_data_len = 0;
1787}
1788
1789static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001790 u8 bdaddr_type, s8 rssi, u32 flags,
1791 u8 *data, u8 len)
Johan Hedbergb9a63282014-03-25 10:51:52 +02001792{
1793 struct discovery_state *d = &hdev->discovery;
1794
Alain Michauda2ec9052020-07-27 20:48:55 +00001795 if (len > HCI_MAX_AD_LENGTH)
1796 return;
1797
Johan Hedbergb9a63282014-03-25 10:51:52 +02001798 bacpy(&d->last_adv_addr, bdaddr);
1799 d->last_adv_addr_type = bdaddr_type;
Johan Hedbergff5cd292014-03-25 14:40:52 +02001800 d->last_adv_rssi = rssi;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001801 d->last_adv_flags = flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02001802 memcpy(d->last_adv_data, data, len);
1803 d->last_adv_data_len = len;
1804}
1805
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301806static void le_set_scan_enable_complete(struct hci_dev *hdev, u8 enable)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001807{
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301808 hci_dev_lock(hdev);
1809
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301810 switch (enable) {
Andre Guedes76a388be2013-04-04 20:21:02 -03001811 case LE_SCAN_ENABLE:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001812 hci_dev_set_flag(hdev, HCI_LE_SCAN);
Johan Hedbergb9a63282014-03-25 10:51:52 +02001813 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1814 clear_pending_adv_report(hdev);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001815 break;
1816
Andre Guedes76a388be2013-04-04 20:21:02 -03001817 case LE_SCAN_DISABLE:
Johan Hedbergb9a63282014-03-25 10:51:52 +02001818 /* We do this here instead of when setting DISCOVERY_STOPPED
1819 * since the latter would potentially require waiting for
1820 * inquiry to stop too.
1821 */
1822 if (has_pending_adv_report(hdev)) {
1823 struct discovery_state *d = &hdev->discovery;
1824
1825 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001826 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001827 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001828 d->last_adv_data,
Johan Hedbergb9a63282014-03-25 10:51:52 +02001829 d->last_adv_data_len, NULL, 0);
1830 }
1831
Johan Hedberg317ac8c2014-02-28 20:26:12 +02001832 /* Cancel this timer so that we don't try to disable scanning
1833 * when it's already disabled.
1834 */
1835 cancel_delayed_work(&hdev->le_scan_disable);
1836
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001837 hci_dev_clear_flag(hdev, HCI_LE_SCAN);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001838
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001839 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1840 * interrupted scanning due to a connect request. Mark
Luiz Augusto von Dentzabfeea42021-10-27 16:58:45 -07001841 * therefore discovery as stopped.
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001842 */
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001843 if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001844 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001845
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001846 break;
1847
1848 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001849 bt_dev_err(hdev, "use of reserved LE_Scan_Enable param %d",
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301850 enable);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001851 break;
Andre Guedes35815082011-05-26 16:23:53 -03001852 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301853
1854 hci_dev_unlock(hdev);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001855}
1856
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301857static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
1858 struct sk_buff *skb)
1859{
1860 struct hci_cp_le_set_scan_enable *cp;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001861 struct hci_ev_status *rp;
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301862
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001863 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_SCAN_ENABLE,
1864 sizeof(*rp));
1865 if (!rp)
1866 return;
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301867
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001868 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1869
1870 if (rp->status)
Jaganath Kanakkassery3baef812018-07-06 17:05:27 +05301871 return;
1872
1873 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1874 if (!cp)
1875 return;
1876
1877 le_set_scan_enable_complete(hdev, cp->enable);
1878}
1879
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301880static void hci_cc_le_set_ext_scan_enable(struct hci_dev *hdev,
1881 struct sk_buff *skb)
1882{
1883 struct hci_cp_le_set_ext_scan_enable *cp;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001884 struct hci_ev_status *rp;
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301885
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001886 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
1887 sizeof(*rp));
1888 if (!rp)
1889 return;
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301890
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001891 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1892
1893 if (rp->status)
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05301894 return;
1895
1896 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE);
1897 if (!cp)
1898 return;
1899
1900 le_set_scan_enable_complete(hdev, cp->enable);
1901}
1902
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05301903static void hci_cc_le_read_num_adv_sets(struct hci_dev *hdev,
1904 struct sk_buff *skb)
1905{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001906 struct hci_rp_le_read_num_supported_adv_sets *rp;
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05301907
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001908 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS,
1909 sizeof(*rp));
1910 if (!rp)
1911 return;
1912
1913 bt_dev_dbg(hdev, "status 0x%2.2x No of Adv sets %u", rp->status,
1914 rp->num_of_sets);
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05301915
1916 if (rp->status)
1917 return;
1918
1919 hdev->le_num_of_adv_sets = rp->num_of_sets;
1920}
1921
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001922static void hci_cc_le_read_accept_list_size(struct hci_dev *hdev,
1923 struct sk_buff *skb)
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001924{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001925 struct hci_rp_le_read_accept_list_size *rp;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001926
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001927 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_READ_ACCEPT_LIST_SIZE,
1928 sizeof(*rp));
1929 if (!rp)
1930 return;
1931
1932 bt_dev_dbg(hdev, "status 0x%2.2x size %u", rp->status, rp->size);
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001933
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001934 if (rp->status)
1935 return;
1936
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001937 hdev->le_accept_list_size = rp->size;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001938}
1939
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001940static void hci_cc_le_clear_accept_list(struct hci_dev *hdev,
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001941 struct sk_buff *skb)
1942{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001943 struct hci_ev_status *rp;
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001944
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001945 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_CLEAR_ACCEPT_LIST,
1946 sizeof(*rp));
1947 if (!rp)
1948 return;
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001949
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001950 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1951
1952 if (rp->status)
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001953 return;
1954
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001955 hci_bdaddr_list_clear(&hdev->le_accept_list);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001956}
1957
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001958static void hci_cc_le_add_to_accept_list(struct hci_dev *hdev,
1959 struct sk_buff *skb)
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001960{
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001961 struct hci_cp_le_add_to_accept_list *sent;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001962 struct hci_ev_status *rp;
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001963
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001964 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_ADD_TO_ACCEPT_LIST,
1965 sizeof(*rp));
1966 if (!rp)
1967 return;
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001968
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001969 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1970
1971 if (rp->status)
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001972 return;
1973
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001974 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_ACCEPT_LIST);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001975 if (!sent)
1976 return;
1977
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001978 hci_bdaddr_list_add(&hdev->le_accept_list, &sent->bdaddr,
1979 sent->bdaddr_type);
1980}
1981
1982static void hci_cc_le_del_from_accept_list(struct hci_dev *hdev,
1983 struct sk_buff *skb)
1984{
1985 struct hci_cp_le_del_from_accept_list *sent;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001986 struct hci_ev_status *rp;
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001987
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001988 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_DEL_FROM_ACCEPT_LIST,
1989 sizeof(*rp));
1990 if (!rp)
1991 return;
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001992
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08001993 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
1994
1995 if (rp->status)
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08001996 return;
1997
1998 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_ACCEPT_LIST);
1999 if (!sent)
2000 return;
2001
2002 hci_bdaddr_list_del(&hdev->le_accept_list, &sent->bdaddr,
Johan Hedbergdcc36c12014-07-09 12:59:13 +03002003 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08002004}
2005
Johan Hedberg9b008c02013-01-22 14:02:01 +02002006static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
2007 struct sk_buff *skb)
2008{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002009 struct hci_rp_le_read_supported_states *rp;
Johan Hedberg9b008c02013-01-22 14:02:01 +02002010
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002011 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_READ_SUPPORTED_STATES,
2012 sizeof(*rp));
2013 if (!rp)
2014 return;
2015
2016 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Johan Hedberg9b008c02013-01-22 14:02:01 +02002017
Marcel Holtmann45296ac2014-07-05 10:48:01 +02002018 if (rp->status)
2019 return;
2020
2021 memcpy(hdev->le_states, rp->le_states, 8);
Johan Hedberg9b008c02013-01-22 14:02:01 +02002022}
2023
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01002024static void hci_cc_le_read_def_data_len(struct hci_dev *hdev,
2025 struct sk_buff *skb)
2026{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002027 struct hci_rp_le_read_def_data_len *rp;
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01002028
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002029 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_READ_DEF_DATA_LEN,
2030 sizeof(*rp));
2031 if (!rp)
2032 return;
2033
2034 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01002035
2036 if (rp->status)
2037 return;
2038
2039 hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
2040 hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
2041}
2042
2043static void hci_cc_le_write_def_data_len(struct hci_dev *hdev,
2044 struct sk_buff *skb)
2045{
2046 struct hci_cp_le_write_def_data_len *sent;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002047 struct hci_ev_status *rp;
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01002048
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002049 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_WRITE_DEF_DATA_LEN,
2050 sizeof(*rp));
2051 if (!rp)
2052 return;
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01002053
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002054 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2055
2056 if (rp->status)
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01002057 return;
2058
2059 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
2060 if (!sent)
2061 return;
2062
2063 hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
2064 hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
2065}
2066
Ankit Navikb950aa82018-08-17 07:29:19 +05302067static void hci_cc_le_add_to_resolv_list(struct hci_dev *hdev,
2068 struct sk_buff *skb)
2069{
2070 struct hci_cp_le_add_to_resolv_list *sent;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002071 struct hci_ev_status *rp;
Ankit Navikb950aa82018-08-17 07:29:19 +05302072
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002073 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_ADD_TO_RESOLV_LIST,
2074 sizeof(*rp));
2075 if (!rp)
2076 return;
Ankit Navikb950aa82018-08-17 07:29:19 +05302077
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002078 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2079
2080 if (rp->status)
Ankit Navikb950aa82018-08-17 07:29:19 +05302081 return;
2082
2083 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST);
2084 if (!sent)
2085 return;
2086
2087 hci_bdaddr_list_add_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
2088 sent->bdaddr_type, sent->peer_irk,
2089 sent->local_irk);
2090}
2091
2092static void hci_cc_le_del_from_resolv_list(struct hci_dev *hdev,
2093 struct sk_buff *skb)
2094{
2095 struct hci_cp_le_del_from_resolv_list *sent;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002096 struct hci_ev_status *rp;
Ankit Navikb950aa82018-08-17 07:29:19 +05302097
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002098 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
2099 sizeof(*rp));
2100 if (!rp)
2101 return;
Ankit Navikb950aa82018-08-17 07:29:19 +05302102
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002103 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2104
2105 if (rp->status)
Ankit Navikb950aa82018-08-17 07:29:19 +05302106 return;
2107
2108 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST);
2109 if (!sent)
2110 return;
2111
2112 hci_bdaddr_list_del_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
2113 sent->bdaddr_type);
2114}
2115
Ankit Navik545f2592018-06-29 12:13:20 +05302116static void hci_cc_le_clear_resolv_list(struct hci_dev *hdev,
2117 struct sk_buff *skb)
2118{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002119 struct hci_ev_status *rp;
Ankit Navik545f2592018-06-29 12:13:20 +05302120
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002121 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_CLEAR_RESOLV_LIST,
2122 sizeof(*rp));
2123 if (!rp)
2124 return;
Ankit Navik545f2592018-06-29 12:13:20 +05302125
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002126 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2127
2128 if (rp->status)
Ankit Navik545f2592018-06-29 12:13:20 +05302129 return;
2130
2131 hci_bdaddr_list_clear(&hdev->le_resolv_list);
2132}
2133
Ankit Navikcfdb0c22018-06-29 12:12:50 +05302134static void hci_cc_le_read_resolv_list_size(struct hci_dev *hdev,
2135 struct sk_buff *skb)
2136{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002137 struct hci_rp_le_read_resolv_list_size *rp;
Ankit Navikcfdb0c22018-06-29 12:12:50 +05302138
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002139 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_READ_RESOLV_LIST_SIZE,
2140 sizeof(*rp));
2141 if (!rp)
2142 return;
2143
2144 bt_dev_dbg(hdev, "status 0x%2.2x size %u", rp->status, rp->size);
Ankit Navikcfdb0c22018-06-29 12:12:50 +05302145
2146 if (rp->status)
2147 return;
2148
2149 hdev->le_resolv_list_size = rp->size;
2150}
2151
Ankit Navikaa12af72018-08-07 13:16:35 +05302152static void hci_cc_le_set_addr_resolution_enable(struct hci_dev *hdev,
2153 struct sk_buff *skb)
2154{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002155 struct hci_ev_status *rp;
2156 __u8 *sent;
Ankit Navikaa12af72018-08-07 13:16:35 +05302157
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002158 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE,
2159 sizeof(*rp));
2160 if (!rp)
2161 return;
Ankit Navikaa12af72018-08-07 13:16:35 +05302162
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002163 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2164
2165 if (rp->status)
Ankit Navikaa12af72018-08-07 13:16:35 +05302166 return;
2167
2168 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE);
2169 if (!sent)
2170 return;
2171
2172 hci_dev_lock(hdev);
2173
2174 if (*sent)
2175 hci_dev_set_flag(hdev, HCI_LL_RPA_RESOLUTION);
2176 else
2177 hci_dev_clear_flag(hdev, HCI_LL_RPA_RESOLUTION);
2178
2179 hci_dev_unlock(hdev);
2180}
2181
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01002182static void hci_cc_le_read_max_data_len(struct hci_dev *hdev,
2183 struct sk_buff *skb)
2184{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002185 struct hci_rp_le_read_max_data_len *rp;
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01002186
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002187 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_READ_MAX_DATA_LEN,
2188 sizeof(*rp));
2189 if (!rp)
2190 return;
2191
2192 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01002193
2194 if (rp->status)
2195 return;
2196
2197 hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
2198 hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
2199 hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
2200 hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
2201}
2202
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002203static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
2204 struct sk_buff *skb)
Andre Guedesf9b49302011-06-30 19:20:53 -03002205{
Johan Hedberg06199cf2012-02-22 16:37:11 +02002206 struct hci_cp_write_le_host_supported *sent;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002207 struct hci_ev_status *rp;
Andre Guedesf9b49302011-06-30 19:20:53 -03002208
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002209 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_LE_HOST_SUPPORTED,
2210 sizeof(*rp));
2211 if (!rp)
2212 return;
Andre Guedesf9b49302011-06-30 19:20:53 -03002213
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002214 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2215
2216 if (rp->status)
Marcel Holtmann45296ac2014-07-05 10:48:01 +02002217 return;
2218
Johan Hedberg06199cf2012-02-22 16:37:11 +02002219 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
Johan Hedberg8f984df2012-02-28 01:07:22 +02002220 if (!sent)
Andre Guedesf9b49302011-06-30 19:20:53 -03002221 return;
2222
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05302223 hci_dev_lock(hdev);
2224
Marcel Holtmann45296ac2014-07-05 10:48:01 +02002225 if (sent->le) {
2226 hdev->features[1][0] |= LMP_HOST_LE;
Marcel Holtmanna1536da2015-03-13 02:11:01 -07002227 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
Marcel Holtmann45296ac2014-07-05 10:48:01 +02002228 } else {
2229 hdev->features[1][0] &= ~LMP_HOST_LE;
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002230 hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
2231 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedberg8f984df2012-02-28 01:07:22 +02002232 }
Marcel Holtmann45296ac2014-07-05 10:48:01 +02002233
2234 if (sent->simul)
2235 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
2236 else
2237 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05302238
2239 hci_dev_unlock(hdev);
Andre Guedesf9b49302011-06-30 19:20:53 -03002240}
2241
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02002242static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
2243{
2244 struct hci_cp_le_set_adv_param *cp;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002245 struct hci_ev_status *rp;
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02002246
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002247 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_ADV_PARAM, sizeof(*rp));
2248 if (!rp)
2249 return;
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02002250
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002251 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2252
2253 if (rp->status)
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02002254 return;
2255
2256 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
2257 if (!cp)
2258 return;
2259
2260 hci_dev_lock(hdev);
2261 hdev->adv_addr_type = cp->own_address_type;
2262 hci_dev_unlock(hdev);
2263}
2264
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05302265static void hci_cc_set_ext_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
2266{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002267 struct hci_rp_le_set_ext_adv_params *rp;
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05302268 struct hci_cp_le_set_ext_adv_params *cp;
2269 struct adv_info *adv_instance;
2270
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002271 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_LE_SET_EXT_ADV_PARAMS,
2272 sizeof(*rp));
2273 if (!rp)
2274 return;
2275
2276 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05302277
2278 if (rp->status)
2279 return;
2280
2281 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS);
2282 if (!cp)
2283 return;
2284
2285 hci_dev_lock(hdev);
2286 hdev->adv_addr_type = cp->own_addr_type;
Daniel Winkler25e70882021-04-05 16:33:04 -07002287 if (!cp->handle) {
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05302288 /* Store in hdev for instance 0 */
2289 hdev->adv_tx_power = rp->tx_power;
2290 } else {
Daniel Winkler25e70882021-04-05 16:33:04 -07002291 adv_instance = hci_find_adv_instance(hdev, cp->handle);
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05302292 if (adv_instance)
2293 adv_instance->tx_power = rp->tx_power;
2294 }
Jaganath Kanakkasserya0fb3722018-07-19 17:09:42 +05302295 /* Update adv data as tx power is known now */
Daniel Winkler25e70882021-04-05 16:33:04 -07002296 hci_req_update_adv_data(hdev, cp->handle);
Daniel Winkler12410572020-12-03 12:12:49 -08002297
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05302298 hci_dev_unlock(hdev);
2299}
2300
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02002301static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
2302{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002303 struct hci_rp_read_rssi *rp;
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02002304 struct hci_conn *conn;
2305
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002306 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_RSSI, sizeof(*rp));
2307 if (!rp)
2308 return;
2309
2310 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02002311
2312 if (rp->status)
2313 return;
2314
2315 hci_dev_lock(hdev);
2316
2317 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
2318 if (conn)
2319 conn->rssi = rp->rssi;
2320
2321 hci_dev_unlock(hdev);
2322}
2323
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02002324static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
2325{
2326 struct hci_cp_read_tx_power *sent;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002327 struct hci_rp_read_tx_power *rp;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02002328 struct hci_conn *conn;
2329
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002330 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_READ_TX_POWER, sizeof(*rp));
2331 if (!rp)
2332 return;
2333
2334 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02002335
2336 if (rp->status)
2337 return;
2338
2339 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
2340 if (!sent)
2341 return;
2342
2343 hci_dev_lock(hdev);
2344
2345 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02002346 if (!conn)
2347 goto unlock;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02002348
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02002349 switch (sent->type) {
2350 case 0x00:
2351 conn->tx_power = rp->tx_power;
2352 break;
2353 case 0x01:
2354 conn->max_tx_power = rp->tx_power;
2355 break;
2356 }
2357
2358unlock:
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02002359 hci_dev_unlock(hdev);
2360}
2361
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08002362static void hci_cc_write_ssp_debug_mode(struct hci_dev *hdev, struct sk_buff *skb)
2363{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002364 struct hci_ev_status *rp;
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08002365 u8 *mode;
2366
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002367 rp = hci_cc_skb_pull(hdev, skb, HCI_OP_WRITE_SSP_DEBUG_MODE,
2368 sizeof(*rp));
2369 if (!rp)
2370 return;
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08002371
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08002372 bt_dev_dbg(hdev, "status 0x%2.2x", rp->status);
2373
2374 if (rp->status)
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08002375 return;
2376
2377 mode = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE);
2378 if (mode)
2379 hdev->ssp_debug_mode = *mode;
2380}
2381
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002382static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002383{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002384 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002385
2386 if (status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002387 hci_conn_check_pending(hdev);
Johan Hedberg314b2382011-04-27 10:29:57 -04002388 return;
2389 }
2390
Andre Guedes89352e72011-11-04 14:16:53 -03002391 set_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002392}
2393
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002394static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002396 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002399 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002400
2401 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (!cp)
2403 return;
2404
2405 hci_dev_lock(hdev);
2406
2407 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
2408
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002409 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 if (status) {
2412 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02002413 if (status != 0x0c || conn->attempt > 2) {
2414 conn->state = BT_CLOSED;
Johan Hedberg539c4962015-02-18 14:53:57 +02002415 hci_connect_cfm(conn, status);
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02002416 hci_conn_del(conn);
2417 } else
2418 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 }
2420 } else {
2421 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03002422 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr,
2423 HCI_ROLE_MASTER);
2424 if (!conn)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002425 bt_dev_err(hdev, "no memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 }
2427 }
2428
2429 hci_dev_unlock(hdev);
2430}
2431
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002432static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002434 struct hci_cp_add_sco *cp;
2435 struct hci_conn *acl, *sco;
2436 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002438 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002439
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002440 if (!status)
2441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002443 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
2444 if (!cp)
2445 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002447 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002449 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01002450
2451 hci_dev_lock(hdev);
2452
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002453 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002454 if (acl) {
2455 sco = acl->link;
2456 if (sco) {
2457 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002458
Johan Hedberg539c4962015-02-18 14:53:57 +02002459 hci_connect_cfm(sco, status);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002460 hci_conn_del(sco);
2461 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002462 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01002463
2464 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465}
2466
Marcel Holtmannf8558552008-07-14 20:13:49 +02002467static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
2468{
2469 struct hci_cp_auth_requested *cp;
2470 struct hci_conn *conn;
2471
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002472 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02002473
2474 if (!status)
2475 return;
2476
2477 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
2478 if (!cp)
2479 return;
2480
2481 hci_dev_lock(hdev);
2482
2483 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2484 if (conn) {
2485 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002486 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002487 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02002488 }
2489 }
2490
2491 hci_dev_unlock(hdev);
2492}
2493
2494static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
2495{
2496 struct hci_cp_set_conn_encrypt *cp;
2497 struct hci_conn *conn;
2498
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002499 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02002500
2501 if (!status)
2502 return;
2503
2504 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
2505 if (!cp)
2506 return;
2507
2508 hci_dev_lock(hdev);
2509
2510 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2511 if (conn) {
2512 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002513 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002514 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02002515 }
2516 }
2517
2518 hci_dev_unlock(hdev);
2519}
2520
Johan Hedberg127178d2010-11-18 22:22:29 +02002521static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002522 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02002523{
Johan Hedberg392599b2010-11-18 22:22:28 +02002524 if (conn->state != BT_CONFIG || !conn->out)
2525 return 0;
2526
Johan Hedberg765c2a92011-01-19 12:06:52 +05302527 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02002528 return 0;
2529
2530 /* Only request authentication for SSP connections or non-SSP
Johan Hedberg264b8b42014-01-08 16:40:39 +02002531 * devices with sec_level MEDIUM or HIGH or if MITM protection
2532 * is requested.
2533 */
Gustavo Padovan807deac2012-05-17 00:36:24 -03002534 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
Johan Hedberg7e3691e2014-05-30 14:45:19 +03002535 conn->pending_sec_level != BT_SECURITY_FIPS &&
Johan Hedberg264b8b42014-01-08 16:40:39 +02002536 conn->pending_sec_level != BT_SECURITY_HIGH &&
2537 conn->pending_sec_level != BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02002538 return 0;
2539
Johan Hedberg392599b2010-11-18 22:22:28 +02002540 return 1;
2541}
2542
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002543static int hci_resolve_name(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002544 struct inquiry_entry *e)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002545{
2546 struct hci_cp_remote_name_req cp;
2547
2548 memset(&cp, 0, sizeof(cp));
2549
2550 bacpy(&cp.bdaddr, &e->data.bdaddr);
2551 cp.pscan_rep_mode = e->data.pscan_rep_mode;
2552 cp.pscan_mode = e->data.pscan_mode;
2553 cp.clock_offset = e->data.clock_offset;
2554
2555 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
2556}
2557
Johan Hedbergb644ba32012-01-17 21:48:47 +02002558static bool hci_resolve_next_name(struct hci_dev *hdev)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002559{
2560 struct discovery_state *discov = &hdev->discovery;
2561 struct inquiry_entry *e;
2562
Johan Hedbergb644ba32012-01-17 21:48:47 +02002563 if (list_empty(&discov->resolve))
2564 return false;
2565
Archie Pusakadbf68112021-11-25 15:04:37 +08002566 /* We should stop if we already spent too much time resolving names. */
2567 if (time_after(jiffies, discov->name_resolve_timeout)) {
2568 bt_dev_warn_ratelimited(hdev, "Name resolve takes too long.");
2569 return false;
2570 }
2571
Johan Hedbergb644ba32012-01-17 21:48:47 +02002572 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
Ram Malovanyc8100892012-07-19 10:26:09 +03002573 if (!e)
2574 return false;
2575
Johan Hedbergb644ba32012-01-17 21:48:47 +02002576 if (hci_resolve_name(hdev, e) == 0) {
2577 e->name_state = NAME_PENDING;
2578 return true;
2579 }
2580
2581 return false;
2582}
2583
2584static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002585 bdaddr_t *bdaddr, u8 *name, u8 name_len)
Johan Hedbergb644ba32012-01-17 21:48:47 +02002586{
2587 struct discovery_state *discov = &hdev->discovery;
2588 struct inquiry_entry *e;
2589
Johan Hedberg60cb49d2014-11-11 11:33:24 +02002590 /* Update the mgmt connected state if necessary. Be careful with
2591 * conn objects that exist but are not (yet) connected however.
2592 * Only those in BT_CONFIG or BT_CONNECTED states can be
2593 * considered connected.
2594 */
2595 if (conn &&
2596 (conn->state == BT_CONFIG || conn->state == BT_CONNECTED) &&
Jaganath Kanakkasserycb77c3e2014-11-07 16:39:09 +05302597 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07002598 mgmt_device_connected(hdev, conn, name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002599
2600 if (discov->state == DISCOVERY_STOPPED)
2601 return;
2602
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002603 if (discov->state == DISCOVERY_STOPPING)
2604 goto discov_complete;
2605
2606 if (discov->state != DISCOVERY_RESOLVING)
2607 return;
2608
2609 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
Ram Malovany7cc83802012-07-19 10:26:10 +03002610 /* If the device was not found in a list of found devices names of which
2611 * are pending. there is no need to continue resolving a next name as it
2612 * will be done upon receiving another Remote Name Request Complete
2613 * Event */
2614 if (!e)
2615 return;
2616
2617 list_del(&e->list);
Archie Pusakaea13aed2021-11-25 15:04:36 +08002618
2619 e->name_state = name ? NAME_KNOWN : NAME_NOT_KNOWN;
2620 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00, e->data.rssi,
2621 name, name_len);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002622
Johan Hedbergb644ba32012-01-17 21:48:47 +02002623 if (hci_resolve_next_name(hdev))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002624 return;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002625
2626discov_complete:
2627 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2628}
2629
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002630static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
2631{
Johan Hedberg127178d2010-11-18 22:22:29 +02002632 struct hci_cp_remote_name_req *cp;
2633 struct hci_conn *conn;
2634
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002635 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02002636
2637 /* If successful wait for the name req complete event before
2638 * checking for the need to do authentication */
2639 if (!status)
2640 return;
2641
2642 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
2643 if (!cp)
2644 return;
2645
2646 hci_dev_lock(hdev);
2647
2648 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002649
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002650 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergb644ba32012-01-17 21:48:47 +02002651 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
2652
Johan Hedberg79c6c702011-04-28 11:28:55 -07002653 if (!conn)
2654 goto unlock;
2655
2656 if (!hci_outgoing_auth_needed(hdev, conn))
2657 goto unlock;
2658
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002659 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02002660 struct hci_cp_auth_requested auth_cp;
2661
Johan Hedberg977f8fc2014-07-17 15:35:39 +03002662 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2663
Johannes Bergc1f23a22013-10-07 18:19:16 +02002664 auth_cp.handle = __cpu_to_le16(conn->handle);
2665 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
2666 sizeof(auth_cp), &auth_cp);
Johan Hedberg127178d2010-11-18 22:22:29 +02002667 }
2668
Johan Hedberg79c6c702011-04-28 11:28:55 -07002669unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02002670 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002671}
2672
Marcel Holtmann769be972008-07-14 20:13:49 +02002673static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
2674{
2675 struct hci_cp_read_remote_features *cp;
2676 struct hci_conn *conn;
2677
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002678 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02002679
2680 if (!status)
2681 return;
2682
2683 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
2684 if (!cp)
2685 return;
2686
2687 hci_dev_lock(hdev);
2688
2689 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2690 if (conn) {
2691 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002692 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002693 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002694 }
2695 }
2696
2697 hci_dev_unlock(hdev);
2698}
2699
2700static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
2701{
2702 struct hci_cp_read_remote_ext_features *cp;
2703 struct hci_conn *conn;
2704
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002705 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02002706
2707 if (!status)
2708 return;
2709
2710 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
2711 if (!cp)
2712 return;
2713
2714 hci_dev_lock(hdev);
2715
2716 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
2717 if (conn) {
2718 if (conn->state == BT_CONFIG) {
Johan Hedberg539c4962015-02-18 14:53:57 +02002719 hci_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002720 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002721 }
2722 }
2723
2724 hci_dev_unlock(hdev);
2725}
2726
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002727static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2728{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002729 struct hci_cp_setup_sync_conn *cp;
2730 struct hci_conn *acl, *sco;
2731 __u16 handle;
2732
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002733 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002734
2735 if (!status)
2736 return;
2737
2738 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
2739 if (!cp)
2740 return;
2741
2742 handle = __le16_to_cpu(cp->handle);
2743
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002744 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002745
2746 hci_dev_lock(hdev);
2747
2748 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002749 if (acl) {
2750 sco = acl->link;
2751 if (sco) {
2752 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002753
Johan Hedberg539c4962015-02-18 14:53:57 +02002754 hci_connect_cfm(sco, status);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02002755 hci_conn_del(sco);
2756 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002757 }
2758
2759 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002760}
2761
Kiran Kb2af2642021-09-07 15:42:43 +05302762static void hci_cs_enhanced_setup_sync_conn(struct hci_dev *hdev, __u8 status)
2763{
2764 struct hci_cp_enhanced_setup_sync_conn *cp;
2765 struct hci_conn *acl, *sco;
2766 __u16 handle;
2767
2768 bt_dev_dbg(hdev, "status 0x%2.2x", status);
2769
2770 if (!status)
2771 return;
2772
2773 cp = hci_sent_cmd_data(hdev, HCI_OP_ENHANCED_SETUP_SYNC_CONN);
2774 if (!cp)
2775 return;
2776
2777 handle = __le16_to_cpu(cp->handle);
2778
2779 bt_dev_dbg(hdev, "handle 0x%4.4x", handle);
2780
2781 hci_dev_lock(hdev);
2782
2783 acl = hci_conn_hash_lookup_handle(hdev, handle);
2784 if (acl) {
2785 sco = acl->link;
2786 if (sco) {
2787 sco->state = BT_CLOSED;
2788
2789 hci_connect_cfm(sco, status);
2790 hci_conn_del(sco);
2791 }
2792 }
2793
2794 hci_dev_unlock(hdev);
2795}
2796
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002797static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
2798{
2799 struct hci_cp_sniff_mode *cp;
2800 struct hci_conn *conn;
2801
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002802 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002803
2804 if (!status)
2805 return;
2806
2807 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
2808 if (!cp)
2809 return;
2810
2811 hci_dev_lock(hdev);
2812
2813 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002814 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002815 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002816
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002817 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002818 hci_sco_setup(conn, status);
2819 }
2820
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002821 hci_dev_unlock(hdev);
2822}
2823
2824static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
2825{
2826 struct hci_cp_exit_sniff_mode *cp;
2827 struct hci_conn *conn;
2828
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002829 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002830
2831 if (!status)
2832 return;
2833
2834 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
2835 if (!cp)
2836 return;
2837
2838 hci_dev_lock(hdev);
2839
2840 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002841 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002842 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002843
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002844 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002845 hci_sco_setup(conn, status);
2846 }
2847
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002848 hci_dev_unlock(hdev);
2849}
2850
Johan Hedberg88c3df12012-02-09 14:27:38 +02002851static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
2852{
2853 struct hci_cp_disconnect *cp;
Luiz Augusto von Dentz182ee452021-10-27 16:59:00 -07002854 struct hci_conn_params *params;
Johan Hedberg88c3df12012-02-09 14:27:38 +02002855 struct hci_conn *conn;
Luiz Augusto von Dentz182ee452021-10-27 16:59:00 -07002856 bool mgmt_conn;
Johan Hedberg88c3df12012-02-09 14:27:38 +02002857
Luiz Augusto von Dentz182ee452021-10-27 16:59:00 -07002858 /* Wait for HCI_EV_DISCONN_COMPLETE if status 0x00 and not suspended
2859 * otherwise cleanup the connection immediately.
2860 */
2861 if (!status && !hdev->suspended)
Johan Hedberg88c3df12012-02-09 14:27:38 +02002862 return;
2863
2864 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
2865 if (!cp)
2866 return;
2867
2868 hci_dev_lock(hdev);
2869
2870 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Luiz Augusto von Dentz182ee452021-10-27 16:59:00 -07002871 if (!conn)
2872 goto unlock;
2873
2874 if (status) {
Johan Hedberg88c3df12012-02-09 14:27:38 +02002875 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002876 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002877
Luiz Augusto von Dentz1eeaa1a2021-08-20 16:40:38 -07002878 if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07002879 hdev->cur_adv_instance = conn->adv_instance;
Luiz Augusto von Dentzabfeea42021-10-27 16:58:45 -07002880 hci_enable_advertising(hdev);
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07002881 }
2882
Luiz Augusto von Dentz182ee452021-10-27 16:59:00 -07002883 goto done;
Joseph Hwangb8d29052020-03-11 19:20:14 -07002884 }
2885
Luiz Augusto von Dentz182ee452021-10-27 16:59:00 -07002886 mgmt_conn = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
2887
2888 if (conn->type == ACL_LINK) {
2889 if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2890 hci_remove_link_key(hdev, &conn->dst);
2891 }
2892
2893 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2894 if (params) {
2895 switch (params->auto_connect) {
2896 case HCI_AUTO_CONN_LINK_LOSS:
2897 if (cp->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2898 break;
2899 fallthrough;
2900
2901 case HCI_AUTO_CONN_DIRECT:
2902 case HCI_AUTO_CONN_ALWAYS:
2903 list_del_init(&params->action);
2904 list_add(&params->action, &hdev->pend_le_conns);
2905 break;
2906
2907 default:
2908 break;
2909 }
2910 }
2911
2912 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2913 cp->reason, mgmt_conn);
2914
2915 hci_disconn_cfm(conn, cp->reason);
2916
2917done:
2918 /* If the disconnection failed for any reason, the upper layer
2919 * does not retry to disconnect in current implementation.
2920 * Hence, we need to do some basic cleanup here and re-enable
2921 * advertising if necessary.
2922 */
2923 hci_conn_del(conn);
2924unlock:
Johan Hedberg88c3df12012-02-09 14:27:38 +02002925 hci_dev_unlock(hdev);
2926}
2927
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002928static u8 ev_bdaddr_type(struct hci_dev *hdev, u8 type, bool *resolved)
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002929{
2930 /* When using controller based address resolution, then the new
2931 * address types 0x02 and 0x03 are used. These types need to be
2932 * converted back into either public address or random address type
2933 */
2934 switch (type) {
2935 case ADDR_LE_DEV_PUBLIC_RESOLVED:
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002936 if (resolved)
2937 *resolved = true;
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002938 return ADDR_LE_DEV_PUBLIC;
2939 case ADDR_LE_DEV_RANDOM_RESOLVED:
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002940 if (resolved)
2941 *resolved = true;
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002942 return ADDR_LE_DEV_RANDOM;
2943 }
2944
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002945 if (resolved)
2946 *resolved = false;
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07002947 return type;
2948}
2949
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302950static void cs_le_create_conn(struct hci_dev *hdev, bdaddr_t *peer_addr,
2951 u8 peer_addr_type, u8 own_address_type,
2952 u8 filter_policy)
2953{
2954 struct hci_conn *conn;
2955
2956 conn = hci_conn_hash_lookup_le(hdev, peer_addr,
2957 peer_addr_type);
2958 if (!conn)
2959 return;
2960
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07002961 own_address_type = ev_bdaddr_type(hdev, own_address_type, NULL);
Sathish Narasimmanb31bc002020-07-23 18:08:59 +05302962
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302963 /* Store the initiator and responder address information which
2964 * is needed for SMP. These values will not change during the
2965 * lifetime of the connection.
2966 */
2967 conn->init_addr_type = own_address_type;
2968 if (own_address_type == ADDR_LE_DEV_RANDOM)
2969 bacpy(&conn->init_addr, &hdev->random_addr);
2970 else
2971 bacpy(&conn->init_addr, &hdev->bdaddr);
2972
2973 conn->resp_addr_type = peer_addr_type;
2974 bacpy(&conn->resp_addr, peer_addr);
2975
2976 /* We don't want the connection attempt to stick around
2977 * indefinitely since LE doesn't have a page timeout concept
2978 * like BR/EDR. Set a timer for any connection that doesn't use
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08002979 * the accept list for connecting.
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05302980 */
2981 if (filter_policy == HCI_LE_USE_PEER_ADDR)
2982 queue_delayed_work(conn->hdev->workqueue,
2983 &conn->le_conn_timeout,
2984 conn->conn_timeout);
2985}
2986
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002987static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
2988{
2989 struct hci_cp_le_create_conn *cp;
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002990
2991 BT_DBG("%s status 0x%2.2x", hdev->name, status);
2992
2993 /* All connection failure handling is taken care of by the
2994 * hci_le_conn_failed function which is triggered by the HCI
2995 * request completion callbacks used for connecting.
2996 */
2997 if (status)
2998 return;
2999
3000 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
3001 if (!cp)
3002 return;
3003
3004 hci_dev_lock(hdev);
3005
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05303006 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
3007 cp->own_address_type, cp->filter_policy);
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02003008
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02003009 hci_dev_unlock(hdev);
3010}
3011
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02003012static void hci_cs_le_ext_create_conn(struct hci_dev *hdev, u8 status)
3013{
3014 struct hci_cp_le_ext_create_conn *cp;
3015
3016 BT_DBG("%s status 0x%2.2x", hdev->name, status);
3017
3018 /* All connection failure handling is taken care of by the
3019 * hci_le_conn_failed function which is triggered by the HCI
3020 * request completion callbacks used for connecting.
3021 */
3022 if (status)
3023 return;
3024
3025 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_EXT_CREATE_CONN);
3026 if (!cp)
3027 return;
3028
3029 hci_dev_lock(hdev);
3030
3031 cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
3032 cp->own_addr_type, cp->filter_policy);
3033
3034 hci_dev_unlock(hdev);
3035}
3036
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07003037static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
3038{
3039 struct hci_cp_le_read_remote_features *cp;
3040 struct hci_conn *conn;
3041
3042 BT_DBG("%s status 0x%2.2x", hdev->name, status);
3043
3044 if (!status)
3045 return;
3046
3047 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
3048 if (!cp)
3049 return;
3050
3051 hci_dev_lock(hdev);
3052
3053 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
3054 if (conn) {
3055 if (conn->state == BT_CONFIG) {
3056 hci_connect_cfm(conn, status);
3057 hci_conn_drop(conn);
3058 }
3059 }
3060
3061 hci_dev_unlock(hdev);
3062}
3063
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02003064static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
3065{
3066 struct hci_cp_le_start_enc *cp;
3067 struct hci_conn *conn;
3068
3069 BT_DBG("%s status 0x%2.2x", hdev->name, status);
3070
3071 if (!status)
3072 return;
3073
3074 hci_dev_lock(hdev);
3075
3076 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
3077 if (!cp)
3078 goto unlock;
3079
3080 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
3081 if (!conn)
3082 goto unlock;
3083
3084 if (conn->state != BT_CONNECTED)
3085 goto unlock;
3086
3087 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
3088 hci_conn_drop(conn);
3089
3090unlock:
3091 hci_dev_unlock(hdev);
3092}
3093
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01003094static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
3095{
3096 struct hci_cp_switch_role *cp;
3097 struct hci_conn *conn;
3098
3099 BT_DBG("%s status 0x%2.2x", hdev->name, status);
3100
3101 if (!status)
3102 return;
3103
3104 cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
3105 if (!cp)
3106 return;
3107
3108 hci_dev_lock(hdev);
3109
3110 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
3111 if (conn)
3112 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
3113
3114 hci_dev_unlock(hdev);
3115}
3116
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003117static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003118{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003119 struct hci_ev_status *ev;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003120 struct discovery_state *discov = &hdev->discovery;
3121 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003122
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003123 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_INQUIRY_COMPLETE, sizeof(*ev));
3124 if (!ev)
3125 return;
3126
3127 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003128
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003129 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03003130
3131 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
3132 return;
3133
Peter Zijlstra4e857c52014-03-17 18:06:10 +01003134 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -03003135 wake_up_bit(&hdev->flags, HCI_INQUIRY);
3136
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003137 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003138 return;
3139
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003140 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003141
Andre Guedes343f9352012-02-17 20:39:37 -03003142 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003143 goto unlock;
3144
3145 if (list_empty(&discov->resolve)) {
Jakub Pawlowski07d23342015-03-17 09:04:14 -07003146 /* When BR/EDR inquiry is active and no LE scanning is in
3147 * progress, then change discovery state to indicate completion.
3148 *
3149 * When running LE scanning and BR/EDR inquiry simultaneously
3150 * and the LE scan already finished, then change the discovery
3151 * state to indicate completion.
3152 */
3153 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
3154 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
3155 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003156 goto unlock;
3157 }
3158
3159 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
3160 if (e && hci_resolve_name(hdev, e) == 0) {
3161 e->name_state = NAME_PENDING;
3162 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
Archie Pusakadbf68112021-11-25 15:04:37 +08003163 discov->name_resolve_timeout = jiffies + NAME_RESOLVE_DURATION;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003164 } else {
Jakub Pawlowski07d23342015-03-17 09:04:14 -07003165 /* When BR/EDR inquiry is active and no LE scanning is in
3166 * progress, then change discovery state to indicate completion.
3167 *
3168 * When running LE scanning and BR/EDR inquiry simultaneously
3169 * and the LE scan already finished, then change the discovery
3170 * state to indicate completion.
3171 */
3172 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
3173 !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
3174 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02003175 }
3176
3177unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003178 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003179}
3180
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003181static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Luiz Augusto von Dentz27d9eb42021-12-01 10:54:56 -08003183 struct hci_ev_inquiry_result *ev;
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07003184 struct inquiry_data data;
Luiz Augusto von Dentz27d9eb42021-12-01 10:54:56 -08003185 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Luiz Augusto von Dentz27d9eb42021-12-01 10:54:56 -08003187 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_INQUIRY_RESULT, sizeof(*ev));
3188 if (!ev)
3189 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190
Luiz Augusto von Dentz27d9eb42021-12-01 10:54:56 -08003191 if (!hci_ev_skb_pull(hdev, skb, HCI_EV_INQUIRY_RESULT,
3192 flex_array_size(ev, info, ev->num)))
3193 return;
3194
3195 BT_DBG("%s num %d", hdev->name, ev->num);
3196
3197 if (!ev->num)
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07003198 return;
3199
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003200 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03003201 return;
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07003204
Luiz Augusto von Dentz27d9eb42021-12-01 10:54:56 -08003205 for (i = 0; i < ev->num; i++) {
3206 struct inquiry_info *info = &ev->info[i];
Marcel Holtmannaf589252014-07-01 14:11:20 +02003207 u32 flags;
Johan Hedberg31754052012-01-04 13:39:52 +02003208
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 bacpy(&data.bdaddr, &info->bdaddr);
3210 data.pscan_rep_mode = info->pscan_rep_mode;
3211 data.pscan_period_mode = info->pscan_period_mode;
3212 data.pscan_mode = info->pscan_mode;
3213 memcpy(data.dev_class, info->dev_class, 3);
3214 data.clock_offset = info->clock_offset;
Marcel Holtmannefb25132014-12-05 13:03:34 +01003215 data.rssi = HCI_RSSI_INVALID;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003216 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02003217
Marcel Holtmannaf589252014-07-01 14:11:20 +02003218 flags = hci_inquiry_cache_update(hdev, &data, false);
3219
Johan Hedberg48264f02011-11-09 13:58:58 +02003220 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannefb25132014-12-05 13:03:34 +01003221 info->dev_class, HCI_RSSI_INVALID,
3222 flags, NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07003224
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 hci_dev_unlock(hdev);
3226}
3227
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003228static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003230 struct hci_ev_conn_complete *ev;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003231 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003233 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_CONN_COMPLETE, sizeof(*ev));
3234 if (!ev)
3235 return;
3236
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003237 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07003238
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07003240
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003241 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02003242 if (!conn) {
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07003243 /* Connection may not exist if auto-connected. Check the bredr
3244 * allowlist to see if this device is allowed to auto connect.
3245 * If link is an ACL type, create a connection class
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07003246 * automatically.
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07003247 *
3248 * Auto-connect will only occur if the event filter is
3249 * programmed with a given address. Right now, event filter is
3250 * only used during suspend.
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07003251 */
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07003252 if (ev->link_type == ACL_LINK &&
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003253 hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
Sonny Sasakaa46b7ed2020-08-14 12:09:09 -07003254 &ev->bdaddr,
3255 BDADDR_BREDR)) {
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07003256 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
3257 HCI_ROLE_SLAVE);
3258 if (!conn) {
3259 bt_dev_err(hdev, "no memory for new conn");
3260 goto unlock;
3261 }
Abhishek Pandit-Subedi2d186fc2020-03-19 17:07:13 -07003262 } else {
3263 if (ev->link_type != SCO_LINK)
3264 goto unlock;
3265
3266 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK,
3267 &ev->bdaddr);
3268 if (!conn)
3269 goto unlock;
3270
3271 conn->type = SCO_LINK;
Abhishek Pandit-Subedi4f40afc2020-03-11 08:54:01 -07003272 }
Marcel Holtmann94992372009-04-19 19:30:03 +02003273 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07003274
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003275 if (!ev->status) {
3276 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02003277
3278 if (conn->type == ACL_LINK) {
3279 conn->state = BT_CONFIG;
3280 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02003281
3282 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
3283 !hci_find_link_key(hdev, &ev->bdaddr))
3284 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
3285 else
3286 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02003287 } else
3288 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003289
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01003290 hci_debugfs_create_conn(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02003291 hci_conn_add_sysfs(conn);
3292
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003293 if (test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03003294 set_bit(HCI_CONN_AUTH, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003295
3296 if (test_bit(HCI_ENCRYPT, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03003297 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003298
3299 /* Get remote features */
3300 if (conn->type == ACL_LINK) {
3301 struct hci_cp_read_remote_features cp;
3302 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02003303 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003304 sizeof(cp), &cp);
Johan Hedberg22f433d2014-08-01 11:13:32 +03003305
Johan Hedberg01b1cb82015-11-16 12:52:21 +02003306 hci_req_update_scan(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07003307 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07003308
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003309 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02003310 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003311 struct hci_cp_change_conn_ptype cp;
3312 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02003313 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003314 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
3315 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003316 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02003317 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003318 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02003319 if (conn->type == ACL_LINK)
Marcel Holtmann64c7b772014-02-18 14:22:20 -08003320 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003321 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02003322 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003323
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003324 if (conn->type == ACL_LINK)
3325 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07003326
Marcel Holtmann769be972008-07-14 20:13:49 +02003327 if (ev->status) {
Johan Hedberg539c4962015-02-18 14:53:57 +02003328 hci_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003329 hci_conn_del(conn);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02003330 } else if (ev->link_type == SCO_LINK) {
3331 switch (conn->setting & SCO_AIRMODE_MASK) {
3332 case SCO_AIRMODE_CVSD:
3333 if (hdev->notify)
3334 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
3335 break;
3336 }
3337
Johan Hedberg539c4962015-02-18 14:53:57 +02003338 hci_connect_cfm(conn, ev->status);
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02003339 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003340
3341unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003343
3344 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345}
3346
Johan Hedberg70c46422014-07-09 12:59:17 +03003347static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
3348{
3349 struct hci_cp_reject_conn_req cp;
3350
3351 bacpy(&cp.bdaddr, bdaddr);
3352 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
3353 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
3354}
3355
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003356static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003358 struct hci_ev_conn_request *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 int mask = hdev->link_mode;
Johan Hedberg70c46422014-07-09 12:59:17 +03003360 struct inquiry_entry *ie;
3361 struct hci_conn *conn;
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01003362 __u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003364 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_CONN_REQUEST, sizeof(*ev));
3365 if (!ev)
3366 return;
3367
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003368 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003369 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
Frédéric Dalleau20714bfe2012-11-21 10:51:12 +01003371 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
3372 &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373
Johan Hedberg70c46422014-07-09 12:59:17 +03003374 if (!(mask & HCI_LM_ACCEPT)) {
3375 hci_reject_conn(hdev, &ev->bdaddr);
3376 return;
3377 }
3378
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003379 if (hci_bdaddr_list_lookup(&hdev->reject_list, &ev->bdaddr,
Johan Hedberg46c4c942014-07-16 16:19:21 +03003380 BDADDR_BREDR)) {
3381 hci_reject_conn(hdev, &ev->bdaddr);
3382 return;
3383 }
3384
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003385 /* Require HCI_CONNECTABLE or an accept list entry to accept the
Johan Hedberg6a8fc952014-12-24 20:43:11 +02003386 * connection. These features are only touched through mgmt so
3387 * only do the checks if HCI_MGMT is set.
3388 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003389 if (hci_dev_test_flag(hdev, HCI_MGMT) &&
3390 !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08003391 !hci_bdaddr_list_lookup_with_flags(&hdev->accept_list, &ev->bdaddr,
Abhishek Pandit-Subedi8baaa402020-06-17 16:39:08 +02003392 BDADDR_BREDR)) {
3393 hci_reject_conn(hdev, &ev->bdaddr);
3394 return;
Johan Hedberg70c46422014-07-09 12:59:17 +03003395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Johan Hedberg70c46422014-07-09 12:59:17 +03003397 /* Connection accepted */
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003398
Johan Hedberg70c46422014-07-09 12:59:17 +03003399 hci_dev_lock(hdev);
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02003400
Johan Hedberg70c46422014-07-09 12:59:17 +03003401 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3402 if (ie)
3403 memcpy(ie->data.dev_class, ev->dev_class, 3);
3404
3405 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
3406 &ev->bdaddr);
3407 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03003408 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
3409 HCI_ROLE_SLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003411 bt_dev_err(hdev, "no memory for new connection");
Johan Hedberg70c46422014-07-09 12:59:17 +03003412 hci_dev_unlock(hdev);
3413 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 }
Johan Hedberg70c46422014-07-09 12:59:17 +03003415 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003416
Johan Hedberg70c46422014-07-09 12:59:17 +03003417 memcpy(conn->dev_class, ev->dev_class, 3);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003418
Johan Hedberg70c46422014-07-09 12:59:17 +03003419 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420
Johan Hedberg70c46422014-07-09 12:59:17 +03003421 if (ev->link_type == ACL_LINK ||
3422 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
3423 struct hci_cp_accept_conn_req cp;
3424 conn->state = BT_CONNECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 bacpy(&cp.bdaddr, &ev->bdaddr);
Johan Hedberg70c46422014-07-09 12:59:17 +03003427
3428 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
Archie Pusaka74be5232021-06-04 16:26:25 +08003429 cp.role = 0x00; /* Become central */
Johan Hedberg70c46422014-07-09 12:59:17 +03003430 else
Archie Pusaka74be5232021-06-04 16:26:25 +08003431 cp.role = 0x01; /* Remain peripheral */
Johan Hedberg70c46422014-07-09 12:59:17 +03003432
3433 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
3434 } else if (!(flags & HCI_PROTO_DEFER)) {
3435 struct hci_cp_accept_sync_conn_req cp;
3436 conn->state = BT_CONNECT;
3437
3438 bacpy(&cp.bdaddr, &ev->bdaddr);
3439 cp.pkt_type = cpu_to_le16(conn->pkt_type);
3440
3441 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
3442 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
3443 cp.max_latency = cpu_to_le16(0xffff);
3444 cp.content_format = cpu_to_le16(hdev->voice_setting);
3445 cp.retrans_effort = 0xff;
3446
3447 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
3448 &cp);
3449 } else {
3450 conn->state = BT_CONNECT2;
Johan Hedberg539c4962015-02-18 14:53:57 +02003451 hci_connect_cfm(conn, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
3453}
3454
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02003455static u8 hci_to_mgmt_reason(u8 err)
3456{
3457 switch (err) {
3458 case HCI_ERROR_CONNECTION_TIMEOUT:
3459 return MGMT_DEV_DISCONN_TIMEOUT;
3460 case HCI_ERROR_REMOTE_USER_TERM:
3461 case HCI_ERROR_REMOTE_LOW_RESOURCES:
3462 case HCI_ERROR_REMOTE_POWER_OFF:
3463 return MGMT_DEV_DISCONN_REMOTE;
3464 case HCI_ERROR_LOCAL_HOST_TERM:
3465 return MGMT_DEV_DISCONN_LOCAL_HOST;
3466 default:
3467 return MGMT_DEV_DISCONN_UNKNOWN;
3468 }
3469}
3470
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003471static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003473 struct hci_ev_disconn_complete *ev;
Szymon Janc160b9252016-07-12 02:12:16 +02003474 u8 reason;
Andre Guedes9fcb18e2014-02-26 20:21:48 -03003475 struct hci_conn_params *params;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003476 struct hci_conn *conn;
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02003477 bool mgmt_connected;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003479 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_DISCONN_COMPLETE, sizeof(*ev));
3480 if (!ev)
3481 return;
3482
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003483 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 hci_dev_lock(hdev);
3486
Marcel Holtmann04837f62006-07-03 10:02:33 +02003487 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02003488 if (!conn)
3489 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02003490
Andre Guedesabf54a52013-11-07 17:36:09 -03003491 if (ev->status) {
3492 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
3493 conn->dst_type, ev->status);
3494 goto unlock;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02003495 }
Johan Hedbergf7520542011-01-20 12:34:39 +02003496
Andre Guedes38462202013-11-07 17:36:10 -03003497 conn->state = BT_CLOSED;
3498
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02003499 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
Szymon Janc160b9252016-07-12 02:12:16 +02003500
3501 if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
3502 reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
3503 else
3504 reason = hci_to_mgmt_reason(ev->reason);
3505
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02003506 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
3507 reason, mgmt_connected);
Andre Guedesabf54a52013-11-07 17:36:09 -03003508
Johan Hedberg22f433d2014-08-01 11:13:32 +03003509 if (conn->type == ACL_LINK) {
3510 if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
3511 hci_remove_link_key(hdev, &conn->dst);
3512
Johan Hedberg01b1cb82015-11-16 12:52:21 +02003513 hci_req_update_scan(hdev);
Johan Hedberg22f433d2014-08-01 11:13:32 +03003514 }
Johan Hedberg22102462013-10-05 12:01:06 +02003515
Andre Guedes9fcb18e2014-02-26 20:21:48 -03003516 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
3517 if (params) {
3518 switch (params->auto_connect) {
3519 case HCI_AUTO_CONN_LINK_LOSS:
3520 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
3521 break;
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05003522 fallthrough;
Andre Guedes9fcb18e2014-02-26 20:21:48 -03003523
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02003524 case HCI_AUTO_CONN_DIRECT:
Andre Guedes9fcb18e2014-02-26 20:21:48 -03003525 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg418025d2014-07-04 12:37:24 +03003526 list_del_init(&params->action);
3527 list_add(&params->action, &hdev->pend_le_conns);
Luiz Augusto von Dentz5bee2fd2021-10-27 16:58:43 -07003528 hci_update_passive_scan(hdev);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03003529 break;
3530
3531 default:
3532 break;
3533 }
3534 }
3535
Johan Hedberg3a6d5762015-02-18 14:53:58 +02003536 hci_disconn_cfm(conn, ev->reason);
Andre Guedes38462202013-11-07 17:36:10 -03003537
3538 /* Re-enable advertising if necessary, since it might
3539 * have been disabled by the connection. From the
3540 * HCI_LE_Set_Advertise_Enable command description in
3541 * the core specification (v4.0):
3542 * "The Controller shall continue advertising until the Host
3543 * issues an LE_Set_Advertise_Enable command with
3544 * Advertising_Enable set to 0x00 (Advertising is disabled)
3545 * or until a connection is created or until the Advertising
3546 * is timed out due to Directed Advertising."
3547 */
Luiz Augusto von Dentz1eeaa1a2021-08-20 16:40:38 -07003548 if (conn->type == LE_LINK && conn->role == HCI_ROLE_SLAVE) {
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07003549 hdev->cur_adv_instance = conn->adv_instance;
Luiz Augusto von Dentzabfeea42021-10-27 16:58:45 -07003550 hci_enable_advertising(hdev);
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07003551 }
3552
3553 hci_conn_del(conn);
Johan Hedbergf7520542011-01-20 12:34:39 +02003554
3555unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 hci_dev_unlock(hdev);
3557}
3558
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003559static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003560{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003561 struct hci_ev_auth_complete *ev;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003562 struct hci_conn *conn;
3563
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003564 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_AUTH_COMPLETE, sizeof(*ev));
3565 if (!ev)
3566 return;
3567
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003568 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003569
3570 hci_dev_lock(hdev);
3571
3572 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003573 if (!conn)
3574 goto unlock;
3575
3576 if (!ev->status) {
Szymon Janc160b9252016-07-12 02:12:16 +02003577 clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3578
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02003579 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03003580 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003581 bt_dev_info(hdev, "re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03003582 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03003583 set_bit(HCI_CONN_AUTH, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003584 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03003585 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003586 } else {
Szymon Janc160b9252016-07-12 02:12:16 +02003587 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3588 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3589
Johan Hedberge1e930f2014-09-08 17:09:49 -07003590 mgmt_auth_failed(conn, ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003591 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003592
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003593 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
3594 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003595
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003596 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02003597 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003598 struct hci_cp_set_conn_encrypt cp;
3599 cp.handle = ev->handle;
3600 cp.encrypt = 0x01;
3601 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03003602 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003603 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003604 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02003605 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003606 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003607 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003608 } else {
3609 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003610
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003611 hci_conn_hold(conn);
3612 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003613 hci_conn_drop(conn);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003614 }
3615
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003616 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003617 if (!ev->status) {
3618 struct hci_cp_set_conn_encrypt cp;
3619 cp.handle = ev->handle;
3620 cp.encrypt = 0x01;
3621 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03003622 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003623 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003624 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003625 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003626 }
3627 }
3628
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02003629unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003630 hci_dev_unlock(hdev);
3631}
3632
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003633static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003634{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003635 struct hci_ev_remote_name *ev;
Johan Hedberg127178d2010-11-18 22:22:29 +02003636 struct hci_conn *conn;
3637
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003638 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_REMOTE_NAME, sizeof(*ev));
3639 if (!ev)
3640 return;
3641
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003642 BT_DBG("%s", hdev->name);
3643
3644 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02003645
3646 hci_dev_lock(hdev);
3647
3648 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003649
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07003650 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedbergb644ba32012-01-17 21:48:47 +02003651 goto check_auth;
3652
3653 if (ev->status == 0)
3654 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003655 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02003656 else
3657 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
3658
3659check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07003660 if (!conn)
3661 goto unlock;
3662
3663 if (!hci_outgoing_auth_needed(hdev, conn))
3664 goto unlock;
3665
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003666 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003667 struct hci_cp_auth_requested cp;
Johan Hedberg977f8fc2014-07-17 15:35:39 +03003668
3669 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
3670
Johan Hedberg127178d2010-11-18 22:22:29 +02003671 cp.handle = __cpu_to_le16(conn->handle);
3672 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
3673 }
3674
Johan Hedberg79c6c702011-04-28 11:28:55 -07003675unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02003676 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003677}
3678
Johan Hedberg821f3762015-06-11 13:52:29 +03003679static void read_enc_key_size_complete(struct hci_dev *hdev, u8 status,
3680 u16 opcode, struct sk_buff *skb)
3681{
3682 const struct hci_rp_read_enc_key_size *rp;
3683 struct hci_conn *conn;
3684 u16 handle;
3685
3686 BT_DBG("%s status 0x%02x", hdev->name, status);
3687
3688 if (!skb || skb->len < sizeof(*rp)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003689 bt_dev_err(hdev, "invalid read key size response");
Johan Hedberg821f3762015-06-11 13:52:29 +03003690 return;
3691 }
3692
3693 rp = (void *)skb->data;
3694 handle = le16_to_cpu(rp->handle);
3695
3696 hci_dev_lock(hdev);
3697
3698 conn = hci_conn_hash_lookup_handle(hdev, handle);
3699 if (!conn)
3700 goto unlock;
3701
Alain Michaud32b50722020-03-25 14:48:34 +00003702 /* While unexpected, the read_enc_key_size command may fail. The most
3703 * secure approach is to then assume the key size is 0 to force a
3704 * disconnection.
Johan Hedberg821f3762015-06-11 13:52:29 +03003705 */
3706 if (rp->status) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003707 bt_dev_err(hdev, "failed to read key size for handle %u",
3708 handle);
Alain Michaud32b50722020-03-25 14:48:34 +00003709 conn->enc_key_size = 0;
Johan Hedberg821f3762015-06-11 13:52:29 +03003710 } else {
3711 conn->enc_key_size = rp->key_size;
3712 }
3713
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003714 hci_encrypt_cfm(conn, 0);
Johan Hedberg821f3762015-06-11 13:52:29 +03003715
3716unlock:
3717 hci_dev_unlock(hdev);
3718}
3719
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003720static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003721{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003722 struct hci_ev_encrypt_change *ev;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003723 struct hci_conn *conn;
3724
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003725 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_ENCRYPT_CHANGE, sizeof(*ev));
3726 if (!ev)
3727 return;
3728
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003729 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003730
3731 hci_dev_lock(hdev);
3732
3733 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003734 if (!conn)
3735 goto unlock;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003736
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003737 if (!ev->status) {
3738 if (ev->encrypt) {
3739 /* Encryption implies authentication */
Johan Hedberg4dae2792014-06-24 17:03:50 +03003740 set_bit(HCI_CONN_AUTH, &conn->flags);
3741 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003742 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003743
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08003744 /* P-256 authentication key implies FIPS */
3745 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
Johan Hedberg4dae2792014-06-24 17:03:50 +03003746 set_bit(HCI_CONN_FIPS, &conn->flags);
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08003747
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003748 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
3749 conn->type == LE_LINK)
3750 set_bit(HCI_CONN_AES_CCM, &conn->flags);
3751 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03003752 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08003753 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
3754 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003755 }
3756
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07003757 /* We should disregard the current RPA and generate a new one
3758 * whenever the encryption procedure fails.
3759 */
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303760 if (ev->status && conn->type == LE_LINK) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07003761 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05303762 hci_adv_instances_set_rpa_expired(hdev, true);
3763 }
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07003764
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003765 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3766
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07003767 /* Check link security requirements are met */
3768 if (!hci_conn_check_link_mode(conn))
3769 ev->status = HCI_ERROR_AUTH_FAILURE;
3770
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003771 if (ev->status && conn->state == BT_CONNECTED) {
Szymon Janc160b9252016-07-12 02:12:16 +02003772 if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
3773 set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
3774
Luiz Augusto von Dentz8746f132020-05-20 14:20:14 -07003775 /* Notify upper layers so they can cleanup before
3776 * disconnecting.
3777 */
3778 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003779 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
3780 hci_conn_drop(conn);
3781 goto unlock;
3782 }
3783
Johan Hedberg821f3762015-06-11 13:52:29 +03003784 /* Try reading the encryption key size for encrypted ACL links */
3785 if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
3786 struct hci_cp_read_enc_key_size cp;
3787 struct hci_request req;
3788
3789 /* Only send HCI_Read_Encryption_Key_Size if the
3790 * controller really supports it. If it doesn't, assume
3791 * the default size (16).
3792 */
3793 if (!(hdev->commands[20] & 0x10)) {
3794 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3795 goto notify;
3796 }
3797
3798 hci_req_init(&req, hdev);
3799
3800 cp.handle = cpu_to_le16(conn->handle);
3801 hci_req_add(&req, HCI_OP_READ_ENC_KEY_SIZE, sizeof(cp), &cp);
3802
3803 if (hci_req_run_skb(&req, read_enc_key_size_complete)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003804 bt_dev_err(hdev, "sending read key size failed");
Johan Hedberg821f3762015-06-11 13:52:29 +03003805 conn->enc_key_size = HCI_LINK_KEY_SIZE;
3806 goto notify;
3807 }
3808
3809 goto unlock;
3810 }
3811
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +05303812 /* Set the default Authenticated Payload Timeout after
3813 * an LE Link is established. As per Core Spec v5.0, Vol 2, Part B
3814 * Section 3.3, the HCI command WRITE_AUTH_PAYLOAD_TIMEOUT should be
3815 * sent when the link is active and Encryption is enabled, the conn
3816 * type can be either LE or ACL and controller must support LMP Ping.
3817 * Ensure for AES-CCM encryption as well.
3818 */
3819 if (test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
3820 test_bit(HCI_CONN_AES_CCM, &conn->flags) &&
3821 ((conn->type == ACL_LINK && lmp_ping_capable(hdev)) ||
3822 (conn->type == LE_LINK && (hdev->le_features[0] & HCI_LE_PING)))) {
3823 struct hci_cp_write_auth_payload_to cp;
3824
3825 cp.handle = cpu_to_le16(conn->handle);
3826 cp.timeout = cpu_to_le16(hdev->auth_payload_timeout);
3827 hci_send_cmd(conn->hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO,
3828 sizeof(cp), &cp);
3829 }
3830
Johan Hedberg821f3762015-06-11 13:52:29 +03003831notify:
Luiz Augusto von Dentz3ca44c12020-05-19 13:25:19 -07003832 hci_encrypt_cfm(conn, ev->status);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08003833
Gustavo Padovana7d77232012-05-13 03:20:07 -03003834unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003835 hci_dev_unlock(hdev);
3836}
3837
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003838static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
3839 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003840{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003841 struct hci_ev_change_link_key_complete *ev;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003842 struct hci_conn *conn;
3843
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003844 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_CHANGE_LINK_KEY_COMPLETE,
3845 sizeof(*ev));
3846 if (!ev)
3847 return;
3848
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003849 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003850
3851 hci_dev_lock(hdev);
3852
3853 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3854 if (conn) {
3855 if (!ev->status)
Johan Hedberg4dae2792014-06-24 17:03:50 +03003856 set_bit(HCI_CONN_SECURE, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003857
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003858 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003859
3860 hci_key_change_cfm(conn, ev->status);
3861 }
3862
3863 hci_dev_unlock(hdev);
3864}
3865
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003866static void hci_remote_features_evt(struct hci_dev *hdev,
3867 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003868{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003869 struct hci_ev_remote_features *ev;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003870 struct hci_conn *conn;
3871
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08003872 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_REMOTE_FEATURES, sizeof(*ev));
3873 if (!ev)
3874 return;
3875
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003876 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003877
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003878 hci_dev_lock(hdev);
3879
3880 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02003881 if (!conn)
3882 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02003883
Johan Hedbergccd556f2010-11-10 17:11:51 +02003884 if (!ev->status)
Johan Hedbergcad718e2013-04-17 15:00:51 +03003885 memcpy(conn->features[0], ev->features, 8);
Johan Hedbergccd556f2010-11-10 17:11:51 +02003886
3887 if (conn->state != BT_CONFIG)
3888 goto unlock;
3889
Szymon Jancac363cf2015-01-29 16:36:59 +01003890 if (!ev->status && lmp_ext_feat_capable(hdev) &&
3891 lmp_ext_feat_capable(conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003892 struct hci_cp_read_remote_ext_features cp;
3893 cp.handle = ev->handle;
3894 cp.page = 0x01;
3895 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003896 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02003897 goto unlock;
3898 }
3899
Johan Hedberg671267b2012-05-12 16:11:50 -03003900 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003901 struct hci_cp_remote_name_req cp;
3902 memset(&cp, 0, sizeof(cp));
3903 bacpy(&cp.bdaddr, &conn->dst);
3904 cp.pscan_rep_mode = 0x02;
3905 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003906 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07003907 mgmt_device_connected(hdev, conn, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02003908
Johan Hedberg127178d2010-11-18 22:22:29 +02003909 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003910 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02003911 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003912 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02003913 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003914
Johan Hedbergccd556f2010-11-10 17:11:51 +02003915unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003916 hci_dev_unlock(hdev);
3917}
3918
Kiran Kecb71f252021-08-16 05:07:47 +05303919static inline void handle_cmd_cnt_and_timer(struct hci_dev *hdev, u8 ncmd)
Manish Mandlikde75cd02021-04-29 10:24:22 -07003920{
Kiran Kecb71f252021-08-16 05:07:47 +05303921 cancel_delayed_work(&hdev->cmd_timer);
Manish Mandlikde75cd02021-04-29 10:24:22 -07003922
3923 if (!test_bit(HCI_RESET, &hdev->flags)) {
3924 if (ncmd) {
3925 cancel_delayed_work(&hdev->ncmd_timer);
3926 atomic_set(&hdev->cmd_cnt, 1);
3927 } else {
3928 schedule_delayed_work(&hdev->ncmd_timer,
3929 HCI_NCMD_TIMEOUT);
3930 }
3931 }
3932}
3933
Johan Hedberge62144872015-04-02 13:41:08 +03003934static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
3935 u16 *opcode, u8 *status,
3936 hci_req_complete_t *req_complete,
3937 hci_req_complete_skb_t *req_complete_skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003938{
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08003939 struct hci_ev_cmd_complete *ev;
3940
3941 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_CMD_COMPLETE, sizeof(*ev));
3942 if (!ev)
3943 return;
Johan Hedberge62144872015-04-02 13:41:08 +03003944
3945 *opcode = __le16_to_cpu(ev->opcode);
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08003946 *status = skb->data[0];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003947
Johan Hedberge62144872015-04-02 13:41:08 +03003948 switch (*opcode) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003949 case HCI_OP_INQUIRY_CANCEL:
Sonny Sasakaadf1d692020-05-06 12:55:03 -07003950 hci_cc_inquiry_cancel(hdev, skb, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003951 break;
3952
Andre Guedes4d934832012-03-21 00:03:35 -03003953 case HCI_OP_PERIODIC_INQ:
3954 hci_cc_periodic_inq(hdev, skb);
3955 break;
3956
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003957 case HCI_OP_EXIT_PERIODIC_INQ:
3958 hci_cc_exit_periodic_inq(hdev, skb);
3959 break;
3960
3961 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
3962 hci_cc_remote_name_req_cancel(hdev, skb);
3963 break;
3964
3965 case HCI_OP_ROLE_DISCOVERY:
3966 hci_cc_role_discovery(hdev, skb);
3967 break;
3968
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02003969 case HCI_OP_READ_LINK_POLICY:
3970 hci_cc_read_link_policy(hdev, skb);
3971 break;
3972
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003973 case HCI_OP_WRITE_LINK_POLICY:
3974 hci_cc_write_link_policy(hdev, skb);
3975 break;
3976
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02003977 case HCI_OP_READ_DEF_LINK_POLICY:
3978 hci_cc_read_def_link_policy(hdev, skb);
3979 break;
3980
3981 case HCI_OP_WRITE_DEF_LINK_POLICY:
3982 hci_cc_write_def_link_policy(hdev, skb);
3983 break;
3984
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003985 case HCI_OP_RESET:
3986 hci_cc_reset(hdev, skb);
3987 break;
3988
Marcel Holtmannc2f0f972015-01-12 09:21:25 -08003989 case HCI_OP_READ_STORED_LINK_KEY:
3990 hci_cc_read_stored_link_key(hdev, skb);
3991 break;
3992
Marcel Holtmanna93661202015-01-12 09:21:28 -08003993 case HCI_OP_DELETE_STORED_LINK_KEY:
3994 hci_cc_delete_stored_link_key(hdev, skb);
3995 break;
3996
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003997 case HCI_OP_WRITE_LOCAL_NAME:
3998 hci_cc_write_local_name(hdev, skb);
3999 break;
4000
4001 case HCI_OP_READ_LOCAL_NAME:
4002 hci_cc_read_local_name(hdev, skb);
4003 break;
4004
4005 case HCI_OP_WRITE_AUTH_ENABLE:
4006 hci_cc_write_auth_enable(hdev, skb);
4007 break;
4008
4009 case HCI_OP_WRITE_ENCRYPT_MODE:
4010 hci_cc_write_encrypt_mode(hdev, skb);
4011 break;
4012
4013 case HCI_OP_WRITE_SCAN_ENABLE:
4014 hci_cc_write_scan_enable(hdev, skb);
4015 break;
4016
Abhishek Pandit-Subedie5b0ad62021-03-03 08:34:04 -08004017 case HCI_OP_SET_EVENT_FLT:
4018 hci_cc_set_event_filter(hdev, skb);
4019 break;
4020
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004021 case HCI_OP_READ_CLASS_OF_DEV:
4022 hci_cc_read_class_of_dev(hdev, skb);
4023 break;
4024
4025 case HCI_OP_WRITE_CLASS_OF_DEV:
4026 hci_cc_write_class_of_dev(hdev, skb);
4027 break;
4028
4029 case HCI_OP_READ_VOICE_SETTING:
4030 hci_cc_read_voice_setting(hdev, skb);
4031 break;
4032
4033 case HCI_OP_WRITE_VOICE_SETTING:
4034 hci_cc_write_voice_setting(hdev, skb);
4035 break;
4036
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -07004037 case HCI_OP_READ_NUM_SUPPORTED_IAC:
4038 hci_cc_read_num_supported_iac(hdev, skb);
4039 break;
4040
Marcel Holtmann333140b2008-07-14 20:13:48 +02004041 case HCI_OP_WRITE_SSP_MODE:
4042 hci_cc_write_ssp_mode(hdev, skb);
4043 break;
4044
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004045 case HCI_OP_WRITE_SC_SUPPORT:
4046 hci_cc_write_sc_support(hdev, skb);
4047 break;
4048
Spoorthi Ravishankar Koppad302975c2019-06-21 14:51:56 +05304049 case HCI_OP_READ_AUTH_PAYLOAD_TO:
4050 hci_cc_read_auth_payload_timeout(hdev, skb);
4051 break;
4052
4053 case HCI_OP_WRITE_AUTH_PAYLOAD_TO:
4054 hci_cc_write_auth_payload_timeout(hdev, skb);
4055 break;
4056
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004057 case HCI_OP_READ_LOCAL_VERSION:
4058 hci_cc_read_local_version(hdev, skb);
4059 break;
4060
4061 case HCI_OP_READ_LOCAL_COMMANDS:
4062 hci_cc_read_local_commands(hdev, skb);
4063 break;
4064
4065 case HCI_OP_READ_LOCAL_FEATURES:
4066 hci_cc_read_local_features(hdev, skb);
4067 break;
4068
Andre Guedes971e3a42011-06-30 19:20:52 -03004069 case HCI_OP_READ_LOCAL_EXT_FEATURES:
4070 hci_cc_read_local_ext_features(hdev, skb);
4071 break;
4072
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004073 case HCI_OP_READ_BUFFER_SIZE:
4074 hci_cc_read_buffer_size(hdev, skb);
4075 break;
4076
4077 case HCI_OP_READ_BD_ADDR:
4078 hci_cc_read_bd_addr(hdev, skb);
4079 break;
4080
Marcel Holtmanna4790362020-04-03 21:44:04 +02004081 case HCI_OP_READ_LOCAL_PAIRING_OPTS:
4082 hci_cc_read_local_pairing_opts(hdev, skb);
4083 break;
4084
Johan Hedbergf332ec62013-03-15 17:07:11 -05004085 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
4086 hci_cc_read_page_scan_activity(hdev, skb);
4087 break;
4088
Johan Hedberg4a3ee762013-03-15 17:07:12 -05004089 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
4090 hci_cc_write_page_scan_activity(hdev, skb);
4091 break;
4092
Johan Hedbergf332ec62013-03-15 17:07:11 -05004093 case HCI_OP_READ_PAGE_SCAN_TYPE:
4094 hci_cc_read_page_scan_type(hdev, skb);
4095 break;
4096
Johan Hedberg4a3ee762013-03-15 17:07:12 -05004097 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
4098 hci_cc_write_page_scan_type(hdev, skb);
4099 break;
4100
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02004101 case HCI_OP_READ_DATA_BLOCK_SIZE:
4102 hci_cc_read_data_block_size(hdev, skb);
4103 break;
4104
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02004105 case HCI_OP_READ_FLOW_CONTROL_MODE:
4106 hci_cc_read_flow_control_mode(hdev, skb);
4107 break;
4108
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03004109 case HCI_OP_READ_LOCAL_AMP_INFO:
4110 hci_cc_read_local_amp_info(hdev, skb);
4111 break;
4112
Johan Hedberg33f35722014-06-28 17:54:06 +03004113 case HCI_OP_READ_CLOCK:
4114 hci_cc_read_clock(hdev, skb);
4115 break;
4116
Johan Hedbergd5859e22011-01-25 01:19:58 +02004117 case HCI_OP_READ_INQ_RSP_TX_POWER:
4118 hci_cc_read_inq_rsp_tx_power(hdev, skb);
4119 break;
4120
Alain Michaud00bce3f2020-03-05 16:14:59 +00004121 case HCI_OP_READ_DEF_ERR_DATA_REPORTING:
4122 hci_cc_read_def_err_data_reporting(hdev, skb);
4123 break;
4124
4125 case HCI_OP_WRITE_DEF_ERR_DATA_REPORTING:
4126 hci_cc_write_def_err_data_reporting(hdev, skb);
4127 break;
4128
Johan Hedberg980e1a52011-01-22 06:10:07 +02004129 case HCI_OP_PIN_CODE_REPLY:
4130 hci_cc_pin_code_reply(hdev, skb);
4131 break;
4132
4133 case HCI_OP_PIN_CODE_NEG_REPLY:
4134 hci_cc_pin_code_neg_reply(hdev, skb);
4135 break;
4136
Szymon Jancc35938b2011-03-22 13:12:21 +01004137 case HCI_OP_READ_LOCAL_OOB_DATA:
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08004138 hci_cc_read_local_oob_data(hdev, skb);
4139 break;
4140
4141 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
4142 hci_cc_read_local_oob_ext_data(hdev, skb);
Szymon Jancc35938b2011-03-22 13:12:21 +01004143 break;
4144
Ville Tervo6ed58ec2011-02-10 22:38:48 -03004145 case HCI_OP_LE_READ_BUFFER_SIZE:
4146 hci_cc_le_read_buffer_size(hdev, skb);
4147 break;
4148
Johan Hedberg60e77322013-01-22 14:01:59 +02004149 case HCI_OP_LE_READ_LOCAL_FEATURES:
4150 hci_cc_le_read_local_features(hdev, skb);
4151 break;
4152
Johan Hedberg8fa19092012-10-19 20:57:49 +03004153 case HCI_OP_LE_READ_ADV_TX_POWER:
4154 hci_cc_le_read_adv_tx_power(hdev, skb);
4155 break;
4156
Johan Hedberga5c29682011-02-19 12:05:57 -03004157 case HCI_OP_USER_CONFIRM_REPLY:
4158 hci_cc_user_confirm_reply(hdev, skb);
4159 break;
4160
4161 case HCI_OP_USER_CONFIRM_NEG_REPLY:
4162 hci_cc_user_confirm_neg_reply(hdev, skb);
4163 break;
4164
Brian Gix1143d452011-11-23 08:28:34 -08004165 case HCI_OP_USER_PASSKEY_REPLY:
4166 hci_cc_user_passkey_reply(hdev, skb);
4167 break;
4168
4169 case HCI_OP_USER_PASSKEY_NEG_REPLY:
4170 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02004171 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09004172
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08004173 case HCI_OP_LE_SET_RANDOM_ADDR:
4174 hci_cc_le_set_random_addr(hdev, skb);
4175 break;
4176
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01004177 case HCI_OP_LE_SET_ADV_ENABLE:
4178 hci_cc_le_set_adv_enable(hdev, skb);
4179 break;
4180
Marcel Holtmann533553f2014-03-21 12:18:10 -07004181 case HCI_OP_LE_SET_SCAN_PARAM:
4182 hci_cc_le_set_scan_param(hdev, skb);
4183 break;
4184
Andre Guedeseb9d91f2011-05-26 16:23:52 -03004185 case HCI_OP_LE_SET_SCAN_ENABLE:
4186 hci_cc_le_set_scan_enable(hdev, skb);
4187 break;
4188
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08004189 case HCI_OP_LE_READ_ACCEPT_LIST_SIZE:
4190 hci_cc_le_read_accept_list_size(hdev, skb);
Johan Hedbergcf1d0812013-01-22 14:02:00 +02004191 break;
4192
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08004193 case HCI_OP_LE_CLEAR_ACCEPT_LIST:
4194 hci_cc_le_clear_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08004195 break;
4196
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08004197 case HCI_OP_LE_ADD_TO_ACCEPT_LIST:
4198 hci_cc_le_add_to_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08004199 break;
4200
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08004201 case HCI_OP_LE_DEL_FROM_ACCEPT_LIST:
4202 hci_cc_le_del_from_accept_list(hdev, skb);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08004203 break;
4204
Johan Hedberg9b008c02013-01-22 14:02:01 +02004205 case HCI_OP_LE_READ_SUPPORTED_STATES:
4206 hci_cc_le_read_supported_states(hdev, skb);
4207 break;
4208
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01004209 case HCI_OP_LE_READ_DEF_DATA_LEN:
4210 hci_cc_le_read_def_data_len(hdev, skb);
4211 break;
4212
4213 case HCI_OP_LE_WRITE_DEF_DATA_LEN:
4214 hci_cc_le_write_def_data_len(hdev, skb);
4215 break;
4216
Ankit Navikb950aa82018-08-17 07:29:19 +05304217 case HCI_OP_LE_ADD_TO_RESOLV_LIST:
4218 hci_cc_le_add_to_resolv_list(hdev, skb);
4219 break;
4220
4221 case HCI_OP_LE_DEL_FROM_RESOLV_LIST:
4222 hci_cc_le_del_from_resolv_list(hdev, skb);
4223 break;
4224
Ankit Navik545f2592018-06-29 12:13:20 +05304225 case HCI_OP_LE_CLEAR_RESOLV_LIST:
4226 hci_cc_le_clear_resolv_list(hdev, skb);
4227 break;
4228
Ankit Navikcfdb0c22018-06-29 12:12:50 +05304229 case HCI_OP_LE_READ_RESOLV_LIST_SIZE:
4230 hci_cc_le_read_resolv_list_size(hdev, skb);
4231 break;
4232
Ankit Navikaa12af72018-08-07 13:16:35 +05304233 case HCI_OP_LE_SET_ADDR_RESOLV_ENABLE:
4234 hci_cc_le_set_addr_resolution_enable(hdev, skb);
4235 break;
4236
Marcel Holtmanna8e1bfa2014-12-20 16:28:40 +01004237 case HCI_OP_LE_READ_MAX_DATA_LEN:
4238 hci_cc_le_read_max_data_len(hdev, skb);
4239 break;
4240
Andre Guedesf9b49302011-06-30 19:20:53 -03004241 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
4242 hci_cc_write_le_host_supported(hdev, skb);
4243 break;
4244
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02004245 case HCI_OP_LE_SET_ADV_PARAM:
4246 hci_cc_set_adv_param(hdev, skb);
4247 break;
4248
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02004249 case HCI_OP_READ_RSSI:
4250 hci_cc_read_rssi(hdev, skb);
4251 break;
4252
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02004253 case HCI_OP_READ_TX_POWER:
4254 hci_cc_read_tx_power(hdev, skb);
4255 break;
4256
Marcel Holtmannc50b33c2015-01-31 15:07:52 -08004257 case HCI_OP_WRITE_SSP_DEBUG_MODE:
4258 hci_cc_write_ssp_debug_mode(hdev, skb);
4259 break;
4260
Jaganath Kanakkasserya2344b92018-07-06 17:05:28 +05304261 case HCI_OP_LE_SET_EXT_SCAN_PARAMS:
4262 hci_cc_le_set_ext_scan_param(hdev, skb);
4263 break;
4264
4265 case HCI_OP_LE_SET_EXT_SCAN_ENABLE:
4266 hci_cc_le_set_ext_scan_enable(hdev, skb);
4267 break;
4268
Jaganath Kanakkassery0314f282018-07-19 17:09:35 +05304269 case HCI_OP_LE_SET_DEFAULT_PHY:
4270 hci_cc_le_set_default_phy(hdev, skb);
4271 break;
4272
Jaganath Kanakkassery6b49bcb2018-07-19 17:09:40 +05304273 case HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS:
4274 hci_cc_le_read_num_adv_sets(hdev, skb);
4275 break;
4276
Jaganath Kanakkasseryde181e82018-07-19 17:09:41 +05304277 case HCI_OP_LE_SET_EXT_ADV_PARAMS:
4278 hci_cc_set_ext_adv_param(hdev, skb);
4279 break;
4280
4281 case HCI_OP_LE_SET_EXT_ADV_ENABLE:
4282 hci_cc_le_set_ext_adv_enable(hdev, skb);
4283 break;
4284
Jaganath Kanakkasserya73c0462018-07-19 17:09:45 +05304285 case HCI_OP_LE_SET_ADV_SET_RAND_ADDR:
4286 hci_cc_le_set_adv_set_random_addr(hdev, skb);
4287 break;
4288
Luiz Augusto von Dentzcba6b752021-10-27 16:58:40 -07004289 case HCI_OP_LE_REMOVE_ADV_SET:
4290 hci_cc_le_remove_adv_set(hdev, skb);
4291 break;
4292
4293 case HCI_OP_LE_CLEAR_ADV_SETS:
4294 hci_cc_le_clear_adv_sets(hdev, skb);
4295 break;
4296
Daniel Winkler7c395ea2020-12-03 12:12:51 -08004297 case HCI_OP_LE_READ_TRANSMIT_POWER:
4298 hci_cc_le_read_transmit_power(hdev, skb);
4299 break;
4300
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004301 default:
Johan Hedberge62144872015-04-02 13:41:08 +03004302 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004303 break;
4304 }
4305
Kiran Kecb71f252021-08-16 05:07:47 +05304306 handle_cmd_cnt_and_timer(hdev, ev->ncmd);
Johan Hedberg600b2152015-03-28 11:17:36 +02004307
Johan Hedberge62144872015-04-02 13:41:08 +03004308 hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
4309 req_complete_skb);
Johan Hedberg9238f362013-03-05 20:37:48 +02004310
João Paulo Rechi Vitaf80c5da2019-05-02 10:01:52 +08004311 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
4312 bt_dev_err(hdev,
4313 "unexpected event for opcode 0x%4.4x", *opcode);
4314 return;
4315 }
4316
Johan Hedberg600b2152015-03-28 11:17:36 +02004317 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
4318 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004319}
4320
Johan Hedberge62144872015-04-02 13:41:08 +03004321static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb,
4322 u16 *opcode, u8 *status,
4323 hci_req_complete_t *req_complete,
4324 hci_req_complete_skb_t *req_complete_skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004325{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004326 struct hci_ev_cmd_status *ev;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004327
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004328 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_CMD_STATUS, sizeof(*ev));
4329 if (!ev)
4330 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004331
Johan Hedberge62144872015-04-02 13:41:08 +03004332 *opcode = __le16_to_cpu(ev->opcode);
4333 *status = ev->status;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004334
Johan Hedberge62144872015-04-02 13:41:08 +03004335 switch (*opcode) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004336 case HCI_OP_INQUIRY:
4337 hci_cs_inquiry(hdev, ev->status);
4338 break;
4339
4340 case HCI_OP_CREATE_CONN:
4341 hci_cs_create_conn(hdev, ev->status);
4342 break;
4343
Kuba Pawlak9645c762014-11-06 19:36:53 +01004344 case HCI_OP_DISCONNECT:
4345 hci_cs_disconnect(hdev, ev->status);
4346 break;
4347
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004348 case HCI_OP_ADD_SCO:
4349 hci_cs_add_sco(hdev, ev->status);
4350 break;
4351
Marcel Holtmannf8558552008-07-14 20:13:49 +02004352 case HCI_OP_AUTH_REQUESTED:
4353 hci_cs_auth_requested(hdev, ev->status);
4354 break;
4355
4356 case HCI_OP_SET_CONN_ENCRYPT:
4357 hci_cs_set_conn_encrypt(hdev, ev->status);
4358 break;
4359
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004360 case HCI_OP_REMOTE_NAME_REQ:
4361 hci_cs_remote_name_req(hdev, ev->status);
4362 break;
4363
Marcel Holtmann769be972008-07-14 20:13:49 +02004364 case HCI_OP_READ_REMOTE_FEATURES:
4365 hci_cs_read_remote_features(hdev, ev->status);
4366 break;
4367
4368 case HCI_OP_READ_REMOTE_EXT_FEATURES:
4369 hci_cs_read_remote_ext_features(hdev, ev->status);
4370 break;
4371
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004372 case HCI_OP_SETUP_SYNC_CONN:
4373 hci_cs_setup_sync_conn(hdev, ev->status);
4374 break;
4375
Kiran Kb2af2642021-09-07 15:42:43 +05304376 case HCI_OP_ENHANCED_SETUP_SYNC_CONN:
4377 hci_cs_enhanced_setup_sync_conn(hdev, ev->status);
4378 break;
4379
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004380 case HCI_OP_SNIFF_MODE:
4381 hci_cs_sniff_mode(hdev, ev->status);
4382 break;
4383
4384 case HCI_OP_EXIT_SNIFF_MODE:
4385 hci_cs_exit_sniff_mode(hdev, ev->status);
4386 break;
4387
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01004388 case HCI_OP_SWITCH_ROLE:
4389 hci_cs_switch_role(hdev, ev->status);
4390 break;
4391
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02004392 case HCI_OP_LE_CREATE_CONN:
4393 hci_cs_le_create_conn(hdev, ev->status);
4394 break;
4395
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07004396 case HCI_OP_LE_READ_REMOTE_FEATURES:
4397 hci_cs_le_read_remote_features(hdev, ev->status);
4398 break;
4399
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02004400 case HCI_OP_LE_START_ENC:
4401 hci_cs_le_start_enc(hdev, ev->status);
4402 break;
4403
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02004404 case HCI_OP_LE_EXT_CREATE_CONN:
4405 hci_cs_le_ext_create_conn(hdev, ev->status);
4406 break;
4407
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004408 default:
Johan Hedberge62144872015-04-02 13:41:08 +03004409 BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004410 break;
4411 }
4412
Kiran Kecb71f252021-08-16 05:07:47 +05304413 handle_cmd_cnt_and_timer(hdev, ev->ncmd);
Johan Hedberg600b2152015-03-28 11:17:36 +02004414
Johan Hedberg444c6dd2015-04-02 13:41:07 +03004415 /* Indicate request completion if the command failed. Also, if
4416 * we're not waiting for a special event and we get a success
4417 * command status we should try to flag the request as completed
4418 * (since for this kind of commands there will not be a command
4419 * complete event).
4420 */
Johan Hedberg02350a72013-04-03 21:50:29 +03004421 if (ev->status ||
Marcel Holtmann242c0eb2015-10-25 22:45:53 +01004422 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->hci.req_event))
Johan Hedberge62144872015-04-02 13:41:08 +03004423 hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
4424 req_complete_skb);
Johan Hedberg9238f362013-03-05 20:37:48 +02004425
João Paulo Rechi Vitaf80c5da2019-05-02 10:01:52 +08004426 if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
4427 bt_dev_err(hdev,
4428 "unexpected event for opcode 0x%4.4x", *opcode);
4429 return;
4430 }
4431
Johan Hedberg600b2152015-03-28 11:17:36 +02004432 if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
4433 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004434}
4435
Marcel Holtmann24dfa342014-11-02 02:56:41 +01004436static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
4437{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004438 struct hci_ev_hardware_error *ev;
4439
4440 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_HARDWARE_ERROR, sizeof(*ev));
4441 if (!ev)
4442 return;
Marcel Holtmann24dfa342014-11-02 02:56:41 +01004443
Marcel Holtmannc7741d12015-01-28 11:09:55 -08004444 hdev->hw_error_code = ev->code;
4445
4446 queue_work(hdev->req_workqueue, &hdev->error_reset);
Marcel Holtmann24dfa342014-11-02 02:56:41 +01004447}
4448
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004449static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004450{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004451 struct hci_ev_role_change *ev;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004452 struct hci_conn *conn;
4453
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004454 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_ROLE_CHANGE, sizeof(*ev));
4455 if (!ev)
4456 return;
4457
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004458 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004459
4460 hci_dev_lock(hdev);
4461
4462 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4463 if (conn) {
Johan Hedberg40bef302014-07-16 11:42:27 +03004464 if (!ev->status)
4465 conn->role = ev->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004466
Johan Hedberg51a8efd2012-01-16 06:10:31 +02004467 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004468
4469 hci_role_switch_cfm(conn, ev->status, ev->role);
4470 }
4471
4472 hci_dev_unlock(hdev);
4473}
4474
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004475static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476{
Luiz Augusto von Dentzaadc3d22021-12-01 10:54:55 -08004477 struct hci_ev_num_comp_pkts *ev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 int i;
4479
Luiz Augusto von Dentzaadc3d22021-12-01 10:54:55 -08004480 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_NUM_COMP_PKTS, sizeof(*ev));
4481 if (!ev)
4482 return;
4483
4484 if (!hci_ev_skb_pull(hdev, skb, HCI_EV_NUM_COMP_PKTS,
4485 flex_array_size(ev, handles, ev->num)))
4486 return;
4487
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02004488 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004489 bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02004490 return;
4491 }
4492
Luiz Augusto von Dentzaadc3d22021-12-01 10:54:55 -08004493 BT_DBG("%s num %d", hdev->name, ev->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Luiz Augusto von Dentzaadc3d22021-12-01 10:54:55 -08004495 for (i = 0; i < ev->num; i++) {
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02004496 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 struct hci_conn *conn;
4498 __u16 handle, count;
4499
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02004500 handle = __le16_to_cpu(info->handle);
4501 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
4503 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02004504 if (!conn)
4505 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02004507 conn->sent -= count;
4508
4509 switch (conn->type) {
4510 case ACL_LINK:
4511 hdev->acl_cnt += count;
4512 if (hdev->acl_cnt > hdev->acl_pkts)
4513 hdev->acl_cnt = hdev->acl_pkts;
4514 break;
4515
4516 case LE_LINK:
4517 if (hdev->le_pkts) {
4518 hdev->le_cnt += count;
4519 if (hdev->le_cnt > hdev->le_pkts)
4520 hdev->le_cnt = hdev->le_pkts;
4521 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02004522 hdev->acl_cnt += count;
4523 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 hdev->acl_cnt = hdev->acl_pkts;
4525 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02004526 break;
4527
4528 case SCO_LINK:
4529 hdev->sco_cnt += count;
4530 if (hdev->sco_cnt > hdev->sco_pkts)
4531 hdev->sco_cnt = hdev->sco_pkts;
4532 break;
4533
4534 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004535 bt_dev_err(hdev, "unknown type %d conn %p",
4536 conn->type, conn);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02004537 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 }
4539 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004540
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02004541 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542}
4543
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03004544static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
4545 __u16 handle)
4546{
4547 struct hci_chan *chan;
4548
4549 switch (hdev->dev_type) {
Marcel Holtmannca8bee52016-07-05 14:30:14 +02004550 case HCI_PRIMARY:
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03004551 return hci_conn_hash_lookup_handle(hdev, handle);
4552 case HCI_AMP:
4553 chan = hci_chan_lookup_handle(hdev, handle);
4554 if (chan)
4555 return chan->conn;
4556 break;
4557 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004558 bt_dev_err(hdev, "unknown dev_type %d", hdev->dev_type);
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03004559 break;
4560 }
4561
4562 return NULL;
4563}
4564
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004565static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004566{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004567 struct hci_ev_num_comp_blocks *ev;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004568 int i;
4569
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004570 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_NUM_COMP_BLOCKS, sizeof(*ev));
4571 if (!ev)
4572 return;
4573
4574 if (!hci_ev_skb_pull(hdev, skb, HCI_EV_NUM_COMP_BLOCKS,
4575 flex_array_size(ev, handles, ev->num_hndl)))
4576 return;
4577
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004578 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004579 bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004580 return;
4581 }
4582
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004583 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004584 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004585
4586 for (i = 0; i < ev->num_hndl; i++) {
4587 struct hci_comp_blocks_info *info = &ev->handles[i];
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03004588 struct hci_conn *conn = NULL;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004589 __u16 handle, block_count;
4590
4591 handle = __le16_to_cpu(info->handle);
4592 block_count = __le16_to_cpu(info->blocks);
4593
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03004594 conn = __hci_conn_lookup_handle(hdev, handle);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004595 if (!conn)
4596 continue;
4597
4598 conn->sent -= block_count;
4599
4600 switch (conn->type) {
4601 case ACL_LINK:
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03004602 case AMP_LINK:
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004603 hdev->block_cnt += block_count;
4604 if (hdev->block_cnt > hdev->num_blocks)
4605 hdev->block_cnt = hdev->num_blocks;
4606 break;
4607
4608 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004609 bt_dev_err(hdev, "unknown type %d conn %p",
4610 conn->type, conn);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004611 break;
4612 }
4613 }
4614
4615 queue_work(hdev->workqueue, &hdev->tx_work);
4616}
4617
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004618static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004620 struct hci_ev_mode_change *ev;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004621 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004623 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_MODE_CHANGE, sizeof(*ev));
4624 if (!ev)
4625 return;
4626
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004627 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628
4629 hci_dev_lock(hdev);
4630
Marcel Holtmann04837f62006-07-03 10:02:33 +02004631 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4632 if (conn) {
4633 conn->mode = ev->mode;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004634
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03004635 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
4636 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02004637 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02004638 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004639 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02004640 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004641 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04004642
Johan Hedberg51a8efd2012-01-16 06:10:31 +02004643 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04004644 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004645 }
4646
4647 hci_dev_unlock(hdev);
4648}
4649
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004650static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004652 struct hci_ev_pin_code_req *ev;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004653 struct hci_conn *conn;
4654
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004655 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_PIN_CODE_REQ, sizeof(*ev));
4656 if (!ev)
4657 return;
4658
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004659 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004660
4661 hci_dev_lock(hdev);
4662
4663 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02004664 if (!conn)
4665 goto unlock;
4666
4667 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004668 hci_conn_hold(conn);
4669 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004670 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004671 }
4672
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004673 if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
Johan Hedberg2f407f02014-07-17 15:35:40 +03004674 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
Johan Hedberg03b555e2011-01-04 15:40:05 +02004675 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03004676 sizeof(ev->bdaddr), &ev->bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004677 } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02004678 u8 secure;
4679
4680 if (conn->pending_sec_level == BT_SECURITY_HIGH)
4681 secure = 1;
4682 else
4683 secure = 0;
4684
Johan Hedberg744cf192011-11-08 20:40:14 +02004685 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02004686 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02004687
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02004688unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004689 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690}
4691
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004692static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
4693{
4694 if (key_type == HCI_LK_CHANGED_COMBINATION)
4695 return;
4696
4697 conn->pin_length = pin_len;
4698 conn->key_type = key_type;
4699
4700 switch (key_type) {
4701 case HCI_LK_LOCAL_UNIT:
4702 case HCI_LK_REMOTE_UNIT:
4703 case HCI_LK_DEBUG_COMBINATION:
4704 return;
4705 case HCI_LK_COMBINATION:
4706 if (pin_len == 16)
4707 conn->pending_sec_level = BT_SECURITY_HIGH;
4708 else
4709 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4710 break;
4711 case HCI_LK_UNAUTH_COMBINATION_P192:
4712 case HCI_LK_UNAUTH_COMBINATION_P256:
4713 conn->pending_sec_level = BT_SECURITY_MEDIUM;
4714 break;
4715 case HCI_LK_AUTH_COMBINATION_P192:
4716 conn->pending_sec_level = BT_SECURITY_HIGH;
4717 break;
4718 case HCI_LK_AUTH_COMBINATION_P256:
4719 conn->pending_sec_level = BT_SECURITY_FIPS;
4720 break;
4721 }
4722}
4723
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004724static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004726 struct hci_ev_link_key_req *ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004727 struct hci_cp_link_key_reply cp;
4728 struct hci_conn *conn;
4729 struct link_key *key;
4730
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004731 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_LINK_KEY_REQ, sizeof(*ev));
4732 if (!ev)
4733 return;
4734
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004735 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004736
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004737 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004738 return;
4739
4740 hci_dev_lock(hdev);
4741
4742 key = hci_find_link_key(hdev, &ev->bdaddr);
4743 if (!key) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03004744 BT_DBG("%s link key not found for %pMR", hdev->name,
4745 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004746 goto not_found;
4747 }
4748
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03004749 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
4750 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004751
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004752 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004753 if (conn) {
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03004754 clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
4755
Marcel Holtmann66138ce2014-01-10 02:07:20 -08004756 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
4757 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03004758 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004759 BT_DBG("%s ignoring unauthenticated key", hdev->name);
4760 goto not_found;
4761 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004762
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004763 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Johan Hedbergf3fb0b52014-06-02 10:12:44 +03004764 (conn->pending_sec_level == BT_SECURITY_HIGH ||
4765 conn->pending_sec_level == BT_SECURITY_FIPS)) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03004766 BT_DBG("%s ignoring key unauthenticated for high security",
4767 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02004768 goto not_found;
4769 }
4770
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004771 conn_set_key(conn, key->type, key->pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004772 }
4773
4774 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03004775 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004776
4777 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
4778
4779 hci_dev_unlock(hdev);
4780
4781 return;
4782
4783not_found:
4784 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
4785 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786}
4787
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004788static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004790 struct hci_ev_link_key_notify *ev;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004791 struct hci_conn *conn;
Johan Hedberg7652ff62014-06-24 13:15:49 +03004792 struct link_key *key;
4793 bool persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004794 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004795
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004796 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_LINK_KEY_NOTIFY, sizeof(*ev));
4797 if (!ev)
4798 return;
4799
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004800 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004801
4802 hci_dev_lock(hdev);
4803
4804 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004805 if (!conn)
4806 goto unlock;
4807
4808 hci_conn_hold(conn);
4809 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
4810 hci_conn_drop(conn);
4811
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03004812 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004813 conn_set_key(conn, ev->key_type, conn->pin_length);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004814
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004815 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg7652ff62014-06-24 13:15:49 +03004816 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02004817
Johan Hedberg7652ff62014-06-24 13:15:49 +03004818 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
4819 ev->key_type, pin_len, &persistent);
4820 if (!key)
4821 goto unlock;
4822
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02004823 /* Update connection information since adding the key will have
4824 * fixed up the type in the case of changed combination keys.
4825 */
4826 if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
4827 conn_set_key(conn, key->type, key->pin_len);
4828
Johan Hedberg7652ff62014-06-24 13:15:49 +03004829 mgmt_new_link_key(hdev, key, persistent);
4830
Johan Hedberg6d5650c2014-06-24 13:15:51 +03004831 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
4832 * is set. If it's not set simply remove the key from the kernel
4833 * list (we've still notified user space about it but with
4834 * store_hint being 0).
4835 */
4836 if (key->type == HCI_LK_DEBUG_COMBINATION &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004837 !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
Johan Hedberg0378b592014-11-19 15:22:22 +02004838 list_del_rcu(&key->list);
4839 kfree_rcu(key, rcu);
Johan Hedberg82c13d42014-12-03 11:03:06 +02004840 goto unlock;
Johan Hedberg6d5650c2014-06-24 13:15:51 +03004841 }
Johan Hedberg7652ff62014-06-24 13:15:49 +03004842
Johan Hedberg82c13d42014-12-03 11:03:06 +02004843 if (persistent)
4844 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4845 else
4846 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
4847
Johan Hedberg7652ff62014-06-24 13:15:49 +03004848unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02004849 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850}
4851
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004852static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02004853{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004854 struct hci_ev_clock_offset *ev;
Marcel Holtmann04837f62006-07-03 10:02:33 +02004855 struct hci_conn *conn;
4856
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004857 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_CLOCK_OFFSET, sizeof(*ev));
4858 if (!ev)
4859 return;
4860
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004861 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02004862
4863 hci_dev_lock(hdev);
4864
4865 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 if (conn && !ev->status) {
4867 struct inquiry_entry *ie;
4868
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004869 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
4870 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871 ie->data.clock_offset = ev->clock_offset;
4872 ie->timestamp = jiffies;
4873 }
4874 }
4875
4876 hci_dev_unlock(hdev);
4877}
4878
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004879static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02004880{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004881 struct hci_ev_pkt_type_change *ev;
Marcel Holtmanna8746412008-07-14 20:13:46 +02004882 struct hci_conn *conn;
4883
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004884 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_PKT_TYPE_CHANGE, sizeof(*ev));
4885 if (!ev)
4886 return;
4887
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004888 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02004889
4890 hci_dev_lock(hdev);
4891
4892 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4893 if (conn && !ev->status)
4894 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
4895
4896 hci_dev_unlock(hdev);
4897}
4898
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004899static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004900{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004901 struct hci_ev_pscan_rep_mode *ev;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004902 struct inquiry_entry *ie;
4903
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08004904 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_PSCAN_REP_MODE, sizeof(*ev));
4905 if (!ev)
4906 return;
4907
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004908 BT_DBG("%s", hdev->name);
4909
4910 hci_dev_lock(hdev);
4911
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004912 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4913 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004914 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
4915 ie->timestamp = jiffies;
4916 }
4917
4918 hci_dev_unlock(hdev);
4919}
4920
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004921static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
4922 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004923{
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004924 union {
4925 struct hci_ev_inquiry_result_rssi *res1;
4926 struct hci_ev_inquiry_result_rssi_pscan *res2;
4927 } *ev;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004928 struct inquiry_data data;
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004929 int i;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004930
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004931 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_INQUIRY_RESULT_WITH_RSSI,
4932 sizeof(*ev));
4933 if (!ev)
4934 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004935
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004936 BT_DBG("%s num_rsp %d", hdev->name, ev->res1->num);
4937
4938 if (!ev->res1->num)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004939 return;
4940
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004941 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03004942 return;
4943
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004944 hci_dev_lock(hdev);
4945
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004946 if (skb->len == flex_array_size(ev, res2->info, ev->res2->num)) {
4947 struct inquiry_info_rssi_pscan *info;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004948
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004949 for (i = 0; i < ev->res2->num; i++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004950 u32 flags;
4951
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004952 info = &ev->res2->info[i];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004953 bacpy(&data.bdaddr, &info->bdaddr);
4954 data.pscan_rep_mode = info->pscan_rep_mode;
4955 data.pscan_period_mode = info->pscan_period_mode;
4956 data.pscan_mode = info->pscan_mode;
4957 memcpy(data.dev_class, info->dev_class, 3);
4958 data.clock_offset = info->clock_offset;
4959 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004960 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02004961
Marcel Holtmannaf589252014-07-01 14:11:20 +02004962 flags = hci_inquiry_cache_update(hdev, &data, false);
4963
Johan Hedberg48264f02011-11-09 13:58:58 +02004964 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004965 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004966 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004967 }
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004968 } else if (skb->len == flex_array_size(ev, res1->info, ev->res1->num)) {
4969 struct inquiry_info_rssi *info;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004970
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004971 for (i = 0; i < ev->res1->num; i++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02004972 u32 flags;
4973
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004974 info = &ev->res1->info[i];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004975 bacpy(&data.bdaddr, &info->bdaddr);
4976 data.pscan_rep_mode = info->pscan_rep_mode;
4977 data.pscan_period_mode = info->pscan_period_mode;
4978 data.pscan_mode = 0x00;
4979 memcpy(data.dev_class, info->dev_class, 3);
4980 data.clock_offset = info->clock_offset;
4981 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004982 data.ssp_mode = 0x00;
Marcel Holtmannaf589252014-07-01 14:11:20 +02004983
4984 flags = hci_inquiry_cache_update(hdev, &data, false);
4985
Johan Hedberg48264f02011-11-09 13:58:58 +02004986 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004987 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02004988 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004989 }
Luiz Augusto von Dentz8d08d322021-12-01 10:54:57 -08004990 } else {
4991 bt_dev_err(hdev, "Malformed HCI Event: 0x%2.2x",
4992 HCI_EV_INQUIRY_RESULT_WITH_RSSI);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004993 }
4994
4995 hci_dev_unlock(hdev);
4996}
4997
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004998static void hci_remote_ext_features_evt(struct hci_dev *hdev,
4999 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005000{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005001 struct hci_ev_remote_ext_features *ev;
Marcel Holtmann41a96212008-07-14 20:13:48 +02005002 struct hci_conn *conn;
5003
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005004 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_REMOTE_EXT_FEATURES,
5005 sizeof(*ev));
5006 if (!ev)
5007 return;
5008
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005009 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02005010
Marcel Holtmann41a96212008-07-14 20:13:48 +02005011 hci_dev_lock(hdev);
5012
5013 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02005014 if (!conn)
5015 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02005016
Johan Hedbergcad718e2013-04-17 15:00:51 +03005017 if (ev->page < HCI_MAX_PAGES)
5018 memcpy(conn->features[ev->page], ev->features, 8);
5019
Johan Hedbergccd556f2010-11-10 17:11:51 +02005020 if (!ev->status && ev->page == 0x01) {
5021 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02005022
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02005023 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
5024 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02005025 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02005026
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05305027 if (ev->features[0] & LMP_HOST_SSP) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02005028 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05305029 } else {
5030 /* It is mandatory by the Bluetooth specification that
5031 * Extended Inquiry Results are only used when Secure
5032 * Simple Pairing is enabled, but some devices violate
5033 * this.
5034 *
5035 * To make these devices work, the internal SSP
5036 * enabled flag needs to be cleared if the remote host
5037 * features do not indicate SSP support */
5038 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
5039 }
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -08005040
5041 if (ev->features[0] & LMP_HOST_SC)
5042 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02005043 }
5044
Johan Hedbergccd556f2010-11-10 17:11:51 +02005045 if (conn->state != BT_CONFIG)
5046 goto unlock;
5047
Johan Hedberg671267b2012-05-12 16:11:50 -03005048 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02005049 struct hci_cp_remote_name_req cp;
5050 memset(&cp, 0, sizeof(cp));
5051 bacpy(&cp.bdaddr, &conn->dst);
5052 cp.pscan_rep_mode = 0x02;
5053 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02005054 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07005055 mgmt_device_connected(hdev, conn, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02005056
Johan Hedberg127178d2010-11-18 22:22:29 +02005057 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02005058 conn->state = BT_CONNECTED;
Johan Hedberg539c4962015-02-18 14:53:57 +02005059 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02005060 hci_conn_drop(conn);
Johan Hedbergccd556f2010-11-10 17:11:51 +02005061 }
5062
5063unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02005064 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005065}
5066
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005067static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
5068 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005069{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005070 struct hci_ev_sync_conn_complete *ev;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02005071 struct hci_conn *conn;
5072
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005073 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_SYNC_CONN_COMPLETE, sizeof(*ev));
5074 if (!ev)
5075 return;
5076
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03005077 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02005078
5079 hci_dev_lock(hdev);
5080
5081 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02005082 if (!conn) {
5083 if (ev->link_type == ESCO_LINK)
5084 goto unlock;
5085
Kuba Pawlak618353b2015-08-28 13:05:22 +01005086 /* When the link type in the event indicates SCO connection
5087 * and lookup of the connection object fails, then check
5088 * if an eSCO connection object exists.
5089 *
5090 * The core limits the synchronous connections to either
5091 * SCO or eSCO. The eSCO connection is preferred and tried
5092 * to be setup first and until successfully established,
5093 * the link type will be hinted as eSCO.
5094 */
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02005095 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
5096 if (!conn)
5097 goto unlock;
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02005098 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02005099
Marcel Holtmann732547f2009-04-19 19:14:14 +02005100 switch (ev->status) {
5101 case 0x00:
Desmond Cheong Zhi Xi92fe24a2021-07-28 15:51:04 +08005102 /* The synchronous connection complete event should only be
5103 * sent once per new connection. Receiving a successful
5104 * complete event when the connection status is already
5105 * BT_CONNECTED means that the device is misbehaving and sent
5106 * multiple complete event packets for the same new connection.
5107 *
5108 * Registering the device more than once can corrupt kernel
5109 * memory, hence upon detecting this invalid event, we report
5110 * an error and ignore the packet.
5111 */
5112 if (conn->state == BT_CONNECTED) {
5113 bt_dev_err(hdev, "Ignoring connect complete event for existing connection");
5114 goto unlock;
5115 }
5116
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02005117 conn->handle = __le16_to_cpu(ev->handle);
5118 conn->state = BT_CONNECTED;
Kuba Pawlak618353b2015-08-28 13:05:22 +01005119 conn->type = ev->link_type;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02005120
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01005121 hci_debugfs_create_conn(conn);
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02005122 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02005123 break;
5124
Nick Pelly81218d22014-06-30 11:25:01 +05305125 case 0x10: /* Connection Accept Timeout */
Frédéric Dalleau1a4c9582013-08-19 14:24:02 +02005126 case 0x0d: /* Connection Rejected due to Limited Resources */
Stephen Coe705e5712010-02-16 11:29:44 -05005127 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02005128 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08005129 case 0x1a: /* Unsupported Remote Feature */
Hsin-Yu Chao56b54532020-05-15 17:27:04 +08005130 case 0x1e: /* Invalid LMP Parameters */
Marcel Holtmann732547f2009-04-19 19:14:14 +02005131 case 0x1f: /* Unspecified error */
Andrew Earl27539bc2014-03-10 10:31:04 +00005132 case 0x20: /* Unsupported LMP Parameter value */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02005133 if (conn->out) {
Marcel Holtmann732547f2009-04-19 19:14:14 +02005134 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
5135 (hdev->esco_type & EDR_ESCO_MASK);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02005136 if (hci_setup_sync(conn, conn->link->handle))
5137 goto unlock;
Marcel Holtmann732547f2009-04-19 19:14:14 +02005138 }
Gustavo A. R. Silva19186c72020-07-08 15:18:23 -05005139 fallthrough;
Marcel Holtmann732547f2009-04-19 19:14:14 +02005140
5141 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02005142 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02005143 break;
5144 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02005145
Sathish Narsimman1f8330e2020-04-03 21:43:58 +02005146 bt_dev_dbg(hdev, "SCO connected with air mode: %02x", ev->air_mode);
Chethan T Nf4f9fa02021-09-07 15:42:48 +05305147 /* Notify only in case of SCO over HCI transport data path which
5148 * is zero and non-zero value shall be non-HCI transport data path
5149 */
Jackie Liua27c5192021-11-16 09:17:17 +08005150 if (conn->codec.data_path == 0 && hdev->notify) {
5151 switch (ev->air_mode) {
5152 case 0x02:
5153 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_CVSD);
5154 break;
5155 case 0x03:
5156 hdev->notify(hdev, HCI_NOTIFY_ENABLE_SCO_TRANSP);
5157 break;
5158 }
Chethan T Nf4f9fa02021-09-07 15:42:48 +05305159 }
5160
Johan Hedberg539c4962015-02-18 14:53:57 +02005161 hci_connect_cfm(conn, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02005162 if (ev->status)
5163 hci_conn_del(conn);
5164
5165unlock:
5166 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005167}
5168
Marcel Holtmannefdcf8e2013-10-15 10:31:12 -07005169static inline size_t eir_get_length(u8 *eir, size_t eir_len)
5170{
5171 size_t parsed = 0;
5172
5173 while (parsed < eir_len) {
5174 u8 field_len = eir[0];
5175
5176 if (field_len == 0)
5177 return parsed;
5178
5179 parsed += field_len + 1;
5180 eir += field_len + 1;
5181 }
5182
5183 return eir_len;
5184}
5185
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005186static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
5187 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005188{
5189 struct inquiry_data data;
5190 struct extended_inquiry_info *info = (void *) (skb->data + 1);
5191 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05305192 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005193
5194 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
5195
Peilin Ye51c19bf2020-07-10 12:09:15 -04005196 if (!num_rsp || skb->len < num_rsp * sizeof(*info) + 1)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005197 return;
5198
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005199 if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
Andre Guedes1519cc12012-03-21 00:03:38 -03005200 return;
5201
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005202 hci_dev_lock(hdev);
5203
Johan Hedberge17acd42011-03-30 23:57:16 +03005204 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02005205 u32 flags;
5206 bool name_known;
Johan Hedberg561aafb2012-01-04 13:31:59 +02005207
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005208 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01005209 data.pscan_rep_mode = info->pscan_rep_mode;
5210 data.pscan_period_mode = info->pscan_period_mode;
5211 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005212 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01005213 data.clock_offset = info->clock_offset;
5214 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02005215 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02005216
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005217 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg0d3b7f62016-01-05 13:19:31 +02005218 name_known = eir_get_data(info->data,
5219 sizeof(info->data),
5220 EIR_NAME_COMPLETE, NULL);
Johan Hedberg561aafb2012-01-04 13:31:59 +02005221 else
5222 name_known = true;
5223
Marcel Holtmannaf589252014-07-01 14:11:20 +02005224 flags = hci_inquiry_cache_update(hdev, &data, name_known);
5225
Vishal Agarwal9d939d92012-04-26 19:19:56 +05305226 eir_len = eir_get_length(info->data, sizeof(info->data));
Marcel Holtmannaf589252014-07-01 14:11:20 +02005227
Johan Hedberg48264f02011-11-09 13:58:58 +02005228 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02005229 info->dev_class, info->rssi,
5230 flags, info->data, eir_len, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005231 }
5232
5233 hci_dev_unlock(hdev);
5234}
5235
Johan Hedberg1c2e0042012-06-08 23:31:13 +08005236static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
5237 struct sk_buff *skb)
5238{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005239 struct hci_ev_key_refresh_complete *ev;
Johan Hedberg1c2e0042012-06-08 23:31:13 +08005240 struct hci_conn *conn;
5241
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005242 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_KEY_REFRESH_COMPLETE,
5243 sizeof(*ev));
5244 if (!ev)
5245 return;
5246
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03005247 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08005248 __le16_to_cpu(ev->handle));
5249
5250 hci_dev_lock(hdev);
5251
5252 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
5253 if (!conn)
5254 goto unlock;
5255
Johan Hedberg9eb1fbf2014-04-11 12:02:31 -07005256 /* For BR/EDR the necessary steps are taken through the
5257 * auth_complete event.
5258 */
5259 if (conn->type != LE_LINK)
5260 goto unlock;
5261
Johan Hedberg1c2e0042012-06-08 23:31:13 +08005262 if (!ev->status)
5263 conn->sec_level = conn->pending_sec_level;
5264
5265 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
5266
5267 if (ev->status && conn->state == BT_CONNECTED) {
Andre Guedesbed71742013-01-30 11:50:56 -03005268 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
David Herrmann76a68ba2013-04-06 20:28:37 +02005269 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08005270 goto unlock;
5271 }
5272
5273 if (conn->state == BT_CONFIG) {
5274 if (!ev->status)
5275 conn->state = BT_CONNECTED;
5276
Johan Hedberg539c4962015-02-18 14:53:57 +02005277 hci_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02005278 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08005279 } else {
5280 hci_auth_cfm(conn, ev->status);
5281
5282 hci_conn_hold(conn);
5283 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02005284 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08005285 }
5286
5287unlock:
5288 hci_dev_unlock(hdev);
5289}
5290
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005291static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02005292{
Johan Hedberg17fa4b92011-01-25 13:28:33 +02005293 /* If remote requests no-bonding follow that lead */
Mikel Astizacabae92013-06-28 10:56:28 +02005294 if (conn->remote_auth == HCI_AT_NO_BONDING ||
5295 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02005296 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02005297
Mikel Astizb7f94c82014-04-08 14:21:31 +02005298 /* If both remote and local have enough IO capabilities, require
5299 * MITM protection
5300 */
5301 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
5302 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
5303 return conn->remote_auth | 0x01;
5304
Timo Mueller7e741702014-04-08 14:21:33 +02005305 /* No MITM protection possible so ignore remote requirement */
5306 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02005307}
5308
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08005309static u8 bredr_oob_data_present(struct hci_conn *conn)
5310{
5311 struct hci_dev *hdev = conn->hdev;
5312 struct oob_data *data;
5313
5314 data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
5315 if (!data)
5316 return 0x00;
5317
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07005318 if (bredr_sc_enabled(hdev)) {
5319 /* When Secure Connections is enabled, then just
5320 * return the present value stored with the OOB
5321 * data. The stored value contains the right present
5322 * information. However it can only be trusted when
5323 * not in Secure Connection Only mode.
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08005324 */
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07005325 if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
5326 return data->present;
5327
5328 /* When Secure Connections Only mode is enabled, then
5329 * the P-256 values are required. If they are not
5330 * available, then do not declare that OOB data is
5331 * present.
5332 */
5333 if (!memcmp(data->rand256, ZERO_KEY, 16) ||
5334 !memcmp(data->hash256, ZERO_KEY, 16))
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08005335 return 0x00;
5336
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07005337 return 0x02;
Marcel Holtmann659c7fb2015-01-30 23:20:56 -08005338 }
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08005339
Marcel Holtmann455c2ff2015-03-15 16:42:53 -07005340 /* When Secure Connections is not enabled or actually
5341 * not supported by the hardware, then check that if
5342 * P-192 data values are present.
5343 */
5344 if (!memcmp(data->rand192, ZERO_KEY, 16) ||
5345 !memcmp(data->hash192, ZERO_KEY, 16))
5346 return 0x00;
5347
5348 return 0x01;
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08005349}
5350
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005351static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02005352{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005353 struct hci_ev_io_capa_request *ev;
Marcel Holtmann04936842008-07-14 20:13:48 +02005354 struct hci_conn *conn;
5355
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005356 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_IO_CAPA_REQUEST, sizeof(*ev));
5357 if (!ev)
5358 return;
5359
Marcel Holtmann04936842008-07-14 20:13:48 +02005360 BT_DBG("%s", hdev->name);
5361
5362 hci_dev_lock(hdev);
5363
5364 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02005365 if (!conn)
5366 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02005367
Johan Hedberg03b555e2011-01-04 15:40:05 +02005368 hci_conn_hold(conn);
5369
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005370 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg03b555e2011-01-04 15:40:05 +02005371 goto unlock;
5372
Johan Hedberg2f407f02014-07-17 15:35:40 +03005373 /* Allow pairing if we're pairable, the initiators of the
5374 * pairing or if the remote is not requesting bonding.
5375 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005376 if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
Johan Hedberg2f407f02014-07-17 15:35:40 +03005377 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03005378 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02005379 struct hci_cp_io_capability_reply cp;
5380
5381 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05305382 /* Change the IO capability from KeyboardDisplay
5383 * to DisplayYesNo as it is not supported by BT spec. */
5384 cp.capability = (conn->io_capability == 0x04) ?
Mikel Astiza7676312013-06-28 10:56:29 +02005385 HCI_IO_DISPLAY_YESNO : conn->io_capability;
Mikel Astizb7f94c82014-04-08 14:21:31 +02005386
5387 /* If we are initiators, there is no remote information yet */
5388 if (conn->remote_auth == 0xff) {
Mikel Astizb16c6602014-04-08 14:21:34 +02005389 /* Request MITM protection if our IO caps allow it
Johan Hedberg4ad51a72014-06-09 14:41:25 +03005390 * except for the no-bonding case.
Mikel Astizb16c6602014-04-08 14:21:34 +02005391 */
Mikel Astiz6fd6b912014-04-08 14:21:32 +02005392 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedberg9f743d72014-07-17 11:56:33 +03005393 conn->auth_type != HCI_AT_NO_BONDING)
Johan Hedberg6c538232014-07-11 15:32:23 +03005394 conn->auth_type |= 0x01;
Mikel Astizb7f94c82014-04-08 14:21:31 +02005395 } else {
5396 conn->auth_type = hci_get_auth_req(conn);
Mikel Astizb7f94c82014-04-08 14:21:31 +02005397 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02005398
Johan Hedberg82c295b2014-07-30 09:22:24 +03005399 /* If we're not bondable, force one of the non-bondable
5400 * authentication requirement values.
5401 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005402 if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedberg82c295b2014-07-30 09:22:24 +03005403 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
5404
5405 cp.authentication = conn->auth_type;
Marcel Holtmanna83ed81e2015-01-27 16:04:32 -08005406 cp.oob_data = bredr_oob_data_present(conn);
Szymon Jancce85ee12011-03-22 13:12:23 +01005407
Johan Hedberg17fa4b92011-01-25 13:28:33 +02005408 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03005409 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02005410 } else {
5411 struct hci_cp_io_capability_neg_reply cp;
5412
5413 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02005414 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02005415
5416 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03005417 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02005418 }
5419
5420unlock:
5421 hci_dev_unlock(hdev);
5422}
5423
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005424static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02005425{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005426 struct hci_ev_io_capa_reply *ev;
Johan Hedberg03b555e2011-01-04 15:40:05 +02005427 struct hci_conn *conn;
5428
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005429 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_IO_CAPA_REPLY, sizeof(*ev));
5430 if (!ev)
5431 return;
5432
Johan Hedberg03b555e2011-01-04 15:40:05 +02005433 BT_DBG("%s", hdev->name);
5434
5435 hci_dev_lock(hdev);
5436
5437 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5438 if (!conn)
5439 goto unlock;
5440
Johan Hedberg03b555e2011-01-04 15:40:05 +02005441 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02005442 conn->remote_auth = ev->authentication;
5443
5444unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02005445 hci_dev_unlock(hdev);
5446}
5447
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005448static void hci_user_confirm_request_evt(struct hci_dev *hdev,
5449 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03005450{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005451 struct hci_ev_user_confirm_req *ev;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07005452 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07005453 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03005454
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005455 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_USER_CONFIRM_REQUEST,
5456 sizeof(*ev));
5457 if (!ev)
5458 return;
5459
Johan Hedberga5c29682011-02-19 12:05:57 -03005460 BT_DBG("%s", hdev->name);
5461
5462 hci_dev_lock(hdev);
5463
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005464 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg7a828902011-04-28 11:28:53 -07005465 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03005466
Johan Hedberg7a828902011-04-28 11:28:53 -07005467 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5468 if (!conn)
5469 goto unlock;
5470
5471 loc_mitm = (conn->auth_type & 0x01);
5472 rem_mitm = (conn->remote_auth & 0x01);
5473
5474 /* If we require MITM but the remote device can't provide that
Johan Hedberg6c538232014-07-11 15:32:23 +03005475 * (it has NoInputNoOutput) then reject the confirmation
5476 * request. We check the security level here since it doesn't
5477 * necessarily match conn->auth_type.
Mikel Astiz6fd6b912014-04-08 14:21:32 +02005478 */
Johan Hedberg6c538232014-07-11 15:32:23 +03005479 if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
5480 conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
Johan Hedberg7a828902011-04-28 11:28:53 -07005481 BT_DBG("Rejecting request: remote device can't provide MITM");
5482 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03005483 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07005484 goto unlock;
5485 }
5486
5487 /* If no side requires MITM protection; auto-accept */
Mikel Astiza7676312013-06-28 10:56:29 +02005488 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
5489 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07005490
5491 /* If we're not the initiators request authorization to
5492 * proceed from user space (mgmt_user_confirm with
Johan Hedbergba15a582014-06-09 13:58:14 +03005493 * confirm_hint set to 1). The exception is if neither
Johan Hedberg02f3e252014-07-16 15:09:13 +03005494 * side had MITM or if the local IO capability is
5495 * NoInputNoOutput, in which case we do auto-accept
Johan Hedbergba15a582014-06-09 13:58:14 +03005496 */
5497 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
Johan Hedberg02f3e252014-07-16 15:09:13 +03005498 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedbergba15a582014-06-09 13:58:14 +03005499 (loc_mitm || rem_mitm)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07005500 BT_DBG("Confirming auto-accept as acceptor");
5501 confirm_hint = 1;
5502 goto confirm;
5503 }
5504
Howard Chungcee5f202020-02-14 19:16:41 +08005505 /* If there already exists link key in local host, leave the
5506 * decision to user space since the remote device could be
5507 * legitimate or malicious.
5508 */
5509 if (hci_find_link_key(hdev, &ev->bdaddr)) {
5510 bt_dev_dbg(hdev, "Local host already has link key");
5511 confirm_hint = 1;
5512 goto confirm;
5513 }
5514
Johan Hedberg9f616562011-04-28 11:28:54 -07005515 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03005516 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07005517
5518 if (hdev->auto_accept_delay > 0) {
5519 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03005520 queue_delayed_work(conn->hdev->workqueue,
5521 &conn->auto_accept_work, delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07005522 goto unlock;
5523 }
5524
Johan Hedberg7a828902011-04-28 11:28:53 -07005525 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03005526 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07005527 goto unlock;
5528 }
5529
Johan Hedberg55bc1a32011-04-28 11:28:56 -07005530confirm:
Johan Hedberg39adbff2014-03-20 08:18:14 +02005531 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
5532 le32_to_cpu(ev->passkey), confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07005533
5534unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03005535 hci_dev_unlock(hdev);
5536}
5537
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005538static void hci_user_passkey_request_evt(struct hci_dev *hdev,
5539 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08005540{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005541 struct hci_ev_user_passkey_req *ev;
5542
5543 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_USER_PASSKEY_REQUEST,
5544 sizeof(*ev));
5545 if (!ev)
5546 return;
Brian Gix1143d452011-11-23 08:28:34 -08005547
5548 BT_DBG("%s", hdev->name);
5549
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005550 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg272d90d2012-02-09 15:26:12 +02005551 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08005552}
5553
Johan Hedberg92a25252012-09-06 18:39:26 +03005554static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
5555 struct sk_buff *skb)
5556{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005557 struct hci_ev_user_passkey_notify *ev;
Johan Hedberg92a25252012-09-06 18:39:26 +03005558 struct hci_conn *conn;
5559
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005560 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_USER_PASSKEY_NOTIFY,
5561 sizeof(*ev));
5562 if (!ev)
5563 return;
5564
Johan Hedberg92a25252012-09-06 18:39:26 +03005565 BT_DBG("%s", hdev->name);
5566
5567 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5568 if (!conn)
5569 return;
5570
5571 conn->passkey_notify = __le32_to_cpu(ev->passkey);
5572 conn->passkey_entered = 0;
5573
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005574 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg92a25252012-09-06 18:39:26 +03005575 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
5576 conn->dst_type, conn->passkey_notify,
5577 conn->passkey_entered);
5578}
5579
5580static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
5581{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005582 struct hci_ev_keypress_notify *ev;
Johan Hedberg92a25252012-09-06 18:39:26 +03005583 struct hci_conn *conn;
5584
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005585 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_KEYPRESS_NOTIFY, sizeof(*ev));
5586 if (!ev)
5587 return;
5588
Johan Hedberg92a25252012-09-06 18:39:26 +03005589 BT_DBG("%s", hdev->name);
5590
5591 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5592 if (!conn)
5593 return;
5594
5595 switch (ev->type) {
5596 case HCI_KEYPRESS_STARTED:
5597 conn->passkey_entered = 0;
5598 return;
5599
5600 case HCI_KEYPRESS_ENTERED:
5601 conn->passkey_entered++;
5602 break;
5603
5604 case HCI_KEYPRESS_ERASED:
5605 conn->passkey_entered--;
5606 break;
5607
5608 case HCI_KEYPRESS_CLEARED:
5609 conn->passkey_entered = 0;
5610 break;
5611
5612 case HCI_KEYPRESS_COMPLETED:
5613 return;
5614 }
5615
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005616 if (hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg92a25252012-09-06 18:39:26 +03005617 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
5618 conn->dst_type, conn->passkey_notify,
5619 conn->passkey_entered);
5620}
5621
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005622static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
5623 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02005624{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005625 struct hci_ev_simple_pair_complete *ev;
Marcel Holtmann04936842008-07-14 20:13:48 +02005626 struct hci_conn *conn;
5627
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005628 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_SIMPLE_PAIR_COMPLETE,
5629 sizeof(*ev));
5630 if (!ev)
5631 return;
5632
Marcel Holtmann04936842008-07-14 20:13:48 +02005633 BT_DBG("%s", hdev->name);
5634
5635 hci_dev_lock(hdev);
5636
5637 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03005638 if (!conn)
5639 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02005640
Johan Hedbergc1d4fa72014-07-17 15:14:50 +03005641 /* Reset the authentication requirement to unknown */
5642 conn->remote_auth = 0xff;
5643
Johan Hedberg2a611692011-02-19 12:06:00 -03005644 /* To avoid duplicate auth_failed events to user space we check
5645 * the HCI_CONN_AUTH_PEND flag which will be set if we
5646 * initiated the authentication. A traditional auth_complete
5647 * event gets always produced as initiator and is also mapped to
5648 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02005649 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedberge1e930f2014-09-08 17:09:49 -07005650 mgmt_auth_failed(conn, ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03005651
David Herrmann76a68ba2013-04-06 20:28:37 +02005652 hci_conn_drop(conn);
Johan Hedberg2a611692011-02-19 12:06:00 -03005653
5654unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02005655 hci_dev_unlock(hdev);
5656}
5657
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005658static void hci_remote_host_features_evt(struct hci_dev *hdev,
5659 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02005660{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005661 struct hci_ev_remote_host_features *ev;
Marcel Holtmann41a96212008-07-14 20:13:48 +02005662 struct inquiry_entry *ie;
Johan Hedbergcad718e2013-04-17 15:00:51 +03005663 struct hci_conn *conn;
Marcel Holtmann41a96212008-07-14 20:13:48 +02005664
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005665 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_REMOTE_HOST_FEATURES,
5666 sizeof(*ev));
5667 if (!ev)
5668 return;
5669
Marcel Holtmann41a96212008-07-14 20:13:48 +02005670 BT_DBG("%s", hdev->name);
5671
5672 hci_dev_lock(hdev);
5673
Johan Hedbergcad718e2013-04-17 15:00:51 +03005674 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
5675 if (conn)
5676 memcpy(conn->features[1], ev->features, 8);
5677
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02005678 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
5679 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02005680 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02005681
5682 hci_dev_unlock(hdev);
5683}
5684
Gustavo Padovan6039aa72012-05-23 04:04:18 -03005685static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
5686 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01005687{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005688 struct hci_ev_remote_oob_data_request *ev;
Szymon Janc2763eda2011-03-22 13:12:22 +01005689 struct oob_data *data;
5690
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005691 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_REMOTE_OOB_DATA_REQUEST,
5692 sizeof(*ev));
5693 if (!ev)
5694 return;
5695
Szymon Janc2763eda2011-03-22 13:12:22 +01005696 BT_DBG("%s", hdev->name);
5697
5698 hci_dev_lock(hdev);
5699
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005700 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Szymon Jance1ba1f12011-04-06 13:01:59 +02005701 goto unlock;
5702
Johan Hedberg6928a922014-10-26 20:46:09 +01005703 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
Marcel Holtmann6665d052015-01-27 16:04:31 -08005704 if (!data) {
Szymon Janc2763eda2011-03-22 13:12:22 +01005705 struct hci_cp_remote_oob_data_neg_reply cp;
5706
5707 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08005708 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
5709 sizeof(cp), &cp);
Marcel Holtmann6665d052015-01-27 16:04:31 -08005710 goto unlock;
5711 }
5712
5713 if (bredr_sc_enabled(hdev)) {
5714 struct hci_cp_remote_oob_ext_data_reply cp;
5715
5716 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005717 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmann6665d052015-01-27 16:04:31 -08005718 memset(cp.hash192, 0, sizeof(cp.hash192));
5719 memset(cp.rand192, 0, sizeof(cp.rand192));
5720 } else {
5721 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
5722 memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
5723 }
5724 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
5725 memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
5726
5727 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
5728 sizeof(cp), &cp);
5729 } else {
5730 struct hci_cp_remote_oob_data_reply cp;
5731
5732 bacpy(&cp.bdaddr, &ev->bdaddr);
5733 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
5734 memcpy(cp.rand, data->rand192, sizeof(cp.rand));
5735
5736 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
5737 sizeof(cp), &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01005738 }
5739
Szymon Jance1ba1f12011-04-06 13:01:59 +02005740unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01005741 hci_dev_unlock(hdev);
5742}
5743
Arron Wanga77a6a12015-07-24 17:13:15 +08005744#if IS_ENABLED(CONFIG_BT_HS)
5745static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
5746{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005747 struct hci_ev_channel_selected *ev;
Arron Wanga77a6a12015-07-24 17:13:15 +08005748 struct hci_conn *hcon;
5749
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005750 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_CHANNEL_SELECTED, sizeof(*ev));
5751 if (!ev)
5752 return;
Arron Wanga77a6a12015-07-24 17:13:15 +08005753
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005754 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
Arron Wanga77a6a12015-07-24 17:13:15 +08005755
5756 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5757 if (!hcon)
5758 return;
5759
5760 amp_read_loc_assoc_final_data(hdev, hcon);
5761}
5762
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005763static void hci_phy_link_complete_evt(struct hci_dev *hdev,
5764 struct sk_buff *skb)
5765{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005766 struct hci_ev_phy_link_complete *ev;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005767 struct hci_conn *hcon, *bredr_hcon;
5768
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005769 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_PHY_LINK_COMPLETE, sizeof(*ev));
5770 if (!ev)
5771 return;
5772
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005773 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
5774 ev->status);
5775
5776 hci_dev_lock(hdev);
5777
5778 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005779 if (!hcon)
5780 goto unlock;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005781
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005782 if (!hcon->amp_mgr)
5783 goto unlock;
Anmol Karn6dfccd12020-09-30 19:48:13 +05305784
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005785 if (ev->status) {
5786 hci_conn_del(hcon);
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005787 goto unlock;
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005788 }
5789
5790 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
5791
5792 hcon->state = BT_CONNECTED;
5793 bacpy(&hcon->dst, &bredr_hcon->dst);
5794
5795 hci_conn_hold(hcon);
5796 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02005797 hci_conn_drop(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005798
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01005799 hci_debugfs_create_conn(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005800 hci_conn_add_sysfs(hcon);
5801
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +02005802 amp_physical_cfm(bredr_hcon, hcon);
5803
Sergey Shtylyov3ae1dc72020-10-07 18:54:15 +03005804unlock:
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005805 hci_dev_unlock(hdev);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005806}
5807
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005808static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
5809{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005810 struct hci_ev_logical_link_complete *ev;
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005811 struct hci_conn *hcon;
5812 struct hci_chan *hchan;
5813 struct amp_mgr *mgr;
5814
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005815 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_LOGICAL_LINK_COMPLETE,
5816 sizeof(*ev));
5817 if (!ev)
5818 return;
5819
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005820 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
5821 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
5822 ev->status);
5823
5824 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5825 if (!hcon)
5826 return;
5827
5828 /* Create AMP hchan */
5829 hchan = hci_chan_create(hcon);
5830 if (!hchan)
5831 return;
5832
5833 hchan->handle = le16_to_cpu(ev->handle);
Archie Pusaka5c4c8c92021-03-22 14:03:11 +08005834 hchan->amp = true;
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005835
5836 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
5837
5838 mgr = hcon->amp_mgr;
5839 if (mgr && mgr->bredr_chan) {
5840 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
5841
5842 l2cap_chan_lock(bredr_chan);
5843
5844 bredr_chan->conn->mtu = hdev->block_mtu;
5845 l2cap_logical_cfm(bredr_chan, hchan, 0);
5846 hci_conn_hold(hcon);
5847
5848 l2cap_chan_unlock(bredr_chan);
5849 }
5850}
5851
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005852static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
5853 struct sk_buff *skb)
5854{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005855 struct hci_ev_disconn_logical_link_complete *ev;
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005856 struct hci_chan *hchan;
5857
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005858 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE,
5859 sizeof(*ev));
5860 if (!ev)
5861 return;
5862
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005863 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
5864 le16_to_cpu(ev->handle), ev->status);
5865
5866 if (ev->status)
5867 return;
5868
5869 hci_dev_lock(hdev);
5870
5871 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
Archie Pusaka5c4c8c92021-03-22 14:03:11 +08005872 if (!hchan || !hchan->amp)
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005873 goto unlock;
5874
5875 amp_destroy_logical_link(hchan, ev->reason);
5876
5877unlock:
5878 hci_dev_unlock(hdev);
5879}
5880
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005881static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
5882 struct sk_buff *skb)
5883{
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005884 struct hci_ev_disconn_phy_link_complete *ev;
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005885 struct hci_conn *hcon;
5886
Luiz Augusto von Dentzae61a102021-12-01 10:54:53 -08005887 ev = hci_ev_skb_pull(hdev, skb, HCI_EV_DISCONN_PHY_LINK_COMPLETE,
5888 sizeof(*ev));
5889 if (!ev)
5890 return;
5891
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005892 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
5893
5894 if (ev->status)
5895 return;
5896
5897 hci_dev_lock(hdev);
5898
5899 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
5900 if (hcon) {
5901 hcon->state = BT_CLOSED;
5902 hci_conn_del(hcon);
5903 }
5904
5905 hci_dev_unlock(hdev);
5906}
Arron Wanga77a6a12015-07-24 17:13:15 +08005907#endif
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005908
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005909static void le_conn_update_addr(struct hci_conn *conn, bdaddr_t *bdaddr,
5910 u8 bdaddr_type, bdaddr_t *local_rpa)
5911{
5912 if (conn->out) {
5913 conn->dst_type = bdaddr_type;
5914 conn->resp_addr_type = bdaddr_type;
5915 bacpy(&conn->resp_addr, bdaddr);
5916
5917 /* Check if the controller has set a Local RPA then it must be
5918 * used instead or hdev->rpa.
5919 */
5920 if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5921 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5922 bacpy(&conn->init_addr, local_rpa);
5923 } else if (hci_dev_test_flag(conn->hdev, HCI_PRIVACY)) {
5924 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
5925 bacpy(&conn->init_addr, &conn->hdev->rpa);
5926 } else {
5927 hci_copy_identity_address(conn->hdev, &conn->init_addr,
5928 &conn->init_addr_type);
5929 }
5930 } else {
5931 conn->resp_addr_type = conn->hdev->adv_addr_type;
5932 /* Check if the controller has set a Local RPA then it must be
5933 * used instead or hdev->rpa.
5934 */
5935 if (local_rpa && bacmp(local_rpa, BDADDR_ANY)) {
5936 conn->resp_addr_type = ADDR_LE_DEV_RANDOM;
5937 bacpy(&conn->resp_addr, local_rpa);
5938 } else if (conn->hdev->adv_addr_type == ADDR_LE_DEV_RANDOM) {
5939 /* In case of ext adv, resp_addr will be updated in
5940 * Adv Terminated event.
5941 */
5942 if (!ext_adv_capable(conn->hdev))
5943 bacpy(&conn->resp_addr,
5944 &conn->hdev->random_addr);
5945 } else {
5946 bacpy(&conn->resp_addr, &conn->hdev->bdaddr);
5947 }
5948
5949 conn->init_addr_type = bdaddr_type;
5950 bacpy(&conn->init_addr, bdaddr);
5951
5952 /* For incoming connections, set the default minimum
5953 * and maximum connection interval. They will be used
5954 * to check if the parameters are in range and if not
5955 * trigger the connection update procedure.
5956 */
5957 conn->le_conn_min_interval = conn->hdev->le_conn_min_interval;
5958 conn->le_conn_max_interval = conn->hdev->le_conn_max_interval;
5959 }
5960}
5961
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305962static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07005963 bdaddr_t *bdaddr, u8 bdaddr_type,
5964 bdaddr_t *local_rpa, u8 role, u16 handle,
5965 u16 interval, u16 latency,
5966 u16 supervision_timeout)
Ville Tervofcd89c02011-02-10 22:38:47 -03005967{
Johan Hedberg912b42ef2014-07-03 19:33:49 +03005968 struct hci_conn_params *params;
Ville Tervofcd89c02011-02-10 22:38:47 -03005969 struct hci_conn *conn;
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02005970 struct smp_irk *irk;
Johan Hedberg837d5022014-07-02 09:36:22 +03005971 u8 addr_type;
Ville Tervofcd89c02011-02-10 22:38:47 -03005972
Ville Tervofcd89c02011-02-10 22:38:47 -03005973 hci_dev_lock(hdev);
5974
Johan Hedbergfbd96c12014-07-08 17:21:51 +03005975 /* All controllers implicitly stop advertising in the event of a
5976 * connection, so ensure that the state bit is cleared.
5977 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005978 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedbergfbd96c12014-07-08 17:21:51 +03005979
Jakub Pawlowskie7d9ab72015-08-07 20:22:52 +02005980 conn = hci_lookup_le_connect(hdev);
Ville Tervob62f3282011-02-10 22:38:50 -03005981 if (!conn) {
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305982 conn = hci_conn_add(hdev, LE_LINK, bdaddr, role);
Ville Tervob62f3282011-02-10 22:38:50 -03005983 if (!conn) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01005984 bt_dev_err(hdev, "no memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03005985 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03005986 }
Andre Guedes29b79882011-05-31 14:20:54 -03005987
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305988 conn->dst_type = bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03005989
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005990 /* If we didn't have a hci_conn object previously
Archie Pusaka74be5232021-06-04 16:26:25 +08005991 * but we're in central role this must be something
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08005992 * initiated using an accept list. Since accept list based
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02005993 * connections are not "first class citizens" we don't
5994 * have full tracking of them. Therefore, we go ahead
5995 * with a "best effort" approach of determining the
5996 * initiator address based on the HCI_PRIVACY flag.
5997 */
5998 if (conn->out) {
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05305999 conn->resp_addr_type = bdaddr_type;
6000 bacpy(&conn->resp_addr, bdaddr);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006001 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02006002 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
6003 bacpy(&conn->init_addr, &hdev->rpa);
6004 } else {
6005 hci_copy_identity_address(hdev,
6006 &conn->init_addr,
6007 &conn->init_addr_type);
6008 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02006009 }
Johan Hedberg9489eca2014-02-28 17:45:46 +02006010 } else {
6011 cancel_delayed_work(&conn->le_conn_timeout);
Ville Tervob62f3282011-02-10 22:38:50 -03006012 }
Ville Tervofcd89c02011-02-10 22:38:47 -03006013
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07006014 le_conn_update_addr(conn, bdaddr, bdaddr_type, local_rpa);
Johan Hedberg7be2edb2014-02-23 19:42:17 +02006015
Marcel Holtmannedb4b462014-02-18 15:13:43 -08006016 /* Lookup the identity address from the stored connection
6017 * address and address type.
6018 *
6019 * When establishing connections to an identity address, the
6020 * connection procedure will store the resolvable random
6021 * address first. Now if it can be converted back into the
6022 * identity address, start using the identity address from
6023 * now on.
6024 */
6025 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02006026 if (irk) {
6027 bacpy(&conn->dst, &irk->bdaddr);
6028 conn->dst_type = irk->addr_type;
6029 }
6030
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07006031 conn->dst_type = ev_bdaddr_type(hdev, conn->dst_type, NULL);
Sathish Narasimman79699a72021-05-20 17:12:01 +05306032
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05306033 if (status) {
6034 hci_le_conn_failed(conn, status);
Johan Hedberg837d5022014-07-02 09:36:22 +03006035 goto unlock;
6036 }
6037
Johan Hedberg08853f12014-08-15 21:06:55 +03006038 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
6039 addr_type = BDADDR_LE_PUBLIC;
6040 else
6041 addr_type = BDADDR_LE_RANDOM;
6042
Johan Hedberg2d3c2262014-07-15 11:51:28 +03006043 /* Drop the connection if the device is blocked */
Archie Pusaka3d4f9c02021-06-04 16:26:27 +08006044 if (hci_bdaddr_list_lookup(&hdev->reject_list, &conn->dst, addr_type)) {
Johan Hedberg2d3c2262014-07-15 11:51:28 +03006045 hci_conn_drop(conn);
Andre Guedescd17dec2012-07-27 15:10:16 -03006046 goto unlock;
6047 }
6048
Johan Hedbergb644ba32012-01-17 21:48:47 +02006049 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Yu Liu1c6ed312021-04-09 15:04:06 -07006050 mgmt_device_connected(hdev, conn, NULL, 0);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03006051
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03006052 conn->sec_level = BT_SECURITY_LOW;
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05306053 conn->handle = handle;
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006054 conn->state = BT_CONFIG;
Ville Tervofcd89c02011-02-10 22:38:47 -03006055
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07006056 /* Store current advertising instance as connection advertising instance
6057 * when sotfware rotation is in use so it can be re-enabled when
6058 * disconnected.
6059 */
6060 if (!ext_adv_capable(hdev))
6061 conn->adv_instance = hdev->cur_adv_instance;
6062
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05306063 conn->le_conn_interval = interval;
6064 conn->le_conn_latency = latency;
6065 conn->le_supv_timeout = supervision_timeout;
Marcel Holtmanne04fde62014-06-23 11:40:04 +02006066
Marcel Holtmann23b9ceb2014-12-20 17:13:41 +01006067 hci_debugfs_create_conn(conn);
Ville Tervofcd89c02011-02-10 22:38:47 -03006068 hci_conn_add_sysfs(conn);
6069
Archie Pusakaef365da2021-05-31 16:37:23 +08006070 /* The remote features procedure is defined for central
Colin Ian Kingd17010b2018-10-10 15:37:31 +01006071 * role only. So only in case of an initiated connection
6072 * request the remote features.
6073 *
Archie Pusakaef365da2021-05-31 16:37:23 +08006074 * If the local controller supports peripheral-initiated features
6075 * exchange, then requesting the remote features in peripheral
Colin Ian Kingd17010b2018-10-10 15:37:31 +01006076 * role is possible. Otherwise just transition into the
6077 * connected state without requesting the remote features.
6078 */
6079 if (conn->out ||
Archie Pusakaef365da2021-05-31 16:37:23 +08006080 (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES)) {
Colin Ian Kingd17010b2018-10-10 15:37:31 +01006081 struct hci_cp_le_read_remote_features cp;
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006082
Colin Ian Kingd17010b2018-10-10 15:37:31 +01006083 cp.handle = __cpu_to_le16(conn->handle);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006084
Colin Ian Kingd17010b2018-10-10 15:37:31 +01006085 hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
6086 sizeof(cp), &cp);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006087
Colin Ian Kingd17010b2018-10-10 15:37:31 +01006088 hci_conn_hold(conn);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006089 } else {
Colin Ian Kingd17010b2018-10-10 15:37:31 +01006090 conn->state = BT_CONNECTED;
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05306091 hci_connect_cfm(conn, status);
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006092 }
Ville Tervofcd89c02011-02-10 22:38:47 -03006093
Johan Hedberg54776102014-08-15 21:06:56 +03006094 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
6095 conn->dst_type);
Johan Hedbergf161dd42014-08-15 21:06:54 +03006096 if (params) {
Johan Hedberg95305ba2014-07-04 12:37:21 +03006097 list_del_init(&params->action);
Johan Hedbergf161dd42014-08-15 21:06:54 +03006098 if (params->conn) {
6099 hci_conn_drop(params->conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03006100 hci_conn_put(params->conn);
Johan Hedbergf161dd42014-08-15 21:06:54 +03006101 params->conn = NULL;
6102 }
6103 }
Andre Guedesa4790db2014-02-26 20:21:47 -03006104
Ville Tervofcd89c02011-02-10 22:38:47 -03006105unlock:
Luiz Augusto von Dentz5bee2fd2021-10-27 16:58:43 -07006106 hci_update_passive_scan(hdev);
Ville Tervofcd89c02011-02-10 22:38:47 -03006107 hci_dev_unlock(hdev);
6108}
6109
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05306110static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
6111{
6112 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
6113
6114 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
6115
6116 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07006117 NULL, ev->role, le16_to_cpu(ev->handle),
Jaganath Kanakkasseryd12fb052018-07-06 17:05:30 +05306118 le16_to_cpu(ev->interval),
6119 le16_to_cpu(ev->latency),
6120 le16_to_cpu(ev->supervision_timeout));
6121}
6122
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02006123static void hci_le_enh_conn_complete_evt(struct hci_dev *hdev,
6124 struct sk_buff *skb)
6125{
6126 struct hci_ev_le_enh_conn_complete *ev = (void *) skb->data;
6127
6128 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
6129
6130 le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07006131 &ev->local_rpa, ev->role, le16_to_cpu(ev->handle),
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02006132 le16_to_cpu(ev->interval),
6133 le16_to_cpu(ev->latency),
6134 le16_to_cpu(ev->supervision_timeout));
6135}
6136
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306137static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, struct sk_buff *skb)
6138{
6139 struct hci_evt_le_ext_adv_set_term *ev = (void *) skb->data;
6140 struct hci_conn *conn;
Archie Pusaka1f9d5652021-11-11 13:20:54 +08006141 struct adv_info *adv, *n;
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306142
6143 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
6144
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07006145 adv = hci_find_adv_instance(hdev, ev->handle);
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07006146
Archie Pusaka0f281a52021-11-11 13:20:53 +08006147 /* The Bluetooth Core 5.3 specification clearly states that this event
6148 * shall not be sent when the Host disables the advertising set. So in
6149 * case of HCI_ERROR_CANCELLED_BY_HOST, just ignore the event.
6150 *
6151 * When the Host disables an advertising set, all cleanup is done via
6152 * its command callback and not needed to be duplicated here.
6153 */
6154 if (ev->status == HCI_ERROR_CANCELLED_BY_HOST) {
6155 bt_dev_warn_ratelimited(hdev, "Unexpected advertising set terminated event");
6156 return;
6157 }
6158
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07006159 if (ev->status) {
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07006160 if (!adv)
6161 return;
6162
6163 /* Remove advertising as it has been terminated */
6164 hci_remove_adv_instance(hdev, ev->handle);
6165 mgmt_advertising_removed(NULL, hdev, ev->handle);
6166
Archie Pusaka1f9d5652021-11-11 13:20:54 +08006167 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
6168 if (adv->enabled)
6169 return;
6170 }
6171
6172 /* We are no longer advertising, clear HCI_LE_ADV */
6173 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306174 return;
Luiz Augusto von Dentz23837a62021-06-22 20:59:02 -07006175 }
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306176
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07006177 if (adv)
6178 adv->enabled = false;
6179
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306180 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle));
6181 if (conn) {
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07006182 /* Store handle in the connection so the correct advertising
6183 * instance can be re-enabled when disconnected.
6184 */
6185 conn->adv_instance = ev->handle;
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306186
Luiz Augusto von Dentzcafae4c2021-08-11 16:20:15 -07006187 if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM ||
6188 bacmp(&conn->resp_addr, BDADDR_ANY))
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306189 return;
6190
Daniel Winkler25e70882021-04-05 16:33:04 -07006191 if (!ev->handle) {
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306192 bacpy(&conn->resp_addr, &hdev->random_addr);
6193 return;
6194 }
6195
Luiz Augusto von Dentz7087c4f2021-08-11 16:20:16 -07006196 if (adv)
6197 bacpy(&conn->resp_addr, &adv->random_addr);
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306198 }
6199}
6200
Marcel Holtmann1855d922014-06-23 11:40:05 +02006201static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
6202 struct sk_buff *skb)
6203{
6204 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
6205 struct hci_conn *conn;
6206
6207 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
6208
6209 if (ev->status)
6210 return;
6211
6212 hci_dev_lock(hdev);
6213
6214 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6215 if (conn) {
6216 conn->le_conn_interval = le16_to_cpu(ev->interval);
6217 conn->le_conn_latency = le16_to_cpu(ev->latency);
6218 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
6219 }
6220
6221 hci_dev_unlock(hdev);
6222}
6223
Andre Guedesa4790db2014-02-26 20:21:47 -03006224/* This function requires the caller holds hdev->lock */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006225static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
6226 bdaddr_t *addr,
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07006227 u8 addr_type, bool addr_resolved,
6228 u8 adv_type, bdaddr_t *direct_rpa)
Andre Guedesa4790db2014-02-26 20:21:47 -03006229{
6230 struct hci_conn *conn;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006231 struct hci_conn_params *params;
Andre Guedesa4790db2014-02-26 20:21:47 -03006232
Johan Hedberg1c1abca2014-07-07 12:45:53 +03006233 /* If the event is not connectable don't proceed further */
6234 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006235 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03006236
Luiz Augusto von Dentz182ee452021-10-27 16:59:00 -07006237 /* Ignore if the device is blocked or hdev is suspended */
6238 if (hci_bdaddr_list_lookup(&hdev->reject_list, addr, addr_type) ||
6239 hdev->suspended)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006240 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03006241
Johan Hedbergf99353c2014-07-16 11:56:09 +03006242 /* Most controller will fail if we try to create new connections
Archie Pusaka39bc74c2021-06-04 16:26:26 +08006243 * while we have an existing one in peripheral role.
Johan Hedbergf99353c2014-07-16 11:56:09 +03006244 */
Archie Pusaka39bc74c2021-06-04 16:26:26 +08006245 if (hdev->conn_hash.le_num_peripheral > 0 &&
Alain Michaud4364f2e2020-04-23 14:43:29 +00006246 (!test_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks) ||
6247 !(hdev->le_states[3] & 0x10)))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006248 return NULL;
Johan Hedbergf99353c2014-07-16 11:56:09 +03006249
Johan Hedberg1c1abca2014-07-07 12:45:53 +03006250 /* If we're not connectable only connect devices that we have in
6251 * our pend_le_conns list.
6252 */
Johan Hedberg49c50922015-10-16 10:07:51 +03006253 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
6254 addr_type);
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006255 if (!params)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006256 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03006257
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02006258 if (!params->explicit_connect) {
6259 switch (params->auto_connect) {
6260 case HCI_AUTO_CONN_DIRECT:
6261 /* Only devices advertising with ADV_DIRECT_IND are
6262 * triggering a connection attempt. This is allowing
Archie Pusaka67ffb182021-05-31 16:37:28 +08006263 * incoming connections from peripheral devices.
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02006264 */
6265 if (adv_type != LE_ADV_DIRECT_IND)
6266 return NULL;
6267 break;
6268 case HCI_AUTO_CONN_ALWAYS:
6269 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
6270 * are triggering a connection attempt. This means
Archie Pusaka67ffb182021-05-31 16:37:28 +08006271 * that incoming connections from peripheral device are
6272 * accepted and also outgoing connections to peripheral
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02006273 * devices are established when found.
6274 */
6275 break;
6276 default:
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006277 return NULL;
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02006278 }
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006279 }
6280
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07006281 conn = hci_connect_le(hdev, addr, addr_type, addr_resolved,
6282 BT_SECURITY_LOW, hdev->def_le_autoconnect_timeout,
6283 HCI_ROLE_MASTER, direct_rpa);
Johan Hedbergf161dd42014-08-15 21:06:54 +03006284 if (!IS_ERR(conn)) {
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02006285 /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
6286 * by higher layer that tried to connect, if no then
6287 * store the pointer since we don't really have any
Johan Hedbergf161dd42014-08-15 21:06:54 +03006288 * other owner of the object besides the params that
6289 * triggered it. This way we can abort the connection if
6290 * the parameters get removed and keep the reference
6291 * count consistent once the connection is established.
6292 */
Jakub Pawlowski28a667c2015-08-07 20:22:54 +02006293
6294 if (!params->explicit_connect)
6295 params->conn = hci_conn_get(conn);
6296
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006297 return conn;
Johan Hedbergf161dd42014-08-15 21:06:54 +03006298 }
Andre Guedesa4790db2014-02-26 20:21:47 -03006299
6300 switch (PTR_ERR(conn)) {
6301 case -EBUSY:
6302 /* If hci_connect() returns -EBUSY it means there is already
6303 * an LE connection attempt going on. Since controllers don't
6304 * support more than one connection attempt at the time, we
6305 * don't consider this an error case.
6306 */
6307 break;
6308 default:
6309 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006310 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03006311 }
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006312
6313 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03006314}
6315
Johan Hedberg4af605d2014-03-24 10:48:00 +02006316static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006317 u8 bdaddr_type, bdaddr_t *direct_addr,
Alain Michauda2ec9052020-07-27 20:48:55 +00006318 u8 direct_addr_type, s8 rssi, u8 *data, u8 len,
6319 bool ext_adv)
Johan Hedberg4af605d2014-03-24 10:48:00 +02006320{
Johan Hedbergb9a63282014-03-25 10:51:52 +02006321 struct discovery_state *d = &hdev->discovery;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03006322 struct smp_irk *irk;
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006323 struct hci_conn *conn;
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07006324 bool match, bdaddr_resolved;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02006325 u32 flags;
Szymon Janc1c58e932021-05-18 16:54:36 +02006326 u8 *ptr;
Szymon Janc68183752015-09-16 20:21:54 +02006327
Johan Hedberg56b40fb2016-04-07 21:01:27 +03006328 switch (type) {
6329 case LE_ADV_IND:
6330 case LE_ADV_DIRECT_IND:
6331 case LE_ADV_SCAN_IND:
6332 case LE_ADV_NONCONN_IND:
6333 case LE_ADV_SCAN_RSP:
6334 break;
6335 default:
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006336 bt_dev_err_ratelimited(hdev, "unknown advertising packet "
6337 "type: 0x%02x", type);
Johan Hedberg56b40fb2016-04-07 21:01:27 +03006338 return;
6339 }
6340
Alain Michauda2ec9052020-07-27 20:48:55 +00006341 if (!ext_adv && len > HCI_MAX_AD_LENGTH) {
6342 bt_dev_err_ratelimited(hdev, "legacy adv larger than 31 bytes");
6343 return;
6344 }
6345
Szymon Janc68183752015-09-16 20:21:54 +02006346 /* Find the end of the data in case the report contains padded zero
6347 * bytes at the end causing an invalid length value.
6348 *
6349 * When data is NULL, len is 0 so there is no need for extra ptr
6350 * check as 'ptr < data + 0' is already false in such case.
6351 */
6352 for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
6353 if (ptr + 1 + *ptr > data + len)
6354 break;
6355 }
6356
Szymon Janc1c58e932021-05-18 16:54:36 +02006357 /* Adjust for actual length. This handles the case when remote
6358 * device is advertising with incorrect data length.
6359 */
6360 len = ptr - data;
Johan Hedbergb9a63282014-03-25 10:51:52 +02006361
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006362 /* If the direct address is present, then this report is from
6363 * a LE Direct Advertising Report event. In that case it is
6364 * important to see if the address is matching the local
6365 * controller address.
6366 */
6367 if (direct_addr) {
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07006368 direct_addr_type = ev_bdaddr_type(hdev, direct_addr_type,
6369 &bdaddr_resolved);
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07006370
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006371 /* Only resolvable random addresses are valid for these
6372 * kind of reports and others can be ignored.
6373 */
6374 if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
6375 return;
6376
6377 /* If the controller is not using resolvable random
6378 * addresses, then this report can be ignored.
6379 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006380 if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006381 return;
6382
6383 /* If the local IRK of the controller does not match
6384 * with the resolvable random address provided, then
6385 * this report can be ignored.
6386 */
6387 if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
6388 return;
6389 }
6390
Johan Hedberg1c1abca2014-07-07 12:45:53 +03006391 /* Check if we need to convert to identity address */
6392 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
6393 if (irk) {
6394 bdaddr = &irk->bdaddr;
6395 bdaddr_type = irk->addr_type;
6396 }
6397
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07006398 bdaddr_type = ev_bdaddr_type(hdev, bdaddr_type, &bdaddr_resolved);
Luiz Augusto von Dentz4ec4d632021-08-30 13:55:36 -07006399
Szymon Janc082f2302018-04-03 13:40:06 +02006400 /* Check if we have been requested to connect to this device.
6401 *
6402 * direct_addr is set only for directed advertising reports (it is NULL
6403 * for advertising reports) and is already verified to be RPA above.
6404 */
Luiz Augusto von Dentzd850bf02021-08-30 13:55:37 -07006405 conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, bdaddr_resolved,
6406 type, direct_addr);
Alain Michauda2ec9052020-07-27 20:48:55 +00006407 if (!ext_adv && conn && type == LE_ADV_IND && len <= HCI_MAX_AD_LENGTH) {
Alfonso Acostafd45ada2014-10-07 08:44:11 +00006408 /* Store report for later inclusion by
6409 * mgmt_device_connected
6410 */
6411 memcpy(conn->le_adv_data, data, len);
6412 conn->le_adv_data_len = len;
6413 }
Johan Hedberg1c1abca2014-07-07 12:45:53 +03006414
Johan Hedberg0d2bf132014-07-02 22:42:02 +03006415 /* Passive scanning shouldn't trigger any device found events,
6416 * except for devices marked as CONN_REPORT for which we do send
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02006417 * device found events, or advertisement monitoring requested.
Johan Hedberg0d2bf132014-07-02 22:42:02 +03006418 */
Johan Hedbergca5c4be2014-03-25 10:30:46 +02006419 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
Johan Hedberg0d2bf132014-07-02 22:42:02 +03006420 if (type == LE_ADV_DIRECT_IND)
6421 return;
6422
Johan Hedberg3a19b6f2014-07-15 08:07:59 +03006423 if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
Miao-chen Chou8208f5a2020-06-17 16:39:18 +02006424 bdaddr, bdaddr_type) &&
6425 idr_is_empty(&hdev->adv_monitors_idr))
Johan Hedberg0d2bf132014-07-02 22:42:02 +03006426 return;
6427
6428 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
6429 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
6430 else
6431 flags = 0;
6432 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
6433 rssi, flags, data, len, NULL, 0);
Johan Hedberg97bf2e92014-07-04 12:37:16 +03006434 return;
Johan Hedbergca5c4be2014-03-25 10:30:46 +02006435 }
Johan Hedberg4af605d2014-03-24 10:48:00 +02006436
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02006437 /* When receiving non-connectable or scannable undirected
6438 * advertising reports, this means that the remote device is
6439 * not connectable and then clearly indicate this in the
6440 * device found event.
6441 *
6442 * When receiving a scan response, then there is no way to
6443 * know if the remote device is connectable or not. However
6444 * since scan responses are merged with a previously seen
6445 * advertising report, the flags field from that report
6446 * will be used.
6447 *
6448 * In the really unlikely case that a controller get confused
6449 * and just sends a scan response event, then it is marked as
6450 * not connectable as well.
6451 */
6452 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
6453 type == LE_ADV_SCAN_RSP)
6454 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
6455 else
6456 flags = 0;
6457
Johan Hedbergb9a63282014-03-25 10:51:52 +02006458 /* If there's nothing pending either store the data from this
6459 * event or send an immediate device found event if the data
6460 * should not be stored for later.
6461 */
Alain Michauda2ec9052020-07-27 20:48:55 +00006462 if (!ext_adv && !has_pending_adv_report(hdev)) {
Johan Hedbergb9a63282014-03-25 10:51:52 +02006463 /* If the report will trigger a SCAN_REQ store it for
6464 * later merging.
6465 */
6466 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
6467 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02006468 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02006469 return;
6470 }
6471
6472 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02006473 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02006474 return;
6475 }
6476
Johan Hedberg474ee062014-03-25 14:34:59 +02006477 /* Check if the pending report is for the same device as the new one */
6478 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
6479 bdaddr_type == d->last_adv_addr_type);
6480
Johan Hedbergb9a63282014-03-25 10:51:52 +02006481 /* If the pending data doesn't match this report or this isn't a
6482 * scan response (e.g. we got a duplicate ADV_IND) then force
6483 * sending of the pending data.
6484 */
Johan Hedberg474ee062014-03-25 14:34:59 +02006485 if (type != LE_ADV_SCAN_RSP || !match) {
6486 /* Send out whatever is in the cache, but skip duplicates */
6487 if (!match)
6488 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergff5cd292014-03-25 14:40:52 +02006489 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02006490 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergff5cd292014-03-25 14:40:52 +02006491 d->last_adv_data,
Johan Hedberg474ee062014-03-25 14:34:59 +02006492 d->last_adv_data_len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02006493
6494 /* If the new report will trigger a SCAN_REQ store it for
6495 * later merging.
6496 */
Alain Michauda2ec9052020-07-27 20:48:55 +00006497 if (!ext_adv && (type == LE_ADV_IND ||
6498 type == LE_ADV_SCAN_IND)) {
Johan Hedbergb9a63282014-03-25 10:51:52 +02006499 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02006500 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02006501 return;
6502 }
6503
6504 /* The advertising reports cannot be merged, so clear
6505 * the pending report and send out a device found event.
6506 */
6507 clear_pending_adv_report(hdev);
Johan Hedberg5c5b93e2014-03-29 08:39:53 +02006508 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02006509 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02006510 return;
6511 }
6512
6513 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
6514 * the new event is a SCAN_RSP. We can therefore proceed with
6515 * sending a merged device found event.
6516 */
6517 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02006518 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
Marcel Holtmann42bd6a52014-07-01 14:11:19 +02006519 d->last_adv_data, d->last_adv_data_len, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02006520 clear_pending_adv_report(hdev);
Johan Hedberg4af605d2014-03-24 10:48:00 +02006521}
6522
Gustavo Padovan6039aa72012-05-23 04:04:18 -03006523static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03006524{
Andre Guedese95beb42011-09-26 20:48:35 -03006525 u8 num_reports = skb->data[0];
6526 void *ptr = &skb->data[1];
Andre Guedes9aa04c92011-05-26 16:23:51 -03006527
Andre Guedesa4790db2014-02-26 20:21:47 -03006528 hci_dev_lock(hdev);
6529
Andre Guedese95beb42011-09-26 20:48:35 -03006530 while (num_reports--) {
6531 struct hci_ev_le_advertising_info *ev = ptr;
Johan Hedberg4af605d2014-03-24 10:48:00 +02006532 s8 rssi;
Andre Guedesa4790db2014-02-26 20:21:47 -03006533
Brian Gix899663b2021-11-24 12:16:28 -08006534 if (ptr > (void *)skb_tail_pointer(skb) - sizeof(*ev)) {
6535 bt_dev_err(hdev, "Malicious advertising data.");
6536 break;
6537 }
6538
Pavel Skripkin3a56ef72021-11-01 10:12:12 +03006539 if (ev->length <= HCI_MAX_AD_LENGTH &&
6540 ev->data + ev->length <= skb_tail_pointer(skb)) {
Chriz Chowee649342018-04-20 15:46:24 +08006541 rssi = ev->data[ev->length];
6542 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
6543 ev->bdaddr_type, NULL, 0, rssi,
Alain Michauda2ec9052020-07-27 20:48:55 +00006544 ev->data, ev->length, false);
Chriz Chowee649342018-04-20 15:46:24 +08006545 } else {
6546 bt_dev_err(hdev, "Dropping invalid advertising data");
6547 }
Andre Guedes3c9e9192012-01-10 18:20:50 -03006548
Andre Guedese95beb42011-09-26 20:48:35 -03006549 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03006550 }
Andre Guedesa4790db2014-02-26 20:21:47 -03006551
6552 hci_dev_unlock(hdev);
Andre Guedes9aa04c92011-05-26 16:23:51 -03006553}
6554
Marcel Holtmann657cc642019-12-11 11:34:36 +01006555static u8 ext_evt_type_to_legacy(struct hci_dev *hdev, u16 evt_type)
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05306556{
Jaganath Kanakkasseryb2cc9762018-07-19 17:09:38 +05306557 if (evt_type & LE_EXT_ADV_LEGACY_PDU) {
6558 switch (evt_type) {
6559 case LE_LEGACY_ADV_IND:
6560 return LE_ADV_IND;
6561 case LE_LEGACY_ADV_DIRECT_IND:
6562 return LE_ADV_DIRECT_IND;
6563 case LE_LEGACY_ADV_SCAN_IND:
6564 return LE_ADV_SCAN_IND;
6565 case LE_LEGACY_NONCONN_IND:
6566 return LE_ADV_NONCONN_IND;
6567 case LE_LEGACY_SCAN_RSP_ADV:
6568 case LE_LEGACY_SCAN_RSP_ADV_SCAN:
6569 return LE_ADV_SCAN_RSP;
6570 }
6571
Marcel Holtmann657cc642019-12-11 11:34:36 +01006572 goto invalid;
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05306573 }
6574
Jaganath Kanakkasseryb2cc9762018-07-19 17:09:38 +05306575 if (evt_type & LE_EXT_ADV_CONN_IND) {
6576 if (evt_type & LE_EXT_ADV_DIRECT_IND)
6577 return LE_ADV_DIRECT_IND;
6578
6579 return LE_ADV_IND;
6580 }
6581
6582 if (evt_type & LE_EXT_ADV_SCAN_RSP)
6583 return LE_ADV_SCAN_RSP;
6584
6585 if (evt_type & LE_EXT_ADV_SCAN_IND)
6586 return LE_ADV_SCAN_IND;
6587
6588 if (evt_type == LE_EXT_ADV_NON_CONN_IND ||
6589 evt_type & LE_EXT_ADV_DIRECT_IND)
6590 return LE_ADV_NONCONN_IND;
6591
Marcel Holtmann657cc642019-12-11 11:34:36 +01006592invalid:
6593 bt_dev_err_ratelimited(hdev, "Unknown advertising packet type: 0x%02x",
6594 evt_type);
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05306595
6596 return LE_ADV_INVALID;
6597}
6598
6599static void hci_le_ext_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
6600{
6601 u8 num_reports = skb->data[0];
6602 void *ptr = &skb->data[1];
6603
6604 hci_dev_lock(hdev);
6605
6606 while (num_reports--) {
6607 struct hci_ev_le_ext_adv_report *ev = ptr;
6608 u8 legacy_evt_type;
6609 u16 evt_type;
6610
6611 evt_type = __le16_to_cpu(ev->evt_type);
Marcel Holtmann657cc642019-12-11 11:34:36 +01006612 legacy_evt_type = ext_evt_type_to_legacy(hdev, evt_type);
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05306613 if (legacy_evt_type != LE_ADV_INVALID) {
6614 process_adv_report(hdev, legacy_evt_type, &ev->bdaddr,
6615 ev->bdaddr_type, NULL, 0, ev->rssi,
Alain Michauda2ec9052020-07-27 20:48:55 +00006616 ev->data, ev->length,
6617 !(evt_type & LE_EXT_ADV_LEGACY_PDU));
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05306618 }
6619
Jaganath Kanakkasserycd9151b2019-04-03 12:11:44 +05306620 ptr += sizeof(*ev) + ev->length;
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05306621 }
6622
6623 hci_dev_unlock(hdev);
6624}
6625
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006626static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev,
6627 struct sk_buff *skb)
6628{
6629 struct hci_ev_le_remote_feat_complete *ev = (void *)skb->data;
6630 struct hci_conn *conn;
6631
6632 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
6633
6634 hci_dev_lock(hdev);
6635
6636 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6637 if (conn) {
6638 if (!ev->status)
6639 memcpy(conn->features[0], ev->features, 8);
6640
6641 if (conn->state == BT_CONFIG) {
6642 __u8 status;
6643
Archie Pusakaef365da2021-05-31 16:37:23 +08006644 /* If the local controller supports peripheral-initiated
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006645 * features exchange, but the remote controller does
6646 * not, then it is possible that the error code 0x1a
6647 * for unsupported remote feature gets returned.
6648 *
6649 * In this specific case, allow the connection to
6650 * transition into connected state and mark it as
6651 * successful.
6652 */
Archie Pusakaef365da2021-05-31 16:37:23 +08006653 if (!conn->out && ev->status == 0x1a &&
6654 (hdev->le_features[0] & HCI_LE_PERIPHERAL_FEATURES))
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006655 status = 0x00;
6656 else
6657 status = ev->status;
6658
6659 conn->state = BT_CONNECTED;
6660 hci_connect_cfm(conn, status);
6661 hci_conn_drop(conn);
6662 }
6663 }
6664
6665 hci_dev_unlock(hdev);
6666}
6667
Gustavo Padovan6039aa72012-05-23 04:04:18 -03006668static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006669{
6670 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
6671 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03006672 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006673 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03006674 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006675
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03006676 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006677
6678 hci_dev_lock(hdev);
6679
6680 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03006681 if (conn == NULL)
6682 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006683
Johan Hedbergf3a73d92014-05-29 15:02:59 +03006684 ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
Johan Hedberg5378bc52014-05-29 14:00:39 +03006685 if (!ltk)
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03006686 goto not_found;
6687
Johan Hedberg5378bc52014-05-29 14:00:39 +03006688 if (smp_ltk_is_sc(ltk)) {
6689 /* With SC both EDiv and Rand are set to zero */
6690 if (ev->ediv || ev->rand)
6691 goto not_found;
6692 } else {
6693 /* For non-SC keys check that EDiv and Rand match */
6694 if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
6695 goto not_found;
6696 }
6697
Johan Hedberg8b76ce32015-06-08 18:14:39 +03006698 memcpy(cp.ltk, ltk->val, ltk->enc_size);
6699 memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006700 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03006701
Johan Hedberga6f78332014-09-10 17:37:45 -07006702 conn->pending_sec_level = smp_ltk_sec_level(ltk);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006703
Andre Guedes89cbb4d2013-07-31 16:25:29 -03006704 conn->enc_key_size = ltk->enc_size;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006705
6706 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
6707
Claudio Takahasi5981a882013-07-25 16:34:24 -03006708 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
6709 * temporary key used to encrypt a connection following
6710 * pairing. It is used during the Encrypted Session Setup to
6711 * distribute the keys. Later, security can be re-established
6712 * using a distributed LTK.
6713 */
Johan Hedberg2ceba532014-06-16 19:25:16 +03006714 if (ltk->type == SMP_STK) {
Johan Hedbergfe59a052014-07-01 19:14:12 +03006715 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Johan Hedberg970d0f12014-11-13 14:37:47 +02006716 list_del_rcu(&ltk->list);
6717 kfree_rcu(ltk, rcu);
Johan Hedbergfe59a052014-07-01 19:14:12 +03006718 } else {
6719 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03006720 }
6721
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006722 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03006723
6724 return;
6725
6726not_found:
6727 neg.handle = ev->handle;
6728 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
6729 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006730}
6731
Andre Guedes8e75b462014-07-01 18:10:08 -03006732static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
6733 u8 reason)
6734{
6735 struct hci_cp_le_conn_param_req_neg_reply cp;
6736
6737 cp.handle = cpu_to_le16(handle);
6738 cp.reason = reason;
6739
6740 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
6741 &cp);
6742}
6743
6744static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
6745 struct sk_buff *skb)
6746{
6747 struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
6748 struct hci_cp_le_conn_param_req_reply cp;
6749 struct hci_conn *hcon;
6750 u16 handle, min, max, latency, timeout;
6751
6752 handle = le16_to_cpu(ev->handle);
6753 min = le16_to_cpu(ev->interval_min);
6754 max = le16_to_cpu(ev->interval_max);
6755 latency = le16_to_cpu(ev->latency);
6756 timeout = le16_to_cpu(ev->timeout);
6757
6758 hcon = hci_conn_hash_lookup_handle(hdev, handle);
6759 if (!hcon || hcon->state != BT_CONNECTED)
6760 return send_conn_param_neg_reply(hdev, handle,
6761 HCI_ERROR_UNKNOWN_CONN_ID);
6762
6763 if (hci_check_conn_params(min, max, latency, timeout))
6764 return send_conn_param_neg_reply(hdev, handle,
6765 HCI_ERROR_INVALID_LL_PARAMS);
6766
Johan Hedberg40bef302014-07-16 11:42:27 +03006767 if (hcon->role == HCI_ROLE_MASTER) {
Johan Hedberg348d50b2014-07-02 17:37:30 +03006768 struct hci_conn_params *params;
Johan Hedbergf4869e22014-07-02 17:37:32 +03006769 u8 store_hint;
Johan Hedberg348d50b2014-07-02 17:37:30 +03006770
6771 hci_dev_lock(hdev);
6772
6773 params = hci_conn_params_lookup(hdev, &hcon->dst,
6774 hcon->dst_type);
6775 if (params) {
6776 params->conn_min_interval = min;
6777 params->conn_max_interval = max;
6778 params->conn_latency = latency;
6779 params->supervision_timeout = timeout;
Johan Hedbergf4869e22014-07-02 17:37:32 +03006780 store_hint = 0x01;
Meng Yu149b3f12021-04-01 14:50:39 +08006781 } else {
Johan Hedbergf4869e22014-07-02 17:37:32 +03006782 store_hint = 0x00;
Johan Hedberg348d50b2014-07-02 17:37:30 +03006783 }
6784
6785 hci_dev_unlock(hdev);
6786
Johan Hedbergf4869e22014-07-02 17:37:32 +03006787 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
6788 store_hint, min, max, latency, timeout);
Johan Hedberg348d50b2014-07-02 17:37:30 +03006789 }
Andre Guedesffb5a8272014-07-01 18:10:11 -03006790
Andre Guedes8e75b462014-07-01 18:10:08 -03006791 cp.handle = ev->handle;
6792 cp.interval_min = ev->interval_min;
6793 cp.interval_max = ev->interval_max;
6794 cp.latency = ev->latency;
6795 cp.timeout = ev->timeout;
6796 cp.min_ce_len = 0;
6797 cp.max_ce_len = 0;
6798
6799 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
6800}
6801
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006802static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
6803 struct sk_buff *skb)
6804{
6805 u8 num_reports = skb->data[0];
Peilin Yef7e0e8b2020-09-09 03:17:00 -04006806 struct hci_ev_le_direct_adv_info *ev = (void *)&skb->data[1];
6807
6808 if (!num_reports || skb->len < num_reports * sizeof(*ev) + 1)
6809 return;
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006810
6811 hci_dev_lock(hdev);
6812
Peilin Yef7e0e8b2020-09-09 03:17:00 -04006813 for (; num_reports; num_reports--, ev++)
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006814 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
6815 ev->bdaddr_type, &ev->direct_addr,
Alain Michauda2ec9052020-07-27 20:48:55 +00006816 ev->direct_addr_type, ev->rssi, NULL, 0,
6817 false);
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006818
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006819 hci_dev_unlock(hdev);
6820}
6821
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006822static void hci_le_phy_update_evt(struct hci_dev *hdev, struct sk_buff *skb)
6823{
6824 struct hci_ev_le_phy_update_complete *ev = (void *) skb->data;
6825 struct hci_conn *conn;
6826
6827 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
6828
Ayush Garg87df8bc2021-03-17 16:52:14 +05306829 if (ev->status)
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006830 return;
6831
6832 hci_dev_lock(hdev);
6833
6834 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
6835 if (!conn)
6836 goto unlock;
6837
6838 conn->le_tx_phy = ev->tx_phy;
6839 conn->le_rx_phy = ev->rx_phy;
6840
6841unlock:
6842 hci_dev_unlock(hdev);
6843}
6844
Gustavo Padovan6039aa72012-05-23 04:04:18 -03006845static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03006846{
6847 struct hci_ev_le_meta *le_ev = (void *) skb->data;
6848
6849 skb_pull(skb, sizeof(*le_ev));
6850
6851 switch (le_ev->subevent) {
6852 case HCI_EV_LE_CONN_COMPLETE:
6853 hci_le_conn_complete_evt(hdev, skb);
6854 break;
6855
Marcel Holtmann1855d922014-06-23 11:40:05 +02006856 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
6857 hci_le_conn_update_complete_evt(hdev, skb);
6858 break;
6859
Andre Guedes9aa04c92011-05-26 16:23:51 -03006860 case HCI_EV_LE_ADVERTISING_REPORT:
6861 hci_le_adv_report_evt(hdev, skb);
6862 break;
6863
Marcel Holtmann0fe29fd2015-04-08 09:05:27 -07006864 case HCI_EV_LE_REMOTE_FEAT_COMPLETE:
6865 hci_le_remote_feat_complete_evt(hdev, skb);
6866 break;
6867
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03006868 case HCI_EV_LE_LTK_REQ:
6869 hci_le_ltk_request_evt(hdev, skb);
6870 break;
6871
Andre Guedes8e75b462014-07-01 18:10:08 -03006872 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
6873 hci_le_remote_conn_param_req_evt(hdev, skb);
6874 break;
6875
Marcel Holtmann2f010b52014-12-05 16:20:13 +01006876 case HCI_EV_LE_DIRECT_ADV_REPORT:
6877 hci_le_direct_adv_report_evt(hdev, skb);
6878 break;
6879
Luiz Augusto von Dentz1efd9272020-01-02 15:00:55 -08006880 case HCI_EV_LE_PHY_UPDATE_COMPLETE:
6881 hci_le_phy_update_evt(hdev, skb);
6882 break;
6883
Jaganath Kanakkasseryc215e932018-07-06 17:05:29 +05306884 case HCI_EV_LE_EXT_ADV_REPORT:
6885 hci_le_ext_adv_report_evt(hdev, skb);
6886 break;
6887
Jaganath Kanakkassery4d94f952018-07-06 22:50:32 +02006888 case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
6889 hci_le_enh_conn_complete_evt(hdev, skb);
6890 break;
6891
Jaganath Kanakkasseryacf0aea2018-07-19 17:09:46 +05306892 case HCI_EV_LE_EXT_ADV_SET_TERM:
6893 hci_le_ext_adv_term_evt(hdev, skb);
6894 break;
6895
Ville Tervofcd89c02011-02-10 22:38:47 -03006896 default:
6897 break;
6898 }
6899}
6900
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006901static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
6902 u8 event, struct sk_buff *skb)
6903{
6904 struct hci_ev_cmd_complete *ev;
6905 struct hci_event_hdr *hdr;
6906
6907 if (!skb)
6908 return false;
6909
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08006910 hdr = hci_ev_skb_pull(hdev, skb, event, sizeof(*hdr));
6911 if (!hdr)
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006912 return false;
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006913
6914 if (event) {
6915 if (hdr->evt != event)
6916 return false;
6917 return true;
6918 }
6919
Zheng Yongjun91641b72021-06-02 14:54:58 +08006920 /* Check if request ended in Command Status - no way to retrieve
Johan Hedberg1629db9c2018-11-27 11:37:46 +02006921 * any extra parameters in this case.
6922 */
6923 if (hdr->evt == HCI_EV_CMD_STATUS)
6924 return false;
6925
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006926 if (hdr->evt != HCI_EV_CMD_COMPLETE) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01006927 bt_dev_err(hdev, "last event is not cmd complete (0x%2.2x)",
6928 hdr->evt);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006929 return false;
6930 }
6931
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08006932 ev = hci_cc_skb_pull(hdev, skb, opcode, sizeof(*ev));
6933 if (!ev)
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006934 return false;
Johan Hedberg757aa0b2015-04-02 13:41:12 +03006935
6936 if (opcode != __le16_to_cpu(ev->opcode)) {
6937 BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
6938 __le16_to_cpu(ev->opcode));
6939 return false;
6940 }
6941
6942 return true;
6943}
6944
Abhishek Pandit-Subedi2f202162020-09-11 14:07:13 -07006945static void hci_store_wake_reason(struct hci_dev *hdev, u8 event,
6946 struct sk_buff *skb)
6947{
6948 struct hci_ev_le_advertising_info *adv;
6949 struct hci_ev_le_direct_adv_info *direct_adv;
6950 struct hci_ev_le_ext_adv_report *ext_adv;
6951 const struct hci_ev_conn_complete *conn_complete = (void *)skb->data;
6952 const struct hci_ev_conn_request *conn_request = (void *)skb->data;
6953
6954 hci_dev_lock(hdev);
6955
6956 /* If we are currently suspended and this is the first BT event seen,
6957 * save the wake reason associated with the event.
6958 */
6959 if (!hdev->suspended || hdev->wake_reason)
6960 goto unlock;
6961
6962 /* Default to remote wake. Values for wake_reason are documented in the
6963 * Bluez mgmt api docs.
6964 */
6965 hdev->wake_reason = MGMT_WAKE_REASON_REMOTE_WAKE;
6966
6967 /* Once configured for remote wakeup, we should only wake up for
6968 * reconnections. It's useful to see which device is waking us up so
6969 * keep track of the bdaddr of the connection event that woke us up.
6970 */
6971 if (event == HCI_EV_CONN_REQUEST) {
6972 bacpy(&hdev->wake_addr, &conn_complete->bdaddr);
6973 hdev->wake_addr_type = BDADDR_BREDR;
6974 } else if (event == HCI_EV_CONN_COMPLETE) {
6975 bacpy(&hdev->wake_addr, &conn_request->bdaddr);
6976 hdev->wake_addr_type = BDADDR_BREDR;
6977 } else if (event == HCI_EV_LE_META) {
6978 struct hci_ev_le_meta *le_ev = (void *)skb->data;
6979 u8 subevent = le_ev->subevent;
6980 u8 *ptr = &skb->data[sizeof(*le_ev)];
6981 u8 num_reports = *ptr;
6982
6983 if ((subevent == HCI_EV_LE_ADVERTISING_REPORT ||
6984 subevent == HCI_EV_LE_DIRECT_ADV_REPORT ||
6985 subevent == HCI_EV_LE_EXT_ADV_REPORT) &&
6986 num_reports) {
6987 adv = (void *)(ptr + 1);
6988 direct_adv = (void *)(ptr + 1);
6989 ext_adv = (void *)(ptr + 1);
6990
6991 switch (subevent) {
6992 case HCI_EV_LE_ADVERTISING_REPORT:
6993 bacpy(&hdev->wake_addr, &adv->bdaddr);
6994 hdev->wake_addr_type = adv->bdaddr_type;
6995 break;
6996 case HCI_EV_LE_DIRECT_ADV_REPORT:
6997 bacpy(&hdev->wake_addr, &direct_adv->bdaddr);
6998 hdev->wake_addr_type = direct_adv->bdaddr_type;
6999 break;
7000 case HCI_EV_LE_EXT_ADV_REPORT:
7001 bacpy(&hdev->wake_addr, &ext_adv->bdaddr);
7002 hdev->wake_addr_type = ext_adv->bdaddr_type;
7003 break;
7004 }
7005 }
7006 } else {
7007 hdev->wake_reason = MGMT_WAKE_REASON_UNEXPECTED;
7008 }
7009
7010unlock:
7011 hci_dev_unlock(hdev);
7012}
7013
Linus Torvalds1da177e2005-04-16 15:20:36 -07007014void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
7015{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007016 struct hci_event_hdr *hdr = (void *) skb->data;
Johan Hedberge62144872015-04-02 13:41:08 +03007017 hci_req_complete_t req_complete = NULL;
7018 hci_req_complete_skb_t req_complete_skb = NULL;
7019 struct sk_buff *orig_skb = NULL;
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08007020 u8 status = 0, event, req_evt = 0;
Johan Hedberge62144872015-04-02 13:41:08 +03007021 u16 opcode = HCI_OP_NOP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007022
Luiz Augusto von Dentze3f3a1a2021-12-01 10:54:54 -08007023 if (skb->len < sizeof(*hdr)) {
7024 bt_dev_err(hdev, "Malformed HCI Event");
7025 goto done;
7026 }
7027
7028 event = hdr->evt;
Alain Michaud08bb4da2020-03-03 15:55:34 +00007029 if (!event) {
7030 bt_dev_warn(hdev, "Received unexpected HCI Event 00000000");
7031 goto done;
7032 }
7033
Marcel Holtmann242c0eb2015-10-25 22:45:53 +01007034 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->hci.req_event == event) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02007035 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
Johan Hedberge62144872015-04-02 13:41:08 +03007036 opcode = __le16_to_cpu(cmd_hdr->opcode);
7037 hci_req_cmd_complete(hdev, opcode, status, &req_complete,
7038 &req_complete_skb);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03007039 req_evt = event;
Johan Hedberg02350a72013-04-03 21:50:29 +03007040 }
7041
Johan Hedberge62144872015-04-02 13:41:08 +03007042 /* If it looks like we might end up having to call
7043 * req_complete_skb, store a pristine copy of the skb since the
7044 * various handlers may modify the original one through
7045 * skb_pull() calls, etc.
7046 */
7047 if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
7048 event == HCI_EV_CMD_COMPLETE)
7049 orig_skb = skb_clone(skb, GFP_KERNEL);
7050
7051 skb_pull(skb, HCI_EVENT_HDR_SIZE);
7052
Abhishek Pandit-Subedi2f202162020-09-11 14:07:13 -07007053 /* Store wake reason if we're suspended */
7054 hci_store_wake_reason(hdev, event, skb);
7055
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007056 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07007057 case HCI_EV_INQUIRY_COMPLETE:
7058 hci_inquiry_complete_evt(hdev, skb);
7059 break;
7060
7061 case HCI_EV_INQUIRY_RESULT:
7062 hci_inquiry_result_evt(hdev, skb);
7063 break;
7064
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007065 case HCI_EV_CONN_COMPLETE:
7066 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02007067 break;
7068
Linus Torvalds1da177e2005-04-16 15:20:36 -07007069 case HCI_EV_CONN_REQUEST:
7070 hci_conn_request_evt(hdev, skb);
7071 break;
7072
Linus Torvalds1da177e2005-04-16 15:20:36 -07007073 case HCI_EV_DISCONN_COMPLETE:
7074 hci_disconn_complete_evt(hdev, skb);
7075 break;
7076
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077 case HCI_EV_AUTH_COMPLETE:
7078 hci_auth_complete_evt(hdev, skb);
7079 break;
7080
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007081 case HCI_EV_REMOTE_NAME:
7082 hci_remote_name_evt(hdev, skb);
7083 break;
7084
Linus Torvalds1da177e2005-04-16 15:20:36 -07007085 case HCI_EV_ENCRYPT_CHANGE:
7086 hci_encrypt_change_evt(hdev, skb);
7087 break;
7088
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007089 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
7090 hci_change_link_key_complete_evt(hdev, skb);
7091 break;
7092
7093 case HCI_EV_REMOTE_FEATURES:
7094 hci_remote_features_evt(hdev, skb);
7095 break;
7096
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007097 case HCI_EV_CMD_COMPLETE:
Johan Hedberge62144872015-04-02 13:41:08 +03007098 hci_cmd_complete_evt(hdev, skb, &opcode, &status,
7099 &req_complete, &req_complete_skb);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007100 break;
7101
7102 case HCI_EV_CMD_STATUS:
Johan Hedberge62144872015-04-02 13:41:08 +03007103 hci_cmd_status_evt(hdev, skb, &opcode, &status, &req_complete,
7104 &req_complete_skb);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007105 break;
7106
Marcel Holtmann24dfa342014-11-02 02:56:41 +01007107 case HCI_EV_HARDWARE_ERROR:
7108 hci_hardware_error_evt(hdev, skb);
7109 break;
7110
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007111 case HCI_EV_ROLE_CHANGE:
7112 hci_role_change_evt(hdev, skb);
7113 break;
7114
7115 case HCI_EV_NUM_COMP_PKTS:
7116 hci_num_comp_pkts_evt(hdev, skb);
7117 break;
7118
7119 case HCI_EV_MODE_CHANGE:
7120 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007121 break;
7122
7123 case HCI_EV_PIN_CODE_REQ:
7124 hci_pin_code_request_evt(hdev, skb);
7125 break;
7126
7127 case HCI_EV_LINK_KEY_REQ:
7128 hci_link_key_request_evt(hdev, skb);
7129 break;
7130
7131 case HCI_EV_LINK_KEY_NOTIFY:
7132 hci_link_key_notify_evt(hdev, skb);
7133 break;
7134
7135 case HCI_EV_CLOCK_OFFSET:
7136 hci_clock_offset_evt(hdev, skb);
7137 break;
7138
Marcel Holtmanna8746412008-07-14 20:13:46 +02007139 case HCI_EV_PKT_TYPE_CHANGE:
7140 hci_pkt_type_change_evt(hdev, skb);
7141 break;
7142
Marcel Holtmann85a1e932005-08-09 20:28:02 -07007143 case HCI_EV_PSCAN_REP_MODE:
7144 hci_pscan_rep_mode_evt(hdev, skb);
7145 break;
7146
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007147 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
7148 hci_inquiry_result_with_rssi_evt(hdev, skb);
7149 break;
7150
7151 case HCI_EV_REMOTE_EXT_FEATURES:
7152 hci_remote_ext_features_evt(hdev, skb);
7153 break;
7154
7155 case HCI_EV_SYNC_CONN_COMPLETE:
7156 hci_sync_conn_complete_evt(hdev, skb);
7157 break;
7158
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007159 case HCI_EV_EXTENDED_INQUIRY_RESULT:
7160 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161 break;
7162
Johan Hedberg1c2e0042012-06-08 23:31:13 +08007163 case HCI_EV_KEY_REFRESH_COMPLETE:
7164 hci_key_refresh_complete_evt(hdev, skb);
7165 break;
7166
Marcel Holtmann04936842008-07-14 20:13:48 +02007167 case HCI_EV_IO_CAPA_REQUEST:
7168 hci_io_capa_request_evt(hdev, skb);
7169 break;
7170
Johan Hedberg03b555e2011-01-04 15:40:05 +02007171 case HCI_EV_IO_CAPA_REPLY:
7172 hci_io_capa_reply_evt(hdev, skb);
7173 break;
7174
Johan Hedberga5c29682011-02-19 12:05:57 -03007175 case HCI_EV_USER_CONFIRM_REQUEST:
7176 hci_user_confirm_request_evt(hdev, skb);
7177 break;
7178
Brian Gix1143d452011-11-23 08:28:34 -08007179 case HCI_EV_USER_PASSKEY_REQUEST:
7180 hci_user_passkey_request_evt(hdev, skb);
7181 break;
7182
Johan Hedberg92a25252012-09-06 18:39:26 +03007183 case HCI_EV_USER_PASSKEY_NOTIFY:
7184 hci_user_passkey_notify_evt(hdev, skb);
7185 break;
7186
7187 case HCI_EV_KEYPRESS_NOTIFY:
7188 hci_keypress_notify_evt(hdev, skb);
7189 break;
7190
Marcel Holtmann04936842008-07-14 20:13:48 +02007191 case HCI_EV_SIMPLE_PAIR_COMPLETE:
7192 hci_simple_pair_complete_evt(hdev, skb);
7193 break;
7194
Marcel Holtmann41a96212008-07-14 20:13:48 +02007195 case HCI_EV_REMOTE_HOST_FEATURES:
7196 hci_remote_host_features_evt(hdev, skb);
7197 break;
7198
Ville Tervofcd89c02011-02-10 22:38:47 -03007199 case HCI_EV_LE_META:
7200 hci_le_meta_evt(hdev, skb);
7201 break;
7202
Szymon Janc2763eda2011-03-22 13:12:22 +01007203 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
7204 hci_remote_oob_data_request_evt(hdev, skb);
7205 break;
7206
Arron Wanga77a6a12015-07-24 17:13:15 +08007207#if IS_ENABLED(CONFIG_BT_HS)
7208 case HCI_EV_CHANNEL_SELECTED:
7209 hci_chan_selected_evt(hdev, skb);
7210 break;
7211
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03007212 case HCI_EV_PHY_LINK_COMPLETE:
7213 hci_phy_link_complete_evt(hdev, skb);
7214 break;
7215
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03007216 case HCI_EV_LOGICAL_LINK_COMPLETE:
7217 hci_loglink_complete_evt(hdev, skb);
7218 break;
7219
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02007220 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
7221 hci_disconn_loglink_complete_evt(hdev, skb);
7222 break;
7223
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02007224 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
7225 hci_disconn_phylink_complete_evt(hdev, skb);
7226 break;
Arron Wanga77a6a12015-07-24 17:13:15 +08007227#endif
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02007228
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02007229 case HCI_EV_NUM_COMP_BLOCKS:
7230 hci_num_comp_blocks_evt(hdev, skb);
7231 break;
7232
Miao-chen Chou145373c2020-04-03 21:44:01 +02007233 case HCI_EV_VENDOR:
7234 msft_vendor_evt(hdev, skb);
7235 break;
7236
Marcel Holtmanna9de9242007-10-20 13:33:56 +02007237 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03007238 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239 break;
7240 }
7241
Johan Hedberg757aa0b2015-04-02 13:41:12 +03007242 if (req_complete) {
Johan Hedberge62144872015-04-02 13:41:08 +03007243 req_complete(hdev, status, opcode);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03007244 } else if (req_complete_skb) {
7245 if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
7246 kfree_skb(orig_skb);
7247 orig_skb = NULL;
7248 }
Johan Hedberge62144872015-04-02 13:41:08 +03007249 req_complete_skb(hdev, status, opcode, orig_skb);
Johan Hedberg757aa0b2015-04-02 13:41:12 +03007250 }
Johan Hedberge62144872015-04-02 13:41:08 +03007251
Alain Michaud08bb4da2020-03-03 15:55:34 +00007252done:
Johan Hedberge62144872015-04-02 13:41:08 +03007253 kfree_skb(orig_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007254 kfree_skb(skb);
7255 hdev->stat.evt_rx++;
7256}