blob: aa152140c3e279bd6599b5b09498f748e501c08e [file] [log] [blame]
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +09001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 BlueZ - Bluetooth protocol stack for Linux
Ron Shaffer2d0a0342010-05-28 11:53:46 -04003 Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090015 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
YOSHIFUJI Hideaki8e87d142007-02-09 23:24:33 +090020 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI event handling. */
26
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/unaligned.h>
28
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
Mikel Astizf0d6a0e2012-08-09 09:52:30 +020031#include <net/bluetooth/mgmt.h>
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070032
Marcel Holtmann70247282013-10-10 14:54:15 -070033#include "a2mp.h"
Marcel Holtmann7ef9fbf2013-10-10 14:54:14 -070034#include "amp.h"
Johan Hedberg2ceba532014-06-16 19:25:16 +030035#include "smp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* Handle HCI Event packets */
38
Marcel Holtmanna9de9242007-10-20 13:33:56 +020039static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020041 __u8 status = *((__u8 *) skb->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030043 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Andre Guedes82f47852013-04-30 15:29:34 -030045 if (status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Andre Guedes89352e72011-11-04 14:16:53 -030048 clear_bit(HCI_INQUIRY, &hdev->flags);
Peter Zijlstra4e857c52014-03-17 18:06:10 +010049 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -030050 wake_up_bit(&hdev->flags, HCI_INQUIRY);
Andre Guedes89352e72011-11-04 14:16:53 -030051
Johan Hedberg50143a42014-06-10 14:05:57 +030052 hci_dev_lock(hdev);
53 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
54 hci_dev_unlock(hdev);
55
Marcel Holtmanna9de9242007-10-20 13:33:56 +020056 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
Andre Guedes4d934832012-03-21 00:03:35 -030059static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
60{
61 __u8 status = *((__u8 *) skb->data);
62
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030063 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesae854a72012-03-21 00:03:36 -030064
65 if (status)
66 return;
67
68 set_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
Andre Guedes4d934832012-03-21 00:03:35 -030069}
70
Marcel Holtmanna9de9242007-10-20 13:33:56 +020071static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Marcel Holtmanna9de9242007-10-20 13:33:56 +020073 __u8 status = *((__u8 *) skb->data);
74
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030075 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +020076
77 if (status)
78 return;
79
Andre Guedesae854a72012-03-21 00:03:36 -030080 clear_bit(HCI_PERIODIC_INQ, &hdev->dev_flags);
81
Marcel Holtmanna9de9242007-10-20 13:33:56 +020082 hci_conn_check_pending(hdev);
83}
84
Gustavo Padovan807deac2012-05-17 00:36:24 -030085static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
86 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +020087{
88 BT_DBG("%s", hdev->name);
89}
90
91static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
92{
93 struct hci_rp_role_discovery *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +030096 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Marcel Holtmanna9de9242007-10-20 13:33:56 +020098 if (rp->status)
99 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200101 hci_dev_lock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200103 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Johan Hedberg40bef302014-07-16 11:42:27 +0300104 if (conn)
105 conn->role = rp->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200106
107 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200110static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
111{
112 struct hci_rp_read_link_policy *rp = (void *) skb->data;
113 struct hci_conn *conn;
114
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300115 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200116
117 if (rp->status)
118 return;
119
120 hci_dev_lock(hdev);
121
122 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
123 if (conn)
124 conn->link_policy = __le16_to_cpu(rp->policy);
125
126 hci_dev_unlock(hdev);
127}
128
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200129static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200131 struct hci_rp_write_link_policy *rp = (void *) skb->data;
132 struct hci_conn *conn;
133 void *sent;
134
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300135 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200136
137 if (rp->status)
138 return;
139
140 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
141 if (!sent)
142 return;
143
144 hci_dev_lock(hdev);
145
146 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200147 if (conn)
Harvey Harrison83985312008-05-02 16:25:46 -0700148 conn->link_policy = get_unaligned_le16(sent + 2);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200149
150 hci_dev_unlock(hdev);
151}
152
Gustavo Padovan807deac2012-05-17 00:36:24 -0300153static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
154 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200155{
156 struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
157
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300158 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200159
160 if (rp->status)
161 return;
162
163 hdev->link_policy = __le16_to_cpu(rp->policy);
164}
165
Gustavo Padovan807deac2012-05-17 00:36:24 -0300166static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
167 struct sk_buff *skb)
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200168{
169 __u8 status = *((__u8 *) skb->data);
170 void *sent;
171
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300172 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200173
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200174 if (status)
175 return;
176
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200177 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
178 if (!sent)
179 return;
180
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200181 hdev->link_policy = get_unaligned_le16(sent);
Marcel Holtmanne4e8e372008-07-14 20:13:47 +0200182}
183
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200184static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
185{
186 __u8 status = *((__u8 *) skb->data);
187
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300188 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200189
Gustavo F. Padovan10572132011-03-16 15:36:29 -0300190 clear_bit(HCI_RESET, &hdev->flags);
191
Johan Hedberga297e972012-02-21 17:55:47 +0200192 /* Reset all non-persistent flags */
Johan Hedberg2cc6fb02013-03-15 17:06:57 -0500193 hdev->dev_flags &= ~HCI_PERSISTENT_MASK;
Andre Guedes69775ff2012-02-23 16:50:05 +0200194
195 hdev->discovery.state = DISCOVERY_STOPPED;
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100196 hdev->inq_tx_power = HCI_TX_POWER_INVALID;
197 hdev->adv_tx_power = HCI_TX_POWER_INVALID;
Johan Hedberg3f0f5242012-11-08 01:23:00 +0100198
199 memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
200 hdev->adv_data_len = 0;
Marcel Holtmannf8e808b2013-10-16 00:16:47 -0700201
202 memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
203 hdev->scan_rsp_data_len = 0;
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700204
Marcel Holtmann533553f2014-03-21 12:18:10 -0700205 hdev->le_scan_type = LE_SCAN_PASSIVE;
206
Marcel Holtmann06f5b772013-10-19 07:09:11 -0700207 hdev->ssp_debug_mode = 0;
Marcel Holtmanna4d55042014-10-29 23:37:53 +0100208
209 hci_bdaddr_list_clear(&hdev->le_white_list);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200210}
211
212static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
213{
214 __u8 status = *((__u8 *) skb->data);
215 void *sent;
216
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300217 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200218
219 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
220 if (!sent)
221 return;
222
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200223 hci_dev_lock(hdev);
224
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200225 if (test_bit(HCI_MGMT, &hdev->dev_flags))
226 mgmt_set_local_name_complete(hdev, sent, status);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +0200227 else if (!status)
228 memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
Johan Hedbergf51d5b22012-02-22 18:17:32 +0200229
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200230 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200231}
232
233static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
234{
235 struct hci_rp_read_local_name *rp = (void *) skb->data;
236
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300237 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200238
239 if (rp->status)
240 return;
241
Johan Hedbergdb99b5f2012-02-22 20:14:22 +0200242 if (test_bit(HCI_SETUP, &hdev->dev_flags))
243 memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200244}
245
246static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
247{
248 __u8 status = *((__u8 *) skb->data);
249 void *sent;
250
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300251 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200252
253 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
254 if (!sent)
255 return;
256
257 if (!status) {
258 __u8 param = *((__u8 *) sent);
259
260 if (param == AUTH_ENABLED)
261 set_bit(HCI_AUTH, &hdev->flags);
262 else
263 clear_bit(HCI_AUTH, &hdev->flags);
264 }
265
Johan Hedberg33ef95e2012-02-16 23:56:27 +0200266 if (test_bit(HCI_MGMT, &hdev->dev_flags))
267 mgmt_auth_enable_complete(hdev, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200268}
269
270static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
271{
272 __u8 status = *((__u8 *) skb->data);
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200273 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200274 void *sent;
275
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300276 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200277
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200278 if (status)
279 return;
280
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200281 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
282 if (!sent)
283 return;
284
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200285 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200286
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200287 if (param)
288 set_bit(HCI_ENCRYPT, &hdev->flags);
289 else
290 clear_bit(HCI_ENCRYPT, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200291}
292
293static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
294{
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200295 __u8 status = *((__u8 *) skb->data);
296 __u8 param;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200297 void *sent;
298
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300299 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200300
301 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
302 if (!sent)
303 return;
304
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200305 param = *((__u8 *) sent);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200306
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200307 hci_dev_lock(hdev);
308
Mikel Astizfa1bd912012-08-09 09:52:29 +0200309 if (status) {
Johan Hedberg2d7cee52011-11-07 22:16:03 +0200310 hdev->discov_timeout = 0;
311 goto done;
312 }
313
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300314 if (param & SCAN_INQUIRY)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200315 set_bit(HCI_ISCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300316 else
317 clear_bit(HCI_ISCAN, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200318
Johan Hedberg031547d2014-07-10 12:09:06 +0300319 if (param & SCAN_PAGE)
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200320 set_bit(HCI_PSCAN, &hdev->flags);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300321 else
Johan Hedberg204e3992014-07-28 15:45:31 +0300322 clear_bit(HCI_PSCAN, &hdev->flags);
Johan Hedberg36f7fc72011-11-04 00:17:45 +0200323
324done:
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200325 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200326}
327
328static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
329{
330 struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
331
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300332 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200333
334 if (rp->status)
335 return;
336
337 memcpy(hdev->dev_class, rp->dev_class, 3);
338
339 BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300340 hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200341}
342
343static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
344{
345 __u8 status = *((__u8 *) skb->data);
346 void *sent;
347
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300348 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200349
350 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
351 if (!sent)
352 return;
353
Marcel Holtmann7f9a9032012-02-22 18:38:01 +0100354 hci_dev_lock(hdev);
355
356 if (status == 0)
357 memcpy(hdev->dev_class, sent, 3);
358
359 if (test_bit(HCI_MGMT, &hdev->dev_flags))
360 mgmt_set_class_of_dev_complete(hdev, sent, status);
361
362 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200363}
364
365static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
366{
367 struct hci_rp_read_voice_setting *rp = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 __u16 setting;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200369
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300370 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200371
372 if (rp->status)
373 return;
374
375 setting = __le16_to_cpu(rp->voice_setting);
376
Marcel Holtmannf383f272008-07-14 20:13:47 +0200377 if (hdev->voice_setting == setting)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200378 return;
379
380 hdev->voice_setting = setting;
381
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300382 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200383
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200384 if (hdev->notify)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200385 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200386}
387
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300388static void hci_cc_write_voice_setting(struct hci_dev *hdev,
389 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200390{
391 __u8 status = *((__u8 *) skb->data);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200392 __u16 setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 void *sent;
394
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300395 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Marcel Holtmannf383f272008-07-14 20:13:47 +0200397 if (status)
398 return;
399
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200400 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
401 if (!sent)
402 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Marcel Holtmannf383f272008-07-14 20:13:47 +0200404 setting = get_unaligned_le16(sent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Marcel Holtmannf383f272008-07-14 20:13:47 +0200406 if (hdev->voice_setting == setting)
407 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Marcel Holtmannf383f272008-07-14 20:13:47 +0200409 hdev->voice_setting = setting;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300411 BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
Marcel Holtmannf383f272008-07-14 20:13:47 +0200412
Gustavo F. Padovan3c547112011-12-14 22:58:44 -0200413 if (hdev->notify)
Marcel Holtmannf383f272008-07-14 20:13:47 +0200414 hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415}
416
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -0700417static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
418 struct sk_buff *skb)
419{
420 struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
421
422 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
423
424 if (rp->status)
425 return;
426
427 hdev->num_iac = rp->num_iac;
428
429 BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
430}
431
Marcel Holtmann333140b2008-07-14 20:13:48 +0200432static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
433{
434 __u8 status = *((__u8 *) skb->data);
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300435 struct hci_cp_write_ssp_mode *sent;
Marcel Holtmann333140b2008-07-14 20:13:48 +0200436
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300437 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann333140b2008-07-14 20:13:48 +0200438
Marcel Holtmann333140b2008-07-14 20:13:48 +0200439 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
440 if (!sent)
441 return;
442
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300443 if (!status) {
444 if (sent->mode)
Johan Hedbergcad718e2013-04-17 15:00:51 +0300445 hdev->features[1][0] |= LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300446 else
Johan Hedbergcad718e2013-04-17 15:00:51 +0300447 hdev->features[1][0] &= ~LMP_HOST_SSP;
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300448 }
449
Johan Hedberged2c4ee2012-02-17 00:56:28 +0200450 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300451 mgmt_ssp_enable_complete(hdev, sent->mode, status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200452 else if (!status) {
Johan Hedberg5ed8eb22012-10-25 00:09:51 +0300453 if (sent->mode)
Johan Hedbergc0ecddc2012-02-22 12:38:31 +0200454 set_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
455 else
456 clear_bit(HCI_SSP_ENABLED, &hdev->dev_flags);
457 }
Marcel Holtmann333140b2008-07-14 20:13:48 +0200458}
459
Marcel Holtmanneac83dc2014-01-10 02:07:23 -0800460static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
461{
462 u8 status = *((u8 *) skb->data);
463 struct hci_cp_write_sc_support *sent;
464
465 BT_DBG("%s status 0x%2.2x", hdev->name, status);
466
467 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
468 if (!sent)
469 return;
470
471 if (!status) {
472 if (sent->support)
473 hdev->features[1][0] |= LMP_HOST_SC;
474 else
475 hdev->features[1][0] &= ~LMP_HOST_SC;
476 }
477
478 if (test_bit(HCI_MGMT, &hdev->dev_flags))
479 mgmt_sc_enable_complete(hdev, sent->support, status);
480 else if (!status) {
481 if (sent->support)
482 set_bit(HCI_SC_ENABLED, &hdev->dev_flags);
483 else
484 clear_bit(HCI_SC_ENABLED, &hdev->dev_flags);
485 }
486}
487
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200488static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
489{
490 struct hci_rp_read_local_version *rp = (void *) skb->data;
491
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300492 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200493
494 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200495 return;
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200496
Marcel Holtmann0d5551f2013-10-18 12:04:50 -0700497 if (test_bit(HCI_SETUP, &hdev->dev_flags)) {
498 hdev->hci_ver = rp->hci_ver;
499 hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
500 hdev->lmp_ver = rp->lmp_ver;
501 hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
502 hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
503 }
Johan Hedbergd5859e22011-01-25 01:19:58 +0200504}
505
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300506static void hci_cc_read_local_commands(struct hci_dev *hdev,
507 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200508{
509 struct hci_rp_read_local_commands *rp = (void *) skb->data;
510
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300511 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200512
Marcel Holtmann6a070e62013-10-31 04:54:33 -0700513 if (rp->status)
514 return;
515
516 if (test_bit(HCI_SETUP, &hdev->dev_flags))
Johan Hedberg2177bab2013-03-05 20:37:43 +0200517 memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200518}
519
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -0300520static void hci_cc_read_local_features(struct hci_dev *hdev,
521 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200522{
523 struct hci_rp_read_local_features *rp = (void *) skb->data;
524
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300525 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200526
527 if (rp->status)
528 return;
529
530 memcpy(hdev->features, rp->features, 8);
531
532 /* Adjust default settings according to features
533 * supported by device. */
534
Johan Hedbergcad718e2013-04-17 15:00:51 +0300535 if (hdev->features[0][0] & LMP_3SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200536 hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
537
Johan Hedbergcad718e2013-04-17 15:00:51 +0300538 if (hdev->features[0][0] & LMP_5SLOT)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200539 hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
540
Johan Hedbergcad718e2013-04-17 15:00:51 +0300541 if (hdev->features[0][1] & LMP_HV2) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200542 hdev->pkt_type |= (HCI_HV2);
543 hdev->esco_type |= (ESCO_HV2);
544 }
545
Johan Hedbergcad718e2013-04-17 15:00:51 +0300546 if (hdev->features[0][1] & LMP_HV3) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200547 hdev->pkt_type |= (HCI_HV3);
548 hdev->esco_type |= (ESCO_HV3);
549 }
550
Andre Guedes45db810f2012-07-24 15:03:49 -0300551 if (lmp_esco_capable(hdev))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200552 hdev->esco_type |= (ESCO_EV3);
553
Johan Hedbergcad718e2013-04-17 15:00:51 +0300554 if (hdev->features[0][4] & LMP_EV4)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200555 hdev->esco_type |= (ESCO_EV4);
556
Johan Hedbergcad718e2013-04-17 15:00:51 +0300557 if (hdev->features[0][4] & LMP_EV5)
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200558 hdev->esco_type |= (ESCO_EV5);
559
Johan Hedbergcad718e2013-04-17 15:00:51 +0300560 if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100561 hdev->esco_type |= (ESCO_2EV3);
562
Johan Hedbergcad718e2013-04-17 15:00:51 +0300563 if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100564 hdev->esco_type |= (ESCO_3EV3);
565
Johan Hedbergcad718e2013-04-17 15:00:51 +0300566 if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
Marcel Holtmannefc76882009-02-06 09:13:37 +0100567 hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200568}
569
Andre Guedes971e3a42011-06-30 19:20:52 -0300570static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300571 struct sk_buff *skb)
Andre Guedes971e3a42011-06-30 19:20:52 -0300572{
573 struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
574
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300575 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andre Guedes971e3a42011-06-30 19:20:52 -0300576
577 if (rp->status)
Johan Hedberg42c6b122013-03-05 20:37:49 +0200578 return;
Andre Guedes971e3a42011-06-30 19:20:52 -0300579
Marcel Holtmann57af75a2013-10-18 12:04:47 -0700580 if (hdev->max_page < rp->max_page)
581 hdev->max_page = rp->max_page;
Johan Hedbergd2c5d772013-04-17 15:00:52 +0300582
Johan Hedbergcad718e2013-04-17 15:00:51 +0300583 if (rp->page < HCI_MAX_PAGES)
584 memcpy(hdev->features[rp->page], rp->features, 8);
Andre Guedes971e3a42011-06-30 19:20:52 -0300585}
586
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200587static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300588 struct sk_buff *skb)
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200589{
590 struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
591
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300592 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200593
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200594 if (rp->status)
595 return;
596
597 hdev->flow_ctl_mode = rp->mode;
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +0200598}
599
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200600static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
601{
602 struct hci_rp_read_buffer_size *rp = (void *) skb->data;
603
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300604 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200605
606 if (rp->status)
607 return;
608
609 hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
610 hdev->sco_mtu = rp->sco_mtu;
611 hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
612 hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
613
614 if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
615 hdev->sco_mtu = 64;
616 hdev->sco_pkts = 8;
617 }
618
619 hdev->acl_cnt = hdev->acl_pkts;
620 hdev->sco_cnt = hdev->sco_pkts;
621
Gustavo Padovan807deac2012-05-17 00:36:24 -0300622 BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
623 hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200624}
625
626static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
627{
628 struct hci_rp_read_bd_addr *rp = (void *) skb->data;
629
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300630 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200631
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200632 if (rp->status)
633 return;
634
635 if (test_bit(HCI_INIT, &hdev->flags))
Marcel Holtmanna9de9242007-10-20 13:33:56 +0200636 bacpy(&hdev->bdaddr, &rp->bdaddr);
Marcel Holtmanne30d3f52014-07-05 10:48:03 +0200637
638 if (test_bit(HCI_SETUP, &hdev->dev_flags))
639 bacpy(&hdev->setup_addr, &rp->bdaddr);
Johan Hedberg23bb5762010-12-21 23:01:27 +0200640}
641
Johan Hedbergf332ec62013-03-15 17:07:11 -0500642static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
643 struct sk_buff *skb)
644{
645 struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
646
647 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
648
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200649 if (rp->status)
650 return;
651
652 if (test_bit(HCI_INIT, &hdev->flags)) {
Johan Hedbergf332ec62013-03-15 17:07:11 -0500653 hdev->page_scan_interval = __le16_to_cpu(rp->interval);
654 hdev->page_scan_window = __le16_to_cpu(rp->window);
655 }
656}
657
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500658static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
659 struct sk_buff *skb)
660{
661 u8 status = *((u8 *) skb->data);
662 struct hci_cp_write_page_scan_activity *sent;
663
664 BT_DBG("%s status 0x%2.2x", hdev->name, status);
665
666 if (status)
667 return;
668
669 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
670 if (!sent)
671 return;
672
673 hdev->page_scan_interval = __le16_to_cpu(sent->interval);
674 hdev->page_scan_window = __le16_to_cpu(sent->window);
675}
676
Johan Hedbergf332ec62013-03-15 17:07:11 -0500677static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
678 struct sk_buff *skb)
679{
680 struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
681
682 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
683
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200684 if (rp->status)
685 return;
686
687 if (test_bit(HCI_INIT, &hdev->flags))
Johan Hedbergf332ec62013-03-15 17:07:11 -0500688 hdev->page_scan_type = rp->type;
689}
690
Johan Hedberg4a3ee762013-03-15 17:07:12 -0500691static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
692 struct sk_buff *skb)
693{
694 u8 status = *((u8 *) skb->data);
695 u8 *type;
696
697 BT_DBG("%s status 0x%2.2x", hdev->name, status);
698
699 if (status)
700 return;
701
702 type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
703 if (type)
704 hdev->page_scan_type = *type;
705}
706
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200707static void hci_cc_read_data_block_size(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300708 struct sk_buff *skb)
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200709{
710 struct hci_rp_read_data_block_size *rp = (void *) skb->data;
711
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300712 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200713
714 if (rp->status)
715 return;
716
717 hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
718 hdev->block_len = __le16_to_cpu(rp->block_len);
719 hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
720
721 hdev->block_cnt = hdev->num_blocks;
722
723 BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300724 hdev->block_cnt, hdev->block_len);
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +0200725}
726
Johan Hedberg33f35722014-06-28 17:54:06 +0300727static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
728{
729 struct hci_rp_read_clock *rp = (void *) skb->data;
730 struct hci_cp_read_clock *cp;
731 struct hci_conn *conn;
732
733 BT_DBG("%s", hdev->name);
734
735 if (skb->len < sizeof(*rp))
736 return;
737
738 if (rp->status)
739 return;
740
741 hci_dev_lock(hdev);
742
743 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
744 if (!cp)
745 goto unlock;
746
747 if (cp->which == 0x00) {
748 hdev->clock = le32_to_cpu(rp->clock);
749 goto unlock;
750 }
751
752 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
753 if (conn) {
754 conn->clock = le32_to_cpu(rp->clock);
755 conn->clock_accuracy = le16_to_cpu(rp->accuracy);
756 }
757
758unlock:
759 hci_dev_unlock(hdev);
760}
761
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300762static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300763 struct sk_buff *skb)
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300764{
765 struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
766
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300767 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300768
769 if (rp->status)
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300770 goto a2mp_rsp;
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300771
772 hdev->amp_status = rp->amp_status;
773 hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
774 hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
775 hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
776 hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
777 hdev->amp_type = rp->amp_type;
778 hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
779 hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
780 hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
781 hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
782
Andrei Emeltchenko8e2a0d92012-09-27 17:26:08 +0300783a2mp_rsp:
784 a2mp_send_getinfo_rsp(hdev);
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +0300785}
786
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300787static void hci_cc_read_local_amp_assoc(struct hci_dev *hdev,
788 struct sk_buff *skb)
789{
790 struct hci_rp_read_local_amp_assoc *rp = (void *) skb->data;
791 struct amp_assoc *assoc = &hdev->loc_assoc;
792 size_t rem_len, frag_len;
793
794 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
795
796 if (rp->status)
797 goto a2mp_rsp;
798
799 frag_len = skb->len - sizeof(*rp);
800 rem_len = __le16_to_cpu(rp->rem_len);
801
802 if (rem_len > frag_len) {
Andrei Emeltchenko2e430be32012-09-28 14:44:23 +0300803 BT_DBG("frag_len %zu rem_len %zu", frag_len, rem_len);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300804
805 memcpy(assoc->data + assoc->offset, rp->frag, frag_len);
806 assoc->offset += frag_len;
807
808 /* Read other fragments */
809 amp_read_loc_assoc_frag(hdev, rp->phy_handle);
810
811 return;
812 }
813
814 memcpy(assoc->data + assoc->offset, rp->frag, rem_len);
815 assoc->len = assoc->offset + rem_len;
816 assoc->offset = 0;
817
818a2mp_rsp:
819 /* Send A2MP Rsp when all fragments are received */
820 a2mp_send_getampassoc_rsp(hdev, rp->status);
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +0300821 a2mp_send_create_phy_link_req(hdev, rp->status);
Andrei Emeltchenko903e4542012-09-27 17:26:09 +0300822}
823
Johan Hedbergd5859e22011-01-25 01:19:58 +0200824static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300825 struct sk_buff *skb)
Johan Hedbergd5859e22011-01-25 01:19:58 +0200826{
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700827 struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200828
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300829 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedbergd5859e22011-01-25 01:19:58 +0200830
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200831 if (rp->status)
832 return;
833
834 hdev->inq_tx_power = rp->tx_power;
Johan Hedbergd5859e22011-01-25 01:19:58 +0200835}
836
Johan Hedberg980e1a52011-01-22 06:10:07 +0200837static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
838{
839 struct hci_rp_pin_code_reply *rp = (void *) skb->data;
840 struct hci_cp_pin_code_reply *cp;
841 struct hci_conn *conn;
842
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300843 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200844
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200845 hci_dev_lock(hdev);
846
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200847 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200848 mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200849
Mikel Astizfa1bd912012-08-09 09:52:29 +0200850 if (rp->status)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200851 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200852
853 cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
854 if (!cp)
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200855 goto unlock;
Johan Hedberg980e1a52011-01-22 06:10:07 +0200856
857 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
858 if (conn)
859 conn->pin_length = cp->pin_len;
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200860
861unlock:
862 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200863}
864
865static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
866{
867 struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
868
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300869 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200870
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200871 hci_dev_lock(hdev);
872
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200873 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg744cf192011-11-08 20:40:14 +0200874 mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300875 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200876
877 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +0200878}
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200879
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300880static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
881 struct sk_buff *skb)
882{
883 struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
884
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300885 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300886
887 if (rp->status)
888 return;
889
890 hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
891 hdev->le_pkts = rp->le_max_pkt;
892
893 hdev->le_cnt = hdev->le_pkts;
894
895 BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
Ville Tervo6ed58ec2011-02-10 22:38:48 -0300896}
Johan Hedberg980e1a52011-01-22 06:10:07 +0200897
Johan Hedberg60e77322013-01-22 14:01:59 +0200898static void hci_cc_le_read_local_features(struct hci_dev *hdev,
899 struct sk_buff *skb)
900{
901 struct hci_rp_le_read_local_features *rp = (void *) skb->data;
902
903 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
904
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200905 if (rp->status)
906 return;
907
908 memcpy(hdev->le_features, rp->features, 8);
Johan Hedberg60e77322013-01-22 14:01:59 +0200909}
910
Johan Hedberg8fa19092012-10-19 20:57:49 +0300911static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
912 struct sk_buff *skb)
913{
914 struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
915
916 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
917
Marcel Holtmann45296ac2014-07-05 10:48:01 +0200918 if (rp->status)
919 return;
920
921 hdev->adv_tx_power = rp->tx_power;
Johan Hedberg8fa19092012-10-19 20:57:49 +0300922}
923
Johan Hedberga5c29682011-02-19 12:05:57 -0300924static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
925{
926 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
927
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300928 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Johan Hedberga5c29682011-02-19 12:05:57 -0300929
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200930 hci_dev_lock(hdev);
931
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200932 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300933 mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
934 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200935
936 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300937}
938
939static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300940 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -0300941{
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))
Johan Hedberg744cf192011-11-08 20:40:14 +0200949 mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300950 ACL_LINK, 0, rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200951
952 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -0300953}
954
Brian Gix1143d452011-11-23 08:28:34 -0800955static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
956{
957 struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
958
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300959 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800960
961 hci_dev_lock(hdev);
962
Johan Hedberga8b2d5c2012-01-08 23:11:15 +0200963 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +0200964 mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300965 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800966
967 hci_dev_unlock(hdev);
968}
969
970static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -0300971 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -0800972{
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))
Brian Gix1143d452011-11-23 08:28:34 -0800980 mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300981 ACL_LINK, 0, rp->status);
Brian Gix1143d452011-11-23 08:28:34 -0800982
983 hci_dev_unlock(hdev);
984}
985
Marcel Holtmann4d2d2792014-01-10 02:07:26 -0800986static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
987 struct sk_buff *skb)
Szymon Jancc35938b2011-03-22 13:12:21 +0100988{
989 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
990
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +0300991 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
Szymon Jancc35938b2011-03-22 13:12:21 +0100992
Johan Hedberg56e5cb82011-11-08 20:40:16 +0200993 hci_dev_lock(hdev);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -0800994 mgmt_read_local_oob_data_complete(hdev, rp->hash, rp->randomizer,
995 NULL, NULL, rp->status);
996 hci_dev_unlock(hdev);
997}
998
999static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
1000 struct sk_buff *skb)
1001{
1002 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
1003
1004 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1005
1006 hci_dev_lock(hdev);
1007 mgmt_read_local_oob_data_complete(hdev, rp->hash192, rp->randomizer192,
1008 rp->hash256, rp->randomizer256,
1009 rp->status);
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001010 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01001011}
1012
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001013
1014static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
1015{
1016 __u8 status = *((__u8 *) skb->data);
1017 bdaddr_t *sent;
1018
1019 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1020
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001021 if (status)
1022 return;
1023
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001024 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
1025 if (!sent)
1026 return;
1027
1028 hci_dev_lock(hdev);
1029
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001030 bacpy(&hdev->random_addr, sent);
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08001031
1032 hci_dev_unlock(hdev);
1033}
1034
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001035static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
1036{
1037 __u8 *sent, status = *((__u8 *) skb->data);
1038
1039 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1040
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001041 if (status)
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001042 return;
1043
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001044 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
1045 if (!sent)
Johan Hedberg3c857752014-03-25 10:30:49 +02001046 return;
1047
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001048 hci_dev_lock(hdev);
1049
Stephen Hemminger49c922b2014-10-27 21:12:20 -07001050 /* If we're doing connection initiation as peripheral. Set a
Johan Hedberg3c857752014-03-25 10:30:49 +02001051 * timeout in case something goes wrong.
1052 */
1053 if (*sent) {
1054 struct hci_conn *conn;
1055
Johan Hedberg66c417c2014-07-08 15:07:47 +03001056 set_bit(HCI_LE_ADV, &hdev->dev_flags);
1057
Johan Hedberg3c857752014-03-25 10:30:49 +02001058 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
1059 if (conn)
1060 queue_delayed_work(hdev->workqueue,
1061 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001062 conn->conn_timeout);
Johan Hedberg66c417c2014-07-08 15:07:47 +03001063 } else {
1064 clear_bit(HCI_LE_ADV, &hdev->dev_flags);
Johan Hedberg3c857752014-03-25 10:30:49 +02001065 }
1066
Johan Hedberg04b4edc2013-03-15 17:07:01 -05001067 hci_dev_unlock(hdev);
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01001068}
1069
Marcel Holtmann533553f2014-03-21 12:18:10 -07001070static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
1071{
1072 struct hci_cp_le_set_scan_param *cp;
1073 __u8 status = *((__u8 *) skb->data);
1074
1075 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1076
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001077 if (status)
1078 return;
1079
Marcel Holtmann533553f2014-03-21 12:18:10 -07001080 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
1081 if (!cp)
1082 return;
1083
1084 hci_dev_lock(hdev);
1085
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001086 hdev->le_scan_type = cp->type;
Marcel Holtmann533553f2014-03-21 12:18:10 -07001087
1088 hci_dev_unlock(hdev);
1089}
1090
Johan Hedbergb9a63282014-03-25 10:51:52 +02001091static bool has_pending_adv_report(struct hci_dev *hdev)
1092{
1093 struct discovery_state *d = &hdev->discovery;
1094
1095 return bacmp(&d->last_adv_addr, BDADDR_ANY);
1096}
1097
1098static void clear_pending_adv_report(struct hci_dev *hdev)
1099{
1100 struct discovery_state *d = &hdev->discovery;
1101
1102 bacpy(&d->last_adv_addr, BDADDR_ANY);
1103 d->last_adv_data_len = 0;
1104}
1105
1106static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001107 u8 bdaddr_type, s8 rssi, u32 flags,
1108 u8 *data, u8 len)
Johan Hedbergb9a63282014-03-25 10:51:52 +02001109{
1110 struct discovery_state *d = &hdev->discovery;
1111
1112 bacpy(&d->last_adv_addr, bdaddr);
1113 d->last_adv_addr_type = bdaddr_type;
Johan Hedbergff5cd292014-03-25 14:40:52 +02001114 d->last_adv_rssi = rssi;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001115 d->last_adv_flags = flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02001116 memcpy(d->last_adv_data, data, len);
1117 d->last_adv_data_len = len;
1118}
1119
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001120static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001121 struct sk_buff *skb)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001122{
1123 struct hci_cp_le_set_scan_enable *cp;
1124 __u8 status = *((__u8 *) skb->data);
1125
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001126 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001127
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001128 if (status)
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001129 return;
1130
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001131 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
1132 if (!cp)
Andre Guedes3fd319b2013-04-30 15:29:36 -03001133 return;
1134
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001135 switch (cp->enable) {
Andre Guedes76a388b2013-04-04 20:21:02 -03001136 case LE_SCAN_ENABLE:
Andre Guedesd23264a2011-11-25 20:53:38 -03001137 set_bit(HCI_LE_SCAN, &hdev->dev_flags);
Johan Hedbergb9a63282014-03-25 10:51:52 +02001138 if (hdev->le_scan_type == LE_SCAN_ACTIVE)
1139 clear_pending_adv_report(hdev);
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001140 break;
1141
Andre Guedes76a388b2013-04-04 20:21:02 -03001142 case LE_SCAN_DISABLE:
Johan Hedbergb9a63282014-03-25 10:51:52 +02001143 /* We do this here instead of when setting DISCOVERY_STOPPED
1144 * since the latter would potentially require waiting for
1145 * inquiry to stop too.
1146 */
1147 if (has_pending_adv_report(hdev)) {
1148 struct discovery_state *d = &hdev->discovery;
1149
1150 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001151 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02001152 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergab0aa432014-03-26 14:17:12 +02001153 d->last_adv_data,
Johan Hedbergb9a63282014-03-25 10:51:52 +02001154 d->last_adv_data_len, NULL, 0);
1155 }
1156
Johan Hedberg317ac8c2014-02-28 20:26:12 +02001157 /* Cancel this timer so that we don't try to disable scanning
1158 * when it's already disabled.
1159 */
1160 cancel_delayed_work(&hdev->le_scan_disable);
1161
Andre Guedesd23264a2011-11-25 20:53:38 -03001162 clear_bit(HCI_LE_SCAN, &hdev->dev_flags);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001163
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001164 /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
1165 * interrupted scanning due to a connect request. Mark
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001166 * therefore discovery as stopped. If this was not
1167 * because of a connect request advertising might have
1168 * been disabled because of active scanning, so
1169 * re-enable it again if necessary.
Johan Hedberg81ad6fd2014-02-28 20:26:13 +02001170 */
1171 if (test_and_clear_bit(HCI_LE_SCAN_INTERRUPTED,
1172 &hdev->dev_flags))
1173 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001174 else if (!test_bit(HCI_LE_ADV, &hdev->dev_flags) &&
Johan Hedberg34722272014-07-08 16:05:05 +03001175 hdev->discovery.state == DISCOVERY_FINDING)
Johan Hedberge8bb6b92014-07-08 15:07:53 +03001176 mgmt_reenable_advertising(hdev);
1177
Andrei Emeltchenko68a8aea2011-12-19 16:14:18 +02001178 break;
1179
1180 default:
1181 BT_ERR("Used reserved LE_Scan_Enable param %d", cp->enable);
1182 break;
Andre Guedes35815082011-05-26 16:23:53 -03001183 }
Andre Guedeseb9d91f2011-05-26 16:23:52 -03001184}
1185
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001186static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
1187 struct sk_buff *skb)
1188{
1189 struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
1190
1191 BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
1192
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001193 if (rp->status)
1194 return;
1195
1196 hdev->le_white_list_size = rp->size;
Johan Hedbergcf1d0812013-01-22 14:02:00 +02001197}
1198
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001199static void hci_cc_le_clear_white_list(struct hci_dev *hdev,
1200 struct sk_buff *skb)
1201{
1202 __u8 status = *((__u8 *) skb->data);
1203
1204 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1205
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001206 if (status)
1207 return;
1208
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001209 hci_bdaddr_list_clear(&hdev->le_white_list);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001210}
1211
1212static void hci_cc_le_add_to_white_list(struct hci_dev *hdev,
1213 struct sk_buff *skb)
1214{
1215 struct hci_cp_le_add_to_white_list *sent;
1216 __u8 status = *((__u8 *) skb->data);
1217
1218 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1219
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001220 if (status)
1221 return;
1222
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001223 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_WHITE_LIST);
1224 if (!sent)
1225 return;
1226
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001227 hci_bdaddr_list_add(&hdev->le_white_list, &sent->bdaddr,
1228 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001229}
1230
1231static void hci_cc_le_del_from_white_list(struct hci_dev *hdev,
1232 struct sk_buff *skb)
1233{
1234 struct hci_cp_le_del_from_white_list *sent;
1235 __u8 status = *((__u8 *) skb->data);
1236
1237 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1238
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001239 if (status)
1240 return;
1241
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001242 sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_WHITE_LIST);
1243 if (!sent)
1244 return;
1245
Johan Hedbergdcc36c12014-07-09 12:59:13 +03001246 hci_bdaddr_list_del(&hdev->le_white_list, &sent->bdaddr,
1247 sent->bdaddr_type);
Marcel Holtmann0f36b582014-02-27 20:37:31 -08001248}
1249
Johan Hedberg9b008c02013-01-22 14:02:01 +02001250static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
1251 struct sk_buff *skb)
1252{
1253 struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
1254
1255 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1256
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001257 if (rp->status)
1258 return;
1259
1260 memcpy(hdev->le_states, rp->le_states, 8);
Johan Hedberg9b008c02013-01-22 14:02:01 +02001261}
1262
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001263static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
1264 struct sk_buff *skb)
Andre Guedesf9b49302011-06-30 19:20:53 -03001265{
Johan Hedberg06199cf2012-02-22 16:37:11 +02001266 struct hci_cp_write_le_host_supported *sent;
Andre Guedesf9b49302011-06-30 19:20:53 -03001267 __u8 status = *((__u8 *) skb->data);
1268
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001269 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andre Guedesf9b49302011-06-30 19:20:53 -03001270
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001271 if (status)
1272 return;
1273
Johan Hedberg06199cf2012-02-22 16:37:11 +02001274 sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001275 if (!sent)
Andre Guedesf9b49302011-06-30 19:20:53 -03001276 return;
1277
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001278 if (sent->le) {
1279 hdev->features[1][0] |= LMP_HOST_LE;
1280 set_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1281 } else {
1282 hdev->features[1][0] &= ~LMP_HOST_LE;
1283 clear_bit(HCI_LE_ENABLED, &hdev->dev_flags);
1284 clear_bit(HCI_ADVERTISING, &hdev->dev_flags);
Johan Hedberg8f984df2012-02-28 01:07:22 +02001285 }
Marcel Holtmann45296ac2014-07-05 10:48:01 +02001286
1287 if (sent->simul)
1288 hdev->features[1][0] |= LMP_HOST_LE_BREDR;
1289 else
1290 hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
Andre Guedesf9b49302011-06-30 19:20:53 -03001291}
1292
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02001293static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
1294{
1295 struct hci_cp_le_set_adv_param *cp;
1296 u8 status = *((u8 *) skb->data);
1297
1298 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1299
1300 if (status)
1301 return;
1302
1303 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
1304 if (!cp)
1305 return;
1306
1307 hci_dev_lock(hdev);
1308 hdev->adv_addr_type = cp->own_address_type;
1309 hci_dev_unlock(hdev);
1310}
1311
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001312static void hci_cc_write_remote_amp_assoc(struct hci_dev *hdev,
1313 struct sk_buff *skb)
1314{
1315 struct hci_rp_write_remote_amp_assoc *rp = (void *) skb->data;
1316
1317 BT_DBG("%s status 0x%2.2x phy_handle 0x%2.2x",
1318 hdev->name, rp->status, rp->phy_handle);
1319
1320 if (rp->status)
1321 return;
1322
1323 amp_write_rem_assoc_continue(hdev, rp->phy_handle);
1324}
1325
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02001326static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
1327{
1328 struct hci_rp_read_rssi *rp = (void *) skb->data;
1329 struct hci_conn *conn;
1330
1331 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1332
1333 if (rp->status)
1334 return;
1335
1336 hci_dev_lock(hdev);
1337
1338 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
1339 if (conn)
1340 conn->rssi = rp->rssi;
1341
1342 hci_dev_unlock(hdev);
1343}
1344
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001345static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
1346{
1347 struct hci_cp_read_tx_power *sent;
1348 struct hci_rp_read_tx_power *rp = (void *) skb->data;
1349 struct hci_conn *conn;
1350
1351 BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
1352
1353 if (rp->status)
1354 return;
1355
1356 sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
1357 if (!sent)
1358 return;
1359
1360 hci_dev_lock(hdev);
1361
1362 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001363 if (!conn)
1364 goto unlock;
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001365
Andrzej Kaczmarekd0455ed2014-05-14 13:43:05 +02001366 switch (sent->type) {
1367 case 0x00:
1368 conn->tx_power = rp->tx_power;
1369 break;
1370 case 0x01:
1371 conn->max_tx_power = rp->tx_power;
1372 break;
1373 }
1374
1375unlock:
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02001376 hci_dev_unlock(hdev);
1377}
1378
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001379static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001380{
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001381 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001382
1383 if (status) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001384 hci_conn_check_pending(hdev);
Johan Hedberg314b2382011-04-27 10:29:57 -04001385 return;
1386 }
1387
Andre Guedes89352e72011-11-04 14:16:53 -03001388 set_bit(HCI_INQUIRY, &hdev->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001389}
1390
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001391static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001393 struct hci_cp_create_conn *cp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001396 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001397
1398 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 if (!cp)
1400 return;
1401
1402 hci_dev_lock(hdev);
1403
1404 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
1405
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03001406 BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 if (status) {
1409 if (conn && conn->state == BT_CONNECT) {
Marcel Holtmann4c67bc72006-10-15 17:30:56 +02001410 if (status != 0x0c || conn->attempt > 2) {
1411 conn->state = BT_CLOSED;
1412 hci_proto_connect_cfm(conn, status);
1413 hci_conn_del(conn);
1414 } else
1415 conn->state = BT_CONNECT2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
1417 } else {
1418 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03001419 conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr,
1420 HCI_ROLE_MASTER);
1421 if (!conn)
Gustavo F. Padovan893ef972010-07-18 15:13:37 -03001422 BT_ERR("No memory for new connection");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 }
1424 }
1425
1426 hci_dev_unlock(hdev);
1427}
1428
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001429static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001431 struct hci_cp_add_sco *cp;
1432 struct hci_conn *acl, *sco;
1433 __u16 handle;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001435 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001436
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001437 if (!status)
1438 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001440 cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
1441 if (!cp)
1442 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001444 handle = __le16_to_cpu(cp->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001446 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001447
1448 hci_dev_lock(hdev);
1449
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001450 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001451 if (acl) {
1452 sco = acl->link;
1453 if (sco) {
1454 sco->state = BT_CLOSED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001455
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001456 hci_proto_connect_cfm(sco, status);
1457 hci_conn_del(sco);
1458 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001459 }
Marcel Holtmann6bd57412006-11-18 22:14:22 +01001460
1461 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462}
1463
Marcel Holtmannf8558552008-07-14 20:13:49 +02001464static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
1465{
1466 struct hci_cp_auth_requested *cp;
1467 struct hci_conn *conn;
1468
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001469 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001470
1471 if (!status)
1472 return;
1473
1474 cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
1475 if (!cp)
1476 return;
1477
1478 hci_dev_lock(hdev);
1479
1480 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1481 if (conn) {
1482 if (conn->state == BT_CONFIG) {
1483 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001484 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001485 }
1486 }
1487
1488 hci_dev_unlock(hdev);
1489}
1490
1491static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
1492{
1493 struct hci_cp_set_conn_encrypt *cp;
1494 struct hci_conn *conn;
1495
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001496 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001497
1498 if (!status)
1499 return;
1500
1501 cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
1502 if (!cp)
1503 return;
1504
1505 hci_dev_lock(hdev);
1506
1507 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1508 if (conn) {
1509 if (conn->state == BT_CONFIG) {
1510 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001511 hci_conn_drop(conn);
Marcel Holtmannf8558552008-07-14 20:13:49 +02001512 }
1513 }
1514
1515 hci_dev_unlock(hdev);
1516}
1517
Johan Hedberg127178d2010-11-18 22:22:29 +02001518static int hci_outgoing_auth_needed(struct hci_dev *hdev,
Gustavo Padovan807deac2012-05-17 00:36:24 -03001519 struct hci_conn *conn)
Johan Hedberg392599b2010-11-18 22:22:28 +02001520{
Johan Hedberg392599b2010-11-18 22:22:28 +02001521 if (conn->state != BT_CONFIG || !conn->out)
1522 return 0;
1523
Johan Hedberg765c2a92011-01-19 12:06:52 +05301524 if (conn->pending_sec_level == BT_SECURITY_SDP)
Johan Hedberg392599b2010-11-18 22:22:28 +02001525 return 0;
1526
1527 /* Only request authentication for SSP connections or non-SSP
Johan Hedberg264b8b42014-01-08 16:40:39 +02001528 * devices with sec_level MEDIUM or HIGH or if MITM protection
1529 * is requested.
1530 */
Gustavo Padovan807deac2012-05-17 00:36:24 -03001531 if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
Johan Hedberg7e3691e2014-05-30 14:45:19 +03001532 conn->pending_sec_level != BT_SECURITY_FIPS &&
Johan Hedberg264b8b42014-01-08 16:40:39 +02001533 conn->pending_sec_level != BT_SECURITY_HIGH &&
1534 conn->pending_sec_level != BT_SECURITY_MEDIUM)
Johan Hedberg392599b2010-11-18 22:22:28 +02001535 return 0;
1536
Johan Hedberg392599b2010-11-18 22:22:28 +02001537 return 1;
1538}
1539
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001540static int hci_resolve_name(struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001541 struct inquiry_entry *e)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001542{
1543 struct hci_cp_remote_name_req cp;
1544
1545 memset(&cp, 0, sizeof(cp));
1546
1547 bacpy(&cp.bdaddr, &e->data.bdaddr);
1548 cp.pscan_rep_mode = e->data.pscan_rep_mode;
1549 cp.pscan_mode = e->data.pscan_mode;
1550 cp.clock_offset = e->data.clock_offset;
1551
1552 return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
1553}
1554
Johan Hedbergb644ba32012-01-17 21:48:47 +02001555static bool hci_resolve_next_name(struct hci_dev *hdev)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001556{
1557 struct discovery_state *discov = &hdev->discovery;
1558 struct inquiry_entry *e;
1559
Johan Hedbergb644ba32012-01-17 21:48:47 +02001560 if (list_empty(&discov->resolve))
1561 return false;
1562
1563 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
Ram Malovanyc8100892012-07-19 10:26:09 +03001564 if (!e)
1565 return false;
1566
Johan Hedbergb644ba32012-01-17 21:48:47 +02001567 if (hci_resolve_name(hdev, e) == 0) {
1568 e->name_state = NAME_PENDING;
1569 return true;
1570 }
1571
1572 return false;
1573}
1574
1575static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001576 bdaddr_t *bdaddr, u8 *name, u8 name_len)
Johan Hedbergb644ba32012-01-17 21:48:47 +02001577{
1578 struct discovery_state *discov = &hdev->discovery;
1579 struct inquiry_entry *e;
1580
1581 if (conn && !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00001582 mgmt_device_connected(hdev, conn, 0, name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001583
1584 if (discov->state == DISCOVERY_STOPPED)
1585 return;
1586
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001587 if (discov->state == DISCOVERY_STOPPING)
1588 goto discov_complete;
1589
1590 if (discov->state != DISCOVERY_RESOLVING)
1591 return;
1592
1593 e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
Ram Malovany7cc83802012-07-19 10:26:10 +03001594 /* If the device was not found in a list of found devices names of which
1595 * are pending. there is no need to continue resolving a next name as it
1596 * will be done upon receiving another Remote Name Request Complete
1597 * Event */
1598 if (!e)
1599 return;
1600
1601 list_del(&e->list);
1602 if (name) {
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001603 e->name_state = NAME_KNOWN;
Ram Malovany7cc83802012-07-19 10:26:10 +03001604 mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
1605 e->data.rssi, name, name_len);
Ram Malovanyc3e7c0d2012-07-19 10:26:11 +03001606 } else {
1607 e->name_state = NAME_NOT_KNOWN;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001608 }
1609
Johan Hedbergb644ba32012-01-17 21:48:47 +02001610 if (hci_resolve_next_name(hdev))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001611 return;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001612
1613discov_complete:
1614 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1615}
1616
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001617static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
1618{
Johan Hedberg127178d2010-11-18 22:22:29 +02001619 struct hci_cp_remote_name_req *cp;
1620 struct hci_conn *conn;
1621
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001622 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Johan Hedberg127178d2010-11-18 22:22:29 +02001623
1624 /* If successful wait for the name req complete event before
1625 * checking for the need to do authentication */
1626 if (!status)
1627 return;
1628
1629 cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
1630 if (!cp)
1631 return;
1632
1633 hci_dev_lock(hdev);
1634
1635 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02001636
1637 if (test_bit(HCI_MGMT, &hdev->dev_flags))
1638 hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
1639
Johan Hedberg79c6c702011-04-28 11:28:55 -07001640 if (!conn)
1641 goto unlock;
1642
1643 if (!hci_outgoing_auth_needed(hdev, conn))
1644 goto unlock;
1645
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001646 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02001647 struct hci_cp_auth_requested auth_cp;
1648
Johan Hedberg977f8fc2014-07-17 15:35:39 +03001649 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
1650
Johannes Bergc1f23a22013-10-07 18:19:16 +02001651 auth_cp.handle = __cpu_to_le16(conn->handle);
1652 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
1653 sizeof(auth_cp), &auth_cp);
Johan Hedberg127178d2010-11-18 22:22:29 +02001654 }
1655
Johan Hedberg79c6c702011-04-28 11:28:55 -07001656unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02001657 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001658}
1659
Marcel Holtmann769be972008-07-14 20:13:49 +02001660static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
1661{
1662 struct hci_cp_read_remote_features *cp;
1663 struct hci_conn *conn;
1664
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001665 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001666
1667 if (!status)
1668 return;
1669
1670 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
1671 if (!cp)
1672 return;
1673
1674 hci_dev_lock(hdev);
1675
1676 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1677 if (conn) {
1678 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001679 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001680 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001681 }
1682 }
1683
1684 hci_dev_unlock(hdev);
1685}
1686
1687static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
1688{
1689 struct hci_cp_read_remote_ext_features *cp;
1690 struct hci_conn *conn;
1691
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001692 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmann769be972008-07-14 20:13:49 +02001693
1694 if (!status)
1695 return;
1696
1697 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
1698 if (!cp)
1699 return;
1700
1701 hci_dev_lock(hdev);
1702
1703 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1704 if (conn) {
1705 if (conn->state == BT_CONFIG) {
Marcel Holtmann769be972008-07-14 20:13:49 +02001706 hci_proto_connect_cfm(conn, status);
David Herrmann76a68ba2013-04-06 20:28:37 +02001707 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02001708 }
1709 }
1710
1711 hci_dev_unlock(hdev);
1712}
1713
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001714static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
1715{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001716 struct hci_cp_setup_sync_conn *cp;
1717 struct hci_conn *acl, *sco;
1718 __u16 handle;
1719
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001720 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001721
1722 if (!status)
1723 return;
1724
1725 cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
1726 if (!cp)
1727 return;
1728
1729 handle = __le16_to_cpu(cp->handle);
1730
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001731 BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001732
1733 hci_dev_lock(hdev);
1734
1735 acl = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001736 if (acl) {
1737 sco = acl->link;
1738 if (sco) {
1739 sco->state = BT_CLOSED;
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001740
Andrei Emeltchenko5a08ecc2011-01-11 17:20:20 +02001741 hci_proto_connect_cfm(sco, status);
1742 hci_conn_del(sco);
1743 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02001744 }
1745
1746 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001747}
1748
1749static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
1750{
1751 struct hci_cp_sniff_mode *cp;
1752 struct hci_conn *conn;
1753
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001754 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001755
1756 if (!status)
1757 return;
1758
1759 cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
1760 if (!cp)
1761 return;
1762
1763 hci_dev_lock(hdev);
1764
1765 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001766 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001767 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001768
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001769 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001770 hci_sco_setup(conn, status);
1771 }
1772
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001773 hci_dev_unlock(hdev);
1774}
1775
1776static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
1777{
1778 struct hci_cp_exit_sniff_mode *cp;
1779 struct hci_conn *conn;
1780
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001781 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001782
1783 if (!status)
1784 return;
1785
1786 cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
1787 if (!cp)
1788 return;
1789
1790 hci_dev_lock(hdev);
1791
1792 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001793 if (conn) {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001794 clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001795
Johan Hedberg51a8efd2012-01-16 06:10:31 +02001796 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04001797 hci_sco_setup(conn, status);
1798 }
1799
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001800 hci_dev_unlock(hdev);
1801}
1802
Johan Hedberg88c3df12012-02-09 14:27:38 +02001803static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
1804{
1805 struct hci_cp_disconnect *cp;
1806 struct hci_conn *conn;
1807
1808 if (!status)
1809 return;
1810
1811 cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
1812 if (!cp)
1813 return;
1814
1815 hci_dev_lock(hdev);
1816
1817 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1818 if (conn)
1819 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001820 conn->dst_type, status);
Johan Hedberg88c3df12012-02-09 14:27:38 +02001821
1822 hci_dev_unlock(hdev);
1823}
1824
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001825static void hci_cs_create_phylink(struct hci_dev *hdev, u8 status)
1826{
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001827 struct hci_cp_create_phy_link *cp;
1828
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001829 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001830
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03001831 cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_PHY_LINK);
1832 if (!cp)
1833 return;
1834
Andrei Emeltchenkoe58917b2012-10-31 15:46:33 +02001835 hci_dev_lock(hdev);
1836
1837 if (status) {
1838 struct hci_conn *hcon;
1839
1840 hcon = hci_conn_hash_lookup_handle(hdev, cp->phy_handle);
1841 if (hcon)
1842 hci_conn_del(hcon);
1843 } else {
1844 amp_write_remote_assoc(hdev, cp->phy_handle);
1845 }
1846
1847 hci_dev_unlock(hdev);
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03001848}
1849
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +03001850static void hci_cs_accept_phylink(struct hci_dev *hdev, u8 status)
1851{
1852 struct hci_cp_accept_phy_link *cp;
1853
1854 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1855
1856 if (status)
1857 return;
1858
1859 cp = hci_sent_cmd_data(hdev, HCI_OP_ACCEPT_PHY_LINK);
1860 if (!cp)
1861 return;
1862
1863 amp_write_remote_assoc(hdev, cp->phy_handle);
1864}
1865
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02001866static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
1867{
1868 struct hci_cp_le_create_conn *cp;
1869 struct hci_conn *conn;
1870
1871 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1872
1873 /* All connection failure handling is taken care of by the
1874 * hci_le_conn_failed function which is triggered by the HCI
1875 * request completion callbacks used for connecting.
1876 */
1877 if (status)
1878 return;
1879
1880 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
1881 if (!cp)
1882 return;
1883
1884 hci_dev_lock(hdev);
1885
1886 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->peer_addr);
1887 if (!conn)
1888 goto unlock;
1889
1890 /* Store the initiator and responder address information which
1891 * is needed for SMP. These values will not change during the
1892 * lifetime of the connection.
1893 */
1894 conn->init_addr_type = cp->own_address_type;
1895 if (cp->own_address_type == ADDR_LE_DEV_RANDOM)
1896 bacpy(&conn->init_addr, &hdev->random_addr);
1897 else
1898 bacpy(&conn->init_addr, &hdev->bdaddr);
1899
1900 conn->resp_addr_type = cp->peer_addr_type;
1901 bacpy(&conn->resp_addr, &cp->peer_addr);
1902
Johan Hedberg9489eca2014-02-28 17:45:46 +02001903 /* We don't want the connection attempt to stick around
1904 * indefinitely since LE doesn't have a page timeout concept
1905 * like BR/EDR. Set a timer for any connection that doesn't use
1906 * the white list for connecting.
1907 */
1908 if (cp->filter_policy == HCI_LE_USE_PEER_ADDR)
1909 queue_delayed_work(conn->hdev->workqueue,
1910 &conn->le_conn_timeout,
Johan Hedberg09ae2602014-07-06 13:41:15 +03001911 conn->conn_timeout);
Johan Hedberg9489eca2014-02-28 17:45:46 +02001912
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02001913unlock:
1914 hci_dev_unlock(hdev);
1915}
1916
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02001917static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
1918{
1919 struct hci_cp_le_start_enc *cp;
1920 struct hci_conn *conn;
1921
1922 BT_DBG("%s status 0x%2.2x", hdev->name, status);
1923
1924 if (!status)
1925 return;
1926
1927 hci_dev_lock(hdev);
1928
1929 cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
1930 if (!cp)
1931 goto unlock;
1932
1933 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
1934 if (!conn)
1935 goto unlock;
1936
1937 if (conn->state != BT_CONNECTED)
1938 goto unlock;
1939
1940 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
1941 hci_conn_drop(conn);
1942
1943unlock:
1944 hci_dev_unlock(hdev);
1945}
1946
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001947static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001948{
1949 __u8 status = *((__u8 *) skb->data);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001950 struct discovery_state *discov = &hdev->discovery;
1951 struct inquiry_entry *e;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001952
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03001953 BT_DBG("%s status 0x%2.2x", hdev->name, status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001954
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001955 hci_conn_check_pending(hdev);
Andre Guedes89352e72011-11-04 14:16:53 -03001956
1957 if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
1958 return;
1959
Peter Zijlstra4e857c52014-03-17 18:06:10 +01001960 smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
Andre Guedes3e13fa12013-03-27 20:04:56 -03001961 wake_up_bit(&hdev->flags, HCI_INQUIRY);
1962
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02001963 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001964 return;
1965
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001966 hci_dev_lock(hdev);
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001967
Andre Guedes343f9352012-02-17 20:39:37 -03001968 if (discov->state != DISCOVERY_FINDING)
Johan Hedberg30dc78e2012-01-04 15:44:20 +02001969 goto unlock;
1970
1971 if (list_empty(&discov->resolve)) {
1972 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1973 goto unlock;
1974 }
1975
1976 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
1977 if (e && hci_resolve_name(hdev, e) == 0) {
1978 e->name_state = NAME_PENDING;
1979 hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
1980 } else {
1981 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
1982 }
1983
1984unlock:
Johan Hedberg56e5cb82011-11-08 20:40:16 +02001985 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001986}
1987
Gustavo Padovan6039aa72012-05-23 04:04:18 -03001988static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001990 struct inquiry_data data;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02001991 struct inquiry_info *info = (void *) (skb->data + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 int num_rsp = *((__u8 *) skb->data);
1993
1994 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
1995
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07001996 if (!num_rsp)
1997 return;
1998
Andre Guedes1519cc12012-03-21 00:03:38 -03001999 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
2000 return;
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002003
Johan Hedberge17acd42011-03-30 23:57:16 +03002004 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02002005 u32 flags;
Johan Hedberg31754052012-01-04 13:39:52 +02002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 bacpy(&data.bdaddr, &info->bdaddr);
2008 data.pscan_rep_mode = info->pscan_rep_mode;
2009 data.pscan_period_mode = info->pscan_period_mode;
2010 data.pscan_mode = info->pscan_mode;
2011 memcpy(data.dev_class, info->dev_class, 3);
2012 data.clock_offset = info->clock_offset;
2013 data.rssi = 0x00;
Marcel Holtmann41a96212008-07-14 20:13:48 +02002014 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02002015
Marcel Holtmannaf589252014-07-01 14:11:20 +02002016 flags = hci_inquiry_cache_update(hdev, &data, false);
2017
Johan Hedberg48264f02011-11-09 13:58:58 +02002018 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02002019 info->dev_class, 0, flags, NULL, 0, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 hci_dev_unlock(hdev);
2023}
2024
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002025static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002027 struct hci_ev_conn_complete *ev = (void *) skb->data;
2028 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002030 BT_DBG("%s", hdev->name);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002031
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 hci_dev_lock(hdev);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002033
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002034 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann94992372009-04-19 19:30:03 +02002035 if (!conn) {
2036 if (ev->link_type != SCO_LINK)
2037 goto unlock;
2038
2039 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
2040 if (!conn)
2041 goto unlock;
2042
2043 conn->type = SCO_LINK;
2044 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002045
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002046 if (!ev->status) {
2047 conn->handle = __le16_to_cpu(ev->handle);
Marcel Holtmann769be972008-07-14 20:13:49 +02002048
2049 if (conn->type == ACL_LINK) {
2050 conn->state = BT_CONFIG;
2051 hci_conn_hold(conn);
Szymon Janca9ea3ed2012-07-19 14:46:08 +02002052
2053 if (!conn->out && !hci_conn_ssp_enabled(conn) &&
2054 !hci_find_link_key(hdev, &ev->bdaddr))
2055 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
2056 else
2057 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Marcel Holtmann769be972008-07-14 20:13:49 +02002058 } else
2059 conn->state = BT_CONNECTED;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002060
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002061 hci_conn_add_sysfs(conn);
2062
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002063 if (test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002064 set_bit(HCI_CONN_AUTH, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002065
2066 if (test_bit(HCI_ENCRYPT, &hdev->flags))
Johan Hedberg4dae2792014-06-24 17:03:50 +03002067 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002068
2069 /* Get remote features */
2070 if (conn->type == ACL_LINK) {
2071 struct hci_cp_read_remote_features cp;
2072 cp.handle = ev->handle;
Marcel Holtmann769be972008-07-14 20:13:49 +02002073 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002074 sizeof(cp), &cp);
Johan Hedberg22f433d2014-08-01 11:13:32 +03002075
2076 hci_update_page_scan(hdev, NULL);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002077 }
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002078
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002079 /* Set packet type for incoming connection */
Andrei Emeltchenkod095c1e2011-12-01 14:33:27 +02002080 if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002081 struct hci_cp_change_conn_ptype cp;
2082 cp.handle = ev->handle;
Marcel Holtmanna8746412008-07-14 20:13:46 +02002083 cp.pkt_type = cpu_to_le16(conn->pkt_type);
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002084 hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
2085 &cp);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002086 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02002087 } else {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002088 conn->state = BT_CLOSED;
Johan Hedberg17d5c042011-01-22 06:09:08 +02002089 if (conn->type == ACL_LINK)
Marcel Holtmann64c7b772014-02-18 14:22:20 -08002090 mgmt_connect_failed(hdev, &conn->dst, conn->type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002091 conn->dst_type, ev->status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02002092 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002093
Marcel Holtmanne73439d2010-07-26 10:06:00 -04002094 if (conn->type == ACL_LINK)
2095 hci_sco_setup(conn, ev->status);
Marcel Holtmann45bb4bf2005-08-09 20:27:49 -07002096
Marcel Holtmann769be972008-07-14 20:13:49 +02002097 if (ev->status) {
2098 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002099 hci_conn_del(conn);
Marcel Holtmannc89b6e62009-01-15 21:57:03 +01002100 } else if (ev->link_type != ACL_LINK)
2101 hci_proto_connect_cfm(conn, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002102
2103unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002105
2106 hci_conn_check_pending(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
2108
Johan Hedberg70c46422014-07-09 12:59:17 +03002109static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
2110{
2111 struct hci_cp_reject_conn_req cp;
2112
2113 bacpy(&cp.bdaddr, bdaddr);
2114 cp.reason = HCI_ERROR_REJ_BAD_ADDR;
2115 hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
2116}
2117
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002118static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002120 struct hci_ev_conn_request *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 int mask = hdev->link_mode;
Johan Hedberg70c46422014-07-09 12:59:17 +03002122 struct inquiry_entry *ie;
2123 struct hci_conn *conn;
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002124 __u8 flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03002126 BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002127 ev->link_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
Frédéric Dalleau20714bf2012-11-21 10:51:12 +01002129 mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
2130 &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Johan Hedberg70c46422014-07-09 12:59:17 +03002132 if (!(mask & HCI_LM_ACCEPT)) {
2133 hci_reject_conn(hdev, &ev->bdaddr);
2134 return;
2135 }
2136
Johan Hedberg46c4c942014-07-16 16:19:21 +03002137 if (hci_bdaddr_list_lookup(&hdev->blacklist, &ev->bdaddr,
2138 BDADDR_BREDR)) {
2139 hci_reject_conn(hdev, &ev->bdaddr);
2140 return;
2141 }
2142
2143 if (!test_bit(HCI_CONNECTABLE, &hdev->dev_flags) &&
2144 !hci_bdaddr_list_lookup(&hdev->whitelist, &ev->bdaddr,
2145 BDADDR_BREDR)) {
2146 hci_reject_conn(hdev, &ev->bdaddr);
2147 return;
Johan Hedberg70c46422014-07-09 12:59:17 +03002148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
Johan Hedberg70c46422014-07-09 12:59:17 +03002150 /* Connection accepted */
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002151
Johan Hedberg70c46422014-07-09 12:59:17 +03002152 hci_dev_lock(hdev);
Marcel Holtmannc7bdd502008-07-14 20:13:47 +02002153
Johan Hedberg70c46422014-07-09 12:59:17 +03002154 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
2155 if (ie)
2156 memcpy(ie->data.dev_class, ev->dev_class, 3);
2157
2158 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
2159 &ev->bdaddr);
2160 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03002161 conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
2162 HCI_ROLE_SLAVE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (!conn) {
Johan Hedberg70c46422014-07-09 12:59:17 +03002164 BT_ERR("No memory for new connection");
2165 hci_dev_unlock(hdev);
2166 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
Johan Hedberg70c46422014-07-09 12:59:17 +03002168 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002169
Johan Hedberg70c46422014-07-09 12:59:17 +03002170 memcpy(conn->dev_class, ev->dev_class, 3);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02002171
Johan Hedberg70c46422014-07-09 12:59:17 +03002172 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Johan Hedberg70c46422014-07-09 12:59:17 +03002174 if (ev->link_type == ACL_LINK ||
2175 (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
2176 struct hci_cp_accept_conn_req cp;
2177 conn->state = BT_CONNECT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 bacpy(&cp.bdaddr, &ev->bdaddr);
Johan Hedberg70c46422014-07-09 12:59:17 +03002180
2181 if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
2182 cp.role = 0x00; /* Become master */
2183 else
2184 cp.role = 0x01; /* Remain slave */
2185
2186 hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
2187 } else if (!(flags & HCI_PROTO_DEFER)) {
2188 struct hci_cp_accept_sync_conn_req cp;
2189 conn->state = BT_CONNECT;
2190
2191 bacpy(&cp.bdaddr, &ev->bdaddr);
2192 cp.pkt_type = cpu_to_le16(conn->pkt_type);
2193
2194 cp.tx_bandwidth = cpu_to_le32(0x00001f40);
2195 cp.rx_bandwidth = cpu_to_le32(0x00001f40);
2196 cp.max_latency = cpu_to_le16(0xffff);
2197 cp.content_format = cpu_to_le16(hdev->voice_setting);
2198 cp.retrans_effort = 0xff;
2199
2200 hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
2201 &cp);
2202 } else {
2203 conn->state = BT_CONNECT2;
2204 hci_proto_connect_cfm(conn, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 }
2206}
2207
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02002208static u8 hci_to_mgmt_reason(u8 err)
2209{
2210 switch (err) {
2211 case HCI_ERROR_CONNECTION_TIMEOUT:
2212 return MGMT_DEV_DISCONN_TIMEOUT;
2213 case HCI_ERROR_REMOTE_USER_TERM:
2214 case HCI_ERROR_REMOTE_LOW_RESOURCES:
2215 case HCI_ERROR_REMOTE_POWER_OFF:
2216 return MGMT_DEV_DISCONN_REMOTE;
2217 case HCI_ERROR_LOCAL_HOST_TERM:
2218 return MGMT_DEV_DISCONN_LOCAL_HOST;
2219 default:
2220 return MGMT_DEV_DISCONN_UNKNOWN;
2221 }
2222}
2223
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002224static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002226 struct hci_ev_disconn_complete *ev = (void *) skb->data;
Andre Guedesabf54a52013-11-07 17:36:09 -03002227 u8 reason = hci_to_mgmt_reason(ev->reason);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002228 struct hci_conn_params *params;
Marcel Holtmann04837f62006-07-03 10:02:33 +02002229 struct hci_conn *conn;
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002230 bool mgmt_connected;
Andre Guedes38462202013-11-07 17:36:10 -03002231 u8 type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002233 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 hci_dev_lock(hdev);
2236
Marcel Holtmann04837f62006-07-03 10:02:33 +02002237 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergf7520542011-01-20 12:34:39 +02002238 if (!conn)
2239 goto unlock;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02002240
Andre Guedesabf54a52013-11-07 17:36:09 -03002241 if (ev->status) {
2242 mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
2243 conn->dst_type, ev->status);
2244 goto unlock;
Johan Hedberg37d9ef72011-11-10 15:54:39 +02002245 }
Johan Hedbergf7520542011-01-20 12:34:39 +02002246
Andre Guedes38462202013-11-07 17:36:10 -03002247 conn->state = BT_CLOSED;
2248
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02002249 mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
2250 mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
2251 reason, mgmt_connected);
Andre Guedesabf54a52013-11-07 17:36:09 -03002252
Johan Hedberg22f433d2014-08-01 11:13:32 +03002253 if (conn->type == ACL_LINK) {
2254 if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
2255 hci_remove_link_key(hdev, &conn->dst);
2256
2257 hci_update_page_scan(hdev, NULL);
2258 }
Johan Hedberg22102462013-10-05 12:01:06 +02002259
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002260 params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
2261 if (params) {
2262 switch (params->auto_connect) {
2263 case HCI_AUTO_CONN_LINK_LOSS:
2264 if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
2265 break;
2266 /* Fall through */
2267
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02002268 case HCI_AUTO_CONN_DIRECT:
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002269 case HCI_AUTO_CONN_ALWAYS:
Johan Hedberg418025d2014-07-04 12:37:24 +03002270 list_del_init(&params->action);
2271 list_add(&params->action, &hdev->pend_le_conns);
2272 hci_update_background_scan(hdev);
Andre Guedes9fcb18e2014-02-26 20:21:48 -03002273 break;
2274
2275 default:
2276 break;
2277 }
2278 }
2279
Andre Guedes38462202013-11-07 17:36:10 -03002280 type = conn->type;
Johan Hedberg22102462013-10-05 12:01:06 +02002281
Andre Guedes38462202013-11-07 17:36:10 -03002282 hci_proto_disconn_cfm(conn, ev->reason);
2283 hci_conn_del(conn);
2284
2285 /* Re-enable advertising if necessary, since it might
2286 * have been disabled by the connection. From the
2287 * HCI_LE_Set_Advertise_Enable command description in
2288 * the core specification (v4.0):
2289 * "The Controller shall continue advertising until the Host
2290 * issues an LE_Set_Advertise_Enable command with
2291 * Advertising_Enable set to 0x00 (Advertising is disabled)
2292 * or until a connection is created or until the Advertising
2293 * is timed out due to Directed Advertising."
2294 */
2295 if (type == LE_LINK)
2296 mgmt_reenable_advertising(hdev);
Johan Hedbergf7520542011-01-20 12:34:39 +02002297
2298unlock:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 hci_dev_unlock(hdev);
2300}
2301
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002302static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002303{
2304 struct hci_ev_auth_complete *ev = (void *) skb->data;
2305 struct hci_conn *conn;
2306
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002307 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002308
2309 hci_dev_lock(hdev);
2310
2311 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002312 if (!conn)
2313 goto unlock;
2314
2315 if (!ev->status) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002316 if (!hci_conn_ssp_enabled(conn) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03002317 test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002318 BT_INFO("re-auth of legacy device is not possible.");
Johan Hedberg2a611692011-02-19 12:06:00 -03002319 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03002320 set_bit(HCI_CONN_AUTH, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002321 conn->sec_level = conn->pending_sec_level;
Johan Hedberg2a611692011-02-19 12:06:00 -03002322 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002323 } else {
Johan Hedberge1e930f2014-09-08 17:09:49 -07002324 mgmt_auth_failed(conn, ev->status);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002325 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002326
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002327 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
2328 clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002329
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002330 if (conn->state == BT_CONFIG) {
Johan Hedbergaa64a8b2012-01-18 21:33:12 +02002331 if (!ev->status && hci_conn_ssp_enabled(conn)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002332 struct hci_cp_set_conn_encrypt cp;
2333 cp.handle = ev->handle;
2334 cp.encrypt = 0x01;
2335 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002336 &cp);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002337 } else {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002338 conn->state = BT_CONNECTED;
2339 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002340 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002341 }
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002342 } else {
2343 hci_auth_cfm(conn, ev->status);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02002344
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002345 hci_conn_hold(conn);
2346 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02002347 hci_conn_drop(conn);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002348 }
2349
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002350 if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002351 if (!ev->status) {
2352 struct hci_cp_set_conn_encrypt cp;
2353 cp.handle = ev->handle;
2354 cp.encrypt = 0x01;
2355 hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
Gustavo Padovan807deac2012-05-17 00:36:24 -03002356 &cp);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002357 } else {
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002358 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002359 hci_encrypt_cfm(conn, ev->status, 0x00);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002360 }
2361 }
2362
Waldemar Rymarkiewiczd7556e22011-05-31 15:49:26 +02002363unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002364 hci_dev_unlock(hdev);
2365}
2366
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002367static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002368{
Johan Hedberg127178d2010-11-18 22:22:29 +02002369 struct hci_ev_remote_name *ev = (void *) skb->data;
2370 struct hci_conn *conn;
2371
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002372 BT_DBG("%s", hdev->name);
2373
2374 hci_conn_check_pending(hdev);
Johan Hedberg127178d2010-11-18 22:22:29 +02002375
2376 hci_dev_lock(hdev);
2377
2378 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002379
2380 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
2381 goto check_auth;
2382
2383 if (ev->status == 0)
2384 hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002385 strnlen(ev->name, HCI_MAX_NAME_LENGTH));
Johan Hedbergb644ba32012-01-17 21:48:47 +02002386 else
2387 hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
2388
2389check_auth:
Johan Hedberg79c6c702011-04-28 11:28:55 -07002390 if (!conn)
2391 goto unlock;
2392
2393 if (!hci_outgoing_auth_needed(hdev, conn))
2394 goto unlock;
2395
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002396 if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002397 struct hci_cp_auth_requested cp;
Johan Hedberg977f8fc2014-07-17 15:35:39 +03002398
2399 set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
2400
Johan Hedberg127178d2010-11-18 22:22:29 +02002401 cp.handle = __cpu_to_le16(conn->handle);
2402 hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
2403 }
2404
Johan Hedberg79c6c702011-04-28 11:28:55 -07002405unlock:
Johan Hedberg127178d2010-11-18 22:22:29 +02002406 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002407}
2408
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002409static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002410{
2411 struct hci_ev_encrypt_change *ev = (void *) skb->data;
2412 struct hci_conn *conn;
2413
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002414 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002415
2416 hci_dev_lock(hdev);
2417
2418 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002419 if (!conn)
2420 goto unlock;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002421
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002422 if (!ev->status) {
2423 if (ev->encrypt) {
2424 /* Encryption implies authentication */
Johan Hedberg4dae2792014-06-24 17:03:50 +03002425 set_bit(HCI_CONN_AUTH, &conn->flags);
2426 set_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002427 conn->sec_level = conn->pending_sec_level;
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002428
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08002429 /* P-256 authentication key implies FIPS */
2430 if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
Johan Hedberg4dae2792014-06-24 17:03:50 +03002431 set_bit(HCI_CONN_FIPS, &conn->flags);
Marcel Holtmann914a6ff2014-02-01 11:52:02 -08002432
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002433 if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
2434 conn->type == LE_LINK)
2435 set_bit(HCI_CONN_AES_CCM, &conn->flags);
2436 } else {
Johan Hedberg4dae2792014-06-24 17:03:50 +03002437 clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
Marcel Holtmannabf76ba2014-01-31 16:24:28 -08002438 clear_bit(HCI_CONN_AES_CCM, &conn->flags);
2439 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002440 }
2441
Johan Hedberg7ed3fa22014-09-10 22:16:35 -07002442 /* We should disregard the current RPA and generate a new one
2443 * whenever the encryption procedure fails.
2444 */
2445 if (ev->status && conn->type == LE_LINK)
2446 set_bit(HCI_RPA_EXPIRED, &hdev->dev_flags);
2447
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002448 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
2449
2450 if (ev->status && conn->state == BT_CONNECTED) {
2451 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
2452 hci_conn_drop(conn);
2453 goto unlock;
2454 }
2455
2456 if (conn->state == BT_CONFIG) {
2457 if (!ev->status)
2458 conn->state = BT_CONNECTED;
2459
Marcel Holtmann40b552a2014-03-19 14:10:25 -07002460 /* In Secure Connections Only mode, do not allow any
2461 * connections that are not encrypted with AES-CCM
2462 * using a P-256 authenticated combination key.
2463 */
2464 if (test_bit(HCI_SC_ONLY, &hdev->dev_flags) &&
2465 (!test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
2466 conn->key_type != HCI_LK_AUTH_COMBINATION_P256)) {
2467 hci_proto_connect_cfm(conn, HCI_ERROR_AUTH_FAILURE);
2468 hci_conn_drop(conn);
2469 goto unlock;
2470 }
2471
Marcel Holtmanndc8357c2014-01-31 16:24:27 -08002472 hci_proto_connect_cfm(conn, ev->status);
2473 hci_conn_drop(conn);
2474 } else
2475 hci_encrypt_cfm(conn, ev->status, ev->encrypt);
2476
Gustavo Padovana7d77232012-05-13 03:20:07 -03002477unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002478 hci_dev_unlock(hdev);
2479}
2480
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002481static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
2482 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002483{
2484 struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
2485 struct hci_conn *conn;
2486
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002487 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002488
2489 hci_dev_lock(hdev);
2490
2491 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
2492 if (conn) {
2493 if (!ev->status)
Johan Hedberg4dae2792014-06-24 17:03:50 +03002494 set_bit(HCI_CONN_SECURE, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002495
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002496 clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002497
2498 hci_key_change_cfm(conn, ev->status);
2499 }
2500
2501 hci_dev_unlock(hdev);
2502}
2503
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002504static void hci_remote_features_evt(struct hci_dev *hdev,
2505 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002506{
2507 struct hci_ev_remote_features *ev = (void *) skb->data;
2508 struct hci_conn *conn;
2509
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002510 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002511
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002512 hci_dev_lock(hdev);
2513
2514 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02002515 if (!conn)
2516 goto unlock;
Marcel Holtmann769be972008-07-14 20:13:49 +02002517
Johan Hedbergccd556f2010-11-10 17:11:51 +02002518 if (!ev->status)
Johan Hedbergcad718e2013-04-17 15:00:51 +03002519 memcpy(conn->features[0], ev->features, 8);
Johan Hedbergccd556f2010-11-10 17:11:51 +02002520
2521 if (conn->state != BT_CONFIG)
2522 goto unlock;
2523
2524 if (!ev->status && lmp_ssp_capable(hdev) && lmp_ssp_capable(conn)) {
2525 struct hci_cp_read_remote_ext_features cp;
2526 cp.handle = ev->handle;
2527 cp.page = 0x01;
2528 hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
Gustavo Padovan807deac2012-05-17 00:36:24 -03002529 sizeof(cp), &cp);
Johan Hedberg392599b2010-11-18 22:22:28 +02002530 goto unlock;
2531 }
2532
Johan Hedberg671267b2012-05-12 16:11:50 -03002533 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02002534 struct hci_cp_remote_name_req cp;
2535 memset(&cp, 0, sizeof(cp));
2536 bacpy(&cp.bdaddr, &conn->dst);
2537 cp.pscan_rep_mode = 0x02;
2538 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02002539 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00002540 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02002541
Johan Hedberg127178d2010-11-18 22:22:29 +02002542 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02002543 conn->state = BT_CONNECTED;
2544 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02002545 hci_conn_drop(conn);
Marcel Holtmann769be972008-07-14 20:13:49 +02002546 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002547
Johan Hedbergccd556f2010-11-10 17:11:51 +02002548unlock:
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002549 hci_dev_unlock(hdev);
2550}
2551
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002552static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002553{
2554 struct hci_ev_cmd_complete *ev = (void *) skb->data;
Johan Hedberg9238f362013-03-05 20:37:48 +02002555 u8 status = skb->data[sizeof(*ev)];
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002556 __u16 opcode;
2557
2558 skb_pull(skb, sizeof(*ev));
2559
2560 opcode = __le16_to_cpu(ev->opcode);
2561
2562 switch (opcode) {
2563 case HCI_OP_INQUIRY_CANCEL:
2564 hci_cc_inquiry_cancel(hdev, skb);
2565 break;
2566
Andre Guedes4d934832012-03-21 00:03:35 -03002567 case HCI_OP_PERIODIC_INQ:
2568 hci_cc_periodic_inq(hdev, skb);
2569 break;
2570
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002571 case HCI_OP_EXIT_PERIODIC_INQ:
2572 hci_cc_exit_periodic_inq(hdev, skb);
2573 break;
2574
2575 case HCI_OP_REMOTE_NAME_REQ_CANCEL:
2576 hci_cc_remote_name_req_cancel(hdev, skb);
2577 break;
2578
2579 case HCI_OP_ROLE_DISCOVERY:
2580 hci_cc_role_discovery(hdev, skb);
2581 break;
2582
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002583 case HCI_OP_READ_LINK_POLICY:
2584 hci_cc_read_link_policy(hdev, skb);
2585 break;
2586
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002587 case HCI_OP_WRITE_LINK_POLICY:
2588 hci_cc_write_link_policy(hdev, skb);
2589 break;
2590
Marcel Holtmanne4e8e372008-07-14 20:13:47 +02002591 case HCI_OP_READ_DEF_LINK_POLICY:
2592 hci_cc_read_def_link_policy(hdev, skb);
2593 break;
2594
2595 case HCI_OP_WRITE_DEF_LINK_POLICY:
2596 hci_cc_write_def_link_policy(hdev, skb);
2597 break;
2598
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002599 case HCI_OP_RESET:
2600 hci_cc_reset(hdev, skb);
2601 break;
2602
2603 case HCI_OP_WRITE_LOCAL_NAME:
2604 hci_cc_write_local_name(hdev, skb);
2605 break;
2606
2607 case HCI_OP_READ_LOCAL_NAME:
2608 hci_cc_read_local_name(hdev, skb);
2609 break;
2610
2611 case HCI_OP_WRITE_AUTH_ENABLE:
2612 hci_cc_write_auth_enable(hdev, skb);
2613 break;
2614
2615 case HCI_OP_WRITE_ENCRYPT_MODE:
2616 hci_cc_write_encrypt_mode(hdev, skb);
2617 break;
2618
2619 case HCI_OP_WRITE_SCAN_ENABLE:
2620 hci_cc_write_scan_enable(hdev, skb);
2621 break;
2622
2623 case HCI_OP_READ_CLASS_OF_DEV:
2624 hci_cc_read_class_of_dev(hdev, skb);
2625 break;
2626
2627 case HCI_OP_WRITE_CLASS_OF_DEV:
2628 hci_cc_write_class_of_dev(hdev, skb);
2629 break;
2630
2631 case HCI_OP_READ_VOICE_SETTING:
2632 hci_cc_read_voice_setting(hdev, skb);
2633 break;
2634
2635 case HCI_OP_WRITE_VOICE_SETTING:
2636 hci_cc_write_voice_setting(hdev, skb);
2637 break;
2638
Marcel Holtmannb4cb9fb2013-10-14 13:56:16 -07002639 case HCI_OP_READ_NUM_SUPPORTED_IAC:
2640 hci_cc_read_num_supported_iac(hdev, skb);
2641 break;
2642
Marcel Holtmann333140b2008-07-14 20:13:48 +02002643 case HCI_OP_WRITE_SSP_MODE:
2644 hci_cc_write_ssp_mode(hdev, skb);
2645 break;
2646
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08002647 case HCI_OP_WRITE_SC_SUPPORT:
2648 hci_cc_write_sc_support(hdev, skb);
2649 break;
2650
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002651 case HCI_OP_READ_LOCAL_VERSION:
2652 hci_cc_read_local_version(hdev, skb);
2653 break;
2654
2655 case HCI_OP_READ_LOCAL_COMMANDS:
2656 hci_cc_read_local_commands(hdev, skb);
2657 break;
2658
2659 case HCI_OP_READ_LOCAL_FEATURES:
2660 hci_cc_read_local_features(hdev, skb);
2661 break;
2662
Andre Guedes971e3a42011-06-30 19:20:52 -03002663 case HCI_OP_READ_LOCAL_EXT_FEATURES:
2664 hci_cc_read_local_ext_features(hdev, skb);
2665 break;
2666
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002667 case HCI_OP_READ_BUFFER_SIZE:
2668 hci_cc_read_buffer_size(hdev, skb);
2669 break;
2670
2671 case HCI_OP_READ_BD_ADDR:
2672 hci_cc_read_bd_addr(hdev, skb);
2673 break;
2674
Johan Hedbergf332ec62013-03-15 17:07:11 -05002675 case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
2676 hci_cc_read_page_scan_activity(hdev, skb);
2677 break;
2678
Johan Hedberg4a3ee762013-03-15 17:07:12 -05002679 case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
2680 hci_cc_write_page_scan_activity(hdev, skb);
2681 break;
2682
Johan Hedbergf332ec62013-03-15 17:07:11 -05002683 case HCI_OP_READ_PAGE_SCAN_TYPE:
2684 hci_cc_read_page_scan_type(hdev, skb);
2685 break;
2686
Johan Hedberg4a3ee762013-03-15 17:07:12 -05002687 case HCI_OP_WRITE_PAGE_SCAN_TYPE:
2688 hci_cc_write_page_scan_type(hdev, skb);
2689 break;
2690
Andrei Emeltchenko350ee4c2011-12-07 15:56:51 +02002691 case HCI_OP_READ_DATA_BLOCK_SIZE:
2692 hci_cc_read_data_block_size(hdev, skb);
2693 break;
2694
Andrei Emeltchenko1e89cff2011-11-24 14:52:02 +02002695 case HCI_OP_READ_FLOW_CONTROL_MODE:
2696 hci_cc_read_flow_control_mode(hdev, skb);
2697 break;
2698
Andrei Emeltchenko928abaa2011-10-12 10:53:57 +03002699 case HCI_OP_READ_LOCAL_AMP_INFO:
2700 hci_cc_read_local_amp_info(hdev, skb);
2701 break;
2702
Johan Hedberg33f35722014-06-28 17:54:06 +03002703 case HCI_OP_READ_CLOCK:
2704 hci_cc_read_clock(hdev, skb);
2705 break;
2706
Andrei Emeltchenko903e4542012-09-27 17:26:09 +03002707 case HCI_OP_READ_LOCAL_AMP_ASSOC:
2708 hci_cc_read_local_amp_assoc(hdev, skb);
2709 break;
2710
Johan Hedbergd5859e22011-01-25 01:19:58 +02002711 case HCI_OP_READ_INQ_RSP_TX_POWER:
2712 hci_cc_read_inq_rsp_tx_power(hdev, skb);
2713 break;
2714
Johan Hedberg980e1a52011-01-22 06:10:07 +02002715 case HCI_OP_PIN_CODE_REPLY:
2716 hci_cc_pin_code_reply(hdev, skb);
2717 break;
2718
2719 case HCI_OP_PIN_CODE_NEG_REPLY:
2720 hci_cc_pin_code_neg_reply(hdev, skb);
2721 break;
2722
Szymon Jancc35938b2011-03-22 13:12:21 +01002723 case HCI_OP_READ_LOCAL_OOB_DATA:
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08002724 hci_cc_read_local_oob_data(hdev, skb);
2725 break;
2726
2727 case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
2728 hci_cc_read_local_oob_ext_data(hdev, skb);
Szymon Jancc35938b2011-03-22 13:12:21 +01002729 break;
2730
Ville Tervo6ed58ec2011-02-10 22:38:48 -03002731 case HCI_OP_LE_READ_BUFFER_SIZE:
2732 hci_cc_le_read_buffer_size(hdev, skb);
2733 break;
2734
Johan Hedberg60e77322013-01-22 14:01:59 +02002735 case HCI_OP_LE_READ_LOCAL_FEATURES:
2736 hci_cc_le_read_local_features(hdev, skb);
2737 break;
2738
Johan Hedberg8fa19092012-10-19 20:57:49 +03002739 case HCI_OP_LE_READ_ADV_TX_POWER:
2740 hci_cc_le_read_adv_tx_power(hdev, skb);
2741 break;
2742
Johan Hedberga5c29682011-02-19 12:05:57 -03002743 case HCI_OP_USER_CONFIRM_REPLY:
2744 hci_cc_user_confirm_reply(hdev, skb);
2745 break;
2746
2747 case HCI_OP_USER_CONFIRM_NEG_REPLY:
2748 hci_cc_user_confirm_neg_reply(hdev, skb);
2749 break;
2750
Brian Gix1143d452011-11-23 08:28:34 -08002751 case HCI_OP_USER_PASSKEY_REPLY:
2752 hci_cc_user_passkey_reply(hdev, skb);
2753 break;
2754
2755 case HCI_OP_USER_PASSKEY_NEG_REPLY:
2756 hci_cc_user_passkey_neg_reply(hdev, skb);
Szymon Janc16cde992012-04-13 12:32:42 +02002757 break;
Andre Guedes07f7fa52011-12-02 21:13:31 +09002758
Marcel Holtmann7a4cd512014-02-19 19:52:13 -08002759 case HCI_OP_LE_SET_RANDOM_ADDR:
2760 hci_cc_le_set_random_addr(hdev, skb);
2761 break;
2762
Johan Hedbergc1d5dc42012-11-08 01:23:01 +01002763 case HCI_OP_LE_SET_ADV_ENABLE:
2764 hci_cc_le_set_adv_enable(hdev, skb);
2765 break;
2766
Marcel Holtmann533553f2014-03-21 12:18:10 -07002767 case HCI_OP_LE_SET_SCAN_PARAM:
2768 hci_cc_le_set_scan_param(hdev, skb);
2769 break;
2770
Andre Guedeseb9d91f2011-05-26 16:23:52 -03002771 case HCI_OP_LE_SET_SCAN_ENABLE:
2772 hci_cc_le_set_scan_enable(hdev, skb);
2773 break;
2774
Johan Hedbergcf1d0812013-01-22 14:02:00 +02002775 case HCI_OP_LE_READ_WHITE_LIST_SIZE:
2776 hci_cc_le_read_white_list_size(hdev, skb);
2777 break;
2778
Marcel Holtmann0f36b582014-02-27 20:37:31 -08002779 case HCI_OP_LE_CLEAR_WHITE_LIST:
2780 hci_cc_le_clear_white_list(hdev, skb);
2781 break;
2782
2783 case HCI_OP_LE_ADD_TO_WHITE_LIST:
2784 hci_cc_le_add_to_white_list(hdev, skb);
2785 break;
2786
2787 case HCI_OP_LE_DEL_FROM_WHITE_LIST:
2788 hci_cc_le_del_from_white_list(hdev, skb);
2789 break;
2790
Johan Hedberg9b008c02013-01-22 14:02:01 +02002791 case HCI_OP_LE_READ_SUPPORTED_STATES:
2792 hci_cc_le_read_supported_states(hdev, skb);
2793 break;
2794
Andre Guedesf9b49302011-06-30 19:20:53 -03002795 case HCI_OP_WRITE_LE_HOST_SUPPORTED:
2796 hci_cc_write_le_host_supported(hdev, skb);
2797 break;
2798
Johan Hedberg56ed2cb2014-02-27 14:05:40 +02002799 case HCI_OP_LE_SET_ADV_PARAM:
2800 hci_cc_set_adv_param(hdev, skb);
2801 break;
2802
Andrei Emeltchenko93c284e2012-09-27 17:26:20 +03002803 case HCI_OP_WRITE_REMOTE_AMP_ASSOC:
2804 hci_cc_write_remote_amp_assoc(hdev, skb);
2805 break;
2806
Andrzej Kaczmarek5ae76a92014-05-08 15:32:08 +02002807 case HCI_OP_READ_RSSI:
2808 hci_cc_read_rssi(hdev, skb);
2809 break;
2810
Andrzej Kaczmarek5a134fa2014-05-09 21:35:28 +02002811 case HCI_OP_READ_TX_POWER:
2812 hci_cc_read_tx_power(hdev, skb);
2813 break;
2814
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002815 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002816 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002817 break;
2818 }
2819
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002820 if (opcode != HCI_OP_NOP)
Marcel Holtmann65cc2b42014-06-16 12:30:56 +02002821 cancel_delayed_work(&hdev->cmd_timer);
Ville Tervo6bd32322011-02-16 16:32:41 +02002822
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002823 hci_req_cmd_complete(hdev, opcode, status);
Johan Hedberg9238f362013-03-05 20:37:48 +02002824
Szymon Jancdbccd792012-12-11 08:51:19 +01002825 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002826 atomic_set(&hdev->cmd_cnt, 1);
2827 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002828 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002829 }
2830}
2831
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002832static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002833{
2834 struct hci_ev_cmd_status *ev = (void *) skb->data;
2835 __u16 opcode;
2836
2837 skb_pull(skb, sizeof(*ev));
2838
2839 opcode = __le16_to_cpu(ev->opcode);
2840
2841 switch (opcode) {
2842 case HCI_OP_INQUIRY:
2843 hci_cs_inquiry(hdev, ev->status);
2844 break;
2845
2846 case HCI_OP_CREATE_CONN:
2847 hci_cs_create_conn(hdev, ev->status);
2848 break;
2849
2850 case HCI_OP_ADD_SCO:
2851 hci_cs_add_sco(hdev, ev->status);
2852 break;
2853
Marcel Holtmannf8558552008-07-14 20:13:49 +02002854 case HCI_OP_AUTH_REQUESTED:
2855 hci_cs_auth_requested(hdev, ev->status);
2856 break;
2857
2858 case HCI_OP_SET_CONN_ENCRYPT:
2859 hci_cs_set_conn_encrypt(hdev, ev->status);
2860 break;
2861
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002862 case HCI_OP_REMOTE_NAME_REQ:
2863 hci_cs_remote_name_req(hdev, ev->status);
2864 break;
2865
Marcel Holtmann769be972008-07-14 20:13:49 +02002866 case HCI_OP_READ_REMOTE_FEATURES:
2867 hci_cs_read_remote_features(hdev, ev->status);
2868 break;
2869
2870 case HCI_OP_READ_REMOTE_EXT_FEATURES:
2871 hci_cs_read_remote_ext_features(hdev, ev->status);
2872 break;
2873
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002874 case HCI_OP_SETUP_SYNC_CONN:
2875 hci_cs_setup_sync_conn(hdev, ev->status);
2876 break;
2877
2878 case HCI_OP_SNIFF_MODE:
2879 hci_cs_sniff_mode(hdev, ev->status);
2880 break;
2881
2882 case HCI_OP_EXIT_SNIFF_MODE:
2883 hci_cs_exit_sniff_mode(hdev, ev->status);
2884 break;
2885
Johan Hedberg8962ee72011-01-20 12:40:27 +02002886 case HCI_OP_DISCONNECT:
Johan Hedberg88c3df12012-02-09 14:27:38 +02002887 hci_cs_disconnect(hdev, ev->status);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002888 break;
2889
Andrei Emeltchenkoa02226d2012-09-27 17:26:19 +03002890 case HCI_OP_CREATE_PHY_LINK:
2891 hci_cs_create_phylink(hdev, ev->status);
2892 break;
2893
Andrei Emeltchenko0b26ab92012-09-27 17:26:24 +03002894 case HCI_OP_ACCEPT_PHY_LINK:
2895 hci_cs_accept_phylink(hdev, ev->status);
2896 break;
2897
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02002898 case HCI_OP_LE_CREATE_CONN:
2899 hci_cs_le_create_conn(hdev, ev->status);
2900 break;
2901
Johan Hedberg81d0c8a2014-03-24 14:39:04 +02002902 case HCI_OP_LE_START_ENC:
2903 hci_cs_le_start_enc(hdev, ev->status);
2904 break;
2905
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002906 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002907 BT_DBG("%s opcode 0x%4.4x", hdev->name, opcode);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002908 break;
2909 }
2910
Johan Hedbergad82cdd2013-03-09 09:53:50 +02002911 if (opcode != HCI_OP_NOP)
Marcel Holtmann65cc2b42014-06-16 12:30:56 +02002912 cancel_delayed_work(&hdev->cmd_timer);
Ville Tervo6bd32322011-02-16 16:32:41 +02002913
Johan Hedberg02350a72013-04-03 21:50:29 +03002914 if (ev->status ||
2915 (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->req.event))
2916 hci_req_cmd_complete(hdev, opcode, ev->status);
Johan Hedberg9238f362013-03-05 20:37:48 +02002917
Gustavo F. Padovan10572132011-03-16 15:36:29 -03002918 if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags)) {
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002919 atomic_set(&hdev->cmd_cnt, 1);
2920 if (!skb_queue_empty(&hdev->cmd_q))
Gustavo F. Padovanc347b762011-12-14 23:53:47 -02002921 queue_work(hdev->workqueue, &hdev->cmd_work);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002922 }
2923}
2924
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002925static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002926{
2927 struct hci_ev_role_change *ev = (void *) skb->data;
2928 struct hci_conn *conn;
2929
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03002930 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002931
2932 hci_dev_lock(hdev);
2933
2934 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
2935 if (conn) {
Johan Hedberg40bef302014-07-16 11:42:27 +03002936 if (!ev->status)
2937 conn->role = ev->role;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002938
Johan Hedberg51a8efd2012-01-16 06:10:31 +02002939 clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002940
2941 hci_role_switch_cfm(conn, ev->status, ev->role);
2942 }
2943
2944 hci_dev_unlock(hdev);
2945}
2946
Gustavo Padovan6039aa72012-05-23 04:04:18 -03002947static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02002949 struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 int i;
2951
Andrei Emeltchenko32ac5b92011-12-19 16:31:29 +02002952 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
2953 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
2954 return;
2955 }
2956
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02002957 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03002958 ev->num_hndl * sizeof(struct hci_comp_pkts_info)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 BT_DBG("%s bad parameters", hdev->name);
2960 return;
2961 }
2962
Andrei Emeltchenkoc5993de2011-12-30 12:07:47 +02002963 BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
2964
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02002965 for (i = 0; i < ev->num_hndl; i++) {
2966 struct hci_comp_pkts_info *info = &ev->handles[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 struct hci_conn *conn;
2968 __u16 handle, count;
2969
Andrei Emeltchenko613a1c02011-12-19 16:31:30 +02002970 handle = __le16_to_cpu(info->handle);
2971 count = __le16_to_cpu(info->count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
2973 conn = hci_conn_hash_lookup_handle(hdev, handle);
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002974 if (!conn)
2975 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002977 conn->sent -= count;
2978
2979 switch (conn->type) {
2980 case ACL_LINK:
2981 hdev->acl_cnt += count;
2982 if (hdev->acl_cnt > hdev->acl_pkts)
2983 hdev->acl_cnt = hdev->acl_pkts;
2984 break;
2985
2986 case LE_LINK:
2987 if (hdev->le_pkts) {
2988 hdev->le_cnt += count;
2989 if (hdev->le_cnt > hdev->le_pkts)
2990 hdev->le_cnt = hdev->le_pkts;
2991 } else {
Andrei Emeltchenko70f230202010-12-01 16:58:25 +02002992 hdev->acl_cnt += count;
2993 if (hdev->acl_cnt > hdev->acl_pkts)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 hdev->acl_cnt = hdev->acl_pkts;
2995 }
Andrei Emeltchenkof4280912011-12-07 15:56:52 +02002996 break;
2997
2998 case SCO_LINK:
2999 hdev->sco_cnt += count;
3000 if (hdev->sco_cnt > hdev->sco_pkts)
3001 hdev->sco_cnt = hdev->sco_pkts;
3002 break;
3003
3004 default:
3005 BT_ERR("Unknown type %d conn %p", conn->type, conn);
3006 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 }
3008 }
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003009
Gustavo F. Padovan3eff45e2011-12-15 00:50:02 -02003010 queue_work(hdev->workqueue, &hdev->tx_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011}
3012
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003013static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
3014 __u16 handle)
3015{
3016 struct hci_chan *chan;
3017
3018 switch (hdev->dev_type) {
3019 case HCI_BREDR:
3020 return hci_conn_hash_lookup_handle(hdev, handle);
3021 case HCI_AMP:
3022 chan = hci_chan_lookup_handle(hdev, handle);
3023 if (chan)
3024 return chan->conn;
3025 break;
3026 default:
3027 BT_ERR("%s unknown dev_type %d", hdev->name, hdev->dev_type);
3028 break;
3029 }
3030
3031 return NULL;
3032}
3033
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003034static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003035{
3036 struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
3037 int i;
3038
3039 if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
3040 BT_ERR("Wrong event for mode %d", hdev->flow_ctl_mode);
3041 return;
3042 }
3043
3044 if (skb->len < sizeof(*ev) || skb->len < sizeof(*ev) +
Gustavo Padovan807deac2012-05-17 00:36:24 -03003045 ev->num_hndl * sizeof(struct hci_comp_blocks_info)) {
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003046 BT_DBG("%s bad parameters", hdev->name);
3047 return;
3048 }
3049
3050 BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003051 ev->num_hndl);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003052
3053 for (i = 0; i < ev->num_hndl; i++) {
3054 struct hci_comp_blocks_info *info = &ev->handles[i];
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003055 struct hci_conn *conn = NULL;
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003056 __u16 handle, block_count;
3057
3058 handle = __le16_to_cpu(info->handle);
3059 block_count = __le16_to_cpu(info->blocks);
3060
Andrei Emeltchenko76ef7cf2012-10-10 17:38:29 +03003061 conn = __hci_conn_lookup_handle(hdev, handle);
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003062 if (!conn)
3063 continue;
3064
3065 conn->sent -= block_count;
3066
3067 switch (conn->type) {
3068 case ACL_LINK:
Andrei Emeltchenkobd1eb662012-10-10 17:38:30 +03003069 case AMP_LINK:
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02003070 hdev->block_cnt += block_count;
3071 if (hdev->block_cnt > hdev->num_blocks)
3072 hdev->block_cnt = hdev->num_blocks;
3073 break;
3074
3075 default:
3076 BT_ERR("Unknown type %d conn %p", conn->type, conn);
3077 break;
3078 }
3079 }
3080
3081 queue_work(hdev->workqueue, &hdev->tx_work);
3082}
3083
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003084static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003086 struct hci_ev_mode_change *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003087 struct hci_conn *conn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003089 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 hci_dev_lock(hdev);
3092
Marcel Holtmann04837f62006-07-03 10:02:33 +02003093 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3094 if (conn) {
3095 conn->mode = ev->mode;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003096
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003097 if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
3098 &conn->flags)) {
Marcel Holtmann04837f62006-07-03 10:02:33 +02003099 if (conn->mode == HCI_CM_ACTIVE)
Johan Hedberg58a681e2012-01-16 06:47:28 +02003100 set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003101 else
Johan Hedberg58a681e2012-01-16 06:47:28 +02003102 clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003103 }
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003104
Johan Hedberg51a8efd2012-01-16 06:10:31 +02003105 if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
Marcel Holtmanne73439d2010-07-26 10:06:00 -04003106 hci_sco_setup(conn, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003107 }
3108
3109 hci_dev_unlock(hdev);
3110}
3111
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003112static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003114 struct hci_ev_pin_code_req *ev = (void *) skb->data;
3115 struct hci_conn *conn;
3116
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003117 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003118
3119 hci_dev_lock(hdev);
3120
3121 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003122 if (!conn)
3123 goto unlock;
3124
3125 if (conn->state == BT_CONNECTED) {
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003126 hci_conn_hold(conn);
3127 conn->disc_timeout = HCI_PAIRING_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003128 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003129 }
3130
Johan Hedbergb6ae8452014-07-30 09:22:22 +03003131 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags) &&
Johan Hedberg2f407f02014-07-17 15:35:40 +03003132 !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
Johan Hedberg03b555e2011-01-04 15:40:05 +02003133 hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003134 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg2f407f02014-07-17 15:35:40 +03003135 } else if (test_bit(HCI_MGMT, &hdev->dev_flags)) {
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003136 u8 secure;
3137
3138 if (conn->pending_sec_level == BT_SECURITY_HIGH)
3139 secure = 1;
3140 else
3141 secure = 0;
3142
Johan Hedberg744cf192011-11-08 20:40:14 +02003143 mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02003144 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003145
Waldemar Rymarkiewiczb6f98042011-09-23 10:01:30 +02003146unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003147 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003150static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151{
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003152 struct hci_ev_link_key_req *ev = (void *) skb->data;
3153 struct hci_cp_link_key_reply cp;
3154 struct hci_conn *conn;
3155 struct link_key *key;
3156
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003157 BT_DBG("%s", hdev->name);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003158
Andrei Emeltchenko034cbea2013-05-14 11:44:16 +03003159 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003160 return;
3161
3162 hci_dev_lock(hdev);
3163
3164 key = hci_find_link_key(hdev, &ev->bdaddr);
3165 if (!key) {
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003166 BT_DBG("%s link key not found for %pMR", hdev->name,
3167 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003168 goto not_found;
3169 }
3170
Andrei Emeltchenko6ed93dc2012-09-25 12:49:43 +03003171 BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
3172 &ev->bdaddr);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003173
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003174 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003175 if (conn) {
Marcel Holtmann66138ce2014-01-10 02:07:20 -08003176 if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
3177 key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
Gustavo Padovan807deac2012-05-17 00:36:24 -03003178 conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003179 BT_DBG("%s ignoring unauthenticated key", hdev->name);
3180 goto not_found;
3181 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003182
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003183 if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
Johan Hedbergf3fb0b52014-06-02 10:12:44 +03003184 (conn->pending_sec_level == BT_SECURITY_HIGH ||
3185 conn->pending_sec_level == BT_SECURITY_FIPS)) {
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003186 BT_DBG("%s ignoring key unauthenticated for high security",
3187 hdev->name);
Waldemar Rymarkiewicz60b83f52011-04-28 12:07:56 +02003188 goto not_found;
3189 }
3190
3191 conn->key_type = key->type;
3192 conn->pin_length = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003193 }
3194
3195 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03003196 memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003197
3198 hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
3199
3200 hci_dev_unlock(hdev);
3201
3202 return;
3203
3204not_found:
3205 hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
3206 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003209static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210{
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003211 struct hci_ev_link_key_notify *ev = (void *) skb->data;
3212 struct hci_conn *conn;
Johan Hedberg7652ff62014-06-24 13:15:49 +03003213 struct link_key *key;
3214 bool persistent;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003215 u8 pin_len = 0;
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003216
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003217 BT_DBG("%s", hdev->name);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003218
3219 hci_dev_lock(hdev);
3220
3221 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3222 if (conn) {
3223 hci_conn_hold(conn);
3224 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003225 pin_len = conn->pin_length;
Waldemar Rymarkiewicz13d39312011-04-28 12:07:55 +02003226
3227 if (ev->key_type != HCI_LK_CHANGED_COMBINATION)
3228 conn->key_type = ev->key_type;
3229
David Herrmann76a68ba2013-04-06 20:28:37 +02003230 hci_conn_drop(conn);
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003231 }
3232
Johan Hedberg7652ff62014-06-24 13:15:49 +03003233 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
3234 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02003235
Johan Hedberg7652ff62014-06-24 13:15:49 +03003236 key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
3237 ev->key_type, pin_len, &persistent);
3238 if (!key)
3239 goto unlock;
3240
3241 mgmt_new_link_key(hdev, key, persistent);
3242
Johan Hedberg6d5650c2014-06-24 13:15:51 +03003243 /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
3244 * is set. If it's not set simply remove the key from the kernel
3245 * list (we've still notified user space about it but with
3246 * store_hint being 0).
3247 */
3248 if (key->type == HCI_LK_DEBUG_COMBINATION &&
3249 !test_bit(HCI_KEEP_DEBUG_KEYS, &hdev->dev_flags)) {
3250 list_del(&key->list);
3251 kfree(key);
3252 } else if (conn) {
Johan Hedbergaf6a9c32014-06-24 13:15:53 +03003253 if (persistent)
3254 clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
3255 else
3256 set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
Johan Hedberg6d5650c2014-06-24 13:15:51 +03003257 }
Johan Hedberg7652ff62014-06-24 13:15:49 +03003258
3259unlock:
Marcel Holtmann052b30b2009-04-26 20:01:22 +02003260 hci_dev_unlock(hdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003263static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04837f62006-07-03 10:02:33 +02003264{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003265 struct hci_ev_clock_offset *ev = (void *) skb->data;
Marcel Holtmann04837f62006-07-03 10:02:33 +02003266 struct hci_conn *conn;
3267
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003268 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmann04837f62006-07-03 10:02:33 +02003269
3270 hci_dev_lock(hdev);
3271
3272 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 if (conn && !ev->status) {
3274 struct inquiry_entry *ie;
3275
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003276 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3277 if (ie) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 ie->data.clock_offset = ev->clock_offset;
3279 ie->timestamp = jiffies;
3280 }
3281 }
3282
3283 hci_dev_unlock(hdev);
3284}
3285
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003286static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmanna8746412008-07-14 20:13:46 +02003287{
3288 struct hci_ev_pkt_type_change *ev = (void *) skb->data;
3289 struct hci_conn *conn;
3290
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003291 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmanna8746412008-07-14 20:13:46 +02003292
3293 hci_dev_lock(hdev);
3294
3295 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3296 if (conn && !ev->status)
3297 conn->pkt_type = __le16_to_cpu(ev->pkt_type);
3298
3299 hci_dev_unlock(hdev);
3300}
3301
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003302static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003303{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003304 struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003305 struct inquiry_entry *ie;
3306
3307 BT_DBG("%s", hdev->name);
3308
3309 hci_dev_lock(hdev);
3310
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003311 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3312 if (ie) {
Marcel Holtmann85a1e932005-08-09 20:28:02 -07003313 ie->data.pscan_rep_mode = ev->pscan_rep_mode;
3314 ie->timestamp = jiffies;
3315 }
3316
3317 hci_dev_unlock(hdev);
3318}
3319
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003320static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
3321 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003322{
3323 struct inquiry_data data;
3324 int num_rsp = *((__u8 *) skb->data);
3325
3326 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3327
3328 if (!num_rsp)
3329 return;
3330
Andre Guedes1519cc12012-03-21 00:03:38 -03003331 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3332 return;
3333
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003334 hci_dev_lock(hdev);
3335
3336 if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
Szymon Janc138d22e2011-02-17 16:44:23 +01003337 struct inquiry_info_with_rssi_and_pscan_mode *info;
3338 info = (void *) (skb->data + 1);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003339
Johan Hedberge17acd42011-03-30 23:57:16 +03003340 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003341 u32 flags;
3342
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003343 bacpy(&data.bdaddr, &info->bdaddr);
3344 data.pscan_rep_mode = info->pscan_rep_mode;
3345 data.pscan_period_mode = info->pscan_period_mode;
3346 data.pscan_mode = info->pscan_mode;
3347 memcpy(data.dev_class, info->dev_class, 3);
3348 data.clock_offset = info->clock_offset;
3349 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003350 data.ssp_mode = 0x00;
Johan Hedberg31754052012-01-04 13:39:52 +02003351
Marcel Holtmannaf589252014-07-01 14:11:20 +02003352 flags = hci_inquiry_cache_update(hdev, &data, false);
3353
Johan Hedberg48264f02011-11-09 13:58:58 +02003354 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003355 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003356 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003357 }
3358 } else {
3359 struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
3360
Johan Hedberge17acd42011-03-30 23:57:16 +03003361 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003362 u32 flags;
3363
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003364 bacpy(&data.bdaddr, &info->bdaddr);
3365 data.pscan_rep_mode = info->pscan_rep_mode;
3366 data.pscan_period_mode = info->pscan_period_mode;
3367 data.pscan_mode = 0x00;
3368 memcpy(data.dev_class, info->dev_class, 3);
3369 data.clock_offset = info->clock_offset;
3370 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003371 data.ssp_mode = 0x00;
Marcel Holtmannaf589252014-07-01 14:11:20 +02003372
3373 flags = hci_inquiry_cache_update(hdev, &data, false);
3374
Johan Hedberg48264f02011-11-09 13:58:58 +02003375 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003376 info->dev_class, info->rssi,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003377 flags, NULL, 0, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003378 }
3379 }
3380
3381 hci_dev_unlock(hdev);
3382}
3383
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003384static void hci_remote_ext_features_evt(struct hci_dev *hdev,
3385 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003386{
Marcel Holtmann41a96212008-07-14 20:13:48 +02003387 struct hci_ev_remote_ext_features *ev = (void *) skb->data;
3388 struct hci_conn *conn;
3389
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003390 BT_DBG("%s", hdev->name);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003391
Marcel Holtmann41a96212008-07-14 20:13:48 +02003392 hci_dev_lock(hdev);
3393
3394 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Johan Hedbergccd556f2010-11-10 17:11:51 +02003395 if (!conn)
3396 goto unlock;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003397
Johan Hedbergcad718e2013-04-17 15:00:51 +03003398 if (ev->page < HCI_MAX_PAGES)
3399 memcpy(conn->features[ev->page], ev->features, 8);
3400
Johan Hedbergccd556f2010-11-10 17:11:51 +02003401 if (!ev->status && ev->page == 0x01) {
3402 struct inquiry_entry *ie;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003403
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003404 ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
3405 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02003406 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann769be972008-07-14 20:13:49 +02003407
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05303408 if (ev->features[0] & LMP_HOST_SSP) {
Johan Hedberg58a681e2012-01-16 06:47:28 +02003409 set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
Jaganath Kanakkasserybbb0ead2013-04-16 20:16:30 +05303410 } else {
3411 /* It is mandatory by the Bluetooth specification that
3412 * Extended Inquiry Results are only used when Secure
3413 * Simple Pairing is enabled, but some devices violate
3414 * this.
3415 *
3416 * To make these devices work, the internal SSP
3417 * enabled flag needs to be cleared if the remote host
3418 * features do not indicate SSP support */
3419 clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
3420 }
Marcel Holtmanneb9a8f32014-01-15 22:37:38 -08003421
3422 if (ev->features[0] & LMP_HOST_SC)
3423 set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003424 }
3425
Johan Hedbergccd556f2010-11-10 17:11:51 +02003426 if (conn->state != BT_CONFIG)
3427 goto unlock;
3428
Johan Hedberg671267b2012-05-12 16:11:50 -03003429 if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
Johan Hedberg127178d2010-11-18 22:22:29 +02003430 struct hci_cp_remote_name_req cp;
3431 memset(&cp, 0, sizeof(cp));
3432 bacpy(&cp.bdaddr, &conn->dst);
3433 cp.pscan_rep_mode = 0x02;
3434 hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
Johan Hedbergb644ba32012-01-17 21:48:47 +02003435 } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00003436 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Johan Hedberg392599b2010-11-18 22:22:28 +02003437
Johan Hedberg127178d2010-11-18 22:22:29 +02003438 if (!hci_outgoing_auth_needed(hdev, conn)) {
Johan Hedbergccd556f2010-11-10 17:11:51 +02003439 conn->state = BT_CONNECTED;
3440 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003441 hci_conn_drop(conn);
Johan Hedbergccd556f2010-11-10 17:11:51 +02003442 }
3443
3444unlock:
Marcel Holtmann41a96212008-07-14 20:13:48 +02003445 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003446}
3447
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003448static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
3449 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003450{
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003451 struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
3452 struct hci_conn *conn;
3453
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003454 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003455
3456 hci_dev_lock(hdev);
3457
3458 conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
Marcel Holtmann9dc0a3a2008-07-14 20:13:46 +02003459 if (!conn) {
3460 if (ev->link_type == ESCO_LINK)
3461 goto unlock;
3462
3463 conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
3464 if (!conn)
3465 goto unlock;
3466
3467 conn->type = SCO_LINK;
3468 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003469
Marcel Holtmann732547f2009-04-19 19:14:14 +02003470 switch (ev->status) {
3471 case 0x00:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003472 conn->handle = __le16_to_cpu(ev->handle);
3473 conn->state = BT_CONNECTED;
Marcel Holtmann7d0db0a2008-07-14 20:13:51 +02003474
3475 hci_conn_add_sysfs(conn);
Marcel Holtmann732547f2009-04-19 19:14:14 +02003476 break;
3477
Nick Pelly81218d22014-06-30 11:25:01 +05303478 case 0x10: /* Connection Accept Timeout */
Frédéric Dalleau1a4c9582013-08-19 14:24:02 +02003479 case 0x0d: /* Connection Rejected due to Limited Resources */
Stephen Coe705e5712010-02-16 11:29:44 -05003480 case 0x11: /* Unsupported Feature or Parameter Value */
Marcel Holtmann732547f2009-04-19 19:14:14 +02003481 case 0x1c: /* SCO interval rejected */
Nick Pelly1038a002010-02-03 11:42:26 -08003482 case 0x1a: /* Unsupported Remote Feature */
Marcel Holtmann732547f2009-04-19 19:14:14 +02003483 case 0x1f: /* Unspecified error */
Andrew Earl27539bc2014-03-10 10:31:04 +00003484 case 0x20: /* Unsupported LMP Parameter value */
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02003485 if (conn->out) {
Marcel Holtmann732547f2009-04-19 19:14:14 +02003486 conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
3487 (hdev->esco_type & EDR_ESCO_MASK);
Frédéric Dalleau2dea6322013-08-19 14:24:03 +02003488 if (hci_setup_sync(conn, conn->link->handle))
3489 goto unlock;
Marcel Holtmann732547f2009-04-19 19:14:14 +02003490 }
3491 /* fall through */
3492
3493 default:
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003494 conn->state = BT_CLOSED;
Marcel Holtmann732547f2009-04-19 19:14:14 +02003495 break;
3496 }
Marcel Holtmannb6a0dc82007-10-20 14:55:10 +02003497
3498 hci_proto_connect_cfm(conn, ev->status);
3499 if (ev->status)
3500 hci_conn_del(conn);
3501
3502unlock:
3503 hci_dev_unlock(hdev);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003504}
3505
Marcel Holtmannefdcf8e2013-10-15 10:31:12 -07003506static inline size_t eir_get_length(u8 *eir, size_t eir_len)
3507{
3508 size_t parsed = 0;
3509
3510 while (parsed < eir_len) {
3511 u8 field_len = eir[0];
3512
3513 if (field_len == 0)
3514 return parsed;
3515
3516 parsed += field_len + 1;
3517 eir += field_len + 1;
3518 }
3519
3520 return eir_len;
3521}
3522
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003523static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
3524 struct sk_buff *skb)
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003525{
3526 struct inquiry_data data;
3527 struct extended_inquiry_info *info = (void *) (skb->data + 1);
3528 int num_rsp = *((__u8 *) skb->data);
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303529 size_t eir_len;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003530
3531 BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
3532
3533 if (!num_rsp)
3534 return;
3535
Andre Guedes1519cc12012-03-21 00:03:38 -03003536 if (test_bit(HCI_PERIODIC_INQ, &hdev->dev_flags))
3537 return;
3538
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003539 hci_dev_lock(hdev);
3540
Johan Hedberge17acd42011-03-30 23:57:16 +03003541 for (; num_rsp; num_rsp--, info++) {
Marcel Holtmannaf589252014-07-01 14:11:20 +02003542 u32 flags;
3543 bool name_known;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003544
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003545 bacpy(&data.bdaddr, &info->bdaddr);
Szymon Janc138d22e2011-02-17 16:44:23 +01003546 data.pscan_rep_mode = info->pscan_rep_mode;
3547 data.pscan_period_mode = info->pscan_period_mode;
3548 data.pscan_mode = 0x00;
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003549 memcpy(data.dev_class, info->dev_class, 3);
Szymon Janc138d22e2011-02-17 16:44:23 +01003550 data.clock_offset = info->clock_offset;
3551 data.rssi = info->rssi;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003552 data.ssp_mode = 0x01;
Johan Hedberg561aafb2012-01-04 13:31:59 +02003553
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003554 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg4ddb1932012-01-15 20:04:43 +02003555 name_known = eir_has_data_type(info->data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003556 sizeof(info->data),
3557 EIR_NAME_COMPLETE);
Johan Hedberg561aafb2012-01-04 13:31:59 +02003558 else
3559 name_known = true;
3560
Marcel Holtmannaf589252014-07-01 14:11:20 +02003561 flags = hci_inquiry_cache_update(hdev, &data, name_known);
3562
Vishal Agarwal9d939d92012-04-26 19:19:56 +05303563 eir_len = eir_get_length(info->data, sizeof(info->data));
Marcel Holtmannaf589252014-07-01 14:11:20 +02003564
Johan Hedberg48264f02011-11-09 13:58:58 +02003565 mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
Marcel Holtmannaf589252014-07-01 14:11:20 +02003566 info->dev_class, info->rssi,
3567 flags, info->data, eir_len, NULL, 0);
Marcel Holtmanna9de9242007-10-20 13:33:56 +02003568 }
3569
3570 hci_dev_unlock(hdev);
3571}
3572
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003573static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
3574 struct sk_buff *skb)
3575{
3576 struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
3577 struct hci_conn *conn;
3578
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03003579 BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003580 __le16_to_cpu(ev->handle));
3581
3582 hci_dev_lock(hdev);
3583
3584 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
3585 if (!conn)
3586 goto unlock;
3587
Johan Hedberg9eb1fbf2014-04-11 12:02:31 -07003588 /* For BR/EDR the necessary steps are taken through the
3589 * auth_complete event.
3590 */
3591 if (conn->type != LE_LINK)
3592 goto unlock;
3593
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003594 if (!ev->status)
3595 conn->sec_level = conn->pending_sec_level;
3596
3597 clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
3598
3599 if (ev->status && conn->state == BT_CONNECTED) {
Andre Guedesbed71742013-01-30 11:50:56 -03003600 hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
David Herrmann76a68ba2013-04-06 20:28:37 +02003601 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003602 goto unlock;
3603 }
3604
3605 if (conn->state == BT_CONFIG) {
3606 if (!ev->status)
3607 conn->state = BT_CONNECTED;
3608
3609 hci_proto_connect_cfm(conn, ev->status);
David Herrmann76a68ba2013-04-06 20:28:37 +02003610 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003611 } else {
3612 hci_auth_cfm(conn, ev->status);
3613
3614 hci_conn_hold(conn);
3615 conn->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02003616 hci_conn_drop(conn);
Johan Hedberg1c2e0042012-06-08 23:31:13 +08003617 }
3618
3619unlock:
3620 hci_dev_unlock(hdev);
3621}
3622
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003623static u8 hci_get_auth_req(struct hci_conn *conn)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003624{
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003625 /* If remote requests no-bonding follow that lead */
Mikel Astizacabae92013-06-28 10:56:28 +02003626 if (conn->remote_auth == HCI_AT_NO_BONDING ||
3627 conn->remote_auth == HCI_AT_NO_BONDING_MITM)
Waldemar Rymarkiewicz58797bf2011-04-28 12:07:58 +02003628 return conn->remote_auth | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003629
Mikel Astizb7f94c82014-04-08 14:21:31 +02003630 /* If both remote and local have enough IO capabilities, require
3631 * MITM protection
3632 */
3633 if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
3634 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
3635 return conn->remote_auth | 0x01;
3636
Timo Mueller7e741702014-04-08 14:21:33 +02003637 /* No MITM protection possible so ignore remote requirement */
3638 return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003639}
3640
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003641static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003642{
3643 struct hci_ev_io_capa_request *ev = (void *) skb->data;
3644 struct hci_conn *conn;
3645
3646 BT_DBG("%s", hdev->name);
3647
3648 hci_dev_lock(hdev);
3649
3650 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003651 if (!conn)
3652 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003653
Johan Hedberg03b555e2011-01-04 15:40:05 +02003654 hci_conn_hold(conn);
3655
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003656 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg03b555e2011-01-04 15:40:05 +02003657 goto unlock;
3658
Johan Hedberg2f407f02014-07-17 15:35:40 +03003659 /* Allow pairing if we're pairable, the initiators of the
3660 * pairing or if the remote is not requesting bonding.
3661 */
Johan Hedbergb6ae8452014-07-30 09:22:22 +03003662 if (test_bit(HCI_BONDABLE, &hdev->dev_flags) ||
Johan Hedberg2f407f02014-07-17 15:35:40 +03003663 test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
Gustavo Padovan807deac2012-05-17 00:36:24 -03003664 (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003665 struct hci_cp_io_capability_reply cp;
3666
3667 bacpy(&cp.bdaddr, &ev->bdaddr);
Hemant Gupta7a7f1e72012-01-16 13:34:29 +05303668 /* Change the IO capability from KeyboardDisplay
3669 * to DisplayYesNo as it is not supported by BT spec. */
3670 cp.capability = (conn->io_capability == 0x04) ?
Mikel Astiza7676312013-06-28 10:56:29 +02003671 HCI_IO_DISPLAY_YESNO : conn->io_capability;
Mikel Astizb7f94c82014-04-08 14:21:31 +02003672
3673 /* If we are initiators, there is no remote information yet */
3674 if (conn->remote_auth == 0xff) {
Mikel Astizb16c6602014-04-08 14:21:34 +02003675 /* Request MITM protection if our IO caps allow it
Johan Hedberg4ad51a72014-06-09 14:41:25 +03003676 * except for the no-bonding case.
Mikel Astizb16c6602014-04-08 14:21:34 +02003677 */
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003678 if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedberg9f743d72014-07-17 11:56:33 +03003679 conn->auth_type != HCI_AT_NO_BONDING)
Johan Hedberg6c538232014-07-11 15:32:23 +03003680 conn->auth_type |= 0x01;
Mikel Astizb7f94c82014-04-08 14:21:31 +02003681 } else {
3682 conn->auth_type = hci_get_auth_req(conn);
Mikel Astizb7f94c82014-04-08 14:21:31 +02003683 }
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003684
Johan Hedberg82c295b2014-07-30 09:22:24 +03003685 /* If we're not bondable, force one of the non-bondable
3686 * authentication requirement values.
3687 */
3688 if (!test_bit(HCI_BONDABLE, &hdev->dev_flags))
3689 conn->auth_type &= HCI_AT_NO_BONDING_MITM;
3690
3691 cp.authentication = conn->auth_type;
3692
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03003693 if (hci_find_remote_oob_data(hdev, &conn->dst) &&
3694 (conn->out || test_bit(HCI_CONN_REMOTE_OOB, &conn->flags)))
Szymon Jancce85ee12011-03-22 13:12:23 +01003695 cp.oob_data = 0x01;
3696 else
3697 cp.oob_data = 0x00;
3698
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003699 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003700 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003701 } else {
3702 struct hci_cp_io_capability_neg_reply cp;
3703
3704 bacpy(&cp.bdaddr, &ev->bdaddr);
Andrei Emeltchenko9f5a0d72011-11-07 14:20:25 +02003705 cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003706
3707 hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003708 sizeof(cp), &cp);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003709 }
3710
3711unlock:
3712 hci_dev_unlock(hdev);
3713}
3714
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003715static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
Johan Hedberg03b555e2011-01-04 15:40:05 +02003716{
3717 struct hci_ev_io_capa_reply *ev = (void *) skb->data;
3718 struct hci_conn *conn;
3719
3720 BT_DBG("%s", hdev->name);
3721
3722 hci_dev_lock(hdev);
3723
3724 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3725 if (!conn)
3726 goto unlock;
3727
Johan Hedberg03b555e2011-01-04 15:40:05 +02003728 conn->remote_cap = ev->capability;
Johan Hedberg03b555e2011-01-04 15:40:05 +02003729 conn->remote_auth = ev->authentication;
Johan Hedberg58a681e2012-01-16 06:47:28 +02003730 if (ev->oob_data)
3731 set_bit(HCI_CONN_REMOTE_OOB, &conn->flags);
Johan Hedberg03b555e2011-01-04 15:40:05 +02003732
3733unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003734 hci_dev_unlock(hdev);
3735}
3736
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003737static void hci_user_confirm_request_evt(struct hci_dev *hdev,
3738 struct sk_buff *skb)
Johan Hedberga5c29682011-02-19 12:05:57 -03003739{
3740 struct hci_ev_user_confirm_req *ev = (void *) skb->data;
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003741 int loc_mitm, rem_mitm, confirm_hint = 0;
Johan Hedberg7a828902011-04-28 11:28:53 -07003742 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003743
3744 BT_DBG("%s", hdev->name);
3745
3746 hci_dev_lock(hdev);
3747
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003748 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg7a828902011-04-28 11:28:53 -07003749 goto unlock;
Johan Hedberga5c29682011-02-19 12:05:57 -03003750
Johan Hedberg7a828902011-04-28 11:28:53 -07003751 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3752 if (!conn)
3753 goto unlock;
3754
3755 loc_mitm = (conn->auth_type & 0x01);
3756 rem_mitm = (conn->remote_auth & 0x01);
3757
3758 /* If we require MITM but the remote device can't provide that
Johan Hedberg6c538232014-07-11 15:32:23 +03003759 * (it has NoInputNoOutput) then reject the confirmation
3760 * request. We check the security level here since it doesn't
3761 * necessarily match conn->auth_type.
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003762 */
Johan Hedberg6c538232014-07-11 15:32:23 +03003763 if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
3764 conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
Johan Hedberg7a828902011-04-28 11:28:53 -07003765 BT_DBG("Rejecting request: remote device can't provide MITM");
3766 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003767 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003768 goto unlock;
3769 }
3770
3771 /* If no side requires MITM protection; auto-accept */
Mikel Astiza7676312013-06-28 10:56:29 +02003772 if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
3773 (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003774
3775 /* If we're not the initiators request authorization to
3776 * proceed from user space (mgmt_user_confirm with
Johan Hedbergba15a582014-06-09 13:58:14 +03003777 * confirm_hint set to 1). The exception is if neither
Johan Hedberg02f3e252014-07-16 15:09:13 +03003778 * side had MITM or if the local IO capability is
3779 * NoInputNoOutput, in which case we do auto-accept
Johan Hedbergba15a582014-06-09 13:58:14 +03003780 */
3781 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
Johan Hedberg02f3e252014-07-16 15:09:13 +03003782 conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
Johan Hedbergba15a582014-06-09 13:58:14 +03003783 (loc_mitm || rem_mitm)) {
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003784 BT_DBG("Confirming auto-accept as acceptor");
3785 confirm_hint = 1;
3786 goto confirm;
3787 }
3788
Johan Hedberg9f616562011-04-28 11:28:54 -07003789 BT_DBG("Auto-accept of user confirmation with %ums delay",
Gustavo Padovan807deac2012-05-17 00:36:24 -03003790 hdev->auto_accept_delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003791
3792 if (hdev->auto_accept_delay > 0) {
3793 int delay = msecs_to_jiffies(hdev->auto_accept_delay);
Johan Hedberg7bc18d92013-10-16 18:11:39 +03003794 queue_delayed_work(conn->hdev->workqueue,
3795 &conn->auto_accept_work, delay);
Johan Hedberg9f616562011-04-28 11:28:54 -07003796 goto unlock;
3797 }
3798
Johan Hedberg7a828902011-04-28 11:28:53 -07003799 hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
Gustavo Padovan807deac2012-05-17 00:36:24 -03003800 sizeof(ev->bdaddr), &ev->bdaddr);
Johan Hedberg7a828902011-04-28 11:28:53 -07003801 goto unlock;
3802 }
3803
Johan Hedberg55bc1a32011-04-28 11:28:56 -07003804confirm:
Johan Hedberg39adbff2014-03-20 08:18:14 +02003805 mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
3806 le32_to_cpu(ev->passkey), confirm_hint);
Johan Hedberg7a828902011-04-28 11:28:53 -07003807
3808unlock:
Johan Hedberga5c29682011-02-19 12:05:57 -03003809 hci_dev_unlock(hdev);
3810}
3811
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003812static void hci_user_passkey_request_evt(struct hci_dev *hdev,
3813 struct sk_buff *skb)
Brian Gix1143d452011-11-23 08:28:34 -08003814{
3815 struct hci_ev_user_passkey_req *ev = (void *) skb->data;
3816
3817 BT_DBG("%s", hdev->name);
3818
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003819 if (test_bit(HCI_MGMT, &hdev->dev_flags))
Johan Hedberg272d90d2012-02-09 15:26:12 +02003820 mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
Brian Gix1143d452011-11-23 08:28:34 -08003821}
3822
Johan Hedberg92a25252012-09-06 18:39:26 +03003823static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
3824 struct sk_buff *skb)
3825{
3826 struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
3827 struct hci_conn *conn;
3828
3829 BT_DBG("%s", hdev->name);
3830
3831 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3832 if (!conn)
3833 return;
3834
3835 conn->passkey_notify = __le32_to_cpu(ev->passkey);
3836 conn->passkey_entered = 0;
3837
3838 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3839 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3840 conn->dst_type, conn->passkey_notify,
3841 conn->passkey_entered);
3842}
3843
3844static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
3845{
3846 struct hci_ev_keypress_notify *ev = (void *) skb->data;
3847 struct hci_conn *conn;
3848
3849 BT_DBG("%s", hdev->name);
3850
3851 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3852 if (!conn)
3853 return;
3854
3855 switch (ev->type) {
3856 case HCI_KEYPRESS_STARTED:
3857 conn->passkey_entered = 0;
3858 return;
3859
3860 case HCI_KEYPRESS_ENTERED:
3861 conn->passkey_entered++;
3862 break;
3863
3864 case HCI_KEYPRESS_ERASED:
3865 conn->passkey_entered--;
3866 break;
3867
3868 case HCI_KEYPRESS_CLEARED:
3869 conn->passkey_entered = 0;
3870 break;
3871
3872 case HCI_KEYPRESS_COMPLETED:
3873 return;
3874 }
3875
3876 if (test_bit(HCI_MGMT, &hdev->dev_flags))
3877 mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
3878 conn->dst_type, conn->passkey_notify,
3879 conn->passkey_entered);
3880}
3881
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003882static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
3883 struct sk_buff *skb)
Marcel Holtmann04936842008-07-14 20:13:48 +02003884{
3885 struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
3886 struct hci_conn *conn;
3887
3888 BT_DBG("%s", hdev->name);
3889
3890 hci_dev_lock(hdev);
3891
3892 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
Johan Hedberg2a611692011-02-19 12:06:00 -03003893 if (!conn)
3894 goto unlock;
Marcel Holtmann04936842008-07-14 20:13:48 +02003895
Johan Hedbergc1d4fa72014-07-17 15:14:50 +03003896 /* Reset the authentication requirement to unknown */
3897 conn->remote_auth = 0xff;
3898
Johan Hedberg2a611692011-02-19 12:06:00 -03003899 /* To avoid duplicate auth_failed events to user space we check
3900 * the HCI_CONN_AUTH_PEND flag which will be set if we
3901 * initiated the authentication. A traditional auth_complete
3902 * event gets always produced as initiator and is also mapped to
3903 * the mgmt_auth_failed event */
Mikel Astizfa1bd912012-08-09 09:52:29 +02003904 if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
Johan Hedberge1e930f2014-09-08 17:09:49 -07003905 mgmt_auth_failed(conn, ev->status);
Johan Hedberg2a611692011-02-19 12:06:00 -03003906
David Herrmann76a68ba2013-04-06 20:28:37 +02003907 hci_conn_drop(conn);
Johan Hedberg2a611692011-02-19 12:06:00 -03003908
3909unlock:
Marcel Holtmann04936842008-07-14 20:13:48 +02003910 hci_dev_unlock(hdev);
3911}
3912
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003913static void hci_remote_host_features_evt(struct hci_dev *hdev,
3914 struct sk_buff *skb)
Marcel Holtmann41a96212008-07-14 20:13:48 +02003915{
3916 struct hci_ev_remote_host_features *ev = (void *) skb->data;
3917 struct inquiry_entry *ie;
Johan Hedbergcad718e2013-04-17 15:00:51 +03003918 struct hci_conn *conn;
Marcel Holtmann41a96212008-07-14 20:13:48 +02003919
3920 BT_DBG("%s", hdev->name);
3921
3922 hci_dev_lock(hdev);
3923
Johan Hedbergcad718e2013-04-17 15:00:51 +03003924 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
3925 if (conn)
3926 memcpy(conn->features[1], ev->features, 8);
3927
Andrei Emeltchenkocc11b9c2010-11-22 13:21:37 +02003928 ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
3929 if (ie)
Johan Hedberg02b7cc62012-02-28 02:28:43 +02003930 ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
Marcel Holtmann41a96212008-07-14 20:13:48 +02003931
3932 hci_dev_unlock(hdev);
3933}
3934
Gustavo Padovan6039aa72012-05-23 04:04:18 -03003935static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
3936 struct sk_buff *skb)
Szymon Janc2763eda2011-03-22 13:12:22 +01003937{
3938 struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
3939 struct oob_data *data;
3940
3941 BT_DBG("%s", hdev->name);
3942
3943 hci_dev_lock(hdev);
3944
Johan Hedberga8b2d5c2012-01-08 23:11:15 +02003945 if (!test_bit(HCI_MGMT, &hdev->dev_flags))
Szymon Jance1ba1f12011-04-06 13:01:59 +02003946 goto unlock;
3947
Szymon Janc2763eda2011-03-22 13:12:22 +01003948 data = hci_find_remote_oob_data(hdev, &ev->bdaddr);
3949 if (data) {
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003950 if (test_bit(HCI_SC_ENABLED, &hdev->dev_flags)) {
3951 struct hci_cp_remote_oob_ext_data_reply cp;
Szymon Janc2763eda2011-03-22 13:12:22 +01003952
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003953 bacpy(&cp.bdaddr, &ev->bdaddr);
3954 memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
3955 memcpy(cp.randomizer192, data->randomizer192,
3956 sizeof(cp.randomizer192));
3957 memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
3958 memcpy(cp.randomizer256, data->randomizer256,
3959 sizeof(cp.randomizer256));
Szymon Janc2763eda2011-03-22 13:12:22 +01003960
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003961 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
3962 sizeof(cp), &cp);
3963 } else {
3964 struct hci_cp_remote_oob_data_reply cp;
3965
3966 bacpy(&cp.bdaddr, &ev->bdaddr);
3967 memcpy(cp.hash, data->hash192, sizeof(cp.hash));
3968 memcpy(cp.randomizer, data->randomizer192,
3969 sizeof(cp.randomizer));
3970
3971 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
3972 sizeof(cp), &cp);
3973 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003974 } else {
3975 struct hci_cp_remote_oob_data_neg_reply cp;
3976
3977 bacpy(&cp.bdaddr, &ev->bdaddr);
Marcel Holtmann519ca9d2014-01-10 02:07:28 -08003978 hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
3979 sizeof(cp), &cp);
Szymon Janc2763eda2011-03-22 13:12:22 +01003980 }
3981
Szymon Jance1ba1f12011-04-06 13:01:59 +02003982unlock:
Szymon Janc2763eda2011-03-22 13:12:22 +01003983 hci_dev_unlock(hdev);
3984}
3985
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03003986static void hci_phy_link_complete_evt(struct hci_dev *hdev,
3987 struct sk_buff *skb)
3988{
3989 struct hci_ev_phy_link_complete *ev = (void *) skb->data;
3990 struct hci_conn *hcon, *bredr_hcon;
3991
3992 BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
3993 ev->status);
3994
3995 hci_dev_lock(hdev);
3996
3997 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
3998 if (!hcon) {
3999 hci_dev_unlock(hdev);
4000 return;
4001 }
4002
4003 if (ev->status) {
4004 hci_conn_del(hcon);
4005 hci_dev_unlock(hdev);
4006 return;
4007 }
4008
4009 bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
4010
4011 hcon->state = BT_CONNECTED;
4012 bacpy(&hcon->dst, &bredr_hcon->dst);
4013
4014 hci_conn_hold(hcon);
4015 hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
David Herrmann76a68ba2013-04-06 20:28:37 +02004016 hci_conn_drop(hcon);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004017
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004018 hci_conn_add_sysfs(hcon);
4019
Andrei Emeltchenkocf70ff22012-10-31 15:46:36 +02004020 amp_physical_cfm(bredr_hcon, hcon);
4021
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004022 hci_dev_unlock(hdev);
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004023}
4024
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004025static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
4026{
4027 struct hci_ev_logical_link_complete *ev = (void *) skb->data;
4028 struct hci_conn *hcon;
4029 struct hci_chan *hchan;
4030 struct amp_mgr *mgr;
4031
4032 BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
4033 hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
4034 ev->status);
4035
4036 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4037 if (!hcon)
4038 return;
4039
4040 /* Create AMP hchan */
4041 hchan = hci_chan_create(hcon);
4042 if (!hchan)
4043 return;
4044
4045 hchan->handle = le16_to_cpu(ev->handle);
4046
4047 BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
4048
4049 mgr = hcon->amp_mgr;
4050 if (mgr && mgr->bredr_chan) {
4051 struct l2cap_chan *bredr_chan = mgr->bredr_chan;
4052
4053 l2cap_chan_lock(bredr_chan);
4054
4055 bredr_chan->conn->mtu = hdev->block_mtu;
4056 l2cap_logical_cfm(bredr_chan, hchan, 0);
4057 hci_conn_hold(hcon);
4058
4059 l2cap_chan_unlock(bredr_chan);
4060 }
4061}
4062
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02004063static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
4064 struct sk_buff *skb)
4065{
4066 struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
4067 struct hci_chan *hchan;
4068
4069 BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
4070 le16_to_cpu(ev->handle), ev->status);
4071
4072 if (ev->status)
4073 return;
4074
4075 hci_dev_lock(hdev);
4076
4077 hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
4078 if (!hchan)
4079 goto unlock;
4080
4081 amp_destroy_logical_link(hchan, ev->reason);
4082
4083unlock:
4084 hci_dev_unlock(hdev);
4085}
4086
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02004087static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
4088 struct sk_buff *skb)
4089{
4090 struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
4091 struct hci_conn *hcon;
4092
4093 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4094
4095 if (ev->status)
4096 return;
4097
4098 hci_dev_lock(hdev);
4099
4100 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4101 if (hcon) {
4102 hcon->state = BT_CLOSED;
4103 hci_conn_del(hcon);
4104 }
4105
4106 hci_dev_unlock(hdev);
4107}
4108
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004109static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03004110{
4111 struct hci_ev_le_conn_complete *ev = (void *) skb->data;
Johan Hedberg912b42e2014-07-03 19:33:49 +03004112 struct hci_conn_params *params;
Ville Tervofcd89c02011-02-10 22:38:47 -03004113 struct hci_conn *conn;
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02004114 struct smp_irk *irk;
Johan Hedberg837d5022014-07-02 09:36:22 +03004115 u8 addr_type;
Ville Tervofcd89c02011-02-10 22:38:47 -03004116
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004117 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
Ville Tervofcd89c02011-02-10 22:38:47 -03004118
4119 hci_dev_lock(hdev);
4120
Johan Hedbergfbd96c12014-07-08 17:21:51 +03004121 /* All controllers implicitly stop advertising in the event of a
4122 * connection, so ensure that the state bit is cleared.
4123 */
4124 clear_bit(HCI_LE_ADV, &hdev->dev_flags);
4125
Andre Guedesb47a09b2012-07-27 15:10:15 -03004126 conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
Ville Tervob62f3282011-02-10 22:38:50 -03004127 if (!conn) {
Johan Hedberga5c4e302014-07-16 11:56:07 +03004128 conn = hci_conn_add(hdev, LE_LINK, &ev->bdaddr, ev->role);
Ville Tervob62f3282011-02-10 22:38:50 -03004129 if (!conn) {
4130 BT_ERR("No memory for new connection");
Andre Guedes230fd162012-07-27 15:10:10 -03004131 goto unlock;
Ville Tervob62f3282011-02-10 22:38:50 -03004132 }
Andre Guedes29b79882011-05-31 14:20:54 -03004133
4134 conn->dst_type = ev->bdaddr_type;
Andre Guedesb9b343d2012-07-27 15:10:11 -03004135
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02004136 /* If we didn't have a hci_conn object previously
4137 * but we're in master role this must be something
4138 * initiated using a white list. Since white list based
4139 * connections are not "first class citizens" we don't
4140 * have full tracking of them. Therefore, we go ahead
4141 * with a "best effort" approach of determining the
4142 * initiator address based on the HCI_PRIVACY flag.
4143 */
4144 if (conn->out) {
4145 conn->resp_addr_type = ev->bdaddr_type;
4146 bacpy(&conn->resp_addr, &ev->bdaddr);
4147 if (test_bit(HCI_PRIVACY, &hdev->dev_flags)) {
4148 conn->init_addr_type = ADDR_LE_DEV_RANDOM;
4149 bacpy(&conn->init_addr, &hdev->rpa);
4150 } else {
4151 hci_copy_identity_address(hdev,
4152 &conn->init_addr,
4153 &conn->init_addr_type);
4154 }
Johan Hedbergcb1d68f2014-02-28 12:54:16 +02004155 }
Johan Hedberg9489eca2014-02-28 17:45:46 +02004156 } else {
4157 cancel_delayed_work(&conn->le_conn_timeout);
Ville Tervob62f3282011-02-10 22:38:50 -03004158 }
Ville Tervofcd89c02011-02-10 22:38:47 -03004159
Johan Hedberg80c24ab2014-03-24 20:21:51 +02004160 if (!conn->out) {
4161 /* Set the responder (our side) address type based on
4162 * the advertising address type.
4163 */
4164 conn->resp_addr_type = hdev->adv_addr_type;
4165 if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM)
4166 bacpy(&conn->resp_addr, &hdev->random_addr);
4167 else
4168 bacpy(&conn->resp_addr, &hdev->bdaddr);
4169
4170 conn->init_addr_type = ev->bdaddr_type;
4171 bacpy(&conn->init_addr, &ev->bdaddr);
Marcel Holtmanna720d732014-06-23 12:14:51 +02004172
4173 /* For incoming connections, set the default minimum
4174 * and maximum connection interval. They will be used
4175 * to check if the parameters are in range and if not
4176 * trigger the connection update procedure.
4177 */
4178 conn->le_conn_min_interval = hdev->le_conn_min_interval;
4179 conn->le_conn_max_interval = hdev->le_conn_max_interval;
Johan Hedberg80c24ab2014-03-24 20:21:51 +02004180 }
Johan Hedberg7be2edb2014-02-23 19:42:17 +02004181
Marcel Holtmannedb4b462014-02-18 15:13:43 -08004182 /* Lookup the identity address from the stored connection
4183 * address and address type.
4184 *
4185 * When establishing connections to an identity address, the
4186 * connection procedure will store the resolvable random
4187 * address first. Now if it can be converted back into the
4188 * identity address, start using the identity address from
4189 * now on.
4190 */
4191 irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
Johan Hedberg68d6f6d2014-02-18 21:41:32 +02004192 if (irk) {
4193 bacpy(&conn->dst, &irk->bdaddr);
4194 conn->dst_type = irk->addr_type;
4195 }
4196
Johan Hedberg2d3c2262014-07-15 11:51:28 +03004197 if (ev->status) {
4198 hci_le_conn_failed(conn, ev->status);
Johan Hedberg837d5022014-07-02 09:36:22 +03004199 goto unlock;
4200 }
4201
Johan Hedberg08853f12014-08-15 21:06:55 +03004202 if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
4203 addr_type = BDADDR_LE_PUBLIC;
4204 else
4205 addr_type = BDADDR_LE_RANDOM;
4206
Johan Hedberg2d3c2262014-07-15 11:51:28 +03004207 /* Drop the connection if the device is blocked */
4208 if (hci_bdaddr_list_lookup(&hdev->blacklist, &conn->dst, addr_type)) {
4209 hci_conn_drop(conn);
Andre Guedescd17dec2012-07-27 15:10:16 -03004210 goto unlock;
4211 }
4212
Johan Hedbergb644ba32012-01-17 21:48:47 +02004213 if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00004214 mgmt_device_connected(hdev, conn, 0, NULL, 0);
Vinicius Costa Gomes83bc71b2011-05-06 18:41:43 -03004215
Vinicius Costa Gomes7b5c0d52011-06-09 18:50:50 -03004216 conn->sec_level = BT_SECURITY_LOW;
Ville Tervofcd89c02011-02-10 22:38:47 -03004217 conn->handle = __le16_to_cpu(ev->handle);
4218 conn->state = BT_CONNECTED;
4219
Marcel Holtmanne04fde62014-06-23 11:40:04 +02004220 conn->le_conn_interval = le16_to_cpu(ev->interval);
4221 conn->le_conn_latency = le16_to_cpu(ev->latency);
4222 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
4223
Ville Tervofcd89c02011-02-10 22:38:47 -03004224 hci_conn_add_sysfs(conn);
4225
4226 hci_proto_connect_cfm(conn, ev->status);
4227
Johan Hedberg54776102014-08-15 21:06:56 +03004228 params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
4229 conn->dst_type);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004230 if (params) {
Johan Hedberg95305ba2014-07-04 12:37:21 +03004231 list_del_init(&params->action);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004232 if (params->conn) {
4233 hci_conn_drop(params->conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004234 hci_conn_put(params->conn);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004235 params->conn = NULL;
4236 }
4237 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004238
Ville Tervofcd89c02011-02-10 22:38:47 -03004239unlock:
Johan Hedberg223683a2014-07-06 15:44:23 +03004240 hci_update_background_scan(hdev);
Ville Tervofcd89c02011-02-10 22:38:47 -03004241 hci_dev_unlock(hdev);
4242}
4243
Marcel Holtmann1855d922014-06-23 11:40:05 +02004244static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
4245 struct sk_buff *skb)
4246{
4247 struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
4248 struct hci_conn *conn;
4249
4250 BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
4251
4252 if (ev->status)
4253 return;
4254
4255 hci_dev_lock(hdev);
4256
4257 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
4258 if (conn) {
4259 conn->le_conn_interval = le16_to_cpu(ev->interval);
4260 conn->le_conn_latency = le16_to_cpu(ev->latency);
4261 conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
4262 }
4263
4264 hci_dev_unlock(hdev);
4265}
4266
Andre Guedesa4790db2014-02-26 20:21:47 -03004267/* This function requires the caller holds hdev->lock */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004268static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
4269 bdaddr_t *addr,
4270 u8 addr_type, u8 adv_type)
Andre Guedesa4790db2014-02-26 20:21:47 -03004271{
4272 struct hci_conn *conn;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004273 struct hci_conn_params *params;
Andre Guedesa4790db2014-02-26 20:21:47 -03004274
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004275 /* If the event is not connectable don't proceed further */
4276 if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004277 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004278
4279 /* Ignore if the device is blocked */
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004280 if (hci_bdaddr_list_lookup(&hdev->blacklist, addr, addr_type))
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004281 return NULL;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004282
Johan Hedbergf99353c2014-07-16 11:56:09 +03004283 /* Most controller will fail if we try to create new connections
4284 * while we have an existing one in slave role.
4285 */
4286 if (hdev->conn_hash.le_num_slave > 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004287 return NULL;
Johan Hedbergf99353c2014-07-16 11:56:09 +03004288
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004289 /* If we're not connectable only connect devices that we have in
4290 * our pend_le_conns list.
4291 */
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004292 params = hci_pend_le_action_lookup(&hdev->pend_le_conns,
4293 addr, addr_type);
4294 if (!params)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004295 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03004296
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004297 switch (params->auto_connect) {
4298 case HCI_AUTO_CONN_DIRECT:
4299 /* Only devices advertising with ADV_DIRECT_IND are
4300 * triggering a connection attempt. This is allowing
4301 * incoming connections from slave devices.
4302 */
4303 if (adv_type != LE_ADV_DIRECT_IND)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004304 return NULL;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004305 break;
4306 case HCI_AUTO_CONN_ALWAYS:
4307 /* Devices advertising with ADV_IND or ADV_DIRECT_IND
4308 * are triggering a connection attempt. This means
4309 * that incoming connectioms from slave device are
4310 * accepted and also outgoing connections to slave
4311 * devices are established when found.
4312 */
4313 break;
4314 default:
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004315 return NULL;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02004316 }
4317
Andre Guedesa4790db2014-02-26 20:21:47 -03004318 conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
Johan Hedberge804d252014-07-16 11:42:28 +03004319 HCI_LE_AUTOCONN_TIMEOUT, HCI_ROLE_MASTER);
Johan Hedbergf161dd42014-08-15 21:06:54 +03004320 if (!IS_ERR(conn)) {
4321 /* Store the pointer since we don't really have any
4322 * other owner of the object besides the params that
4323 * triggered it. This way we can abort the connection if
4324 * the parameters get removed and keep the reference
4325 * count consistent once the connection is established.
4326 */
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03004327 params->conn = hci_conn_get(conn);
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004328 return conn;
Johan Hedbergf161dd42014-08-15 21:06:54 +03004329 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004330
4331 switch (PTR_ERR(conn)) {
4332 case -EBUSY:
4333 /* If hci_connect() returns -EBUSY it means there is already
4334 * an LE connection attempt going on. Since controllers don't
4335 * support more than one connection attempt at the time, we
4336 * don't consider this an error case.
4337 */
4338 break;
4339 default:
4340 BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004341 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03004342 }
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004343
4344 return NULL;
Andre Guedesa4790db2014-02-26 20:21:47 -03004345}
4346
Johan Hedberg4af605d2014-03-24 10:48:00 +02004347static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
4348 u8 bdaddr_type, s8 rssi, u8 *data, u8 len)
4349{
Johan Hedbergb9a63282014-03-25 10:51:52 +02004350 struct discovery_state *d = &hdev->discovery;
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004351 struct smp_irk *irk;
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004352 struct hci_conn *conn;
Johan Hedberg474ee062014-03-25 14:34:59 +02004353 bool match;
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004354 u32 flags;
Johan Hedbergb9a63282014-03-25 10:51:52 +02004355
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004356 /* Check if we need to convert to identity address */
4357 irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
4358 if (irk) {
4359 bdaddr = &irk->bdaddr;
4360 bdaddr_type = irk->addr_type;
4361 }
4362
4363 /* Check if we have been requested to connect to this device */
Alfonso Acostafd45ada2014-10-07 08:44:11 +00004364 conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, type);
4365 if (conn && type == LE_ADV_IND) {
4366 /* Store report for later inclusion by
4367 * mgmt_device_connected
4368 */
4369 memcpy(conn->le_adv_data, data, len);
4370 conn->le_adv_data_len = len;
4371 }
Johan Hedberg1c1abca2014-07-07 12:45:53 +03004372
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004373 /* Passive scanning shouldn't trigger any device found events,
4374 * except for devices marked as CONN_REPORT for which we do send
4375 * device found events.
4376 */
Johan Hedbergca5c4be2014-03-25 10:30:46 +02004377 if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004378 if (type == LE_ADV_DIRECT_IND)
4379 return;
4380
Johan Hedberg3a19b6f2014-07-15 08:07:59 +03004381 if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
4382 bdaddr, bdaddr_type))
Johan Hedberg0d2bf132014-07-02 22:42:02 +03004383 return;
4384
4385 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
4386 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
4387 else
4388 flags = 0;
4389 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
4390 rssi, flags, data, len, NULL, 0);
Johan Hedberg97bf2e92014-07-04 12:37:16 +03004391 return;
Johan Hedbergca5c4be2014-03-25 10:30:46 +02004392 }
Johan Hedberg4af605d2014-03-24 10:48:00 +02004393
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004394 /* When receiving non-connectable or scannable undirected
4395 * advertising reports, this means that the remote device is
4396 * not connectable and then clearly indicate this in the
4397 * device found event.
4398 *
4399 * When receiving a scan response, then there is no way to
4400 * know if the remote device is connectable or not. However
4401 * since scan responses are merged with a previously seen
4402 * advertising report, the flags field from that report
4403 * will be used.
4404 *
4405 * In the really unlikely case that a controller get confused
4406 * and just sends a scan response event, then it is marked as
4407 * not connectable as well.
4408 */
4409 if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
4410 type == LE_ADV_SCAN_RSP)
4411 flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
4412 else
4413 flags = 0;
4414
Johan Hedbergb9a63282014-03-25 10:51:52 +02004415 /* If there's nothing pending either store the data from this
4416 * event or send an immediate device found event if the data
4417 * should not be stored for later.
4418 */
4419 if (!has_pending_adv_report(hdev)) {
4420 /* If the report will trigger a SCAN_REQ store it for
4421 * later merging.
4422 */
4423 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4424 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004425 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004426 return;
4427 }
4428
4429 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004430 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004431 return;
4432 }
4433
Johan Hedberg474ee062014-03-25 14:34:59 +02004434 /* Check if the pending report is for the same device as the new one */
4435 match = (!bacmp(bdaddr, &d->last_adv_addr) &&
4436 bdaddr_type == d->last_adv_addr_type);
4437
Johan Hedbergb9a63282014-03-25 10:51:52 +02004438 /* If the pending data doesn't match this report or this isn't a
4439 * scan response (e.g. we got a duplicate ADV_IND) then force
4440 * sending of the pending data.
4441 */
Johan Hedberg474ee062014-03-25 14:34:59 +02004442 if (type != LE_ADV_SCAN_RSP || !match) {
4443 /* Send out whatever is in the cache, but skip duplicates */
4444 if (!match)
4445 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004446 d->last_adv_addr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004447 d->last_adv_rssi, d->last_adv_flags,
Johan Hedbergff5cd292014-03-25 14:40:52 +02004448 d->last_adv_data,
Johan Hedberg474ee062014-03-25 14:34:59 +02004449 d->last_adv_data_len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004450
4451 /* If the new report will trigger a SCAN_REQ store it for
4452 * later merging.
4453 */
4454 if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
4455 store_pending_adv_report(hdev, bdaddr, bdaddr_type,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004456 rssi, flags, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004457 return;
4458 }
4459
4460 /* The advertising reports cannot be merged, so clear
4461 * the pending report and send out a device found event.
4462 */
4463 clear_pending_adv_report(hdev);
Johan Hedberg5c5b93e2014-03-29 08:39:53 +02004464 mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004465 rssi, flags, data, len, NULL, 0);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004466 return;
4467 }
4468
4469 /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
4470 * the new event is a SCAN_RSP. We can therefore proceed with
4471 * sending a merged device found event.
4472 */
4473 mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
Marcel Holtmannc70a7e42014-07-01 14:11:21 +02004474 d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
Marcel Holtmann42bd6a52014-07-01 14:11:19 +02004475 d->last_adv_data, d->last_adv_data_len, data, len);
Johan Hedbergb9a63282014-03-25 10:51:52 +02004476 clear_pending_adv_report(hdev);
Johan Hedberg4af605d2014-03-24 10:48:00 +02004477}
4478
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004479static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
Andre Guedes9aa04c92011-05-26 16:23:51 -03004480{
Andre Guedese95beb42011-09-26 20:48:35 -03004481 u8 num_reports = skb->data[0];
4482 void *ptr = &skb->data[1];
Andre Guedes9aa04c92011-05-26 16:23:51 -03004483
Andre Guedesa4790db2014-02-26 20:21:47 -03004484 hci_dev_lock(hdev);
4485
Andre Guedese95beb42011-09-26 20:48:35 -03004486 while (num_reports--) {
4487 struct hci_ev_le_advertising_info *ev = ptr;
Johan Hedberg4af605d2014-03-24 10:48:00 +02004488 s8 rssi;
Andre Guedesa4790db2014-02-26 20:21:47 -03004489
Andre Guedes3c9e9192012-01-10 18:20:50 -03004490 rssi = ev->data[ev->length];
Johan Hedberg4af605d2014-03-24 10:48:00 +02004491 process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
4492 ev->bdaddr_type, rssi, ev->data, ev->length);
Andre Guedes3c9e9192012-01-10 18:20:50 -03004493
Andre Guedese95beb42011-09-26 20:48:35 -03004494 ptr += sizeof(*ev) + ev->length + 1;
Andre Guedes9aa04c92011-05-26 16:23:51 -03004495 }
Andre Guedesa4790db2014-02-26 20:21:47 -03004496
4497 hci_dev_unlock(hdev);
Andre Guedes9aa04c92011-05-26 16:23:51 -03004498}
4499
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004500static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004501{
4502 struct hci_ev_le_ltk_req *ev = (void *) skb->data;
4503 struct hci_cp_le_ltk_reply cp;
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004504 struct hci_cp_le_ltk_neg_reply neg;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004505 struct hci_conn *conn;
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004506 struct smp_ltk *ltk;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004507
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004508 BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004509
4510 hci_dev_lock(hdev);
4511
4512 conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004513 if (conn == NULL)
4514 goto not_found;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004515
Johan Hedberge804d252014-07-16 11:42:28 +03004516 ltk = hci_find_ltk(hdev, ev->ediv, ev->rand, conn->role);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004517 if (ltk == NULL)
4518 goto not_found;
4519
4520 memcpy(cp.ltk, ltk->val, sizeof(ltk->val));
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004521 cp.handle = cpu_to_le16(conn->handle);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004522
Johan Hedberga6f78332014-09-10 17:37:45 -07004523 conn->pending_sec_level = smp_ltk_sec_level(ltk);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004524
Andre Guedes89cbb4d2013-07-31 16:25:29 -03004525 conn->enc_key_size = ltk->enc_size;
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004526
4527 hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
4528
Claudio Takahasi5981a882013-07-25 16:34:24 -03004529 /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
4530 * temporary key used to encrypt a connection following
4531 * pairing. It is used during the Encrypted Session Setup to
4532 * distribute the keys. Later, security can be re-established
4533 * using a distributed LTK.
4534 */
Johan Hedberg2ceba532014-06-16 19:25:16 +03004535 if (ltk->type == SMP_STK) {
Johan Hedbergfe59a052014-07-01 19:14:12 +03004536 set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004537 list_del(&ltk->list);
4538 kfree(ltk);
Johan Hedbergfe59a052014-07-01 19:14:12 +03004539 } else {
4540 clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
Vinicius Costa Gomesc9839a12012-02-02 21:08:01 -03004541 }
4542
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004543 hci_dev_unlock(hdev);
Vinicius Costa Gomesbea710f2011-07-07 18:59:37 -03004544
4545 return;
4546
4547not_found:
4548 neg.handle = ev->handle;
4549 hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
4550 hci_dev_unlock(hdev);
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004551}
4552
Andre Guedes8e75b462014-07-01 18:10:08 -03004553static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
4554 u8 reason)
4555{
4556 struct hci_cp_le_conn_param_req_neg_reply cp;
4557
4558 cp.handle = cpu_to_le16(handle);
4559 cp.reason = reason;
4560
4561 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
4562 &cp);
4563}
4564
4565static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
4566 struct sk_buff *skb)
4567{
4568 struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
4569 struct hci_cp_le_conn_param_req_reply cp;
4570 struct hci_conn *hcon;
4571 u16 handle, min, max, latency, timeout;
4572
4573 handle = le16_to_cpu(ev->handle);
4574 min = le16_to_cpu(ev->interval_min);
4575 max = le16_to_cpu(ev->interval_max);
4576 latency = le16_to_cpu(ev->latency);
4577 timeout = le16_to_cpu(ev->timeout);
4578
4579 hcon = hci_conn_hash_lookup_handle(hdev, handle);
4580 if (!hcon || hcon->state != BT_CONNECTED)
4581 return send_conn_param_neg_reply(hdev, handle,
4582 HCI_ERROR_UNKNOWN_CONN_ID);
4583
4584 if (hci_check_conn_params(min, max, latency, timeout))
4585 return send_conn_param_neg_reply(hdev, handle,
4586 HCI_ERROR_INVALID_LL_PARAMS);
4587
Johan Hedberg40bef302014-07-16 11:42:27 +03004588 if (hcon->role == HCI_ROLE_MASTER) {
Johan Hedberg348d50b2014-07-02 17:37:30 +03004589 struct hci_conn_params *params;
Johan Hedbergf4869e22014-07-02 17:37:32 +03004590 u8 store_hint;
Johan Hedberg348d50b2014-07-02 17:37:30 +03004591
4592 hci_dev_lock(hdev);
4593
4594 params = hci_conn_params_lookup(hdev, &hcon->dst,
4595 hcon->dst_type);
4596 if (params) {
4597 params->conn_min_interval = min;
4598 params->conn_max_interval = max;
4599 params->conn_latency = latency;
4600 params->supervision_timeout = timeout;
Johan Hedbergf4869e22014-07-02 17:37:32 +03004601 store_hint = 0x01;
4602 } else{
4603 store_hint = 0x00;
Johan Hedberg348d50b2014-07-02 17:37:30 +03004604 }
4605
4606 hci_dev_unlock(hdev);
4607
Johan Hedbergf4869e22014-07-02 17:37:32 +03004608 mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
4609 store_hint, min, max, latency, timeout);
Johan Hedberg348d50b2014-07-02 17:37:30 +03004610 }
Andre Guedesffb5a8272014-07-01 18:10:11 -03004611
Andre Guedes8e75b462014-07-01 18:10:08 -03004612 cp.handle = ev->handle;
4613 cp.interval_min = ev->interval_min;
4614 cp.interval_max = ev->interval_max;
4615 cp.latency = ev->latency;
4616 cp.timeout = ev->timeout;
4617 cp.min_ce_len = 0;
4618 cp.max_ce_len = 0;
4619
4620 hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
4621}
4622
Gustavo Padovan6039aa72012-05-23 04:04:18 -03004623static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
Ville Tervofcd89c02011-02-10 22:38:47 -03004624{
4625 struct hci_ev_le_meta *le_ev = (void *) skb->data;
4626
4627 skb_pull(skb, sizeof(*le_ev));
4628
4629 switch (le_ev->subevent) {
4630 case HCI_EV_LE_CONN_COMPLETE:
4631 hci_le_conn_complete_evt(hdev, skb);
4632 break;
4633
Marcel Holtmann1855d922014-06-23 11:40:05 +02004634 case HCI_EV_LE_CONN_UPDATE_COMPLETE:
4635 hci_le_conn_update_complete_evt(hdev, skb);
4636 break;
4637
Andre Guedes9aa04c92011-05-26 16:23:51 -03004638 case HCI_EV_LE_ADVERTISING_REPORT:
4639 hci_le_adv_report_evt(hdev, skb);
4640 break;
4641
Vinicius Costa Gomesa7a595f2011-06-09 18:50:47 -03004642 case HCI_EV_LE_LTK_REQ:
4643 hci_le_ltk_request_evt(hdev, skb);
4644 break;
4645
Andre Guedes8e75b462014-07-01 18:10:08 -03004646 case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
4647 hci_le_remote_conn_param_req_evt(hdev, skb);
4648 break;
4649
Ville Tervofcd89c02011-02-10 22:38:47 -03004650 default:
4651 break;
4652 }
4653}
4654
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +03004655static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
4656{
4657 struct hci_ev_channel_selected *ev = (void *) skb->data;
4658 struct hci_conn *hcon;
4659
4660 BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
4661
4662 skb_pull(skb, sizeof(*ev));
4663
4664 hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
4665 if (!hcon)
4666 return;
4667
4668 amp_read_loc_assoc_final_data(hdev, hcon);
4669}
4670
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
4672{
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004673 struct hci_event_hdr *hdr = (void *) skb->data;
4674 __u8 event = hdr->evt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
Johan Hedbergb6ddb632013-04-02 13:34:31 +03004676 hci_dev_lock(hdev);
4677
4678 /* Received events are (currently) only needed when a request is
4679 * ongoing so avoid unnecessary memory allocation.
4680 */
Marcel Holtmann899de762014-07-11 05:51:58 +02004681 if (hci_req_pending(hdev)) {
Johan Hedbergb6ddb632013-04-02 13:34:31 +03004682 kfree_skb(hdev->recv_evt);
4683 hdev->recv_evt = skb_clone(skb, GFP_KERNEL);
4684 }
4685
4686 hci_dev_unlock(hdev);
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 skb_pull(skb, HCI_EVENT_HDR_SIZE);
4689
Johan Hedberg02350a72013-04-03 21:50:29 +03004690 if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->req.event == event) {
Johannes Bergc1f23a22013-10-07 18:19:16 +02004691 struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
4692 u16 opcode = __le16_to_cpu(cmd_hdr->opcode);
Johan Hedberg02350a72013-04-03 21:50:29 +03004693
4694 hci_req_cmd_complete(hdev, opcode, 0);
4695 }
4696
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004697 switch (event) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 case HCI_EV_INQUIRY_COMPLETE:
4699 hci_inquiry_complete_evt(hdev, skb);
4700 break;
4701
4702 case HCI_EV_INQUIRY_RESULT:
4703 hci_inquiry_result_evt(hdev, skb);
4704 break;
4705
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004706 case HCI_EV_CONN_COMPLETE:
4707 hci_conn_complete_evt(hdev, skb);
Marcel Holtmann21d9e302005-09-13 01:32:25 +02004708 break;
4709
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 case HCI_EV_CONN_REQUEST:
4711 hci_conn_request_evt(hdev, skb);
4712 break;
4713
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 case HCI_EV_DISCONN_COMPLETE:
4715 hci_disconn_complete_evt(hdev, skb);
4716 break;
4717
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 case HCI_EV_AUTH_COMPLETE:
4719 hci_auth_complete_evt(hdev, skb);
4720 break;
4721
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004722 case HCI_EV_REMOTE_NAME:
4723 hci_remote_name_evt(hdev, skb);
4724 break;
4725
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 case HCI_EV_ENCRYPT_CHANGE:
4727 hci_encrypt_change_evt(hdev, skb);
4728 break;
4729
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004730 case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
4731 hci_change_link_key_complete_evt(hdev, skb);
4732 break;
4733
4734 case HCI_EV_REMOTE_FEATURES:
4735 hci_remote_features_evt(hdev, skb);
4736 break;
4737
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004738 case HCI_EV_CMD_COMPLETE:
4739 hci_cmd_complete_evt(hdev, skb);
4740 break;
4741
4742 case HCI_EV_CMD_STATUS:
4743 hci_cmd_status_evt(hdev, skb);
4744 break;
4745
4746 case HCI_EV_ROLE_CHANGE:
4747 hci_role_change_evt(hdev, skb);
4748 break;
4749
4750 case HCI_EV_NUM_COMP_PKTS:
4751 hci_num_comp_pkts_evt(hdev, skb);
4752 break;
4753
4754 case HCI_EV_MODE_CHANGE:
4755 hci_mode_change_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 break;
4757
4758 case HCI_EV_PIN_CODE_REQ:
4759 hci_pin_code_request_evt(hdev, skb);
4760 break;
4761
4762 case HCI_EV_LINK_KEY_REQ:
4763 hci_link_key_request_evt(hdev, skb);
4764 break;
4765
4766 case HCI_EV_LINK_KEY_NOTIFY:
4767 hci_link_key_notify_evt(hdev, skb);
4768 break;
4769
4770 case HCI_EV_CLOCK_OFFSET:
4771 hci_clock_offset_evt(hdev, skb);
4772 break;
4773
Marcel Holtmanna8746412008-07-14 20:13:46 +02004774 case HCI_EV_PKT_TYPE_CHANGE:
4775 hci_pkt_type_change_evt(hdev, skb);
4776 break;
4777
Marcel Holtmann85a1e932005-08-09 20:28:02 -07004778 case HCI_EV_PSCAN_REP_MODE:
4779 hci_pscan_rep_mode_evt(hdev, skb);
4780 break;
4781
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004782 case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
4783 hci_inquiry_result_with_rssi_evt(hdev, skb);
4784 break;
4785
4786 case HCI_EV_REMOTE_EXT_FEATURES:
4787 hci_remote_ext_features_evt(hdev, skb);
4788 break;
4789
4790 case HCI_EV_SYNC_CONN_COMPLETE:
4791 hci_sync_conn_complete_evt(hdev, skb);
4792 break;
4793
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004794 case HCI_EV_EXTENDED_INQUIRY_RESULT:
4795 hci_extended_inquiry_result_evt(hdev, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796 break;
4797
Johan Hedberg1c2e0042012-06-08 23:31:13 +08004798 case HCI_EV_KEY_REFRESH_COMPLETE:
4799 hci_key_refresh_complete_evt(hdev, skb);
4800 break;
4801
Marcel Holtmann04936842008-07-14 20:13:48 +02004802 case HCI_EV_IO_CAPA_REQUEST:
4803 hci_io_capa_request_evt(hdev, skb);
4804 break;
4805
Johan Hedberg03b555e2011-01-04 15:40:05 +02004806 case HCI_EV_IO_CAPA_REPLY:
4807 hci_io_capa_reply_evt(hdev, skb);
4808 break;
4809
Johan Hedberga5c29682011-02-19 12:05:57 -03004810 case HCI_EV_USER_CONFIRM_REQUEST:
4811 hci_user_confirm_request_evt(hdev, skb);
4812 break;
4813
Brian Gix1143d452011-11-23 08:28:34 -08004814 case HCI_EV_USER_PASSKEY_REQUEST:
4815 hci_user_passkey_request_evt(hdev, skb);
4816 break;
4817
Johan Hedberg92a25252012-09-06 18:39:26 +03004818 case HCI_EV_USER_PASSKEY_NOTIFY:
4819 hci_user_passkey_notify_evt(hdev, skb);
4820 break;
4821
4822 case HCI_EV_KEYPRESS_NOTIFY:
4823 hci_keypress_notify_evt(hdev, skb);
4824 break;
4825
Marcel Holtmann04936842008-07-14 20:13:48 +02004826 case HCI_EV_SIMPLE_PAIR_COMPLETE:
4827 hci_simple_pair_complete_evt(hdev, skb);
4828 break;
4829
Marcel Holtmann41a96212008-07-14 20:13:48 +02004830 case HCI_EV_REMOTE_HOST_FEATURES:
4831 hci_remote_host_features_evt(hdev, skb);
4832 break;
4833
Ville Tervofcd89c02011-02-10 22:38:47 -03004834 case HCI_EV_LE_META:
4835 hci_le_meta_evt(hdev, skb);
4836 break;
4837
Andrei Emeltchenko9495b2e2012-09-27 17:26:22 +03004838 case HCI_EV_CHANNEL_SELECTED:
4839 hci_chan_selected_evt(hdev, skb);
4840 break;
4841
Szymon Janc2763eda2011-03-22 13:12:22 +01004842 case HCI_EV_REMOTE_OOB_DATA_REQUEST:
4843 hci_remote_oob_data_request_evt(hdev, skb);
4844 break;
4845
Andrei Emeltchenkod5e91192012-10-25 15:20:44 +03004846 case HCI_EV_PHY_LINK_COMPLETE:
4847 hci_phy_link_complete_evt(hdev, skb);
4848 break;
4849
Andrei Emeltchenko27695fb2012-10-25 15:20:45 +03004850 case HCI_EV_LOGICAL_LINK_COMPLETE:
4851 hci_loglink_complete_evt(hdev, skb);
4852 break;
4853
Andrei Emeltchenko606e2a12012-10-31 15:46:31 +02004854 case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
4855 hci_disconn_loglink_complete_evt(hdev, skb);
4856 break;
4857
Andrei Emeltchenko9eef6b32012-10-31 15:46:32 +02004858 case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
4859 hci_disconn_phylink_complete_evt(hdev, skb);
4860 break;
4861
Andrei Emeltchenko25e89e92012-01-04 12:41:58 +02004862 case HCI_EV_NUM_COMP_BLOCKS:
4863 hci_num_comp_blocks_evt(hdev, skb);
4864 break;
4865
Marcel Holtmanna9de9242007-10-20 13:33:56 +02004866 default:
Andrei Emeltchenko9f1db002012-07-11 14:32:43 +03004867 BT_DBG("%s event 0x%2.2x", hdev->name, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 break;
4869 }
4870
4871 kfree_skb(skb);
4872 hdev->stat.evt_rx++;
4873}