blob: a412eb1e1f6143283737013064581e71f9361dac [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI event handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/unaligned.h>
28
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
Mikel Astizf0d6a0e2012-08-09 09:52:30 +020031#include <net/bluetooth/mgmt.h>
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070032
Johan Hedberg0857dd32014-12-19 13:40:20 +020033#include "hci_request.h"
Marcel Holtmann70247282013-10-10 14:54:15 -070034#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070035#include "amp.h"
Johan Hedberg2ceba532014-06-16 19:25:16 +030036#include "smp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038/* Handle HCI Event packets */
39
Marcel Holtmanna9de9242007-10-20 13:33:56 +020040static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020042 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030044 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Andre Guedes82f47852013-04-30 15:29:34 -030046 if (status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020047 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Andre Guedes89352e72011-11-04 14:16:53 -030049 clear_bit(HCI_INQUIRY, &hdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010050 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -030051 wake_up_bit(&hdev->flags, HCI_INQUIRY);
Andre Guedes89352e72011-11-04 14:16:53 -030052
Johan Hedberg50143a42014-06-10 14:05:57 +030053 hci_dev_lock(hdev);
54 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
55 hci_dev_unlock(hdev);
56
Marcel Holtmanna9de9242007-10-20 13:33:56 +020057 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058}
59
Andre Guedes4d934832012-03-21 00:03:35 -030060static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
61{
62 __u8 status = *((__u8 *) skb->data);
63
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030064 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesae854a72012-03-21 00:03:36 -030065
66 if (status)
67 return;
68
69 set_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
Andre Guedes4d934832012-03-21 00:03:35 -030070}
71
Marcel Holtmanna9de9242007-10-20 13:33:56 +020072static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020074 __u8 status = *((__u8 *) skb->data);
75
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030076 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +020077
78 if (status)
79 return;
80
Andre Guedesae854a72012-03-21 00:03:36 -030081 clear_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
82
Marcel Holtmanna9de9242007-10-20 13:33:56 +020083 hci_conn_check_pending(hdev);
84}
85
Gustavo Padovan807deac2012-05-17 00:36:24 -030086static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
87 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020088{
89 BT_DBG("%s", hdev->name);
90}
91
92static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
93{
94 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030097 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Marcel Holtmanna9de9242007-10-20 13:33:56 +020099 if (rp->status)
100 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200102 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200104 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Johan Hedberg40bef302014-07-16 11:42:27 +0300105 if (conn)
106 conn->role = rp->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200107
108 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200111static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
112{
113 struct hci_rp_read_link_policy *rp = (void *) skb->data;
114 struct hci_conn *conn;
115
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300116 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200117
118 if (rp->status)
119 return;
120
121 hci_dev_lock(hdev);
122
123 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
124 if (conn)
125 conn->link_policy = __le16_to_cpu(rp->policy);
126
127 hci_dev_unlock(hdev);
128}
129
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200130static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200132 struct hci_rp_write_link_policy *rp = (void *) skb->data;
133 struct hci_conn *conn;
134 void *sent;
135
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300136 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200137
138 if (rp->status)
139 return;
140
141 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
142 if (!sent)
143 return;
144
145 hci_dev_lock(hdev);
146
147 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200148 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700149 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200150
151 hci_dev_unlock(hdev);
152}
153
Gustavo Padovan807deac2012-05-17 00:36:24 -0300154static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
155 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200156{
157 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
158
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300159 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200160
161 if (rp->status)
162 return;
163
164 hdev->link_policy = __le16_to_cpu(rp->policy);
165}
166
Gustavo Padovan807deac2012-05-17 00:36:24 -0300167static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
168 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200169{
170 __u8 status = *((__u8 *) skb->data);
171 void *sent;
172
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300173 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200174
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200175 if (status)
176 return;
177
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200178 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
179 if (!sent)
180 return;
181
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200182 hdev->link_policy = get_unaligned_le16(sent);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200183}
184
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200185static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
186{
187 __u8 status = *((__u8 *) skb->data);
188
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300189 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200190
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300191 clear_bit(HCI_RESET, &hdev->flags);
192
Marcel Holtmann8761f9d2014-11-02 02:45:58 +0100193 if (status)
194 return;
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 Holtmanna4d55042014-10-29 23:37:53 +0100212
213 hci_bdaddr_list_clear(&hdev->le_white_list);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200214}
215
216static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
217{
218 __u8 status = *((__u8 *) skb->data);
219 void *sent;
220
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300221 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200222
223 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
224 if (!sent)
225 return;
226
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200227 hci_dev_lock(hdev);
228
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200229 if (test_bit(HCI_MGMT, &hdev->dev_flags))
230 mgmt_set_local_name_complete(hdev, sent, status);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +0200231 else if (!status)
232 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200233
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200234 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200235}
236
237static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
238{
239 struct hci_rp_read_local_name *rp = (void *) skb->data;
240
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300241 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200242
243 if (rp->status)
244 return;
245
Johan Hedbergdb99b5f2012-02-22 20:14:22 +0200246 if (test_bit(HCI_SETUP, &hdev->dev_flags))
247 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200248}
249
250static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
251{
252 __u8 status = *((__u8 *) skb->data);
253 void *sent;
254
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300255 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200256
257 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
258 if (!sent)
259 return;
260
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530261 hci_dev_lock(hdev);
262
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200263 if (!status) {
264 __u8 param = *((__u8 *) sent);
265
266 if (param == AUTH_ENABLED)
267 set_bit(HCI_AUTH, &hdev->flags);
268 else
269 clear_bit(HCI_AUTH, &hdev->flags);
270 }
271
Johan Hedberg33ef95e2012-02-16 23:56:27 +0200272 if (test_bit(HCI_MGMT, &hdev->dev_flags))
273 mgmt_auth_enable_complete(hdev, status);
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530274
275 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200276}
277
278static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
279{
280 __u8 status = *((__u8 *) skb->data);
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200281 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200282 void *sent;
283
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300284 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200285
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200286 if (status)
287 return;
288
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200289 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
290 if (!sent)
291 return;
292
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200293 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200294
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200295 if (param)
296 set_bit(HCI_ENCRYPT, &hdev->flags);
297 else
298 clear_bit(HCI_ENCRYPT, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200299}
300
301static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
302{
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200303 __u8 status = *((__u8 *) skb->data);
304 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200305 void *sent;
306
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300307 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200308
309 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
310 if (!sent)
311 return;
312
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200313 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200314
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200315 hci_dev_lock(hdev);
316
Mikel Astizfa1bd912012-08-09 09:52:29 +0200317 if (status) {
Johan Hedberg2d7cee52011-11-07 22:16:03 +0200318 hdev->discov_timeout = 0;
319 goto done;
320 }
321
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300322 if (param & SCAN_INQUIRY)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200323 set_bit(HCI_ISCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300324 else
325 clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200326
Johan Hedberg031547d2014-07-10 12:09:06 +0300327 if (param & SCAN_PAGE)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200328 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300329 else
Johan Hedberg204e3992014-07-28 15:45:31 +0300330 clear_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200331
332done:
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200333 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200334}
335
336static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
337{
338 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
339
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300340 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200341
342 if (rp->status)
343 return;
344
345 memcpy(hdev->dev_class, rp->dev_class, 3);
346
347 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300348 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200349}
350
351static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
352{
353 __u8 status = *((__u8 *) skb->data);
354 void *sent;
355
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300356 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200357
358 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
359 if (!sent)
360 return;
361
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100362 hci_dev_lock(hdev);
363
364 if (status == 0)
365 memcpy(hdev->dev_class, sent, 3);
366
367 if (test_bit(HCI_MGMT, &hdev->dev_flags))
368 mgmt_set_class_of_dev_complete(hdev, sent, status);
369
370 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200371}
372
373static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
374{
375 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200377
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300378 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200379
380 if (rp->status)
381 return;
382
383 setting = __le16_to_cpu(rp->voice_setting);
384
Marcel Holtmannf383f272008-07-14 20:13:47 +0200385 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200386 return;
387
388 hdev->voice_setting = setting;
389
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300390 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200391
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200392 if (hdev->notify)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200393 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200394}
395
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300396static void hci_cc_write_voice_setting(struct hci_dev *hdev,
397 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200398{
399 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200400 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 void *sent;
402
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300403 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Marcel Holtmannf383f272008-07-14 20:13:47 +0200405 if (status)
406 return;
407
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200408 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
409 if (!sent)
410 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Marcel Holtmannf383f272008-07-14 20:13:47 +0200412 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Marcel Holtmannf383f272008-07-14 20:13:47 +0200414 if (hdev->voice_setting == setting)
415 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Marcel Holtmannf383f272008-07-14 20:13:47 +0200417 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300419 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200420
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200421 if (hdev->notify)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200422 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700425static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
426 struct sk_buff *skb)
427{
428 struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
429
430 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
431
432 if (rp->status)
433 return;
434
435 hdev->num_iac = rp->num_iac;
436
437 BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
438}
439
Marcel Holtmann333140b2008-07-14 20:13:48 +0200440static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
441{
442 __u8 status = *((__u8 *) skb->data);
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300443 struct hci_cp_write_ssp_mode *sent;
Marcel Holtmann333140b2008-07-14 20:13:48 +0200444
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300445 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200446
Marcel Holtmann333140b2008-07-14 20:13:48 +0200447 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
448 if (!sent)
449 return;
450
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530451 hci_dev_lock(hdev);
452
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300453 if (!status) {
454 if (sent->mode)
Johan Hedbergcad718e2013-04-17 15:00:51 +0300455 hdev->features[1][0] |= LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300456 else
Johan Hedbergcad718e2013-04-17 15:00:51 +0300457 hdev->features[1][0] &= ~LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300458 }
459
Johan Hedberged2c4ee2012-02-17 00:56:28 +0200460 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300461 mgmt_ssp_enable_complete(hdev, sent->mode, status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200462 else if (!status) {
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300463 if (sent->mode)
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200464 set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
465 else
466 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
467 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530468
469 hci_dev_unlock(hdev);
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
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530483 hci_dev_lock(hdev);
484
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800485 if (!status) {
486 if (sent->support)
487 hdev->features[1][0] |= LMP_HOST_SC;
488 else
489 hdev->features[1][0] &= ~LMP_HOST_SC;
490 }
491
492 if (test_bit(HCI_MGMT, &hdev->dev_flags))
493 mgmt_sc_enable_complete(hdev, sent->support, status);
494 else if (!status) {
495 if (sent->support)
496 set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
497 else
498 clear_bit(HCI_SC_ENABLED, &hdev->dev_flags);
499 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +0530500
501 hci_dev_unlock(hdev);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800502}
503
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200504static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
505{
506 struct hci_rp_read_local_version *rp = (void *) skb->data;
507
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300508 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200509
510 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200511 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200512
Marcel Holtmann0d5551f2013-10-18 12:04:50 -0700513 if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
514 hdev->hci_ver = rp->hci_ver;
515 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
516 hdev->lmp_ver = rp->lmp_ver;
517 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
518 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
519 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200520}
521
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300522static void hci_cc_read_local_commands(struct hci_dev *hdev,
523 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200524{
525 struct hci_rp_read_local_commands *rp = (void *) skb->data;
526
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300527 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200528
Marcel Holtmann6a070e62013-10-31 04:54:33 -0700529 if (rp->status)
530 return;
531
532 if (test_bit(HCI_SETUP, &hdev->dev_flags))
Johan Hedberg2177bab2013-03-05 20:37:43 +0200533 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200534}
535
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300536static void hci_cc_read_local_features(struct hci_dev *hdev,
537 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200538{
539 struct hci_rp_read_local_features *rp = (void *) skb->data;
540
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300541 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200542
543 if (rp->status)
544 return;
545
546 memcpy(hdev->features, rp->features, 8);
547
548 /* Adjust default settings according to features
549 * supported by device. */
550
Johan Hedbergcad718e2013-04-17 15:00:51 +0300551 if (hdev->features[0][0] & LMP_3SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200552 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
553
Johan Hedbergcad718e2013-04-17 15:00:51 +0300554 if (hdev->features[0][0] & LMP_5SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200555 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
556
Johan Hedbergcad718e2013-04-17 15:00:51 +0300557 if (hdev->features[0][1] & LMP_HV2) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200558 hdev->pkt_type |= (HCI_HV2);
559 hdev->esco_type |= (ESCO_HV2);
560 }
561
Johan Hedbergcad718e2013-04-17 15:00:51 +0300562 if (hdev->features[0][1] & LMP_HV3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200563 hdev->pkt_type |= (HCI_HV3);
564 hdev->esco_type |= (ESCO_HV3);
565 }
566
Andre Guedes45db810f2012-07-24 15:03:49 -0300567 if (lmp_esco_capable(hdev))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200568 hdev->esco_type |= (ESCO_EV3);
569
Johan Hedbergcad718e2013-04-17 15:00:51 +0300570 if (hdev->features[0][4] & LMP_EV4)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200571 hdev->esco_type |= (ESCO_EV4);
572
Johan Hedbergcad718e2013-04-17 15:00:51 +0300573 if (hdev->features[0][4] & LMP_EV5)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200574 hdev->esco_type |= (ESCO_EV5);
575
Johan Hedbergcad718e2013-04-17 15:00:51 +0300576 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100577 hdev->esco_type |= (ESCO_2EV3);
578
Johan Hedbergcad718e2013-04-17 15:00:51 +0300579 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100580 hdev->esco_type |= (ESCO_3EV3);
581
Johan Hedbergcad718e2013-04-17 15:00:51 +0300582 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100583 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200584}
585
Andre Guedes971e3a42011-06-30 19:20:52 -0300586static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300587 struct sk_buff *skb)
Andre Guedes971e3a42011-06-30 19:20:52 -0300588{
589 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
590
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300591 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andre Guedes971e3a42011-06-30 19:20:52 -0300592
593 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200594 return;
Andre Guedes971e3a42011-06-30 19:20:52 -0300595
Marcel Holtmann57af75a2013-10-18 12:04:47 -0700596 if (hdev->max_page < rp->max_page)
597 hdev->max_page = rp->max_page;
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300598
Johan Hedbergcad718e2013-04-17 15:00:51 +0300599 if (rp->page < HCI_MAX_PAGES)
600 memcpy(hdev->features[rp->page], rp->features, 8);
Andre Guedes971e3a42011-06-30 19:20:52 -0300601}
602
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200603static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300604 struct sk_buff *skb)
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200605{
606 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
607
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300608 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200609
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200610 if (rp->status)
611 return;
612
613 hdev->flow_ctl_mode = rp->mode;
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200614}
615
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200616static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
617{
618 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
619
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300620 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200621
622 if (rp->status)
623 return;
624
625 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
626 hdev->sco_mtu = rp->sco_mtu;
627 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
628 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
629
630 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
631 hdev->sco_mtu = 64;
632 hdev->sco_pkts = 8;
633 }
634
635 hdev->acl_cnt = hdev->acl_pkts;
636 hdev->sco_cnt = hdev->sco_pkts;
637
Gustavo Padovan807deac2012-05-17 00:36:24 -0300638 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
639 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200640}
641
642static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
643{
644 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
645
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300646 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200647
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200648 if (rp->status)
649 return;
650
651 if (test_bit(HCI_INIT, &hdev->flags))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200652 bacpy(&hdev->bdaddr, &rp->bdaddr);
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200653
654 if (test_bit(HCI_SETUP, &hdev->dev_flags))
655 bacpy(&hdev->setup_addr, &rp->bdaddr);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200656}
657
Johan Hedbergf332ec62013-03-15 17:07:11 -0500658static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
659 struct sk_buff *skb)
660{
661 struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
662
663 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
664
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200665 if (rp->status)
666 return;
667
668 if (test_bit(HCI_INIT, &hdev->flags)) {
Johan Hedbergf332ec62013-03-15 17:07:11 -0500669 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
670 hdev->page_scan_window = __le16_to_cpu(rp->window);
671 }
672}
673
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500674static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
675 struct sk_buff *skb)
676{
677 u8 status = *((u8 *) skb->data);
678 struct hci_cp_write_page_scan_activity *sent;
679
680 BT_DBG("%s status 0x%2.2x", hdev->name, status);
681
682 if (status)
683 return;
684
685 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
686 if (!sent)
687 return;
688
689 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
690 hdev->page_scan_window = __le16_to_cpu(sent->window);
691}
692
Johan Hedbergf332ec62013-03-15 17:07:11 -0500693static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
694 struct sk_buff *skb)
695{
696 struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
697
698 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
699
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200700 if (rp->status)
701 return;
702
703 if (test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergf332ec62013-03-15 17:07:11 -0500704 hdev->page_scan_type = rp->type;
705}
706
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500707static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
708 struct sk_buff *skb)
709{
710 u8 status = *((u8 *) skb->data);
711 u8 *type;
712
713 BT_DBG("%s status 0x%2.2x", hdev->name, status);
714
715 if (status)
716 return;
717
718 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
719 if (type)
720 hdev->page_scan_type = *type;
721}
722
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200723static void hci_cc_read_data_block_size(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300724 struct sk_buff *skb)
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200725{
726 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
727
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300728 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200729
730 if (rp->status)
731 return;
732
733 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
734 hdev->block_len = __le16_to_cpu(rp->block_len);
735 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
736
737 hdev->block_cnt = hdev->num_blocks;
738
739 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300740 hdev->block_cnt, hdev->block_len);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200741}
742
Johan Hedberg33f35722014-06-28 17:54:06 +0300743static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
744{
745 struct hci_rp_read_clock *rp = (void *) skb->data;
746 struct hci_cp_read_clock *cp;
747 struct hci_conn *conn;
748
749 BT_DBG("%s", hdev->name);
750
751 if (skb->len < sizeof(*rp))
752 return;
753
754 if (rp->status)
755 return;
756
757 hci_dev_lock(hdev);
758
759 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
760 if (!cp)
761 goto unlock;
762
763 if (cp->which == 0x00) {
764 hdev->clock = le32_to_cpu(rp->clock);
765 goto unlock;
766 }
767
768 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
769 if (conn) {
770 conn->clock = le32_to_cpu(rp->clock);
771 conn->clock_accuracy = le16_to_cpu(rp->accuracy);
772 }
773
774unlock:
775 hci_dev_unlock(hdev);
776}
777
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300778static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300779 struct sk_buff *skb)
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300780{
781 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
782
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300783 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300784
785 if (rp->status)
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300786 goto a2mp_rsp;
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300787
788 hdev->amp_status = rp->amp_status;
789 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
790 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
791 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
792 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
793 hdev->amp_type = rp->amp_type;
794 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
795 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
796 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
797 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
798
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300799a2mp_rsp:
800 a2mp_send_getinfo_rsp(hdev);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300801}
802
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300803static void hci_cc_read_local_amp_assoc(struct hci_dev *hdev,
804 struct sk_buff *skb)
805{
806 struct hci_rp_read_local_amp_assoc *rp = (void *) skb->data;
807 struct amp_assoc *assoc = &hdev->loc_assoc;
808 size_t rem_len, frag_len;
809
810 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
811
812 if (rp->status)
813 goto a2mp_rsp;
814
815 frag_len = skb->len - sizeof(*rp);
816 rem_len = __le16_to_cpu(rp->rem_len);
817
818 if (rem_len > frag_len) {
Andrei Emeltchenko2e430be32012-09-28 14:44:23 +0300819 BT_DBG("frag_len %zu rem_len %zu", frag_len, rem_len);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300820
821 memcpy(assoc->data + assoc->offset, rp->frag, frag_len);
822 assoc->offset += frag_len;
823
824 /* Read other fragments */
825 amp_read_loc_assoc_frag(hdev, rp->phy_handle);
826
827 return;
828 }
829
830 memcpy(assoc->data + assoc->offset, rp->frag, rem_len);
831 assoc->len = assoc->offset + rem_len;
832 assoc->offset = 0;
833
834a2mp_rsp:
835 /* Send A2MP Rsp when all fragments are received */
836 a2mp_send_getampassoc_rsp(hdev, rp->status);
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300837 a2mp_send_create_phy_link_req(hdev, rp->status);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300838}
839
Johan Hedbergd5859e22011-01-25 01:19:58 +0200840static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300841 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200842{
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700843 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200844
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300845 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200846
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200847 if (rp->status)
848 return;
849
850 hdev->inq_tx_power = rp->tx_power;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200851}
852
Johan Hedberg980e1a52011-01-22 06:10:07 +0200853static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
854{
855 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
856 struct hci_cp_pin_code_reply *cp;
857 struct hci_conn *conn;
858
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300859 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200860
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200861 hci_dev_lock(hdev);
862
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200863 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200864 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200865
Mikel Astizfa1bd912012-08-09 09:52:29 +0200866 if (rp->status)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200867 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200868
869 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
870 if (!cp)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200871 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200872
873 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
874 if (conn)
875 conn->pin_length = cp->pin_len;
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200876
877unlock:
878 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200879}
880
881static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
882{
883 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
884
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300885 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200886
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200887 hci_dev_lock(hdev);
888
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200889 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200890 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300891 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200892
893 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200894}
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200895
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300896static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
897 struct sk_buff *skb)
898{
899 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
900
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300901 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300902
903 if (rp->status)
904 return;
905
906 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
907 hdev->le_pkts = rp->le_max_pkt;
908
909 hdev->le_cnt = hdev->le_pkts;
910
911 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300912}
Johan Hedberg980e1a52011-01-22 06:10:07 +0200913
Johan Hedberg60e77322013-01-22 14:01:59 +0200914static void hci_cc_le_read_local_features(struct hci_dev *hdev,
915 struct sk_buff *skb)
916{
917 struct hci_rp_le_read_local_features *rp = (void *) skb->data;
918
919 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
920
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200921 if (rp->status)
922 return;
923
924 memcpy(hdev->le_features, rp->features, 8);
Johan Hedberg60e77322013-01-22 14:01:59 +0200925}
926
Johan Hedberg8fa19092012-10-19 20:57:49 +0300927static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
928 struct sk_buff *skb)
929{
930 struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
931
932 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
933
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200934 if (rp->status)
935 return;
936
937 hdev->adv_tx_power = rp->tx_power;
Johan Hedberg8fa19092012-10-19 20:57:49 +0300938}
939
Johan Hedberga5c29682011-02-19 12:05:57 -0300940static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
941{
942 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
943
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300944 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300945
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200946 hci_dev_lock(hdev);
947
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200948 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300949 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
950 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200951
952 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300953}
954
955static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300956 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -0300957{
958 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
959
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300960 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300961
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200962 hci_dev_lock(hdev);
963
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200964 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200965 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300966 ACL_LINK, 0, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200967
968 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300969}
970
Brian Gix1143d452011-11-23 08:28:34 -0800971static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
972{
973 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
974
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300975 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800976
977 hci_dev_lock(hdev);
978
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200979 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +0200980 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300981 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800982
983 hci_dev_unlock(hdev);
984}
985
986static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300987 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -0800988{
989 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
990
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300991 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800992
993 hci_dev_lock(hdev);
994
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200995 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Brian Gix1143d452011-11-23 08:28:34 -0800996 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300997 ACL_LINK, 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800998
999 hci_dev_unlock(hdev);
1000}
1001
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001002static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
1003 struct sk_buff *skb)
Szymon Jancc35938b2011-03-22 13:12:21 +01001004{
1005 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
1006
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001007 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Szymon Jancc35938b2011-03-22 13:12:21 +01001008
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001009 hci_dev_lock(hdev);
Johan Hedberg38da1702014-11-17 20:52:20 +02001010 mgmt_read_local_oob_data_complete(hdev, rp->hash, rp->rand, NULL, NULL,
1011 rp->status);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001012 hci_dev_unlock(hdev);
1013}
1014
1015static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
1016 struct sk_buff *skb)
1017{
1018 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
1019
1020 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1021
1022 hci_dev_lock(hdev);
Johan Hedberg38da1702014-11-17 20:52:20 +02001023 mgmt_read_local_oob_data_complete(hdev, rp->hash192, rp->rand192,
1024 rp->hash256, rp->rand256,
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08001025 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001026 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01001027}
1028
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001029
1030static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
1031{
1032 __u8 status = *((__u8 *) skb->data);
1033 bdaddr_t *sent;
1034
1035 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1036
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001037 if (status)
1038 return;
1039
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001040 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1041 if (!sent)
1042 return;
1043
1044 hci_dev_lock(hdev);
1045
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001046 bacpy(&hdev->random_addr, sent);
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001047
1048 hci_dev_unlock(hdev);
1049}
1050
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001051static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1052{
1053 __u8 *sent, status = *((__u8 *) skb->data);
1054
1055 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1056
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001057 if (status)
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001058 return;
1059
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001060 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1061 if (!sent)
Johan Hedberg3c857752014-03-25 10:30:49 +02001062 return;
1063
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001064 hci_dev_lock(hdev);
1065
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001066 /* If we're doing connection initiation as peripheral. Set a
Johan Hedberg3c857752014-03-25 10:30:49 +02001067 * timeout in case something goes wrong.
1068 */
1069 if (*sent) {
1070 struct hci_conn *conn;
1071
Johan Hedberg66c417c2014-07-08 15:07:47 +03001072 set_bit(HCI_LE_ADV, &hdev->dev_flags);
1073
Johan Hedberg3c857752014-03-25 10:30:49 +02001074 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
1075 if (conn)
1076 queue_delayed_work(hdev->workqueue,
1077 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001078 conn->conn_timeout);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001079 } else {
1080 clear_bit(HCI_LE_ADV, &hdev->dev_flags);
Johan Hedberg3c857752014-03-25 10:30:49 +02001081 }
1082
Johan Hedberg04b4edc2013-03-15 17:07:01 -05001083 hci_dev_unlock(hdev);
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001084}
1085
Marcel Holtmann533553f2014-03-21 12:18:10 -07001086static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1087{
1088 struct hci_cp_le_set_scan_param *cp;
1089 __u8 status = *((__u8 *) skb->data);
1090
1091 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1092
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001093 if (status)
1094 return;
1095
Marcel Holtmann533553f2014-03-21 12:18:10 -07001096 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1097 if (!cp)
1098 return;
1099
1100 hci_dev_lock(hdev);
1101
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001102 hdev->le_scan_type = cp->type;
Marcel Holtmann533553f2014-03-21 12:18:10 -07001103
1104 hci_dev_unlock(hdev);
1105}
1106
Johan Hedbergb9a63282014-03-25 10:51:52 +02001107static bool has_pending_adv_report(struct hci_dev *hdev)
1108{
1109 struct discovery_state *d = &hdev->discovery;
1110
1111 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1112}
1113
1114static void clear_pending_adv_report(struct hci_dev *hdev)
1115{
1116 struct discovery_state *d = &hdev->discovery;
1117
1118 bacpy(&d->last_adv_addr, BDADDR_ANY);
1119 d->last_adv_data_len = 0;
1120}
1121
1122static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001123 u8 bdaddr_type, s8 rssi, u32 flags,
1124 u8 *data, u8 len)
Johan Hedbergb9a63282014-03-25 10:51:52 +02001125{
1126 struct discovery_state *d = &hdev->discovery;
1127
1128 bacpy(&d->last_adv_addr, bdaddr);
1129 d->last_adv_addr_type = bdaddr_type;
Johan Hedbergff5cd292014-03-25 14:40:52 +02001130 d->last_adv_rssi = rssi;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001131 d->last_adv_flags = flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02001132 memcpy(d->last_adv_data, data, len);
1133 d->last_adv_data_len = len;
1134}
1135
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001136static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001137 struct sk_buff *skb)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001138{
1139 struct hci_cp_le_set_scan_enable *cp;
1140 __u8 status = *((__u8 *) skb->data);
1141
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001142 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001143
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001144 if (status)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001145 return;
1146
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001147 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1148 if (!cp)
Andre Guedes3fd319b2013-04-30 15:29:36 -03001149 return;
1150
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301151 hci_dev_lock(hdev);
1152
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001153 switch (cp->enable) {
Andre Guedes76a388b2013-04-04 20:21:02 -03001154 case LE_SCAN_ENABLE:
Andre Guedesd23264a2011-11-25 20:53:38 -03001155 set_bit(HCI_LE_SCAN, &hdev->dev_flags);
Johan Hedbergb9a63282014-03-25 10:51:52 +02001156 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1157 clear_pending_adv_report(hdev);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001158 break;
1159
Andre Guedes76a388b2013-04-04 20:21:02 -03001160 case LE_SCAN_DISABLE:
Johan Hedbergb9a63282014-03-25 10:51:52 +02001161 /* We do this here instead of when setting DISCOVERY_STOPPED
1162 * since the latter would potentially require waiting for
1163 * inquiry to stop too.
1164 */
1165 if (has_pending_adv_report(hdev)) {
1166 struct discovery_state *d = &hdev->discovery;
1167
1168 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001169 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001170 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001171 d->last_adv_data,
Johan Hedbergb9a63282014-03-25 10:51:52 +02001172 d->last_adv_data_len, NULL, 0);
1173 }
1174
Johan Hedberg317ac8c2014-02-28 20:26:12 +02001175 /* Cancel this timer so that we don't try to disable scanning
1176 * when it's already disabled.
1177 */
1178 cancel_delayed_work(&hdev->le_scan_disable);
1179
Andre Guedesd23264a2011-11-25 20:53:38 -03001180 clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001181
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001182 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1183 * interrupted scanning due to a connect request. Mark
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001184 * therefore discovery as stopped. If this was not
1185 * because of a connect request advertising might have
1186 * been disabled because of active scanning, so
1187 * re-enable it again if necessary.
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001188 */
1189 if (test_and_clear_bit(HCI_LE_SCAN_INTERRUPTED,
1190 &hdev->dev_flags))
1191 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001192 else if (!test_bit(HCI_LE_ADV, &hdev->dev_flags) &&
Johan Hedberg34722272014-07-08 16:05:05 +03001193 hdev->discovery.state == DISCOVERY_FINDING)
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001194 mgmt_reenable_advertising(hdev);
1195
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001196 break;
1197
1198 default:
1199 BT_ERR("Used reserved LE_Scan_Enable param %d", cp->enable);
1200 break;
Andre Guedes35815082011-05-26 16:23:53 -03001201 }
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301202
1203 hci_dev_unlock(hdev);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001204}
1205
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001206static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
1207 struct sk_buff *skb)
1208{
1209 struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
1210
1211 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1212
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001213 if (rp->status)
1214 return;
1215
1216 hdev->le_white_list_size = rp->size;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001217}
1218
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001219static void hci_cc_le_clear_white_list(struct hci_dev *hdev,
1220 struct sk_buff *skb)
1221{
1222 __u8 status = *((__u8 *) skb->data);
1223
1224 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1225
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001226 if (status)
1227 return;
1228
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001229 hci_bdaddr_list_clear(&hdev->le_white_list);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001230}
1231
1232static void hci_cc_le_add_to_white_list(struct hci_dev *hdev,
1233 struct sk_buff *skb)
1234{
1235 struct hci_cp_le_add_to_white_list *sent;
1236 __u8 status = *((__u8 *) skb->data);
1237
1238 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1239
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001240 if (status)
1241 return;
1242
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001243 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_WHITE_LIST);
1244 if (!sent)
1245 return;
1246
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001247 hci_bdaddr_list_add(&hdev->le_white_list, &sent->bdaddr,
1248 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001249}
1250
1251static void hci_cc_le_del_from_white_list(struct hci_dev *hdev,
1252 struct sk_buff *skb)
1253{
1254 struct hci_cp_le_del_from_white_list *sent;
1255 __u8 status = *((__u8 *) skb->data);
1256
1257 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1258
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001259 if (status)
1260 return;
1261
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001262 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_WHITE_LIST);
1263 if (!sent)
1264 return;
1265
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001266 hci_bdaddr_list_del(&hdev->le_white_list, &sent->bdaddr,
1267 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001268}
1269
Johan Hedberg9b008c02013-01-22 14:02:01 +02001270static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
1271 struct sk_buff *skb)
1272{
1273 struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
1274
1275 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1276
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001277 if (rp->status)
1278 return;
1279
1280 memcpy(hdev->le_states, rp->le_states, 8);
Johan Hedberg9b008c02013-01-22 14:02:01 +02001281}
1282
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001283static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1284 struct sk_buff *skb)
Andre Guedesf9b49302011-06-30 19:20:53 -03001285{
Johan Hedberg06199cf2012-02-22 16:37:11 +02001286 struct hci_cp_write_le_host_supported *sent;
Andre Guedesf9b49302011-06-30 19:20:53 -03001287 __u8 status = *((__u8 *) skb->data);
1288
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001289 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesf9b49302011-06-30 19:20:53 -03001290
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001291 if (status)
1292 return;
1293
Johan Hedberg06199cf2012-02-22 16:37:11 +02001294 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001295 if (!sent)
Andre Guedesf9b49302011-06-30 19:20:53 -03001296 return;
1297
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301298 hci_dev_lock(hdev);
1299
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001300 if (sent->le) {
1301 hdev->features[1][0] |= LMP_HOST_LE;
1302 set_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1303 } else {
1304 hdev->features[1][0] &= ~LMP_HOST_LE;
1305 clear_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1306 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001307 }
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001308
1309 if (sent->simul)
1310 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
1311 else
1312 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
Jaganath Kanakkassery5c1a4c82014-12-11 18:58:15 +05301313
1314 hci_dev_unlock(hdev);
Andre Guedesf9b49302011-06-30 19:20:53 -03001315}
1316
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02001317static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1318{
1319 struct hci_cp_le_set_adv_param *cp;
1320 u8 status = *((u8 *) skb->data);
1321
1322 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1323
1324 if (status)
1325 return;
1326
1327 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
1328 if (!cp)
1329 return;
1330
1331 hci_dev_lock(hdev);
1332 hdev->adv_addr_type = cp->own_address_type;
1333 hci_dev_unlock(hdev);
1334}
1335
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001336static void hci_cc_write_remote_amp_assoc(struct hci_dev *hdev,
1337 struct sk_buff *skb)
1338{
1339 struct hci_rp_write_remote_amp_assoc *rp = (void *) skb->data;
1340
1341 BT_DBG("%s status 0x%2.2x phy_handle 0x%2.2x",
1342 hdev->name, rp->status, rp->phy_handle);
1343
1344 if (rp->status)
1345 return;
1346
1347 amp_write_rem_assoc_continue(hdev, rp->phy_handle);
1348}
1349
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02001350static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
1351{
1352 struct hci_rp_read_rssi *rp = (void *) skb->data;
1353 struct hci_conn *conn;
1354
1355 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1356
1357 if (rp->status)
1358 return;
1359
1360 hci_dev_lock(hdev);
1361
1362 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1363 if (conn)
1364 conn->rssi = rp->rssi;
1365
1366 hci_dev_unlock(hdev);
1367}
1368
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001369static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
1370{
1371 struct hci_cp_read_tx_power *sent;
1372 struct hci_rp_read_tx_power *rp = (void *) skb->data;
1373 struct hci_conn *conn;
1374
1375 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1376
1377 if (rp->status)
1378 return;
1379
1380 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
1381 if (!sent)
1382 return;
1383
1384 hci_dev_lock(hdev);
1385
1386 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001387 if (!conn)
1388 goto unlock;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001389
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001390 switch (sent->type) {
1391 case 0x00:
1392 conn->tx_power = rp->tx_power;
1393 break;
1394 case 0x01:
1395 conn->max_tx_power = rp->tx_power;
1396 break;
1397 }
1398
1399unlock:
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001400 hci_dev_unlock(hdev);
1401}
1402
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001403static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001404{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001405 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001406
1407 if (status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001408 hci_conn_check_pending(hdev);
Johan Hedberg314b2382011-04-27 10:29:57 -04001409 return;
1410 }
1411
Andre Guedes89352e72011-11-04 14:16:53 -03001412 set_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001413}
1414
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001415static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001417 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001420 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001421
1422 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 if (!cp)
1424 return;
1425
1426 hci_dev_lock(hdev);
1427
1428 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1429
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001430 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432 if (status) {
1433 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001434 if (status != 0x0c || conn->attempt > 2) {
1435 conn->state = BT_CLOSED;
1436 hci_proto_connect_cfm(conn, status);
1437 hci_conn_del(conn);
1438 } else
1439 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441 } else {
1442 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03001443 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr,
1444 HCI_ROLE_MASTER);
1445 if (!conn)
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001446 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 }
1448 }
1449
1450 hci_dev_unlock(hdev);
1451}
1452
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001453static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001455 struct hci_cp_add_sco *cp;
1456 struct hci_conn *acl, *sco;
1457 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001459 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001460
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001461 if (!status)
1462 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001464 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1465 if (!cp)
1466 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001468 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001470 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001471
1472 hci_dev_lock(hdev);
1473
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001474 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001475 if (acl) {
1476 sco = acl->link;
1477 if (sco) {
1478 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001479
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001480 hci_proto_connect_cfm(sco, status);
1481 hci_conn_del(sco);
1482 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001483 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001484
1485 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486}
1487
Marcel Holtmannf8558552008-07-14 20:13:49 +02001488static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1489{
1490 struct hci_cp_auth_requested *cp;
1491 struct hci_conn *conn;
1492
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001493 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001494
1495 if (!status)
1496 return;
1497
1498 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1499 if (!cp)
1500 return;
1501
1502 hci_dev_lock(hdev);
1503
1504 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1505 if (conn) {
1506 if (conn->state == BT_CONFIG) {
1507 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001508 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001509 }
1510 }
1511
1512 hci_dev_unlock(hdev);
1513}
1514
1515static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1516{
1517 struct hci_cp_set_conn_encrypt *cp;
1518 struct hci_conn *conn;
1519
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001520 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001521
1522 if (!status)
1523 return;
1524
1525 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1526 if (!cp)
1527 return;
1528
1529 hci_dev_lock(hdev);
1530
1531 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1532 if (conn) {
1533 if (conn->state == BT_CONFIG) {
1534 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001535 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001536 }
1537 }
1538
1539 hci_dev_unlock(hdev);
1540}
1541
Johan Hedberg127178d2010-11-18 22:22:29 +02001542static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001543 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02001544{
Johan Hedberg392599b2010-11-18 22:22:28 +02001545 if (conn->state != BT_CONFIG || !conn->out)
1546 return 0;
1547
Johan Hedberg765c2a92011-01-19 12:06:52 +05301548 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02001549 return 0;
1550
1551 /* Only request authentication for SSP connections or non-SSP
Johan Hedberg264b8b42014-01-08 16:40:39 +02001552 * devices with sec_level MEDIUM or HIGH or if MITM protection
1553 * is requested.
1554 */
Gustavo Padovan807deac2012-05-17 00:36:24 -03001555 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
Johan Hedberg7e3691e2014-05-30 14:45:19 +03001556 conn->pending_sec_level != BT_SECURITY_FIPS &&
Johan Hedberg264b8b42014-01-08 16:40:39 +02001557 conn->pending_sec_level != BT_SECURITY_HIGH &&
1558 conn->pending_sec_level != BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02001559 return 0;
1560
Johan Hedberg392599b2010-11-18 22:22:28 +02001561 return 1;
1562}
1563
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001564static int hci_resolve_name(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001565 struct inquiry_entry *e)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001566{
1567 struct hci_cp_remote_name_req cp;
1568
1569 memset(&cp, 0, sizeof(cp));
1570
1571 bacpy(&cp.bdaddr, &e->data.bdaddr);
1572 cp.pscan_rep_mode = e->data.pscan_rep_mode;
1573 cp.pscan_mode = e->data.pscan_mode;
1574 cp.clock_offset = e->data.clock_offset;
1575
1576 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1577}
1578
Johan Hedbergb644ba32012-01-17 21:48:47 +02001579static bool hci_resolve_next_name(struct hci_dev *hdev)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001580{
1581 struct discovery_state *discov = &hdev->discovery;
1582 struct inquiry_entry *e;
1583
Johan Hedbergb644ba32012-01-17 21:48:47 +02001584 if (list_empty(&discov->resolve))
1585 return false;
1586
1587 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
Ram Malovanyc8100892012-07-19 10:26:09 +03001588 if (!e)
1589 return false;
1590
Johan Hedbergb644ba32012-01-17 21:48:47 +02001591 if (hci_resolve_name(hdev, e) == 0) {
1592 e->name_state = NAME_PENDING;
1593 return true;
1594 }
1595
1596 return false;
1597}
1598
1599static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001600 bdaddr_t *bdaddr, u8 *name, u8 name_len)
Johan Hedbergb644ba32012-01-17 21:48:47 +02001601{
1602 struct discovery_state *discov = &hdev->discovery;
1603 struct inquiry_entry *e;
1604
Johan Hedberg60cb49d2014-11-11 11:33:24 +02001605 /* Update the mgmt connected state if necessary. Be careful with
1606 * conn objects that exist but are not (yet) connected however.
1607 * Only those in BT_CONFIG or BT_CONNECTED states can be
1608 * considered connected.
1609 */
1610 if (conn &&
1611 (conn->state == BT_CONFIG || conn->state == BT_CONNECTED) &&
Jaganath Kanakkasserycb77c3e2014-11-07 16:39:09 +05301612 !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00001613 mgmt_device_connected(hdev, conn, 0, name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001614
1615 if (discov->state == DISCOVERY_STOPPED)
1616 return;
1617
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001618 if (discov->state == DISCOVERY_STOPPING)
1619 goto discov_complete;
1620
1621 if (discov->state != DISCOVERY_RESOLVING)
1622 return;
1623
1624 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
Ram Malovany7cc83802012-07-19 10:26:10 +03001625 /* If the device was not found in a list of found devices names of which
1626 * are pending. there is no need to continue resolving a next name as it
1627 * will be done upon receiving another Remote Name Request Complete
1628 * Event */
1629 if (!e)
1630 return;
1631
1632 list_del(&e->list);
1633 if (name) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001634 e->name_state = NAME_KNOWN;
Ram Malovany7cc83802012-07-19 10:26:10 +03001635 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
1636 e->data.rssi, name, name_len);
Ram Malovanyc3e7c0d2012-07-19 10:26:11 +03001637 } else {
1638 e->name_state = NAME_NOT_KNOWN;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001639 }
1640
Johan Hedbergb644ba32012-01-17 21:48:47 +02001641 if (hci_resolve_next_name(hdev))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001642 return;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001643
1644discov_complete:
1645 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1646}
1647
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001648static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1649{
Johan Hedberg127178d2010-11-18 22:22:29 +02001650 struct hci_cp_remote_name_req *cp;
1651 struct hci_conn *conn;
1652
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001653 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02001654
1655 /* If successful wait for the name req complete event before
1656 * checking for the need to do authentication */
1657 if (!status)
1658 return;
1659
1660 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1661 if (!cp)
1662 return;
1663
1664 hci_dev_lock(hdev);
1665
1666 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001667
1668 if (test_bit(HCI_MGMT, &hdev->dev_flags))
1669 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
1670
Johan Hedberg79c6c702011-04-28 11:28:55 -07001671 if (!conn)
1672 goto unlock;
1673
1674 if (!hci_outgoing_auth_needed(hdev, conn))
1675 goto unlock;
1676
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001677 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02001678 struct hci_cp_auth_requested auth_cp;
1679
Johan Hedberg977f8fc2014-07-17 15:35:39 +03001680 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
1681
Johannes Bergc1f23a22013-10-07 18:19:16 +02001682 auth_cp.handle = __cpu_to_le16(conn->handle);
1683 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
1684 sizeof(auth_cp), &auth_cp);
Johan Hedberg127178d2010-11-18 22:22:29 +02001685 }
1686
Johan Hedberg79c6c702011-04-28 11:28:55 -07001687unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02001688 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001689}
1690
Marcel Holtmann769be972008-07-14 20:13:49 +02001691static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1692{
1693 struct hci_cp_read_remote_features *cp;
1694 struct hci_conn *conn;
1695
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001696 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001697
1698 if (!status)
1699 return;
1700
1701 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1702 if (!cp)
1703 return;
1704
1705 hci_dev_lock(hdev);
1706
1707 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1708 if (conn) {
1709 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001710 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001711 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001712 }
1713 }
1714
1715 hci_dev_unlock(hdev);
1716}
1717
1718static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1719{
1720 struct hci_cp_read_remote_ext_features *cp;
1721 struct hci_conn *conn;
1722
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001723 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001724
1725 if (!status)
1726 return;
1727
1728 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1729 if (!cp)
1730 return;
1731
1732 hci_dev_lock(hdev);
1733
1734 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1735 if (conn) {
1736 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001737 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001738 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001739 }
1740 }
1741
1742 hci_dev_unlock(hdev);
1743}
1744
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001745static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1746{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001747 struct hci_cp_setup_sync_conn *cp;
1748 struct hci_conn *acl, *sco;
1749 __u16 handle;
1750
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001751 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001752
1753 if (!status)
1754 return;
1755
1756 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1757 if (!cp)
1758 return;
1759
1760 handle = __le16_to_cpu(cp->handle);
1761
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001762 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001763
1764 hci_dev_lock(hdev);
1765
1766 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001767 if (acl) {
1768 sco = acl->link;
1769 if (sco) {
1770 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001771
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001772 hci_proto_connect_cfm(sco, status);
1773 hci_conn_del(sco);
1774 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001775 }
1776
1777 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001778}
1779
1780static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1781{
1782 struct hci_cp_sniff_mode *cp;
1783 struct hci_conn *conn;
1784
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001785 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001786
1787 if (!status)
1788 return;
1789
1790 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1791 if (!cp)
1792 return;
1793
1794 hci_dev_lock(hdev);
1795
1796 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001797 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001798 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001799
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001800 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001801 hci_sco_setup(conn, status);
1802 }
1803
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001804 hci_dev_unlock(hdev);
1805}
1806
1807static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1808{
1809 struct hci_cp_exit_sniff_mode *cp;
1810 struct hci_conn *conn;
1811
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001812 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001813
1814 if (!status)
1815 return;
1816
1817 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1818 if (!cp)
1819 return;
1820
1821 hci_dev_lock(hdev);
1822
1823 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001824 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001825 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001826
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001827 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001828 hci_sco_setup(conn, status);
1829 }
1830
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001831 hci_dev_unlock(hdev);
1832}
1833
Johan Hedberg88c3df12012-02-09 14:27:38 +02001834static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
1835{
1836 struct hci_cp_disconnect *cp;
1837 struct hci_conn *conn;
1838
1839 if (!status)
1840 return;
1841
1842 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
1843 if (!cp)
1844 return;
1845
1846 hci_dev_lock(hdev);
1847
1848 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1849 if (conn)
1850 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001851 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02001852
1853 hci_dev_unlock(hdev);
1854}
1855
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001856static void hci_cs_create_phylink(struct hci_dev *hdev, u8 status)
1857{
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001858 struct hci_cp_create_phy_link *cp;
1859
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001860 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001861
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001862 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_PHY_LINK);
1863 if (!cp)
1864 return;
1865
Andrei Emeltchenkoe58917b2012-10-31 15:46:33 +02001866 hci_dev_lock(hdev);
1867
1868 if (status) {
1869 struct hci_conn *hcon;
1870
1871 hcon = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
1872 if (hcon)
1873 hci_conn_del(hcon);
1874 } else {
1875 amp_write_remote_assoc(hdev, cp->phy_handle);
1876 }
1877
1878 hci_dev_unlock(hdev);
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001879}
1880
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +03001881static void hci_cs_accept_phylink(struct hci_dev *hdev, u8 status)
1882{
1883 struct hci_cp_accept_phy_link *cp;
1884
1885 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1886
1887 if (status)
1888 return;
1889
1890 cp = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_PHY_LINK);
1891 if (!cp)
1892 return;
1893
1894 amp_write_remote_assoc(hdev, cp->phy_handle);
1895}
1896
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02001897static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
1898{
1899 struct hci_cp_le_create_conn *cp;
1900 struct hci_conn *conn;
1901
1902 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1903
1904 /* All connection failure handling is taken care of by the
1905 * hci_le_conn_failed function which is triggered by the HCI
1906 * request completion callbacks used for connecting.
1907 */
1908 if (status)
1909 return;
1910
1911 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
1912 if (!cp)
1913 return;
1914
1915 hci_dev_lock(hdev);
1916
1917 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->peer_addr);
1918 if (!conn)
1919 goto unlock;
1920
1921 /* Store the initiator and responder address information which
1922 * is needed for SMP. These values will not change during the
1923 * lifetime of the connection.
1924 */
1925 conn->init_addr_type = cp->own_address_type;
1926 if (cp->own_address_type == ADDR_LE_DEV_RANDOM)
1927 bacpy(&conn->init_addr, &hdev->random_addr);
1928 else
1929 bacpy(&conn->init_addr, &hdev->bdaddr);
1930
1931 conn->resp_addr_type = cp->peer_addr_type;
1932 bacpy(&conn->resp_addr, &cp->peer_addr);
1933
Johan Hedberg9489eca2014-02-28 17:45:46 +02001934 /* We don't want the connection attempt to stick around
1935 * indefinitely since LE doesn't have a page timeout concept
1936 * like BR/EDR. Set a timer for any connection that doesn't use
1937 * the white list for connecting.
1938 */
1939 if (cp->filter_policy == HCI_LE_USE_PEER_ADDR)
1940 queue_delayed_work(conn->hdev->workqueue,
1941 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001942 conn->conn_timeout);
Johan Hedberg9489eca2014-02-28 17:45:46 +02001943
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02001944unlock:
1945 hci_dev_unlock(hdev);
1946}
1947
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02001948static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
1949{
1950 struct hci_cp_le_start_enc *cp;
1951 struct hci_conn *conn;
1952
1953 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1954
1955 if (!status)
1956 return;
1957
1958 hci_dev_lock(hdev);
1959
1960 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
1961 if (!cp)
1962 goto unlock;
1963
1964 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1965 if (!conn)
1966 goto unlock;
1967
1968 if (conn->state != BT_CONNECTED)
1969 goto unlock;
1970
1971 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
1972 hci_conn_drop(conn);
1973
1974unlock:
1975 hci_dev_unlock(hdev);
1976}
1977
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01001978static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
1979{
1980 struct hci_cp_switch_role *cp;
1981 struct hci_conn *conn;
1982
1983 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1984
1985 if (!status)
1986 return;
1987
1988 cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
1989 if (!cp)
1990 return;
1991
1992 hci_dev_lock(hdev);
1993
1994 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1995 if (conn)
1996 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
1997
1998 hci_dev_unlock(hdev);
1999}
2000
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002001static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002002{
2003 __u8 status = *((__u8 *) skb->data);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002004 struct discovery_state *discov = &hdev->discovery;
2005 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002006
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002007 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002008
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002009 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03002010
2011 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
2012 return;
2013
Peter Zijlstra4e857c52014-03-17 18:06:10 +01002014 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -03002015 wake_up_bit(&hdev->flags, HCI_INQUIRY);
2016
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02002017 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002018 return;
2019
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002020 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002021
Andre Guedes343f9352012-02-17 20:39:37 -03002022 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02002023 goto unlock;
2024
2025 if (list_empty(&discov->resolve)) {
2026 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2027 goto unlock;
2028 }
2029
2030 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
2031 if (e && hci_resolve_name(hdev, e) == 0) {
2032 e->name_state = NAME_PENDING;
2033 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
2034 } else {
2035 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
2036 }
2037
2038unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02002039 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002040}
2041
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002042static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002044 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002045 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 int num_rsp = *((__u8 *) skb->data);
2047
2048 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
2049
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002050 if (!num_rsp)
2051 return;
2052
Andre Guedes1519cc12012-03-21 00:03:38 -03002053 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
2054 return;
2055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002057
Johan Hedberge17acd42011-03-30 23:57:16 +03002058 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02002059 u32 flags;
Johan Hedberg31754052012-01-04 13:39:52 +02002060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 bacpy(&data.bdaddr, &info->bdaddr);
2062 data.pscan_rep_mode = info->pscan_rep_mode;
2063 data.pscan_period_mode = info->pscan_period_mode;
2064 data.pscan_mode = info->pscan_mode;
2065 memcpy(data.dev_class, info->dev_class, 3);
2066 data.clock_offset = info->clock_offset;
Marcel Holtmannefb25132014-12-05 13:03:34 +01002067 data.rssi = HCI_RSSI_INVALID;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002068 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02002069
Marcel Holtmannaf589252014-07-01 14:11:20 +02002070 flags = hci_inquiry_cache_update(hdev, &data, false);
2071
Johan Hedberg48264f02011-11-09 13:58:58 +02002072 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannefb25132014-12-05 13:03:34 +01002073 info->dev_class, HCI_RSSI_INVALID,
2074 flags, NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002076
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 hci_dev_unlock(hdev);
2078}
2079
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002080static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002082 struct hci_ev_conn_complete *ev = (void *) skb->data;
2083 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002085 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002088
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002089 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02002090 if (!conn) {
2091 if (ev->link_type != SCO_LINK)
2092 goto unlock;
2093
2094 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2095 if (!conn)
2096 goto unlock;
2097
2098 conn->type = SCO_LINK;
2099 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002100
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002101 if (!ev->status) {
2102 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02002103
2104 if (conn->type == ACL_LINK) {
2105 conn->state = BT_CONFIG;
2106 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02002107
2108 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
2109 !hci_find_link_key(hdev, &ev->bdaddr))
2110 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2111 else
2112 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02002113 } else
2114 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002115
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002116 hci_conn_add_sysfs(conn);
2117
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002118 if (test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002119 set_bit(HCI_CONN_AUTH, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002120
2121 if (test_bit(HCI_ENCRYPT, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002122 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002123
2124 /* Get remote features */
2125 if (conn->type == ACL_LINK) {
2126 struct hci_cp_read_remote_features cp;
2127 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02002128 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002129 sizeof(cp), &cp);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002130
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02002131 hci_update_page_scan(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002132 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002133
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002134 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02002135 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002136 struct hci_cp_change_conn_ptype cp;
2137 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02002138 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002139 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
2140 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002141 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02002142 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002143 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02002144 if (conn->type == ACL_LINK)
Marcel Holtmann64c7b772014-02-18 14:22:20 -08002145 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002146 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02002147 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002148
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002149 if (conn->type == ACL_LINK)
2150 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002151
Marcel Holtmann769be972008-07-14 20:13:49 +02002152 if (ev->status) {
2153 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002154 hci_conn_del(conn);
Marcel Holtmannc89b6e62009-01-15 21:57:03 +01002155 } else if (ev->link_type != ACL_LINK)
2156 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002157
2158unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002160
2161 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Johan Hedberg70c46422014-07-09 12:59:17 +03002164static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
2165{
2166 struct hci_cp_reject_conn_req cp;
2167
2168 bacpy(&cp.bdaddr, bdaddr);
2169 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
2170 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
2171}
2172
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002173static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002175 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 int mask = hdev->link_mode;
Johan Hedberg70c46422014-07-09 12:59:17 +03002177 struct inquiry_entry *ie;
2178 struct hci_conn *conn;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002179 __u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002181 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002182 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002184 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
2185 &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
Johan Hedberg70c46422014-07-09 12:59:17 +03002187 if (!(mask & HCI_LM_ACCEPT)) {
2188 hci_reject_conn(hdev, &ev->bdaddr);
2189 return;
2190 }
2191
Johan Hedberg46c4c942014-07-16 16:19:21 +03002192 if (hci_bdaddr_list_lookup(&hdev->blacklist, &ev->bdaddr,
2193 BDADDR_BREDR)) {
2194 hci_reject_conn(hdev, &ev->bdaddr);
2195 return;
2196 }
2197
2198 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags) &&
2199 !hci_bdaddr_list_lookup(&hdev->whitelist, &ev->bdaddr,
2200 BDADDR_BREDR)) {
2201 hci_reject_conn(hdev, &ev->bdaddr);
2202 return;
Johan Hedberg70c46422014-07-09 12:59:17 +03002203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Johan Hedberg70c46422014-07-09 12:59:17 +03002205 /* Connection accepted */
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002206
Johan Hedberg70c46422014-07-09 12:59:17 +03002207 hci_dev_lock(hdev);
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02002208
Johan Hedberg70c46422014-07-09 12:59:17 +03002209 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2210 if (ie)
2211 memcpy(ie->data.dev_class, ev->dev_class, 3);
2212
2213 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
2214 &ev->bdaddr);
2215 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03002216 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2217 HCI_ROLE_SLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (!conn) {
Johan Hedberg70c46422014-07-09 12:59:17 +03002219 BT_ERR("No memory for new connection");
2220 hci_dev_unlock(hdev);
2221 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 }
Johan Hedberg70c46422014-07-09 12:59:17 +03002223 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002224
Johan Hedberg70c46422014-07-09 12:59:17 +03002225 memcpy(conn->dev_class, ev->dev_class, 3);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002226
Johan Hedberg70c46422014-07-09 12:59:17 +03002227 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Johan Hedberg70c46422014-07-09 12:59:17 +03002229 if (ev->link_type == ACL_LINK ||
2230 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
2231 struct hci_cp_accept_conn_req cp;
2232 conn->state = BT_CONNECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
2234 bacpy(&cp.bdaddr, &ev->bdaddr);
Johan Hedberg70c46422014-07-09 12:59:17 +03002235
2236 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
2237 cp.role = 0x00; /* Become master */
2238 else
2239 cp.role = 0x01; /* Remain slave */
2240
2241 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
2242 } else if (!(flags & HCI_PROTO_DEFER)) {
2243 struct hci_cp_accept_sync_conn_req cp;
2244 conn->state = BT_CONNECT;
2245
2246 bacpy(&cp.bdaddr, &ev->bdaddr);
2247 cp.pkt_type = cpu_to_le16(conn->pkt_type);
2248
2249 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
2250 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
2251 cp.max_latency = cpu_to_le16(0xffff);
2252 cp.content_format = cpu_to_le16(hdev->voice_setting);
2253 cp.retrans_effort = 0xff;
2254
2255 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
2256 &cp);
2257 } else {
2258 conn->state = BT_CONNECT2;
2259 hci_proto_connect_cfm(conn, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261}
2262
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02002263static u8 hci_to_mgmt_reason(u8 err)
2264{
2265 switch (err) {
2266 case HCI_ERROR_CONNECTION_TIMEOUT:
2267 return MGMT_DEV_DISCONN_TIMEOUT;
2268 case HCI_ERROR_REMOTE_USER_TERM:
2269 case HCI_ERROR_REMOTE_LOW_RESOURCES:
2270 case HCI_ERROR_REMOTE_POWER_OFF:
2271 return MGMT_DEV_DISCONN_REMOTE;
2272 case HCI_ERROR_LOCAL_HOST_TERM:
2273 return MGMT_DEV_DISCONN_LOCAL_HOST;
2274 default:
2275 return MGMT_DEV_DISCONN_UNKNOWN;
2276 }
2277}
2278
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002279static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002281 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Andre Guedesabf54a52013-11-07 17:36:09 -03002282 u8 reason = hci_to_mgmt_reason(ev->reason);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002283 struct hci_conn_params *params;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002284 struct hci_conn *conn;
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002285 bool mgmt_connected;
Andre Guedes38462202013-11-07 17:36:10 -03002286 u8 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002288 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 hci_dev_lock(hdev);
2291
Marcel Holtmann04837f62006-07-03 10:02:33 +02002292 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02002293 if (!conn)
2294 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002295
Andre Guedesabf54a52013-11-07 17:36:09 -03002296 if (ev->status) {
2297 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2298 conn->dst_type, ev->status);
2299 goto unlock;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02002300 }
Johan Hedbergf7520542011-01-20 12:34:39 +02002301
Andre Guedes38462202013-11-07 17:36:10 -03002302 conn->state = BT_CLOSED;
2303
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002304 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
2305 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2306 reason, mgmt_connected);
Andre Guedesabf54a52013-11-07 17:36:09 -03002307
Johan Hedberg22f433d2014-08-01 11:13:32 +03002308 if (conn->type == ACL_LINK) {
2309 if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2310 hci_remove_link_key(hdev, &conn->dst);
2311
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02002312 hci_update_page_scan(hdev);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002313 }
Johan Hedberg22102462013-10-05 12:01:06 +02002314
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002315 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2316 if (params) {
2317 switch (params->auto_connect) {
2318 case HCI_AUTO_CONN_LINK_LOSS:
2319 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2320 break;
2321 /* Fall through */
2322
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02002323 case HCI_AUTO_CONN_DIRECT:
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002324 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg418025d2014-07-04 12:37:24 +03002325 list_del_init(&params->action);
2326 list_add(&params->action, &hdev->pend_le_conns);
2327 hci_update_background_scan(hdev);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002328 break;
2329
2330 default:
2331 break;
2332 }
2333 }
2334
Andre Guedes38462202013-11-07 17:36:10 -03002335 type = conn->type;
Johan Hedberg22102462013-10-05 12:01:06 +02002336
Andre Guedes38462202013-11-07 17:36:10 -03002337 hci_proto_disconn_cfm(conn, ev->reason);
2338 hci_conn_del(conn);
2339
2340 /* Re-enable advertising if necessary, since it might
2341 * have been disabled by the connection. From the
2342 * HCI_LE_Set_Advertise_Enable command description in
2343 * the core specification (v4.0):
2344 * "The Controller shall continue advertising until the Host
2345 * issues an LE_Set_Advertise_Enable command with
2346 * Advertising_Enable set to 0x00 (Advertising is disabled)
2347 * or until a connection is created or until the Advertising
2348 * is timed out due to Directed Advertising."
2349 */
2350 if (type == LE_LINK)
2351 mgmt_reenable_advertising(hdev);
Johan Hedbergf7520542011-01-20 12:34:39 +02002352
2353unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 hci_dev_unlock(hdev);
2355}
2356
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002357static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002358{
2359 struct hci_ev_auth_complete *ev = (void *) skb->data;
2360 struct hci_conn *conn;
2361
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002362 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002363
2364 hci_dev_lock(hdev);
2365
2366 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002367 if (!conn)
2368 goto unlock;
2369
2370 if (!ev->status) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002371 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03002372 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002373 BT_INFO("re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03002374 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03002375 set_bit(HCI_CONN_AUTH, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002376 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03002377 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002378 } else {
Johan Hedberge1e930f2014-09-08 17:09:49 -07002379 mgmt_auth_failed(conn, ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002380 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002381
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002382 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
2383 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002384
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002385 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002386 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002387 struct hci_cp_set_conn_encrypt cp;
2388 cp.handle = ev->handle;
2389 cp.encrypt = 0x01;
2390 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002391 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002392 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002393 conn->state = BT_CONNECTED;
2394 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002395 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002396 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002397 } else {
2398 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002399
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002400 hci_conn_hold(conn);
2401 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02002402 hci_conn_drop(conn);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002403 }
2404
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002405 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002406 if (!ev->status) {
2407 struct hci_cp_set_conn_encrypt cp;
2408 cp.handle = ev->handle;
2409 cp.encrypt = 0x01;
2410 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002411 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002412 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002413 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002414 hci_encrypt_cfm(conn, ev->status, 0x00);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002415 }
2416 }
2417
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002418unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002419 hci_dev_unlock(hdev);
2420}
2421
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002422static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002423{
Johan Hedberg127178d2010-11-18 22:22:29 +02002424 struct hci_ev_remote_name *ev = (void *) skb->data;
2425 struct hci_conn *conn;
2426
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002427 BT_DBG("%s", hdev->name);
2428
2429 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02002430
2431 hci_dev_lock(hdev);
2432
2433 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002434
2435 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
2436 goto check_auth;
2437
2438 if (ev->status == 0)
2439 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002440 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02002441 else
2442 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
2443
2444check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07002445 if (!conn)
2446 goto unlock;
2447
2448 if (!hci_outgoing_auth_needed(hdev, conn))
2449 goto unlock;
2450
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002451 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002452 struct hci_cp_auth_requested cp;
Johan Hedberg977f8fc2014-07-17 15:35:39 +03002453
2454 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2455
Johan Hedberg127178d2010-11-18 22:22:29 +02002456 cp.handle = __cpu_to_le16(conn->handle);
2457 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
2458 }
2459
Johan Hedberg79c6c702011-04-28 11:28:55 -07002460unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02002461 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002462}
2463
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002464static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002465{
2466 struct hci_ev_encrypt_change *ev = (void *) skb->data;
2467 struct hci_conn *conn;
2468
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002469 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002470
2471 hci_dev_lock(hdev);
2472
2473 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002474 if (!conn)
2475 goto unlock;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002476
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002477 if (!ev->status) {
2478 if (ev->encrypt) {
2479 /* Encryption implies authentication */
Johan Hedberg4dae2792014-06-24 17:03:50 +03002480 set_bit(HCI_CONN_AUTH, &conn->flags);
2481 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002482 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002483
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08002484 /* P-256 authentication key implies FIPS */
2485 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
Johan Hedberg4dae2792014-06-24 17:03:50 +03002486 set_bit(HCI_CONN_FIPS, &conn->flags);
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08002487
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002488 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
2489 conn->type == LE_LINK)
2490 set_bit(HCI_CONN_AES_CCM, &conn->flags);
2491 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03002492 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002493 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
2494 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002495 }
2496
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07002497 /* We should disregard the current RPA and generate a new one
2498 * whenever the encryption procedure fails.
2499 */
2500 if (ev->status && conn->type == LE_LINK)
2501 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
2502
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002503 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
2504
2505 if (ev->status && conn->state == BT_CONNECTED) {
2506 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2507 hci_conn_drop(conn);
2508 goto unlock;
2509 }
2510
2511 if (conn->state == BT_CONFIG) {
2512 if (!ev->status)
2513 conn->state = BT_CONNECTED;
2514
Marcel Holtmann40b552a2014-03-19 14:10:25 -07002515 /* In Secure Connections Only mode, do not allow any
2516 * connections that are not encrypted with AES-CCM
2517 * using a P-256 authenticated combination key.
2518 */
2519 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) &&
2520 (!test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2521 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)) {
2522 hci_proto_connect_cfm(conn, HCI_ERROR_AUTH_FAILURE);
2523 hci_conn_drop(conn);
2524 goto unlock;
2525 }
2526
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002527 hci_proto_connect_cfm(conn, ev->status);
2528 hci_conn_drop(conn);
2529 } else
2530 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
2531
Gustavo Padovana7d77232012-05-13 03:20:07 -03002532unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002533 hci_dev_unlock(hdev);
2534}
2535
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002536static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
2537 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002538{
2539 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
2540 struct hci_conn *conn;
2541
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002542 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002543
2544 hci_dev_lock(hdev);
2545
2546 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2547 if (conn) {
2548 if (!ev->status)
Johan Hedberg4dae2792014-06-24 17:03:50 +03002549 set_bit(HCI_CONN_SECURE, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002550
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002551 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002552
2553 hci_key_change_cfm(conn, ev->status);
2554 }
2555
2556 hci_dev_unlock(hdev);
2557}
2558
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002559static void hci_remote_features_evt(struct hci_dev *hdev,
2560 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002561{
2562 struct hci_ev_remote_features *ev = (void *) skb->data;
2563 struct hci_conn *conn;
2564
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002565 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002566
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002567 hci_dev_lock(hdev);
2568
2569 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002570 if (!conn)
2571 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02002572
Johan Hedbergccd556f2010-11-10 17:11:51 +02002573 if (!ev->status)
Johan Hedbergcad718e2013-04-17 15:00:51 +03002574 memcpy(conn->features[0], ev->features, 8);
Johan Hedbergccd556f2010-11-10 17:11:51 +02002575
2576 if (conn->state != BT_CONFIG)
2577 goto unlock;
2578
2579 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
2580 struct hci_cp_read_remote_ext_features cp;
2581 cp.handle = ev->handle;
2582 cp.page = 0x01;
2583 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002584 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02002585 goto unlock;
2586 }
2587
Johan Hedberg671267b2012-05-12 16:11:50 -03002588 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002589 struct hci_cp_remote_name_req cp;
2590 memset(&cp, 0, sizeof(cp));
2591 bacpy(&cp.bdaddr, &conn->dst);
2592 cp.pscan_rep_mode = 0x02;
2593 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002594 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00002595 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02002596
Johan Hedberg127178d2010-11-18 22:22:29 +02002597 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002598 conn->state = BT_CONNECTED;
2599 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002600 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002601 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002602
Johan Hedbergccd556f2010-11-10 17:11:51 +02002603unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002604 hci_dev_unlock(hdev);
2605}
2606
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002607static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002608{
2609 struct hci_ev_cmd_complete *ev = (void *) skb->data;
Johan Hedberg9238f362013-03-05 20:37:48 +02002610 u8 status = skb->data[sizeof(*ev)];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002611 __u16 opcode;
2612
2613 skb_pull(skb, sizeof(*ev));
2614
2615 opcode = __le16_to_cpu(ev->opcode);
2616
2617 switch (opcode) {
2618 case HCI_OP_INQUIRY_CANCEL:
2619 hci_cc_inquiry_cancel(hdev, skb);
2620 break;
2621
Andre Guedes4d934832012-03-21 00:03:35 -03002622 case HCI_OP_PERIODIC_INQ:
2623 hci_cc_periodic_inq(hdev, skb);
2624 break;
2625
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002626 case HCI_OP_EXIT_PERIODIC_INQ:
2627 hci_cc_exit_periodic_inq(hdev, skb);
2628 break;
2629
2630 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
2631 hci_cc_remote_name_req_cancel(hdev, skb);
2632 break;
2633
2634 case HCI_OP_ROLE_DISCOVERY:
2635 hci_cc_role_discovery(hdev, skb);
2636 break;
2637
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002638 case HCI_OP_READ_LINK_POLICY:
2639 hci_cc_read_link_policy(hdev, skb);
2640 break;
2641
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002642 case HCI_OP_WRITE_LINK_POLICY:
2643 hci_cc_write_link_policy(hdev, skb);
2644 break;
2645
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002646 case HCI_OP_READ_DEF_LINK_POLICY:
2647 hci_cc_read_def_link_policy(hdev, skb);
2648 break;
2649
2650 case HCI_OP_WRITE_DEF_LINK_POLICY:
2651 hci_cc_write_def_link_policy(hdev, skb);
2652 break;
2653
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002654 case HCI_OP_RESET:
2655 hci_cc_reset(hdev, skb);
2656 break;
2657
2658 case HCI_OP_WRITE_LOCAL_NAME:
2659 hci_cc_write_local_name(hdev, skb);
2660 break;
2661
2662 case HCI_OP_READ_LOCAL_NAME:
2663 hci_cc_read_local_name(hdev, skb);
2664 break;
2665
2666 case HCI_OP_WRITE_AUTH_ENABLE:
2667 hci_cc_write_auth_enable(hdev, skb);
2668 break;
2669
2670 case HCI_OP_WRITE_ENCRYPT_MODE:
2671 hci_cc_write_encrypt_mode(hdev, skb);
2672 break;
2673
2674 case HCI_OP_WRITE_SCAN_ENABLE:
2675 hci_cc_write_scan_enable(hdev, skb);
2676 break;
2677
2678 case HCI_OP_READ_CLASS_OF_DEV:
2679 hci_cc_read_class_of_dev(hdev, skb);
2680 break;
2681
2682 case HCI_OP_WRITE_CLASS_OF_DEV:
2683 hci_cc_write_class_of_dev(hdev, skb);
2684 break;
2685
2686 case HCI_OP_READ_VOICE_SETTING:
2687 hci_cc_read_voice_setting(hdev, skb);
2688 break;
2689
2690 case HCI_OP_WRITE_VOICE_SETTING:
2691 hci_cc_write_voice_setting(hdev, skb);
2692 break;
2693
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -07002694 case HCI_OP_READ_NUM_SUPPORTED_IAC:
2695 hci_cc_read_num_supported_iac(hdev, skb);
2696 break;
2697
Marcel Holtmann333140b2008-07-14 20:13:48 +02002698 case HCI_OP_WRITE_SSP_MODE:
2699 hci_cc_write_ssp_mode(hdev, skb);
2700 break;
2701
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08002702 case HCI_OP_WRITE_SC_SUPPORT:
2703 hci_cc_write_sc_support(hdev, skb);
2704 break;
2705
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002706 case HCI_OP_READ_LOCAL_VERSION:
2707 hci_cc_read_local_version(hdev, skb);
2708 break;
2709
2710 case HCI_OP_READ_LOCAL_COMMANDS:
2711 hci_cc_read_local_commands(hdev, skb);
2712 break;
2713
2714 case HCI_OP_READ_LOCAL_FEATURES:
2715 hci_cc_read_local_features(hdev, skb);
2716 break;
2717
Andre Guedes971e3a42011-06-30 19:20:52 -03002718 case HCI_OP_READ_LOCAL_EXT_FEATURES:
2719 hci_cc_read_local_ext_features(hdev, skb);
2720 break;
2721
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002722 case HCI_OP_READ_BUFFER_SIZE:
2723 hci_cc_read_buffer_size(hdev, skb);
2724 break;
2725
2726 case HCI_OP_READ_BD_ADDR:
2727 hci_cc_read_bd_addr(hdev, skb);
2728 break;
2729
Johan Hedbergf332ec62013-03-15 17:07:11 -05002730 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
2731 hci_cc_read_page_scan_activity(hdev, skb);
2732 break;
2733
Johan Hedberg4a3ee762013-03-15 17:07:12 -05002734 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
2735 hci_cc_write_page_scan_activity(hdev, skb);
2736 break;
2737
Johan Hedbergf332ec62013-03-15 17:07:11 -05002738 case HCI_OP_READ_PAGE_SCAN_TYPE:
2739 hci_cc_read_page_scan_type(hdev, skb);
2740 break;
2741
Johan Hedberg4a3ee762013-03-15 17:07:12 -05002742 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
2743 hci_cc_write_page_scan_type(hdev, skb);
2744 break;
2745
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02002746 case HCI_OP_READ_DATA_BLOCK_SIZE:
2747 hci_cc_read_data_block_size(hdev, skb);
2748 break;
2749
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02002750 case HCI_OP_READ_FLOW_CONTROL_MODE:
2751 hci_cc_read_flow_control_mode(hdev, skb);
2752 break;
2753
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03002754 case HCI_OP_READ_LOCAL_AMP_INFO:
2755 hci_cc_read_local_amp_info(hdev, skb);
2756 break;
2757
Johan Hedberg33f35722014-06-28 17:54:06 +03002758 case HCI_OP_READ_CLOCK:
2759 hci_cc_read_clock(hdev, skb);
2760 break;
2761
Andrei Emeltchenko903e4542012-09-27 17:26:09 +03002762 case HCI_OP_READ_LOCAL_AMP_ASSOC:
2763 hci_cc_read_local_amp_assoc(hdev, skb);
2764 break;
2765
Johan Hedbergd5859e22011-01-25 01:19:58 +02002766 case HCI_OP_READ_INQ_RSP_TX_POWER:
2767 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2768 break;
2769
Johan Hedberg980e1a52011-01-22 06:10:07 +02002770 case HCI_OP_PIN_CODE_REPLY:
2771 hci_cc_pin_code_reply(hdev, skb);
2772 break;
2773
2774 case HCI_OP_PIN_CODE_NEG_REPLY:
2775 hci_cc_pin_code_neg_reply(hdev, skb);
2776 break;
2777
Szymon Jancc35938b2011-03-22 13:12:21 +01002778 case HCI_OP_READ_LOCAL_OOB_DATA:
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08002779 hci_cc_read_local_oob_data(hdev, skb);
2780 break;
2781
2782 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
2783 hci_cc_read_local_oob_ext_data(hdev, skb);
Szymon Jancc35938b2011-03-22 13:12:21 +01002784 break;
2785
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002786 case HCI_OP_LE_READ_BUFFER_SIZE:
2787 hci_cc_le_read_buffer_size(hdev, skb);
2788 break;
2789
Johan Hedberg60e77322013-01-22 14:01:59 +02002790 case HCI_OP_LE_READ_LOCAL_FEATURES:
2791 hci_cc_le_read_local_features(hdev, skb);
2792 break;
2793
Johan Hedberg8fa19092012-10-19 20:57:49 +03002794 case HCI_OP_LE_READ_ADV_TX_POWER:
2795 hci_cc_le_read_adv_tx_power(hdev, skb);
2796 break;
2797
Johan Hedberga5c29682011-02-19 12:05:57 -03002798 case HCI_OP_USER_CONFIRM_REPLY:
2799 hci_cc_user_confirm_reply(hdev, skb);
2800 break;
2801
2802 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2803 hci_cc_user_confirm_neg_reply(hdev, skb);
2804 break;
2805
Brian Gix1143d452011-11-23 08:28:34 -08002806 case HCI_OP_USER_PASSKEY_REPLY:
2807 hci_cc_user_passkey_reply(hdev, skb);
2808 break;
2809
2810 case HCI_OP_USER_PASSKEY_NEG_REPLY:
2811 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02002812 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09002813
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08002814 case HCI_OP_LE_SET_RANDOM_ADDR:
2815 hci_cc_le_set_random_addr(hdev, skb);
2816 break;
2817
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01002818 case HCI_OP_LE_SET_ADV_ENABLE:
2819 hci_cc_le_set_adv_enable(hdev, skb);
2820 break;
2821
Marcel Holtmann533553f2014-03-21 12:18:10 -07002822 case HCI_OP_LE_SET_SCAN_PARAM:
2823 hci_cc_le_set_scan_param(hdev, skb);
2824 break;
2825
Andre Guedeseb9d91f2011-05-26 16:23:52 -03002826 case HCI_OP_LE_SET_SCAN_ENABLE:
2827 hci_cc_le_set_scan_enable(hdev, skb);
2828 break;
2829
Johan Hedbergcf1d0812013-01-22 14:02:00 +02002830 case HCI_OP_LE_READ_WHITE_LIST_SIZE:
2831 hci_cc_le_read_white_list_size(hdev, skb);
2832 break;
2833
Marcel Holtmann0f36b582014-02-27 20:37:31 -08002834 case HCI_OP_LE_CLEAR_WHITE_LIST:
2835 hci_cc_le_clear_white_list(hdev, skb);
2836 break;
2837
2838 case HCI_OP_LE_ADD_TO_WHITE_LIST:
2839 hci_cc_le_add_to_white_list(hdev, skb);
2840 break;
2841
2842 case HCI_OP_LE_DEL_FROM_WHITE_LIST:
2843 hci_cc_le_del_from_white_list(hdev, skb);
2844 break;
2845
Johan Hedberg9b008c02013-01-22 14:02:01 +02002846 case HCI_OP_LE_READ_SUPPORTED_STATES:
2847 hci_cc_le_read_supported_states(hdev, skb);
2848 break;
2849
Andre Guedesf9b49302011-06-30 19:20:53 -03002850 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
2851 hci_cc_write_le_host_supported(hdev, skb);
2852 break;
2853
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02002854 case HCI_OP_LE_SET_ADV_PARAM:
2855 hci_cc_set_adv_param(hdev, skb);
2856 break;
2857
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03002858 case HCI_OP_WRITE_REMOTE_AMP_ASSOC:
2859 hci_cc_write_remote_amp_assoc(hdev, skb);
2860 break;
2861
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02002862 case HCI_OP_READ_RSSI:
2863 hci_cc_read_rssi(hdev, skb);
2864 break;
2865
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02002866 case HCI_OP_READ_TX_POWER:
2867 hci_cc_read_tx_power(hdev, skb);
2868 break;
2869
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002870 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002871 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002872 break;
2873 }
2874
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002875 if (opcode != HCI_OP_NOP)
Marcel Holtmann65cc2b42014-06-16 12:30:56 +02002876 cancel_delayed_work(&hdev->cmd_timer);
Ville Tervo6bd32322011-02-16 16:32:41 +02002877
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002878 hci_req_cmd_complete(hdev, opcode, status);
Johan Hedberg9238f362013-03-05 20:37:48 +02002879
Szymon Jancdbccd792012-12-11 08:51:19 +01002880 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002881 atomic_set(&hdev->cmd_cnt, 1);
2882 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002883 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002884 }
2885}
2886
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002887static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002888{
2889 struct hci_ev_cmd_status *ev = (void *) skb->data;
2890 __u16 opcode;
2891
2892 skb_pull(skb, sizeof(*ev));
2893
2894 opcode = __le16_to_cpu(ev->opcode);
2895
2896 switch (opcode) {
2897 case HCI_OP_INQUIRY:
2898 hci_cs_inquiry(hdev, ev->status);
2899 break;
2900
2901 case HCI_OP_CREATE_CONN:
2902 hci_cs_create_conn(hdev, ev->status);
2903 break;
2904
Kuba Pawlak9645c762014-11-06 19:36:53 +01002905 case HCI_OP_DISCONNECT:
2906 hci_cs_disconnect(hdev, ev->status);
2907 break;
2908
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002909 case HCI_OP_ADD_SCO:
2910 hci_cs_add_sco(hdev, ev->status);
2911 break;
2912
Marcel Holtmannf8558552008-07-14 20:13:49 +02002913 case HCI_OP_AUTH_REQUESTED:
2914 hci_cs_auth_requested(hdev, ev->status);
2915 break;
2916
2917 case HCI_OP_SET_CONN_ENCRYPT:
2918 hci_cs_set_conn_encrypt(hdev, ev->status);
2919 break;
2920
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002921 case HCI_OP_REMOTE_NAME_REQ:
2922 hci_cs_remote_name_req(hdev, ev->status);
2923 break;
2924
Marcel Holtmann769be972008-07-14 20:13:49 +02002925 case HCI_OP_READ_REMOTE_FEATURES:
2926 hci_cs_read_remote_features(hdev, ev->status);
2927 break;
2928
2929 case HCI_OP_READ_REMOTE_EXT_FEATURES:
2930 hci_cs_read_remote_ext_features(hdev, ev->status);
2931 break;
2932
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002933 case HCI_OP_SETUP_SYNC_CONN:
2934 hci_cs_setup_sync_conn(hdev, ev->status);
2935 break;
2936
Kuba Pawlak9645c762014-11-06 19:36:53 +01002937 case HCI_OP_CREATE_PHY_LINK:
2938 hci_cs_create_phylink(hdev, ev->status);
2939 break;
2940
2941 case HCI_OP_ACCEPT_PHY_LINK:
2942 hci_cs_accept_phylink(hdev, ev->status);
2943 break;
2944
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002945 case HCI_OP_SNIFF_MODE:
2946 hci_cs_sniff_mode(hdev, ev->status);
2947 break;
2948
2949 case HCI_OP_EXIT_SNIFF_MODE:
2950 hci_cs_exit_sniff_mode(hdev, ev->status);
2951 break;
2952
Kuba Pawlak50fc85f2014-11-06 19:36:52 +01002953 case HCI_OP_SWITCH_ROLE:
2954 hci_cs_switch_role(hdev, ev->status);
2955 break;
2956
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002957 case HCI_OP_LE_CREATE_CONN:
2958 hci_cs_le_create_conn(hdev, ev->status);
2959 break;
2960
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02002961 case HCI_OP_LE_START_ENC:
2962 hci_cs_le_start_enc(hdev, ev->status);
2963 break;
2964
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002965 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002966 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002967 break;
2968 }
2969
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002970 if (opcode != HCI_OP_NOP)
Marcel Holtmann65cc2b42014-06-16 12:30:56 +02002971 cancel_delayed_work(&hdev->cmd_timer);
Ville Tervo6bd32322011-02-16 16:32:41 +02002972
Johan Hedberg02350a72013-04-03 21:50:29 +03002973 if (ev->status ||
2974 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event))
2975 hci_req_cmd_complete(hdev, opcode, ev->status);
Johan Hedberg9238f362013-03-05 20:37:48 +02002976
Gustavo F. Padovan10572132011-03-16 15:36:29 -03002977 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002978 atomic_set(&hdev->cmd_cnt, 1);
2979 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002980 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002981 }
2982}
2983
Marcel Holtmann24dfa342014-11-02 02:56:41 +01002984static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
2985{
2986 struct hci_ev_hardware_error *ev = (void *) skb->data;
2987
2988 BT_ERR("%s hardware error 0x%2.2x", hdev->name, ev->code);
2989}
2990
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002991static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002992{
2993 struct hci_ev_role_change *ev = (void *) skb->data;
2994 struct hci_conn *conn;
2995
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002996 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002997
2998 hci_dev_lock(hdev);
2999
3000 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3001 if (conn) {
Johan Hedberg40bef302014-07-16 11:42:27 +03003002 if (!ev->status)
3003 conn->role = ev->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003004
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003005 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003006
3007 hci_role_switch_cfm(conn, ev->status, ev->role);
3008 }
3009
3010 hci_dev_unlock(hdev);
3011}
3012
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003013static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003015 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 int i;
3017
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02003018 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
3019 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
3020 return;
3021 }
3022
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02003023 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03003024 ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 BT_DBG("%s bad parameters", hdev->name);
3026 return;
3027 }
3028
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02003029 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
3030
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003031 for (i = 0; i < ev->num_hndl; i++) {
3032 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 struct hci_conn *conn;
3034 __u16 handle, count;
3035
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02003036 handle = __le16_to_cpu(info->handle);
3037 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038
3039 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003040 if (!conn)
3041 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003043 conn->sent -= count;
3044
3045 switch (conn->type) {
3046 case ACL_LINK:
3047 hdev->acl_cnt += count;
3048 if (hdev->acl_cnt > hdev->acl_pkts)
3049 hdev->acl_cnt = hdev->acl_pkts;
3050 break;
3051
3052 case LE_LINK:
3053 if (hdev->le_pkts) {
3054 hdev->le_cnt += count;
3055 if (hdev->le_cnt > hdev->le_pkts)
3056 hdev->le_cnt = hdev->le_pkts;
3057 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02003058 hdev->acl_cnt += count;
3059 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 hdev->acl_cnt = hdev->acl_pkts;
3061 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02003062 break;
3063
3064 case SCO_LINK:
3065 hdev->sco_cnt += count;
3066 if (hdev->sco_cnt > hdev->sco_pkts)
3067 hdev->sco_cnt = hdev->sco_pkts;
3068 break;
3069
3070 default:
3071 BT_ERR("Unknown type %d conn %p", conn->type, conn);
3072 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
3074 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003075
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02003076 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077}
3078
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003079static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
3080 __u16 handle)
3081{
3082 struct hci_chan *chan;
3083
3084 switch (hdev->dev_type) {
3085 case HCI_BREDR:
3086 return hci_conn_hash_lookup_handle(hdev, handle);
3087 case HCI_AMP:
3088 chan = hci_chan_lookup_handle(hdev, handle);
3089 if (chan)
3090 return chan->conn;
3091 break;
3092 default:
3093 BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type);
3094 break;
3095 }
3096
3097 return NULL;
3098}
3099
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003100static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003101{
3102 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
3103 int i;
3104
3105 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
3106 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
3107 return;
3108 }
3109
3110 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03003111 ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003112 BT_DBG("%s bad parameters", hdev->name);
3113 return;
3114 }
3115
3116 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003117 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003118
3119 for (i = 0; i < ev->num_hndl; i++) {
3120 struct hci_comp_blocks_info *info = &ev->handles[i];
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003121 struct hci_conn *conn = NULL;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003122 __u16 handle, block_count;
3123
3124 handle = __le16_to_cpu(info->handle);
3125 block_count = __le16_to_cpu(info->blocks);
3126
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003127 conn = __hci_conn_lookup_handle(hdev, handle);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003128 if (!conn)
3129 continue;
3130
3131 conn->sent -= block_count;
3132
3133 switch (conn->type) {
3134 case ACL_LINK:
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03003135 case AMP_LINK:
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003136 hdev->block_cnt += block_count;
3137 if (hdev->block_cnt > hdev->num_blocks)
3138 hdev->block_cnt = hdev->num_blocks;
3139 break;
3140
3141 default:
3142 BT_ERR("Unknown type %d conn %p", conn->type, conn);
3143 break;
3144 }
3145 }
3146
3147 queue_work(hdev->workqueue, &hdev->tx_work);
3148}
3149
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003150static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003152 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003153 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003155 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
3157 hci_dev_lock(hdev);
3158
Marcel Holtmann04837f62006-07-03 10:02:33 +02003159 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3160 if (conn) {
3161 conn->mode = ev->mode;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003162
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003163 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
3164 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02003165 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02003166 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003167 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02003168 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003169 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003170
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003171 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003172 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003173 }
3174
3175 hci_dev_unlock(hdev);
3176}
3177
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003178static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003180 struct hci_ev_pin_code_req *ev = (void *) skb->data;
3181 struct hci_conn *conn;
3182
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003183 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003184
3185 hci_dev_lock(hdev);
3186
3187 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003188 if (!conn)
3189 goto unlock;
3190
3191 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003192 hci_conn_hold(conn);
3193 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003194 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003195 }
3196
Johan Hedbergb6ae8452014-07-30 09:22:22 +03003197 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedberg2f407f02014-07-17 15:35:40 +03003198 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
Johan Hedberg03b555e2011-01-04 15:40:05 +02003199 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003200 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg2f407f02014-07-17 15:35:40 +03003201 } else if (test_bit(HCI_MGMT, &hdev->dev_flags)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003202 u8 secure;
3203
3204 if (conn->pending_sec_level == BT_SECURITY_HIGH)
3205 secure = 1;
3206 else
3207 secure = 0;
3208
Johan Hedberg744cf192011-11-08 20:40:14 +02003209 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003210 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003211
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003212unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003213 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214}
3215
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02003216static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
3217{
3218 if (key_type == HCI_LK_CHANGED_COMBINATION)
3219 return;
3220
3221 conn->pin_length = pin_len;
3222 conn->key_type = key_type;
3223
3224 switch (key_type) {
3225 case HCI_LK_LOCAL_UNIT:
3226 case HCI_LK_REMOTE_UNIT:
3227 case HCI_LK_DEBUG_COMBINATION:
3228 return;
3229 case HCI_LK_COMBINATION:
3230 if (pin_len == 16)
3231 conn->pending_sec_level = BT_SECURITY_HIGH;
3232 else
3233 conn->pending_sec_level = BT_SECURITY_MEDIUM;
3234 break;
3235 case HCI_LK_UNAUTH_COMBINATION_P192:
3236 case HCI_LK_UNAUTH_COMBINATION_P256:
3237 conn->pending_sec_level = BT_SECURITY_MEDIUM;
3238 break;
3239 case HCI_LK_AUTH_COMBINATION_P192:
3240 conn->pending_sec_level = BT_SECURITY_HIGH;
3241 break;
3242 case HCI_LK_AUTH_COMBINATION_P256:
3243 conn->pending_sec_level = BT_SECURITY_FIPS;
3244 break;
3245 }
3246}
3247
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003248static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003250 struct hci_ev_link_key_req *ev = (void *) skb->data;
3251 struct hci_cp_link_key_reply cp;
3252 struct hci_conn *conn;
3253 struct link_key *key;
3254
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003255 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003256
Andrei Emeltchenko034cbea2013-05-14 11:44:16 +03003257 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003258 return;
3259
3260 hci_dev_lock(hdev);
3261
3262 key = hci_find_link_key(hdev, &ev->bdaddr);
3263 if (!key) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003264 BT_DBG("%s link key not found for %pMR", hdev->name,
3265 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003266 goto not_found;
3267 }
3268
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003269 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
3270 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003271
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003272 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003273 if (conn) {
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03003274 clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
3275
Marcel Holtmann66138ce2014-01-10 02:07:20 -08003276 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
3277 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03003278 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003279 BT_DBG("%s ignoring unauthenticated key", hdev->name);
3280 goto not_found;
3281 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003282
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003283 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Johan Hedbergf3fb0b52014-06-02 10:12:44 +03003284 (conn->pending_sec_level == BT_SECURITY_HIGH ||
3285 conn->pending_sec_level == BT_SECURITY_FIPS)) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003286 BT_DBG("%s ignoring key unauthenticated for high security",
3287 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003288 goto not_found;
3289 }
3290
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02003291 conn_set_key(conn, key->type, key->pin_len);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003292 }
3293
3294 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03003295 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003296
3297 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
3298
3299 hci_dev_unlock(hdev);
3300
3301 return;
3302
3303not_found:
3304 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
3305 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306}
3307
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003308static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003310 struct hci_ev_link_key_notify *ev = (void *) skb->data;
3311 struct hci_conn *conn;
Johan Hedberg7652ff62014-06-24 13:15:49 +03003312 struct link_key *key;
3313 bool persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003314 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003315
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003316 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003317
3318 hci_dev_lock(hdev);
3319
3320 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg82c13d42014-12-03 11:03:06 +02003321 if (!conn)
3322 goto unlock;
3323
3324 hci_conn_hold(conn);
3325 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
3326 hci_conn_drop(conn);
3327
Johan Hedbergfe8bc5a2014-08-14 12:33:17 +03003328 set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
Johan Hedberg82c13d42014-12-03 11:03:06 +02003329 conn_set_key(conn, ev->key_type, conn->pin_length);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003330
Johan Hedberg7652ff62014-06-24 13:15:49 +03003331 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
3332 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003333
Johan Hedberg7652ff62014-06-24 13:15:49 +03003334 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
3335 ev->key_type, pin_len, &persistent);
3336 if (!key)
3337 goto unlock;
3338
Johan Hedbergcb6f3f72014-11-19 14:53:04 +02003339 /* Update connection information since adding the key will have
3340 * fixed up the type in the case of changed combination keys.
3341 */
3342 if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
3343 conn_set_key(conn, key->type, key->pin_len);
3344
Johan Hedberg7652ff62014-06-24 13:15:49 +03003345 mgmt_new_link_key(hdev, key, persistent);
3346
Johan Hedberg6d5650c2014-06-24 13:15:51 +03003347 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
3348 * is set. If it's not set simply remove the key from the kernel
3349 * list (we've still notified user space about it but with
3350 * store_hint being 0).
3351 */
3352 if (key->type == HCI_LK_DEBUG_COMBINATION &&
3353 !test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) {
Johan Hedberg0378b592014-11-19 15:22:22 +02003354 list_del_rcu(&key->list);
3355 kfree_rcu(key, rcu);
Johan Hedberg82c13d42014-12-03 11:03:06 +02003356 goto unlock;
Johan Hedberg6d5650c2014-06-24 13:15:51 +03003357 }
Johan Hedberg7652ff62014-06-24 13:15:49 +03003358
Johan Hedberg82c13d42014-12-03 11:03:06 +02003359 if (persistent)
3360 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
3361 else
3362 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
3363
Johan Hedberg7652ff62014-06-24 13:15:49 +03003364unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003365 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366}
3367
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003368static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02003369{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003370 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003371 struct hci_conn *conn;
3372
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003373 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003374
3375 hci_dev_lock(hdev);
3376
3377 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 if (conn && !ev->status) {
3379 struct inquiry_entry *ie;
3380
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003381 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3382 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 ie->data.clock_offset = ev->clock_offset;
3384 ie->timestamp = jiffies;
3385 }
3386 }
3387
3388 hci_dev_unlock(hdev);
3389}
3390
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003391static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02003392{
3393 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
3394 struct hci_conn *conn;
3395
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003396 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02003397
3398 hci_dev_lock(hdev);
3399
3400 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3401 if (conn && !ev->status)
3402 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
3403
3404 hci_dev_unlock(hdev);
3405}
3406
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003407static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003408{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003409 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003410 struct inquiry_entry *ie;
3411
3412 BT_DBG("%s", hdev->name);
3413
3414 hci_dev_lock(hdev);
3415
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003416 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3417 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003418 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
3419 ie->timestamp = jiffies;
3420 }
3421
3422 hci_dev_unlock(hdev);
3423}
3424
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003425static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
3426 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003427{
3428 struct inquiry_data data;
3429 int num_rsp = *((__u8 *) skb->data);
3430
3431 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3432
3433 if (!num_rsp)
3434 return;
3435
Andre Guedes1519cc12012-03-21 00:03:38 -03003436 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3437 return;
3438
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003439 hci_dev_lock(hdev);
3440
3441 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01003442 struct inquiry_info_with_rssi_and_pscan_mode *info;
3443 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003444
Johan Hedberge17acd42011-03-30 23:57:16 +03003445 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003446 u32 flags;
3447
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003448 bacpy(&data.bdaddr, &info->bdaddr);
3449 data.pscan_rep_mode = info->pscan_rep_mode;
3450 data.pscan_period_mode = info->pscan_period_mode;
3451 data.pscan_mode = info->pscan_mode;
3452 memcpy(data.dev_class, info->dev_class, 3);
3453 data.clock_offset = info->clock_offset;
3454 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003455 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02003456
Marcel Holtmannaf589252014-07-01 14:11:20 +02003457 flags = hci_inquiry_cache_update(hdev, &data, false);
3458
Johan Hedberg48264f02011-11-09 13:58:58 +02003459 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003460 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003461 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003462 }
3463 } else {
3464 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
3465
Johan Hedberge17acd42011-03-30 23:57:16 +03003466 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003467 u32 flags;
3468
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003469 bacpy(&data.bdaddr, &info->bdaddr);
3470 data.pscan_rep_mode = info->pscan_rep_mode;
3471 data.pscan_period_mode = info->pscan_period_mode;
3472 data.pscan_mode = 0x00;
3473 memcpy(data.dev_class, info->dev_class, 3);
3474 data.clock_offset = info->clock_offset;
3475 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003476 data.ssp_mode = 0x00;
Marcel Holtmannaf589252014-07-01 14:11:20 +02003477
3478 flags = hci_inquiry_cache_update(hdev, &data, false);
3479
Johan Hedberg48264f02011-11-09 13:58:58 +02003480 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003481 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003482 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003483 }
3484 }
3485
3486 hci_dev_unlock(hdev);
3487}
3488
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003489static void hci_remote_ext_features_evt(struct hci_dev *hdev,
3490 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003491{
Marcel Holtmann41a96212008-07-14 20:13:48 +02003492 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
3493 struct hci_conn *conn;
3494
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003495 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003496
Marcel Holtmann41a96212008-07-14 20:13:48 +02003497 hci_dev_lock(hdev);
3498
3499 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02003500 if (!conn)
3501 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003502
Johan Hedbergcad718e2013-04-17 15:00:51 +03003503 if (ev->page < HCI_MAX_PAGES)
3504 memcpy(conn->features[ev->page], ev->features, 8);
3505
Johan Hedbergccd556f2010-11-10 17:11:51 +02003506 if (!ev->status && ev->page == 0x01) {
3507 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003508
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003509 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3510 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02003511 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02003512
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05303513 if (ev->features[0] & LMP_HOST_SSP) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02003514 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05303515 } else {
3516 /* It is mandatory by the Bluetooth specification that
3517 * Extended Inquiry Results are only used when Secure
3518 * Simple Pairing is enabled, but some devices violate
3519 * this.
3520 *
3521 * To make these devices work, the internal SSP
3522 * enabled flag needs to be cleared if the remote host
3523 * features do not indicate SSP support */
3524 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
3525 }
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -08003526
3527 if (ev->features[0] & LMP_HOST_SC)
3528 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003529 }
3530
Johan Hedbergccd556f2010-11-10 17:11:51 +02003531 if (conn->state != BT_CONFIG)
3532 goto unlock;
3533
Johan Hedberg671267b2012-05-12 16:11:50 -03003534 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003535 struct hci_cp_remote_name_req cp;
3536 memset(&cp, 0, sizeof(cp));
3537 bacpy(&cp.bdaddr, &conn->dst);
3538 cp.pscan_rep_mode = 0x02;
3539 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003540 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00003541 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02003542
Johan Hedberg127178d2010-11-18 22:22:29 +02003543 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003544 conn->state = BT_CONNECTED;
3545 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003546 hci_conn_drop(conn);
Johan Hedbergccd556f2010-11-10 17:11:51 +02003547 }
3548
3549unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02003550 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003551}
3552
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003553static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
3554 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003555{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003556 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
3557 struct hci_conn *conn;
3558
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003559 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003560
3561 hci_dev_lock(hdev);
3562
3563 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02003564 if (!conn) {
3565 if (ev->link_type == ESCO_LINK)
3566 goto unlock;
3567
3568 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
3569 if (!conn)
3570 goto unlock;
3571
3572 conn->type = SCO_LINK;
3573 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003574
Marcel Holtmann732547f2009-04-19 19:14:14 +02003575 switch (ev->status) {
3576 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003577 conn->handle = __le16_to_cpu(ev->handle);
3578 conn->state = BT_CONNECTED;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02003579
3580 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02003581 break;
3582
Nick Pelly81218d22014-06-30 11:25:01 +05303583 case 0x10: /* Connection Accept Timeout */
Frédéric Dalleau1a4c9582013-08-19 14:24:02 +02003584 case 0x0d: /* Connection Rejected due to Limited Resources */
Stephen Coe705e5712010-02-16 11:29:44 -05003585 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02003586 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08003587 case 0x1a: /* Unsupported Remote Feature */
Marcel Holtmann732547f2009-04-19 19:14:14 +02003588 case 0x1f: /* Unspecified error */
Andrew Earl27539bc2014-03-10 10:31:04 +00003589 case 0x20: /* Unsupported LMP Parameter value */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02003590 if (conn->out) {
Marcel Holtmann732547f2009-04-19 19:14:14 +02003591 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
3592 (hdev->esco_type & EDR_ESCO_MASK);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02003593 if (hci_setup_sync(conn, conn->link->handle))
3594 goto unlock;
Marcel Holtmann732547f2009-04-19 19:14:14 +02003595 }
3596 /* fall through */
3597
3598 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003599 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02003600 break;
3601 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003602
3603 hci_proto_connect_cfm(conn, ev->status);
3604 if (ev->status)
3605 hci_conn_del(conn);
3606
3607unlock:
3608 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003609}
3610
Marcel Holtmannefdcf8e2013-10-15 10:31:12 -07003611static inline size_t eir_get_length(u8 *eir, size_t eir_len)
3612{
3613 size_t parsed = 0;
3614
3615 while (parsed < eir_len) {
3616 u8 field_len = eir[0];
3617
3618 if (field_len == 0)
3619 return parsed;
3620
3621 parsed += field_len + 1;
3622 eir += field_len + 1;
3623 }
3624
3625 return eir_len;
3626}
3627
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003628static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
3629 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003630{
3631 struct inquiry_data data;
3632 struct extended_inquiry_info *info = (void *) (skb->data + 1);
3633 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303634 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003635
3636 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3637
3638 if (!num_rsp)
3639 return;
3640
Andre Guedes1519cc12012-03-21 00:03:38 -03003641 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3642 return;
3643
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003644 hci_dev_lock(hdev);
3645
Johan Hedberge17acd42011-03-30 23:57:16 +03003646 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003647 u32 flags;
3648 bool name_known;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003649
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003650 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01003651 data.pscan_rep_mode = info->pscan_rep_mode;
3652 data.pscan_period_mode = info->pscan_period_mode;
3653 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003654 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01003655 data.clock_offset = info->clock_offset;
3656 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003657 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003658
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003659 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg4ddb1932012-01-15 20:04:43 +02003660 name_known = eir_has_data_type(info->data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003661 sizeof(info->data),
3662 EIR_NAME_COMPLETE);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003663 else
3664 name_known = true;
3665
Marcel Holtmannaf589252014-07-01 14:11:20 +02003666 flags = hci_inquiry_cache_update(hdev, &data, name_known);
3667
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303668 eir_len = eir_get_length(info->data, sizeof(info->data));
Marcel Holtmannaf589252014-07-01 14:11:20 +02003669
Johan Hedberg48264f02011-11-09 13:58:58 +02003670 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003671 info->dev_class, info->rssi,
3672 flags, info->data, eir_len, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003673 }
3674
3675 hci_dev_unlock(hdev);
3676}
3677
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003678static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
3679 struct sk_buff *skb)
3680{
3681 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
3682 struct hci_conn *conn;
3683
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003684 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003685 __le16_to_cpu(ev->handle));
3686
3687 hci_dev_lock(hdev);
3688
3689 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3690 if (!conn)
3691 goto unlock;
3692
Johan Hedberg9eb1fbf2014-04-11 12:02:31 -07003693 /* For BR/EDR the necessary steps are taken through the
3694 * auth_complete event.
3695 */
3696 if (conn->type != LE_LINK)
3697 goto unlock;
3698
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003699 if (!ev->status)
3700 conn->sec_level = conn->pending_sec_level;
3701
3702 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3703
3704 if (ev->status && conn->state == BT_CONNECTED) {
Andre Guedesbed71742013-01-30 11:50:56 -03003705 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
David Herrmann76a68ba2013-04-06 20:28:37 +02003706 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003707 goto unlock;
3708 }
3709
3710 if (conn->state == BT_CONFIG) {
3711 if (!ev->status)
3712 conn->state = BT_CONNECTED;
3713
3714 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003715 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003716 } else {
3717 hci_auth_cfm(conn, ev->status);
3718
3719 hci_conn_hold(conn);
3720 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003721 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003722 }
3723
3724unlock:
3725 hci_dev_unlock(hdev);
3726}
3727
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003728static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003729{
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003730 /* If remote requests no-bonding follow that lead */
Mikel Astizacabae92013-06-28 10:56:28 +02003731 if (conn->remote_auth == HCI_AT_NO_BONDING ||
3732 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02003733 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003734
Mikel Astizb7f94c82014-04-08 14:21:31 +02003735 /* If both remote and local have enough IO capabilities, require
3736 * MITM protection
3737 */
3738 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
3739 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
3740 return conn->remote_auth | 0x01;
3741
Timo Mueller7e741702014-04-08 14:21:33 +02003742 /* No MITM protection possible so ignore remote requirement */
3743 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003744}
3745
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003746static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003747{
3748 struct hci_ev_io_capa_request *ev = (void *) skb->data;
3749 struct hci_conn *conn;
3750
3751 BT_DBG("%s", hdev->name);
3752
3753 hci_dev_lock(hdev);
3754
3755 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003756 if (!conn)
3757 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003758
Johan Hedberg03b555e2011-01-04 15:40:05 +02003759 hci_conn_hold(conn);
3760
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003761 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg03b555e2011-01-04 15:40:05 +02003762 goto unlock;
3763
Johan Hedberg2f407f02014-07-17 15:35:40 +03003764 /* Allow pairing if we're pairable, the initiators of the
3765 * pairing or if the remote is not requesting bonding.
3766 */
Johan Hedbergb6ae8452014-07-30 09:22:22 +03003767 if (test_bit(HCI_BONDABLE, &hdev->dev_flags) ||
Johan Hedberg2f407f02014-07-17 15:35:40 +03003768 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03003769 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003770 struct hci_cp_io_capability_reply cp;
3771
3772 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05303773 /* Change the IO capability from KeyboardDisplay
3774 * to DisplayYesNo as it is not supported by BT spec. */
3775 cp.capability = (conn->io_capability == 0x04) ?
Mikel Astiza7676312013-06-28 10:56:29 +02003776 HCI_IO_DISPLAY_YESNO : conn->io_capability;
Mikel Astizb7f94c82014-04-08 14:21:31 +02003777
3778 /* If we are initiators, there is no remote information yet */
3779 if (conn->remote_auth == 0xff) {
Mikel Astizb16c6602014-04-08 14:21:34 +02003780 /* Request MITM protection if our IO caps allow it
Johan Hedberg4ad51a72014-06-09 14:41:25 +03003781 * except for the no-bonding case.
Mikel Astizb16c6602014-04-08 14:21:34 +02003782 */
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003783 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedberg9f743d72014-07-17 11:56:33 +03003784 conn->auth_type != HCI_AT_NO_BONDING)
Johan Hedberg6c538232014-07-11 15:32:23 +03003785 conn->auth_type |= 0x01;
Mikel Astizb7f94c82014-04-08 14:21:31 +02003786 } else {
3787 conn->auth_type = hci_get_auth_req(conn);
Mikel Astizb7f94c82014-04-08 14:21:31 +02003788 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003789
Johan Hedberg82c295b2014-07-30 09:22:24 +03003790 /* If we're not bondable, force one of the non-bondable
3791 * authentication requirement values.
3792 */
3793 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags))
3794 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
3795
3796 cp.authentication = conn->auth_type;
3797
Johan Hedberg6928a922014-10-26 20:46:09 +01003798 if (hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR) &&
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003799 (conn->out || test_bit(HCI_CONN_REMOTE_OOB, &conn->flags)))
Szymon Jancce85ee12011-03-22 13:12:23 +01003800 cp.oob_data = 0x01;
3801 else
3802 cp.oob_data = 0x00;
3803
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003804 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003805 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003806 } else {
3807 struct hci_cp_io_capability_neg_reply cp;
3808
3809 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003810 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003811
3812 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003813 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003814 }
3815
3816unlock:
3817 hci_dev_unlock(hdev);
3818}
3819
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003820static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02003821{
3822 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
3823 struct hci_conn *conn;
3824
3825 BT_DBG("%s", hdev->name);
3826
3827 hci_dev_lock(hdev);
3828
3829 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3830 if (!conn)
3831 goto unlock;
3832
Johan Hedberg03b555e2011-01-04 15:40:05 +02003833 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003834 conn->remote_auth = ev->authentication;
Johan Hedberg58a681e2012-01-16 06:47:28 +02003835 if (ev->oob_data)
3836 set_bit(HCI_CONN_REMOTE_OOB, &conn->flags);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003837
3838unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003839 hci_dev_unlock(hdev);
3840}
3841
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003842static void hci_user_confirm_request_evt(struct hci_dev *hdev,
3843 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03003844{
3845 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003846 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07003847 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003848
3849 BT_DBG("%s", hdev->name);
3850
3851 hci_dev_lock(hdev);
3852
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003853 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg7a828902011-04-28 11:28:53 -07003854 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03003855
Johan Hedberg7a828902011-04-28 11:28:53 -07003856 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3857 if (!conn)
3858 goto unlock;
3859
3860 loc_mitm = (conn->auth_type & 0x01);
3861 rem_mitm = (conn->remote_auth & 0x01);
3862
3863 /* If we require MITM but the remote device can't provide that
Johan Hedberg6c538232014-07-11 15:32:23 +03003864 * (it has NoInputNoOutput) then reject the confirmation
3865 * request. We check the security level here since it doesn't
3866 * necessarily match conn->auth_type.
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003867 */
Johan Hedberg6c538232014-07-11 15:32:23 +03003868 if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
3869 conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
Johan Hedberg7a828902011-04-28 11:28:53 -07003870 BT_DBG("Rejecting request: remote device can't provide MITM");
3871 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003872 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003873 goto unlock;
3874 }
3875
3876 /* If no side requires MITM protection; auto-accept */
Mikel Astiza7676312013-06-28 10:56:29 +02003877 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
3878 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003879
3880 /* If we're not the initiators request authorization to
3881 * proceed from user space (mgmt_user_confirm with
Johan Hedbergba15a582014-06-09 13:58:14 +03003882 * confirm_hint set to 1). The exception is if neither
Johan Hedberg02f3e252014-07-16 15:09:13 +03003883 * side had MITM or if the local IO capability is
3884 * NoInputNoOutput, in which case we do auto-accept
Johan Hedbergba15a582014-06-09 13:58:14 +03003885 */
3886 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
Johan Hedberg02f3e252014-07-16 15:09:13 +03003887 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedbergba15a582014-06-09 13:58:14 +03003888 (loc_mitm || rem_mitm)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003889 BT_DBG("Confirming auto-accept as acceptor");
3890 confirm_hint = 1;
3891 goto confirm;
3892 }
3893
Johan Hedberg9f616562011-04-28 11:28:54 -07003894 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03003895 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003896
3897 if (hdev->auto_accept_delay > 0) {
3898 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03003899 queue_delayed_work(conn->hdev->workqueue,
3900 &conn->auto_accept_work, delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003901 goto unlock;
3902 }
3903
Johan Hedberg7a828902011-04-28 11:28:53 -07003904 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003905 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003906 goto unlock;
3907 }
3908
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003909confirm:
Johan Hedberg39adbff2014-03-20 08:18:14 +02003910 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
3911 le32_to_cpu(ev->passkey), confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07003912
3913unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03003914 hci_dev_unlock(hdev);
3915}
3916
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003917static void hci_user_passkey_request_evt(struct hci_dev *hdev,
3918 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08003919{
3920 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
3921
3922 BT_DBG("%s", hdev->name);
3923
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003924 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +02003925 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08003926}
3927
Johan Hedberg92a25252012-09-06 18:39:26 +03003928static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
3929 struct sk_buff *skb)
3930{
3931 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
3932 struct hci_conn *conn;
3933
3934 BT_DBG("%s", hdev->name);
3935
3936 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3937 if (!conn)
3938 return;
3939
3940 conn->passkey_notify = __le32_to_cpu(ev->passkey);
3941 conn->passkey_entered = 0;
3942
3943 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3944 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3945 conn->dst_type, conn->passkey_notify,
3946 conn->passkey_entered);
3947}
3948
3949static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
3950{
3951 struct hci_ev_keypress_notify *ev = (void *) skb->data;
3952 struct hci_conn *conn;
3953
3954 BT_DBG("%s", hdev->name);
3955
3956 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3957 if (!conn)
3958 return;
3959
3960 switch (ev->type) {
3961 case HCI_KEYPRESS_STARTED:
3962 conn->passkey_entered = 0;
3963 return;
3964
3965 case HCI_KEYPRESS_ENTERED:
3966 conn->passkey_entered++;
3967 break;
3968
3969 case HCI_KEYPRESS_ERASED:
3970 conn->passkey_entered--;
3971 break;
3972
3973 case HCI_KEYPRESS_CLEARED:
3974 conn->passkey_entered = 0;
3975 break;
3976
3977 case HCI_KEYPRESS_COMPLETED:
3978 return;
3979 }
3980
3981 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3982 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3983 conn->dst_type, conn->passkey_notify,
3984 conn->passkey_entered);
3985}
3986
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003987static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
3988 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003989{
3990 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
3991 struct hci_conn *conn;
3992
3993 BT_DBG("%s", hdev->name);
3994
3995 hci_dev_lock(hdev);
3996
3997 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03003998 if (!conn)
3999 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02004000
Johan Hedbergc1d4fa72014-07-17 15:14:50 +03004001 /* Reset the authentication requirement to unknown */
4002 conn->remote_auth = 0xff;
4003
Johan Hedberg2a611692011-02-19 12:06:00 -03004004 /* To avoid duplicate auth_failed events to user space we check
4005 * the HCI_CONN_AUTH_PEND flag which will be set if we
4006 * initiated the authentication. A traditional auth_complete
4007 * event gets always produced as initiator and is also mapped to
4008 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02004009 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedberge1e930f2014-09-08 17:09:49 -07004010 mgmt_auth_failed(conn, ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03004011
David Herrmann76a68ba2013-04-06 20:28:37 +02004012 hci_conn_drop(conn);
Johan Hedberg2a611692011-02-19 12:06:00 -03004013
4014unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02004015 hci_dev_unlock(hdev);
4016}
4017
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004018static void hci_remote_host_features_evt(struct hci_dev *hdev,
4019 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02004020{
4021 struct hci_ev_remote_host_features *ev = (void *) skb->data;
4022 struct inquiry_entry *ie;
Johan Hedbergcad718e2013-04-17 15:00:51 +03004023 struct hci_conn *conn;
Marcel Holtmann41a96212008-07-14 20:13:48 +02004024
4025 BT_DBG("%s", hdev->name);
4026
4027 hci_dev_lock(hdev);
4028
Johan Hedbergcad718e2013-04-17 15:00:51 +03004029 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
4030 if (conn)
4031 memcpy(conn->features[1], ev->features, 8);
4032
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02004033 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
4034 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02004035 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02004036
4037 hci_dev_unlock(hdev);
4038}
4039
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004040static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
4041 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01004042{
4043 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
4044 struct oob_data *data;
4045
4046 BT_DBG("%s", hdev->name);
4047
4048 hci_dev_lock(hdev);
4049
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02004050 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Szymon Jance1ba1f12011-04-06 13:01:59 +02004051 goto unlock;
4052
Johan Hedberg6928a922014-10-26 20:46:09 +01004053 data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
Szymon Janc2763eda2011-03-22 13:12:22 +01004054 if (data) {
Johan Hedberg710f11c2014-05-26 11:21:22 +03004055 if (bredr_sc_enabled(hdev)) {
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004056 struct hci_cp_remote_oob_ext_data_reply cp;
Szymon Janc2763eda2011-03-22 13:12:22 +01004057
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004058 bacpy(&cp.bdaddr, &ev->bdaddr);
4059 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
Johan Hedberg38da1702014-11-17 20:52:20 +02004060 memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004061 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
Johan Hedberg38da1702014-11-17 20:52:20 +02004062 memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
Szymon Janc2763eda2011-03-22 13:12:22 +01004063
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004064 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
4065 sizeof(cp), &cp);
4066 } else {
4067 struct hci_cp_remote_oob_data_reply cp;
4068
4069 bacpy(&cp.bdaddr, &ev->bdaddr);
4070 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
Johan Hedberg38da1702014-11-17 20:52:20 +02004071 memcpy(cp.rand, data->rand192, sizeof(cp.rand));
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004072
4073 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
4074 sizeof(cp), &cp);
4075 }
Szymon Janc2763eda2011-03-22 13:12:22 +01004076 } else {
4077 struct hci_cp_remote_oob_data_neg_reply cp;
4078
4079 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08004080 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
4081 sizeof(cp), &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01004082 }
4083
Szymon Jance1ba1f12011-04-06 13:01:59 +02004084unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01004085 hci_dev_unlock(hdev);
4086}
4087
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004088static void hci_phy_link_complete_evt(struct hci_dev *hdev,
4089 struct sk_buff *skb)
4090{
4091 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
4092 struct hci_conn *hcon, *bredr_hcon;
4093
4094 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
4095 ev->status);
4096
4097 hci_dev_lock(hdev);
4098
4099 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4100 if (!hcon) {
4101 hci_dev_unlock(hdev);
4102 return;
4103 }
4104
4105 if (ev->status) {
4106 hci_conn_del(hcon);
4107 hci_dev_unlock(hdev);
4108 return;
4109 }
4110
4111 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
4112
4113 hcon->state = BT_CONNECTED;
4114 bacpy(&hcon->dst, &bredr_hcon->dst);
4115
4116 hci_conn_hold(hcon);
4117 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004118 hci_conn_drop(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004119
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004120 hci_conn_add_sysfs(hcon);
4121
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +02004122 amp_physical_cfm(bredr_hcon, hcon);
4123
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004124 hci_dev_unlock(hdev);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004125}
4126
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004127static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
4128{
4129 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
4130 struct hci_conn *hcon;
4131 struct hci_chan *hchan;
4132 struct amp_mgr *mgr;
4133
4134 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
4135 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
4136 ev->status);
4137
4138 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4139 if (!hcon)
4140 return;
4141
4142 /* Create AMP hchan */
4143 hchan = hci_chan_create(hcon);
4144 if (!hchan)
4145 return;
4146
4147 hchan->handle = le16_to_cpu(ev->handle);
4148
4149 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
4150
4151 mgr = hcon->amp_mgr;
4152 if (mgr && mgr->bredr_chan) {
4153 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
4154
4155 l2cap_chan_lock(bredr_chan);
4156
4157 bredr_chan->conn->mtu = hdev->block_mtu;
4158 l2cap_logical_cfm(bredr_chan, hchan, 0);
4159 hci_conn_hold(hcon);
4160
4161 l2cap_chan_unlock(bredr_chan);
4162 }
4163}
4164
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02004165static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
4166 struct sk_buff *skb)
4167{
4168 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
4169 struct hci_chan *hchan;
4170
4171 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
4172 le16_to_cpu(ev->handle), ev->status);
4173
4174 if (ev->status)
4175 return;
4176
4177 hci_dev_lock(hdev);
4178
4179 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
4180 if (!hchan)
4181 goto unlock;
4182
4183 amp_destroy_logical_link(hchan, ev->reason);
4184
4185unlock:
4186 hci_dev_unlock(hdev);
4187}
4188
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02004189static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
4190 struct sk_buff *skb)
4191{
4192 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
4193 struct hci_conn *hcon;
4194
4195 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4196
4197 if (ev->status)
4198 return;
4199
4200 hci_dev_lock(hdev);
4201
4202 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4203 if (hcon) {
4204 hcon->state = BT_CLOSED;
4205 hci_conn_del(hcon);
4206 }
4207
4208 hci_dev_unlock(hdev);
4209}
4210
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004211static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03004212{
4213 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
Johan Hedberg912b42e2014-07-03 19:33:49 +03004214 struct hci_conn_params *params;
Ville Tervofcd89c02011-02-10 22:38:47 -03004215 struct hci_conn *conn;
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02004216 struct smp_irk *irk;
Johan Hedberg837d5022014-07-02 09:36:22 +03004217 u8 addr_type;
Ville Tervofcd89c02011-02-10 22:38:47 -03004218
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004219 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Ville Tervofcd89c02011-02-10 22:38:47 -03004220
4221 hci_dev_lock(hdev);
4222
Johan Hedbergfbd96c12014-07-08 17:21:51 +03004223 /* All controllers implicitly stop advertising in the event of a
4224 * connection, so ensure that the state bit is cleared.
4225 */
4226 clear_bit(HCI_LE_ADV, &hdev->dev_flags);
4227
Andre Guedesb47a09b2012-07-27 15:10:15 -03004228 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
Ville Tervob62f3282011-02-10 22:38:50 -03004229 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03004230 conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr, ev->role);
Ville Tervob62f3282011-02-10 22:38:50 -03004231 if (!conn) {
4232 BT_ERR("No memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03004233 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03004234 }
Andre Guedes29b79882011-05-31 14:20:54 -03004235
4236 conn->dst_type = ev->bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03004237
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02004238 /* If we didn't have a hci_conn object previously
4239 * but we're in master role this must be something
4240 * initiated using a white list. Since white list based
4241 * connections are not "first class citizens" we don't
4242 * have full tracking of them. Therefore, we go ahead
4243 * with a "best effort" approach of determining the
4244 * initiator address based on the HCI_PRIVACY flag.
4245 */
4246 if (conn->out) {
4247 conn->resp_addr_type = ev->bdaddr_type;
4248 bacpy(&conn->resp_addr, &ev->bdaddr);
4249 if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) {
4250 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
4251 bacpy(&conn->init_addr, &hdev->rpa);
4252 } else {
4253 hci_copy_identity_address(hdev,
4254 &conn->init_addr,
4255 &conn->init_addr_type);
4256 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02004257 }
Johan Hedberg9489eca2014-02-28 17:45:46 +02004258 } else {
4259 cancel_delayed_work(&conn->le_conn_timeout);
Ville Tervob62f3282011-02-10 22:38:50 -03004260 }
Ville Tervofcd89c02011-02-10 22:38:47 -03004261
Johan Hedberg80c24ab2014-03-24 20:21:51 +02004262 if (!conn->out) {
4263 /* Set the responder (our side) address type based on
4264 * the advertising address type.
4265 */
4266 conn->resp_addr_type = hdev->adv_addr_type;
4267 if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM)
4268 bacpy(&conn->resp_addr, &hdev->random_addr);
4269 else
4270 bacpy(&conn->resp_addr, &hdev->bdaddr);
4271
4272 conn->init_addr_type = ev->bdaddr_type;
4273 bacpy(&conn->init_addr, &ev->bdaddr);
Marcel Holtmanna720d732014-06-23 12:14:51 +02004274
4275 /* For incoming connections, set the default minimum
4276 * and maximum connection interval. They will be used
4277 * to check if the parameters are in range and if not
4278 * trigger the connection update procedure.
4279 */
4280 conn->le_conn_min_interval = hdev->le_conn_min_interval;
4281 conn->le_conn_max_interval = hdev->le_conn_max_interval;
Johan Hedberg80c24ab2014-03-24 20:21:51 +02004282 }
Johan Hedberg7be2edb2014-02-23 19:42:17 +02004283
Marcel Holtmannedb4b462014-02-18 15:13:43 -08004284 /* Lookup the identity address from the stored connection
4285 * address and address type.
4286 *
4287 * When establishing connections to an identity address, the
4288 * connection procedure will store the resolvable random
4289 * address first. Now if it can be converted back into the
4290 * identity address, start using the identity address from
4291 * now on.
4292 */
4293 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02004294 if (irk) {
4295 bacpy(&conn->dst, &irk->bdaddr);
4296 conn->dst_type = irk->addr_type;
4297 }
4298
Johan Hedberg2d3c2262014-07-15 11:51:28 +03004299 if (ev->status) {
4300 hci_le_conn_failed(conn, ev->status);
Johan Hedberg837d5022014-07-02 09:36:22 +03004301 goto unlock;
4302 }
4303
Johan Hedberg08853f12014-08-15 21:06:55 +03004304 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
4305 addr_type = BDADDR_LE_PUBLIC;
4306 else
4307 addr_type = BDADDR_LE_RANDOM;
4308
Johan Hedberg2d3c2262014-07-15 11:51:28 +03004309 /* Drop the connection if the device is blocked */
4310 if (hci_bdaddr_list_lookup(&hdev->blacklist, &conn->dst, addr_type)) {
4311 hci_conn_drop(conn);
Andre Guedescd17dec2012-07-27 15:10:16 -03004312 goto unlock;
4313 }
4314
Johan Hedbergb644ba32012-01-17 21:48:47 +02004315 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00004316 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03004317
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03004318 conn->sec_level = BT_SECURITY_LOW;
Ville Tervofcd89c02011-02-10 22:38:47 -03004319 conn->handle = __le16_to_cpu(ev->handle);
4320 conn->state = BT_CONNECTED;
4321
Marcel Holtmanne04fde62014-06-23 11:40:04 +02004322 conn->le_conn_interval = le16_to_cpu(ev->interval);
4323 conn->le_conn_latency = le16_to_cpu(ev->latency);
4324 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
4325
Ville Tervofcd89c02011-02-10 22:38:47 -03004326 hci_conn_add_sysfs(conn);
4327
4328 hci_proto_connect_cfm(conn, ev->status);
4329
Johan Hedberg54776102014-08-15 21:06:56 +03004330 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
4331 conn->dst_type);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004332 if (params) {
Johan Hedberg95305ba2014-07-04 12:37:21 +03004333 list_del_init(&params->action);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004334 if (params->conn) {
4335 hci_conn_drop(params->conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004336 hci_conn_put(params->conn);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004337 params->conn = NULL;
4338 }
4339 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004340
Ville Tervofcd89c02011-02-10 22:38:47 -03004341unlock:
Johan Hedberg223683a2014-07-06 15:44:23 +03004342 hci_update_background_scan(hdev);
Ville Tervofcd89c02011-02-10 22:38:47 -03004343 hci_dev_unlock(hdev);
4344}
4345
Marcel Holtmann1855d922014-06-23 11:40:05 +02004346static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
4347 struct sk_buff *skb)
4348{
4349 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
4350 struct hci_conn *conn;
4351
4352 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4353
4354 if (ev->status)
4355 return;
4356
4357 hci_dev_lock(hdev);
4358
4359 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4360 if (conn) {
4361 conn->le_conn_interval = le16_to_cpu(ev->interval);
4362 conn->le_conn_latency = le16_to_cpu(ev->latency);
4363 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
4364 }
4365
4366 hci_dev_unlock(hdev);
4367}
4368
Andre Guedesa4790db2014-02-26 20:21:47 -03004369/* This function requires the caller holds hdev->lock */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004370static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
4371 bdaddr_t *addr,
4372 u8 addr_type, u8 adv_type)
Andre Guedesa4790db2014-02-26 20:21:47 -03004373{
4374 struct hci_conn *conn;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004375 struct hci_conn_params *params;
Andre Guedesa4790db2014-02-26 20:21:47 -03004376
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004377 /* If the event is not connectable don't proceed further */
4378 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004379 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004380
4381 /* Ignore if the device is blocked */
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004382 if (hci_bdaddr_list_lookup(&hdev->blacklist, addr, addr_type))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004383 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004384
Johan Hedbergf99353c2014-07-16 11:56:09 +03004385 /* Most controller will fail if we try to create new connections
4386 * while we have an existing one in slave role.
4387 */
4388 if (hdev->conn_hash.le_num_slave > 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004389 return NULL;
Johan Hedbergf99353c2014-07-16 11:56:09 +03004390
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004391 /* If we're not connectable only connect devices that we have in
4392 * our pend_le_conns list.
4393 */
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004394 params = hci_pend_le_action_lookup(&hdev->pend_le_conns,
4395 addr, addr_type);
4396 if (!params)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004397 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03004398
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004399 switch (params->auto_connect) {
4400 case HCI_AUTO_CONN_DIRECT:
4401 /* Only devices advertising with ADV_DIRECT_IND are
4402 * triggering a connection attempt. This is allowing
4403 * incoming connections from slave devices.
4404 */
4405 if (adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004406 return NULL;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004407 break;
4408 case HCI_AUTO_CONN_ALWAYS:
4409 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
4410 * are triggering a connection attempt. This means
4411 * that incoming connectioms from slave device are
4412 * accepted and also outgoing connections to slave
4413 * devices are established when found.
4414 */
4415 break;
4416 default:
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004417 return NULL;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004418 }
4419
Andre Guedesa4790db2014-02-26 20:21:47 -03004420 conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
Johan Hedberge804d252014-07-16 11:42:28 +03004421 HCI_LE_AUTOCONN_TIMEOUT, HCI_ROLE_MASTER);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004422 if (!IS_ERR(conn)) {
4423 /* Store the pointer since we don't really have any
4424 * other owner of the object besides the params that
4425 * triggered it. This way we can abort the connection if
4426 * the parameters get removed and keep the reference
4427 * count consistent once the connection is established.
4428 */
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004429 params->conn = hci_conn_get(conn);
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004430 return conn;
Johan Hedbergf161dd42014-08-15 21:06:54 +03004431 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004432
4433 switch (PTR_ERR(conn)) {
4434 case -EBUSY:
4435 /* If hci_connect() returns -EBUSY it means there is already
4436 * an LE connection attempt going on. Since controllers don't
4437 * support more than one connection attempt at the time, we
4438 * don't consider this an error case.
4439 */
4440 break;
4441 default:
4442 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004443 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03004444 }
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004445
4446 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03004447}
4448
Johan Hedberg4af605d2014-03-24 10:48:00 +02004449static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
Marcel Holtmann2f010b52014-12-05 16:20:13 +01004450 u8 bdaddr_type, bdaddr_t *direct_addr,
4451 u8 direct_addr_type, s8 rssi, u8 *data, u8 len)
Johan Hedberg4af605d2014-03-24 10:48:00 +02004452{
Johan Hedbergb9a63282014-03-25 10:51:52 +02004453 struct discovery_state *d = &hdev->discovery;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004454 struct smp_irk *irk;
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004455 struct hci_conn *conn;
Johan Hedberg474ee062014-03-25 14:34:59 +02004456 bool match;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004457 u32 flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02004458
Marcel Holtmann2f010b52014-12-05 16:20:13 +01004459 /* If the direct address is present, then this report is from
4460 * a LE Direct Advertising Report event. In that case it is
4461 * important to see if the address is matching the local
4462 * controller address.
4463 */
4464 if (direct_addr) {
4465 /* Only resolvable random addresses are valid for these
4466 * kind of reports and others can be ignored.
4467 */
4468 if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
4469 return;
4470
4471 /* If the controller is not using resolvable random
4472 * addresses, then this report can be ignored.
4473 */
4474 if (!test_bit(HCI_PRIVACY, &hdev->dev_flags))
4475 return;
4476
4477 /* If the local IRK of the controller does not match
4478 * with the resolvable random address provided, then
4479 * this report can be ignored.
4480 */
4481 if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
4482 return;
4483 }
4484
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004485 /* Check if we need to convert to identity address */
4486 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
4487 if (irk) {
4488 bdaddr = &irk->bdaddr;
4489 bdaddr_type = irk->addr_type;
4490 }
4491
4492 /* Check if we have been requested to connect to this device */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004493 conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, type);
4494 if (conn && type == LE_ADV_IND) {
4495 /* Store report for later inclusion by
4496 * mgmt_device_connected
4497 */
4498 memcpy(conn->le_adv_data, data, len);
4499 conn->le_adv_data_len = len;
4500 }
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004501
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004502 /* Passive scanning shouldn't trigger any device found events,
4503 * except for devices marked as CONN_REPORT for which we do send
4504 * device found events.
4505 */
Johan Hedbergca5c4be2014-03-25 10:30:46 +02004506 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004507 if (type == LE_ADV_DIRECT_IND)
4508 return;
4509
Johan Hedberg3a19b6f2014-07-15 08:07:59 +03004510 if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
4511 bdaddr, bdaddr_type))
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004512 return;
4513
4514 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
4515 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
4516 else
4517 flags = 0;
4518 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
4519 rssi, flags, data, len, NULL, 0);
Johan Hedberg97bf2e92014-07-04 12:37:16 +03004520 return;
Johan Hedbergca5c4be2014-03-25 10:30:46 +02004521 }
Johan Hedberg4af605d2014-03-24 10:48:00 +02004522
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004523 /* When receiving non-connectable or scannable undirected
4524 * advertising reports, this means that the remote device is
4525 * not connectable and then clearly indicate this in the
4526 * device found event.
4527 *
4528 * When receiving a scan response, then there is no way to
4529 * know if the remote device is connectable or not. However
4530 * since scan responses are merged with a previously seen
4531 * advertising report, the flags field from that report
4532 * will be used.
4533 *
4534 * In the really unlikely case that a controller get confused
4535 * and just sends a scan response event, then it is marked as
4536 * not connectable as well.
4537 */
4538 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
4539 type == LE_ADV_SCAN_RSP)
4540 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
4541 else
4542 flags = 0;
4543
Johan Hedbergb9a63282014-03-25 10:51:52 +02004544 /* If there's nothing pending either store the data from this
4545 * event or send an immediate device found event if the data
4546 * should not be stored for later.
4547 */
4548 if (!has_pending_adv_report(hdev)) {
4549 /* If the report will trigger a SCAN_REQ store it for
4550 * later merging.
4551 */
4552 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4553 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004554 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004555 return;
4556 }
4557
4558 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004559 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004560 return;
4561 }
4562
Johan Hedberg474ee062014-03-25 14:34:59 +02004563 /* Check if the pending report is for the same device as the new one */
4564 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
4565 bdaddr_type == d->last_adv_addr_type);
4566
Johan Hedbergb9a63282014-03-25 10:51:52 +02004567 /* If the pending data doesn't match this report or this isn't a
4568 * scan response (e.g. we got a duplicate ADV_IND) then force
4569 * sending of the pending data.
4570 */
Johan Hedberg474ee062014-03-25 14:34:59 +02004571 if (type != LE_ADV_SCAN_RSP || !match) {
4572 /* Send out whatever is in the cache, but skip duplicates */
4573 if (!match)
4574 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004575 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004576 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004577 d->last_adv_data,
Johan Hedberg474ee062014-03-25 14:34:59 +02004578 d->last_adv_data_len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004579
4580 /* If the new report will trigger a SCAN_REQ store it for
4581 * later merging.
4582 */
4583 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4584 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004585 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004586 return;
4587 }
4588
4589 /* The advertising reports cannot be merged, so clear
4590 * the pending report and send out a device found event.
4591 */
4592 clear_pending_adv_report(hdev);
Johan Hedberg5c5b93e2014-03-29 08:39:53 +02004593 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004594 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004595 return;
4596 }
4597
4598 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
4599 * the new event is a SCAN_RSP. We can therefore proceed with
4600 * sending a merged device found event.
4601 */
4602 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004603 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
Marcel Holtmann42bd6a52014-07-01 14:11:19 +02004604 d->last_adv_data, d->last_adv_data_len, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004605 clear_pending_adv_report(hdev);
Johan Hedberg4af605d2014-03-24 10:48:00 +02004606}
4607
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004608static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03004609{
Andre Guedese95beb42011-09-26 20:48:35 -03004610 u8 num_reports = skb->data[0];
4611 void *ptr = &skb->data[1];
Andre Guedes9aa04c92011-05-26 16:23:51 -03004612
Andre Guedesa4790db2014-02-26 20:21:47 -03004613 hci_dev_lock(hdev);
4614
Andre Guedese95beb42011-09-26 20:48:35 -03004615 while (num_reports--) {
4616 struct hci_ev_le_advertising_info *ev = ptr;
Johan Hedberg4af605d2014-03-24 10:48:00 +02004617 s8 rssi;
Andre Guedesa4790db2014-02-26 20:21:47 -03004618
Andre Guedes3c9e9192012-01-10 18:20:50 -03004619 rssi = ev->data[ev->length];
Johan Hedberg4af605d2014-03-24 10:48:00 +02004620 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
Marcel Holtmann2f010b52014-12-05 16:20:13 +01004621 ev->bdaddr_type, NULL, 0, rssi,
4622 ev->data, ev->length);
Andre Guedes3c9e9192012-01-10 18:20:50 -03004623
Andre Guedese95beb42011-09-26 20:48:35 -03004624 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03004625 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004626
4627 hci_dev_unlock(hdev);
Andre Guedes9aa04c92011-05-26 16:23:51 -03004628}
4629
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004630static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004631{
4632 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
4633 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004634 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004635 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004636 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004637
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004638 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004639
4640 hci_dev_lock(hdev);
4641
4642 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004643 if (conn == NULL)
4644 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004645
Johan Hedbergf3a73d92014-05-29 15:02:59 +03004646 ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
Johan Hedberg5378bc52014-05-29 14:00:39 +03004647 if (!ltk)
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004648 goto not_found;
4649
Johan Hedberg5378bc52014-05-29 14:00:39 +03004650 if (smp_ltk_is_sc(ltk)) {
4651 /* With SC both EDiv and Rand are set to zero */
4652 if (ev->ediv || ev->rand)
4653 goto not_found;
4654 } else {
4655 /* For non-SC keys check that EDiv and Rand match */
4656 if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
4657 goto not_found;
4658 }
4659
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004660 memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004661 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004662
Johan Hedberga6f78332014-09-10 17:37:45 -07004663 conn->pending_sec_level = smp_ltk_sec_level(ltk);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004664
Andre Guedes89cbb4d2013-07-31 16:25:29 -03004665 conn->enc_key_size = ltk->enc_size;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004666
4667 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
4668
Claudio Takahasi5981a882013-07-25 16:34:24 -03004669 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
4670 * temporary key used to encrypt a connection following
4671 * pairing. It is used during the Encrypted Session Setup to
4672 * distribute the keys. Later, security can be re-established
4673 * using a distributed LTK.
4674 */
Johan Hedberg2ceba532014-06-16 19:25:16 +03004675 if (ltk->type == SMP_STK) {
Johan Hedbergfe59a052014-07-01 19:14:12 +03004676 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Johan Hedberg970d0f12014-11-13 14:37:47 +02004677 list_del_rcu(&ltk->list);
4678 kfree_rcu(ltk, rcu);
Johan Hedbergfe59a052014-07-01 19:14:12 +03004679 } else {
4680 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004681 }
4682
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004683 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004684
4685 return;
4686
4687not_found:
4688 neg.handle = ev->handle;
4689 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
4690 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004691}
4692
Andre Guedes8e75b462014-07-01 18:10:08 -03004693static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
4694 u8 reason)
4695{
4696 struct hci_cp_le_conn_param_req_neg_reply cp;
4697
4698 cp.handle = cpu_to_le16(handle);
4699 cp.reason = reason;
4700
4701 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
4702 &cp);
4703}
4704
4705static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
4706 struct sk_buff *skb)
4707{
4708 struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
4709 struct hci_cp_le_conn_param_req_reply cp;
4710 struct hci_conn *hcon;
4711 u16 handle, min, max, latency, timeout;
4712
4713 handle = le16_to_cpu(ev->handle);
4714 min = le16_to_cpu(ev->interval_min);
4715 max = le16_to_cpu(ev->interval_max);
4716 latency = le16_to_cpu(ev->latency);
4717 timeout = le16_to_cpu(ev->timeout);
4718
4719 hcon = hci_conn_hash_lookup_handle(hdev, handle);
4720 if (!hcon || hcon->state != BT_CONNECTED)
4721 return send_conn_param_neg_reply(hdev, handle,
4722 HCI_ERROR_UNKNOWN_CONN_ID);
4723
4724 if (hci_check_conn_params(min, max, latency, timeout))
4725 return send_conn_param_neg_reply(hdev, handle,
4726 HCI_ERROR_INVALID_LL_PARAMS);
4727
Johan Hedberg40bef302014-07-16 11:42:27 +03004728 if (hcon->role == HCI_ROLE_MASTER) {
Johan Hedberg348d50b2014-07-02 17:37:30 +03004729 struct hci_conn_params *params;
Johan Hedbergf4869e22014-07-02 17:37:32 +03004730 u8 store_hint;
Johan Hedberg348d50b2014-07-02 17:37:30 +03004731
4732 hci_dev_lock(hdev);
4733
4734 params = hci_conn_params_lookup(hdev, &hcon->dst,
4735 hcon->dst_type);
4736 if (params) {
4737 params->conn_min_interval = min;
4738 params->conn_max_interval = max;
4739 params->conn_latency = latency;
4740 params->supervision_timeout = timeout;
Johan Hedbergf4869e22014-07-02 17:37:32 +03004741 store_hint = 0x01;
4742 } else{
4743 store_hint = 0x00;
Johan Hedberg348d50b2014-07-02 17:37:30 +03004744 }
4745
4746 hci_dev_unlock(hdev);
4747
Johan Hedbergf4869e22014-07-02 17:37:32 +03004748 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
4749 store_hint, min, max, latency, timeout);
Johan Hedberg348d50b2014-07-02 17:37:30 +03004750 }
Andre Guedesffb5a8272014-07-01 18:10:11 -03004751
Andre Guedes8e75b462014-07-01 18:10:08 -03004752 cp.handle = ev->handle;
4753 cp.interval_min = ev->interval_min;
4754 cp.interval_max = ev->interval_max;
4755 cp.latency = ev->latency;
4756 cp.timeout = ev->timeout;
4757 cp.min_ce_len = 0;
4758 cp.max_ce_len = 0;
4759
4760 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
4761}
4762
Marcel Holtmann2f010b52014-12-05 16:20:13 +01004763static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
4764 struct sk_buff *skb)
4765{
4766 u8 num_reports = skb->data[0];
4767 void *ptr = &skb->data[1];
4768
4769 hci_dev_lock(hdev);
4770
4771 while (num_reports--) {
4772 struct hci_ev_le_direct_adv_info *ev = ptr;
4773
4774 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
4775 ev->bdaddr_type, &ev->direct_addr,
4776 ev->direct_addr_type, ev->rssi, NULL, 0);
4777
4778 ptr += sizeof(*ev);
4779 }
4780
4781 hci_dev_unlock(hdev);
4782}
4783
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004784static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03004785{
4786 struct hci_ev_le_meta *le_ev = (void *) skb->data;
4787
4788 skb_pull(skb, sizeof(*le_ev));
4789
4790 switch (le_ev->subevent) {
4791 case HCI_EV_LE_CONN_COMPLETE:
4792 hci_le_conn_complete_evt(hdev, skb);
4793 break;
4794
Marcel Holtmann1855d922014-06-23 11:40:05 +02004795 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
4796 hci_le_conn_update_complete_evt(hdev, skb);
4797 break;
4798
Andre Guedes9aa04c92011-05-26 16:23:51 -03004799 case HCI_EV_LE_ADVERTISING_REPORT:
4800 hci_le_adv_report_evt(hdev, skb);
4801 break;
4802
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004803 case HCI_EV_LE_LTK_REQ:
4804 hci_le_ltk_request_evt(hdev, skb);
4805 break;
4806
Andre Guedes8e75b462014-07-01 18:10:08 -03004807 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
4808 hci_le_remote_conn_param_req_evt(hdev, skb);
4809 break;
4810
Marcel Holtmann2f010b52014-12-05 16:20:13 +01004811 case HCI_EV_LE_DIRECT_ADV_REPORT:
4812 hci_le_direct_adv_report_evt(hdev, skb);
4813 break;
4814
Ville Tervofcd89c02011-02-10 22:38:47 -03004815 default:
4816 break;
4817 }
4818}
4819
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +03004820static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
4821{
4822 struct hci_ev_channel_selected *ev = (void *) skb->data;
4823 struct hci_conn *hcon;
4824
4825 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
4826
4827 skb_pull(skb, sizeof(*ev));
4828
4829 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4830 if (!hcon)
4831 return;
4832
4833 amp_read_loc_assoc_final_data(hdev, hcon);
4834}
4835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
4837{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004838 struct hci_event_hdr *hdr = (void *) skb->data;
4839 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840
Johan Hedbergb6ddb632013-04-02 13:34:31 +03004841 hci_dev_lock(hdev);
4842
4843 /* Received events are (currently) only needed when a request is
4844 * ongoing so avoid unnecessary memory allocation.
4845 */
Marcel Holtmann899de762014-07-11 05:51:58 +02004846 if (hci_req_pending(hdev)) {
Johan Hedbergb6ddb632013-04-02 13:34:31 +03004847 kfree_skb(hdev->recv_evt);
4848 hdev->recv_evt = skb_clone(skb, GFP_KERNEL);
4849 }
4850
4851 hci_dev_unlock(hdev);
4852
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 skb_pull(skb, HCI_EVENT_HDR_SIZE);
4854
Johan Hedberg02350a72013-04-03 21:50:29 +03004855 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02004856 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
4857 u16 opcode = __le16_to_cpu(cmd_hdr->opcode);
Johan Hedberg02350a72013-04-03 21:50:29 +03004858
4859 hci_req_cmd_complete(hdev, opcode, 0);
4860 }
4861
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004862 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 case HCI_EV_INQUIRY_COMPLETE:
4864 hci_inquiry_complete_evt(hdev, skb);
4865 break;
4866
4867 case HCI_EV_INQUIRY_RESULT:
4868 hci_inquiry_result_evt(hdev, skb);
4869 break;
4870
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004871 case HCI_EV_CONN_COMPLETE:
4872 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02004873 break;
4874
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 case HCI_EV_CONN_REQUEST:
4876 hci_conn_request_evt(hdev, skb);
4877 break;
4878
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 case HCI_EV_DISCONN_COMPLETE:
4880 hci_disconn_complete_evt(hdev, skb);
4881 break;
4882
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 case HCI_EV_AUTH_COMPLETE:
4884 hci_auth_complete_evt(hdev, skb);
4885 break;
4886
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004887 case HCI_EV_REMOTE_NAME:
4888 hci_remote_name_evt(hdev, skb);
4889 break;
4890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 case HCI_EV_ENCRYPT_CHANGE:
4892 hci_encrypt_change_evt(hdev, skb);
4893 break;
4894
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004895 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
4896 hci_change_link_key_complete_evt(hdev, skb);
4897 break;
4898
4899 case HCI_EV_REMOTE_FEATURES:
4900 hci_remote_features_evt(hdev, skb);
4901 break;
4902
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004903 case HCI_EV_CMD_COMPLETE:
4904 hci_cmd_complete_evt(hdev, skb);
4905 break;
4906
4907 case HCI_EV_CMD_STATUS:
4908 hci_cmd_status_evt(hdev, skb);
4909 break;
4910
Marcel Holtmann24dfa342014-11-02 02:56:41 +01004911 case HCI_EV_HARDWARE_ERROR:
4912 hci_hardware_error_evt(hdev, skb);
4913 break;
4914
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004915 case HCI_EV_ROLE_CHANGE:
4916 hci_role_change_evt(hdev, skb);
4917 break;
4918
4919 case HCI_EV_NUM_COMP_PKTS:
4920 hci_num_comp_pkts_evt(hdev, skb);
4921 break;
4922
4923 case HCI_EV_MODE_CHANGE:
4924 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 break;
4926
4927 case HCI_EV_PIN_CODE_REQ:
4928 hci_pin_code_request_evt(hdev, skb);
4929 break;
4930
4931 case HCI_EV_LINK_KEY_REQ:
4932 hci_link_key_request_evt(hdev, skb);
4933 break;
4934
4935 case HCI_EV_LINK_KEY_NOTIFY:
4936 hci_link_key_notify_evt(hdev, skb);
4937 break;
4938
4939 case HCI_EV_CLOCK_OFFSET:
4940 hci_clock_offset_evt(hdev, skb);
4941 break;
4942
Marcel Holtmanna8746412008-07-14 20:13:46 +02004943 case HCI_EV_PKT_TYPE_CHANGE:
4944 hci_pkt_type_change_evt(hdev, skb);
4945 break;
4946
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004947 case HCI_EV_PSCAN_REP_MODE:
4948 hci_pscan_rep_mode_evt(hdev, skb);
4949 break;
4950
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004951 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
4952 hci_inquiry_result_with_rssi_evt(hdev, skb);
4953 break;
4954
4955 case HCI_EV_REMOTE_EXT_FEATURES:
4956 hci_remote_ext_features_evt(hdev, skb);
4957 break;
4958
4959 case HCI_EV_SYNC_CONN_COMPLETE:
4960 hci_sync_conn_complete_evt(hdev, skb);
4961 break;
4962
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004963 case HCI_EV_EXTENDED_INQUIRY_RESULT:
4964 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965 break;
4966
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004967 case HCI_EV_KEY_REFRESH_COMPLETE:
4968 hci_key_refresh_complete_evt(hdev, skb);
4969 break;
4970
Marcel Holtmann04936842008-07-14 20:13:48 +02004971 case HCI_EV_IO_CAPA_REQUEST:
4972 hci_io_capa_request_evt(hdev, skb);
4973 break;
4974
Johan Hedberg03b555e2011-01-04 15:40:05 +02004975 case HCI_EV_IO_CAPA_REPLY:
4976 hci_io_capa_reply_evt(hdev, skb);
4977 break;
4978
Johan Hedberga5c29682011-02-19 12:05:57 -03004979 case HCI_EV_USER_CONFIRM_REQUEST:
4980 hci_user_confirm_request_evt(hdev, skb);
4981 break;
4982
Brian Gix1143d452011-11-23 08:28:34 -08004983 case HCI_EV_USER_PASSKEY_REQUEST:
4984 hci_user_passkey_request_evt(hdev, skb);
4985 break;
4986
Johan Hedberg92a25252012-09-06 18:39:26 +03004987 case HCI_EV_USER_PASSKEY_NOTIFY:
4988 hci_user_passkey_notify_evt(hdev, skb);
4989 break;
4990
4991 case HCI_EV_KEYPRESS_NOTIFY:
4992 hci_keypress_notify_evt(hdev, skb);
4993 break;
4994
Marcel Holtmann04936842008-07-14 20:13:48 +02004995 case HCI_EV_SIMPLE_PAIR_COMPLETE:
4996 hci_simple_pair_complete_evt(hdev, skb);
4997 break;
4998
Marcel Holtmann41a96212008-07-14 20:13:48 +02004999 case HCI_EV_REMOTE_HOST_FEATURES:
5000 hci_remote_host_features_evt(hdev, skb);
5001 break;
5002
Ville Tervofcd89c02011-02-10 22:38:47 -03005003 case HCI_EV_LE_META:
5004 hci_le_meta_evt(hdev, skb);
5005 break;
5006
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +03005007 case HCI_EV_CHANNEL_SELECTED:
5008 hci_chan_selected_evt(hdev, skb);
5009 break;
5010
Szymon Janc2763eda2011-03-22 13:12:22 +01005011 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
5012 hci_remote_oob_data_request_evt(hdev, skb);
5013 break;
5014
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03005015 case HCI_EV_PHY_LINK_COMPLETE:
5016 hci_phy_link_complete_evt(hdev, skb);
5017 break;
5018
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03005019 case HCI_EV_LOGICAL_LINK_COMPLETE:
5020 hci_loglink_complete_evt(hdev, skb);
5021 break;
5022
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02005023 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
5024 hci_disconn_loglink_complete_evt(hdev, skb);
5025 break;
5026
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02005027 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
5028 hci_disconn_phylink_complete_evt(hdev, skb);
5029 break;
5030
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02005031 case HCI_EV_NUM_COMP_BLOCKS:
5032 hci_num_comp_blocks_evt(hdev, skb);
5033 break;
5034
Marcel Holtmanna9de9242007-10-20 13:33:56 +02005035 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03005036 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037 break;
5038 }
5039
5040 kfree_skb(skb);
5041 hdev->stat.evt_rx++;
5042}