blob: 38a0e457eaf9eaa9fc8fbd5fa9626ad601d7fa17 [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
Marcel Holtmann70247282013-10-10 14:54:15 -070033#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070034#include "amp.h"
Johan Hedberg2ceba532014-06-16 19:25:16 +030035#include "smp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* Handle HCI Event packets */
38
Marcel Holtmanna9de9242007-10-20 13:33:56 +020039static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020041 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030043 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Andre Guedes82f47852013-04-30 15:29:34 -030045 if (status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andre Guedes89352e72011-11-04 14:16:53 -030048 clear_bit(HCI_INQUIRY, &hdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010049 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -030050 wake_up_bit(&hdev->flags, HCI_INQUIRY);
Andre Guedes89352e72011-11-04 14:16:53 -030051
Johan Hedberg50143a42014-06-10 14:05:57 +030052 hci_dev_lock(hdev);
53 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
54 hci_dev_unlock(hdev);
55
Marcel Holtmanna9de9242007-10-20 13:33:56 +020056 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Andre Guedes4d934832012-03-21 00:03:35 -030059static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
60{
61 __u8 status = *((__u8 *) skb->data);
62
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030063 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesae854a72012-03-21 00:03:36 -030064
65 if (status)
66 return;
67
68 set_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
Andre Guedes4d934832012-03-21 00:03:35 -030069}
70
Marcel Holtmanna9de9242007-10-20 13:33:56 +020071static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020073 __u8 status = *((__u8 *) skb->data);
74
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030075 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +020076
77 if (status)
78 return;
79
Andre Guedesae854a72012-03-21 00:03:36 -030080 clear_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
81
Marcel Holtmanna9de9242007-10-20 13:33:56 +020082 hci_conn_check_pending(hdev);
83}
84
Gustavo Padovan807deac2012-05-17 00:36:24 -030085static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
86 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020087{
88 BT_DBG("%s", hdev->name);
89}
90
91static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
92{
93 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030096 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Marcel Holtmanna9de9242007-10-20 13:33:56 +020098 if (rp->status)
99 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200101 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200103 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
104 if (conn) {
105 if (rp->role)
Johan Hedberg4dae2792014-06-24 17:03:50 +0300106 clear_bit(HCI_CONN_MASTER, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200107 else
Johan Hedberg4dae2792014-06-24 17:03:50 +0300108 set_bit(HCI_CONN_MASTER, &conn->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200110
111 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200114static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
115{
116 struct hci_rp_read_link_policy *rp = (void *) skb->data;
117 struct hci_conn *conn;
118
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300119 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200120
121 if (rp->status)
122 return;
123
124 hci_dev_lock(hdev);
125
126 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
127 if (conn)
128 conn->link_policy = __le16_to_cpu(rp->policy);
129
130 hci_dev_unlock(hdev);
131}
132
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200133static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200135 struct hci_rp_write_link_policy *rp = (void *) skb->data;
136 struct hci_conn *conn;
137 void *sent;
138
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300139 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200140
141 if (rp->status)
142 return;
143
144 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
145 if (!sent)
146 return;
147
148 hci_dev_lock(hdev);
149
150 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200151 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700152 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200153
154 hci_dev_unlock(hdev);
155}
156
Gustavo Padovan807deac2012-05-17 00:36:24 -0300157static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
158 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200159{
160 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
161
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300162 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200163
164 if (rp->status)
165 return;
166
167 hdev->link_policy = __le16_to_cpu(rp->policy);
168}
169
Gustavo Padovan807deac2012-05-17 00:36:24 -0300170static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
171 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200172{
173 __u8 status = *((__u8 *) skb->data);
174 void *sent;
175
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300176 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200177
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200178 if (status)
179 return;
180
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200181 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
182 if (!sent)
183 return;
184
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200185 hdev->link_policy = get_unaligned_le16(sent);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200186}
187
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200188static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
189{
190 __u8 status = *((__u8 *) skb->data);
191
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300192 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200193
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300194 clear_bit(HCI_RESET, &hdev->flags);
195
Johan Hedberga297e972012-02-21 17:55:47 +0200196 /* Reset all non-persistent flags */
Johan Hedberg2cc6fb02013-03-15 17:06:57 -0500197 hdev->dev_flags &= ~HCI_PERSISTENT_MASK;
Andre Guedes69775ff2012-02-23 16:50:05 +0200198
199 hdev->discovery.state = DISCOVERY_STOPPED;
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100200 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
201 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
Johan Hedberg3f0f5242012-11-08 01:23:00 +0100202
203 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
204 hdev->adv_data_len = 0;
Marcel Holtmannf8e808b2013-10-16 00:16:47 -0700205
206 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
207 hdev->scan_rsp_data_len = 0;
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700208
Marcel Holtmann533553f2014-03-21 12:18:10 -0700209 hdev->le_scan_type = LE_SCAN_PASSIVE;
210
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700211 hdev->ssp_debug_mode = 0;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200212}
213
214static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
215{
216 __u8 status = *((__u8 *) skb->data);
217 void *sent;
218
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300219 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200220
221 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
222 if (!sent)
223 return;
224
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200225 hci_dev_lock(hdev);
226
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200227 if (test_bit(HCI_MGMT, &hdev->dev_flags))
228 mgmt_set_local_name_complete(hdev, sent, status);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +0200229 else if (!status)
230 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200231
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200232 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200233}
234
235static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
236{
237 struct hci_rp_read_local_name *rp = (void *) skb->data;
238
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300239 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200240
241 if (rp->status)
242 return;
243
Johan Hedbergdb99b5f2012-02-22 20:14:22 +0200244 if (test_bit(HCI_SETUP, &hdev->dev_flags))
245 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200246}
247
248static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
249{
250 __u8 status = *((__u8 *) skb->data);
251 void *sent;
252
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300253 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200254
255 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
256 if (!sent)
257 return;
258
259 if (!status) {
260 __u8 param = *((__u8 *) sent);
261
262 if (param == AUTH_ENABLED)
263 set_bit(HCI_AUTH, &hdev->flags);
264 else
265 clear_bit(HCI_AUTH, &hdev->flags);
266 }
267
Johan Hedberg33ef95e2012-02-16 23:56:27 +0200268 if (test_bit(HCI_MGMT, &hdev->dev_flags))
269 mgmt_auth_enable_complete(hdev, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200270}
271
272static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
273{
274 __u8 status = *((__u8 *) skb->data);
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200275 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200276 void *sent;
277
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300278 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200279
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200280 if (status)
281 return;
282
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200283 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
284 if (!sent)
285 return;
286
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200287 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200288
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200289 if (param)
290 set_bit(HCI_ENCRYPT, &hdev->flags);
291 else
292 clear_bit(HCI_ENCRYPT, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200293}
294
295static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
296{
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200297 __u8 status = *((__u8 *) skb->data);
298 __u8 param;
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200299 int old_pscan, old_iscan;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200300 void *sent;
301
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300302 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200303
304 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
305 if (!sent)
306 return;
307
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200308 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200309
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200310 hci_dev_lock(hdev);
311
Mikel Astizfa1bd912012-08-09 09:52:29 +0200312 if (status) {
Johan Hedberg744cf192011-11-08 20:40:14 +0200313 mgmt_write_scan_failed(hdev, param, status);
Johan Hedberg2d7cee52011-11-07 22:16:03 +0200314 hdev->discov_timeout = 0;
315 goto done;
316 }
317
Johan Hedberg0663ca22013-10-02 13:43:14 +0300318 /* We need to ensure that we set this back on if someone changed
319 * the scan mode through a raw HCI socket.
320 */
321 set_bit(HCI_BREDR_ENABLED, &hdev->dev_flags);
322
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200323 old_pscan = test_and_clear_bit(HCI_PSCAN, &hdev->flags);
324 old_iscan = test_and_clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200325
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200326 if (param & SCAN_INQUIRY) {
327 set_bit(HCI_ISCAN, &hdev->flags);
328 if (!old_iscan)
Johan Hedberg744cf192011-11-08 20:40:14 +0200329 mgmt_discoverable(hdev, 1);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200330 } else if (old_iscan)
Johan Hedberg744cf192011-11-08 20:40:14 +0200331 mgmt_discoverable(hdev, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200332
Johan Hedberg031547d2014-07-10 12:09:06 +0300333 if (param & SCAN_PAGE)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200334 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200335
336done:
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200337 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200338}
339
340static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
341{
342 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
343
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300344 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200345
346 if (rp->status)
347 return;
348
349 memcpy(hdev->dev_class, rp->dev_class, 3);
350
351 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300352 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200353}
354
355static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
356{
357 __u8 status = *((__u8 *) skb->data);
358 void *sent;
359
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300360 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200361
362 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
363 if (!sent)
364 return;
365
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100366 hci_dev_lock(hdev);
367
368 if (status == 0)
369 memcpy(hdev->dev_class, sent, 3);
370
371 if (test_bit(HCI_MGMT, &hdev->dev_flags))
372 mgmt_set_class_of_dev_complete(hdev, sent, status);
373
374 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200375}
376
377static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
378{
379 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200381
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300382 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200383
384 if (rp->status)
385 return;
386
387 setting = __le16_to_cpu(rp->voice_setting);
388
Marcel Holtmannf383f272008-07-14 20:13:47 +0200389 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200390 return;
391
392 hdev->voice_setting = setting;
393
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300394 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200395
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200396 if (hdev->notify)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200397 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200398}
399
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300400static void hci_cc_write_voice_setting(struct hci_dev *hdev,
401 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200402{
403 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200404 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 void *sent;
406
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300407 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Marcel Holtmannf383f272008-07-14 20:13:47 +0200409 if (status)
410 return;
411
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200412 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
413 if (!sent)
414 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Marcel Holtmannf383f272008-07-14 20:13:47 +0200416 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Marcel Holtmannf383f272008-07-14 20:13:47 +0200418 if (hdev->voice_setting == setting)
419 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Marcel Holtmannf383f272008-07-14 20:13:47 +0200421 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300423 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200424
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200425 if (hdev->notify)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200426 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700429static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
430 struct sk_buff *skb)
431{
432 struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
433
434 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
435
436 if (rp->status)
437 return;
438
439 hdev->num_iac = rp->num_iac;
440
441 BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
442}
443
Marcel Holtmann333140b2008-07-14 20:13:48 +0200444static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
445{
446 __u8 status = *((__u8 *) skb->data);
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300447 struct hci_cp_write_ssp_mode *sent;
Marcel Holtmann333140b2008-07-14 20:13:48 +0200448
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300449 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200450
Marcel Holtmann333140b2008-07-14 20:13:48 +0200451 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
452 if (!sent)
453 return;
454
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300455 if (!status) {
456 if (sent->mode)
Johan Hedbergcad718e2013-04-17 15:00:51 +0300457 hdev->features[1][0] |= LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300458 else
Johan Hedbergcad718e2013-04-17 15:00:51 +0300459 hdev->features[1][0] &= ~LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300460 }
461
Johan Hedberged2c4ee2012-02-17 00:56:28 +0200462 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300463 mgmt_ssp_enable_complete(hdev, sent->mode, status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200464 else if (!status) {
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300465 if (sent->mode)
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200466 set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
467 else
468 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
469 }
Marcel Holtmann333140b2008-07-14 20:13:48 +0200470}
471
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800472static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
473{
474 u8 status = *((u8 *) skb->data);
475 struct hci_cp_write_sc_support *sent;
476
477 BT_DBG("%s status 0x%2.2x", hdev->name, status);
478
479 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
480 if (!sent)
481 return;
482
483 if (!status) {
484 if (sent->support)
485 hdev->features[1][0] |= LMP_HOST_SC;
486 else
487 hdev->features[1][0] &= ~LMP_HOST_SC;
488 }
489
490 if (test_bit(HCI_MGMT, &hdev->dev_flags))
491 mgmt_sc_enable_complete(hdev, sent->support, status);
492 else if (!status) {
493 if (sent->support)
494 set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
495 else
496 clear_bit(HCI_SC_ENABLED, &hdev->dev_flags);
497 }
498}
499
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200500static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
501{
502 struct hci_rp_read_local_version *rp = (void *) skb->data;
503
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300504 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200505
506 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200507 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200508
Marcel Holtmann0d5551f2013-10-18 12:04:50 -0700509 if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
510 hdev->hci_ver = rp->hci_ver;
511 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
512 hdev->lmp_ver = rp->lmp_ver;
513 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
514 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
515 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200516}
517
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300518static void hci_cc_read_local_commands(struct hci_dev *hdev,
519 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200520{
521 struct hci_rp_read_local_commands *rp = (void *) skb->data;
522
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300523 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200524
Marcel Holtmann6a070e62013-10-31 04:54:33 -0700525 if (rp->status)
526 return;
527
528 if (test_bit(HCI_SETUP, &hdev->dev_flags))
Johan Hedberg2177bab2013-03-05 20:37:43 +0200529 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200530}
531
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300532static void hci_cc_read_local_features(struct hci_dev *hdev,
533 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200534{
535 struct hci_rp_read_local_features *rp = (void *) skb->data;
536
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300537 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200538
539 if (rp->status)
540 return;
541
542 memcpy(hdev->features, rp->features, 8);
543
544 /* Adjust default settings according to features
545 * supported by device. */
546
Johan Hedbergcad718e2013-04-17 15:00:51 +0300547 if (hdev->features[0][0] & LMP_3SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200548 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
549
Johan Hedbergcad718e2013-04-17 15:00:51 +0300550 if (hdev->features[0][0] & LMP_5SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200551 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
552
Johan Hedbergcad718e2013-04-17 15:00:51 +0300553 if (hdev->features[0][1] & LMP_HV2) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200554 hdev->pkt_type |= (HCI_HV2);
555 hdev->esco_type |= (ESCO_HV2);
556 }
557
Johan Hedbergcad718e2013-04-17 15:00:51 +0300558 if (hdev->features[0][1] & LMP_HV3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200559 hdev->pkt_type |= (HCI_HV3);
560 hdev->esco_type |= (ESCO_HV3);
561 }
562
Andre Guedes45db810f2012-07-24 15:03:49 -0300563 if (lmp_esco_capable(hdev))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200564 hdev->esco_type |= (ESCO_EV3);
565
Johan Hedbergcad718e2013-04-17 15:00:51 +0300566 if (hdev->features[0][4] & LMP_EV4)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200567 hdev->esco_type |= (ESCO_EV4);
568
Johan Hedbergcad718e2013-04-17 15:00:51 +0300569 if (hdev->features[0][4] & LMP_EV5)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200570 hdev->esco_type |= (ESCO_EV5);
571
Johan Hedbergcad718e2013-04-17 15:00:51 +0300572 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100573 hdev->esco_type |= (ESCO_2EV3);
574
Johan Hedbergcad718e2013-04-17 15:00:51 +0300575 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100576 hdev->esco_type |= (ESCO_3EV3);
577
Johan Hedbergcad718e2013-04-17 15:00:51 +0300578 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100579 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200580}
581
Andre Guedes971e3a42011-06-30 19:20:52 -0300582static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300583 struct sk_buff *skb)
Andre Guedes971e3a42011-06-30 19:20:52 -0300584{
585 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
586
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300587 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andre Guedes971e3a42011-06-30 19:20:52 -0300588
589 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200590 return;
Andre Guedes971e3a42011-06-30 19:20:52 -0300591
Marcel Holtmann57af75a2013-10-18 12:04:47 -0700592 if (hdev->max_page < rp->max_page)
593 hdev->max_page = rp->max_page;
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300594
Johan Hedbergcad718e2013-04-17 15:00:51 +0300595 if (rp->page < HCI_MAX_PAGES)
596 memcpy(hdev->features[rp->page], rp->features, 8);
Andre Guedes971e3a42011-06-30 19:20:52 -0300597}
598
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200599static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300600 struct sk_buff *skb)
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200601{
602 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
603
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300604 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200605
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200606 if (rp->status)
607 return;
608
609 hdev->flow_ctl_mode = rp->mode;
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200610}
611
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200612static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
613{
614 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
615
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300616 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200617
618 if (rp->status)
619 return;
620
621 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
622 hdev->sco_mtu = rp->sco_mtu;
623 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
624 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
625
626 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
627 hdev->sco_mtu = 64;
628 hdev->sco_pkts = 8;
629 }
630
631 hdev->acl_cnt = hdev->acl_pkts;
632 hdev->sco_cnt = hdev->sco_pkts;
633
Gustavo Padovan807deac2012-05-17 00:36:24 -0300634 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
635 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200636}
637
638static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
639{
640 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
641
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300642 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200643
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200644 if (rp->status)
645 return;
646
647 if (test_bit(HCI_INIT, &hdev->flags))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200648 bacpy(&hdev->bdaddr, &rp->bdaddr);
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200649
650 if (test_bit(HCI_SETUP, &hdev->dev_flags))
651 bacpy(&hdev->setup_addr, &rp->bdaddr);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200652}
653
Johan Hedbergf332ec62013-03-15 17:07:11 -0500654static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
655 struct sk_buff *skb)
656{
657 struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
658
659 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
660
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200661 if (rp->status)
662 return;
663
664 if (test_bit(HCI_INIT, &hdev->flags)) {
Johan Hedbergf332ec62013-03-15 17:07:11 -0500665 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
666 hdev->page_scan_window = __le16_to_cpu(rp->window);
667 }
668}
669
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500670static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
671 struct sk_buff *skb)
672{
673 u8 status = *((u8 *) skb->data);
674 struct hci_cp_write_page_scan_activity *sent;
675
676 BT_DBG("%s status 0x%2.2x", hdev->name, status);
677
678 if (status)
679 return;
680
681 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
682 if (!sent)
683 return;
684
685 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
686 hdev->page_scan_window = __le16_to_cpu(sent->window);
687}
688
Johan Hedbergf332ec62013-03-15 17:07:11 -0500689static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
690 struct sk_buff *skb)
691{
692 struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
693
694 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
695
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200696 if (rp->status)
697 return;
698
699 if (test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergf332ec62013-03-15 17:07:11 -0500700 hdev->page_scan_type = rp->type;
701}
702
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500703static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
704 struct sk_buff *skb)
705{
706 u8 status = *((u8 *) skb->data);
707 u8 *type;
708
709 BT_DBG("%s status 0x%2.2x", hdev->name, status);
710
711 if (status)
712 return;
713
714 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
715 if (type)
716 hdev->page_scan_type = *type;
717}
718
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200719static void hci_cc_read_data_block_size(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300720 struct sk_buff *skb)
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200721{
722 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
723
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300724 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200725
726 if (rp->status)
727 return;
728
729 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
730 hdev->block_len = __le16_to_cpu(rp->block_len);
731 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
732
733 hdev->block_cnt = hdev->num_blocks;
734
735 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300736 hdev->block_cnt, hdev->block_len);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200737}
738
Johan Hedberg33f35722014-06-28 17:54:06 +0300739static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
740{
741 struct hci_rp_read_clock *rp = (void *) skb->data;
742 struct hci_cp_read_clock *cp;
743 struct hci_conn *conn;
744
745 BT_DBG("%s", hdev->name);
746
747 if (skb->len < sizeof(*rp))
748 return;
749
750 if (rp->status)
751 return;
752
753 hci_dev_lock(hdev);
754
755 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
756 if (!cp)
757 goto unlock;
758
759 if (cp->which == 0x00) {
760 hdev->clock = le32_to_cpu(rp->clock);
761 goto unlock;
762 }
763
764 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
765 if (conn) {
766 conn->clock = le32_to_cpu(rp->clock);
767 conn->clock_accuracy = le16_to_cpu(rp->accuracy);
768 }
769
770unlock:
771 hci_dev_unlock(hdev);
772}
773
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300774static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300775 struct sk_buff *skb)
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300776{
777 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
778
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300779 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300780
781 if (rp->status)
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300782 goto a2mp_rsp;
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300783
784 hdev->amp_status = rp->amp_status;
785 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
786 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
787 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
788 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
789 hdev->amp_type = rp->amp_type;
790 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
791 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
792 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
793 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
794
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300795a2mp_rsp:
796 a2mp_send_getinfo_rsp(hdev);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300797}
798
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300799static void hci_cc_read_local_amp_assoc(struct hci_dev *hdev,
800 struct sk_buff *skb)
801{
802 struct hci_rp_read_local_amp_assoc *rp = (void *) skb->data;
803 struct amp_assoc *assoc = &hdev->loc_assoc;
804 size_t rem_len, frag_len;
805
806 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
807
808 if (rp->status)
809 goto a2mp_rsp;
810
811 frag_len = skb->len - sizeof(*rp);
812 rem_len = __le16_to_cpu(rp->rem_len);
813
814 if (rem_len > frag_len) {
Andrei Emeltchenko2e430be32012-09-28 14:44:23 +0300815 BT_DBG("frag_len %zu rem_len %zu", frag_len, rem_len);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300816
817 memcpy(assoc->data + assoc->offset, rp->frag, frag_len);
818 assoc->offset += frag_len;
819
820 /* Read other fragments */
821 amp_read_loc_assoc_frag(hdev, rp->phy_handle);
822
823 return;
824 }
825
826 memcpy(assoc->data + assoc->offset, rp->frag, rem_len);
827 assoc->len = assoc->offset + rem_len;
828 assoc->offset = 0;
829
830a2mp_rsp:
831 /* Send A2MP Rsp when all fragments are received */
832 a2mp_send_getampassoc_rsp(hdev, rp->status);
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300833 a2mp_send_create_phy_link_req(hdev, rp->status);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300834}
835
Johan Hedbergd5859e22011-01-25 01:19:58 +0200836static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300837 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200838{
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700839 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200840
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300841 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200842
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200843 if (rp->status)
844 return;
845
846 hdev->inq_tx_power = rp->tx_power;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200847}
848
Johan Hedberg980e1a52011-01-22 06:10:07 +0200849static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
850{
851 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
852 struct hci_cp_pin_code_reply *cp;
853 struct hci_conn *conn;
854
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300855 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200856
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200857 hci_dev_lock(hdev);
858
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200859 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200860 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200861
Mikel Astizfa1bd912012-08-09 09:52:29 +0200862 if (rp->status)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200863 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200864
865 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
866 if (!cp)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200867 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200868
869 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
870 if (conn)
871 conn->pin_length = cp->pin_len;
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200872
873unlock:
874 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200875}
876
877static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
878{
879 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
880
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300881 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200882
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200883 hci_dev_lock(hdev);
884
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200885 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200886 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300887 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200888
889 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200890}
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200891
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300892static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
893 struct sk_buff *skb)
894{
895 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
896
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300897 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300898
899 if (rp->status)
900 return;
901
902 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
903 hdev->le_pkts = rp->le_max_pkt;
904
905 hdev->le_cnt = hdev->le_pkts;
906
907 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300908}
Johan Hedberg980e1a52011-01-22 06:10:07 +0200909
Johan Hedberg60e77322013-01-22 14:01:59 +0200910static void hci_cc_le_read_local_features(struct hci_dev *hdev,
911 struct sk_buff *skb)
912{
913 struct hci_rp_le_read_local_features *rp = (void *) skb->data;
914
915 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
916
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200917 if (rp->status)
918 return;
919
920 memcpy(hdev->le_features, rp->features, 8);
Johan Hedberg60e77322013-01-22 14:01:59 +0200921}
922
Johan Hedberg8fa19092012-10-19 20:57:49 +0300923static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
924 struct sk_buff *skb)
925{
926 struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
927
928 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
929
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200930 if (rp->status)
931 return;
932
933 hdev->adv_tx_power = rp->tx_power;
Johan Hedberg8fa19092012-10-19 20:57:49 +0300934}
935
Johan Hedberga5c29682011-02-19 12:05:57 -0300936static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
937{
938 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
939
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300940 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300941
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200942 hci_dev_lock(hdev);
943
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200944 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300945 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
946 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200947
948 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300949}
950
951static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300952 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -0300953{
954 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
955
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300956 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300957
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200958 hci_dev_lock(hdev);
959
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200960 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200961 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300962 ACL_LINK, 0, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200963
964 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300965}
966
Brian Gix1143d452011-11-23 08:28:34 -0800967static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
968{
969 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
970
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300971 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800972
973 hci_dev_lock(hdev);
974
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200975 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +0200976 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300977 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800978
979 hci_dev_unlock(hdev);
980}
981
982static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300983 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -0800984{
985 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
986
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300987 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800988
989 hci_dev_lock(hdev);
990
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200991 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Brian Gix1143d452011-11-23 08:28:34 -0800992 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300993 ACL_LINK, 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800994
995 hci_dev_unlock(hdev);
996}
997
Marcel Holtmann4d2d2792014-01-10 02:07:26 -0800998static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
999 struct sk_buff *skb)
Szymon Jancc35938b2011-03-22 13:12:21 +01001000{
1001 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
1002
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001003 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Szymon Jancc35938b2011-03-22 13:12:21 +01001004
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001005 hci_dev_lock(hdev);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001006 mgmt_read_local_oob_data_complete(hdev, rp->hash, rp->randomizer,
1007 NULL, NULL, rp->status);
1008 hci_dev_unlock(hdev);
1009}
1010
1011static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
1012 struct sk_buff *skb)
1013{
1014 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
1015
1016 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1017
1018 hci_dev_lock(hdev);
1019 mgmt_read_local_oob_data_complete(hdev, rp->hash192, rp->randomizer192,
1020 rp->hash256, rp->randomizer256,
1021 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001022 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01001023}
1024
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001025
1026static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
1027{
1028 __u8 status = *((__u8 *) skb->data);
1029 bdaddr_t *sent;
1030
1031 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1032
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001033 if (status)
1034 return;
1035
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001036 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1037 if (!sent)
1038 return;
1039
1040 hci_dev_lock(hdev);
1041
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001042 bacpy(&hdev->random_addr, sent);
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001043
1044 hci_dev_unlock(hdev);
1045}
1046
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001047static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1048{
1049 __u8 *sent, status = *((__u8 *) skb->data);
1050
1051 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1052
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001053 if (status)
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001054 return;
1055
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001056 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1057 if (!sent)
Johan Hedberg3c857752014-03-25 10:30:49 +02001058 return;
1059
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001060 hci_dev_lock(hdev);
1061
Johan Hedberg3c857752014-03-25 10:30:49 +02001062 /* If we're doing connection initation as peripheral. Set a
1063 * timeout in case something goes wrong.
1064 */
1065 if (*sent) {
1066 struct hci_conn *conn;
1067
Johan Hedberg66c417c2014-07-08 15:07:47 +03001068 set_bit(HCI_LE_ADV, &hdev->dev_flags);
1069
Johan Hedberg3c857752014-03-25 10:30:49 +02001070 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
1071 if (conn)
1072 queue_delayed_work(hdev->workqueue,
1073 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001074 conn->conn_timeout);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001075 } else {
1076 clear_bit(HCI_LE_ADV, &hdev->dev_flags);
Johan Hedberg3c857752014-03-25 10:30:49 +02001077 }
1078
Johan Hedberg04b4edc2013-03-15 17:07:01 -05001079 hci_dev_unlock(hdev);
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001080}
1081
Marcel Holtmann533553f2014-03-21 12:18:10 -07001082static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1083{
1084 struct hci_cp_le_set_scan_param *cp;
1085 __u8 status = *((__u8 *) skb->data);
1086
1087 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1088
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001089 if (status)
1090 return;
1091
Marcel Holtmann533553f2014-03-21 12:18:10 -07001092 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1093 if (!cp)
1094 return;
1095
1096 hci_dev_lock(hdev);
1097
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001098 hdev->le_scan_type = cp->type;
Marcel Holtmann533553f2014-03-21 12:18:10 -07001099
1100 hci_dev_unlock(hdev);
1101}
1102
Johan Hedbergb9a63282014-03-25 10:51:52 +02001103static bool has_pending_adv_report(struct hci_dev *hdev)
1104{
1105 struct discovery_state *d = &hdev->discovery;
1106
1107 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1108}
1109
1110static void clear_pending_adv_report(struct hci_dev *hdev)
1111{
1112 struct discovery_state *d = &hdev->discovery;
1113
1114 bacpy(&d->last_adv_addr, BDADDR_ANY);
1115 d->last_adv_data_len = 0;
1116}
1117
1118static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001119 u8 bdaddr_type, s8 rssi, u32 flags,
1120 u8 *data, u8 len)
Johan Hedbergb9a63282014-03-25 10:51:52 +02001121{
1122 struct discovery_state *d = &hdev->discovery;
1123
1124 bacpy(&d->last_adv_addr, bdaddr);
1125 d->last_adv_addr_type = bdaddr_type;
Johan Hedbergff5cd292014-03-25 14:40:52 +02001126 d->last_adv_rssi = rssi;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001127 d->last_adv_flags = flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02001128 memcpy(d->last_adv_data, data, len);
1129 d->last_adv_data_len = len;
1130}
1131
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001132static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001133 struct sk_buff *skb)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001134{
1135 struct hci_cp_le_set_scan_enable *cp;
1136 __u8 status = *((__u8 *) skb->data);
1137
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001138 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001139
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001140 if (status)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001141 return;
1142
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001143 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1144 if (!cp)
Andre Guedes3fd319b2013-04-30 15:29:36 -03001145 return;
1146
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001147 switch (cp->enable) {
Andre Guedes76a388b2013-04-04 20:21:02 -03001148 case LE_SCAN_ENABLE:
Andre Guedesd23264a2011-11-25 20:53:38 -03001149 set_bit(HCI_LE_SCAN, &hdev->dev_flags);
Johan Hedbergb9a63282014-03-25 10:51:52 +02001150 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1151 clear_pending_adv_report(hdev);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001152 break;
1153
Andre Guedes76a388b2013-04-04 20:21:02 -03001154 case LE_SCAN_DISABLE:
Johan Hedbergb9a63282014-03-25 10:51:52 +02001155 /* We do this here instead of when setting DISCOVERY_STOPPED
1156 * since the latter would potentially require waiting for
1157 * inquiry to stop too.
1158 */
1159 if (has_pending_adv_report(hdev)) {
1160 struct discovery_state *d = &hdev->discovery;
1161
1162 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001163 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001164 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001165 d->last_adv_data,
Johan Hedbergb9a63282014-03-25 10:51:52 +02001166 d->last_adv_data_len, NULL, 0);
1167 }
1168
Johan Hedberg317ac8c2014-02-28 20:26:12 +02001169 /* Cancel this timer so that we don't try to disable scanning
1170 * when it's already disabled.
1171 */
1172 cancel_delayed_work(&hdev->le_scan_disable);
1173
Andre Guedesd23264a2011-11-25 20:53:38 -03001174 clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001175
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001176 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1177 * interrupted scanning due to a connect request. Mark
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001178 * therefore discovery as stopped. If this was not
1179 * because of a connect request advertising might have
1180 * been disabled because of active scanning, so
1181 * re-enable it again if necessary.
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001182 */
1183 if (test_and_clear_bit(HCI_LE_SCAN_INTERRUPTED,
1184 &hdev->dev_flags))
1185 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001186 else if (!test_bit(HCI_LE_ADV, &hdev->dev_flags) &&
Johan Hedberg34722272014-07-08 16:05:05 +03001187 hdev->discovery.state == DISCOVERY_FINDING)
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001188 mgmt_reenable_advertising(hdev);
1189
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001190 break;
1191
1192 default:
1193 BT_ERR("Used reserved LE_Scan_Enable param %d", cp->enable);
1194 break;
Andre Guedes35815082011-05-26 16:23:53 -03001195 }
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001196}
1197
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001198static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
1199 struct sk_buff *skb)
1200{
1201 struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
1202
1203 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1204
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001205 if (rp->status)
1206 return;
1207
1208 hdev->le_white_list_size = rp->size;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001209}
1210
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001211static void hci_cc_le_clear_white_list(struct hci_dev *hdev,
1212 struct sk_buff *skb)
1213{
1214 __u8 status = *((__u8 *) skb->data);
1215
1216 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1217
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001218 if (status)
1219 return;
1220
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001221 hci_bdaddr_list_clear(&hdev->le_white_list);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001222}
1223
1224static void hci_cc_le_add_to_white_list(struct hci_dev *hdev,
1225 struct sk_buff *skb)
1226{
1227 struct hci_cp_le_add_to_white_list *sent;
1228 __u8 status = *((__u8 *) skb->data);
1229
1230 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1231
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001232 if (status)
1233 return;
1234
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001235 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_WHITE_LIST);
1236 if (!sent)
1237 return;
1238
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001239 hci_bdaddr_list_add(&hdev->le_white_list, &sent->bdaddr,
1240 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001241}
1242
1243static void hci_cc_le_del_from_white_list(struct hci_dev *hdev,
1244 struct sk_buff *skb)
1245{
1246 struct hci_cp_le_del_from_white_list *sent;
1247 __u8 status = *((__u8 *) skb->data);
1248
1249 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1250
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001251 if (status)
1252 return;
1253
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001254 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_WHITE_LIST);
1255 if (!sent)
1256 return;
1257
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001258 hci_bdaddr_list_del(&hdev->le_white_list, &sent->bdaddr,
1259 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001260}
1261
Johan Hedberg9b008c02013-01-22 14:02:01 +02001262static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
1263 struct sk_buff *skb)
1264{
1265 struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
1266
1267 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1268
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001269 if (rp->status)
1270 return;
1271
1272 memcpy(hdev->le_states, rp->le_states, 8);
Johan Hedberg9b008c02013-01-22 14:02:01 +02001273}
1274
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001275static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1276 struct sk_buff *skb)
Andre Guedesf9b49302011-06-30 19:20:53 -03001277{
Johan Hedberg06199cf2012-02-22 16:37:11 +02001278 struct hci_cp_write_le_host_supported *sent;
Andre Guedesf9b49302011-06-30 19:20:53 -03001279 __u8 status = *((__u8 *) skb->data);
1280
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001281 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesf9b49302011-06-30 19:20:53 -03001282
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001283 if (status)
1284 return;
1285
Johan Hedberg06199cf2012-02-22 16:37:11 +02001286 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001287 if (!sent)
Andre Guedesf9b49302011-06-30 19:20:53 -03001288 return;
1289
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001290 if (sent->le) {
1291 hdev->features[1][0] |= LMP_HOST_LE;
1292 set_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1293 } else {
1294 hdev->features[1][0] &= ~LMP_HOST_LE;
1295 clear_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1296 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001297 }
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001298
1299 if (sent->simul)
1300 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
1301 else
1302 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
Andre Guedesf9b49302011-06-30 19:20:53 -03001303}
1304
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02001305static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1306{
1307 struct hci_cp_le_set_adv_param *cp;
1308 u8 status = *((u8 *) skb->data);
1309
1310 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1311
1312 if (status)
1313 return;
1314
1315 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
1316 if (!cp)
1317 return;
1318
1319 hci_dev_lock(hdev);
1320 hdev->adv_addr_type = cp->own_address_type;
1321 hci_dev_unlock(hdev);
1322}
1323
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001324static void hci_cc_write_remote_amp_assoc(struct hci_dev *hdev,
1325 struct sk_buff *skb)
1326{
1327 struct hci_rp_write_remote_amp_assoc *rp = (void *) skb->data;
1328
1329 BT_DBG("%s status 0x%2.2x phy_handle 0x%2.2x",
1330 hdev->name, rp->status, rp->phy_handle);
1331
1332 if (rp->status)
1333 return;
1334
1335 amp_write_rem_assoc_continue(hdev, rp->phy_handle);
1336}
1337
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02001338static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
1339{
1340 struct hci_rp_read_rssi *rp = (void *) skb->data;
1341 struct hci_conn *conn;
1342
1343 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1344
1345 if (rp->status)
1346 return;
1347
1348 hci_dev_lock(hdev);
1349
1350 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1351 if (conn)
1352 conn->rssi = rp->rssi;
1353
1354 hci_dev_unlock(hdev);
1355}
1356
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001357static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
1358{
1359 struct hci_cp_read_tx_power *sent;
1360 struct hci_rp_read_tx_power *rp = (void *) skb->data;
1361 struct hci_conn *conn;
1362
1363 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1364
1365 if (rp->status)
1366 return;
1367
1368 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
1369 if (!sent)
1370 return;
1371
1372 hci_dev_lock(hdev);
1373
1374 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001375 if (!conn)
1376 goto unlock;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001377
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001378 switch (sent->type) {
1379 case 0x00:
1380 conn->tx_power = rp->tx_power;
1381 break;
1382 case 0x01:
1383 conn->max_tx_power = rp->tx_power;
1384 break;
1385 }
1386
1387unlock:
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001388 hci_dev_unlock(hdev);
1389}
1390
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001391static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001392{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001393 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001394
1395 if (status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001396 hci_conn_check_pending(hdev);
Johan Hedberg314b2382011-04-27 10:29:57 -04001397 return;
1398 }
1399
Andre Guedes89352e72011-11-04 14:16:53 -03001400 set_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001401}
1402
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001403static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001405 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001408 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001409
1410 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (!cp)
1412 return;
1413
1414 hci_dev_lock(hdev);
1415
1416 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1417
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001418 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 if (status) {
1421 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001422 if (status != 0x0c || conn->attempt > 2) {
1423 conn->state = BT_CLOSED;
1424 hci_proto_connect_cfm(conn, status);
1425 hci_conn_del(conn);
1426 } else
1427 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429 } else {
1430 if (!conn) {
1431 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr);
1432 if (conn) {
Johan Hedberga0c808b2012-01-16 09:49:58 +02001433 conn->out = true;
Johan Hedberg4dae2792014-06-24 17:03:50 +03001434 set_bit(HCI_CONN_MASTER, &conn->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 } else
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001436 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438 }
1439
1440 hci_dev_unlock(hdev);
1441}
1442
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001443static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001445 struct hci_cp_add_sco *cp;
1446 struct hci_conn *acl, *sco;
1447 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001449 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001450
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001451 if (!status)
1452 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001454 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1455 if (!cp)
1456 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001458 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001460 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001461
1462 hci_dev_lock(hdev);
1463
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001464 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001465 if (acl) {
1466 sco = acl->link;
1467 if (sco) {
1468 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001469
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001470 hci_proto_connect_cfm(sco, status);
1471 hci_conn_del(sco);
1472 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001473 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001474
1475 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Marcel Holtmannf8558552008-07-14 20:13:49 +02001478static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1479{
1480 struct hci_cp_auth_requested *cp;
1481 struct hci_conn *conn;
1482
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001483 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001484
1485 if (!status)
1486 return;
1487
1488 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1489 if (!cp)
1490 return;
1491
1492 hci_dev_lock(hdev);
1493
1494 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1495 if (conn) {
1496 if (conn->state == BT_CONFIG) {
1497 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001498 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001499 }
1500 }
1501
1502 hci_dev_unlock(hdev);
1503}
1504
1505static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1506{
1507 struct hci_cp_set_conn_encrypt *cp;
1508 struct hci_conn *conn;
1509
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001510 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001511
1512 if (!status)
1513 return;
1514
1515 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1516 if (!cp)
1517 return;
1518
1519 hci_dev_lock(hdev);
1520
1521 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1522 if (conn) {
1523 if (conn->state == BT_CONFIG) {
1524 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001525 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001526 }
1527 }
1528
1529 hci_dev_unlock(hdev);
1530}
1531
Johan Hedberg127178d2010-11-18 22:22:29 +02001532static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001533 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02001534{
Johan Hedberg392599b2010-11-18 22:22:28 +02001535 if (conn->state != BT_CONFIG || !conn->out)
1536 return 0;
1537
Johan Hedberg765c2a92011-01-19 12:06:52 +05301538 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02001539 return 0;
1540
1541 /* Only request authentication for SSP connections or non-SSP
Johan Hedberg264b8b42014-01-08 16:40:39 +02001542 * devices with sec_level MEDIUM or HIGH or if MITM protection
1543 * is requested.
1544 */
Gustavo Padovan807deac2012-05-17 00:36:24 -03001545 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
Johan Hedberg7e3691e2014-05-30 14:45:19 +03001546 conn->pending_sec_level != BT_SECURITY_FIPS &&
Johan Hedberg264b8b42014-01-08 16:40:39 +02001547 conn->pending_sec_level != BT_SECURITY_HIGH &&
1548 conn->pending_sec_level != BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02001549 return 0;
1550
Johan Hedberg392599b2010-11-18 22:22:28 +02001551 return 1;
1552}
1553
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001554static int hci_resolve_name(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001555 struct inquiry_entry *e)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001556{
1557 struct hci_cp_remote_name_req cp;
1558
1559 memset(&cp, 0, sizeof(cp));
1560
1561 bacpy(&cp.bdaddr, &e->data.bdaddr);
1562 cp.pscan_rep_mode = e->data.pscan_rep_mode;
1563 cp.pscan_mode = e->data.pscan_mode;
1564 cp.clock_offset = e->data.clock_offset;
1565
1566 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1567}
1568
Johan Hedbergb644ba32012-01-17 21:48:47 +02001569static bool hci_resolve_next_name(struct hci_dev *hdev)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001570{
1571 struct discovery_state *discov = &hdev->discovery;
1572 struct inquiry_entry *e;
1573
Johan Hedbergb644ba32012-01-17 21:48:47 +02001574 if (list_empty(&discov->resolve))
1575 return false;
1576
1577 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
Ram Malovanyc8100892012-07-19 10:26:09 +03001578 if (!e)
1579 return false;
1580
Johan Hedbergb644ba32012-01-17 21:48:47 +02001581 if (hci_resolve_name(hdev, e) == 0) {
1582 e->name_state = NAME_PENDING;
1583 return true;
1584 }
1585
1586 return false;
1587}
1588
1589static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001590 bdaddr_t *bdaddr, u8 *name, u8 name_len)
Johan Hedbergb644ba32012-01-17 21:48:47 +02001591{
1592 struct discovery_state *discov = &hdev->discovery;
1593 struct inquiry_entry *e;
1594
1595 if (conn && !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001596 mgmt_device_connected(hdev, bdaddr, ACL_LINK, 0x00, 0, name,
1597 name_len, conn->dev_class);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001598
1599 if (discov->state == DISCOVERY_STOPPED)
1600 return;
1601
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001602 if (discov->state == DISCOVERY_STOPPING)
1603 goto discov_complete;
1604
1605 if (discov->state != DISCOVERY_RESOLVING)
1606 return;
1607
1608 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
Ram Malovany7cc83802012-07-19 10:26:10 +03001609 /* If the device was not found in a list of found devices names of which
1610 * are pending. there is no need to continue resolving a next name as it
1611 * will be done upon receiving another Remote Name Request Complete
1612 * Event */
1613 if (!e)
1614 return;
1615
1616 list_del(&e->list);
1617 if (name) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001618 e->name_state = NAME_KNOWN;
Ram Malovany7cc83802012-07-19 10:26:10 +03001619 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
1620 e->data.rssi, name, name_len);
Ram Malovanyc3e7c0d2012-07-19 10:26:11 +03001621 } else {
1622 e->name_state = NAME_NOT_KNOWN;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001623 }
1624
Johan Hedbergb644ba32012-01-17 21:48:47 +02001625 if (hci_resolve_next_name(hdev))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001626 return;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001627
1628discov_complete:
1629 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1630}
1631
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001632static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1633{
Johan Hedberg127178d2010-11-18 22:22:29 +02001634 struct hci_cp_remote_name_req *cp;
1635 struct hci_conn *conn;
1636
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001637 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02001638
1639 /* If successful wait for the name req complete event before
1640 * checking for the need to do authentication */
1641 if (!status)
1642 return;
1643
1644 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1645 if (!cp)
1646 return;
1647
1648 hci_dev_lock(hdev);
1649
1650 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001651
1652 if (test_bit(HCI_MGMT, &hdev->dev_flags))
1653 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
1654
Johan Hedberg79c6c702011-04-28 11:28:55 -07001655 if (!conn)
1656 goto unlock;
1657
1658 if (!hci_outgoing_auth_needed(hdev, conn))
1659 goto unlock;
1660
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001661 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02001662 struct hci_cp_auth_requested auth_cp;
1663
1664 auth_cp.handle = __cpu_to_le16(conn->handle);
1665 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
1666 sizeof(auth_cp), &auth_cp);
Johan Hedberg127178d2010-11-18 22:22:29 +02001667 }
1668
Johan Hedberg79c6c702011-04-28 11:28:55 -07001669unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02001670 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001671}
1672
Marcel Holtmann769be972008-07-14 20:13:49 +02001673static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1674{
1675 struct hci_cp_read_remote_features *cp;
1676 struct hci_conn *conn;
1677
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001678 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001679
1680 if (!status)
1681 return;
1682
1683 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1684 if (!cp)
1685 return;
1686
1687 hci_dev_lock(hdev);
1688
1689 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1690 if (conn) {
1691 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001692 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001693 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001694 }
1695 }
1696
1697 hci_dev_unlock(hdev);
1698}
1699
1700static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1701{
1702 struct hci_cp_read_remote_ext_features *cp;
1703 struct hci_conn *conn;
1704
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001705 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001706
1707 if (!status)
1708 return;
1709
1710 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1711 if (!cp)
1712 return;
1713
1714 hci_dev_lock(hdev);
1715
1716 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1717 if (conn) {
1718 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001719 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001720 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001721 }
1722 }
1723
1724 hci_dev_unlock(hdev);
1725}
1726
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001727static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1728{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001729 struct hci_cp_setup_sync_conn *cp;
1730 struct hci_conn *acl, *sco;
1731 __u16 handle;
1732
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001733 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001734
1735 if (!status)
1736 return;
1737
1738 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1739 if (!cp)
1740 return;
1741
1742 handle = __le16_to_cpu(cp->handle);
1743
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001744 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001745
1746 hci_dev_lock(hdev);
1747
1748 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001749 if (acl) {
1750 sco = acl->link;
1751 if (sco) {
1752 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001753
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001754 hci_proto_connect_cfm(sco, status);
1755 hci_conn_del(sco);
1756 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001757 }
1758
1759 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001760}
1761
1762static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1763{
1764 struct hci_cp_sniff_mode *cp;
1765 struct hci_conn *conn;
1766
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001767 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001768
1769 if (!status)
1770 return;
1771
1772 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1773 if (!cp)
1774 return;
1775
1776 hci_dev_lock(hdev);
1777
1778 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001779 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001780 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001781
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001782 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001783 hci_sco_setup(conn, status);
1784 }
1785
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001786 hci_dev_unlock(hdev);
1787}
1788
1789static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1790{
1791 struct hci_cp_exit_sniff_mode *cp;
1792 struct hci_conn *conn;
1793
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001794 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001795
1796 if (!status)
1797 return;
1798
1799 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1800 if (!cp)
1801 return;
1802
1803 hci_dev_lock(hdev);
1804
1805 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001806 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001807 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001808
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001809 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001810 hci_sco_setup(conn, status);
1811 }
1812
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001813 hci_dev_unlock(hdev);
1814}
1815
Johan Hedberg88c3df12012-02-09 14:27:38 +02001816static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
1817{
1818 struct hci_cp_disconnect *cp;
1819 struct hci_conn *conn;
1820
1821 if (!status)
1822 return;
1823
1824 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
1825 if (!cp)
1826 return;
1827
1828 hci_dev_lock(hdev);
1829
1830 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1831 if (conn)
1832 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001833 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02001834
1835 hci_dev_unlock(hdev);
1836}
1837
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001838static void hci_cs_create_phylink(struct hci_dev *hdev, u8 status)
1839{
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001840 struct hci_cp_create_phy_link *cp;
1841
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001842 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001843
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001844 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_PHY_LINK);
1845 if (!cp)
1846 return;
1847
Andrei Emeltchenkoe58917b2012-10-31 15:46:33 +02001848 hci_dev_lock(hdev);
1849
1850 if (status) {
1851 struct hci_conn *hcon;
1852
1853 hcon = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
1854 if (hcon)
1855 hci_conn_del(hcon);
1856 } else {
1857 amp_write_remote_assoc(hdev, cp->phy_handle);
1858 }
1859
1860 hci_dev_unlock(hdev);
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001861}
1862
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +03001863static void hci_cs_accept_phylink(struct hci_dev *hdev, u8 status)
1864{
1865 struct hci_cp_accept_phy_link *cp;
1866
1867 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1868
1869 if (status)
1870 return;
1871
1872 cp = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_PHY_LINK);
1873 if (!cp)
1874 return;
1875
1876 amp_write_remote_assoc(hdev, cp->phy_handle);
1877}
1878
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02001879static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
1880{
1881 struct hci_cp_le_create_conn *cp;
1882 struct hci_conn *conn;
1883
1884 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1885
1886 /* All connection failure handling is taken care of by the
1887 * hci_le_conn_failed function which is triggered by the HCI
1888 * request completion callbacks used for connecting.
1889 */
1890 if (status)
1891 return;
1892
1893 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
1894 if (!cp)
1895 return;
1896
1897 hci_dev_lock(hdev);
1898
1899 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->peer_addr);
1900 if (!conn)
1901 goto unlock;
1902
1903 /* Store the initiator and responder address information which
1904 * is needed for SMP. These values will not change during the
1905 * lifetime of the connection.
1906 */
1907 conn->init_addr_type = cp->own_address_type;
1908 if (cp->own_address_type == ADDR_LE_DEV_RANDOM)
1909 bacpy(&conn->init_addr, &hdev->random_addr);
1910 else
1911 bacpy(&conn->init_addr, &hdev->bdaddr);
1912
1913 conn->resp_addr_type = cp->peer_addr_type;
1914 bacpy(&conn->resp_addr, &cp->peer_addr);
1915
Johan Hedberg9489eca2014-02-28 17:45:46 +02001916 /* We don't want the connection attempt to stick around
1917 * indefinitely since LE doesn't have a page timeout concept
1918 * like BR/EDR. Set a timer for any connection that doesn't use
1919 * the white list for connecting.
1920 */
1921 if (cp->filter_policy == HCI_LE_USE_PEER_ADDR)
1922 queue_delayed_work(conn->hdev->workqueue,
1923 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001924 conn->conn_timeout);
Johan Hedberg9489eca2014-02-28 17:45:46 +02001925
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02001926unlock:
1927 hci_dev_unlock(hdev);
1928}
1929
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02001930static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
1931{
1932 struct hci_cp_le_start_enc *cp;
1933 struct hci_conn *conn;
1934
1935 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1936
1937 if (!status)
1938 return;
1939
1940 hci_dev_lock(hdev);
1941
1942 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
1943 if (!cp)
1944 goto unlock;
1945
1946 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1947 if (!conn)
1948 goto unlock;
1949
1950 if (conn->state != BT_CONNECTED)
1951 goto unlock;
1952
1953 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
1954 hci_conn_drop(conn);
1955
1956unlock:
1957 hci_dev_unlock(hdev);
1958}
1959
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001960static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001961{
1962 __u8 status = *((__u8 *) skb->data);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001963 struct discovery_state *discov = &hdev->discovery;
1964 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001965
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001966 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001967
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001968 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03001969
1970 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
1971 return;
1972
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001973 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -03001974 wake_up_bit(&hdev->flags, HCI_INQUIRY);
1975
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001976 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001977 return;
1978
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001979 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001980
Andre Guedes343f9352012-02-17 20:39:37 -03001981 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001982 goto unlock;
1983
1984 if (list_empty(&discov->resolve)) {
1985 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1986 goto unlock;
1987 }
1988
1989 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
1990 if (e && hci_resolve_name(hdev, e) == 0) {
1991 e->name_state = NAME_PENDING;
1992 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
1993 } else {
1994 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1995 }
1996
1997unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001998 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001999}
2000
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002001static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002003 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002004 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 int num_rsp = *((__u8 *) skb->data);
2006
2007 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2008
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002009 if (!num_rsp)
2010 return;
2011
Andre Guedes1519cc12012-03-21 00:03:38 -03002012 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
2013 return;
2014
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002016
Johan Hedberge17acd42011-03-30 23:57:16 +03002017 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02002018 u32 flags;
Johan Hedberg31754052012-01-04 13:39:52 +02002019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 bacpy(&data.bdaddr, &info->bdaddr);
2021 data.pscan_rep_mode = info->pscan_rep_mode;
2022 data.pscan_period_mode = info->pscan_period_mode;
2023 data.pscan_mode = info->pscan_mode;
2024 memcpy(data.dev_class, info->dev_class, 3);
2025 data.clock_offset = info->clock_offset;
2026 data.rssi = 0x00;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002027 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02002028
Marcel Holtmannaf589252014-07-01 14:11:20 +02002029 flags = hci_inquiry_cache_update(hdev, &data, false);
2030
Johan Hedberg48264f02011-11-09 13:58:58 +02002031 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02002032 info->dev_class, 0, flags, NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002034
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 hci_dev_unlock(hdev);
2036}
2037
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002038static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002040 struct hci_ev_conn_complete *ev = (void *) skb->data;
2041 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002043 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002046
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002047 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02002048 if (!conn) {
2049 if (ev->link_type != SCO_LINK)
2050 goto unlock;
2051
2052 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2053 if (!conn)
2054 goto unlock;
2055
2056 conn->type = SCO_LINK;
2057 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002058
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002059 if (!ev->status) {
2060 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02002061
2062 if (conn->type == ACL_LINK) {
2063 conn->state = BT_CONFIG;
2064 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02002065
2066 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
2067 !hci_find_link_key(hdev, &ev->bdaddr))
2068 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2069 else
2070 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02002071 } else
2072 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002073
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002074 hci_conn_add_sysfs(conn);
2075
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002076 if (test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002077 set_bit(HCI_CONN_AUTH, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002078
2079 if (test_bit(HCI_ENCRYPT, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002080 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002081
2082 /* Get remote features */
2083 if (conn->type == ACL_LINK) {
2084 struct hci_cp_read_remote_features cp;
2085 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02002086 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002087 sizeof(cp), &cp);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002088 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002089
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002090 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02002091 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002092 struct hci_cp_change_conn_ptype cp;
2093 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02002094 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002095 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
2096 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002097 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02002098 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002099 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02002100 if (conn->type == ACL_LINK)
Marcel Holtmann64c7b772014-02-18 14:22:20 -08002101 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002102 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02002103 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002104
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002105 if (conn->type == ACL_LINK)
2106 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002107
Marcel Holtmann769be972008-07-14 20:13:49 +02002108 if (ev->status) {
2109 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002110 hci_conn_del(conn);
Marcel Holtmannc89b6e62009-01-15 21:57:03 +01002111 } else if (ev->link_type != ACL_LINK)
2112 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002113
2114unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002116
2117 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118}
2119
Johan Hedberg70c46422014-07-09 12:59:17 +03002120static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
2121{
2122 struct hci_cp_reject_conn_req cp;
2123
2124 bacpy(&cp.bdaddr, bdaddr);
2125 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
2126 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
2127}
2128
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002129static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002131 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 int mask = hdev->link_mode;
Johan Hedberg70c46422014-07-09 12:59:17 +03002133 struct inquiry_entry *ie;
2134 struct hci_conn *conn;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002135 __u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002137 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002138 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002140 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
2141 &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Johan Hedberg70c46422014-07-09 12:59:17 +03002143 if (!(mask & HCI_LM_ACCEPT)) {
2144 hci_reject_conn(hdev, &ev->bdaddr);
2145 return;
2146 }
2147
Johan Hedberga55bd292014-07-09 12:59:19 +03002148 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags)) {
2149 if (hci_bdaddr_list_lookup(&hdev->blacklist, &ev->bdaddr,
2150 BDADDR_BREDR)) {
2151 hci_reject_conn(hdev, &ev->bdaddr);
2152 return;
2153 }
2154 } else {
2155 if (!hci_bdaddr_list_lookup(&hdev->whitelist, &ev->bdaddr,
2156 BDADDR_BREDR)) {
2157 hci_reject_conn(hdev, &ev->bdaddr);
2158 return;
2159 }
Johan Hedberg70c46422014-07-09 12:59:17 +03002160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Johan Hedberg70c46422014-07-09 12:59:17 +03002162 /* Connection accepted */
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002163
Johan Hedberg70c46422014-07-09 12:59:17 +03002164 hci_dev_lock(hdev);
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02002165
Johan Hedberg70c46422014-07-09 12:59:17 +03002166 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2167 if (ie)
2168 memcpy(ie->data.dev_class, ev->dev_class, 3);
2169
2170 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
2171 &ev->bdaddr);
2172 if (!conn) {
2173 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (!conn) {
Johan Hedberg70c46422014-07-09 12:59:17 +03002175 BT_ERR("No memory for new connection");
2176 hci_dev_unlock(hdev);
2177 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
Johan Hedberg70c46422014-07-09 12:59:17 +03002179 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002180
Johan Hedberg70c46422014-07-09 12:59:17 +03002181 memcpy(conn->dev_class, ev->dev_class, 3);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002182
Johan Hedberg70c46422014-07-09 12:59:17 +03002183 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Johan Hedberg70c46422014-07-09 12:59:17 +03002185 if (ev->link_type == ACL_LINK ||
2186 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
2187 struct hci_cp_accept_conn_req cp;
2188 conn->state = BT_CONNECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 bacpy(&cp.bdaddr, &ev->bdaddr);
Johan Hedberg70c46422014-07-09 12:59:17 +03002191
2192 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
2193 cp.role = 0x00; /* Become master */
2194 else
2195 cp.role = 0x01; /* Remain slave */
2196
2197 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
2198 } else if (!(flags & HCI_PROTO_DEFER)) {
2199 struct hci_cp_accept_sync_conn_req cp;
2200 conn->state = BT_CONNECT;
2201
2202 bacpy(&cp.bdaddr, &ev->bdaddr);
2203 cp.pkt_type = cpu_to_le16(conn->pkt_type);
2204
2205 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
2206 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
2207 cp.max_latency = cpu_to_le16(0xffff);
2208 cp.content_format = cpu_to_le16(hdev->voice_setting);
2209 cp.retrans_effort = 0xff;
2210
2211 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
2212 &cp);
2213 } else {
2214 conn->state = BT_CONNECT2;
2215 hci_proto_connect_cfm(conn, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
2217}
2218
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02002219static u8 hci_to_mgmt_reason(u8 err)
2220{
2221 switch (err) {
2222 case HCI_ERROR_CONNECTION_TIMEOUT:
2223 return MGMT_DEV_DISCONN_TIMEOUT;
2224 case HCI_ERROR_REMOTE_USER_TERM:
2225 case HCI_ERROR_REMOTE_LOW_RESOURCES:
2226 case HCI_ERROR_REMOTE_POWER_OFF:
2227 return MGMT_DEV_DISCONN_REMOTE;
2228 case HCI_ERROR_LOCAL_HOST_TERM:
2229 return MGMT_DEV_DISCONN_LOCAL_HOST;
2230 default:
2231 return MGMT_DEV_DISCONN_UNKNOWN;
2232 }
2233}
2234
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002235static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002237 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Andre Guedesabf54a52013-11-07 17:36:09 -03002238 u8 reason = hci_to_mgmt_reason(ev->reason);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002239 struct hci_conn_params *params;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002240 struct hci_conn *conn;
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002241 bool mgmt_connected;
Andre Guedes38462202013-11-07 17:36:10 -03002242 u8 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002244 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 hci_dev_lock(hdev);
2247
Marcel Holtmann04837f62006-07-03 10:02:33 +02002248 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02002249 if (!conn)
2250 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002251
Andre Guedesabf54a52013-11-07 17:36:09 -03002252 if (ev->status) {
2253 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2254 conn->dst_type, ev->status);
2255 goto unlock;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02002256 }
Johan Hedbergf7520542011-01-20 12:34:39 +02002257
Andre Guedes38462202013-11-07 17:36:10 -03002258 conn->state = BT_CLOSED;
2259
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002260 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
2261 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2262 reason, mgmt_connected);
Andre Guedesabf54a52013-11-07 17:36:09 -03002263
Johan Hedbergaf6a9c32014-06-24 13:15:53 +03002264 if (conn->type == ACL_LINK &&
2265 test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
Andre Guedes38462202013-11-07 17:36:10 -03002266 hci_remove_link_key(hdev, &conn->dst);
Johan Hedberg22102462013-10-05 12:01:06 +02002267
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002268 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2269 if (params) {
2270 switch (params->auto_connect) {
2271 case HCI_AUTO_CONN_LINK_LOSS:
2272 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2273 break;
2274 /* Fall through */
2275
2276 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg418025d2014-07-04 12:37:24 +03002277 list_del_init(&params->action);
2278 list_add(&params->action, &hdev->pend_le_conns);
2279 hci_update_background_scan(hdev);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002280 break;
2281
2282 default:
2283 break;
2284 }
2285 }
2286
Andre Guedes38462202013-11-07 17:36:10 -03002287 type = conn->type;
Johan Hedberg22102462013-10-05 12:01:06 +02002288
Andre Guedes38462202013-11-07 17:36:10 -03002289 hci_proto_disconn_cfm(conn, ev->reason);
2290 hci_conn_del(conn);
2291
2292 /* Re-enable advertising if necessary, since it might
2293 * have been disabled by the connection. From the
2294 * HCI_LE_Set_Advertise_Enable command description in
2295 * the core specification (v4.0):
2296 * "The Controller shall continue advertising until the Host
2297 * issues an LE_Set_Advertise_Enable command with
2298 * Advertising_Enable set to 0x00 (Advertising is disabled)
2299 * or until a connection is created or until the Advertising
2300 * is timed out due to Directed Advertising."
2301 */
2302 if (type == LE_LINK)
2303 mgmt_reenable_advertising(hdev);
Johan Hedbergf7520542011-01-20 12:34:39 +02002304
2305unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 hci_dev_unlock(hdev);
2307}
2308
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002309static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002310{
2311 struct hci_ev_auth_complete *ev = (void *) skb->data;
2312 struct hci_conn *conn;
2313
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002314 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002315
2316 hci_dev_lock(hdev);
2317
2318 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002319 if (!conn)
2320 goto unlock;
2321
2322 if (!ev->status) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002323 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03002324 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002325 BT_INFO("re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03002326 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03002327 set_bit(HCI_CONN_AUTH, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002328 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03002329 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002330 } else {
Johan Hedbergbab73cb2012-02-09 16:07:29 +02002331 mgmt_auth_failed(hdev, &conn->dst, conn->type, conn->dst_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002332 ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002333 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002334
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002335 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
2336 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002337
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002338 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002339 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002340 struct hci_cp_set_conn_encrypt cp;
2341 cp.handle = ev->handle;
2342 cp.encrypt = 0x01;
2343 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002344 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002345 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002346 conn->state = BT_CONNECTED;
2347 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002348 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002349 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002350 } else {
2351 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002352
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002353 hci_conn_hold(conn);
2354 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02002355 hci_conn_drop(conn);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002356 }
2357
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002358 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002359 if (!ev->status) {
2360 struct hci_cp_set_conn_encrypt cp;
2361 cp.handle = ev->handle;
2362 cp.encrypt = 0x01;
2363 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002364 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002365 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002366 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002367 hci_encrypt_cfm(conn, ev->status, 0x00);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002368 }
2369 }
2370
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002371unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002372 hci_dev_unlock(hdev);
2373}
2374
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002375static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002376{
Johan Hedberg127178d2010-11-18 22:22:29 +02002377 struct hci_ev_remote_name *ev = (void *) skb->data;
2378 struct hci_conn *conn;
2379
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002380 BT_DBG("%s", hdev->name);
2381
2382 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02002383
2384 hci_dev_lock(hdev);
2385
2386 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002387
2388 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
2389 goto check_auth;
2390
2391 if (ev->status == 0)
2392 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002393 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02002394 else
2395 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
2396
2397check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07002398 if (!conn)
2399 goto unlock;
2400
2401 if (!hci_outgoing_auth_needed(hdev, conn))
2402 goto unlock;
2403
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002404 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002405 struct hci_cp_auth_requested cp;
2406 cp.handle = __cpu_to_le16(conn->handle);
2407 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
2408 }
2409
Johan Hedberg79c6c702011-04-28 11:28:55 -07002410unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02002411 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002412}
2413
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002414static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002415{
2416 struct hci_ev_encrypt_change *ev = (void *) skb->data;
2417 struct hci_conn *conn;
2418
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002419 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002420
2421 hci_dev_lock(hdev);
2422
2423 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002424 if (!conn)
2425 goto unlock;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002426
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002427 if (!ev->status) {
2428 if (ev->encrypt) {
2429 /* Encryption implies authentication */
Johan Hedberg4dae2792014-06-24 17:03:50 +03002430 set_bit(HCI_CONN_AUTH, &conn->flags);
2431 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002432 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002433
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08002434 /* P-256 authentication key implies FIPS */
2435 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
Johan Hedberg4dae2792014-06-24 17:03:50 +03002436 set_bit(HCI_CONN_FIPS, &conn->flags);
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08002437
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002438 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
2439 conn->type == LE_LINK)
2440 set_bit(HCI_CONN_AES_CCM, &conn->flags);
2441 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03002442 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002443 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
2444 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002445 }
2446
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002447 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
2448
2449 if (ev->status && conn->state == BT_CONNECTED) {
2450 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2451 hci_conn_drop(conn);
2452 goto unlock;
2453 }
2454
2455 if (conn->state == BT_CONFIG) {
2456 if (!ev->status)
2457 conn->state = BT_CONNECTED;
2458
Marcel Holtmann40b552a2014-03-19 14:10:25 -07002459 /* In Secure Connections Only mode, do not allow any
2460 * connections that are not encrypted with AES-CCM
2461 * using a P-256 authenticated combination key.
2462 */
2463 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) &&
2464 (!test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2465 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)) {
2466 hci_proto_connect_cfm(conn, HCI_ERROR_AUTH_FAILURE);
2467 hci_conn_drop(conn);
2468 goto unlock;
2469 }
2470
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002471 hci_proto_connect_cfm(conn, ev->status);
2472 hci_conn_drop(conn);
2473 } else
2474 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
2475
Gustavo Padovana7d77232012-05-13 03:20:07 -03002476unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002477 hci_dev_unlock(hdev);
2478}
2479
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002480static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
2481 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002482{
2483 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
2484 struct hci_conn *conn;
2485
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002486 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002487
2488 hci_dev_lock(hdev);
2489
2490 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2491 if (conn) {
2492 if (!ev->status)
Johan Hedberg4dae2792014-06-24 17:03:50 +03002493 set_bit(HCI_CONN_SECURE, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002494
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002495 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002496
2497 hci_key_change_cfm(conn, ev->status);
2498 }
2499
2500 hci_dev_unlock(hdev);
2501}
2502
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002503static void hci_remote_features_evt(struct hci_dev *hdev,
2504 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002505{
2506 struct hci_ev_remote_features *ev = (void *) skb->data;
2507 struct hci_conn *conn;
2508
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002509 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002510
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002511 hci_dev_lock(hdev);
2512
2513 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002514 if (!conn)
2515 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02002516
Johan Hedbergccd556f2010-11-10 17:11:51 +02002517 if (!ev->status)
Johan Hedbergcad718e2013-04-17 15:00:51 +03002518 memcpy(conn->features[0], ev->features, 8);
Johan Hedbergccd556f2010-11-10 17:11:51 +02002519
2520 if (conn->state != BT_CONFIG)
2521 goto unlock;
2522
2523 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
2524 struct hci_cp_read_remote_ext_features cp;
2525 cp.handle = ev->handle;
2526 cp.page = 0x01;
2527 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002528 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02002529 goto unlock;
2530 }
2531
Johan Hedberg671267b2012-05-12 16:11:50 -03002532 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002533 struct hci_cp_remote_name_req cp;
2534 memset(&cp, 0, sizeof(cp));
2535 bacpy(&cp.bdaddr, &conn->dst);
2536 cp.pscan_rep_mode = 0x02;
2537 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002538 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
2539 mgmt_device_connected(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002540 conn->dst_type, 0, NULL, 0,
2541 conn->dev_class);
Johan Hedberg392599b2010-11-18 22:22:28 +02002542
Johan Hedberg127178d2010-11-18 22:22:29 +02002543 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002544 conn->state = BT_CONNECTED;
2545 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002546 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002547 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002548
Johan Hedbergccd556f2010-11-10 17:11:51 +02002549unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002550 hci_dev_unlock(hdev);
2551}
2552
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002553static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002554{
2555 struct hci_ev_cmd_complete *ev = (void *) skb->data;
Johan Hedberg9238f362013-03-05 20:37:48 +02002556 u8 status = skb->data[sizeof(*ev)];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002557 __u16 opcode;
2558
2559 skb_pull(skb, sizeof(*ev));
2560
2561 opcode = __le16_to_cpu(ev->opcode);
2562
2563 switch (opcode) {
2564 case HCI_OP_INQUIRY_CANCEL:
2565 hci_cc_inquiry_cancel(hdev, skb);
2566 break;
2567
Andre Guedes4d934832012-03-21 00:03:35 -03002568 case HCI_OP_PERIODIC_INQ:
2569 hci_cc_periodic_inq(hdev, skb);
2570 break;
2571
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002572 case HCI_OP_EXIT_PERIODIC_INQ:
2573 hci_cc_exit_periodic_inq(hdev, skb);
2574 break;
2575
2576 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
2577 hci_cc_remote_name_req_cancel(hdev, skb);
2578 break;
2579
2580 case HCI_OP_ROLE_DISCOVERY:
2581 hci_cc_role_discovery(hdev, skb);
2582 break;
2583
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002584 case HCI_OP_READ_LINK_POLICY:
2585 hci_cc_read_link_policy(hdev, skb);
2586 break;
2587
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002588 case HCI_OP_WRITE_LINK_POLICY:
2589 hci_cc_write_link_policy(hdev, skb);
2590 break;
2591
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002592 case HCI_OP_READ_DEF_LINK_POLICY:
2593 hci_cc_read_def_link_policy(hdev, skb);
2594 break;
2595
2596 case HCI_OP_WRITE_DEF_LINK_POLICY:
2597 hci_cc_write_def_link_policy(hdev, skb);
2598 break;
2599
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002600 case HCI_OP_RESET:
2601 hci_cc_reset(hdev, skb);
2602 break;
2603
2604 case HCI_OP_WRITE_LOCAL_NAME:
2605 hci_cc_write_local_name(hdev, skb);
2606 break;
2607
2608 case HCI_OP_READ_LOCAL_NAME:
2609 hci_cc_read_local_name(hdev, skb);
2610 break;
2611
2612 case HCI_OP_WRITE_AUTH_ENABLE:
2613 hci_cc_write_auth_enable(hdev, skb);
2614 break;
2615
2616 case HCI_OP_WRITE_ENCRYPT_MODE:
2617 hci_cc_write_encrypt_mode(hdev, skb);
2618 break;
2619
2620 case HCI_OP_WRITE_SCAN_ENABLE:
2621 hci_cc_write_scan_enable(hdev, skb);
2622 break;
2623
2624 case HCI_OP_READ_CLASS_OF_DEV:
2625 hci_cc_read_class_of_dev(hdev, skb);
2626 break;
2627
2628 case HCI_OP_WRITE_CLASS_OF_DEV:
2629 hci_cc_write_class_of_dev(hdev, skb);
2630 break;
2631
2632 case HCI_OP_READ_VOICE_SETTING:
2633 hci_cc_read_voice_setting(hdev, skb);
2634 break;
2635
2636 case HCI_OP_WRITE_VOICE_SETTING:
2637 hci_cc_write_voice_setting(hdev, skb);
2638 break;
2639
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -07002640 case HCI_OP_READ_NUM_SUPPORTED_IAC:
2641 hci_cc_read_num_supported_iac(hdev, skb);
2642 break;
2643
Marcel Holtmann333140b2008-07-14 20:13:48 +02002644 case HCI_OP_WRITE_SSP_MODE:
2645 hci_cc_write_ssp_mode(hdev, skb);
2646 break;
2647
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08002648 case HCI_OP_WRITE_SC_SUPPORT:
2649 hci_cc_write_sc_support(hdev, skb);
2650 break;
2651
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002652 case HCI_OP_READ_LOCAL_VERSION:
2653 hci_cc_read_local_version(hdev, skb);
2654 break;
2655
2656 case HCI_OP_READ_LOCAL_COMMANDS:
2657 hci_cc_read_local_commands(hdev, skb);
2658 break;
2659
2660 case HCI_OP_READ_LOCAL_FEATURES:
2661 hci_cc_read_local_features(hdev, skb);
2662 break;
2663
Andre Guedes971e3a42011-06-30 19:20:52 -03002664 case HCI_OP_READ_LOCAL_EXT_FEATURES:
2665 hci_cc_read_local_ext_features(hdev, skb);
2666 break;
2667
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002668 case HCI_OP_READ_BUFFER_SIZE:
2669 hci_cc_read_buffer_size(hdev, skb);
2670 break;
2671
2672 case HCI_OP_READ_BD_ADDR:
2673 hci_cc_read_bd_addr(hdev, skb);
2674 break;
2675
Johan Hedbergf332ec62013-03-15 17:07:11 -05002676 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
2677 hci_cc_read_page_scan_activity(hdev, skb);
2678 break;
2679
Johan Hedberg4a3ee762013-03-15 17:07:12 -05002680 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
2681 hci_cc_write_page_scan_activity(hdev, skb);
2682 break;
2683
Johan Hedbergf332ec62013-03-15 17:07:11 -05002684 case HCI_OP_READ_PAGE_SCAN_TYPE:
2685 hci_cc_read_page_scan_type(hdev, skb);
2686 break;
2687
Johan Hedberg4a3ee762013-03-15 17:07:12 -05002688 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
2689 hci_cc_write_page_scan_type(hdev, skb);
2690 break;
2691
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02002692 case HCI_OP_READ_DATA_BLOCK_SIZE:
2693 hci_cc_read_data_block_size(hdev, skb);
2694 break;
2695
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02002696 case HCI_OP_READ_FLOW_CONTROL_MODE:
2697 hci_cc_read_flow_control_mode(hdev, skb);
2698 break;
2699
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03002700 case HCI_OP_READ_LOCAL_AMP_INFO:
2701 hci_cc_read_local_amp_info(hdev, skb);
2702 break;
2703
Johan Hedberg33f35722014-06-28 17:54:06 +03002704 case HCI_OP_READ_CLOCK:
2705 hci_cc_read_clock(hdev, skb);
2706 break;
2707
Andrei Emeltchenko903e4542012-09-27 17:26:09 +03002708 case HCI_OP_READ_LOCAL_AMP_ASSOC:
2709 hci_cc_read_local_amp_assoc(hdev, skb);
2710 break;
2711
Johan Hedbergd5859e22011-01-25 01:19:58 +02002712 case HCI_OP_READ_INQ_RSP_TX_POWER:
2713 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2714 break;
2715
Johan Hedberg980e1a52011-01-22 06:10:07 +02002716 case HCI_OP_PIN_CODE_REPLY:
2717 hci_cc_pin_code_reply(hdev, skb);
2718 break;
2719
2720 case HCI_OP_PIN_CODE_NEG_REPLY:
2721 hci_cc_pin_code_neg_reply(hdev, skb);
2722 break;
2723
Szymon Jancc35938b2011-03-22 13:12:21 +01002724 case HCI_OP_READ_LOCAL_OOB_DATA:
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08002725 hci_cc_read_local_oob_data(hdev, skb);
2726 break;
2727
2728 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
2729 hci_cc_read_local_oob_ext_data(hdev, skb);
Szymon Jancc35938b2011-03-22 13:12:21 +01002730 break;
2731
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002732 case HCI_OP_LE_READ_BUFFER_SIZE:
2733 hci_cc_le_read_buffer_size(hdev, skb);
2734 break;
2735
Johan Hedberg60e77322013-01-22 14:01:59 +02002736 case HCI_OP_LE_READ_LOCAL_FEATURES:
2737 hci_cc_le_read_local_features(hdev, skb);
2738 break;
2739
Johan Hedberg8fa19092012-10-19 20:57:49 +03002740 case HCI_OP_LE_READ_ADV_TX_POWER:
2741 hci_cc_le_read_adv_tx_power(hdev, skb);
2742 break;
2743
Johan Hedberga5c29682011-02-19 12:05:57 -03002744 case HCI_OP_USER_CONFIRM_REPLY:
2745 hci_cc_user_confirm_reply(hdev, skb);
2746 break;
2747
2748 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2749 hci_cc_user_confirm_neg_reply(hdev, skb);
2750 break;
2751
Brian Gix1143d452011-11-23 08:28:34 -08002752 case HCI_OP_USER_PASSKEY_REPLY:
2753 hci_cc_user_passkey_reply(hdev, skb);
2754 break;
2755
2756 case HCI_OP_USER_PASSKEY_NEG_REPLY:
2757 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02002758 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09002759
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08002760 case HCI_OP_LE_SET_RANDOM_ADDR:
2761 hci_cc_le_set_random_addr(hdev, skb);
2762 break;
2763
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01002764 case HCI_OP_LE_SET_ADV_ENABLE:
2765 hci_cc_le_set_adv_enable(hdev, skb);
2766 break;
2767
Marcel Holtmann533553f2014-03-21 12:18:10 -07002768 case HCI_OP_LE_SET_SCAN_PARAM:
2769 hci_cc_le_set_scan_param(hdev, skb);
2770 break;
2771
Andre Guedeseb9d91f2011-05-26 16:23:52 -03002772 case HCI_OP_LE_SET_SCAN_ENABLE:
2773 hci_cc_le_set_scan_enable(hdev, skb);
2774 break;
2775
Johan Hedbergcf1d0812013-01-22 14:02:00 +02002776 case HCI_OP_LE_READ_WHITE_LIST_SIZE:
2777 hci_cc_le_read_white_list_size(hdev, skb);
2778 break;
2779
Marcel Holtmann0f36b582014-02-27 20:37:31 -08002780 case HCI_OP_LE_CLEAR_WHITE_LIST:
2781 hci_cc_le_clear_white_list(hdev, skb);
2782 break;
2783
2784 case HCI_OP_LE_ADD_TO_WHITE_LIST:
2785 hci_cc_le_add_to_white_list(hdev, skb);
2786 break;
2787
2788 case HCI_OP_LE_DEL_FROM_WHITE_LIST:
2789 hci_cc_le_del_from_white_list(hdev, skb);
2790 break;
2791
Johan Hedberg9b008c02013-01-22 14:02:01 +02002792 case HCI_OP_LE_READ_SUPPORTED_STATES:
2793 hci_cc_le_read_supported_states(hdev, skb);
2794 break;
2795
Andre Guedesf9b49302011-06-30 19:20:53 -03002796 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
2797 hci_cc_write_le_host_supported(hdev, skb);
2798 break;
2799
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02002800 case HCI_OP_LE_SET_ADV_PARAM:
2801 hci_cc_set_adv_param(hdev, skb);
2802 break;
2803
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03002804 case HCI_OP_WRITE_REMOTE_AMP_ASSOC:
2805 hci_cc_write_remote_amp_assoc(hdev, skb);
2806 break;
2807
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02002808 case HCI_OP_READ_RSSI:
2809 hci_cc_read_rssi(hdev, skb);
2810 break;
2811
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02002812 case HCI_OP_READ_TX_POWER:
2813 hci_cc_read_tx_power(hdev, skb);
2814 break;
2815
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002816 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002817 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002818 break;
2819 }
2820
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002821 if (opcode != HCI_OP_NOP)
Marcel Holtmann65cc2b42014-06-16 12:30:56 +02002822 cancel_delayed_work(&hdev->cmd_timer);
Ville Tervo6bd32322011-02-16 16:32:41 +02002823
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002824 hci_req_cmd_complete(hdev, opcode, status);
Johan Hedberg9238f362013-03-05 20:37:48 +02002825
Szymon Jancdbccd792012-12-11 08:51:19 +01002826 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002827 atomic_set(&hdev->cmd_cnt, 1);
2828 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002829 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002830 }
2831}
2832
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002833static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002834{
2835 struct hci_ev_cmd_status *ev = (void *) skb->data;
2836 __u16 opcode;
2837
2838 skb_pull(skb, sizeof(*ev));
2839
2840 opcode = __le16_to_cpu(ev->opcode);
2841
2842 switch (opcode) {
2843 case HCI_OP_INQUIRY:
2844 hci_cs_inquiry(hdev, ev->status);
2845 break;
2846
2847 case HCI_OP_CREATE_CONN:
2848 hci_cs_create_conn(hdev, ev->status);
2849 break;
2850
2851 case HCI_OP_ADD_SCO:
2852 hci_cs_add_sco(hdev, ev->status);
2853 break;
2854
Marcel Holtmannf8558552008-07-14 20:13:49 +02002855 case HCI_OP_AUTH_REQUESTED:
2856 hci_cs_auth_requested(hdev, ev->status);
2857 break;
2858
2859 case HCI_OP_SET_CONN_ENCRYPT:
2860 hci_cs_set_conn_encrypt(hdev, ev->status);
2861 break;
2862
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002863 case HCI_OP_REMOTE_NAME_REQ:
2864 hci_cs_remote_name_req(hdev, ev->status);
2865 break;
2866
Marcel Holtmann769be972008-07-14 20:13:49 +02002867 case HCI_OP_READ_REMOTE_FEATURES:
2868 hci_cs_read_remote_features(hdev, ev->status);
2869 break;
2870
2871 case HCI_OP_READ_REMOTE_EXT_FEATURES:
2872 hci_cs_read_remote_ext_features(hdev, ev->status);
2873 break;
2874
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002875 case HCI_OP_SETUP_SYNC_CONN:
2876 hci_cs_setup_sync_conn(hdev, ev->status);
2877 break;
2878
2879 case HCI_OP_SNIFF_MODE:
2880 hci_cs_sniff_mode(hdev, ev->status);
2881 break;
2882
2883 case HCI_OP_EXIT_SNIFF_MODE:
2884 hci_cs_exit_sniff_mode(hdev, ev->status);
2885 break;
2886
Johan Hedberg8962ee72011-01-20 12:40:27 +02002887 case HCI_OP_DISCONNECT:
Johan Hedberg88c3df12012-02-09 14:27:38 +02002888 hci_cs_disconnect(hdev, ev->status);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002889 break;
2890
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03002891 case HCI_OP_CREATE_PHY_LINK:
2892 hci_cs_create_phylink(hdev, ev->status);
2893 break;
2894
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +03002895 case HCI_OP_ACCEPT_PHY_LINK:
2896 hci_cs_accept_phylink(hdev, ev->status);
2897 break;
2898
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002899 case HCI_OP_LE_CREATE_CONN:
2900 hci_cs_le_create_conn(hdev, ev->status);
2901 break;
2902
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02002903 case HCI_OP_LE_START_ENC:
2904 hci_cs_le_start_enc(hdev, ev->status);
2905 break;
2906
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002907 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002908 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002909 break;
2910 }
2911
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002912 if (opcode != HCI_OP_NOP)
Marcel Holtmann65cc2b42014-06-16 12:30:56 +02002913 cancel_delayed_work(&hdev->cmd_timer);
Ville Tervo6bd32322011-02-16 16:32:41 +02002914
Johan Hedberg02350a72013-04-03 21:50:29 +03002915 if (ev->status ||
2916 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event))
2917 hci_req_cmd_complete(hdev, opcode, ev->status);
Johan Hedberg9238f362013-03-05 20:37:48 +02002918
Gustavo F. Padovan10572132011-03-16 15:36:29 -03002919 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002920 atomic_set(&hdev->cmd_cnt, 1);
2921 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002922 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002923 }
2924}
2925
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002926static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002927{
2928 struct hci_ev_role_change *ev = (void *) skb->data;
2929 struct hci_conn *conn;
2930
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002931 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002932
2933 hci_dev_lock(hdev);
2934
2935 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2936 if (conn) {
2937 if (!ev->status) {
2938 if (ev->role)
Johan Hedberg4dae2792014-06-24 17:03:50 +03002939 clear_bit(HCI_CONN_MASTER, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002940 else
Johan Hedberg4dae2792014-06-24 17:03:50 +03002941 set_bit(HCI_CONN_MASTER, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002942 }
2943
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002944 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002945
2946 hci_role_switch_cfm(conn, ev->status, ev->role);
2947 }
2948
2949 hci_dev_unlock(hdev);
2950}
2951
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002952static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002954 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 int i;
2956
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02002957 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
2958 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
2959 return;
2960 }
2961
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02002962 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03002963 ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 BT_DBG("%s bad parameters", hdev->name);
2965 return;
2966 }
2967
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02002968 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
2969
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02002970 for (i = 0; i < ev->num_hndl; i++) {
2971 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 struct hci_conn *conn;
2973 __u16 handle, count;
2974
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02002975 handle = __le16_to_cpu(info->handle);
2976 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
2978 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002979 if (!conn)
2980 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002982 conn->sent -= count;
2983
2984 switch (conn->type) {
2985 case ACL_LINK:
2986 hdev->acl_cnt += count;
2987 if (hdev->acl_cnt > hdev->acl_pkts)
2988 hdev->acl_cnt = hdev->acl_pkts;
2989 break;
2990
2991 case LE_LINK:
2992 if (hdev->le_pkts) {
2993 hdev->le_cnt += count;
2994 if (hdev->le_cnt > hdev->le_pkts)
2995 hdev->le_cnt = hdev->le_pkts;
2996 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002997 hdev->acl_cnt += count;
2998 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 hdev->acl_cnt = hdev->acl_pkts;
3000 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003001 break;
3002
3003 case SCO_LINK:
3004 hdev->sco_cnt += count;
3005 if (hdev->sco_cnt > hdev->sco_pkts)
3006 hdev->sco_cnt = hdev->sco_pkts;
3007 break;
3008
3009 default:
3010 BT_ERR("Unknown type %d conn %p", conn->type, conn);
3011 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 }
3013 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003014
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02003015 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016}
3017
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003018static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
3019 __u16 handle)
3020{
3021 struct hci_chan *chan;
3022
3023 switch (hdev->dev_type) {
3024 case HCI_BREDR:
3025 return hci_conn_hash_lookup_handle(hdev, handle);
3026 case HCI_AMP:
3027 chan = hci_chan_lookup_handle(hdev, handle);
3028 if (chan)
3029 return chan->conn;
3030 break;
3031 default:
3032 BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type);
3033 break;
3034 }
3035
3036 return NULL;
3037}
3038
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003039static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003040{
3041 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
3042 int i;
3043
3044 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
3045 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
3046 return;
3047 }
3048
3049 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03003050 ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003051 BT_DBG("%s bad parameters", hdev->name);
3052 return;
3053 }
3054
3055 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003056 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003057
3058 for (i = 0; i < ev->num_hndl; i++) {
3059 struct hci_comp_blocks_info *info = &ev->handles[i];
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003060 struct hci_conn *conn = NULL;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003061 __u16 handle, block_count;
3062
3063 handle = __le16_to_cpu(info->handle);
3064 block_count = __le16_to_cpu(info->blocks);
3065
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003066 conn = __hci_conn_lookup_handle(hdev, handle);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003067 if (!conn)
3068 continue;
3069
3070 conn->sent -= block_count;
3071
3072 switch (conn->type) {
3073 case ACL_LINK:
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03003074 case AMP_LINK:
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003075 hdev->block_cnt += block_count;
3076 if (hdev->block_cnt > hdev->num_blocks)
3077 hdev->block_cnt = hdev->num_blocks;
3078 break;
3079
3080 default:
3081 BT_ERR("Unknown type %d conn %p", conn->type, conn);
3082 break;
3083 }
3084 }
3085
3086 queue_work(hdev->workqueue, &hdev->tx_work);
3087}
3088
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003089static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003091 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003092 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003094 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
3096 hci_dev_lock(hdev);
3097
Marcel Holtmann04837f62006-07-03 10:02:33 +02003098 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3099 if (conn) {
3100 conn->mode = ev->mode;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003101
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003102 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
3103 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02003104 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02003105 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003106 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02003107 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003108 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003109
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003110 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003111 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003112 }
3113
3114 hci_dev_unlock(hdev);
3115}
3116
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003117static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003119 struct hci_ev_pin_code_req *ev = (void *) skb->data;
3120 struct hci_conn *conn;
3121
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003122 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003123
3124 hci_dev_lock(hdev);
3125
3126 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003127 if (!conn)
3128 goto unlock;
3129
3130 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003131 hci_conn_hold(conn);
3132 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003133 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003134 }
3135
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003136 if (!test_bit(HCI_PAIRABLE, &hdev->dev_flags))
Johan Hedberg03b555e2011-01-04 15:40:05 +02003137 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003138 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003139 else if (test_bit(HCI_MGMT, &hdev->dev_flags)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003140 u8 secure;
3141
3142 if (conn->pending_sec_level == BT_SECURITY_HIGH)
3143 secure = 1;
3144 else
3145 secure = 0;
3146
Johan Hedberg744cf192011-11-08 20:40:14 +02003147 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003148 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003149
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003150unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003151 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152}
3153
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003154static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003156 struct hci_ev_link_key_req *ev = (void *) skb->data;
3157 struct hci_cp_link_key_reply cp;
3158 struct hci_conn *conn;
3159 struct link_key *key;
3160
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003161 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003162
Andrei Emeltchenko034cbea2013-05-14 11:44:16 +03003163 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003164 return;
3165
3166 hci_dev_lock(hdev);
3167
3168 key = hci_find_link_key(hdev, &ev->bdaddr);
3169 if (!key) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003170 BT_DBG("%s link key not found for %pMR", hdev->name,
3171 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003172 goto not_found;
3173 }
3174
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003175 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
3176 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003177
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003178 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003179 if (conn) {
Marcel Holtmann66138ce2014-01-10 02:07:20 -08003180 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
3181 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03003182 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003183 BT_DBG("%s ignoring unauthenticated key", hdev->name);
3184 goto not_found;
3185 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003186
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003187 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Johan Hedbergf3fb0b52014-06-02 10:12:44 +03003188 (conn->pending_sec_level == BT_SECURITY_HIGH ||
3189 conn->pending_sec_level == BT_SECURITY_FIPS)) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003190 BT_DBG("%s ignoring key unauthenticated for high security",
3191 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003192 goto not_found;
3193 }
3194
3195 conn->key_type = key->type;
3196 conn->pin_length = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003197 }
3198
3199 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03003200 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003201
3202 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
3203
3204 hci_dev_unlock(hdev);
3205
3206 return;
3207
3208not_found:
3209 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
3210 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211}
3212
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003213static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003215 struct hci_ev_link_key_notify *ev = (void *) skb->data;
3216 struct hci_conn *conn;
Johan Hedberg7652ff62014-06-24 13:15:49 +03003217 struct link_key *key;
3218 bool persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003219 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003220
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003221 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003222
3223 hci_dev_lock(hdev);
3224
3225 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3226 if (conn) {
3227 hci_conn_hold(conn);
3228 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003229 pin_len = conn->pin_length;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02003230
3231 if (ev->key_type != HCI_LK_CHANGED_COMBINATION)
3232 conn->key_type = ev->key_type;
3233
David Herrmann76a68ba2013-04-06 20:28:37 +02003234 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003235 }
3236
Johan Hedberg7652ff62014-06-24 13:15:49 +03003237 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
3238 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003239
Johan Hedberg7652ff62014-06-24 13:15:49 +03003240 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
3241 ev->key_type, pin_len, &persistent);
3242 if (!key)
3243 goto unlock;
3244
3245 mgmt_new_link_key(hdev, key, persistent);
3246
Johan Hedberg6d5650c2014-06-24 13:15:51 +03003247 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
3248 * is set. If it's not set simply remove the key from the kernel
3249 * list (we've still notified user space about it but with
3250 * store_hint being 0).
3251 */
3252 if (key->type == HCI_LK_DEBUG_COMBINATION &&
3253 !test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) {
3254 list_del(&key->list);
3255 kfree(key);
3256 } else if (conn) {
Johan Hedbergaf6a9c32014-06-24 13:15:53 +03003257 if (persistent)
3258 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
3259 else
3260 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
Johan Hedberg6d5650c2014-06-24 13:15:51 +03003261 }
Johan Hedberg7652ff62014-06-24 13:15:49 +03003262
3263unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003264 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003267static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02003268{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003269 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003270 struct hci_conn *conn;
3271
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003272 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003273
3274 hci_dev_lock(hdev);
3275
3276 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 if (conn && !ev->status) {
3278 struct inquiry_entry *ie;
3279
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003280 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3281 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 ie->data.clock_offset = ev->clock_offset;
3283 ie->timestamp = jiffies;
3284 }
3285 }
3286
3287 hci_dev_unlock(hdev);
3288}
3289
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003290static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02003291{
3292 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
3293 struct hci_conn *conn;
3294
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003295 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02003296
3297 hci_dev_lock(hdev);
3298
3299 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3300 if (conn && !ev->status)
3301 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
3302
3303 hci_dev_unlock(hdev);
3304}
3305
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003306static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003307{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003308 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003309 struct inquiry_entry *ie;
3310
3311 BT_DBG("%s", hdev->name);
3312
3313 hci_dev_lock(hdev);
3314
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003315 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3316 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003317 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
3318 ie->timestamp = jiffies;
3319 }
3320
3321 hci_dev_unlock(hdev);
3322}
3323
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003324static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
3325 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003326{
3327 struct inquiry_data data;
3328 int num_rsp = *((__u8 *) skb->data);
3329
3330 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3331
3332 if (!num_rsp)
3333 return;
3334
Andre Guedes1519cc12012-03-21 00:03:38 -03003335 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3336 return;
3337
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003338 hci_dev_lock(hdev);
3339
3340 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01003341 struct inquiry_info_with_rssi_and_pscan_mode *info;
3342 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003343
Johan Hedberge17acd42011-03-30 23:57:16 +03003344 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003345 u32 flags;
3346
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003347 bacpy(&data.bdaddr, &info->bdaddr);
3348 data.pscan_rep_mode = info->pscan_rep_mode;
3349 data.pscan_period_mode = info->pscan_period_mode;
3350 data.pscan_mode = info->pscan_mode;
3351 memcpy(data.dev_class, info->dev_class, 3);
3352 data.clock_offset = info->clock_offset;
3353 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003354 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02003355
Marcel Holtmannaf589252014-07-01 14:11:20 +02003356 flags = hci_inquiry_cache_update(hdev, &data, false);
3357
Johan Hedberg48264f02011-11-09 13:58:58 +02003358 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003359 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003360 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003361 }
3362 } else {
3363 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
3364
Johan Hedberge17acd42011-03-30 23:57:16 +03003365 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003366 u32 flags;
3367
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003368 bacpy(&data.bdaddr, &info->bdaddr);
3369 data.pscan_rep_mode = info->pscan_rep_mode;
3370 data.pscan_period_mode = info->pscan_period_mode;
3371 data.pscan_mode = 0x00;
3372 memcpy(data.dev_class, info->dev_class, 3);
3373 data.clock_offset = info->clock_offset;
3374 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003375 data.ssp_mode = 0x00;
Marcel Holtmannaf589252014-07-01 14:11:20 +02003376
3377 flags = hci_inquiry_cache_update(hdev, &data, false);
3378
Johan Hedberg48264f02011-11-09 13:58:58 +02003379 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003380 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003381 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003382 }
3383 }
3384
3385 hci_dev_unlock(hdev);
3386}
3387
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003388static void hci_remote_ext_features_evt(struct hci_dev *hdev,
3389 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003390{
Marcel Holtmann41a96212008-07-14 20:13:48 +02003391 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
3392 struct hci_conn *conn;
3393
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003394 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003395
Marcel Holtmann41a96212008-07-14 20:13:48 +02003396 hci_dev_lock(hdev);
3397
3398 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02003399 if (!conn)
3400 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003401
Johan Hedbergcad718e2013-04-17 15:00:51 +03003402 if (ev->page < HCI_MAX_PAGES)
3403 memcpy(conn->features[ev->page], ev->features, 8);
3404
Johan Hedbergccd556f2010-11-10 17:11:51 +02003405 if (!ev->status && ev->page == 0x01) {
3406 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003407
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003408 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3409 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02003410 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02003411
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05303412 if (ev->features[0] & LMP_HOST_SSP) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02003413 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05303414 } else {
3415 /* It is mandatory by the Bluetooth specification that
3416 * Extended Inquiry Results are only used when Secure
3417 * Simple Pairing is enabled, but some devices violate
3418 * this.
3419 *
3420 * To make these devices work, the internal SSP
3421 * enabled flag needs to be cleared if the remote host
3422 * features do not indicate SSP support */
3423 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
3424 }
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -08003425
3426 if (ev->features[0] & LMP_HOST_SC)
3427 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003428 }
3429
Johan Hedbergccd556f2010-11-10 17:11:51 +02003430 if (conn->state != BT_CONFIG)
3431 goto unlock;
3432
Johan Hedberg671267b2012-05-12 16:11:50 -03003433 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003434 struct hci_cp_remote_name_req cp;
3435 memset(&cp, 0, sizeof(cp));
3436 bacpy(&cp.bdaddr, &conn->dst);
3437 cp.pscan_rep_mode = 0x02;
3438 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003439 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
3440 mgmt_device_connected(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003441 conn->dst_type, 0, NULL, 0,
3442 conn->dev_class);
Johan Hedberg392599b2010-11-18 22:22:28 +02003443
Johan Hedberg127178d2010-11-18 22:22:29 +02003444 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003445 conn->state = BT_CONNECTED;
3446 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003447 hci_conn_drop(conn);
Johan Hedbergccd556f2010-11-10 17:11:51 +02003448 }
3449
3450unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02003451 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003452}
3453
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003454static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
3455 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003456{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003457 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
3458 struct hci_conn *conn;
3459
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003460 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003461
3462 hci_dev_lock(hdev);
3463
3464 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02003465 if (!conn) {
3466 if (ev->link_type == ESCO_LINK)
3467 goto unlock;
3468
3469 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
3470 if (!conn)
3471 goto unlock;
3472
3473 conn->type = SCO_LINK;
3474 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003475
Marcel Holtmann732547f2009-04-19 19:14:14 +02003476 switch (ev->status) {
3477 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003478 conn->handle = __le16_to_cpu(ev->handle);
3479 conn->state = BT_CONNECTED;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02003480
3481 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02003482 break;
3483
Nick Pelly81218d22014-06-30 11:25:01 +05303484 case 0x10: /* Connection Accept Timeout */
Frédéric Dalleau1a4c9582013-08-19 14:24:02 +02003485 case 0x0d: /* Connection Rejected due to Limited Resources */
Stephen Coe705e5712010-02-16 11:29:44 -05003486 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02003487 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08003488 case 0x1a: /* Unsupported Remote Feature */
Marcel Holtmann732547f2009-04-19 19:14:14 +02003489 case 0x1f: /* Unspecified error */
Andrew Earl27539bc2014-03-10 10:31:04 +00003490 case 0x20: /* Unsupported LMP Parameter value */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02003491 if (conn->out) {
Marcel Holtmann732547f2009-04-19 19:14:14 +02003492 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
3493 (hdev->esco_type & EDR_ESCO_MASK);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02003494 if (hci_setup_sync(conn, conn->link->handle))
3495 goto unlock;
Marcel Holtmann732547f2009-04-19 19:14:14 +02003496 }
3497 /* fall through */
3498
3499 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003500 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02003501 break;
3502 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003503
3504 hci_proto_connect_cfm(conn, ev->status);
3505 if (ev->status)
3506 hci_conn_del(conn);
3507
3508unlock:
3509 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003510}
3511
Marcel Holtmannefdcf8e2013-10-15 10:31:12 -07003512static inline size_t eir_get_length(u8 *eir, size_t eir_len)
3513{
3514 size_t parsed = 0;
3515
3516 while (parsed < eir_len) {
3517 u8 field_len = eir[0];
3518
3519 if (field_len == 0)
3520 return parsed;
3521
3522 parsed += field_len + 1;
3523 eir += field_len + 1;
3524 }
3525
3526 return eir_len;
3527}
3528
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003529static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
3530 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003531{
3532 struct inquiry_data data;
3533 struct extended_inquiry_info *info = (void *) (skb->data + 1);
3534 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303535 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003536
3537 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3538
3539 if (!num_rsp)
3540 return;
3541
Andre Guedes1519cc12012-03-21 00:03:38 -03003542 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3543 return;
3544
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003545 hci_dev_lock(hdev);
3546
Johan Hedberge17acd42011-03-30 23:57:16 +03003547 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003548 u32 flags;
3549 bool name_known;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003550
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003551 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01003552 data.pscan_rep_mode = info->pscan_rep_mode;
3553 data.pscan_period_mode = info->pscan_period_mode;
3554 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003555 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01003556 data.clock_offset = info->clock_offset;
3557 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003558 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003559
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003560 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg4ddb1932012-01-15 20:04:43 +02003561 name_known = eir_has_data_type(info->data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003562 sizeof(info->data),
3563 EIR_NAME_COMPLETE);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003564 else
3565 name_known = true;
3566
Marcel Holtmannaf589252014-07-01 14:11:20 +02003567 flags = hci_inquiry_cache_update(hdev, &data, name_known);
3568
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303569 eir_len = eir_get_length(info->data, sizeof(info->data));
Marcel Holtmannaf589252014-07-01 14:11:20 +02003570
Johan Hedberg48264f02011-11-09 13:58:58 +02003571 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003572 info->dev_class, info->rssi,
3573 flags, info->data, eir_len, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003574 }
3575
3576 hci_dev_unlock(hdev);
3577}
3578
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003579static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
3580 struct sk_buff *skb)
3581{
3582 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
3583 struct hci_conn *conn;
3584
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003585 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003586 __le16_to_cpu(ev->handle));
3587
3588 hci_dev_lock(hdev);
3589
3590 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3591 if (!conn)
3592 goto unlock;
3593
Johan Hedberg9eb1fbf2014-04-11 12:02:31 -07003594 /* For BR/EDR the necessary steps are taken through the
3595 * auth_complete event.
3596 */
3597 if (conn->type != LE_LINK)
3598 goto unlock;
3599
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003600 if (!ev->status)
3601 conn->sec_level = conn->pending_sec_level;
3602
3603 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3604
3605 if (ev->status && conn->state == BT_CONNECTED) {
Andre Guedesbed71742013-01-30 11:50:56 -03003606 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
David Herrmann76a68ba2013-04-06 20:28:37 +02003607 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003608 goto unlock;
3609 }
3610
3611 if (conn->state == BT_CONFIG) {
3612 if (!ev->status)
3613 conn->state = BT_CONNECTED;
3614
3615 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003616 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003617 } else {
3618 hci_auth_cfm(conn, ev->status);
3619
3620 hci_conn_hold(conn);
3621 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003622 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003623 }
3624
3625unlock:
3626 hci_dev_unlock(hdev);
3627}
3628
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003629static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003630{
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003631 /* If remote requests no-bonding follow that lead */
Mikel Astizacabae92013-06-28 10:56:28 +02003632 if (conn->remote_auth == HCI_AT_NO_BONDING ||
3633 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02003634 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003635
Mikel Astizb7f94c82014-04-08 14:21:31 +02003636 /* If both remote and local have enough IO capabilities, require
3637 * MITM protection
3638 */
3639 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
3640 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
3641 return conn->remote_auth | 0x01;
3642
Timo Mueller7e741702014-04-08 14:21:33 +02003643 /* No MITM protection possible so ignore remote requirement */
3644 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003645}
3646
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003647static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003648{
3649 struct hci_ev_io_capa_request *ev = (void *) skb->data;
3650 struct hci_conn *conn;
3651
3652 BT_DBG("%s", hdev->name);
3653
3654 hci_dev_lock(hdev);
3655
3656 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003657 if (!conn)
3658 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003659
Johan Hedberg03b555e2011-01-04 15:40:05 +02003660 hci_conn_hold(conn);
3661
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003662 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg03b555e2011-01-04 15:40:05 +02003663 goto unlock;
3664
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003665 if (test_bit(HCI_PAIRABLE, &hdev->dev_flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03003666 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003667 struct hci_cp_io_capability_reply cp;
3668
3669 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05303670 /* Change the IO capability from KeyboardDisplay
3671 * to DisplayYesNo as it is not supported by BT spec. */
3672 cp.capability = (conn->io_capability == 0x04) ?
Mikel Astiza7676312013-06-28 10:56:29 +02003673 HCI_IO_DISPLAY_YESNO : conn->io_capability;
Mikel Astizb7f94c82014-04-08 14:21:31 +02003674
3675 /* If we are initiators, there is no remote information yet */
3676 if (conn->remote_auth == 0xff) {
3677 cp.authentication = conn->auth_type;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003678
Mikel Astizb16c6602014-04-08 14:21:34 +02003679 /* Request MITM protection if our IO caps allow it
Johan Hedberg4ad51a72014-06-09 14:41:25 +03003680 * except for the no-bonding case.
3681 * conn->auth_type is not updated here since
3682 * that might cause the user confirmation to be
3683 * rejected in case the remote doesn't have the
3684 * IO capabilities for MITM.
Mikel Astizb16c6602014-04-08 14:21:34 +02003685 */
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003686 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Mikel Astizb16c6602014-04-08 14:21:34 +02003687 cp.authentication != HCI_AT_NO_BONDING)
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003688 cp.authentication |= 0x01;
Mikel Astizb7f94c82014-04-08 14:21:31 +02003689 } else {
3690 conn->auth_type = hci_get_auth_req(conn);
3691 cp.authentication = conn->auth_type;
3692 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003693
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003694 if (hci_find_remote_oob_data(hdev, &conn->dst) &&
3695 (conn->out || test_bit(HCI_CONN_REMOTE_OOB, &conn->flags)))
Szymon Jancce85ee12011-03-22 13:12:23 +01003696 cp.oob_data = 0x01;
3697 else
3698 cp.oob_data = 0x00;
3699
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003700 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003701 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003702 } else {
3703 struct hci_cp_io_capability_neg_reply cp;
3704
3705 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003706 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003707
3708 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003709 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003710 }
3711
3712unlock:
3713 hci_dev_unlock(hdev);
3714}
3715
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003716static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02003717{
3718 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
3719 struct hci_conn *conn;
3720
3721 BT_DBG("%s", hdev->name);
3722
3723 hci_dev_lock(hdev);
3724
3725 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3726 if (!conn)
3727 goto unlock;
3728
Johan Hedberg03b555e2011-01-04 15:40:05 +02003729 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003730 conn->remote_auth = ev->authentication;
Johan Hedberg58a681e2012-01-16 06:47:28 +02003731 if (ev->oob_data)
3732 set_bit(HCI_CONN_REMOTE_OOB, &conn->flags);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003733
3734unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003735 hci_dev_unlock(hdev);
3736}
3737
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003738static void hci_user_confirm_request_evt(struct hci_dev *hdev,
3739 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03003740{
3741 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003742 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07003743 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003744
3745 BT_DBG("%s", hdev->name);
3746
3747 hci_dev_lock(hdev);
3748
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003749 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg7a828902011-04-28 11:28:53 -07003750 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03003751
Johan Hedberg7a828902011-04-28 11:28:53 -07003752 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3753 if (!conn)
3754 goto unlock;
3755
3756 loc_mitm = (conn->auth_type & 0x01);
3757 rem_mitm = (conn->remote_auth & 0x01);
3758
3759 /* If we require MITM but the remote device can't provide that
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003760 * (it has NoInputNoOutput) then reject the confirmation request
3761 */
3762 if (loc_mitm && conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
Johan Hedberg7a828902011-04-28 11:28:53 -07003763 BT_DBG("Rejecting request: remote device can't provide MITM");
3764 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003765 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003766 goto unlock;
3767 }
3768
3769 /* If no side requires MITM protection; auto-accept */
Mikel Astiza7676312013-06-28 10:56:29 +02003770 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
3771 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003772
3773 /* If we're not the initiators request authorization to
3774 * proceed from user space (mgmt_user_confirm with
Johan Hedbergba15a582014-06-09 13:58:14 +03003775 * confirm_hint set to 1). The exception is if neither
3776 * side had MITM in which case we do auto-accept.
3777 */
3778 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
3779 (loc_mitm || rem_mitm)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003780 BT_DBG("Confirming auto-accept as acceptor");
3781 confirm_hint = 1;
3782 goto confirm;
3783 }
3784
Johan Hedberg9f616562011-04-28 11:28:54 -07003785 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03003786 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003787
3788 if (hdev->auto_accept_delay > 0) {
3789 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03003790 queue_delayed_work(conn->hdev->workqueue,
3791 &conn->auto_accept_work, delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003792 goto unlock;
3793 }
3794
Johan Hedberg7a828902011-04-28 11:28:53 -07003795 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003796 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003797 goto unlock;
3798 }
3799
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003800confirm:
Johan Hedberg39adbff2014-03-20 08:18:14 +02003801 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
3802 le32_to_cpu(ev->passkey), confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07003803
3804unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03003805 hci_dev_unlock(hdev);
3806}
3807
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003808static void hci_user_passkey_request_evt(struct hci_dev *hdev,
3809 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08003810{
3811 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
3812
3813 BT_DBG("%s", hdev->name);
3814
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003815 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +02003816 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08003817}
3818
Johan Hedberg92a25252012-09-06 18:39:26 +03003819static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
3820 struct sk_buff *skb)
3821{
3822 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
3823 struct hci_conn *conn;
3824
3825 BT_DBG("%s", hdev->name);
3826
3827 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3828 if (!conn)
3829 return;
3830
3831 conn->passkey_notify = __le32_to_cpu(ev->passkey);
3832 conn->passkey_entered = 0;
3833
3834 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3835 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3836 conn->dst_type, conn->passkey_notify,
3837 conn->passkey_entered);
3838}
3839
3840static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
3841{
3842 struct hci_ev_keypress_notify *ev = (void *) skb->data;
3843 struct hci_conn *conn;
3844
3845 BT_DBG("%s", hdev->name);
3846
3847 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3848 if (!conn)
3849 return;
3850
3851 switch (ev->type) {
3852 case HCI_KEYPRESS_STARTED:
3853 conn->passkey_entered = 0;
3854 return;
3855
3856 case HCI_KEYPRESS_ENTERED:
3857 conn->passkey_entered++;
3858 break;
3859
3860 case HCI_KEYPRESS_ERASED:
3861 conn->passkey_entered--;
3862 break;
3863
3864 case HCI_KEYPRESS_CLEARED:
3865 conn->passkey_entered = 0;
3866 break;
3867
3868 case HCI_KEYPRESS_COMPLETED:
3869 return;
3870 }
3871
3872 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3873 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3874 conn->dst_type, conn->passkey_notify,
3875 conn->passkey_entered);
3876}
3877
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003878static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
3879 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003880{
3881 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
3882 struct hci_conn *conn;
3883
3884 BT_DBG("%s", hdev->name);
3885
3886 hci_dev_lock(hdev);
3887
3888 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03003889 if (!conn)
3890 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003891
Johan Hedberg2a611692011-02-19 12:06:00 -03003892 /* To avoid duplicate auth_failed events to user space we check
3893 * the HCI_CONN_AUTH_PEND flag which will be set if we
3894 * initiated the authentication. A traditional auth_complete
3895 * event gets always produced as initiator and is also mapped to
3896 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02003897 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedbergbab73cb2012-02-09 16:07:29 +02003898 mgmt_auth_failed(hdev, &conn->dst, conn->type, conn->dst_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003899 ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03003900
David Herrmann76a68ba2013-04-06 20:28:37 +02003901 hci_conn_drop(conn);
Johan Hedberg2a611692011-02-19 12:06:00 -03003902
3903unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003904 hci_dev_unlock(hdev);
3905}
3906
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003907static void hci_remote_host_features_evt(struct hci_dev *hdev,
3908 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02003909{
3910 struct hci_ev_remote_host_features *ev = (void *) skb->data;
3911 struct inquiry_entry *ie;
Johan Hedbergcad718e2013-04-17 15:00:51 +03003912 struct hci_conn *conn;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003913
3914 BT_DBG("%s", hdev->name);
3915
3916 hci_dev_lock(hdev);
3917
Johan Hedbergcad718e2013-04-17 15:00:51 +03003918 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3919 if (conn)
3920 memcpy(conn->features[1], ev->features, 8);
3921
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003922 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3923 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02003924 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003925
3926 hci_dev_unlock(hdev);
3927}
3928
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003929static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
3930 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01003931{
3932 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
3933 struct oob_data *data;
3934
3935 BT_DBG("%s", hdev->name);
3936
3937 hci_dev_lock(hdev);
3938
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003939 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Szymon Jance1ba1f12011-04-06 13:01:59 +02003940 goto unlock;
3941
Szymon Janc2763eda2011-03-22 13:12:22 +01003942 data = hci_find_remote_oob_data(hdev, &ev->bdaddr);
3943 if (data) {
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003944 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
3945 struct hci_cp_remote_oob_ext_data_reply cp;
Szymon Janc2763eda2011-03-22 13:12:22 +01003946
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003947 bacpy(&cp.bdaddr, &ev->bdaddr);
3948 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
3949 memcpy(cp.randomizer192, data->randomizer192,
3950 sizeof(cp.randomizer192));
3951 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
3952 memcpy(cp.randomizer256, data->randomizer256,
3953 sizeof(cp.randomizer256));
Szymon Janc2763eda2011-03-22 13:12:22 +01003954
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003955 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
3956 sizeof(cp), &cp);
3957 } else {
3958 struct hci_cp_remote_oob_data_reply cp;
3959
3960 bacpy(&cp.bdaddr, &ev->bdaddr);
3961 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
3962 memcpy(cp.randomizer, data->randomizer192,
3963 sizeof(cp.randomizer));
3964
3965 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
3966 sizeof(cp), &cp);
3967 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003968 } else {
3969 struct hci_cp_remote_oob_data_neg_reply cp;
3970
3971 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003972 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
3973 sizeof(cp), &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01003974 }
3975
Szymon Jance1ba1f12011-04-06 13:01:59 +02003976unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01003977 hci_dev_unlock(hdev);
3978}
3979
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03003980static void hci_phy_link_complete_evt(struct hci_dev *hdev,
3981 struct sk_buff *skb)
3982{
3983 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
3984 struct hci_conn *hcon, *bredr_hcon;
3985
3986 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
3987 ev->status);
3988
3989 hci_dev_lock(hdev);
3990
3991 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3992 if (!hcon) {
3993 hci_dev_unlock(hdev);
3994 return;
3995 }
3996
3997 if (ev->status) {
3998 hci_conn_del(hcon);
3999 hci_dev_unlock(hdev);
4000 return;
4001 }
4002
4003 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
4004
4005 hcon->state = BT_CONNECTED;
4006 bacpy(&hcon->dst, &bredr_hcon->dst);
4007
4008 hci_conn_hold(hcon);
4009 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004010 hci_conn_drop(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004011
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004012 hci_conn_add_sysfs(hcon);
4013
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +02004014 amp_physical_cfm(bredr_hcon, hcon);
4015
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004016 hci_dev_unlock(hdev);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004017}
4018
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004019static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
4020{
4021 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
4022 struct hci_conn *hcon;
4023 struct hci_chan *hchan;
4024 struct amp_mgr *mgr;
4025
4026 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
4027 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
4028 ev->status);
4029
4030 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4031 if (!hcon)
4032 return;
4033
4034 /* Create AMP hchan */
4035 hchan = hci_chan_create(hcon);
4036 if (!hchan)
4037 return;
4038
4039 hchan->handle = le16_to_cpu(ev->handle);
4040
4041 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
4042
4043 mgr = hcon->amp_mgr;
4044 if (mgr && mgr->bredr_chan) {
4045 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
4046
4047 l2cap_chan_lock(bredr_chan);
4048
4049 bredr_chan->conn->mtu = hdev->block_mtu;
4050 l2cap_logical_cfm(bredr_chan, hchan, 0);
4051 hci_conn_hold(hcon);
4052
4053 l2cap_chan_unlock(bredr_chan);
4054 }
4055}
4056
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02004057static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
4058 struct sk_buff *skb)
4059{
4060 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
4061 struct hci_chan *hchan;
4062
4063 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
4064 le16_to_cpu(ev->handle), ev->status);
4065
4066 if (ev->status)
4067 return;
4068
4069 hci_dev_lock(hdev);
4070
4071 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
4072 if (!hchan)
4073 goto unlock;
4074
4075 amp_destroy_logical_link(hchan, ev->reason);
4076
4077unlock:
4078 hci_dev_unlock(hdev);
4079}
4080
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02004081static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
4082 struct sk_buff *skb)
4083{
4084 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
4085 struct hci_conn *hcon;
4086
4087 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4088
4089 if (ev->status)
4090 return;
4091
4092 hci_dev_lock(hdev);
4093
4094 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4095 if (hcon) {
4096 hcon->state = BT_CLOSED;
4097 hci_conn_del(hcon);
4098 }
4099
4100 hci_dev_unlock(hdev);
4101}
4102
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004103static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03004104{
4105 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
Johan Hedberg912b42e2014-07-03 19:33:49 +03004106 struct hci_conn_params *params;
Ville Tervofcd89c02011-02-10 22:38:47 -03004107 struct hci_conn *conn;
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02004108 struct smp_irk *irk;
Johan Hedberg837d5022014-07-02 09:36:22 +03004109 u8 addr_type;
Ville Tervofcd89c02011-02-10 22:38:47 -03004110
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004111 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Ville Tervofcd89c02011-02-10 22:38:47 -03004112
4113 hci_dev_lock(hdev);
4114
Johan Hedbergfbd96c12014-07-08 17:21:51 +03004115 /* All controllers implicitly stop advertising in the event of a
4116 * connection, so ensure that the state bit is cleared.
4117 */
4118 clear_bit(HCI_LE_ADV, &hdev->dev_flags);
4119
Andre Guedesb47a09b2012-07-27 15:10:15 -03004120 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
Ville Tervob62f3282011-02-10 22:38:50 -03004121 if (!conn) {
4122 conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr);
4123 if (!conn) {
4124 BT_ERR("No memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03004125 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03004126 }
Andre Guedes29b79882011-05-31 14:20:54 -03004127
4128 conn->dst_type = ev->bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03004129
4130 if (ev->role == LE_CONN_ROLE_MASTER) {
4131 conn->out = true;
Johan Hedberg4dae2792014-06-24 17:03:50 +03004132 set_bit(HCI_CONN_MASTER, &conn->flags);
Andre Guedesb9b343d2012-07-27 15:10:11 -03004133 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02004134
4135 /* If we didn't have a hci_conn object previously
4136 * but we're in master role this must be something
4137 * initiated using a white list. Since white list based
4138 * connections are not "first class citizens" we don't
4139 * have full tracking of them. Therefore, we go ahead
4140 * with a "best effort" approach of determining the
4141 * initiator address based on the HCI_PRIVACY flag.
4142 */
4143 if (conn->out) {
4144 conn->resp_addr_type = ev->bdaddr_type;
4145 bacpy(&conn->resp_addr, &ev->bdaddr);
4146 if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) {
4147 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
4148 bacpy(&conn->init_addr, &hdev->rpa);
4149 } else {
4150 hci_copy_identity_address(hdev,
4151 &conn->init_addr,
4152 &conn->init_addr_type);
4153 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02004154 }
Johan Hedberg9489eca2014-02-28 17:45:46 +02004155 } else {
4156 cancel_delayed_work(&conn->le_conn_timeout);
Ville Tervob62f3282011-02-10 22:38:50 -03004157 }
Ville Tervofcd89c02011-02-10 22:38:47 -03004158
Johan Hedberg80c24ab2014-03-24 20:21:51 +02004159 if (!conn->out) {
4160 /* Set the responder (our side) address type based on
4161 * the advertising address type.
4162 */
4163 conn->resp_addr_type = hdev->adv_addr_type;
4164 if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM)
4165 bacpy(&conn->resp_addr, &hdev->random_addr);
4166 else
4167 bacpy(&conn->resp_addr, &hdev->bdaddr);
4168
4169 conn->init_addr_type = ev->bdaddr_type;
4170 bacpy(&conn->init_addr, &ev->bdaddr);
Marcel Holtmanna720d732014-06-23 12:14:51 +02004171
4172 /* For incoming connections, set the default minimum
4173 * and maximum connection interval. They will be used
4174 * to check if the parameters are in range and if not
4175 * trigger the connection update procedure.
4176 */
4177 conn->le_conn_min_interval = hdev->le_conn_min_interval;
4178 conn->le_conn_max_interval = hdev->le_conn_max_interval;
Johan Hedberg80c24ab2014-03-24 20:21:51 +02004179 }
Johan Hedberg7be2edb2014-02-23 19:42:17 +02004180
Marcel Holtmannedb4b462014-02-18 15:13:43 -08004181 /* Lookup the identity address from the stored connection
4182 * address and address type.
4183 *
4184 * When establishing connections to an identity address, the
4185 * connection procedure will store the resolvable random
4186 * address first. Now if it can be converted back into the
4187 * identity address, start using the identity address from
4188 * now on.
4189 */
4190 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02004191 if (irk) {
4192 bacpy(&conn->dst, &irk->bdaddr);
4193 conn->dst_type = irk->addr_type;
4194 }
4195
Johan Hedberg837d5022014-07-02 09:36:22 +03004196 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
4197 addr_type = BDADDR_LE_PUBLIC;
4198 else
4199 addr_type = BDADDR_LE_RANDOM;
4200
4201 /* Drop the connection if he device is blocked */
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004202 if (hci_bdaddr_list_lookup(&hdev->blacklist, &conn->dst, addr_type)) {
Johan Hedberg837d5022014-07-02 09:36:22 +03004203 hci_conn_drop(conn);
4204 goto unlock;
4205 }
4206
Andre Guedescd17dec2012-07-27 15:10:16 -03004207 if (ev->status) {
Andre Guedes06c053f2014-02-26 20:21:41 -03004208 hci_le_conn_failed(conn, ev->status);
Andre Guedescd17dec2012-07-27 15:10:16 -03004209 goto unlock;
4210 }
4211
Johan Hedbergb644ba32012-01-17 21:48:47 +02004212 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Marcel Holtmann01fdb0f2014-02-18 14:22:19 -08004213 mgmt_device_connected(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004214 conn->dst_type, 0, NULL, 0, NULL);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03004215
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03004216 conn->sec_level = BT_SECURITY_LOW;
Ville Tervofcd89c02011-02-10 22:38:47 -03004217 conn->handle = __le16_to_cpu(ev->handle);
4218 conn->state = BT_CONNECTED;
4219
Marcel Holtmanne04fde62014-06-23 11:40:04 +02004220 conn->le_conn_interval = le16_to_cpu(ev->interval);
4221 conn->le_conn_latency = le16_to_cpu(ev->latency);
4222 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
4223
Ville Tervofcd89c02011-02-10 22:38:47 -03004224 hci_conn_add_sysfs(conn);
4225
4226 hci_proto_connect_cfm(conn, ev->status);
4227
Johan Hedberg912b42e2014-07-03 19:33:49 +03004228 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
Johan Hedberg223683a2014-07-06 15:44:23 +03004229 if (params)
Johan Hedberg95305ba2014-07-04 12:37:21 +03004230 list_del_init(&params->action);
Andre Guedesa4790db2014-02-26 20:21:47 -03004231
Ville Tervofcd89c02011-02-10 22:38:47 -03004232unlock:
Johan Hedberg223683a2014-07-06 15:44:23 +03004233 hci_update_background_scan(hdev);
Ville Tervofcd89c02011-02-10 22:38:47 -03004234 hci_dev_unlock(hdev);
4235}
4236
Marcel Holtmann1855d922014-06-23 11:40:05 +02004237static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
4238 struct sk_buff *skb)
4239{
4240 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
4241 struct hci_conn *conn;
4242
4243 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4244
4245 if (ev->status)
4246 return;
4247
4248 hci_dev_lock(hdev);
4249
4250 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4251 if (conn) {
4252 conn->le_conn_interval = le16_to_cpu(ev->interval);
4253 conn->le_conn_latency = le16_to_cpu(ev->latency);
4254 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
4255 }
4256
4257 hci_dev_unlock(hdev);
4258}
4259
Andre Guedesa4790db2014-02-26 20:21:47 -03004260/* This function requires the caller holds hdev->lock */
Johan Hedberg7e899c92014-07-07 12:45:55 +03004261static void check_pending_le_conn(struct hci_dev *hdev, bdaddr_t *addr,
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004262 u8 addr_type, u8 adv_type)
Andre Guedesa4790db2014-02-26 20:21:47 -03004263{
4264 struct hci_conn *conn;
4265
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004266 /* If the event is not connectable don't proceed further */
4267 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
Johan Hedberg7e899c92014-07-07 12:45:55 +03004268 return;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004269
4270 /* Ignore if the device is blocked */
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004271 if (hci_bdaddr_list_lookup(&hdev->blacklist, addr, addr_type))
Johan Hedberg7e899c92014-07-07 12:45:55 +03004272 return;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004273
4274 /* If we're connectable, always connect any ADV_DIRECT_IND event */
4275 if (test_bit(HCI_CONNECTABLE, &hdev->dev_flags) &&
4276 adv_type == LE_ADV_DIRECT_IND)
4277 goto connect;
4278
4279 /* If we're not connectable only connect devices that we have in
4280 * our pend_le_conns list.
4281 */
Johan Hedberg501f8822014-07-04 12:37:26 +03004282 if (!hci_pend_le_action_lookup(&hdev->pend_le_conns, addr, addr_type))
Johan Hedberg7e899c92014-07-07 12:45:55 +03004283 return;
Andre Guedesa4790db2014-02-26 20:21:47 -03004284
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004285connect:
Johan Hedbergcdd62752014-07-07 15:02:28 +03004286 /* Request connection in master = true role */
Andre Guedesa4790db2014-02-26 20:21:47 -03004287 conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
Johan Hedbergcdd62752014-07-07 15:02:28 +03004288 HCI_LE_AUTOCONN_TIMEOUT, true);
Andre Guedesa4790db2014-02-26 20:21:47 -03004289 if (!IS_ERR(conn))
Johan Hedberg7e899c92014-07-07 12:45:55 +03004290 return;
Andre Guedesa4790db2014-02-26 20:21:47 -03004291
4292 switch (PTR_ERR(conn)) {
4293 case -EBUSY:
4294 /* If hci_connect() returns -EBUSY it means there is already
4295 * an LE connection attempt going on. Since controllers don't
4296 * support more than one connection attempt at the time, we
4297 * don't consider this an error case.
4298 */
4299 break;
4300 default:
4301 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
4302 }
4303}
4304
Johan Hedberg4af605d2014-03-24 10:48:00 +02004305static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
4306 u8 bdaddr_type, s8 rssi, u8 *data, u8 len)
4307{
Johan Hedbergb9a63282014-03-25 10:51:52 +02004308 struct discovery_state *d = &hdev->discovery;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004309 struct smp_irk *irk;
Johan Hedberg474ee062014-03-25 14:34:59 +02004310 bool match;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004311 u32 flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02004312
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004313 /* Check if we need to convert to identity address */
4314 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
4315 if (irk) {
4316 bdaddr = &irk->bdaddr;
4317 bdaddr_type = irk->addr_type;
4318 }
4319
4320 /* Check if we have been requested to connect to this device */
4321 check_pending_le_conn(hdev, bdaddr, bdaddr_type, type);
4322
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004323 /* Passive scanning shouldn't trigger any device found events,
4324 * except for devices marked as CONN_REPORT for which we do send
4325 * device found events.
4326 */
Johan Hedbergca5c4be2014-03-25 10:30:46 +02004327 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004328 struct hci_conn_params *param;
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004329
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004330 if (type == LE_ADV_DIRECT_IND)
4331 return;
4332
Johan Hedberga7545f22014-07-04 12:37:27 +03004333 param = hci_pend_le_action_lookup(&hdev->pend_le_reports,
4334 bdaddr, bdaddr_type);
4335 if (!param)
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004336 return;
4337
4338 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
4339 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
4340 else
4341 flags = 0;
4342 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
4343 rssi, flags, data, len, NULL, 0);
Johan Hedberg97bf2e92014-07-04 12:37:16 +03004344 return;
Johan Hedbergca5c4be2014-03-25 10:30:46 +02004345 }
Johan Hedberg4af605d2014-03-24 10:48:00 +02004346
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004347 /* When receiving non-connectable or scannable undirected
4348 * advertising reports, this means that the remote device is
4349 * not connectable and then clearly indicate this in the
4350 * device found event.
4351 *
4352 * When receiving a scan response, then there is no way to
4353 * know if the remote device is connectable or not. However
4354 * since scan responses are merged with a previously seen
4355 * advertising report, the flags field from that report
4356 * will be used.
4357 *
4358 * In the really unlikely case that a controller get confused
4359 * and just sends a scan response event, then it is marked as
4360 * not connectable as well.
4361 */
4362 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
4363 type == LE_ADV_SCAN_RSP)
4364 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
4365 else
4366 flags = 0;
4367
Johan Hedbergb9a63282014-03-25 10:51:52 +02004368 /* If there's nothing pending either store the data from this
4369 * event or send an immediate device found event if the data
4370 * should not be stored for later.
4371 */
4372 if (!has_pending_adv_report(hdev)) {
4373 /* If the report will trigger a SCAN_REQ store it for
4374 * later merging.
4375 */
4376 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4377 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004378 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004379 return;
4380 }
4381
4382 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004383 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004384 return;
4385 }
4386
Johan Hedberg474ee062014-03-25 14:34:59 +02004387 /* Check if the pending report is for the same device as the new one */
4388 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
4389 bdaddr_type == d->last_adv_addr_type);
4390
Johan Hedbergb9a63282014-03-25 10:51:52 +02004391 /* If the pending data doesn't match this report or this isn't a
4392 * scan response (e.g. we got a duplicate ADV_IND) then force
4393 * sending of the pending data.
4394 */
Johan Hedberg474ee062014-03-25 14:34:59 +02004395 if (type != LE_ADV_SCAN_RSP || !match) {
4396 /* Send out whatever is in the cache, but skip duplicates */
4397 if (!match)
4398 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004399 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004400 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004401 d->last_adv_data,
Johan Hedberg474ee062014-03-25 14:34:59 +02004402 d->last_adv_data_len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004403
4404 /* If the new report will trigger a SCAN_REQ store it for
4405 * later merging.
4406 */
4407 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4408 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004409 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004410 return;
4411 }
4412
4413 /* The advertising reports cannot be merged, so clear
4414 * the pending report and send out a device found event.
4415 */
4416 clear_pending_adv_report(hdev);
Johan Hedberg5c5b93e2014-03-29 08:39:53 +02004417 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004418 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004419 return;
4420 }
4421
4422 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
4423 * the new event is a SCAN_RSP. We can therefore proceed with
4424 * sending a merged device found event.
4425 */
4426 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004427 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
Marcel Holtmann42bd6a52014-07-01 14:11:19 +02004428 d->last_adv_data, d->last_adv_data_len, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004429 clear_pending_adv_report(hdev);
Johan Hedberg4af605d2014-03-24 10:48:00 +02004430}
4431
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004432static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03004433{
Andre Guedese95beb42011-09-26 20:48:35 -03004434 u8 num_reports = skb->data[0];
4435 void *ptr = &skb->data[1];
Andre Guedes9aa04c92011-05-26 16:23:51 -03004436
Andre Guedesa4790db2014-02-26 20:21:47 -03004437 hci_dev_lock(hdev);
4438
Andre Guedese95beb42011-09-26 20:48:35 -03004439 while (num_reports--) {
4440 struct hci_ev_le_advertising_info *ev = ptr;
Johan Hedberg4af605d2014-03-24 10:48:00 +02004441 s8 rssi;
Andre Guedesa4790db2014-02-26 20:21:47 -03004442
Andre Guedes3c9e9192012-01-10 18:20:50 -03004443 rssi = ev->data[ev->length];
Johan Hedberg4af605d2014-03-24 10:48:00 +02004444 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
4445 ev->bdaddr_type, rssi, ev->data, ev->length);
Andre Guedes3c9e9192012-01-10 18:20:50 -03004446
Andre Guedese95beb42011-09-26 20:48:35 -03004447 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03004448 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004449
4450 hci_dev_unlock(hdev);
Andre Guedes9aa04c92011-05-26 16:23:51 -03004451}
4452
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004453static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004454{
4455 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
4456 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004457 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004458 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004459 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004460
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004461 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004462
4463 hci_dev_lock(hdev);
4464
4465 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004466 if (conn == NULL)
4467 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004468
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08004469 ltk = hci_find_ltk(hdev, ev->ediv, ev->rand, conn->out);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004470 if (ltk == NULL)
4471 goto not_found;
4472
4473 memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004474 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004475
4476 if (ltk->authenticated)
Andre Guedesf8776212013-07-31 16:25:28 -03004477 conn->pending_sec_level = BT_SECURITY_HIGH;
4478 else
4479 conn->pending_sec_level = BT_SECURITY_MEDIUM;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004480
Andre Guedes89cbb4d2013-07-31 16:25:29 -03004481 conn->enc_key_size = ltk->enc_size;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004482
4483 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
4484
Claudio Takahasi5981a882013-07-25 16:34:24 -03004485 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
4486 * temporary key used to encrypt a connection following
4487 * pairing. It is used during the Encrypted Session Setup to
4488 * distribute the keys. Later, security can be re-established
4489 * using a distributed LTK.
4490 */
Johan Hedberg2ceba532014-06-16 19:25:16 +03004491 if (ltk->type == SMP_STK) {
Johan Hedbergfe59a052014-07-01 19:14:12 +03004492 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004493 list_del(&ltk->list);
4494 kfree(ltk);
Johan Hedbergfe59a052014-07-01 19:14:12 +03004495 } else {
4496 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004497 }
4498
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004499 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004500
4501 return;
4502
4503not_found:
4504 neg.handle = ev->handle;
4505 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
4506 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004507}
4508
Andre Guedes8e75b462014-07-01 18:10:08 -03004509static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
4510 u8 reason)
4511{
4512 struct hci_cp_le_conn_param_req_neg_reply cp;
4513
4514 cp.handle = cpu_to_le16(handle);
4515 cp.reason = reason;
4516
4517 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
4518 &cp);
4519}
4520
4521static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
4522 struct sk_buff *skb)
4523{
4524 struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
4525 struct hci_cp_le_conn_param_req_reply cp;
4526 struct hci_conn *hcon;
4527 u16 handle, min, max, latency, timeout;
4528
4529 handle = le16_to_cpu(ev->handle);
4530 min = le16_to_cpu(ev->interval_min);
4531 max = le16_to_cpu(ev->interval_max);
4532 latency = le16_to_cpu(ev->latency);
4533 timeout = le16_to_cpu(ev->timeout);
4534
4535 hcon = hci_conn_hash_lookup_handle(hdev, handle);
4536 if (!hcon || hcon->state != BT_CONNECTED)
4537 return send_conn_param_neg_reply(hdev, handle,
4538 HCI_ERROR_UNKNOWN_CONN_ID);
4539
4540 if (hci_check_conn_params(min, max, latency, timeout))
4541 return send_conn_param_neg_reply(hdev, handle,
4542 HCI_ERROR_INVALID_LL_PARAMS);
4543
Johan Hedberg348d50b2014-07-02 17:37:30 +03004544 if (test_bit(HCI_CONN_MASTER, &hcon->flags)) {
4545 struct hci_conn_params *params;
Johan Hedbergf4869e22014-07-02 17:37:32 +03004546 u8 store_hint;
Johan Hedberg348d50b2014-07-02 17:37:30 +03004547
4548 hci_dev_lock(hdev);
4549
4550 params = hci_conn_params_lookup(hdev, &hcon->dst,
4551 hcon->dst_type);
4552 if (params) {
4553 params->conn_min_interval = min;
4554 params->conn_max_interval = max;
4555 params->conn_latency = latency;
4556 params->supervision_timeout = timeout;
Johan Hedbergf4869e22014-07-02 17:37:32 +03004557 store_hint = 0x01;
4558 } else{
4559 store_hint = 0x00;
Johan Hedberg348d50b2014-07-02 17:37:30 +03004560 }
4561
4562 hci_dev_unlock(hdev);
4563
Johan Hedbergf4869e22014-07-02 17:37:32 +03004564 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
4565 store_hint, min, max, latency, timeout);
Johan Hedberg348d50b2014-07-02 17:37:30 +03004566 }
Andre Guedesffb5a8272014-07-01 18:10:11 -03004567
Andre Guedes8e75b462014-07-01 18:10:08 -03004568 cp.handle = ev->handle;
4569 cp.interval_min = ev->interval_min;
4570 cp.interval_max = ev->interval_max;
4571 cp.latency = ev->latency;
4572 cp.timeout = ev->timeout;
4573 cp.min_ce_len = 0;
4574 cp.max_ce_len = 0;
4575
4576 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
4577}
4578
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004579static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03004580{
4581 struct hci_ev_le_meta *le_ev = (void *) skb->data;
4582
4583 skb_pull(skb, sizeof(*le_ev));
4584
4585 switch (le_ev->subevent) {
4586 case HCI_EV_LE_CONN_COMPLETE:
4587 hci_le_conn_complete_evt(hdev, skb);
4588 break;
4589
Marcel Holtmann1855d922014-06-23 11:40:05 +02004590 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
4591 hci_le_conn_update_complete_evt(hdev, skb);
4592 break;
4593
Andre Guedes9aa04c92011-05-26 16:23:51 -03004594 case HCI_EV_LE_ADVERTISING_REPORT:
4595 hci_le_adv_report_evt(hdev, skb);
4596 break;
4597
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004598 case HCI_EV_LE_LTK_REQ:
4599 hci_le_ltk_request_evt(hdev, skb);
4600 break;
4601
Andre Guedes8e75b462014-07-01 18:10:08 -03004602 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
4603 hci_le_remote_conn_param_req_evt(hdev, skb);
4604 break;
4605
Ville Tervofcd89c02011-02-10 22:38:47 -03004606 default:
4607 break;
4608 }
4609}
4610
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +03004611static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
4612{
4613 struct hci_ev_channel_selected *ev = (void *) skb->data;
4614 struct hci_conn *hcon;
4615
4616 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
4617
4618 skb_pull(skb, sizeof(*ev));
4619
4620 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4621 if (!hcon)
4622 return;
4623
4624 amp_read_loc_assoc_final_data(hdev, hcon);
4625}
4626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
4628{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004629 struct hci_event_hdr *hdr = (void *) skb->data;
4630 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631
Johan Hedbergb6ddb632013-04-02 13:34:31 +03004632 hci_dev_lock(hdev);
4633
4634 /* Received events are (currently) only needed when a request is
4635 * ongoing so avoid unnecessary memory allocation.
4636 */
4637 if (hdev->req_status == HCI_REQ_PEND) {
4638 kfree_skb(hdev->recv_evt);
4639 hdev->recv_evt = skb_clone(skb, GFP_KERNEL);
4640 }
4641
4642 hci_dev_unlock(hdev);
4643
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 skb_pull(skb, HCI_EVENT_HDR_SIZE);
4645
Johan Hedberg02350a72013-04-03 21:50:29 +03004646 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02004647 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
4648 u16 opcode = __le16_to_cpu(cmd_hdr->opcode);
Johan Hedberg02350a72013-04-03 21:50:29 +03004649
4650 hci_req_cmd_complete(hdev, opcode, 0);
4651 }
4652
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004653 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654 case HCI_EV_INQUIRY_COMPLETE:
4655 hci_inquiry_complete_evt(hdev, skb);
4656 break;
4657
4658 case HCI_EV_INQUIRY_RESULT:
4659 hci_inquiry_result_evt(hdev, skb);
4660 break;
4661
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004662 case HCI_EV_CONN_COMPLETE:
4663 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02004664 break;
4665
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 case HCI_EV_CONN_REQUEST:
4667 hci_conn_request_evt(hdev, skb);
4668 break;
4669
Linus Torvalds1da177e2005-04-16 15:20:36 -07004670 case HCI_EV_DISCONN_COMPLETE:
4671 hci_disconn_complete_evt(hdev, skb);
4672 break;
4673
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 case HCI_EV_AUTH_COMPLETE:
4675 hci_auth_complete_evt(hdev, skb);
4676 break;
4677
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004678 case HCI_EV_REMOTE_NAME:
4679 hci_remote_name_evt(hdev, skb);
4680 break;
4681
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 case HCI_EV_ENCRYPT_CHANGE:
4683 hci_encrypt_change_evt(hdev, skb);
4684 break;
4685
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004686 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
4687 hci_change_link_key_complete_evt(hdev, skb);
4688 break;
4689
4690 case HCI_EV_REMOTE_FEATURES:
4691 hci_remote_features_evt(hdev, skb);
4692 break;
4693
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004694 case HCI_EV_CMD_COMPLETE:
4695 hci_cmd_complete_evt(hdev, skb);
4696 break;
4697
4698 case HCI_EV_CMD_STATUS:
4699 hci_cmd_status_evt(hdev, skb);
4700 break;
4701
4702 case HCI_EV_ROLE_CHANGE:
4703 hci_role_change_evt(hdev, skb);
4704 break;
4705
4706 case HCI_EV_NUM_COMP_PKTS:
4707 hci_num_comp_pkts_evt(hdev, skb);
4708 break;
4709
4710 case HCI_EV_MODE_CHANGE:
4711 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 break;
4713
4714 case HCI_EV_PIN_CODE_REQ:
4715 hci_pin_code_request_evt(hdev, skb);
4716 break;
4717
4718 case HCI_EV_LINK_KEY_REQ:
4719 hci_link_key_request_evt(hdev, skb);
4720 break;
4721
4722 case HCI_EV_LINK_KEY_NOTIFY:
4723 hci_link_key_notify_evt(hdev, skb);
4724 break;
4725
4726 case HCI_EV_CLOCK_OFFSET:
4727 hci_clock_offset_evt(hdev, skb);
4728 break;
4729
Marcel Holtmanna8746412008-07-14 20:13:46 +02004730 case HCI_EV_PKT_TYPE_CHANGE:
4731 hci_pkt_type_change_evt(hdev, skb);
4732 break;
4733
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004734 case HCI_EV_PSCAN_REP_MODE:
4735 hci_pscan_rep_mode_evt(hdev, skb);
4736 break;
4737
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004738 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
4739 hci_inquiry_result_with_rssi_evt(hdev, skb);
4740 break;
4741
4742 case HCI_EV_REMOTE_EXT_FEATURES:
4743 hci_remote_ext_features_evt(hdev, skb);
4744 break;
4745
4746 case HCI_EV_SYNC_CONN_COMPLETE:
4747 hci_sync_conn_complete_evt(hdev, skb);
4748 break;
4749
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004750 case HCI_EV_EXTENDED_INQUIRY_RESULT:
4751 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 break;
4753
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004754 case HCI_EV_KEY_REFRESH_COMPLETE:
4755 hci_key_refresh_complete_evt(hdev, skb);
4756 break;
4757
Marcel Holtmann04936842008-07-14 20:13:48 +02004758 case HCI_EV_IO_CAPA_REQUEST:
4759 hci_io_capa_request_evt(hdev, skb);
4760 break;
4761
Johan Hedberg03b555e2011-01-04 15:40:05 +02004762 case HCI_EV_IO_CAPA_REPLY:
4763 hci_io_capa_reply_evt(hdev, skb);
4764 break;
4765
Johan Hedberga5c29682011-02-19 12:05:57 -03004766 case HCI_EV_USER_CONFIRM_REQUEST:
4767 hci_user_confirm_request_evt(hdev, skb);
4768 break;
4769
Brian Gix1143d452011-11-23 08:28:34 -08004770 case HCI_EV_USER_PASSKEY_REQUEST:
4771 hci_user_passkey_request_evt(hdev, skb);
4772 break;
4773
Johan Hedberg92a25252012-09-06 18:39:26 +03004774 case HCI_EV_USER_PASSKEY_NOTIFY:
4775 hci_user_passkey_notify_evt(hdev, skb);
4776 break;
4777
4778 case HCI_EV_KEYPRESS_NOTIFY:
4779 hci_keypress_notify_evt(hdev, skb);
4780 break;
4781
Marcel Holtmann04936842008-07-14 20:13:48 +02004782 case HCI_EV_SIMPLE_PAIR_COMPLETE:
4783 hci_simple_pair_complete_evt(hdev, skb);
4784 break;
4785
Marcel Holtmann41a96212008-07-14 20:13:48 +02004786 case HCI_EV_REMOTE_HOST_FEATURES:
4787 hci_remote_host_features_evt(hdev, skb);
4788 break;
4789
Ville Tervofcd89c02011-02-10 22:38:47 -03004790 case HCI_EV_LE_META:
4791 hci_le_meta_evt(hdev, skb);
4792 break;
4793
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +03004794 case HCI_EV_CHANNEL_SELECTED:
4795 hci_chan_selected_evt(hdev, skb);
4796 break;
4797
Szymon Janc2763eda2011-03-22 13:12:22 +01004798 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
4799 hci_remote_oob_data_request_evt(hdev, skb);
4800 break;
4801
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004802 case HCI_EV_PHY_LINK_COMPLETE:
4803 hci_phy_link_complete_evt(hdev, skb);
4804 break;
4805
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004806 case HCI_EV_LOGICAL_LINK_COMPLETE:
4807 hci_loglink_complete_evt(hdev, skb);
4808 break;
4809
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02004810 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
4811 hci_disconn_loglink_complete_evt(hdev, skb);
4812 break;
4813
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02004814 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
4815 hci_disconn_phylink_complete_evt(hdev, skb);
4816 break;
4817
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004818 case HCI_EV_NUM_COMP_BLOCKS:
4819 hci_num_comp_blocks_evt(hdev, skb);
4820 break;
4821
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004822 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004823 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 break;
4825 }
4826
4827 kfree_skb(skb);
4828 hdev->stat.evt_rx++;
4829}