blob: 7b4c0b027e9029258d58232126e95bca1d79a94c [file] [log] [blame]
Johan Hedberg03811012010-12-08 00:21:06 +02001/*
2 BlueZ - Bluetooth protocol stack for Linux
Johan Hedbergea585ab2012-02-17 14:50:39 +02003
Johan Hedberg03811012010-12-08 00:21:06 +02004 Copyright (C) 2010 Nokia Corporation
Johan Hedbergea585ab2012-02-17 14:50:39 +02005 Copyright (C) 2011-2012 Intel Corporation
Johan Hedberg03811012010-12-08 00:21:06 +02006
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
15 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
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
23*/
24
25/* Bluetooth HCI Management interface */
26
Paul Gortmaker3a9a2312011-05-27 09:12:25 -040027#include <linux/module.h>
Johan Hedberg03811012010-12-08 00:21:06 +020028#include <asm/unaligned.h>
29
30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
Johan Hedberg71290692015-02-20 13:26:23 +020032#include <net/bluetooth/hci_sock.h>
Johan Hedberg4bc58f52014-05-20 09:45:47 +030033#include <net/bluetooth/l2cap.h>
Johan Hedberg03811012010-12-08 00:21:06 +020034#include <net/bluetooth/mgmt.h>
Marcel Holtmannac4b7232013-10-10 14:54:16 -070035
Johan Hedberg0857dd32014-12-19 13:40:20 +020036#include "hci_request.h"
Marcel Holtmannac4b7232013-10-10 14:54:16 -070037#include "smp.h"
Johan Hedberga380b6c2015-03-17 13:48:48 +020038#include "mgmt_util.h"
Johan Hedberg03811012010-12-08 00:21:06 +020039
Johan Hedberg2da9c552012-02-17 14:39:28 +020040#define MGMT_VERSION 1
Marcel Holtmannbeb1c212015-03-10 14:04:52 -070041#define MGMT_REVISION 9
Johan Hedberg02d98122010-12-13 21:07:04 +020042
Johan Hedberge70bb2e2012-02-13 16:59:33 +020043static const u16 mgmt_commands[] = {
44 MGMT_OP_READ_INDEX_LIST,
45 MGMT_OP_READ_INFO,
46 MGMT_OP_SET_POWERED,
47 MGMT_OP_SET_DISCOVERABLE,
48 MGMT_OP_SET_CONNECTABLE,
49 MGMT_OP_SET_FAST_CONNECTABLE,
Johan Hedbergb2939472014-07-30 09:22:23 +030050 MGMT_OP_SET_BONDABLE,
Johan Hedberge70bb2e2012-02-13 16:59:33 +020051 MGMT_OP_SET_LINK_SECURITY,
52 MGMT_OP_SET_SSP,
53 MGMT_OP_SET_HS,
54 MGMT_OP_SET_LE,
55 MGMT_OP_SET_DEV_CLASS,
56 MGMT_OP_SET_LOCAL_NAME,
57 MGMT_OP_ADD_UUID,
58 MGMT_OP_REMOVE_UUID,
59 MGMT_OP_LOAD_LINK_KEYS,
60 MGMT_OP_LOAD_LONG_TERM_KEYS,
61 MGMT_OP_DISCONNECT,
62 MGMT_OP_GET_CONNECTIONS,
63 MGMT_OP_PIN_CODE_REPLY,
64 MGMT_OP_PIN_CODE_NEG_REPLY,
65 MGMT_OP_SET_IO_CAPABILITY,
66 MGMT_OP_PAIR_DEVICE,
67 MGMT_OP_CANCEL_PAIR_DEVICE,
68 MGMT_OP_UNPAIR_DEVICE,
69 MGMT_OP_USER_CONFIRM_REPLY,
70 MGMT_OP_USER_CONFIRM_NEG_REPLY,
71 MGMT_OP_USER_PASSKEY_REPLY,
72 MGMT_OP_USER_PASSKEY_NEG_REPLY,
73 MGMT_OP_READ_LOCAL_OOB_DATA,
74 MGMT_OP_ADD_REMOTE_OOB_DATA,
75 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
76 MGMT_OP_START_DISCOVERY,
77 MGMT_OP_STOP_DISCOVERY,
78 MGMT_OP_CONFIRM_NAME,
79 MGMT_OP_BLOCK_DEVICE,
80 MGMT_OP_UNBLOCK_DEVICE,
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -070081 MGMT_OP_SET_DEVICE_ID,
Johan Hedberg4375f102013-09-25 13:26:10 +030082 MGMT_OP_SET_ADVERTISING,
Johan Hedberg0663ca22013-10-02 13:43:14 +030083 MGMT_OP_SET_BREDR,
Marcel Holtmannd13eafc2013-10-02 04:41:30 -070084 MGMT_OP_SET_STATIC_ADDRESS,
Marcel Holtmann7f72134e2013-10-11 14:44:58 -070085 MGMT_OP_SET_SCAN_PARAMS,
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -080086 MGMT_OP_SET_SECURE_CONN,
Marcel Holtmann4e39ac82014-01-31 11:55:22 -080087 MGMT_OP_SET_DEBUG_KEYS,
Johan Hedberg62b04cd2014-02-23 19:42:27 +020088 MGMT_OP_SET_PRIVACY,
Johan Hedberg41edf162014-02-18 10:19:35 +020089 MGMT_OP_LOAD_IRKS,
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +020090 MGMT_OP_GET_CONN_INFO,
Johan Hedberg95868422014-06-28 17:54:07 +030091 MGMT_OP_GET_CLOCK_INFO,
Marcel Holtmann2faade52014-06-29 19:44:03 +020092 MGMT_OP_ADD_DEVICE,
93 MGMT_OP_REMOVE_DEVICE,
Johan Hedberga26f3dc2014-07-02 17:37:29 +030094 MGMT_OP_LOAD_CONN_PARAM,
Marcel Holtmann73d1df22014-07-02 22:10:52 +020095 MGMT_OP_READ_UNCONF_INDEX_LIST,
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +020096 MGMT_OP_READ_CONFIG_INFO,
Marcel Holtmanndbece372014-07-04 18:11:55 +020097 MGMT_OP_SET_EXTERNAL_CONFIG,
Marcel Holtmann9713c172014-07-06 12:11:15 +020098 MGMT_OP_SET_PUBLIC_ADDRESS,
Jakub Pawlowski66ea9422014-12-05 10:55:59 +010099 MGMT_OP_START_SERVICE_DISCOVERY,
Marcel Holtmann4f0f1552015-03-14 22:43:19 -0700100 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann96f14742015-03-14 19:27:57 -0700101 MGMT_OP_READ_EXT_INDEX_LIST,
Marcel Holtmannd3d53052015-03-14 20:53:25 -0700102 MGMT_OP_READ_ADV_FEATURES,
Arman Uguray24b4f382015-03-23 15:57:12 -0700103 MGMT_OP_ADD_ADVERTISING,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200104};
105
106static const u16 mgmt_events[] = {
107 MGMT_EV_CONTROLLER_ERROR,
108 MGMT_EV_INDEX_ADDED,
109 MGMT_EV_INDEX_REMOVED,
110 MGMT_EV_NEW_SETTINGS,
111 MGMT_EV_CLASS_OF_DEV_CHANGED,
112 MGMT_EV_LOCAL_NAME_CHANGED,
113 MGMT_EV_NEW_LINK_KEY,
114 MGMT_EV_NEW_LONG_TERM_KEY,
115 MGMT_EV_DEVICE_CONNECTED,
116 MGMT_EV_DEVICE_DISCONNECTED,
117 MGMT_EV_CONNECT_FAILED,
118 MGMT_EV_PIN_CODE_REQUEST,
119 MGMT_EV_USER_CONFIRM_REQUEST,
120 MGMT_EV_USER_PASSKEY_REQUEST,
121 MGMT_EV_AUTH_FAILED,
122 MGMT_EV_DEVICE_FOUND,
123 MGMT_EV_DISCOVERING,
124 MGMT_EV_DEVICE_BLOCKED,
125 MGMT_EV_DEVICE_UNBLOCKED,
126 MGMT_EV_DEVICE_UNPAIRED,
Johan Hedberg92a25252012-09-06 18:39:26 +0300127 MGMT_EV_PASSKEY_NOTIFY,
Marcel Holtmann1b60ef22014-02-21 21:35:30 -0800128 MGMT_EV_NEW_IRK,
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -0700129 MGMT_EV_NEW_CSRK,
Marcel Holtmann8afef092014-06-29 22:28:34 +0200130 MGMT_EV_DEVICE_ADDED,
131 MGMT_EV_DEVICE_REMOVED,
Andre Guedesffb5a8272014-07-01 18:10:11 -0300132 MGMT_EV_NEW_CONN_PARAM,
Marcel Holtmann0602a8a2014-07-02 21:30:54 +0200133 MGMT_EV_UNCONF_INDEX_ADDED,
Marcel Holtmannedd38962014-07-02 21:30:55 +0200134 MGMT_EV_UNCONF_INDEX_REMOVED,
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200135 MGMT_EV_NEW_CONFIG_OPTIONS,
Marcel Holtmannced85542015-03-14 19:27:56 -0700136 MGMT_EV_EXT_INDEX_ADDED,
137 MGMT_EV_EXT_INDEX_REMOVED,
Marcel Holtmann72000df2015-03-16 16:11:21 -0700138 MGMT_EV_LOCAL_OOB_DATA_UPDATED,
Arman Uguray24b4f382015-03-23 15:57:12 -0700139 MGMT_EV_ADVERTISING_ADDED,
140 MGMT_EV_ADVERTISING_REMOVED,
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200141};
142
Marcel Holtmann17b02e62012-03-01 14:32:37 -0800143#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
Johan Hedberg7d785252011-12-15 00:47:39 +0200144
Johan Hedbergd25b78e2015-01-27 12:55:52 +0200145#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
146 "\x00\x00\x00\x00\x00\x00\x00\x00"
147
Johan Hedbergca69b792011-11-11 18:10:00 +0200148/* HCI to MGMT error code conversion table */
149static u8 mgmt_status_table[] = {
150 MGMT_STATUS_SUCCESS,
151 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
152 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
153 MGMT_STATUS_FAILED, /* Hardware Failure */
154 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
155 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
Johan Hedbergeadd6632014-01-13 17:15:53 +0200156 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
Johan Hedbergca69b792011-11-11 18:10:00 +0200157 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
158 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
159 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
160 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
161 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
162 MGMT_STATUS_BUSY, /* Command Disallowed */
163 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
164 MGMT_STATUS_REJECTED, /* Rejected Security */
165 MGMT_STATUS_REJECTED, /* Rejected Personal */
166 MGMT_STATUS_TIMEOUT, /* Host Timeout */
167 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
168 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
169 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
170 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
171 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
172 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
173 MGMT_STATUS_BUSY, /* Repeated Attempts */
174 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
175 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
176 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
177 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
178 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
179 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
180 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
181 MGMT_STATUS_FAILED, /* Unspecified Error */
182 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
183 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
184 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
185 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
186 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
187 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
188 MGMT_STATUS_FAILED, /* Unit Link Key Used */
189 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
190 MGMT_STATUS_TIMEOUT, /* Instant Passed */
191 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
192 MGMT_STATUS_FAILED, /* Transaction Collision */
193 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
194 MGMT_STATUS_REJECTED, /* QoS Rejected */
195 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
196 MGMT_STATUS_REJECTED, /* Insufficient Security */
197 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
198 MGMT_STATUS_BUSY, /* Role Switch Pending */
199 MGMT_STATUS_FAILED, /* Slot Violation */
200 MGMT_STATUS_FAILED, /* Role Switch Failed */
201 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
202 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
203 MGMT_STATUS_BUSY, /* Host Busy Pairing */
204 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
205 MGMT_STATUS_BUSY, /* Controller Busy */
206 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
207 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
208 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
209 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
210 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
211};
212
213static u8 mgmt_status(u8 hci_status)
214{
215 if (hci_status < ARRAY_SIZE(mgmt_status_table))
216 return mgmt_status_table[hci_status];
217
218 return MGMT_STATUS_FAILED;
219}
220
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700221static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
222 u16 len, int flag)
Marcel Holtmannf9207332015-03-14 19:27:55 -0700223{
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700224 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
225 flag, NULL);
Marcel Holtmannf9207332015-03-14 19:27:55 -0700226}
227
Marcel Holtmann72000df2015-03-16 16:11:21 -0700228static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
229 u16 len, int flag, struct sock *skip_sk)
230{
231 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
232 flag, skip_sk);
233}
234
Marcel Holtmannf6b77122015-03-14 19:28:05 -0700235static int mgmt_generic_event(u16 event, struct hci_dev *hdev, void *data,
236 u16 len, struct sock *skip_sk)
237{
238 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
239 HCI_MGMT_GENERIC_EVENTS, skip_sk);
240}
241
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200242static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
243 struct sock *skip_sk)
244{
245 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
Marcel Holtmannc08b1a12015-03-14 19:27:59 -0700246 HCI_SOCK_TRUSTED, skip_sk);
Johan Hedberg7a00ff42015-03-06 21:08:56 +0200247}
248
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300249static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
250 u16 data_len)
Johan Hedberga38528f2011-01-22 06:46:43 +0200251{
252 struct mgmt_rp_read_version rp;
253
254 BT_DBG("sock %p", sk);
255
256 rp.version = MGMT_VERSION;
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700257 rp.revision = cpu_to_le16(MGMT_REVISION);
Johan Hedberga38528f2011-01-22 06:46:43 +0200258
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200259 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
260 &rp, sizeof(rp));
Johan Hedberga38528f2011-01-22 06:46:43 +0200261}
262
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300263static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
264 u16 data_len)
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200265{
266 struct mgmt_rp_read_commands *rp;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +0200267 const u16 num_commands = ARRAY_SIZE(mgmt_commands);
268 const u16 num_events = ARRAY_SIZE(mgmt_events);
Andrei Emeltchenko2e3c35e2012-03-14 18:54:15 +0200269 __le16 *opcode;
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200270 size_t rp_size;
271 int i, err;
272
273 BT_DBG("sock %p", sk);
274
275 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
276
277 rp = kmalloc(rp_size, GFP_KERNEL);
278 if (!rp)
279 return -ENOMEM;
280
Joe Perchesdcf4adb2014-03-12 10:52:35 -0700281 rp->num_commands = cpu_to_le16(num_commands);
282 rp->num_events = cpu_to_le16(num_events);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200283
284 for (i = 0, opcode = rp->opcodes; i < num_commands; i++, opcode++)
285 put_unaligned_le16(mgmt_commands[i], opcode);
286
287 for (i = 0; i < num_events; i++, opcode++)
288 put_unaligned_le16(mgmt_events[i], opcode);
289
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200290 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
291 rp, rp_size);
Johan Hedberge70bb2e2012-02-13 16:59:33 +0200292 kfree(rp);
293
294 return err;
295}
296
Gustavo F. Padovan04124682012-03-08 01:25:00 -0300297static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
298 u16 data_len)
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200299{
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200300 struct mgmt_rp_read_index_list *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200301 struct hci_dev *d;
Johan Hedberga38528f2011-01-22 06:46:43 +0200302 size_t rp_len;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200303 u16 count;
Johan Hedberg476e44c2012-10-19 20:10:46 +0300304 int err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200305
306 BT_DBG("sock %p", sk);
307
308 read_lock(&hci_dev_list_lock);
309
310 count = 0;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +0300311 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200312 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700313 !hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann1514b892013-10-06 08:25:01 -0700314 count++;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200315 }
316
Johan Hedberga38528f2011-01-22 06:46:43 +0200317 rp_len = sizeof(*rp) + (2 * count);
318 rp = kmalloc(rp_len, GFP_ATOMIC);
319 if (!rp) {
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100320 read_unlock(&hci_dev_list_lock);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200321 return -ENOMEM;
Jesper Juhlb2c60d42011-01-14 00:18:49 +0100322 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200323
Johan Hedberg476e44c2012-10-19 20:10:46 +0300324 count = 0;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +0200325 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700326 if (hci_dev_test_flag(d, HCI_SETUP) ||
327 hci_dev_test_flag(d, HCI_CONFIG) ||
328 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Johan Hedbergab81cbf2010-12-15 13:53:18 +0200329 continue;
330
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200331 /* Devices marked as raw-only are neither configured
332 * nor unconfigured controllers.
333 */
334 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
Marcel Holtmann0736cfa2013-08-26 21:40:51 -0700335 continue;
336
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200337 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700338 !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann1514b892013-10-06 08:25:01 -0700339 rp->index[count++] = cpu_to_le16(d->id);
340 BT_DBG("Added hci%u", d->id);
341 }
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200342 }
343
Johan Hedberg476e44c2012-10-19 20:10:46 +0300344 rp->num_controllers = cpu_to_le16(count);
345 rp_len = sizeof(*rp) + (2 * count);
346
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200347 read_unlock(&hci_dev_list_lock);
348
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200349 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
350 0, rp, rp_len);
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200351
Johan Hedberga38528f2011-01-22 06:46:43 +0200352 kfree(rp);
353
354 return err;
Johan Hedbergfaba42e2010-12-13 21:07:05 +0200355}
356
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200357static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
358 void *data, u16 data_len)
359{
360 struct mgmt_rp_read_unconf_index_list *rp;
361 struct hci_dev *d;
362 size_t rp_len;
363 u16 count;
364 int err;
365
366 BT_DBG("sock %p", sk);
367
368 read_lock(&hci_dev_list_lock);
369
370 count = 0;
371 list_for_each_entry(d, &hci_dev_list, list) {
372 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700373 hci_dev_test_flag(d, HCI_UNCONFIGURED))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200374 count++;
375 }
376
377 rp_len = sizeof(*rp) + (2 * count);
378 rp = kmalloc(rp_len, GFP_ATOMIC);
379 if (!rp) {
380 read_unlock(&hci_dev_list_lock);
381 return -ENOMEM;
382 }
383
384 count = 0;
385 list_for_each_entry(d, &hci_dev_list, list) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700386 if (hci_dev_test_flag(d, HCI_SETUP) ||
387 hci_dev_test_flag(d, HCI_CONFIG) ||
388 hci_dev_test_flag(d, HCI_USER_CHANNEL))
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200389 continue;
390
391 /* Devices marked as raw-only are neither configured
392 * nor unconfigured controllers.
393 */
394 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
395 continue;
396
397 if (d->dev_type == HCI_BREDR &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700398 hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200399 rp->index[count++] = cpu_to_le16(d->id);
400 BT_DBG("Added hci%u", d->id);
401 }
402 }
403
404 rp->num_controllers = cpu_to_le16(count);
405 rp_len = sizeof(*rp) + (2 * count);
406
407 read_unlock(&hci_dev_list_lock);
408
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200409 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
410 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
Marcel Holtmann73d1df22014-07-02 22:10:52 +0200411
412 kfree(rp);
413
414 return err;
415}
416
Marcel Holtmann96f14742015-03-14 19:27:57 -0700417static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
418 void *data, u16 data_len)
419{
420 struct mgmt_rp_read_ext_index_list *rp;
421 struct hci_dev *d;
422 size_t rp_len;
423 u16 count;
424 int err;
425
426 BT_DBG("sock %p", sk);
427
428 read_lock(&hci_dev_list_lock);
429
430 count = 0;
431 list_for_each_entry(d, &hci_dev_list, list) {
432 if (d->dev_type == HCI_BREDR || d->dev_type == HCI_AMP)
433 count++;
434 }
435
436 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
437 rp = kmalloc(rp_len, GFP_ATOMIC);
438 if (!rp) {
439 read_unlock(&hci_dev_list_lock);
440 return -ENOMEM;
441 }
442
443 count = 0;
444 list_for_each_entry(d, &hci_dev_list, list) {
445 if (hci_dev_test_flag(d, HCI_SETUP) ||
446 hci_dev_test_flag(d, HCI_CONFIG) ||
447 hci_dev_test_flag(d, HCI_USER_CHANNEL))
448 continue;
449
450 /* Devices marked as raw-only are neither configured
451 * nor unconfigured controllers.
452 */
453 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
454 continue;
455
456 if (d->dev_type == HCI_BREDR) {
457 if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
458 rp->entry[count].type = 0x01;
459 else
460 rp->entry[count].type = 0x00;
461 } else if (d->dev_type == HCI_AMP) {
462 rp->entry[count].type = 0x02;
463 } else {
464 continue;
465 }
466
467 rp->entry[count].bus = d->bus;
468 rp->entry[count++].index = cpu_to_le16(d->id);
469 BT_DBG("Added hci%u", d->id);
470 }
471
472 rp->num_controllers = cpu_to_le16(count);
473 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
474
475 read_unlock(&hci_dev_list_lock);
476
477 /* If this command is called at least once, then all the
478 * default index and unconfigured index events are disabled
479 * and from now on only extended index events are used.
480 */
481 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
482 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
483 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
484
485 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
486 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len);
487
488 kfree(rp);
489
490 return err;
491}
492
Marcel Holtmanndbece372014-07-04 18:11:55 +0200493static bool is_configured(struct hci_dev *hdev)
494{
495 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700496 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanndbece372014-07-04 18:11:55 +0200497 return false;
498
499 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
500 !bacmp(&hdev->public_addr, BDADDR_ANY))
501 return false;
502
503 return true;
504}
505
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200506static __le32 get_missing_options(struct hci_dev *hdev)
507{
508 u32 options = 0;
509
Marcel Holtmanndbece372014-07-04 18:11:55 +0200510 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700511 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200512 options |= MGMT_OPTION_EXTERNAL_CONFIG;
513
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200514 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
515 !bacmp(&hdev->public_addr, BDADDR_ANY))
516 options |= MGMT_OPTION_PUBLIC_ADDRESS;
517
518 return cpu_to_le32(options);
519}
520
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200521static int new_options(struct hci_dev *hdev, struct sock *skip)
522{
523 __le32 options = get_missing_options(hdev);
524
Marcel Holtmannf6b77122015-03-14 19:28:05 -0700525 return mgmt_generic_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
526 sizeof(options), skip);
Marcel Holtmannf4537c02014-07-04 19:06:23 +0200527}
528
Marcel Holtmanndbece372014-07-04 18:11:55 +0200529static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
530{
531 __le32 options = get_missing_options(hdev);
532
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200533 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
534 sizeof(options));
Marcel Holtmanndbece372014-07-04 18:11:55 +0200535}
536
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200537static int read_config_info(struct sock *sk, struct hci_dev *hdev,
538 void *data, u16 data_len)
539{
540 struct mgmt_rp_read_config_info rp;
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200541 u32 options = 0;
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200542
543 BT_DBG("sock %p %s", sk, hdev->name);
544
545 hci_dev_lock(hdev);
546
547 memset(&rp, 0, sizeof(rp));
548 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200549
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200550 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
551 options |= MGMT_OPTION_EXTERNAL_CONFIG;
552
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200553 if (hdev->set_bdaddr)
Marcel Holtmann89bc22d2014-07-04 16:54:37 +0200554 options |= MGMT_OPTION_PUBLIC_ADDRESS;
555
556 rp.supported_options = cpu_to_le32(options);
557 rp.missing_options = get_missing_options(hdev);
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200558
559 hci_dev_unlock(hdev);
560
Johan Hedberg2a1afb52015-03-06 21:08:54 +0200561 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
562 &rp, sizeof(rp));
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200563}
564
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200565static u32 get_supported_settings(struct hci_dev *hdev)
Johan Hedberg03811012010-12-08 00:21:06 +0200566{
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200567 u32 settings = 0;
Johan Hedberg03811012010-12-08 00:21:06 +0200568
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200569 settings |= MGMT_SETTING_POWERED;
Johan Hedbergb2939472014-07-30 09:22:23 +0300570 settings |= MGMT_SETTING_BONDABLE;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800571 settings |= MGMT_SETTING_DEBUG_KEYS;
Johan Hedberg3742abf2014-07-08 16:07:34 +0300572 settings |= MGMT_SETTING_CONNECTABLE;
573 settings |= MGMT_SETTING_DISCOVERABLE;
Johan Hedberg03811012010-12-08 00:21:06 +0200574
Andre Guedesed3fa312012-07-24 15:03:46 -0300575 if (lmp_bredr_capable(hdev)) {
Johan Hedberg1a47aee2013-03-15 17:07:06 -0500576 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
577 settings |= MGMT_SETTING_FAST_CONNECTABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200578 settings |= MGMT_SETTING_BREDR;
579 settings |= MGMT_SETTING_LINK_SECURITY;
Marcel Holtmanna82974c2013-10-11 09:48:47 -0700580
581 if (lmp_ssp_capable(hdev)) {
582 settings |= MGMT_SETTING_SSP;
583 settings |= MGMT_SETTING_HS;
584 }
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800585
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -0800586 if (lmp_sc_capable(hdev))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800587 settings |= MGMT_SETTING_SECURE_CONN;
Marcel Holtmann848566b2013-10-01 22:59:22 -0700588 }
Marcel Holtmannd7b7e792012-02-20 21:47:49 +0100589
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300590 if (lmp_le_capable(hdev)) {
Marcel Holtmann9d428202012-05-03 07:12:31 +0200591 settings |= MGMT_SETTING_LE;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300592 settings |= MGMT_SETTING_ADVERTISING;
Johan Hedberga3209692014-05-26 11:23:35 +0300593 settings |= MGMT_SETTING_SECURE_CONN;
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200594 settings |= MGMT_SETTING_PRIVACY;
Marcel Holtmann93690c22015-03-06 10:11:21 -0800595 settings |= MGMT_SETTING_STATIC_ADDRESS;
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300596 }
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200597
Marcel Holtmanneb1904f2014-07-04 17:23:33 +0200598 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
599 hdev->set_bdaddr)
Marcel Holtmann9fc3bfb2014-07-04 00:46:56 +0200600 settings |= MGMT_SETTING_CONFIGURATION;
601
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200602 return settings;
603}
Johan Hedbergebc99fe2011-01-04 11:54:26 +0200604
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200605static u32 get_current_settings(struct hci_dev *hdev)
606{
607 u32 settings = 0;
Johan Hedbergdc4fe302011-03-16 14:29:36 +0200608
Johan Hedbergf1f0eb02012-02-21 17:15:41 +0200609 if (hdev_is_powered(hdev))
Marcel Holtmannf0d4b782012-02-21 12:14:25 +0100610 settings |= MGMT_SETTING_POWERED;
611
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700612 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200613 settings |= MGMT_SETTING_CONNECTABLE;
614
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700615 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg1a4d3c42013-03-15 17:07:08 -0500616 settings |= MGMT_SETTING_FAST_CONNECTABLE;
617
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700618 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200619 settings |= MGMT_SETTING_DISCOVERABLE;
620
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700621 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
Johan Hedbergb2939472014-07-30 09:22:23 +0300622 settings |= MGMT_SETTING_BONDABLE;
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200623
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700624 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200625 settings |= MGMT_SETTING_BREDR;
626
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700627 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200628 settings |= MGMT_SETTING_LE;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200629
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700630 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200631 settings |= MGMT_SETTING_LINK_SECURITY;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200632
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700633 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200634 settings |= MGMT_SETTING_SSP;
Johan Hedbergf7b64e692010-12-13 21:07:06 +0200635
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700636 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
Johan Hedberg6d80dfd2012-02-20 23:50:38 +0200637 settings |= MGMT_SETTING_HS;
638
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700639 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergeeca6f82013-09-25 13:26:09 +0300640 settings |= MGMT_SETTING_ADVERTISING;
641
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700642 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
Marcel Holtmanne98d2ce2014-01-10 02:07:22 -0800643 settings |= MGMT_SETTING_SECURE_CONN;
644
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700645 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
Marcel Holtmannb1de97d2014-01-31 11:55:21 -0800646 settings |= MGMT_SETTING_DEBUG_KEYS;
647
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700648 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
Johan Hedberg0f4bd942014-02-22 19:06:35 +0200649 settings |= MGMT_SETTING_PRIVACY;
650
Marcel Holtmann93690c22015-03-06 10:11:21 -0800651 /* The current setting for static address has two purposes. The
652 * first is to indicate if the static address will be used and
653 * the second is to indicate if it is actually set.
654 *
655 * This means if the static address is not configured, this flag
656 * will never bet set. If the address is configured, then if the
657 * address is actually used decides if the flag is set or not.
658 *
659 * For single mode LE only controllers and dual-mode controllers
660 * with BR/EDR disabled, the existence of the static address will
661 * be evaluated.
662 */
Marcel Holtmannb7cb93e2015-03-13 10:20:35 -0700663 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700664 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Marcel Holtmann93690c22015-03-06 10:11:21 -0800665 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
666 if (bacmp(&hdev->static_addr, BDADDR_ANY))
667 settings |= MGMT_SETTING_STATIC_ADDRESS;
668 }
669
Johan Hedberg69ab39e2011-12-15 00:47:35 +0200670 return settings;
Johan Hedbergc542a062011-01-26 13:11:03 +0200671}
672
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300673#define PNP_INFO_SVCLASS_ID 0x1200
674
Johan Hedberg213202e2013-01-27 00:31:33 +0200675static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
676{
677 u8 *ptr = data, *uuids_start = NULL;
678 struct bt_uuid *uuid;
679
680 if (len < 4)
681 return ptr;
682
683 list_for_each_entry(uuid, &hdev->uuids, list) {
684 u16 uuid16;
685
686 if (uuid->size != 16)
687 continue;
688
689 uuid16 = get_unaligned_le16(&uuid->uuid[12]);
690 if (uuid16 < 0x1100)
691 continue;
692
693 if (uuid16 == PNP_INFO_SVCLASS_ID)
694 continue;
695
696 if (!uuids_start) {
697 uuids_start = ptr;
698 uuids_start[0] = 1;
699 uuids_start[1] = EIR_UUID16_ALL;
700 ptr += 2;
701 }
702
703 /* Stop if not enough space to put next UUID */
704 if ((ptr - data) + sizeof(u16) > len) {
705 uuids_start[1] = EIR_UUID16_SOME;
706 break;
707 }
708
709 *ptr++ = (uuid16 & 0x00ff);
710 *ptr++ = (uuid16 & 0xff00) >> 8;
711 uuids_start[0] += sizeof(uuid16);
712 }
713
714 return ptr;
715}
716
Johan Hedbergcdf19632013-01-27 00:31:34 +0200717static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
718{
719 u8 *ptr = data, *uuids_start = NULL;
720 struct bt_uuid *uuid;
721
722 if (len < 6)
723 return ptr;
724
725 list_for_each_entry(uuid, &hdev->uuids, list) {
726 if (uuid->size != 32)
727 continue;
728
729 if (!uuids_start) {
730 uuids_start = ptr;
731 uuids_start[0] = 1;
732 uuids_start[1] = EIR_UUID32_ALL;
733 ptr += 2;
734 }
735
736 /* Stop if not enough space to put next UUID */
737 if ((ptr - data) + sizeof(u32) > len) {
738 uuids_start[1] = EIR_UUID32_SOME;
739 break;
740 }
741
742 memcpy(ptr, &uuid->uuid[12], sizeof(u32));
743 ptr += sizeof(u32);
744 uuids_start[0] += sizeof(u32);
745 }
746
747 return ptr;
748}
749
Johan Hedbergc00d5752013-01-27 00:31:35 +0200750static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
751{
752 u8 *ptr = data, *uuids_start = NULL;
753 struct bt_uuid *uuid;
754
755 if (len < 18)
756 return ptr;
757
758 list_for_each_entry(uuid, &hdev->uuids, list) {
759 if (uuid->size != 128)
760 continue;
761
762 if (!uuids_start) {
763 uuids_start = ptr;
764 uuids_start[0] = 1;
765 uuids_start[1] = EIR_UUID128_ALL;
766 ptr += 2;
767 }
768
769 /* Stop if not enough space to put next UUID */
770 if ((ptr - data) + 16 > len) {
771 uuids_start[1] = EIR_UUID128_SOME;
772 break;
773 }
774
775 memcpy(ptr, uuid->uuid, 16);
776 ptr += 16;
777 uuids_start[0] += 16;
778 }
779
780 return ptr;
781}
782
Johan Hedberg333ae952015-03-17 13:48:47 +0200783static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
784{
785 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
786}
787
Johan Hedberg333ae952015-03-17 13:48:47 +0200788static struct mgmt_pending_cmd *pending_find_data(u16 opcode,
789 struct hci_dev *hdev,
790 const void *data)
791{
792 return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
793}
794
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700795static u8 create_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
796{
Marcel Holtmann7a5f4992013-10-16 00:16:49 -0700797 u8 ad_len = 0;
798 size_t name_len;
799
800 name_len = strlen(hdev->dev_name);
801 if (name_len > 0) {
802 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
803
804 if (name_len > max_len) {
805 name_len = max_len;
806 ptr[1] = EIR_NAME_SHORT;
807 } else
808 ptr[1] = EIR_NAME_COMPLETE;
809
810 ptr[0] = name_len + 1;
811
812 memcpy(ptr + 2, hdev->dev_name, name_len);
813
814 ad_len += (name_len + 2);
815 ptr += (name_len + 2);
816 }
817
818 return ad_len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700819}
820
821static void update_scan_rsp_data(struct hci_request *req)
822{
823 struct hci_dev *hdev = req->hdev;
824 struct hci_cp_le_set_scan_rsp_data cp;
825 u8 len;
826
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700827 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700828 return;
829
830 memset(&cp, 0, sizeof(cp));
831
832 len = create_scan_rsp_data(hdev, cp.data);
833
Johan Hedbergeb438b52013-10-16 15:31:07 +0300834 if (hdev->scan_rsp_data_len == len &&
835 memcmp(cp.data, hdev->scan_rsp_data, len) == 0)
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700836 return;
837
Johan Hedbergeb438b52013-10-16 15:31:07 +0300838 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
839 hdev->scan_rsp_data_len = len;
Marcel Holtmannf14d8f62013-10-16 00:16:48 -0700840
841 cp.length = len;
842
843 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
844}
845
Johan Hedberg9a43e252013-10-20 19:00:07 +0300846static u8 get_adv_discov_flags(struct hci_dev *hdev)
847{
Johan Hedberg3b0602c2015-03-06 21:08:55 +0200848 struct mgmt_pending_cmd *cmd;
Johan Hedberg9a43e252013-10-20 19:00:07 +0300849
850 /* If there's a pending mgmt command the flags will not yet have
851 * their final values, so check for this first.
852 */
Johan Hedberg333ae952015-03-17 13:48:47 +0200853 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg9a43e252013-10-20 19:00:07 +0300854 if (cmd) {
855 struct mgmt_mode *cp = cmd->param;
856 if (cp->val == 0x01)
857 return LE_AD_GENERAL;
858 else if (cp->val == 0x02)
859 return LE_AD_LIMITED;
860 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700861 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300862 return LE_AD_LIMITED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700863 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
Johan Hedberg9a43e252013-10-20 19:00:07 +0300864 return LE_AD_GENERAL;
865 }
866
867 return 0;
868}
869
Arman Uguray24b4f382015-03-23 15:57:12 -0700870static u8 create_default_adv_data(struct hci_dev *hdev, u8 *ptr)
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700871{
872 u8 ad_len = 0, flags = 0;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700873
Johan Hedberg9a43e252013-10-20 19:00:07 +0300874 flags |= get_adv_discov_flags(hdev);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700875
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700876 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700877 flags |= LE_AD_NO_BREDR;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700878
879 if (flags) {
880 BT_DBG("adv flags 0x%02x", flags);
881
882 ptr[0] = 2;
883 ptr[1] = EIR_FLAGS;
884 ptr[2] = flags;
885
886 ad_len += 3;
887 ptr += 3;
888 }
889
890 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID) {
891 ptr[0] = 2;
892 ptr[1] = EIR_TX_POWER;
893 ptr[2] = (u8) hdev->adv_tx_power;
894
895 ad_len += 3;
896 ptr += 3;
897 }
898
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700899 return ad_len;
900}
901
Arman Uguray24b4f382015-03-23 15:57:12 -0700902static u8 create_instance_adv_data(struct hci_dev *hdev, u8 *ptr)
903{
904 /* TODO: Set the appropriate entries based on advertising instance flags
905 * here once flags other than 0 are supported.
906 */
907 memcpy(ptr, hdev->adv_instance.adv_data,
908 hdev->adv_instance.adv_data_len);
909
910 return hdev->adv_instance.adv_data_len;
911}
912
913static void update_adv_data_for_instance(struct hci_request *req, u8 instance)
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700914{
915 struct hci_dev *hdev = req->hdev;
916 struct hci_cp_le_set_adv_data cp;
917 u8 len;
918
Marcel Holtmannd7a5a112015-03-13 02:11:00 -0700919 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700920 return;
921
922 memset(&cp, 0, sizeof(cp));
923
Arman Uguray24b4f382015-03-23 15:57:12 -0700924 if (instance)
925 len = create_instance_adv_data(hdev, cp.data);
926 else
927 len = create_default_adv_data(hdev, cp.data);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700928
Arman Uguray24b4f382015-03-23 15:57:12 -0700929 /* There's nothing to do if the data hasn't changed */
Marcel Holtmann441ad2d2013-10-15 06:33:52 -0700930 if (hdev->adv_data_len == len &&
931 memcmp(cp.data, hdev->adv_data, len) == 0)
932 return;
933
934 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
935 hdev->adv_data_len = len;
936
937 cp.length = len;
938
939 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
940}
941
Arman Uguray24b4f382015-03-23 15:57:12 -0700942static void update_adv_data(struct hci_request *req)
943{
944 struct hci_dev *hdev = req->hdev;
945 u8 instance;
946
947 /* The "Set Advertising" setting supersedes the "Add Advertising"
948 * setting. Here we set the advertising data based on which
949 * setting was set. When neither apply, default to the global settings,
950 * represented by instance "0".
951 */
952 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
953 !hci_dev_test_flag(hdev, HCI_ADVERTISING))
954 instance = 0x01;
955 else
956 instance = 0x00;
957
958 update_adv_data_for_instance(req, instance);
959}
960
Johan Hedbergbc6d2d02014-07-10 12:09:08 +0300961int mgmt_update_adv_data(struct hci_dev *hdev)
962{
963 struct hci_request req;
964
965 hci_req_init(&req, hdev);
966 update_adv_data(&req);
967
968 return hci_req_run(&req, NULL);
969}
970
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300971static void create_eir(struct hci_dev *hdev, u8 *data)
972{
973 u8 *ptr = data;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300974 size_t name_len;
975
976 name_len = strlen(hdev->dev_name);
977
978 if (name_len > 0) {
979 /* EIR Data type */
980 if (name_len > 48) {
981 name_len = 48;
982 ptr[1] = EIR_NAME_SHORT;
983 } else
984 ptr[1] = EIR_NAME_COMPLETE;
985
986 /* EIR Data length */
987 ptr[0] = name_len + 1;
988
989 memcpy(ptr + 2, hdev->dev_name, name_len);
990
Johan Hedberg80a1e1d2011-03-28 14:07:23 +0300991 ptr += (name_len + 2);
992 }
993
Johan Hedbergbbaf4442012-11-08 01:22:59 +0100994 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700995 ptr[0] = 2;
996 ptr[1] = EIR_TX_POWER;
997 ptr[2] = (u8) hdev->inq_tx_power;
998
Marcel Holtmann91c4e9b2012-03-11 19:27:21 -0700999 ptr += 3;
1000 }
1001
Marcel Holtmann2b9be132012-03-11 19:32:12 -07001002 if (hdev->devid_source > 0) {
1003 ptr[0] = 9;
1004 ptr[1] = EIR_DEVICE_ID;
1005
1006 put_unaligned_le16(hdev->devid_source, ptr + 2);
1007 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
1008 put_unaligned_le16(hdev->devid_product, ptr + 6);
1009 put_unaligned_le16(hdev->devid_version, ptr + 8);
1010
Marcel Holtmann2b9be132012-03-11 19:32:12 -07001011 ptr += 10;
1012 }
1013
Johan Hedberg213202e2013-01-27 00:31:33 +02001014 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergcdf19632013-01-27 00:31:34 +02001015 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedbergc00d5752013-01-27 00:31:35 +02001016 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001017}
1018
Johan Hedberg890ea892013-03-15 17:06:52 -05001019static void update_eir(struct hci_request *req)
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001020{
Johan Hedberg890ea892013-03-15 17:06:52 -05001021 struct hci_dev *hdev = req->hdev;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001022 struct hci_cp_write_eir cp;
1023
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001024 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001025 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001026
Johan Hedberg976eb202012-10-24 21:12:01 +03001027 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001028 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001029
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001030 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberg890ea892013-03-15 17:06:52 -05001031 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001032
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001033 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg890ea892013-03-15 17:06:52 -05001034 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001035
1036 memset(&cp, 0, sizeof(cp));
1037
1038 create_eir(hdev, cp.data);
1039
1040 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001041 return;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001042
1043 memcpy(hdev->eir, cp.data, sizeof(cp.data));
1044
Johan Hedberg890ea892013-03-15 17:06:52 -05001045 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03001046}
1047
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001048static u8 get_service_classes(struct hci_dev *hdev)
1049{
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001050 struct bt_uuid *uuid;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001051 u8 val = 0;
1052
Gustavo F. Padovan12dc0742011-10-14 19:32:56 -03001053 list_for_each_entry(uuid, &hdev->uuids, list)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001054 val |= uuid->svc_hint;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001055
1056 return val;
1057}
1058
Johan Hedberg890ea892013-03-15 17:06:52 -05001059static void update_class(struct hci_request *req)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001060{
Johan Hedberg890ea892013-03-15 17:06:52 -05001061 struct hci_dev *hdev = req->hdev;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001062 u8 cod[3];
1063
1064 BT_DBG("%s", hdev->name);
1065
Johan Hedberg504c8dc2012-02-23 13:30:41 +02001066 if (!hdev_is_powered(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05001067 return;
Johan Hedberg7770c4a2012-02-22 22:06:38 +02001068
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001069 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedbergf87ea1d2013-10-19 23:38:17 +03001070 return;
1071
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001072 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg890ea892013-03-15 17:06:52 -05001073 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001074
1075 cod[0] = hdev->minor_class;
1076 cod[1] = hdev->major_class;
1077 cod[2] = get_service_classes(hdev);
1078
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001079 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
Marcel Holtmann6acd7db2013-10-15 06:33:53 -07001080 cod[1] |= 0x20;
1081
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001082 if (memcmp(cod, hdev->dev_class, 3) == 0)
Johan Hedberg890ea892013-03-15 17:06:52 -05001083 return;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001084
Johan Hedberg890ea892013-03-15 17:06:52 -05001085 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02001086}
1087
Johan Hedberga4858cb2014-02-25 19:56:31 +02001088static bool get_connectable(struct hci_dev *hdev)
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001089{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001090 struct mgmt_pending_cmd *cmd;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001091
1092 /* If there's a pending mgmt command the flag will not yet have
1093 * it's final value, so check for this first.
1094 */
Johan Hedberg333ae952015-03-17 13:48:47 +02001095 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001096 if (cmd) {
1097 struct mgmt_mode *cp = cmd->param;
Johan Hedberga4858cb2014-02-25 19:56:31 +02001098 return cp->val;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001099 }
1100
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001101 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001102}
1103
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001104static void disable_advertising(struct hci_request *req)
1105{
1106 u8 enable = 0x00;
1107
1108 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1109}
1110
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001111static void enable_advertising(struct hci_request *req)
1112{
1113 struct hci_dev *hdev = req->hdev;
1114 struct hci_cp_le_set_adv_param cp;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001115 u8 own_addr_type, enable = 0x01;
Johan Hedberga4858cb2014-02-25 19:56:31 +02001116 bool connectable;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001117
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001118 if (hci_conn_num(hdev, LE_LINK) > 0)
1119 return;
1120
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001121 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03001122 disable_advertising(req);
1123
Johan Hedberg5ce194c2014-07-08 15:07:49 +03001124 /* Clear the HCI_LE_ADV bit temporarily so that the
Johan Hedberg8d972502014-02-28 12:54:14 +02001125 * hci_update_random_address knows that it's safe to go ahead
1126 * and write a new random address. The flag will be set back on
1127 * as soon as the SET_ADV_ENABLE HCI command completes.
1128 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001129 hci_dev_clear_flag(hdev, HCI_LE_ADV);
Johan Hedberg8d972502014-02-28 12:54:14 +02001130
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001131 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07001132 connectable = true;
1133 else
1134 connectable = get_connectable(hdev);
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001135
Johan Hedberga4858cb2014-02-25 19:56:31 +02001136 /* Set require_privacy to true only when non-connectable
1137 * advertising is used. In that case it is fine to use a
1138 * non-resolvable private address.
1139 */
1140 if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001141 return;
1142
Marcel Holtmann41c90c12014-02-23 20:25:55 -08001143 memset(&cp, 0, sizeof(cp));
Georg Lukas628531c2014-07-26 13:59:57 +02001144 cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
1145 cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
Johan Hedberga4858cb2014-02-25 19:56:31 +02001146 cp.type = connectable ? LE_ADV_IND : LE_ADV_NONCONN_IND;
Johan Hedberg8f2a0602014-02-23 19:42:23 +02001147 cp.own_address_type = own_addr_type;
Johan Hedberg199a2fb2014-02-22 19:06:33 +02001148 cp.channel_map = hdev->le_adv_channel_map;
1149
1150 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1151
1152 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1153}
1154
Johan Hedberg7d785252011-12-15 00:47:39 +02001155static void service_cache_off(struct work_struct *work)
1156{
1157 struct hci_dev *hdev = container_of(work, struct hci_dev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001158 service_cache.work);
Johan Hedberg890ea892013-03-15 17:06:52 -05001159 struct hci_request req;
Johan Hedberg7d785252011-12-15 00:47:39 +02001160
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001161 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
Johan Hedberg7d785252011-12-15 00:47:39 +02001162 return;
1163
Johan Hedberg890ea892013-03-15 17:06:52 -05001164 hci_req_init(&req, hdev);
1165
Johan Hedberg7d785252011-12-15 00:47:39 +02001166 hci_dev_lock(hdev);
1167
Johan Hedberg890ea892013-03-15 17:06:52 -05001168 update_eir(&req);
1169 update_class(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02001170
1171 hci_dev_unlock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05001172
1173 hci_req_run(&req, NULL);
Johan Hedberg7d785252011-12-15 00:47:39 +02001174}
1175
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001176static void rpa_expired(struct work_struct *work)
1177{
1178 struct hci_dev *hdev = container_of(work, struct hci_dev,
1179 rpa_expired.work);
1180 struct hci_request req;
1181
1182 BT_DBG("");
1183
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001184 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001185
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001186 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001187 return;
1188
1189 /* The generation of a new RPA and programming it into the
1190 * controller happens in the enable_advertising() function.
1191 */
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001192 hci_req_init(&req, hdev);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001193 enable_advertising(&req);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001194 hci_req_run(&req, NULL);
1195}
1196
Johan Hedberg6a919082012-02-28 06:17:26 +02001197static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
Johan Hedberg7d785252011-12-15 00:47:39 +02001198{
Marcel Holtmann238be782015-03-13 02:11:06 -07001199 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
Johan Hedberg6a919082012-02-28 06:17:26 +02001200 return;
1201
Johan Hedberg4f87da82012-03-02 19:55:56 +02001202 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
Johan Hedbergd6bfd592014-02-23 19:42:20 +02001203 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
Johan Hedberg7d785252011-12-15 00:47:39 +02001204
Johan Hedberg4f87da82012-03-02 19:55:56 +02001205 /* Non-mgmt controlled devices get this bit set
1206 * implicitly so that pairing works for them, however
1207 * for mgmt we require user-space to explicitly enable
1208 * it
1209 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001210 hci_dev_clear_flag(hdev, HCI_BONDABLE);
Johan Hedberg7d785252011-12-15 00:47:39 +02001211}
1212
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02001213static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001214 void *data, u16 data_len)
Johan Hedberg03811012010-12-08 00:21:06 +02001215{
1216 struct mgmt_rp_read_info rp;
Johan Hedberg03811012010-12-08 00:21:06 +02001217
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001218 BT_DBG("sock %p %s", sk, hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001219
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001220 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001221
Johan Hedberg03811012010-12-08 00:21:06 +02001222 memset(&rp, 0, sizeof(rp));
1223
Johan Hedberg03811012010-12-08 00:21:06 +02001224 bacpy(&rp.bdaddr, &hdev->bdaddr);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001225
1226 rp.version = hdev->hci_ver;
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02001227 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001228
1229 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1230 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1231
1232 memcpy(rp.dev_class, hdev->dev_class, 3);
Johan Hedberg03811012010-12-08 00:21:06 +02001233
1234 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
Johan Hedberg27fcc362012-02-22 21:46:22 +02001235 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
Johan Hedberg03811012010-12-08 00:21:06 +02001236
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001237 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001238
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001239 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1240 sizeof(rp));
Johan Hedberg03811012010-12-08 00:21:06 +02001241}
1242
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001243static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
Johan Hedberg86805702011-11-11 16:18:52 +02001244{
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001245 __le32 settings = cpu_to_le32(get_current_settings(hdev));
Johan Hedberg86805702011-11-11 16:18:52 +02001246
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001247 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1248 sizeof(settings));
Johan Hedberg86805702011-11-11 16:18:52 +02001249}
1250
Marcel Holtmann1904a852015-01-11 13:50:44 -08001251static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg8b064a32014-02-24 14:52:22 +02001252{
1253 BT_DBG("%s status 0x%02x", hdev->name, status);
1254
Johan Hedberga3172b72014-02-28 09:33:44 +02001255 if (hci_conn_count(hdev) == 0) {
1256 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001257 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberga3172b72014-02-28 09:33:44 +02001258 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001259}
1260
Johan Hedberg23a48092014-07-08 16:05:06 +03001261static bool hci_stop_discovery(struct hci_request *req)
Johan Hedberg21a60d32014-06-10 14:05:58 +03001262{
1263 struct hci_dev *hdev = req->hdev;
1264 struct hci_cp_remote_name_req_cancel cp;
1265 struct inquiry_entry *e;
1266
1267 switch (hdev->discovery.state) {
1268 case DISCOVERY_FINDING:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07001269 if (test_bit(HCI_INQUIRY, &hdev->flags))
Johan Hedberg21a60d32014-06-10 14:05:58 +03001270 hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
Jakub Pawlowski07d23342015-03-17 09:04:14 -07001271
1272 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001273 cancel_delayed_work(&hdev->le_scan_disable);
1274 hci_req_add_le_scan_disable(req);
1275 }
1276
Johan Hedberg23a48092014-07-08 16:05:06 +03001277 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001278
1279 case DISCOVERY_RESOLVING:
1280 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
1281 NAME_PENDING);
1282 if (!e)
Johan Hedberg23a48092014-07-08 16:05:06 +03001283 break;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001284
1285 bacpy(&cp.bdaddr, &e->data.bdaddr);
1286 hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
1287 &cp);
1288
Johan Hedberg23a48092014-07-08 16:05:06 +03001289 return true;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001290
1291 default:
1292 /* Passive scanning */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001293 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
Johan Hedberg21a60d32014-06-10 14:05:58 +03001294 hci_req_add_le_scan_disable(req);
Johan Hedberg23a48092014-07-08 16:05:06 +03001295 return true;
1296 }
1297
Johan Hedberg21a60d32014-06-10 14:05:58 +03001298 break;
1299 }
Johan Hedberg23a48092014-07-08 16:05:06 +03001300
1301 return false;
Johan Hedberg21a60d32014-06-10 14:05:58 +03001302}
1303
Johan Hedberg8b064a32014-02-24 14:52:22 +02001304static int clean_up_hci_state(struct hci_dev *hdev)
1305{
1306 struct hci_request req;
1307 struct hci_conn *conn;
Johan Hedberg23a48092014-07-08 16:05:06 +03001308 bool discov_stopped;
1309 int err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001310
1311 hci_req_init(&req, hdev);
1312
1313 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1314 test_bit(HCI_PSCAN, &hdev->flags)) {
1315 u8 scan = 0x00;
1316 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1317 }
1318
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001319 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Johan Hedberg8b064a32014-02-24 14:52:22 +02001320 disable_advertising(&req);
1321
Johan Hedberg23a48092014-07-08 16:05:06 +03001322 discov_stopped = hci_stop_discovery(&req);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001323
1324 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1325 struct hci_cp_disconnect dc;
Johan Hedbergc9910d02014-02-27 14:35:12 +02001326 struct hci_cp_reject_conn_req rej;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001327
Johan Hedbergc9910d02014-02-27 14:35:12 +02001328 switch (conn->state) {
1329 case BT_CONNECTED:
1330 case BT_CONFIG:
1331 dc.handle = cpu_to_le16(conn->handle);
1332 dc.reason = 0x15; /* Terminated due to Power Off */
1333 hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1334 break;
1335 case BT_CONNECT:
1336 if (conn->type == LE_LINK)
1337 hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL,
1338 0, NULL);
1339 else if (conn->type == ACL_LINK)
1340 hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL,
1341 6, &conn->dst);
1342 break;
1343 case BT_CONNECT2:
1344 bacpy(&rej.bdaddr, &conn->dst);
1345 rej.reason = 0x15; /* Terminated due to Power Off */
1346 if (conn->type == ACL_LINK)
1347 hci_req_add(&req, HCI_OP_REJECT_CONN_REQ,
1348 sizeof(rej), &rej);
1349 else if (conn->type == SCO_LINK)
1350 hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ,
1351 sizeof(rej), &rej);
1352 break;
1353 }
Johan Hedberg8b064a32014-02-24 14:52:22 +02001354 }
1355
Johan Hedberg23a48092014-07-08 16:05:06 +03001356 err = hci_req_run(&req, clean_up_hci_complete);
1357 if (!err && discov_stopped)
1358 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1359
1360 return err;
Johan Hedberg8b064a32014-02-24 14:52:22 +02001361}
1362
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001363static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001364 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001365{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001366 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001367 struct mgmt_pending_cmd *cmd;
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001368 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001369
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001370 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001371
Johan Hedberga7e80f22013-01-09 16:05:19 +02001372 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001373 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1374 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001375
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001376 hci_dev_lock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001377
Johan Hedberg333ae952015-03-17 13:48:47 +02001378 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001379 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1380 MGMT_STATUS_BUSY);
Johan Hedberg87b95ba2013-09-25 13:26:06 +03001381 goto failed;
1382 }
1383
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001384 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001385 cancel_delayed_work(&hdev->power_off);
1386
1387 if (cp->val) {
Johan Hedberga1d70452013-01-09 15:29:40 +02001388 mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev,
1389 data, len);
1390 err = mgmt_powered(hdev, 1);
Marcel Holtmannf0d4b782012-02-21 12:14:25 +01001391 goto failed;
1392 }
1393 }
1394
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001395 if (!!cp->val == hdev_is_powered(hdev)) {
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001396 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001397 goto failed;
1398 }
1399
Johan Hedberg03811012010-12-08 00:21:06 +02001400 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1401 if (!cmd) {
1402 err = -ENOMEM;
1403 goto failed;
1404 }
1405
Johan Hedberg8b064a32014-02-24 14:52:22 +02001406 if (cp->val) {
Johan Hedberg19202572013-01-14 22:33:51 +02001407 queue_work(hdev->req_workqueue, &hdev->power_on);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001408 err = 0;
1409 } else {
1410 /* Disconnect connections, stop scans, etc */
1411 err = clean_up_hci_state(hdev);
Johan Hedberga3172b72014-02-28 09:33:44 +02001412 if (!err)
1413 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1414 HCI_POWER_OFF_TIMEOUT);
Johan Hedberg03811012010-12-08 00:21:06 +02001415
Johan Hedberg8b064a32014-02-24 14:52:22 +02001416 /* ENODATA means there were no HCI commands queued */
1417 if (err == -ENODATA) {
Johan Hedberga3172b72014-02-28 09:33:44 +02001418 cancel_delayed_work(&hdev->power_off);
Johan Hedberg8b064a32014-02-24 14:52:22 +02001419 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1420 err = 0;
1421 }
1422 }
Johan Hedberg03811012010-12-08 00:21:06 +02001423
1424failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001425 hci_dev_unlock(hdev);
Johan Hedberg03811012010-12-08 00:21:06 +02001426 return err;
1427}
1428
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001429static int new_settings(struct hci_dev *hdev, struct sock *skip)
1430{
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001431 __le32 ev = cpu_to_le32(get_current_settings(hdev));
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001432
Marcel Holtmannf6b77122015-03-14 19:28:05 -07001433 return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1434 sizeof(ev), skip);
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02001435}
1436
Johan Hedberg91a668b2014-07-09 13:28:26 +03001437int mgmt_new_settings(struct hci_dev *hdev)
1438{
1439 return new_settings(hdev, NULL);
1440}
1441
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001442struct cmd_lookup {
1443 struct sock *sk;
1444 struct hci_dev *hdev;
1445 u8 mgmt_status;
1446};
1447
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001448static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001449{
1450 struct cmd_lookup *match = data;
1451
1452 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1453
1454 list_del(&cmd->list);
1455
1456 if (match->sk == NULL) {
1457 match->sk = cmd->sk;
1458 sock_hold(match->sk);
1459 }
1460
1461 mgmt_pending_free(cmd);
1462}
1463
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001464static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001465{
1466 u8 *status = data;
1467
Johan Hedberga69e8372015-03-06 21:08:53 +02001468 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
Johan Hedbergbd99abd2013-09-25 13:26:07 +03001469 mgmt_pending_remove(cmd);
1470}
1471
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001472static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg1b9b5ee2014-12-05 13:36:00 +02001473{
1474 if (cmd->cmd_complete) {
1475 u8 *status = data;
1476
1477 cmd->cmd_complete(cmd, *status);
1478 mgmt_pending_remove(cmd);
1479
1480 return;
1481 }
1482
1483 cmd_status_rsp(cmd, data);
1484}
1485
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001486static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedbergf5818c22014-12-05 13:36:02 +02001487{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001488 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1489 cmd->param, cmd->param_len);
Johan Hedbergf5818c22014-12-05 13:36:02 +02001490}
1491
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001492static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001493{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02001494 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1495 cmd->param, sizeof(struct mgmt_addr_info));
Johan Hedberg7776d1d2014-12-05 13:36:03 +02001496}
1497
Johan Hedberge6fe7982013-10-02 15:45:22 +03001498static u8 mgmt_bredr_support(struct hci_dev *hdev)
1499{
1500 if (!lmp_bredr_capable(hdev))
1501 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001502 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001503 return MGMT_STATUS_REJECTED;
1504 else
1505 return MGMT_STATUS_SUCCESS;
1506}
1507
1508static u8 mgmt_le_support(struct hci_dev *hdev)
1509{
1510 if (!lmp_le_capable(hdev))
1511 return MGMT_STATUS_NOT_SUPPORTED;
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001512 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberge6fe7982013-10-02 15:45:22 +03001513 return MGMT_STATUS_REJECTED;
1514 else
1515 return MGMT_STATUS_SUCCESS;
1516}
1517
Marcel Holtmann1904a852015-01-11 13:50:44 -08001518static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
1519 u16 opcode)
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001520{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001521 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001522 struct mgmt_mode *cp;
Marcel Holtmann970ba522013-10-15 06:33:57 -07001523 struct hci_request req;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001524 bool changed;
1525
1526 BT_DBG("status 0x%02x", status);
1527
1528 hci_dev_lock(hdev);
1529
Johan Hedberg333ae952015-03-17 13:48:47 +02001530 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001531 if (!cmd)
1532 goto unlock;
1533
1534 if (status) {
1535 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001536 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001537 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001538 goto remove_cmd;
1539 }
1540
1541 cp = cmd->param;
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001542 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001543 changed = !hci_dev_test_and_set_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001544
1545 if (hdev->discov_timeout > 0) {
1546 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1547 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
1548 to);
1549 }
1550 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001551 changed = hci_dev_test_and_clear_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd4462a02013-10-15 08:11:02 -07001552 }
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001553
1554 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1555
1556 if (changed)
1557 new_settings(hdev, cmd->sk);
1558
Marcel Holtmann970ba522013-10-15 06:33:57 -07001559 /* When the discoverable mode gets changed, make sure
1560 * that class of device has the limited discoverable
Johan Hedberg432df052014-08-01 11:13:31 +03001561 * bit correctly set. Also update page scan based on whitelist
1562 * entries.
Marcel Holtmann970ba522013-10-15 06:33:57 -07001563 */
1564 hci_req_init(&req, hdev);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001565 __hci_update_page_scan(&req);
Marcel Holtmann970ba522013-10-15 06:33:57 -07001566 update_class(&req);
1567 hci_req_run(&req, NULL);
1568
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001569remove_cmd:
1570 mgmt_pending_remove(cmd);
1571
1572unlock:
1573 hci_dev_unlock(hdev);
1574}
1575
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001576static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001577 u16 len)
Johan Hedberg03811012010-12-08 00:21:06 +02001578{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001579 struct mgmt_cp_set_discoverable *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001580 struct mgmt_pending_cmd *cmd;
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001581 struct hci_request req;
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001582 u16 timeout;
Johan Hedberg9a43e252013-10-20 19:00:07 +03001583 u8 scan;
Johan Hedberg03811012010-12-08 00:21:06 +02001584 int err;
Johan Hedberge41d8b42010-12-13 21:07:03 +02001585
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001586 BT_DBG("request for %s", hdev->name);
Johan Hedberg03811012010-12-08 00:21:06 +02001587
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001588 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1589 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001590 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1591 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001592
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001593 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02001594 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1595 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001596
Marcel Holtmann1f350c82012-03-12 20:31:08 -07001597 timeout = __le16_to_cpu(cp->timeout);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001598
1599 /* Disabling discoverable requires that no timeout is set,
1600 * and enabling limited discoverable requires a timeout.
1601 */
1602 if ((cp->val == 0x00 && timeout > 0) ||
1603 (cp->val == 0x02 && timeout == 0))
Johan Hedberga69e8372015-03-06 21:08:53 +02001604 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1605 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001606
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001607 hci_dev_lock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001608
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001609 if (!hdev_is_powered(hdev) && timeout > 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001610 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1611 MGMT_STATUS_NOT_POWERED);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001612 goto failed;
1613 }
1614
Johan Hedberg333ae952015-03-17 13:48:47 +02001615 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1616 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001617 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1618 MGMT_STATUS_BUSY);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001619 goto failed;
1620 }
1621
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001622 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001623 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1624 MGMT_STATUS_REJECTED);
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001625 goto failed;
1626 }
1627
1628 if (!hdev_is_powered(hdev)) {
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001629 bool changed = false;
1630
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001631 /* Setting limited discoverable when powered off is
1632 * not a valid operation since it requires a timeout
1633 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1634 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001635 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07001636 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001637 changed = true;
1638 }
1639
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001640 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedberg0224d2f2012-02-21 19:40:05 +02001641 if (err < 0)
1642 goto failed;
1643
1644 if (changed)
1645 err = new_settings(hdev, sk);
1646
Johan Hedberg5e5282b2012-02-21 16:01:30 +02001647 goto failed;
1648 }
1649
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001650 /* If the current mode is the same, then just update the timeout
1651 * value with the new value. And if only the timeout gets updated,
1652 * then no need for any HCI transactions.
1653 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001654 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1655 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1656 HCI_LIMITED_DISCOVERABLE)) {
Marcel Holtmann36261542013-10-15 08:28:51 -07001657 cancel_delayed_work(&hdev->discov_off);
1658 hdev->discov_timeout = timeout;
Marcel Holtmann955638e2012-02-22 18:21:00 +01001659
Marcel Holtmann36261542013-10-15 08:28:51 -07001660 if (cp->val && hdev->discov_timeout > 0) {
1661 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001662 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
Marcel Holtmann36261542013-10-15 08:28:51 -07001663 to);
Marcel Holtmann955638e2012-02-22 18:21:00 +01001664 }
1665
Johan Hedberg69ab39e2011-12-15 00:47:35 +02001666 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001667 goto failed;
1668 }
1669
1670 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1671 if (!cmd) {
1672 err = -ENOMEM;
1673 goto failed;
1674 }
1675
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001676 /* Cancel any potential discoverable timeout that might be
1677 * still active and store new timeout value. The arming of
1678 * the timeout happens in the complete handler.
1679 */
1680 cancel_delayed_work(&hdev->discov_off);
1681 hdev->discov_timeout = timeout;
1682
Johan Hedbergb456f872013-10-19 23:38:22 +03001683 /* Limited discoverable mode */
1684 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001685 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001686 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001687 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Johan Hedbergb456f872013-10-19 23:38:22 +03001688
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001689 hci_req_init(&req, hdev);
1690
Johan Hedberg9a43e252013-10-20 19:00:07 +03001691 /* The procedure for LE-only controllers is much simpler - just
1692 * update the advertising data.
1693 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001694 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg9a43e252013-10-20 19:00:07 +03001695 goto update_ad;
1696
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001697 scan = SCAN_PAGE;
1698
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001699 if (cp->val) {
1700 struct hci_cp_write_current_iac_lap hci_cp;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001701
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001702 if (cp->val == 0x02) {
1703 /* Limited discoverable mode */
Marcel Holtmann33337dc2013-10-23 08:28:01 -07001704 hci_cp.num_iac = min_t(u8, hdev->num_iac, 2);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001705 hci_cp.iac_lap[0] = 0x00; /* LIAC */
1706 hci_cp.iac_lap[1] = 0x8b;
1707 hci_cp.iac_lap[2] = 0x9e;
1708 hci_cp.iac_lap[3] = 0x33; /* GIAC */
1709 hci_cp.iac_lap[4] = 0x8b;
1710 hci_cp.iac_lap[5] = 0x9e;
1711 } else {
1712 /* General discoverable mode */
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001713 hci_cp.num_iac = 1;
1714 hci_cp.iac_lap[0] = 0x33; /* GIAC */
1715 hci_cp.iac_lap[1] = 0x8b;
1716 hci_cp.iac_lap[2] = 0x9e;
1717 }
1718
1719 hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP,
1720 (hci_cp.num_iac * 3) + 1, &hci_cp);
1721
1722 scan |= SCAN_INQUIRY;
1723 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001724 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
Marcel Holtmann310a3d42013-10-15 09:13:39 -07001725 }
1726
1727 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001728
Johan Hedberg9a43e252013-10-20 19:00:07 +03001729update_ad:
1730 update_adv_data(&req);
1731
Johan Hedbergbfaf8c92013-10-14 21:15:27 +03001732 err = hci_req_run(&req, set_discoverable_complete);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001733 if (err < 0)
1734 mgmt_pending_remove(cmd);
1735
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001736failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001737 hci_dev_unlock(hdev);
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001738 return err;
1739}
1740
Johan Hedberg406d7802013-03-15 17:07:09 -05001741static void write_fast_connectable(struct hci_request *req, bool enable)
1742{
Johan Hedbergbd98b992013-03-15 17:07:13 -05001743 struct hci_dev *hdev = req->hdev;
Johan Hedberg406d7802013-03-15 17:07:09 -05001744 struct hci_cp_write_page_scan_activity acp;
1745 u8 type;
1746
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001747 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberg547003b2013-10-21 16:51:53 +03001748 return;
1749
Johan Hedberg4c01f8b2013-03-15 17:07:14 -05001750 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
1751 return;
1752
Johan Hedberg406d7802013-03-15 17:07:09 -05001753 if (enable) {
1754 type = PAGE_SCAN_TYPE_INTERLACED;
1755
1756 /* 160 msec page scan interval */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001757 acp.interval = cpu_to_le16(0x0100);
Johan Hedberg406d7802013-03-15 17:07:09 -05001758 } else {
1759 type = PAGE_SCAN_TYPE_STANDARD; /* default */
1760
1761 /* default 1.28 sec page scan */
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001762 acp.interval = cpu_to_le16(0x0800);
Johan Hedberg406d7802013-03-15 17:07:09 -05001763 }
1764
Joe Perchesdcf4adb2014-03-12 10:52:35 -07001765 acp.window = cpu_to_le16(0x0012);
Johan Hedberg406d7802013-03-15 17:07:09 -05001766
Johan Hedbergbd98b992013-03-15 17:07:13 -05001767 if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
1768 __cpu_to_le16(hdev->page_scan_window) != acp.window)
1769 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
1770 sizeof(acp), &acp);
1771
1772 if (hdev->page_scan_type != type)
1773 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
Johan Hedberg406d7802013-03-15 17:07:09 -05001774}
1775
Marcel Holtmann1904a852015-01-11 13:50:44 -08001776static void set_connectable_complete(struct hci_dev *hdev, u8 status,
1777 u16 opcode)
Johan Hedberg2b76f452013-03-15 17:07:04 -05001778{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001779 struct mgmt_pending_cmd *cmd;
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001780 struct mgmt_mode *cp;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001781 bool conn_changed, discov_changed;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001782
1783 BT_DBG("status 0x%02x", status);
1784
1785 hci_dev_lock(hdev);
1786
Johan Hedberg333ae952015-03-17 13:48:47 +02001787 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
Johan Hedberg2b76f452013-03-15 17:07:04 -05001788 if (!cmd)
1789 goto unlock;
1790
Johan Hedberg37438c12013-10-14 16:20:05 +03001791 if (status) {
1792 u8 mgmt_err = mgmt_status(status);
Johan Hedberga69e8372015-03-06 21:08:53 +02001793 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg37438c12013-10-14 16:20:05 +03001794 goto remove_cmd;
1795 }
1796
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001797 cp = cmd->param;
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001798 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07001799 conn_changed = !hci_dev_test_and_set_flag(hdev,
1800 HCI_CONNECTABLE);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001801 discov_changed = false;
1802 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001803 conn_changed = hci_dev_test_and_clear_flag(hdev,
1804 HCI_CONNECTABLE);
1805 discov_changed = hci_dev_test_and_clear_flag(hdev,
1806 HCI_DISCOVERABLE);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001807 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001808
Johan Hedberg2b76f452013-03-15 17:07:04 -05001809 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1810
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001811 if (conn_changed || discov_changed) {
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001812 new_settings(hdev, cmd->sk);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001813 hci_update_page_scan(hdev);
Johan Hedbergbc6d2d02014-07-10 12:09:08 +03001814 if (discov_changed)
1815 mgmt_update_adv_data(hdev);
Johan Hedberg2b7be332014-07-07 14:40:22 +03001816 hci_update_background_scan(hdev);
1817 }
Johan Hedbergd7b856f2013-10-14 16:20:04 +03001818
Johan Hedberg37438c12013-10-14 16:20:05 +03001819remove_cmd:
Johan Hedberg2b76f452013-03-15 17:07:04 -05001820 mgmt_pending_remove(cmd);
1821
1822unlock:
1823 hci_dev_unlock(hdev);
1824}
1825
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001826static int set_connectable_update_settings(struct hci_dev *hdev,
1827 struct sock *sk, u8 val)
1828{
1829 bool changed = false;
1830 int err;
1831
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001832 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001833 changed = true;
1834
1835 if (val) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07001836 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001837 } else {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001838 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1839 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001840 }
1841
1842 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1843 if (err < 0)
1844 return err;
1845
Johan Hedberg562064e2014-07-08 16:35:34 +03001846 if (changed) {
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02001847 hci_update_page_scan(hdev);
Johan Hedberg562064e2014-07-08 16:35:34 +03001848 hci_update_background_scan(hdev);
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001849 return new_settings(hdev, sk);
Johan Hedberg562064e2014-07-08 16:35:34 +03001850 }
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001851
1852 return 0;
1853}
1854
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001855static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001856 u16 len)
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001857{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001858 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001859 struct mgmt_pending_cmd *cmd;
Johan Hedberg2b76f452013-03-15 17:07:04 -05001860 struct hci_request req;
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001861 u8 scan;
Johan Hedbergf7b64e692010-12-13 21:07:06 +02001862 int err;
Johan Hedberg03811012010-12-08 00:21:06 +02001863
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001864 BT_DBG("request for %s", hdev->name);
Johan Hedberge41d8b42010-12-13 21:07:03 +02001865
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001866 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1867 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02001868 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1869 MGMT_STATUS_REJECTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001870
Johan Hedberga7e80f22013-01-09 16:05:19 +02001871 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001872 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1873 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001874
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001875 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001876
Johan Hedberg4b34ee782012-02-21 14:13:02 +02001877 if (!hdev_is_powered(hdev)) {
Johan Hedberge8ba3a1f2013-10-19 23:38:18 +03001878 err = set_connectable_update_settings(hdev, sk, cp->val);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001879 goto failed;
1880 }
1881
Johan Hedberg333ae952015-03-17 13:48:47 +02001882 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1883 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02001884 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1885 MGMT_STATUS_BUSY);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001886 goto failed;
1887 }
1888
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001889 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1890 if (!cmd) {
1891 err = -ENOMEM;
1892 goto failed;
1893 }
1894
Johan Hedberg2b76f452013-03-15 17:07:04 -05001895 hci_req_init(&req, hdev);
1896
Johan Hedberg9a43e252013-10-20 19:00:07 +03001897 /* If BR/EDR is not enabled and we disable advertising as a
1898 * by-product of disabling connectable, we need to update the
1899 * advertising flags.
1900 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001901 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg9a43e252013-10-20 19:00:07 +03001902 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07001903 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1904 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Johan Hedberg9a43e252013-10-20 19:00:07 +03001905 }
1906 update_adv_data(&req);
1907 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
Johan Hedberg9b742462013-10-14 16:20:03 +03001908 if (cp->val) {
1909 scan = SCAN_PAGE;
1910 } else {
Johan Hedberg3bd27242014-07-28 20:53:58 +03001911 /* If we don't have any whitelist entries just
1912 * disable all scanning. If there are entries
1913 * and we had both page and inquiry scanning
1914 * enabled then fall back to only page scanning.
1915 * Otherwise no changes are needed.
1916 */
1917 if (list_empty(&hdev->whitelist))
1918 scan = SCAN_DISABLED;
1919 else if (test_bit(HCI_ISCAN, &hdev->flags))
1920 scan = SCAN_PAGE;
1921 else
1922 goto no_scan_update;
Johan Hedberg9b742462013-10-14 16:20:03 +03001923
1924 if (test_bit(HCI_ISCAN, &hdev->flags) &&
Marcel Holtmann8d6083f2013-10-14 16:38:45 -07001925 hdev->discov_timeout > 0)
Johan Hedberg9b742462013-10-14 16:20:03 +03001926 cancel_delayed_work(&hdev->discov_off);
1927 }
1928
1929 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1930 }
Johan Hedberg2b76f452013-03-15 17:07:04 -05001931
Johan Hedberg3bd27242014-07-28 20:53:58 +03001932no_scan_update:
Johan Hedberge8b12022014-07-10 10:51:27 +03001933 /* Update the advertising parameters if necessary */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07001934 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001935 enable_advertising(&req);
Johan Hedberg1987fdc2013-10-14 21:15:24 +03001936
Johan Hedberg2b76f452013-03-15 17:07:04 -05001937 err = hci_req_run(&req, set_connectable_complete);
Johan Hedberg9b742462013-10-14 16:20:03 +03001938 if (err < 0) {
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001939 mgmt_pending_remove(cmd);
Johan Hedberg9b742462013-10-14 16:20:03 +03001940 if (err == -ENODATA)
Johan Hedberga81070b2013-10-19 23:38:19 +03001941 err = set_connectable_update_settings(hdev, sk,
1942 cp->val);
Johan Hedberg9b742462013-10-14 16:20:03 +03001943 goto failed;
1944 }
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001945
1946failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001947 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001948 return err;
1949}
1950
Johan Hedbergb2939472014-07-30 09:22:23 +03001951static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001952 u16 len)
Johan Hedberg73f22f62010-12-29 16:00:25 +02001953{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03001954 struct mgmt_mode *cp = data;
Marcel Holtmann55594352013-10-06 16:11:57 -07001955 bool changed;
Johan Hedberg73f22f62010-12-29 16:00:25 +02001956 int err;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001957
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001958 BT_DBG("request for %s", hdev->name);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001959
Johan Hedberga7e80f22013-01-09 16:05:19 +02001960 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001961 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
1962 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02001963
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001964 hci_dev_lock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001965
1966 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07001967 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001968 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07001969 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001970
Johan Hedbergb2939472014-07-30 09:22:23 +03001971 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001972 if (err < 0)
Marcel Holtmann55594352013-10-06 16:11:57 -07001973 goto unlock;
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001974
Marcel Holtmann55594352013-10-06 16:11:57 -07001975 if (changed)
1976 err = new_settings(hdev, sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001977
Marcel Holtmann55594352013-10-06 16:11:57 -07001978unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03001979 hci_dev_unlock(hdev);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02001980 return err;
1981}
Johan Hedberg72a734e2010-12-30 00:38:22 +02001982
Gustavo F. Padovan04124682012-03-08 01:25:00 -03001983static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1984 u16 len)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001985{
1986 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02001987 struct mgmt_pending_cmd *cmd;
Johan Hedberge6fe7982013-10-02 15:45:22 +03001988 u8 val, status;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001989 int err;
1990
Johan Hedbergbdb6d972012-02-28 06:13:32 +02001991 BT_DBG("request for %s", hdev->name);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02001992
Johan Hedberge6fe7982013-10-02 15:45:22 +03001993 status = mgmt_bredr_support(hdev);
1994 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02001995 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1996 status);
Johan Hedberg33c525c2012-10-24 21:11:58 +03001997
Johan Hedberga7e80f22013-01-09 16:05:19 +02001998 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02001999 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2000 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002001
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002002 hci_dev_lock(hdev);
2003
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002004 if (!hdev_is_powered(hdev)) {
Johan Hedberg47990ea2012-02-22 11:58:37 +02002005 bool changed = false;
2006
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002007 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07002008 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02002009 changed = true;
2010 }
2011
2012 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2013 if (err < 0)
2014 goto failed;
2015
2016 if (changed)
2017 err = new_settings(hdev, sk);
2018
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002019 goto failed;
2020 }
2021
Johan Hedberg333ae952015-03-17 13:48:47 +02002022 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002023 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2024 MGMT_STATUS_BUSY);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002025 goto failed;
2026 }
2027
2028 val = !!cp->val;
2029
2030 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
2031 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2032 goto failed;
2033 }
2034
2035 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
2036 if (!cmd) {
2037 err = -ENOMEM;
2038 goto failed;
2039 }
2040
2041 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
2042 if (err < 0) {
2043 mgmt_pending_remove(cmd);
2044 goto failed;
2045 }
2046
2047failed:
2048 hci_dev_unlock(hdev);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02002049 return err;
2050}
2051
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002052static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002053{
2054 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002055 struct mgmt_pending_cmd *cmd;
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002056 u8 status;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002057 int err;
2058
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002059 BT_DBG("request for %s", hdev->name);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002060
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002061 status = mgmt_bredr_support(hdev);
2062 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002063 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
Marcel Holtmanncdba5282013-10-02 21:31:52 -07002064
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002065 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002066 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2067 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002068
Johan Hedberga7e80f22013-01-09 16:05:19 +02002069 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002070 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2071 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002072
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002073 hci_dev_lock(hdev);
Johan Hedberg6c8f12c2012-02-22 16:35:26 +02002074
Johan Hedberg4b34ee782012-02-21 14:13:02 +02002075 if (!hdev_is_powered(hdev)) {
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002076 bool changed;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002077
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002078 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07002079 changed = !hci_dev_test_and_set_flag(hdev,
2080 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002081 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002082 changed = hci_dev_test_and_clear_flag(hdev,
2083 HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002084 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002085 changed = hci_dev_test_and_clear_flag(hdev,
2086 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002087 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002088 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02002089 }
2090
2091 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2092 if (err < 0)
2093 goto failed;
2094
2095 if (changed)
2096 err = new_settings(hdev, sk);
2097
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002098 goto failed;
2099 }
2100
Johan Hedberg333ae952015-03-17 13:48:47 +02002101 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002102 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2103 MGMT_STATUS_BUSY);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002104 goto failed;
2105 }
2106
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002107 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002108 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2109 goto failed;
2110 }
2111
2112 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
2113 if (!cmd) {
2114 err = -ENOMEM;
2115 goto failed;
2116 }
2117
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002118 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03002119 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2120 sizeof(cp->val), &cp->val);
2121
Marcel Holtmann72ef0c12013-10-10 03:08:10 -07002122 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002123 if (err < 0) {
2124 mgmt_pending_remove(cmd);
2125 goto failed;
2126 }
2127
2128failed:
2129 hci_dev_unlock(hdev);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02002130 return err;
2131}
2132
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002133static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002134{
2135 struct mgmt_mode *cp = data;
Marcel Holtmannee392692013-10-01 22:59:23 -07002136 bool changed;
Johan Hedberge6fe7982013-10-02 15:45:22 +03002137 u8 status;
Marcel Holtmannee392692013-10-01 22:59:23 -07002138 int err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002139
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002140 BT_DBG("request for %s", hdev->name);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002141
Johan Hedberge6fe7982013-10-02 15:45:22 +03002142 status = mgmt_bredr_support(hdev);
2143 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02002144 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002145
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002146 if (!lmp_ssp_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002147 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2148 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002149
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002150 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02002151 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2152 MGMT_STATUS_REJECTED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07002153
Johan Hedberga7e80f22013-01-09 16:05:19 +02002154 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002155 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2156 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002157
Marcel Holtmannee392692013-10-01 22:59:23 -07002158 hci_dev_lock(hdev);
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002159
Johan Hedberg333ae952015-03-17 13:48:47 +02002160 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002161 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2162 MGMT_STATUS_BUSY);
Johan Hedberga2cb01d2015-02-19 17:38:07 +02002163 goto unlock;
2164 }
2165
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002166 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07002167 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002168 } else {
2169 if (hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002170 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2171 MGMT_STATUS_REJECTED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002172 goto unlock;
2173 }
2174
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002175 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmanna0cdf9602013-10-02 00:27:02 -07002176 }
Marcel Holtmannee392692013-10-01 22:59:23 -07002177
2178 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
2179 if (err < 0)
2180 goto unlock;
2181
2182 if (changed)
2183 err = new_settings(hdev, sk);
2184
2185unlock:
2186 hci_dev_unlock(hdev);
2187 return err;
Johan Hedberg6d80dfd2012-02-20 23:50:38 +02002188}
2189
Marcel Holtmann1904a852015-01-11 13:50:44 -08002190static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002191{
2192 struct cmd_lookup match = { NULL, hdev };
2193
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302194 hci_dev_lock(hdev);
2195
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002196 if (status) {
2197 u8 mgmt_err = mgmt_status(status);
2198
2199 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
2200 &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302201 goto unlock;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002202 }
2203
2204 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
2205
2206 new_settings(hdev, match.sk);
2207
2208 if (match.sk)
2209 sock_put(match.sk);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002210
2211 /* Make sure the controller has a good default for
2212 * advertising data. Restrict the update to when LE
2213 * has actually been enabled. During power on, the
2214 * update in powered_update_hci will take care of it.
2215 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002216 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002217 struct hci_request req;
2218
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002219 hci_req_init(&req, hdev);
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07002220 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07002221 update_scan_rsp_data(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02002222 __hci_update_background_scan(&req);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002223 hci_req_run(&req, NULL);
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002224 }
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05302225
2226unlock:
2227 hci_dev_unlock(hdev);
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002228}
2229
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002230static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002231{
2232 struct mgmt_mode *cp = data;
2233 struct hci_cp_write_le_host_supported hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002234 struct mgmt_pending_cmd *cmd;
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002235 struct hci_request req;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002236 int err;
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002237 u8 val, enabled;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002238
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002239 BT_DBG("request for %s", hdev->name);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002240
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002241 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002242 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2243 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002244
Johan Hedberga7e80f22013-01-09 16:05:19 +02002245 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002246 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2247 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02002248
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07002249 /* Bluetooth single mode LE only controllers or dual-mode
2250 * controllers configured as LE only devices, do not allow
2251 * switching LE off. These have either LE enabled explicitly
2252 * or BR/EDR has been previously switched off.
2253 *
2254 * When trying to enable an already enabled LE, then gracefully
2255 * send a positive response. Trying to disable it however will
2256 * result into rejection.
2257 */
2258 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
2259 if (cp->val == 0x01)
2260 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2261
Johan Hedberga69e8372015-03-06 21:08:53 +02002262 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2263 MGMT_STATUS_REJECTED);
Marcel Holtmanne7844ee2015-03-18 16:15:07 -07002264 }
Johan Hedbergc73eee92013-04-19 18:35:21 +03002265
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002266 hci_dev_lock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002267
2268 val = !!cp->val;
Gustavo Padovanffa88e02012-11-23 16:50:51 -02002269 enabled = lmp_host_le_capable(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002270
Johan Hedberg0b60eba2012-02-28 00:57:24 +02002271 if (!hdev_is_powered(hdev) || val == enabled) {
Johan Hedberg06199cf2012-02-22 16:37:11 +02002272 bool changed = false;
2273
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002274 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07002275 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002276 changed = true;
2277 }
2278
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002279 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07002280 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03002281 changed = true;
2282 }
2283
Johan Hedberg06199cf2012-02-22 16:37:11 +02002284 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2285 if (err < 0)
Johan Hedberg1de028c2012-02-29 19:55:35 -08002286 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002287
2288 if (changed)
2289 err = new_settings(hdev, sk);
2290
Johan Hedberg1de028c2012-02-29 19:55:35 -08002291 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002292 }
2293
Johan Hedberg333ae952015-03-17 13:48:47 +02002294 if (pending_find(MGMT_OP_SET_LE, hdev) ||
2295 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002296 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2297 MGMT_STATUS_BUSY);
Johan Hedberg1de028c2012-02-29 19:55:35 -08002298 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002299 }
2300
2301 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2302 if (!cmd) {
2303 err = -ENOMEM;
Johan Hedberg1de028c2012-02-29 19:55:35 -08002304 goto unlock;
Johan Hedberg06199cf2012-02-22 16:37:11 +02002305 }
2306
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002307 hci_req_init(&req, hdev);
2308
Johan Hedberg06199cf2012-02-22 16:37:11 +02002309 memset(&hci_cp, 0, sizeof(hci_cp));
2310
2311 if (val) {
2312 hci_cp.le = val;
Marcel Holtmann32226e42014-07-24 20:04:16 +02002313 hci_cp.simul = 0x00;
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002314 } else {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07002315 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07002316 disable_advertising(&req);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002317 }
2318
Johan Hedberg416a4ae2013-09-25 13:26:08 +03002319 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
2320 &hci_cp);
2321
2322 err = hci_req_run(&req, le_enable_complete);
Syam Sidhardhan0c01bc42012-04-12 20:33:21 +05302323 if (err < 0)
Johan Hedberg06199cf2012-02-22 16:37:11 +02002324 mgmt_pending_remove(cmd);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002325
Johan Hedberg1de028c2012-02-29 19:55:35 -08002326unlock:
2327 hci_dev_unlock(hdev);
Johan Hedberg06199cf2012-02-22 16:37:11 +02002328 return err;
2329}
2330
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002331/* This is a helper function to test for pending mgmt commands that can
2332 * cause CoD or EIR HCI commands. We can only allow one such pending
2333 * mgmt command at a time since otherwise we cannot easily track what
2334 * the current values are, will be, and based on that calculate if a new
2335 * HCI command needs to be sent and if yes with what value.
2336 */
2337static bool pending_eir_or_class(struct hci_dev *hdev)
2338{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002339 struct mgmt_pending_cmd *cmd;
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002340
2341 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2342 switch (cmd->opcode) {
2343 case MGMT_OP_ADD_UUID:
2344 case MGMT_OP_REMOVE_UUID:
2345 case MGMT_OP_SET_DEV_CLASS:
2346 case MGMT_OP_SET_POWERED:
2347 return true;
2348 }
2349 }
2350
2351 return false;
2352}
2353
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002354static const u8 bluetooth_base_uuid[] = {
2355 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2356 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2357};
2358
2359static u8 get_uuid_size(const u8 *uuid)
2360{
2361 u32 val;
2362
2363 if (memcmp(uuid, bluetooth_base_uuid, 12))
2364 return 128;
2365
2366 val = get_unaligned_le32(&uuid[12]);
2367 if (val > 0xffff)
2368 return 32;
2369
2370 return 16;
2371}
2372
Johan Hedberg92da6092013-03-15 17:06:55 -05002373static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2374{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002375 struct mgmt_pending_cmd *cmd;
Johan Hedberg92da6092013-03-15 17:06:55 -05002376
2377 hci_dev_lock(hdev);
2378
Johan Hedberg333ae952015-03-17 13:48:47 +02002379 cmd = pending_find(mgmt_op, hdev);
Johan Hedberg92da6092013-03-15 17:06:55 -05002380 if (!cmd)
2381 goto unlock;
2382
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002383 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
2384 mgmt_status(status), hdev->dev_class, 3);
Johan Hedberg92da6092013-03-15 17:06:55 -05002385
2386 mgmt_pending_remove(cmd);
2387
2388unlock:
2389 hci_dev_unlock(hdev);
2390}
2391
Marcel Holtmann1904a852015-01-11 13:50:44 -08002392static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002393{
2394 BT_DBG("status 0x%02x", status);
2395
2396 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2397}
2398
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002399static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002400{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002401 struct mgmt_cp_add_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002402 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002403 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002404 struct bt_uuid *uuid;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002405 int err;
2406
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002407 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002408
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002409 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002410
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002411 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002412 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2413 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002414 goto failed;
2415 }
2416
Andre Guedes92c4c202012-06-07 19:05:44 -03002417 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002418 if (!uuid) {
2419 err = -ENOMEM;
2420 goto failed;
2421 }
2422
2423 memcpy(uuid->uuid, cp->uuid, 16);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002424 uuid->svc_hint = cp->svc_hint;
Johan Hedberg83be8ec2013-01-27 00:31:29 +02002425 uuid->size = get_uuid_size(cp->uuid);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002426
Johan Hedbergde66aa62013-01-27 00:31:27 +02002427 list_add_tail(&uuid->list, &hdev->uuids);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002428
Johan Hedberg890ea892013-03-15 17:06:52 -05002429 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002430
Johan Hedberg890ea892013-03-15 17:06:52 -05002431 update_class(&req);
2432 update_eir(&req);
2433
Johan Hedberg92da6092013-03-15 17:06:55 -05002434 err = hci_req_run(&req, add_uuid_complete);
2435 if (err < 0) {
2436 if (err != -ENODATA)
2437 goto failed;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002438
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002439 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
2440 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002441 goto failed;
2442 }
2443
2444 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002445 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002446 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002447 goto failed;
2448 }
2449
2450 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002451
2452failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002453 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002454 return err;
2455}
2456
Johan Hedberg24b78d02012-02-23 23:24:30 +02002457static bool enable_service_cache(struct hci_dev *hdev)
2458{
2459 if (!hdev_is_powered(hdev))
2460 return false;
2461
Marcel Holtmann238be782015-03-13 02:11:06 -07002462 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg46818ed2013-01-14 22:33:52 +02002463 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2464 CACHE_TIMEOUT);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002465 return true;
2466 }
2467
2468 return false;
2469}
2470
Marcel Holtmann1904a852015-01-11 13:50:44 -08002471static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002472{
2473 BT_DBG("status 0x%02x", status);
2474
2475 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2476}
2477
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002478static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002479 u16 len)
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002480{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002481 struct mgmt_cp_remove_uuid *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002482 struct mgmt_pending_cmd *cmd;
Johan Hedberg056341c2013-01-27 00:31:30 +02002483 struct bt_uuid *match, *tmp;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002484 u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
Johan Hedberg890ea892013-03-15 17:06:52 -05002485 struct hci_request req;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002486 int err, found;
2487
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002488 BT_DBG("request for %s", hdev->name);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002489
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002490 hci_dev_lock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002491
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002492 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002493 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2494 MGMT_STATUS_BUSY);
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02002495 goto unlock;
2496 }
2497
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002498 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
Johan Hedberg35f74982014-02-18 17:14:32 +02002499 hci_uuids_clear(hdev);
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002500
Johan Hedberg24b78d02012-02-23 23:24:30 +02002501 if (enable_service_cache(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002502 err = mgmt_cmd_complete(sk, hdev->id,
2503 MGMT_OP_REMOVE_UUID,
2504 0, hdev->dev_class, 3);
Johan Hedberg24b78d02012-02-23 23:24:30 +02002505 goto unlock;
2506 }
Johan Hedberg4004b6d2012-02-23 21:30:12 +02002507
Johan Hedberg9246a862012-02-23 21:33:16 +02002508 goto update_class;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002509 }
2510
2511 found = 0;
2512
Johan Hedberg056341c2013-01-27 00:31:30 +02002513 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002514 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2515 continue;
2516
2517 list_del(&match->list);
Johan Hedberg482049f2012-11-08 10:25:26 +01002518 kfree(match);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002519 found++;
2520 }
2521
2522 if (found == 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002523 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2524 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002525 goto unlock;
2526 }
2527
Johan Hedberg9246a862012-02-23 21:33:16 +02002528update_class:
Johan Hedberg890ea892013-03-15 17:06:52 -05002529 hci_req_init(&req, hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002530
Johan Hedberg890ea892013-03-15 17:06:52 -05002531 update_class(&req);
2532 update_eir(&req);
2533
Johan Hedberg92da6092013-03-15 17:06:55 -05002534 err = hci_req_run(&req, remove_uuid_complete);
2535 if (err < 0) {
2536 if (err != -ENODATA)
2537 goto unlock;
Johan Hedberg80a1e1d2011-03-28 14:07:23 +03002538
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002539 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2540 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002541 goto unlock;
2542 }
2543
2544 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002545 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002546 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002547 goto unlock;
2548 }
2549
2550 err = 0;
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002551
2552unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002553 hci_dev_unlock(hdev);
Johan Hedberg2aeb9a12011-01-04 12:08:51 +02002554 return err;
2555}
2556
Marcel Holtmann1904a852015-01-11 13:50:44 -08002557static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg92da6092013-03-15 17:06:55 -05002558{
2559 BT_DBG("status 0x%02x", status);
2560
2561 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2562}
2563
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002564static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002565 u16 len)
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002566{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002567 struct mgmt_cp_set_dev_class *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002568 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05002569 struct hci_request req;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002570 int err;
2571
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002572 BT_DBG("request for %s", hdev->name);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002573
Marcel Holtmann6203fc92013-10-02 23:37:29 -07002574 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002575 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2576 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg13ecd8b2013-01-09 15:29:38 +02002577
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002578 hci_dev_lock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002579
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002580 if (pending_eir_or_class(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002581 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2582 MGMT_STATUS_BUSY);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002583 goto unlock;
2584 }
2585
2586 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002587 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2588 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0cab9c82013-03-15 17:06:54 -05002589 goto unlock;
2590 }
2591
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002592 hdev->major_class = cp->major;
2593 hdev->minor_class = cp->minor;
2594
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002595 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002596 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2597 hdev->dev_class, 3);
Johan Hedberg932f5ff2012-02-22 22:11:32 +02002598 goto unlock;
2599 }
2600
Johan Hedberg890ea892013-03-15 17:06:52 -05002601 hci_req_init(&req, hdev);
2602
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002603 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
Johan Hedberg7d785252011-12-15 00:47:39 +02002604 hci_dev_unlock(hdev);
2605 cancel_delayed_work_sync(&hdev->service_cache);
2606 hci_dev_lock(hdev);
Johan Hedberg890ea892013-03-15 17:06:52 -05002607 update_eir(&req);
Johan Hedberg7d785252011-12-15 00:47:39 +02002608 }
Johan Hedberg14c0b602011-12-15 00:47:37 +02002609
Johan Hedberg890ea892013-03-15 17:06:52 -05002610 update_class(&req);
2611
Johan Hedberg92da6092013-03-15 17:06:55 -05002612 err = hci_req_run(&req, set_class_complete);
2613 if (err < 0) {
2614 if (err != -ENODATA)
2615 goto unlock;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002616
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002617 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2618 hdev->dev_class, 3);
Johan Hedberg90e70452012-02-23 23:09:40 +02002619 goto unlock;
2620 }
2621
2622 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
Johan Hedberg890ea892013-03-15 17:06:52 -05002623 if (!cmd) {
Johan Hedberg90e70452012-02-23 23:09:40 +02002624 err = -ENOMEM;
Johan Hedberg890ea892013-03-15 17:06:52 -05002625 goto unlock;
2626 }
2627
2628 err = 0;
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002629
Johan Hedbergb5235a62012-02-21 14:32:24 +02002630unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002631 hci_dev_unlock(hdev);
Johan Hedberg1aff6f02011-01-13 21:56:52 +02002632 return err;
2633}
2634
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002635static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002636 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002637{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002638 struct mgmt_cp_load_link_keys *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03002639 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2640 sizeof(struct mgmt_link_key_info));
Szymon Janc4e51eae2011-02-25 19:05:48 +01002641 u16 key_count, expected_len;
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002642 bool changed;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002643 int i;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002644
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002645 BT_DBG("request for %s", hdev->name);
2646
2647 if (!lmp_bredr_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02002648 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2649 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9060d5c2013-10-02 21:16:07 -07002650
Marcel Holtmann1f350c82012-03-12 20:31:08 -07002651 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002652 if (key_count > max_key_count) {
2653 BT_ERR("load_link_keys: too big key_count value %u",
2654 key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02002655 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2656 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03002657 }
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002658
Johan Hedberg86742e12011-11-07 23:13:38 +02002659 expected_len = sizeof(*cp) + key_count *
2660 sizeof(struct mgmt_link_key_info);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002661 if (expected_len != len) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002662 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02002663 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02002664 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2665 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002666 }
2667
Johan Hedberg4ae14302013-01-20 14:27:13 +02002668 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02002669 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2670 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ae14302013-01-20 14:27:13 +02002671
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002672 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03002673 key_count);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002674
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002675 for (i = 0; i < key_count; i++) {
2676 struct mgmt_link_key_info *key = &cp->keys[i];
2677
Marcel Holtmann8e991132014-01-10 02:07:25 -08002678 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
Johan Hedberga69e8372015-03-06 21:08:53 +02002679 return mgmt_cmd_status(sk, hdev->id,
2680 MGMT_OP_LOAD_LINK_KEYS,
2681 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002682 }
2683
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002684 hci_dev_lock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002685
2686 hci_link_keys_clear(hdev);
2687
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002688 if (cp->debug_keys)
Marcel Holtmann238be782015-03-13 02:11:06 -07002689 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002690 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07002691 changed = hci_dev_test_and_clear_flag(hdev,
2692 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmannb1de97d2014-01-31 11:55:21 -08002693
2694 if (changed)
2695 new_settings(hdev, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002696
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002697 for (i = 0; i < key_count; i++) {
Johan Hedberg86742e12011-11-07 23:13:38 +02002698 struct mgmt_link_key_info *key = &cp->keys[i];
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002699
Johan Hedberg58e92932014-06-24 14:00:26 +03002700 /* Always ignore debug keys and require a new pairing if
2701 * the user wants to use them.
2702 */
2703 if (key->type == HCI_LK_DEBUG_COMBINATION)
2704 continue;
2705
Johan Hedberg7652ff62014-06-24 13:15:49 +03002706 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2707 key->type, key->pin_len, NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002708 }
2709
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002710 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
Johan Hedberg0e5f8752011-11-11 16:18:54 +02002711
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002712 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002713
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03002714 return 0;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002715}
2716
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002717static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002718 u8 addr_type, struct sock *skip_sk)
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002719{
2720 struct mgmt_ev_device_unpaired ev;
2721
2722 bacpy(&ev.addr.bdaddr, bdaddr);
2723 ev.addr.type = addr_type;
2724
2725 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002726 skip_sk);
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002727}
2728
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002729static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002730 u16 len)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002731{
Johan Hedberg124f6e32012-02-09 13:50:12 +02002732 struct mgmt_cp_unpair_device *cp = data;
2733 struct mgmt_rp_unpair_device rp;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002734 struct hci_cp_disconnect dc;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002735 struct mgmt_pending_cmd *cmd;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002736 struct hci_conn *conn;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002737 int err;
2738
Johan Hedberga8a1d192011-11-10 15:54:38 +02002739 memset(&rp, 0, sizeof(rp));
Johan Hedberg124f6e32012-02-09 13:50:12 +02002740 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2741 rp.addr.type = cp->addr.type;
Johan Hedberga8a1d192011-11-10 15:54:38 +02002742
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002743 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002744 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2745 MGMT_STATUS_INVALID_PARAMS,
2746 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002747
Johan Hedberg118da702013-01-20 14:27:20 +02002748 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002749 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2750 MGMT_STATUS_INVALID_PARAMS,
2751 &rp, sizeof(rp));
Johan Hedberg118da702013-01-20 14:27:20 +02002752
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002753 hci_dev_lock(hdev);
2754
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002755 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002756 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2757 MGMT_STATUS_NOT_POWERED, &rp,
2758 sizeof(rp));
Johan Hedberg86a8cfc2012-02-22 22:53:34 +02002759 goto unlock;
2760 }
2761
Johan Hedberge0b2b272014-02-18 17:14:31 +02002762 if (cp->addr.type == BDADDR_BREDR) {
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002763 /* If disconnection is requested, then look up the
2764 * connection. If the remote device is connected, it
2765 * will be later used to terminate the link.
2766 *
2767 * Setting it to NULL explicitly will cause no
2768 * termination of the link.
2769 */
2770 if (cp->disconnect)
2771 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2772 &cp->addr.bdaddr);
2773 else
2774 conn = NULL;
2775
Johan Hedberg124f6e32012-02-09 13:50:12 +02002776 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
Johan Hedberge0b2b272014-02-18 17:14:31 +02002777 } else {
2778 u8 addr_type;
2779
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002780 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
2781 &cp->addr.bdaddr);
2782 if (conn) {
2783 /* Defer clearing up the connection parameters
2784 * until closing to give a chance of keeping
2785 * them if a repairing happens.
2786 */
2787 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
2788
2789 /* If disconnection is not requested, then
2790 * clear the connection variable so that the
2791 * link is not terminated.
2792 */
2793 if (!cp->disconnect)
2794 conn = NULL;
2795 }
2796
Johan Hedberge0b2b272014-02-18 17:14:31 +02002797 if (cp->addr.type == BDADDR_LE_PUBLIC)
2798 addr_type = ADDR_LE_DEV_PUBLIC;
2799 else
2800 addr_type = ADDR_LE_DEV_RANDOM;
2801
Johan Hedberga7ec7332014-02-18 17:14:35 +02002802 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
2803
Johan Hedberge0b2b272014-02-18 17:14:31 +02002804 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
2805 }
Vinicius Costa Gomesb0dbfb42012-02-02 21:08:03 -03002806
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002807 if (err < 0) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002808 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2809 MGMT_STATUS_NOT_PAIRED, &rp,
2810 sizeof(rp));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002811 goto unlock;
2812 }
2813
Alfonso Acosta89cbb062014-10-11 21:44:47 +00002814 /* If the connection variable is set, then termination of the
2815 * link is requested.
2816 */
Johan Hedberga8a1d192011-11-10 15:54:38 +02002817 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002818 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
2819 &rp, sizeof(rp));
Johan Hedbergb1078ad2012-02-09 17:21:16 +02002820 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002821 goto unlock;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002822 }
2823
Johan Hedberg124f6e32012-02-09 13:50:12 +02002824 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002825 sizeof(*cp));
Johan Hedberga8a1d192011-11-10 15:54:38 +02002826 if (!cmd) {
2827 err = -ENOMEM;
2828 goto unlock;
2829 }
2830
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02002831 cmd->cmd_complete = addr_cmd_complete;
2832
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002833 dc.handle = cpu_to_le16(conn->handle);
Johan Hedberga8a1d192011-11-10 15:54:38 +02002834 dc.reason = 0x13; /* Remote User Terminated Connection */
2835 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
2836 if (err < 0)
2837 mgmt_pending_remove(cmd);
2838
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002839unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002840 hci_dev_unlock(hdev);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02002841 return err;
2842}
2843
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002844static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002845 u16 len)
Johan Hedberg8962ee72011-01-20 12:40:27 +02002846{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03002847 struct mgmt_cp_disconnect *cp = data;
Johan Hedberg06a63b12013-01-20 14:27:21 +02002848 struct mgmt_rp_disconnect rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002849 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002850 struct hci_conn *conn;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002851 int err;
2852
2853 BT_DBG("");
2854
Johan Hedberg06a63b12013-01-20 14:27:21 +02002855 memset(&rp, 0, sizeof(rp));
2856 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2857 rp.addr.type = cp->addr.type;
2858
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002859 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002860 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2861 MGMT_STATUS_INVALID_PARAMS,
2862 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02002863
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002864 hci_dev_lock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002865
2866 if (!test_bit(HCI_UP, &hdev->flags)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002867 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2868 MGMT_STATUS_NOT_POWERED, &rp,
2869 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002870 goto failed;
2871 }
2872
Johan Hedberg333ae952015-03-17 13:48:47 +02002873 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002874 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2875 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002876 goto failed;
2877 }
2878
Andre Guedes591f47f2012-04-24 21:02:49 -03002879 if (cp->addr.type == BDADDR_BREDR)
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03002880 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2881 &cp->addr.bdaddr);
Johan Hedberg88c3df12012-02-09 14:27:38 +02002882 else
2883 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
Vinicius Costa Gomes365227e2011-05-06 18:41:44 -03002884
Vishal Agarwalf9607272012-06-13 05:32:43 +05302885 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002886 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
2887 MGMT_STATUS_NOT_CONNECTED, &rp,
2888 sizeof(rp));
Johan Hedberg8962ee72011-01-20 12:40:27 +02002889 goto failed;
2890 }
2891
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002892 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03002893 if (!cmd) {
2894 err = -ENOMEM;
Johan Hedberg8962ee72011-01-20 12:40:27 +02002895 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03002896 }
Johan Hedberg8962ee72011-01-20 12:40:27 +02002897
Johan Hedbergf5818c22014-12-05 13:36:02 +02002898 cmd->cmd_complete = generic_cmd_complete;
2899
Johan Hedberge3f2f922014-08-18 20:33:33 +03002900 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002901 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03002902 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002903
2904failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002905 hci_dev_unlock(hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02002906 return err;
2907}
2908
Andre Guedes57c14772012-04-24 21:02:50 -03002909static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002910{
2911 switch (link_type) {
2912 case LE_LINK:
Johan Hedberg48264f02011-11-09 13:58:58 +02002913 switch (addr_type) {
2914 case ADDR_LE_DEV_PUBLIC:
Andre Guedes591f47f2012-04-24 21:02:49 -03002915 return BDADDR_LE_PUBLIC;
Andre Guedes0ed09142012-04-03 08:46:54 -03002916
Johan Hedberg48264f02011-11-09 13:58:58 +02002917 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002918 /* Fallback to LE Random address type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002919 return BDADDR_LE_RANDOM;
Johan Hedberg48264f02011-11-09 13:58:58 +02002920 }
Andre Guedes0ed09142012-04-03 08:46:54 -03002921
Johan Hedberg4c659c32011-11-07 23:13:39 +02002922 default:
Andre Guedes0ed09142012-04-03 08:46:54 -03002923 /* Fallback to BR/EDR type */
Andre Guedes591f47f2012-04-24 21:02:49 -03002924 return BDADDR_BREDR;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002925 }
2926}
2927
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002928static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
2929 u16 data_len)
Johan Hedberg2784eb42011-01-21 13:56:35 +02002930{
Johan Hedberg2784eb42011-01-21 13:56:35 +02002931 struct mgmt_rp_get_connections *rp;
Luiz Augusto von Dentz8035ded2011-11-01 10:58:56 +02002932 struct hci_conn *c;
Johan Hedberga38528f2011-01-22 06:46:43 +02002933 size_t rp_len;
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002934 int err;
2935 u16 i;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002936
2937 BT_DBG("");
2938
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002939 hci_dev_lock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002940
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002941 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02002942 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
2943 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002944 goto unlock;
2945 }
2946
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002947 i = 0;
Johan Hedbergb644ba32012-01-17 21:48:47 +02002948 list_for_each_entry(c, &hdev->conn_hash.list, list) {
2949 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002950 i++;
Johan Hedberg2784eb42011-01-21 13:56:35 +02002951 }
2952
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002953 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Andre Guedes92c4c202012-06-07 19:05:44 -03002954 rp = kmalloc(rp_len, GFP_KERNEL);
Johan Hedberga38528f2011-01-22 06:46:43 +02002955 if (!rp) {
Johan Hedberg2784eb42011-01-21 13:56:35 +02002956 err = -ENOMEM;
2957 goto unlock;
2958 }
2959
Johan Hedberg2784eb42011-01-21 13:56:35 +02002960 i = 0;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002961 list_for_each_entry(c, &hdev->conn_hash.list, list) {
Johan Hedbergb644ba32012-01-17 21:48:47 +02002962 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
2963 continue;
Johan Hedberg4c659c32011-11-07 23:13:39 +02002964 bacpy(&rp->addr[i].bdaddr, &c->dst);
Andre Guedes57c14772012-04-24 21:02:50 -03002965 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
Andre Guedes0ed09142012-04-03 08:46:54 -03002966 if (c->type == SCO_LINK || c->type == ESCO_LINK)
Johan Hedberg4c659c32011-11-07 23:13:39 +02002967 continue;
2968 i++;
2969 }
2970
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02002971 rp->conn_count = cpu_to_le16(i);
Johan Hedberg60fc5fb62012-02-23 09:52:28 +02002972
Johan Hedberg4c659c32011-11-07 23:13:39 +02002973 /* Recalculate length in case of filtered SCO connections, etc */
2974 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
Johan Hedberg2784eb42011-01-21 13:56:35 +02002975
Johan Hedberg2a1afb52015-03-06 21:08:54 +02002976 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
2977 rp_len);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002978
Johan Hedberga38528f2011-01-22 06:46:43 +02002979 kfree(rp);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02002980
2981unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03002982 hci_dev_unlock(hdev);
Johan Hedberg2784eb42011-01-21 13:56:35 +02002983 return err;
2984}
2985
Johan Hedbergbdb6d972012-02-28 06:13:32 +02002986static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002987 struct mgmt_cp_pin_code_neg_reply *cp)
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002988{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02002989 struct mgmt_pending_cmd *cmd;
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002990 int err;
2991
Johan Hedberg2e58ef32011-11-08 20:40:15 +02002992 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002993 sizeof(*cp));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002994 if (!cmd)
2995 return -ENOMEM;
2996
Johan Hedbergd8457692012-02-17 14:24:57 +02002997 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03002998 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02002999 if (err < 0)
3000 mgmt_pending_remove(cmd);
3001
3002 return err;
3003}
3004
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003005static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003006 u16 len)
Johan Hedberg980e1a52011-01-22 06:10:07 +02003007{
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003008 struct hci_conn *conn;
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003009 struct mgmt_cp_pin_code_reply *cp = data;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003010 struct hci_cp_pin_code_reply reply;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003011 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003012 int err;
3013
3014 BT_DBG("");
3015
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003016 hci_dev_lock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003017
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003018 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003019 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3020 MGMT_STATUS_NOT_POWERED);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003021 goto failed;
3022 }
3023
Johan Hedbergd8457692012-02-17 14:24:57 +02003024 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003025 if (!conn) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003026 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3027 MGMT_STATUS_NOT_CONNECTED);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003028 goto failed;
3029 }
3030
3031 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
Johan Hedbergd8457692012-02-17 14:24:57 +02003032 struct mgmt_cp_pin_code_neg_reply ncp;
3033
3034 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003035
3036 BT_ERR("PIN code is not 16 bytes long");
3037
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003038 err = send_pin_code_neg_reply(sk, hdev, &ncp);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003039 if (err >= 0)
Johan Hedberga69e8372015-03-06 21:08:53 +02003040 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3041 MGMT_STATUS_INVALID_PARAMS);
Waldemar Rymarkiewicz96d97a62011-06-01 17:28:48 +02003042
3043 goto failed;
3044 }
3045
Gustavo F. Padovan00abfe42012-03-01 00:37:10 -03003046 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
Johan Hedberg366a0332011-02-19 12:05:55 -03003047 if (!cmd) {
3048 err = -ENOMEM;
Johan Hedberg980e1a52011-01-22 06:10:07 +02003049 goto failed;
Johan Hedberg366a0332011-02-19 12:05:55 -03003050 }
Johan Hedberg980e1a52011-01-22 06:10:07 +02003051
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003052 cmd->cmd_complete = addr_cmd_complete;
3053
Johan Hedbergd8457692012-02-17 14:24:57 +02003054 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003055 reply.pin_len = cp->pin_len;
Waldemar Rymarkiewicz24718ca2011-06-01 17:28:47 +02003056 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
Johan Hedberg980e1a52011-01-22 06:10:07 +02003057
3058 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
3059 if (err < 0)
Johan Hedberga664b5b2011-02-19 12:06:02 -03003060 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003061
3062failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003063 hci_dev_unlock(hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02003064 return err;
3065}
3066
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003067static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3068 u16 len)
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003069{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003070 struct mgmt_cp_set_io_capability *cp = data;
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003071
3072 BT_DBG("");
3073
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003074 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003075 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3076 MGMT_STATUS_INVALID_PARAMS, NULL, 0);
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003077
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003078 hci_dev_lock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003079
3080 hdev->io_capability = cp->io_capability;
3081
3082 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003083 hdev->io_capability);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003084
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003085 hci_dev_unlock(hdev);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003086
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003087 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
3088 NULL, 0);
Johan Hedberg17fa4b92011-01-25 13:28:33 +02003089}
3090
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003091static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003092{
3093 struct hci_dev *hdev = conn->hdev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003094 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003095
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003096 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
Johan Hedberge9a416b2011-02-19 12:05:56 -03003097 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3098 continue;
3099
Johan Hedberge9a416b2011-02-19 12:05:56 -03003100 if (cmd->user_data != conn)
3101 continue;
3102
3103 return cmd;
3104 }
3105
3106 return NULL;
3107}
3108
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003109static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003110{
3111 struct mgmt_rp_pair_device rp;
3112 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9df74652014-12-19 22:26:03 +02003113 int err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003114
Johan Hedberg61b1a7f2014-03-20 12:54:16 +02003115 bacpy(&rp.addr.bdaddr, &conn->dst);
3116 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003117
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003118 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
3119 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003120
3121 /* So we don't get further callbacks for this connection */
3122 conn->connect_cfm_cb = NULL;
3123 conn->security_cfm_cb = NULL;
3124 conn->disconn_cfm_cb = NULL;
3125
David Herrmann76a68ba2013-04-06 20:28:37 +02003126 hci_conn_drop(conn);
Alfonso Acosta89cbb062014-10-11 21:44:47 +00003127
3128 /* The device is paired so there is no need to remove
3129 * its connection parameters anymore.
3130 */
3131 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
Johan Hedberg15013ae2014-12-11 21:45:44 +02003132
3133 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02003134
3135 return err;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003136}
3137
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003138void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3139{
3140 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003141 struct mgmt_pending_cmd *cmd;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003142
3143 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003144 if (cmd) {
Johan Hedberg04ab2742014-12-05 13:36:04 +02003145 cmd->cmd_complete(cmd, status);
Johan Hedberga511b352014-12-11 21:45:45 +02003146 mgmt_pending_remove(cmd);
3147 }
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003148}
3149
Johan Hedberge9a416b2011-02-19 12:05:56 -03003150static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3151{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003152 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003153
3154 BT_DBG("status %u", status);
3155
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003156 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003157 if (!cmd) {
Johan Hedberg56e5cb82011-11-08 20:40:16 +02003158 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003159 return;
3160 }
3161
3162 cmd->cmd_complete(cmd, mgmt_status(status));
3163 mgmt_pending_remove(cmd);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003164}
3165
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003166static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303167{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003168 struct mgmt_pending_cmd *cmd;
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303169
3170 BT_DBG("status %u", status);
3171
3172 if (!status)
3173 return;
3174
3175 cmd = find_pairing(conn);
Johan Hedberga511b352014-12-11 21:45:45 +02003176 if (!cmd) {
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303177 BT_DBG("Unable to find a pending command");
Johan Hedberga511b352014-12-11 21:45:45 +02003178 return;
3179 }
3180
3181 cmd->cmd_complete(cmd, mgmt_status(status));
3182 mgmt_pending_remove(cmd);
Vishal Agarwal4c47d732012-06-07 20:27:35 +05303183}
3184
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003185static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003186 u16 len)
Johan Hedberge9a416b2011-02-19 12:05:56 -03003187{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003188 struct mgmt_cp_pair_device *cp = data;
Johan Hedberg1425acb2011-11-11 00:07:35 +02003189 struct mgmt_rp_pair_device rp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003190 struct mgmt_pending_cmd *cmd;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003191 u8 sec_level, auth_type;
3192 struct hci_conn *conn;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003193 int err;
3194
3195 BT_DBG("");
3196
Szymon Jancf950a30e2013-01-18 12:48:07 +01003197 memset(&rp, 0, sizeof(rp));
3198 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3199 rp.addr.type = cp->addr.type;
3200
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003201 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003202 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3203 MGMT_STATUS_INVALID_PARAMS,
3204 &rp, sizeof(rp));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02003205
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003206 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003207 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3208 MGMT_STATUS_INVALID_PARAMS,
3209 &rp, sizeof(rp));
Johan Hedberg4ec86d42014-06-17 15:14:48 +03003210
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003211 hci_dev_lock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003212
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003213 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003214 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3215 MGMT_STATUS_NOT_POWERED, &rp,
3216 sizeof(rp));
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003217 goto unlock;
3218 }
3219
Johan Hedberg55e76b32015-03-10 22:34:40 +02003220 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
3221 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3222 MGMT_STATUS_ALREADY_PAIRED, &rp,
3223 sizeof(rp));
3224 goto unlock;
3225 }
3226
Vinicius Costa Gomesc908df32011-09-02 14:51:22 -03003227 sec_level = BT_SECURITY_MEDIUM;
Mikel Astiz6fd6b912014-04-08 14:21:32 +02003228 auth_type = HCI_AT_DEDICATED_BONDING;
Johan Hedberge9a416b2011-02-19 12:05:56 -03003229
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003230 if (cp->addr.type == BDADDR_BREDR) {
Andre Guedes04a6c582014-02-26 20:21:44 -03003231 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
3232 auth_type);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003233 } else {
3234 u8 addr_type;
3235
3236 /* Convert from L2CAP channel address type to HCI address type
3237 */
3238 if (cp->addr.type == BDADDR_LE_PUBLIC)
3239 addr_type = ADDR_LE_DEV_PUBLIC;
3240 else
3241 addr_type = ADDR_LE_DEV_RANDOM;
3242
Marcel Holtmann7c264b12014-06-30 12:34:40 +02003243 /* When pairing a new device, it is expected to remember
3244 * this device for future connections. Adding the connection
3245 * parameter information ahead of time allows tracking
3246 * of the slave preferred values and will speed up any
3247 * further connection establishment.
3248 *
3249 * If connection parameters already exist, then they
3250 * will be kept and this function does nothing.
3251 */
3252 hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
3253
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003254 conn = hci_connect_le(hdev, &cp->addr.bdaddr, addr_type,
Johan Hedberge804d252014-07-16 11:42:28 +03003255 sec_level, HCI_LE_CONN_TIMEOUT,
3256 HCI_ROLE_MASTER);
Andre Guedes6f77d8c2014-02-26 20:21:45 -03003257 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003258
Ville Tervo30e76272011-02-22 16:10:53 -03003259 if (IS_ERR(conn)) {
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003260 int status;
3261
3262 if (PTR_ERR(conn) == -EBUSY)
3263 status = MGMT_STATUS_BUSY;
Lukasz Rymanowskifaa81032015-02-11 12:31:42 +01003264 else if (PTR_ERR(conn) == -EOPNOTSUPP)
3265 status = MGMT_STATUS_NOT_SUPPORTED;
3266 else if (PTR_ERR(conn) == -ECONNREFUSED)
3267 status = MGMT_STATUS_REJECTED;
Andrzej Kaczmarek489dc482012-05-30 15:39:22 +02003268 else
3269 status = MGMT_STATUS_CONNECT_FAILED;
3270
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003271 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3272 status, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003273 goto unlock;
3274 }
3275
3276 if (conn->connect_cfm_cb) {
David Herrmann76a68ba2013-04-06 20:28:37 +02003277 hci_conn_drop(conn);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003278 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3279 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberge9a416b2011-02-19 12:05:56 -03003280 goto unlock;
3281 }
3282
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003283 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003284 if (!cmd) {
3285 err = -ENOMEM;
David Herrmann76a68ba2013-04-06 20:28:37 +02003286 hci_conn_drop(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003287 goto unlock;
3288 }
3289
Johan Hedberg04ab2742014-12-05 13:36:04 +02003290 cmd->cmd_complete = pairing_complete;
3291
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003292 /* For LE, just connecting isn't a proof that the pairing finished */
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003293 if (cp->addr.type == BDADDR_BREDR) {
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003294 conn->connect_cfm_cb = pairing_complete_cb;
Johan Hedbergf4a407b2014-02-18 21:41:34 +02003295 conn->security_cfm_cb = pairing_complete_cb;
3296 conn->disconn_cfm_cb = pairing_complete_cb;
3297 } else {
3298 conn->connect_cfm_cb = le_pairing_complete_cb;
3299 conn->security_cfm_cb = le_pairing_complete_cb;
3300 conn->disconn_cfm_cb = le_pairing_complete_cb;
3301 }
Vinicius Costa Gomes7a512d02011-08-19 21:06:54 -03003302
Johan Hedberge9a416b2011-02-19 12:05:56 -03003303 conn->io_capability = cp->io_cap;
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03003304 cmd->user_data = hci_conn_get(conn);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003305
Johan Hedberg6f78fd42014-07-30 08:35:48 +03003306 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
Johan Hedberga511b352014-12-11 21:45:45 +02003307 hci_conn_security(conn, sec_level, auth_type, true)) {
3308 cmd->cmd_complete(cmd, 0);
3309 mgmt_pending_remove(cmd);
3310 }
Johan Hedberge9a416b2011-02-19 12:05:56 -03003311
3312 err = 0;
3313
3314unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003315 hci_dev_unlock(hdev);
Johan Hedberge9a416b2011-02-19 12:05:56 -03003316 return err;
3317}
3318
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003319static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3320 u16 len)
Johan Hedberg28424702012-02-02 04:02:29 +02003321{
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003322 struct mgmt_addr_info *addr = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003323 struct mgmt_pending_cmd *cmd;
Johan Hedberg28424702012-02-02 04:02:29 +02003324 struct hci_conn *conn;
3325 int err;
3326
3327 BT_DBG("");
3328
Johan Hedberg28424702012-02-02 04:02:29 +02003329 hci_dev_lock(hdev);
3330
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003331 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003332 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3333 MGMT_STATUS_NOT_POWERED);
Johan Hedberg5f97c1d2012-02-22 22:41:18 +02003334 goto unlock;
3335 }
3336
Johan Hedberg333ae952015-03-17 13:48:47 +02003337 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003338 if (!cmd) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003339 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3340 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003341 goto unlock;
3342 }
3343
3344 conn = cmd->user_data;
3345
3346 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003347 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3348 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg28424702012-02-02 04:02:29 +02003349 goto unlock;
3350 }
3351
Johan Hedberga511b352014-12-11 21:45:45 +02003352 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3353 mgmt_pending_remove(cmd);
Johan Hedberg28424702012-02-02 04:02:29 +02003354
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003355 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3356 addr, sizeof(*addr));
Johan Hedberg28424702012-02-02 04:02:29 +02003357unlock:
3358 hci_dev_unlock(hdev);
Johan Hedberg28424702012-02-02 04:02:29 +02003359 return err;
3360}
3361
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003362static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
Johan Hedberg1707c602013-03-15 17:07:15 -05003363 struct mgmt_addr_info *addr, u16 mgmt_op,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003364 u16 hci_op, __le32 passkey)
Johan Hedberga5c29682011-02-19 12:05:57 -03003365{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003366 struct mgmt_pending_cmd *cmd;
Brian Gix0df4c182011-11-16 13:53:13 -08003367 struct hci_conn *conn;
Johan Hedberga5c29682011-02-19 12:05:57 -03003368 int err;
3369
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003370 hci_dev_lock(hdev);
Johan Hedberg08ba5382011-03-16 14:29:34 +02003371
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003372 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003373 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3374 MGMT_STATUS_NOT_POWERED, addr,
3375 sizeof(*addr));
Brian Gix0df4c182011-11-16 13:53:13 -08003376 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003377 }
3378
Johan Hedberg1707c602013-03-15 17:07:15 -05003379 if (addr->type == BDADDR_BREDR)
3380 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
Johan Hedberg272d90d2012-02-09 15:26:12 +02003381 else
Johan Hedberg1707c602013-03-15 17:07:15 -05003382 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr);
Brian Gix47c15e22011-11-16 13:53:14 -08003383
Johan Hedberg272d90d2012-02-09 15:26:12 +02003384 if (!conn) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003385 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3386 MGMT_STATUS_NOT_CONNECTED, addr,
3387 sizeof(*addr));
Johan Hedberg272d90d2012-02-09 15:26:12 +02003388 goto done;
3389 }
3390
Johan Hedberg1707c602013-03-15 17:07:15 -05003391 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
Brian Gix5fe57d92011-12-21 16:12:13 -08003392 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
Brian Gix5fe57d92011-12-21 16:12:13 -08003393 if (!err)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003394 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3395 MGMT_STATUS_SUCCESS, addr,
3396 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003397 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003398 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3399 MGMT_STATUS_FAILED, addr,
3400 sizeof(*addr));
Brian Gix5fe57d92011-12-21 16:12:13 -08003401
Brian Gix47c15e22011-11-16 13:53:14 -08003402 goto done;
3403 }
3404
Johan Hedberg1707c602013-03-15 17:07:15 -05003405 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
Johan Hedberga5c29682011-02-19 12:05:57 -03003406 if (!cmd) {
3407 err = -ENOMEM;
Brian Gix0df4c182011-11-16 13:53:13 -08003408 goto done;
Johan Hedberga5c29682011-02-19 12:05:57 -03003409 }
3410
Johan Hedberg7776d1d2014-12-05 13:36:03 +02003411 cmd->cmd_complete = addr_cmd_complete;
3412
Brian Gix0df4c182011-11-16 13:53:13 -08003413 /* Continue with pairing via HCI */
Brian Gix604086b2011-11-23 08:28:33 -08003414 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3415 struct hci_cp_user_passkey_reply cp;
3416
Johan Hedberg1707c602013-03-15 17:07:15 -05003417 bacpy(&cp.bdaddr, &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003418 cp.passkey = passkey;
3419 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3420 } else
Johan Hedberg1707c602013-03-15 17:07:15 -05003421 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3422 &addr->bdaddr);
Brian Gix604086b2011-11-23 08:28:33 -08003423
Johan Hedberga664b5b2011-02-19 12:06:02 -03003424 if (err < 0)
3425 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03003426
Brian Gix0df4c182011-11-16 13:53:13 -08003427done:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003428 hci_dev_unlock(hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03003429 return err;
3430}
3431
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303432static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3433 void *data, u16 len)
3434{
3435 struct mgmt_cp_pin_code_neg_reply *cp = data;
3436
3437 BT_DBG("");
3438
Johan Hedberg1707c602013-03-15 17:07:15 -05003439 return user_pairing_resp(sk, hdev, &cp->addr,
Jaganath Kanakkasseryafeb0192012-07-09 16:11:51 +05303440 MGMT_OP_PIN_CODE_NEG_REPLY,
3441 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3442}
3443
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003444static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3445 u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003446{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003447 struct mgmt_cp_user_confirm_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003448
3449 BT_DBG("");
3450
3451 if (len != sizeof(*cp))
Johan Hedberga69e8372015-03-06 21:08:53 +02003452 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3453 MGMT_STATUS_INVALID_PARAMS);
Brian Gix0df4c182011-11-16 13:53:13 -08003454
Johan Hedberg1707c602013-03-15 17:07:15 -05003455 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003456 MGMT_OP_USER_CONFIRM_REPLY,
3457 HCI_OP_USER_CONFIRM_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003458}
3459
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003460static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003461 void *data, u16 len)
Brian Gix0df4c182011-11-16 13:53:13 -08003462{
Johan Hedbergc9c26592011-12-15 00:47:41 +02003463 struct mgmt_cp_user_confirm_neg_reply *cp = data;
Brian Gix0df4c182011-11-16 13:53:13 -08003464
3465 BT_DBG("");
3466
Johan Hedberg1707c602013-03-15 17:07:15 -05003467 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003468 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3469 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
Brian Gix0df4c182011-11-16 13:53:13 -08003470}
3471
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003472static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3473 u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003474{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003475 struct mgmt_cp_user_passkey_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003476
3477 BT_DBG("");
3478
Johan Hedberg1707c602013-03-15 17:07:15 -05003479 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003480 MGMT_OP_USER_PASSKEY_REPLY,
3481 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
Brian Gix604086b2011-11-23 08:28:33 -08003482}
3483
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003484static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003485 void *data, u16 len)
Brian Gix604086b2011-11-23 08:28:33 -08003486{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003487 struct mgmt_cp_user_passkey_neg_reply *cp = data;
Brian Gix604086b2011-11-23 08:28:33 -08003488
3489 BT_DBG("");
3490
Johan Hedberg1707c602013-03-15 17:07:15 -05003491 return user_pairing_resp(sk, hdev, &cp->addr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003492 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3493 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
Brian Gix604086b2011-11-23 08:28:33 -08003494}
3495
Johan Hedberg13928972013-03-15 17:07:00 -05003496static void update_name(struct hci_request *req)
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003497{
Johan Hedberg13928972013-03-15 17:07:00 -05003498 struct hci_dev *hdev = req->hdev;
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003499 struct hci_cp_write_local_name cp;
3500
Johan Hedberg13928972013-03-15 17:07:00 -05003501 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003502
Johan Hedberg890ea892013-03-15 17:06:52 -05003503 hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003504}
3505
Marcel Holtmann1904a852015-01-11 13:50:44 -08003506static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg13928972013-03-15 17:07:00 -05003507{
3508 struct mgmt_cp_set_local_name *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003509 struct mgmt_pending_cmd *cmd;
Johan Hedberg13928972013-03-15 17:07:00 -05003510
3511 BT_DBG("status 0x%02x", status);
3512
3513 hci_dev_lock(hdev);
3514
Johan Hedberg333ae952015-03-17 13:48:47 +02003515 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05003516 if (!cmd)
3517 goto unlock;
3518
3519 cp = cmd->param;
3520
3521 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02003522 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3523 mgmt_status(status));
Johan Hedberg13928972013-03-15 17:07:00 -05003524 else
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003525 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3526 cp, sizeof(*cp));
Johan Hedberg13928972013-03-15 17:07:00 -05003527
3528 mgmt_pending_remove(cmd);
3529
3530unlock:
3531 hci_dev_unlock(hdev);
3532}
3533
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003534static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003535 u16 len)
Johan Hedbergb312b1612011-03-16 14:29:37 +02003536{
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003537 struct mgmt_cp_set_local_name *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003538 struct mgmt_pending_cmd *cmd;
Johan Hedberg890ea892013-03-15 17:06:52 -05003539 struct hci_request req;
Johan Hedbergb312b1612011-03-16 14:29:37 +02003540 int err;
3541
3542 BT_DBG("");
3543
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003544 hci_dev_lock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003545
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003546 /* If the old values are the same as the new ones just return a
3547 * direct command complete event.
3548 */
3549 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3550 !memcmp(hdev->short_name, cp->short_name,
3551 sizeof(hdev->short_name))) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003552 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3553 data, len);
Johan Hedbergb3f2ca92013-03-15 17:07:03 -05003554 goto failed;
3555 }
3556
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003557 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003558
Johan Hedbergb5235a62012-02-21 14:32:24 +02003559 if (!hdev_is_powered(hdev)) {
Johan Hedberg2b4bf392012-03-03 00:19:06 +02003560 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003561
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003562 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3563 data, len);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003564 if (err < 0)
3565 goto failed;
3566
Marcel Holtmannf6b77122015-03-14 19:28:05 -07003567 err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev,
3568 data, len, sk);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003569
Johan Hedbergb5235a62012-02-21 14:32:24 +02003570 goto failed;
3571 }
3572
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02003573 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003574 if (!cmd) {
3575 err = -ENOMEM;
3576 goto failed;
3577 }
3578
Johan Hedberg13928972013-03-15 17:07:00 -05003579 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3580
Johan Hedberg890ea892013-03-15 17:06:52 -05003581 hci_req_init(&req, hdev);
Johan Hedberg3f985052013-03-15 17:07:02 -05003582
3583 if (lmp_bredr_capable(hdev)) {
3584 update_name(&req);
3585 update_eir(&req);
3586 }
3587
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003588 /* The name is stored in the scan response data and so
3589 * no need to udpate the advertising data here.
3590 */
Johan Hedberg3f985052013-03-15 17:07:02 -05003591 if (lmp_le_capable(hdev))
Marcel Holtmann7a5f4992013-10-16 00:16:49 -07003592 update_scan_rsp_data(&req);
Johan Hedberg3f985052013-03-15 17:07:02 -05003593
Johan Hedberg13928972013-03-15 17:07:00 -05003594 err = hci_req_run(&req, set_name_complete);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003595 if (err < 0)
3596 mgmt_pending_remove(cmd);
3597
3598failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003599 hci_dev_unlock(hdev);
Johan Hedbergb312b1612011-03-16 14:29:37 +02003600 return err;
3601}
3602
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02003603static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003604 void *data, u16 data_len)
Szymon Jancc35938b2011-03-22 13:12:21 +01003605{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003606 struct mgmt_pending_cmd *cmd;
Szymon Jancc35938b2011-03-22 13:12:21 +01003607 int err;
3608
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003609 BT_DBG("%s", hdev->name);
Szymon Jancc35938b2011-03-22 13:12:21 +01003610
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003611 hci_dev_lock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003612
Johan Hedberg4b34ee782012-02-21 14:13:02 +02003613 if (!hdev_is_powered(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003614 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3615 MGMT_STATUS_NOT_POWERED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003616 goto unlock;
3617 }
3618
Andre Guedes9a1a1992012-07-24 15:03:48 -03003619 if (!lmp_ssp_capable(hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003620 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3621 MGMT_STATUS_NOT_SUPPORTED);
Szymon Jancc35938b2011-03-22 13:12:21 +01003622 goto unlock;
3623 }
3624
Johan Hedberg333ae952015-03-17 13:48:47 +02003625 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02003626 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3627 MGMT_STATUS_BUSY);
Szymon Jancc35938b2011-03-22 13:12:21 +01003628 goto unlock;
3629 }
3630
Johan Hedberg2e58ef32011-11-08 20:40:15 +02003631 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
Szymon Jancc35938b2011-03-22 13:12:21 +01003632 if (!cmd) {
3633 err = -ENOMEM;
3634 goto unlock;
3635 }
3636
Johan Hedberg710f11c2014-05-26 11:21:22 +03003637 if (bredr_sc_enabled(hdev))
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08003638 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_EXT_DATA,
3639 0, NULL);
3640 else
3641 err = hci_send_cmd(hdev, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
3642
Szymon Jancc35938b2011-03-22 13:12:21 +01003643 if (err < 0)
3644 mgmt_pending_remove(cmd);
3645
3646unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003647 hci_dev_unlock(hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01003648 return err;
3649}
3650
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003651static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03003652 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003653{
Johan Hedberg5d57e792015-01-23 10:10:38 +02003654 struct mgmt_addr_info *addr = data;
Szymon Janc2763eda2011-03-22 13:12:22 +01003655 int err;
3656
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003657 BT_DBG("%s ", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003658
Johan Hedberg5d57e792015-01-23 10:10:38 +02003659 if (!bdaddr_type_is_valid(addr->type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003660 return mgmt_cmd_complete(sk, hdev->id,
3661 MGMT_OP_ADD_REMOTE_OOB_DATA,
3662 MGMT_STATUS_INVALID_PARAMS,
3663 addr, sizeof(*addr));
Johan Hedberg5d57e792015-01-23 10:10:38 +02003664
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003665 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003666
Marcel Holtmannec109112014-01-10 02:07:30 -08003667 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
3668 struct mgmt_cp_add_remote_oob_data *cp = data;
3669 u8 status;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003670
Johan Hedbergc19a4952014-11-17 20:52:19 +02003671 if (cp->addr.type != BDADDR_BREDR) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003672 err = mgmt_cmd_complete(sk, hdev->id,
3673 MGMT_OP_ADD_REMOTE_OOB_DATA,
3674 MGMT_STATUS_INVALID_PARAMS,
3675 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003676 goto unlock;
3677 }
3678
Marcel Holtmannec109112014-01-10 02:07:30 -08003679 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg6928a922014-10-26 20:46:09 +01003680 cp->addr.type, cp->hash,
3681 cp->rand, NULL, NULL);
Marcel Holtmannec109112014-01-10 02:07:30 -08003682 if (err < 0)
3683 status = MGMT_STATUS_FAILED;
3684 else
3685 status = MGMT_STATUS_SUCCESS;
3686
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003687 err = mgmt_cmd_complete(sk, hdev->id,
3688 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
3689 &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003690 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
3691 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003692 u8 *rand192, *hash192, *rand256, *hash256;
Marcel Holtmannec109112014-01-10 02:07:30 -08003693 u8 status;
3694
Johan Hedberg86df9202014-10-26 20:52:27 +01003695 if (bdaddr_type_is_le(cp->addr.type)) {
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003696 /* Enforce zero-valued 192-bit parameters as
3697 * long as legacy SMP OOB isn't implemented.
3698 */
3699 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
3700 memcmp(cp->hash192, ZERO_KEY, 16)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003701 err = mgmt_cmd_complete(sk, hdev->id,
3702 MGMT_OP_ADD_REMOTE_OOB_DATA,
3703 MGMT_STATUS_INVALID_PARAMS,
3704 addr, sizeof(*addr));
Johan Hedbergd25b78e2015-01-27 12:55:52 +02003705 goto unlock;
3706 }
3707
Johan Hedberg86df9202014-10-26 20:52:27 +01003708 rand192 = NULL;
3709 hash192 = NULL;
3710 } else {
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003711 /* In case one of the P-192 values is set to zero,
3712 * then just disable OOB data for P-192.
3713 */
3714 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
3715 !memcmp(cp->hash192, ZERO_KEY, 16)) {
3716 rand192 = NULL;
3717 hash192 = NULL;
3718 } else {
3719 rand192 = cp->rand192;
3720 hash192 = cp->hash192;
3721 }
3722 }
3723
3724 /* In case one of the P-256 values is set to zero, then just
3725 * disable OOB data for P-256.
3726 */
3727 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
3728 !memcmp(cp->hash256, ZERO_KEY, 16)) {
3729 rand256 = NULL;
3730 hash256 = NULL;
3731 } else {
3732 rand256 = cp->rand256;
3733 hash256 = cp->hash256;
Johan Hedberg86df9202014-10-26 20:52:27 +01003734 }
3735
Johan Hedberg81328d5c2014-10-26 20:33:47 +01003736 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
Johan Hedberg86df9202014-10-26 20:52:27 +01003737 cp->addr.type, hash192, rand192,
Marcel Holtmann41bcfd52015-01-31 00:37:02 -08003738 hash256, rand256);
Marcel Holtmannec109112014-01-10 02:07:30 -08003739 if (err < 0)
3740 status = MGMT_STATUS_FAILED;
3741 else
3742 status = MGMT_STATUS_SUCCESS;
3743
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003744 err = mgmt_cmd_complete(sk, hdev->id,
3745 MGMT_OP_ADD_REMOTE_OOB_DATA,
3746 status, &cp->addr, sizeof(cp->addr));
Marcel Holtmannec109112014-01-10 02:07:30 -08003747 } else {
3748 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
Johan Hedberga69e8372015-03-06 21:08:53 +02003749 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
3750 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannec109112014-01-10 02:07:30 -08003751 }
Szymon Janc2763eda2011-03-22 13:12:22 +01003752
Johan Hedbergc19a4952014-11-17 20:52:19 +02003753unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003754 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003755 return err;
3756}
3757
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003758static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03003759 void *data, u16 len)
Szymon Janc2763eda2011-03-22 13:12:22 +01003760{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03003761 struct mgmt_cp_remove_remote_oob_data *cp = data;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003762 u8 status;
Szymon Janc2763eda2011-03-22 13:12:22 +01003763 int err;
3764
Johan Hedbergbdb6d972012-02-28 06:13:32 +02003765 BT_DBG("%s", hdev->name);
Szymon Janc2763eda2011-03-22 13:12:22 +01003766
Johan Hedbergc19a4952014-11-17 20:52:19 +02003767 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003768 return mgmt_cmd_complete(sk, hdev->id,
3769 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3770 MGMT_STATUS_INVALID_PARAMS,
3771 &cp->addr, sizeof(cp->addr));
Johan Hedbergc19a4952014-11-17 20:52:19 +02003772
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003773 hci_dev_lock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003774
Johan Hedbergeedbd582014-11-15 09:34:23 +02003775 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
3776 hci_remote_oob_data_clear(hdev);
3777 status = MGMT_STATUS_SUCCESS;
3778 goto done;
3779 }
3780
Johan Hedberg6928a922014-10-26 20:46:09 +01003781 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
Szymon Janc2763eda2011-03-22 13:12:22 +01003782 if (err < 0)
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003783 status = MGMT_STATUS_INVALID_PARAMS;
Szymon Janc2763eda2011-03-22 13:12:22 +01003784 else
Szymon Janca6785be2012-12-13 15:11:21 +01003785 status = MGMT_STATUS_SUCCESS;
Johan Hedbergbf1e3542012-02-19 13:16:14 +02003786
Johan Hedbergeedbd582014-11-15 09:34:23 +02003787done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02003788 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
3789 status, &cp->addr, sizeof(cp->addr));
Szymon Janc2763eda2011-03-22 13:12:22 +01003790
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03003791 hci_dev_unlock(hdev);
Szymon Janc2763eda2011-03-22 13:12:22 +01003792 return err;
3793}
3794
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003795static bool trigger_bredr_inquiry(struct hci_request *req, u8 *status)
3796{
3797 struct hci_dev *hdev = req->hdev;
3798 struct hci_cp_inquiry cp;
3799 /* General inquiry access code (GIAC) */
3800 u8 lap[3] = { 0x33, 0x8b, 0x9e };
3801
3802 *status = mgmt_bredr_support(hdev);
3803 if (*status)
3804 return false;
3805
3806 if (hci_dev_test_flag(hdev, HCI_INQUIRY)) {
3807 *status = MGMT_STATUS_BUSY;
3808 return false;
3809 }
3810
3811 hci_inquiry_cache_flush(hdev);
3812
3813 memset(&cp, 0, sizeof(cp));
3814 memcpy(&cp.lap, lap, sizeof(cp.lap));
3815 cp.length = DISCOV_BREDR_INQUIRY_LEN;
3816
3817 hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
3818
3819 return true;
3820}
3821
3822static bool trigger_le_scan(struct hci_request *req, u16 interval, u8 *status)
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003823{
Marcel Holtmann80190442014-12-04 11:36:36 +01003824 struct hci_dev *hdev = req->hdev;
3825 struct hci_cp_le_set_scan_param param_cp;
3826 struct hci_cp_le_set_scan_enable enable_cp;
Marcel Holtmann80190442014-12-04 11:36:36 +01003827 u8 own_addr_type;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003828 int err;
3829
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003830 *status = mgmt_le_support(hdev);
3831 if (*status)
3832 return false;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003833
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003834 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
3835 /* Don't let discovery abort an outgoing connection attempt
3836 * that's using directed advertising.
3837 */
3838 if (hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) {
3839 *status = MGMT_STATUS_REJECTED;
Marcel Holtmann80190442014-12-04 11:36:36 +01003840 return false;
3841 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003842
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003843 disable_advertising(req);
3844 }
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003845
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003846 /* If controller is scanning, it means the background scanning is
3847 * running. Thus, we should temporarily stop it in order to set the
3848 * discovery scanning parameters.
3849 */
3850 if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
3851 hci_req_add_le_scan_disable(req);
3852
3853 /* All active scans will be done with either a resolvable private
3854 * address (when privacy feature has been enabled) or non-resolvable
3855 * private address.
3856 */
3857 err = hci_update_random_address(req, true, &own_addr_type);
3858 if (err < 0) {
3859 *status = MGMT_STATUS_FAILED;
3860 return false;
3861 }
3862
3863 memset(&param_cp, 0, sizeof(param_cp));
3864 param_cp.type = LE_SCAN_ACTIVE;
3865 param_cp.interval = cpu_to_le16(interval);
3866 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
3867 param_cp.own_address_type = own_addr_type;
3868
3869 hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
3870 &param_cp);
3871
3872 memset(&enable_cp, 0, sizeof(enable_cp));
3873 enable_cp.enable = LE_SCAN_ENABLE;
3874 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
3875
3876 hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
3877 &enable_cp);
3878
3879 return true;
3880}
3881
3882static bool trigger_discovery(struct hci_request *req, u8 *status)
3883{
3884 struct hci_dev *hdev = req->hdev;
3885
3886 switch (hdev->discovery.type) {
3887 case DISCOV_TYPE_BREDR:
3888 if (!trigger_bredr_inquiry(req, status))
3889 return false;
Marcel Holtmann80190442014-12-04 11:36:36 +01003890 break;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003891
Marcel Holtmann80190442014-12-04 11:36:36 +01003892 case DISCOV_TYPE_INTERLEAVED:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07003893 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
3894 &hdev->quirks)) {
3895 /* During simultaneous discovery, we double LE scan
3896 * interval. We must leave some time for the controller
3897 * to do BR/EDR inquiry.
3898 */
3899 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT * 2,
3900 status))
3901 return false;
3902
3903 if (!trigger_bredr_inquiry(req, status))
3904 return false;
3905
3906 return true;
3907 }
3908
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003909 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmann80190442014-12-04 11:36:36 +01003910 *status = MGMT_STATUS_NOT_SUPPORTED;
3911 return false;
3912 }
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003913 /* fall through */
Marcel Holtmann80190442014-12-04 11:36:36 +01003914
Jakub Pawlowski812abb12015-03-17 09:04:13 -07003915 case DISCOV_TYPE_LE:
3916 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT, status))
Marcel Holtmann80190442014-12-04 11:36:36 +01003917 return false;
Marcel Holtmann80190442014-12-04 11:36:36 +01003918 break;
3919
3920 default:
3921 *status = MGMT_STATUS_INVALID_PARAMS;
3922 return false;
3923 }
3924
3925 return true;
Andre Guedes41dc2bd2013-04-30 15:29:30 -03003926}
3927
Marcel Holtmann1904a852015-01-11 13:50:44 -08003928static void start_discovery_complete(struct hci_dev *hdev, u8 status,
3929 u16 opcode)
Andre Guedes7c307722013-04-30 15:29:28 -03003930{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02003931 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003932 unsigned long timeout;
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003933
Andre Guedes7c307722013-04-30 15:29:28 -03003934 BT_DBG("status %d", status);
3935
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003936 hci_dev_lock(hdev);
3937
Johan Hedberg333ae952015-03-17 13:48:47 +02003938 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003939 if (!cmd)
Johan Hedberg333ae952015-03-17 13:48:47 +02003940 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01003941
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003942 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02003943 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003944 mgmt_pending_remove(cmd);
Andre Guedes7c307722013-04-30 15:29:28 -03003945 }
3946
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003947 if (status) {
3948 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
3949 goto unlock;
3950 }
3951
Andre Guedes7c307722013-04-30 15:29:28 -03003952 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
Andre Guedes7c307722013-04-30 15:29:28 -03003953
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08003954 /* If the scan involves LE scan, pick proper timeout to schedule
3955 * hdev->le_scan_disable that will stop it.
3956 */
Andre Guedes7c307722013-04-30 15:29:28 -03003957 switch (hdev->discovery.type) {
3958 case DISCOV_TYPE_LE:
Lukasz Rymanowski3d5a76f2014-03-27 20:55:21 +01003959 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
Andre Guedes7c307722013-04-30 15:29:28 -03003960 break;
Andre Guedes7c307722013-04-30 15:29:28 -03003961 case DISCOV_TYPE_INTERLEAVED:
Jakub Pawlowski07d23342015-03-17 09:04:14 -07003962 /* When running simultaneous discovery, the LE scanning time
3963 * should occupy the whole discovery time sine BR/EDR inquiry
3964 * and LE scanning are scheduled by the controller.
3965 *
3966 * For interleaving discovery in comparison, BR/EDR inquiry
3967 * and LE scanning are done sequentially with separate
3968 * timeouts.
3969 */
3970 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
3971 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
3972 else
3973 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
Andre Guedes7c307722013-04-30 15:29:28 -03003974 break;
Andre Guedes7c307722013-04-30 15:29:28 -03003975 case DISCOV_TYPE_BREDR:
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003976 timeout = 0;
Andre Guedes7c307722013-04-30 15:29:28 -03003977 break;
Andre Guedes7c307722013-04-30 15:29:28 -03003978 default:
3979 BT_ERR("Invalid discovery type %d", hdev->discovery.type);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003980 timeout = 0;
3981 break;
Andre Guedes7c307722013-04-30 15:29:28 -03003982 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01003983
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08003984 if (timeout) {
3985 /* When service discovery is used and the controller has
3986 * a strict duplicate filter, it is important to remember
3987 * the start and duration of the scan. This is required
3988 * for restarting scanning during the discovery phase.
3989 */
3990 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
3991 &hdev->quirks) &&
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08003992 hdev->discovery.result_filtering) {
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08003993 hdev->discovery.scan_start = jiffies;
3994 hdev->discovery.scan_duration = timeout;
3995 }
3996
Marcel Holtmann11e6e252014-12-04 11:36:35 +01003997 queue_delayed_work(hdev->workqueue,
3998 &hdev->le_scan_disable, timeout);
Jakub Pawlowski2d28cfe2015-02-01 23:07:54 -08003999 }
Lukasz Rymanowskiae55f592014-03-27 20:55:19 +01004000
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004001unlock:
4002 hci_dev_unlock(hdev);
Andre Guedes7c307722013-04-30 15:29:28 -03004003}
4004
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004005static int start_discovery(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004006 void *data, u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004007{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004008 struct mgmt_cp_start_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004009 struct mgmt_pending_cmd *cmd;
Andre Guedes7c307722013-04-30 15:29:28 -03004010 struct hci_request req;
Marcel Holtmann80190442014-12-04 11:36:36 +01004011 u8 status;
Johan Hedberg14a53662011-04-27 10:29:56 -04004012 int err;
4013
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004014 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004015
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004016 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004017
Johan Hedberg4b34ee782012-02-21 14:13:02 +02004018 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004019 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4020 MGMT_STATUS_NOT_POWERED,
4021 &cp->type, sizeof(cp->type));
Johan Hedbergbd2d1332011-11-07 23:13:37 +02004022 goto failed;
4023 }
4024
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004025 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004026 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004027 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4028 MGMT_STATUS_BUSY, &cp->type,
4029 sizeof(cp->type));
Andre Guedes642be6c2012-03-21 00:03:37 -03004030 goto failed;
4031 }
4032
Johan Hedberg2922a942014-12-05 13:36:06 +02004033 cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004034 if (!cmd) {
4035 err = -ENOMEM;
4036 goto failed;
4037 }
4038
Johan Hedberg2922a942014-12-05 13:36:06 +02004039 cmd->cmd_complete = generic_cmd_complete;
4040
Marcel Holtmann22078802014-12-05 11:45:22 +01004041 /* Clear the discovery filter first to free any previously
4042 * allocated memory for the UUID list.
4043 */
4044 hci_discovery_filter_clear(hdev);
4045
Andre Guedes4aab14e2012-02-17 20:39:36 -03004046 hdev->discovery.type = cp->type;
Marcel Holtmannda25cf62014-12-05 13:03:35 +01004047 hdev->discovery.report_invalid_rssi = false;
Andre Guedes4aab14e2012-02-17 20:39:36 -03004048
Andre Guedes7c307722013-04-30 15:29:28 -03004049 hci_req_init(&req, hdev);
4050
Marcel Holtmann80190442014-12-04 11:36:36 +01004051 if (!trigger_discovery(&req, &status)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004052 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4053 status, &cp->type, sizeof(cp->type));
Johan Hedberg04106752013-01-10 14:54:09 +02004054 mgmt_pending_remove(cmd);
4055 goto failed;
Andre Guedesf39799f2012-02-17 20:39:35 -03004056 }
Andre Guedes3fd24152012-02-03 17:48:01 -03004057
Andre Guedes7c307722013-04-30 15:29:28 -03004058 err = hci_req_run(&req, start_discovery_complete);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004059 if (err < 0) {
Johan Hedberg14a53662011-04-27 10:29:56 -04004060 mgmt_pending_remove(cmd);
Marcel Holtmannf5a969f2014-12-04 11:36:34 +01004061 goto failed;
4062 }
4063
4064 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
Johan Hedberg14a53662011-04-27 10:29:56 -04004065
4066failed:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004067 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004068 return err;
4069}
4070
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004071static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
4072 u8 status)
Andre Guedes1183fdc2013-04-30 15:29:35 -03004073{
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004074 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
4075 cmd->param, 1);
Johan Hedberg2922a942014-12-05 13:36:06 +02004076}
4077
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004078static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4079 void *data, u16 len)
4080{
4081 struct mgmt_cp_start_service_discovery *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004082 struct mgmt_pending_cmd *cmd;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004083 struct hci_request req;
4084 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
4085 u16 uuid_count, expected_len;
4086 u8 status;
Andre Guedes1183fdc2013-04-30 15:29:35 -03004087 int err;
4088
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004089 BT_DBG("%s", hdev->name);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004090
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004091 hci_dev_lock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004092
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004093 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004094 err = mgmt_cmd_complete(sk, hdev->id,
4095 MGMT_OP_START_SERVICE_DISCOVERY,
4096 MGMT_STATUS_NOT_POWERED,
4097 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004098 goto failed;
4099 }
4100
4101 if (hdev->discovery.state != DISCOVERY_STOPPED ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004102 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004103 err = mgmt_cmd_complete(sk, hdev->id,
4104 MGMT_OP_START_SERVICE_DISCOVERY,
4105 MGMT_STATUS_BUSY, &cp->type,
4106 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004107 goto failed;
4108 }
4109
4110 uuid_count = __le16_to_cpu(cp->uuid_count);
4111 if (uuid_count > max_uuid_count) {
4112 BT_ERR("service_discovery: too big uuid_count value %u",
4113 uuid_count);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004114 err = mgmt_cmd_complete(sk, hdev->id,
4115 MGMT_OP_START_SERVICE_DISCOVERY,
4116 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4117 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004118 goto failed;
4119 }
4120
4121 expected_len = sizeof(*cp) + uuid_count * 16;
4122 if (expected_len != len) {
4123 BT_ERR("service_discovery: expected %u bytes, got %u bytes",
4124 expected_len, len);
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004125 err = mgmt_cmd_complete(sk, hdev->id,
4126 MGMT_OP_START_SERVICE_DISCOVERY,
4127 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4128 sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004129 goto failed;
4130 }
4131
4132 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
Johan Hedberg2922a942014-12-05 13:36:06 +02004133 hdev, data, len);
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004134 if (!cmd) {
4135 err = -ENOMEM;
4136 goto failed;
4137 }
4138
Johan Hedberg2922a942014-12-05 13:36:06 +02004139 cmd->cmd_complete = service_discovery_cmd_complete;
4140
Marcel Holtmann22078802014-12-05 11:45:22 +01004141 /* Clear the discovery filter first to free any previously
4142 * allocated memory for the UUID list.
4143 */
4144 hci_discovery_filter_clear(hdev);
4145
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08004146 hdev->discovery.result_filtering = true;
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004147 hdev->discovery.type = cp->type;
4148 hdev->discovery.rssi = cp->rssi;
4149 hdev->discovery.uuid_count = uuid_count;
4150
4151 if (uuid_count > 0) {
4152 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
4153 GFP_KERNEL);
4154 if (!hdev->discovery.uuids) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004155 err = mgmt_cmd_complete(sk, hdev->id,
4156 MGMT_OP_START_SERVICE_DISCOVERY,
4157 MGMT_STATUS_FAILED,
4158 &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004159 mgmt_pending_remove(cmd);
4160 goto failed;
4161 }
4162 }
4163
4164 hci_req_init(&req, hdev);
4165
4166 if (!trigger_discovery(&req, &status)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004167 err = mgmt_cmd_complete(sk, hdev->id,
4168 MGMT_OP_START_SERVICE_DISCOVERY,
4169 status, &cp->type, sizeof(cp->type));
Jakub Pawlowski66ea9422014-12-05 10:55:59 +01004170 mgmt_pending_remove(cmd);
4171 goto failed;
4172 }
4173
4174 err = hci_req_run(&req, start_discovery_complete);
4175 if (err < 0) {
4176 mgmt_pending_remove(cmd);
4177 goto failed;
4178 }
4179
4180 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
4181
4182failed:
4183 hci_dev_unlock(hdev);
Andre Guedes1183fdc2013-04-30 15:29:35 -03004184 return err;
4185}
4186
Marcel Holtmann1904a852015-01-11 13:50:44 -08004187static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Andre Guedes0e05bba2013-04-30 15:29:33 -03004188{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004189 struct mgmt_pending_cmd *cmd;
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004190
Andre Guedes0e05bba2013-04-30 15:29:33 -03004191 BT_DBG("status %d", status);
4192
4193 hci_dev_lock(hdev);
4194
Johan Hedberg333ae952015-03-17 13:48:47 +02004195 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004196 if (cmd) {
Johan Hedberg2922a942014-12-05 13:36:06 +02004197 cmd->cmd_complete(cmd, mgmt_status(status));
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004198 mgmt_pending_remove(cmd);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004199 }
4200
Marcel Holtmann11e6e252014-12-04 11:36:35 +01004201 if (!status)
4202 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004203
Andre Guedes0e05bba2013-04-30 15:29:33 -03004204 hci_dev_unlock(hdev);
4205}
4206
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004207static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004208 u16 len)
Johan Hedberg14a53662011-04-27 10:29:56 -04004209{
Johan Hedbergd9306502012-02-20 23:25:18 +02004210 struct mgmt_cp_stop_discovery *mgmt_cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004211 struct mgmt_pending_cmd *cmd;
Andre Guedes0e05bba2013-04-30 15:29:33 -03004212 struct hci_request req;
Johan Hedberg14a53662011-04-27 10:29:56 -04004213 int err;
4214
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004215 BT_DBG("%s", hdev->name);
Johan Hedberg14a53662011-04-27 10:29:56 -04004216
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004217 hci_dev_lock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004218
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004219 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004220 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4221 MGMT_STATUS_REJECTED, &mgmt_cp->type,
4222 sizeof(mgmt_cp->type));
Johan Hedbergd9306502012-02-20 23:25:18 +02004223 goto unlock;
4224 }
4225
4226 if (hdev->discovery.type != mgmt_cp->type) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004227 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4228 MGMT_STATUS_INVALID_PARAMS,
4229 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004230 goto unlock;
Johan Hedbergff9ef572012-01-04 14:23:45 +02004231 }
4232
Johan Hedberg2922a942014-12-05 13:36:06 +02004233 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
Johan Hedberg14a53662011-04-27 10:29:56 -04004234 if (!cmd) {
4235 err = -ENOMEM;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004236 goto unlock;
Johan Hedberg14a53662011-04-27 10:29:56 -04004237 }
4238
Johan Hedberg2922a942014-12-05 13:36:06 +02004239 cmd->cmd_complete = generic_cmd_complete;
4240
Andre Guedes0e05bba2013-04-30 15:29:33 -03004241 hci_req_init(&req, hdev);
4242
Johan Hedberg21a60d32014-06-10 14:05:58 +03004243 hci_stop_discovery(&req);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004244
Johan Hedberg21a60d32014-06-10 14:05:58 +03004245 err = hci_req_run(&req, stop_discovery_complete);
4246 if (!err) {
4247 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
Andre Guedes0e05bba2013-04-30 15:29:33 -03004248 goto unlock;
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004249 }
4250
Johan Hedberg21a60d32014-06-10 14:05:58 +03004251 mgmt_pending_remove(cmd);
4252
4253 /* If no HCI commands were sent we're done */
4254 if (err == -ENODATA) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004255 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
4256 &mgmt_cp->type, sizeof(mgmt_cp->type));
Johan Hedberg21a60d32014-06-10 14:05:58 +03004257 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4258 }
Johan Hedberg14a53662011-04-27 10:29:56 -04004259
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004260unlock:
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004261 hci_dev_unlock(hdev);
Johan Hedberg14a53662011-04-27 10:29:56 -04004262 return err;
4263}
4264
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004265static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004266 u16 len)
Johan Hedberg561aafb2012-01-04 13:31:59 +02004267{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004268 struct mgmt_cp_confirm_name *cp = data;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004269 struct inquiry_entry *e;
Johan Hedberg561aafb2012-01-04 13:31:59 +02004270 int err;
4271
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004272 BT_DBG("%s", hdev->name);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004273
Johan Hedberg561aafb2012-01-04 13:31:59 +02004274 hci_dev_lock(hdev);
4275
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004276 if (!hci_discovery_active(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004277 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4278 MGMT_STATUS_FAILED, &cp->addr,
4279 sizeof(cp->addr));
Johan Hedberg30dc78e2012-01-04 15:44:20 +02004280 goto failed;
4281 }
4282
Johan Hedberga198e7b2012-02-17 14:27:06 +02004283 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004284 if (!e) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004285 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4286 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
4287 sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004288 goto failed;
4289 }
4290
4291 if (cp->name_known) {
4292 e->name_state = NAME_KNOWN;
4293 list_del(&e->list);
4294 } else {
4295 e->name_state = NAME_NEEDED;
Johan Hedberga3d4e202012-01-09 00:53:02 +02004296 hci_inquiry_cache_update_resolve(hdev, e);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004297 }
4298
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004299 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
4300 &cp->addr, sizeof(cp->addr));
Johan Hedberg561aafb2012-01-04 13:31:59 +02004301
4302failed:
4303 hci_dev_unlock(hdev);
Johan Hedberg561aafb2012-01-04 13:31:59 +02004304 return err;
4305}
4306
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004307static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004308 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004309{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004310 struct mgmt_cp_block_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004311 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004312 int err;
4313
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004314 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004315
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004316 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004317 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
4318 MGMT_STATUS_INVALID_PARAMS,
4319 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004320
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004321 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004322
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004323 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
4324 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004325 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004326 status = MGMT_STATUS_FAILED;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004327 goto done;
4328 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004329
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004330 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4331 sk);
4332 status = MGMT_STATUS_SUCCESS;
4333
4334done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004335 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
4336 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004337
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004338 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004339
4340 return err;
4341}
4342
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004343static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004344 u16 len)
Antti Julku7fbec222011-06-15 12:01:15 +03004345{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004346 struct mgmt_cp_unblock_device *cp = data;
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004347 u8 status;
Antti Julku7fbec222011-06-15 12:01:15 +03004348 int err;
4349
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004350 BT_DBG("%s", hdev->name);
Antti Julku7fbec222011-06-15 12:01:15 +03004351
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004352 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004353 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
4354 MGMT_STATUS_INVALID_PARAMS,
4355 &cp->addr, sizeof(cp->addr));
Johan Hedberg4ee71b22013-01-20 14:27:19 +02004356
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004357 hci_dev_lock(hdev);
Antti Julku5e762442011-08-25 16:48:02 +03004358
Johan Hedbergdcc36c12014-07-09 12:59:13 +03004359 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
4360 cp->addr.type);
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004361 if (err < 0) {
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004362 status = MGMT_STATUS_INVALID_PARAMS;
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004363 goto done;
4364 }
Johan Hedbergf0eeea82012-02-19 12:58:54 +02004365
Johan Hedberg2a8357f2014-07-01 22:09:47 +03004366 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4367 sk);
4368 status = MGMT_STATUS_SUCCESS;
4369
4370done:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004371 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
4372 &cp->addr, sizeof(cp->addr));
Antti Julku5e762442011-08-25 16:48:02 +03004373
Gustavo F. Padovan09fd0de2011-06-17 13:03:21 -03004374 hci_dev_unlock(hdev);
Antti Julku7fbec222011-06-15 12:01:15 +03004375
4376 return err;
4377}
4378
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004379static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4380 u16 len)
4381{
4382 struct mgmt_cp_set_device_id *cp = data;
Johan Hedberg890ea892013-03-15 17:06:52 -05004383 struct hci_request req;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004384 int err;
Szymon Jancc72d4b82012-03-16 16:02:57 +01004385 __u16 source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004386
4387 BT_DBG("%s", hdev->name);
4388
Szymon Jancc72d4b82012-03-16 16:02:57 +01004389 source = __le16_to_cpu(cp->source);
4390
4391 if (source > 0x0002)
Johan Hedberga69e8372015-03-06 21:08:53 +02004392 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
4393 MGMT_STATUS_INVALID_PARAMS);
Szymon Jancc72d4b82012-03-16 16:02:57 +01004394
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004395 hci_dev_lock(hdev);
4396
Szymon Jancc72d4b82012-03-16 16:02:57 +01004397 hdev->devid_source = source;
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004398 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
4399 hdev->devid_product = __le16_to_cpu(cp->product);
4400 hdev->devid_version = __le16_to_cpu(cp->version);
4401
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004402 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
4403 NULL, 0);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004404
Johan Hedberg890ea892013-03-15 17:06:52 -05004405 hci_req_init(&req, hdev);
4406 update_eir(&req);
4407 hci_req_run(&req, NULL);
Marcel Holtmanncdbaccc2012-03-11 20:00:29 -07004408
4409 hci_dev_unlock(hdev);
4410
4411 return err;
4412}
4413
Arman Uguray24b4f382015-03-23 15:57:12 -07004414static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
4415 u16 opcode)
4416{
4417 BT_DBG("status %d", status);
4418}
4419
Marcel Holtmann1904a852015-01-11 13:50:44 -08004420static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4421 u16 opcode)
Johan Hedberg4375f102013-09-25 13:26:10 +03004422{
4423 struct cmd_lookup match = { NULL, hdev };
Arman Uguray24b4f382015-03-23 15:57:12 -07004424 struct hci_request req;
Johan Hedberg4375f102013-09-25 13:26:10 +03004425
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304426 hci_dev_lock(hdev);
4427
Johan Hedberg4375f102013-09-25 13:26:10 +03004428 if (status) {
4429 u8 mgmt_err = mgmt_status(status);
4430
4431 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
4432 cmd_status_rsp, &mgmt_err);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304433 goto unlock;
Johan Hedberg4375f102013-09-25 13:26:10 +03004434 }
4435
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004436 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004437 hci_dev_set_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004438 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004439 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
Johan Hedbergc93bd152014-07-08 15:07:48 +03004440
Johan Hedberg4375f102013-09-25 13:26:10 +03004441 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
4442 &match);
4443
4444 new_settings(hdev, match.sk);
4445
4446 if (match.sk)
4447 sock_put(match.sk);
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304448
Arman Uguray24b4f382015-03-23 15:57:12 -07004449 /* If "Set Advertising" was just disabled and instance advertising was
4450 * set up earlier, then enable the advertising instance.
4451 */
4452 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
4453 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
4454 goto unlock;
4455
4456 hci_req_init(&req, hdev);
4457
4458 update_adv_data(&req);
4459 enable_advertising(&req);
4460
4461 if (hci_req_run(&req, enable_advertising_instance) < 0)
4462 BT_ERR("Failed to re-configure advertising");
4463
Jaganath Kanakkassery3ad67582014-12-11 11:43:12 +05304464unlock:
4465 hci_dev_unlock(hdev);
Johan Hedberg4375f102013-09-25 13:26:10 +03004466}
4467
Marcel Holtmann21b51872013-10-10 09:47:53 -07004468static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4469 u16 len)
Johan Hedberg4375f102013-09-25 13:26:10 +03004470{
4471 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004472 struct mgmt_pending_cmd *cmd;
Johan Hedberg4375f102013-09-25 13:26:10 +03004473 struct hci_request req;
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004474 u8 val, status;
Johan Hedberg4375f102013-09-25 13:26:10 +03004475 int err;
4476
4477 BT_DBG("request for %s", hdev->name);
4478
Johan Hedberge6fe7982013-10-02 15:45:22 +03004479 status = mgmt_le_support(hdev);
4480 if (status)
Johan Hedberga69e8372015-03-06 21:08:53 +02004481 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4482 status);
Johan Hedberg4375f102013-09-25 13:26:10 +03004483
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004484 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004485 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4486 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg4375f102013-09-25 13:26:10 +03004487
4488 hci_dev_lock(hdev);
4489
4490 val = !!cp->val;
Johan Hedberg4375f102013-09-25 13:26:10 +03004491
Johan Hedbergf74ca9b2013-10-08 15:52:18 +02004492 /* The following conditions are ones which mean that we should
4493 * not do any HCI communication but directly send a mgmt
4494 * response to user space (after toggling the flag if
4495 * necessary).
4496 */
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004497 if (!hdev_is_powered(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004498 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4499 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004500 hci_conn_num(hdev, LE_LINK) > 0 ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004501 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Johan Hedberge8bb6b92014-07-08 15:07:53 +03004502 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004503 bool changed;
Johan Hedberg4375f102013-09-25 13:26:10 +03004504
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004505 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07004506 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004507 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004508 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004509 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004510 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004511 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004512 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004513 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Johan Hedberg4375f102013-09-25 13:26:10 +03004514 }
4515
4516 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
4517 if (err < 0)
4518 goto unlock;
4519
4520 if (changed)
4521 err = new_settings(hdev, sk);
4522
4523 goto unlock;
4524 }
4525
Johan Hedberg333ae952015-03-17 13:48:47 +02004526 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4527 pending_find(MGMT_OP_SET_LE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004528 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4529 MGMT_STATUS_BUSY);
Johan Hedberg4375f102013-09-25 13:26:10 +03004530 goto unlock;
4531 }
4532
4533 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
4534 if (!cmd) {
4535 err = -ENOMEM;
4536 goto unlock;
4537 }
4538
4539 hci_req_init(&req, hdev);
4540
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004541 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004542 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004543 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004544 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
Marcel Holtmanncc91cb02015-03-12 22:30:58 -07004545
Arman Uguray24b4f382015-03-23 15:57:12 -07004546 if (val) {
4547 /* Switch to instance "0" for the Set Advertising setting. */
4548 update_adv_data_for_instance(&req, 0);
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004549 enable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004550 } else {
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07004551 disable_advertising(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07004552 }
Johan Hedberg4375f102013-09-25 13:26:10 +03004553
4554 err = hci_req_run(&req, set_advertising_complete);
4555 if (err < 0)
4556 mgmt_pending_remove(cmd);
4557
4558unlock:
4559 hci_dev_unlock(hdev);
4560 return err;
4561}
4562
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004563static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4564 void *data, u16 len)
4565{
4566 struct mgmt_cp_set_static_address *cp = data;
4567 int err;
4568
4569 BT_DBG("%s", hdev->name);
4570
Marcel Holtmann62af4442013-10-02 22:10:32 -07004571 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004572 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4573 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004574
4575 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004576 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4577 MGMT_STATUS_REJECTED);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004578
4579 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4580 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
Johan Hedberga69e8372015-03-06 21:08:53 +02004581 return mgmt_cmd_status(sk, hdev->id,
4582 MGMT_OP_SET_STATIC_ADDRESS,
4583 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004584
4585 /* Two most significant bits shall be set */
4586 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
Johan Hedberga69e8372015-03-06 21:08:53 +02004587 return mgmt_cmd_status(sk, hdev->id,
4588 MGMT_OP_SET_STATIC_ADDRESS,
4589 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004590 }
4591
4592 hci_dev_lock(hdev);
4593
4594 bacpy(&hdev->static_addr, &cp->bdaddr);
4595
Marcel Holtmann93690c22015-03-06 10:11:21 -08004596 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
4597 if (err < 0)
4598 goto unlock;
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004599
Marcel Holtmann93690c22015-03-06 10:11:21 -08004600 err = new_settings(hdev, sk);
4601
4602unlock:
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004603 hci_dev_unlock(hdev);
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07004604 return err;
4605}
4606
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004607static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
4608 void *data, u16 len)
4609{
4610 struct mgmt_cp_set_scan_params *cp = data;
4611 __u16 interval, window;
4612 int err;
4613
4614 BT_DBG("%s", hdev->name);
4615
4616 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004617 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4618 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004619
4620 interval = __le16_to_cpu(cp->interval);
4621
4622 if (interval < 0x0004 || interval > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004623 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4624 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004625
4626 window = __le16_to_cpu(cp->window);
4627
4628 if (window < 0x0004 || window > 0x4000)
Johan Hedberga69e8372015-03-06 21:08:53 +02004629 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4630 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004631
Marcel Holtmann899e1072013-10-14 09:55:32 -07004632 if (window > interval)
Johan Hedberga69e8372015-03-06 21:08:53 +02004633 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
4634 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann899e1072013-10-14 09:55:32 -07004635
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004636 hci_dev_lock(hdev);
4637
4638 hdev->le_scan_interval = interval;
4639 hdev->le_scan_window = window;
4640
Johan Hedberg2a1afb52015-03-06 21:08:54 +02004641 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
4642 NULL, 0);
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004643
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004644 /* If background scan is running, restart it so new parameters are
4645 * loaded.
4646 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004647 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
Andre Guedesdd2ef8e2014-02-26 20:21:56 -03004648 hdev->discovery.state == DISCOVERY_STOPPED) {
4649 struct hci_request req;
4650
4651 hci_req_init(&req, hdev);
4652
4653 hci_req_add_le_scan_disable(&req);
4654 hci_req_add_le_passive_scan(&req);
4655
4656 hci_req_run(&req, NULL);
4657 }
4658
Marcel Holtmann14b49b92013-10-11 08:23:20 -07004659 hci_dev_unlock(hdev);
4660
4661 return err;
4662}
4663
Marcel Holtmann1904a852015-01-11 13:50:44 -08004664static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
4665 u16 opcode)
Johan Hedberg33e38b32013-03-15 17:07:05 -05004666{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004667 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004668
4669 BT_DBG("status 0x%02x", status);
4670
4671 hci_dev_lock(hdev);
4672
Johan Hedberg333ae952015-03-17 13:48:47 +02004673 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004674 if (!cmd)
4675 goto unlock;
4676
4677 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004678 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4679 mgmt_status(status));
Johan Hedberg33e38b32013-03-15 17:07:05 -05004680 } else {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004681 struct mgmt_mode *cp = cmd->param;
4682
4683 if (cp->val)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004684 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004685 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004686 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004687
Johan Hedberg33e38b32013-03-15 17:07:05 -05004688 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
4689 new_settings(hdev, cmd->sk);
4690 }
4691
4692 mgmt_pending_remove(cmd);
4693
4694unlock:
4695 hci_dev_unlock(hdev);
4696}
4697
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004698static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03004699 void *data, u16 len)
Antti Julkuf6422ec2011-06-22 13:11:56 +03004700{
Vinicius Costa Gomes650f7262012-02-02 21:07:59 -03004701 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004702 struct mgmt_pending_cmd *cmd;
Johan Hedberg33e38b32013-03-15 17:07:05 -05004703 struct hci_request req;
Antti Julkuf6422ec2011-06-22 13:11:56 +03004704 int err;
4705
Johan Hedbergbdb6d972012-02-28 06:13:32 +02004706 BT_DBG("%s", hdev->name);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004707
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004708 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
Johan Hedberg56f87902013-10-02 13:43:13 +03004709 hdev->hci_ver < BLUETOOTH_VER_1_2)
Johan Hedberga69e8372015-03-06 21:08:53 +02004710 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4711 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg33c525c2012-10-24 21:11:58 +03004712
Johan Hedberga7e80f22013-01-09 16:05:19 +02004713 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004714 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4715 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga7e80f22013-01-09 16:05:19 +02004716
Antti Julkuf6422ec2011-06-22 13:11:56 +03004717 hci_dev_lock(hdev);
4718
Johan Hedberg333ae952015-03-17 13:48:47 +02004719 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004720 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4721 MGMT_STATUS_BUSY);
Johan Hedberg05cbf292013-03-15 17:07:07 -05004722 goto unlock;
4723 }
4724
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004725 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
Johan Hedberg1a4d3c42013-03-15 17:07:08 -05004726 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4727 hdev);
4728 goto unlock;
4729 }
4730
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004731 if (!hdev_is_powered(hdev)) {
Marcel Holtmannce05d602015-03-13 02:11:03 -07004732 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
Johan Hedberg406ef2a2015-03-10 20:14:27 +02004733 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
4734 hdev);
4735 new_settings(hdev, sk);
4736 goto unlock;
4737 }
4738
Johan Hedberg33e38b32013-03-15 17:07:05 -05004739 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
4740 data, len);
4741 if (!cmd) {
4742 err = -ENOMEM;
4743 goto unlock;
4744 }
4745
4746 hci_req_init(&req, hdev);
4747
Johan Hedberg406d7802013-03-15 17:07:09 -05004748 write_fast_connectable(&req, cp->val);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004749
4750 err = hci_req_run(&req, fast_connectable_complete);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004751 if (err < 0) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004752 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
4753 MGMT_STATUS_FAILED);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004754 mgmt_pending_remove(cmd);
Antti Julkuf6422ec2011-06-22 13:11:56 +03004755 }
4756
Johan Hedberg33e38b32013-03-15 17:07:05 -05004757unlock:
Antti Julkuf6422ec2011-06-22 13:11:56 +03004758 hci_dev_unlock(hdev);
Johan Hedberg33e38b32013-03-15 17:07:05 -05004759
Antti Julkuf6422ec2011-06-22 13:11:56 +03004760 return err;
4761}
4762
Marcel Holtmann1904a852015-01-11 13:50:44 -08004763static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg0663ca22013-10-02 13:43:14 +03004764{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004765 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004766
4767 BT_DBG("status 0x%02x", status);
4768
4769 hci_dev_lock(hdev);
4770
Johan Hedberg333ae952015-03-17 13:48:47 +02004771 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004772 if (!cmd)
4773 goto unlock;
4774
4775 if (status) {
4776 u8 mgmt_err = mgmt_status(status);
4777
4778 /* We need to restore the flag if related HCI commands
4779 * failed.
4780 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004781 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004782
Johan Hedberga69e8372015-03-06 21:08:53 +02004783 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004784 } else {
4785 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
4786 new_settings(hdev, cmd->sk);
4787 }
4788
4789 mgmt_pending_remove(cmd);
4790
4791unlock:
4792 hci_dev_unlock(hdev);
4793}
4794
4795static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
4796{
4797 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004798 struct mgmt_pending_cmd *cmd;
Johan Hedberg0663ca22013-10-02 13:43:14 +03004799 struct hci_request req;
4800 int err;
4801
4802 BT_DBG("request for %s", hdev->name);
4803
4804 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02004805 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4806 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004807
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004808 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004809 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4810 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004811
4812 if (cp->val != 0x00 && cp->val != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02004813 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4814 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004815
4816 hci_dev_lock(hdev);
4817
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004818 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg0663ca22013-10-02 13:43:14 +03004819 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4820 goto unlock;
4821 }
4822
4823 if (!hdev_is_powered(hdev)) {
4824 if (!cp->val) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004825 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
4826 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
4827 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
4828 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
4829 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004830 }
4831
Marcel Holtmannce05d602015-03-13 02:11:03 -07004832 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004833
4834 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
4835 if (err < 0)
4836 goto unlock;
4837
4838 err = new_settings(hdev, sk);
4839 goto unlock;
4840 }
4841
4842 /* Reject disabling when powered on */
4843 if (!cp->val) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004844 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4845 MGMT_STATUS_REJECTED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004846 goto unlock;
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004847 } else {
4848 /* When configuring a dual-mode controller to operate
4849 * with LE only and using a static address, then switching
4850 * BR/EDR back on is not allowed.
4851 *
4852 * Dual-mode controllers shall operate with the public
4853 * address as its identity address for BR/EDR and LE. So
4854 * reject the attempt to create an invalid configuration.
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08004855 *
4856 * The same restrictions applies when secure connections
4857 * has been enabled. For BR/EDR this is a controller feature
4858 * while for LE it is a host stack feature. This means that
4859 * switching BR/EDR back on when secure connections has been
4860 * enabled is not a supported transaction.
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004861 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004862 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Marcel Holtmann3a5486e2015-01-22 11:15:21 -08004863 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004864 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004865 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4866 MGMT_STATUS_REJECTED);
Marcel Holtmann111e4bc2015-01-14 14:40:42 -08004867 goto unlock;
4868 }
Johan Hedberg0663ca22013-10-02 13:43:14 +03004869 }
4870
Johan Hedberg333ae952015-03-17 13:48:47 +02004871 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004872 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
4873 MGMT_STATUS_BUSY);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004874 goto unlock;
4875 }
4876
4877 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
4878 if (!cmd) {
4879 err = -ENOMEM;
4880 goto unlock;
4881 }
4882
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07004883 /* We need to flip the bit already here so that update_adv_data
Johan Hedberg0663ca22013-10-02 13:43:14 +03004884 * generates the correct flags.
4885 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004886 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
Johan Hedberg0663ca22013-10-02 13:43:14 +03004887
4888 hci_req_init(&req, hdev);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004889
Johan Hedberg432df052014-08-01 11:13:31 +03004890 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02004891 __hci_update_page_scan(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004892
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07004893 /* Since only the advertising data flags will change, there
4894 * is no need to update the scan response data.
4895 */
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07004896 update_adv_data(&req);
Johan Hedbergaa8af462013-10-14 21:15:26 +03004897
Johan Hedberg0663ca22013-10-02 13:43:14 +03004898 err = hci_req_run(&req, set_bredr_complete);
4899 if (err < 0)
4900 mgmt_pending_remove(cmd);
4901
4902unlock:
4903 hci_dev_unlock(hdev);
4904 return err;
4905}
4906
Johan Hedberga1443f52015-01-23 15:42:46 +02004907static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
4908{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004909 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02004910 struct mgmt_mode *cp;
4911
4912 BT_DBG("%s status %u", hdev->name, status);
4913
4914 hci_dev_lock(hdev);
4915
Johan Hedberg333ae952015-03-17 13:48:47 +02004916 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
Johan Hedberga1443f52015-01-23 15:42:46 +02004917 if (!cmd)
4918 goto unlock;
4919
4920 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02004921 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
4922 mgmt_status(status));
Johan Hedberga1443f52015-01-23 15:42:46 +02004923 goto remove;
4924 }
4925
4926 cp = cmd->param;
4927
4928 switch (cp->val) {
4929 case 0x00:
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004930 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
4931 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004932 break;
4933 case 0x01:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004934 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004935 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004936 break;
4937 case 0x02:
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004938 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
4939 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Johan Hedberga1443f52015-01-23 15:42:46 +02004940 break;
4941 }
4942
4943 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
4944 new_settings(hdev, cmd->sk);
4945
4946remove:
4947 mgmt_pending_remove(cmd);
4948unlock:
4949 hci_dev_unlock(hdev);
4950}
4951
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004952static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
4953 void *data, u16 len)
4954{
4955 struct mgmt_mode *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02004956 struct mgmt_pending_cmd *cmd;
Johan Hedberga1443f52015-01-23 15:42:46 +02004957 struct hci_request req;
Johan Hedberga3209692014-05-26 11:23:35 +03004958 u8 val;
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004959 int err;
4960
4961 BT_DBG("request for %s", hdev->name);
4962
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08004963 if (!lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004964 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004965 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4966 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004967
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004968 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
Johan Hedberg59200282015-01-28 19:56:00 +02004969 lmp_sc_capable(hdev) &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004970 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
Johan Hedberga69e8372015-03-06 21:08:53 +02004971 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
4972 MGMT_STATUS_REJECTED);
Marcel Holtmanned93ec62015-01-22 11:15:22 -08004973
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004974 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02004975 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004976 MGMT_STATUS_INVALID_PARAMS);
4977
4978 hci_dev_lock(hdev);
4979
Marcel Holtmann05b3c3e2014-12-31 14:43:18 -08004980 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07004981 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004982 bool changed;
4983
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004984 if (cp->val) {
Marcel Holtmann238be782015-03-13 02:11:06 -07004985 changed = !hci_dev_test_and_set_flag(hdev,
4986 HCI_SC_ENABLED);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004987 if (cp->val == 0x02)
Marcel Holtmanna1536da2015-03-13 02:11:01 -07004988 hci_dev_set_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004989 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004990 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004991 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07004992 changed = hci_dev_test_and_clear_flag(hdev,
4993 HCI_SC_ENABLED);
Marcel Holtmanna358dc12015-03-13 02:11:02 -07004994 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08004995 }
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08004996
4997 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
4998 if (err < 0)
4999 goto failed;
5000
5001 if (changed)
5002 err = new_settings(hdev, sk);
5003
5004 goto failed;
5005 }
5006
Johan Hedberg333ae952015-03-17 13:48:47 +02005007 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
Johan Hedberga69e8372015-03-06 21:08:53 +02005008 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5009 MGMT_STATUS_BUSY);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005010 goto failed;
5011 }
5012
Marcel Holtmann0ab04a92014-02-01 09:19:57 -08005013 val = !!cp->val;
5014
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005015 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
5016 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005017 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5018 goto failed;
5019 }
5020
5021 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
5022 if (!cmd) {
5023 err = -ENOMEM;
5024 goto failed;
5025 }
5026
Johan Hedberga1443f52015-01-23 15:42:46 +02005027 hci_req_init(&req, hdev);
5028 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
5029 err = hci_req_run(&req, sc_enable_complete);
Marcel Holtmanneac83dc2014-01-10 02:07:23 -08005030 if (err < 0) {
5031 mgmt_pending_remove(cmd);
5032 goto failed;
5033 }
5034
5035failed:
5036 hci_dev_unlock(hdev);
5037 return err;
5038}
5039
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005040static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
5041 void *data, u16 len)
5042{
5043 struct mgmt_mode *cp = data;
Johan Hedbergb97109792014-06-24 14:00:28 +03005044 bool changed, use_changed;
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005045 int err;
5046
5047 BT_DBG("request for %s", hdev->name);
5048
Johan Hedbergb97109792014-06-24 14:00:28 +03005049 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
Johan Hedberga69e8372015-03-06 21:08:53 +02005050 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
5051 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005052
5053 hci_dev_lock(hdev);
5054
5055 if (cp->val)
Marcel Holtmann238be782015-03-13 02:11:06 -07005056 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005057 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005058 changed = hci_dev_test_and_clear_flag(hdev,
5059 HCI_KEEP_DEBUG_KEYS);
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005060
Johan Hedbergb97109792014-06-24 14:00:28 +03005061 if (cp->val == 0x02)
Marcel Holtmann238be782015-03-13 02:11:06 -07005062 use_changed = !hci_dev_test_and_set_flag(hdev,
5063 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005064 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005065 use_changed = hci_dev_test_and_clear_flag(hdev,
5066 HCI_USE_DEBUG_KEYS);
Johan Hedbergb97109792014-06-24 14:00:28 +03005067
5068 if (hdev_is_powered(hdev) && use_changed &&
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07005069 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
Johan Hedbergb97109792014-06-24 14:00:28 +03005070 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
5071 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
5072 sizeof(mode), &mode);
5073 }
5074
Marcel Holtmann4e39ac82014-01-31 11:55:22 -08005075 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
5076 if (err < 0)
5077 goto unlock;
5078
5079 if (changed)
5080 err = new_settings(hdev, sk);
5081
5082unlock:
5083 hci_dev_unlock(hdev);
5084 return err;
5085}
5086
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005087static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5088 u16 len)
5089{
5090 struct mgmt_cp_set_privacy *cp = cp_data;
5091 bool changed;
5092 int err;
5093
5094 BT_DBG("request for %s", hdev->name);
5095
5096 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005097 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5098 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005099
5100 if (cp->privacy != 0x00 && cp->privacy != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02005101 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5102 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005103
5104 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005105 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5106 MGMT_STATUS_REJECTED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005107
5108 hci_dev_lock(hdev);
5109
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005110 /* If user space supports this command it is also expected to
5111 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
5112 */
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005113 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedbergc21c0ea2014-02-24 11:10:30 +02005114
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005115 if (cp->privacy) {
Marcel Holtmann238be782015-03-13 02:11:06 -07005116 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005117 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005118 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005119 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07005120 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005121 memset(hdev->irk, 0, sizeof(hdev->irk));
Marcel Holtmanna358dc12015-03-13 02:11:02 -07005122 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
Johan Hedberg62b04cd2014-02-23 19:42:27 +02005123 }
5124
5125 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
5126 if (err < 0)
5127 goto unlock;
5128
5129 if (changed)
5130 err = new_settings(hdev, sk);
5131
5132unlock:
5133 hci_dev_unlock(hdev);
5134 return err;
5135}
5136
Johan Hedberg41edf162014-02-18 10:19:35 +02005137static bool irk_is_valid(struct mgmt_irk_info *irk)
5138{
5139 switch (irk->addr.type) {
5140 case BDADDR_LE_PUBLIC:
5141 return true;
5142
5143 case BDADDR_LE_RANDOM:
5144 /* Two most significant bits shall be set */
5145 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5146 return false;
5147 return true;
5148 }
5149
5150 return false;
5151}
5152
5153static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5154 u16 len)
5155{
5156 struct mgmt_cp_load_irks *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005157 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
5158 sizeof(struct mgmt_irk_info));
Johan Hedberg41edf162014-02-18 10:19:35 +02005159 u16 irk_count, expected_len;
5160 int i, err;
5161
5162 BT_DBG("request for %s", hdev->name);
5163
5164 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005165 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5166 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberg41edf162014-02-18 10:19:35 +02005167
5168 irk_count = __le16_to_cpu(cp->irk_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005169 if (irk_count > max_irk_count) {
5170 BT_ERR("load_irks: too big irk_count value %u", irk_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005171 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5172 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005173 }
Johan Hedberg41edf162014-02-18 10:19:35 +02005174
5175 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
5176 if (expected_len != len) {
5177 BT_ERR("load_irks: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005178 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005179 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5180 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005181 }
5182
5183 BT_DBG("%s irk_count %u", hdev->name, irk_count);
5184
5185 for (i = 0; i < irk_count; i++) {
5186 struct mgmt_irk_info *key = &cp->irks[i];
5187
5188 if (!irk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005189 return mgmt_cmd_status(sk, hdev->id,
5190 MGMT_OP_LOAD_IRKS,
5191 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg41edf162014-02-18 10:19:35 +02005192 }
5193
5194 hci_dev_lock(hdev);
5195
5196 hci_smp_irks_clear(hdev);
5197
5198 for (i = 0; i < irk_count; i++) {
5199 struct mgmt_irk_info *irk = &cp->irks[i];
5200 u8 addr_type;
5201
5202 if (irk->addr.type == BDADDR_LE_PUBLIC)
5203 addr_type = ADDR_LE_DEV_PUBLIC;
5204 else
5205 addr_type = ADDR_LE_DEV_RANDOM;
5206
5207 hci_add_irk(hdev, &irk->addr.bdaddr, addr_type, irk->val,
5208 BDADDR_ANY);
5209 }
5210
Marcel Holtmanna1536da2015-03-13 02:11:01 -07005211 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
Johan Hedberg41edf162014-02-18 10:19:35 +02005212
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005213 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
Johan Hedberg41edf162014-02-18 10:19:35 +02005214
5215 hci_dev_unlock(hdev);
5216
5217 return err;
5218}
5219
Johan Hedberg3f706b72013-01-20 14:27:16 +02005220static bool ltk_is_valid(struct mgmt_ltk_info *key)
5221{
5222 if (key->master != 0x00 && key->master != 0x01)
5223 return false;
Marcel Holtmann490cb0b2014-02-16 12:59:05 -08005224
5225 switch (key->addr.type) {
5226 case BDADDR_LE_PUBLIC:
5227 return true;
5228
5229 case BDADDR_LE_RANDOM:
5230 /* Two most significant bits shall be set */
5231 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5232 return false;
5233 return true;
5234 }
5235
5236 return false;
Johan Hedberg3f706b72013-01-20 14:27:16 +02005237}
5238
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005239static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03005240 void *cp_data, u16 len)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005241{
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005242 struct mgmt_cp_load_long_term_keys *cp = cp_data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03005243 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
5244 sizeof(struct mgmt_ltk_info));
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005245 u16 key_count, expected_len;
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005246 int i, err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005247
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005248 BT_DBG("request for %s", hdev->name);
5249
5250 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02005251 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5252 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanncf99ba12013-10-02 21:16:08 -07005253
Marcel Holtmann1f350c82012-03-12 20:31:08 -07005254 key_count = __le16_to_cpu(cp->key_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005255 if (key_count > max_key_count) {
5256 BT_ERR("load_ltks: too big key_count value %u", key_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02005257 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5258 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03005259 }
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005260
5261 expected_len = sizeof(*cp) + key_count *
5262 sizeof(struct mgmt_ltk_info);
5263 if (expected_len != len) {
5264 BT_ERR("load_keys: expected %u bytes, got %u bytes",
Johan Hedberg2606ecb2014-03-07 15:04:13 +02005265 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02005266 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5267 MGMT_STATUS_INVALID_PARAMS);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005268 }
5269
Johan Hedbergbdb6d972012-02-28 06:13:32 +02005270 BT_DBG("%s key_count %u", hdev->name, key_count);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005271
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005272 for (i = 0; i < key_count; i++) {
5273 struct mgmt_ltk_info *key = &cp->keys[i];
5274
Johan Hedberg3f706b72013-01-20 14:27:16 +02005275 if (!ltk_is_valid(key))
Johan Hedberga69e8372015-03-06 21:08:53 +02005276 return mgmt_cmd_status(sk, hdev->id,
5277 MGMT_OP_LOAD_LONG_TERM_KEYS,
5278 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg54ad6d82013-01-20 14:27:15 +02005279 }
5280
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005281 hci_dev_lock(hdev);
5282
5283 hci_smp_ltks_clear(hdev);
5284
5285 for (i = 0; i < key_count; i++) {
5286 struct mgmt_ltk_info *key = &cp->keys[i];
Johan Hedbergd7b25452014-05-23 13:19:53 +03005287 u8 type, addr_type, authenticated;
Marcel Holtmann79d95a12013-10-13 03:57:38 -07005288
5289 if (key->addr.type == BDADDR_LE_PUBLIC)
5290 addr_type = ADDR_LE_DEV_PUBLIC;
5291 else
5292 addr_type = ADDR_LE_DEV_RANDOM;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005293
Johan Hedberg61b43352014-05-29 19:36:53 +03005294 switch (key->type) {
5295 case MGMT_LTK_UNAUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005296 authenticated = 0x00;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005297 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005298 break;
5299 case MGMT_LTK_AUTHENTICATED:
Johan Hedbergd7b25452014-05-23 13:19:53 +03005300 authenticated = 0x01;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005301 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
Johan Hedberg61b43352014-05-29 19:36:53 +03005302 break;
Johan Hedberg23fb8de2014-05-23 13:15:37 +03005303 case MGMT_LTK_P256_UNAUTH:
5304 authenticated = 0x00;
5305 type = SMP_LTK_P256;
5306 break;
5307 case MGMT_LTK_P256_AUTH:
5308 authenticated = 0x01;
5309 type = SMP_LTK_P256;
5310 break;
5311 case MGMT_LTK_P256_DEBUG:
5312 authenticated = 0x00;
5313 type = SMP_LTK_P256_DEBUG;
Johan Hedberg61b43352014-05-29 19:36:53 +03005314 default:
5315 continue;
5316 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03005317
Johan Hedberg35d70272014-02-19 14:57:47 +02005318 hci_add_ltk(hdev, &key->addr.bdaddr, addr_type, type,
Johan Hedbergd7b25452014-05-23 13:19:53 +03005319 authenticated, key->val, key->enc_size, key->ediv,
Johan Hedberg35d70272014-02-19 14:57:47 +02005320 key->rand);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005321 }
5322
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005323 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005324 NULL, 0);
5325
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005326 hci_dev_unlock(hdev);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005327
Johan Hedberg715a5bf2013-01-09 15:29:34 +02005328 return err;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03005329}
5330
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005331static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005332{
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005333 struct hci_conn *conn = cmd->user_data;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005334 struct mgmt_rp_get_conn_info rp;
Johan Hedberg9df74652014-12-19 22:26:03 +02005335 int err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005336
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005337 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005338
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005339 if (status == MGMT_STATUS_SUCCESS) {
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005340 rp.rssi = conn->rssi;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005341 rp.tx_power = conn->tx_power;
5342 rp.max_tx_power = conn->max_tx_power;
5343 } else {
5344 rp.rssi = HCI_RSSI_INVALID;
5345 rp.tx_power = HCI_TX_POWER_INVALID;
5346 rp.max_tx_power = HCI_TX_POWER_INVALID;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005347 }
5348
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005349 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
5350 status, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005351
5352 hci_conn_drop(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005353 hci_conn_put(conn);
Johan Hedberg9df74652014-12-19 22:26:03 +02005354
5355 return err;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005356}
5357
Marcel Holtmann1904a852015-01-11 13:50:44 -08005358static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
5359 u16 opcode)
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005360{
5361 struct hci_cp_read_rssi *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005362 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005363 struct hci_conn *conn;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005364 u16 handle;
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005365 u8 status;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005366
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005367 BT_DBG("status 0x%02x", hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005368
5369 hci_dev_lock(hdev);
5370
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005371 /* Commands sent in request are either Read RSSI or Read Transmit Power
5372 * Level so we check which one was last sent to retrieve connection
5373 * handle. Both commands have handle as first parameter so it's safe to
5374 * cast data on the same command struct.
5375 *
5376 * First command sent is always Read RSSI and we fail only if it fails.
5377 * In other case we simply override error to indicate success as we
5378 * already remembered if TX power value is actually valid.
5379 */
5380 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
5381 if (!cp) {
5382 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005383 status = MGMT_STATUS_SUCCESS;
5384 } else {
5385 status = mgmt_status(hci_status);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005386 }
5387
5388 if (!cp) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005389 BT_ERR("invalid sent_cmd in conn_info response");
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005390 goto unlock;
5391 }
5392
5393 handle = __le16_to_cpu(cp->handle);
5394 conn = hci_conn_hash_lookup_handle(hdev, handle);
5395 if (!conn) {
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005396 BT_ERR("unknown handle (%d) in conn_info response", handle);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005397 goto unlock;
5398 }
5399
Johan Hedberg333ae952015-03-17 13:48:47 +02005400 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005401 if (!cmd)
5402 goto unlock;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005403
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005404 cmd->cmd_complete(cmd, status);
5405 mgmt_pending_remove(cmd);
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005406
5407unlock:
5408 hci_dev_unlock(hdev);
5409}
5410
5411static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5412 u16 len)
5413{
5414 struct mgmt_cp_get_conn_info *cp = data;
5415 struct mgmt_rp_get_conn_info rp;
5416 struct hci_conn *conn;
5417 unsigned long conn_info_age;
5418 int err = 0;
5419
5420 BT_DBG("%s", hdev->name);
5421
5422 memset(&rp, 0, sizeof(rp));
5423 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5424 rp.addr.type = cp->addr.type;
5425
5426 if (!bdaddr_type_is_valid(cp->addr.type))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005427 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5428 MGMT_STATUS_INVALID_PARAMS,
5429 &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005430
5431 hci_dev_lock(hdev);
5432
5433 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005434 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5435 MGMT_STATUS_NOT_POWERED, &rp,
5436 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005437 goto unlock;
5438 }
5439
5440 if (cp->addr.type == BDADDR_BREDR)
5441 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5442 &cp->addr.bdaddr);
5443 else
5444 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
5445
5446 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005447 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5448 MGMT_STATUS_NOT_CONNECTED, &rp,
5449 sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005450 goto unlock;
5451 }
5452
Johan Hedberg333ae952015-03-17 13:48:47 +02005453 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005454 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5455 MGMT_STATUS_BUSY, &rp, sizeof(rp));
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005456 goto unlock;
5457 }
5458
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005459 /* To avoid client trying to guess when to poll again for information we
5460 * calculate conn info age as random value between min/max set in hdev.
5461 */
5462 conn_info_age = hdev->conn_info_min_age +
5463 prandom_u32_max(hdev->conn_info_max_age -
5464 hdev->conn_info_min_age);
5465
5466 /* Query controller to refresh cached values if they are too old or were
5467 * never read.
5468 */
Andrzej Kaczmarekf4e2dd52014-05-16 16:48:57 +02005469 if (time_after(jiffies, conn->conn_info_timestamp +
5470 msecs_to_jiffies(conn_info_age)) ||
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005471 !conn->conn_info_timestamp) {
5472 struct hci_request req;
5473 struct hci_cp_read_tx_power req_txp_cp;
5474 struct hci_cp_read_rssi req_rssi_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005475 struct mgmt_pending_cmd *cmd;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005476
5477 hci_req_init(&req, hdev);
5478 req_rssi_cp.handle = cpu_to_le16(conn->handle);
5479 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
5480 &req_rssi_cp);
5481
Andrzej Kaczmarekf7faab02014-05-14 13:43:04 +02005482 /* For LE links TX power does not change thus we don't need to
5483 * query for it once value is known.
5484 */
5485 if (!bdaddr_type_is_le(cp->addr.type) ||
5486 conn->tx_power == HCI_TX_POWER_INVALID) {
5487 req_txp_cp.handle = cpu_to_le16(conn->handle);
5488 req_txp_cp.type = 0x00;
5489 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5490 sizeof(req_txp_cp), &req_txp_cp);
5491 }
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005492
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005493 /* Max TX power needs to be read only once per connection */
5494 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
5495 req_txp_cp.handle = cpu_to_le16(conn->handle);
5496 req_txp_cp.type = 0x01;
5497 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5498 sizeof(req_txp_cp), &req_txp_cp);
5499 }
5500
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005501 err = hci_req_run(&req, conn_info_refresh_complete);
5502 if (err < 0)
5503 goto unlock;
5504
5505 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
5506 data, len);
5507 if (!cmd) {
5508 err = -ENOMEM;
5509 goto unlock;
5510 }
5511
5512 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005513 cmd->user_data = hci_conn_get(conn);
Johan Hedberg9981bdb2014-12-05 13:42:57 +02005514 cmd->cmd_complete = conn_info_cmd_complete;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005515
5516 conn->conn_info_timestamp = jiffies;
5517 } else {
5518 /* Cache is valid, just reply with values cached in hci_conn */
5519 rp.rssi = conn->rssi;
5520 rp.tx_power = conn->tx_power;
Andrzej Kaczmarekeed5daf2014-05-14 13:43:06 +02005521 rp.max_tx_power = conn->max_tx_power;
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005522
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005523 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5524 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
Andrzej Kaczmarekdd983802014-05-14 13:43:03 +02005525 }
5526
5527unlock:
5528 hci_dev_unlock(hdev);
5529 return err;
5530}
5531
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005532static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
Johan Hedberg69487372014-12-05 13:36:07 +02005533{
5534 struct hci_conn *conn = cmd->user_data;
5535 struct mgmt_rp_get_clock_info rp;
5536 struct hci_dev *hdev;
Johan Hedberg9df74652014-12-19 22:26:03 +02005537 int err;
Johan Hedberg69487372014-12-05 13:36:07 +02005538
5539 memset(&rp, 0, sizeof(rp));
5540 memcpy(&rp.addr, &cmd->param, sizeof(rp.addr));
5541
5542 if (status)
5543 goto complete;
5544
5545 hdev = hci_dev_get(cmd->index);
5546 if (hdev) {
5547 rp.local_clock = cpu_to_le32(hdev->clock);
5548 hci_dev_put(hdev);
5549 }
5550
5551 if (conn) {
5552 rp.piconet_clock = cpu_to_le32(conn->clock);
5553 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
5554 }
5555
5556complete:
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005557 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
5558 sizeof(rp));
Johan Hedberg69487372014-12-05 13:36:07 +02005559
5560 if (conn) {
5561 hci_conn_drop(conn);
5562 hci_conn_put(conn);
5563 }
Johan Hedberg9df74652014-12-19 22:26:03 +02005564
5565 return err;
Johan Hedberg69487372014-12-05 13:36:07 +02005566}
5567
Marcel Holtmann1904a852015-01-11 13:50:44 -08005568static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg95868422014-06-28 17:54:07 +03005569{
Johan Hedberg95868422014-06-28 17:54:07 +03005570 struct hci_cp_read_clock *hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005571 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005572 struct hci_conn *conn;
5573
5574 BT_DBG("%s status %u", hdev->name, status);
5575
5576 hci_dev_lock(hdev);
5577
5578 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
5579 if (!hci_cp)
5580 goto unlock;
5581
5582 if (hci_cp->which) {
5583 u16 handle = __le16_to_cpu(hci_cp->handle);
5584 conn = hci_conn_hash_lookup_handle(hdev, handle);
5585 } else {
5586 conn = NULL;
5587 }
5588
Johan Hedberg333ae952015-03-17 13:48:47 +02005589 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005590 if (!cmd)
5591 goto unlock;
5592
Johan Hedberg69487372014-12-05 13:36:07 +02005593 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberg95868422014-06-28 17:54:07 +03005594 mgmt_pending_remove(cmd);
Johan Hedberg95868422014-06-28 17:54:07 +03005595
5596unlock:
5597 hci_dev_unlock(hdev);
5598}
5599
5600static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5601 u16 len)
5602{
5603 struct mgmt_cp_get_clock_info *cp = data;
5604 struct mgmt_rp_get_clock_info rp;
5605 struct hci_cp_read_clock hci_cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005606 struct mgmt_pending_cmd *cmd;
Johan Hedberg95868422014-06-28 17:54:07 +03005607 struct hci_request req;
5608 struct hci_conn *conn;
5609 int err;
5610
5611 BT_DBG("%s", hdev->name);
5612
5613 memset(&rp, 0, sizeof(rp));
5614 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5615 rp.addr.type = cp->addr.type;
5616
5617 if (cp->addr.type != BDADDR_BREDR)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005618 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5619 MGMT_STATUS_INVALID_PARAMS,
5620 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005621
5622 hci_dev_lock(hdev);
5623
5624 if (!hdev_is_powered(hdev)) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005625 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
5626 MGMT_STATUS_NOT_POWERED, &rp,
5627 sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005628 goto unlock;
5629 }
5630
5631 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
5632 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5633 &cp->addr.bdaddr);
5634 if (!conn || conn->state != BT_CONNECTED) {
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005635 err = mgmt_cmd_complete(sk, hdev->id,
5636 MGMT_OP_GET_CLOCK_INFO,
5637 MGMT_STATUS_NOT_CONNECTED,
5638 &rp, sizeof(rp));
Johan Hedberg95868422014-06-28 17:54:07 +03005639 goto unlock;
5640 }
5641 } else {
5642 conn = NULL;
5643 }
5644
5645 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
5646 if (!cmd) {
5647 err = -ENOMEM;
5648 goto unlock;
5649 }
5650
Johan Hedberg69487372014-12-05 13:36:07 +02005651 cmd->cmd_complete = clock_info_cmd_complete;
5652
Johan Hedberg95868422014-06-28 17:54:07 +03005653 hci_req_init(&req, hdev);
5654
5655 memset(&hci_cp, 0, sizeof(hci_cp));
5656 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5657
5658 if (conn) {
5659 hci_conn_hold(conn);
Johan Hedbergf8aaf9b2014-08-17 23:28:57 +03005660 cmd->user_data = hci_conn_get(conn);
Johan Hedberg95868422014-06-28 17:54:07 +03005661
5662 hci_cp.handle = cpu_to_le16(conn->handle);
5663 hci_cp.which = 0x01; /* Piconet clock */
5664 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
5665 }
5666
5667 err = hci_req_run(&req, get_clock_info_complete);
5668 if (err < 0)
5669 mgmt_pending_remove(cmd);
5670
5671unlock:
5672 hci_dev_unlock(hdev);
5673 return err;
5674}
5675
Johan Hedberg5a154e62014-12-19 22:26:02 +02005676static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
5677{
5678 struct hci_conn *conn;
5679
5680 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
5681 if (!conn)
5682 return false;
5683
5684 if (conn->dst_type != type)
5685 return false;
5686
5687 if (conn->state != BT_CONNECTED)
5688 return false;
5689
5690 return true;
5691}
5692
5693/* This function requires the caller holds hdev->lock */
5694static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,
5695 u8 addr_type, u8 auto_connect)
5696{
5697 struct hci_dev *hdev = req->hdev;
5698 struct hci_conn_params *params;
5699
5700 params = hci_conn_params_add(hdev, addr, addr_type);
5701 if (!params)
5702 return -EIO;
5703
5704 if (params->auto_connect == auto_connect)
5705 return 0;
5706
5707 list_del_init(&params->action);
5708
5709 switch (auto_connect) {
5710 case HCI_AUTO_CONN_DISABLED:
5711 case HCI_AUTO_CONN_LINK_LOSS:
5712 __hci_update_background_scan(req);
5713 break;
5714 case HCI_AUTO_CONN_REPORT:
5715 list_add(&params->action, &hdev->pend_le_reports);
5716 __hci_update_background_scan(req);
5717 break;
5718 case HCI_AUTO_CONN_DIRECT:
5719 case HCI_AUTO_CONN_ALWAYS:
5720 if (!is_connected(hdev, addr, addr_type)) {
5721 list_add(&params->action, &hdev->pend_le_conns);
5722 __hci_update_background_scan(req);
5723 }
5724 break;
5725 }
5726
5727 params->auto_connect = auto_connect;
5728
5729 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
5730 auto_connect);
5731
5732 return 0;
5733}
5734
Marcel Holtmann8afef092014-06-29 22:28:34 +02005735static void device_added(struct sock *sk, struct hci_dev *hdev,
5736 bdaddr_t *bdaddr, u8 type, u8 action)
5737{
5738 struct mgmt_ev_device_added ev;
5739
5740 bacpy(&ev.addr.bdaddr, bdaddr);
5741 ev.addr.type = type;
5742 ev.action = action;
5743
5744 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
5745}
5746
Marcel Holtmann1904a852015-01-11 13:50:44 -08005747static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg5a154e62014-12-19 22:26:02 +02005748{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005749 struct mgmt_pending_cmd *cmd;
Johan Hedberg5a154e62014-12-19 22:26:02 +02005750
5751 BT_DBG("status 0x%02x", status);
5752
5753 hci_dev_lock(hdev);
5754
Johan Hedberg333ae952015-03-17 13:48:47 +02005755 cmd = pending_find(MGMT_OP_ADD_DEVICE, hdev);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005756 if (!cmd)
5757 goto unlock;
5758
5759 cmd->cmd_complete(cmd, mgmt_status(status));
5760 mgmt_pending_remove(cmd);
5761
5762unlock:
5763 hci_dev_unlock(hdev);
5764}
5765
Marcel Holtmann2faade52014-06-29 19:44:03 +02005766static int add_device(struct sock *sk, struct hci_dev *hdev,
5767 void *data, u16 len)
5768{
5769 struct mgmt_cp_add_device *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005770 struct mgmt_pending_cmd *cmd;
Johan Hedberg5a154e62014-12-19 22:26:02 +02005771 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005772 u8 auto_conn, addr_type;
5773 int err;
5774
5775 BT_DBG("%s", hdev->name);
5776
Johan Hedberg66593582014-07-09 12:59:14 +03005777 if (!bdaddr_type_is_valid(cp->addr.type) ||
Marcel Holtmann2faade52014-06-29 19:44:03 +02005778 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005779 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5780 MGMT_STATUS_INVALID_PARAMS,
5781 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005782
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005783 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
Johan Hedberg2a1afb52015-03-06 21:08:54 +02005784 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
5785 MGMT_STATUS_INVALID_PARAMS,
5786 &cp->addr, sizeof(cp->addr));
Marcel Holtmann2faade52014-06-29 19:44:03 +02005787
Johan Hedberg5a154e62014-12-19 22:26:02 +02005788 hci_req_init(&req, hdev);
5789
Marcel Holtmann2faade52014-06-29 19:44:03 +02005790 hci_dev_lock(hdev);
5791
Johan Hedberg5a154e62014-12-19 22:26:02 +02005792 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_DEVICE, hdev, data, len);
5793 if (!cmd) {
5794 err = -ENOMEM;
5795 goto unlock;
5796 }
5797
5798 cmd->cmd_complete = addr_cmd_complete;
5799
Johan Hedberg66593582014-07-09 12:59:14 +03005800 if (cp->addr.type == BDADDR_BREDR) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005801 /* Only incoming connections action is supported for now */
Johan Hedberg66593582014-07-09 12:59:14 +03005802 if (cp->action != 0x01) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005803 err = cmd->cmd_complete(cmd,
5804 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005805 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03005806 goto unlock;
5807 }
5808
5809 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
5810 cp->addr.type);
5811 if (err)
5812 goto unlock;
Johan Hedberga3974072014-07-09 12:59:15 +03005813
Johan Hedberg5a154e62014-12-19 22:26:02 +02005814 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03005815
Johan Hedberg66593582014-07-09 12:59:14 +03005816 goto added;
5817 }
5818
Marcel Holtmann2faade52014-06-29 19:44:03 +02005819 if (cp->addr.type == BDADDR_LE_PUBLIC)
5820 addr_type = ADDR_LE_DEV_PUBLIC;
5821 else
5822 addr_type = ADDR_LE_DEV_RANDOM;
5823
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005824 if (cp->action == 0x02)
Marcel Holtmann2faade52014-06-29 19:44:03 +02005825 auto_conn = HCI_AUTO_CONN_ALWAYS;
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02005826 else if (cp->action == 0x01)
5827 auto_conn = HCI_AUTO_CONN_DIRECT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005828 else
Johan Hedberga3451d22014-07-02 17:37:27 +03005829 auto_conn = HCI_AUTO_CONN_REPORT;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005830
Marcel Holtmannbf5b3c82014-06-30 12:34:39 +02005831 /* If the connection parameters don't exist for this device,
5832 * they will be created and configured with defaults.
5833 */
Johan Hedberg5a154e62014-12-19 22:26:02 +02005834 if (hci_conn_params_set(&req, &cp->addr.bdaddr, addr_type,
Marcel Holtmannd06b50c2014-07-01 12:11:06 +02005835 auto_conn) < 0) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005836 err = cmd->cmd_complete(cmd, MGMT_STATUS_FAILED);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005837 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005838 goto unlock;
5839 }
5840
Johan Hedberg66593582014-07-09 12:59:14 +03005841added:
Marcel Holtmann8afef092014-06-29 22:28:34 +02005842 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
5843
Johan Hedberg5a154e62014-12-19 22:26:02 +02005844 err = hci_req_run(&req, add_device_complete);
5845 if (err < 0) {
5846 /* ENODATA means no HCI commands were needed (e.g. if
5847 * the adapter is powered off).
5848 */
Johan Hedberg9df74652014-12-19 22:26:03 +02005849 if (err == -ENODATA)
5850 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg5a154e62014-12-19 22:26:02 +02005851 mgmt_pending_remove(cmd);
5852 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02005853
5854unlock:
5855 hci_dev_unlock(hdev);
5856 return err;
5857}
5858
Marcel Holtmann8afef092014-06-29 22:28:34 +02005859static void device_removed(struct sock *sk, struct hci_dev *hdev,
5860 bdaddr_t *bdaddr, u8 type)
5861{
5862 struct mgmt_ev_device_removed ev;
5863
5864 bacpy(&ev.addr.bdaddr, bdaddr);
5865 ev.addr.type = type;
5866
5867 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
5868}
5869
Marcel Holtmann1904a852015-01-11 13:50:44 -08005870static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005871{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005872 struct mgmt_pending_cmd *cmd;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005873
5874 BT_DBG("status 0x%02x", status);
5875
5876 hci_dev_lock(hdev);
5877
Johan Hedberg333ae952015-03-17 13:48:47 +02005878 cmd = pending_find(MGMT_OP_REMOVE_DEVICE, hdev);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005879 if (!cmd)
5880 goto unlock;
5881
5882 cmd->cmd_complete(cmd, mgmt_status(status));
5883 mgmt_pending_remove(cmd);
5884
5885unlock:
5886 hci_dev_unlock(hdev);
5887}
5888
Marcel Holtmann2faade52014-06-29 19:44:03 +02005889static int remove_device(struct sock *sk, struct hci_dev *hdev,
5890 void *data, u16 len)
5891{
5892 struct mgmt_cp_remove_device *cp = data;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02005893 struct mgmt_pending_cmd *cmd;
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005894 struct hci_request req;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005895 int err;
5896
5897 BT_DBG("%s", hdev->name);
5898
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005899 hci_req_init(&req, hdev);
5900
Marcel Holtmann2faade52014-06-29 19:44:03 +02005901 hci_dev_lock(hdev);
5902
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005903 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_DEVICE, hdev, data, len);
5904 if (!cmd) {
5905 err = -ENOMEM;
5906 goto unlock;
5907 }
5908
5909 cmd->cmd_complete = addr_cmd_complete;
5910
Marcel Holtmann2faade52014-06-29 19:44:03 +02005911 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
Johan Hedbergc71593d2014-07-02 17:37:28 +03005912 struct hci_conn_params *params;
Marcel Holtmann2faade52014-06-29 19:44:03 +02005913 u8 addr_type;
5914
Johan Hedberg66593582014-07-09 12:59:14 +03005915 if (!bdaddr_type_is_valid(cp->addr.type)) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005916 err = cmd->cmd_complete(cmd,
5917 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005918 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005919 goto unlock;
5920 }
5921
Johan Hedberg66593582014-07-09 12:59:14 +03005922 if (cp->addr.type == BDADDR_BREDR) {
5923 err = hci_bdaddr_list_del(&hdev->whitelist,
5924 &cp->addr.bdaddr,
5925 cp->addr.type);
5926 if (err) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005927 err = cmd->cmd_complete(cmd,
5928 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005929 mgmt_pending_remove(cmd);
Johan Hedberg66593582014-07-09 12:59:14 +03005930 goto unlock;
5931 }
5932
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005933 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03005934
Johan Hedberg66593582014-07-09 12:59:14 +03005935 device_removed(sk, hdev, &cp->addr.bdaddr,
5936 cp->addr.type);
5937 goto complete;
5938 }
5939
Marcel Holtmann2faade52014-06-29 19:44:03 +02005940 if (cp->addr.type == BDADDR_LE_PUBLIC)
5941 addr_type = ADDR_LE_DEV_PUBLIC;
5942 else
5943 addr_type = ADDR_LE_DEV_RANDOM;
5944
Johan Hedbergc71593d2014-07-02 17:37:28 +03005945 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
5946 addr_type);
5947 if (!params) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005948 err = cmd->cmd_complete(cmd,
5949 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005950 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03005951 goto unlock;
5952 }
5953
5954 if (params->auto_connect == HCI_AUTO_CONN_DISABLED) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005955 err = cmd->cmd_complete(cmd,
5956 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005957 mgmt_pending_remove(cmd);
Johan Hedbergc71593d2014-07-02 17:37:28 +03005958 goto unlock;
5959 }
5960
Johan Hedbergd1dbf122014-07-04 16:17:23 +03005961 list_del(&params->action);
Johan Hedbergc71593d2014-07-02 17:37:28 +03005962 list_del(&params->list);
5963 kfree(params);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005964 __hci_update_background_scan(&req);
Marcel Holtmann8afef092014-06-29 22:28:34 +02005965
5966 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005967 } else {
Johan Hedberg19de0822014-07-06 13:06:51 +03005968 struct hci_conn_params *p, *tmp;
Johan Hedberg66593582014-07-09 12:59:14 +03005969 struct bdaddr_list *b, *btmp;
Johan Hedberg19de0822014-07-06 13:06:51 +03005970
Marcel Holtmann2faade52014-06-29 19:44:03 +02005971 if (cp->addr.type) {
Johan Hedberg9df74652014-12-19 22:26:03 +02005972 err = cmd->cmd_complete(cmd,
5973 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005974 mgmt_pending_remove(cmd);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005975 goto unlock;
5976 }
5977
Johan Hedberg66593582014-07-09 12:59:14 +03005978 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
5979 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
5980 list_del(&b->list);
5981 kfree(b);
5982 }
5983
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005984 __hci_update_page_scan(&req);
Johan Hedberga3974072014-07-09 12:59:15 +03005985
Johan Hedberg19de0822014-07-06 13:06:51 +03005986 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
5987 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
5988 continue;
5989 device_removed(sk, hdev, &p->addr, p->addr_type);
5990 list_del(&p->action);
5991 list_del(&p->list);
5992 kfree(p);
5993 }
5994
5995 BT_DBG("All LE connection parameters were removed");
5996
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02005997 __hci_update_background_scan(&req);
Marcel Holtmann2faade52014-06-29 19:44:03 +02005998 }
5999
Johan Hedberg66593582014-07-09 12:59:14 +03006000complete:
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006001 err = hci_req_run(&req, remove_device_complete);
6002 if (err < 0) {
6003 /* ENODATA means no HCI commands were needed (e.g. if
6004 * the adapter is powered off).
6005 */
Johan Hedberg9df74652014-12-19 22:26:03 +02006006 if (err == -ENODATA)
6007 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
Johan Hedberg51ef3eb2014-12-19 22:26:01 +02006008 mgmt_pending_remove(cmd);
6009 }
Marcel Holtmann2faade52014-06-29 19:44:03 +02006010
6011unlock:
6012 hci_dev_unlock(hdev);
6013 return err;
6014}
6015
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006016static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
6017 u16 len)
6018{
6019 struct mgmt_cp_load_conn_param *cp = data;
Johan Hedbergba1d6932014-07-03 13:52:27 +03006020 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
6021 sizeof(struct mgmt_conn_param));
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006022 u16 param_count, expected_len;
6023 int i;
6024
6025 if (!lmp_le_capable(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006026 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6027 MGMT_STATUS_NOT_SUPPORTED);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006028
6029 param_count = __le16_to_cpu(cp->param_count);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006030 if (param_count > max_param_count) {
6031 BT_ERR("load_conn_param: too big param_count value %u",
6032 param_count);
Johan Hedberga69e8372015-03-06 21:08:53 +02006033 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6034 MGMT_STATUS_INVALID_PARAMS);
Johan Hedbergba1d6932014-07-03 13:52:27 +03006035 }
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006036
6037 expected_len = sizeof(*cp) + param_count *
6038 sizeof(struct mgmt_conn_param);
6039 if (expected_len != len) {
6040 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
6041 expected_len, len);
Johan Hedberga69e8372015-03-06 21:08:53 +02006042 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6043 MGMT_STATUS_INVALID_PARAMS);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006044 }
6045
6046 BT_DBG("%s param_count %u", hdev->name, param_count);
6047
6048 hci_dev_lock(hdev);
6049
6050 hci_conn_params_clear_disabled(hdev);
6051
6052 for (i = 0; i < param_count; i++) {
6053 struct mgmt_conn_param *param = &cp->params[i];
6054 struct hci_conn_params *hci_param;
6055 u16 min, max, latency, timeout;
6056 u8 addr_type;
6057
6058 BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
6059 param->addr.type);
6060
6061 if (param->addr.type == BDADDR_LE_PUBLIC) {
6062 addr_type = ADDR_LE_DEV_PUBLIC;
6063 } else if (param->addr.type == BDADDR_LE_RANDOM) {
6064 addr_type = ADDR_LE_DEV_RANDOM;
6065 } else {
6066 BT_ERR("Ignoring invalid connection parameters");
6067 continue;
6068 }
6069
6070 min = le16_to_cpu(param->min_interval);
6071 max = le16_to_cpu(param->max_interval);
6072 latency = le16_to_cpu(param->latency);
6073 timeout = le16_to_cpu(param->timeout);
6074
6075 BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
6076 min, max, latency, timeout);
6077
6078 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
6079 BT_ERR("Ignoring invalid connection parameters");
6080 continue;
6081 }
6082
6083 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
6084 addr_type);
6085 if (!hci_param) {
6086 BT_ERR("Failed to add connection parameters");
6087 continue;
6088 }
6089
6090 hci_param->conn_min_interval = min;
6091 hci_param->conn_max_interval = max;
6092 hci_param->conn_latency = latency;
6093 hci_param->supervision_timeout = timeout;
6094 }
6095
6096 hci_dev_unlock(hdev);
6097
Johan Hedberg2a1afb52015-03-06 21:08:54 +02006098 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
6099 NULL, 0);
Johan Hedberga26f3dc2014-07-02 17:37:29 +03006100}
6101
Marcel Holtmanndbece372014-07-04 18:11:55 +02006102static int set_external_config(struct sock *sk, struct hci_dev *hdev,
6103 void *data, u16 len)
6104{
6105 struct mgmt_cp_set_external_config *cp = data;
6106 bool changed;
6107 int err;
6108
6109 BT_DBG("%s", hdev->name);
6110
6111 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006112 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6113 MGMT_STATUS_REJECTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006114
6115 if (cp->config != 0x00 && cp->config != 0x01)
Johan Hedberga69e8372015-03-06 21:08:53 +02006116 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6117 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006118
6119 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
Johan Hedberga69e8372015-03-06 21:08:53 +02006120 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6121 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006122
6123 hci_dev_lock(hdev);
6124
6125 if (cp->config)
Marcel Holtmann238be782015-03-13 02:11:06 -07006126 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006127 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07006128 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
Marcel Holtmanndbece372014-07-04 18:11:55 +02006129
6130 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
6131 if (err < 0)
6132 goto unlock;
6133
6134 if (!changed)
6135 goto unlock;
6136
Marcel Holtmannf4537c02014-07-04 19:06:23 +02006137 err = new_options(hdev, sk);
6138
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006139 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
Marcel Holtmanndbece372014-07-04 18:11:55 +02006140 mgmt_index_removed(hdev);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006141
Marcel Holtmann516018a2015-03-13 02:11:04 -07006142 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006143 hci_dev_set_flag(hdev, HCI_CONFIG);
6144 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006145
6146 queue_work(hdev->req_workqueue, &hdev->power_on);
6147 } else {
Marcel Holtmann5ea234d2014-07-06 12:11:16 +02006148 set_bit(HCI_RAW, &hdev->flags);
Marcel Holtmannd603b76b2014-07-06 12:11:14 +02006149 mgmt_index_added(hdev);
6150 }
Marcel Holtmanndbece372014-07-04 18:11:55 +02006151 }
6152
6153unlock:
6154 hci_dev_unlock(hdev);
6155 return err;
6156}
6157
Marcel Holtmann9713c172014-07-06 12:11:15 +02006158static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6159 void *data, u16 len)
6160{
6161 struct mgmt_cp_set_public_address *cp = data;
6162 bool changed;
6163 int err;
6164
6165 BT_DBG("%s", hdev->name);
6166
6167 if (hdev_is_powered(hdev))
Johan Hedberga69e8372015-03-06 21:08:53 +02006168 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6169 MGMT_STATUS_REJECTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006170
6171 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
Johan Hedberga69e8372015-03-06 21:08:53 +02006172 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6173 MGMT_STATUS_INVALID_PARAMS);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006174
6175 if (!hdev->set_bdaddr)
Johan Hedberga69e8372015-03-06 21:08:53 +02006176 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6177 MGMT_STATUS_NOT_SUPPORTED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006178
6179 hci_dev_lock(hdev);
6180
6181 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
6182 bacpy(&hdev->public_addr, &cp->bdaddr);
6183
6184 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
6185 if (err < 0)
6186 goto unlock;
6187
6188 if (!changed)
6189 goto unlock;
6190
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006191 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
Marcel Holtmann9713c172014-07-06 12:11:15 +02006192 err = new_options(hdev, sk);
6193
6194 if (is_configured(hdev)) {
6195 mgmt_index_removed(hdev);
6196
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006197 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006198
Marcel Holtmanna1536da2015-03-13 02:11:01 -07006199 hci_dev_set_flag(hdev, HCI_CONFIG);
6200 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
Marcel Holtmann9713c172014-07-06 12:11:15 +02006201
6202 queue_work(hdev->req_workqueue, &hdev->power_on);
6203 }
6204
6205unlock:
6206 hci_dev_unlock(hdev);
6207 return err;
6208}
6209
Marcel Holtmannbea41602015-03-14 22:43:17 -07006210static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
6211 u8 data_len)
6212{
6213 eir[eir_len++] = sizeof(type) + data_len;
6214 eir[eir_len++] = type;
6215 memcpy(&eir[eir_len], data, data_len);
6216 eir_len += data_len;
6217
6218 return eir_len;
6219}
6220
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006221static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
6222 void *data, u16 data_len)
6223{
6224 struct mgmt_cp_read_local_oob_ext_data *cp = data;
6225 struct mgmt_rp_read_local_oob_ext_data *rp;
6226 size_t rp_len;
6227 u16 eir_len;
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006228 u8 status, flags, role, addr[7], hash[16], rand[16];
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006229 int err;
6230
6231 BT_DBG("%s", hdev->name);
6232
6233 if (!hdev_is_powered(hdev))
6234 return mgmt_cmd_complete(sk, hdev->id,
6235 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6236 MGMT_STATUS_NOT_POWERED,
6237 &cp->type, sizeof(cp->type));
6238
6239 switch (cp->type) {
6240 case BIT(BDADDR_BREDR):
6241 status = mgmt_bredr_support(hdev);
6242 if (status)
6243 return mgmt_cmd_complete(sk, hdev->id,
6244 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6245 status, &cp->type,
6246 sizeof(cp->type));
6247 eir_len = 5;
6248 break;
6249 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
6250 status = mgmt_le_support(hdev);
6251 if (status)
6252 return mgmt_cmd_complete(sk, hdev->id,
6253 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6254 status, &cp->type,
6255 sizeof(cp->type));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006256 eir_len = 9 + 3 + 18 + 18 + 3;
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006257 break;
6258 default:
6259 return mgmt_cmd_complete(sk, hdev->id,
6260 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6261 MGMT_STATUS_INVALID_PARAMS,
6262 &cp->type, sizeof(cp->type));
6263 }
6264
6265 hci_dev_lock(hdev);
6266
6267 rp_len = sizeof(*rp) + eir_len;
6268 rp = kmalloc(rp_len, GFP_ATOMIC);
6269 if (!rp) {
6270 hci_dev_unlock(hdev);
6271 return -ENOMEM;
6272 }
6273
6274 eir_len = 0;
6275 switch (cp->type) {
6276 case BIT(BDADDR_BREDR):
6277 eir_len = eir_append_data(rp->eir, eir_len, EIR_CLASS_OF_DEV,
6278 hdev->dev_class, 3);
6279 break;
6280 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
Marcel Holtmann5082a592015-03-16 12:39:00 -07006281 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
6282 smp_generate_oob(hdev, hash, rand) < 0) {
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006283 hci_dev_unlock(hdev);
6284 err = mgmt_cmd_complete(sk, hdev->id,
Marcel Holtmann5082a592015-03-16 12:39:00 -07006285 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
6286 MGMT_STATUS_FAILED,
6287 &cp->type, sizeof(cp->type));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006288 goto done;
6289 }
6290
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006291 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
6292 memcpy(addr, &hdev->rpa, 6);
6293 addr[6] = 0x01;
6294 } else if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
6295 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
6296 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
6297 bacmp(&hdev->static_addr, BDADDR_ANY))) {
6298 memcpy(addr, &hdev->static_addr, 6);
6299 addr[6] = 0x01;
6300 } else {
6301 memcpy(addr, &hdev->bdaddr, 6);
6302 addr[6] = 0x00;
6303 }
6304
6305 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
6306 addr, sizeof(addr));
6307
6308 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
6309 role = 0x02;
6310 else
6311 role = 0x01;
6312
6313 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
6314 &role, sizeof(role));
6315
Marcel Holtmann5082a592015-03-16 12:39:00 -07006316 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
6317 eir_len = eir_append_data(rp->eir, eir_len,
6318 EIR_LE_SC_CONFIRM,
6319 hash, sizeof(hash));
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006320
Marcel Holtmann5082a592015-03-16 12:39:00 -07006321 eir_len = eir_append_data(rp->eir, eir_len,
6322 EIR_LE_SC_RANDOM,
6323 rand, sizeof(rand));
6324 }
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006325
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006326 flags = get_adv_discov_flags(hdev);
6327
6328 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
6329 flags |= LE_AD_NO_BREDR;
6330
6331 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
6332 &flags, sizeof(flags));
6333 break;
6334 }
6335
6336 rp->type = cp->type;
6337 rp->eir_len = cpu_to_le16(eir_len);
6338
6339 hci_dev_unlock(hdev);
6340
Marcel Holtmann72000df2015-03-16 16:11:21 -07006341 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
6342
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006343 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
Marcel Holtmann5425f982015-03-16 16:05:44 -07006344 MGMT_STATUS_SUCCESS, rp, sizeof(*rp) + eir_len);
Marcel Holtmann72000df2015-03-16 16:11:21 -07006345 if (err < 0)
6346 goto done;
6347
6348 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6349 rp, sizeof(*rp) + eir_len,
6350 HCI_MGMT_OOB_DATA_EVENTS, sk);
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006351
Marcel Holtmann0821a2c2015-03-16 01:10:23 -07006352done:
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006353 kfree(rp);
6354
6355 return err;
6356}
6357
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006358static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
6359 void *data, u16 data_len)
6360{
6361 struct mgmt_rp_read_adv_features *rp;
6362 size_t rp_len;
6363 int err;
Arman Uguray24b4f382015-03-23 15:57:12 -07006364 bool instance;
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006365
6366 BT_DBG("%s", hdev->name);
6367
6368 hci_dev_lock(hdev);
6369
6370 rp_len = sizeof(*rp);
Arman Uguray24b4f382015-03-23 15:57:12 -07006371
6372 /* Currently only one instance is supported, so just add 1 to the
6373 * response length.
6374 */
6375 instance = hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE);
6376 if (instance)
6377 rp_len++;
6378
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006379 rp = kmalloc(rp_len, GFP_ATOMIC);
6380 if (!rp) {
6381 hci_dev_unlock(hdev);
6382 return -ENOMEM;
6383 }
6384
6385 rp->supported_flags = cpu_to_le32(0);
Marcel Holtmanndc5d82a2015-03-19 17:22:25 -07006386 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
6387 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
Arman Uguray24b4f382015-03-23 15:57:12 -07006388 rp->max_instances = 1;
6389
6390 /* Currently only one instance is supported, so simply return the
6391 * current instance number.
6392 */
6393 if (instance) {
6394 rp->num_instances = 1;
6395 rp->instance[0] = 1;
6396 } else {
6397 rp->num_instances = 0;
6398 }
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006399
6400 hci_dev_unlock(hdev);
6401
6402 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6403 MGMT_STATUS_SUCCESS, rp, rp_len);
6404
6405 kfree(rp);
6406
6407 return err;
6408}
6409
Arman Uguray24b4f382015-03-23 15:57:12 -07006410static bool adv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *adv_data,
6411 u8 adv_data_len)
6412{
6413 u8 max_adv_len = HCI_MAX_AD_LENGTH;
6414 int i, cur_len;
6415
6416 /* TODO: Correctly reduce adv_len based on adv_flags. */
6417
6418 if (adv_data_len > max_adv_len)
6419 return false;
6420
6421 /* Make sure that adv_data is correctly formatted. */
6422 for (i = 0, cur_len = 0; i < adv_data_len; i += (cur_len + 1)) {
6423 cur_len = adv_data[i];
6424
6425 /* If the current field length would exceed the total data
6426 * length, then it's invalid.
6427 */
6428 if (i + cur_len >= adv_data_len)
6429 return false;
6430 }
6431
6432 return true;
6433}
6434
6435static void advertising_added(struct sock *sk, struct hci_dev *hdev,
6436 u8 instance)
6437{
6438 struct mgmt_ev_advertising_added ev;
6439
6440 ev.instance = instance;
6441
6442 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
6443}
6444
6445static void advertising_removed(struct sock *sk, struct hci_dev *hdev,
6446 u8 instance)
6447{
6448 struct mgmt_ev_advertising_removed ev;
6449
6450 ev.instance = instance;
6451
6452 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
6453}
6454
6455static void add_advertising_complete(struct hci_dev *hdev, u8 status,
6456 u16 opcode)
6457{
6458 struct mgmt_pending_cmd *cmd;
6459 struct mgmt_rp_add_advertising rp;
6460
6461 BT_DBG("status %d", status);
6462
6463 hci_dev_lock(hdev);
6464
6465 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
6466
6467 if (status) {
6468 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
6469 memset(&hdev->adv_instance, 0, sizeof(hdev->adv_instance));
6470 advertising_removed(cmd ? cmd->sk : NULL, hdev, 1);
6471 }
6472
6473 if (!cmd)
6474 goto unlock;
6475
6476 rp.instance = 0x01;
6477
6478 if (status)
6479 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
6480 mgmt_status(status));
6481 else
6482 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
6483 mgmt_status(status), &rp, sizeof(rp));
6484
6485 mgmt_pending_remove(cmd);
6486
6487unlock:
6488 hci_dev_unlock(hdev);
6489}
6490
6491static int add_advertising(struct sock *sk, struct hci_dev *hdev,
6492 void *data, u16 data_len)
6493{
6494 struct mgmt_cp_add_advertising *cp = data;
6495 struct mgmt_rp_add_advertising rp;
6496 u32 flags;
6497 u8 status;
6498 int err;
6499 struct mgmt_pending_cmd *cmd;
6500 struct hci_request req;
6501
6502 BT_DBG("%s", hdev->name);
6503
6504 status = mgmt_le_support(hdev);
6505 if (status)
6506 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6507 status);
6508
6509 flags = __le32_to_cpu(cp->flags);
6510
6511 /* The current implementation only supports adding one instance and
6512 * doesn't support flags.
6513 */
6514 if (cp->instance != 0x01 || flags)
6515 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6516 MGMT_STATUS_INVALID_PARAMS);
6517
6518 hci_dev_lock(hdev);
6519
6520 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
6521 pending_find(MGMT_OP_SET_LE, hdev)) {
6522 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6523 MGMT_STATUS_BUSY);
6524 goto unlock;
6525 }
6526
6527 if (!adv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len)) {
6528 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6529 MGMT_STATUS_INVALID_PARAMS);
6530 goto unlock;
6531 }
6532
6533 hdev->adv_instance.flags = flags;
6534 hdev->adv_instance.adv_data_len = cp->adv_data_len;
6535 hdev->adv_instance.scan_rsp_len = cp->scan_rsp_len;
6536
6537 if (cp->adv_data_len)
6538 memcpy(hdev->adv_instance.adv_data, cp->data, cp->adv_data_len);
6539
6540 if (cp->scan_rsp_len)
6541 memcpy(hdev->adv_instance.scan_rsp_data,
6542 cp->data + cp->adv_data_len, cp->scan_rsp_len);
6543
6544 if (!hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING_INSTANCE))
6545 advertising_added(sk, hdev, 1);
6546
6547 /* If the HCI_ADVERTISING flag is set or the device isn't powered then
6548 * we have no HCI communication to make. Simply return.
6549 */
6550 if (!hdev_is_powered(hdev) ||
6551 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
6552 rp.instance = 0x01;
6553 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
6554 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
6555 goto unlock;
6556 }
6557
6558 /* We're good to go, update advertising data, parameters, and start
6559 * advertising.
6560 */
6561 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
6562 data_len);
6563 if (!cmd) {
6564 err = -ENOMEM;
6565 goto unlock;
6566 }
6567
6568 hci_req_init(&req, hdev);
6569
6570 update_adv_data(&req);
6571 enable_advertising(&req);
6572
6573 err = hci_req_run(&req, add_advertising_complete);
6574 if (err < 0)
6575 mgmt_pending_remove(cmd);
6576
6577unlock:
6578 hci_dev_unlock(hdev);
6579
6580 return err;
6581}
6582
Johan Hedberg6d785aa32015-03-06 21:08:51 +02006583static const struct hci_mgmt_handler mgmt_handlers[] = {
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006584 { NULL }, /* 0x0000 (no command) */
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006585 { read_version, MGMT_READ_VERSION_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006586 HCI_MGMT_NO_HDEV |
6587 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006588 { read_commands, MGMT_READ_COMMANDS_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006589 HCI_MGMT_NO_HDEV |
6590 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006591 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006592 HCI_MGMT_NO_HDEV |
6593 HCI_MGMT_UNTRUSTED },
6594 { read_controller_info, MGMT_READ_INFO_SIZE,
6595 HCI_MGMT_UNTRUSTED },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006596 { set_powered, MGMT_SETTING_SIZE },
6597 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
6598 { set_connectable, MGMT_SETTING_SIZE },
6599 { set_fast_connectable, MGMT_SETTING_SIZE },
6600 { set_bondable, MGMT_SETTING_SIZE },
6601 { set_link_security, MGMT_SETTING_SIZE },
6602 { set_ssp, MGMT_SETTING_SIZE },
6603 { set_hs, MGMT_SETTING_SIZE },
6604 { set_le, MGMT_SETTING_SIZE },
6605 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
6606 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
6607 { add_uuid, MGMT_ADD_UUID_SIZE },
6608 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006609 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
6610 HCI_MGMT_VAR_LEN },
6611 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
6612 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006613 { disconnect, MGMT_DISCONNECT_SIZE },
6614 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
6615 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
6616 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
6617 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
6618 { pair_device, MGMT_PAIR_DEVICE_SIZE },
6619 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
6620 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
6621 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
6622 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
6623 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
6624 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006625 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
6626 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
6627 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006628 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
6629 { start_discovery, MGMT_START_DISCOVERY_SIZE },
6630 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
6631 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
6632 { block_device, MGMT_BLOCK_DEVICE_SIZE },
6633 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
6634 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
6635 { set_advertising, MGMT_SETTING_SIZE },
6636 { set_bredr, MGMT_SETTING_SIZE },
6637 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
6638 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
6639 { set_secure_conn, MGMT_SETTING_SIZE },
6640 { set_debug_keys, MGMT_SETTING_SIZE },
6641 { set_privacy, MGMT_SET_PRIVACY_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006642 { load_irks, MGMT_LOAD_IRKS_SIZE,
6643 HCI_MGMT_VAR_LEN },
Marcel Holtmann7aea86162015-03-14 19:28:02 -07006644 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
6645 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
6646 { add_device, MGMT_ADD_DEVICE_SIZE },
6647 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006648 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
6649 HCI_MGMT_VAR_LEN },
6650 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006651 HCI_MGMT_NO_HDEV |
6652 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006653 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006654 HCI_MGMT_UNCONFIGURED |
6655 HCI_MGMT_UNTRUSTED },
Johan Hedbergb9a245fb2015-03-06 21:08:52 +02006656 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
6657 HCI_MGMT_UNCONFIGURED },
6658 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
6659 HCI_MGMT_UNCONFIGURED },
6660 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
6661 HCI_MGMT_VAR_LEN },
Marcel Holtmann4f0f1552015-03-14 22:43:19 -07006662 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
Marcel Holtmann96f14742015-03-14 19:27:57 -07006663 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
Marcel Holtmannc91041d2015-03-14 19:28:01 -07006664 HCI_MGMT_NO_HDEV |
6665 HCI_MGMT_UNTRUSTED },
Marcel Holtmannd3d53052015-03-14 20:53:25 -07006666 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
Arman Uguray24b4f382015-03-23 15:57:12 -07006667 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
6668 HCI_MGMT_VAR_LEN },
Johan Hedberg0f4e68c2012-02-28 17:18:30 +02006669};
6670
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006671void mgmt_index_added(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006672{
Marcel Holtmannced85542015-03-14 19:27:56 -07006673 struct mgmt_ev_ext_index ev;
Andrei Emeltchenkobb4b2a92012-07-19 17:03:40 +03006674
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02006675 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6676 return;
6677
Marcel Holtmannf9207332015-03-14 19:27:55 -07006678 switch (hdev->dev_type) {
6679 case HCI_BREDR:
6680 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
6681 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
6682 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006683 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006684 } else {
6685 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
6686 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006687 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006688 }
6689 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07006690 case HCI_AMP:
6691 ev.type = 0x02;
6692 break;
6693 default:
6694 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006695 }
Marcel Holtmannced85542015-03-14 19:27:56 -07006696
6697 ev.bus = hdev->bus;
6698
6699 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
6700 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006701}
6702
Marcel Holtmannbf6b56d2013-10-06 23:55:45 -07006703void mgmt_index_removed(struct hci_dev *hdev)
Johan Hedbergc71e97b2010-12-13 21:07:07 +02006704{
Marcel Holtmannced85542015-03-14 19:27:56 -07006705 struct mgmt_ev_ext_index ev;
Johan Hedberg5f159032012-03-02 03:13:19 +02006706 u8 status = MGMT_STATUS_INVALID_INDEX;
Johan Hedbergb24752f2011-11-03 14:40:33 +02006707
Marcel Holtmann0602a8a2014-07-02 21:30:54 +02006708 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
6709 return;
6710
Marcel Holtmannf9207332015-03-14 19:27:55 -07006711 switch (hdev->dev_type) {
6712 case HCI_BREDR:
6713 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedbergb24752f2011-11-03 14:40:33 +02006714
Marcel Holtmannf9207332015-03-14 19:27:55 -07006715 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
6716 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
6717 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006718 ev.type = 0x01;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006719 } else {
6720 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
6721 HCI_MGMT_INDEX_EVENTS);
Marcel Holtmannced85542015-03-14 19:27:56 -07006722 ev.type = 0x00;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006723 }
6724 break;
Marcel Holtmannced85542015-03-14 19:27:56 -07006725 case HCI_AMP:
6726 ev.type = 0x02;
6727 break;
6728 default:
6729 return;
Marcel Holtmannf9207332015-03-14 19:27:55 -07006730 }
Marcel Holtmannced85542015-03-14 19:27:56 -07006731
6732 ev.bus = hdev->bus;
6733
6734 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
6735 HCI_MGMT_EXT_INDEX_EVENTS);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02006736}
6737
Andre Guedes6046dc32014-02-26 20:21:51 -03006738/* This function requires the caller holds hdev->lock */
Johan Hedberg2cf22212014-12-19 22:26:00 +02006739static void restart_le_actions(struct hci_request *req)
Andre Guedes6046dc32014-02-26 20:21:51 -03006740{
Johan Hedberg2cf22212014-12-19 22:26:00 +02006741 struct hci_dev *hdev = req->hdev;
Andre Guedes6046dc32014-02-26 20:21:51 -03006742 struct hci_conn_params *p;
6743
6744 list_for_each_entry(p, &hdev->le_conn_params, list) {
Johan Hedbergd7347f32014-07-04 12:37:23 +03006745 /* Needed for AUTO_OFF case where might not "really"
6746 * have been powered off.
6747 */
6748 list_del_init(&p->action);
6749
6750 switch (p->auto_connect) {
Marcel Holtmann4b9e7e72014-07-23 21:55:23 +02006751 case HCI_AUTO_CONN_DIRECT:
Johan Hedbergd7347f32014-07-04 12:37:23 +03006752 case HCI_AUTO_CONN_ALWAYS:
6753 list_add(&p->action, &hdev->pend_le_conns);
6754 break;
6755 case HCI_AUTO_CONN_REPORT:
6756 list_add(&p->action, &hdev->pend_le_reports);
6757 break;
6758 default:
6759 break;
Marcel Holtmannc83ed192014-07-01 19:28:24 +02006760 }
Andre Guedes6046dc32014-02-26 20:21:51 -03006761 }
Marcel Holtmannc83ed192014-07-01 19:28:24 +02006762
Johan Hedberg2cf22212014-12-19 22:26:00 +02006763 __hci_update_background_scan(req);
Andre Guedes6046dc32014-02-26 20:21:51 -03006764}
6765
Marcel Holtmann1904a852015-01-11 13:50:44 -08006766static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Johan Hedberg229ab392013-03-15 17:06:53 -05006767{
6768 struct cmd_lookup match = { NULL, hdev };
6769
6770 BT_DBG("status 0x%02x", status);
6771
Marcel Holtmann162a3ba2015-01-14 15:43:11 -08006772 if (!status) {
6773 /* Register the available SMP channels (BR/EDR and LE) only
6774 * when successfully powering on the controller. This late
6775 * registration is required so that LE SMP can clearly
6776 * decide if the public address or static address is used.
6777 */
6778 smp_register(hdev);
6779 }
6780
Johan Hedberg229ab392013-03-15 17:06:53 -05006781 hci_dev_lock(hdev);
6782
6783 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
6784
6785 new_settings(hdev, match.sk);
6786
6787 hci_dev_unlock(hdev);
6788
6789 if (match.sk)
6790 sock_put(match.sk);
6791}
6792
Johan Hedberg70da6242013-03-15 17:06:51 -05006793static int powered_update_hci(struct hci_dev *hdev)
6794{
Johan Hedberg890ea892013-03-15 17:06:52 -05006795 struct hci_request req;
Johan Hedberg70da6242013-03-15 17:06:51 -05006796 u8 link_sec;
6797
Johan Hedberg890ea892013-03-15 17:06:52 -05006798 hci_req_init(&req, hdev);
6799
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006800 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
Johan Hedberg70da6242013-03-15 17:06:51 -05006801 !lmp_host_ssp_capable(hdev)) {
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08006802 u8 mode = 0x01;
Johan Hedberg70da6242013-03-15 17:06:51 -05006803
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08006804 hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
Johan Hedberg70da6242013-03-15 17:06:51 -05006805
Marcel Holtmann574ea3c2015-01-22 11:15:20 -08006806 if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
6807 u8 support = 0x01;
6808
6809 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT,
6810 sizeof(support), &support);
6811 }
Johan Hedbergec6f99b2014-12-12 13:30:11 +02006812 }
6813
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006814 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
Johan Hedbergc73eee92013-04-19 18:35:21 +03006815 lmp_bredr_capable(hdev)) {
Johan Hedberg70da6242013-03-15 17:06:51 -05006816 struct hci_cp_write_le_host_supported cp;
6817
Marcel Holtmann32226e42014-07-24 20:04:16 +02006818 cp.le = 0x01;
6819 cp.simul = 0x00;
Johan Hedberg70da6242013-03-15 17:06:51 -05006820
6821 /* Check first if we already have the right
6822 * host state (host features set)
6823 */
6824 if (cp.le != lmp_host_le_capable(hdev) ||
6825 cp.simul != lmp_host_le_br_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05006826 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
6827 sizeof(cp), &cp);
Johan Hedberg70da6242013-03-15 17:06:51 -05006828 }
6829
Marcel Holtmannd13eafc2013-10-02 04:41:30 -07006830 if (lmp_le_capable(hdev)) {
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07006831 /* Make sure the controller has a good default for
6832 * advertising data. This also applies to the case
6833 * where BR/EDR was toggled during the AUTO_OFF phase.
6834 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006835 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
Marcel Holtmann5947f4b2013-10-16 00:16:50 -07006836 update_adv_data(&req);
Marcel Holtmannf14d8f62013-10-16 00:16:48 -07006837 update_scan_rsp_data(&req);
6838 }
Marcel Holtmann441ad2d2013-10-15 06:33:52 -07006839
Arman Uguray24b4f382015-03-23 15:57:12 -07006840 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
6841 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Marcel Holtmannbba3aa52013-10-06 02:55:21 -07006842 enable_advertising(&req);
Johan Hedberg2cf22212014-12-19 22:26:00 +02006843
6844 restart_le_actions(&req);
Johan Hedbergeeca6f82013-09-25 13:26:09 +03006845 }
6846
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006847 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg70da6242013-03-15 17:06:51 -05006848 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
Johan Hedberg890ea892013-03-15 17:06:52 -05006849 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
6850 sizeof(link_sec), &link_sec);
Johan Hedberg70da6242013-03-15 17:06:51 -05006851
6852 if (lmp_bredr_capable(hdev)) {
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006853 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
Johan Hedberg406ef2a2015-03-10 20:14:27 +02006854 write_fast_connectable(&req, true);
6855 else
6856 write_fast_connectable(&req, false);
Johan Hedberg1d2dc5b2014-12-19 13:40:19 +02006857 __hci_update_page_scan(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05006858 update_class(&req);
Johan Hedberg13928972013-03-15 17:07:00 -05006859 update_name(&req);
Johan Hedberg890ea892013-03-15 17:06:52 -05006860 update_eir(&req);
Johan Hedberg70da6242013-03-15 17:06:51 -05006861 }
6862
Johan Hedberg229ab392013-03-15 17:06:53 -05006863 return hci_req_run(&req, powered_complete);
Johan Hedberg70da6242013-03-15 17:06:51 -05006864}
6865
Johan Hedberg744cf192011-11-08 20:40:14 +02006866int mgmt_powered(struct hci_dev *hdev, u8 powered)
Johan Hedberg5add6af2010-12-16 10:00:37 +02006867{
Johan Hedberg76a7f3a2012-02-17 00:34:40 +02006868 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg98459042014-12-12 11:15:21 +02006869 u8 status, zero_cod[] = { 0, 0, 0 };
Johan Hedberg7bb895d2012-02-17 01:20:00 +02006870 int err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02006871
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006872 if (!hci_dev_test_flag(hdev, HCI_MGMT))
Johan Hedberg5e5282b2012-02-21 16:01:30 +02006873 return 0;
6874
Johan Hedberg5e5282b2012-02-21 16:01:30 +02006875 if (powered) {
Johan Hedberg229ab392013-03-15 17:06:53 -05006876 if (powered_update_hci(hdev) == 0)
6877 return 0;
Johan Hedbergfe038882013-01-16 16:15:34 +02006878
Johan Hedberg229ab392013-03-15 17:06:53 -05006879 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp,
6880 &match);
6881 goto new_settings;
Johan Hedbergb24752f2011-11-03 14:40:33 +02006882 }
6883
Johan Hedberg229ab392013-03-15 17:06:53 -05006884 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
Johan Hedberg98459042014-12-12 11:15:21 +02006885
6886 /* If the power off is because of hdev unregistration let
6887 * use the appropriate INVALID_INDEX status. Otherwise use
6888 * NOT_POWERED. We cover both scenarios here since later in
6889 * mgmt_index_removed() any hci_conn callbacks will have already
6890 * been triggered, potentially causing misleading DISCONNECTED
6891 * status responses.
6892 */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006893 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
Johan Hedberg98459042014-12-12 11:15:21 +02006894 status = MGMT_STATUS_INVALID_INDEX;
6895 else
6896 status = MGMT_STATUS_NOT_POWERED;
6897
6898 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
Johan Hedberg229ab392013-03-15 17:06:53 -05006899
6900 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07006901 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
6902 zero_cod, sizeof(zero_cod), NULL);
Johan Hedberg229ab392013-03-15 17:06:53 -05006903
6904new_settings:
Johan Hedbergbeadb2b2012-02-21 16:55:31 +02006905 err = new_settings(hdev, match.sk);
Johan Hedbergeec8d2b2010-12-16 10:17:38 +02006906
6907 if (match.sk)
6908 sock_put(match.sk);
6909
Johan Hedberg7bb895d2012-02-17 01:20:00 +02006910 return err;
Johan Hedberg5add6af2010-12-16 10:00:37 +02006911}
Johan Hedberg73f22f62010-12-29 16:00:25 +02006912
Marcel Holtmann3eec7052013-10-06 23:55:46 -07006913void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
Johan Hedberg96570ff2013-05-29 09:51:29 +03006914{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02006915 struct mgmt_pending_cmd *cmd;
Johan Hedberg96570ff2013-05-29 09:51:29 +03006916 u8 status;
6917
Johan Hedberg333ae952015-03-17 13:48:47 +02006918 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006919 if (!cmd)
Marcel Holtmann3eec7052013-10-06 23:55:46 -07006920 return;
Johan Hedberg96570ff2013-05-29 09:51:29 +03006921
6922 if (err == -ERFKILL)
6923 status = MGMT_STATUS_RFKILLED;
6924 else
6925 status = MGMT_STATUS_FAILED;
6926
Johan Hedberga69e8372015-03-06 21:08:53 +02006927 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006928
6929 mgmt_pending_remove(cmd);
Johan Hedberg96570ff2013-05-29 09:51:29 +03006930}
6931
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006932void mgmt_discoverable_timeout(struct hci_dev *hdev)
6933{
6934 struct hci_request req;
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006935
6936 hci_dev_lock(hdev);
6937
6938 /* When discoverable timeout triggers, then just make sure
6939 * the limited discoverable flag is cleared. Even in the case
6940 * of a timeout triggered from general discoverable, it is
6941 * safe to unconditionally clear the flag.
6942 */
Marcel Holtmanna358dc12015-03-13 02:11:02 -07006943 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
6944 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006945
6946 hci_req_init(&req, hdev);
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07006947 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
Johan Hedberg4b580612013-10-19 23:38:21 +03006948 u8 scan = SCAN_PAGE;
6949 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
6950 sizeof(scan), &scan);
6951 }
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006952 update_class(&req);
Arman Uguray24b4f382015-03-23 15:57:12 -07006953
6954 /* Advertising instances don't use the global discoverable setting, so
6955 * only update AD if advertising was enabled using Set Advertising.
6956 */
6957 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
6958 update_adv_data(&req);
6959
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006960 hci_req_run(&req, NULL);
6961
6962 hdev->discov_timeout = 0;
6963
Johan Hedberg9a43e252013-10-20 19:00:07 +03006964 new_settings(hdev, NULL);
6965
Marcel Holtmannd1967ff2013-10-15 10:57:40 -07006966 hci_dev_unlock(hdev);
6967}
6968
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07006969void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
6970 bool persistent)
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006971{
Johan Hedberg86742e12011-11-07 23:13:38 +02006972 struct mgmt_ev_new_link_key ev;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006973
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006974 memset(&ev, 0, sizeof(ev));
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006975
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006976 ev.store_hint = persistent;
Johan Hedbergd753fdc2012-02-17 14:06:34 +02006977 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03006978 ev.key.addr.type = BDADDR_BREDR;
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006979 ev.key.type = key->type;
Andrei Emeltchenko9b3b4462012-05-23 11:31:20 +03006980 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
Vinicius Costa Gomesa492cd52011-08-25 20:02:29 -03006981 ev.key.pin_len = key->pin_len;
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006982
Marcel Holtmanndc4a5ee2013-10-15 10:15:57 -07006983 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg55ed8ca12011-01-17 14:41:05 +02006984}
Johan Hedbergf7520542011-01-20 12:34:39 +02006985
Johan Hedbergd7b25452014-05-23 13:19:53 +03006986static u8 mgmt_ltk_type(struct smp_ltk *ltk)
6987{
Johan Hedberg23fb8de2014-05-23 13:15:37 +03006988 switch (ltk->type) {
6989 case SMP_LTK:
6990 case SMP_LTK_SLAVE:
6991 if (ltk->authenticated)
6992 return MGMT_LTK_AUTHENTICATED;
6993 return MGMT_LTK_UNAUTHENTICATED;
6994 case SMP_LTK_P256:
6995 if (ltk->authenticated)
6996 return MGMT_LTK_P256_AUTH;
6997 return MGMT_LTK_P256_UNAUTH;
6998 case SMP_LTK_P256_DEBUG:
6999 return MGMT_LTK_P256_DEBUG;
7000 }
Johan Hedbergd7b25452014-05-23 13:19:53 +03007001
7002 return MGMT_LTK_UNAUTHENTICATED;
7003}
7004
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007005void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007006{
7007 struct mgmt_ev_new_long_term_key ev;
7008
7009 memset(&ev, 0, sizeof(ev));
7010
Marcel Holtmann5192d302014-02-19 17:11:58 -08007011 /* Devices using resolvable or non-resolvable random addresses
7012 * without providing an indentity resolving key don't require
7013 * to store long term keys. Their addresses will change the
7014 * next time around.
7015 *
7016 * Only when a remote device provides an identity address
7017 * make sure the long term key is stored. If the remote
7018 * identity is known, the long term keys are internally
7019 * mapped to the identity address. So allow static random
7020 * and public addresses here.
7021 */
Johan Hedbergba74b662014-02-19 14:57:45 +02007022 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7023 (key->bdaddr.b[5] & 0xc0) != 0xc0)
7024 ev.store_hint = 0x00;
7025 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007026 ev.store_hint = persistent;
Johan Hedbergba74b662014-02-19 14:57:45 +02007027
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007028 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007029 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
Johan Hedbergd7b25452014-05-23 13:19:53 +03007030 ev.key.type = mgmt_ltk_type(key);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007031 ev.key.enc_size = key->enc_size;
7032 ev.key.ediv = key->ediv;
Marcel Holtmannfe39c7b2014-02-27 16:00:28 -08007033 ev.key.rand = key->rand;
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007034
Johan Hedberg2ceba532014-06-16 19:25:16 +03007035 if (key->type == SMP_LTK)
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007036 ev.key.master = 1;
7037
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007038 memcpy(ev.key.val, key->val, sizeof(key->val));
7039
Marcel Holtmann083368f2013-10-15 14:26:29 -07007040 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
Vinicius Costa Gomes346af672012-02-02 21:08:02 -03007041}
7042
Johan Hedberg95fbac82014-02-19 15:18:31 +02007043void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk)
7044{
7045 struct mgmt_ev_new_irk ev;
7046
7047 memset(&ev, 0, sizeof(ev));
7048
Marcel Holtmannbab6d1e2014-02-19 11:51:54 -08007049 /* For identity resolving keys from devices that are already
7050 * using a public address or static random address, do not
7051 * ask for storing this key. The identity resolving key really
7052 * is only mandatory for devices using resovlable random
7053 * addresses.
7054 *
7055 * Storing all identity resolving keys has the downside that
7056 * they will be also loaded on next boot of they system. More
7057 * identity resolving keys, means more time during scanning is
7058 * needed to actually resolve these addresses.
7059 */
7060 if (bacmp(&irk->rpa, BDADDR_ANY))
7061 ev.store_hint = 0x01;
7062 else
7063 ev.store_hint = 0x00;
7064
Johan Hedberg95fbac82014-02-19 15:18:31 +02007065 bacpy(&ev.rpa, &irk->rpa);
7066 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
7067 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
7068 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
7069
7070 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
7071}
7072
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007073void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
7074 bool persistent)
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007075{
7076 struct mgmt_ev_new_csrk ev;
7077
7078 memset(&ev, 0, sizeof(ev));
7079
7080 /* Devices using resolvable or non-resolvable random addresses
7081 * without providing an indentity resolving key don't require
7082 * to store signature resolving keys. Their addresses will change
7083 * the next time around.
7084 *
7085 * Only when a remote device provides an identity address
7086 * make sure the signature resolving key is stored. So allow
7087 * static random and public addresses here.
7088 */
7089 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7090 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
7091 ev.store_hint = 0x00;
7092 else
Marcel Holtmann53ac6ab2014-03-09 23:38:42 -07007093 ev.store_hint = persistent;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007094
7095 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
7096 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
Johan Hedberg4cd39282015-02-27 10:11:13 +02007097 ev.key.type = csrk->type;
Marcel Holtmann7ee4ea32014-03-09 12:19:17 -07007098 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
7099
7100 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
7101}
7102
Andre Guedesffb5a8272014-07-01 18:10:11 -03007103void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedbergf4869e22014-07-02 17:37:32 +03007104 u8 bdaddr_type, u8 store_hint, u16 min_interval,
7105 u16 max_interval, u16 latency, u16 timeout)
Andre Guedesffb5a8272014-07-01 18:10:11 -03007106{
7107 struct mgmt_ev_new_conn_param ev;
7108
Johan Hedbergc103aea2014-07-02 17:37:34 +03007109 if (!hci_is_identity_address(bdaddr, bdaddr_type))
7110 return;
7111
Andre Guedesffb5a8272014-07-01 18:10:11 -03007112 memset(&ev, 0, sizeof(ev));
7113 bacpy(&ev.addr.bdaddr, bdaddr);
7114 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
Johan Hedbergf4869e22014-07-02 17:37:32 +03007115 ev.store_hint = store_hint;
Andre Guedesffb5a8272014-07-01 18:10:11 -03007116 ev.min_interval = cpu_to_le16(min_interval);
7117 ev.max_interval = cpu_to_le16(max_interval);
7118 ev.latency = cpu_to_le16(latency);
7119 ev.timeout = cpu_to_le16(timeout);
7120
7121 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
7122}
7123
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007124void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
7125 u32 flags, u8 *name, u8 name_len)
Johan Hedbergf7520542011-01-20 12:34:39 +02007126{
Johan Hedbergb644ba32012-01-17 21:48:47 +02007127 char buf[512];
7128 struct mgmt_ev_device_connected *ev = (void *) buf;
7129 u16 eir_len = 0;
Johan Hedbergf7520542011-01-20 12:34:39 +02007130
Alfonso Acosta48ec92f2014-10-07 08:44:10 +00007131 bacpy(&ev->addr.bdaddr, &conn->dst);
7132 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
Johan Hedbergf7520542011-01-20 12:34:39 +02007133
Johan Hedbergc95f0ba2012-02-23 22:54:38 +02007134 ev->flags = __cpu_to_le32(flags);
Johan Hedberg08c79b62012-02-23 22:31:51 +02007135
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007136 /* We must ensure that the EIR Data fields are ordered and
7137 * unique. Keep it simple for now and avoid the problem by not
7138 * adding any BR/EDR data to the LE adv.
7139 */
7140 if (conn->le_adv_data_len > 0) {
7141 memcpy(&ev->eir[eir_len],
7142 conn->le_adv_data, conn->le_adv_data_len);
7143 eir_len = conn->le_adv_data_len;
7144 } else {
7145 if (name_len > 0)
7146 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
7147 name, name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007148
Alfonso Acostaddbea5c2014-10-07 08:44:12 +00007149 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
Alfonso Acostafd45ada2014-10-07 08:44:11 +00007150 eir_len = eir_append_data(ev->eir, eir_len,
7151 EIR_CLASS_OF_DEV,
7152 conn->dev_class, 3);
7153 }
Johan Hedbergb644ba32012-01-17 21:48:47 +02007154
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02007155 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007156
Marcel Holtmannecd90ae2013-10-06 23:55:49 -07007157 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
7158 sizeof(*ev) + eir_len, NULL);
Johan Hedbergf7520542011-01-20 12:34:39 +02007159}
7160
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007161static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007162{
Johan Hedberg8962ee72011-01-20 12:40:27 +02007163 struct sock **sk = data;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007164
Johan Hedbergf5818c22014-12-05 13:36:02 +02007165 cmd->cmd_complete(cmd, 0);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007166
7167 *sk = cmd->sk;
7168 sock_hold(*sk);
7169
Johan Hedberga664b5b2011-02-19 12:06:02 -03007170 mgmt_pending_remove(cmd);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007171}
7172
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007173static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberga8a1d192011-11-10 15:54:38 +02007174{
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007175 struct hci_dev *hdev = data;
Johan Hedberg124f6e32012-02-09 13:50:12 +02007176 struct mgmt_cp_unpair_device *cp = cmd->param;
Johan Hedberga8a1d192011-11-10 15:54:38 +02007177
Johan Hedbergb1078ad2012-02-09 17:21:16 +02007178 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
7179
Johan Hedbergd8b7b1e2014-12-05 13:36:05 +02007180 cmd->cmd_complete(cmd, 0);
Johan Hedberga8a1d192011-11-10 15:54:38 +02007181 mgmt_pending_remove(cmd);
7182}
7183
Johan Hedberg84c61d92014-08-01 11:13:30 +03007184bool mgmt_powering_down(struct hci_dev *hdev)
7185{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007186 struct mgmt_pending_cmd *cmd;
Johan Hedberg84c61d92014-08-01 11:13:30 +03007187 struct mgmt_mode *cp;
7188
Johan Hedberg333ae952015-03-17 13:48:47 +02007189 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
Johan Hedberg84c61d92014-08-01 11:13:30 +03007190 if (!cmd)
7191 return false;
7192
7193 cp = cmd->param;
7194 if (!cp->val)
7195 return true;
7196
7197 return false;
7198}
7199
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007200void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007201 u8 link_type, u8 addr_type, u8 reason,
7202 bool mgmt_connected)
Johan Hedbergf7520542011-01-20 12:34:39 +02007203{
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007204 struct mgmt_ev_device_disconnected ev;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007205 struct sock *sk = NULL;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007206
Johan Hedberg84c61d92014-08-01 11:13:30 +03007207 /* The connection is still in hci_conn_hash so test for 1
7208 * instead of 0 to know if this is the last one.
7209 */
7210 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7211 cancel_delayed_work(&hdev->power_off);
7212 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedberg8b064a32014-02-24 14:52:22 +02007213 }
7214
Johan Hedberg12d4a3b2014-02-24 14:52:18 +02007215 if (!mgmt_connected)
7216 return;
7217
Andre Guedes57eb7762013-10-30 19:01:41 -03007218 if (link_type != ACL_LINK && link_type != LE_LINK)
7219 return;
7220
Johan Hedberg744cf192011-11-08 20:40:14 +02007221 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
Johan Hedbergf7520542011-01-20 12:34:39 +02007222
Mikel Astizf0d6a0e2012-08-09 09:52:30 +02007223 bacpy(&ev.addr.bdaddr, bdaddr);
7224 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7225 ev.reason = reason;
Johan Hedbergf7520542011-01-20 12:34:39 +02007226
Marcel Holtmann9b80ec52013-10-06 23:55:50 -07007227 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007228
7229 if (sk)
Szymon Jancd97dcb62012-03-16 16:02:56 +01007230 sock_put(sk);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007231
Johan Hedberg124f6e32012-02-09 13:50:12 +02007232 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007233 hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007234}
7235
Marcel Holtmann78929242013-10-06 23:55:47 -07007236void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
7237 u8 link_type, u8 addr_type, u8 status)
Johan Hedberg8962ee72011-01-20 12:40:27 +02007238{
Andre Guedes3655bba2013-10-30 19:01:40 -03007239 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
7240 struct mgmt_cp_disconnect *cp;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007241 struct mgmt_pending_cmd *cmd;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007242
Jefferson Delfes36a75f12012-09-18 13:36:54 -04007243 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
7244 hdev);
7245
Johan Hedberg333ae952015-03-17 13:48:47 +02007246 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
Johan Hedberg8962ee72011-01-20 12:40:27 +02007247 if (!cmd)
Marcel Holtmann78929242013-10-06 23:55:47 -07007248 return;
Johan Hedberg8962ee72011-01-20 12:40:27 +02007249
Andre Guedes3655bba2013-10-30 19:01:40 -03007250 cp = cmd->param;
7251
7252 if (bacmp(bdaddr, &cp->addr.bdaddr))
7253 return;
7254
7255 if (cp->addr.type != bdaddr_type)
7256 return;
7257
Johan Hedbergf5818c22014-12-05 13:36:02 +02007258 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007259 mgmt_pending_remove(cmd);
Johan Hedbergf7520542011-01-20 12:34:39 +02007260}
Johan Hedberg17d5c042011-01-22 06:09:08 +02007261
Marcel Holtmann445608d2013-10-06 23:55:48 -07007262void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7263 u8 addr_type, u8 status)
Johan Hedberg17d5c042011-01-22 06:09:08 +02007264{
7265 struct mgmt_ev_connect_failed ev;
Johan Hedbergc9910d02014-02-27 14:35:12 +02007266
Johan Hedberg84c61d92014-08-01 11:13:30 +03007267 /* The connection is still in hci_conn_hash so test for 1
7268 * instead of 0 to know if this is the last one.
7269 */
7270 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
7271 cancel_delayed_work(&hdev->power_off);
7272 queue_work(hdev->req_workqueue, &hdev->power_off.work);
Johan Hedbergc9910d02014-02-27 14:35:12 +02007273 }
Johan Hedberg17d5c042011-01-22 06:09:08 +02007274
Johan Hedberg4c659c32011-11-07 23:13:39 +02007275 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007276 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergca69b792011-11-11 18:10:00 +02007277 ev.status = mgmt_status(status);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007278
Marcel Holtmann445608d2013-10-06 23:55:48 -07007279 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg17d5c042011-01-22 06:09:08 +02007280}
Johan Hedberg980e1a52011-01-22 06:10:07 +02007281
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007282void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007283{
7284 struct mgmt_ev_pin_code_request ev;
7285
Johan Hedbergd8457692012-02-17 14:24:57 +02007286 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes591f47f2012-04-24 21:02:49 -03007287 ev.addr.type = BDADDR_BREDR;
Waldemar Rymarkiewicza770bb52011-04-28 12:07:59 +02007288 ev.secure = secure;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007289
Marcel Holtmannce0e4a02013-10-15 14:26:20 -07007290 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007291}
7292
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007293void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7294 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007295{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007296 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007297
Johan Hedberg333ae952015-03-17 13:48:47 +02007298 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007299 if (!cmd)
Marcel Holtmanne669cf82013-10-15 14:26:21 -07007300 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007301
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007302 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007303 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007304}
7305
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007306void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
7307 u8 status)
Johan Hedberg980e1a52011-01-22 06:10:07 +02007308{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007309 struct mgmt_pending_cmd *cmd;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007310
Johan Hedberg333ae952015-03-17 13:48:47 +02007311 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007312 if (!cmd)
Marcel Holtmann3eb38522013-10-15 14:26:22 -07007313 return;
Johan Hedberg980e1a52011-01-22 06:10:07 +02007314
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007315 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007316 mgmt_pending_remove(cmd);
Johan Hedberg980e1a52011-01-22 06:10:07 +02007317}
Johan Hedberga5c29682011-02-19 12:05:57 -03007318
Johan Hedberg744cf192011-11-08 20:40:14 +02007319int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Johan Hedberg39adbff2014-03-20 08:18:14 +02007320 u8 link_type, u8 addr_type, u32 value,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007321 u8 confirm_hint)
Johan Hedberga5c29682011-02-19 12:05:57 -03007322{
7323 struct mgmt_ev_user_confirm_request ev;
7324
Johan Hedberg744cf192011-11-08 20:40:14 +02007325 BT_DBG("%s", hdev->name);
Johan Hedberga5c29682011-02-19 12:05:57 -03007326
Johan Hedberg272d90d2012-02-09 15:26:12 +02007327 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007328 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberg55bc1a32011-04-28 11:28:56 -07007329 ev.confirm_hint = confirm_hint;
Johan Hedberg39adbff2014-03-20 08:18:14 +02007330 ev.value = cpu_to_le32(value);
Johan Hedberga5c29682011-02-19 12:05:57 -03007331
Johan Hedberg744cf192011-11-08 20:40:14 +02007332 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007333 NULL);
Johan Hedberga5c29682011-02-19 12:05:57 -03007334}
7335
Johan Hedberg272d90d2012-02-09 15:26:12 +02007336int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007337 u8 link_type, u8 addr_type)
Brian Gix604086b2011-11-23 08:28:33 -08007338{
7339 struct mgmt_ev_user_passkey_request ev;
7340
7341 BT_DBG("%s", hdev->name);
7342
Johan Hedberg272d90d2012-02-09 15:26:12 +02007343 bacpy(&ev.addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007344 ev.addr.type = link_to_bdaddr(link_type, addr_type);
Brian Gix604086b2011-11-23 08:28:33 -08007345
7346 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007347 NULL);
Brian Gix604086b2011-11-23 08:28:33 -08007348}
7349
Brian Gix0df4c182011-11-16 13:53:13 -08007350static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo Padovan8ce8e2b2012-05-17 00:36:20 -03007351 u8 link_type, u8 addr_type, u8 status,
7352 u8 opcode)
Johan Hedberga5c29682011-02-19 12:05:57 -03007353{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007354 struct mgmt_pending_cmd *cmd;
Johan Hedberga5c29682011-02-19 12:05:57 -03007355
Johan Hedberg333ae952015-03-17 13:48:47 +02007356 cmd = pending_find(opcode, hdev);
Johan Hedberga5c29682011-02-19 12:05:57 -03007357 if (!cmd)
7358 return -ENOENT;
7359
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007360 cmd->cmd_complete(cmd, mgmt_status(status));
Johan Hedberga664b5b2011-02-19 12:06:02 -03007361 mgmt_pending_remove(cmd);
Johan Hedberga5c29682011-02-19 12:05:57 -03007362
Johan Hedberg7776d1d2014-12-05 13:36:03 +02007363 return 0;
Johan Hedberga5c29682011-02-19 12:05:57 -03007364}
7365
Johan Hedberg744cf192011-11-08 20:40:14 +02007366int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007367 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007368{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007369 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007370 status, MGMT_OP_USER_CONFIRM_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007371}
7372
Johan Hedberg272d90d2012-02-09 15:26:12 +02007373int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007374 u8 link_type, u8 addr_type, u8 status)
Johan Hedberga5c29682011-02-19 12:05:57 -03007375{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007376 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007377 status,
7378 MGMT_OP_USER_CONFIRM_NEG_REPLY);
Johan Hedberga5c29682011-02-19 12:05:57 -03007379}
Johan Hedberg2a611692011-02-19 12:06:00 -03007380
Brian Gix604086b2011-11-23 08:28:33 -08007381int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007382 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007383{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007384 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007385 status, MGMT_OP_USER_PASSKEY_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007386}
7387
Johan Hedberg272d90d2012-02-09 15:26:12 +02007388int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007389 u8 link_type, u8 addr_type, u8 status)
Brian Gix604086b2011-11-23 08:28:33 -08007390{
Johan Hedberg272d90d2012-02-09 15:26:12 +02007391 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
Gustavo Padovan8fc9ced2012-05-23 04:04:21 -03007392 status,
7393 MGMT_OP_USER_PASSKEY_NEG_REPLY);
Brian Gix604086b2011-11-23 08:28:33 -08007394}
7395
Johan Hedberg92a25252012-09-06 18:39:26 +03007396int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
7397 u8 link_type, u8 addr_type, u32 passkey,
7398 u8 entered)
7399{
7400 struct mgmt_ev_passkey_notify ev;
7401
7402 BT_DBG("%s", hdev->name);
7403
7404 bacpy(&ev.addr.bdaddr, bdaddr);
7405 ev.addr.type = link_to_bdaddr(link_type, addr_type);
7406 ev.passkey = __cpu_to_le32(passkey);
7407 ev.entered = entered;
7408
7409 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
7410}
7411
Johan Hedberge1e930f2014-09-08 17:09:49 -07007412void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
Johan Hedberg2a611692011-02-19 12:06:00 -03007413{
7414 struct mgmt_ev_auth_failed ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007415 struct mgmt_pending_cmd *cmd;
Johan Hedberge1e930f2014-09-08 17:09:49 -07007416 u8 status = mgmt_status(hci_status);
Johan Hedberg2a611692011-02-19 12:06:00 -03007417
Johan Hedberge1e930f2014-09-08 17:09:49 -07007418 bacpy(&ev.addr.bdaddr, &conn->dst);
7419 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
7420 ev.status = status;
Johan Hedberg2a611692011-02-19 12:06:00 -03007421
Johan Hedberge1e930f2014-09-08 17:09:49 -07007422 cmd = find_pairing(conn);
7423
7424 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
7425 cmd ? cmd->sk : NULL);
7426
Johan Hedberga511b352014-12-11 21:45:45 +02007427 if (cmd) {
7428 cmd->cmd_complete(cmd, status);
7429 mgmt_pending_remove(cmd);
7430 }
Johan Hedberg2a611692011-02-19 12:06:00 -03007431}
Johan Hedbergb312b1612011-03-16 14:29:37 +02007432
Marcel Holtmann464996a2013-10-15 14:26:24 -07007433void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007434{
7435 struct cmd_lookup match = { NULL, hdev };
Marcel Holtmann464996a2013-10-15 14:26:24 -07007436 bool changed;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007437
7438 if (status) {
7439 u8 mgmt_err = mgmt_status(status);
7440 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007441 cmd_status_rsp, &mgmt_err);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007442 return;
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007443 }
7444
Marcel Holtmann464996a2013-10-15 14:26:24 -07007445 if (test_bit(HCI_AUTH, &hdev->flags))
Marcel Holtmann238be782015-03-13 02:11:06 -07007446 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
Marcel Holtmann464996a2013-10-15 14:26:24 -07007447 else
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007448 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
Johan Hedberg47990ea2012-02-22 11:58:37 +02007449
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007450 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007451 &match);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007452
Johan Hedberg47990ea2012-02-22 11:58:37 +02007453 if (changed)
Marcel Holtmann464996a2013-10-15 14:26:24 -07007454 new_settings(hdev, match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007455
7456 if (match.sk)
7457 sock_put(match.sk);
Johan Hedberg33ef95e2012-02-16 23:56:27 +02007458}
7459
Johan Hedberg890ea892013-03-15 17:06:52 -05007460static void clear_eir(struct hci_request *req)
Johan Hedbergcacaf522012-02-21 00:52:42 +02007461{
Johan Hedberg890ea892013-03-15 17:06:52 -05007462 struct hci_dev *hdev = req->hdev;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007463 struct hci_cp_write_eir cp;
7464
Johan Hedberg976eb202012-10-24 21:12:01 +03007465 if (!lmp_ext_inq_capable(hdev))
Johan Hedberg890ea892013-03-15 17:06:52 -05007466 return;
Johan Hedbergcacaf522012-02-21 00:52:42 +02007467
Johan Hedbergc80da272012-02-22 15:38:48 +02007468 memset(hdev->eir, 0, sizeof(hdev->eir));
7469
Johan Hedbergcacaf522012-02-21 00:52:42 +02007470 memset(&cp, 0, sizeof(cp));
7471
Johan Hedberg890ea892013-03-15 17:06:52 -05007472 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
Johan Hedbergcacaf522012-02-21 00:52:42 +02007473}
7474
Marcel Holtmann3e248562013-10-15 14:26:25 -07007475void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007476{
7477 struct cmd_lookup match = { NULL, hdev };
Johan Hedberg890ea892013-03-15 17:06:52 -05007478 struct hci_request req;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007479 bool changed = false;
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007480
7481 if (status) {
7482 u8 mgmt_err = mgmt_status(status);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007483
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007484 if (enable && hci_dev_test_and_clear_flag(hdev,
7485 HCI_SSP_ENABLED)) {
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007486 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007487 new_settings(hdev, NULL);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007488 }
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007489
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007490 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
7491 &mgmt_err);
Marcel Holtmann3e248562013-10-15 14:26:25 -07007492 return;
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007493 }
7494
7495 if (enable) {
Marcel Holtmann238be782015-03-13 02:11:06 -07007496 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007497 } else {
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007498 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007499 if (!changed)
Marcel Holtmanna69d8922015-03-13 02:11:05 -07007500 changed = hci_dev_test_and_clear_flag(hdev,
7501 HCI_HS_ENABLED);
Marcel Holtmann9ecb3e22013-10-10 03:08:11 -07007502 else
Marcel Holtmanna358dc12015-03-13 02:11:02 -07007503 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007504 }
7505
7506 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
7507
Johan Hedbergc0ecddc2012-02-22 12:38:31 +02007508 if (changed)
Marcel Holtmann3e248562013-10-15 14:26:25 -07007509 new_settings(hdev, match.sk);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007510
Johan Hedberg5fc6ebb2012-02-22 15:10:59 +02007511 if (match.sk)
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007512 sock_put(match.sk);
7513
Johan Hedberg890ea892013-03-15 17:06:52 -05007514 hci_req_init(&req, hdev);
7515
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007516 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
7517 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
Johan Hedberg37699722014-06-24 14:00:27 +03007518 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
7519 sizeof(enable), &enable);
Johan Hedberg890ea892013-03-15 17:06:52 -05007520 update_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007521 } else {
Johan Hedberg890ea892013-03-15 17:06:52 -05007522 clear_eir(&req);
Johan Hedberg37699722014-06-24 14:00:27 +03007523 }
Johan Hedberg890ea892013-03-15 17:06:52 -05007524
7525 hci_req_run(&req, NULL);
Johan Hedberged2c4ee2012-02-17 00:56:28 +02007526}
7527
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007528static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
Johan Hedberg90e70452012-02-23 23:09:40 +02007529{
7530 struct cmd_lookup *match = data;
7531
Johan Hedberg90e70452012-02-23 23:09:40 +02007532 if (match->sk == NULL) {
7533 match->sk = cmd->sk;
7534 sock_hold(match->sk);
7535 }
Johan Hedberg90e70452012-02-23 23:09:40 +02007536}
7537
Marcel Holtmann4e1b0242013-10-15 14:26:26 -07007538void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
7539 u8 status)
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007540{
Johan Hedberg90e70452012-02-23 23:09:40 +02007541 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007542
Johan Hedberg92da6092013-03-15 17:06:55 -05007543 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
7544 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
7545 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
Johan Hedberg90e70452012-02-23 23:09:40 +02007546
7547 if (!status)
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007548 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7549 dev_class, 3, NULL);
Johan Hedberg90e70452012-02-23 23:09:40 +02007550
7551 if (match.sk)
7552 sock_put(match.sk);
Marcel Holtmann7f9a9032012-02-22 18:38:01 +01007553}
7554
Marcel Holtmann7667da32013-10-15 14:26:27 -07007555void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
Johan Hedbergb312b1612011-03-16 14:29:37 +02007556{
Johan Hedbergb312b1612011-03-16 14:29:37 +02007557 struct mgmt_cp_set_local_name ev;
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007558 struct mgmt_pending_cmd *cmd;
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007559
Johan Hedberg13928972013-03-15 17:07:00 -05007560 if (status)
Marcel Holtmann7667da32013-10-15 14:26:27 -07007561 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007562
7563 memset(&ev, 0, sizeof(ev));
7564 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
Johan Hedberg28cc7bd2012-02-22 21:06:55 +02007565 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007566
Johan Hedberg333ae952015-03-17 13:48:47 +02007567 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
Johan Hedberg13928972013-03-15 17:07:00 -05007568 if (!cmd) {
7569 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
Johan Hedbergb312b1612011-03-16 14:29:37 +02007570
Johan Hedberg13928972013-03-15 17:07:00 -05007571 /* If this is a HCI command related to powering on the
7572 * HCI dev don't send any mgmt signals.
7573 */
Johan Hedberg333ae952015-03-17 13:48:47 +02007574 if (pending_find(MGMT_OP_SET_POWERED, hdev))
Marcel Holtmann7667da32013-10-15 14:26:27 -07007575 return;
Johan Hedbergb312b1612011-03-16 14:29:37 +02007576 }
7577
Marcel Holtmannf6b77122015-03-14 19:28:05 -07007578 mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
7579 cmd ? cmd->sk : NULL);
Johan Hedbergb312b1612011-03-16 14:29:37 +02007580}
Szymon Jancc35938b2011-03-22 13:12:21 +01007581
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007582void mgmt_read_local_oob_data_complete(struct hci_dev *hdev, u8 *hash192,
Johan Hedberg38da1702014-11-17 20:52:20 +02007583 u8 *rand192, u8 *hash256, u8 *rand256,
7584 u8 status)
Szymon Jancc35938b2011-03-22 13:12:21 +01007585{
Johan Hedberg3b0602c2015-03-06 21:08:55 +02007586 struct mgmt_pending_cmd *cmd;
Szymon Jancc35938b2011-03-22 13:12:21 +01007587
Johan Hedberg744cf192011-11-08 20:40:14 +02007588 BT_DBG("%s status %u", hdev->name, status);
Szymon Jancc35938b2011-03-22 13:12:21 +01007589
Johan Hedberg333ae952015-03-17 13:48:47 +02007590 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
Szymon Jancc35938b2011-03-22 13:12:21 +01007591 if (!cmd)
Marcel Holtmann3edaf092013-10-15 14:26:28 -07007592 return;
Szymon Jancc35938b2011-03-22 13:12:21 +01007593
7594 if (status) {
Johan Hedberga69e8372015-03-06 21:08:53 +02007595 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
7596 mgmt_status(status));
Szymon Jancc35938b2011-03-22 13:12:21 +01007597 } else {
Johan Hedberg66f096f2015-02-02 13:23:42 +02007598 struct mgmt_rp_read_local_oob_data rp;
7599 size_t rp_size = sizeof(rp);
7600
7601 memcpy(rp.hash192, hash192, sizeof(rp.hash192));
7602 memcpy(rp.rand192, rand192, sizeof(rp.rand192));
7603
Johan Hedberg710f11c2014-05-26 11:21:22 +03007604 if (bredr_sc_enabled(hdev) && hash256 && rand256) {
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007605 memcpy(rp.hash256, hash256, sizeof(rp.hash256));
Johan Hedberg38da1702014-11-17 20:52:20 +02007606 memcpy(rp.rand256, rand256, sizeof(rp.rand256));
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007607 } else {
Johan Hedberg66f096f2015-02-02 13:23:42 +02007608 rp_size -= sizeof(rp.hash256) + sizeof(rp.rand256);
Marcel Holtmann4d2d2792014-01-10 02:07:26 -08007609 }
Johan Hedberg66f096f2015-02-02 13:23:42 +02007610
Johan Hedberg2a1afb52015-03-06 21:08:54 +02007611 mgmt_cmd_complete(cmd->sk, hdev->id,
7612 MGMT_OP_READ_LOCAL_OOB_DATA, 0,
7613 &rp, rp_size);
Szymon Jancc35938b2011-03-22 13:12:21 +01007614 }
7615
7616 mgmt_pending_remove(cmd);
Szymon Jancc35938b2011-03-22 13:12:21 +01007617}
Johan Hedberge17acd42011-03-30 23:57:16 +03007618
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007619static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
7620{
7621 int i;
7622
7623 for (i = 0; i < uuid_count; i++) {
7624 if (!memcmp(uuid, uuids[i], 16))
7625 return true;
7626 }
7627
7628 return false;
7629}
7630
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007631static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
7632{
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007633 u16 parsed = 0;
7634
7635 while (parsed < eir_len) {
7636 u8 field_len = eir[0];
7637 u8 uuid[16];
7638 int i;
7639
7640 if (field_len == 0)
7641 break;
7642
7643 if (eir_len - parsed < field_len + 1)
7644 break;
7645
7646 switch (eir[1]) {
7647 case EIR_UUID16_ALL:
7648 case EIR_UUID16_SOME:
7649 for (i = 0; i + 3 <= field_len; i += 2) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007650 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007651 uuid[13] = eir[i + 3];
7652 uuid[12] = eir[i + 2];
7653 if (has_uuid(uuid, uuid_count, uuids))
7654 return true;
7655 }
7656 break;
7657 case EIR_UUID32_ALL:
7658 case EIR_UUID32_SOME:
7659 for (i = 0; i + 5 <= field_len; i += 4) {
Johan Hedberg189f6ad2014-12-05 13:40:01 +02007660 memcpy(uuid, bluetooth_base_uuid, 16);
Jakub Pawlowski799ce932014-12-05 10:55:58 +01007661 uuid[15] = eir[i + 5];
7662 uuid[14] = eir[i + 4];
7663 uuid[13] = eir[i + 3];
7664 uuid[12] = eir[i + 2];
7665 if (has_uuid(uuid, uuid_count, uuids))
7666 return true;
7667 }
7668 break;
7669 case EIR_UUID128_ALL:
7670 case EIR_UUID128_SOME:
7671 for (i = 0; i + 17 <= field_len; i += 16) {
7672 memcpy(uuid, eir + i + 2, 16);
7673 if (has_uuid(uuid, uuid_count, uuids))
7674 return true;
7675 }
7676 break;
7677 }
7678
7679 parsed += field_len + 1;
7680 eir += field_len + 1;
7681 }
7682
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007683 return false;
7684}
7685
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007686static void restart_le_scan(struct hci_dev *hdev)
7687{
7688 /* If controller is not scanning we are done. */
Marcel Holtmannd7a5a112015-03-13 02:11:00 -07007689 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007690 return;
7691
7692 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
7693 hdev->discovery.scan_start +
7694 hdev->discovery.scan_duration))
7695 return;
7696
7697 queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,
7698 DISCOV_LE_RESTART_DELAY);
7699}
7700
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007701static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
7702 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
7703{
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007704 /* If a RSSI threshold has been specified, and
7705 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
7706 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
7707 * is set, let it through for further processing, as we might need to
7708 * restart the scan.
7709 *
7710 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
7711 * the results are also dropped.
7712 */
7713 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
7714 (rssi == HCI_RSSI_INVALID ||
7715 (rssi < hdev->discovery.rssi &&
7716 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
7717 return false;
7718
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007719 if (hdev->discovery.uuid_count != 0) {
7720 /* If a list of UUIDs is provided in filter, results with no
7721 * matching UUID should be dropped.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007722 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007723 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
7724 hdev->discovery.uuids) &&
7725 !eir_has_uuids(scan_rsp, scan_rsp_len,
7726 hdev->discovery.uuid_count,
7727 hdev->discovery.uuids))
7728 return false;
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007729 }
7730
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007731 /* If duplicate filtering does not report RSSI changes, then restart
7732 * scanning to ensure updated result with updated RSSI values.
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007733 */
Jakub Pawlowski2976cde2015-03-04 16:24:25 -08007734 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
7735 restart_le_scan(hdev);
7736
7737 /* Validate RSSI value against the RSSI threshold once more. */
7738 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
7739 rssi < hdev->discovery.rssi)
7740 return false;
7741 }
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007742
7743 return true;
7744}
7745
Marcel Holtmann901801b2013-10-06 23:55:51 -07007746void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
Marcel Holtmannaf589252014-07-01 14:11:20 +02007747 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
7748 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
Johan Hedberge17acd42011-03-30 23:57:16 +03007749{
Johan Hedberge319d2e2012-01-15 19:51:59 +02007750 char buf[512];
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007751 struct mgmt_ev_device_found *ev = (void *)buf;
Johan Hedberg1dc06092012-01-15 21:01:23 +02007752 size_t ev_size;
Johan Hedberge17acd42011-03-30 23:57:16 +03007753
Johan Hedberg75ce2082014-07-02 22:42:01 +03007754 /* Don't send events for a non-kernel initiated discovery. With
7755 * LE one exception is if we have pend_le_reports > 0 in which
7756 * case we're doing passive scanning and want these events.
7757 */
7758 if (!hci_discovery_active(hdev)) {
7759 if (link_type == ACL_LINK)
7760 return;
Johan Hedberg66f84552014-07-04 12:37:18 +03007761 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
Johan Hedberg75ce2082014-07-02 22:42:01 +03007762 return;
7763 }
Andre Guedes12602d02013-04-30 15:29:40 -03007764
Jakub Pawlowski82f8b652015-03-04 16:24:26 -08007765 if (hdev->discovery.result_filtering) {
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007766 /* We are using service discovery */
7767 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
7768 scan_rsp_len))
7769 return;
7770 }
Marcel Holtmannbda157a2014-12-05 10:55:56 +01007771
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007772 /* Make sure that the buffer is big enough. The 5 extra bytes
7773 * are for the potential CoD field.
7774 */
7775 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
Marcel Holtmann901801b2013-10-06 23:55:51 -07007776 return;
Andre Guedes7d262f82012-01-10 18:20:49 -03007777
Johan Hedberg1dc06092012-01-15 21:01:23 +02007778 memset(buf, 0, sizeof(buf));
7779
Marcel Holtmannda25cf62014-12-05 13:03:35 +01007780 /* In case of device discovery with BR/EDR devices (pre 1.2), the
7781 * RSSI value was reported as 0 when not available. This behavior
7782 * is kept when using device discovery. This is required for full
7783 * backwards compatibility with the API.
7784 *
7785 * However when using service discovery, the value 127 will be
7786 * returned when the RSSI is not available.
7787 */
Szymon Janc91200e92015-01-22 16:57:05 +01007788 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
7789 link_type == ACL_LINK)
Marcel Holtmannefb25132014-12-05 13:03:34 +01007790 rssi = 0;
7791
Johan Hedberg841c5642014-07-07 12:45:54 +03007792 bacpy(&ev->addr.bdaddr, bdaddr);
7793 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedberge319d2e2012-01-15 19:51:59 +02007794 ev->rssi = rssi;
Marcel Holtmannaf589252014-07-01 14:11:20 +02007795 ev->flags = cpu_to_le32(flags);
Johan Hedberge17acd42011-03-30 23:57:16 +03007796
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007797 if (eir_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007798 /* Copy EIR or advertising data into event */
Johan Hedberge319d2e2012-01-15 19:51:59 +02007799 memcpy(ev->eir, eir, eir_len);
Johan Hedberge17acd42011-03-30 23:57:16 +03007800
Johan Hedberg1dc06092012-01-15 21:01:23 +02007801 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
7802 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007803 dev_class, 3);
Johan Hedberg1dc06092012-01-15 21:01:23 +02007804
Jakub Pawlowski48f86b72015-03-04 16:24:24 -08007805 if (scan_rsp_len > 0)
Marcel Holtmannb487b9c2014-12-05 10:55:57 +01007806 /* Append scan response data to event */
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007807 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
Jakub Pawlowski4b0e0ce2015-02-01 23:07:55 -08007808
Johan Hedberg5d2e9fa2014-03-25 10:30:47 +02007809 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
7810 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
Andre Guedesf8523592011-09-09 18:56:26 -03007811
Marcel Holtmann901801b2013-10-06 23:55:51 -07007812 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
Johan Hedberge17acd42011-03-30 23:57:16 +03007813}
Johan Hedberga88a9652011-03-30 13:18:12 +03007814
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07007815void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
7816 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
Johan Hedberga88a9652011-03-30 13:18:12 +03007817{
Johan Hedbergb644ba32012-01-17 21:48:47 +02007818 struct mgmt_ev_device_found *ev;
7819 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
7820 u16 eir_len;
Johan Hedberga88a9652011-03-30 13:18:12 +03007821
Johan Hedbergb644ba32012-01-17 21:48:47 +02007822 ev = (struct mgmt_ev_device_found *) buf;
Johan Hedberga88a9652011-03-30 13:18:12 +03007823
Johan Hedbergb644ba32012-01-17 21:48:47 +02007824 memset(buf, 0, sizeof(buf));
Johan Hedberga88a9652011-03-30 13:18:12 +03007825
Johan Hedbergb644ba32012-01-17 21:48:47 +02007826 bacpy(&ev->addr.bdaddr, bdaddr);
Andre Guedes57c14772012-04-24 21:02:50 -03007827 ev->addr.type = link_to_bdaddr(link_type, addr_type);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007828 ev->rssi = rssi;
7829
7830 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
Gustavo F. Padovan04124682012-03-08 01:25:00 -03007831 name_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007832
Marcel Holtmanneb55ef02012-03-14 18:08:46 +02007833 ev->eir_len = cpu_to_le16(eir_len);
Johan Hedbergb644ba32012-01-17 21:48:47 +02007834
Marcel Holtmann9cf12ae2013-10-06 23:55:52 -07007835 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
Johan Hedberga88a9652011-03-30 13:18:12 +03007836}
Johan Hedberg314b2382011-04-27 10:29:57 -04007837
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07007838void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
Johan Hedberg314b2382011-04-27 10:29:57 -04007839{
Johan Hedbergf963e8e2012-02-20 23:30:44 +02007840 struct mgmt_ev_discovering ev;
Johan Hedberg164a6e72011-11-01 17:06:44 +02007841
Andre Guedes343fb142011-11-22 17:14:19 -03007842 BT_DBG("%s discovering %u", hdev->name, discovering);
7843
Johan Hedbergf963e8e2012-02-20 23:30:44 +02007844 memset(&ev, 0, sizeof(ev));
7845 ev.type = hdev->discovery.type;
7846 ev.discovering = discovering;
7847
Marcel Holtmann2f1e0632013-10-06 23:55:53 -07007848 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
Johan Hedberg314b2382011-04-27 10:29:57 -04007849}
Antti Julku5e762442011-08-25 16:48:02 +03007850
Marcel Holtmann1904a852015-01-11 13:50:44 -08007851static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
Marcel Holtmann5976e602013-10-06 04:08:14 -07007852{
7853 BT_DBG("%s status %u", hdev->name, status);
Marcel Holtmann5976e602013-10-06 04:08:14 -07007854}
7855
7856void mgmt_reenable_advertising(struct hci_dev *hdev)
7857{
7858 struct hci_request req;
7859
Arman Uguray24b4f382015-03-23 15:57:12 -07007860 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
7861 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
Marcel Holtmann5976e602013-10-06 04:08:14 -07007862 return;
7863
7864 hci_req_init(&req, hdev);
7865 enable_advertising(&req);
Johan Hedberg0ec5ae82014-07-08 15:07:50 +03007866 hci_req_run(&req, adv_enable_complete);
Marcel Holtmann5976e602013-10-06 04:08:14 -07007867}
Johan Hedberg6d785aa32015-03-06 21:08:51 +02007868
7869static struct hci_mgmt_chan chan = {
7870 .channel = HCI_CHANNEL_CONTROL,
7871 .handler_count = ARRAY_SIZE(mgmt_handlers),
7872 .handlers = mgmt_handlers,
Johan Hedberg88b94ce2015-03-17 13:48:49 +02007873 .hdev_init = mgmt_init_hdev,
Johan Hedberg6d785aa32015-03-06 21:08:51 +02007874};
7875
7876int mgmt_init(void)
7877{
7878 return hci_mgmt_chan_register(&chan);
7879}
7880
7881void mgmt_exit(void)
7882{
7883 hci_mgmt_chan_unregister(&chan);
7884}