Bluetooth: Add hci_conn_hash_lookup_le() helper function
[deliverable/linux.git] / net / bluetooth / mgmt.c
CommitLineData
0381101f
JH
1/*
2 BlueZ - Bluetooth protocol stack for Linux
ea585ab5 3
0381101f 4 Copyright (C) 2010 Nokia Corporation
ea585ab5 5 Copyright (C) 2011-2012 Intel Corporation
0381101f
JH
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
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
3a9a231d 27#include <linux/module.h>
0381101f
JH
28#include <asm/unaligned.h>
29
30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
7129069e 32#include <net/bluetooth/hci_sock.h>
4bc58f51 33#include <net/bluetooth/l2cap.h>
0381101f 34#include <net/bluetooth/mgmt.h>
ac4b7236 35
0857dd3b 36#include "hci_request.h"
ac4b7236 37#include "smp.h"
a380b6cf 38#include "mgmt_util.h"
0381101f 39
2da9c55c 40#define MGMT_VERSION 1
e58627d1 41#define MGMT_REVISION 10
02d98129 42
e70bb2e8
JH
43static 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,
b2939475 50 MGMT_OP_SET_BONDABLE,
e70bb2e8
JH
51 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,
cdbaccca 81 MGMT_OP_SET_DEVICE_ID,
4375f103 82 MGMT_OP_SET_ADVERTISING,
0663ca2a 83 MGMT_OP_SET_BREDR,
d13eafce 84 MGMT_OP_SET_STATIC_ADDRESS,
7f72134e 85 MGMT_OP_SET_SCAN_PARAMS,
e98d2ce2 86 MGMT_OP_SET_SECURE_CONN,
4e39ac81 87 MGMT_OP_SET_DEBUG_KEYS,
62b04cd1 88 MGMT_OP_SET_PRIVACY,
41edf160 89 MGMT_OP_LOAD_IRKS,
dd983808 90 MGMT_OP_GET_CONN_INFO,
95868426 91 MGMT_OP_GET_CLOCK_INFO,
2faade53
MH
92 MGMT_OP_ADD_DEVICE,
93 MGMT_OP_REMOVE_DEVICE,
a26f3dcf 94 MGMT_OP_LOAD_CONN_PARAM,
73d1df2a 95 MGMT_OP_READ_UNCONF_INDEX_LIST,
9fc3bfb6 96 MGMT_OP_READ_CONFIG_INFO,
dbece37a 97 MGMT_OP_SET_EXTERNAL_CONFIG,
9713c17b 98 MGMT_OP_SET_PUBLIC_ADDRESS,
66ea9427 99 MGMT_OP_START_SERVICE_DISCOVERY,
4f0f155c 100 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
96f1474a 101 MGMT_OP_READ_EXT_INDEX_LIST,
d3d5305b 102 MGMT_OP_READ_ADV_FEATURES,
24b4f38f 103 MGMT_OP_ADD_ADVERTISING,
da929335 104 MGMT_OP_REMOVE_ADVERTISING,
e70bb2e8
JH
105};
106
107static const u16 mgmt_events[] = {
108 MGMT_EV_CONTROLLER_ERROR,
109 MGMT_EV_INDEX_ADDED,
110 MGMT_EV_INDEX_REMOVED,
111 MGMT_EV_NEW_SETTINGS,
112 MGMT_EV_CLASS_OF_DEV_CHANGED,
113 MGMT_EV_LOCAL_NAME_CHANGED,
114 MGMT_EV_NEW_LINK_KEY,
115 MGMT_EV_NEW_LONG_TERM_KEY,
116 MGMT_EV_DEVICE_CONNECTED,
117 MGMT_EV_DEVICE_DISCONNECTED,
118 MGMT_EV_CONNECT_FAILED,
119 MGMT_EV_PIN_CODE_REQUEST,
120 MGMT_EV_USER_CONFIRM_REQUEST,
121 MGMT_EV_USER_PASSKEY_REQUEST,
122 MGMT_EV_AUTH_FAILED,
123 MGMT_EV_DEVICE_FOUND,
124 MGMT_EV_DISCOVERING,
125 MGMT_EV_DEVICE_BLOCKED,
126 MGMT_EV_DEVICE_UNBLOCKED,
127 MGMT_EV_DEVICE_UNPAIRED,
92a25256 128 MGMT_EV_PASSKEY_NOTIFY,
1b60ef21 129 MGMT_EV_NEW_IRK,
7ee4ea36 130 MGMT_EV_NEW_CSRK,
8afef092
MH
131 MGMT_EV_DEVICE_ADDED,
132 MGMT_EV_DEVICE_REMOVED,
ffb5a827 133 MGMT_EV_NEW_CONN_PARAM,
0602a8ad 134 MGMT_EV_UNCONF_INDEX_ADDED,
edd3896b 135 MGMT_EV_UNCONF_INDEX_REMOVED,
f4537c04 136 MGMT_EV_NEW_CONFIG_OPTIONS,
ced85549
MH
137 MGMT_EV_EXT_INDEX_ADDED,
138 MGMT_EV_EXT_INDEX_REMOVED,
72000df2 139 MGMT_EV_LOCAL_OOB_DATA_UPDATED,
24b4f38f
AU
140 MGMT_EV_ADVERTISING_ADDED,
141 MGMT_EV_ADVERTISING_REMOVED,
e70bb2e8
JH
142};
143
99c679ac
MH
144static const u16 mgmt_untrusted_commands[] = {
145 MGMT_OP_READ_INDEX_LIST,
146 MGMT_OP_READ_INFO,
147 MGMT_OP_READ_UNCONF_INDEX_LIST,
148 MGMT_OP_READ_CONFIG_INFO,
149 MGMT_OP_READ_EXT_INDEX_LIST,
150};
151
152static const u16 mgmt_untrusted_events[] = {
153 MGMT_EV_INDEX_ADDED,
154 MGMT_EV_INDEX_REMOVED,
155 MGMT_EV_NEW_SETTINGS,
156 MGMT_EV_CLASS_OF_DEV_CHANGED,
157 MGMT_EV_LOCAL_NAME_CHANGED,
158 MGMT_EV_UNCONF_INDEX_ADDED,
159 MGMT_EV_UNCONF_INDEX_REMOVED,
160 MGMT_EV_NEW_CONFIG_OPTIONS,
161 MGMT_EV_EXT_INDEX_ADDED,
162 MGMT_EV_EXT_INDEX_REMOVED,
163};
164
17b02e62 165#define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
7d78525d 166
d25b78e2
JH
167#define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
168 "\x00\x00\x00\x00\x00\x00\x00\x00"
169
ca69b795
JH
170/* HCI to MGMT error code conversion table */
171static u8 mgmt_status_table[] = {
172 MGMT_STATUS_SUCCESS,
173 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
174 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
175 MGMT_STATUS_FAILED, /* Hardware Failure */
176 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
177 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
eadd663a 178 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
ca69b795
JH
179 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
180 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
181 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
182 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
183 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
184 MGMT_STATUS_BUSY, /* Command Disallowed */
185 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
186 MGMT_STATUS_REJECTED, /* Rejected Security */
187 MGMT_STATUS_REJECTED, /* Rejected Personal */
188 MGMT_STATUS_TIMEOUT, /* Host Timeout */
189 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
190 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
191 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
192 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
193 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
194 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
195 MGMT_STATUS_BUSY, /* Repeated Attempts */
196 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
197 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
198 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
199 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
200 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
201 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
202 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
203 MGMT_STATUS_FAILED, /* Unspecified Error */
204 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
205 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
206 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
207 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
208 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
209 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
210 MGMT_STATUS_FAILED, /* Unit Link Key Used */
211 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
212 MGMT_STATUS_TIMEOUT, /* Instant Passed */
213 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
214 MGMT_STATUS_FAILED, /* Transaction Collision */
215 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
216 MGMT_STATUS_REJECTED, /* QoS Rejected */
217 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
218 MGMT_STATUS_REJECTED, /* Insufficient Security */
219 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
220 MGMT_STATUS_BUSY, /* Role Switch Pending */
221 MGMT_STATUS_FAILED, /* Slot Violation */
222 MGMT_STATUS_FAILED, /* Role Switch Failed */
223 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
224 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
225 MGMT_STATUS_BUSY, /* Host Busy Pairing */
226 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
227 MGMT_STATUS_BUSY, /* Controller Busy */
228 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
229 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
230 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
231 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
232 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
233};
234
235static u8 mgmt_status(u8 hci_status)
236{
237 if (hci_status < ARRAY_SIZE(mgmt_status_table))
238 return mgmt_status_table[hci_status];
239
240 return MGMT_STATUS_FAILED;
241}
242
c08b1a1d
MH
243static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
244 u16 len, int flag)
f9207338 245{
c08b1a1d
MH
246 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
247 flag, NULL);
f9207338
MH
248}
249
72000df2
MH
250static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
251 u16 len, int flag, struct sock *skip_sk)
252{
253 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
254 flag, skip_sk);
255}
256
f6b7712e
MH
257static int mgmt_generic_event(u16 event, struct hci_dev *hdev, void *data,
258 u16 len, struct sock *skip_sk)
259{
260 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
261 HCI_MGMT_GENERIC_EVENTS, skip_sk);
262}
263
7a00ff44
JH
264static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
265 struct sock *skip_sk)
266{
267 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
c08b1a1d 268 HCI_SOCK_TRUSTED, skip_sk);
7a00ff44
JH
269}
270
85813a7e
JH
271static u8 le_addr_type(u8 mgmt_addr_type)
272{
273 if (mgmt_addr_type == BDADDR_LE_PUBLIC)
274 return ADDR_LE_DEV_PUBLIC;
275 else
276 return ADDR_LE_DEV_RANDOM;
277}
278
04124681
GP
279static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
280 u16 data_len)
a38528f1
JH
281{
282 struct mgmt_rp_read_version rp;
283
284 BT_DBG("sock %p", sk);
285
286 rp.version = MGMT_VERSION;
dcf4adbf 287 rp.revision = cpu_to_le16(MGMT_REVISION);
a38528f1 288
2a1afb5a
JH
289 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
290 &rp, sizeof(rp));
a38528f1
JH
291}
292
04124681
GP
293static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
294 u16 data_len)
e70bb2e8
JH
295{
296 struct mgmt_rp_read_commands *rp;
99c679ac 297 u16 num_commands, num_events;
e70bb2e8
JH
298 size_t rp_size;
299 int i, err;
300
301 BT_DBG("sock %p", sk);
302
99c679ac
MH
303 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
304 num_commands = ARRAY_SIZE(mgmt_commands);
305 num_events = ARRAY_SIZE(mgmt_events);
306 } else {
307 num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
308 num_events = ARRAY_SIZE(mgmt_untrusted_events);
309 }
310
e70bb2e8
JH
311 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
312
313 rp = kmalloc(rp_size, GFP_KERNEL);
314 if (!rp)
315 return -ENOMEM;
316
dcf4adbf
JP
317 rp->num_commands = cpu_to_le16(num_commands);
318 rp->num_events = cpu_to_le16(num_events);
e70bb2e8 319
99c679ac
MH
320 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
321 __le16 *opcode = rp->opcodes;
322
323 for (i = 0; i < num_commands; i++, opcode++)
324 put_unaligned_le16(mgmt_commands[i], opcode);
e70bb2e8 325
99c679ac
MH
326 for (i = 0; i < num_events; i++, opcode++)
327 put_unaligned_le16(mgmt_events[i], opcode);
328 } else {
329 __le16 *opcode = rp->opcodes;
330
331 for (i = 0; i < num_commands; i++, opcode++)
332 put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
333
334 for (i = 0; i < num_events; i++, opcode++)
335 put_unaligned_le16(mgmt_untrusted_events[i], opcode);
336 }
e70bb2e8 337
2a1afb5a
JH
338 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
339 rp, rp_size);
e70bb2e8
JH
340 kfree(rp);
341
342 return err;
343}
344
04124681
GP
345static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
346 u16 data_len)
faba42eb 347{
faba42eb 348 struct mgmt_rp_read_index_list *rp;
8035ded4 349 struct hci_dev *d;
a38528f1 350 size_t rp_len;
faba42eb 351 u16 count;
476e44cb 352 int err;
faba42eb
JH
353
354 BT_DBG("sock %p", sk);
355
356 read_lock(&hci_dev_list_lock);
357
358 count = 0;
bb4b2a9a 359 list_for_each_entry(d, &hci_dev_list, list) {
73d1df2a 360 if (d->dev_type == HCI_BREDR &&
d7a5a11d 361 !hci_dev_test_flag(d, HCI_UNCONFIGURED))
1514b892 362 count++;
faba42eb
JH
363 }
364
a38528f1
JH
365 rp_len = sizeof(*rp) + (2 * count);
366 rp = kmalloc(rp_len, GFP_ATOMIC);
367 if (!rp) {
b2c60d42 368 read_unlock(&hci_dev_list_lock);
faba42eb 369 return -ENOMEM;
b2c60d42 370 }
faba42eb 371
476e44cb 372 count = 0;
8035ded4 373 list_for_each_entry(d, &hci_dev_list, list) {
d7a5a11d
MH
374 if (hci_dev_test_flag(d, HCI_SETUP) ||
375 hci_dev_test_flag(d, HCI_CONFIG) ||
376 hci_dev_test_flag(d, HCI_USER_CHANNEL))
ab81cbf9
JH
377 continue;
378
73d1df2a
MH
379 /* Devices marked as raw-only are neither configured
380 * nor unconfigured controllers.
381 */
382 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
fee746b0
MH
383 continue;
384
73d1df2a 385 if (d->dev_type == HCI_BREDR &&
d7a5a11d 386 !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
1514b892
MH
387 rp->index[count++] = cpu_to_le16(d->id);
388 BT_DBG("Added hci%u", d->id);
389 }
faba42eb
JH
390 }
391
476e44cb
JH
392 rp->num_controllers = cpu_to_le16(count);
393 rp_len = sizeof(*rp) + (2 * count);
394
faba42eb
JH
395 read_unlock(&hci_dev_list_lock);
396
2a1afb5a
JH
397 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
398 0, rp, rp_len);
faba42eb 399
a38528f1
JH
400 kfree(rp);
401
402 return err;
faba42eb
JH
403}
404
73d1df2a
MH
405static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
406 void *data, u16 data_len)
407{
408 struct mgmt_rp_read_unconf_index_list *rp;
409 struct hci_dev *d;
410 size_t rp_len;
411 u16 count;
412 int err;
413
414 BT_DBG("sock %p", sk);
415
416 read_lock(&hci_dev_list_lock);
417
418 count = 0;
419 list_for_each_entry(d, &hci_dev_list, list) {
420 if (d->dev_type == HCI_BREDR &&
d7a5a11d 421 hci_dev_test_flag(d, HCI_UNCONFIGURED))
73d1df2a
MH
422 count++;
423 }
424
425 rp_len = sizeof(*rp) + (2 * count);
426 rp = kmalloc(rp_len, GFP_ATOMIC);
427 if (!rp) {
428 read_unlock(&hci_dev_list_lock);
429 return -ENOMEM;
430 }
431
432 count = 0;
433 list_for_each_entry(d, &hci_dev_list, list) {
d7a5a11d
MH
434 if (hci_dev_test_flag(d, HCI_SETUP) ||
435 hci_dev_test_flag(d, HCI_CONFIG) ||
436 hci_dev_test_flag(d, HCI_USER_CHANNEL))
73d1df2a
MH
437 continue;
438
439 /* Devices marked as raw-only are neither configured
440 * nor unconfigured controllers.
441 */
442 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
443 continue;
444
445 if (d->dev_type == HCI_BREDR &&
d7a5a11d 446 hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
73d1df2a
MH
447 rp->index[count++] = cpu_to_le16(d->id);
448 BT_DBG("Added hci%u", d->id);
449 }
450 }
451
452 rp->num_controllers = cpu_to_le16(count);
453 rp_len = sizeof(*rp) + (2 * count);
454
455 read_unlock(&hci_dev_list_lock);
456
2a1afb5a
JH
457 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
458 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
73d1df2a
MH
459
460 kfree(rp);
461
462 return err;
463}
464
96f1474a
MH
465static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
466 void *data, u16 data_len)
467{
468 struct mgmt_rp_read_ext_index_list *rp;
469 struct hci_dev *d;
470 size_t rp_len;
471 u16 count;
472 int err;
473
474 BT_DBG("sock %p", sk);
475
476 read_lock(&hci_dev_list_lock);
477
478 count = 0;
479 list_for_each_entry(d, &hci_dev_list, list) {
480 if (d->dev_type == HCI_BREDR || d->dev_type == HCI_AMP)
481 count++;
482 }
483
484 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
485 rp = kmalloc(rp_len, GFP_ATOMIC);
486 if (!rp) {
487 read_unlock(&hci_dev_list_lock);
488 return -ENOMEM;
489 }
490
491 count = 0;
492 list_for_each_entry(d, &hci_dev_list, list) {
493 if (hci_dev_test_flag(d, HCI_SETUP) ||
494 hci_dev_test_flag(d, HCI_CONFIG) ||
495 hci_dev_test_flag(d, HCI_USER_CHANNEL))
496 continue;
497
498 /* Devices marked as raw-only are neither configured
499 * nor unconfigured controllers.
500 */
501 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
502 continue;
503
504 if (d->dev_type == HCI_BREDR) {
505 if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
506 rp->entry[count].type = 0x01;
507 else
508 rp->entry[count].type = 0x00;
509 } else if (d->dev_type == HCI_AMP) {
510 rp->entry[count].type = 0x02;
511 } else {
512 continue;
513 }
514
515 rp->entry[count].bus = d->bus;
516 rp->entry[count++].index = cpu_to_le16(d->id);
517 BT_DBG("Added hci%u", d->id);
518 }
519
520 rp->num_controllers = cpu_to_le16(count);
521 rp_len = sizeof(*rp) + (sizeof(rp->entry[0]) * count);
522
523 read_unlock(&hci_dev_list_lock);
524
525 /* If this command is called at least once, then all the
526 * default index and unconfigured index events are disabled
527 * and from now on only extended index events are used.
528 */
529 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
530 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
531 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
532
533 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
534 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp, rp_len);
535
536 kfree(rp);
537
538 return err;
539}
540
dbece37a
MH
541static bool is_configured(struct hci_dev *hdev)
542{
543 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
d7a5a11d 544 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
dbece37a
MH
545 return false;
546
547 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
548 !bacmp(&hdev->public_addr, BDADDR_ANY))
549 return false;
550
551 return true;
552}
553
89bc22d2
MH
554static __le32 get_missing_options(struct hci_dev *hdev)
555{
556 u32 options = 0;
557
dbece37a 558 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
d7a5a11d 559 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
eb1904f4
MH
560 options |= MGMT_OPTION_EXTERNAL_CONFIG;
561
89bc22d2
MH
562 if (test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) &&
563 !bacmp(&hdev->public_addr, BDADDR_ANY))
564 options |= MGMT_OPTION_PUBLIC_ADDRESS;
565
566 return cpu_to_le32(options);
567}
568
f4537c04
MH
569static int new_options(struct hci_dev *hdev, struct sock *skip)
570{
571 __le32 options = get_missing_options(hdev);
572
f6b7712e
MH
573 return mgmt_generic_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
574 sizeof(options), skip);
f4537c04
MH
575}
576
dbece37a
MH
577static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
578{
579 __le32 options = get_missing_options(hdev);
580
2a1afb5a
JH
581 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
582 sizeof(options));
dbece37a
MH
583}
584
9fc3bfb6
MH
585static int read_config_info(struct sock *sk, struct hci_dev *hdev,
586 void *data, u16 data_len)
587{
588 struct mgmt_rp_read_config_info rp;
89bc22d2 589 u32 options = 0;
9fc3bfb6
MH
590
591 BT_DBG("sock %p %s", sk, hdev->name);
592
593 hci_dev_lock(hdev);
594
595 memset(&rp, 0, sizeof(rp));
596 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
89bc22d2 597
eb1904f4
MH
598 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
599 options |= MGMT_OPTION_EXTERNAL_CONFIG;
600
9fc3bfb6 601 if (hdev->set_bdaddr)
89bc22d2
MH
602 options |= MGMT_OPTION_PUBLIC_ADDRESS;
603
604 rp.supported_options = cpu_to_le32(options);
605 rp.missing_options = get_missing_options(hdev);
9fc3bfb6
MH
606
607 hci_dev_unlock(hdev);
608
2a1afb5a
JH
609 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
610 &rp, sizeof(rp));
9fc3bfb6
MH
611}
612
69ab39ea
JH
613static u32 get_supported_settings(struct hci_dev *hdev)
614{
615 u32 settings = 0;
616
617 settings |= MGMT_SETTING_POWERED;
b2939475 618 settings |= MGMT_SETTING_BONDABLE;
b1de97d8 619 settings |= MGMT_SETTING_DEBUG_KEYS;
3742abfc
JH
620 settings |= MGMT_SETTING_CONNECTABLE;
621 settings |= MGMT_SETTING_DISCOVERABLE;
69ab39ea 622
ed3fa31f 623 if (lmp_bredr_capable(hdev)) {
1a47aee8
JH
624 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
625 settings |= MGMT_SETTING_FAST_CONNECTABLE;
69ab39ea
JH
626 settings |= MGMT_SETTING_BREDR;
627 settings |= MGMT_SETTING_LINK_SECURITY;
a82974c9
MH
628
629 if (lmp_ssp_capable(hdev)) {
630 settings |= MGMT_SETTING_SSP;
631 settings |= MGMT_SETTING_HS;
632 }
e98d2ce2 633
05b3c3e7 634 if (lmp_sc_capable(hdev))
e98d2ce2 635 settings |= MGMT_SETTING_SECURE_CONN;
848566b3 636 }
d7b7e796 637
eeca6f89 638 if (lmp_le_capable(hdev)) {
9d42820f 639 settings |= MGMT_SETTING_LE;
eeca6f89 640 settings |= MGMT_SETTING_ADVERTISING;
a3209694 641 settings |= MGMT_SETTING_SECURE_CONN;
0f4bd942 642 settings |= MGMT_SETTING_PRIVACY;
93690c22 643 settings |= MGMT_SETTING_STATIC_ADDRESS;
eeca6f89 644 }
69ab39ea 645
eb1904f4
MH
646 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
647 hdev->set_bdaddr)
9fc3bfb6
MH
648 settings |= MGMT_SETTING_CONFIGURATION;
649
69ab39ea
JH
650 return settings;
651}
652
653static u32 get_current_settings(struct hci_dev *hdev)
654{
655 u32 settings = 0;
656
f1f0eb02 657 if (hdev_is_powered(hdev))
f0d4b78a
MH
658 settings |= MGMT_SETTING_POWERED;
659
d7a5a11d 660 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
69ab39ea
JH
661 settings |= MGMT_SETTING_CONNECTABLE;
662
d7a5a11d 663 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
1a4d3c4b
JH
664 settings |= MGMT_SETTING_FAST_CONNECTABLE;
665
d7a5a11d 666 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
69ab39ea
JH
667 settings |= MGMT_SETTING_DISCOVERABLE;
668
d7a5a11d 669 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
b2939475 670 settings |= MGMT_SETTING_BONDABLE;
69ab39ea 671
d7a5a11d 672 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
69ab39ea
JH
673 settings |= MGMT_SETTING_BREDR;
674
d7a5a11d 675 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
69ab39ea
JH
676 settings |= MGMT_SETTING_LE;
677
d7a5a11d 678 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
69ab39ea
JH
679 settings |= MGMT_SETTING_LINK_SECURITY;
680
d7a5a11d 681 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
69ab39ea
JH
682 settings |= MGMT_SETTING_SSP;
683
d7a5a11d 684 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
6d80dfd0
JH
685 settings |= MGMT_SETTING_HS;
686
d7a5a11d 687 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
eeca6f89
JH
688 settings |= MGMT_SETTING_ADVERTISING;
689
d7a5a11d 690 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
e98d2ce2
MH
691 settings |= MGMT_SETTING_SECURE_CONN;
692
d7a5a11d 693 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
b1de97d8
MH
694 settings |= MGMT_SETTING_DEBUG_KEYS;
695
d7a5a11d 696 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
0f4bd942
JH
697 settings |= MGMT_SETTING_PRIVACY;
698
93690c22
MH
699 /* The current setting for static address has two purposes. The
700 * first is to indicate if the static address will be used and
701 * the second is to indicate if it is actually set.
702 *
703 * This means if the static address is not configured, this flag
08dc0e98 704 * will never be set. If the address is configured, then if the
93690c22
MH
705 * address is actually used decides if the flag is set or not.
706 *
707 * For single mode LE only controllers and dual-mode controllers
708 * with BR/EDR disabled, the existence of the static address will
709 * be evaluated.
710 */
b7cb93e5 711 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
d7a5a11d 712 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
93690c22
MH
713 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
714 if (bacmp(&hdev->static_addr, BDADDR_ANY))
715 settings |= MGMT_SETTING_STATIC_ADDRESS;
716 }
717
69ab39ea
JH
718 return settings;
719}
720
ef580372
JH
721#define PNP_INFO_SVCLASS_ID 0x1200
722
213202ed
JH
723static u8 *create_uuid16_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
724{
725 u8 *ptr = data, *uuids_start = NULL;
726 struct bt_uuid *uuid;
727
728 if (len < 4)
729 return ptr;
730
731 list_for_each_entry(uuid, &hdev->uuids, list) {
732 u16 uuid16;
733
734 if (uuid->size != 16)
735 continue;
736
737 uuid16 = get_unaligned_le16(&uuid->uuid[12]);
738 if (uuid16 < 0x1100)
739 continue;
740
741 if (uuid16 == PNP_INFO_SVCLASS_ID)
742 continue;
743
744 if (!uuids_start) {
745 uuids_start = ptr;
746 uuids_start[0] = 1;
747 uuids_start[1] = EIR_UUID16_ALL;
748 ptr += 2;
749 }
750
751 /* Stop if not enough space to put next UUID */
752 if ((ptr - data) + sizeof(u16) > len) {
753 uuids_start[1] = EIR_UUID16_SOME;
754 break;
755 }
756
757 *ptr++ = (uuid16 & 0x00ff);
758 *ptr++ = (uuid16 & 0xff00) >> 8;
759 uuids_start[0] += sizeof(uuid16);
760 }
761
762 return ptr;
763}
764
cdf1963f
JH
765static u8 *create_uuid32_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
766{
767 u8 *ptr = data, *uuids_start = NULL;
768 struct bt_uuid *uuid;
769
770 if (len < 6)
771 return ptr;
772
773 list_for_each_entry(uuid, &hdev->uuids, list) {
774 if (uuid->size != 32)
775 continue;
776
777 if (!uuids_start) {
778 uuids_start = ptr;
779 uuids_start[0] = 1;
780 uuids_start[1] = EIR_UUID32_ALL;
781 ptr += 2;
782 }
783
784 /* Stop if not enough space to put next UUID */
785 if ((ptr - data) + sizeof(u32) > len) {
786 uuids_start[1] = EIR_UUID32_SOME;
787 break;
788 }
789
790 memcpy(ptr, &uuid->uuid[12], sizeof(u32));
791 ptr += sizeof(u32);
792 uuids_start[0] += sizeof(u32);
793 }
794
795 return ptr;
796}
797
c00d575b
JH
798static u8 *create_uuid128_list(struct hci_dev *hdev, u8 *data, ptrdiff_t len)
799{
800 u8 *ptr = data, *uuids_start = NULL;
801 struct bt_uuid *uuid;
802
803 if (len < 18)
804 return ptr;
805
806 list_for_each_entry(uuid, &hdev->uuids, list) {
807 if (uuid->size != 128)
808 continue;
809
810 if (!uuids_start) {
811 uuids_start = ptr;
812 uuids_start[0] = 1;
813 uuids_start[1] = EIR_UUID128_ALL;
814 ptr += 2;
815 }
816
817 /* Stop if not enough space to put next UUID */
818 if ((ptr - data) + 16 > len) {
819 uuids_start[1] = EIR_UUID128_SOME;
820 break;
821 }
822
823 memcpy(ptr, uuid->uuid, 16);
824 ptr += 16;
825 uuids_start[0] += 16;
826 }
827
828 return ptr;
829}
830
333ae95d
JH
831static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
832{
833 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
834}
835
333ae95d
JH
836static struct mgmt_pending_cmd *pending_find_data(u16 opcode,
837 struct hci_dev *hdev,
838 const void *data)
839{
840 return mgmt_pending_find_data(HCI_CHANNEL_CONTROL, opcode, hdev, data);
841}
842
91aa9bb2
FG
843static u8 get_current_adv_instance(struct hci_dev *hdev)
844{
845 /* The "Set Advertising" setting supersedes the "Add Advertising"
846 * setting. Here we set the advertising data based on which
847 * setting was set. When neither apply, default to the global settings,
848 * represented by instance "0".
849 */
850 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
851 !hci_dev_test_flag(hdev, HCI_ADVERTISING))
3ff37e6b 852 return hdev->cur_adv_instance;
91aa9bb2
FG
853
854 return 0x00;
855}
856
4117ed70 857static u8 create_default_scan_rsp_data(struct hci_dev *hdev, u8 *ptr)
f14d8f64 858{
7a5f4990
MH
859 u8 ad_len = 0;
860 size_t name_len;
861
862 name_len = strlen(hdev->dev_name);
863 if (name_len > 0) {
864 size_t max_len = HCI_MAX_AD_LENGTH - ad_len - 2;
865
866 if (name_len > max_len) {
867 name_len = max_len;
868 ptr[1] = EIR_NAME_SHORT;
869 } else
870 ptr[1] = EIR_NAME_COMPLETE;
871
872 ptr[0] = name_len + 1;
873
874 memcpy(ptr + 2, hdev->dev_name, name_len);
875
876 ad_len += (name_len + 2);
877 ptr += (name_len + 2);
878 }
879
880 return ad_len;
f14d8f64
MH
881}
882
ca21fbe9
FG
883static u8 create_instance_scan_rsp_data(struct hci_dev *hdev, u8 instance,
884 u8 *ptr)
4117ed70 885{
ca21fbe9
FG
886 struct adv_info *adv_instance;
887
888 adv_instance = hci_find_adv_instance(hdev, instance);
889 if (!adv_instance)
890 return 0;
891
4117ed70
AU
892 /* TODO: Set the appropriate entries based on advertising instance flags
893 * here once flags other than 0 are supported.
894 */
ca21fbe9
FG
895 memcpy(ptr, adv_instance->scan_rsp_data,
896 adv_instance->scan_rsp_len);
4117ed70 897
ca21fbe9 898 return adv_instance->scan_rsp_len;
4117ed70
AU
899}
900
efae002c 901static void update_inst_scan_rsp_data(struct hci_request *req, u8 instance)
f14d8f64
MH
902{
903 struct hci_dev *hdev = req->hdev;
904 struct hci_cp_le_set_scan_rsp_data cp;
905 u8 len;
906
d7a5a11d 907 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
f14d8f64
MH
908 return;
909
910 memset(&cp, 0, sizeof(cp));
911
4117ed70 912 if (instance)
ca21fbe9 913 len = create_instance_scan_rsp_data(hdev, instance, cp.data);
4117ed70
AU
914 else
915 len = create_default_scan_rsp_data(hdev, cp.data);
f14d8f64 916
eb438b5f 917 if (hdev->scan_rsp_data_len == len &&
4117ed70 918 !memcmp(cp.data, hdev->scan_rsp_data, len))
f14d8f64
MH
919 return;
920
eb438b5f
JH
921 memcpy(hdev->scan_rsp_data, cp.data, sizeof(cp.data));
922 hdev->scan_rsp_data_len = len;
f14d8f64
MH
923
924 cp.length = len;
925
926 hci_req_add(req, HCI_OP_LE_SET_SCAN_RSP_DATA, sizeof(cp), &cp);
927}
928
4117ed70
AU
929static void update_scan_rsp_data(struct hci_request *req)
930{
efae002c 931 update_inst_scan_rsp_data(req, get_current_adv_instance(req->hdev));
4117ed70
AU
932}
933
9a43e25f
JH
934static u8 get_adv_discov_flags(struct hci_dev *hdev)
935{
3b0602cd 936 struct mgmt_pending_cmd *cmd;
9a43e25f
JH
937
938 /* If there's a pending mgmt command the flags will not yet have
939 * their final values, so check for this first.
940 */
333ae95d 941 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
9a43e25f
JH
942 if (cmd) {
943 struct mgmt_mode *cp = cmd->param;
944 if (cp->val == 0x01)
945 return LE_AD_GENERAL;
946 else if (cp->val == 0x02)
947 return LE_AD_LIMITED;
948 } else {
d7a5a11d 949 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
9a43e25f 950 return LE_AD_LIMITED;
d7a5a11d 951 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
9a43e25f
JH
952 return LE_AD_GENERAL;
953 }
954
955 return 0;
956}
957
fdf51784
AU
958static bool get_connectable(struct hci_dev *hdev)
959{
960 struct mgmt_pending_cmd *cmd;
441ad2d0 961
fdf51784
AU
962 /* If there's a pending mgmt command the flag will not yet have
963 * it's final value, so check for this first.
964 */
965 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
966 if (cmd) {
967 struct mgmt_mode *cp = cmd->param;
441ad2d0 968
fdf51784 969 return cp->val;
441ad2d0
MH
970 }
971
fdf51784
AU
972 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
973}
441ad2d0 974
fdf51784
AU
975static u32 get_adv_instance_flags(struct hci_dev *hdev, u8 instance)
976{
977 u32 flags;
411b4121 978 struct adv_info *adv_instance;
441ad2d0 979
bea28e65
FG
980 if (instance == 0x00) {
981 /* Instance 0 always manages the "Tx Power" and "Flags"
982 * fields
983 */
984 flags = MGMT_ADV_FLAG_TX_POWER | MGMT_ADV_FLAG_MANAGED_FLAGS;
411b4121 985
bea28e65
FG
986 /* For instance 0, the HCI_ADVERTISING_CONNECTABLE setting
987 * corresponds to the "connectable" instance flag.
988 */
989 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE))
990 flags |= MGMT_ADV_FLAG_CONNECTABLE;
fdf51784 991
bea28e65 992 return flags;
411b4121 993 }
fdf51784 994
bea28e65 995 adv_instance = hci_find_adv_instance(hdev, instance);
fdf51784 996
bea28e65
FG
997 /* Return 0 when we got an invalid instance identifier. */
998 if (!adv_instance)
999 return 0;
fdf51784 1000
bea28e65 1001 return adv_instance->flags;
441ad2d0
MH
1002}
1003
7b683b74 1004static u8 get_cur_adv_instance_scan_rsp_len(struct hci_dev *hdev)
c7d4883b 1005{
7b683b74
FG
1006 u8 instance = get_current_adv_instance(hdev);
1007 struct adv_info *adv_instance;
1008
1009 /* Ignore instance 0 */
1010 if (instance == 0x00)
1011 return 0;
1012
1013 adv_instance = hci_find_adv_instance(hdev, instance);
1014 if (!adv_instance)
c7d4883b
AU
1015 return 0;
1016
1017 /* TODO: Take into account the "appearance" and "local-name" flags here.
1018 * These are currently being ignored as they are not supported.
1019 */
7b683b74 1020 return adv_instance->scan_rsp_len;
c7d4883b
AU
1021}
1022
fdf51784 1023static u8 create_instance_adv_data(struct hci_dev *hdev, u8 instance, u8 *ptr)
24b4f38f 1024{
f63ba24b 1025 struct adv_info *adv_instance = NULL;
b44133ff 1026 u8 ad_len = 0, flags = 0;
f63ba24b
FG
1027 u32 instance_flags;
1028
1029 /* Return 0 when the current instance identifier is invalid. */
1030 if (instance) {
1031 adv_instance = hci_find_adv_instance(hdev, instance);
1032 if (!adv_instance)
1033 return 0;
1034 }
1035
1036 instance_flags = get_adv_instance_flags(hdev, instance);
b44133ff 1037
807ec772
AU
1038 /* The Add Advertising command allows userspace to set both the general
1039 * and limited discoverable flags.
1040 */
fdf51784 1041 if (instance_flags & MGMT_ADV_FLAG_DISCOV)
b44133ff
AU
1042 flags |= LE_AD_GENERAL;
1043
fdf51784 1044 if (instance_flags & MGMT_ADV_FLAG_LIMITED_DISCOV)
807ec772
AU
1045 flags |= LE_AD_LIMITED;
1046
fdf51784 1047 if (flags || (instance_flags & MGMT_ADV_FLAG_MANAGED_FLAGS)) {
67e0c0cd
AU
1048 /* If a discovery flag wasn't provided, simply use the global
1049 * settings.
1050 */
1051 if (!flags)
1052 flags |= get_adv_discov_flags(hdev);
1053
b44133ff
AU
1054 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1055 flags |= LE_AD_NO_BREDR;
1056
fdf51784
AU
1057 /* If flags would still be empty, then there is no need to
1058 * include the "Flags" AD field".
1059 */
1060 if (flags) {
1061 ptr[0] = 0x02;
1062 ptr[1] = EIR_FLAGS;
1063 ptr[2] = flags;
b44133ff 1064
fdf51784
AU
1065 ad_len += 3;
1066 ptr += 3;
1067 }
b44133ff
AU
1068 }
1069
f63ba24b
FG
1070 if (adv_instance) {
1071 memcpy(ptr, adv_instance->adv_data,
1072 adv_instance->adv_data_len);
1073 ad_len += adv_instance->adv_data_len;
1074 ptr += adv_instance->adv_data_len;
38c8af60
MH
1075 }
1076
fdf51784 1077 /* Provide Tx Power only if we can provide a valid value for it */
5507e358 1078 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID &&
fdf51784 1079 (instance_flags & MGMT_ADV_FLAG_TX_POWER)) {
5507e358
AU
1080 ptr[0] = 0x02;
1081 ptr[1] = EIR_TX_POWER;
1082 ptr[2] = (u8)hdev->adv_tx_power;
1083
1084 ad_len += 3;
1085 ptr += 3;
1086 }
1087
b44133ff 1088 return ad_len;
24b4f38f
AU
1089}
1090
efae002c 1091static void update_inst_adv_data(struct hci_request *req, u8 instance)
441ad2d0
MH
1092{
1093 struct hci_dev *hdev = req->hdev;
1094 struct hci_cp_le_set_adv_data cp;
1095 u8 len;
1096
d7a5a11d 1097 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
441ad2d0
MH
1098 return;
1099
1100 memset(&cp, 0, sizeof(cp));
1101
fdf51784 1102 len = create_instance_adv_data(hdev, instance, cp.data);
441ad2d0 1103
24b4f38f 1104 /* There's nothing to do if the data hasn't changed */
441ad2d0
MH
1105 if (hdev->adv_data_len == len &&
1106 memcmp(cp.data, hdev->adv_data, len) == 0)
1107 return;
1108
1109 memcpy(hdev->adv_data, cp.data, sizeof(cp.data));
1110 hdev->adv_data_len = len;
1111
1112 cp.length = len;
1113
1114 hci_req_add(req, HCI_OP_LE_SET_ADV_DATA, sizeof(cp), &cp);
1115}
1116
e7a685d3
AU
1117static void update_adv_data(struct hci_request *req)
1118{
efae002c 1119 update_inst_adv_data(req, get_current_adv_instance(req->hdev));
24b4f38f
AU
1120}
1121
bc6d2d04
JH
1122int mgmt_update_adv_data(struct hci_dev *hdev)
1123{
1124 struct hci_request req;
1125
1126 hci_req_init(&req, hdev);
1127 update_adv_data(&req);
1128
1129 return hci_req_run(&req, NULL);
1130}
1131
ef580372
JH
1132static void create_eir(struct hci_dev *hdev, u8 *data)
1133{
1134 u8 *ptr = data;
ef580372
JH
1135 size_t name_len;
1136
1137 name_len = strlen(hdev->dev_name);
1138
1139 if (name_len > 0) {
1140 /* EIR Data type */
1141 if (name_len > 48) {
1142 name_len = 48;
1143 ptr[1] = EIR_NAME_SHORT;
1144 } else
1145 ptr[1] = EIR_NAME_COMPLETE;
1146
1147 /* EIR Data length */
1148 ptr[0] = name_len + 1;
1149
1150 memcpy(ptr + 2, hdev->dev_name, name_len);
1151
ef580372
JH
1152 ptr += (name_len + 2);
1153 }
1154
bbaf444a 1155 if (hdev->inq_tx_power != HCI_TX_POWER_INVALID) {
91c4e9b1
MH
1156 ptr[0] = 2;
1157 ptr[1] = EIR_TX_POWER;
1158 ptr[2] = (u8) hdev->inq_tx_power;
1159
91c4e9b1
MH
1160 ptr += 3;
1161 }
1162
2b9be137
MH
1163 if (hdev->devid_source > 0) {
1164 ptr[0] = 9;
1165 ptr[1] = EIR_DEVICE_ID;
1166
1167 put_unaligned_le16(hdev->devid_source, ptr + 2);
1168 put_unaligned_le16(hdev->devid_vendor, ptr + 4);
1169 put_unaligned_le16(hdev->devid_product, ptr + 6);
1170 put_unaligned_le16(hdev->devid_version, ptr + 8);
1171
2b9be137
MH
1172 ptr += 10;
1173 }
1174
213202ed 1175 ptr = create_uuid16_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
cdf1963f 1176 ptr = create_uuid32_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
c00d575b 1177 ptr = create_uuid128_list(hdev, ptr, HCI_MAX_EIR_LENGTH - (ptr - data));
ef580372
JH
1178}
1179
890ea898 1180static void update_eir(struct hci_request *req)
ef580372 1181{
890ea898 1182 struct hci_dev *hdev = req->hdev;
ef580372
JH
1183 struct hci_cp_write_eir cp;
1184
504c8dcd 1185 if (!hdev_is_powered(hdev))
890ea898 1186 return;
7770c4aa 1187
976eb20e 1188 if (!lmp_ext_inq_capable(hdev))
890ea898 1189 return;
ef580372 1190
d7a5a11d 1191 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
890ea898 1192 return;
ef580372 1193
d7a5a11d 1194 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
890ea898 1195 return;
ef580372
JH
1196
1197 memset(&cp, 0, sizeof(cp));
1198
1199 create_eir(hdev, cp.data);
1200
1201 if (memcmp(cp.data, hdev->eir, sizeof(cp.data)) == 0)
890ea898 1202 return;
ef580372
JH
1203
1204 memcpy(hdev->eir, cp.data, sizeof(cp.data));
1205
890ea898 1206 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
ef580372
JH
1207}
1208
1209static u8 get_service_classes(struct hci_dev *hdev)
1210{
1211 struct bt_uuid *uuid;
1212 u8 val = 0;
1213
1214 list_for_each_entry(uuid, &hdev->uuids, list)
1215 val |= uuid->svc_hint;
1216
1217 return val;
1218}
1219
890ea898 1220static void update_class(struct hci_request *req)
ef580372 1221{
890ea898 1222 struct hci_dev *hdev = req->hdev;
ef580372
JH
1223 u8 cod[3];
1224
1225 BT_DBG("%s", hdev->name);
1226
504c8dcd 1227 if (!hdev_is_powered(hdev))
890ea898 1228 return;
7770c4aa 1229
d7a5a11d 1230 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
f87ea1da
JH
1231 return;
1232
d7a5a11d 1233 if (hci_dev_test_flag(hdev, HCI_SERVICE_CACHE))
890ea898 1234 return;
ef580372
JH
1235
1236 cod[0] = hdev->minor_class;
1237 cod[1] = hdev->major_class;
1238 cod[2] = get_service_classes(hdev);
1239
d7a5a11d 1240 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
6acd7db4
MH
1241 cod[1] |= 0x20;
1242
ef580372 1243 if (memcmp(cod, hdev->dev_class, 3) == 0)
890ea898 1244 return;
ef580372 1245
890ea898 1246 hci_req_add(req, HCI_OP_WRITE_CLASS_OF_DEV, sizeof(cod), cod);
ef580372
JH
1247}
1248
0ec5ae84
JH
1249static void disable_advertising(struct hci_request *req)
1250{
1251 u8 enable = 0x00;
1252
1253 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1254}
1255
199a2fb1
JH
1256static void enable_advertising(struct hci_request *req)
1257{
1258 struct hci_dev *hdev = req->hdev;
1259 struct hci_cp_le_set_adv_param cp;
8f2a0601 1260 u8 own_addr_type, enable = 0x01;
a4858cb9 1261 bool connectable;
e7a685d3
AU
1262 u8 instance;
1263 u32 flags;
199a2fb1 1264
0ec5ae84
JH
1265 if (hci_conn_num(hdev, LE_LINK) > 0)
1266 return;
1267
d7a5a11d 1268 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
0ec5ae84
JH
1269 disable_advertising(req);
1270
5ce194c4 1271 /* Clear the HCI_LE_ADV bit temporarily so that the
8d97250e
JH
1272 * hci_update_random_address knows that it's safe to go ahead
1273 * and write a new random address. The flag will be set back on
1274 * as soon as the SET_ADV_ENABLE HCI command completes.
1275 */
a358dc11 1276 hci_dev_clear_flag(hdev, HCI_LE_ADV);
8d97250e 1277
e7a685d3
AU
1278 instance = get_current_adv_instance(hdev);
1279 flags = get_adv_instance_flags(hdev, instance);
faccb950
AU
1280
1281 /* If the "connectable" instance flag was not set, then choose between
1282 * ADV_IND and ADV_NONCONN_IND based on the global connectable setting.
1283 */
1284 connectable = (flags & MGMT_ADV_FLAG_CONNECTABLE) ||
1285 get_connectable(hdev);
8f2a0601 1286
a4858cb9
JH
1287 /* Set require_privacy to true only when non-connectable
1288 * advertising is used. In that case it is fine to use a
1289 * non-resolvable private address.
1290 */
1291 if (hci_update_random_address(req, !connectable, &own_addr_type) < 0)
8f2a0601
JH
1292 return;
1293
41c90c18 1294 memset(&cp, 0, sizeof(cp));
628531c9
GL
1295 cp.min_interval = cpu_to_le16(hdev->le_adv_min_interval);
1296 cp.max_interval = cpu_to_le16(hdev->le_adv_max_interval);
c7d4883b
AU
1297
1298 if (connectable)
1299 cp.type = LE_ADV_IND;
7b683b74 1300 else if (get_cur_adv_instance_scan_rsp_len(hdev))
c7d4883b
AU
1301 cp.type = LE_ADV_SCAN_IND;
1302 else
1303 cp.type = LE_ADV_NONCONN_IND;
1304
8f2a0601 1305 cp.own_address_type = own_addr_type;
199a2fb1
JH
1306 cp.channel_map = hdev->le_adv_channel_map;
1307
1308 hci_req_add(req, HCI_OP_LE_SET_ADV_PARAM, sizeof(cp), &cp);
1309
1310 hci_req_add(req, HCI_OP_LE_SET_ADV_ENABLE, sizeof(enable), &enable);
1311}
1312
7d78525d
JH
1313static void service_cache_off(struct work_struct *work)
1314{
1315 struct hci_dev *hdev = container_of(work, struct hci_dev,
04124681 1316 service_cache.work);
890ea898 1317 struct hci_request req;
7d78525d 1318
a69d8927 1319 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
7d78525d
JH
1320 return;
1321
890ea898
JH
1322 hci_req_init(&req, hdev);
1323
7d78525d
JH
1324 hci_dev_lock(hdev);
1325
890ea898
JH
1326 update_eir(&req);
1327 update_class(&req);
7d78525d
JH
1328
1329 hci_dev_unlock(hdev);
890ea898
JH
1330
1331 hci_req_run(&req, NULL);
7d78525d
JH
1332}
1333
d6bfd59c
JH
1334static void rpa_expired(struct work_struct *work)
1335{
1336 struct hci_dev *hdev = container_of(work, struct hci_dev,
1337 rpa_expired.work);
1338 struct hci_request req;
1339
1340 BT_DBG("");
1341
a1536da2 1342 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
d6bfd59c 1343
d7a5a11d 1344 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
d6bfd59c
JH
1345 return;
1346
1347 /* The generation of a new RPA and programming it into the
1348 * controller happens in the enable_advertising() function.
1349 */
d6bfd59c 1350 hci_req_init(&req, hdev);
d6bfd59c 1351 enable_advertising(&req);
d6bfd59c
JH
1352 hci_req_run(&req, NULL);
1353}
1354
6a919082 1355static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
7d78525d 1356{
238be788 1357 if (hci_dev_test_and_set_flag(hdev, HCI_MGMT))
6a919082
JH
1358 return;
1359
4f87da80 1360 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
d6bfd59c 1361 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
7d78525d 1362
4f87da80
JH
1363 /* Non-mgmt controlled devices get this bit set
1364 * implicitly so that pairing works for them, however
1365 * for mgmt we require user-space to explicitly enable
1366 * it
1367 */
a358dc11 1368 hci_dev_clear_flag(hdev, HCI_BONDABLE);
7d78525d
JH
1369}
1370
0f4e68cf 1371static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
04124681 1372 void *data, u16 data_len)
0381101f 1373{
a38528f1 1374 struct mgmt_rp_read_info rp;
f7b64e69 1375
bdb6d971 1376 BT_DBG("sock %p %s", sk, hdev->name);
f7b64e69 1377
09fd0de5 1378 hci_dev_lock(hdev);
f7b64e69 1379
dc4fe30b
JH
1380 memset(&rp, 0, sizeof(rp));
1381
69ab39ea 1382 bacpy(&rp.bdaddr, &hdev->bdaddr);
f7b64e69 1383
69ab39ea 1384 rp.version = hdev->hci_ver;
eb55ef07 1385 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
69ab39ea
JH
1386
1387 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1388 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
f7b64e69 1389
a38528f1 1390 memcpy(rp.dev_class, hdev->dev_class, 3);
f7b64e69 1391
dc4fe30b 1392 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
27fcc362 1393 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
dc4fe30b 1394
09fd0de5 1395 hci_dev_unlock(hdev);
0381101f 1396
2a1afb5a
JH
1397 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1398 sizeof(rp));
0381101f
JH
1399}
1400
69ab39ea 1401static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
8680570b 1402{
69ab39ea 1403 __le32 settings = cpu_to_le32(get_current_settings(hdev));
8680570b 1404
2a1afb5a
JH
1405 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1406 sizeof(settings));
8680570b
JH
1407}
1408
1904a853 1409static void clean_up_hci_complete(struct hci_dev *hdev, u8 status, u16 opcode)
8b064a3a
JH
1410{
1411 BT_DBG("%s status 0x%02x", hdev->name, status);
1412
a3172b7e
JH
1413 if (hci_conn_count(hdev) == 0) {
1414 cancel_delayed_work(&hdev->power_off);
8b064a3a 1415 queue_work(hdev->req_workqueue, &hdev->power_off.work);
a3172b7e 1416 }
8b064a3a
JH
1417}
1418
23a48093 1419static bool hci_stop_discovery(struct hci_request *req)
21a60d30
JH
1420{
1421 struct hci_dev *hdev = req->hdev;
1422 struct hci_cp_remote_name_req_cancel cp;
1423 struct inquiry_entry *e;
1424
1425 switch (hdev->discovery.state) {
1426 case DISCOVERY_FINDING:
07d2334a 1427 if (test_bit(HCI_INQUIRY, &hdev->flags))
21a60d30 1428 hci_req_add(req, HCI_OP_INQUIRY_CANCEL, 0, NULL);
07d2334a
JP
1429
1430 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
21a60d30
JH
1431 cancel_delayed_work(&hdev->le_scan_disable);
1432 hci_req_add_le_scan_disable(req);
1433 }
1434
23a48093 1435 return true;
21a60d30
JH
1436
1437 case DISCOVERY_RESOLVING:
1438 e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY,
1439 NAME_PENDING);
1440 if (!e)
23a48093 1441 break;
21a60d30
JH
1442
1443 bacpy(&cp.bdaddr, &e->data.bdaddr);
1444 hci_req_add(req, HCI_OP_REMOTE_NAME_REQ_CANCEL, sizeof(cp),
1445 &cp);
1446
23a48093 1447 return true;
21a60d30
JH
1448
1449 default:
1450 /* Passive scanning */
d7a5a11d 1451 if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) {
21a60d30 1452 hci_req_add_le_scan_disable(req);
23a48093
JH
1453 return true;
1454 }
1455
21a60d30
JH
1456 break;
1457 }
23a48093
JH
1458
1459 return false;
21a60d30
JH
1460}
1461
912098a6
AU
1462static void advertising_added(struct sock *sk, struct hci_dev *hdev,
1463 u8 instance)
1464{
1465 struct mgmt_ev_advertising_added ev;
1466
1467 ev.instance = instance;
1468
1469 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1470}
1471
1472static void advertising_removed(struct sock *sk, struct hci_dev *hdev,
1473 u8 instance)
1474{
1475 struct mgmt_ev_advertising_removed ev;
1476
1477 ev.instance = instance;
1478
1479 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1480}
1481
7816b820
FG
1482static int schedule_adv_instance(struct hci_request *req, u8 instance,
1483 bool force) {
1484 struct hci_dev *hdev = req->hdev;
1485 struct adv_info *adv_instance = NULL;
1486 u16 timeout;
1487
1488 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
1489 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
1490 return -EPERM;
1491
1492 if (hdev->adv_instance_timeout)
1493 return -EBUSY;
1494
1495 adv_instance = hci_find_adv_instance(hdev, instance);
1496 if (!adv_instance)
1497 return -ENOENT;
1498
1499 /* A zero timeout means unlimited advertising. As long as there is
1500 * only one instance, duration should be ignored. We still set a timeout
1501 * in case further instances are being added later on.
1502 *
1503 * If the remaining lifetime of the instance is more than the duration
1504 * then the timeout corresponds to the duration, otherwise it will be
1505 * reduced to the remaining instance lifetime.
1506 */
1507 if (adv_instance->timeout == 0 ||
1508 adv_instance->duration <= adv_instance->remaining_time)
1509 timeout = adv_instance->duration;
1510 else
1511 timeout = adv_instance->remaining_time;
1512
1513 /* The remaining time is being reduced unless the instance is being
1514 * advertised without time limit.
1515 */
1516 if (adv_instance->timeout)
1517 adv_instance->remaining_time =
1518 adv_instance->remaining_time - timeout;
1519
1520 hdev->adv_instance_timeout = timeout;
1521 queue_delayed_work(hdev->workqueue,
1522 &hdev->adv_instance_expire,
1523 msecs_to_jiffies(timeout * 1000));
1524
1525 /* If we're just re-scheduling the same instance again then do not
1526 * execute any HCI commands. This happens when a single instance is
1527 * being advertised.
1528 */
1529 if (!force && hdev->cur_adv_instance == instance &&
1530 hci_dev_test_flag(hdev, HCI_LE_ADV))
1531 return 0;
1532
1533 hdev->cur_adv_instance = instance;
1534 update_adv_data(req);
1535 update_scan_rsp_data(req);
1536 enable_advertising(req);
1537
1538 return 0;
1539}
1540
1541static void cancel_adv_timeout(struct hci_dev *hdev)
1542{
1543 if (hdev->adv_instance_timeout) {
1544 hdev->adv_instance_timeout = 0;
1545 cancel_delayed_work(&hdev->adv_instance_expire);
1546 }
1547}
1548
847818d9
FG
1549/* For a single instance:
1550 * - force == true: The instance will be removed even when its remaining
1551 * lifetime is not zero.
1552 * - force == false: the instance will be deactivated but kept stored unless
1553 * the remaining lifetime is zero.
1554 *
1555 * For instance == 0x00:
1556 * - force == true: All instances will be removed regardless of their timeout
1557 * setting.
1558 * - force == false: Only instances that have a timeout will be removed.
1559 */
1560static void clear_adv_instance(struct hci_dev *hdev, struct hci_request *req,
1561 u8 instance, bool force)
912098a6 1562{
847818d9
FG
1563 struct adv_info *adv_instance, *n, *next_instance = NULL;
1564 int err;
1565 u8 rem_inst;
912098a6 1566
847818d9
FG
1567 /* Cancel any timeout concerning the removed instance(s). */
1568 if (!instance || hdev->cur_adv_instance == instance)
1569 cancel_adv_timeout(hdev);
912098a6 1570
847818d9
FG
1571 /* Get the next instance to advertise BEFORE we remove
1572 * the current one. This can be the same instance again
1573 * if there is only one instance.
1574 */
1575 if (instance && hdev->cur_adv_instance == instance)
1576 next_instance = hci_get_next_instance(hdev, instance);
912098a6 1577
847818d9
FG
1578 if (instance == 0x00) {
1579 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances,
1580 list) {
1581 if (!(force || adv_instance->timeout))
1582 continue;
912098a6 1583
847818d9
FG
1584 rem_inst = adv_instance->instance;
1585 err = hci_remove_adv_instance(hdev, rem_inst);
1586 if (!err)
1587 advertising_removed(NULL, hdev, rem_inst);
1588 }
1589 hdev->cur_adv_instance = 0x00;
1590 } else {
1591 adv_instance = hci_find_adv_instance(hdev, instance);
1592
1593 if (force || (adv_instance && adv_instance->timeout &&
1594 !adv_instance->remaining_time)) {
1595 /* Don't advertise a removed instance. */
1596 if (next_instance &&
1597 next_instance->instance == instance)
1598 next_instance = NULL;
1599
1600 err = hci_remove_adv_instance(hdev, instance);
1601 if (!err)
1602 advertising_removed(NULL, hdev, instance);
1603 }
1604 }
1605
1606 if (list_empty(&hdev->adv_instances)) {
1607 hdev->cur_adv_instance = 0x00;
1608 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
1609 }
1610
1611 if (!req || !hdev_is_powered(hdev) ||
912098a6
AU
1612 hci_dev_test_flag(hdev, HCI_ADVERTISING))
1613 return;
1614
847818d9
FG
1615 if (next_instance)
1616 schedule_adv_instance(req, next_instance->instance, false);
912098a6
AU
1617}
1618
8b064a3a
JH
1619static int clean_up_hci_state(struct hci_dev *hdev)
1620{
1621 struct hci_request req;
1622 struct hci_conn *conn;
23a48093
JH
1623 bool discov_stopped;
1624 int err;
8b064a3a
JH
1625
1626 hci_req_init(&req, hdev);
1627
1628 if (test_bit(HCI_ISCAN, &hdev->flags) ||
1629 test_bit(HCI_PSCAN, &hdev->flags)) {
1630 u8 scan = 0x00;
1631 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
1632 }
1633
847818d9 1634 clear_adv_instance(hdev, NULL, 0x00, false);
912098a6 1635
d7a5a11d 1636 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
8b064a3a
JH
1637 disable_advertising(&req);
1638
23a48093 1639 discov_stopped = hci_stop_discovery(&req);
8b064a3a
JH
1640
1641 list_for_each_entry(conn, &hdev->conn_hash.list, list) {
1642 struct hci_cp_disconnect dc;
c9910d0f
JH
1643 struct hci_cp_reject_conn_req rej;
1644
1645 switch (conn->state) {
1646 case BT_CONNECTED:
1647 case BT_CONFIG:
1648 dc.handle = cpu_to_le16(conn->handle);
1649 dc.reason = 0x15; /* Terminated due to Power Off */
1650 hci_req_add(&req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
1651 break;
1652 case BT_CONNECT:
1653 if (conn->type == LE_LINK)
1654 hci_req_add(&req, HCI_OP_LE_CREATE_CONN_CANCEL,
1655 0, NULL);
1656 else if (conn->type == ACL_LINK)
1657 hci_req_add(&req, HCI_OP_CREATE_CONN_CANCEL,
1658 6, &conn->dst);
1659 break;
1660 case BT_CONNECT2:
1661 bacpy(&rej.bdaddr, &conn->dst);
1662 rej.reason = 0x15; /* Terminated due to Power Off */
1663 if (conn->type == ACL_LINK)
1664 hci_req_add(&req, HCI_OP_REJECT_CONN_REQ,
1665 sizeof(rej), &rej);
1666 else if (conn->type == SCO_LINK)
1667 hci_req_add(&req, HCI_OP_REJECT_SYNC_CONN_REQ,
1668 sizeof(rej), &rej);
1669 break;
1670 }
8b064a3a
JH
1671 }
1672
23a48093
JH
1673 err = hci_req_run(&req, clean_up_hci_complete);
1674 if (!err && discov_stopped)
1675 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
1676
1677 return err;
8b064a3a
JH
1678}
1679
bdb6d971 1680static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 1681 u16 len)
eec8d2bc 1682{
650f726d 1683 struct mgmt_mode *cp = data;
3b0602cd 1684 struct mgmt_pending_cmd *cmd;
4b34ee78 1685 int err;
eec8d2bc 1686
bdb6d971 1687 BT_DBG("request for %s", hdev->name);
eec8d2bc 1688
a7e80f25 1689 if (cp->val != 0x00 && cp->val != 0x01)
a69e8375
JH
1690 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1691 MGMT_STATUS_INVALID_PARAMS);
a7e80f25 1692
09fd0de5 1693 hci_dev_lock(hdev);
eec8d2bc 1694
333ae95d 1695 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
a69e8375
JH
1696 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1697 MGMT_STATUS_BUSY);
87b95ba6
JH
1698 goto failed;
1699 }
1700
a69d8927 1701 if (hci_dev_test_and_clear_flag(hdev, HCI_AUTO_OFF)) {
f0d4b78a
MH
1702 cancel_delayed_work(&hdev->power_off);
1703
1704 if (cp->val) {
a1d70450
JH
1705 mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev,
1706 data, len);
1707 err = mgmt_powered(hdev, 1);
f0d4b78a
MH
1708 goto failed;
1709 }
1710 }
1711
4b34ee78 1712 if (!!cp->val == hdev_is_powered(hdev)) {
69ab39ea 1713 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
eec8d2bc
JH
1714 goto failed;
1715 }
1716
2e58ef3e 1717 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
366a0336
JH
1718 if (!cmd) {
1719 err = -ENOMEM;
eec8d2bc 1720 goto failed;
366a0336 1721 }
eec8d2bc 1722
8b064a3a 1723 if (cp->val) {
19202573 1724 queue_work(hdev->req_workqueue, &hdev->power_on);
8b064a3a
JH
1725 err = 0;
1726 } else {
1727 /* Disconnect connections, stop scans, etc */
1728 err = clean_up_hci_state(hdev);
a3172b7e
JH
1729 if (!err)
1730 queue_delayed_work(hdev->req_workqueue, &hdev->power_off,
1731 HCI_POWER_OFF_TIMEOUT);
eec8d2bc 1732
8b064a3a
JH
1733 /* ENODATA means there were no HCI commands queued */
1734 if (err == -ENODATA) {
a3172b7e 1735 cancel_delayed_work(&hdev->power_off);
8b064a3a
JH
1736 queue_work(hdev->req_workqueue, &hdev->power_off.work);
1737 err = 0;
1738 }
1739 }
eec8d2bc
JH
1740
1741failed:
09fd0de5 1742 hci_dev_unlock(hdev);
366a0336 1743 return err;
eec8d2bc
JH
1744}
1745
beadb2bd
JH
1746static int new_settings(struct hci_dev *hdev, struct sock *skip)
1747{
f6b7712e 1748 __le32 ev = cpu_to_le32(get_current_settings(hdev));
beadb2bd 1749
f6b7712e
MH
1750 return mgmt_generic_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1751 sizeof(ev), skip);
beadb2bd
JH
1752}
1753
91a668b0
JH
1754int mgmt_new_settings(struct hci_dev *hdev)
1755{
1756 return new_settings(hdev, NULL);
1757}
1758
bd99abdd
JH
1759struct cmd_lookup {
1760 struct sock *sk;
1761 struct hci_dev *hdev;
1762 u8 mgmt_status;
1763};
1764
3b0602cd 1765static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
bd99abdd
JH
1766{
1767 struct cmd_lookup *match = data;
1768
1769 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1770
1771 list_del(&cmd->list);
1772
1773 if (match->sk == NULL) {
1774 match->sk = cmd->sk;
1775 sock_hold(match->sk);
1776 }
1777
1778 mgmt_pending_free(cmd);
1779}
1780
3b0602cd 1781static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
bd99abdd
JH
1782{
1783 u8 *status = data;
1784
a69e8375 1785 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
bd99abdd
JH
1786 mgmt_pending_remove(cmd);
1787}
1788
3b0602cd 1789static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
1b9b5ee5
JH
1790{
1791 if (cmd->cmd_complete) {
1792 u8 *status = data;
1793
1794 cmd->cmd_complete(cmd, *status);
1795 mgmt_pending_remove(cmd);
1796
1797 return;
1798 }
1799
1800 cmd_status_rsp(cmd, data);
1801}
1802
3b0602cd 1803static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
f5818c22 1804{
2a1afb5a
JH
1805 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1806 cmd->param, cmd->param_len);
f5818c22
JH
1807}
1808
3b0602cd 1809static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
7776d1d8 1810{
2a1afb5a
JH
1811 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1812 cmd->param, sizeof(struct mgmt_addr_info));
7776d1d8
JH
1813}
1814
e6fe7986
JH
1815static u8 mgmt_bredr_support(struct hci_dev *hdev)
1816{
1817 if (!lmp_bredr_capable(hdev))
1818 return MGMT_STATUS_NOT_SUPPORTED;
d7a5a11d 1819 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
e6fe7986
JH
1820 return MGMT_STATUS_REJECTED;
1821 else
1822 return MGMT_STATUS_SUCCESS;
1823}
1824
1825static u8 mgmt_le_support(struct hci_dev *hdev)
1826{
1827 if (!lmp_le_capable(hdev))
1828 return MGMT_STATUS_NOT_SUPPORTED;
d7a5a11d 1829 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
e6fe7986
JH
1830 return MGMT_STATUS_REJECTED;
1831 else
1832 return MGMT_STATUS_SUCCESS;
1833}
1834
1904a853
MH
1835static void set_discoverable_complete(struct hci_dev *hdev, u8 status,
1836 u16 opcode)
bfaf8c9f 1837{
3b0602cd 1838 struct mgmt_pending_cmd *cmd;
bfaf8c9f 1839 struct mgmt_mode *cp;
970ba524 1840 struct hci_request req;
bfaf8c9f
JH
1841 bool changed;
1842
1843 BT_DBG("status 0x%02x", status);
1844
1845 hci_dev_lock(hdev);
1846
333ae95d 1847 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
bfaf8c9f
JH
1848 if (!cmd)
1849 goto unlock;
1850
1851 if (status) {
1852 u8 mgmt_err = mgmt_status(status);
a69e8375 1853 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
a358dc11 1854 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
bfaf8c9f
JH
1855 goto remove_cmd;
1856 }
1857
1858 cp = cmd->param;
d4462a07 1859 if (cp->val) {
238be788 1860 changed = !hci_dev_test_and_set_flag(hdev, HCI_DISCOVERABLE);
d4462a07
MH
1861
1862 if (hdev->discov_timeout > 0) {
1863 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1864 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
1865 to);
1866 }
1867 } else {
a69d8927 1868 changed = hci_dev_test_and_clear_flag(hdev, HCI_DISCOVERABLE);
d4462a07 1869 }
bfaf8c9f
JH
1870
1871 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1872
1873 if (changed)
1874 new_settings(hdev, cmd->sk);
1875
970ba524
MH
1876 /* When the discoverable mode gets changed, make sure
1877 * that class of device has the limited discoverable
432df05e
JH
1878 * bit correctly set. Also update page scan based on whitelist
1879 * entries.
970ba524
MH
1880 */
1881 hci_req_init(&req, hdev);
1d2dc5b7 1882 __hci_update_page_scan(&req);
970ba524
MH
1883 update_class(&req);
1884 hci_req_run(&req, NULL);
1885
bfaf8c9f
JH
1886remove_cmd:
1887 mgmt_pending_remove(cmd);
1888
1889unlock:
1890 hci_dev_unlock(hdev);
1891}
1892
bdb6d971 1893static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 1894 u16 len)
73f22f62 1895{
650f726d 1896 struct mgmt_cp_set_discoverable *cp = data;
3b0602cd 1897 struct mgmt_pending_cmd *cmd;
bfaf8c9f 1898 struct hci_request req;
5e5282bb 1899 u16 timeout;
9a43e25f 1900 u8 scan;
73f22f62
JH
1901 int err;
1902
bdb6d971 1903 BT_DBG("request for %s", hdev->name);
73f22f62 1904
d7a5a11d
MH
1905 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1906 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
a69e8375
JH
1907 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1908 MGMT_STATUS_REJECTED);
33c525c0 1909
310a3d48 1910 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
a69e8375
JH
1911 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1912 MGMT_STATUS_INVALID_PARAMS);
a7e80f25 1913
1f350c87 1914 timeout = __le16_to_cpu(cp->timeout);
310a3d48
MH
1915
1916 /* Disabling discoverable requires that no timeout is set,
1917 * and enabling limited discoverable requires a timeout.
1918 */
1919 if ((cp->val == 0x00 && timeout > 0) ||
1920 (cp->val == 0x02 && timeout == 0))
a69e8375
JH
1921 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1922 MGMT_STATUS_INVALID_PARAMS);
73f22f62 1923
09fd0de5 1924 hci_dev_lock(hdev);
73f22f62 1925
5e5282bb 1926 if (!hdev_is_powered(hdev) && timeout > 0) {
a69e8375
JH
1927 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1928 MGMT_STATUS_NOT_POWERED);
73f22f62
JH
1929 goto failed;
1930 }
1931
333ae95d
JH
1932 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1933 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
a69e8375
JH
1934 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1935 MGMT_STATUS_BUSY);
73f22f62
JH
1936 goto failed;
1937 }
1938
d7a5a11d 1939 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
a69e8375
JH
1940 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1941 MGMT_STATUS_REJECTED);
5e5282bb
JH
1942 goto failed;
1943 }
1944
1945 if (!hdev_is_powered(hdev)) {
0224d2fa
JH
1946 bool changed = false;
1947
310a3d48
MH
1948 /* Setting limited discoverable when powered off is
1949 * not a valid operation since it requires a timeout
1950 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1951 */
d7a5a11d 1952 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
ce05d603 1953 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
0224d2fa
JH
1954 changed = true;
1955 }
1956
5e5282bb 1957 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
0224d2fa
JH
1958 if (err < 0)
1959 goto failed;
1960
1961 if (changed)
1962 err = new_settings(hdev, sk);
1963
5e5282bb
JH
1964 goto failed;
1965 }
1966
310a3d48
MH
1967 /* If the current mode is the same, then just update the timeout
1968 * value with the new value. And if only the timeout gets updated,
1969 * then no need for any HCI transactions.
1970 */
d7a5a11d
MH
1971 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1972 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1973 HCI_LIMITED_DISCOVERABLE)) {
36261547
MH
1974 cancel_delayed_work(&hdev->discov_off);
1975 hdev->discov_timeout = timeout;
955638ec 1976
36261547
MH
1977 if (cp->val && hdev->discov_timeout > 0) {
1978 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
955638ec 1979 queue_delayed_work(hdev->workqueue, &hdev->discov_off,
36261547 1980 to);
955638ec
MH
1981 }
1982
69ab39ea 1983 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
73f22f62
JH
1984 goto failed;
1985 }
1986
2e58ef3e 1987 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
366a0336
JH
1988 if (!cmd) {
1989 err = -ENOMEM;
73f22f62 1990 goto failed;
366a0336 1991 }
73f22f62 1992
310a3d48
MH
1993 /* Cancel any potential discoverable timeout that might be
1994 * still active and store new timeout value. The arming of
1995 * the timeout happens in the complete handler.
1996 */
1997 cancel_delayed_work(&hdev->discov_off);
1998 hdev->discov_timeout = timeout;
1999
b456f87c
JH
2000 /* Limited discoverable mode */
2001 if (cp->val == 0x02)
a1536da2 2002 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
b456f87c 2003 else
a358dc11 2004 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
b456f87c 2005
bfaf8c9f
JH
2006 hci_req_init(&req, hdev);
2007
9a43e25f
JH
2008 /* The procedure for LE-only controllers is much simpler - just
2009 * update the advertising data.
2010 */
d7a5a11d 2011 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
9a43e25f
JH
2012 goto update_ad;
2013
73f22f62
JH
2014 scan = SCAN_PAGE;
2015
310a3d48
MH
2016 if (cp->val) {
2017 struct hci_cp_write_current_iac_lap hci_cp;
2018
2019 if (cp->val == 0x02) {
2020 /* Limited discoverable mode */
33337dcb 2021 hci_cp.num_iac = min_t(u8, hdev->num_iac, 2);
310a3d48
MH
2022 hci_cp.iac_lap[0] = 0x00; /* LIAC */
2023 hci_cp.iac_lap[1] = 0x8b;
2024 hci_cp.iac_lap[2] = 0x9e;
2025 hci_cp.iac_lap[3] = 0x33; /* GIAC */
2026 hci_cp.iac_lap[4] = 0x8b;
2027 hci_cp.iac_lap[5] = 0x9e;
2028 } else {
2029 /* General discoverable mode */
310a3d48
MH
2030 hci_cp.num_iac = 1;
2031 hci_cp.iac_lap[0] = 0x33; /* GIAC */
2032 hci_cp.iac_lap[1] = 0x8b;
2033 hci_cp.iac_lap[2] = 0x9e;
2034 }
2035
2036 hci_req_add(&req, HCI_OP_WRITE_CURRENT_IAC_LAP,
2037 (hci_cp.num_iac * 3) + 1, &hci_cp);
2038
73f22f62 2039 scan |= SCAN_INQUIRY;
310a3d48 2040 } else {
a358dc11 2041 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
310a3d48 2042 }
73f22f62 2043
310a3d48 2044 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, sizeof(scan), &scan);
bfaf8c9f 2045
9a43e25f
JH
2046update_ad:
2047 update_adv_data(&req);
2048
bfaf8c9f 2049 err = hci_req_run(&req, set_discoverable_complete);
73f22f62 2050 if (err < 0)
a664b5bc 2051 mgmt_pending_remove(cmd);
73f22f62
JH
2052
2053failed:
09fd0de5 2054 hci_dev_unlock(hdev);
73f22f62
JH
2055 return err;
2056}
2057
406d7804
JH
2058static void write_fast_connectable(struct hci_request *req, bool enable)
2059{
bd98b996 2060 struct hci_dev *hdev = req->hdev;
406d7804
JH
2061 struct hci_cp_write_page_scan_activity acp;
2062 u8 type;
2063
d7a5a11d 2064 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
547003b1
JH
2065 return;
2066
4c01f8b8
JH
2067 if (hdev->hci_ver < BLUETOOTH_VER_1_2)
2068 return;
2069
406d7804
JH
2070 if (enable) {
2071 type = PAGE_SCAN_TYPE_INTERLACED;
2072
2073 /* 160 msec page scan interval */
dcf4adbf 2074 acp.interval = cpu_to_le16(0x0100);
406d7804
JH
2075 } else {
2076 type = PAGE_SCAN_TYPE_STANDARD; /* default */
2077
2078 /* default 1.28 sec page scan */
dcf4adbf 2079 acp.interval = cpu_to_le16(0x0800);
406d7804
JH
2080 }
2081
dcf4adbf 2082 acp.window = cpu_to_le16(0x0012);
406d7804 2083
bd98b996
JH
2084 if (__cpu_to_le16(hdev->page_scan_interval) != acp.interval ||
2085 __cpu_to_le16(hdev->page_scan_window) != acp.window)
2086 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY,
2087 sizeof(acp), &acp);
2088
2089 if (hdev->page_scan_type != type)
2090 hci_req_add(req, HCI_OP_WRITE_PAGE_SCAN_TYPE, 1, &type);
406d7804
JH
2091}
2092
1904a853
MH
2093static void set_connectable_complete(struct hci_dev *hdev, u8 status,
2094 u16 opcode)
2b76f453 2095{
3b0602cd 2096 struct mgmt_pending_cmd *cmd;
d7b856f9 2097 struct mgmt_mode *cp;
bc6d2d04 2098 bool conn_changed, discov_changed;
2b76f453
JH
2099
2100 BT_DBG("status 0x%02x", status);
2101
2102 hci_dev_lock(hdev);
2103
333ae95d 2104 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
2b76f453
JH
2105 if (!cmd)
2106 goto unlock;
2107
37438c1f
JH
2108 if (status) {
2109 u8 mgmt_err = mgmt_status(status);
a69e8375 2110 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
37438c1f
JH
2111 goto remove_cmd;
2112 }
2113
d7b856f9 2114 cp = cmd->param;
bc6d2d04 2115 if (cp->val) {
238be788
MH
2116 conn_changed = !hci_dev_test_and_set_flag(hdev,
2117 HCI_CONNECTABLE);
bc6d2d04
JH
2118 discov_changed = false;
2119 } else {
a69d8927
MH
2120 conn_changed = hci_dev_test_and_clear_flag(hdev,
2121 HCI_CONNECTABLE);
2122 discov_changed = hci_dev_test_and_clear_flag(hdev,
2123 HCI_DISCOVERABLE);
bc6d2d04 2124 }
d7b856f9 2125
2b76f453
JH
2126 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
2127
bc6d2d04 2128 if (conn_changed || discov_changed) {
d7b856f9 2129 new_settings(hdev, cmd->sk);
1d2dc5b7 2130 hci_update_page_scan(hdev);
bc6d2d04
JH
2131 if (discov_changed)
2132 mgmt_update_adv_data(hdev);
2b7be33e
JH
2133 hci_update_background_scan(hdev);
2134 }
d7b856f9 2135
37438c1f 2136remove_cmd:
2b76f453
JH
2137 mgmt_pending_remove(cmd);
2138
2139unlock:
2140 hci_dev_unlock(hdev);
2141}
2142
e8ba3a1f
JH
2143static int set_connectable_update_settings(struct hci_dev *hdev,
2144 struct sock *sk, u8 val)
2145{
2146 bool changed = false;
2147 int err;
2148
d7a5a11d 2149 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
e8ba3a1f
JH
2150 changed = true;
2151
2152 if (val) {
a1536da2 2153 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
e8ba3a1f 2154 } else {
a358dc11
MH
2155 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
2156 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
e8ba3a1f
JH
2157 }
2158
2159 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
2160 if (err < 0)
2161 return err;
2162
562064e6 2163 if (changed) {
1d2dc5b7 2164 hci_update_page_scan(hdev);
562064e6 2165 hci_update_background_scan(hdev);
e8ba3a1f 2166 return new_settings(hdev, sk);
562064e6 2167 }
e8ba3a1f
JH
2168
2169 return 0;
2170}
2171
bdb6d971 2172static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 2173 u16 len)
9fbcbb45 2174{
650f726d 2175 struct mgmt_mode *cp = data;
3b0602cd 2176 struct mgmt_pending_cmd *cmd;
2b76f453 2177 struct hci_request req;
1987fdc7 2178 u8 scan;
9fbcbb45
JH
2179 int err;
2180
bdb6d971 2181 BT_DBG("request for %s", hdev->name);
9fbcbb45 2182
d7a5a11d
MH
2183 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
2184 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
a69e8375
JH
2185 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2186 MGMT_STATUS_REJECTED);
33c525c0 2187
a7e80f25 2188 if (cp->val != 0x00 && cp->val != 0x01)
a69e8375
JH
2189 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2190 MGMT_STATUS_INVALID_PARAMS);
a7e80f25 2191
09fd0de5 2192 hci_dev_lock(hdev);
9fbcbb45 2193
4b34ee78 2194 if (!hdev_is_powered(hdev)) {
e8ba3a1f 2195 err = set_connectable_update_settings(hdev, sk, cp->val);
9fbcbb45
JH
2196 goto failed;
2197 }
2198
333ae95d
JH
2199 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
2200 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
a69e8375
JH
2201 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
2202 MGMT_STATUS_BUSY);
9fbcbb45
JH
2203 goto failed;
2204 }
2205
2e58ef3e 2206 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
366a0336
JH
2207 if (!cmd) {
2208 err = -ENOMEM;
9fbcbb45 2209 goto failed;
366a0336 2210 }
9fbcbb45 2211
9b74246f 2212 hci_req_init(&req, hdev);
9fbcbb45 2213
9a43e25f
JH
2214 /* If BR/EDR is not enabled and we disable advertising as a
2215 * by-product of disabling connectable, we need to update the
2216 * advertising flags.
2217 */
d7a5a11d 2218 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
9a43e25f 2219 if (!cp->val) {
a358dc11
MH
2220 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
2221 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
9a43e25f
JH
2222 }
2223 update_adv_data(&req);
2224 } else if (cp->val != test_bit(HCI_PSCAN, &hdev->flags)) {
9b74246f
JH
2225 if (cp->val) {
2226 scan = SCAN_PAGE;
2227 } else {
3bd27240
JH
2228 /* If we don't have any whitelist entries just
2229 * disable all scanning. If there are entries
2230 * and we had both page and inquiry scanning
2231 * enabled then fall back to only page scanning.
2232 * Otherwise no changes are needed.
2233 */
2234 if (list_empty(&hdev->whitelist))
2235 scan = SCAN_DISABLED;
2236 else if (test_bit(HCI_ISCAN, &hdev->flags))
2237 scan = SCAN_PAGE;
2238 else
2239 goto no_scan_update;
9b74246f
JH
2240
2241 if (test_bit(HCI_ISCAN, &hdev->flags) &&
8d6083fe 2242 hdev->discov_timeout > 0)
9b74246f
JH
2243 cancel_delayed_work(&hdev->discov_off);
2244 }
2b76f453 2245
9b74246f
JH
2246 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
2247 }
2b76f453 2248
3bd27240 2249no_scan_update:
e8b1202c 2250 /* Update the advertising parameters if necessary */
880897d4
AU
2251 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
2252 hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
1987fdc7 2253 enable_advertising(&req);
1987fdc7 2254
2b76f453 2255 err = hci_req_run(&req, set_connectable_complete);
9b74246f 2256 if (err < 0) {
a664b5bc 2257 mgmt_pending_remove(cmd);
9b74246f 2258 if (err == -ENODATA)
a81070ba
JH
2259 err = set_connectable_update_settings(hdev, sk,
2260 cp->val);
9b74246f
JH
2261 goto failed;
2262 }
9fbcbb45
JH
2263
2264failed:
09fd0de5 2265 hci_dev_unlock(hdev);
9fbcbb45
JH
2266 return err;
2267}
2268
b2939475 2269static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 2270 u16 len)
c542a06c 2271{
650f726d 2272 struct mgmt_mode *cp = data;
55594356 2273 bool changed;
c542a06c
JH
2274 int err;
2275
bdb6d971 2276 BT_DBG("request for %s", hdev->name);
c542a06c 2277
a7e80f25 2278 if (cp->val != 0x00 && cp->val != 0x01)
a69e8375
JH
2279 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
2280 MGMT_STATUS_INVALID_PARAMS);
a7e80f25 2281
09fd0de5 2282 hci_dev_lock(hdev);
c542a06c
JH
2283
2284 if (cp->val)
238be788 2285 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
c542a06c 2286 else
a69d8927 2287 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
c542a06c 2288
b2939475 2289 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
c542a06c 2290 if (err < 0)
55594356 2291 goto unlock;
c542a06c 2292
55594356
MH
2293 if (changed)
2294 err = new_settings(hdev, sk);
c542a06c 2295
55594356 2296unlock:
09fd0de5 2297 hci_dev_unlock(hdev);
c542a06c
JH
2298 return err;
2299}
2300
04124681
GP
2301static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
2302 u16 len)
33ef95ed
JH
2303{
2304 struct mgmt_mode *cp = data;
3b0602cd 2305 struct mgmt_pending_cmd *cmd;
e6fe7986 2306 u8 val, status;
33ef95ed
JH
2307 int err;
2308
bdb6d971 2309 BT_DBG("request for %s", hdev->name);
33ef95ed 2310
e6fe7986
JH
2311 status = mgmt_bredr_support(hdev);
2312 if (status)
a69e8375
JH
2313 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2314 status);
33c525c0 2315
a7e80f25 2316 if (cp->val != 0x00 && cp->val != 0x01)
a69e8375
JH
2317 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2318 MGMT_STATUS_INVALID_PARAMS);
a7e80f25 2319
33ef95ed
JH
2320 hci_dev_lock(hdev);
2321
4b34ee78 2322 if (!hdev_is_powered(hdev)) {
47990ea0
JH
2323 bool changed = false;
2324
d7a5a11d 2325 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
ce05d603 2326 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
47990ea0
JH
2327 changed = true;
2328 }
2329
2330 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2331 if (err < 0)
2332 goto failed;
2333
2334 if (changed)
2335 err = new_settings(hdev, sk);
2336
33ef95ed
JH
2337 goto failed;
2338 }
2339
333ae95d 2340 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
a69e8375
JH
2341 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
2342 MGMT_STATUS_BUSY);
33ef95ed
JH
2343 goto failed;
2344 }
2345
2346 val = !!cp->val;
2347
2348 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
2349 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
2350 goto failed;
2351 }
2352
2353 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
2354 if (!cmd) {
2355 err = -ENOMEM;
2356 goto failed;
2357 }
2358
2359 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
2360 if (err < 0) {
2361 mgmt_pending_remove(cmd);
2362 goto failed;
2363 }
2364
2365failed:
2366 hci_dev_unlock(hdev);
33ef95ed
JH
2367 return err;
2368}
2369
bdb6d971 2370static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
ed2c4ee3
JH
2371{
2372 struct mgmt_mode *cp = data;
3b0602cd 2373 struct mgmt_pending_cmd *cmd;
72ef0c1a 2374 u8 status;
ed2c4ee3
JH
2375 int err;
2376
bdb6d971 2377 BT_DBG("request for %s", hdev->name);
ed2c4ee3 2378
cdba5281
MH
2379 status = mgmt_bredr_support(hdev);
2380 if (status)
a69e8375 2381 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
cdba5281 2382
13ecd8b6 2383 if (!lmp_ssp_capable(hdev))
a69e8375
JH
2384 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2385 MGMT_STATUS_NOT_SUPPORTED);
ed2c4ee3 2386
a7e80f25 2387 if (cp->val != 0x00 && cp->val != 0x01)
a69e8375
JH
2388 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2389 MGMT_STATUS_INVALID_PARAMS);
a7e80f25 2390
13ecd8b6 2391 hci_dev_lock(hdev);
6c8f12c1 2392
4b34ee78 2393 if (!hdev_is_powered(hdev)) {
9ecb3e24 2394 bool changed;
c0ecddc2 2395
9ecb3e24 2396 if (cp->val) {
238be788
MH
2397 changed = !hci_dev_test_and_set_flag(hdev,
2398 HCI_SSP_ENABLED);
9ecb3e24 2399 } else {
a69d8927
MH
2400 changed = hci_dev_test_and_clear_flag(hdev,
2401 HCI_SSP_ENABLED);
9ecb3e24 2402 if (!changed)
a69d8927
MH
2403 changed = hci_dev_test_and_clear_flag(hdev,
2404 HCI_HS_ENABLED);
9ecb3e24 2405 else
a358dc11 2406 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
c0ecddc2
JH
2407 }
2408
2409 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2410 if (err < 0)
2411 goto failed;
2412
2413 if (changed)
2414 err = new_settings(hdev, sk);
2415
ed2c4ee3
JH
2416 goto failed;
2417 }
2418
333ae95d 2419 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
a69e8375
JH
2420 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2421 MGMT_STATUS_BUSY);
ed2c4ee3
JH
2422 goto failed;
2423 }
2424
d7a5a11d 2425 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
ed2c4ee3
JH
2426 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2427 goto failed;
2428 }
2429
2430 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
2431 if (!cmd) {
2432 err = -ENOMEM;
2433 goto failed;
2434 }
2435
d7a5a11d 2436 if (!cp->val && hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
3769972b
JH
2437 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
2438 sizeof(cp->val), &cp->val);
2439
72ef0c1a 2440 err = hci_send_cmd(hdev, HCI_OP_WRITE_SSP_MODE, 1, &cp->val);
ed2c4ee3
JH
2441 if (err < 0) {
2442 mgmt_pending_remove(cmd);
2443 goto failed;
2444 }
2445
2446failed:
2447 hci_dev_unlock(hdev);
ed2c4ee3
JH
2448 return err;
2449}
2450
bdb6d971 2451static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
6d80dfd0
JH
2452{
2453 struct mgmt_mode *cp = data;
ee392693 2454 bool changed;
e6fe7986 2455 u8 status;
ee392693 2456 int err;
6d80dfd0 2457
bdb6d971 2458 BT_DBG("request for %s", hdev->name);
6d80dfd0 2459
e6fe7986
JH
2460 status = mgmt_bredr_support(hdev);
2461 if (status)
a69e8375 2462 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
6d80dfd0 2463
9ecb3e24 2464 if (!lmp_ssp_capable(hdev))
a69e8375
JH
2465 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2466 MGMT_STATUS_NOT_SUPPORTED);
9ecb3e24 2467
d7a5a11d 2468 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
a69e8375
JH
2469 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2470 MGMT_STATUS_REJECTED);
9ecb3e24 2471
a7e80f25 2472 if (cp->val != 0x00 && cp->val != 0x01)
a69e8375
JH
2473 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2474 MGMT_STATUS_INVALID_PARAMS);
a7e80f25 2475
ee392693
MH
2476 hci_dev_lock(hdev);
2477
333ae95d 2478 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
a69e8375
JH
2479 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2480 MGMT_STATUS_BUSY);
a2cb01de
JH
2481 goto unlock;
2482 }
2483
a0cdf960 2484 if (cp->val) {
238be788 2485 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
a0cdf960
MH
2486 } else {
2487 if (hdev_is_powered(hdev)) {
a69e8375
JH
2488 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2489 MGMT_STATUS_REJECTED);
a0cdf960
MH
2490 goto unlock;
2491 }
2492
a69d8927 2493 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
a0cdf960 2494 }
ee392693
MH
2495
2496 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
2497 if (err < 0)
2498 goto unlock;
2499
2500 if (changed)
2501 err = new_settings(hdev, sk);
6d80dfd0 2502
ee392693
MH
2503unlock:
2504 hci_dev_unlock(hdev);
2505 return err;
6d80dfd0
JH
2506}
2507
1904a853 2508static void le_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
416a4ae5
JH
2509{
2510 struct cmd_lookup match = { NULL, hdev };
2511
3ad67582
JK
2512 hci_dev_lock(hdev);
2513
416a4ae5
JH
2514 if (status) {
2515 u8 mgmt_err = mgmt_status(status);
2516
2517 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
2518 &mgmt_err);
3ad67582 2519 goto unlock;
416a4ae5
JH
2520 }
2521
2522 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
2523
2524 new_settings(hdev, match.sk);
2525
2526 if (match.sk)
2527 sock_put(match.sk);
441ad2d0
MH
2528
2529 /* Make sure the controller has a good default for
2530 * advertising data. Restrict the update to when LE
2531 * has actually been enabled. During power on, the
2532 * update in powered_update_hci will take care of it.
2533 */
d7a5a11d 2534 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
441ad2d0
MH
2535 struct hci_request req;
2536
441ad2d0 2537 hci_req_init(&req, hdev);
5947f4bc 2538 update_adv_data(&req);
f14d8f64 2539 update_scan_rsp_data(&req);
2cf22218 2540 __hci_update_background_scan(&req);
441ad2d0 2541 hci_req_run(&req, NULL);
441ad2d0 2542 }
3ad67582
JK
2543
2544unlock:
2545 hci_dev_unlock(hdev);
416a4ae5
JH
2546}
2547
bdb6d971 2548static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
06199cf8
JH
2549{
2550 struct mgmt_mode *cp = data;
2551 struct hci_cp_write_le_host_supported hci_cp;
3b0602cd 2552 struct mgmt_pending_cmd *cmd;
416a4ae5 2553 struct hci_request req;
06199cf8 2554 int err;
0b60eba1 2555 u8 val, enabled;
06199cf8 2556
bdb6d971 2557 BT_DBG("request for %s", hdev->name);
06199cf8 2558
13ecd8b6 2559 if (!lmp_le_capable(hdev))
a69e8375
JH
2560 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2561 MGMT_STATUS_NOT_SUPPORTED);
1de028ce 2562
a7e80f25 2563 if (cp->val != 0x00 && cp->val != 0x01)
a69e8375
JH
2564 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2565 MGMT_STATUS_INVALID_PARAMS);
a7e80f25 2566
e7844ee5
MH
2567 /* Bluetooth single mode LE only controllers or dual-mode
2568 * controllers configured as LE only devices, do not allow
2569 * switching LE off. These have either LE enabled explicitly
2570 * or BR/EDR has been previously switched off.
2571 *
2572 * When trying to enable an already enabled LE, then gracefully
2573 * send a positive response. Trying to disable it however will
2574 * result into rejection.
2575 */
2576 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
2577 if (cp->val == 0x01)
2578 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2579
a69e8375
JH
2580 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2581 MGMT_STATUS_REJECTED);
e7844ee5 2582 }
c73eee91 2583
13ecd8b6 2584 hci_dev_lock(hdev);
06199cf8
JH
2585
2586 val = !!cp->val;
ffa88e02 2587 enabled = lmp_host_le_capable(hdev);
06199cf8 2588
847818d9
FG
2589 if (!val)
2590 clear_adv_instance(hdev, NULL, 0x00, true);
2591
0b60eba1 2592 if (!hdev_is_powered(hdev) || val == enabled) {
06199cf8
JH
2593 bool changed = false;
2594
d7a5a11d 2595 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
ce05d603 2596 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
06199cf8
JH
2597 changed = true;
2598 }
2599
d7a5a11d 2600 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
a358dc11 2601 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
eeca6f89
JH
2602 changed = true;
2603 }
2604
06199cf8
JH
2605 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2606 if (err < 0)
1de028ce 2607 goto unlock;
06199cf8
JH
2608
2609 if (changed)
2610 err = new_settings(hdev, sk);
2611
1de028ce 2612 goto unlock;
06199cf8
JH
2613 }
2614
333ae95d
JH
2615 if (pending_find(MGMT_OP_SET_LE, hdev) ||
2616 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
a69e8375
JH
2617 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2618 MGMT_STATUS_BUSY);
1de028ce 2619 goto unlock;
06199cf8
JH
2620 }
2621
2622 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2623 if (!cmd) {
2624 err = -ENOMEM;
1de028ce 2625 goto unlock;
06199cf8
JH
2626 }
2627
441ad2d0
MH
2628 hci_req_init(&req, hdev);
2629
06199cf8
JH
2630 memset(&hci_cp, 0, sizeof(hci_cp));
2631
2632 if (val) {
2633 hci_cp.le = val;
32226e4f 2634 hci_cp.simul = 0x00;
441ad2d0 2635 } else {
d7a5a11d 2636 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
441ad2d0 2637 disable_advertising(&req);
06199cf8
JH
2638 }
2639
416a4ae5
JH
2640 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED, sizeof(hci_cp),
2641 &hci_cp);
2642
2643 err = hci_req_run(&req, le_enable_complete);
0c01bc48 2644 if (err < 0)
06199cf8 2645 mgmt_pending_remove(cmd);
06199cf8 2646
1de028ce
JH
2647unlock:
2648 hci_dev_unlock(hdev);
06199cf8
JH
2649 return err;
2650}
2651
0cab9c80
JH
2652/* This is a helper function to test for pending mgmt commands that can
2653 * cause CoD or EIR HCI commands. We can only allow one such pending
2654 * mgmt command at a time since otherwise we cannot easily track what
2655 * the current values are, will be, and based on that calculate if a new
2656 * HCI command needs to be sent and if yes with what value.
2657 */
2658static bool pending_eir_or_class(struct hci_dev *hdev)
2659{
3b0602cd 2660 struct mgmt_pending_cmd *cmd;
0cab9c80
JH
2661
2662 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2663 switch (cmd->opcode) {
2664 case MGMT_OP_ADD_UUID:
2665 case MGMT_OP_REMOVE_UUID:
2666 case MGMT_OP_SET_DEV_CLASS:
2667 case MGMT_OP_SET_POWERED:
2668 return true;
2669 }
2670 }
2671
2672 return false;
2673}
2674
83be8eca
JH
2675static const u8 bluetooth_base_uuid[] = {
2676 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2677 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2678};
2679
2680static u8 get_uuid_size(const u8 *uuid)
2681{
2682 u32 val;
2683
2684 if (memcmp(uuid, bluetooth_base_uuid, 12))
2685 return 128;
2686
2687 val = get_unaligned_le32(&uuid[12]);
2688 if (val > 0xffff)
2689 return 32;
2690
2691 return 16;
2692}
2693
92da6097
JH
2694static void mgmt_class_complete(struct hci_dev *hdev, u16 mgmt_op, u8 status)
2695{
3b0602cd 2696 struct mgmt_pending_cmd *cmd;
92da6097
JH
2697
2698 hci_dev_lock(hdev);
2699
333ae95d 2700 cmd = pending_find(mgmt_op, hdev);
92da6097
JH
2701 if (!cmd)
2702 goto unlock;
2703
2a1afb5a
JH
2704 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
2705 mgmt_status(status), hdev->dev_class, 3);
92da6097
JH
2706
2707 mgmt_pending_remove(cmd);
2708
2709unlock:
2710 hci_dev_unlock(hdev);
2711}
2712
1904a853 2713static void add_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
92da6097
JH
2714{
2715 BT_DBG("status 0x%02x", status);
2716
2717 mgmt_class_complete(hdev, MGMT_OP_ADD_UUID, status);
2718}
2719
bdb6d971 2720static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2aeb9a1a 2721{
650f726d 2722 struct mgmt_cp_add_uuid *cp = data;
3b0602cd 2723 struct mgmt_pending_cmd *cmd;
890ea898 2724 struct hci_request req;
2aeb9a1a 2725 struct bt_uuid *uuid;
2aeb9a1a
JH
2726 int err;
2727
bdb6d971 2728 BT_DBG("request for %s", hdev->name);
2aeb9a1a 2729
09fd0de5 2730 hci_dev_lock(hdev);
2aeb9a1a 2731
0cab9c80 2732 if (pending_eir_or_class(hdev)) {
a69e8375
JH
2733 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2734 MGMT_STATUS_BUSY);
c95f0ba7
JH
2735 goto failed;
2736 }
2737
92c4c204 2738 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
2aeb9a1a
JH
2739 if (!uuid) {
2740 err = -ENOMEM;
2741 goto failed;
2742 }
2743
2744 memcpy(uuid->uuid, cp->uuid, 16);
1aff6f09 2745 uuid->svc_hint = cp->svc_hint;
83be8eca 2746 uuid->size = get_uuid_size(cp->uuid);
2aeb9a1a 2747
de66aa63 2748 list_add_tail(&uuid->list, &hdev->uuids);
2aeb9a1a 2749
890ea898 2750 hci_req_init(&req, hdev);
1aff6f09 2751
890ea898
JH
2752 update_class(&req);
2753 update_eir(&req);
2754
92da6097
JH
2755 err = hci_req_run(&req, add_uuid_complete);
2756 if (err < 0) {
2757 if (err != -ENODATA)
2758 goto failed;
80a1e1db 2759
2a1afb5a
JH
2760 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_UUID, 0,
2761 hdev->dev_class, 3);
90e70454
JH
2762 goto failed;
2763 }
2764
2765 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_UUID, hdev, data, len);
890ea898 2766 if (!cmd) {
90e70454 2767 err = -ENOMEM;
890ea898
JH
2768 goto failed;
2769 }
2770
2771 err = 0;
2aeb9a1a
JH
2772
2773failed:
09fd0de5 2774 hci_dev_unlock(hdev);
2aeb9a1a
JH
2775 return err;
2776}
2777
24b78d0f
JH
2778static bool enable_service_cache(struct hci_dev *hdev)
2779{
2780 if (!hdev_is_powered(hdev))
2781 return false;
2782
238be788 2783 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
46818ed5
JH
2784 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2785 CACHE_TIMEOUT);
24b78d0f
JH
2786 return true;
2787 }
2788
2789 return false;
2790}
2791
1904a853 2792static void remove_uuid_complete(struct hci_dev *hdev, u8 status, u16 opcode)
92da6097
JH
2793{
2794 BT_DBG("status 0x%02x", status);
2795
2796 mgmt_class_complete(hdev, MGMT_OP_REMOVE_UUID, status);
2797}
2798
bdb6d971 2799static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
8ce8e2b5 2800 u16 len)
2aeb9a1a 2801{
650f726d 2802 struct mgmt_cp_remove_uuid *cp = data;
3b0602cd 2803 struct mgmt_pending_cmd *cmd;
056341c8 2804 struct bt_uuid *match, *tmp;
2aeb9a1a 2805 u8 bt_uuid_any[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
890ea898 2806 struct hci_request req;
2aeb9a1a
JH
2807 int err, found;
2808
bdb6d971 2809 BT_DBG("request for %s", hdev->name);
2aeb9a1a 2810
09fd0de5 2811 hci_dev_lock(hdev);
2aeb9a1a 2812
0cab9c80 2813 if (pending_eir_or_class(hdev)) {
a69e8375
JH
2814 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2815 MGMT_STATUS_BUSY);
c95f0ba7
JH
2816 goto unlock;
2817 }
2818
2aeb9a1a 2819 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
35f7498a 2820 hci_uuids_clear(hdev);
4004b6d9 2821
24b78d0f 2822 if (enable_service_cache(hdev)) {
2a1afb5a
JH
2823 err = mgmt_cmd_complete(sk, hdev->id,
2824 MGMT_OP_REMOVE_UUID,
2825 0, hdev->dev_class, 3);
24b78d0f
JH
2826 goto unlock;
2827 }
4004b6d9 2828
9246a869 2829 goto update_class;
2aeb9a1a
JH
2830 }
2831
2832 found = 0;
2833
056341c8 2834 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
2aeb9a1a
JH
2835 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2836 continue;
2837
2838 list_del(&match->list);
482049f7 2839 kfree(match);
2aeb9a1a
JH
2840 found++;
2841 }
2842
2843 if (found == 0) {
a69e8375
JH
2844 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2845 MGMT_STATUS_INVALID_PARAMS);
2aeb9a1a
JH
2846 goto unlock;
2847 }
2848
9246a869 2849update_class:
890ea898 2850 hci_req_init(&req, hdev);
1aff6f09 2851
890ea898
JH
2852 update_class(&req);
2853 update_eir(&req);
2854
92da6097
JH
2855 err = hci_req_run(&req, remove_uuid_complete);
2856 if (err < 0) {
2857 if (err != -ENODATA)
2858 goto unlock;
80a1e1db 2859
2a1afb5a
JH
2860 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_UUID, 0,
2861 hdev->dev_class, 3);
90e70454
JH
2862 goto unlock;
2863 }
2864
2865 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
890ea898 2866 if (!cmd) {
90e70454 2867 err = -ENOMEM;
890ea898
JH
2868 goto unlock;
2869 }
2870
2871 err = 0;
2aeb9a1a
JH
2872
2873unlock:
09fd0de5 2874 hci_dev_unlock(hdev);
2aeb9a1a
JH
2875 return err;
2876}
2877
1904a853 2878static void set_class_complete(struct hci_dev *hdev, u8 status, u16 opcode)
92da6097
JH
2879{
2880 BT_DBG("status 0x%02x", status);
2881
2882 mgmt_class_complete(hdev, MGMT_OP_SET_DEV_CLASS, status);
2883}
2884
bdb6d971 2885static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 2886 u16 len)
1aff6f09 2887{
650f726d 2888 struct mgmt_cp_set_dev_class *cp = data;
3b0602cd 2889 struct mgmt_pending_cmd *cmd;
890ea898 2890 struct hci_request req;
1aff6f09
JH
2891 int err;
2892
bdb6d971 2893 BT_DBG("request for %s", hdev->name);
1aff6f09 2894
6203fc98 2895 if (!lmp_bredr_capable(hdev))
a69e8375
JH
2896 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2897 MGMT_STATUS_NOT_SUPPORTED);
1aff6f09 2898
0cab9c80 2899 hci_dev_lock(hdev);
ee98f473 2900
0cab9c80 2901 if (pending_eir_or_class(hdev)) {
a69e8375
JH
2902 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2903 MGMT_STATUS_BUSY);
0cab9c80
JH
2904 goto unlock;
2905 }
c95f0ba7 2906
0cab9c80 2907 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
a69e8375
JH
2908 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2909 MGMT_STATUS_INVALID_PARAMS);
0cab9c80
JH
2910 goto unlock;
2911 }
575b3a02 2912
932f5ff5
JH
2913 hdev->major_class = cp->major;
2914 hdev->minor_class = cp->minor;
2915
b5235a65 2916 if (!hdev_is_powered(hdev)) {
2a1afb5a
JH
2917 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2918 hdev->dev_class, 3);
b5235a65
JH
2919 goto unlock;
2920 }
2921
890ea898
JH
2922 hci_req_init(&req, hdev);
2923
a69d8927 2924 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
7d78525d
JH
2925 hci_dev_unlock(hdev);
2926 cancel_delayed_work_sync(&hdev->service_cache);
2927 hci_dev_lock(hdev);
890ea898 2928 update_eir(&req);
7d78525d 2929 }
14c0b608 2930
890ea898
JH
2931 update_class(&req);
2932
92da6097
JH
2933 err = hci_req_run(&req, set_class_complete);
2934 if (err < 0) {
2935 if (err != -ENODATA)
2936 goto unlock;
1aff6f09 2937
2a1afb5a
JH
2938 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2939 hdev->dev_class, 3);
90e70454
JH
2940 goto unlock;
2941 }
2942
2943 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
890ea898 2944 if (!cmd) {
90e70454 2945 err = -ENOMEM;
890ea898
JH
2946 goto unlock;
2947 }
2948
2949 err = 0;
1aff6f09 2950
b5235a65 2951unlock:
09fd0de5 2952 hci_dev_unlock(hdev);
1aff6f09
JH
2953 return err;
2954}
2955
bdb6d971 2956static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
8ce8e2b5 2957 u16 len)
55ed8ca1 2958{
650f726d 2959 struct mgmt_cp_load_link_keys *cp = data;
ba1d6936
JH
2960 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2961 sizeof(struct mgmt_link_key_info));
4e51eae9 2962 u16 key_count, expected_len;
b1de97d8 2963 bool changed;
a492cd52 2964 int i;
55ed8ca1 2965
9060d5cf
MH
2966 BT_DBG("request for %s", hdev->name);
2967
2968 if (!lmp_bredr_capable(hdev))
a69e8375
JH
2969 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2970 MGMT_STATUS_NOT_SUPPORTED);
9060d5cf 2971
1f350c87 2972 key_count = __le16_to_cpu(cp->key_count);
ba1d6936
JH
2973 if (key_count > max_key_count) {
2974 BT_ERR("load_link_keys: too big key_count value %u",
2975 key_count);
a69e8375
JH
2976 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2977 MGMT_STATUS_INVALID_PARAMS);
ba1d6936 2978 }
55ed8ca1 2979
86742e1e
JH
2980 expected_len = sizeof(*cp) + key_count *
2981 sizeof(struct mgmt_link_key_info);
a492cd52 2982 if (expected_len != len) {
86742e1e 2983 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
2606ecbc 2984 expected_len, len);
a69e8375
JH
2985 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2986 MGMT_STATUS_INVALID_PARAMS);
55ed8ca1
JH
2987 }
2988
4ae14301 2989 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
a69e8375
JH
2990 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2991 MGMT_STATUS_INVALID_PARAMS);
4ae14301 2992
bdb6d971 2993 BT_DBG("%s debug_keys %u key_count %u", hdev->name, cp->debug_keys,
8ce8e2b5 2994 key_count);
55ed8ca1 2995
4ee71b20
JH
2996 for (i = 0; i < key_count; i++) {
2997 struct mgmt_link_key_info *key = &cp->keys[i];
2998
8e991132 2999 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
a69e8375
JH
3000 return mgmt_cmd_status(sk, hdev->id,
3001 MGMT_OP_LOAD_LINK_KEYS,
3002 MGMT_STATUS_INVALID_PARAMS);
4ee71b20
JH
3003 }
3004
09fd0de5 3005 hci_dev_lock(hdev);
55ed8ca1
JH
3006
3007 hci_link_keys_clear(hdev);
3008
55ed8ca1 3009 if (cp->debug_keys)
238be788 3010 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
55ed8ca1 3011 else
a69d8927
MH
3012 changed = hci_dev_test_and_clear_flag(hdev,
3013 HCI_KEEP_DEBUG_KEYS);
b1de97d8
MH
3014
3015 if (changed)
3016 new_settings(hdev, NULL);
55ed8ca1 3017
a492cd52 3018 for (i = 0; i < key_count; i++) {
86742e1e 3019 struct mgmt_link_key_info *key = &cp->keys[i];
55ed8ca1 3020
58e9293c
JH
3021 /* Always ignore debug keys and require a new pairing if
3022 * the user wants to use them.
3023 */
3024 if (key->type == HCI_LK_DEBUG_COMBINATION)
3025 continue;
3026
7652ff6a
JH
3027 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
3028 key->type, key->pin_len, NULL);
55ed8ca1
JH
3029 }
3030
2a1afb5a 3031 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
0e5f875a 3032
09fd0de5 3033 hci_dev_unlock(hdev);
55ed8ca1 3034
a492cd52 3035 return 0;
55ed8ca1
JH
3036}
3037
b1078ad0 3038static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
04124681 3039 u8 addr_type, struct sock *skip_sk)
b1078ad0
JH
3040{
3041 struct mgmt_ev_device_unpaired ev;
3042
3043 bacpy(&ev.addr.bdaddr, bdaddr);
3044 ev.addr.type = addr_type;
3045
3046 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
04124681 3047 skip_sk);
b1078ad0
JH
3048}
3049
bdb6d971 3050static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 3051 u16 len)
55ed8ca1 3052{
124f6e35
JH
3053 struct mgmt_cp_unpair_device *cp = data;
3054 struct mgmt_rp_unpair_device rp;
a8a1d19e 3055 struct hci_cp_disconnect dc;
3b0602cd 3056 struct mgmt_pending_cmd *cmd;
55ed8ca1 3057 struct hci_conn *conn;
55ed8ca1
JH
3058 int err;
3059
a8a1d19e 3060 memset(&rp, 0, sizeof(rp));
124f6e35
JH
3061 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3062 rp.addr.type = cp->addr.type;
a8a1d19e 3063
4ee71b20 3064 if (!bdaddr_type_is_valid(cp->addr.type))
2a1afb5a
JH
3065 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
3066 MGMT_STATUS_INVALID_PARAMS,
3067 &rp, sizeof(rp));
4ee71b20 3068
118da70b 3069 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
2a1afb5a
JH
3070 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
3071 MGMT_STATUS_INVALID_PARAMS,
3072 &rp, sizeof(rp));
118da70b 3073
4ee71b20
JH
3074 hci_dev_lock(hdev);
3075
86a8cfc6 3076 if (!hdev_is_powered(hdev)) {
2a1afb5a
JH
3077 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
3078 MGMT_STATUS_NOT_POWERED, &rp,
3079 sizeof(rp));
86a8cfc6
JH
3080 goto unlock;
3081 }
3082
e0b2b27e 3083 if (cp->addr.type == BDADDR_BREDR) {
89cbb063
AA
3084 /* If disconnection is requested, then look up the
3085 * connection. If the remote device is connected, it
3086 * will be later used to terminate the link.
3087 *
3088 * Setting it to NULL explicitly will cause no
3089 * termination of the link.
3090 */
3091 if (cp->disconnect)
3092 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3093 &cp->addr.bdaddr);
3094 else
3095 conn = NULL;
3096
124f6e35 3097 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
e0b2b27e 3098 } else {
85813a7e 3099 u8 addr_type = le_addr_type(cp->addr.type);
a6ad2a6b 3100
89cbb063
AA
3101 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK,
3102 &cp->addr.bdaddr);
3103 if (conn) {
3104 /* Defer clearing up the connection parameters
3105 * until closing to give a chance of keeping
3106 * them if a repairing happens.
3107 */
3108 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
3109
3110 /* If disconnection is not requested, then
3111 * clear the connection variable so that the
3112 * link is not terminated.
3113 */
3114 if (!cp->disconnect)
3115 conn = NULL;
a6ad2a6b
JH
3116 } else {
3117 hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
89cbb063
AA
3118 }
3119
a7ec7338
JH
3120 hci_remove_irk(hdev, &cp->addr.bdaddr, addr_type);
3121
e0b2b27e
JH
3122 err = hci_remove_ltk(hdev, &cp->addr.bdaddr, addr_type);
3123 }
b0dbfb46 3124
55ed8ca1 3125 if (err < 0) {
2a1afb5a
JH
3126 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
3127 MGMT_STATUS_NOT_PAIRED, &rp,
3128 sizeof(rp));
55ed8ca1
JH
3129 goto unlock;
3130 }
3131
89cbb063
AA
3132 /* If the connection variable is set, then termination of the
3133 * link is requested.
3134 */
a8a1d19e 3135 if (!conn) {
2a1afb5a
JH
3136 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
3137 &rp, sizeof(rp));
b1078ad0 3138 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
a8a1d19e
JH
3139 goto unlock;
3140 }
55ed8ca1 3141
124f6e35 3142 cmd = mgmt_pending_add(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
04124681 3143 sizeof(*cp));
a8a1d19e
JH
3144 if (!cmd) {
3145 err = -ENOMEM;
3146 goto unlock;
55ed8ca1
JH
3147 }
3148
d8b7b1e4
JH
3149 cmd->cmd_complete = addr_cmd_complete;
3150
eb55ef07 3151 dc.handle = cpu_to_le16(conn->handle);
a8a1d19e
JH
3152 dc.reason = 0x13; /* Remote User Terminated Connection */
3153 err = hci_send_cmd(hdev, HCI_OP_DISCONNECT, sizeof(dc), &dc);
3154 if (err < 0)
3155 mgmt_pending_remove(cmd);
3156
55ed8ca1 3157unlock:
09fd0de5 3158 hci_dev_unlock(hdev);
55ed8ca1
JH
3159 return err;
3160}
3161
bdb6d971 3162static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 3163 u16 len)
8962ee74 3164{
650f726d 3165 struct mgmt_cp_disconnect *cp = data;
06a63b19 3166 struct mgmt_rp_disconnect rp;
3b0602cd 3167 struct mgmt_pending_cmd *cmd;
8962ee74 3168 struct hci_conn *conn;
8962ee74
JH
3169 int err;
3170
3171 BT_DBG("");
3172
06a63b19
JH
3173 memset(&rp, 0, sizeof(rp));
3174 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3175 rp.addr.type = cp->addr.type;
3176
4ee71b20 3177 if (!bdaddr_type_is_valid(cp->addr.type))
2a1afb5a
JH
3178 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3179 MGMT_STATUS_INVALID_PARAMS,
3180 &rp, sizeof(rp));
4ee71b20 3181
09fd0de5 3182 hci_dev_lock(hdev);
8962ee74
JH
3183
3184 if (!test_bit(HCI_UP, &hdev->flags)) {
2a1afb5a
JH
3185 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3186 MGMT_STATUS_NOT_POWERED, &rp,
3187 sizeof(rp));
8962ee74
JH
3188 goto failed;
3189 }
3190
333ae95d 3191 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
2a1afb5a
JH
3192 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3193 MGMT_STATUS_BUSY, &rp, sizeof(rp));
8962ee74
JH
3194 goto failed;
3195 }
3196
591f47f3 3197 if (cp->addr.type == BDADDR_BREDR)
8fc9ced3
GP
3198 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3199 &cp->addr.bdaddr);
88c3df13
JH
3200 else
3201 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
365227e5 3202
f960727e 3203 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
2a1afb5a
JH
3204 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3205 MGMT_STATUS_NOT_CONNECTED, &rp,
3206 sizeof(rp));
8962ee74
JH
3207 goto failed;
3208 }
3209
2e58ef3e 3210 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
366a0336
JH
3211 if (!cmd) {
3212 err = -ENOMEM;
8962ee74 3213 goto failed;
366a0336 3214 }
8962ee74 3215
f5818c22
JH
3216 cmd->cmd_complete = generic_cmd_complete;
3217
e3f2f92a 3218 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
8962ee74 3219 if (err < 0)
a664b5bc 3220 mgmt_pending_remove(cmd);
8962ee74
JH
3221
3222failed:
09fd0de5 3223 hci_dev_unlock(hdev);
8962ee74
JH
3224 return err;
3225}
3226
57c1477c 3227static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
4c659c39
JH
3228{
3229 switch (link_type) {
3230 case LE_LINK:
48264f06
JH
3231 switch (addr_type) {
3232 case ADDR_LE_DEV_PUBLIC:
591f47f3 3233 return BDADDR_LE_PUBLIC;
0ed09148 3234
48264f06 3235 default:
0ed09148 3236 /* Fallback to LE Random address type */
591f47f3 3237 return BDADDR_LE_RANDOM;
48264f06 3238 }
0ed09148 3239
4c659c39 3240 default:
0ed09148 3241 /* Fallback to BR/EDR type */
591f47f3 3242 return BDADDR_BREDR;
4c659c39
JH
3243 }
3244}
3245
04124681
GP
3246static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
3247 u16 data_len)
2784eb41 3248{
2784eb41 3249 struct mgmt_rp_get_connections *rp;
8035ded4 3250 struct hci_conn *c;
a38528f1 3251 size_t rp_len;
60fc5fb6
JH
3252 int err;
3253 u16 i;
2784eb41
JH
3254
3255 BT_DBG("");
3256
09fd0de5 3257 hci_dev_lock(hdev);
2784eb41 3258
5f97c1df 3259 if (!hdev_is_powered(hdev)) {
a69e8375
JH
3260 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
3261 MGMT_STATUS_NOT_POWERED);
5f97c1df
JH
3262 goto unlock;
3263 }
3264
60fc5fb6 3265 i = 0;
b644ba33
JH
3266 list_for_each_entry(c, &hdev->conn_hash.list, list) {
3267 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
60fc5fb6 3268 i++;
2784eb41
JH
3269 }
3270
60fc5fb6 3271 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
92c4c204 3272 rp = kmalloc(rp_len, GFP_KERNEL);
a38528f1 3273 if (!rp) {
2784eb41
JH
3274 err = -ENOMEM;
3275 goto unlock;
3276 }
3277
2784eb41 3278 i = 0;
4c659c39 3279 list_for_each_entry(c, &hdev->conn_hash.list, list) {
b644ba33
JH
3280 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
3281 continue;
4c659c39 3282 bacpy(&rp->addr[i].bdaddr, &c->dst);
57c1477c 3283 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
0ed09148 3284 if (c->type == SCO_LINK || c->type == ESCO_LINK)
4c659c39
JH
3285 continue;
3286 i++;
3287 }
3288
eb55ef07 3289 rp->conn_count = cpu_to_le16(i);
60fc5fb6 3290
4c659c39
JH
3291 /* Recalculate length in case of filtered SCO connections, etc */
3292 rp_len = sizeof(*rp) + (i * sizeof(struct mgmt_addr_info));
2784eb41 3293
2a1afb5a
JH
3294 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
3295 rp_len);
2784eb41 3296
a38528f1 3297 kfree(rp);
5f97c1df
JH
3298
3299unlock:
09fd0de5 3300 hci_dev_unlock(hdev);
2784eb41
JH
3301 return err;
3302}
3303
bdb6d971 3304static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
04124681 3305 struct mgmt_cp_pin_code_neg_reply *cp)
96d97a67 3306{
3b0602cd 3307 struct mgmt_pending_cmd *cmd;
96d97a67
WR
3308 int err;
3309
2e58ef3e 3310 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
04124681 3311 sizeof(*cp));
96d97a67
WR
3312 if (!cmd)
3313 return -ENOMEM;
3314
d8457698 3315 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
04124681 3316 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
96d97a67
WR
3317 if (err < 0)
3318 mgmt_pending_remove(cmd);
3319
3320 return err;
3321}
3322
bdb6d971 3323static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 3324 u16 len)
980e1a53 3325{
96d97a67 3326 struct hci_conn *conn;
650f726d 3327 struct mgmt_cp_pin_code_reply *cp = data;
980e1a53 3328 struct hci_cp_pin_code_reply reply;
3b0602cd 3329 struct mgmt_pending_cmd *cmd;
980e1a53
JH
3330 int err;
3331
3332 BT_DBG("");
3333
09fd0de5 3334 hci_dev_lock(hdev);
980e1a53 3335
4b34ee78 3336 if (!hdev_is_powered(hdev)) {
a69e8375
JH
3337 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3338 MGMT_STATUS_NOT_POWERED);
980e1a53
JH
3339 goto failed;
3340 }
3341
d8457698 3342 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
96d97a67 3343 if (!conn) {
a69e8375
JH
3344 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3345 MGMT_STATUS_NOT_CONNECTED);
96d97a67
WR
3346 goto failed;
3347 }
3348
3349 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
d8457698
JH
3350 struct mgmt_cp_pin_code_neg_reply ncp;
3351
3352 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
96d97a67
WR
3353
3354 BT_ERR("PIN code is not 16 bytes long");
3355
bdb6d971 3356 err = send_pin_code_neg_reply(sk, hdev, &ncp);
96d97a67 3357 if (err >= 0)
a69e8375
JH
3358 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3359 MGMT_STATUS_INVALID_PARAMS);
96d97a67
WR
3360
3361 goto failed;
3362 }
3363
00abfe44 3364 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
366a0336
JH
3365 if (!cmd) {
3366 err = -ENOMEM;
980e1a53 3367 goto failed;
366a0336 3368 }
980e1a53 3369
7776d1d8
JH
3370 cmd->cmd_complete = addr_cmd_complete;
3371
d8457698 3372 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
980e1a53 3373 reply.pin_len = cp->pin_len;
24718ca5 3374 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
980e1a53
JH
3375
3376 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
3377 if (err < 0)
a664b5bc 3378 mgmt_pending_remove(cmd);
980e1a53
JH
3379
3380failed:
09fd0de5 3381 hci_dev_unlock(hdev);
980e1a53
JH
3382 return err;
3383}
3384
04124681
GP
3385static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3386 u16 len)
17fa4b9d 3387{
650f726d 3388 struct mgmt_cp_set_io_capability *cp = data;
17fa4b9d
JH
3389
3390 BT_DBG("");
3391
4ec86d4c 3392 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
2a1afb5a
JH
3393 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3394 MGMT_STATUS_INVALID_PARAMS, NULL, 0);
4ec86d4c 3395
09fd0de5 3396 hci_dev_lock(hdev);
17fa4b9d
JH
3397
3398 hdev->io_capability = cp->io_capability;
3399
3400 BT_DBG("%s IO capability set to 0x%02x", hdev->name,
8ce8e2b5 3401 hdev->io_capability);
17fa4b9d 3402
09fd0de5 3403 hci_dev_unlock(hdev);
17fa4b9d 3404
2a1afb5a
JH
3405 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
3406 NULL, 0);
17fa4b9d
JH
3407}
3408
3b0602cd 3409static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
e9a416b5
JH
3410{
3411 struct hci_dev *hdev = conn->hdev;
3b0602cd 3412 struct mgmt_pending_cmd *cmd;
e9a416b5 3413
2e58ef3e 3414 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
e9a416b5
JH
3415 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3416 continue;
3417
e9a416b5
JH
3418 if (cmd->user_data != conn)
3419 continue;
3420
3421 return cmd;
3422 }
3423
3424 return NULL;
3425}
3426
3b0602cd 3427static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
e9a416b5
JH
3428{
3429 struct mgmt_rp_pair_device rp;
3430 struct hci_conn *conn = cmd->user_data;
9df74653 3431 int err;
e9a416b5 3432
61b1a7fb
JH
3433 bacpy(&rp.addr.bdaddr, &conn->dst);
3434 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
e9a416b5 3435
2a1afb5a
JH
3436 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
3437 status, &rp, sizeof(rp));
e9a416b5
JH
3438
3439 /* So we don't get further callbacks for this connection */
3440 conn->connect_cfm_cb = NULL;
3441 conn->security_cfm_cb = NULL;
3442 conn->disconn_cfm_cb = NULL;
3443
76a68ba0 3444 hci_conn_drop(conn);
89cbb063
AA
3445
3446 /* The device is paired so there is no need to remove
3447 * its connection parameters anymore.
3448 */
3449 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
15013aeb
JH
3450
3451 hci_conn_put(conn);
9df74653
JH
3452
3453 return err;
e9a416b5
JH
3454}
3455
f4a407be
JH
3456void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3457{
3458 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
3b0602cd 3459 struct mgmt_pending_cmd *cmd;
f4a407be
JH
3460
3461 cmd = find_pairing(conn);
a511b35b 3462 if (cmd) {
04ab2749 3463 cmd->cmd_complete(cmd, status);
a511b35b
JH
3464 mgmt_pending_remove(cmd);
3465 }
f4a407be
JH
3466}
3467
e9a416b5
JH
3468static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3469{
3b0602cd 3470 struct mgmt_pending_cmd *cmd;
e9a416b5
JH
3471
3472 BT_DBG("status %u", status);
3473
3474 cmd = find_pairing(conn);
a511b35b 3475 if (!cmd) {
e9a416b5 3476 BT_DBG("Unable to find a pending command");
a511b35b
JH
3477 return;
3478 }
3479
3480 cmd->cmd_complete(cmd, mgmt_status(status));
3481 mgmt_pending_remove(cmd);
e9a416b5
JH
3482}
3483
f4a407be 3484static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
4c47d739 3485{
3b0602cd 3486 struct mgmt_pending_cmd *cmd;
4c47d739
VA
3487
3488 BT_DBG("status %u", status);
3489
3490 if (!status)
3491 return;
3492
3493 cmd = find_pairing(conn);
a511b35b 3494 if (!cmd) {
4c47d739 3495 BT_DBG("Unable to find a pending command");
a511b35b
JH
3496 return;
3497 }
3498
3499 cmd->cmd_complete(cmd, mgmt_status(status));
3500 mgmt_pending_remove(cmd);
4c47d739
VA
3501}
3502
bdb6d971 3503static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 3504 u16 len)
e9a416b5 3505{
650f726d 3506 struct mgmt_cp_pair_device *cp = data;
1425acb7 3507 struct mgmt_rp_pair_device rp;
3b0602cd 3508 struct mgmt_pending_cmd *cmd;
e9a416b5
JH
3509 u8 sec_level, auth_type;
3510 struct hci_conn *conn;
e9a416b5
JH
3511 int err;
3512
3513 BT_DBG("");
3514
f950a30e
SJ
3515 memset(&rp, 0, sizeof(rp));
3516 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3517 rp.addr.type = cp->addr.type;
3518
4ee71b20 3519 if (!bdaddr_type_is_valid(cp->addr.type))
2a1afb5a
JH
3520 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3521 MGMT_STATUS_INVALID_PARAMS,
3522 &rp, sizeof(rp));
4ee71b20 3523
4ec86d4c 3524 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
2a1afb5a
JH
3525 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3526 MGMT_STATUS_INVALID_PARAMS,
3527 &rp, sizeof(rp));
4ec86d4c 3528
09fd0de5 3529 hci_dev_lock(hdev);
e9a416b5 3530
5f97c1df 3531 if (!hdev_is_powered(hdev)) {
2a1afb5a
JH
3532 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3533 MGMT_STATUS_NOT_POWERED, &rp,
3534 sizeof(rp));
5f97c1df
JH
3535 goto unlock;
3536 }
3537
55e76b38
JH
3538 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
3539 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3540 MGMT_STATUS_ALREADY_PAIRED, &rp,
3541 sizeof(rp));
3542 goto unlock;
3543 }
3544
c908df36 3545 sec_level = BT_SECURITY_MEDIUM;
6fd6b915 3546 auth_type = HCI_AT_DEDICATED_BONDING;
e9a416b5 3547
6f77d8c7 3548 if (cp->addr.type == BDADDR_BREDR) {
04a6c589
AG
3549 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
3550 auth_type);
6f77d8c7 3551 } else {
85813a7e 3552 u8 addr_type = le_addr_type(cp->addr.type);
5157b8a5 3553 struct hci_conn_params *p;
6f77d8c7 3554
7c264b10
MH
3555 /* When pairing a new device, it is expected to remember
3556 * this device for future connections. Adding the connection
3557 * parameter information ahead of time allows tracking
3558 * of the slave preferred values and will speed up any
3559 * further connection establishment.
3560 *
3561 * If connection parameters already exist, then they
3562 * will be kept and this function does nothing.
3563 */
5157b8a5
JP
3564 p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
3565
3566 if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT)
3567 p->auto_connect = HCI_AUTO_CONN_DISABLED;
7c264b10 3568
fa142220
JP
3569 conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr,
3570 addr_type, sec_level,
3571 HCI_LE_CONN_TIMEOUT,
3572 HCI_ROLE_MASTER);
6f77d8c7 3573 }
7a512d01 3574
30e76272 3575 if (IS_ERR(conn)) {
489dc48e
AK
3576 int status;
3577
3578 if (PTR_ERR(conn) == -EBUSY)
3579 status = MGMT_STATUS_BUSY;
faa81030
LR
3580 else if (PTR_ERR(conn) == -EOPNOTSUPP)
3581 status = MGMT_STATUS_NOT_SUPPORTED;
3582 else if (PTR_ERR(conn) == -ECONNREFUSED)
3583 status = MGMT_STATUS_REJECTED;
489dc48e
AK
3584 else
3585 status = MGMT_STATUS_CONNECT_FAILED;
3586
2a1afb5a
JH
3587 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3588 status, &rp, sizeof(rp));
e9a416b5
JH
3589 goto unlock;
3590 }
3591
3592 if (conn->connect_cfm_cb) {
76a68ba0 3593 hci_conn_drop(conn);
2a1afb5a
JH
3594 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3595 MGMT_STATUS_BUSY, &rp, sizeof(rp));
e9a416b5
JH
3596 goto unlock;
3597 }
3598
2e58ef3e 3599 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
e9a416b5
JH
3600 if (!cmd) {
3601 err = -ENOMEM;
76a68ba0 3602 hci_conn_drop(conn);
e9a416b5
JH
3603 goto unlock;
3604 }
3605
04ab2749
JH
3606 cmd->cmd_complete = pairing_complete;
3607
7a512d01 3608 /* For LE, just connecting isn't a proof that the pairing finished */
f4a407be 3609 if (cp->addr.type == BDADDR_BREDR) {
7a512d01 3610 conn->connect_cfm_cb = pairing_complete_cb;
f4a407be
JH
3611 conn->security_cfm_cb = pairing_complete_cb;
3612 conn->disconn_cfm_cb = pairing_complete_cb;
3613 } else {
3614 conn->connect_cfm_cb = le_pairing_complete_cb;
3615 conn->security_cfm_cb = le_pairing_complete_cb;
3616 conn->disconn_cfm_cb = le_pairing_complete_cb;
3617 }
7a512d01 3618
e9a416b5 3619 conn->io_capability = cp->io_cap;
f8aaf9b6 3620 cmd->user_data = hci_conn_get(conn);
e9a416b5 3621
6f78fd4b 3622 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
a511b35b
JH
3623 hci_conn_security(conn, sec_level, auth_type, true)) {
3624 cmd->cmd_complete(cmd, 0);
3625 mgmt_pending_remove(cmd);
3626 }
e9a416b5
JH
3627
3628 err = 0;
3629
3630unlock:
09fd0de5 3631 hci_dev_unlock(hdev);
e9a416b5
JH
3632 return err;
3633}
3634
04124681
GP
3635static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3636 u16 len)
28424707 3637{
0f4e68cf 3638 struct mgmt_addr_info *addr = data;
3b0602cd 3639 struct mgmt_pending_cmd *cmd;
28424707
JH
3640 struct hci_conn *conn;
3641 int err;
3642
3643 BT_DBG("");
3644
28424707
JH
3645 hci_dev_lock(hdev);
3646
5f97c1df 3647 if (!hdev_is_powered(hdev)) {
a69e8375
JH
3648 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3649 MGMT_STATUS_NOT_POWERED);
5f97c1df
JH
3650 goto unlock;
3651 }
3652
333ae95d 3653 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
28424707 3654 if (!cmd) {
a69e8375
JH
3655 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3656 MGMT_STATUS_INVALID_PARAMS);
28424707
JH
3657 goto unlock;
3658 }
3659
3660 conn = cmd->user_data;
3661
3662 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
a69e8375
JH
3663 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3664 MGMT_STATUS_INVALID_PARAMS);
28424707
JH
3665 goto unlock;
3666 }
3667
a511b35b
JH
3668 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3669 mgmt_pending_remove(cmd);
28424707 3670
2a1afb5a
JH
3671 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3672 addr, sizeof(*addr));
28424707
JH
3673unlock:
3674 hci_dev_unlock(hdev);
28424707
JH
3675 return err;
3676}
3677
bdb6d971 3678static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
1707c60e 3679 struct mgmt_addr_info *addr, u16 mgmt_op,
04124681 3680 u16 hci_op, __le32 passkey)
a5c29683 3681{
3b0602cd 3682 struct mgmt_pending_cmd *cmd;
0df4c185 3683 struct hci_conn *conn;
a5c29683
JH
3684 int err;
3685
09fd0de5 3686 hci_dev_lock(hdev);
08ba5382 3687
4b34ee78 3688 if (!hdev_is_powered(hdev)) {
2a1afb5a
JH
3689 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3690 MGMT_STATUS_NOT_POWERED, addr,
3691 sizeof(*addr));
0df4c185 3692 goto done;
a5c29683
JH
3693 }
3694
1707c60e
JH
3695 if (addr->type == BDADDR_BREDR)
3696 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
272d90df 3697 else
1707c60e 3698 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &addr->bdaddr);
272d90df
JH
3699
3700 if (!conn) {
2a1afb5a
JH
3701 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3702 MGMT_STATUS_NOT_CONNECTED, addr,
3703 sizeof(*addr));
272d90df
JH
3704 goto done;
3705 }
47c15e2b 3706
1707c60e 3707 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
5fe57d9e 3708 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
5fe57d9e 3709 if (!err)
2a1afb5a
JH
3710 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3711 MGMT_STATUS_SUCCESS, addr,
3712 sizeof(*addr));
5fe57d9e 3713 else
2a1afb5a
JH
3714 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3715 MGMT_STATUS_FAILED, addr,
3716 sizeof(*addr));
47c15e2b 3717
47c15e2b
BG
3718 goto done;
3719 }
3720
1707c60e 3721 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
a5c29683
JH
3722 if (!cmd) {
3723 err = -ENOMEM;
0df4c185 3724 goto done;
a5c29683
JH
3725 }
3726
7776d1d8
JH
3727 cmd->cmd_complete = addr_cmd_complete;
3728
0df4c185 3729 /* Continue with pairing via HCI */
604086b7
BG
3730 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3731 struct hci_cp_user_passkey_reply cp;
3732
1707c60e 3733 bacpy(&cp.bdaddr, &addr->bdaddr);
604086b7
BG
3734 cp.passkey = passkey;
3735 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3736 } else
1707c60e
JH
3737 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3738 &addr->bdaddr);
604086b7 3739
a664b5bc
JH
3740 if (err < 0)
3741 mgmt_pending_remove(cmd);
a5c29683 3742
0df4c185 3743done:
09fd0de5 3744 hci_dev_unlock(hdev);
a5c29683
JH
3745 return err;
3746}
3747
afeb019d
JK
3748static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3749 void *data, u16 len)
3750{
3751 struct mgmt_cp_pin_code_neg_reply *cp = data;
3752
3753 BT_DBG("");
3754
1707c60e 3755 return user_pairing_resp(sk, hdev, &cp->addr,
afeb019d
JK
3756 MGMT_OP_PIN_CODE_NEG_REPLY,
3757 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3758}
3759
04124681
GP
3760static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3761 u16 len)
0df4c185 3762{
650f726d 3763 struct mgmt_cp_user_confirm_reply *cp = data;
0df4c185
BG
3764
3765 BT_DBG("");
3766
3767 if (len != sizeof(*cp))
a69e8375
JH
3768 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3769 MGMT_STATUS_INVALID_PARAMS);
0df4c185 3770
1707c60e 3771 return user_pairing_resp(sk, hdev, &cp->addr,
04124681
GP
3772 MGMT_OP_USER_CONFIRM_REPLY,
3773 HCI_OP_USER_CONFIRM_REPLY, 0);
0df4c185
BG
3774}
3775
bdb6d971 3776static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
04124681 3777 void *data, u16 len)
0df4c185 3778{
c9c2659f 3779 struct mgmt_cp_user_confirm_neg_reply *cp = data;
0df4c185
BG
3780
3781 BT_DBG("");
3782
1707c60e 3783 return user_pairing_resp(sk, hdev, &cp->addr,
04124681
GP
3784 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3785 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
0df4c185
BG
3786}
3787
04124681
GP
3788static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3789 u16 len)
604086b7 3790{
650f726d 3791 struct mgmt_cp_user_passkey_reply *cp = data;
604086b7
BG
3792
3793 BT_DBG("");
3794
1707c60e 3795 return user_pairing_resp(sk, hdev, &cp->addr,
04124681
GP
3796 MGMT_OP_USER_PASSKEY_REPLY,
3797 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
604086b7
BG
3798}
3799
bdb6d971 3800static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
04124681 3801 void *data, u16 len)
604086b7 3802{
650f726d 3803 struct mgmt_cp_user_passkey_neg_reply *cp = data;
604086b7
BG
3804
3805 BT_DBG("");
3806
1707c60e 3807 return user_pairing_resp(sk, hdev, &cp->addr,
04124681
GP
3808 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3809 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
604086b7
BG
3810}
3811
13928971 3812static void update_name(struct hci_request *req)
2b4bf397 3813{
13928971 3814 struct hci_dev *hdev = req->hdev;
2b4bf397
JH
3815 struct hci_cp_write_local_name cp;
3816
13928971 3817 memcpy(cp.name, hdev->dev_name, sizeof(cp.name));
2b4bf397 3818
890ea898 3819 hci_req_add(req, HCI_OP_WRITE_LOCAL_NAME, sizeof(cp), &cp);
2b4bf397
JH
3820}
3821
1904a853 3822static void set_name_complete(struct hci_dev *hdev, u8 status, u16 opcode)
13928971
JH
3823{
3824 struct mgmt_cp_set_local_name *cp;
3b0602cd 3825 struct mgmt_pending_cmd *cmd;
13928971
JH
3826
3827 BT_DBG("status 0x%02x", status);
3828
3829 hci_dev_lock(hdev);
3830
333ae95d 3831 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
13928971
JH
3832 if (!cmd)
3833 goto unlock;
3834
3835 cp = cmd->param;
3836
3837 if (status)
a69e8375
JH
3838 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3839 mgmt_status(status));
13928971 3840 else
2a1afb5a
JH
3841 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3842 cp, sizeof(*cp));
13928971
JH
3843
3844 mgmt_pending_remove(cmd);
3845
3846unlock:
3847 hci_dev_unlock(hdev);
3848}
3849
bdb6d971 3850static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 3851 u16 len)
b312b161 3852{
2b4bf397 3853 struct mgmt_cp_set_local_name *cp = data;
3b0602cd 3854 struct mgmt_pending_cmd *cmd;
890ea898 3855 struct hci_request req;
b312b161
JH
3856 int err;
3857
3858 BT_DBG("");
3859
09fd0de5 3860 hci_dev_lock(hdev);
b312b161 3861
b3f2ca94
JH
3862 /* If the old values are the same as the new ones just return a
3863 * direct command complete event.
3864 */
3865 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3866 !memcmp(hdev->short_name, cp->short_name,
3867 sizeof(hdev->short_name))) {
2a1afb5a
JH
3868 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3869 data, len);
b3f2ca94
JH
3870 goto failed;
3871 }
3872
2b4bf397 3873 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
28cc7bde 3874
b5235a65 3875 if (!hdev_is_powered(hdev)) {
2b4bf397 3876 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
28cc7bde 3877
2a1afb5a
JH
3878 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3879 data, len);
28cc7bde
JH
3880 if (err < 0)
3881 goto failed;
3882
f6b7712e
MH
3883 err = mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev,
3884 data, len, sk);
28cc7bde 3885
b5235a65
JH
3886 goto failed;
3887 }
3888
28cc7bde 3889 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
b312b161
JH
3890 if (!cmd) {
3891 err = -ENOMEM;
3892 goto failed;
3893 }
3894
13928971
JH
3895 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3896
890ea898 3897 hci_req_init(&req, hdev);
3f985050
JH
3898
3899 if (lmp_bredr_capable(hdev)) {
3900 update_name(&req);
3901 update_eir(&req);
3902 }
3903
7a5f4990
MH
3904 /* The name is stored in the scan response data and so
3905 * no need to udpate the advertising data here.
3906 */
3f985050 3907 if (lmp_le_capable(hdev))
7a5f4990 3908 update_scan_rsp_data(&req);
3f985050 3909
13928971 3910 err = hci_req_run(&req, set_name_complete);
b312b161
JH
3911 if (err < 0)
3912 mgmt_pending_remove(cmd);
3913
3914failed:
09fd0de5 3915 hci_dev_unlock(hdev);
b312b161
JH
3916 return err;
3917}
3918
1b9441f8
JH
3919static void read_local_oob_data_complete(struct hci_dev *hdev, u8 status,
3920 u16 opcode, struct sk_buff *skb)
3921{
3922 struct mgmt_rp_read_local_oob_data mgmt_rp;
3923 size_t rp_size = sizeof(mgmt_rp);
3924 struct mgmt_pending_cmd *cmd;
3925
3926 BT_DBG("%s status %u", hdev->name, status);
3927
3928 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev);
3929 if (!cmd)
3930 return;
3931
3932 if (status || !skb) {
3933 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3934 status ? mgmt_status(status) : MGMT_STATUS_FAILED);
3935 goto remove;
3936 }
3937
3938 memset(&mgmt_rp, 0, sizeof(mgmt_rp));
3939
3940 if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
3941 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
3942
3943 if (skb->len < sizeof(*rp)) {
3944 mgmt_cmd_status(cmd->sk, hdev->id,
3945 MGMT_OP_READ_LOCAL_OOB_DATA,
3946 MGMT_STATUS_FAILED);
3947 goto remove;
3948 }
3949
3950 memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
3951 memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
3952
3953 rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
3954 } else {
3955 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
3956
3957 if (skb->len < sizeof(*rp)) {
3958 mgmt_cmd_status(cmd->sk, hdev->id,
3959 MGMT_OP_READ_LOCAL_OOB_DATA,
3960 MGMT_STATUS_FAILED);
3961 goto remove;
3962 }
3963
3964 memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
3965 memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
3966
3967 memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
3968 memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
3969 }
3970
3971 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3972 MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
3973
3974remove:
3975 mgmt_pending_remove(cmd);
3976}
3977
0f4e68cf 3978static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
04124681 3979 void *data, u16 data_len)
c35938b2 3980{
3b0602cd 3981 struct mgmt_pending_cmd *cmd;
1b9441f8 3982 struct hci_request req;
c35938b2
SJ
3983 int err;
3984
bdb6d971 3985 BT_DBG("%s", hdev->name);
c35938b2 3986
09fd0de5 3987 hci_dev_lock(hdev);
c35938b2 3988
4b34ee78 3989 if (!hdev_is_powered(hdev)) {
a69e8375
JH
3990 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3991 MGMT_STATUS_NOT_POWERED);
c35938b2
SJ
3992 goto unlock;
3993 }
3994
9a1a1996 3995 if (!lmp_ssp_capable(hdev)) {
a69e8375
JH
3996 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
3997 MGMT_STATUS_NOT_SUPPORTED);
c35938b2
SJ
3998 goto unlock;
3999 }
4000
333ae95d 4001 if (pending_find(MGMT_OP_READ_LOCAL_OOB_DATA, hdev)) {
a69e8375
JH
4002 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
4003 MGMT_STATUS_BUSY);
c35938b2
SJ
4004 goto unlock;
4005 }
4006
2e58ef3e 4007 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
c35938b2
SJ
4008 if (!cmd) {
4009 err = -ENOMEM;
4010 goto unlock;
4011 }
4012
1b9441f8
JH
4013 hci_req_init(&req, hdev);
4014
710f11c0 4015 if (bredr_sc_enabled(hdev))
1b9441f8 4016 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
4d2d2796 4017 else
1b9441f8 4018 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
4d2d2796 4019
1b9441f8 4020 err = hci_req_run_skb(&req, read_local_oob_data_complete);
c35938b2
SJ
4021 if (err < 0)
4022 mgmt_pending_remove(cmd);
4023
4024unlock:
09fd0de5 4025 hci_dev_unlock(hdev);
c35938b2
SJ
4026 return err;
4027}
4028
bdb6d971 4029static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
04124681 4030 void *data, u16 len)
2763eda6 4031{
5d57e796 4032 struct mgmt_addr_info *addr = data;
2763eda6
SJ
4033 int err;
4034
bdb6d971 4035 BT_DBG("%s ", hdev->name);
2763eda6 4036
5d57e796 4037 if (!bdaddr_type_is_valid(addr->type))
2a1afb5a
JH
4038 return mgmt_cmd_complete(sk, hdev->id,
4039 MGMT_OP_ADD_REMOTE_OOB_DATA,
4040 MGMT_STATUS_INVALID_PARAMS,
4041 addr, sizeof(*addr));
5d57e796 4042
09fd0de5 4043 hci_dev_lock(hdev);
2763eda6 4044
ec109113
MH
4045 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
4046 struct mgmt_cp_add_remote_oob_data *cp = data;
4047 u8 status;
bf1e3541 4048
c19a495c 4049 if (cp->addr.type != BDADDR_BREDR) {
2a1afb5a
JH
4050 err = mgmt_cmd_complete(sk, hdev->id,
4051 MGMT_OP_ADD_REMOTE_OOB_DATA,
4052 MGMT_STATUS_INVALID_PARAMS,
4053 &cp->addr, sizeof(cp->addr));
c19a495c
JH
4054 goto unlock;
4055 }
4056
ec109113 4057 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
6928a924
JH
4058 cp->addr.type, cp->hash,
4059 cp->rand, NULL, NULL);
ec109113
MH
4060 if (err < 0)
4061 status = MGMT_STATUS_FAILED;
4062 else
4063 status = MGMT_STATUS_SUCCESS;
4064
2a1afb5a
JH
4065 err = mgmt_cmd_complete(sk, hdev->id,
4066 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
4067 &cp->addr, sizeof(cp->addr));
ec109113
MH
4068 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
4069 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
41bcfd50 4070 u8 *rand192, *hash192, *rand256, *hash256;
ec109113
MH
4071 u8 status;
4072
86df9200 4073 if (bdaddr_type_is_le(cp->addr.type)) {
d25b78e2
JH
4074 /* Enforce zero-valued 192-bit parameters as
4075 * long as legacy SMP OOB isn't implemented.
4076 */
4077 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
4078 memcmp(cp->hash192, ZERO_KEY, 16)) {
2a1afb5a
JH
4079 err = mgmt_cmd_complete(sk, hdev->id,
4080 MGMT_OP_ADD_REMOTE_OOB_DATA,
4081 MGMT_STATUS_INVALID_PARAMS,
4082 addr, sizeof(*addr));
d25b78e2
JH
4083 goto unlock;
4084 }
4085
86df9200
JH
4086 rand192 = NULL;
4087 hash192 = NULL;
4088 } else {
41bcfd50
MH
4089 /* In case one of the P-192 values is set to zero,
4090 * then just disable OOB data for P-192.
4091 */
4092 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
4093 !memcmp(cp->hash192, ZERO_KEY, 16)) {
4094 rand192 = NULL;
4095 hash192 = NULL;
4096 } else {
4097 rand192 = cp->rand192;
4098 hash192 = cp->hash192;
4099 }
4100 }
4101
4102 /* In case one of the P-256 values is set to zero, then just
4103 * disable OOB data for P-256.
4104 */
4105 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
4106 !memcmp(cp->hash256, ZERO_KEY, 16)) {
4107 rand256 = NULL;
4108 hash256 = NULL;
4109 } else {
4110 rand256 = cp->rand256;
4111 hash256 = cp->hash256;
86df9200
JH
4112 }
4113
81328d5c 4114 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
86df9200 4115 cp->addr.type, hash192, rand192,
41bcfd50 4116 hash256, rand256);
ec109113
MH
4117 if (err < 0)
4118 status = MGMT_STATUS_FAILED;
4119 else
4120 status = MGMT_STATUS_SUCCESS;
4121
2a1afb5a
JH
4122 err = mgmt_cmd_complete(sk, hdev->id,
4123 MGMT_OP_ADD_REMOTE_OOB_DATA,
4124 status, &cp->addr, sizeof(cp->addr));
ec109113
MH
4125 } else {
4126 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len);
a69e8375
JH
4127 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
4128 MGMT_STATUS_INVALID_PARAMS);
ec109113 4129 }
2763eda6 4130
c19a495c 4131unlock:
09fd0de5 4132 hci_dev_unlock(hdev);
2763eda6
SJ
4133 return err;
4134}
4135
bdb6d971 4136static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
8ce8e2b5 4137 void *data, u16 len)
2763eda6 4138{
650f726d 4139 struct mgmt_cp_remove_remote_oob_data *cp = data;
bf1e3541 4140 u8 status;
2763eda6
SJ
4141 int err;
4142
bdb6d971 4143 BT_DBG("%s", hdev->name);
2763eda6 4144
c19a495c 4145 if (cp->addr.type != BDADDR_BREDR)
2a1afb5a
JH
4146 return mgmt_cmd_complete(sk, hdev->id,
4147 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
4148 MGMT_STATUS_INVALID_PARAMS,
4149 &cp->addr, sizeof(cp->addr));
c19a495c 4150
09fd0de5 4151 hci_dev_lock(hdev);
2763eda6 4152
eedbd581
JH
4153 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
4154 hci_remote_oob_data_clear(hdev);
4155 status = MGMT_STATUS_SUCCESS;
4156 goto done;
4157 }
4158
6928a924 4159 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
2763eda6 4160 if (err < 0)
bf1e3541 4161 status = MGMT_STATUS_INVALID_PARAMS;
2763eda6 4162 else
a6785be2 4163 status = MGMT_STATUS_SUCCESS;
bf1e3541 4164
eedbd581 4165done:
2a1afb5a
JH
4166 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
4167 status, &cp->addr, sizeof(cp->addr));
2763eda6 4168
09fd0de5 4169 hci_dev_unlock(hdev);
2763eda6
SJ
4170 return err;
4171}
4172
812abb13 4173static bool trigger_bredr_inquiry(struct hci_request *req, u8 *status)
41dc2bd6 4174{
8019044d 4175 struct hci_dev *hdev = req->hdev;
812abb13 4176 struct hci_cp_inquiry cp;
8019044d
MH
4177 /* General inquiry access code (GIAC) */
4178 u8 lap[3] = { 0x33, 0x8b, 0x9e };
812abb13
JP
4179
4180 *status = mgmt_bredr_support(hdev);
4181 if (*status)
4182 return false;
4183
4184 if (hci_dev_test_flag(hdev, HCI_INQUIRY)) {
4185 *status = MGMT_STATUS_BUSY;
4186 return false;
4187 }
4188
4189 hci_inquiry_cache_flush(hdev);
4190
4191 memset(&cp, 0, sizeof(cp));
4192 memcpy(&cp.lap, lap, sizeof(cp.lap));
4193 cp.length = DISCOV_BREDR_INQUIRY_LEN;
4194
4195 hci_req_add(req, HCI_OP_INQUIRY, sizeof(cp), &cp);
4196
4197 return true;
4198}
4199
4200static bool trigger_le_scan(struct hci_request *req, u16 interval, u8 *status)
4201{
4202 struct hci_dev *hdev = req->hdev;
4203 struct hci_cp_le_set_scan_param param_cp;
4204 struct hci_cp_le_set_scan_enable enable_cp;
8019044d 4205 u8 own_addr_type;
41dc2bd6
AG
4206 int err;
4207
812abb13
JP
4208 *status = mgmt_le_support(hdev);
4209 if (*status)
4210 return false;
41dc2bd6 4211
812abb13
JP
4212 if (hci_dev_test_flag(hdev, HCI_LE_ADV)) {
4213 /* Don't let discovery abort an outgoing connection attempt
4214 * that's using directed advertising.
4215 */
e7d9ab73 4216 if (hci_lookup_le_connect(hdev)) {
812abb13 4217 *status = MGMT_STATUS_REJECTED;
8019044d
MH
4218 return false;
4219 }
41dc2bd6 4220
9d5fc2f2 4221 cancel_adv_timeout(hdev);
812abb13
JP
4222 disable_advertising(req);
4223 }
41dc2bd6 4224
812abb13
JP
4225 /* If controller is scanning, it means the background scanning is
4226 * running. Thus, we should temporarily stop it in order to set the
4227 * discovery scanning parameters.
4228 */
4229 if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
4230 hci_req_add_le_scan_disable(req);
41dc2bd6 4231
812abb13
JP
4232 /* All active scans will be done with either a resolvable private
4233 * address (when privacy feature has been enabled) or non-resolvable
4234 * private address.
4235 */
4236 err = hci_update_random_address(req, true, &own_addr_type);
4237 if (err < 0) {
4238 *status = MGMT_STATUS_FAILED;
4239 return false;
4240 }
8019044d 4241
812abb13
JP
4242 memset(&param_cp, 0, sizeof(param_cp));
4243 param_cp.type = LE_SCAN_ACTIVE;
4244 param_cp.interval = cpu_to_le16(interval);
4245 param_cp.window = cpu_to_le16(DISCOV_LE_SCAN_WIN);
4246 param_cp.own_address_type = own_addr_type;
8019044d 4247
812abb13
JP
4248 hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
4249 &param_cp);
8019044d 4250
812abb13
JP
4251 memset(&enable_cp, 0, sizeof(enable_cp));
4252 enable_cp.enable = LE_SCAN_ENABLE;
4253 enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
8019044d 4254
812abb13
JP
4255 hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
4256 &enable_cp);
4257
4258 return true;
4259}
8019044d 4260
812abb13
JP
4261static bool trigger_discovery(struct hci_request *req, u8 *status)
4262{
4263 struct hci_dev *hdev = req->hdev;
8019044d 4264
812abb13
JP
4265 switch (hdev->discovery.type) {
4266 case DISCOV_TYPE_BREDR:
4267 if (!trigger_bredr_inquiry(req, status))
4268 return false;
4269 break;
4270
4271 case DISCOV_TYPE_INTERLEAVED:
07d2334a
JP
4272 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY,
4273 &hdev->quirks)) {
4274 /* During simultaneous discovery, we double LE scan
4275 * interval. We must leave some time for the controller
4276 * to do BR/EDR inquiry.
4277 */
4278 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT * 2,
4279 status))
4280 return false;
4281
4282 if (!trigger_bredr_inquiry(req, status))
4283 return false;
4284
4285 return true;
4286 }
4287
812abb13
JP
4288 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
4289 *status = MGMT_STATUS_NOT_SUPPORTED;
8019044d
MH
4290 return false;
4291 }
812abb13 4292 /* fall through */
8019044d 4293
812abb13
JP
4294 case DISCOV_TYPE_LE:
4295 if (!trigger_le_scan(req, DISCOV_LE_SCAN_INT, status))
4296 return false;
8019044d
MH
4297 break;
4298
4299 default:
4300 *status = MGMT_STATUS_INVALID_PARAMS;
4301 return false;
4302 }
4303
4304 return true;
41dc2bd6
AG
4305}
4306
1904a853
MH
4307static void start_discovery_complete(struct hci_dev *hdev, u8 status,
4308 u16 opcode)
7c307720 4309{
3b0602cd 4310 struct mgmt_pending_cmd *cmd;
11e6e25d 4311 unsigned long timeout;
ae55f598 4312
7c307720
AG
4313 BT_DBG("status %d", status);
4314
11e6e25d 4315 hci_dev_lock(hdev);
41dc2bd6 4316
333ae95d 4317 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
66ea9427 4318 if (!cmd)
333ae95d 4319 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
66ea9427 4320
11e6e25d 4321 if (cmd) {
2922a94f 4322 cmd->cmd_complete(cmd, mgmt_status(status));
11e6e25d
MH
4323 mgmt_pending_remove(cmd);
4324 }
7c307720
AG
4325
4326 if (status) {
11e6e25d
MH
4327 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4328 goto unlock;
7c307720
AG
4329 }
4330
7c307720 4331 hci_discovery_set_state(hdev, DISCOVERY_FINDING);
7c307720 4332
2d28cfe7
JP
4333 /* If the scan involves LE scan, pick proper timeout to schedule
4334 * hdev->le_scan_disable that will stop it.
4335 */
7c307720
AG
4336 switch (hdev->discovery.type) {
4337 case DISCOV_TYPE_LE:
3d5a76f0 4338 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
7c307720 4339 break;
7c307720 4340 case DISCOV_TYPE_INTERLEAVED:
07d2334a
JP
4341 /* When running simultaneous discovery, the LE scanning time
4342 * should occupy the whole discovery time sine BR/EDR inquiry
4343 * and LE scanning are scheduled by the controller.
4344 *
4345 * For interleaving discovery in comparison, BR/EDR inquiry
4346 * and LE scanning are done sequentially with separate
4347 * timeouts.
4348 */
4349 if (test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
4350 timeout = msecs_to_jiffies(DISCOV_LE_TIMEOUT);
4351 else
4352 timeout = msecs_to_jiffies(hdev->discov_interleaved_timeout);
7c307720 4353 break;
7c307720 4354 case DISCOV_TYPE_BREDR:
11e6e25d 4355 timeout = 0;
7c307720 4356 break;
7c307720
AG
4357 default:
4358 BT_ERR("Invalid discovery type %d", hdev->discovery.type);
11e6e25d
MH
4359 timeout = 0;
4360 break;
7c307720 4361 }
ae55f598 4362
2d28cfe7
JP
4363 if (timeout) {
4364 /* When service discovery is used and the controller has
4365 * a strict duplicate filter, it is important to remember
4366 * the start and duration of the scan. This is required
4367 * for restarting scanning during the discovery phase.
4368 */
4369 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER,
4370 &hdev->quirks) &&
82f8b651 4371 hdev->discovery.result_filtering) {
2d28cfe7
JP
4372 hdev->discovery.scan_start = jiffies;
4373 hdev->discovery.scan_duration = timeout;
4374 }
4375
11e6e25d
MH
4376 queue_delayed_work(hdev->workqueue,
4377 &hdev->le_scan_disable, timeout);
2d28cfe7 4378 }
ae55f598 4379
11e6e25d
MH
4380unlock:
4381 hci_dev_unlock(hdev);
7c307720
AG
4382}
4383
bdb6d971 4384static int start_discovery(struct sock *sk, struct hci_dev *hdev,
04124681 4385 void *data, u16 len)
14a53664 4386{
650f726d 4387 struct mgmt_cp_start_discovery *cp = data;
3b0602cd 4388 struct mgmt_pending_cmd *cmd;
7c307720 4389 struct hci_request req;
8019044d 4390 u8 status;
14a53664
JH
4391 int err;
4392
bdb6d971 4393 BT_DBG("%s", hdev->name);
14a53664 4394
09fd0de5 4395 hci_dev_lock(hdev);
14a53664 4396
4b34ee78 4397 if (!hdev_is_powered(hdev)) {
2a1afb5a
JH
4398 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4399 MGMT_STATUS_NOT_POWERED,
4400 &cp->type, sizeof(cp->type));
bd2d1334
JH
4401 goto failed;
4402 }
4403
f5a969f2 4404 if (hdev->discovery.state != DISCOVERY_STOPPED ||
d7a5a11d 4405 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
2a1afb5a
JH
4406 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4407 MGMT_STATUS_BUSY, &cp->type,
4408 sizeof(cp->type));
642be6c7
AG
4409 goto failed;
4410 }
4411
2922a94f 4412 cmd = mgmt_pending_add(sk, MGMT_OP_START_DISCOVERY, hdev, data, len);
14a53664
JH
4413 if (!cmd) {
4414 err = -ENOMEM;
4415 goto failed;
4416 }
4417
2922a94f
JH
4418 cmd->cmd_complete = generic_cmd_complete;
4419
22078800
MH
4420 /* Clear the discovery filter first to free any previously
4421 * allocated memory for the UUID list.
4422 */
4423 hci_discovery_filter_clear(hdev);
4424
4aab14e5 4425 hdev->discovery.type = cp->type;
da25cf6a 4426 hdev->discovery.report_invalid_rssi = false;
4aab14e5 4427
7c307720
AG
4428 hci_req_init(&req, hdev);
4429
8019044d 4430 if (!trigger_discovery(&req, &status)) {
2a1afb5a
JH
4431 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_START_DISCOVERY,
4432 status, &cp->type, sizeof(cp->type));
04106755
JH
4433 mgmt_pending_remove(cmd);
4434 goto failed;
f39799f5 4435 }
04106755 4436
7c307720 4437 err = hci_req_run(&req, start_discovery_complete);
f5a969f2 4438 if (err < 0) {
14a53664 4439 mgmt_pending_remove(cmd);
f5a969f2
MH
4440 goto failed;
4441 }
7c307720 4442
f5a969f2 4443 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
7c307720 4444
14a53664 4445failed:
09fd0de5 4446 hci_dev_unlock(hdev);
14a53664
JH
4447 return err;
4448}
f39799f5 4449
3b0602cd
JH
4450static int service_discovery_cmd_complete(struct mgmt_pending_cmd *cmd,
4451 u8 status)
2922a94f 4452{
2a1afb5a
JH
4453 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
4454 cmd->param, 1);
2922a94f 4455}
04106755 4456
66ea9427
JP
4457static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
4458 void *data, u16 len)
4459{
4460 struct mgmt_cp_start_service_discovery *cp = data;
3b0602cd 4461 struct mgmt_pending_cmd *cmd;
66ea9427
JP
4462 struct hci_request req;
4463 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
4464 u16 uuid_count, expected_len;
4465 u8 status;
4466 int err;
04106755 4467
66ea9427 4468 BT_DBG("%s", hdev->name);
e8bb6b97 4469
66ea9427 4470 hci_dev_lock(hdev);
7c307720 4471
66ea9427 4472 if (!hdev_is_powered(hdev)) {
2a1afb5a
JH
4473 err = mgmt_cmd_complete(sk, hdev->id,
4474 MGMT_OP_START_SERVICE_DISCOVERY,
4475 MGMT_STATUS_NOT_POWERED,
4476 &cp->type, sizeof(cp->type));
66ea9427
JP
4477 goto failed;
4478 }
7c307720 4479
66ea9427 4480 if (hdev->discovery.state != DISCOVERY_STOPPED ||
d7a5a11d 4481 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
2a1afb5a
JH
4482 err = mgmt_cmd_complete(sk, hdev->id,
4483 MGMT_OP_START_SERVICE_DISCOVERY,
4484 MGMT_STATUS_BUSY, &cp->type,
4485 sizeof(cp->type));
66ea9427
JP
4486 goto failed;
4487 }
d9483943 4488
66ea9427
JP
4489 uuid_count = __le16_to_cpu(cp->uuid_count);
4490 if (uuid_count > max_uuid_count) {
4491 BT_ERR("service_discovery: too big uuid_count value %u",
4492 uuid_count);
2a1afb5a
JH
4493 err = mgmt_cmd_complete(sk, hdev->id,
4494 MGMT_OP_START_SERVICE_DISCOVERY,
4495 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4496 sizeof(cp->type));
66ea9427
JP
4497 goto failed;
4498 }
4499
4500 expected_len = sizeof(*cp) + uuid_count * 16;
4501 if (expected_len != len) {
4502 BT_ERR("service_discovery: expected %u bytes, got %u bytes",
4503 expected_len, len);
2a1afb5a
JH
4504 err = mgmt_cmd_complete(sk, hdev->id,
4505 MGMT_OP_START_SERVICE_DISCOVERY,
4506 MGMT_STATUS_INVALID_PARAMS, &cp->type,
4507 sizeof(cp->type));
66ea9427
JP
4508 goto failed;
4509 }
4510
4511 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
2922a94f 4512 hdev, data, len);
66ea9427
JP
4513 if (!cmd) {
4514 err = -ENOMEM;
4515 goto failed;
4516 }
4517
2922a94f
JH
4518 cmd->cmd_complete = service_discovery_cmd_complete;
4519
22078800
MH
4520 /* Clear the discovery filter first to free any previously
4521 * allocated memory for the UUID list.
4522 */
4523 hci_discovery_filter_clear(hdev);
4524
82f8b651 4525 hdev->discovery.result_filtering = true;
66ea9427
JP
4526 hdev->discovery.type = cp->type;
4527 hdev->discovery.rssi = cp->rssi;
4528 hdev->discovery.uuid_count = uuid_count;
4529
4530 if (uuid_count > 0) {
4531 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
4532 GFP_KERNEL);
4533 if (!hdev->discovery.uuids) {
2a1afb5a
JH
4534 err = mgmt_cmd_complete(sk, hdev->id,
4535 MGMT_OP_START_SERVICE_DISCOVERY,
4536 MGMT_STATUS_FAILED,
4537 &cp->type, sizeof(cp->type));
d9483943
JH
4538 mgmt_pending_remove(cmd);
4539 goto failed;
4540 }
66ea9427 4541 }
d9483943 4542
66ea9427 4543 hci_req_init(&req, hdev);
5e0452c0 4544
66ea9427 4545 if (!trigger_discovery(&req, &status)) {
2a1afb5a
JH
4546 err = mgmt_cmd_complete(sk, hdev->id,
4547 MGMT_OP_START_SERVICE_DISCOVERY,
4548 status, &cp->type, sizeof(cp->type));
04106755
JH
4549 mgmt_pending_remove(cmd);
4550 goto failed;
f39799f5 4551 }
3fd24153 4552
7c307720 4553 err = hci_req_run(&req, start_discovery_complete);
66ea9427 4554 if (err < 0) {
14a53664 4555 mgmt_pending_remove(cmd);
66ea9427
JP
4556 goto failed;
4557 }
4558
4559 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
14a53664
JH
4560
4561failed:
09fd0de5 4562 hci_dev_unlock(hdev);
14a53664
JH
4563 return err;
4564}
4565
1904a853 4566static void stop_discovery_complete(struct hci_dev *hdev, u8 status, u16 opcode)
1183fdca 4567{
3b0602cd 4568 struct mgmt_pending_cmd *cmd;
1183fdca 4569
0e05bba6
AG
4570 BT_DBG("status %d", status);
4571
4572 hci_dev_lock(hdev);
4573
333ae95d 4574 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
11e6e25d 4575 if (cmd) {
2922a94f 4576 cmd->cmd_complete(cmd, mgmt_status(status));
11e6e25d 4577 mgmt_pending_remove(cmd);
0e05bba6
AG
4578 }
4579
11e6e25d
MH
4580 if (!status)
4581 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
0e05bba6 4582
0e05bba6
AG
4583 hci_dev_unlock(hdev);
4584}
4585
bdb6d971 4586static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 4587 u16 len)
14a53664 4588{
d930650b 4589 struct mgmt_cp_stop_discovery *mgmt_cp = data;
3b0602cd 4590 struct mgmt_pending_cmd *cmd;
0e05bba6 4591 struct hci_request req;
14a53664
JH
4592 int err;
4593
bdb6d971 4594 BT_DBG("%s", hdev->name);
14a53664 4595
09fd0de5 4596 hci_dev_lock(hdev);
14a53664 4597
30dc78e1 4598 if (!hci_discovery_active(hdev)) {
2a1afb5a
JH
4599 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4600 MGMT_STATUS_REJECTED, &mgmt_cp->type,
4601 sizeof(mgmt_cp->type));
d930650b
JH
4602 goto unlock;
4603 }
4604
4605 if (hdev->discovery.type != mgmt_cp->type) {
2a1afb5a
JH
4606 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
4607 MGMT_STATUS_INVALID_PARAMS,
4608 &mgmt_cp->type, sizeof(mgmt_cp->type));
30dc78e1 4609 goto unlock;
ff9ef578
JH
4610 }
4611
2922a94f 4612 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
14a53664
JH
4613 if (!cmd) {
4614 err = -ENOMEM;
30dc78e1
JH
4615 goto unlock;
4616 }
4617
2922a94f
JH
4618 cmd->cmd_complete = generic_cmd_complete;
4619
0e05bba6
AG
4620 hci_req_init(&req, hdev);
4621
21a60d30 4622 hci_stop_discovery(&req);
e0d9727e 4623
21a60d30
JH
4624 err = hci_req_run(&req, stop_discovery_complete);
4625 if (!err) {
4626 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
0e05bba6 4627 goto unlock;
14a53664
JH
4628 }
4629
21a60d30
JH
4630 mgmt_pending_remove(cmd);
4631
4632 /* If no HCI commands were sent we're done */
4633 if (err == -ENODATA) {
2a1afb5a
JH
4634 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY, 0,
4635 &mgmt_cp->type, sizeof(mgmt_cp->type));
21a60d30
JH
4636 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
4637 }
14a53664 4638
30dc78e1 4639unlock:
09fd0de5 4640 hci_dev_unlock(hdev);
14a53664
JH
4641 return err;
4642}
4643
bdb6d971 4644static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 4645 u16 len)
561aafbc 4646{
650f726d 4647 struct mgmt_cp_confirm_name *cp = data;
561aafbc 4648 struct inquiry_entry *e;
561aafbc
JH
4649 int err;
4650
bdb6d971 4651 BT_DBG("%s", hdev->name);
561aafbc 4652
561aafbc
JH
4653 hci_dev_lock(hdev);
4654
30dc78e1 4655 if (!hci_discovery_active(hdev)) {
2a1afb5a
JH
4656 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4657 MGMT_STATUS_FAILED, &cp->addr,
4658 sizeof(cp->addr));
30dc78e1
JH
4659 goto failed;
4660 }
4661
a198e7b1 4662 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
561aafbc 4663 if (!e) {
2a1afb5a
JH
4664 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
4665 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
4666 sizeof(cp->addr));
561aafbc
JH
4667 goto failed;
4668 }
4669
4670 if (cp->name_known) {
4671 e->name_state = NAME_KNOWN;
4672 list_del(&e->list);
4673 } else {
4674 e->name_state = NAME_NEEDED;
a3d4e20a 4675 hci_inquiry_cache_update_resolve(hdev, e);
561aafbc
JH
4676 }
4677
2a1afb5a
JH
4678 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
4679 &cp->addr, sizeof(cp->addr));
561aafbc
JH
4680
4681failed:
4682 hci_dev_unlock(hdev);
561aafbc
JH
4683 return err;
4684}
4685
bdb6d971 4686static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 4687 u16 len)
7fbec224 4688{
650f726d 4689 struct mgmt_cp_block_device *cp = data;
f0eeea8b 4690 u8 status;
7fbec224
AJ
4691 int err;
4692
bdb6d971 4693 BT_DBG("%s", hdev->name);
7fbec224 4694
4ee71b20 4695 if (!bdaddr_type_is_valid(cp->addr.type))
2a1afb5a
JH
4696 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
4697 MGMT_STATUS_INVALID_PARAMS,
4698 &cp->addr, sizeof(cp->addr));
4ee71b20 4699
09fd0de5 4700 hci_dev_lock(hdev);
5e762444 4701
dcc36c16
JH
4702 err = hci_bdaddr_list_add(&hdev->blacklist, &cp->addr.bdaddr,
4703 cp->addr.type);
2a8357f2 4704 if (err < 0) {
f0eeea8b 4705 status = MGMT_STATUS_FAILED;
2a8357f2
JH
4706 goto done;
4707 }
4708
4709 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4710 sk);
4711 status = MGMT_STATUS_SUCCESS;
f0eeea8b 4712
2a8357f2 4713done:
2a1afb5a
JH
4714 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
4715 &cp->addr, sizeof(cp->addr));
5e762444 4716
09fd0de5 4717 hci_dev_unlock(hdev);
7fbec224
AJ
4718
4719 return err;
4720}
4721
bdb6d971 4722static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
04124681 4723 u16 len)
7fbec224 4724{
650f726d 4725 struct mgmt_cp_unblock_device *cp = data;
f0eeea8b 4726 u8 status;
7fbec224
AJ
4727 int err;
4728
bdb6d971 4729 BT_DBG("%s", hdev->name);
7fbec224 4730
4ee71b20 4731 if (!bdaddr_type_is_valid(cp->addr.type))
2a1afb5a
JH
4732 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
4733 MGMT_STATUS_INVALID_PARAMS,
4734 &cp->addr, sizeof(cp->addr));
4ee71b20 4735
09fd0de5 4736 hci_dev_lock(hdev);
5e762444 4737
dcc36c16
JH
4738 err = hci_bdaddr_list_del(&hdev->blacklist, &cp->addr.bdaddr,
4739 cp->addr.type);
2a8357f2 4740 if (err < 0) {
f0eeea8b 4741 status = MGMT_STATUS_INVALID_PARAMS;
2a8357f2
JH
4742 goto done;
4743 }
4744
4745 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
4746 sk);
4747 status = MGMT_STATUS_SUCCESS;
f0eeea8b 4748
2a8357f2 4749done:
2a1afb5a
JH
4750 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
4751 &cp->addr, sizeof(cp->addr));
5e762444 4752
09fd0de5 4753 hci_dev_unlock(hdev);
7fbec224
AJ
4754
4755 return err;
4756}
4757
cdbaccca
MH
4758static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
4759 u16 len)
4760{
4761 struct mgmt_cp_set_device_id *cp = data;
890ea898 4762 struct hci_request req;
cdbaccca 4763 int err;
c72d4b8a 4764 __u16 source;
cdbaccca
MH
4765
4766 BT_DBG("%s", hdev->name);
4767
c72d4b8a
SJ
4768 source = __le16_to_cpu(cp->source);
4769
4770 if (source > 0x0002)
a69e8375
JH
4771 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
4772 MGMT_STATUS_INVALID_PARAMS);
c72d4b8a 4773
cdbaccca
MH
4774 hci_dev_lock(hdev);
4775
c72d4b8a 4776 hdev->devid_source = source;
cdbaccca
MH
4777 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
4778 hdev->devid_product = __le16_to_cpu(cp->product);
4779 hdev->devid_version = __le16_to_cpu(cp->version);
4780
2a1afb5a
JH
4781 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
4782 NULL, 0);
cdbaccca 4783
890ea898
JH
4784 hci_req_init(&req, hdev);
4785 update_eir(&req);
4786 hci_req_run(&req, NULL);
cdbaccca
MH
4787
4788 hci_dev_unlock(hdev);
4789
4790 return err;
4791}
4792
24b4f38f
AU
4793static void enable_advertising_instance(struct hci_dev *hdev, u8 status,
4794 u16 opcode)
4795{
4796 BT_DBG("status %d", status);
4797}
4798
1904a853
MH
4799static void set_advertising_complete(struct hci_dev *hdev, u8 status,
4800 u16 opcode)
4375f103
JH
4801{
4802 struct cmd_lookup match = { NULL, hdev };
24b4f38f 4803 struct hci_request req;
7816b820
FG
4804 u8 instance;
4805 struct adv_info *adv_instance;
4806 int err;
4375f103 4807
3ad67582
JK
4808 hci_dev_lock(hdev);
4809
4375f103
JH
4810 if (status) {
4811 u8 mgmt_err = mgmt_status(status);
4812
4813 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
4814 cmd_status_rsp, &mgmt_err);
3ad67582 4815 goto unlock;
4375f103
JH
4816 }
4817
d7a5a11d 4818 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
a1536da2 4819 hci_dev_set_flag(hdev, HCI_ADVERTISING);
c93bd150 4820 else
a358dc11 4821 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
c93bd150 4822
4375f103
JH
4823 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
4824 &match);
4825
4826 new_settings(hdev, match.sk);
4827
4828 if (match.sk)
4829 sock_put(match.sk);
3ad67582 4830
24b4f38f 4831 /* If "Set Advertising" was just disabled and instance advertising was
7816b820 4832 * set up earlier, then re-enable multi-instance advertising.
24b4f38f
AU
4833 */
4834 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
7816b820
FG
4835 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) ||
4836 list_empty(&hdev->adv_instances))
24b4f38f
AU
4837 goto unlock;
4838
7816b820
FG
4839 instance = hdev->cur_adv_instance;
4840 if (!instance) {
4841 adv_instance = list_first_entry_or_null(&hdev->adv_instances,
4842 struct adv_info, list);
4843 if (!adv_instance)
4844 goto unlock;
4845
4846 instance = adv_instance->instance;
4847 }
4848
24b4f38f
AU
4849 hci_req_init(&req, hdev);
4850
7816b820
FG
4851 err = schedule_adv_instance(&req, instance, true);
4852
4853 if (!err)
4854 err = hci_req_run(&req, enable_advertising_instance);
24b4f38f 4855
7816b820 4856 if (err)
24b4f38f
AU
4857 BT_ERR("Failed to re-configure advertising");
4858
3ad67582
JK
4859unlock:
4860 hci_dev_unlock(hdev);
4375f103
JH
4861}
4862
21b5187f
MH
4863static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
4864 u16 len)
4375f103
JH
4865{
4866 struct mgmt_mode *cp = data;
3b0602cd 4867 struct mgmt_pending_cmd *cmd;
4375f103 4868 struct hci_request req;
cc91cb04 4869 u8 val, status;
4375f103
JH
4870 int err;
4871
4872 BT_DBG("request for %s", hdev->name);
4873
e6fe7986
JH
4874 status = mgmt_le_support(hdev);
4875 if (status)
a69e8375
JH
4876 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4877 status);
4375f103 4878
cc91cb04 4879 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
a69e8375
JH
4880 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4881 MGMT_STATUS_INVALID_PARAMS);
4375f103
JH
4882
4883 hci_dev_lock(hdev);
4884
4885 val = !!cp->val;
4375f103 4886
f74ca9b8
JH
4887 /* The following conditions are ones which mean that we should
4888 * not do any HCI communication but directly send a mgmt
4889 * response to user space (after toggling the flag if
4890 * necessary).
4891 */
cc91cb04 4892 if (!hdev_is_powered(hdev) ||
d7a5a11d
MH
4893 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
4894 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
e8bb6b97 4895 hci_conn_num(hdev, LE_LINK) > 0 ||
d7a5a11d 4896 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
e8bb6b97 4897 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
cc91cb04 4898 bool changed;
4375f103 4899
cc91cb04 4900 if (cp->val) {
238be788 4901 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
cc91cb04 4902 if (cp->val == 0x02)
a1536da2 4903 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
cc91cb04 4904 else
a358dc11 4905 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
cc91cb04 4906 } else {
a69d8927 4907 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
a358dc11 4908 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
4375f103
JH
4909 }
4910
4911 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
4912 if (err < 0)
4913 goto unlock;
4914
4915 if (changed)
4916 err = new_settings(hdev, sk);
4917
4918 goto unlock;
4919 }
4920
333ae95d
JH
4921 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
4922 pending_find(MGMT_OP_SET_LE, hdev)) {
a69e8375
JH
4923 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
4924 MGMT_STATUS_BUSY);
4375f103
JH
4925 goto unlock;
4926 }
4927
4928 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
4929 if (!cmd) {
4930 err = -ENOMEM;
4931 goto unlock;
4932 }
4933
4934 hci_req_init(&req, hdev);
4935
cc91cb04 4936 if (cp->val == 0x02)
a1536da2 4937 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
cc91cb04 4938 else
a358dc11 4939 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
cc91cb04 4940
7816b820
FG
4941 cancel_adv_timeout(hdev);
4942
24b4f38f 4943 if (val) {
7816b820
FG
4944 /* Switch to instance "0" for the Set Advertising setting.
4945 * We cannot use update_[adv|scan_rsp]_data() here as the
4946 * HCI_ADVERTISING flag is not yet set.
4947 */
efae002c
FG
4948 update_inst_adv_data(&req, 0x00);
4949 update_inst_scan_rsp_data(&req, 0x00);
bba3aa55 4950 enable_advertising(&req);
24b4f38f 4951 } else {
bba3aa55 4952 disable_advertising(&req);
24b4f38f 4953 }
4375f103
JH
4954
4955 err = hci_req_run(&req, set_advertising_complete);
4956 if (err < 0)
4957 mgmt_pending_remove(cmd);
4958
4959unlock:
4960 hci_dev_unlock(hdev);
4961 return err;
4962}
4963
d13eafce
MH
4964static int set_static_address(struct sock *sk, struct hci_dev *hdev,
4965 void *data, u16 len)
4966{
4967 struct mgmt_cp_set_static_address *cp = data;
4968 int err;
4969
4970 BT_DBG("%s", hdev->name);
4971
62af4443 4972 if (!lmp_le_capable(hdev))
a69e8375
JH
4973 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4974 MGMT_STATUS_NOT_SUPPORTED);
d13eafce
MH
4975
4976 if (hdev_is_powered(hdev))
a69e8375
JH
4977 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
4978 MGMT_STATUS_REJECTED);
d13eafce
MH
4979
4980 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
4981 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
a69e8375
JH
4982 return mgmt_cmd_status(sk, hdev->id,
4983 MGMT_OP_SET_STATIC_ADDRESS,
4984 MGMT_STATUS_INVALID_PARAMS);
d13eafce
MH
4985
4986 /* Two most significant bits shall be set */
4987 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
a69e8375
JH
4988 return mgmt_cmd_status(sk, hdev->id,
4989 MGMT_OP_SET_STATIC_ADDRESS,
4990 MGMT_STATUS_INVALID_PARAMS);
d13eafce
MH
4991 }
4992
4993 hci_dev_lock(hdev);
4994
4995 bacpy(&hdev->static_addr, &cp->bdaddr);
4996
93690c22
MH
4997 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
4998 if (err < 0)
4999 goto unlock;
5000
5001 err = new_settings(hdev, sk);
d13eafce 5002
93690c22 5003unlock:
d13eafce 5004 hci_dev_unlock(hdev);
d13eafce
MH
5005 return err;
5006}
5007
14b49b9a
MH
5008static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
5009 void *data, u16 len)
5010{
5011 struct mgmt_cp_set_scan_params *cp = data;
5012 __u16 interval, window;
5013 int err;
5014
5015 BT_DBG("%s", hdev->name);
5016
5017 if (!lmp_le_capable(hdev))
a69e8375
JH
5018 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5019 MGMT_STATUS_NOT_SUPPORTED);
14b49b9a
MH
5020
5021 interval = __le16_to_cpu(cp->interval);
5022
5023 if (interval < 0x0004 || interval > 0x4000)
a69e8375
JH
5024 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5025 MGMT_STATUS_INVALID_PARAMS);
14b49b9a
MH
5026
5027 window = __le16_to_cpu(cp->window);
5028
5029 if (window < 0x0004 || window > 0x4000)
a69e8375
JH
5030 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5031 MGMT_STATUS_INVALID_PARAMS);
14b49b9a 5032
899e1075 5033 if (window > interval)
a69e8375
JH
5034 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
5035 MGMT_STATUS_INVALID_PARAMS);
899e1075 5036
14b49b9a
MH
5037 hci_dev_lock(hdev);
5038
5039 hdev->le_scan_interval = interval;
5040 hdev->le_scan_window = window;
5041
2a1afb5a
JH
5042 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
5043 NULL, 0);
14b49b9a 5044
dd2ef8e2
AG
5045 /* If background scan is running, restart it so new parameters are
5046 * loaded.
5047 */
d7a5a11d 5048 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
dd2ef8e2
AG
5049 hdev->discovery.state == DISCOVERY_STOPPED) {
5050 struct hci_request req;
5051
5052 hci_req_init(&req, hdev);
5053
5054 hci_req_add_le_scan_disable(&req);
5055 hci_req_add_le_passive_scan(&req);
5056
5057 hci_req_run(&req, NULL);
5058 }
5059
14b49b9a
MH
5060 hci_dev_unlock(hdev);
5061
5062 return err;
5063}
5064
1904a853
MH
5065static void fast_connectable_complete(struct hci_dev *hdev, u8 status,
5066 u16 opcode)
33e38b3e 5067{
3b0602cd 5068 struct mgmt_pending_cmd *cmd;
33e38b3e
JH
5069
5070 BT_DBG("status 0x%02x", status);
5071
5072 hci_dev_lock(hdev);
5073
333ae95d 5074 cmd = pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev);
33e38b3e
JH
5075 if (!cmd)
5076 goto unlock;
5077
5078 if (status) {
a69e8375
JH
5079 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5080 mgmt_status(status));
33e38b3e 5081 } else {
1a4d3c4b
JH
5082 struct mgmt_mode *cp = cmd->param;
5083
5084 if (cp->val)
a1536da2 5085 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
1a4d3c4b 5086 else
a358dc11 5087 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
1a4d3c4b 5088
33e38b3e
JH
5089 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
5090 new_settings(hdev, cmd->sk);
5091 }
5092
5093 mgmt_pending_remove(cmd);
5094
5095unlock:
5096 hci_dev_unlock(hdev);
5097}
5098
bdb6d971 5099static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
04124681 5100 void *data, u16 len)
f6422ec6 5101{
650f726d 5102 struct mgmt_mode *cp = data;
3b0602cd 5103 struct mgmt_pending_cmd *cmd;
33e38b3e 5104 struct hci_request req;
f6422ec6
AJ
5105 int err;
5106
bdb6d971 5107 BT_DBG("%s", hdev->name);
f6422ec6 5108
d7a5a11d 5109 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
56f87901 5110 hdev->hci_ver < BLUETOOTH_VER_1_2)
a69e8375
JH
5111 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5112 MGMT_STATUS_NOT_SUPPORTED);
33c525c0 5113
a7e80f25 5114 if (cp->val != 0x00 && cp->val != 0x01)
a69e8375
JH
5115 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5116 MGMT_STATUS_INVALID_PARAMS);
a7e80f25 5117
f6422ec6
AJ
5118 hci_dev_lock(hdev);
5119
333ae95d 5120 if (pending_find(MGMT_OP_SET_FAST_CONNECTABLE, hdev)) {
a69e8375
JH
5121 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5122 MGMT_STATUS_BUSY);
05cbf29f
JH
5123 goto unlock;
5124 }
5125
d7a5a11d 5126 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
1a4d3c4b
JH
5127 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
5128 hdev);
5129 goto unlock;
5130 }
5131
406ef2a6 5132 if (!hdev_is_powered(hdev)) {
ce05d603 5133 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
406ef2a6
JH
5134 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE,
5135 hdev);
5136 new_settings(hdev, sk);
5137 goto unlock;
5138 }
5139
33e38b3e
JH
5140 cmd = mgmt_pending_add(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev,
5141 data, len);
5142 if (!cmd) {
5143 err = -ENOMEM;
5144 goto unlock;
f6422ec6
AJ
5145 }
5146
33e38b3e
JH
5147 hci_req_init(&req, hdev);
5148
406d7804 5149 write_fast_connectable(&req, cp->val);
33e38b3e
JH
5150
5151 err = hci_req_run(&req, fast_connectable_complete);
f6422ec6 5152 if (err < 0) {
a69e8375
JH
5153 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
5154 MGMT_STATUS_FAILED);
33e38b3e 5155 mgmt_pending_remove(cmd);
f6422ec6
AJ
5156 }
5157
33e38b3e 5158unlock:
f6422ec6 5159 hci_dev_unlock(hdev);
33e38b3e 5160
f6422ec6
AJ
5161 return err;
5162}
5163
1904a853 5164static void set_bredr_complete(struct hci_dev *hdev, u8 status, u16 opcode)
0663ca2a 5165{
3b0602cd 5166 struct mgmt_pending_cmd *cmd;
0663ca2a
JH
5167
5168 BT_DBG("status 0x%02x", status);
5169
5170 hci_dev_lock(hdev);
5171
333ae95d 5172 cmd = pending_find(MGMT_OP_SET_BREDR, hdev);
0663ca2a
JH
5173 if (!cmd)
5174 goto unlock;
5175
5176 if (status) {
5177 u8 mgmt_err = mgmt_status(status);
5178
5179 /* We need to restore the flag if related HCI commands
5180 * failed.
5181 */
a358dc11 5182 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
0663ca2a 5183
a69e8375 5184 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
0663ca2a
JH
5185 } else {
5186 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
5187 new_settings(hdev, cmd->sk);
5188 }
5189
5190 mgmt_pending_remove(cmd);
5191
5192unlock:
5193 hci_dev_unlock(hdev);
5194}
5195
5196static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
5197{
5198 struct mgmt_mode *cp = data;
3b0602cd 5199 struct mgmt_pending_cmd *cmd;
0663ca2a
JH
5200 struct hci_request req;
5201 int err;
5202
5203 BT_DBG("request for %s", hdev->name);
5204
5205 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
a69e8375
JH
5206 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5207 MGMT_STATUS_NOT_SUPPORTED);
0663ca2a 5208
d7a5a11d 5209 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
a69e8375
JH
5210 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5211 MGMT_STATUS_REJECTED);
0663ca2a
JH
5212
5213 if (cp->val != 0x00 && cp->val != 0x01)
a69e8375
JH
5214 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5215 MGMT_STATUS_INVALID_PARAMS);
0663ca2a
JH
5216
5217 hci_dev_lock(hdev);
5218
d7a5a11d 5219 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
0663ca2a
JH
5220 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5221 goto unlock;
5222 }
5223
5224 if (!hdev_is_powered(hdev)) {
5225 if (!cp->val) {
a358dc11
MH
5226 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
5227 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
5228 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
5229 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
5230 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
0663ca2a
JH
5231 }
5232
ce05d603 5233 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
0663ca2a
JH
5234
5235 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
5236 if (err < 0)
5237 goto unlock;
5238
5239 err = new_settings(hdev, sk);
5240 goto unlock;
5241 }
5242
5243 /* Reject disabling when powered on */
5244 if (!cp->val) {
a69e8375
JH
5245 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5246 MGMT_STATUS_REJECTED);
0663ca2a 5247 goto unlock;
111e4bcc
MH
5248 } else {
5249 /* When configuring a dual-mode controller to operate
5250 * with LE only and using a static address, then switching
5251 * BR/EDR back on is not allowed.
5252 *
5253 * Dual-mode controllers shall operate with the public
5254 * address as its identity address for BR/EDR and LE. So
5255 * reject the attempt to create an invalid configuration.
3a5486e1
MH
5256 *
5257 * The same restrictions applies when secure connections
5258 * has been enabled. For BR/EDR this is a controller feature
5259 * while for LE it is a host stack feature. This means that
5260 * switching BR/EDR back on when secure connections has been
5261 * enabled is not a supported transaction.
111e4bcc 5262 */
d7a5a11d 5263 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
3a5486e1 5264 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
d7a5a11d 5265 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
a69e8375
JH
5266 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5267 MGMT_STATUS_REJECTED);
111e4bcc
MH
5268 goto unlock;
5269 }
0663ca2a
JH
5270 }
5271
333ae95d 5272 if (pending_find(MGMT_OP_SET_BREDR, hdev)) {
a69e8375
JH
5273 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
5274 MGMT_STATUS_BUSY);
0663ca2a
JH
5275 goto unlock;
5276 }
5277
5278 cmd = mgmt_pending_add(sk, MGMT_OP_SET_BREDR, hdev, data, len);
5279 if (!cmd) {
5280 err = -ENOMEM;
5281 goto unlock;
5282 }
5283
5947f4bc 5284 /* We need to flip the bit already here so that update_adv_data
0663ca2a
JH
5285 * generates the correct flags.
5286 */
a1536da2 5287 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
0663ca2a
JH
5288
5289 hci_req_init(&req, hdev);
aa8af46e 5290
432df05e 5291 write_fast_connectable(&req, false);
1d2dc5b7 5292 __hci_update_page_scan(&req);
aa8af46e 5293
f14d8f64
MH
5294 /* Since only the advertising data flags will change, there
5295 * is no need to update the scan response data.
5296 */
5947f4bc 5297 update_adv_data(&req);
aa8af46e 5298
0663ca2a
JH
5299 err = hci_req_run(&req, set_bredr_complete);
5300 if (err < 0)
5301 mgmt_pending_remove(cmd);
5302
5303unlock:
5304 hci_dev_unlock(hdev);
5305 return err;
5306}
5307
a1443f5a
JH
5308static void sc_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5309{
3b0602cd 5310 struct mgmt_pending_cmd *cmd;
a1443f5a
JH
5311 struct mgmt_mode *cp;
5312
5313 BT_DBG("%s status %u", hdev->name, status);
5314
5315 hci_dev_lock(hdev);
5316
333ae95d 5317 cmd = pending_find(MGMT_OP_SET_SECURE_CONN, hdev);
a1443f5a
JH
5318 if (!cmd)
5319 goto unlock;
5320
5321 if (status) {
a69e8375
JH
5322 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
5323 mgmt_status(status));
a1443f5a
JH
5324 goto remove;
5325 }
5326
5327 cp = cmd->param;
5328
5329 switch (cp->val) {
5330 case 0x00:
a358dc11
MH
5331 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
5332 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
a1443f5a
JH
5333 break;
5334 case 0x01:
a1536da2 5335 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
a358dc11 5336 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
a1443f5a
JH
5337 break;
5338 case 0x02:
a1536da2
MH
5339 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
5340 hci_dev_set_flag(hdev, HCI_SC_ONLY);
a1443f5a
JH
5341 break;
5342 }
5343
5344 send_settings_rsp(cmd->sk, MGMT_OP_SET_SECURE_CONN, hdev);
5345 new_settings(hdev, cmd->sk);
5346
5347remove:
5348 mgmt_pending_remove(cmd);
5349unlock:
5350 hci_dev_unlock(hdev);
5351}
5352
eac83dc6
MH
5353static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
5354 void *data, u16 len)
5355{
5356 struct mgmt_mode *cp = data;
3b0602cd 5357 struct mgmt_pending_cmd *cmd;
a1443f5a 5358 struct hci_request req;
a3209694 5359 u8 val;
eac83dc6
MH
5360 int err;
5361
5362 BT_DBG("request for %s", hdev->name);
5363
05b3c3e7 5364 if (!lmp_sc_capable(hdev) &&
d7a5a11d 5365 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
a69e8375
JH
5366 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5367 MGMT_STATUS_NOT_SUPPORTED);
eac83dc6 5368
d7a5a11d 5369 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
59200286 5370 lmp_sc_capable(hdev) &&
d7a5a11d 5371 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
a69e8375
JH
5372 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5373 MGMT_STATUS_REJECTED);
ed93ec69 5374
0ab04a9c 5375 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
a69e8375 5376 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
eac83dc6
MH
5377 MGMT_STATUS_INVALID_PARAMS);
5378
5379 hci_dev_lock(hdev);
5380
05b3c3e7 5381 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
d7a5a11d 5382 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
eac83dc6
MH
5383 bool changed;
5384
0ab04a9c 5385 if (cp->val) {
238be788
MH
5386 changed = !hci_dev_test_and_set_flag(hdev,
5387 HCI_SC_ENABLED);
0ab04a9c 5388 if (cp->val == 0x02)
a1536da2 5389 hci_dev_set_flag(hdev, HCI_SC_ONLY);
0ab04a9c 5390 else
a358dc11 5391 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
0ab04a9c 5392 } else {
a69d8927
MH
5393 changed = hci_dev_test_and_clear_flag(hdev,
5394 HCI_SC_ENABLED);
a358dc11 5395 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
0ab04a9c 5396 }
eac83dc6
MH
5397
5398 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5399 if (err < 0)
5400 goto failed;
5401
5402 if (changed)
5403 err = new_settings(hdev, sk);
5404
5405 goto failed;
5406 }
5407
333ae95d 5408 if (pending_find(MGMT_OP_SET_SECURE_CONN, hdev)) {
a69e8375
JH
5409 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
5410 MGMT_STATUS_BUSY);
eac83dc6
MH
5411 goto failed;
5412 }
5413
0ab04a9c
MH
5414 val = !!cp->val;
5415
d7a5a11d
MH
5416 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
5417 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
eac83dc6
MH
5418 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
5419 goto failed;
5420 }
5421
5422 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
5423 if (!cmd) {
5424 err = -ENOMEM;
5425 goto failed;
5426 }
5427
a1443f5a
JH
5428 hci_req_init(&req, hdev);
5429 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT, 1, &val);
5430 err = hci_req_run(&req, sc_enable_complete);
eac83dc6
MH
5431 if (err < 0) {
5432 mgmt_pending_remove(cmd);
5433 goto failed;
5434 }
5435
5436failed:
5437 hci_dev_unlock(hdev);
5438 return err;
5439}
5440
4e39ac81
MH
5441static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
5442 void *data, u16 len)
5443{
5444 struct mgmt_mode *cp = data;
b9710979 5445 bool changed, use_changed;
4e39ac81
MH
5446 int err;
5447
5448 BT_DBG("request for %s", hdev->name);
5449
b9710979 5450 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
a69e8375
JH
5451 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
5452 MGMT_STATUS_INVALID_PARAMS);
4e39ac81
MH
5453
5454 hci_dev_lock(hdev);
5455
5456 if (cp->val)
238be788 5457 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
4e39ac81 5458 else
a69d8927
MH
5459 changed = hci_dev_test_and_clear_flag(hdev,
5460 HCI_KEEP_DEBUG_KEYS);
4e39ac81 5461
b9710979 5462 if (cp->val == 0x02)
238be788
MH
5463 use_changed = !hci_dev_test_and_set_flag(hdev,
5464 HCI_USE_DEBUG_KEYS);
b9710979 5465 else
a69d8927
MH
5466 use_changed = hci_dev_test_and_clear_flag(hdev,
5467 HCI_USE_DEBUG_KEYS);
b9710979
JH
5468
5469 if (hdev_is_powered(hdev) && use_changed &&
d7a5a11d 5470 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
b9710979
JH
5471 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
5472 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
5473 sizeof(mode), &mode);
5474 }
5475
4e39ac81
MH
5476 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
5477 if (err < 0)
5478 goto unlock;
5479
5480 if (changed)
5481 err = new_settings(hdev, sk);
5482
5483unlock:
5484 hci_dev_unlock(hdev);
5485 return err;
5486}
5487
62b04cd1
JH
5488static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5489 u16 len)
5490{
5491 struct mgmt_cp_set_privacy *cp = cp_data;
5492 bool changed;
5493 int err;
5494
5495 BT_DBG("request for %s", hdev->name);
5496
5497 if (!lmp_le_capable(hdev))
a69e8375
JH
5498 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5499 MGMT_STATUS_NOT_SUPPORTED);
62b04cd1
JH
5500
5501 if (cp->privacy != 0x00 && cp->privacy != 0x01)
a69e8375
JH
5502 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5503 MGMT_STATUS_INVALID_PARAMS);
62b04cd1
JH
5504
5505 if (hdev_is_powered(hdev))
a69e8375
JH
5506 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
5507 MGMT_STATUS_REJECTED);
62b04cd1
JH
5508
5509 hci_dev_lock(hdev);
5510
c21c0ea0
JH
5511 /* If user space supports this command it is also expected to
5512 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
5513 */
a1536da2 5514 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
c21c0ea0 5515
62b04cd1 5516 if (cp->privacy) {
238be788 5517 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
62b04cd1 5518 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
a1536da2 5519 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
62b04cd1 5520 } else {
a69d8927 5521 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
62b04cd1 5522 memset(hdev->irk, 0, sizeof(hdev->irk));
a358dc11 5523 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
62b04cd1
JH
5524 }
5525
5526 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
5527 if (err < 0)
5528 goto unlock;
5529
5530 if (changed)
5531 err = new_settings(hdev, sk);
5532
5533unlock:
5534 hci_dev_unlock(hdev);
5535 return err;
5536}
5537
41edf160
JH
5538static bool irk_is_valid(struct mgmt_irk_info *irk)
5539{
5540 switch (irk->addr.type) {
5541 case BDADDR_LE_PUBLIC:
5542 return true;
5543
5544 case BDADDR_LE_RANDOM:
5545 /* Two most significant bits shall be set */
5546 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5547 return false;
5548 return true;
5549 }
5550
5551 return false;
5552}
5553
5554static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
5555 u16 len)
5556{
5557 struct mgmt_cp_load_irks *cp = cp_data;
ba1d6936
JH
5558 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
5559 sizeof(struct mgmt_irk_info));
41edf160
JH
5560 u16 irk_count, expected_len;
5561 int i, err;
5562
5563 BT_DBG("request for %s", hdev->name);
5564
5565 if (!lmp_le_capable(hdev))
a69e8375
JH
5566 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5567 MGMT_STATUS_NOT_SUPPORTED);
41edf160
JH
5568
5569 irk_count = __le16_to_cpu(cp->irk_count);
ba1d6936
JH
5570 if (irk_count > max_irk_count) {
5571 BT_ERR("load_irks: too big irk_count value %u", irk_count);
a69e8375
JH
5572 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5573 MGMT_STATUS_INVALID_PARAMS);
ba1d6936 5574 }
41edf160
JH
5575
5576 expected_len = sizeof(*cp) + irk_count * sizeof(struct mgmt_irk_info);
5577 if (expected_len != len) {
5578 BT_ERR("load_irks: expected %u bytes, got %u bytes",
2606ecbc 5579 expected_len, len);
a69e8375
JH
5580 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
5581 MGMT_STATUS_INVALID_PARAMS);
41edf160
JH
5582 }
5583
5584 BT_DBG("%s irk_count %u", hdev->name, irk_count);
5585
5586 for (i = 0; i < irk_count; i++) {
5587 struct mgmt_irk_info *key = &cp->irks[i];
5588
5589 if (!irk_is_valid(key))
a69e8375
JH
5590 return mgmt_cmd_status(sk, hdev->id,
5591 MGMT_OP_LOAD_IRKS,
5592 MGMT_STATUS_INVALID_PARAMS);
41edf160
JH
5593 }
5594
5595 hci_dev_lock(hdev);
5596
5597 hci_smp_irks_clear(hdev);
5598
5599 for (i = 0; i < irk_count; i++) {
5600 struct mgmt_irk_info *irk = &cp->irks[i];
41edf160 5601
85813a7e
JH
5602 hci_add_irk(hdev, &irk->addr.bdaddr,
5603 le_addr_type(irk->addr.type), irk->val,
41edf160
JH
5604 BDADDR_ANY);
5605 }
5606
a1536da2 5607 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
41edf160 5608
2a1afb5a 5609 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
41edf160
JH
5610
5611 hci_dev_unlock(hdev);
5612
5613 return err;
5614}
5615
3f706b72
JH
5616static bool ltk_is_valid(struct mgmt_ltk_info *key)
5617{
5618 if (key->master != 0x00 && key->master != 0x01)
5619 return false;
490cb0b3
MH
5620
5621 switch (key->addr.type) {
5622 case BDADDR_LE_PUBLIC:
5623 return true;
5624
5625 case BDADDR_LE_RANDOM:
5626 /* Two most significant bits shall be set */
5627 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
5628 return false;
5629 return true;
5630 }
5631
5632 return false;
3f706b72
JH
5633}
5634
bdb6d971 5635static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
04124681 5636 void *cp_data, u16 len)
346af67b 5637{
346af67b 5638 struct mgmt_cp_load_long_term_keys *cp = cp_data;
ba1d6936
JH
5639 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
5640 sizeof(struct mgmt_ltk_info));
346af67b 5641 u16 key_count, expected_len;
715a5bf2 5642 int i, err;
346af67b 5643
cf99ba13
MH
5644 BT_DBG("request for %s", hdev->name);
5645
5646 if (!lmp_le_capable(hdev))
a69e8375
JH
5647 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5648 MGMT_STATUS_NOT_SUPPORTED);
cf99ba13 5649
1f350c87 5650 key_count = __le16_to_cpu(cp->key_count);
ba1d6936
JH
5651 if (key_count > max_key_count) {
5652 BT_ERR("load_ltks: too big key_count value %u", key_count);
a69e8375
JH
5653 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5654 MGMT_STATUS_INVALID_PARAMS);
ba1d6936 5655 }
346af67b
VCG
5656
5657 expected_len = sizeof(*cp) + key_count *
5658 sizeof(struct mgmt_ltk_info);
5659 if (expected_len != len) {
5660 BT_ERR("load_keys: expected %u bytes, got %u bytes",
2606ecbc 5661 expected_len, len);
a69e8375
JH
5662 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
5663 MGMT_STATUS_INVALID_PARAMS);
346af67b
VCG
5664 }
5665
bdb6d971 5666 BT_DBG("%s key_count %u", hdev->name, key_count);
346af67b 5667
54ad6d8a
JH
5668 for (i = 0; i < key_count; i++) {
5669 struct mgmt_ltk_info *key = &cp->keys[i];
5670
3f706b72 5671 if (!ltk_is_valid(key))
a69e8375
JH
5672 return mgmt_cmd_status(sk, hdev->id,
5673 MGMT_OP_LOAD_LONG_TERM_KEYS,
5674 MGMT_STATUS_INVALID_PARAMS);
54ad6d8a
JH
5675 }
5676
346af67b
VCG
5677 hci_dev_lock(hdev);
5678
5679 hci_smp_ltks_clear(hdev);
5680
5681 for (i = 0; i < key_count; i++) {
5682 struct mgmt_ltk_info *key = &cp->keys[i];
85813a7e 5683 u8 type, authenticated;
346af67b 5684
61b43357
JH
5685 switch (key->type) {
5686 case MGMT_LTK_UNAUTHENTICATED:
d7b25450 5687 authenticated = 0x00;
23fb8de3 5688 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
61b43357
JH
5689 break;
5690 case MGMT_LTK_AUTHENTICATED:
d7b25450 5691 authenticated = 0x01;
23fb8de3
JH
5692 type = key->master ? SMP_LTK : SMP_LTK_SLAVE;
5693 break;
5694 case MGMT_LTK_P256_UNAUTH:
5695 authenticated = 0x00;
5696 type = SMP_LTK_P256;
61b43357 5697 break;
23fb8de3
JH
5698 case MGMT_LTK_P256_AUTH:
5699 authenticated = 0x01;
5700 type = SMP_LTK_P256;
61b43357 5701 break;
23fb8de3
JH
5702 case MGMT_LTK_P256_DEBUG:
5703 authenticated = 0x00;
5704 type = SMP_LTK_P256_DEBUG;
61b43357
JH
5705 default:
5706 continue;
5707 }
d7b25450 5708
85813a7e
JH
5709 hci_add_ltk(hdev, &key->addr.bdaddr,
5710 le_addr_type(key->addr.type), type, authenticated,
5711 key->val, key->enc_size, key->ediv, key->rand);
346af67b
VCG
5712 }
5713
2a1afb5a 5714 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
715a5bf2
JH
5715 NULL, 0);
5716
346af67b 5717 hci_dev_unlock(hdev);
346af67b 5718
715a5bf2 5719 return err;
346af67b
VCG
5720}
5721
3b0602cd 5722static int conn_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
dd983808 5723{
dd983808 5724 struct hci_conn *conn = cmd->user_data;
9981bdb0 5725 struct mgmt_rp_get_conn_info rp;
9df74653 5726 int err;
dd983808 5727
9981bdb0 5728 memcpy(&rp.addr, cmd->param, sizeof(rp.addr));
dd983808 5729
9981bdb0 5730 if (status == MGMT_STATUS_SUCCESS) {
dd983808 5731 rp.rssi = conn->rssi;
9981bdb0
JH
5732 rp.tx_power = conn->tx_power;
5733 rp.max_tx_power = conn->max_tx_power;
5734 } else {
5735 rp.rssi = HCI_RSSI_INVALID;
5736 rp.tx_power = HCI_TX_POWER_INVALID;
5737 rp.max_tx_power = HCI_TX_POWER_INVALID;
dd983808
AK
5738 }
5739
2a1afb5a
JH
5740 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO,
5741 status, &rp, sizeof(rp));
dd983808
AK
5742
5743 hci_conn_drop(conn);
f8aaf9b6 5744 hci_conn_put(conn);
9df74653
JH
5745
5746 return err;
dd983808
AK
5747}
5748
1904a853
MH
5749static void conn_info_refresh_complete(struct hci_dev *hdev, u8 hci_status,
5750 u16 opcode)
dd983808
AK
5751{
5752 struct hci_cp_read_rssi *cp;
3b0602cd 5753 struct mgmt_pending_cmd *cmd;
dd983808 5754 struct hci_conn *conn;
dd983808 5755 u16 handle;
9981bdb0 5756 u8 status;
dd983808 5757
9981bdb0 5758 BT_DBG("status 0x%02x", hci_status);
dd983808
AK
5759
5760 hci_dev_lock(hdev);
5761
dd983808
AK
5762 /* Commands sent in request are either Read RSSI or Read Transmit Power
5763 * Level so we check which one was last sent to retrieve connection
5764 * handle. Both commands have handle as first parameter so it's safe to
5765 * cast data on the same command struct.
5766 *
5767 * First command sent is always Read RSSI and we fail only if it fails.
5768 * In other case we simply override error to indicate success as we
5769 * already remembered if TX power value is actually valid.
5770 */
5771 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_RSSI);
5772 if (!cp) {
5773 cp = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
9981bdb0
JH
5774 status = MGMT_STATUS_SUCCESS;
5775 } else {
5776 status = mgmt_status(hci_status);
dd983808
AK
5777 }
5778
5779 if (!cp) {
9981bdb0 5780 BT_ERR("invalid sent_cmd in conn_info response");
dd983808
AK
5781 goto unlock;
5782 }
5783
5784 handle = __le16_to_cpu(cp->handle);
5785 conn = hci_conn_hash_lookup_handle(hdev, handle);
5786 if (!conn) {
9981bdb0 5787 BT_ERR("unknown handle (%d) in conn_info response", handle);
dd983808
AK
5788 goto unlock;
5789 }
5790
333ae95d 5791 cmd = pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn);
9981bdb0
JH
5792 if (!cmd)
5793 goto unlock;
dd983808 5794
9981bdb0
JH
5795 cmd->cmd_complete(cmd, status);
5796 mgmt_pending_remove(cmd);
dd983808
AK
5797
5798unlock:
5799 hci_dev_unlock(hdev);
5800}
5801
5802static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
5803 u16 len)
5804{
5805 struct mgmt_cp_get_conn_info *cp = data;
5806 struct mgmt_rp_get_conn_info rp;
5807 struct hci_conn *conn;
5808 unsigned long conn_info_age;
5809 int err = 0;
5810
5811 BT_DBG("%s", hdev->name);
5812
5813 memset(&rp, 0, sizeof(rp));
5814 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5815 rp.addr.type = cp->addr.type;
5816
5817 if (!bdaddr_type_is_valid(cp->addr.type))
2a1afb5a
JH
5818 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5819 MGMT_STATUS_INVALID_PARAMS,
5820 &rp, sizeof(rp));
dd983808
AK
5821
5822 hci_dev_lock(hdev);
5823
5824 if (!hdev_is_powered(hdev)) {
2a1afb5a
JH
5825 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5826 MGMT_STATUS_NOT_POWERED, &rp,
5827 sizeof(rp));
dd983808
AK
5828 goto unlock;
5829 }
5830
5831 if (cp->addr.type == BDADDR_BREDR)
5832 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
5833 &cp->addr.bdaddr);
5834 else
5835 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
5836
5837 if (!conn || conn->state != BT_CONNECTED) {
2a1afb5a
JH
5838 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5839 MGMT_STATUS_NOT_CONNECTED, &rp,
5840 sizeof(rp));
dd983808
AK
5841 goto unlock;
5842 }
5843
333ae95d 5844 if (pending_find_data(MGMT_OP_GET_CONN_INFO, hdev, conn)) {
2a1afb5a
JH
5845 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5846 MGMT_STATUS_BUSY, &rp, sizeof(rp));
9981bdb0
JH
5847 goto unlock;
5848 }
5849
dd983808
AK
5850 /* To avoid client trying to guess when to poll again for information we
5851 * calculate conn info age as random value between min/max set in hdev.
5852 */
5853 conn_info_age = hdev->conn_info_min_age +
5854 prandom_u32_max(hdev->conn_info_max_age -
5855 hdev->conn_info_min_age);
5856
5857 /* Query controller to refresh cached values if they are too old or were
5858 * never read.
5859 */
f4e2dd53
AK
5860 if (time_after(jiffies, conn->conn_info_timestamp +
5861 msecs_to_jiffies(conn_info_age)) ||
dd983808
AK
5862 !conn->conn_info_timestamp) {
5863 struct hci_request req;
5864 struct hci_cp_read_tx_power req_txp_cp;
5865 struct hci_cp_read_rssi req_rssi_cp;
3b0602cd 5866 struct mgmt_pending_cmd *cmd;
dd983808
AK
5867
5868 hci_req_init(&req, hdev);
5869 req_rssi_cp.handle = cpu_to_le16(conn->handle);
5870 hci_req_add(&req, HCI_OP_READ_RSSI, sizeof(req_rssi_cp),
5871 &req_rssi_cp);
5872
f7faab0c
AK
5873 /* For LE links TX power does not change thus we don't need to
5874 * query for it once value is known.
5875 */
5876 if (!bdaddr_type_is_le(cp->addr.type) ||
5877 conn->tx_power == HCI_TX_POWER_INVALID) {
5878 req_txp_cp.handle = cpu_to_le16(conn->handle);
5879 req_txp_cp.type = 0x00;
5880 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5881 sizeof(req_txp_cp), &req_txp_cp);
5882 }
dd983808 5883
eed5daf3
AK
5884 /* Max TX power needs to be read only once per connection */
5885 if (conn->max_tx_power == HCI_TX_POWER_INVALID) {
5886 req_txp_cp.handle = cpu_to_le16(conn->handle);
5887 req_txp_cp.type = 0x01;
5888 hci_req_add(&req, HCI_OP_READ_TX_POWER,
5889 sizeof(req_txp_cp), &req_txp_cp);
5890 }
5891
dd983808
AK
5892 err = hci_req_run(&req, conn_info_refresh_complete);
5893 if (err < 0)
5894 goto unlock;
5895
5896 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CONN_INFO, hdev,
5897 data, len);
5898 if (!cmd) {
5899 err = -ENOMEM;
5900 goto unlock;
5901 }
5902
5903 hci_conn_hold(conn);
f8aaf9b6 5904 cmd->user_data = hci_conn_get(conn);
9981bdb0 5905 cmd->cmd_complete = conn_info_cmd_complete;
dd983808
AK
5906
5907 conn->conn_info_timestamp = jiffies;
5908 } else {
5909 /* Cache is valid, just reply with values cached in hci_conn */
5910 rp.rssi = conn->rssi;
5911 rp.tx_power = conn->tx_power;
eed5daf3 5912 rp.max_tx_power = conn->max_tx_power;
dd983808 5913
2a1afb5a
JH
5914 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
5915 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
dd983808
AK
5916 }
5917
5918unlock:
5919 hci_dev_unlock(hdev);
5920 return err;
5921}
5922
3b0602cd 5923static int clock_info_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
95868426 5924{
69487371 5925 struct hci_conn *conn = cmd->user_data;
95868426 5926 struct mgmt_rp_get_clock_info rp;
69487371 5927 struct hci_dev *hdev;
9df74653 5928 int err;
69487371
JH
5929
5930 memset(&rp, 0, sizeof(rp));
5931 memcpy(&rp.addr, &cmd->param, sizeof(rp.addr));
5932
5933 if (status)
5934 goto complete;
5935
5936 hdev = hci_dev_get(cmd->index);
5937 if (hdev) {
5938 rp.local_clock = cpu_to_le32(hdev->clock);
5939 hci_dev_put(hdev);
5940 }
5941
5942 if (conn) {
5943 rp.piconet_clock = cpu_to_le32(conn->clock);
5944 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
5945 }
5946
5947complete:
2a1afb5a
JH
5948 err = mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
5949 sizeof(rp));
69487371
JH
5950
5951 if (conn) {
5952 hci_conn_drop(conn);
5953 hci_conn_put(conn);
5954 }
9df74653
JH
5955
5956 return err;
69487371
JH
5957}
5958
1904a853 5959static void get_clock_info_complete(struct hci_dev *hdev, u8 status, u16 opcode)
69487371 5960{
95868426 5961 struct hci_cp_read_clock *hci_cp;
3b0602cd 5962 struct mgmt_pending_cmd *cmd;
95868426
JH
5963 struct hci_conn *conn;
5964
5965 BT_DBG("%s status %u", hdev->name, status);
5966
5967 hci_dev_lock(hdev);
5968
5969 hci_cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
5970 if (!hci_cp)
5971 goto unlock;
5972
5973 if (hci_cp->which) {
5974 u16 handle = __le16_to_cpu(hci_cp->handle);
5975 conn = hci_conn_hash_lookup_handle(hdev, handle);
5976 } else {
5977 conn = NULL;
5978 }
5979
333ae95d 5980 cmd = pending_find_data(MGMT_OP_GET_CLOCK_INFO, hdev, conn);
95868426
JH
5981 if (!cmd)
5982 goto unlock;
5983
69487371 5984 cmd->cmd_complete(cmd, mgmt_status(status));
95868426 5985 mgmt_pending_remove(cmd);
95868426
JH
5986
5987unlock:
5988 hci_dev_unlock(hdev);
5989}
5990
5991static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
5992 u16 len)
5993{
5994 struct mgmt_cp_get_clock_info *cp = data;
5995 struct mgmt_rp_get_clock_info rp;
5996 struct hci_cp_read_clock hci_cp;
3b0602cd 5997 struct mgmt_pending_cmd *cmd;
95868426
JH
5998 struct hci_request req;
5999 struct hci_conn *conn;
6000 int err;
6001
6002 BT_DBG("%s", hdev->name);
6003
6004 memset(&rp, 0, sizeof(rp));
6005 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
6006 rp.addr.type = cp->addr.type;
6007
6008 if (cp->addr.type != BDADDR_BREDR)
2a1afb5a
JH
6009 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
6010 MGMT_STATUS_INVALID_PARAMS,
6011 &rp, sizeof(rp));
95868426
JH
6012
6013 hci_dev_lock(hdev);
6014
6015 if (!hdev_is_powered(hdev)) {
2a1afb5a
JH
6016 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
6017 MGMT_STATUS_NOT_POWERED, &rp,
6018 sizeof(rp));
95868426
JH
6019 goto unlock;
6020 }
6021
6022 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
6023 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
6024 &cp->addr.bdaddr);
6025 if (!conn || conn->state != BT_CONNECTED) {
2a1afb5a
JH
6026 err = mgmt_cmd_complete(sk, hdev->id,
6027 MGMT_OP_GET_CLOCK_INFO,
6028 MGMT_STATUS_NOT_CONNECTED,
6029 &rp, sizeof(rp));
95868426
JH
6030 goto unlock;
6031 }
6032 } else {
6033 conn = NULL;
6034 }
6035
6036 cmd = mgmt_pending_add(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
6037 if (!cmd) {
6038 err = -ENOMEM;
6039 goto unlock;
6040 }
6041
69487371
JH
6042 cmd->cmd_complete = clock_info_cmd_complete;
6043
95868426
JH
6044 hci_req_init(&req, hdev);
6045
6046 memset(&hci_cp, 0, sizeof(hci_cp));
6047 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
6048
6049 if (conn) {
6050 hci_conn_hold(conn);
f8aaf9b6 6051 cmd->user_data = hci_conn_get(conn);
95868426
JH
6052
6053 hci_cp.handle = cpu_to_le16(conn->handle);
6054 hci_cp.which = 0x01; /* Piconet clock */
6055 hci_req_add(&req, HCI_OP_READ_CLOCK, sizeof(hci_cp), &hci_cp);
6056 }
6057
6058 err = hci_req_run(&req, get_clock_info_complete);
6059 if (err < 0)
6060 mgmt_pending_remove(cmd);
6061
6062unlock:
6063 hci_dev_unlock(hdev);
6064 return err;
6065}
6066
5a154e6f
JH
6067static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
6068{
6069 struct hci_conn *conn;
6070
6071 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
6072 if (!conn)
6073 return false;
6074
6075 if (conn->dst_type != type)
6076 return false;
6077
6078 if (conn->state != BT_CONNECTED)
6079 return false;
6080
6081 return true;
6082}
6083
6084/* This function requires the caller holds hdev->lock */
6085static int hci_conn_params_set(struct hci_request *req, bdaddr_t *addr,
6086 u8 addr_type, u8 auto_connect)
6087{
6088 struct hci_dev *hdev = req->hdev;
6089 struct hci_conn_params *params;
6090
6091 params = hci_conn_params_add(hdev, addr, addr_type);
6092 if (!params)
6093 return -EIO;
6094
6095 if (params->auto_connect == auto_connect)
6096 return 0;
6097
6098 list_del_init(&params->action);
6099
6100 switch (auto_connect) {
6101 case HCI_AUTO_CONN_DISABLED:
6102 case HCI_AUTO_CONN_LINK_LOSS:
28a667c9
JP
6103 /* If auto connect is being disabled when we're trying to
6104 * connect to device, keep connecting.
6105 */
6106 if (params->explicit_connect)
6107 list_add(&params->action, &hdev->pend_le_conns);
6108
5a154e6f
JH
6109 __hci_update_background_scan(req);
6110 break;
6111 case HCI_AUTO_CONN_REPORT:
49c50922
JH
6112 if (params->explicit_connect)
6113 list_add(&params->action, &hdev->pend_le_conns);
6114 else
6115 list_add(&params->action, &hdev->pend_le_reports);
5a154e6f
JH
6116 __hci_update_background_scan(req);
6117 break;
6118 case HCI_AUTO_CONN_DIRECT:
6119 case HCI_AUTO_CONN_ALWAYS:
6120 if (!is_connected(hdev, addr, addr_type)) {
6121 list_add(&params->action, &hdev->pend_le_conns);
168b8a25
JP
6122 /* If we are in scan phase of connecting, we were
6123 * already added to pend_le_conns and scanning.
6124 */
6125 if (params->auto_connect != HCI_AUTO_CONN_EXPLICIT)
6126 __hci_update_background_scan(req);
5a154e6f
JH
6127 }
6128 break;
6129 }
6130
6131 params->auto_connect = auto_connect;
6132
6133 BT_DBG("addr %pMR (type %u) auto_connect %u", addr, addr_type,
6134 auto_connect);
6135
6136 return 0;
6137}
6138
8afef092
MH
6139static void device_added(struct sock *sk, struct hci_dev *hdev,
6140 bdaddr_t *bdaddr, u8 type, u8 action)
6141{
6142 struct mgmt_ev_device_added ev;
6143
6144 bacpy(&ev.addr.bdaddr, bdaddr);
6145 ev.addr.type = type;
6146 ev.action = action;
6147
6148 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
6149}
6150
1904a853 6151static void add_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5a154e6f 6152{
3b0602cd 6153 struct mgmt_pending_cmd *cmd;
5a154e6f
JH
6154
6155 BT_DBG("status 0x%02x", status);
6156
6157 hci_dev_lock(hdev);
6158
333ae95d 6159 cmd = pending_find(MGMT_OP_ADD_DEVICE, hdev);
5a154e6f
JH
6160 if (!cmd)
6161 goto unlock;
6162
6163 cmd->cmd_complete(cmd, mgmt_status(status));
6164 mgmt_pending_remove(cmd);
6165
6166unlock:
6167 hci_dev_unlock(hdev);
6168}
6169
2faade53
MH
6170static int add_device(struct sock *sk, struct hci_dev *hdev,
6171 void *data, u16 len)
6172{
6173 struct mgmt_cp_add_device *cp = data;
3b0602cd 6174 struct mgmt_pending_cmd *cmd;
5a154e6f 6175 struct hci_request req;
2faade53
MH
6176 u8 auto_conn, addr_type;
6177 int err;
6178
6179 BT_DBG("%s", hdev->name);
6180
6659358e 6181 if (!bdaddr_type_is_valid(cp->addr.type) ||
2faade53 6182 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
2a1afb5a
JH
6183 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6184 MGMT_STATUS_INVALID_PARAMS,
6185 &cp->addr, sizeof(cp->addr));
2faade53 6186
4b9e7e75 6187 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
2a1afb5a
JH
6188 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
6189 MGMT_STATUS_INVALID_PARAMS,
6190 &cp->addr, sizeof(cp->addr));
2faade53 6191
5a154e6f
JH
6192 hci_req_init(&req, hdev);
6193
2faade53
MH
6194 hci_dev_lock(hdev);
6195
5a154e6f
JH
6196 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_DEVICE, hdev, data, len);
6197 if (!cmd) {
6198 err = -ENOMEM;
6199 goto unlock;
6200 }
6201
6202 cmd->cmd_complete = addr_cmd_complete;
6203
6659358e 6204 if (cp->addr.type == BDADDR_BREDR) {
4b9e7e75 6205 /* Only incoming connections action is supported for now */
6659358e 6206 if (cp->action != 0x01) {
9df74653
JH
6207 err = cmd->cmd_complete(cmd,
6208 MGMT_STATUS_INVALID_PARAMS);
5a154e6f 6209 mgmt_pending_remove(cmd);
6659358e
JH
6210 goto unlock;
6211 }
6212
6213 err = hci_bdaddr_list_add(&hdev->whitelist, &cp->addr.bdaddr,
6214 cp->addr.type);
6215 if (err)
6216 goto unlock;
a397407f 6217
5a154e6f 6218 __hci_update_page_scan(&req);
a397407f 6219
6659358e
JH
6220 goto added;
6221 }
6222
85813a7e 6223 addr_type = le_addr_type(cp->addr.type);
2faade53 6224
4b9e7e75 6225 if (cp->action == 0x02)
2faade53 6226 auto_conn = HCI_AUTO_CONN_ALWAYS;
4b9e7e75
MH
6227 else if (cp->action == 0x01)
6228 auto_conn = HCI_AUTO_CONN_DIRECT;
2faade53 6229 else
a3451d27 6230 auto_conn = HCI_AUTO_CONN_REPORT;
2faade53 6231
9a0a8a8e
JP
6232 /* Kernel internally uses conn_params with resolvable private
6233 * address, but Add Device allows only identity addresses.
6234 * Make sure it is enforced before calling
6235 * hci_conn_params_lookup.
6236 */
6237 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
6238 err = cmd->cmd_complete(cmd, MGMT_STATUS_INVALID_PARAMS);
6239 mgmt_pending_remove(cmd);
6240 goto unlock;
6241 }
6242
bf5b3c8b
MH
6243 /* If the connection parameters don't exist for this device,
6244 * they will be created and configured with defaults.
6245 */
5a154e6f 6246 if (hci_conn_params_set(&req, &cp->addr.bdaddr, addr_type,
d06b50ce 6247 auto_conn) < 0) {
9df74653 6248 err = cmd->cmd_complete(cmd, MGMT_STATUS_FAILED);
5a154e6f 6249 mgmt_pending_remove(cmd);
2faade53
MH
6250 goto unlock;
6251 }
6252
6659358e 6253added:
8afef092
MH
6254 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
6255
5a154e6f
JH
6256 err = hci_req_run(&req, add_device_complete);
6257 if (err < 0) {
6258 /* ENODATA means no HCI commands were needed (e.g. if
6259 * the adapter is powered off).
6260 */
9df74653
JH
6261 if (err == -ENODATA)
6262 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
5a154e6f
JH
6263 mgmt_pending_remove(cmd);
6264 }
2faade53
MH
6265
6266unlock:
6267 hci_dev_unlock(hdev);
6268 return err;
6269}
6270
8afef092
MH
6271static void device_removed(struct sock *sk, struct hci_dev *hdev,
6272 bdaddr_t *bdaddr, u8 type)
6273{
6274 struct mgmt_ev_device_removed ev;
6275
6276 bacpy(&ev.addr.bdaddr, bdaddr);
6277 ev.addr.type = type;
6278
6279 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
6280}
6281
1904a853 6282static void remove_device_complete(struct hci_dev *hdev, u8 status, u16 opcode)
51ef3ebe 6283{
3b0602cd 6284 struct mgmt_pending_cmd *cmd;
51ef3ebe
JH
6285
6286 BT_DBG("status 0x%02x", status);
6287
6288 hci_dev_lock(hdev);
6289
333ae95d 6290 cmd = pending_find(MGMT_OP_REMOVE_DEVICE, hdev);
51ef3ebe
JH
6291 if (!cmd)
6292 goto unlock;
6293
6294 cmd->cmd_complete(cmd, mgmt_status(status));
6295 mgmt_pending_remove(cmd);
6296
6297unlock:
6298 hci_dev_unlock(hdev);
6299}
6300
2faade53
MH
6301static int remove_device(struct sock *sk, struct hci_dev *hdev,
6302 void *data, u16 len)
6303{
6304 struct mgmt_cp_remove_device *cp = data;
3b0602cd 6305 struct mgmt_pending_cmd *cmd;
51ef3ebe 6306 struct hci_request req;
2faade53
MH
6307 int err;
6308
6309 BT_DBG("%s", hdev->name);
6310
51ef3ebe
JH
6311 hci_req_init(&req, hdev);
6312
2faade53
MH
6313 hci_dev_lock(hdev);
6314
51ef3ebe
JH
6315 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_DEVICE, hdev, data, len);
6316 if (!cmd) {
6317 err = -ENOMEM;
6318 goto unlock;
6319 }
6320
6321 cmd->cmd_complete = addr_cmd_complete;
6322
2faade53 6323 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
c71593dd 6324 struct hci_conn_params *params;
2faade53
MH
6325 u8 addr_type;
6326
6659358e 6327 if (!bdaddr_type_is_valid(cp->addr.type)) {
9df74653
JH
6328 err = cmd->cmd_complete(cmd,
6329 MGMT_STATUS_INVALID_PARAMS);
51ef3ebe 6330 mgmt_pending_remove(cmd);
2faade53
MH
6331 goto unlock;
6332 }
6333
6659358e
JH
6334 if (cp->addr.type == BDADDR_BREDR) {
6335 err = hci_bdaddr_list_del(&hdev->whitelist,
6336 &cp->addr.bdaddr,
6337 cp->addr.type);
6338 if (err) {
9df74653
JH
6339 err = cmd->cmd_complete(cmd,
6340 MGMT_STATUS_INVALID_PARAMS);
51ef3ebe 6341 mgmt_pending_remove(cmd);
6659358e
JH
6342 goto unlock;
6343 }
6344
51ef3ebe 6345 __hci_update_page_scan(&req);
a397407f 6346
6659358e
JH
6347 device_removed(sk, hdev, &cp->addr.bdaddr,
6348 cp->addr.type);
6349 goto complete;
6350 }
6351
85813a7e 6352 addr_type = le_addr_type(cp->addr.type);
2faade53 6353
9a0a8a8e
JP
6354 /* Kernel internally uses conn_params with resolvable private
6355 * address, but Remove Device allows only identity addresses.
6356 * Make sure it is enforced before calling
6357 * hci_conn_params_lookup.
6358 */
6359 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
6360 err = cmd->cmd_complete(cmd,
6361 MGMT_STATUS_INVALID_PARAMS);
6362 mgmt_pending_remove(cmd);
6363 goto unlock;
6364 }
6365
c71593dd
JH
6366 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
6367 addr_type);
6368 if (!params) {
9df74653
JH
6369 err = cmd->cmd_complete(cmd,
6370 MGMT_STATUS_INVALID_PARAMS);
51ef3ebe 6371 mgmt_pending_remove(cmd);
c71593dd
JH
6372 goto unlock;
6373 }
6374
679d2b6f
JH
6375 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
6376 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
9df74653
JH
6377 err = cmd->cmd_complete(cmd,
6378 MGMT_STATUS_INVALID_PARAMS);
51ef3ebe 6379 mgmt_pending_remove(cmd);
c71593dd
JH
6380 goto unlock;
6381 }
6382
d1dbf12e 6383 list_del(&params->action);
c71593dd
JH
6384 list_del(&params->list);
6385 kfree(params);
51ef3ebe 6386 __hci_update_background_scan(&req);
8afef092
MH
6387
6388 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
2faade53 6389 } else {
19de0825 6390 struct hci_conn_params *p, *tmp;
6659358e 6391 struct bdaddr_list *b, *btmp;
19de0825 6392
2faade53 6393 if (cp->addr.type) {
9df74653
JH
6394 err = cmd->cmd_complete(cmd,
6395 MGMT_STATUS_INVALID_PARAMS);
51ef3ebe 6396 mgmt_pending_remove(cmd);
2faade53
MH
6397 goto unlock;
6398 }
6399
6659358e
JH
6400 list_for_each_entry_safe(b, btmp, &hdev->whitelist, list) {
6401 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
6402 list_del(&b->list);
6403 kfree(b);
6404 }
6405
51ef3ebe 6406 __hci_update_page_scan(&req);
a397407f 6407
19de0825
JH
6408 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
6409 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
6410 continue;
6411 device_removed(sk, hdev, &p->addr, p->addr_type);
679d2b6f
JH
6412 if (p->explicit_connect) {
6413 p->auto_connect = HCI_AUTO_CONN_EXPLICIT;
6414 continue;
6415 }
19de0825
JH
6416 list_del(&p->action);
6417 list_del(&p->list);
6418 kfree(p);
6419 }
6420
6421 BT_DBG("All LE connection parameters were removed");
6422
51ef3ebe 6423 __hci_update_background_scan(&req);
2faade53
MH
6424 }
6425
6659358e 6426complete:
51ef3ebe
JH
6427 err = hci_req_run(&req, remove_device_complete);
6428 if (err < 0) {
6429 /* ENODATA means no HCI commands were needed (e.g. if
6430 * the adapter is powered off).
6431 */
9df74653
JH
6432 if (err == -ENODATA)
6433 err = cmd->cmd_complete(cmd, MGMT_STATUS_SUCCESS);
51ef3ebe
JH
6434 mgmt_pending_remove(cmd);
6435 }
2faade53
MH
6436
6437unlock:
6438 hci_dev_unlock(hdev);
6439 return err;
6440}
6441
a26f3dcf
JH
6442static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
6443 u16 len)
6444{
6445 struct mgmt_cp_load_conn_param *cp = data;
ba1d6936
JH
6446 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
6447 sizeof(struct mgmt_conn_param));
a26f3dcf
JH
6448 u16 param_count, expected_len;
6449 int i;
6450
6451 if (!lmp_le_capable(hdev))
a69e8375
JH
6452 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6453 MGMT_STATUS_NOT_SUPPORTED);
a26f3dcf
JH
6454
6455 param_count = __le16_to_cpu(cp->param_count);
ba1d6936
JH
6456 if (param_count > max_param_count) {
6457 BT_ERR("load_conn_param: too big param_count value %u",
6458 param_count);
a69e8375
JH
6459 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6460 MGMT_STATUS_INVALID_PARAMS);
ba1d6936 6461 }
a26f3dcf
JH
6462
6463 expected_len = sizeof(*cp) + param_count *
6464 sizeof(struct mgmt_conn_param);
6465 if (expected_len != len) {
6466 BT_ERR("load_conn_param: expected %u bytes, got %u bytes",
6467 expected_len, len);
a69e8375
JH
6468 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
6469 MGMT_STATUS_INVALID_PARAMS);
a26f3dcf
JH
6470 }
6471
6472 BT_DBG("%s param_count %u", hdev->name, param_count);
6473
6474 hci_dev_lock(hdev);
6475
6476 hci_conn_params_clear_disabled(hdev);
6477
6478 for (i = 0; i < param_count; i++) {
6479 struct mgmt_conn_param *param = &cp->params[i];
6480 struct hci_conn_params *hci_param;
6481 u16 min, max, latency, timeout;
6482 u8 addr_type;
6483
6484 BT_DBG("Adding %pMR (type %u)", &param->addr.bdaddr,
6485 param->addr.type);
6486
6487 if (param->addr.type == BDADDR_LE_PUBLIC) {
6488 addr_type = ADDR_LE_DEV_PUBLIC;
6489 } else if (param->addr.type == BDADDR_LE_RANDOM) {
6490 addr_type = ADDR_LE_DEV_RANDOM;
6491 } else {
6492 BT_ERR("Ignoring invalid connection parameters");
6493 continue;
6494 }
6495
6496 min = le16_to_cpu(param->min_interval);
6497 max = le16_to_cpu(param->max_interval);
6498 latency = le16_to_cpu(param->latency);
6499 timeout = le16_to_cpu(param->timeout);
6500
6501 BT_DBG("min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
6502 min, max, latency, timeout);
6503
6504 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
6505 BT_ERR("Ignoring invalid connection parameters");
6506 continue;
6507 }
6508
6509 hci_param = hci_conn_params_add(hdev, &param->addr.bdaddr,
6510 addr_type);
6511 if (!hci_param) {
6512 BT_ERR("Failed to add connection parameters");
6513 continue;
6514 }
6515
6516 hci_param->conn_min_interval = min;
6517 hci_param->conn_max_interval = max;
6518 hci_param->conn_latency = latency;
6519 hci_param->supervision_timeout = timeout;
6520 }
6521
6522 hci_dev_unlock(hdev);
6523
2a1afb5a
JH
6524 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
6525 NULL, 0);
a26f3dcf
JH
6526}
6527
dbece37a
MH
6528static int set_external_config(struct sock *sk, struct hci_dev *hdev,
6529 void *data, u16 len)
6530{
6531 struct mgmt_cp_set_external_config *cp = data;
6532 bool changed;
6533 int err;
6534
6535 BT_DBG("%s", hdev->name);
6536
6537 if (hdev_is_powered(hdev))
a69e8375
JH
6538 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6539 MGMT_STATUS_REJECTED);
dbece37a
MH
6540
6541 if (cp->config != 0x00 && cp->config != 0x01)
a69e8375
JH
6542 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6543 MGMT_STATUS_INVALID_PARAMS);
dbece37a
MH
6544
6545 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
a69e8375
JH
6546 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
6547 MGMT_STATUS_NOT_SUPPORTED);
dbece37a
MH
6548
6549 hci_dev_lock(hdev);
6550
6551 if (cp->config)
238be788 6552 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
dbece37a 6553 else
a69d8927 6554 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
dbece37a
MH
6555
6556 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
6557 if (err < 0)
6558 goto unlock;
6559
6560 if (!changed)
6561 goto unlock;
6562
f4537c04
MH
6563 err = new_options(hdev, sk);
6564
d7a5a11d 6565 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
dbece37a 6566 mgmt_index_removed(hdev);
d603b76b 6567
516018a9 6568 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
a1536da2
MH
6569 hci_dev_set_flag(hdev, HCI_CONFIG);
6570 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
d603b76b
MH
6571
6572 queue_work(hdev->req_workqueue, &hdev->power_on);
6573 } else {
5ea234d3 6574 set_bit(HCI_RAW, &hdev->flags);
d603b76b
MH
6575 mgmt_index_added(hdev);
6576 }
dbece37a
MH
6577 }
6578
6579unlock:
6580 hci_dev_unlock(hdev);
6581 return err;
6582}
6583
9713c17b
MH
6584static int set_public_address(struct sock *sk, struct hci_dev *hdev,
6585 void *data, u16 len)
6586{
6587 struct mgmt_cp_set_public_address *cp = data;
6588 bool changed;
6589 int err;
6590
6591 BT_DBG("%s", hdev->name);
6592
6593 if (hdev_is_powered(hdev))
a69e8375
JH
6594 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6595 MGMT_STATUS_REJECTED);
9713c17b
MH
6596
6597 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
a69e8375
JH
6598 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6599 MGMT_STATUS_INVALID_PARAMS);
9713c17b
MH
6600
6601 if (!hdev->set_bdaddr)
a69e8375
JH
6602 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
6603 MGMT_STATUS_NOT_SUPPORTED);
9713c17b
MH
6604
6605 hci_dev_lock(hdev);
6606
6607 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
6608 bacpy(&hdev->public_addr, &cp->bdaddr);
6609
6610 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
6611 if (err < 0)
6612 goto unlock;
6613
6614 if (!changed)
6615 goto unlock;
6616
d7a5a11d 6617 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
9713c17b
MH
6618 err = new_options(hdev, sk);
6619
6620 if (is_configured(hdev)) {
6621 mgmt_index_removed(hdev);
6622
a358dc11 6623 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
9713c17b 6624
a1536da2
MH
6625 hci_dev_set_flag(hdev, HCI_CONFIG);
6626 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
9713c17b
MH
6627
6628 queue_work(hdev->req_workqueue, &hdev->power_on);
6629 }
6630
6631unlock:
6632 hci_dev_unlock(hdev);
6633 return err;
6634}
6635
bea41609
MH
6636static inline u16 eir_append_data(u8 *eir, u16 eir_len, u8 type, u8 *data,
6637 u8 data_len)
6638{
6639 eir[eir_len++] = sizeof(type) + data_len;
6640 eir[eir_len++] = type;
6641 memcpy(&eir[eir_len], data, data_len);
6642 eir_len += data_len;
6643
6644 return eir_len;
6645}
6646
40f66c05
JH
6647static void read_local_oob_ext_data_complete(struct hci_dev *hdev, u8 status,
6648 u16 opcode, struct sk_buff *skb)
6649{
6650 const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
6651 struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
6652 u8 *h192, *r192, *h256, *r256;
6653 struct mgmt_pending_cmd *cmd;
6654 u16 eir_len;
6655 int err;
6656
6657 BT_DBG("%s status %u", hdev->name, status);
6658
6659 cmd = pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev);
6660 if (!cmd)
6661 return;
6662
6663 mgmt_cp = cmd->param;
6664
6665 if (status) {
6666 status = mgmt_status(status);
6667 eir_len = 0;
6668
6669 h192 = NULL;
6670 r192 = NULL;
6671 h256 = NULL;
6672 r256 = NULL;
6673 } else if (opcode == HCI_OP_READ_LOCAL_OOB_DATA) {
6674 struct hci_rp_read_local_oob_data *rp;
6675
6676 if (skb->len != sizeof(*rp)) {
6677 status = MGMT_STATUS_FAILED;
6678 eir_len = 0;
6679 } else {
6680 status = MGMT_STATUS_SUCCESS;
6681 rp = (void *)skb->data;
6682
6683 eir_len = 5 + 18 + 18;
6684 h192 = rp->hash;
6685 r192 = rp->rand;
6686 h256 = NULL;
6687 r256 = NULL;
6688 }
6689 } else {
6690 struct hci_rp_read_local_oob_ext_data *rp;
6691
6692 if (skb->len != sizeof(*rp)) {
6693 status = MGMT_STATUS_FAILED;
6694 eir_len = 0;
6695 } else {
6696 status = MGMT_STATUS_SUCCESS;
6697 rp = (void *)skb->data;
6698
6699 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
6700 eir_len = 5 + 18 + 18;
6701 h192 = NULL;
6702 r192 = NULL;
6703 } else {
6704 eir_len = 5 + 18 + 18 + 18 + 18;
6705 h192 = rp->hash192;
6706 r192 = rp->rand192;
6707 }
6708
6709 h256 = rp->hash256;
6710 r256 = rp->rand256;
6711 }
6712 }
6713
6714 mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
6715 if (!mgmt_rp)
6716 goto done;
6717
6718 if (status)
6719 goto send_rsp;
6720
6721 eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
6722 hdev->dev_class, 3);
6723
6724 if (h192 && r192) {
6725 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6726 EIR_SSP_HASH_C192, h192, 16);
6727 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6728 EIR_SSP_RAND_R192, r192, 16);
6729 }
6730
6731 if (h256 && r256) {
6732 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6733 EIR_SSP_HASH_C256, h256, 16);
6734 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
6735 EIR_SSP_RAND_R256, r256, 16);
6736 }
6737
6738send_rsp:
6739 mgmt_rp->type = mgmt_cp->type;
6740 mgmt_rp->eir_len = cpu_to_le16(eir_len);
6741
6742 err = mgmt_cmd_complete(cmd->sk, hdev->id,
6743 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
6744 mgmt_rp, sizeof(*mgmt_rp) + eir_len);
6745 if (err < 0 || status)
6746 goto done;
6747
6748 hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
6749
6750 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6751 mgmt_rp, sizeof(*mgmt_rp) + eir_len,
6752 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
6753done:
6754 kfree(mgmt_rp);
6755 mgmt_pending_remove(cmd);
6756}
6757
6758static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
6759 struct mgmt_cp_read_local_oob_ext_data *cp)
6760{
6761 struct mgmt_pending_cmd *cmd;
6762 struct hci_request req;
6763 int err;
6764
6765 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
6766 cp, sizeof(*cp));
6767 if (!cmd)
6768 return -ENOMEM;
6769
6770 hci_req_init(&req, hdev);
6771
6772 if (bredr_sc_enabled(hdev))
6773 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_EXT_DATA, 0, NULL);
6774 else
6775 hci_req_add(&req, HCI_OP_READ_LOCAL_OOB_DATA, 0, NULL);
6776
6777 err = hci_req_run_skb(&req, read_local_oob_ext_data_complete);
6778 if (err < 0) {
6779 mgmt_pending_remove(cmd);
6780 return err;
6781 }
6782
6783 return 0;
6784}
6785
4f0f155c
MH
6786static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
6787 void *data, u16 data_len)
6788{
6789 struct mgmt_cp_read_local_oob_ext_data *cp = data;
6790 struct mgmt_rp_read_local_oob_ext_data *rp;
6791 size_t rp_len;
6792 u16 eir_len;
0821a2c5 6793 u8 status, flags, role, addr[7], hash[16], rand[16];
4f0f155c
MH
6794 int err;
6795
6796 BT_DBG("%s", hdev->name);
6797
57b0d3e8
MH
6798 if (hdev_is_powered(hdev)) {
6799 switch (cp->type) {
6800 case BIT(BDADDR_BREDR):
6801 status = mgmt_bredr_support(hdev);
6802 if (status)
6803 eir_len = 0;
6804 else
6805 eir_len = 5;
6806 break;
6807 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
6808 status = mgmt_le_support(hdev);
6809 if (status)
6810 eir_len = 0;
6811 else
6812 eir_len = 9 + 3 + 18 + 18 + 3;
6813 break;
6814 default:
6815 status = MGMT_STATUS_INVALID_PARAMS;
6816 eir_len = 0;
6817 break;
6818 }
6819 } else {
6820 status = MGMT_STATUS_NOT_POWERED;
6821 eir_len = 0;
4f0f155c
MH
6822 }
6823
4f0f155c
MH
6824 rp_len = sizeof(*rp) + eir_len;
6825 rp = kmalloc(rp_len, GFP_ATOMIC);
efcd8c98 6826 if (!rp)
4f0f155c 6827 return -ENOMEM;
efcd8c98 6828
57b0d3e8
MH
6829 if (status)
6830 goto complete;
6831
efcd8c98 6832 hci_dev_lock(hdev);
4f0f155c
MH
6833
6834 eir_len = 0;
6835 switch (cp->type) {
6836 case BIT(BDADDR_BREDR):
40f66c05
JH
6837 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
6838 err = read_local_ssp_oob_req(hdev, sk, cp);
6839 hci_dev_unlock(hdev);
6840 if (!err)
6841 goto done;
6842
6843 status = MGMT_STATUS_FAILED;
6844 goto complete;
6845 } else {
6846 eir_len = eir_append_data(rp->eir, eir_len,
6847 EIR_CLASS_OF_DEV,
6848 hdev->dev_class, 3);
6849 }
4f0f155c
MH
6850 break;
6851 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
5082a599
MH
6852 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
6853 smp_generate_oob(hdev, hash, rand) < 0) {
0821a2c5 6854 hci_dev_unlock(hdev);
57b0d3e8
MH
6855 status = MGMT_STATUS_FAILED;
6856 goto complete;
0821a2c5
MH
6857 }
6858
e213568a
MH
6859 /* This should return the active RPA, but since the RPA
6860 * is only programmed on demand, it is really hard to fill
6861 * this in at the moment. For now disallow retrieving
6862 * local out-of-band data when privacy is in use.
6863 *
6864 * Returning the identity address will not help here since
6865 * pairing happens before the identity resolving key is
6866 * known and thus the connection establishment happens
6867 * based on the RPA and not the identity address.
6868 */
4f0f155c 6869 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
e213568a
MH
6870 hci_dev_unlock(hdev);
6871 status = MGMT_STATUS_REJECTED;
6872 goto complete;
6873 }
6874
6875 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
6876 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
6877 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
6878 bacmp(&hdev->static_addr, BDADDR_ANY))) {
4f0f155c
MH
6879 memcpy(addr, &hdev->static_addr, 6);
6880 addr[6] = 0x01;
6881 } else {
6882 memcpy(addr, &hdev->bdaddr, 6);
6883 addr[6] = 0x00;
6884 }
6885
6886 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
6887 addr, sizeof(addr));
6888
6889 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
6890 role = 0x02;
6891 else
6892 role = 0x01;
6893
6894 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
6895 &role, sizeof(role));
6896
5082a599
MH
6897 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
6898 eir_len = eir_append_data(rp->eir, eir_len,
6899 EIR_LE_SC_CONFIRM,
6900 hash, sizeof(hash));
0821a2c5 6901
5082a599
MH
6902 eir_len = eir_append_data(rp->eir, eir_len,
6903 EIR_LE_SC_RANDOM,
6904 rand, sizeof(rand));
6905 }
0821a2c5 6906
4f0f155c
MH
6907 flags = get_adv_discov_flags(hdev);
6908
6909 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
6910 flags |= LE_AD_NO_BREDR;
6911
6912 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
6913 &flags, sizeof(flags));
6914 break;
6915 }
6916
4f0f155c
MH
6917 hci_dev_unlock(hdev);
6918
72000df2
MH
6919 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
6920
57b0d3e8
MH
6921 status = MGMT_STATUS_SUCCESS;
6922
6923complete:
efcd8c98
MH
6924 rp->type = cp->type;
6925 rp->eir_len = cpu_to_le16(eir_len);
6926
4f0f155c 6927 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
57b0d3e8
MH
6928 status, rp, sizeof(*rp) + eir_len);
6929 if (err < 0 || status)
72000df2
MH
6930 goto done;
6931
6932 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
6933 rp, sizeof(*rp) + eir_len,
6934 HCI_MGMT_OOB_DATA_EVENTS, sk);
4f0f155c 6935
0821a2c5 6936done:
4f0f155c
MH
6937 kfree(rp);
6938
6939 return err;
6940}
6941
089fa8c0
AU
6942static u32 get_supported_adv_flags(struct hci_dev *hdev)
6943{
6944 u32 flags = 0;
6945
6946 flags |= MGMT_ADV_FLAG_CONNECTABLE;
6947 flags |= MGMT_ADV_FLAG_DISCOV;
6948 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
6949 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
6950
6951 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID)
6952 flags |= MGMT_ADV_FLAG_TX_POWER;
6953
6954 return flags;
6955}
6956
d3d5305b
MH
6957static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
6958 void *data, u16 data_len)
6959{
6960 struct mgmt_rp_read_adv_features *rp;
6961 size_t rp_len;
286e0c83 6962 int err, i;
24b4f38f 6963 bool instance;
286e0c83 6964 struct adv_info *adv_instance;
089fa8c0 6965 u32 supported_flags;
d3d5305b
MH
6966
6967 BT_DBG("%s", hdev->name);
6968
089fa8c0
AU
6969 if (!lmp_le_capable(hdev))
6970 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
6971 MGMT_STATUS_REJECTED);
6972
d3d5305b
MH
6973 hci_dev_lock(hdev);
6974
6975 rp_len = sizeof(*rp);
24b4f38f 6976
24b4f38f
AU
6977 instance = hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE);
6978 if (instance)
286e0c83 6979 rp_len += hdev->adv_instance_cnt;
24b4f38f 6980
d3d5305b
MH
6981 rp = kmalloc(rp_len, GFP_ATOMIC);
6982 if (!rp) {
6983 hci_dev_unlock(hdev);
6984 return -ENOMEM;
6985 }
6986
089fa8c0
AU
6987 supported_flags = get_supported_adv_flags(hdev);
6988
6989 rp->supported_flags = cpu_to_le32(supported_flags);
dc5d82a9
MH
6990 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
6991 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
d2609b34 6992 rp->max_instances = HCI_MAX_ADV_INSTANCES;
24b4f38f 6993
24b4f38f 6994 if (instance) {
286e0c83
FG
6995 i = 0;
6996 list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
6997 if (i >= hdev->adv_instance_cnt)
6998 break;
6999
7000 rp->instance[i] = adv_instance->instance;
7001 i++;
7002 }
7003 rp->num_instances = hdev->adv_instance_cnt;
24b4f38f
AU
7004 } else {
7005 rp->num_instances = 0;
7006 }
d3d5305b
MH
7007
7008 hci_dev_unlock(hdev);
7009
7010 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
7011 MGMT_STATUS_SUCCESS, rp, rp_len);
7012
7013 kfree(rp);
7014
7015 return err;
7016}
7017
4117ed70 7018static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
b44133ff 7019 u8 len, bool is_adv_data)
24b4f38f 7020{
4117ed70 7021 u8 max_len = HCI_MAX_AD_LENGTH;
24b4f38f 7022 int i, cur_len;
b44133ff 7023 bool flags_managed = false;
5507e358 7024 bool tx_power_managed = false;
67e0c0cd
AU
7025 u32 flags_params = MGMT_ADV_FLAG_DISCOV | MGMT_ADV_FLAG_LIMITED_DISCOV |
7026 MGMT_ADV_FLAG_MANAGED_FLAGS;
24b4f38f 7027
807ec772 7028 if (is_adv_data && (adv_flags & flags_params)) {
b44133ff
AU
7029 flags_managed = true;
7030 max_len -= 3;
7031 }
24b4f38f 7032
5507e358
AU
7033 if (is_adv_data && (adv_flags & MGMT_ADV_FLAG_TX_POWER)) {
7034 tx_power_managed = true;
7035 max_len -= 3;
7036 }
7037
4117ed70 7038 if (len > max_len)
24b4f38f
AU
7039 return false;
7040
4117ed70
AU
7041 /* Make sure that the data is correctly formatted. */
7042 for (i = 0, cur_len = 0; i < len; i += (cur_len + 1)) {
7043 cur_len = data[i];
24b4f38f 7044
b44133ff
AU
7045 if (flags_managed && data[i + 1] == EIR_FLAGS)
7046 return false;
7047
5507e358
AU
7048 if (tx_power_managed && data[i + 1] == EIR_TX_POWER)
7049 return false;
7050
24b4f38f
AU
7051 /* If the current field length would exceed the total data
7052 * length, then it's invalid.
7053 */
4117ed70 7054 if (i + cur_len >= len)
24b4f38f
AU
7055 return false;
7056 }
7057
7058 return true;
7059}
7060
24b4f38f
AU
7061static void add_advertising_complete(struct hci_dev *hdev, u8 status,
7062 u16 opcode)
7063{
7064 struct mgmt_pending_cmd *cmd;
fffd38bc 7065 struct mgmt_cp_add_advertising *cp;
24b4f38f 7066 struct mgmt_rp_add_advertising rp;
fffd38bc
FG
7067 struct adv_info *adv_instance, *n;
7068 u8 instance;
24b4f38f
AU
7069
7070 BT_DBG("status %d", status);
7071
7072 hci_dev_lock(hdev);
7073
7074 cmd = pending_find(MGMT_OP_ADD_ADVERTISING, hdev);
7075
fffd38bc 7076 if (status)
24b4f38f 7077 hci_dev_clear_flag(hdev, HCI_ADVERTISING_INSTANCE);
fffd38bc
FG
7078
7079 list_for_each_entry_safe(adv_instance, n, &hdev->adv_instances, list) {
7080 if (!adv_instance->pending)
7081 continue;
7082
7083 if (!status) {
7084 adv_instance->pending = false;
7085 continue;
7086 }
7087
7088 instance = adv_instance->instance;
7089
7090 if (hdev->cur_adv_instance == instance)
7091 cancel_adv_timeout(hdev);
7092
7093 hci_remove_adv_instance(hdev, instance);
7094 advertising_removed(cmd ? cmd->sk : NULL, hdev, instance);
24b4f38f
AU
7095 }
7096
7097 if (!cmd)
7098 goto unlock;
7099
fffd38bc
FG
7100 cp = cmd->param;
7101 rp.instance = cp->instance;
24b4f38f
AU
7102
7103 if (status)
7104 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
7105 mgmt_status(status));
7106 else
7107 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
7108 mgmt_status(status), &rp, sizeof(rp));
7109
7110 mgmt_pending_remove(cmd);
7111
7112unlock:
7113 hci_dev_unlock(hdev);
7114}
7115
5d900e46 7116void mgmt_adv_timeout_expired(struct hci_dev *hdev)
912098a6 7117{
847818d9
FG
7118 u8 instance;
7119 struct hci_request req;
7120
5d900e46 7121 hdev->adv_instance_timeout = 0;
912098a6 7122
847818d9
FG
7123 instance = get_current_adv_instance(hdev);
7124 if (instance == 0x00)
7125 return;
7126
912098a6 7127 hci_dev_lock(hdev);
847818d9
FG
7128 hci_req_init(&req, hdev);
7129
7130 clear_adv_instance(hdev, &req, instance, false);
7131
7132 if (list_empty(&hdev->adv_instances))
7133 disable_advertising(&req);
7134
7135 if (!skb_queue_empty(&req.cmd_q))
7136 hci_req_run(&req, NULL);
7137
912098a6
AU
7138 hci_dev_unlock(hdev);
7139}
7140
24b4f38f
AU
7141static int add_advertising(struct sock *sk, struct hci_dev *hdev,
7142 void *data, u16 data_len)
7143{
7144 struct mgmt_cp_add_advertising *cp = data;
7145 struct mgmt_rp_add_advertising rp;
7146 u32 flags;
089fa8c0 7147 u32 supported_flags;
24b4f38f 7148 u8 status;
fffd38bc
FG
7149 u16 timeout, duration;
7150 unsigned int prev_instance_cnt = hdev->adv_instance_cnt;
7151 u8 schedule_instance = 0;
7152 struct adv_info *next_instance;
24b4f38f
AU
7153 int err;
7154 struct mgmt_pending_cmd *cmd;
7155 struct hci_request req;
7156
7157 BT_DBG("%s", hdev->name);
7158
7159 status = mgmt_le_support(hdev);
7160 if (status)
7161 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7162 status);
7163
7164 flags = __le32_to_cpu(cp->flags);
912098a6 7165 timeout = __le16_to_cpu(cp->timeout);
fffd38bc 7166 duration = __le16_to_cpu(cp->duration);
24b4f38f 7167
fffd38bc
FG
7168 /* The current implementation only supports a subset of the specified
7169 * flags.
089fa8c0
AU
7170 */
7171 supported_flags = get_supported_adv_flags(hdev);
fffd38bc 7172 if (flags & ~supported_flags)
24b4f38f
AU
7173 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7174 MGMT_STATUS_INVALID_PARAMS);
7175
7176 hci_dev_lock(hdev);
7177
912098a6
AU
7178 if (timeout && !hdev_is_powered(hdev)) {
7179 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7180 MGMT_STATUS_REJECTED);
7181 goto unlock;
7182 }
7183
24b4f38f 7184 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
da929335 7185 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
24b4f38f
AU
7186 pending_find(MGMT_OP_SET_LE, hdev)) {
7187 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7188 MGMT_STATUS_BUSY);
7189 goto unlock;
7190 }
7191
b44133ff 7192 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
4117ed70 7193 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
b44133ff 7194 cp->scan_rsp_len, false)) {
24b4f38f
AU
7195 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7196 MGMT_STATUS_INVALID_PARAMS);
7197 goto unlock;
7198 }
7199
fffd38bc
FG
7200 err = hci_add_adv_instance(hdev, cp->instance, flags,
7201 cp->adv_data_len, cp->data,
7202 cp->scan_rsp_len,
7203 cp->data + cp->adv_data_len,
7204 timeout, duration);
7205 if (err < 0) {
7206 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7207 MGMT_STATUS_FAILED);
7208 goto unlock;
7209 }
24b4f38f 7210
fffd38bc
FG
7211 /* Only trigger an advertising added event if a new instance was
7212 * actually added.
7213 */
7214 if (hdev->adv_instance_cnt > prev_instance_cnt)
7215 advertising_added(sk, hdev, cp->instance);
912098a6 7216
fffd38bc 7217 hci_dev_set_flag(hdev, HCI_ADVERTISING_INSTANCE);
912098a6 7218
fffd38bc
FG
7219 if (hdev->cur_adv_instance == cp->instance) {
7220 /* If the currently advertised instance is being changed then
7221 * cancel the current advertising and schedule the next
7222 * instance. If there is only one instance then the overridden
7223 * advertising data will be visible right away.
7224 */
7225 cancel_adv_timeout(hdev);
912098a6 7226
fffd38bc
FG
7227 next_instance = hci_get_next_instance(hdev, cp->instance);
7228 if (next_instance)
7229 schedule_instance = next_instance->instance;
7230 } else if (!hdev->adv_instance_timeout) {
7231 /* Immediately advertise the new instance if no other
7232 * instance is currently being advertised.
7233 */
7234 schedule_instance = cp->instance;
7235 }
24b4f38f 7236
fffd38bc
FG
7237 /* If the HCI_ADVERTISING flag is set or the device isn't powered or
7238 * there is no instance to be advertised then we have no HCI
7239 * communication to make. Simply return.
24b4f38f
AU
7240 */
7241 if (!hdev_is_powered(hdev) ||
fffd38bc
FG
7242 hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
7243 !schedule_instance) {
7244 rp.instance = cp->instance;
24b4f38f
AU
7245 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
7246 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7247 goto unlock;
7248 }
7249
7250 /* We're good to go, update advertising data, parameters, and start
7251 * advertising.
7252 */
7253 cmd = mgmt_pending_add(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
7254 data_len);
7255 if (!cmd) {
7256 err = -ENOMEM;
7257 goto unlock;
7258 }
7259
7260 hci_req_init(&req, hdev);
7261
fffd38bc
FG
7262 err = schedule_adv_instance(&req, schedule_instance, true);
7263
7264 if (!err)
7265 err = hci_req_run(&req, add_advertising_complete);
24b4f38f 7266
24b4f38f
AU
7267 if (err < 0)
7268 mgmt_pending_remove(cmd);
7269
7270unlock:
7271 hci_dev_unlock(hdev);
7272
7273 return err;
7274}
7275
da929335
AU
7276static void remove_advertising_complete(struct hci_dev *hdev, u8 status,
7277 u16 opcode)
7278{
7279 struct mgmt_pending_cmd *cmd;
01948331 7280 struct mgmt_cp_remove_advertising *cp;
da929335
AU
7281 struct mgmt_rp_remove_advertising rp;
7282
7283 BT_DBG("status %d", status);
7284
7285 hci_dev_lock(hdev);
7286
7287 /* A failure status here only means that we failed to disable
7288 * advertising. Otherwise, the advertising instance has been removed,
7289 * so report success.
7290 */
7291 cmd = pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev);
7292 if (!cmd)
7293 goto unlock;
7294
01948331
FG
7295 cp = cmd->param;
7296 rp.instance = cp->instance;
da929335
AU
7297
7298 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, MGMT_STATUS_SUCCESS,
7299 &rp, sizeof(rp));
7300 mgmt_pending_remove(cmd);
7301
7302unlock:
7303 hci_dev_unlock(hdev);
7304}
7305
7306static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
7307 void *data, u16 data_len)
7308{
7309 struct mgmt_cp_remove_advertising *cp = data;
7310 struct mgmt_rp_remove_advertising rp;
da929335
AU
7311 struct mgmt_pending_cmd *cmd;
7312 struct hci_request req;
952497b1 7313 int err;
da929335
AU
7314
7315 BT_DBG("%s", hdev->name);
7316
da929335
AU
7317 hci_dev_lock(hdev);
7318
952497b1 7319 if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) {
01948331
FG
7320 err = mgmt_cmd_status(sk, hdev->id,
7321 MGMT_OP_REMOVE_ADVERTISING,
7322 MGMT_STATUS_INVALID_PARAMS);
7323 goto unlock;
7324 }
7325
da929335
AU
7326 if (pending_find(MGMT_OP_ADD_ADVERTISING, hdev) ||
7327 pending_find(MGMT_OP_REMOVE_ADVERTISING, hdev) ||
7328 pending_find(MGMT_OP_SET_LE, hdev)) {
7329 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7330 MGMT_STATUS_BUSY);
7331 goto unlock;
7332 }
7333
7334 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE)) {
7335 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
7336 MGMT_STATUS_INVALID_PARAMS);
7337 goto unlock;
7338 }
7339
01948331 7340 hci_req_init(&req, hdev);
da929335 7341
01948331 7342 clear_adv_instance(hdev, &req, cp->instance, true);
da929335 7343
01948331
FG
7344 if (list_empty(&hdev->adv_instances))
7345 disable_advertising(&req);
da929335 7346
01948331
FG
7347 /* If no HCI commands have been collected so far or the HCI_ADVERTISING
7348 * flag is set or the device isn't powered then we have no HCI
7349 * communication to make. Simply return.
da929335 7350 */
01948331
FG
7351 if (skb_queue_empty(&req.cmd_q) ||
7352 !hdev_is_powered(hdev) ||
da929335 7353 hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
01948331 7354 rp.instance = cp->instance;
da929335
AU
7355 err = mgmt_cmd_complete(sk, hdev->id,
7356 MGMT_OP_REMOVE_ADVERTISING,
7357 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7358 goto unlock;
7359 }
7360
7361 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
7362 data_len);
7363 if (!cmd) {
7364 err = -ENOMEM;
7365 goto unlock;
7366 }
7367
da929335
AU
7368 err = hci_req_run(&req, remove_advertising_complete);
7369 if (err < 0)
7370 mgmt_pending_remove(cmd);
7371
7372unlock:
7373 hci_dev_unlock(hdev);
7374
7375 return err;
7376}
7377
6d785aa3 7378static const struct hci_mgmt_handler mgmt_handlers[] = {
0f4e68cf 7379 { NULL }, /* 0x0000 (no command) */
b9a245fb 7380 { read_version, MGMT_READ_VERSION_SIZE,
c91041dc
MH
7381 HCI_MGMT_NO_HDEV |
7382 HCI_MGMT_UNTRUSTED },
b9a245fb 7383 { read_commands, MGMT_READ_COMMANDS_SIZE,
c91041dc
MH
7384 HCI_MGMT_NO_HDEV |
7385 HCI_MGMT_UNTRUSTED },
b9a245fb 7386 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
c91041dc
MH
7387 HCI_MGMT_NO_HDEV |
7388 HCI_MGMT_UNTRUSTED },
7389 { read_controller_info, MGMT_READ_INFO_SIZE,
7390 HCI_MGMT_UNTRUSTED },
7aea8616
MH
7391 { set_powered, MGMT_SETTING_SIZE },
7392 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
7393 { set_connectable, MGMT_SETTING_SIZE },
7394 { set_fast_connectable, MGMT_SETTING_SIZE },
7395 { set_bondable, MGMT_SETTING_SIZE },
7396 { set_link_security, MGMT_SETTING_SIZE },
7397 { set_ssp, MGMT_SETTING_SIZE },
7398 { set_hs, MGMT_SETTING_SIZE },
7399 { set_le, MGMT_SETTING_SIZE },
7400 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
7401 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
7402 { add_uuid, MGMT_ADD_UUID_SIZE },
7403 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
b9a245fb
JH
7404 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
7405 HCI_MGMT_VAR_LEN },
7406 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
7407 HCI_MGMT_VAR_LEN },
7aea8616
MH
7408 { disconnect, MGMT_DISCONNECT_SIZE },
7409 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
7410 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
7411 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
7412 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
7413 { pair_device, MGMT_PAIR_DEVICE_SIZE },
7414 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
7415 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
7416 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
7417 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
7418 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
7419 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
b9a245fb
JH
7420 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
7421 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
7422 HCI_MGMT_VAR_LEN },
7aea8616
MH
7423 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
7424 { start_discovery, MGMT_START_DISCOVERY_SIZE },
7425 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
7426 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
7427 { block_device, MGMT_BLOCK_DEVICE_SIZE },
7428 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
7429 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
7430 { set_advertising, MGMT_SETTING_SIZE },
7431 { set_bredr, MGMT_SETTING_SIZE },
7432 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
7433 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
7434 { set_secure_conn, MGMT_SETTING_SIZE },
7435 { set_debug_keys, MGMT_SETTING_SIZE },
7436 { set_privacy, MGMT_SET_PRIVACY_SIZE },
b9a245fb
JH
7437 { load_irks, MGMT_LOAD_IRKS_SIZE,
7438 HCI_MGMT_VAR_LEN },
7aea8616
MH
7439 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
7440 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
7441 { add_device, MGMT_ADD_DEVICE_SIZE },
7442 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
b9a245fb
JH
7443 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
7444 HCI_MGMT_VAR_LEN },
7445 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
c91041dc
MH
7446 HCI_MGMT_NO_HDEV |
7447 HCI_MGMT_UNTRUSTED },
b9a245fb 7448 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
c91041dc
MH
7449 HCI_MGMT_UNCONFIGURED |
7450 HCI_MGMT_UNTRUSTED },
b9a245fb
JH
7451 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
7452 HCI_MGMT_UNCONFIGURED },
7453 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
7454 HCI_MGMT_UNCONFIGURED },
7455 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
7456 HCI_MGMT_VAR_LEN },
4f0f155c 7457 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
96f1474a 7458 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
c91041dc
MH
7459 HCI_MGMT_NO_HDEV |
7460 HCI_MGMT_UNTRUSTED },
d3d5305b 7461 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
24b4f38f
AU
7462 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
7463 HCI_MGMT_VAR_LEN },
da929335 7464 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
0f4e68cf
JH
7465};
7466
bf6b56db 7467void mgmt_index_added(struct hci_dev *hdev)
c71e97bf 7468{
ced85549 7469 struct mgmt_ev_ext_index ev;
bb4b2a9a 7470
0602a8ad
MH
7471 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7472 return;
7473
f9207338
MH
7474 switch (hdev->dev_type) {
7475 case HCI_BREDR:
7476 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7477 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
7478 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
ced85549 7479 ev.type = 0x01;
f9207338
MH
7480 } else {
7481 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
7482 HCI_MGMT_INDEX_EVENTS);
ced85549 7483 ev.type = 0x00;
f9207338
MH
7484 }
7485 break;
ced85549
MH
7486 case HCI_AMP:
7487 ev.type = 0x02;
7488 break;
7489 default:
7490 return;
f9207338 7491 }
ced85549
MH
7492
7493 ev.bus = hdev->bus;
7494
7495 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
7496 HCI_MGMT_EXT_INDEX_EVENTS);
c71e97bf
JH
7497}
7498
bf6b56db 7499void mgmt_index_removed(struct hci_dev *hdev)
c71e97bf 7500{
ced85549 7501 struct mgmt_ev_ext_index ev;
5f159032 7502 u8 status = MGMT_STATUS_INVALID_INDEX;
b24752fe 7503
0602a8ad
MH
7504 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
7505 return;
7506
f9207338
MH
7507 switch (hdev->dev_type) {
7508 case HCI_BREDR:
7509 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
b24752fe 7510
f9207338
MH
7511 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
7512 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
7513 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
ced85549 7514 ev.type = 0x01;
f9207338
MH
7515 } else {
7516 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
7517 HCI_MGMT_INDEX_EVENTS);
ced85549 7518 ev.type = 0x00;
f9207338
MH
7519 }
7520 break;
ced85549
MH
7521 case HCI_AMP:
7522 ev.type = 0x02;
7523 break;
7524 default:
7525 return;
f9207338 7526 }
ced85549
MH
7527
7528 ev.bus = hdev->bus;
7529
7530 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
7531 HCI_MGMT_EXT_INDEX_EVENTS);
eec8d2bc
JH
7532}
7533
6046dc3e 7534/* This function requires the caller holds hdev->lock */
2cf22218 7535static void restart_le_actions(struct hci_request *req)
6046dc3e 7536{
2cf22218 7537 struct hci_dev *hdev = req->hdev;
6046dc3e
AG
7538 struct hci_conn_params *p;
7539
7540 list_for_each_entry(p, &hdev->le_conn_params, list) {
d7347f3c
JH
7541 /* Needed for AUTO_OFF case where might not "really"
7542 * have been powered off.
7543 */
7544 list_del_init(&p->action);
7545
7546 switch (p->auto_connect) {
4b9e7e75 7547 case HCI_AUTO_CONN_DIRECT:
d7347f3c
JH
7548 case HCI_AUTO_CONN_ALWAYS:
7549 list_add(&p->action, &hdev->pend_le_conns);
7550 break;
7551 case HCI_AUTO_CONN_REPORT:
7552 list_add(&p->action, &hdev->pend_le_reports);
7553 break;
7554 default:
7555 break;
c83ed19d 7556 }
6046dc3e 7557 }
c83ed19d 7558
2cf22218 7559 __hci_update_background_scan(req);
6046dc3e
AG
7560}
7561
1904a853 7562static void powered_complete(struct hci_dev *hdev, u8 status, u16 opcode)
229ab39c
JH
7563{
7564 struct cmd_lookup match = { NULL, hdev };
7565
7566 BT_DBG("status 0x%02x", status);
7567
162a3bac
MH
7568 if (!status) {
7569 /* Register the available SMP channels (BR/EDR and LE) only
7570 * when successfully powering on the controller. This late
7571 * registration is required so that LE SMP can clearly
7572 * decide if the public address or static address is used.
7573 */
7574 smp_register(hdev);
7575 }
7576
229ab39c
JH
7577 hci_dev_lock(hdev);
7578
7579 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
7580
7581 new_settings(hdev, match.sk);
7582
7583 hci_dev_unlock(hdev);
7584
7585 if (match.sk)
7586 sock_put(match.sk);
7587}
7588
70da6243 7589static int powered_update_hci(struct hci_dev *hdev)
5add6af8 7590{
890ea898 7591 struct hci_request req;
320b3bf7 7592 struct adv_info *adv_instance;
70da6243 7593 u8 link_sec;
5add6af8 7594
890ea898
JH
7595 hci_req_init(&req, hdev);
7596
d7a5a11d 7597 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED) &&
70da6243 7598 !lmp_host_ssp_capable(hdev)) {
574ea3c7 7599 u8 mode = 0x01;
5e5282bb 7600
574ea3c7
MH
7601 hci_req_add(&req, HCI_OP_WRITE_SSP_MODE, sizeof(mode), &mode);
7602
7603 if (bredr_sc_enabled(hdev) && !lmp_host_sc_capable(hdev)) {
7604 u8 support = 0x01;
5add6af8 7605
574ea3c7
MH
7606 hci_req_add(&req, HCI_OP_WRITE_SC_SUPPORT,
7607 sizeof(support), &support);
7608 }
ec6f99b8
JH
7609 }
7610
d7a5a11d 7611 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
c73eee91 7612 lmp_bredr_capable(hdev)) {
70da6243 7613 struct hci_cp_write_le_host_supported cp;
f0ff92fb 7614
32226e4f
MH
7615 cp.le = 0x01;
7616 cp.simul = 0x00;
3d1cbdd6 7617
70da6243
JH
7618 /* Check first if we already have the right
7619 * host state (host features set)
7620 */
7621 if (cp.le != lmp_host_le_capable(hdev) ||
7622 cp.simul != lmp_host_le_br_capable(hdev))
890ea898
JH
7623 hci_req_add(&req, HCI_OP_WRITE_LE_HOST_SUPPORTED,
7624 sizeof(cp), &cp);
70da6243 7625 }
3d1cbdd6 7626
d13eafce 7627 if (lmp_le_capable(hdev)) {
441ad2d0
MH
7628 /* Make sure the controller has a good default for
7629 * advertising data. This also applies to the case
7630 * where BR/EDR was toggled during the AUTO_OFF phase.
7631 */
320b3bf7
FG
7632 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
7633 (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
7634 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))) {
5947f4bc 7635 update_adv_data(&req);
f14d8f64
MH
7636 update_scan_rsp_data(&req);
7637 }
441ad2d0 7638
320b3bf7
FG
7639 if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
7640 hdev->cur_adv_instance == 0x00 &&
7641 !list_empty(&hdev->adv_instances)) {
7642 adv_instance = list_first_entry(&hdev->adv_instances,
7643 struct adv_info, list);
7644 hdev->cur_adv_instance = adv_instance->instance;
7645 }
7646
7647 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
bba3aa55 7648 enable_advertising(&req);
320b3bf7
FG
7649 else if (hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE) &&
7650 hdev->cur_adv_instance)
7651 schedule_adv_instance(&req, hdev->cur_adv_instance,
7652 true);
2cf22218
JH
7653
7654 restart_le_actions(&req);
eeca6f89
JH
7655 }
7656
d7a5a11d 7657 link_sec = hci_dev_test_flag(hdev, HCI_LINK_SECURITY);
70da6243 7658 if (link_sec != test_bit(HCI_AUTH, &hdev->flags))
890ea898
JH
7659 hci_req_add(&req, HCI_OP_WRITE_AUTH_ENABLE,
7660 sizeof(link_sec), &link_sec);
562fcc24 7661
70da6243 7662 if (lmp_bredr_capable(hdev)) {
d7a5a11d 7663 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
406ef2a6
JH
7664 write_fast_connectable(&req, true);
7665 else
7666 write_fast_connectable(&req, false);
1d2dc5b7 7667 __hci_update_page_scan(&req);
890ea898 7668 update_class(&req);
13928971 7669 update_name(&req);
890ea898 7670 update_eir(&req);
70da6243 7671 }
562fcc24 7672
229ab39c 7673 return hci_req_run(&req, powered_complete);
70da6243 7674}
562fcc24 7675
70da6243
JH
7676int mgmt_powered(struct hci_dev *hdev, u8 powered)
7677{
7678 struct cmd_lookup match = { NULL, hdev };
9845904f 7679 u8 status, zero_cod[] = { 0, 0, 0 };
70da6243 7680 int err;
f0ff92fb 7681
d7a5a11d 7682 if (!hci_dev_test_flag(hdev, HCI_MGMT))
70da6243
JH
7683 return 0;
7684
70da6243 7685 if (powered) {
229ab39c
JH
7686 if (powered_update_hci(hdev) == 0)
7687 return 0;
fe038884 7688
229ab39c
JH
7689 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp,
7690 &match);
7691 goto new_settings;
b24752fe
JH
7692 }
7693
229ab39c 7694 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
9845904f
JH
7695
7696 /* If the power off is because of hdev unregistration let
7697 * use the appropriate INVALID_INDEX status. Otherwise use
7698 * NOT_POWERED. We cover both scenarios here since later in
7699 * mgmt_index_removed() any hci_conn callbacks will have already
7700 * been triggered, potentially causing misleading DISCONNECTED
7701 * status responses.
7702 */
d7a5a11d 7703 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
9845904f
JH
7704 status = MGMT_STATUS_INVALID_INDEX;
7705 else
7706 status = MGMT_STATUS_NOT_POWERED;
7707
7708 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
229ab39c
JH
7709
7710 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0)
f6b7712e
MH
7711 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
7712 zero_cod, sizeof(zero_cod), NULL);
229ab39c
JH
7713
7714new_settings:
beadb2bd 7715 err = new_settings(hdev, match.sk);
eec8d2bc
JH
7716
7717 if (match.sk)
7718 sock_put(match.sk);
7719
7bb895d6 7720 return err;
5add6af8 7721}
73f22f62 7722
3eec705e 7723void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
96570ffc 7724{
3b0602cd 7725 struct mgmt_pending_cmd *cmd;
96570ffc
JH
7726 u8 status;
7727
333ae95d 7728 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
96570ffc 7729 if (!cmd)
3eec705e 7730 return;
96570ffc
JH
7731
7732 if (err == -ERFKILL)
7733 status = MGMT_STATUS_RFKILLED;
7734 else
7735 status = MGMT_STATUS_FAILED;
7736
a69e8375 7737 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
96570ffc
JH
7738
7739 mgmt_pending_remove(cmd);
96570ffc
JH
7740}
7741
d1967ff8
MH
7742void mgmt_discoverable_timeout(struct hci_dev *hdev)
7743{
7744 struct hci_request req;
d1967ff8
MH
7745
7746 hci_dev_lock(hdev);
7747
7748 /* When discoverable timeout triggers, then just make sure
7749 * the limited discoverable flag is cleared. Even in the case
7750 * of a timeout triggered from general discoverable, it is
7751 * safe to unconditionally clear the flag.
7752 */
a358dc11
MH
7753 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
7754 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
d1967ff8
MH
7755
7756 hci_req_init(&req, hdev);
d7a5a11d 7757 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
4b580614
JH
7758 u8 scan = SCAN_PAGE;
7759 hci_req_add(&req, HCI_OP_WRITE_SCAN_ENABLE,
7760 sizeof(scan), &scan);
7761 }
d1967ff8 7762 update_class(&req);
24b4f38f
AU
7763
7764 /* Advertising instances don't use the global discoverable setting, so
7765 * only update AD if advertising was enabled using Set Advertising.
7766 */
7767 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
7768 update_adv_data(&req);
7769
d1967ff8
MH
7770 hci_req_run(&req, NULL);
7771
7772 hdev->discov_timeout = 0;
7773
9a43e25f
JH
7774 new_settings(hdev, NULL);
7775
d1967ff8
MH
7776 hci_dev_unlock(hdev);
7777}
7778
dc4a5ee2
MH
7779void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
7780 bool persistent)
55ed8ca1 7781{
86742e1e 7782 struct mgmt_ev_new_link_key ev;
55ed8ca1 7783
a492cd52 7784 memset(&ev, 0, sizeof(ev));
55ed8ca1 7785
a492cd52 7786 ev.store_hint = persistent;
d753fdc4 7787 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
591f47f3 7788 ev.key.addr.type = BDADDR_BREDR;
a492cd52 7789 ev.key.type = key->type;
9b3b4460 7790 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
a492cd52 7791 ev.key.pin_len = key->pin_len;
55ed8ca1 7792
dc4a5ee2 7793 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
55ed8ca1 7794}
f7520543 7795
d7b25450
JH
7796static u8 mgmt_ltk_type(struct smp_ltk *ltk)
7797{
23fb8de3
JH
7798 switch (ltk->type) {
7799 case SMP_LTK:
7800 case SMP_LTK_SLAVE:
7801 if (ltk->authenticated)
7802 return MGMT_LTK_AUTHENTICATED;
7803 return MGMT_LTK_UNAUTHENTICATED;
7804 case SMP_LTK_P256:
7805 if (ltk->authenticated)
7806 return MGMT_LTK_P256_AUTH;
7807 return MGMT_LTK_P256_UNAUTH;
7808 case SMP_LTK_P256_DEBUG:
7809 return MGMT_LTK_P256_DEBUG;
7810 }
d7b25450
JH
7811
7812 return MGMT_LTK_UNAUTHENTICATED;
7813}
7814
53ac6ab6 7815void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
346af67b
VCG
7816{
7817 struct mgmt_ev_new_long_term_key ev;
7818
7819 memset(&ev, 0, sizeof(ev));
7820
5192d301 7821 /* Devices using resolvable or non-resolvable random addresses
f72186d2 7822 * without providing an identity resolving key don't require
5192d301
MH
7823 * to store long term keys. Their addresses will change the
7824 * next time around.
7825 *
7826 * Only when a remote device provides an identity address
7827 * make sure the long term key is stored. If the remote
7828 * identity is known, the long term keys are internally
7829 * mapped to the identity address. So allow static random
7830 * and public addresses here.
7831 */
ba74b666
JH
7832 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7833 (key->bdaddr.b[5] & 0xc0) != 0xc0)
7834 ev.store_hint = 0x00;
7835 else
53ac6ab6 7836 ev.store_hint = persistent;
ba74b666 7837
346af67b 7838 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
57c1477c 7839 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
d7b25450 7840 ev.key.type = mgmt_ltk_type(key);
346af67b
VCG
7841 ev.key.enc_size = key->enc_size;
7842 ev.key.ediv = key->ediv;
fe39c7b2 7843 ev.key.rand = key->rand;
346af67b 7844
2ceba539 7845 if (key->type == SMP_LTK)
346af67b
VCG
7846 ev.key.master = 1;
7847
1fc62c52
JH
7848 /* Make sure we copy only the significant bytes based on the
7849 * encryption key size, and set the rest of the value to zeroes.
7850 */
cb92205b 7851 memcpy(ev.key.val, key->val, key->enc_size);
1fc62c52
JH
7852 memset(ev.key.val + key->enc_size, 0,
7853 sizeof(ev.key.val) - key->enc_size);
346af67b 7854
083368f7 7855 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
346af67b
VCG
7856}
7857
cad20c27 7858void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent)
95fbac8a
JH
7859{
7860 struct mgmt_ev_new_irk ev;
7861
7862 memset(&ev, 0, sizeof(ev));
7863
cad20c27 7864 ev.store_hint = persistent;
bab6d1e5 7865
95fbac8a
JH
7866 bacpy(&ev.rpa, &irk->rpa);
7867 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
7868 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
7869 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
7870
7871 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
7872}
7873
53ac6ab6
MH
7874void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
7875 bool persistent)
7ee4ea36
MH
7876{
7877 struct mgmt_ev_new_csrk ev;
7878
7879 memset(&ev, 0, sizeof(ev));
7880
7881 /* Devices using resolvable or non-resolvable random addresses
f72186d2 7882 * without providing an identity resolving key don't require
7ee4ea36
MH
7883 * to store signature resolving keys. Their addresses will change
7884 * the next time around.
7885 *
7886 * Only when a remote device provides an identity address
7887 * make sure the signature resolving key is stored. So allow
7888 * static random and public addresses here.
7889 */
7890 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
7891 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
7892 ev.store_hint = 0x00;
7893 else
53ac6ab6 7894 ev.store_hint = persistent;
7ee4ea36
MH
7895
7896 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
7897 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
4cd3928a 7898 ev.key.type = csrk->type;
7ee4ea36
MH
7899 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
7900
7901 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
7902}
7903
ffb5a827 7904void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
f4869e2a
JH
7905 u8 bdaddr_type, u8 store_hint, u16 min_interval,
7906 u16 max_interval, u16 latency, u16 timeout)
ffb5a827
AG
7907{
7908 struct mgmt_ev_new_conn_param ev;
7909
c103aea6
JH
7910 if (!hci_is_identity_address(bdaddr, bdaddr_type))
7911 return;
7912
ffb5a827
AG
7913 memset(&ev, 0, sizeof(ev));
7914 bacpy(&ev.addr.bdaddr, bdaddr);
7915 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
f4869e2a 7916 ev.store_hint = store_hint;
ffb5a827
AG
7917 ev.min_interval = cpu_to_le16(min_interval);
7918 ev.max_interval = cpu_to_le16(max_interval);
7919 ev.latency = cpu_to_le16(latency);
7920 ev.timeout = cpu_to_le16(timeout);
7921
7922 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
7923}
7924
48ec92fa
AA
7925void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
7926 u32 flags, u8 *name, u8 name_len)
f7520543 7927{
b644ba33
JH
7928 char buf[512];
7929 struct mgmt_ev_device_connected *ev = (void *) buf;
7930 u16 eir_len = 0;
f7520543 7931
48ec92fa
AA
7932 bacpy(&ev->addr.bdaddr, &conn->dst);
7933 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
f7520543 7934
c95f0ba7 7935 ev->flags = __cpu_to_le32(flags);
08c79b61 7936
fd45ada9
AA
7937 /* We must ensure that the EIR Data fields are ordered and
7938 * unique. Keep it simple for now and avoid the problem by not
7939 * adding any BR/EDR data to the LE adv.
7940 */
7941 if (conn->le_adv_data_len > 0) {
7942 memcpy(&ev->eir[eir_len],
7943 conn->le_adv_data, conn->le_adv_data_len);
7944 eir_len = conn->le_adv_data_len;
7945 } else {
7946 if (name_len > 0)
7947 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE,
7948 name, name_len);
b644ba33 7949
ddbea5cf 7950 if (memcmp(conn->dev_class, "\0\0\0", 3) != 0)
fd45ada9
AA
7951 eir_len = eir_append_data(ev->eir, eir_len,
7952 EIR_CLASS_OF_DEV,
7953 conn->dev_class, 3);
7954 }
b644ba33 7955
eb55ef07 7956 ev->eir_len = cpu_to_le16(eir_len);
b644ba33 7957
ecd90ae7
MH
7958 mgmt_event(MGMT_EV_DEVICE_CONNECTED, hdev, buf,
7959 sizeof(*ev) + eir_len, NULL);
f7520543
JH
7960}
7961
3b0602cd 7962static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
8962ee74 7963{
8962ee74 7964 struct sock **sk = data;
8962ee74 7965
f5818c22 7966 cmd->cmd_complete(cmd, 0);
8962ee74
JH
7967
7968 *sk = cmd->sk;
7969 sock_hold(*sk);
7970
a664b5bc 7971 mgmt_pending_remove(cmd);
8962ee74
JH
7972}
7973
3b0602cd 7974static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
a8a1d19e 7975{
b1078ad0 7976 struct hci_dev *hdev = data;
124f6e35 7977 struct mgmt_cp_unpair_device *cp = cmd->param;
a8a1d19e 7978
b1078ad0
JH
7979 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
7980
d8b7b1e4 7981 cmd->cmd_complete(cmd, 0);
a8a1d19e
JH
7982 mgmt_pending_remove(cmd);
7983}
7984
84c61d92
JH
7985bool mgmt_powering_down(struct hci_dev *hdev)
7986{
3b0602cd 7987 struct mgmt_pending_cmd *cmd;
84c61d92
JH
7988 struct mgmt_mode *cp;
7989
333ae95d 7990 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
84c61d92
JH
7991 if (!cmd)
7992 return false;
7993
7994 cp = cmd->param;
7995 if (!cp->val)
7996 return true;
7997
7998 return false;
7999}
8000
9b80ec5e 8001void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
12d4a3b2
JH
8002 u8 link_type, u8 addr_type, u8 reason,
8003 bool mgmt_connected)
f7520543 8004{
f0d6a0ea 8005 struct mgmt_ev_device_disconnected ev;
8962ee74 8006 struct sock *sk = NULL;
8962ee74 8007
84c61d92
JH
8008 /* The connection is still in hci_conn_hash so test for 1
8009 * instead of 0 to know if this is the last one.
8010 */
8011 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
8012 cancel_delayed_work(&hdev->power_off);
8013 queue_work(hdev->req_workqueue, &hdev->power_off.work);
8b064a3a
JH
8014 }
8015
12d4a3b2
JH
8016 if (!mgmt_connected)
8017 return;
8018
57eb776f
AG
8019 if (link_type != ACL_LINK && link_type != LE_LINK)
8020 return;
8021
744cf19e 8022 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
f7520543 8023
f0d6a0ea
MA
8024 bacpy(&ev.addr.bdaddr, bdaddr);
8025 ev.addr.type = link_to_bdaddr(link_type, addr_type);
8026 ev.reason = reason;
f7520543 8027
9b80ec5e 8028 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
8962ee74
JH
8029
8030 if (sk)
d97dcb66 8031 sock_put(sk);
8962ee74 8032
124f6e35 8033 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
04124681 8034 hdev);
8962ee74
JH
8035}
8036
7892924c
MH
8037void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
8038 u8 link_type, u8 addr_type, u8 status)
8962ee74 8039{
3655bba8
AG
8040 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
8041 struct mgmt_cp_disconnect *cp;
3b0602cd 8042 struct mgmt_pending_cmd *cmd;
8962ee74 8043
36a75f1b
JD
8044 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
8045 hdev);
8046
333ae95d 8047 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
8962ee74 8048 if (!cmd)
7892924c 8049 return;
8962ee74 8050
3655bba8
AG
8051 cp = cmd->param;
8052
8053 if (bacmp(bdaddr, &cp->addr.bdaddr))
8054 return;
8055
8056 if (cp->addr.type != bdaddr_type)
8057 return;
8058
f5818c22 8059 cmd->cmd_complete(cmd, mgmt_status(status));
a664b5bc 8060 mgmt_pending_remove(cmd);
f7520543 8061}
17d5c04c 8062
445608d0
MH
8063void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8064 u8 addr_type, u8 status)
17d5c04c
JH
8065{
8066 struct mgmt_ev_connect_failed ev;
c9910d0f 8067
84c61d92
JH
8068 /* The connection is still in hci_conn_hash so test for 1
8069 * instead of 0 to know if this is the last one.
8070 */
8071 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
8072 cancel_delayed_work(&hdev->power_off);
8073 queue_work(hdev->req_workqueue, &hdev->power_off.work);
c9910d0f 8074 }
17d5c04c 8075
4c659c39 8076 bacpy(&ev.addr.bdaddr, bdaddr);
57c1477c 8077 ev.addr.type = link_to_bdaddr(link_type, addr_type);
ca69b795 8078 ev.status = mgmt_status(status);
17d5c04c 8079
445608d0 8080 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
17d5c04c 8081}
980e1a53 8082
ce0e4a0d 8083void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
980e1a53
JH
8084{
8085 struct mgmt_ev_pin_code_request ev;
8086
d8457698 8087 bacpy(&ev.addr.bdaddr, bdaddr);
591f47f3 8088 ev.addr.type = BDADDR_BREDR;
a770bb5a 8089 ev.secure = secure;
980e1a53 8090
ce0e4a0d 8091 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
980e1a53
JH
8092}
8093
e669cf80
MH
8094void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
8095 u8 status)
980e1a53 8096{
3b0602cd 8097 struct mgmt_pending_cmd *cmd;
980e1a53 8098
333ae95d 8099 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
980e1a53 8100 if (!cmd)
e669cf80 8101 return;
980e1a53 8102
7776d1d8 8103 cmd->cmd_complete(cmd, mgmt_status(status));
a664b5bc 8104 mgmt_pending_remove(cmd);
980e1a53
JH
8105}
8106
3eb38528
MH
8107void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
8108 u8 status)
980e1a53 8109{
3b0602cd 8110 struct mgmt_pending_cmd *cmd;
980e1a53 8111
333ae95d 8112 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
980e1a53 8113 if (!cmd)
3eb38528 8114 return;
980e1a53 8115
7776d1d8 8116 cmd->cmd_complete(cmd, mgmt_status(status));
a664b5bc 8117 mgmt_pending_remove(cmd);
980e1a53 8118}
a5c29683 8119
744cf19e 8120int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
39adbffe 8121 u8 link_type, u8 addr_type, u32 value,
04124681 8122 u8 confirm_hint)
a5c29683
JH
8123{
8124 struct mgmt_ev_user_confirm_request ev;
8125
744cf19e 8126 BT_DBG("%s", hdev->name);
a5c29683 8127
272d90df 8128 bacpy(&ev.addr.bdaddr, bdaddr);
57c1477c 8129 ev.addr.type = link_to_bdaddr(link_type, addr_type);
55bc1a37 8130 ev.confirm_hint = confirm_hint;
39adbffe 8131 ev.value = cpu_to_le32(value);
a5c29683 8132
744cf19e 8133 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
04124681 8134 NULL);
a5c29683
JH
8135}
8136
272d90df 8137int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
8ce8e2b5 8138 u8 link_type, u8 addr_type)
604086b7
BG
8139{
8140 struct mgmt_ev_user_passkey_request ev;
8141
8142 BT_DBG("%s", hdev->name);
8143
272d90df 8144 bacpy(&ev.addr.bdaddr, bdaddr);
57c1477c 8145 ev.addr.type = link_to_bdaddr(link_type, addr_type);
604086b7
BG
8146
8147 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
04124681 8148 NULL);
604086b7
BG
8149}
8150
0df4c185 8151static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
8ce8e2b5
GP
8152 u8 link_type, u8 addr_type, u8 status,
8153 u8 opcode)
a5c29683 8154{
3b0602cd 8155 struct mgmt_pending_cmd *cmd;
a5c29683 8156
333ae95d 8157 cmd = pending_find(opcode, hdev);
a5c29683
JH
8158 if (!cmd)
8159 return -ENOENT;
8160
7776d1d8 8161 cmd->cmd_complete(cmd, mgmt_status(status));
a664b5bc 8162 mgmt_pending_remove(cmd);
a5c29683 8163
7776d1d8 8164 return 0;
a5c29683
JH
8165}
8166
744cf19e 8167int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
04124681 8168 u8 link_type, u8 addr_type, u8 status)
a5c29683 8169{
272d90df 8170 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
04124681 8171 status, MGMT_OP_USER_CONFIRM_REPLY);
a5c29683
JH
8172}
8173
272d90df 8174int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
04124681 8175 u8 link_type, u8 addr_type, u8 status)
a5c29683 8176{
272d90df 8177 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
8fc9ced3
GP
8178 status,
8179 MGMT_OP_USER_CONFIRM_NEG_REPLY);
a5c29683 8180}
2a611692 8181
604086b7 8182int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
04124681 8183 u8 link_type, u8 addr_type, u8 status)
604086b7 8184{
272d90df 8185 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
04124681 8186 status, MGMT_OP_USER_PASSKEY_REPLY);
604086b7
BG
8187}
8188
272d90df 8189int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
04124681 8190 u8 link_type, u8 addr_type, u8 status)
604086b7 8191{
272d90df 8192 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
8fc9ced3
GP
8193 status,
8194 MGMT_OP_USER_PASSKEY_NEG_REPLY);
604086b7
BG
8195}
8196
92a25256
JH
8197int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
8198 u8 link_type, u8 addr_type, u32 passkey,
8199 u8 entered)
8200{
8201 struct mgmt_ev_passkey_notify ev;
8202
8203 BT_DBG("%s", hdev->name);
8204
8205 bacpy(&ev.addr.bdaddr, bdaddr);
8206 ev.addr.type = link_to_bdaddr(link_type, addr_type);
8207 ev.passkey = __cpu_to_le32(passkey);
8208 ev.entered = entered;
8209
8210 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
8211}
8212
e1e930f5 8213void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
2a611692
JH
8214{
8215 struct mgmt_ev_auth_failed ev;
3b0602cd 8216 struct mgmt_pending_cmd *cmd;
e1e930f5 8217 u8 status = mgmt_status(hci_status);
2a611692 8218
e1e930f5
JH
8219 bacpy(&ev.addr.bdaddr, &conn->dst);
8220 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
8221 ev.status = status;
2a611692 8222
e1e930f5
JH
8223 cmd = find_pairing(conn);
8224
8225 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
8226 cmd ? cmd->sk : NULL);
8227
a511b35b
JH
8228 if (cmd) {
8229 cmd->cmd_complete(cmd, status);
8230 mgmt_pending_remove(cmd);
8231 }
2a611692 8232}
b312b161 8233
464996ae 8234void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
33ef95ed
JH
8235{
8236 struct cmd_lookup match = { NULL, hdev };
464996ae 8237 bool changed;
33ef95ed
JH
8238
8239 if (status) {
8240 u8 mgmt_err = mgmt_status(status);
8241 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
04124681 8242 cmd_status_rsp, &mgmt_err);
464996ae 8243 return;
33ef95ed
JH
8244 }
8245
464996ae 8246 if (test_bit(HCI_AUTH, &hdev->flags))
238be788 8247 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
464996ae 8248 else
a69d8927 8249 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
47990ea0 8250
33ef95ed 8251 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
04124681 8252 &match);
33ef95ed 8253
47990ea0 8254 if (changed)
464996ae 8255 new_settings(hdev, match.sk);
33ef95ed
JH
8256
8257 if (match.sk)
8258 sock_put(match.sk);
33ef95ed
JH
8259}
8260
890ea898 8261static void clear_eir(struct hci_request *req)
cacaf52f 8262{
890ea898 8263 struct hci_dev *hdev = req->hdev;
cacaf52f
JH
8264 struct hci_cp_write_eir cp;
8265
976eb20e 8266 if (!lmp_ext_inq_capable(hdev))
890ea898 8267 return;
cacaf52f 8268
c80da27e
JH
8269 memset(hdev->eir, 0, sizeof(hdev->eir));
8270
cacaf52f
JH
8271 memset(&cp, 0, sizeof(cp));
8272
890ea898 8273 hci_req_add(req, HCI_OP_WRITE_EIR, sizeof(cp), &cp);
cacaf52f
JH
8274}
8275
3e248560 8276void mgmt_ssp_enable_complete(struct hci_dev *hdev, u8 enable, u8 status)
ed2c4ee3
JH
8277{
8278 struct cmd_lookup match = { NULL, hdev };
890ea898 8279 struct hci_request req;
c0ecddc2 8280 bool changed = false;
ed2c4ee3
JH
8281
8282 if (status) {
8283 u8 mgmt_err = mgmt_status(status);
c0ecddc2 8284
a69d8927
MH
8285 if (enable && hci_dev_test_and_clear_flag(hdev,
8286 HCI_SSP_ENABLED)) {
a358dc11 8287 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
3e248560 8288 new_settings(hdev, NULL);
9ecb3e24 8289 }
c0ecddc2 8290
04124681
GP
8291 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
8292 &mgmt_err);
3e248560 8293 return;
c0ecddc2
JH
8294 }
8295
8296 if (enable) {
238be788 8297 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
c0ecddc2 8298 } else {
a69d8927 8299 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
9ecb3e24 8300 if (!changed)
a69d8927
MH
8301 changed = hci_dev_test_and_clear_flag(hdev,
8302 HCI_HS_ENABLED);
9ecb3e24 8303 else
a358dc11 8304 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
ed2c4ee3
JH
8305 }
8306
8307 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
8308
c0ecddc2 8309 if (changed)
3e248560 8310 new_settings(hdev, match.sk);
ed2c4ee3 8311
5fc6ebb1 8312 if (match.sk)
ed2c4ee3
JH
8313 sock_put(match.sk);
8314
890ea898
JH
8315 hci_req_init(&req, hdev);
8316
d7a5a11d
MH
8317 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
8318 if (hci_dev_test_flag(hdev, HCI_USE_DEBUG_KEYS))
3769972b
JH
8319 hci_req_add(&req, HCI_OP_WRITE_SSP_DEBUG_MODE,
8320 sizeof(enable), &enable);
890ea898 8321 update_eir(&req);
3769972b 8322 } else {
890ea898 8323 clear_eir(&req);
3769972b 8324 }
890ea898
JH
8325
8326 hci_req_run(&req, NULL);
ed2c4ee3
JH
8327}
8328
3b0602cd 8329static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
90e70454
JH
8330{
8331 struct cmd_lookup *match = data;
8332
90e70454
JH
8333 if (match->sk == NULL) {
8334 match->sk = cmd->sk;
8335 sock_hold(match->sk);
8336 }
90e70454
JH
8337}
8338
4e1b0245
MH
8339void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
8340 u8 status)
7f9a903c 8341{
90e70454 8342 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
7f9a903c 8343
92da6097
JH
8344 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
8345 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
8346 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
90e70454
JH
8347
8348 if (!status)
f6b7712e
MH
8349 mgmt_generic_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
8350 dev_class, 3, NULL);
90e70454
JH
8351
8352 if (match.sk)
8353 sock_put(match.sk);
7f9a903c
MH
8354}
8355
7667da34 8356void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
b312b161 8357{
b312b161 8358 struct mgmt_cp_set_local_name ev;
3b0602cd 8359 struct mgmt_pending_cmd *cmd;
28cc7bde 8360
13928971 8361 if (status)
7667da34 8362 return;
b312b161
JH
8363
8364 memset(&ev, 0, sizeof(ev));
8365 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
28cc7bde 8366 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
b312b161 8367
333ae95d 8368 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
13928971
JH
8369 if (!cmd) {
8370 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
28cc7bde 8371
13928971
JH
8372 /* If this is a HCI command related to powering on the
8373 * HCI dev don't send any mgmt signals.
8374 */
333ae95d 8375 if (pending_find(MGMT_OP_SET_POWERED, hdev))
7667da34 8376 return;
890ea898 8377 }
b312b161 8378
f6b7712e
MH
8379 mgmt_generic_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
8380 cmd ? cmd->sk : NULL);
b312b161 8381}
c35938b2 8382
799ce93d
JP
8383static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
8384{
8385 int i;
8386
8387 for (i = 0; i < uuid_count; i++) {
8388 if (!memcmp(uuid, uuids[i], 16))
8389 return true;
8390 }
8391
8392 return false;
8393}
8394
b487b9ce
MH
8395static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
8396{
799ce93d
JP
8397 u16 parsed = 0;
8398
8399 while (parsed < eir_len) {
8400 u8 field_len = eir[0];
8401 u8 uuid[16];
8402 int i;
8403
8404 if (field_len == 0)
8405 break;
8406
8407 if (eir_len - parsed < field_len + 1)
8408 break;
8409
8410 switch (eir[1]) {
8411 case EIR_UUID16_ALL:
8412 case EIR_UUID16_SOME:
8413 for (i = 0; i + 3 <= field_len; i += 2) {
189f6ad2 8414 memcpy(uuid, bluetooth_base_uuid, 16);
799ce93d
JP
8415 uuid[13] = eir[i + 3];
8416 uuid[12] = eir[i + 2];
8417 if (has_uuid(uuid, uuid_count, uuids))
8418 return true;
8419 }
8420 break;
8421 case EIR_UUID32_ALL:
8422 case EIR_UUID32_SOME:
8423 for (i = 0; i + 5 <= field_len; i += 4) {
189f6ad2 8424 memcpy(uuid, bluetooth_base_uuid, 16);
799ce93d
JP
8425 uuid[15] = eir[i + 5];
8426 uuid[14] = eir[i + 4];
8427 uuid[13] = eir[i + 3];
8428 uuid[12] = eir[i + 2];
8429 if (has_uuid(uuid, uuid_count, uuids))
8430 return true;
8431 }
8432 break;
8433 case EIR_UUID128_ALL:
8434 case EIR_UUID128_SOME:
8435 for (i = 0; i + 17 <= field_len; i += 16) {
8436 memcpy(uuid, eir + i + 2, 16);
8437 if (has_uuid(uuid, uuid_count, uuids))
8438 return true;
8439 }
8440 break;
8441 }
8442
8443 parsed += field_len + 1;
8444 eir += field_len + 1;
8445 }
8446
b487b9ce
MH
8447 return false;
8448}
8449
4b0e0ced
JP
8450static void restart_le_scan(struct hci_dev *hdev)
8451{
8452 /* If controller is not scanning we are done. */
d7a5a11d 8453 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
4b0e0ced
JP
8454 return;
8455
8456 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
8457 hdev->discovery.scan_start +
8458 hdev->discovery.scan_duration))
8459 return;
8460
8461 queue_delayed_work(hdev->workqueue, &hdev->le_scan_restart,
8462 DISCOV_LE_RESTART_DELAY);
8463}
8464
48f86b7f
JP
8465static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
8466 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
e17acd40 8467{
48f86b7f
JP
8468 /* If a RSSI threshold has been specified, and
8469 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
8470 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
8471 * is set, let it through for further processing, as we might need to
8472 * restart the scan.
efb2513f
MH
8473 *
8474 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
8475 * the results are also dropped.
bda157a4
MH
8476 */
8477 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
4b0e0ced
JP
8478 (rssi == HCI_RSSI_INVALID ||
8479 (rssi < hdev->discovery.rssi &&
8480 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
48f86b7f 8481 return false;
efb2513f 8482
2976cdeb
JP
8483 if (hdev->discovery.uuid_count != 0) {
8484 /* If a list of UUIDs is provided in filter, results with no
8485 * matching UUID should be dropped.
b487b9ce 8486 */
2976cdeb
JP
8487 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
8488 hdev->discovery.uuids) &&
8489 !eir_has_uuids(scan_rsp, scan_rsp_len,
8490 hdev->discovery.uuid_count,
8491 hdev->discovery.uuids))
8492 return false;
b487b9ce 8493 }
5d2e9fad 8494
2976cdeb
JP
8495 /* If duplicate filtering does not report RSSI changes, then restart
8496 * scanning to ensure updated result with updated RSSI values.
4b0e0ced 8497 */
2976cdeb
JP
8498 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
8499 restart_le_scan(hdev);
8500
8501 /* Validate RSSI value against the RSSI threshold once more. */
8502 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
8503 rssi < hdev->discovery.rssi)
8504 return false;
8505 }
48f86b7f
JP
8506
8507 return true;
8508}
8509
8510void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8511 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
8512 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
8513{
8514 char buf[512];
8515 struct mgmt_ev_device_found *ev = (void *)buf;
8516 size_t ev_size;
8517
8518 /* Don't send events for a non-kernel initiated discovery. With
8519 * LE one exception is if we have pend_le_reports > 0 in which
8520 * case we're doing passive scanning and want these events.
8521 */
8522 if (!hci_discovery_active(hdev)) {
8523 if (link_type == ACL_LINK)
8524 return;
8525 if (link_type == LE_LINK && list_empty(&hdev->pend_le_reports))
8526 return;
8527 }
8528
82f8b651 8529 if (hdev->discovery.result_filtering) {
48f86b7f
JP
8530 /* We are using service discovery */
8531 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
8532 scan_rsp_len))
8533 return;
8534 }
8535
8536 /* Make sure that the buffer is big enough. The 5 extra bytes
8537 * are for the potential CoD field.
8538 */
8539 if (sizeof(*ev) + eir_len + scan_rsp_len + 5 > sizeof(buf))
4b0e0ced
JP
8540 return;
8541
48f86b7f
JP
8542 memset(buf, 0, sizeof(buf));
8543
8544 /* In case of device discovery with BR/EDR devices (pre 1.2), the
8545 * RSSI value was reported as 0 when not available. This behavior
8546 * is kept when using device discovery. This is required for full
8547 * backwards compatibility with the API.
8548 *
8549 * However when using service discovery, the value 127 will be
8550 * returned when the RSSI is not available.
8551 */
8552 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
8553 link_type == ACL_LINK)
8554 rssi = 0;
8555
8556 bacpy(&ev->addr.bdaddr, bdaddr);
8557 ev->addr.type = link_to_bdaddr(link_type, addr_type);
8558 ev->rssi = rssi;
8559 ev->flags = cpu_to_le32(flags);
8560
8561 if (eir_len > 0)
8562 /* Copy EIR or advertising data into event */
8563 memcpy(ev->eir, eir, eir_len);
8564
8565 if (dev_class && !eir_has_data_type(ev->eir, eir_len, EIR_CLASS_OF_DEV))
8566 eir_len = eir_append_data(ev->eir, eir_len, EIR_CLASS_OF_DEV,
8567 dev_class, 3);
8568
8569 if (scan_rsp_len > 0)
8570 /* Append scan response data to event */
8571 memcpy(ev->eir + eir_len, scan_rsp, scan_rsp_len);
8572
5d2e9fad
JH
8573 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
8574 ev_size = sizeof(*ev) + eir_len + scan_rsp_len;
f8523598 8575
901801b9 8576 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, ev_size, NULL);
e17acd40 8577}
a88a9652 8578
9cf12aee
MH
8579void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
8580 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
a88a9652 8581{
b644ba33
JH
8582 struct mgmt_ev_device_found *ev;
8583 char buf[sizeof(*ev) + HCI_MAX_NAME_LENGTH + 2];
8584 u16 eir_len;
a88a9652 8585
b644ba33 8586 ev = (struct mgmt_ev_device_found *) buf;
a88a9652 8587
b644ba33
JH
8588 memset(buf, 0, sizeof(buf));
8589
8590 bacpy(&ev->addr.bdaddr, bdaddr);
57c1477c 8591 ev->addr.type = link_to_bdaddr(link_type, addr_type);
b644ba33
JH
8592 ev->rssi = rssi;
8593
8594 eir_len = eir_append_data(ev->eir, 0, EIR_NAME_COMPLETE, name,
04124681 8595 name_len);
b644ba33 8596
eb55ef07 8597 ev->eir_len = cpu_to_le16(eir_len);
a88a9652 8598
9cf12aee 8599 mgmt_event(MGMT_EV_DEVICE_FOUND, hdev, ev, sizeof(*ev) + eir_len, NULL);
a88a9652 8600}
314b2381 8601
2f1e063b 8602void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
314b2381 8603{
f963e8e9 8604 struct mgmt_ev_discovering ev;
164a6e78 8605
343fb145
AG
8606 BT_DBG("%s discovering %u", hdev->name, discovering);
8607
f963e8e9
JH
8608 memset(&ev, 0, sizeof(ev));
8609 ev.type = hdev->discovery.type;
8610 ev.discovering = discovering;
8611
2f1e063b 8612 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
314b2381 8613}
5e762444 8614
1904a853 8615static void adv_enable_complete(struct hci_dev *hdev, u8 status, u16 opcode)
5976e608
MH
8616{
8617 BT_DBG("%s status %u", hdev->name, status);
5976e608
MH
8618}
8619
8620void mgmt_reenable_advertising(struct hci_dev *hdev)
8621{
8622 struct hci_request req;
eb6f95f9 8623 u8 instance;
5976e608 8624
24b4f38f
AU
8625 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
8626 !hci_dev_test_flag(hdev, HCI_ADVERTISING_INSTANCE))
5976e608
MH
8627 return;
8628
eb6f95f9
FG
8629 instance = get_current_adv_instance(hdev);
8630
5976e608 8631 hci_req_init(&req, hdev);
eb6f95f9
FG
8632
8633 if (instance) {
8634 schedule_adv_instance(&req, instance, true);
8635 } else {
8636 update_adv_data(&req);
8637 update_scan_rsp_data(&req);
8638 enable_advertising(&req);
8639 }
8640
0ec5ae84 8641 hci_req_run(&req, adv_enable_complete);
5976e608 8642}
6d785aa3
JH
8643
8644static struct hci_mgmt_chan chan = {
8645 .channel = HCI_CHANNEL_CONTROL,
8646 .handler_count = ARRAY_SIZE(mgmt_handlers),
8647 .handlers = mgmt_handlers,
88b94ce9 8648 .hdev_init = mgmt_init_hdev,
6d785aa3
JH
8649};
8650
8651int mgmt_init(void)
8652{
8653 return hci_mgmt_chan_register(&chan);
8654}
8655
8656void mgmt_exit(void)
8657{
8658 hci_mgmt_chan_unregister(&chan);
8659}
This page took 0.996829 seconds and 5 git commands to generate.