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