2 BlueZ - Bluetooth protocol stack for Linux
4 Copyright (C) 2010 Nokia Corporation
5 Copyright (C) 2011-2012 Intel Corporation
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;
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.
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.
25 /* Bluetooth HCI Management interface */
27 #include <linux/module.h>
28 #include <asm/unaligned.h>
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
32 #include <net/bluetooth/hci_sock.h>
33 #include <net/bluetooth/l2cap.h>
34 #include <net/bluetooth/mgmt.h>
36 #include "hci_request.h"
38 #include "mgmt_util.h"
39 #include "mgmt_config.h"
44 #define MGMT_VERSION 1
45 #define MGMT_REVISION 22
47 static const u16 mgmt_commands[] = {
48 MGMT_OP_READ_INDEX_LIST,
51 MGMT_OP_SET_DISCOVERABLE,
52 MGMT_OP_SET_CONNECTABLE,
53 MGMT_OP_SET_FAST_CONNECTABLE,
55 MGMT_OP_SET_LINK_SECURITY,
59 MGMT_OP_SET_DEV_CLASS,
60 MGMT_OP_SET_LOCAL_NAME,
63 MGMT_OP_LOAD_LINK_KEYS,
64 MGMT_OP_LOAD_LONG_TERM_KEYS,
66 MGMT_OP_GET_CONNECTIONS,
67 MGMT_OP_PIN_CODE_REPLY,
68 MGMT_OP_PIN_CODE_NEG_REPLY,
69 MGMT_OP_SET_IO_CAPABILITY,
71 MGMT_OP_CANCEL_PAIR_DEVICE,
72 MGMT_OP_UNPAIR_DEVICE,
73 MGMT_OP_USER_CONFIRM_REPLY,
74 MGMT_OP_USER_CONFIRM_NEG_REPLY,
75 MGMT_OP_USER_PASSKEY_REPLY,
76 MGMT_OP_USER_PASSKEY_NEG_REPLY,
77 MGMT_OP_READ_LOCAL_OOB_DATA,
78 MGMT_OP_ADD_REMOTE_OOB_DATA,
79 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
80 MGMT_OP_START_DISCOVERY,
81 MGMT_OP_STOP_DISCOVERY,
84 MGMT_OP_UNBLOCK_DEVICE,
85 MGMT_OP_SET_DEVICE_ID,
86 MGMT_OP_SET_ADVERTISING,
88 MGMT_OP_SET_STATIC_ADDRESS,
89 MGMT_OP_SET_SCAN_PARAMS,
90 MGMT_OP_SET_SECURE_CONN,
91 MGMT_OP_SET_DEBUG_KEYS,
94 MGMT_OP_GET_CONN_INFO,
95 MGMT_OP_GET_CLOCK_INFO,
97 MGMT_OP_REMOVE_DEVICE,
98 MGMT_OP_LOAD_CONN_PARAM,
99 MGMT_OP_READ_UNCONF_INDEX_LIST,
100 MGMT_OP_READ_CONFIG_INFO,
101 MGMT_OP_SET_EXTERNAL_CONFIG,
102 MGMT_OP_SET_PUBLIC_ADDRESS,
103 MGMT_OP_START_SERVICE_DISCOVERY,
104 MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
105 MGMT_OP_READ_EXT_INDEX_LIST,
106 MGMT_OP_READ_ADV_FEATURES,
107 MGMT_OP_ADD_ADVERTISING,
108 MGMT_OP_REMOVE_ADVERTISING,
109 MGMT_OP_GET_ADV_SIZE_INFO,
110 MGMT_OP_START_LIMITED_DISCOVERY,
111 MGMT_OP_READ_EXT_INFO,
112 MGMT_OP_SET_APPEARANCE,
113 MGMT_OP_GET_PHY_CONFIGURATION,
114 MGMT_OP_SET_PHY_CONFIGURATION,
115 MGMT_OP_SET_BLOCKED_KEYS,
116 MGMT_OP_SET_WIDEBAND_SPEECH,
117 MGMT_OP_READ_CONTROLLER_CAP,
118 MGMT_OP_READ_EXP_FEATURES_INFO,
119 MGMT_OP_SET_EXP_FEATURE,
120 MGMT_OP_READ_DEF_SYSTEM_CONFIG,
121 MGMT_OP_SET_DEF_SYSTEM_CONFIG,
122 MGMT_OP_READ_DEF_RUNTIME_CONFIG,
123 MGMT_OP_SET_DEF_RUNTIME_CONFIG,
124 MGMT_OP_GET_DEVICE_FLAGS,
125 MGMT_OP_SET_DEVICE_FLAGS,
126 MGMT_OP_READ_ADV_MONITOR_FEATURES,
127 MGMT_OP_ADD_ADV_PATTERNS_MONITOR,
128 MGMT_OP_REMOVE_ADV_MONITOR,
129 MGMT_OP_ADD_EXT_ADV_PARAMS,
130 MGMT_OP_ADD_EXT_ADV_DATA,
131 MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI,
132 MGMT_OP_SET_MESH_RECEIVER,
133 MGMT_OP_MESH_READ_FEATURES,
135 MGMT_OP_MESH_SEND_CANCEL,
138 static const u16 mgmt_events[] = {
139 MGMT_EV_CONTROLLER_ERROR,
141 MGMT_EV_INDEX_REMOVED,
142 MGMT_EV_NEW_SETTINGS,
143 MGMT_EV_CLASS_OF_DEV_CHANGED,
144 MGMT_EV_LOCAL_NAME_CHANGED,
145 MGMT_EV_NEW_LINK_KEY,
146 MGMT_EV_NEW_LONG_TERM_KEY,
147 MGMT_EV_DEVICE_CONNECTED,
148 MGMT_EV_DEVICE_DISCONNECTED,
149 MGMT_EV_CONNECT_FAILED,
150 MGMT_EV_PIN_CODE_REQUEST,
151 MGMT_EV_USER_CONFIRM_REQUEST,
152 MGMT_EV_USER_PASSKEY_REQUEST,
154 MGMT_EV_DEVICE_FOUND,
156 MGMT_EV_DEVICE_BLOCKED,
157 MGMT_EV_DEVICE_UNBLOCKED,
158 MGMT_EV_DEVICE_UNPAIRED,
159 MGMT_EV_PASSKEY_NOTIFY,
162 MGMT_EV_DEVICE_ADDED,
163 MGMT_EV_DEVICE_REMOVED,
164 MGMT_EV_NEW_CONN_PARAM,
165 MGMT_EV_UNCONF_INDEX_ADDED,
166 MGMT_EV_UNCONF_INDEX_REMOVED,
167 MGMT_EV_NEW_CONFIG_OPTIONS,
168 MGMT_EV_EXT_INDEX_ADDED,
169 MGMT_EV_EXT_INDEX_REMOVED,
170 MGMT_EV_LOCAL_OOB_DATA_UPDATED,
171 MGMT_EV_ADVERTISING_ADDED,
172 MGMT_EV_ADVERTISING_REMOVED,
173 MGMT_EV_EXT_INFO_CHANGED,
174 MGMT_EV_PHY_CONFIGURATION_CHANGED,
175 MGMT_EV_EXP_FEATURE_CHANGED,
176 MGMT_EV_DEVICE_FLAGS_CHANGED,
177 MGMT_EV_ADV_MONITOR_ADDED,
178 MGMT_EV_ADV_MONITOR_REMOVED,
179 MGMT_EV_CONTROLLER_SUSPEND,
180 MGMT_EV_CONTROLLER_RESUME,
181 MGMT_EV_ADV_MONITOR_DEVICE_FOUND,
182 MGMT_EV_ADV_MONITOR_DEVICE_LOST,
185 static const u16 mgmt_untrusted_commands[] = {
186 MGMT_OP_READ_INDEX_LIST,
188 MGMT_OP_READ_UNCONF_INDEX_LIST,
189 MGMT_OP_READ_CONFIG_INFO,
190 MGMT_OP_READ_EXT_INDEX_LIST,
191 MGMT_OP_READ_EXT_INFO,
192 MGMT_OP_READ_CONTROLLER_CAP,
193 MGMT_OP_READ_EXP_FEATURES_INFO,
194 MGMT_OP_READ_DEF_SYSTEM_CONFIG,
195 MGMT_OP_READ_DEF_RUNTIME_CONFIG,
198 static const u16 mgmt_untrusted_events[] = {
200 MGMT_EV_INDEX_REMOVED,
201 MGMT_EV_NEW_SETTINGS,
202 MGMT_EV_CLASS_OF_DEV_CHANGED,
203 MGMT_EV_LOCAL_NAME_CHANGED,
204 MGMT_EV_UNCONF_INDEX_ADDED,
205 MGMT_EV_UNCONF_INDEX_REMOVED,
206 MGMT_EV_NEW_CONFIG_OPTIONS,
207 MGMT_EV_EXT_INDEX_ADDED,
208 MGMT_EV_EXT_INDEX_REMOVED,
209 MGMT_EV_EXT_INFO_CHANGED,
210 MGMT_EV_EXP_FEATURE_CHANGED,
213 #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
215 #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
216 "\x00\x00\x00\x00\x00\x00\x00\x00"
218 /* HCI to MGMT error code conversion table */
219 static const u8 mgmt_status_table[] = {
221 MGMT_STATUS_UNKNOWN_COMMAND, /* Unknown Command */
222 MGMT_STATUS_NOT_CONNECTED, /* No Connection */
223 MGMT_STATUS_FAILED, /* Hardware Failure */
224 MGMT_STATUS_CONNECT_FAILED, /* Page Timeout */
225 MGMT_STATUS_AUTH_FAILED, /* Authentication Failed */
226 MGMT_STATUS_AUTH_FAILED, /* PIN or Key Missing */
227 MGMT_STATUS_NO_RESOURCES, /* Memory Full */
228 MGMT_STATUS_TIMEOUT, /* Connection Timeout */
229 MGMT_STATUS_NO_RESOURCES, /* Max Number of Connections */
230 MGMT_STATUS_NO_RESOURCES, /* Max Number of SCO Connections */
231 MGMT_STATUS_ALREADY_CONNECTED, /* ACL Connection Exists */
232 MGMT_STATUS_BUSY, /* Command Disallowed */
233 MGMT_STATUS_NO_RESOURCES, /* Rejected Limited Resources */
234 MGMT_STATUS_REJECTED, /* Rejected Security */
235 MGMT_STATUS_REJECTED, /* Rejected Personal */
236 MGMT_STATUS_TIMEOUT, /* Host Timeout */
237 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Feature */
238 MGMT_STATUS_INVALID_PARAMS, /* Invalid Parameters */
239 MGMT_STATUS_DISCONNECTED, /* OE User Ended Connection */
240 MGMT_STATUS_NO_RESOURCES, /* OE Low Resources */
241 MGMT_STATUS_DISCONNECTED, /* OE Power Off */
242 MGMT_STATUS_DISCONNECTED, /* Connection Terminated */
243 MGMT_STATUS_BUSY, /* Repeated Attempts */
244 MGMT_STATUS_REJECTED, /* Pairing Not Allowed */
245 MGMT_STATUS_FAILED, /* Unknown LMP PDU */
246 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported Remote Feature */
247 MGMT_STATUS_REJECTED, /* SCO Offset Rejected */
248 MGMT_STATUS_REJECTED, /* SCO Interval Rejected */
249 MGMT_STATUS_REJECTED, /* Air Mode Rejected */
250 MGMT_STATUS_INVALID_PARAMS, /* Invalid LMP Parameters */
251 MGMT_STATUS_FAILED, /* Unspecified Error */
252 MGMT_STATUS_NOT_SUPPORTED, /* Unsupported LMP Parameter Value */
253 MGMT_STATUS_FAILED, /* Role Change Not Allowed */
254 MGMT_STATUS_TIMEOUT, /* LMP Response Timeout */
255 MGMT_STATUS_FAILED, /* LMP Error Transaction Collision */
256 MGMT_STATUS_FAILED, /* LMP PDU Not Allowed */
257 MGMT_STATUS_REJECTED, /* Encryption Mode Not Accepted */
258 MGMT_STATUS_FAILED, /* Unit Link Key Used */
259 MGMT_STATUS_NOT_SUPPORTED, /* QoS Not Supported */
260 MGMT_STATUS_TIMEOUT, /* Instant Passed */
261 MGMT_STATUS_NOT_SUPPORTED, /* Pairing Not Supported */
262 MGMT_STATUS_FAILED, /* Transaction Collision */
263 MGMT_STATUS_FAILED, /* Reserved for future use */
264 MGMT_STATUS_INVALID_PARAMS, /* Unacceptable Parameter */
265 MGMT_STATUS_REJECTED, /* QoS Rejected */
266 MGMT_STATUS_NOT_SUPPORTED, /* Classification Not Supported */
267 MGMT_STATUS_REJECTED, /* Insufficient Security */
268 MGMT_STATUS_INVALID_PARAMS, /* Parameter Out Of Range */
269 MGMT_STATUS_FAILED, /* Reserved for future use */
270 MGMT_STATUS_BUSY, /* Role Switch Pending */
271 MGMT_STATUS_FAILED, /* Reserved for future use */
272 MGMT_STATUS_FAILED, /* Slot Violation */
273 MGMT_STATUS_FAILED, /* Role Switch Failed */
274 MGMT_STATUS_INVALID_PARAMS, /* EIR Too Large */
275 MGMT_STATUS_NOT_SUPPORTED, /* Simple Pairing Not Supported */
276 MGMT_STATUS_BUSY, /* Host Busy Pairing */
277 MGMT_STATUS_REJECTED, /* Rejected, No Suitable Channel */
278 MGMT_STATUS_BUSY, /* Controller Busy */
279 MGMT_STATUS_INVALID_PARAMS, /* Unsuitable Connection Interval */
280 MGMT_STATUS_TIMEOUT, /* Directed Advertising Timeout */
281 MGMT_STATUS_AUTH_FAILED, /* Terminated Due to MIC Failure */
282 MGMT_STATUS_CONNECT_FAILED, /* Connection Establishment Failed */
283 MGMT_STATUS_CONNECT_FAILED, /* MAC Connection Failed */
286 static u8 mgmt_errno_status(int err)
290 return MGMT_STATUS_SUCCESS;
292 return MGMT_STATUS_REJECTED;
294 return MGMT_STATUS_INVALID_PARAMS;
296 return MGMT_STATUS_NOT_SUPPORTED;
298 return MGMT_STATUS_BUSY;
300 return MGMT_STATUS_AUTH_FAILED;
302 return MGMT_STATUS_NO_RESOURCES;
304 return MGMT_STATUS_ALREADY_CONNECTED;
306 return MGMT_STATUS_DISCONNECTED;
309 return MGMT_STATUS_FAILED;
312 static u8 mgmt_status(int err)
315 return mgmt_errno_status(err);
317 if (err < ARRAY_SIZE(mgmt_status_table))
318 return mgmt_status_table[err];
320 return MGMT_STATUS_FAILED;
323 static int mgmt_index_event(u16 event, struct hci_dev *hdev, void *data,
326 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
330 static int mgmt_limited_event(u16 event, struct hci_dev *hdev, void *data,
331 u16 len, int flag, struct sock *skip_sk)
333 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
337 static int mgmt_event(u16 event, struct hci_dev *hdev, void *data, u16 len,
338 struct sock *skip_sk)
340 return mgmt_send_event(event, hdev, HCI_CHANNEL_CONTROL, data, len,
341 HCI_SOCK_TRUSTED, skip_sk);
344 static int mgmt_event_skb(struct sk_buff *skb, struct sock *skip_sk)
346 return mgmt_send_event_skb(HCI_CHANNEL_CONTROL, skb, HCI_SOCK_TRUSTED,
350 static u8 le_addr_type(u8 mgmt_addr_type)
352 if (mgmt_addr_type == BDADDR_LE_PUBLIC)
353 return ADDR_LE_DEV_PUBLIC;
355 return ADDR_LE_DEV_RANDOM;
358 void mgmt_fill_version_info(void *ver)
360 struct mgmt_rp_read_version *rp = ver;
362 rp->version = MGMT_VERSION;
363 rp->revision = cpu_to_le16(MGMT_REVISION);
366 static int read_version(struct sock *sk, struct hci_dev *hdev, void *data,
369 struct mgmt_rp_read_version rp;
371 bt_dev_dbg(hdev, "sock %p", sk);
373 mgmt_fill_version_info(&rp);
375 return mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_VERSION, 0,
379 static int read_commands(struct sock *sk, struct hci_dev *hdev, void *data,
382 struct mgmt_rp_read_commands *rp;
383 u16 num_commands, num_events;
387 bt_dev_dbg(hdev, "sock %p", sk);
389 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
390 num_commands = ARRAY_SIZE(mgmt_commands);
391 num_events = ARRAY_SIZE(mgmt_events);
393 num_commands = ARRAY_SIZE(mgmt_untrusted_commands);
394 num_events = ARRAY_SIZE(mgmt_untrusted_events);
397 rp_size = sizeof(*rp) + ((num_commands + num_events) * sizeof(u16));
399 rp = kmalloc(rp_size, GFP_KERNEL);
403 rp->num_commands = cpu_to_le16(num_commands);
404 rp->num_events = cpu_to_le16(num_events);
406 if (hci_sock_test_flag(sk, HCI_SOCK_TRUSTED)) {
407 __le16 *opcode = rp->opcodes;
409 for (i = 0; i < num_commands; i++, opcode++)
410 put_unaligned_le16(mgmt_commands[i], opcode);
412 for (i = 0; i < num_events; i++, opcode++)
413 put_unaligned_le16(mgmt_events[i], opcode);
415 __le16 *opcode = rp->opcodes;
417 for (i = 0; i < num_commands; i++, opcode++)
418 put_unaligned_le16(mgmt_untrusted_commands[i], opcode);
420 for (i = 0; i < num_events; i++, opcode++)
421 put_unaligned_le16(mgmt_untrusted_events[i], opcode);
424 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_COMMANDS, 0,
431 static int read_index_list(struct sock *sk, struct hci_dev *hdev, void *data,
434 struct mgmt_rp_read_index_list *rp;
440 bt_dev_dbg(hdev, "sock %p", sk);
442 read_lock(&hci_dev_list_lock);
445 list_for_each_entry(d, &hci_dev_list, list) {
446 if (d->dev_type == HCI_PRIMARY &&
447 !hci_dev_test_flag(d, HCI_UNCONFIGURED))
451 rp_len = sizeof(*rp) + (2 * count);
452 rp = kmalloc(rp_len, GFP_ATOMIC);
454 read_unlock(&hci_dev_list_lock);
459 list_for_each_entry(d, &hci_dev_list, list) {
460 if (hci_dev_test_flag(d, HCI_SETUP) ||
461 hci_dev_test_flag(d, HCI_CONFIG) ||
462 hci_dev_test_flag(d, HCI_USER_CHANNEL))
465 /* Devices marked as raw-only are neither configured
466 * nor unconfigured controllers.
468 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
471 if (d->dev_type == HCI_PRIMARY &&
472 !hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
473 rp->index[count++] = cpu_to_le16(d->id);
474 bt_dev_dbg(hdev, "Added hci%u", d->id);
478 rp->num_controllers = cpu_to_le16(count);
479 rp_len = sizeof(*rp) + (2 * count);
481 read_unlock(&hci_dev_list_lock);
483 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE, MGMT_OP_READ_INDEX_LIST,
491 static int read_unconf_index_list(struct sock *sk, struct hci_dev *hdev,
492 void *data, u16 data_len)
494 struct mgmt_rp_read_unconf_index_list *rp;
500 bt_dev_dbg(hdev, "sock %p", sk);
502 read_lock(&hci_dev_list_lock);
505 list_for_each_entry(d, &hci_dev_list, list) {
506 if (d->dev_type == HCI_PRIMARY &&
507 hci_dev_test_flag(d, HCI_UNCONFIGURED))
511 rp_len = sizeof(*rp) + (2 * count);
512 rp = kmalloc(rp_len, GFP_ATOMIC);
514 read_unlock(&hci_dev_list_lock);
519 list_for_each_entry(d, &hci_dev_list, list) {
520 if (hci_dev_test_flag(d, HCI_SETUP) ||
521 hci_dev_test_flag(d, HCI_CONFIG) ||
522 hci_dev_test_flag(d, HCI_USER_CHANNEL))
525 /* Devices marked as raw-only are neither configured
526 * nor unconfigured controllers.
528 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
531 if (d->dev_type == HCI_PRIMARY &&
532 hci_dev_test_flag(d, HCI_UNCONFIGURED)) {
533 rp->index[count++] = cpu_to_le16(d->id);
534 bt_dev_dbg(hdev, "Added hci%u", d->id);
538 rp->num_controllers = cpu_to_le16(count);
539 rp_len = sizeof(*rp) + (2 * count);
541 read_unlock(&hci_dev_list_lock);
543 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
544 MGMT_OP_READ_UNCONF_INDEX_LIST, 0, rp, rp_len);
551 static int read_ext_index_list(struct sock *sk, struct hci_dev *hdev,
552 void *data, u16 data_len)
554 struct mgmt_rp_read_ext_index_list *rp;
559 bt_dev_dbg(hdev, "sock %p", sk);
561 read_lock(&hci_dev_list_lock);
564 list_for_each_entry(d, &hci_dev_list, list) {
565 if (d->dev_type == HCI_PRIMARY || d->dev_type == HCI_AMP)
569 rp = kmalloc(struct_size(rp, entry, count), GFP_ATOMIC);
571 read_unlock(&hci_dev_list_lock);
576 list_for_each_entry(d, &hci_dev_list, list) {
577 if (hci_dev_test_flag(d, HCI_SETUP) ||
578 hci_dev_test_flag(d, HCI_CONFIG) ||
579 hci_dev_test_flag(d, HCI_USER_CHANNEL))
582 /* Devices marked as raw-only are neither configured
583 * nor unconfigured controllers.
585 if (test_bit(HCI_QUIRK_RAW_DEVICE, &d->quirks))
588 if (d->dev_type == HCI_PRIMARY) {
589 if (hci_dev_test_flag(d, HCI_UNCONFIGURED))
590 rp->entry[count].type = 0x01;
592 rp->entry[count].type = 0x00;
593 } else if (d->dev_type == HCI_AMP) {
594 rp->entry[count].type = 0x02;
599 rp->entry[count].bus = d->bus;
600 rp->entry[count++].index = cpu_to_le16(d->id);
601 bt_dev_dbg(hdev, "Added hci%u", d->id);
604 rp->num_controllers = cpu_to_le16(count);
606 read_unlock(&hci_dev_list_lock);
608 /* If this command is called at least once, then all the
609 * default index and unconfigured index events are disabled
610 * and from now on only extended index events are used.
612 hci_sock_set_flag(sk, HCI_MGMT_EXT_INDEX_EVENTS);
613 hci_sock_clear_flag(sk, HCI_MGMT_INDEX_EVENTS);
614 hci_sock_clear_flag(sk, HCI_MGMT_UNCONF_INDEX_EVENTS);
616 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
617 MGMT_OP_READ_EXT_INDEX_LIST, 0, rp,
618 struct_size(rp, entry, count));
625 static bool is_configured(struct hci_dev *hdev)
627 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
628 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
631 if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
632 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) &&
633 !bacmp(&hdev->public_addr, BDADDR_ANY))
639 static __le32 get_missing_options(struct hci_dev *hdev)
643 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) &&
644 !hci_dev_test_flag(hdev, HCI_EXT_CONFIGURED))
645 options |= MGMT_OPTION_EXTERNAL_CONFIG;
647 if ((test_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks) ||
648 test_bit(HCI_QUIRK_USE_BDADDR_PROPERTY, &hdev->quirks)) &&
649 !bacmp(&hdev->public_addr, BDADDR_ANY))
650 options |= MGMT_OPTION_PUBLIC_ADDRESS;
652 return cpu_to_le32(options);
655 static int new_options(struct hci_dev *hdev, struct sock *skip)
657 __le32 options = get_missing_options(hdev);
659 return mgmt_limited_event(MGMT_EV_NEW_CONFIG_OPTIONS, hdev, &options,
660 sizeof(options), HCI_MGMT_OPTION_EVENTS, skip);
663 static int send_options_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
665 __le32 options = get_missing_options(hdev);
667 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &options,
671 static int read_config_info(struct sock *sk, struct hci_dev *hdev,
672 void *data, u16 data_len)
674 struct mgmt_rp_read_config_info rp;
677 bt_dev_dbg(hdev, "sock %p", sk);
681 memset(&rp, 0, sizeof(rp));
682 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
684 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
685 options |= MGMT_OPTION_EXTERNAL_CONFIG;
687 if (hdev->set_bdaddr)
688 options |= MGMT_OPTION_PUBLIC_ADDRESS;
690 rp.supported_options = cpu_to_le32(options);
691 rp.missing_options = get_missing_options(hdev);
693 hci_dev_unlock(hdev);
695 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONFIG_INFO, 0,
699 static u32 get_supported_phys(struct hci_dev *hdev)
701 u32 supported_phys = 0;
703 if (lmp_bredr_capable(hdev)) {
704 supported_phys |= MGMT_PHY_BR_1M_1SLOT;
706 if (hdev->features[0][0] & LMP_3SLOT)
707 supported_phys |= MGMT_PHY_BR_1M_3SLOT;
709 if (hdev->features[0][0] & LMP_5SLOT)
710 supported_phys |= MGMT_PHY_BR_1M_5SLOT;
712 if (lmp_edr_2m_capable(hdev)) {
713 supported_phys |= MGMT_PHY_EDR_2M_1SLOT;
715 if (lmp_edr_3slot_capable(hdev))
716 supported_phys |= MGMT_PHY_EDR_2M_3SLOT;
718 if (lmp_edr_5slot_capable(hdev))
719 supported_phys |= MGMT_PHY_EDR_2M_5SLOT;
721 if (lmp_edr_3m_capable(hdev)) {
722 supported_phys |= MGMT_PHY_EDR_3M_1SLOT;
724 if (lmp_edr_3slot_capable(hdev))
725 supported_phys |= MGMT_PHY_EDR_3M_3SLOT;
727 if (lmp_edr_5slot_capable(hdev))
728 supported_phys |= MGMT_PHY_EDR_3M_5SLOT;
733 if (lmp_le_capable(hdev)) {
734 supported_phys |= MGMT_PHY_LE_1M_TX;
735 supported_phys |= MGMT_PHY_LE_1M_RX;
737 if (hdev->le_features[1] & HCI_LE_PHY_2M) {
738 supported_phys |= MGMT_PHY_LE_2M_TX;
739 supported_phys |= MGMT_PHY_LE_2M_RX;
742 if (hdev->le_features[1] & HCI_LE_PHY_CODED) {
743 supported_phys |= MGMT_PHY_LE_CODED_TX;
744 supported_phys |= MGMT_PHY_LE_CODED_RX;
748 return supported_phys;
751 static u32 get_selected_phys(struct hci_dev *hdev)
753 u32 selected_phys = 0;
755 if (lmp_bredr_capable(hdev)) {
756 selected_phys |= MGMT_PHY_BR_1M_1SLOT;
758 if (hdev->pkt_type & (HCI_DM3 | HCI_DH3))
759 selected_phys |= MGMT_PHY_BR_1M_3SLOT;
761 if (hdev->pkt_type & (HCI_DM5 | HCI_DH5))
762 selected_phys |= MGMT_PHY_BR_1M_5SLOT;
764 if (lmp_edr_2m_capable(hdev)) {
765 if (!(hdev->pkt_type & HCI_2DH1))
766 selected_phys |= MGMT_PHY_EDR_2M_1SLOT;
768 if (lmp_edr_3slot_capable(hdev) &&
769 !(hdev->pkt_type & HCI_2DH3))
770 selected_phys |= MGMT_PHY_EDR_2M_3SLOT;
772 if (lmp_edr_5slot_capable(hdev) &&
773 !(hdev->pkt_type & HCI_2DH5))
774 selected_phys |= MGMT_PHY_EDR_2M_5SLOT;
776 if (lmp_edr_3m_capable(hdev)) {
777 if (!(hdev->pkt_type & HCI_3DH1))
778 selected_phys |= MGMT_PHY_EDR_3M_1SLOT;
780 if (lmp_edr_3slot_capable(hdev) &&
781 !(hdev->pkt_type & HCI_3DH3))
782 selected_phys |= MGMT_PHY_EDR_3M_3SLOT;
784 if (lmp_edr_5slot_capable(hdev) &&
785 !(hdev->pkt_type & HCI_3DH5))
786 selected_phys |= MGMT_PHY_EDR_3M_5SLOT;
791 if (lmp_le_capable(hdev)) {
792 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_1M)
793 selected_phys |= MGMT_PHY_LE_1M_TX;
795 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_1M)
796 selected_phys |= MGMT_PHY_LE_1M_RX;
798 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_2M)
799 selected_phys |= MGMT_PHY_LE_2M_TX;
801 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_2M)
802 selected_phys |= MGMT_PHY_LE_2M_RX;
804 if (hdev->le_tx_def_phys & HCI_LE_SET_PHY_CODED)
805 selected_phys |= MGMT_PHY_LE_CODED_TX;
807 if (hdev->le_rx_def_phys & HCI_LE_SET_PHY_CODED)
808 selected_phys |= MGMT_PHY_LE_CODED_RX;
811 return selected_phys;
814 static u32 get_configurable_phys(struct hci_dev *hdev)
816 return (get_supported_phys(hdev) & ~MGMT_PHY_BR_1M_1SLOT &
817 ~MGMT_PHY_LE_1M_TX & ~MGMT_PHY_LE_1M_RX);
820 static u32 get_supported_settings(struct hci_dev *hdev)
824 settings |= MGMT_SETTING_POWERED;
825 settings |= MGMT_SETTING_BONDABLE;
826 settings |= MGMT_SETTING_DEBUG_KEYS;
827 settings |= MGMT_SETTING_CONNECTABLE;
828 settings |= MGMT_SETTING_DISCOVERABLE;
830 if (lmp_bredr_capable(hdev)) {
831 if (hdev->hci_ver >= BLUETOOTH_VER_1_2)
832 settings |= MGMT_SETTING_FAST_CONNECTABLE;
833 settings |= MGMT_SETTING_BREDR;
834 settings |= MGMT_SETTING_LINK_SECURITY;
836 if (lmp_ssp_capable(hdev)) {
837 settings |= MGMT_SETTING_SSP;
838 if (IS_ENABLED(CONFIG_BT_HS))
839 settings |= MGMT_SETTING_HS;
842 if (lmp_sc_capable(hdev))
843 settings |= MGMT_SETTING_SECURE_CONN;
845 if (test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED,
847 settings |= MGMT_SETTING_WIDEBAND_SPEECH;
850 if (lmp_le_capable(hdev)) {
851 settings |= MGMT_SETTING_LE;
852 settings |= MGMT_SETTING_SECURE_CONN;
853 settings |= MGMT_SETTING_PRIVACY;
854 settings |= MGMT_SETTING_STATIC_ADDRESS;
855 settings |= MGMT_SETTING_ADVERTISING;
858 if (test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks) ||
860 settings |= MGMT_SETTING_CONFIGURATION;
862 settings |= MGMT_SETTING_PHY_CONFIGURATION;
867 static u32 get_current_settings(struct hci_dev *hdev)
871 if (hdev_is_powered(hdev))
872 settings |= MGMT_SETTING_POWERED;
874 if (hci_dev_test_flag(hdev, HCI_CONNECTABLE))
875 settings |= MGMT_SETTING_CONNECTABLE;
877 if (hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE))
878 settings |= MGMT_SETTING_FAST_CONNECTABLE;
880 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
881 settings |= MGMT_SETTING_DISCOVERABLE;
883 if (hci_dev_test_flag(hdev, HCI_BONDABLE))
884 settings |= MGMT_SETTING_BONDABLE;
886 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
887 settings |= MGMT_SETTING_BREDR;
889 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
890 settings |= MGMT_SETTING_LE;
892 if (hci_dev_test_flag(hdev, HCI_LINK_SECURITY))
893 settings |= MGMT_SETTING_LINK_SECURITY;
895 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
896 settings |= MGMT_SETTING_SSP;
898 if (hci_dev_test_flag(hdev, HCI_HS_ENABLED))
899 settings |= MGMT_SETTING_HS;
901 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
902 settings |= MGMT_SETTING_ADVERTISING;
904 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED))
905 settings |= MGMT_SETTING_SECURE_CONN;
907 if (hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS))
908 settings |= MGMT_SETTING_DEBUG_KEYS;
910 if (hci_dev_test_flag(hdev, HCI_PRIVACY))
911 settings |= MGMT_SETTING_PRIVACY;
913 /* The current setting for static address has two purposes. The
914 * first is to indicate if the static address will be used and
915 * the second is to indicate if it is actually set.
917 * This means if the static address is not configured, this flag
918 * will never be set. If the address is configured, then if the
919 * address is actually used decides if the flag is set or not.
921 * For single mode LE only controllers and dual-mode controllers
922 * with BR/EDR disabled, the existence of the static address will
925 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
926 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
927 !bacmp(&hdev->bdaddr, BDADDR_ANY)) {
928 if (bacmp(&hdev->static_addr, BDADDR_ANY))
929 settings |= MGMT_SETTING_STATIC_ADDRESS;
932 if (hci_dev_test_flag(hdev, HCI_WIDEBAND_SPEECH_ENABLED))
933 settings |= MGMT_SETTING_WIDEBAND_SPEECH;
938 static struct mgmt_pending_cmd *pending_find(u16 opcode, struct hci_dev *hdev)
940 return mgmt_pending_find(HCI_CHANNEL_CONTROL, opcode, hdev);
943 u8 mgmt_get_adv_discov_flags(struct hci_dev *hdev)
945 struct mgmt_pending_cmd *cmd;
947 /* If there's a pending mgmt command the flags will not yet have
948 * their final values, so check for this first.
950 cmd = pending_find(MGMT_OP_SET_DISCOVERABLE, hdev);
952 struct mgmt_mode *cp = cmd->param;
954 return LE_AD_GENERAL;
955 else if (cp->val == 0x02)
956 return LE_AD_LIMITED;
958 if (hci_dev_test_flag(hdev, HCI_LIMITED_DISCOVERABLE))
959 return LE_AD_LIMITED;
960 else if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
961 return LE_AD_GENERAL;
967 bool mgmt_get_connectable(struct hci_dev *hdev)
969 struct mgmt_pending_cmd *cmd;
971 /* If there's a pending mgmt command the flag will not yet have
972 * it's final value, so check for this first.
974 cmd = pending_find(MGMT_OP_SET_CONNECTABLE, hdev);
976 struct mgmt_mode *cp = cmd->param;
981 return hci_dev_test_flag(hdev, HCI_CONNECTABLE);
984 static int service_cache_sync(struct hci_dev *hdev, void *data)
986 hci_update_eir_sync(hdev);
987 hci_update_class_sync(hdev);
992 static void service_cache_off(struct work_struct *work)
994 struct hci_dev *hdev = container_of(work, struct hci_dev,
997 if (!hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE))
1000 hci_cmd_sync_queue(hdev, service_cache_sync, NULL, NULL);
1003 static int rpa_expired_sync(struct hci_dev *hdev, void *data)
1005 /* The generation of a new RPA and programming it into the
1006 * controller happens in the hci_req_enable_advertising()
1009 if (ext_adv_capable(hdev))
1010 return hci_start_ext_adv_sync(hdev, hdev->cur_adv_instance);
1012 return hci_enable_advertising_sync(hdev);
1015 static void rpa_expired(struct work_struct *work)
1017 struct hci_dev *hdev = container_of(work, struct hci_dev,
1020 bt_dev_dbg(hdev, "");
1022 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
1024 if (!hci_dev_test_flag(hdev, HCI_ADVERTISING))
1027 hci_cmd_sync_queue(hdev, rpa_expired_sync, NULL, NULL);
1030 static void discov_off(struct work_struct *work)
1032 struct hci_dev *hdev = container_of(work, struct hci_dev,
1035 bt_dev_dbg(hdev, "");
1039 /* When discoverable timeout triggers, then just make sure
1040 * the limited discoverable flag is cleared. Even in the case
1041 * of a timeout triggered from general discoverable, it is
1042 * safe to unconditionally clear the flag.
1044 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1045 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1046 hdev->discov_timeout = 0;
1048 hci_update_discoverable(hdev);
1050 mgmt_new_settings(hdev);
1052 hci_dev_unlock(hdev);
1055 static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev);
1057 static void mesh_send_complete(struct hci_dev *hdev,
1058 struct mgmt_mesh_tx *mesh_tx, bool silent)
1060 u8 handle = mesh_tx->handle;
1063 mgmt_event(MGMT_EV_MESH_PACKET_CMPLT, hdev, &handle,
1064 sizeof(handle), NULL);
1066 mgmt_mesh_remove(mesh_tx);
1069 static int mesh_send_done_sync(struct hci_dev *hdev, void *data)
1071 struct mgmt_mesh_tx *mesh_tx;
1073 hci_dev_clear_flag(hdev, HCI_MESH_SENDING);
1074 hci_disable_advertising_sync(hdev);
1075 mesh_tx = mgmt_mesh_next(hdev, NULL);
1078 mesh_send_complete(hdev, mesh_tx, false);
1083 static int mesh_send_sync(struct hci_dev *hdev, void *data);
1084 static void mesh_send_start_complete(struct hci_dev *hdev, void *data, int err);
1085 static void mesh_next(struct hci_dev *hdev, void *data, int err)
1087 struct mgmt_mesh_tx *mesh_tx = mgmt_mesh_next(hdev, NULL);
1092 err = hci_cmd_sync_queue(hdev, mesh_send_sync, mesh_tx,
1093 mesh_send_start_complete);
1096 mesh_send_complete(hdev, mesh_tx, false);
1098 hci_dev_set_flag(hdev, HCI_MESH_SENDING);
1101 static void mesh_send_done(struct work_struct *work)
1103 struct hci_dev *hdev = container_of(work, struct hci_dev,
1104 mesh_send_done.work);
1106 if (!hci_dev_test_flag(hdev, HCI_MESH_SENDING))
1109 hci_cmd_sync_queue(hdev, mesh_send_done_sync, NULL, mesh_next);
1112 static void mgmt_init_hdev(struct sock *sk, struct hci_dev *hdev)
1114 if (hci_dev_test_flag(hdev, HCI_MGMT))
1117 BT_INFO("MGMT ver %d.%d", MGMT_VERSION, MGMT_REVISION);
1119 INIT_DELAYED_WORK(&hdev->discov_off, discov_off);
1120 INIT_DELAYED_WORK(&hdev->service_cache, service_cache_off);
1121 INIT_DELAYED_WORK(&hdev->rpa_expired, rpa_expired);
1122 INIT_DELAYED_WORK(&hdev->mesh_send_done, mesh_send_done);
1124 /* Non-mgmt controlled devices get this bit set
1125 * implicitly so that pairing works for them, however
1126 * for mgmt we require user-space to explicitly enable
1129 hci_dev_clear_flag(hdev, HCI_BONDABLE);
1131 hci_dev_set_flag(hdev, HCI_MGMT);
1134 static int read_controller_info(struct sock *sk, struct hci_dev *hdev,
1135 void *data, u16 data_len)
1137 struct mgmt_rp_read_info rp;
1139 bt_dev_dbg(hdev, "sock %p", sk);
1143 memset(&rp, 0, sizeof(rp));
1145 bacpy(&rp.bdaddr, &hdev->bdaddr);
1147 rp.version = hdev->hci_ver;
1148 rp.manufacturer = cpu_to_le16(hdev->manufacturer);
1150 rp.supported_settings = cpu_to_le32(get_supported_settings(hdev));
1151 rp.current_settings = cpu_to_le32(get_current_settings(hdev));
1153 memcpy(rp.dev_class, hdev->dev_class, 3);
1155 memcpy(rp.name, hdev->dev_name, sizeof(hdev->dev_name));
1156 memcpy(rp.short_name, hdev->short_name, sizeof(hdev->short_name));
1158 hci_dev_unlock(hdev);
1160 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_INFO, 0, &rp,
1164 static u16 append_eir_data_to_buf(struct hci_dev *hdev, u8 *eir)
1169 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1170 eir_len = eir_append_data(eir, eir_len, EIR_CLASS_OF_DEV,
1171 hdev->dev_class, 3);
1173 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1174 eir_len = eir_append_le16(eir, eir_len, EIR_APPEARANCE,
1177 name_len = strnlen(hdev->dev_name, sizeof(hdev->dev_name));
1178 eir_len = eir_append_data(eir, eir_len, EIR_NAME_COMPLETE,
1179 hdev->dev_name, name_len);
1181 name_len = strnlen(hdev->short_name, sizeof(hdev->short_name));
1182 eir_len = eir_append_data(eir, eir_len, EIR_NAME_SHORT,
1183 hdev->short_name, name_len);
1188 static int read_ext_controller_info(struct sock *sk, struct hci_dev *hdev,
1189 void *data, u16 data_len)
1192 struct mgmt_rp_read_ext_info *rp = (void *)buf;
1195 bt_dev_dbg(hdev, "sock %p", sk);
1197 memset(&buf, 0, sizeof(buf));
1201 bacpy(&rp->bdaddr, &hdev->bdaddr);
1203 rp->version = hdev->hci_ver;
1204 rp->manufacturer = cpu_to_le16(hdev->manufacturer);
1206 rp->supported_settings = cpu_to_le32(get_supported_settings(hdev));
1207 rp->current_settings = cpu_to_le32(get_current_settings(hdev));
1210 eir_len = append_eir_data_to_buf(hdev, rp->eir);
1211 rp->eir_len = cpu_to_le16(eir_len);
1213 hci_dev_unlock(hdev);
1215 /* If this command is called at least once, then the events
1216 * for class of device and local name changes are disabled
1217 * and only the new extended controller information event
1220 hci_sock_set_flag(sk, HCI_MGMT_EXT_INFO_EVENTS);
1221 hci_sock_clear_flag(sk, HCI_MGMT_DEV_CLASS_EVENTS);
1222 hci_sock_clear_flag(sk, HCI_MGMT_LOCAL_NAME_EVENTS);
1224 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_EXT_INFO, 0, rp,
1225 sizeof(*rp) + eir_len);
1228 static int ext_info_changed(struct hci_dev *hdev, struct sock *skip)
1231 struct mgmt_ev_ext_info_changed *ev = (void *)buf;
1234 memset(buf, 0, sizeof(buf));
1236 eir_len = append_eir_data_to_buf(hdev, ev->eir);
1237 ev->eir_len = cpu_to_le16(eir_len);
1239 return mgmt_limited_event(MGMT_EV_EXT_INFO_CHANGED, hdev, ev,
1240 sizeof(*ev) + eir_len,
1241 HCI_MGMT_EXT_INFO_EVENTS, skip);
1244 static int send_settings_rsp(struct sock *sk, u16 opcode, struct hci_dev *hdev)
1246 __le32 settings = cpu_to_le32(get_current_settings(hdev));
1248 return mgmt_cmd_complete(sk, hdev->id, opcode, 0, &settings,
1252 void mgmt_advertising_added(struct sock *sk, struct hci_dev *hdev, u8 instance)
1254 struct mgmt_ev_advertising_added ev;
1256 ev.instance = instance;
1258 mgmt_event(MGMT_EV_ADVERTISING_ADDED, hdev, &ev, sizeof(ev), sk);
1261 void mgmt_advertising_removed(struct sock *sk, struct hci_dev *hdev,
1264 struct mgmt_ev_advertising_removed ev;
1266 ev.instance = instance;
1268 mgmt_event(MGMT_EV_ADVERTISING_REMOVED, hdev, &ev, sizeof(ev), sk);
1271 static void cancel_adv_timeout(struct hci_dev *hdev)
1273 if (hdev->adv_instance_timeout) {
1274 hdev->adv_instance_timeout = 0;
1275 cancel_delayed_work(&hdev->adv_instance_expire);
1279 /* This function requires the caller holds hdev->lock */
1280 static void restart_le_actions(struct hci_dev *hdev)
1282 struct hci_conn_params *p;
1284 list_for_each_entry(p, &hdev->le_conn_params, list) {
1285 /* Needed for AUTO_OFF case where might not "really"
1286 * have been powered off.
1288 list_del_init(&p->action);
1290 switch (p->auto_connect) {
1291 case HCI_AUTO_CONN_DIRECT:
1292 case HCI_AUTO_CONN_ALWAYS:
1293 list_add(&p->action, &hdev->pend_le_conns);
1295 case HCI_AUTO_CONN_REPORT:
1296 list_add(&p->action, &hdev->pend_le_reports);
1304 static int new_settings(struct hci_dev *hdev, struct sock *skip)
1306 __le32 ev = cpu_to_le32(get_current_settings(hdev));
1308 return mgmt_limited_event(MGMT_EV_NEW_SETTINGS, hdev, &ev,
1309 sizeof(ev), HCI_MGMT_SETTING_EVENTS, skip);
1312 static void mgmt_set_powered_complete(struct hci_dev *hdev, void *data, int err)
1314 struct mgmt_pending_cmd *cmd = data;
1315 struct mgmt_mode *cp;
1317 /* Make sure cmd still outstanding. */
1318 if (cmd != pending_find(MGMT_OP_SET_POWERED, hdev))
1323 bt_dev_dbg(hdev, "err %d", err);
1328 restart_le_actions(hdev);
1329 hci_update_passive_scan(hdev);
1330 hci_dev_unlock(hdev);
1333 send_settings_rsp(cmd->sk, cmd->opcode, hdev);
1335 /* Only call new_setting for power on as power off is deferred
1336 * to hdev->power_off work which does call hci_dev_do_close.
1339 new_settings(hdev, cmd->sk);
1341 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED,
1345 mgmt_pending_remove(cmd);
1348 static int set_powered_sync(struct hci_dev *hdev, void *data)
1350 struct mgmt_pending_cmd *cmd = data;
1351 struct mgmt_mode *cp = cmd->param;
1353 BT_DBG("%s", hdev->name);
1355 return hci_set_powered_sync(hdev, cp->val);
1358 static int set_powered(struct sock *sk, struct hci_dev *hdev, void *data,
1361 struct mgmt_mode *cp = data;
1362 struct mgmt_pending_cmd *cmd;
1365 bt_dev_dbg(hdev, "sock %p", sk);
1367 if (cp->val != 0x00 && cp->val != 0x01)
1368 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1369 MGMT_STATUS_INVALID_PARAMS);
1373 if (pending_find(MGMT_OP_SET_POWERED, hdev)) {
1374 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_POWERED,
1379 if (!!cp->val == hdev_is_powered(hdev)) {
1380 err = send_settings_rsp(sk, MGMT_OP_SET_POWERED, hdev);
1384 cmd = mgmt_pending_add(sk, MGMT_OP_SET_POWERED, hdev, data, len);
1390 err = hci_cmd_sync_queue(hdev, set_powered_sync, cmd,
1391 mgmt_set_powered_complete);
1394 mgmt_pending_remove(cmd);
1397 hci_dev_unlock(hdev);
1401 int mgmt_new_settings(struct hci_dev *hdev)
1403 return new_settings(hdev, NULL);
1408 struct hci_dev *hdev;
1412 static void settings_rsp(struct mgmt_pending_cmd *cmd, void *data)
1414 struct cmd_lookup *match = data;
1416 send_settings_rsp(cmd->sk, cmd->opcode, match->hdev);
1418 list_del(&cmd->list);
1420 if (match->sk == NULL) {
1421 match->sk = cmd->sk;
1422 sock_hold(match->sk);
1425 mgmt_pending_free(cmd);
1428 static void cmd_status_rsp(struct mgmt_pending_cmd *cmd, void *data)
1432 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, *status);
1433 mgmt_pending_remove(cmd);
1436 static void cmd_complete_rsp(struct mgmt_pending_cmd *cmd, void *data)
1438 if (cmd->cmd_complete) {
1441 cmd->cmd_complete(cmd, *status);
1442 mgmt_pending_remove(cmd);
1447 cmd_status_rsp(cmd, data);
1450 static int generic_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
1452 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1453 cmd->param, cmd->param_len);
1456 static int addr_cmd_complete(struct mgmt_pending_cmd *cmd, u8 status)
1458 return mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status,
1459 cmd->param, sizeof(struct mgmt_addr_info));
1462 static u8 mgmt_bredr_support(struct hci_dev *hdev)
1464 if (!lmp_bredr_capable(hdev))
1465 return MGMT_STATUS_NOT_SUPPORTED;
1466 else if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1467 return MGMT_STATUS_REJECTED;
1469 return MGMT_STATUS_SUCCESS;
1472 static u8 mgmt_le_support(struct hci_dev *hdev)
1474 if (!lmp_le_capable(hdev))
1475 return MGMT_STATUS_NOT_SUPPORTED;
1476 else if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
1477 return MGMT_STATUS_REJECTED;
1479 return MGMT_STATUS_SUCCESS;
1482 static void mgmt_set_discoverable_complete(struct hci_dev *hdev, void *data,
1485 struct mgmt_pending_cmd *cmd = data;
1487 bt_dev_dbg(hdev, "err %d", err);
1489 /* Make sure cmd still outstanding. */
1490 if (cmd != pending_find(MGMT_OP_SET_DISCOVERABLE, hdev))
1496 u8 mgmt_err = mgmt_status(err);
1497 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
1498 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1502 if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1503 hdev->discov_timeout > 0) {
1504 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1505 queue_delayed_work(hdev->req_workqueue, &hdev->discov_off, to);
1508 send_settings_rsp(cmd->sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1509 new_settings(hdev, cmd->sk);
1512 mgmt_pending_remove(cmd);
1513 hci_dev_unlock(hdev);
1516 static int set_discoverable_sync(struct hci_dev *hdev, void *data)
1518 BT_DBG("%s", hdev->name);
1520 return hci_update_discoverable_sync(hdev);
1523 static int set_discoverable(struct sock *sk, struct hci_dev *hdev, void *data,
1526 struct mgmt_cp_set_discoverable *cp = data;
1527 struct mgmt_pending_cmd *cmd;
1531 bt_dev_dbg(hdev, "sock %p", sk);
1533 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1534 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1535 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1536 MGMT_STATUS_REJECTED);
1538 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
1539 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1540 MGMT_STATUS_INVALID_PARAMS);
1542 timeout = __le16_to_cpu(cp->timeout);
1544 /* Disabling discoverable requires that no timeout is set,
1545 * and enabling limited discoverable requires a timeout.
1547 if ((cp->val == 0x00 && timeout > 0) ||
1548 (cp->val == 0x02 && timeout == 0))
1549 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1550 MGMT_STATUS_INVALID_PARAMS);
1554 if (!hdev_is_powered(hdev) && timeout > 0) {
1555 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1556 MGMT_STATUS_NOT_POWERED);
1560 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1561 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
1562 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1567 if (!hci_dev_test_flag(hdev, HCI_CONNECTABLE)) {
1568 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1569 MGMT_STATUS_REJECTED);
1573 if (hdev->advertising_paused) {
1574 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DISCOVERABLE,
1579 if (!hdev_is_powered(hdev)) {
1580 bool changed = false;
1582 /* Setting limited discoverable when powered off is
1583 * not a valid operation since it requires a timeout
1584 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1586 if (!!cp->val != hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) {
1587 hci_dev_change_flag(hdev, HCI_DISCOVERABLE);
1591 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1596 err = new_settings(hdev, sk);
1601 /* If the current mode is the same, then just update the timeout
1602 * value with the new value. And if only the timeout gets updated,
1603 * then no need for any HCI transactions.
1605 if (!!cp->val == hci_dev_test_flag(hdev, HCI_DISCOVERABLE) &&
1606 (cp->val == 0x02) == hci_dev_test_flag(hdev,
1607 HCI_LIMITED_DISCOVERABLE)) {
1608 cancel_delayed_work(&hdev->discov_off);
1609 hdev->discov_timeout = timeout;
1611 if (cp->val && hdev->discov_timeout > 0) {
1612 int to = msecs_to_jiffies(hdev->discov_timeout * 1000);
1613 queue_delayed_work(hdev->req_workqueue,
1614 &hdev->discov_off, to);
1617 err = send_settings_rsp(sk, MGMT_OP_SET_DISCOVERABLE, hdev);
1621 cmd = mgmt_pending_add(sk, MGMT_OP_SET_DISCOVERABLE, hdev, data, len);
1627 /* Cancel any potential discoverable timeout that might be
1628 * still active and store new timeout value. The arming of
1629 * the timeout happens in the complete handler.
1631 cancel_delayed_work(&hdev->discov_off);
1632 hdev->discov_timeout = timeout;
1635 hci_dev_set_flag(hdev, HCI_DISCOVERABLE);
1637 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1639 /* Limited discoverable mode */
1640 if (cp->val == 0x02)
1641 hci_dev_set_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1643 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1645 err = hci_cmd_sync_queue(hdev, set_discoverable_sync, cmd,
1646 mgmt_set_discoverable_complete);
1649 mgmt_pending_remove(cmd);
1652 hci_dev_unlock(hdev);
1656 static void mgmt_set_connectable_complete(struct hci_dev *hdev, void *data,
1659 struct mgmt_pending_cmd *cmd = data;
1661 bt_dev_dbg(hdev, "err %d", err);
1663 /* Make sure cmd still outstanding. */
1664 if (cmd != pending_find(MGMT_OP_SET_CONNECTABLE, hdev))
1670 u8 mgmt_err = mgmt_status(err);
1671 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
1675 send_settings_rsp(cmd->sk, MGMT_OP_SET_CONNECTABLE, hdev);
1676 new_settings(hdev, cmd->sk);
1680 mgmt_pending_remove(cmd);
1682 hci_dev_unlock(hdev);
1685 static int set_connectable_update_settings(struct hci_dev *hdev,
1686 struct sock *sk, u8 val)
1688 bool changed = false;
1691 if (!!val != hci_dev_test_flag(hdev, HCI_CONNECTABLE))
1695 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1697 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1698 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1701 err = send_settings_rsp(sk, MGMT_OP_SET_CONNECTABLE, hdev);
1706 hci_update_scan(hdev);
1707 hci_update_passive_scan(hdev);
1708 return new_settings(hdev, sk);
1714 static int set_connectable_sync(struct hci_dev *hdev, void *data)
1716 BT_DBG("%s", hdev->name);
1718 return hci_update_connectable_sync(hdev);
1721 static int set_connectable(struct sock *sk, struct hci_dev *hdev, void *data,
1724 struct mgmt_mode *cp = data;
1725 struct mgmt_pending_cmd *cmd;
1728 bt_dev_dbg(hdev, "sock %p", sk);
1730 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) &&
1731 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
1732 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1733 MGMT_STATUS_REJECTED);
1735 if (cp->val != 0x00 && cp->val != 0x01)
1736 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1737 MGMT_STATUS_INVALID_PARAMS);
1741 if (!hdev_is_powered(hdev)) {
1742 err = set_connectable_update_settings(hdev, sk, cp->val);
1746 if (pending_find(MGMT_OP_SET_DISCOVERABLE, hdev) ||
1747 pending_find(MGMT_OP_SET_CONNECTABLE, hdev)) {
1748 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_CONNECTABLE,
1753 cmd = mgmt_pending_add(sk, MGMT_OP_SET_CONNECTABLE, hdev, data, len);
1760 hci_dev_set_flag(hdev, HCI_CONNECTABLE);
1762 if (hdev->discov_timeout > 0)
1763 cancel_delayed_work(&hdev->discov_off);
1765 hci_dev_clear_flag(hdev, HCI_LIMITED_DISCOVERABLE);
1766 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
1767 hci_dev_clear_flag(hdev, HCI_CONNECTABLE);
1770 err = hci_cmd_sync_queue(hdev, set_connectable_sync, cmd,
1771 mgmt_set_connectable_complete);
1774 mgmt_pending_remove(cmd);
1777 hci_dev_unlock(hdev);
1781 static int set_bondable(struct sock *sk, struct hci_dev *hdev, void *data,
1784 struct mgmt_mode *cp = data;
1788 bt_dev_dbg(hdev, "sock %p", sk);
1790 if (cp->val != 0x00 && cp->val != 0x01)
1791 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BONDABLE,
1792 MGMT_STATUS_INVALID_PARAMS);
1797 changed = !hci_dev_test_and_set_flag(hdev, HCI_BONDABLE);
1799 changed = hci_dev_test_and_clear_flag(hdev, HCI_BONDABLE);
1801 err = send_settings_rsp(sk, MGMT_OP_SET_BONDABLE, hdev);
1806 /* In limited privacy mode the change of bondable mode
1807 * may affect the local advertising address.
1809 hci_update_discoverable(hdev);
1811 err = new_settings(hdev, sk);
1815 hci_dev_unlock(hdev);
1819 static int set_link_security(struct sock *sk, struct hci_dev *hdev, void *data,
1822 struct mgmt_mode *cp = data;
1823 struct mgmt_pending_cmd *cmd;
1827 bt_dev_dbg(hdev, "sock %p", sk);
1829 status = mgmt_bredr_support(hdev);
1831 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1834 if (cp->val != 0x00 && cp->val != 0x01)
1835 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1836 MGMT_STATUS_INVALID_PARAMS);
1840 if (!hdev_is_powered(hdev)) {
1841 bool changed = false;
1843 if (!!cp->val != hci_dev_test_flag(hdev, HCI_LINK_SECURITY)) {
1844 hci_dev_change_flag(hdev, HCI_LINK_SECURITY);
1848 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1853 err = new_settings(hdev, sk);
1858 if (pending_find(MGMT_OP_SET_LINK_SECURITY, hdev)) {
1859 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LINK_SECURITY,
1866 if (test_bit(HCI_AUTH, &hdev->flags) == val) {
1867 err = send_settings_rsp(sk, MGMT_OP_SET_LINK_SECURITY, hdev);
1871 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LINK_SECURITY, hdev, data, len);
1877 err = hci_send_cmd(hdev, HCI_OP_WRITE_AUTH_ENABLE, sizeof(val), &val);
1879 mgmt_pending_remove(cmd);
1884 hci_dev_unlock(hdev);
1888 static void set_ssp_complete(struct hci_dev *hdev, void *data, int err)
1890 struct cmd_lookup match = { NULL, hdev };
1891 struct mgmt_pending_cmd *cmd = data;
1892 struct mgmt_mode *cp = cmd->param;
1893 u8 enable = cp->val;
1896 /* Make sure cmd still outstanding. */
1897 if (cmd != pending_find(MGMT_OP_SET_SSP, hdev))
1901 u8 mgmt_err = mgmt_status(err);
1903 if (enable && hci_dev_test_and_clear_flag(hdev,
1905 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
1906 new_settings(hdev, NULL);
1909 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, cmd_status_rsp,
1915 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
1917 changed = hci_dev_test_and_clear_flag(hdev, HCI_SSP_ENABLED);
1920 changed = hci_dev_test_and_clear_flag(hdev,
1923 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
1926 mgmt_pending_foreach(MGMT_OP_SET_SSP, hdev, settings_rsp, &match);
1929 new_settings(hdev, match.sk);
1934 hci_update_eir_sync(hdev);
1937 static int set_ssp_sync(struct hci_dev *hdev, void *data)
1939 struct mgmt_pending_cmd *cmd = data;
1940 struct mgmt_mode *cp = cmd->param;
1941 bool changed = false;
1945 changed = !hci_dev_test_and_set_flag(hdev, HCI_SSP_ENABLED);
1947 err = hci_write_ssp_mode_sync(hdev, cp->val);
1949 if (!err && changed)
1950 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
1955 static int set_ssp(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
1957 struct mgmt_mode *cp = data;
1958 struct mgmt_pending_cmd *cmd;
1962 bt_dev_dbg(hdev, "sock %p", sk);
1964 status = mgmt_bredr_support(hdev);
1966 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP, status);
1968 if (!lmp_ssp_capable(hdev))
1969 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1970 MGMT_STATUS_NOT_SUPPORTED);
1972 if (cp->val != 0x00 && cp->val != 0x01)
1973 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
1974 MGMT_STATUS_INVALID_PARAMS);
1978 if (!hdev_is_powered(hdev)) {
1982 changed = !hci_dev_test_and_set_flag(hdev,
1985 changed = hci_dev_test_and_clear_flag(hdev,
1988 changed = hci_dev_test_and_clear_flag(hdev,
1991 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
1994 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
1999 err = new_settings(hdev, sk);
2004 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
2005 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2010 if (!!cp->val == hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
2011 err = send_settings_rsp(sk, MGMT_OP_SET_SSP, hdev);
2015 cmd = mgmt_pending_add(sk, MGMT_OP_SET_SSP, hdev, data, len);
2019 err = hci_cmd_sync_queue(hdev, set_ssp_sync, cmd,
2023 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SSP,
2024 MGMT_STATUS_FAILED);
2027 mgmt_pending_remove(cmd);
2031 hci_dev_unlock(hdev);
2035 static int set_hs(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2037 struct mgmt_mode *cp = data;
2042 bt_dev_dbg(hdev, "sock %p", sk);
2044 if (!IS_ENABLED(CONFIG_BT_HS))
2045 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2046 MGMT_STATUS_NOT_SUPPORTED);
2048 status = mgmt_bredr_support(hdev);
2050 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS, status);
2052 if (!lmp_ssp_capable(hdev))
2053 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2054 MGMT_STATUS_NOT_SUPPORTED);
2056 if (!hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
2057 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2058 MGMT_STATUS_REJECTED);
2060 if (cp->val != 0x00 && cp->val != 0x01)
2061 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2062 MGMT_STATUS_INVALID_PARAMS);
2066 if (pending_find(MGMT_OP_SET_SSP, hdev)) {
2067 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2073 changed = !hci_dev_test_and_set_flag(hdev, HCI_HS_ENABLED);
2075 if (hdev_is_powered(hdev)) {
2076 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_HS,
2077 MGMT_STATUS_REJECTED);
2081 changed = hci_dev_test_and_clear_flag(hdev, HCI_HS_ENABLED);
2084 err = send_settings_rsp(sk, MGMT_OP_SET_HS, hdev);
2089 err = new_settings(hdev, sk);
2092 hci_dev_unlock(hdev);
2096 static void set_le_complete(struct hci_dev *hdev, void *data, int err)
2098 struct cmd_lookup match = { NULL, hdev };
2099 u8 status = mgmt_status(err);
2101 bt_dev_dbg(hdev, "err %d", err);
2104 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, cmd_status_rsp,
2109 mgmt_pending_foreach(MGMT_OP_SET_LE, hdev, settings_rsp, &match);
2111 new_settings(hdev, match.sk);
2117 static int set_le_sync(struct hci_dev *hdev, void *data)
2119 struct mgmt_pending_cmd *cmd = data;
2120 struct mgmt_mode *cp = cmd->param;
2125 hci_clear_adv_instance_sync(hdev, NULL, 0x00, true);
2127 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
2128 hci_disable_advertising_sync(hdev);
2130 if (ext_adv_capable(hdev))
2131 hci_remove_ext_adv_instance_sync(hdev, 0, cmd->sk);
2133 hci_dev_set_flag(hdev, HCI_LE_ENABLED);
2136 err = hci_write_le_host_supported_sync(hdev, val, 0);
2138 /* Make sure the controller has a good default for
2139 * advertising data. Restrict the update to when LE
2140 * has actually been enabled. During power on, the
2141 * update in powered_update_hci will take care of it.
2143 if (!err && hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
2144 if (ext_adv_capable(hdev)) {
2147 status = hci_setup_ext_adv_instance_sync(hdev, 0x00);
2149 hci_update_scan_rsp_data_sync(hdev, 0x00);
2151 hci_update_adv_data_sync(hdev, 0x00);
2152 hci_update_scan_rsp_data_sync(hdev, 0x00);
2155 hci_update_passive_scan(hdev);
2161 static void set_mesh_complete(struct hci_dev *hdev, void *data, int err)
2163 struct mgmt_pending_cmd *cmd = data;
2164 u8 status = mgmt_status(err);
2165 struct sock *sk = cmd->sk;
2168 mgmt_pending_foreach(MGMT_OP_SET_MESH_RECEIVER, hdev,
2169 cmd_status_rsp, &status);
2173 mgmt_pending_remove(cmd);
2174 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER, 0, NULL, 0);
2177 static int set_mesh_sync(struct hci_dev *hdev, void *data)
2179 struct mgmt_pending_cmd *cmd = data;
2180 struct mgmt_cp_set_mesh *cp = cmd->param;
2181 size_t len = cmd->param_len;
2183 memset(hdev->mesh_ad_types, 0, sizeof(hdev->mesh_ad_types));
2186 hci_dev_set_flag(hdev, HCI_MESH);
2188 hci_dev_clear_flag(hdev, HCI_MESH);
2192 /* If filters don't fit, forward all adv pkts */
2193 if (len <= sizeof(hdev->mesh_ad_types))
2194 memcpy(hdev->mesh_ad_types, cp->ad_types, len);
2196 hci_update_passive_scan_sync(hdev);
2200 static int set_mesh(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2202 struct mgmt_cp_set_mesh *cp = data;
2203 struct mgmt_pending_cmd *cmd;
2206 bt_dev_dbg(hdev, "sock %p", sk);
2208 if (!lmp_le_capable(hdev) ||
2209 !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL))
2210 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER,
2211 MGMT_STATUS_NOT_SUPPORTED);
2213 if (cp->enable != 0x00 && cp->enable != 0x01)
2214 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER,
2215 MGMT_STATUS_INVALID_PARAMS);
2219 cmd = mgmt_pending_add(sk, MGMT_OP_SET_MESH_RECEIVER, hdev, data, len);
2223 err = hci_cmd_sync_queue(hdev, set_mesh_sync, cmd,
2227 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_MESH_RECEIVER,
2228 MGMT_STATUS_FAILED);
2231 mgmt_pending_remove(cmd);
2234 hci_dev_unlock(hdev);
2238 static void mesh_send_start_complete(struct hci_dev *hdev, void *data, int err)
2240 struct mgmt_mesh_tx *mesh_tx = data;
2241 struct mgmt_cp_mesh_send *send = (void *)mesh_tx->param;
2242 unsigned long mesh_send_interval;
2243 u8 mgmt_err = mgmt_status(err);
2245 /* Report any errors here, but don't report completion */
2248 hci_dev_clear_flag(hdev, HCI_MESH_SENDING);
2249 /* Send Complete Error Code for handle */
2250 mesh_send_complete(hdev, mesh_tx, false);
2254 mesh_send_interval = msecs_to_jiffies((send->cnt) * 25);
2255 queue_delayed_work(hdev->req_workqueue, &hdev->mesh_send_done,
2256 mesh_send_interval);
2259 static int mesh_send_sync(struct hci_dev *hdev, void *data)
2261 struct mgmt_mesh_tx *mesh_tx = data;
2262 struct mgmt_cp_mesh_send *send = (void *)mesh_tx->param;
2263 struct adv_info *adv, *next_instance;
2264 u8 instance = hdev->le_num_of_adv_sets + 1;
2265 u16 timeout, duration;
2268 if (hdev->le_num_of_adv_sets <= hdev->adv_instance_cnt)
2269 return MGMT_STATUS_BUSY;
2272 duration = send->cnt * INTERVAL_TO_MS(hdev->le_adv_max_interval);
2273 adv = hci_add_adv_instance(hdev, instance, 0,
2274 send->adv_data_len, send->adv_data,
2277 HCI_ADV_TX_POWER_NO_PREFERENCE,
2278 hdev->le_adv_min_interval,
2279 hdev->le_adv_max_interval,
2283 mesh_tx->instance = instance;
2287 if (hdev->cur_adv_instance == instance) {
2288 /* If the currently advertised instance is being changed then
2289 * cancel the current advertising and schedule the next
2290 * instance. If there is only one instance then the overridden
2291 * advertising data will be visible right away.
2293 cancel_adv_timeout(hdev);
2295 next_instance = hci_get_next_instance(hdev, instance);
2297 instance = next_instance->instance;
2300 } else if (hdev->adv_instance_timeout) {
2301 /* Immediately advertise the new instance if no other, or
2302 * let it go naturally from queue if ADV is already happening
2308 return hci_schedule_adv_instance_sync(hdev, instance, true);
2313 static void send_count(struct mgmt_mesh_tx *mesh_tx, void *data)
2315 struct mgmt_rp_mesh_read_features *rp = data;
2317 if (rp->used_handles >= rp->max_handles)
2320 rp->handles[rp->used_handles++] = mesh_tx->handle;
2323 static int mesh_features(struct sock *sk, struct hci_dev *hdev,
2324 void *data, u16 len)
2326 struct mgmt_rp_mesh_read_features rp;
2328 if (!lmp_le_capable(hdev) ||
2329 !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL))
2330 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_READ_FEATURES,
2331 MGMT_STATUS_NOT_SUPPORTED);
2333 memset(&rp, 0, sizeof(rp));
2334 rp.index = cpu_to_le16(hdev->id);
2335 if (hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2336 rp.max_handles = MESH_HANDLES_MAX;
2341 mgmt_mesh_foreach(hdev, send_count, &rp, sk);
2343 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_MESH_READ_FEATURES, 0, &rp,
2344 rp.used_handles + sizeof(rp) - MESH_HANDLES_MAX);
2346 hci_dev_unlock(hdev);
2350 static int send_cancel(struct hci_dev *hdev, void *data)
2352 struct mgmt_pending_cmd *cmd = data;
2353 struct mgmt_cp_mesh_send_cancel *cancel = (void *)cmd->param;
2354 struct mgmt_mesh_tx *mesh_tx;
2356 if (!cancel->handle) {
2358 mesh_tx = mgmt_mesh_next(hdev, cmd->sk);
2361 mesh_send_complete(hdev, mesh_tx, false);
2364 mesh_tx = mgmt_mesh_find(hdev, cancel->handle);
2366 if (mesh_tx && mesh_tx->sk == cmd->sk)
2367 mesh_send_complete(hdev, mesh_tx, false);
2370 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL,
2372 mgmt_pending_free(cmd);
2377 static int mesh_send_cancel(struct sock *sk, struct hci_dev *hdev,
2378 void *data, u16 len)
2380 struct mgmt_pending_cmd *cmd;
2383 if (!lmp_le_capable(hdev) ||
2384 !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL))
2385 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL,
2386 MGMT_STATUS_NOT_SUPPORTED);
2388 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
2389 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL,
2390 MGMT_STATUS_REJECTED);
2393 cmd = mgmt_pending_new(sk, MGMT_OP_MESH_SEND_CANCEL, hdev, data, len);
2397 err = hci_cmd_sync_queue(hdev, send_cancel, cmd, NULL);
2400 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND_CANCEL,
2401 MGMT_STATUS_FAILED);
2404 mgmt_pending_free(cmd);
2407 hci_dev_unlock(hdev);
2411 static int mesh_send(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2413 struct mgmt_mesh_tx *mesh_tx;
2414 struct mgmt_cp_mesh_send *send = data;
2415 struct mgmt_rp_mesh_read_features rp;
2419 if (!lmp_le_capable(hdev) ||
2420 !hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL))
2421 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND,
2422 MGMT_STATUS_NOT_SUPPORTED);
2423 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED) ||
2424 len <= MGMT_MESH_SEND_SIZE ||
2425 len > (MGMT_MESH_SEND_SIZE + 31))
2426 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND,
2427 MGMT_STATUS_REJECTED);
2431 memset(&rp, 0, sizeof(rp));
2432 rp.max_handles = MESH_HANDLES_MAX;
2434 mgmt_mesh_foreach(hdev, send_count, &rp, sk);
2436 if (rp.max_handles <= rp.used_handles) {
2437 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND,
2442 sending = hci_dev_test_flag(hdev, HCI_MESH_SENDING);
2443 mesh_tx = mgmt_mesh_add(sk, hdev, send, len);
2448 err = hci_cmd_sync_queue(hdev, mesh_send_sync, mesh_tx,
2449 mesh_send_start_complete);
2452 bt_dev_err(hdev, "Send Mesh Failed %d", err);
2453 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_MESH_SEND,
2454 MGMT_STATUS_FAILED);
2458 mgmt_mesh_remove(mesh_tx);
2461 hci_dev_set_flag(hdev, HCI_MESH_SENDING);
2463 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_MESH_SEND, 0,
2464 &mesh_tx->handle, 1);
2468 hci_dev_unlock(hdev);
2472 static int set_le(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2474 struct mgmt_mode *cp = data;
2475 struct mgmt_pending_cmd *cmd;
2479 bt_dev_dbg(hdev, "sock %p", sk);
2481 if (!lmp_le_capable(hdev))
2482 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2483 MGMT_STATUS_NOT_SUPPORTED);
2485 if (cp->val != 0x00 && cp->val != 0x01)
2486 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2487 MGMT_STATUS_INVALID_PARAMS);
2489 /* Bluetooth single mode LE only controllers or dual-mode
2490 * controllers configured as LE only devices, do not allow
2491 * switching LE off. These have either LE enabled explicitly
2492 * or BR/EDR has been previously switched off.
2494 * When trying to enable an already enabled LE, then gracefully
2495 * send a positive response. Trying to disable it however will
2496 * result into rejection.
2498 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
2499 if (cp->val == 0x01)
2500 return send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2502 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2503 MGMT_STATUS_REJECTED);
2509 enabled = lmp_host_le_capable(hdev);
2511 if (!hdev_is_powered(hdev) || val == enabled) {
2512 bool changed = false;
2514 if (val != hci_dev_test_flag(hdev, HCI_LE_ENABLED)) {
2515 hci_dev_change_flag(hdev, HCI_LE_ENABLED);
2519 if (!val && hci_dev_test_flag(hdev, HCI_ADVERTISING)) {
2520 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
2524 err = send_settings_rsp(sk, MGMT_OP_SET_LE, hdev);
2529 err = new_settings(hdev, sk);
2534 if (pending_find(MGMT_OP_SET_LE, hdev) ||
2535 pending_find(MGMT_OP_SET_ADVERTISING, hdev)) {
2536 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2541 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LE, hdev, data, len);
2545 err = hci_cmd_sync_queue(hdev, set_le_sync, cmd,
2549 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LE,
2550 MGMT_STATUS_FAILED);
2553 mgmt_pending_remove(cmd);
2557 hci_dev_unlock(hdev);
2561 /* This is a helper function to test for pending mgmt commands that can
2562 * cause CoD or EIR HCI commands. We can only allow one such pending
2563 * mgmt command at a time since otherwise we cannot easily track what
2564 * the current values are, will be, and based on that calculate if a new
2565 * HCI command needs to be sent and if yes with what value.
2567 static bool pending_eir_or_class(struct hci_dev *hdev)
2569 struct mgmt_pending_cmd *cmd;
2571 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
2572 switch (cmd->opcode) {
2573 case MGMT_OP_ADD_UUID:
2574 case MGMT_OP_REMOVE_UUID:
2575 case MGMT_OP_SET_DEV_CLASS:
2576 case MGMT_OP_SET_POWERED:
2584 static const u8 bluetooth_base_uuid[] = {
2585 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2586 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2589 static u8 get_uuid_size(const u8 *uuid)
2593 if (memcmp(uuid, bluetooth_base_uuid, 12))
2596 val = get_unaligned_le32(&uuid[12]);
2603 static void mgmt_class_complete(struct hci_dev *hdev, void *data, int err)
2605 struct mgmt_pending_cmd *cmd = data;
2607 bt_dev_dbg(hdev, "err %d", err);
2609 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
2610 mgmt_status(err), hdev->dev_class, 3);
2612 mgmt_pending_free(cmd);
2615 static int add_uuid_sync(struct hci_dev *hdev, void *data)
2619 err = hci_update_class_sync(hdev);
2623 return hci_update_eir_sync(hdev);
2626 static int add_uuid(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
2628 struct mgmt_cp_add_uuid *cp = data;
2629 struct mgmt_pending_cmd *cmd;
2630 struct bt_uuid *uuid;
2633 bt_dev_dbg(hdev, "sock %p", sk);
2637 if (pending_eir_or_class(hdev)) {
2638 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_UUID,
2643 uuid = kmalloc(sizeof(*uuid), GFP_KERNEL);
2649 memcpy(uuid->uuid, cp->uuid, 16);
2650 uuid->svc_hint = cp->svc_hint;
2651 uuid->size = get_uuid_size(cp->uuid);
2653 list_add_tail(&uuid->list, &hdev->uuids);
2655 cmd = mgmt_pending_new(sk, MGMT_OP_ADD_UUID, hdev, data, len);
2661 err = hci_cmd_sync_queue(hdev, add_uuid_sync, cmd, mgmt_class_complete);
2663 mgmt_pending_free(cmd);
2668 hci_dev_unlock(hdev);
2672 static bool enable_service_cache(struct hci_dev *hdev)
2674 if (!hdev_is_powered(hdev))
2677 if (!hci_dev_test_and_set_flag(hdev, HCI_SERVICE_CACHE)) {
2678 queue_delayed_work(hdev->workqueue, &hdev->service_cache,
2686 static int remove_uuid_sync(struct hci_dev *hdev, void *data)
2690 err = hci_update_class_sync(hdev);
2694 return hci_update_eir_sync(hdev);
2697 static int remove_uuid(struct sock *sk, struct hci_dev *hdev, void *data,
2700 struct mgmt_cp_remove_uuid *cp = data;
2701 struct mgmt_pending_cmd *cmd;
2702 struct bt_uuid *match, *tmp;
2703 static const u8 bt_uuid_any[] = {
2704 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2708 bt_dev_dbg(hdev, "sock %p", sk);
2712 if (pending_eir_or_class(hdev)) {
2713 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2718 if (memcmp(cp->uuid, bt_uuid_any, 16) == 0) {
2719 hci_uuids_clear(hdev);
2721 if (enable_service_cache(hdev)) {
2722 err = mgmt_cmd_complete(sk, hdev->id,
2723 MGMT_OP_REMOVE_UUID,
2724 0, hdev->dev_class, 3);
2733 list_for_each_entry_safe(match, tmp, &hdev->uuids, list) {
2734 if (memcmp(match->uuid, cp->uuid, 16) != 0)
2737 list_del(&match->list);
2743 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_UUID,
2744 MGMT_STATUS_INVALID_PARAMS);
2749 cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_UUID, hdev, data, len);
2755 err = hci_cmd_sync_queue(hdev, remove_uuid_sync, cmd,
2756 mgmt_class_complete);
2758 mgmt_pending_free(cmd);
2761 hci_dev_unlock(hdev);
2765 static int set_class_sync(struct hci_dev *hdev, void *data)
2769 if (hci_dev_test_and_clear_flag(hdev, HCI_SERVICE_CACHE)) {
2770 cancel_delayed_work_sync(&hdev->service_cache);
2771 err = hci_update_eir_sync(hdev);
2777 return hci_update_class_sync(hdev);
2780 static int set_dev_class(struct sock *sk, struct hci_dev *hdev, void *data,
2783 struct mgmt_cp_set_dev_class *cp = data;
2784 struct mgmt_pending_cmd *cmd;
2787 bt_dev_dbg(hdev, "sock %p", sk);
2789 if (!lmp_bredr_capable(hdev))
2790 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2791 MGMT_STATUS_NOT_SUPPORTED);
2795 if (pending_eir_or_class(hdev)) {
2796 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2801 if ((cp->minor & 0x03) != 0 || (cp->major & 0xe0) != 0) {
2802 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEV_CLASS,
2803 MGMT_STATUS_INVALID_PARAMS);
2807 hdev->major_class = cp->major;
2808 hdev->minor_class = cp->minor;
2810 if (!hdev_is_powered(hdev)) {
2811 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEV_CLASS, 0,
2812 hdev->dev_class, 3);
2816 cmd = mgmt_pending_new(sk, MGMT_OP_SET_DEV_CLASS, hdev, data, len);
2822 err = hci_cmd_sync_queue(hdev, set_class_sync, cmd,
2823 mgmt_class_complete);
2825 mgmt_pending_free(cmd);
2828 hci_dev_unlock(hdev);
2832 static int load_link_keys(struct sock *sk, struct hci_dev *hdev, void *data,
2835 struct mgmt_cp_load_link_keys *cp = data;
2836 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
2837 sizeof(struct mgmt_link_key_info));
2838 u16 key_count, expected_len;
2842 bt_dev_dbg(hdev, "sock %p", sk);
2844 if (!lmp_bredr_capable(hdev))
2845 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2846 MGMT_STATUS_NOT_SUPPORTED);
2848 key_count = __le16_to_cpu(cp->key_count);
2849 if (key_count > max_key_count) {
2850 bt_dev_err(hdev, "load_link_keys: too big key_count value %u",
2852 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2853 MGMT_STATUS_INVALID_PARAMS);
2856 expected_len = struct_size(cp, keys, key_count);
2857 if (expected_len != len) {
2858 bt_dev_err(hdev, "load_link_keys: expected %u bytes, got %u bytes",
2860 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2861 MGMT_STATUS_INVALID_PARAMS);
2864 if (cp->debug_keys != 0x00 && cp->debug_keys != 0x01)
2865 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS,
2866 MGMT_STATUS_INVALID_PARAMS);
2868 bt_dev_dbg(hdev, "debug_keys %u key_count %u", cp->debug_keys,
2871 for (i = 0; i < key_count; i++) {
2872 struct mgmt_link_key_info *key = &cp->keys[i];
2874 if (key->addr.type != BDADDR_BREDR || key->type > 0x08)
2875 return mgmt_cmd_status(sk, hdev->id,
2876 MGMT_OP_LOAD_LINK_KEYS,
2877 MGMT_STATUS_INVALID_PARAMS);
2882 hci_link_keys_clear(hdev);
2885 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
2887 changed = hci_dev_test_and_clear_flag(hdev,
2888 HCI_KEEP_DEBUG_KEYS);
2891 new_settings(hdev, NULL);
2893 for (i = 0; i < key_count; i++) {
2894 struct mgmt_link_key_info *key = &cp->keys[i];
2896 if (hci_is_blocked_key(hdev,
2897 HCI_BLOCKED_KEY_TYPE_LINKKEY,
2899 bt_dev_warn(hdev, "Skipping blocked link key for %pMR",
2904 /* Always ignore debug keys and require a new pairing if
2905 * the user wants to use them.
2907 if (key->type == HCI_LK_DEBUG_COMBINATION)
2910 hci_add_link_key(hdev, NULL, &key->addr.bdaddr, key->val,
2911 key->type, key->pin_len, NULL);
2914 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LINK_KEYS, 0, NULL, 0);
2916 hci_dev_unlock(hdev);
2921 static int device_unpaired(struct hci_dev *hdev, bdaddr_t *bdaddr,
2922 u8 addr_type, struct sock *skip_sk)
2924 struct mgmt_ev_device_unpaired ev;
2926 bacpy(&ev.addr.bdaddr, bdaddr);
2927 ev.addr.type = addr_type;
2929 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED, hdev, &ev, sizeof(ev),
2933 static void unpair_device_complete(struct hci_dev *hdev, void *data, int err)
2935 struct mgmt_pending_cmd *cmd = data;
2936 struct mgmt_cp_unpair_device *cp = cmd->param;
2939 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
2941 cmd->cmd_complete(cmd, err);
2942 mgmt_pending_free(cmd);
2945 static int unpair_device_sync(struct hci_dev *hdev, void *data)
2947 struct mgmt_pending_cmd *cmd = data;
2948 struct mgmt_cp_unpair_device *cp = cmd->param;
2949 struct hci_conn *conn;
2951 if (cp->addr.type == BDADDR_BREDR)
2952 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
2955 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
2956 le_addr_type(cp->addr.type));
2961 return hci_abort_conn_sync(hdev, conn, HCI_ERROR_REMOTE_USER_TERM);
2964 static int unpair_device(struct sock *sk, struct hci_dev *hdev, void *data,
2967 struct mgmt_cp_unpair_device *cp = data;
2968 struct mgmt_rp_unpair_device rp;
2969 struct hci_conn_params *params;
2970 struct mgmt_pending_cmd *cmd;
2971 struct hci_conn *conn;
2975 memset(&rp, 0, sizeof(rp));
2976 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
2977 rp.addr.type = cp->addr.type;
2979 if (!bdaddr_type_is_valid(cp->addr.type))
2980 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2981 MGMT_STATUS_INVALID_PARAMS,
2984 if (cp->disconnect != 0x00 && cp->disconnect != 0x01)
2985 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2986 MGMT_STATUS_INVALID_PARAMS,
2991 if (!hdev_is_powered(hdev)) {
2992 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
2993 MGMT_STATUS_NOT_POWERED, &rp,
2998 if (cp->addr.type == BDADDR_BREDR) {
2999 /* If disconnection is requested, then look up the
3000 * connection. If the remote device is connected, it
3001 * will be later used to terminate the link.
3003 * Setting it to NULL explicitly will cause no
3004 * termination of the link.
3007 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3012 err = hci_remove_link_key(hdev, &cp->addr.bdaddr);
3014 err = mgmt_cmd_complete(sk, hdev->id,
3015 MGMT_OP_UNPAIR_DEVICE,
3016 MGMT_STATUS_NOT_PAIRED, &rp,
3024 /* LE address type */
3025 addr_type = le_addr_type(cp->addr.type);
3027 /* Abort any ongoing SMP pairing. Removes ltk and irk if they exist. */
3028 err = smp_cancel_and_remove_pairing(hdev, &cp->addr.bdaddr, addr_type);
3030 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE,
3031 MGMT_STATUS_NOT_PAIRED, &rp,
3036 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr, addr_type);
3038 hci_conn_params_del(hdev, &cp->addr.bdaddr, addr_type);
3043 /* Defer clearing up the connection parameters until closing to
3044 * give a chance of keeping them if a repairing happens.
3046 set_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
3048 /* Disable auto-connection parameters if present */
3049 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr, addr_type);
3051 if (params->explicit_connect)
3052 params->auto_connect = HCI_AUTO_CONN_EXPLICIT;
3054 params->auto_connect = HCI_AUTO_CONN_DISABLED;
3057 /* If disconnection is not requested, then clear the connection
3058 * variable so that the link is not terminated.
3060 if (!cp->disconnect)
3064 /* If the connection variable is set, then termination of the
3065 * link is requested.
3068 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNPAIR_DEVICE, 0,
3070 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, sk);
3074 cmd = mgmt_pending_new(sk, MGMT_OP_UNPAIR_DEVICE, hdev, cp,
3081 cmd->cmd_complete = addr_cmd_complete;
3083 err = hci_cmd_sync_queue(hdev, unpair_device_sync, cmd,
3084 unpair_device_complete);
3086 mgmt_pending_free(cmd);
3089 hci_dev_unlock(hdev);
3093 static int disconnect(struct sock *sk, struct hci_dev *hdev, void *data,
3096 struct mgmt_cp_disconnect *cp = data;
3097 struct mgmt_rp_disconnect rp;
3098 struct mgmt_pending_cmd *cmd;
3099 struct hci_conn *conn;
3102 bt_dev_dbg(hdev, "sock %p", sk);
3104 memset(&rp, 0, sizeof(rp));
3105 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3106 rp.addr.type = cp->addr.type;
3108 if (!bdaddr_type_is_valid(cp->addr.type))
3109 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3110 MGMT_STATUS_INVALID_PARAMS,
3115 if (!test_bit(HCI_UP, &hdev->flags)) {
3116 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3117 MGMT_STATUS_NOT_POWERED, &rp,
3122 if (pending_find(MGMT_OP_DISCONNECT, hdev)) {
3123 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3124 MGMT_STATUS_BUSY, &rp, sizeof(rp));
3128 if (cp->addr.type == BDADDR_BREDR)
3129 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
3132 conn = hci_conn_hash_lookup_le(hdev, &cp->addr.bdaddr,
3133 le_addr_type(cp->addr.type));
3135 if (!conn || conn->state == BT_OPEN || conn->state == BT_CLOSED) {
3136 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_DISCONNECT,
3137 MGMT_STATUS_NOT_CONNECTED, &rp,
3142 cmd = mgmt_pending_add(sk, MGMT_OP_DISCONNECT, hdev, data, len);
3148 cmd->cmd_complete = generic_cmd_complete;
3150 err = hci_disconnect(conn, HCI_ERROR_REMOTE_USER_TERM);
3152 mgmt_pending_remove(cmd);
3155 hci_dev_unlock(hdev);
3159 static u8 link_to_bdaddr(u8 link_type, u8 addr_type)
3161 switch (link_type) {
3163 switch (addr_type) {
3164 case ADDR_LE_DEV_PUBLIC:
3165 return BDADDR_LE_PUBLIC;
3168 /* Fallback to LE Random address type */
3169 return BDADDR_LE_RANDOM;
3173 /* Fallback to BR/EDR type */
3174 return BDADDR_BREDR;
3178 static int get_connections(struct sock *sk, struct hci_dev *hdev, void *data,
3181 struct mgmt_rp_get_connections *rp;
3186 bt_dev_dbg(hdev, "sock %p", sk);
3190 if (!hdev_is_powered(hdev)) {
3191 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_CONNECTIONS,
3192 MGMT_STATUS_NOT_POWERED);
3197 list_for_each_entry(c, &hdev->conn_hash.list, list) {
3198 if (test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
3202 rp = kmalloc(struct_size(rp, addr, i), GFP_KERNEL);
3209 list_for_each_entry(c, &hdev->conn_hash.list, list) {
3210 if (!test_bit(HCI_CONN_MGMT_CONNECTED, &c->flags))
3212 bacpy(&rp->addr[i].bdaddr, &c->dst);
3213 rp->addr[i].type = link_to_bdaddr(c->type, c->dst_type);
3214 if (c->type == SCO_LINK || c->type == ESCO_LINK)
3219 rp->conn_count = cpu_to_le16(i);
3221 /* Recalculate length in case of filtered SCO connections, etc */
3222 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONNECTIONS, 0, rp,
3223 struct_size(rp, addr, i));
3228 hci_dev_unlock(hdev);
3232 static int send_pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3233 struct mgmt_cp_pin_code_neg_reply *cp)
3235 struct mgmt_pending_cmd *cmd;
3238 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_NEG_REPLY, hdev, cp,
3243 cmd->cmd_complete = addr_cmd_complete;
3245 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
3246 sizeof(cp->addr.bdaddr), &cp->addr.bdaddr);
3248 mgmt_pending_remove(cmd);
3253 static int pin_code_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3256 struct hci_conn *conn;
3257 struct mgmt_cp_pin_code_reply *cp = data;
3258 struct hci_cp_pin_code_reply reply;
3259 struct mgmt_pending_cmd *cmd;
3262 bt_dev_dbg(hdev, "sock %p", sk);
3266 if (!hdev_is_powered(hdev)) {
3267 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3268 MGMT_STATUS_NOT_POWERED);
3272 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
3274 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3275 MGMT_STATUS_NOT_CONNECTED);
3279 if (conn->pending_sec_level == BT_SECURITY_HIGH && cp->pin_len != 16) {
3280 struct mgmt_cp_pin_code_neg_reply ncp;
3282 memcpy(&ncp.addr, &cp->addr, sizeof(ncp.addr));
3284 bt_dev_err(hdev, "PIN code is not 16 bytes long");
3286 err = send_pin_code_neg_reply(sk, hdev, &ncp);
3288 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_PIN_CODE_REPLY,
3289 MGMT_STATUS_INVALID_PARAMS);
3294 cmd = mgmt_pending_add(sk, MGMT_OP_PIN_CODE_REPLY, hdev, data, len);
3300 cmd->cmd_complete = addr_cmd_complete;
3302 bacpy(&reply.bdaddr, &cp->addr.bdaddr);
3303 reply.pin_len = cp->pin_len;
3304 memcpy(reply.pin_code, cp->pin_code, sizeof(reply.pin_code));
3306 err = hci_send_cmd(hdev, HCI_OP_PIN_CODE_REPLY, sizeof(reply), &reply);
3308 mgmt_pending_remove(cmd);
3311 hci_dev_unlock(hdev);
3315 static int set_io_capability(struct sock *sk, struct hci_dev *hdev, void *data,
3318 struct mgmt_cp_set_io_capability *cp = data;
3320 bt_dev_dbg(hdev, "sock %p", sk);
3322 if (cp->io_capability > SMP_IO_KEYBOARD_DISPLAY)
3323 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY,
3324 MGMT_STATUS_INVALID_PARAMS);
3328 hdev->io_capability = cp->io_capability;
3330 bt_dev_dbg(hdev, "IO capability set to 0x%02x", hdev->io_capability);
3332 hci_dev_unlock(hdev);
3334 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_IO_CAPABILITY, 0,
3338 static struct mgmt_pending_cmd *find_pairing(struct hci_conn *conn)
3340 struct hci_dev *hdev = conn->hdev;
3341 struct mgmt_pending_cmd *cmd;
3343 list_for_each_entry(cmd, &hdev->mgmt_pending, list) {
3344 if (cmd->opcode != MGMT_OP_PAIR_DEVICE)
3347 if (cmd->user_data != conn)
3356 static int pairing_complete(struct mgmt_pending_cmd *cmd, u8 status)
3358 struct mgmt_rp_pair_device rp;
3359 struct hci_conn *conn = cmd->user_data;
3362 bacpy(&rp.addr.bdaddr, &conn->dst);
3363 rp.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
3365 err = mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_PAIR_DEVICE,
3366 status, &rp, sizeof(rp));
3368 /* So we don't get further callbacks for this connection */
3369 conn->connect_cfm_cb = NULL;
3370 conn->security_cfm_cb = NULL;
3371 conn->disconn_cfm_cb = NULL;
3373 hci_conn_drop(conn);
3375 /* The device is paired so there is no need to remove
3376 * its connection parameters anymore.
3378 clear_bit(HCI_CONN_PARAM_REMOVAL_PEND, &conn->flags);
3385 void mgmt_smp_complete(struct hci_conn *conn, bool complete)
3387 u8 status = complete ? MGMT_STATUS_SUCCESS : MGMT_STATUS_FAILED;
3388 struct mgmt_pending_cmd *cmd;
3390 cmd = find_pairing(conn);
3392 cmd->cmd_complete(cmd, status);
3393 mgmt_pending_remove(cmd);
3397 static void pairing_complete_cb(struct hci_conn *conn, u8 status)
3399 struct mgmt_pending_cmd *cmd;
3401 BT_DBG("status %u", status);
3403 cmd = find_pairing(conn);
3405 BT_DBG("Unable to find a pending command");
3409 cmd->cmd_complete(cmd, mgmt_status(status));
3410 mgmt_pending_remove(cmd);
3413 static void le_pairing_complete_cb(struct hci_conn *conn, u8 status)
3415 struct mgmt_pending_cmd *cmd;
3417 BT_DBG("status %u", status);
3422 cmd = find_pairing(conn);
3424 BT_DBG("Unable to find a pending command");
3428 cmd->cmd_complete(cmd, mgmt_status(status));
3429 mgmt_pending_remove(cmd);
3432 static int pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3435 struct mgmt_cp_pair_device *cp = data;
3436 struct mgmt_rp_pair_device rp;
3437 struct mgmt_pending_cmd *cmd;
3438 u8 sec_level, auth_type;
3439 struct hci_conn *conn;
3442 bt_dev_dbg(hdev, "sock %p", sk);
3444 memset(&rp, 0, sizeof(rp));
3445 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
3446 rp.addr.type = cp->addr.type;
3448 if (!bdaddr_type_is_valid(cp->addr.type))
3449 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3450 MGMT_STATUS_INVALID_PARAMS,
3453 if (cp->io_cap > SMP_IO_KEYBOARD_DISPLAY)
3454 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3455 MGMT_STATUS_INVALID_PARAMS,
3460 if (!hdev_is_powered(hdev)) {
3461 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3462 MGMT_STATUS_NOT_POWERED, &rp,
3467 if (hci_bdaddr_is_paired(hdev, &cp->addr.bdaddr, cp->addr.type)) {
3468 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3469 MGMT_STATUS_ALREADY_PAIRED, &rp,
3474 sec_level = BT_SECURITY_MEDIUM;
3475 auth_type = HCI_AT_DEDICATED_BONDING;
3477 if (cp->addr.type == BDADDR_BREDR) {
3478 conn = hci_connect_acl(hdev, &cp->addr.bdaddr, sec_level,
3479 auth_type, CONN_REASON_PAIR_DEVICE);
3481 u8 addr_type = le_addr_type(cp->addr.type);
3482 struct hci_conn_params *p;
3484 /* When pairing a new device, it is expected to remember
3485 * this device for future connections. Adding the connection
3486 * parameter information ahead of time allows tracking
3487 * of the peripheral preferred values and will speed up any
3488 * further connection establishment.
3490 * If connection parameters already exist, then they
3491 * will be kept and this function does nothing.
3493 p = hci_conn_params_add(hdev, &cp->addr.bdaddr, addr_type);
3495 if (p->auto_connect == HCI_AUTO_CONN_EXPLICIT)
3496 p->auto_connect = HCI_AUTO_CONN_DISABLED;
3498 conn = hci_connect_le_scan(hdev, &cp->addr.bdaddr, addr_type,
3499 sec_level, HCI_LE_CONN_TIMEOUT,
3500 CONN_REASON_PAIR_DEVICE);
3506 if (PTR_ERR(conn) == -EBUSY)
3507 status = MGMT_STATUS_BUSY;
3508 else if (PTR_ERR(conn) == -EOPNOTSUPP)
3509 status = MGMT_STATUS_NOT_SUPPORTED;
3510 else if (PTR_ERR(conn) == -ECONNREFUSED)
3511 status = MGMT_STATUS_REJECTED;
3513 status = MGMT_STATUS_CONNECT_FAILED;
3515 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3516 status, &rp, sizeof(rp));
3520 if (conn->connect_cfm_cb) {
3521 hci_conn_drop(conn);
3522 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_PAIR_DEVICE,
3523 MGMT_STATUS_BUSY, &rp, sizeof(rp));
3527 cmd = mgmt_pending_add(sk, MGMT_OP_PAIR_DEVICE, hdev, data, len);
3530 hci_conn_drop(conn);
3534 cmd->cmd_complete = pairing_complete;
3536 /* For LE, just connecting isn't a proof that the pairing finished */
3537 if (cp->addr.type == BDADDR_BREDR) {
3538 conn->connect_cfm_cb = pairing_complete_cb;
3539 conn->security_cfm_cb = pairing_complete_cb;
3540 conn->disconn_cfm_cb = pairing_complete_cb;
3542 conn->connect_cfm_cb = le_pairing_complete_cb;
3543 conn->security_cfm_cb = le_pairing_complete_cb;
3544 conn->disconn_cfm_cb = le_pairing_complete_cb;
3547 conn->io_capability = cp->io_cap;
3548 cmd->user_data = hci_conn_get(conn);
3550 if ((conn->state == BT_CONNECTED || conn->state == BT_CONFIG) &&
3551 hci_conn_security(conn, sec_level, auth_type, true)) {
3552 cmd->cmd_complete(cmd, 0);
3553 mgmt_pending_remove(cmd);
3559 hci_dev_unlock(hdev);
3563 static int abort_conn_sync(struct hci_dev *hdev, void *data)
3565 struct hci_conn *conn;
3566 u16 handle = PTR_ERR(data);
3568 conn = hci_conn_hash_lookup_handle(hdev, handle);
3572 return hci_abort_conn_sync(hdev, conn, HCI_ERROR_REMOTE_USER_TERM);
3575 static int cancel_pair_device(struct sock *sk, struct hci_dev *hdev, void *data,
3578 struct mgmt_addr_info *addr = data;
3579 struct mgmt_pending_cmd *cmd;
3580 struct hci_conn *conn;
3583 bt_dev_dbg(hdev, "sock %p", sk);
3587 if (!hdev_is_powered(hdev)) {
3588 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3589 MGMT_STATUS_NOT_POWERED);
3593 cmd = pending_find(MGMT_OP_PAIR_DEVICE, hdev);
3595 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3596 MGMT_STATUS_INVALID_PARAMS);
3600 conn = cmd->user_data;
3602 if (bacmp(&addr->bdaddr, &conn->dst) != 0) {
3603 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE,
3604 MGMT_STATUS_INVALID_PARAMS);
3608 cmd->cmd_complete(cmd, MGMT_STATUS_CANCELLED);
3609 mgmt_pending_remove(cmd);
3611 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CANCEL_PAIR_DEVICE, 0,
3612 addr, sizeof(*addr));
3614 /* Since user doesn't want to proceed with the connection, abort any
3615 * ongoing pairing and then terminate the link if it was created
3616 * because of the pair device action.
3618 if (addr->type == BDADDR_BREDR)
3619 hci_remove_link_key(hdev, &addr->bdaddr);
3621 smp_cancel_and_remove_pairing(hdev, &addr->bdaddr,
3622 le_addr_type(addr->type));
3624 if (conn->conn_reason == CONN_REASON_PAIR_DEVICE)
3625 hci_cmd_sync_queue(hdev, abort_conn_sync, ERR_PTR(conn->handle),
3629 hci_dev_unlock(hdev);
3633 static int user_pairing_resp(struct sock *sk, struct hci_dev *hdev,
3634 struct mgmt_addr_info *addr, u16 mgmt_op,
3635 u16 hci_op, __le32 passkey)
3637 struct mgmt_pending_cmd *cmd;
3638 struct hci_conn *conn;
3643 if (!hdev_is_powered(hdev)) {
3644 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3645 MGMT_STATUS_NOT_POWERED, addr,
3650 if (addr->type == BDADDR_BREDR)
3651 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &addr->bdaddr);
3653 conn = hci_conn_hash_lookup_le(hdev, &addr->bdaddr,
3654 le_addr_type(addr->type));
3657 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3658 MGMT_STATUS_NOT_CONNECTED, addr,
3663 if (addr->type == BDADDR_LE_PUBLIC || addr->type == BDADDR_LE_RANDOM) {
3664 err = smp_user_confirm_reply(conn, mgmt_op, passkey);
3666 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3667 MGMT_STATUS_SUCCESS, addr,
3670 err = mgmt_cmd_complete(sk, hdev->id, mgmt_op,
3671 MGMT_STATUS_FAILED, addr,
3677 cmd = mgmt_pending_add(sk, mgmt_op, hdev, addr, sizeof(*addr));
3683 cmd->cmd_complete = addr_cmd_complete;
3685 /* Continue with pairing via HCI */
3686 if (hci_op == HCI_OP_USER_PASSKEY_REPLY) {
3687 struct hci_cp_user_passkey_reply cp;
3689 bacpy(&cp.bdaddr, &addr->bdaddr);
3690 cp.passkey = passkey;
3691 err = hci_send_cmd(hdev, hci_op, sizeof(cp), &cp);
3693 err = hci_send_cmd(hdev, hci_op, sizeof(addr->bdaddr),
3697 mgmt_pending_remove(cmd);
3700 hci_dev_unlock(hdev);
3704 static int pin_code_neg_reply(struct sock *sk, struct hci_dev *hdev,
3705 void *data, u16 len)
3707 struct mgmt_cp_pin_code_neg_reply *cp = data;
3709 bt_dev_dbg(hdev, "sock %p", sk);
3711 return user_pairing_resp(sk, hdev, &cp->addr,
3712 MGMT_OP_PIN_CODE_NEG_REPLY,
3713 HCI_OP_PIN_CODE_NEG_REPLY, 0);
3716 static int user_confirm_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3719 struct mgmt_cp_user_confirm_reply *cp = data;
3721 bt_dev_dbg(hdev, "sock %p", sk);
3723 if (len != sizeof(*cp))
3724 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_USER_CONFIRM_REPLY,
3725 MGMT_STATUS_INVALID_PARAMS);
3727 return user_pairing_resp(sk, hdev, &cp->addr,
3728 MGMT_OP_USER_CONFIRM_REPLY,
3729 HCI_OP_USER_CONFIRM_REPLY, 0);
3732 static int user_confirm_neg_reply(struct sock *sk, struct hci_dev *hdev,
3733 void *data, u16 len)
3735 struct mgmt_cp_user_confirm_neg_reply *cp = data;
3737 bt_dev_dbg(hdev, "sock %p", sk);
3739 return user_pairing_resp(sk, hdev, &cp->addr,
3740 MGMT_OP_USER_CONFIRM_NEG_REPLY,
3741 HCI_OP_USER_CONFIRM_NEG_REPLY, 0);
3744 static int user_passkey_reply(struct sock *sk, struct hci_dev *hdev, void *data,
3747 struct mgmt_cp_user_passkey_reply *cp = data;
3749 bt_dev_dbg(hdev, "sock %p", sk);
3751 return user_pairing_resp(sk, hdev, &cp->addr,
3752 MGMT_OP_USER_PASSKEY_REPLY,
3753 HCI_OP_USER_PASSKEY_REPLY, cp->passkey);
3756 static int user_passkey_neg_reply(struct sock *sk, struct hci_dev *hdev,
3757 void *data, u16 len)
3759 struct mgmt_cp_user_passkey_neg_reply *cp = data;
3761 bt_dev_dbg(hdev, "sock %p", sk);
3763 return user_pairing_resp(sk, hdev, &cp->addr,
3764 MGMT_OP_USER_PASSKEY_NEG_REPLY,
3765 HCI_OP_USER_PASSKEY_NEG_REPLY, 0);
3768 static int adv_expire_sync(struct hci_dev *hdev, u32 flags)
3770 struct adv_info *adv_instance;
3772 adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
3776 /* stop if current instance doesn't need to be changed */
3777 if (!(adv_instance->flags & flags))
3780 cancel_adv_timeout(hdev);
3782 adv_instance = hci_get_next_instance(hdev, adv_instance->instance);
3786 hci_schedule_adv_instance_sync(hdev, adv_instance->instance, true);
3791 static int name_changed_sync(struct hci_dev *hdev, void *data)
3793 return adv_expire_sync(hdev, MGMT_ADV_FLAG_LOCAL_NAME);
3796 static void set_name_complete(struct hci_dev *hdev, void *data, int err)
3798 struct mgmt_pending_cmd *cmd = data;
3799 struct mgmt_cp_set_local_name *cp = cmd->param;
3800 u8 status = mgmt_status(err);
3802 bt_dev_dbg(hdev, "err %d", err);
3804 if (cmd != pending_find(MGMT_OP_SET_LOCAL_NAME, hdev))
3808 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3811 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3814 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
3815 hci_cmd_sync_queue(hdev, name_changed_sync, NULL, NULL);
3818 mgmt_pending_remove(cmd);
3821 static int set_name_sync(struct hci_dev *hdev, void *data)
3823 if (lmp_bredr_capable(hdev)) {
3824 hci_update_name_sync(hdev);
3825 hci_update_eir_sync(hdev);
3828 /* The name is stored in the scan response data and so
3829 * no need to update the advertising data here.
3831 if (lmp_le_capable(hdev) && hci_dev_test_flag(hdev, HCI_ADVERTISING))
3832 hci_update_scan_rsp_data_sync(hdev, hdev->cur_adv_instance);
3837 static int set_local_name(struct sock *sk, struct hci_dev *hdev, void *data,
3840 struct mgmt_cp_set_local_name *cp = data;
3841 struct mgmt_pending_cmd *cmd;
3844 bt_dev_dbg(hdev, "sock %p", sk);
3848 /* If the old values are the same as the new ones just return a
3849 * direct command complete event.
3851 if (!memcmp(hdev->dev_name, cp->name, sizeof(hdev->dev_name)) &&
3852 !memcmp(hdev->short_name, cp->short_name,
3853 sizeof(hdev->short_name))) {
3854 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3859 memcpy(hdev->short_name, cp->short_name, sizeof(hdev->short_name));
3861 if (!hdev_is_powered(hdev)) {
3862 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3864 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME, 0,
3869 err = mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, data,
3870 len, HCI_MGMT_LOCAL_NAME_EVENTS, sk);
3871 ext_info_changed(hdev, sk);
3876 cmd = mgmt_pending_add(sk, MGMT_OP_SET_LOCAL_NAME, hdev, data, len);
3880 err = hci_cmd_sync_queue(hdev, set_name_sync, cmd,
3884 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_LOCAL_NAME,
3885 MGMT_STATUS_FAILED);
3888 mgmt_pending_remove(cmd);
3893 memcpy(hdev->dev_name, cp->name, sizeof(hdev->dev_name));
3896 hci_dev_unlock(hdev);
3900 static int appearance_changed_sync(struct hci_dev *hdev, void *data)
3902 return adv_expire_sync(hdev, MGMT_ADV_FLAG_APPEARANCE);
3905 static int set_appearance(struct sock *sk, struct hci_dev *hdev, void *data,
3908 struct mgmt_cp_set_appearance *cp = data;
3912 bt_dev_dbg(hdev, "sock %p", sk);
3914 if (!lmp_le_capable(hdev))
3915 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_APPEARANCE,
3916 MGMT_STATUS_NOT_SUPPORTED);
3918 appearance = le16_to_cpu(cp->appearance);
3922 if (hdev->appearance != appearance) {
3923 hdev->appearance = appearance;
3925 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
3926 hci_cmd_sync_queue(hdev, appearance_changed_sync, NULL,
3929 ext_info_changed(hdev, sk);
3932 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_APPEARANCE, 0, NULL,
3935 hci_dev_unlock(hdev);
3940 static int get_phy_configuration(struct sock *sk, struct hci_dev *hdev,
3941 void *data, u16 len)
3943 struct mgmt_rp_get_phy_configuration rp;
3945 bt_dev_dbg(hdev, "sock %p", sk);
3949 memset(&rp, 0, sizeof(rp));
3951 rp.supported_phys = cpu_to_le32(get_supported_phys(hdev));
3952 rp.selected_phys = cpu_to_le32(get_selected_phys(hdev));
3953 rp.configurable_phys = cpu_to_le32(get_configurable_phys(hdev));
3955 hci_dev_unlock(hdev);
3957 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_PHY_CONFIGURATION, 0,
3961 int mgmt_phy_configuration_changed(struct hci_dev *hdev, struct sock *skip)
3963 struct mgmt_ev_phy_configuration_changed ev;
3965 memset(&ev, 0, sizeof(ev));
3967 ev.selected_phys = cpu_to_le32(get_selected_phys(hdev));
3969 return mgmt_event(MGMT_EV_PHY_CONFIGURATION_CHANGED, hdev, &ev,
3973 static void set_default_phy_complete(struct hci_dev *hdev, void *data, int err)
3975 struct mgmt_pending_cmd *cmd = data;
3976 struct sk_buff *skb = cmd->skb;
3977 u8 status = mgmt_status(err);
3979 if (cmd != pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev))
3984 status = MGMT_STATUS_FAILED;
3985 else if (IS_ERR(skb))
3986 status = mgmt_status(PTR_ERR(skb));
3988 status = mgmt_status(skb->data[0]);
3991 bt_dev_dbg(hdev, "status %d", status);
3994 mgmt_cmd_status(cmd->sk, hdev->id,
3995 MGMT_OP_SET_PHY_CONFIGURATION, status);
3997 mgmt_cmd_complete(cmd->sk, hdev->id,
3998 MGMT_OP_SET_PHY_CONFIGURATION, 0,
4001 mgmt_phy_configuration_changed(hdev, cmd->sk);
4004 if (skb && !IS_ERR(skb))
4007 mgmt_pending_remove(cmd);
4010 static int set_default_phy_sync(struct hci_dev *hdev, void *data)
4012 struct mgmt_pending_cmd *cmd = data;
4013 struct mgmt_cp_set_phy_configuration *cp = cmd->param;
4014 struct hci_cp_le_set_default_phy cp_phy;
4015 u32 selected_phys = __le32_to_cpu(cp->selected_phys);
4017 memset(&cp_phy, 0, sizeof(cp_phy));
4019 if (!(selected_phys & MGMT_PHY_LE_TX_MASK))
4020 cp_phy.all_phys |= 0x01;
4022 if (!(selected_phys & MGMT_PHY_LE_RX_MASK))
4023 cp_phy.all_phys |= 0x02;
4025 if (selected_phys & MGMT_PHY_LE_1M_TX)
4026 cp_phy.tx_phys |= HCI_LE_SET_PHY_1M;
4028 if (selected_phys & MGMT_PHY_LE_2M_TX)
4029 cp_phy.tx_phys |= HCI_LE_SET_PHY_2M;
4031 if (selected_phys & MGMT_PHY_LE_CODED_TX)
4032 cp_phy.tx_phys |= HCI_LE_SET_PHY_CODED;
4034 if (selected_phys & MGMT_PHY_LE_1M_RX)
4035 cp_phy.rx_phys |= HCI_LE_SET_PHY_1M;
4037 if (selected_phys & MGMT_PHY_LE_2M_RX)
4038 cp_phy.rx_phys |= HCI_LE_SET_PHY_2M;
4040 if (selected_phys & MGMT_PHY_LE_CODED_RX)
4041 cp_phy.rx_phys |= HCI_LE_SET_PHY_CODED;
4043 cmd->skb = __hci_cmd_sync(hdev, HCI_OP_LE_SET_DEFAULT_PHY,
4044 sizeof(cp_phy), &cp_phy, HCI_CMD_TIMEOUT);
4049 static int set_phy_configuration(struct sock *sk, struct hci_dev *hdev,
4050 void *data, u16 len)
4052 struct mgmt_cp_set_phy_configuration *cp = data;
4053 struct mgmt_pending_cmd *cmd;
4054 u32 selected_phys, configurable_phys, supported_phys, unconfigure_phys;
4055 u16 pkt_type = (HCI_DH1 | HCI_DM1);
4056 bool changed = false;
4059 bt_dev_dbg(hdev, "sock %p", sk);
4061 configurable_phys = get_configurable_phys(hdev);
4062 supported_phys = get_supported_phys(hdev);
4063 selected_phys = __le32_to_cpu(cp->selected_phys);
4065 if (selected_phys & ~supported_phys)
4066 return mgmt_cmd_status(sk, hdev->id,
4067 MGMT_OP_SET_PHY_CONFIGURATION,
4068 MGMT_STATUS_INVALID_PARAMS);
4070 unconfigure_phys = supported_phys & ~configurable_phys;
4072 if ((selected_phys & unconfigure_phys) != unconfigure_phys)
4073 return mgmt_cmd_status(sk, hdev->id,
4074 MGMT_OP_SET_PHY_CONFIGURATION,
4075 MGMT_STATUS_INVALID_PARAMS);
4077 if (selected_phys == get_selected_phys(hdev))
4078 return mgmt_cmd_complete(sk, hdev->id,
4079 MGMT_OP_SET_PHY_CONFIGURATION,
4084 if (!hdev_is_powered(hdev)) {
4085 err = mgmt_cmd_status(sk, hdev->id,
4086 MGMT_OP_SET_PHY_CONFIGURATION,
4087 MGMT_STATUS_REJECTED);
4091 if (pending_find(MGMT_OP_SET_PHY_CONFIGURATION, hdev)) {
4092 err = mgmt_cmd_status(sk, hdev->id,
4093 MGMT_OP_SET_PHY_CONFIGURATION,
4098 if (selected_phys & MGMT_PHY_BR_1M_3SLOT)
4099 pkt_type |= (HCI_DH3 | HCI_DM3);
4101 pkt_type &= ~(HCI_DH3 | HCI_DM3);
4103 if (selected_phys & MGMT_PHY_BR_1M_5SLOT)
4104 pkt_type |= (HCI_DH5 | HCI_DM5);
4106 pkt_type &= ~(HCI_DH5 | HCI_DM5);
4108 if (selected_phys & MGMT_PHY_EDR_2M_1SLOT)
4109 pkt_type &= ~HCI_2DH1;
4111 pkt_type |= HCI_2DH1;
4113 if (selected_phys & MGMT_PHY_EDR_2M_3SLOT)
4114 pkt_type &= ~HCI_2DH3;
4116 pkt_type |= HCI_2DH3;
4118 if (selected_phys & MGMT_PHY_EDR_2M_5SLOT)
4119 pkt_type &= ~HCI_2DH5;
4121 pkt_type |= HCI_2DH5;
4123 if (selected_phys & MGMT_PHY_EDR_3M_1SLOT)
4124 pkt_type &= ~HCI_3DH1;
4126 pkt_type |= HCI_3DH1;
4128 if (selected_phys & MGMT_PHY_EDR_3M_3SLOT)
4129 pkt_type &= ~HCI_3DH3;
4131 pkt_type |= HCI_3DH3;
4133 if (selected_phys & MGMT_PHY_EDR_3M_5SLOT)
4134 pkt_type &= ~HCI_3DH5;
4136 pkt_type |= HCI_3DH5;
4138 if (pkt_type != hdev->pkt_type) {
4139 hdev->pkt_type = pkt_type;
4143 if ((selected_phys & MGMT_PHY_LE_MASK) ==
4144 (get_selected_phys(hdev) & MGMT_PHY_LE_MASK)) {
4146 mgmt_phy_configuration_changed(hdev, sk);
4148 err = mgmt_cmd_complete(sk, hdev->id,
4149 MGMT_OP_SET_PHY_CONFIGURATION,
4155 cmd = mgmt_pending_add(sk, MGMT_OP_SET_PHY_CONFIGURATION, hdev, data,
4160 err = hci_cmd_sync_queue(hdev, set_default_phy_sync, cmd,
4161 set_default_phy_complete);
4164 err = mgmt_cmd_status(sk, hdev->id,
4165 MGMT_OP_SET_PHY_CONFIGURATION,
4166 MGMT_STATUS_FAILED);
4169 mgmt_pending_remove(cmd);
4173 hci_dev_unlock(hdev);
4178 static int set_blocked_keys(struct sock *sk, struct hci_dev *hdev, void *data,
4181 int err = MGMT_STATUS_SUCCESS;
4182 struct mgmt_cp_set_blocked_keys *keys = data;
4183 const u16 max_key_count = ((U16_MAX - sizeof(*keys)) /
4184 sizeof(struct mgmt_blocked_key_info));
4185 u16 key_count, expected_len;
4188 bt_dev_dbg(hdev, "sock %p", sk);
4190 key_count = __le16_to_cpu(keys->key_count);
4191 if (key_count > max_key_count) {
4192 bt_dev_err(hdev, "too big key_count value %u", key_count);
4193 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
4194 MGMT_STATUS_INVALID_PARAMS);
4197 expected_len = struct_size(keys, keys, key_count);
4198 if (expected_len != len) {
4199 bt_dev_err(hdev, "expected %u bytes, got %u bytes",
4201 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
4202 MGMT_STATUS_INVALID_PARAMS);
4207 hci_blocked_keys_clear(hdev);
4209 for (i = 0; i < key_count; ++i) {
4210 struct blocked_key *b = kzalloc(sizeof(*b), GFP_KERNEL);
4213 err = MGMT_STATUS_NO_RESOURCES;
4217 b->type = keys->keys[i].type;
4218 memcpy(b->val, keys->keys[i].val, sizeof(b->val));
4219 list_add_rcu(&b->list, &hdev->blocked_keys);
4221 hci_dev_unlock(hdev);
4223 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_BLOCKED_KEYS,
4227 static int set_wideband_speech(struct sock *sk, struct hci_dev *hdev,
4228 void *data, u16 len)
4230 struct mgmt_mode *cp = data;
4232 bool changed = false;
4234 bt_dev_dbg(hdev, "sock %p", sk);
4236 if (!test_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks))
4237 return mgmt_cmd_status(sk, hdev->id,
4238 MGMT_OP_SET_WIDEBAND_SPEECH,
4239 MGMT_STATUS_NOT_SUPPORTED);
4241 if (cp->val != 0x00 && cp->val != 0x01)
4242 return mgmt_cmd_status(sk, hdev->id,
4243 MGMT_OP_SET_WIDEBAND_SPEECH,
4244 MGMT_STATUS_INVALID_PARAMS);
4248 if (hdev_is_powered(hdev) &&
4249 !!cp->val != hci_dev_test_flag(hdev,
4250 HCI_WIDEBAND_SPEECH_ENABLED)) {
4251 err = mgmt_cmd_status(sk, hdev->id,
4252 MGMT_OP_SET_WIDEBAND_SPEECH,
4253 MGMT_STATUS_REJECTED);
4258 changed = !hci_dev_test_and_set_flag(hdev,
4259 HCI_WIDEBAND_SPEECH_ENABLED);
4261 changed = hci_dev_test_and_clear_flag(hdev,
4262 HCI_WIDEBAND_SPEECH_ENABLED);
4264 err = send_settings_rsp(sk, MGMT_OP_SET_WIDEBAND_SPEECH, hdev);
4269 err = new_settings(hdev, sk);
4272 hci_dev_unlock(hdev);
4276 static int read_controller_cap(struct sock *sk, struct hci_dev *hdev,
4277 void *data, u16 data_len)
4280 struct mgmt_rp_read_controller_cap *rp = (void *)buf;
4283 u8 tx_power_range[2];
4285 bt_dev_dbg(hdev, "sock %p", sk);
4287 memset(&buf, 0, sizeof(buf));
4291 /* When the Read Simple Pairing Options command is supported, then
4292 * the remote public key validation is supported.
4294 * Alternatively, when Microsoft extensions are available, they can
4295 * indicate support for public key validation as well.
4297 if ((hdev->commands[41] & 0x08) || msft_curve_validity(hdev))
4298 flags |= 0x01; /* Remote public key validation (BR/EDR) */
4300 flags |= 0x02; /* Remote public key validation (LE) */
4302 /* When the Read Encryption Key Size command is supported, then the
4303 * encryption key size is enforced.
4305 if (hdev->commands[20] & 0x10)
4306 flags |= 0x04; /* Encryption key size enforcement (BR/EDR) */
4308 flags |= 0x08; /* Encryption key size enforcement (LE) */
4310 cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_SEC_FLAGS,
4313 /* When the Read Simple Pairing Options command is supported, then
4314 * also max encryption key size information is provided.
4316 if (hdev->commands[41] & 0x08)
4317 cap_len = eir_append_le16(rp->cap, cap_len,
4318 MGMT_CAP_MAX_ENC_KEY_SIZE,
4319 hdev->max_enc_key_size);
4321 cap_len = eir_append_le16(rp->cap, cap_len,
4322 MGMT_CAP_SMP_MAX_ENC_KEY_SIZE,
4323 SMP_MAX_ENC_KEY_SIZE);
4325 /* Append the min/max LE tx power parameters if we were able to fetch
4326 * it from the controller
4328 if (hdev->commands[38] & 0x80) {
4329 memcpy(&tx_power_range[0], &hdev->min_le_tx_power, 1);
4330 memcpy(&tx_power_range[1], &hdev->max_le_tx_power, 1);
4331 cap_len = eir_append_data(rp->cap, cap_len, MGMT_CAP_LE_TX_PWR,
4335 rp->cap_len = cpu_to_le16(cap_len);
4337 hci_dev_unlock(hdev);
4339 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_CONTROLLER_CAP, 0,
4340 rp, sizeof(*rp) + cap_len);
4343 #ifdef CONFIG_BT_FEATURE_DEBUG
4344 /* d4992530-b9ec-469f-ab01-6c481c47da1c */
4345 static const u8 debug_uuid[16] = {
4346 0x1c, 0xda, 0x47, 0x1c, 0x48, 0x6c, 0x01, 0xab,
4347 0x9f, 0x46, 0xec, 0xb9, 0x30, 0x25, 0x99, 0xd4,
4351 /* 330859bc-7506-492d-9370-9a6f0614037f */
4352 static const u8 quality_report_uuid[16] = {
4353 0x7f, 0x03, 0x14, 0x06, 0x6f, 0x9a, 0x70, 0x93,
4354 0x2d, 0x49, 0x06, 0x75, 0xbc, 0x59, 0x08, 0x33,
4357 /* a6695ace-ee7f-4fb9-881a-5fac66c629af */
4358 static const u8 offload_codecs_uuid[16] = {
4359 0xaf, 0x29, 0xc6, 0x66, 0xac, 0x5f, 0x1a, 0x88,
4360 0xb9, 0x4f, 0x7f, 0xee, 0xce, 0x5a, 0x69, 0xa6,
4363 /* 671b10b5-42c0-4696-9227-eb28d1b049d6 */
4364 static const u8 le_simultaneous_roles_uuid[16] = {
4365 0xd6, 0x49, 0xb0, 0xd1, 0x28, 0xeb, 0x27, 0x92,
4366 0x96, 0x46, 0xc0, 0x42, 0xb5, 0x10, 0x1b, 0x67,
4369 /* 15c0a148-c273-11ea-b3de-0242ac130004 */
4370 static const u8 rpa_resolution_uuid[16] = {
4371 0x04, 0x00, 0x13, 0xac, 0x42, 0x02, 0xde, 0xb3,
4372 0xea, 0x11, 0x73, 0xc2, 0x48, 0xa1, 0xc0, 0x15,
4375 /* 6fbaf188-05e0-496a-9885-d6ddfdb4e03e */
4376 static const u8 iso_socket_uuid[16] = {
4377 0x3e, 0xe0, 0xb4, 0xfd, 0xdd, 0xd6, 0x85, 0x98,
4378 0x6a, 0x49, 0xe0, 0x05, 0x88, 0xf1, 0xba, 0x6f,
4381 /* 2ce463d7-7a03-4d8d-bf05-5f24e8f36e76 */
4382 static const u8 mgmt_mesh_uuid[16] = {
4383 0x76, 0x6e, 0xf3, 0xe8, 0x24, 0x5f, 0x05, 0xbf,
4384 0x8d, 0x4d, 0x03, 0x7a, 0xd7, 0x63, 0xe4, 0x2c,
4387 static int read_exp_features_info(struct sock *sk, struct hci_dev *hdev,
4388 void *data, u16 data_len)
4390 struct mgmt_rp_read_exp_features_info *rp;
4396 bt_dev_dbg(hdev, "sock %p", sk);
4398 /* Enough space for 7 features */
4399 len = sizeof(*rp) + (sizeof(rp->features[0]) * 7);
4400 rp = kzalloc(len, GFP_KERNEL);
4404 #ifdef CONFIG_BT_FEATURE_DEBUG
4406 flags = bt_dbg_get() ? BIT(0) : 0;
4408 memcpy(rp->features[idx].uuid, debug_uuid, 16);
4409 rp->features[idx].flags = cpu_to_le32(flags);
4414 if (hdev && hci_dev_le_state_simultaneous(hdev)) {
4415 if (hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES))
4420 memcpy(rp->features[idx].uuid, le_simultaneous_roles_uuid, 16);
4421 rp->features[idx].flags = cpu_to_le32(flags);
4425 if (hdev && ll_privacy_capable(hdev)) {
4426 if (hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY))
4427 flags = BIT(0) | BIT(1);
4431 memcpy(rp->features[idx].uuid, rpa_resolution_uuid, 16);
4432 rp->features[idx].flags = cpu_to_le32(flags);
4436 if (hdev && (aosp_has_quality_report(hdev) ||
4437 hdev->set_quality_report)) {
4438 if (hci_dev_test_flag(hdev, HCI_QUALITY_REPORT))
4443 memcpy(rp->features[idx].uuid, quality_report_uuid, 16);
4444 rp->features[idx].flags = cpu_to_le32(flags);
4448 if (hdev && hdev->get_data_path_id) {
4449 if (hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED))
4454 memcpy(rp->features[idx].uuid, offload_codecs_uuid, 16);
4455 rp->features[idx].flags = cpu_to_le32(flags);
4459 if (IS_ENABLED(CONFIG_BT_LE)) {
4460 flags = iso_enabled() ? BIT(0) : 0;
4461 memcpy(rp->features[idx].uuid, iso_socket_uuid, 16);
4462 rp->features[idx].flags = cpu_to_le32(flags);
4466 if (hdev && lmp_le_capable(hdev)) {
4467 if (hci_dev_test_flag(hdev, HCI_MESH_EXPERIMENTAL))
4472 memcpy(rp->features[idx].uuid, mgmt_mesh_uuid, 16);
4473 rp->features[idx].flags = cpu_to_le32(flags);
4477 rp->feature_count = cpu_to_le16(idx);
4479 /* After reading the experimental features information, enable
4480 * the events to update client on any future change.
4482 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4484 status = mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
4485 MGMT_OP_READ_EXP_FEATURES_INFO,
4486 0, rp, sizeof(*rp) + (20 * idx));
4492 static int exp_ll_privacy_feature_changed(bool enabled, struct hci_dev *hdev,
4495 struct mgmt_ev_exp_feature_changed ev;
4497 memset(&ev, 0, sizeof(ev));
4498 memcpy(ev.uuid, rpa_resolution_uuid, 16);
4499 ev.flags = cpu_to_le32((enabled ? BIT(0) : 0) | BIT(1));
4501 // Do we need to be atomic with the conn_flags?
4502 if (enabled && privacy_mode_capable(hdev))
4503 hdev->conn_flags |= HCI_CONN_FLAG_DEVICE_PRIVACY;
4505 hdev->conn_flags &= ~HCI_CONN_FLAG_DEVICE_PRIVACY;
4507 return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev,
4509 HCI_MGMT_EXP_FEATURE_EVENTS, skip);
4513 static int exp_feature_changed(struct hci_dev *hdev, const u8 *uuid,
4514 bool enabled, struct sock *skip)
4516 struct mgmt_ev_exp_feature_changed ev;
4518 memset(&ev, 0, sizeof(ev));
4519 memcpy(ev.uuid, uuid, 16);
4520 ev.flags = cpu_to_le32(enabled ? BIT(0) : 0);
4522 return mgmt_limited_event(MGMT_EV_EXP_FEATURE_CHANGED, hdev,
4524 HCI_MGMT_EXP_FEATURE_EVENTS, skip);
4527 #define EXP_FEAT(_uuid, _set_func) \
4530 .set_func = _set_func, \
4533 /* The zero key uuid is special. Multiple exp features are set through it. */
4534 static int set_zero_key_func(struct sock *sk, struct hci_dev *hdev,
4535 struct mgmt_cp_set_exp_feature *cp, u16 data_len)
4537 struct mgmt_rp_set_exp_feature rp;
4539 memset(rp.uuid, 0, 16);
4540 rp.flags = cpu_to_le32(0);
4542 #ifdef CONFIG_BT_FEATURE_DEBUG
4544 bool changed = bt_dbg_get();
4549 exp_feature_changed(NULL, ZERO_KEY, false, sk);
4553 if (hdev && use_ll_privacy(hdev) && !hdev_is_powered(hdev)) {
4556 changed = hci_dev_test_and_clear_flag(hdev,
4557 HCI_ENABLE_LL_PRIVACY);
4559 exp_feature_changed(hdev, rpa_resolution_uuid, false,
4563 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4565 return mgmt_cmd_complete(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
4566 MGMT_OP_SET_EXP_FEATURE, 0,
4570 #ifdef CONFIG_BT_FEATURE_DEBUG
4571 static int set_debug_func(struct sock *sk, struct hci_dev *hdev,
4572 struct mgmt_cp_set_exp_feature *cp, u16 data_len)
4574 struct mgmt_rp_set_exp_feature rp;
4579 /* Command requires to use the non-controller index */
4581 return mgmt_cmd_status(sk, hdev->id,
4582 MGMT_OP_SET_EXP_FEATURE,
4583 MGMT_STATUS_INVALID_INDEX);
4585 /* Parameters are limited to a single octet */
4586 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4587 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4588 MGMT_OP_SET_EXP_FEATURE,
4589 MGMT_STATUS_INVALID_PARAMS);
4591 /* Only boolean on/off is supported */
4592 if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4593 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4594 MGMT_OP_SET_EXP_FEATURE,
4595 MGMT_STATUS_INVALID_PARAMS);
4597 val = !!cp->param[0];
4598 changed = val ? !bt_dbg_get() : bt_dbg_get();
4601 memcpy(rp.uuid, debug_uuid, 16);
4602 rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4604 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4606 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
4607 MGMT_OP_SET_EXP_FEATURE, 0,
4611 exp_feature_changed(hdev, debug_uuid, val, sk);
4617 static int set_mgmt_mesh_func(struct sock *sk, struct hci_dev *hdev,
4618 struct mgmt_cp_set_exp_feature *cp, u16 data_len)
4620 struct mgmt_rp_set_exp_feature rp;
4624 /* Command requires to use the controller index */
4626 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4627 MGMT_OP_SET_EXP_FEATURE,
4628 MGMT_STATUS_INVALID_INDEX);
4630 /* Changes can only be made when controller is powered down */
4631 if (hdev_is_powered(hdev))
4632 return mgmt_cmd_status(sk, hdev->id,
4633 MGMT_OP_SET_EXP_FEATURE,
4634 MGMT_STATUS_REJECTED);
4636 /* Parameters are limited to a single octet */
4637 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4638 return mgmt_cmd_status(sk, hdev->id,
4639 MGMT_OP_SET_EXP_FEATURE,
4640 MGMT_STATUS_INVALID_PARAMS);
4642 /* Only boolean on/off is supported */
4643 if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4644 return mgmt_cmd_status(sk, hdev->id,
4645 MGMT_OP_SET_EXP_FEATURE,
4646 MGMT_STATUS_INVALID_PARAMS);
4648 val = !!cp->param[0];
4651 changed = !hci_dev_test_and_set_flag(hdev,
4652 HCI_MESH_EXPERIMENTAL);
4654 hci_dev_clear_flag(hdev, HCI_MESH);
4655 changed = hci_dev_test_and_clear_flag(hdev,
4656 HCI_MESH_EXPERIMENTAL);
4659 memcpy(rp.uuid, mgmt_mesh_uuid, 16);
4660 rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4662 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4664 err = mgmt_cmd_complete(sk, hdev->id,
4665 MGMT_OP_SET_EXP_FEATURE, 0,
4669 exp_feature_changed(hdev, mgmt_mesh_uuid, val, sk);
4674 static int set_rpa_resolution_func(struct sock *sk, struct hci_dev *hdev,
4675 struct mgmt_cp_set_exp_feature *cp,
4678 struct mgmt_rp_set_exp_feature rp;
4683 /* Command requires to use the controller index */
4685 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4686 MGMT_OP_SET_EXP_FEATURE,
4687 MGMT_STATUS_INVALID_INDEX);
4689 /* Changes can only be made when controller is powered down */
4690 if (hdev_is_powered(hdev))
4691 return mgmt_cmd_status(sk, hdev->id,
4692 MGMT_OP_SET_EXP_FEATURE,
4693 MGMT_STATUS_REJECTED);
4695 /* Parameters are limited to a single octet */
4696 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4697 return mgmt_cmd_status(sk, hdev->id,
4698 MGMT_OP_SET_EXP_FEATURE,
4699 MGMT_STATUS_INVALID_PARAMS);
4701 /* Only boolean on/off is supported */
4702 if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4703 return mgmt_cmd_status(sk, hdev->id,
4704 MGMT_OP_SET_EXP_FEATURE,
4705 MGMT_STATUS_INVALID_PARAMS);
4707 val = !!cp->param[0];
4710 changed = !hci_dev_test_and_set_flag(hdev,
4711 HCI_ENABLE_LL_PRIVACY);
4712 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
4714 /* Enable LL privacy + supported settings changed */
4715 flags = BIT(0) | BIT(1);
4717 changed = hci_dev_test_and_clear_flag(hdev,
4718 HCI_ENABLE_LL_PRIVACY);
4720 /* Disable LL privacy + supported settings changed */
4724 memcpy(rp.uuid, rpa_resolution_uuid, 16);
4725 rp.flags = cpu_to_le32(flags);
4727 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4729 err = mgmt_cmd_complete(sk, hdev->id,
4730 MGMT_OP_SET_EXP_FEATURE, 0,
4734 exp_ll_privacy_feature_changed(val, hdev, sk);
4739 static int set_quality_report_func(struct sock *sk, struct hci_dev *hdev,
4740 struct mgmt_cp_set_exp_feature *cp,
4743 struct mgmt_rp_set_exp_feature rp;
4747 /* Command requires to use a valid controller index */
4749 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4750 MGMT_OP_SET_EXP_FEATURE,
4751 MGMT_STATUS_INVALID_INDEX);
4753 /* Parameters are limited to a single octet */
4754 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4755 return mgmt_cmd_status(sk, hdev->id,
4756 MGMT_OP_SET_EXP_FEATURE,
4757 MGMT_STATUS_INVALID_PARAMS);
4759 /* Only boolean on/off is supported */
4760 if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4761 return mgmt_cmd_status(sk, hdev->id,
4762 MGMT_OP_SET_EXP_FEATURE,
4763 MGMT_STATUS_INVALID_PARAMS);
4765 hci_req_sync_lock(hdev);
4767 val = !!cp->param[0];
4768 changed = (val != hci_dev_test_flag(hdev, HCI_QUALITY_REPORT));
4770 if (!aosp_has_quality_report(hdev) && !hdev->set_quality_report) {
4771 err = mgmt_cmd_status(sk, hdev->id,
4772 MGMT_OP_SET_EXP_FEATURE,
4773 MGMT_STATUS_NOT_SUPPORTED);
4774 goto unlock_quality_report;
4778 if (hdev->set_quality_report)
4779 err = hdev->set_quality_report(hdev, val);
4781 err = aosp_set_quality_report(hdev, val);
4784 err = mgmt_cmd_status(sk, hdev->id,
4785 MGMT_OP_SET_EXP_FEATURE,
4786 MGMT_STATUS_FAILED);
4787 goto unlock_quality_report;
4791 hci_dev_set_flag(hdev, HCI_QUALITY_REPORT);
4793 hci_dev_clear_flag(hdev, HCI_QUALITY_REPORT);
4796 bt_dev_dbg(hdev, "quality report enable %d changed %d", val, changed);
4798 memcpy(rp.uuid, quality_report_uuid, 16);
4799 rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4800 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4802 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_EXP_FEATURE, 0,
4806 exp_feature_changed(hdev, quality_report_uuid, val, sk);
4808 unlock_quality_report:
4809 hci_req_sync_unlock(hdev);
4813 static int set_offload_codec_func(struct sock *sk, struct hci_dev *hdev,
4814 struct mgmt_cp_set_exp_feature *cp,
4819 struct mgmt_rp_set_exp_feature rp;
4821 /* Command requires to use a valid controller index */
4823 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4824 MGMT_OP_SET_EXP_FEATURE,
4825 MGMT_STATUS_INVALID_INDEX);
4827 /* Parameters are limited to a single octet */
4828 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4829 return mgmt_cmd_status(sk, hdev->id,
4830 MGMT_OP_SET_EXP_FEATURE,
4831 MGMT_STATUS_INVALID_PARAMS);
4833 /* Only boolean on/off is supported */
4834 if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4835 return mgmt_cmd_status(sk, hdev->id,
4836 MGMT_OP_SET_EXP_FEATURE,
4837 MGMT_STATUS_INVALID_PARAMS);
4839 val = !!cp->param[0];
4840 changed = (val != hci_dev_test_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED));
4842 if (!hdev->get_data_path_id) {
4843 return mgmt_cmd_status(sk, hdev->id,
4844 MGMT_OP_SET_EXP_FEATURE,
4845 MGMT_STATUS_NOT_SUPPORTED);
4850 hci_dev_set_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED);
4852 hci_dev_clear_flag(hdev, HCI_OFFLOAD_CODECS_ENABLED);
4855 bt_dev_info(hdev, "offload codecs enable %d changed %d",
4858 memcpy(rp.uuid, offload_codecs_uuid, 16);
4859 rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4860 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4861 err = mgmt_cmd_complete(sk, hdev->id,
4862 MGMT_OP_SET_EXP_FEATURE, 0,
4866 exp_feature_changed(hdev, offload_codecs_uuid, val, sk);
4871 static int set_le_simultaneous_roles_func(struct sock *sk, struct hci_dev *hdev,
4872 struct mgmt_cp_set_exp_feature *cp,
4877 struct mgmt_rp_set_exp_feature rp;
4879 /* Command requires to use a valid controller index */
4881 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4882 MGMT_OP_SET_EXP_FEATURE,
4883 MGMT_STATUS_INVALID_INDEX);
4885 /* Parameters are limited to a single octet */
4886 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4887 return mgmt_cmd_status(sk, hdev->id,
4888 MGMT_OP_SET_EXP_FEATURE,
4889 MGMT_STATUS_INVALID_PARAMS);
4891 /* Only boolean on/off is supported */
4892 if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4893 return mgmt_cmd_status(sk, hdev->id,
4894 MGMT_OP_SET_EXP_FEATURE,
4895 MGMT_STATUS_INVALID_PARAMS);
4897 val = !!cp->param[0];
4898 changed = (val != hci_dev_test_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES));
4900 if (!hci_dev_le_state_simultaneous(hdev)) {
4901 return mgmt_cmd_status(sk, hdev->id,
4902 MGMT_OP_SET_EXP_FEATURE,
4903 MGMT_STATUS_NOT_SUPPORTED);
4908 hci_dev_set_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES);
4910 hci_dev_clear_flag(hdev, HCI_LE_SIMULTANEOUS_ROLES);
4913 bt_dev_info(hdev, "LE simultaneous roles enable %d changed %d",
4916 memcpy(rp.uuid, le_simultaneous_roles_uuid, 16);
4917 rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4918 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4919 err = mgmt_cmd_complete(sk, hdev->id,
4920 MGMT_OP_SET_EXP_FEATURE, 0,
4924 exp_feature_changed(hdev, le_simultaneous_roles_uuid, val, sk);
4930 static int set_iso_socket_func(struct sock *sk, struct hci_dev *hdev,
4931 struct mgmt_cp_set_exp_feature *cp, u16 data_len)
4933 struct mgmt_rp_set_exp_feature rp;
4934 bool val, changed = false;
4937 /* Command requires to use the non-controller index */
4939 return mgmt_cmd_status(sk, hdev->id,
4940 MGMT_OP_SET_EXP_FEATURE,
4941 MGMT_STATUS_INVALID_INDEX);
4943 /* Parameters are limited to a single octet */
4944 if (data_len != MGMT_SET_EXP_FEATURE_SIZE + 1)
4945 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4946 MGMT_OP_SET_EXP_FEATURE,
4947 MGMT_STATUS_INVALID_PARAMS);
4949 /* Only boolean on/off is supported */
4950 if (cp->param[0] != 0x00 && cp->param[0] != 0x01)
4951 return mgmt_cmd_status(sk, MGMT_INDEX_NONE,
4952 MGMT_OP_SET_EXP_FEATURE,
4953 MGMT_STATUS_INVALID_PARAMS);
4955 val = cp->param[0] ? true : false;
4964 memcpy(rp.uuid, iso_socket_uuid, 16);
4965 rp.flags = cpu_to_le32(val ? BIT(0) : 0);
4967 hci_sock_set_flag(sk, HCI_MGMT_EXP_FEATURE_EVENTS);
4969 err = mgmt_cmd_complete(sk, MGMT_INDEX_NONE,
4970 MGMT_OP_SET_EXP_FEATURE, 0,
4974 exp_feature_changed(hdev, iso_socket_uuid, val, sk);
4980 static const struct mgmt_exp_feature {
4982 int (*set_func)(struct sock *sk, struct hci_dev *hdev,
4983 struct mgmt_cp_set_exp_feature *cp, u16 data_len);
4984 } exp_features[] = {
4985 EXP_FEAT(ZERO_KEY, set_zero_key_func),
4986 #ifdef CONFIG_BT_FEATURE_DEBUG
4987 EXP_FEAT(debug_uuid, set_debug_func),
4989 EXP_FEAT(mgmt_mesh_uuid, set_mgmt_mesh_func),
4990 EXP_FEAT(rpa_resolution_uuid, set_rpa_resolution_func),
4991 EXP_FEAT(quality_report_uuid, set_quality_report_func),
4992 EXP_FEAT(offload_codecs_uuid, set_offload_codec_func),
4993 EXP_FEAT(le_simultaneous_roles_uuid, set_le_simultaneous_roles_func),
4995 EXP_FEAT(iso_socket_uuid, set_iso_socket_func),
4998 /* end with a null feature */
4999 EXP_FEAT(NULL, NULL)
5002 static int set_exp_feature(struct sock *sk, struct hci_dev *hdev,
5003 void *data, u16 data_len)
5005 struct mgmt_cp_set_exp_feature *cp = data;
5008 bt_dev_dbg(hdev, "sock %p", sk);
5010 for (i = 0; exp_features[i].uuid; i++) {
5011 if (!memcmp(cp->uuid, exp_features[i].uuid, 16))
5012 return exp_features[i].set_func(sk, hdev, cp, data_len);
5015 return mgmt_cmd_status(sk, hdev ? hdev->id : MGMT_INDEX_NONE,
5016 MGMT_OP_SET_EXP_FEATURE,
5017 MGMT_STATUS_NOT_SUPPORTED);
5020 static u32 get_params_flags(struct hci_dev *hdev,
5021 struct hci_conn_params *params)
5023 u32 flags = hdev->conn_flags;
5025 /* Devices using RPAs can only be programmed in the acceptlist if
5026 * LL Privacy has been enable otherwise they cannot mark
5027 * HCI_CONN_FLAG_REMOTE_WAKEUP.
5029 if ((flags & HCI_CONN_FLAG_REMOTE_WAKEUP) && !use_ll_privacy(hdev) &&
5030 hci_find_irk_by_addr(hdev, ¶ms->addr, params->addr_type))
5031 flags &= ~HCI_CONN_FLAG_REMOTE_WAKEUP;
5036 static int get_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
5039 struct mgmt_cp_get_device_flags *cp = data;
5040 struct mgmt_rp_get_device_flags rp;
5041 struct bdaddr_list_with_flags *br_params;
5042 struct hci_conn_params *params;
5043 u32 supported_flags;
5044 u32 current_flags = 0;
5045 u8 status = MGMT_STATUS_INVALID_PARAMS;
5047 bt_dev_dbg(hdev, "Get device flags %pMR (type 0x%x)\n",
5048 &cp->addr.bdaddr, cp->addr.type);
5052 supported_flags = hdev->conn_flags;
5054 memset(&rp, 0, sizeof(rp));
5056 if (cp->addr.type == BDADDR_BREDR) {
5057 br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
5063 current_flags = br_params->flags;
5065 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
5066 le_addr_type(cp->addr.type));
5070 supported_flags = get_params_flags(hdev, params);
5071 current_flags = params->flags;
5074 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
5075 rp.addr.type = cp->addr.type;
5076 rp.supported_flags = cpu_to_le32(supported_flags);
5077 rp.current_flags = cpu_to_le32(current_flags);
5079 status = MGMT_STATUS_SUCCESS;
5082 hci_dev_unlock(hdev);
5084 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_DEVICE_FLAGS, status,
5088 static void device_flags_changed(struct sock *sk, struct hci_dev *hdev,
5089 bdaddr_t *bdaddr, u8 bdaddr_type,
5090 u32 supported_flags, u32 current_flags)
5092 struct mgmt_ev_device_flags_changed ev;
5094 bacpy(&ev.addr.bdaddr, bdaddr);
5095 ev.addr.type = bdaddr_type;
5096 ev.supported_flags = cpu_to_le32(supported_flags);
5097 ev.current_flags = cpu_to_le32(current_flags);
5099 mgmt_event(MGMT_EV_DEVICE_FLAGS_CHANGED, hdev, &ev, sizeof(ev), sk);
5102 static int set_device_flags(struct sock *sk, struct hci_dev *hdev, void *data,
5105 struct mgmt_cp_set_device_flags *cp = data;
5106 struct bdaddr_list_with_flags *br_params;
5107 struct hci_conn_params *params;
5108 u8 status = MGMT_STATUS_INVALID_PARAMS;
5109 u32 supported_flags;
5110 u32 current_flags = __le32_to_cpu(cp->current_flags);
5112 bt_dev_dbg(hdev, "Set device flags %pMR (type 0x%x) = 0x%x",
5113 &cp->addr.bdaddr, cp->addr.type, current_flags);
5115 // We should take hci_dev_lock() early, I think.. conn_flags can change
5116 supported_flags = hdev->conn_flags;
5118 if ((supported_flags | current_flags) != supported_flags) {
5119 bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)",
5120 current_flags, supported_flags);
5126 if (cp->addr.type == BDADDR_BREDR) {
5127 br_params = hci_bdaddr_list_lookup_with_flags(&hdev->accept_list,
5132 br_params->flags = current_flags;
5133 status = MGMT_STATUS_SUCCESS;
5135 bt_dev_warn(hdev, "No such BR/EDR device %pMR (0x%x)",
5136 &cp->addr.bdaddr, cp->addr.type);
5142 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
5143 le_addr_type(cp->addr.type));
5145 bt_dev_warn(hdev, "No such LE device %pMR (0x%x)",
5146 &cp->addr.bdaddr, le_addr_type(cp->addr.type));
5150 supported_flags = get_params_flags(hdev, params);
5152 if ((supported_flags | current_flags) != supported_flags) {
5153 bt_dev_warn(hdev, "Bad flag given (0x%x) vs supported (0x%0x)",
5154 current_flags, supported_flags);
5158 params->flags = current_flags;
5159 status = MGMT_STATUS_SUCCESS;
5161 /* Update passive scan if HCI_CONN_FLAG_DEVICE_PRIVACY
5164 if (params->flags & HCI_CONN_FLAG_DEVICE_PRIVACY)
5165 hci_update_passive_scan(hdev);
5168 hci_dev_unlock(hdev);
5171 if (status == MGMT_STATUS_SUCCESS)
5172 device_flags_changed(sk, hdev, &cp->addr.bdaddr, cp->addr.type,
5173 supported_flags, current_flags);
5175 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_FLAGS, status,
5176 &cp->addr, sizeof(cp->addr));
5179 static void mgmt_adv_monitor_added(struct sock *sk, struct hci_dev *hdev,
5182 struct mgmt_ev_adv_monitor_added ev;
5184 ev.monitor_handle = cpu_to_le16(handle);
5186 mgmt_event(MGMT_EV_ADV_MONITOR_ADDED, hdev, &ev, sizeof(ev), sk);
5189 void mgmt_adv_monitor_removed(struct hci_dev *hdev, u16 handle)
5191 struct mgmt_ev_adv_monitor_removed ev;
5192 struct mgmt_pending_cmd *cmd;
5193 struct sock *sk_skip = NULL;
5194 struct mgmt_cp_remove_adv_monitor *cp;
5196 cmd = pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev);
5200 if (cp->monitor_handle)
5204 ev.monitor_handle = cpu_to_le16(handle);
5206 mgmt_event(MGMT_EV_ADV_MONITOR_REMOVED, hdev, &ev, sizeof(ev), sk_skip);
5209 static int read_adv_mon_features(struct sock *sk, struct hci_dev *hdev,
5210 void *data, u16 len)
5212 struct adv_monitor *monitor = NULL;
5213 struct mgmt_rp_read_adv_monitor_features *rp = NULL;
5216 __u32 supported = 0;
5218 __u16 num_handles = 0;
5219 __u16 handles[HCI_MAX_ADV_MONITOR_NUM_HANDLES];
5221 BT_DBG("request for %s", hdev->name);
5225 if (msft_monitor_supported(hdev))
5226 supported |= MGMT_ADV_MONITOR_FEATURE_MASK_OR_PATTERNS;
5228 idr_for_each_entry(&hdev->adv_monitors_idr, monitor, handle)
5229 handles[num_handles++] = monitor->handle;
5231 hci_dev_unlock(hdev);
5233 rp_size = sizeof(*rp) + (num_handles * sizeof(u16));
5234 rp = kmalloc(rp_size, GFP_KERNEL);
5238 /* All supported features are currently enabled */
5239 enabled = supported;
5241 rp->supported_features = cpu_to_le32(supported);
5242 rp->enabled_features = cpu_to_le32(enabled);
5243 rp->max_num_handles = cpu_to_le16(HCI_MAX_ADV_MONITOR_NUM_HANDLES);
5244 rp->max_num_patterns = HCI_MAX_ADV_MONITOR_NUM_PATTERNS;
5245 rp->num_handles = cpu_to_le16(num_handles);
5247 memcpy(&rp->handles, &handles, (num_handles * sizeof(u16)));
5249 err = mgmt_cmd_complete(sk, hdev->id,
5250 MGMT_OP_READ_ADV_MONITOR_FEATURES,
5251 MGMT_STATUS_SUCCESS, rp, rp_size);
5258 static void mgmt_add_adv_patterns_monitor_complete(struct hci_dev *hdev,
5259 void *data, int status)
5261 struct mgmt_rp_add_adv_patterns_monitor rp;
5262 struct mgmt_pending_cmd *cmd = data;
5263 struct adv_monitor *monitor = cmd->user_data;
5267 rp.monitor_handle = cpu_to_le16(monitor->handle);
5270 mgmt_adv_monitor_added(cmd->sk, hdev, monitor->handle);
5271 hdev->adv_monitors_cnt++;
5272 if (monitor->state == ADV_MONITOR_STATE_NOT_REGISTERED)
5273 monitor->state = ADV_MONITOR_STATE_REGISTERED;
5274 hci_update_passive_scan(hdev);
5277 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
5278 mgmt_status(status), &rp, sizeof(rp));
5279 mgmt_pending_remove(cmd);
5281 hci_dev_unlock(hdev);
5282 bt_dev_dbg(hdev, "add monitor %d complete, status %d",
5283 rp.monitor_handle, status);
5286 static int mgmt_add_adv_patterns_monitor_sync(struct hci_dev *hdev, void *data)
5288 struct mgmt_pending_cmd *cmd = data;
5289 struct adv_monitor *monitor = cmd->user_data;
5291 return hci_add_adv_monitor(hdev, monitor);
5294 static int __add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev,
5295 struct adv_monitor *m, u8 status,
5296 void *data, u16 len, u16 op)
5298 struct mgmt_pending_cmd *cmd;
5306 if (pending_find(MGMT_OP_SET_LE, hdev) ||
5307 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) ||
5308 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev) ||
5309 pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev)) {
5310 status = MGMT_STATUS_BUSY;
5314 cmd = mgmt_pending_add(sk, op, hdev, data, len);
5316 status = MGMT_STATUS_NO_RESOURCES;
5321 err = hci_cmd_sync_queue(hdev, mgmt_add_adv_patterns_monitor_sync, cmd,
5322 mgmt_add_adv_patterns_monitor_complete);
5325 status = MGMT_STATUS_NO_RESOURCES;
5327 status = MGMT_STATUS_FAILED;
5332 hci_dev_unlock(hdev);
5337 hci_free_adv_monitor(hdev, m);
5338 hci_dev_unlock(hdev);
5339 return mgmt_cmd_status(sk, hdev->id, op, status);
5342 static void parse_adv_monitor_rssi(struct adv_monitor *m,
5343 struct mgmt_adv_rssi_thresholds *rssi)
5346 m->rssi.low_threshold = rssi->low_threshold;
5347 m->rssi.low_threshold_timeout =
5348 __le16_to_cpu(rssi->low_threshold_timeout);
5349 m->rssi.high_threshold = rssi->high_threshold;
5350 m->rssi.high_threshold_timeout =
5351 __le16_to_cpu(rssi->high_threshold_timeout);
5352 m->rssi.sampling_period = rssi->sampling_period;
5354 /* Default values. These numbers are the least constricting
5355 * parameters for MSFT API to work, so it behaves as if there
5356 * are no rssi parameter to consider. May need to be changed
5357 * if other API are to be supported.
5359 m->rssi.low_threshold = -127;
5360 m->rssi.low_threshold_timeout = 60;
5361 m->rssi.high_threshold = -127;
5362 m->rssi.high_threshold_timeout = 0;
5363 m->rssi.sampling_period = 0;
5367 static u8 parse_adv_monitor_pattern(struct adv_monitor *m, u8 pattern_count,
5368 struct mgmt_adv_pattern *patterns)
5370 u8 offset = 0, length = 0;
5371 struct adv_pattern *p = NULL;
5374 for (i = 0; i < pattern_count; i++) {
5375 offset = patterns[i].offset;
5376 length = patterns[i].length;
5377 if (offset >= HCI_MAX_AD_LENGTH ||
5378 length > HCI_MAX_AD_LENGTH ||
5379 (offset + length) > HCI_MAX_AD_LENGTH)
5380 return MGMT_STATUS_INVALID_PARAMS;
5382 p = kmalloc(sizeof(*p), GFP_KERNEL);
5384 return MGMT_STATUS_NO_RESOURCES;
5386 p->ad_type = patterns[i].ad_type;
5387 p->offset = patterns[i].offset;
5388 p->length = patterns[i].length;
5389 memcpy(p->value, patterns[i].value, p->length);
5391 INIT_LIST_HEAD(&p->list);
5392 list_add(&p->list, &m->patterns);
5395 return MGMT_STATUS_SUCCESS;
5398 static int add_adv_patterns_monitor(struct sock *sk, struct hci_dev *hdev,
5399 void *data, u16 len)
5401 struct mgmt_cp_add_adv_patterns_monitor *cp = data;
5402 struct adv_monitor *m = NULL;
5403 u8 status = MGMT_STATUS_SUCCESS;
5404 size_t expected_size = sizeof(*cp);
5406 BT_DBG("request for %s", hdev->name);
5408 if (len <= sizeof(*cp)) {
5409 status = MGMT_STATUS_INVALID_PARAMS;
5413 expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern);
5414 if (len != expected_size) {
5415 status = MGMT_STATUS_INVALID_PARAMS;
5419 m = kzalloc(sizeof(*m), GFP_KERNEL);
5421 status = MGMT_STATUS_NO_RESOURCES;
5425 INIT_LIST_HEAD(&m->patterns);
5427 parse_adv_monitor_rssi(m, NULL);
5428 status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns);
5431 return __add_adv_patterns_monitor(sk, hdev, m, status, data, len,
5432 MGMT_OP_ADD_ADV_PATTERNS_MONITOR);
5435 static int add_adv_patterns_monitor_rssi(struct sock *sk, struct hci_dev *hdev,
5436 void *data, u16 len)
5438 struct mgmt_cp_add_adv_patterns_monitor_rssi *cp = data;
5439 struct adv_monitor *m = NULL;
5440 u8 status = MGMT_STATUS_SUCCESS;
5441 size_t expected_size = sizeof(*cp);
5443 BT_DBG("request for %s", hdev->name);
5445 if (len <= sizeof(*cp)) {
5446 status = MGMT_STATUS_INVALID_PARAMS;
5450 expected_size += cp->pattern_count * sizeof(struct mgmt_adv_pattern);
5451 if (len != expected_size) {
5452 status = MGMT_STATUS_INVALID_PARAMS;
5456 m = kzalloc(sizeof(*m), GFP_KERNEL);
5458 status = MGMT_STATUS_NO_RESOURCES;
5462 INIT_LIST_HEAD(&m->patterns);
5464 parse_adv_monitor_rssi(m, &cp->rssi);
5465 status = parse_adv_monitor_pattern(m, cp->pattern_count, cp->patterns);
5468 return __add_adv_patterns_monitor(sk, hdev, m, status, data, len,
5469 MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI);
5472 static void mgmt_remove_adv_monitor_complete(struct hci_dev *hdev,
5473 void *data, int status)
5475 struct mgmt_rp_remove_adv_monitor rp;
5476 struct mgmt_pending_cmd *cmd = data;
5477 struct mgmt_cp_remove_adv_monitor *cp = cmd->param;
5481 rp.monitor_handle = cp->monitor_handle;
5484 hci_update_passive_scan(hdev);
5486 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
5487 mgmt_status(status), &rp, sizeof(rp));
5488 mgmt_pending_remove(cmd);
5490 hci_dev_unlock(hdev);
5491 bt_dev_dbg(hdev, "remove monitor %d complete, status %d",
5492 rp.monitor_handle, status);
5495 static int mgmt_remove_adv_monitor_sync(struct hci_dev *hdev, void *data)
5497 struct mgmt_pending_cmd *cmd = data;
5498 struct mgmt_cp_remove_adv_monitor *cp = cmd->param;
5499 u16 handle = __le16_to_cpu(cp->monitor_handle);
5502 return hci_remove_all_adv_monitor(hdev);
5504 return hci_remove_single_adv_monitor(hdev, handle);
5507 static int remove_adv_monitor(struct sock *sk, struct hci_dev *hdev,
5508 void *data, u16 len)
5510 struct mgmt_pending_cmd *cmd;
5515 if (pending_find(MGMT_OP_SET_LE, hdev) ||
5516 pending_find(MGMT_OP_REMOVE_ADV_MONITOR, hdev) ||
5517 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR, hdev) ||
5518 pending_find(MGMT_OP_ADD_ADV_PATTERNS_MONITOR_RSSI, hdev)) {
5519 status = MGMT_STATUS_BUSY;
5523 cmd = mgmt_pending_add(sk, MGMT_OP_REMOVE_ADV_MONITOR, hdev, data, len);
5525 status = MGMT_STATUS_NO_RESOURCES;
5529 err = hci_cmd_sync_queue(hdev, mgmt_remove_adv_monitor_sync, cmd,
5530 mgmt_remove_adv_monitor_complete);
5533 mgmt_pending_remove(cmd);
5536 status = MGMT_STATUS_NO_RESOURCES;
5538 status = MGMT_STATUS_FAILED;
5543 hci_dev_unlock(hdev);
5548 hci_dev_unlock(hdev);
5549 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADV_MONITOR,
5553 static void read_local_oob_data_complete(struct hci_dev *hdev, void *data, int err)
5555 struct mgmt_rp_read_local_oob_data mgmt_rp;
5556 size_t rp_size = sizeof(mgmt_rp);
5557 struct mgmt_pending_cmd *cmd = data;
5558 struct sk_buff *skb = cmd->skb;
5559 u8 status = mgmt_status(err);
5563 status = MGMT_STATUS_FAILED;
5564 else if (IS_ERR(skb))
5565 status = mgmt_status(PTR_ERR(skb));
5567 status = mgmt_status(skb->data[0]);
5570 bt_dev_dbg(hdev, "status %d", status);
5573 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA, status);
5577 memset(&mgmt_rp, 0, sizeof(mgmt_rp));
5579 if (!bredr_sc_enabled(hdev)) {
5580 struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
5582 if (skb->len < sizeof(*rp)) {
5583 mgmt_cmd_status(cmd->sk, hdev->id,
5584 MGMT_OP_READ_LOCAL_OOB_DATA,
5585 MGMT_STATUS_FAILED);
5589 memcpy(mgmt_rp.hash192, rp->hash, sizeof(rp->hash));
5590 memcpy(mgmt_rp.rand192, rp->rand, sizeof(rp->rand));
5592 rp_size -= sizeof(mgmt_rp.hash256) + sizeof(mgmt_rp.rand256);
5594 struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
5596 if (skb->len < sizeof(*rp)) {
5597 mgmt_cmd_status(cmd->sk, hdev->id,
5598 MGMT_OP_READ_LOCAL_OOB_DATA,
5599 MGMT_STATUS_FAILED);
5603 memcpy(mgmt_rp.hash192, rp->hash192, sizeof(rp->hash192));
5604 memcpy(mgmt_rp.rand192, rp->rand192, sizeof(rp->rand192));
5606 memcpy(mgmt_rp.hash256, rp->hash256, sizeof(rp->hash256));
5607 memcpy(mgmt_rp.rand256, rp->rand256, sizeof(rp->rand256));
5610 mgmt_cmd_complete(cmd->sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
5611 MGMT_STATUS_SUCCESS, &mgmt_rp, rp_size);
5614 if (skb && !IS_ERR(skb))
5617 mgmt_pending_free(cmd);
5620 static int read_local_oob_data_sync(struct hci_dev *hdev, void *data)
5622 struct mgmt_pending_cmd *cmd = data;
5624 if (bredr_sc_enabled(hdev))
5625 cmd->skb = hci_read_local_oob_data_sync(hdev, true, cmd->sk);
5627 cmd->skb = hci_read_local_oob_data_sync(hdev, false, cmd->sk);
5629 if (IS_ERR(cmd->skb))
5630 return PTR_ERR(cmd->skb);
5635 static int read_local_oob_data(struct sock *sk, struct hci_dev *hdev,
5636 void *data, u16 data_len)
5638 struct mgmt_pending_cmd *cmd;
5641 bt_dev_dbg(hdev, "sock %p", sk);
5645 if (!hdev_is_powered(hdev)) {
5646 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
5647 MGMT_STATUS_NOT_POWERED);
5651 if (!lmp_ssp_capable(hdev)) {
5652 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
5653 MGMT_STATUS_NOT_SUPPORTED);
5657 cmd = mgmt_pending_new(sk, MGMT_OP_READ_LOCAL_OOB_DATA, hdev, NULL, 0);
5661 err = hci_cmd_sync_queue(hdev, read_local_oob_data_sync, cmd,
5662 read_local_oob_data_complete);
5665 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_DATA,
5666 MGMT_STATUS_FAILED);
5669 mgmt_pending_free(cmd);
5673 hci_dev_unlock(hdev);
5677 static int add_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
5678 void *data, u16 len)
5680 struct mgmt_addr_info *addr = data;
5683 bt_dev_dbg(hdev, "sock %p", sk);
5685 if (!bdaddr_type_is_valid(addr->type))
5686 return mgmt_cmd_complete(sk, hdev->id,
5687 MGMT_OP_ADD_REMOTE_OOB_DATA,
5688 MGMT_STATUS_INVALID_PARAMS,
5689 addr, sizeof(*addr));
5693 if (len == MGMT_ADD_REMOTE_OOB_DATA_SIZE) {
5694 struct mgmt_cp_add_remote_oob_data *cp = data;
5697 if (cp->addr.type != BDADDR_BREDR) {
5698 err = mgmt_cmd_complete(sk, hdev->id,
5699 MGMT_OP_ADD_REMOTE_OOB_DATA,
5700 MGMT_STATUS_INVALID_PARAMS,
5701 &cp->addr, sizeof(cp->addr));
5705 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
5706 cp->addr.type, cp->hash,
5707 cp->rand, NULL, NULL);
5709 status = MGMT_STATUS_FAILED;
5711 status = MGMT_STATUS_SUCCESS;
5713 err = mgmt_cmd_complete(sk, hdev->id,
5714 MGMT_OP_ADD_REMOTE_OOB_DATA, status,
5715 &cp->addr, sizeof(cp->addr));
5716 } else if (len == MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE) {
5717 struct mgmt_cp_add_remote_oob_ext_data *cp = data;
5718 u8 *rand192, *hash192, *rand256, *hash256;
5721 if (bdaddr_type_is_le(cp->addr.type)) {
5722 /* Enforce zero-valued 192-bit parameters as
5723 * long as legacy SMP OOB isn't implemented.
5725 if (memcmp(cp->rand192, ZERO_KEY, 16) ||
5726 memcmp(cp->hash192, ZERO_KEY, 16)) {
5727 err = mgmt_cmd_complete(sk, hdev->id,
5728 MGMT_OP_ADD_REMOTE_OOB_DATA,
5729 MGMT_STATUS_INVALID_PARAMS,
5730 addr, sizeof(*addr));
5737 /* In case one of the P-192 values is set to zero,
5738 * then just disable OOB data for P-192.
5740 if (!memcmp(cp->rand192, ZERO_KEY, 16) ||
5741 !memcmp(cp->hash192, ZERO_KEY, 16)) {
5745 rand192 = cp->rand192;
5746 hash192 = cp->hash192;
5750 /* In case one of the P-256 values is set to zero, then just
5751 * disable OOB data for P-256.
5753 if (!memcmp(cp->rand256, ZERO_KEY, 16) ||
5754 !memcmp(cp->hash256, ZERO_KEY, 16)) {
5758 rand256 = cp->rand256;
5759 hash256 = cp->hash256;
5762 err = hci_add_remote_oob_data(hdev, &cp->addr.bdaddr,
5763 cp->addr.type, hash192, rand192,
5766 status = MGMT_STATUS_FAILED;
5768 status = MGMT_STATUS_SUCCESS;
5770 err = mgmt_cmd_complete(sk, hdev->id,
5771 MGMT_OP_ADD_REMOTE_OOB_DATA,
5772 status, &cp->addr, sizeof(cp->addr));
5774 bt_dev_err(hdev, "add_remote_oob_data: invalid len of %u bytes",
5776 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_REMOTE_OOB_DATA,
5777 MGMT_STATUS_INVALID_PARAMS);
5781 hci_dev_unlock(hdev);
5785 static int remove_remote_oob_data(struct sock *sk, struct hci_dev *hdev,
5786 void *data, u16 len)
5788 struct mgmt_cp_remove_remote_oob_data *cp = data;
5792 bt_dev_dbg(hdev, "sock %p", sk);
5794 if (cp->addr.type != BDADDR_BREDR)
5795 return mgmt_cmd_complete(sk, hdev->id,
5796 MGMT_OP_REMOVE_REMOTE_OOB_DATA,
5797 MGMT_STATUS_INVALID_PARAMS,
5798 &cp->addr, sizeof(cp->addr));
5802 if (!bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
5803 hci_remote_oob_data_clear(hdev);
5804 status = MGMT_STATUS_SUCCESS;
5808 err = hci_remove_remote_oob_data(hdev, &cp->addr.bdaddr, cp->addr.type);
5810 status = MGMT_STATUS_INVALID_PARAMS;
5812 status = MGMT_STATUS_SUCCESS;
5815 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_REMOTE_OOB_DATA,
5816 status, &cp->addr, sizeof(cp->addr));
5818 hci_dev_unlock(hdev);
5822 void mgmt_start_discovery_complete(struct hci_dev *hdev, u8 status)
5824 struct mgmt_pending_cmd *cmd;
5826 bt_dev_dbg(hdev, "status %u", status);
5830 cmd = pending_find(MGMT_OP_START_DISCOVERY, hdev);
5832 cmd = pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev);
5835 cmd = pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev);
5838 cmd->cmd_complete(cmd, mgmt_status(status));
5839 mgmt_pending_remove(cmd);
5842 hci_dev_unlock(hdev);
5845 static bool discovery_type_is_valid(struct hci_dev *hdev, uint8_t type,
5846 uint8_t *mgmt_status)
5849 case DISCOV_TYPE_LE:
5850 *mgmt_status = mgmt_le_support(hdev);
5854 case DISCOV_TYPE_INTERLEAVED:
5855 *mgmt_status = mgmt_le_support(hdev);
5859 case DISCOV_TYPE_BREDR:
5860 *mgmt_status = mgmt_bredr_support(hdev);
5865 *mgmt_status = MGMT_STATUS_INVALID_PARAMS;
5872 static void start_discovery_complete(struct hci_dev *hdev, void *data, int err)
5874 struct mgmt_pending_cmd *cmd = data;
5876 if (cmd != pending_find(MGMT_OP_START_DISCOVERY, hdev) &&
5877 cmd != pending_find(MGMT_OP_START_LIMITED_DISCOVERY, hdev) &&
5878 cmd != pending_find(MGMT_OP_START_SERVICE_DISCOVERY, hdev))
5881 bt_dev_dbg(hdev, "err %d", err);
5883 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(err),
5885 mgmt_pending_remove(cmd);
5887 hci_discovery_set_state(hdev, err ? DISCOVERY_STOPPED:
5891 static int start_discovery_sync(struct hci_dev *hdev, void *data)
5893 return hci_start_discovery_sync(hdev);
5896 static int start_discovery_internal(struct sock *sk, struct hci_dev *hdev,
5897 u16 op, void *data, u16 len)
5899 struct mgmt_cp_start_discovery *cp = data;
5900 struct mgmt_pending_cmd *cmd;
5904 bt_dev_dbg(hdev, "sock %p", sk);
5908 if (!hdev_is_powered(hdev)) {
5909 err = mgmt_cmd_complete(sk, hdev->id, op,
5910 MGMT_STATUS_NOT_POWERED,
5911 &cp->type, sizeof(cp->type));
5915 if (hdev->discovery.state != DISCOVERY_STOPPED ||
5916 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
5917 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
5918 &cp->type, sizeof(cp->type));
5922 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
5923 err = mgmt_cmd_complete(sk, hdev->id, op, status,
5924 &cp->type, sizeof(cp->type));
5928 /* Can't start discovery when it is paused */
5929 if (hdev->discovery_paused) {
5930 err = mgmt_cmd_complete(sk, hdev->id, op, MGMT_STATUS_BUSY,
5931 &cp->type, sizeof(cp->type));
5935 /* Clear the discovery filter first to free any previously
5936 * allocated memory for the UUID list.
5938 hci_discovery_filter_clear(hdev);
5940 hdev->discovery.type = cp->type;
5941 hdev->discovery.report_invalid_rssi = false;
5942 if (op == MGMT_OP_START_LIMITED_DISCOVERY)
5943 hdev->discovery.limited = true;
5945 hdev->discovery.limited = false;
5947 cmd = mgmt_pending_add(sk, op, hdev, data, len);
5953 err = hci_cmd_sync_queue(hdev, start_discovery_sync, cmd,
5954 start_discovery_complete);
5956 mgmt_pending_remove(cmd);
5960 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
5963 hci_dev_unlock(hdev);
5967 static int start_discovery(struct sock *sk, struct hci_dev *hdev,
5968 void *data, u16 len)
5970 return start_discovery_internal(sk, hdev, MGMT_OP_START_DISCOVERY,
5974 static int start_limited_discovery(struct sock *sk, struct hci_dev *hdev,
5975 void *data, u16 len)
5977 return start_discovery_internal(sk, hdev,
5978 MGMT_OP_START_LIMITED_DISCOVERY,
5982 static int start_service_discovery(struct sock *sk, struct hci_dev *hdev,
5983 void *data, u16 len)
5985 struct mgmt_cp_start_service_discovery *cp = data;
5986 struct mgmt_pending_cmd *cmd;
5987 const u16 max_uuid_count = ((U16_MAX - sizeof(*cp)) / 16);
5988 u16 uuid_count, expected_len;
5992 bt_dev_dbg(hdev, "sock %p", sk);
5996 if (!hdev_is_powered(hdev)) {
5997 err = mgmt_cmd_complete(sk, hdev->id,
5998 MGMT_OP_START_SERVICE_DISCOVERY,
5999 MGMT_STATUS_NOT_POWERED,
6000 &cp->type, sizeof(cp->type));
6004 if (hdev->discovery.state != DISCOVERY_STOPPED ||
6005 hci_dev_test_flag(hdev, HCI_PERIODIC_INQ)) {
6006 err = mgmt_cmd_complete(sk, hdev->id,
6007 MGMT_OP_START_SERVICE_DISCOVERY,
6008 MGMT_STATUS_BUSY, &cp->type,
6013 if (hdev->discovery_paused) {
6014 err = mgmt_cmd_complete(sk, hdev->id,
6015 MGMT_OP_START_SERVICE_DISCOVERY,
6016 MGMT_STATUS_BUSY, &cp->type,
6021 uuid_count = __le16_to_cpu(cp->uuid_count);
6022 if (uuid_count > max_uuid_count) {
6023 bt_dev_err(hdev, "service_discovery: too big uuid_count value %u",
6025 err = mgmt_cmd_complete(sk, hdev->id,
6026 MGMT_OP_START_SERVICE_DISCOVERY,
6027 MGMT_STATUS_INVALID_PARAMS, &cp->type,
6032 expected_len = sizeof(*cp) + uuid_count * 16;
6033 if (expected_len != len) {
6034 bt_dev_err(hdev, "service_discovery: expected %u bytes, got %u bytes",
6036 err = mgmt_cmd_complete(sk, hdev->id,
6037 MGMT_OP_START_SERVICE_DISCOVERY,
6038 MGMT_STATUS_INVALID_PARAMS, &cp->type,
6043 if (!discovery_type_is_valid(hdev, cp->type, &status)) {
6044 err = mgmt_cmd_complete(sk, hdev->id,
6045 MGMT_OP_START_SERVICE_DISCOVERY,
6046 status, &cp->type, sizeof(cp->type));
6050 cmd = mgmt_pending_add(sk, MGMT_OP_START_SERVICE_DISCOVERY,
6057 /* Clear the discovery filter first to free any previously
6058 * allocated memory for the UUID list.
6060 hci_discovery_filter_clear(hdev);
6062 hdev->discovery.result_filtering = true;
6063 hdev->discovery.type = cp->type;
6064 hdev->discovery.rssi = cp->rssi;
6065 hdev->discovery.uuid_count = uuid_count;
6067 if (uuid_count > 0) {
6068 hdev->discovery.uuids = kmemdup(cp->uuids, uuid_count * 16,
6070 if (!hdev->discovery.uuids) {
6071 err = mgmt_cmd_complete(sk, hdev->id,
6072 MGMT_OP_START_SERVICE_DISCOVERY,
6074 &cp->type, sizeof(cp->type));
6075 mgmt_pending_remove(cmd);
6080 err = hci_cmd_sync_queue(hdev, start_discovery_sync, cmd,
6081 start_discovery_complete);
6083 mgmt_pending_remove(cmd);
6087 hci_discovery_set_state(hdev, DISCOVERY_STARTING);
6090 hci_dev_unlock(hdev);
6094 void mgmt_stop_discovery_complete(struct hci_dev *hdev, u8 status)
6096 struct mgmt_pending_cmd *cmd;
6098 bt_dev_dbg(hdev, "status %u", status);
6102 cmd = pending_find(MGMT_OP_STOP_DISCOVERY, hdev);
6104 cmd->cmd_complete(cmd, mgmt_status(status));
6105 mgmt_pending_remove(cmd);
6108 hci_dev_unlock(hdev);
6111 static void stop_discovery_complete(struct hci_dev *hdev, void *data, int err)
6113 struct mgmt_pending_cmd *cmd = data;
6115 if (cmd != pending_find(MGMT_OP_STOP_DISCOVERY, hdev))
6118 bt_dev_dbg(hdev, "err %d", err);
6120 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, mgmt_status(err),
6122 mgmt_pending_remove(cmd);
6125 hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
6128 static int stop_discovery_sync(struct hci_dev *hdev, void *data)
6130 return hci_stop_discovery_sync(hdev);
6133 static int stop_discovery(struct sock *sk, struct hci_dev *hdev, void *data,
6136 struct mgmt_cp_stop_discovery *mgmt_cp = data;
6137 struct mgmt_pending_cmd *cmd;
6140 bt_dev_dbg(hdev, "sock %p", sk);
6144 if (!hci_discovery_active(hdev)) {
6145 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
6146 MGMT_STATUS_REJECTED, &mgmt_cp->type,
6147 sizeof(mgmt_cp->type));
6151 if (hdev->discovery.type != mgmt_cp->type) {
6152 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_STOP_DISCOVERY,
6153 MGMT_STATUS_INVALID_PARAMS,
6154 &mgmt_cp->type, sizeof(mgmt_cp->type));
6158 cmd = mgmt_pending_add(sk, MGMT_OP_STOP_DISCOVERY, hdev, data, len);
6164 err = hci_cmd_sync_queue(hdev, stop_discovery_sync, cmd,
6165 stop_discovery_complete);
6167 mgmt_pending_remove(cmd);
6171 hci_discovery_set_state(hdev, DISCOVERY_STOPPING);
6174 hci_dev_unlock(hdev);
6178 static int confirm_name(struct sock *sk, struct hci_dev *hdev, void *data,
6181 struct mgmt_cp_confirm_name *cp = data;
6182 struct inquiry_entry *e;
6185 bt_dev_dbg(hdev, "sock %p", sk);
6189 if (!hci_discovery_active(hdev)) {
6190 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
6191 MGMT_STATUS_FAILED, &cp->addr,
6196 e = hci_inquiry_cache_lookup_unknown(hdev, &cp->addr.bdaddr);
6198 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME,
6199 MGMT_STATUS_INVALID_PARAMS, &cp->addr,
6204 if (cp->name_known) {
6205 e->name_state = NAME_KNOWN;
6208 e->name_state = NAME_NEEDED;
6209 hci_inquiry_cache_update_resolve(hdev, e);
6212 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_CONFIRM_NAME, 0,
6213 &cp->addr, sizeof(cp->addr));
6216 hci_dev_unlock(hdev);
6220 static int block_device(struct sock *sk, struct hci_dev *hdev, void *data,
6223 struct mgmt_cp_block_device *cp = data;
6227 bt_dev_dbg(hdev, "sock %p", sk);
6229 if (!bdaddr_type_is_valid(cp->addr.type))
6230 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE,
6231 MGMT_STATUS_INVALID_PARAMS,
6232 &cp->addr, sizeof(cp->addr));
6236 err = hci_bdaddr_list_add(&hdev->reject_list, &cp->addr.bdaddr,
6239 status = MGMT_STATUS_FAILED;
6243 mgmt_event(MGMT_EV_DEVICE_BLOCKED, hdev, &cp->addr, sizeof(cp->addr),
6245 status = MGMT_STATUS_SUCCESS;
6248 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_BLOCK_DEVICE, status,
6249 &cp->addr, sizeof(cp->addr));
6251 hci_dev_unlock(hdev);
6256 static int unblock_device(struct sock *sk, struct hci_dev *hdev, void *data,
6259 struct mgmt_cp_unblock_device *cp = data;
6263 bt_dev_dbg(hdev, "sock %p", sk);
6265 if (!bdaddr_type_is_valid(cp->addr.type))
6266 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE,
6267 MGMT_STATUS_INVALID_PARAMS,
6268 &cp->addr, sizeof(cp->addr));
6272 err = hci_bdaddr_list_del(&hdev->reject_list, &cp->addr.bdaddr,
6275 status = MGMT_STATUS_INVALID_PARAMS;
6279 mgmt_event(MGMT_EV_DEVICE_UNBLOCKED, hdev, &cp->addr, sizeof(cp->addr),
6281 status = MGMT_STATUS_SUCCESS;
6284 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_UNBLOCK_DEVICE, status,
6285 &cp->addr, sizeof(cp->addr));
6287 hci_dev_unlock(hdev);
6292 static int set_device_id_sync(struct hci_dev *hdev, void *data)
6294 return hci_update_eir_sync(hdev);
6297 static int set_device_id(struct sock *sk, struct hci_dev *hdev, void *data,
6300 struct mgmt_cp_set_device_id *cp = data;
6304 bt_dev_dbg(hdev, "sock %p", sk);
6306 source = __le16_to_cpu(cp->source);
6308 if (source > 0x0002)
6309 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEVICE_ID,
6310 MGMT_STATUS_INVALID_PARAMS);
6314 hdev->devid_source = source;
6315 hdev->devid_vendor = __le16_to_cpu(cp->vendor);
6316 hdev->devid_product = __le16_to_cpu(cp->product);
6317 hdev->devid_version = __le16_to_cpu(cp->version);
6319 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_DEVICE_ID, 0,
6322 hci_cmd_sync_queue(hdev, set_device_id_sync, NULL, NULL);
6324 hci_dev_unlock(hdev);
6329 static void enable_advertising_instance(struct hci_dev *hdev, int err)
6332 bt_dev_err(hdev, "failed to re-configure advertising %d", err);
6334 bt_dev_dbg(hdev, "status %d", err);
6337 static void set_advertising_complete(struct hci_dev *hdev, void *data, int err)
6339 struct cmd_lookup match = { NULL, hdev };
6341 struct adv_info *adv_instance;
6342 u8 status = mgmt_status(err);
6345 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev,
6346 cmd_status_rsp, &status);
6350 if (hci_dev_test_flag(hdev, HCI_LE_ADV))
6351 hci_dev_set_flag(hdev, HCI_ADVERTISING);
6353 hci_dev_clear_flag(hdev, HCI_ADVERTISING);
6355 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING, hdev, settings_rsp,
6358 new_settings(hdev, match.sk);
6363 /* If "Set Advertising" was just disabled and instance advertising was
6364 * set up earlier, then re-enable multi-instance advertising.
6366 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
6367 list_empty(&hdev->adv_instances))
6370 instance = hdev->cur_adv_instance;
6372 adv_instance = list_first_entry_or_null(&hdev->adv_instances,
6373 struct adv_info, list);
6377 instance = adv_instance->instance;
6380 err = hci_schedule_adv_instance_sync(hdev, instance, true);
6382 enable_advertising_instance(hdev, err);
6385 static int set_adv_sync(struct hci_dev *hdev, void *data)
6387 struct mgmt_pending_cmd *cmd = data;
6388 struct mgmt_mode *cp = cmd->param;
6391 if (cp->val == 0x02)
6392 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
6394 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
6396 cancel_adv_timeout(hdev);
6399 /* Switch to instance "0" for the Set Advertising setting.
6400 * We cannot use update_[adv|scan_rsp]_data() here as the
6401 * HCI_ADVERTISING flag is not yet set.
6403 hdev->cur_adv_instance = 0x00;
6405 if (ext_adv_capable(hdev)) {
6406 hci_start_ext_adv_sync(hdev, 0x00);
6408 hci_update_adv_data_sync(hdev, 0x00);
6409 hci_update_scan_rsp_data_sync(hdev, 0x00);
6410 hci_enable_advertising_sync(hdev);
6413 hci_disable_advertising_sync(hdev);
6419 static int set_advertising(struct sock *sk, struct hci_dev *hdev, void *data,
6422 struct mgmt_mode *cp = data;
6423 struct mgmt_pending_cmd *cmd;
6427 bt_dev_dbg(hdev, "sock %p", sk);
6429 status = mgmt_le_support(hdev);
6431 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
6434 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
6435 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
6436 MGMT_STATUS_INVALID_PARAMS);
6438 if (hdev->advertising_paused)
6439 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
6446 /* The following conditions are ones which mean that we should
6447 * not do any HCI communication but directly send a mgmt
6448 * response to user space (after toggling the flag if
6451 if (!hdev_is_powered(hdev) ||
6452 (val == hci_dev_test_flag(hdev, HCI_ADVERTISING) &&
6453 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_ADVERTISING_CONNECTABLE)) ||
6454 hci_dev_test_flag(hdev, HCI_MESH) ||
6455 hci_conn_num(hdev, LE_LINK) > 0 ||
6456 (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6457 hdev->le_scan_type == LE_SCAN_ACTIVE)) {
6461 hdev->cur_adv_instance = 0x00;
6462 changed = !hci_dev_test_and_set_flag(hdev, HCI_ADVERTISING);
6463 if (cp->val == 0x02)
6464 hci_dev_set_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
6466 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
6468 changed = hci_dev_test_and_clear_flag(hdev, HCI_ADVERTISING);
6469 hci_dev_clear_flag(hdev, HCI_ADVERTISING_CONNECTABLE);
6472 err = send_settings_rsp(sk, MGMT_OP_SET_ADVERTISING, hdev);
6477 err = new_settings(hdev, sk);
6482 if (pending_find(MGMT_OP_SET_ADVERTISING, hdev) ||
6483 pending_find(MGMT_OP_SET_LE, hdev)) {
6484 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_ADVERTISING,
6489 cmd = mgmt_pending_add(sk, MGMT_OP_SET_ADVERTISING, hdev, data, len);
6493 err = hci_cmd_sync_queue(hdev, set_adv_sync, cmd,
6494 set_advertising_complete);
6497 mgmt_pending_remove(cmd);
6500 hci_dev_unlock(hdev);
6504 static int set_static_address(struct sock *sk, struct hci_dev *hdev,
6505 void *data, u16 len)
6507 struct mgmt_cp_set_static_address *cp = data;
6510 bt_dev_dbg(hdev, "sock %p", sk);
6512 if (!lmp_le_capable(hdev))
6513 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
6514 MGMT_STATUS_NOT_SUPPORTED);
6516 if (hdev_is_powered(hdev))
6517 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_STATIC_ADDRESS,
6518 MGMT_STATUS_REJECTED);
6520 if (bacmp(&cp->bdaddr, BDADDR_ANY)) {
6521 if (!bacmp(&cp->bdaddr, BDADDR_NONE))
6522 return mgmt_cmd_status(sk, hdev->id,
6523 MGMT_OP_SET_STATIC_ADDRESS,
6524 MGMT_STATUS_INVALID_PARAMS);
6526 /* Two most significant bits shall be set */
6527 if ((cp->bdaddr.b[5] & 0xc0) != 0xc0)
6528 return mgmt_cmd_status(sk, hdev->id,
6529 MGMT_OP_SET_STATIC_ADDRESS,
6530 MGMT_STATUS_INVALID_PARAMS);
6535 bacpy(&hdev->static_addr, &cp->bdaddr);
6537 err = send_settings_rsp(sk, MGMT_OP_SET_STATIC_ADDRESS, hdev);
6541 err = new_settings(hdev, sk);
6544 hci_dev_unlock(hdev);
6548 static int set_scan_params(struct sock *sk, struct hci_dev *hdev,
6549 void *data, u16 len)
6551 struct mgmt_cp_set_scan_params *cp = data;
6552 __u16 interval, window;
6555 bt_dev_dbg(hdev, "sock %p", sk);
6557 if (!lmp_le_capable(hdev))
6558 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
6559 MGMT_STATUS_NOT_SUPPORTED);
6561 interval = __le16_to_cpu(cp->interval);
6563 if (interval < 0x0004 || interval > 0x4000)
6564 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
6565 MGMT_STATUS_INVALID_PARAMS);
6567 window = __le16_to_cpu(cp->window);
6569 if (window < 0x0004 || window > 0x4000)
6570 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
6571 MGMT_STATUS_INVALID_PARAMS);
6573 if (window > interval)
6574 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS,
6575 MGMT_STATUS_INVALID_PARAMS);
6579 hdev->le_scan_interval = interval;
6580 hdev->le_scan_window = window;
6582 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_SET_SCAN_PARAMS, 0,
6585 /* If background scan is running, restart it so new parameters are
6588 if (hci_dev_test_flag(hdev, HCI_LE_SCAN) &&
6589 hdev->discovery.state == DISCOVERY_STOPPED)
6590 hci_update_passive_scan(hdev);
6592 hci_dev_unlock(hdev);
6597 static void fast_connectable_complete(struct hci_dev *hdev, void *data, int err)
6599 struct mgmt_pending_cmd *cmd = data;
6601 bt_dev_dbg(hdev, "err %d", err);
6604 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
6607 struct mgmt_mode *cp = cmd->param;
6610 hci_dev_set_flag(hdev, HCI_FAST_CONNECTABLE);
6612 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
6614 send_settings_rsp(cmd->sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
6615 new_settings(hdev, cmd->sk);
6618 mgmt_pending_free(cmd);
6621 static int write_fast_connectable_sync(struct hci_dev *hdev, void *data)
6623 struct mgmt_pending_cmd *cmd = data;
6624 struct mgmt_mode *cp = cmd->param;
6626 return hci_write_fast_connectable_sync(hdev, cp->val);
6629 static int set_fast_connectable(struct sock *sk, struct hci_dev *hdev,
6630 void *data, u16 len)
6632 struct mgmt_mode *cp = data;
6633 struct mgmt_pending_cmd *cmd;
6636 bt_dev_dbg(hdev, "sock %p", sk);
6638 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) ||
6639 hdev->hci_ver < BLUETOOTH_VER_1_2)
6640 return mgmt_cmd_status(sk, hdev->id,
6641 MGMT_OP_SET_FAST_CONNECTABLE,
6642 MGMT_STATUS_NOT_SUPPORTED);
6644 if (cp->val != 0x00 && cp->val != 0x01)
6645 return mgmt_cmd_status(sk, hdev->id,
6646 MGMT_OP_SET_FAST_CONNECTABLE,
6647 MGMT_STATUS_INVALID_PARAMS);
6651 if (!!cp->val == hci_dev_test_flag(hdev, HCI_FAST_CONNECTABLE)) {
6652 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
6656 if (!hdev_is_powered(hdev)) {
6657 hci_dev_change_flag(hdev, HCI_FAST_CONNECTABLE);
6658 err = send_settings_rsp(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev);
6659 new_settings(hdev, sk);
6663 cmd = mgmt_pending_new(sk, MGMT_OP_SET_FAST_CONNECTABLE, hdev, data,
6668 err = hci_cmd_sync_queue(hdev, write_fast_connectable_sync, cmd,
6669 fast_connectable_complete);
6672 mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_FAST_CONNECTABLE,
6673 MGMT_STATUS_FAILED);
6676 mgmt_pending_free(cmd);
6680 hci_dev_unlock(hdev);
6685 static void set_bredr_complete(struct hci_dev *hdev, void *data, int err)
6687 struct mgmt_pending_cmd *cmd = data;
6689 bt_dev_dbg(hdev, "err %d", err);
6692 u8 mgmt_err = mgmt_status(err);
6694 /* We need to restore the flag if related HCI commands
6697 hci_dev_clear_flag(hdev, HCI_BREDR_ENABLED);
6699 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
6701 send_settings_rsp(cmd->sk, MGMT_OP_SET_BREDR, hdev);
6702 new_settings(hdev, cmd->sk);
6705 mgmt_pending_free(cmd);
6708 static int set_bredr_sync(struct hci_dev *hdev, void *data)
6712 status = hci_write_fast_connectable_sync(hdev, false);
6715 status = hci_update_scan_sync(hdev);
6717 /* Since only the advertising data flags will change, there
6718 * is no need to update the scan response data.
6721 status = hci_update_adv_data_sync(hdev, hdev->cur_adv_instance);
6726 static int set_bredr(struct sock *sk, struct hci_dev *hdev, void *data, u16 len)
6728 struct mgmt_mode *cp = data;
6729 struct mgmt_pending_cmd *cmd;
6732 bt_dev_dbg(hdev, "sock %p", sk);
6734 if (!lmp_bredr_capable(hdev) || !lmp_le_capable(hdev))
6735 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
6736 MGMT_STATUS_NOT_SUPPORTED);
6738 if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
6739 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
6740 MGMT_STATUS_REJECTED);
6742 if (cp->val != 0x00 && cp->val != 0x01)
6743 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
6744 MGMT_STATUS_INVALID_PARAMS);
6748 if (cp->val == hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
6749 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
6753 if (!hdev_is_powered(hdev)) {
6755 hci_dev_clear_flag(hdev, HCI_DISCOVERABLE);
6756 hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
6757 hci_dev_clear_flag(hdev, HCI_LINK_SECURITY);
6758 hci_dev_clear_flag(hdev, HCI_FAST_CONNECTABLE);
6759 hci_dev_clear_flag(hdev, HCI_HS_ENABLED);
6762 hci_dev_change_flag(hdev, HCI_BREDR_ENABLED);
6764 err = send_settings_rsp(sk, MGMT_OP_SET_BREDR, hdev);
6768 err = new_settings(hdev, sk);
6772 /* Reject disabling when powered on */
6774 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
6775 MGMT_STATUS_REJECTED);
6778 /* When configuring a dual-mode controller to operate
6779 * with LE only and using a static address, then switching
6780 * BR/EDR back on is not allowed.
6782 * Dual-mode controllers shall operate with the public
6783 * address as its identity address for BR/EDR and LE. So
6784 * reject the attempt to create an invalid configuration.
6786 * The same restrictions applies when secure connections
6787 * has been enabled. For BR/EDR this is a controller feature
6788 * while for LE it is a host stack feature. This means that
6789 * switching BR/EDR back on when secure connections has been
6790 * enabled is not a supported transaction.
6792 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
6793 (bacmp(&hdev->static_addr, BDADDR_ANY) ||
6794 hci_dev_test_flag(hdev, HCI_SC_ENABLED))) {
6795 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
6796 MGMT_STATUS_REJECTED);
6801 cmd = mgmt_pending_new(sk, MGMT_OP_SET_BREDR, hdev, data, len);
6805 err = hci_cmd_sync_queue(hdev, set_bredr_sync, cmd,
6806 set_bredr_complete);
6809 mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_BREDR,
6810 MGMT_STATUS_FAILED);
6812 mgmt_pending_free(cmd);
6817 /* We need to flip the bit already here so that
6818 * hci_req_update_adv_data generates the correct flags.
6820 hci_dev_set_flag(hdev, HCI_BREDR_ENABLED);
6823 hci_dev_unlock(hdev);
6827 static void set_secure_conn_complete(struct hci_dev *hdev, void *data, int err)
6829 struct mgmt_pending_cmd *cmd = data;
6830 struct mgmt_mode *cp;
6832 bt_dev_dbg(hdev, "err %d", err);
6835 u8 mgmt_err = mgmt_status(err);
6837 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode, mgmt_err);
6845 hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
6846 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
6849 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
6850 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
6853 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
6854 hci_dev_set_flag(hdev, HCI_SC_ONLY);
6858 send_settings_rsp(cmd->sk, cmd->opcode, hdev);
6859 new_settings(hdev, cmd->sk);
6862 mgmt_pending_free(cmd);
6865 static int set_secure_conn_sync(struct hci_dev *hdev, void *data)
6867 struct mgmt_pending_cmd *cmd = data;
6868 struct mgmt_mode *cp = cmd->param;
6871 /* Force write of val */
6872 hci_dev_set_flag(hdev, HCI_SC_ENABLED);
6874 return hci_write_sc_support_sync(hdev, val);
6877 static int set_secure_conn(struct sock *sk, struct hci_dev *hdev,
6878 void *data, u16 len)
6880 struct mgmt_mode *cp = data;
6881 struct mgmt_pending_cmd *cmd;
6885 bt_dev_dbg(hdev, "sock %p", sk);
6887 if (!lmp_sc_capable(hdev) &&
6888 !hci_dev_test_flag(hdev, HCI_LE_ENABLED))
6889 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
6890 MGMT_STATUS_NOT_SUPPORTED);
6892 if (hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
6893 lmp_sc_capable(hdev) &&
6894 !hci_dev_test_flag(hdev, HCI_SSP_ENABLED))
6895 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
6896 MGMT_STATUS_REJECTED);
6898 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
6899 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
6900 MGMT_STATUS_INVALID_PARAMS);
6904 if (!hdev_is_powered(hdev) || !lmp_sc_capable(hdev) ||
6905 !hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) {
6909 changed = !hci_dev_test_and_set_flag(hdev,
6911 if (cp->val == 0x02)
6912 hci_dev_set_flag(hdev, HCI_SC_ONLY);
6914 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
6916 changed = hci_dev_test_and_clear_flag(hdev,
6918 hci_dev_clear_flag(hdev, HCI_SC_ONLY);
6921 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
6926 err = new_settings(hdev, sk);
6933 if (val == hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
6934 (cp->val == 0x02) == hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
6935 err = send_settings_rsp(sk, MGMT_OP_SET_SECURE_CONN, hdev);
6939 cmd = mgmt_pending_new(sk, MGMT_OP_SET_SECURE_CONN, hdev, data, len);
6943 err = hci_cmd_sync_queue(hdev, set_secure_conn_sync, cmd,
6944 set_secure_conn_complete);
6947 mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_SECURE_CONN,
6948 MGMT_STATUS_FAILED);
6950 mgmt_pending_free(cmd);
6954 hci_dev_unlock(hdev);
6958 static int set_debug_keys(struct sock *sk, struct hci_dev *hdev,
6959 void *data, u16 len)
6961 struct mgmt_mode *cp = data;
6962 bool changed, use_changed;
6965 bt_dev_dbg(hdev, "sock %p", sk);
6967 if (cp->val != 0x00 && cp->val != 0x01 && cp->val != 0x02)
6968 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_DEBUG_KEYS,
6969 MGMT_STATUS_INVALID_PARAMS);
6974 changed = !hci_dev_test_and_set_flag(hdev, HCI_KEEP_DEBUG_KEYS);
6976 changed = hci_dev_test_and_clear_flag(hdev,
6977 HCI_KEEP_DEBUG_KEYS);
6979 if (cp->val == 0x02)
6980 use_changed = !hci_dev_test_and_set_flag(hdev,
6981 HCI_USE_DEBUG_KEYS);
6983 use_changed = hci_dev_test_and_clear_flag(hdev,
6984 HCI_USE_DEBUG_KEYS);
6986 if (hdev_is_powered(hdev) && use_changed &&
6987 hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
6988 u8 mode = (cp->val == 0x02) ? 0x01 : 0x00;
6989 hci_send_cmd(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE,
6990 sizeof(mode), &mode);
6993 err = send_settings_rsp(sk, MGMT_OP_SET_DEBUG_KEYS, hdev);
6998 err = new_settings(hdev, sk);
7001 hci_dev_unlock(hdev);
7005 static int set_privacy(struct sock *sk, struct hci_dev *hdev, void *cp_data,
7008 struct mgmt_cp_set_privacy *cp = cp_data;
7012 bt_dev_dbg(hdev, "sock %p", sk);
7014 if (!lmp_le_capable(hdev))
7015 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
7016 MGMT_STATUS_NOT_SUPPORTED);
7018 if (cp->privacy != 0x00 && cp->privacy != 0x01 && cp->privacy != 0x02)
7019 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
7020 MGMT_STATUS_INVALID_PARAMS);
7022 if (hdev_is_powered(hdev))
7023 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PRIVACY,
7024 MGMT_STATUS_REJECTED);
7028 /* If user space supports this command it is also expected to
7029 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
7031 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
7034 changed = !hci_dev_test_and_set_flag(hdev, HCI_PRIVACY);
7035 memcpy(hdev->irk, cp->irk, sizeof(hdev->irk));
7036 hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
7037 hci_adv_instances_set_rpa_expired(hdev, true);
7038 if (cp->privacy == 0x02)
7039 hci_dev_set_flag(hdev, HCI_LIMITED_PRIVACY);
7041 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
7043 changed = hci_dev_test_and_clear_flag(hdev, HCI_PRIVACY);
7044 memset(hdev->irk, 0, sizeof(hdev->irk));
7045 hci_dev_clear_flag(hdev, HCI_RPA_EXPIRED);
7046 hci_adv_instances_set_rpa_expired(hdev, false);
7047 hci_dev_clear_flag(hdev, HCI_LIMITED_PRIVACY);
7050 err = send_settings_rsp(sk, MGMT_OP_SET_PRIVACY, hdev);
7055 err = new_settings(hdev, sk);
7058 hci_dev_unlock(hdev);
7062 static bool irk_is_valid(struct mgmt_irk_info *irk)
7064 switch (irk->addr.type) {
7065 case BDADDR_LE_PUBLIC:
7068 case BDADDR_LE_RANDOM:
7069 /* Two most significant bits shall be set */
7070 if ((irk->addr.bdaddr.b[5] & 0xc0) != 0xc0)
7078 static int load_irks(struct sock *sk, struct hci_dev *hdev, void *cp_data,
7081 struct mgmt_cp_load_irks *cp = cp_data;
7082 const u16 max_irk_count = ((U16_MAX - sizeof(*cp)) /
7083 sizeof(struct mgmt_irk_info));
7084 u16 irk_count, expected_len;
7087 bt_dev_dbg(hdev, "sock %p", sk);
7089 if (!lmp_le_capable(hdev))
7090 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
7091 MGMT_STATUS_NOT_SUPPORTED);
7093 irk_count = __le16_to_cpu(cp->irk_count);
7094 if (irk_count > max_irk_count) {
7095 bt_dev_err(hdev, "load_irks: too big irk_count value %u",
7097 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
7098 MGMT_STATUS_INVALID_PARAMS);
7101 expected_len = struct_size(cp, irks, irk_count);
7102 if (expected_len != len) {
7103 bt_dev_err(hdev, "load_irks: expected %u bytes, got %u bytes",
7105 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_IRKS,
7106 MGMT_STATUS_INVALID_PARAMS);
7109 bt_dev_dbg(hdev, "irk_count %u", irk_count);
7111 for (i = 0; i < irk_count; i++) {
7112 struct mgmt_irk_info *key = &cp->irks[i];
7114 if (!irk_is_valid(key))
7115 return mgmt_cmd_status(sk, hdev->id,
7117 MGMT_STATUS_INVALID_PARAMS);
7122 hci_smp_irks_clear(hdev);
7124 for (i = 0; i < irk_count; i++) {
7125 struct mgmt_irk_info *irk = &cp->irks[i];
7127 if (hci_is_blocked_key(hdev,
7128 HCI_BLOCKED_KEY_TYPE_IRK,
7130 bt_dev_warn(hdev, "Skipping blocked IRK for %pMR",
7135 hci_add_irk(hdev, &irk->addr.bdaddr,
7136 le_addr_type(irk->addr.type), irk->val,
7140 hci_dev_set_flag(hdev, HCI_RPA_RESOLVING);
7142 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_IRKS, 0, NULL, 0);
7144 hci_dev_unlock(hdev);
7149 static bool ltk_is_valid(struct mgmt_ltk_info *key)
7151 if (key->initiator != 0x00 && key->initiator != 0x01)
7154 switch (key->addr.type) {
7155 case BDADDR_LE_PUBLIC:
7158 case BDADDR_LE_RANDOM:
7159 /* Two most significant bits shall be set */
7160 if ((key->addr.bdaddr.b[5] & 0xc0) != 0xc0)
7168 static int load_long_term_keys(struct sock *sk, struct hci_dev *hdev,
7169 void *cp_data, u16 len)
7171 struct mgmt_cp_load_long_term_keys *cp = cp_data;
7172 const u16 max_key_count = ((U16_MAX - sizeof(*cp)) /
7173 sizeof(struct mgmt_ltk_info));
7174 u16 key_count, expected_len;
7177 bt_dev_dbg(hdev, "sock %p", sk);
7179 if (!lmp_le_capable(hdev))
7180 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
7181 MGMT_STATUS_NOT_SUPPORTED);
7183 key_count = __le16_to_cpu(cp->key_count);
7184 if (key_count > max_key_count) {
7185 bt_dev_err(hdev, "load_ltks: too big key_count value %u",
7187 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
7188 MGMT_STATUS_INVALID_PARAMS);
7191 expected_len = struct_size(cp, keys, key_count);
7192 if (expected_len != len) {
7193 bt_dev_err(hdev, "load_keys: expected %u bytes, got %u bytes",
7195 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS,
7196 MGMT_STATUS_INVALID_PARAMS);
7199 bt_dev_dbg(hdev, "key_count %u", key_count);
7201 for (i = 0; i < key_count; i++) {
7202 struct mgmt_ltk_info *key = &cp->keys[i];
7204 if (!ltk_is_valid(key))
7205 return mgmt_cmd_status(sk, hdev->id,
7206 MGMT_OP_LOAD_LONG_TERM_KEYS,
7207 MGMT_STATUS_INVALID_PARAMS);
7212 hci_smp_ltks_clear(hdev);
7214 for (i = 0; i < key_count; i++) {
7215 struct mgmt_ltk_info *key = &cp->keys[i];
7216 u8 type, authenticated;
7218 if (hci_is_blocked_key(hdev,
7219 HCI_BLOCKED_KEY_TYPE_LTK,
7221 bt_dev_warn(hdev, "Skipping blocked LTK for %pMR",
7226 switch (key->type) {
7227 case MGMT_LTK_UNAUTHENTICATED:
7228 authenticated = 0x00;
7229 type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER;
7231 case MGMT_LTK_AUTHENTICATED:
7232 authenticated = 0x01;
7233 type = key->initiator ? SMP_LTK : SMP_LTK_RESPONDER;
7235 case MGMT_LTK_P256_UNAUTH:
7236 authenticated = 0x00;
7237 type = SMP_LTK_P256;
7239 case MGMT_LTK_P256_AUTH:
7240 authenticated = 0x01;
7241 type = SMP_LTK_P256;
7243 case MGMT_LTK_P256_DEBUG:
7244 authenticated = 0x00;
7245 type = SMP_LTK_P256_DEBUG;
7251 hci_add_ltk(hdev, &key->addr.bdaddr,
7252 le_addr_type(key->addr.type), type, authenticated,
7253 key->val, key->enc_size, key->ediv, key->rand);
7256 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_LONG_TERM_KEYS, 0,
7259 hci_dev_unlock(hdev);
7264 static void get_conn_info_complete(struct hci_dev *hdev, void *data, int err)
7266 struct mgmt_pending_cmd *cmd = data;
7267 struct hci_conn *conn = cmd->user_data;
7268 struct mgmt_cp_get_conn_info *cp = cmd->param;
7269 struct mgmt_rp_get_conn_info rp;
7272 bt_dev_dbg(hdev, "err %d", err);
7274 memcpy(&rp.addr, &cp->addr.bdaddr, sizeof(rp.addr));
7276 status = mgmt_status(err);
7277 if (status == MGMT_STATUS_SUCCESS) {
7278 rp.rssi = conn->rssi;
7279 rp.tx_power = conn->tx_power;
7280 rp.max_tx_power = conn->max_tx_power;
7282 rp.rssi = HCI_RSSI_INVALID;
7283 rp.tx_power = HCI_TX_POWER_INVALID;
7284 rp.max_tx_power = HCI_TX_POWER_INVALID;
7287 mgmt_cmd_complete(cmd->sk, cmd->index, MGMT_OP_GET_CONN_INFO, status,
7290 mgmt_pending_free(cmd);
7293 static int get_conn_info_sync(struct hci_dev *hdev, void *data)
7295 struct mgmt_pending_cmd *cmd = data;
7296 struct mgmt_cp_get_conn_info *cp = cmd->param;
7297 struct hci_conn *conn;
7301 /* Make sure we are still connected */
7302 if (cp->addr.type == BDADDR_BREDR)
7303 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
7306 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
7308 if (!conn || conn->state != BT_CONNECTED)
7309 return MGMT_STATUS_NOT_CONNECTED;
7311 cmd->user_data = conn;
7312 handle = cpu_to_le16(conn->handle);
7314 /* Refresh RSSI each time */
7315 err = hci_read_rssi_sync(hdev, handle);
7317 /* For LE links TX power does not change thus we don't need to
7318 * query for it once value is known.
7320 if (!err && (!bdaddr_type_is_le(cp->addr.type) ||
7321 conn->tx_power == HCI_TX_POWER_INVALID))
7322 err = hci_read_tx_power_sync(hdev, handle, 0x00);
7324 /* Max TX power needs to be read only once per connection */
7325 if (!err && conn->max_tx_power == HCI_TX_POWER_INVALID)
7326 err = hci_read_tx_power_sync(hdev, handle, 0x01);
7331 static int get_conn_info(struct sock *sk, struct hci_dev *hdev, void *data,
7334 struct mgmt_cp_get_conn_info *cp = data;
7335 struct mgmt_rp_get_conn_info rp;
7336 struct hci_conn *conn;
7337 unsigned long conn_info_age;
7340 bt_dev_dbg(hdev, "sock %p", sk);
7342 memset(&rp, 0, sizeof(rp));
7343 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
7344 rp.addr.type = cp->addr.type;
7346 if (!bdaddr_type_is_valid(cp->addr.type))
7347 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
7348 MGMT_STATUS_INVALID_PARAMS,
7353 if (!hdev_is_powered(hdev)) {
7354 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
7355 MGMT_STATUS_NOT_POWERED, &rp,
7360 if (cp->addr.type == BDADDR_BREDR)
7361 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
7364 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, &cp->addr.bdaddr);
7366 if (!conn || conn->state != BT_CONNECTED) {
7367 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
7368 MGMT_STATUS_NOT_CONNECTED, &rp,
7373 /* To avoid client trying to guess when to poll again for information we
7374 * calculate conn info age as random value between min/max set in hdev.
7376 conn_info_age = hdev->conn_info_min_age +
7377 prandom_u32_max(hdev->conn_info_max_age -
7378 hdev->conn_info_min_age);
7380 /* Query controller to refresh cached values if they are too old or were
7383 if (time_after(jiffies, conn->conn_info_timestamp +
7384 msecs_to_jiffies(conn_info_age)) ||
7385 !conn->conn_info_timestamp) {
7386 struct mgmt_pending_cmd *cmd;
7388 cmd = mgmt_pending_new(sk, MGMT_OP_GET_CONN_INFO, hdev, data,
7393 err = hci_cmd_sync_queue(hdev, get_conn_info_sync,
7394 cmd, get_conn_info_complete);
7398 mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
7399 MGMT_STATUS_FAILED, &rp, sizeof(rp));
7402 mgmt_pending_free(cmd);
7407 conn->conn_info_timestamp = jiffies;
7409 /* Cache is valid, just reply with values cached in hci_conn */
7410 rp.rssi = conn->rssi;
7411 rp.tx_power = conn->tx_power;
7412 rp.max_tx_power = conn->max_tx_power;
7414 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CONN_INFO,
7415 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
7419 hci_dev_unlock(hdev);
7423 static void get_clock_info_complete(struct hci_dev *hdev, void *data, int err)
7425 struct mgmt_pending_cmd *cmd = data;
7426 struct mgmt_cp_get_clock_info *cp = cmd->param;
7427 struct mgmt_rp_get_clock_info rp;
7428 struct hci_conn *conn = cmd->user_data;
7429 u8 status = mgmt_status(err);
7431 bt_dev_dbg(hdev, "err %d", err);
7433 memset(&rp, 0, sizeof(rp));
7434 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
7435 rp.addr.type = cp->addr.type;
7440 rp.local_clock = cpu_to_le32(hdev->clock);
7443 rp.piconet_clock = cpu_to_le32(conn->clock);
7444 rp.accuracy = cpu_to_le16(conn->clock_accuracy);
7448 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode, status, &rp,
7451 mgmt_pending_free(cmd);
7454 static int get_clock_info_sync(struct hci_dev *hdev, void *data)
7456 struct mgmt_pending_cmd *cmd = data;
7457 struct mgmt_cp_get_clock_info *cp = cmd->param;
7458 struct hci_cp_read_clock hci_cp;
7459 struct hci_conn *conn;
7461 memset(&hci_cp, 0, sizeof(hci_cp));
7462 hci_read_clock_sync(hdev, &hci_cp);
7464 /* Make sure connection still exists */
7465 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->addr.bdaddr);
7466 if (!conn || conn->state != BT_CONNECTED)
7467 return MGMT_STATUS_NOT_CONNECTED;
7469 cmd->user_data = conn;
7470 hci_cp.handle = cpu_to_le16(conn->handle);
7471 hci_cp.which = 0x01; /* Piconet clock */
7473 return hci_read_clock_sync(hdev, &hci_cp);
7476 static int get_clock_info(struct sock *sk, struct hci_dev *hdev, void *data,
7479 struct mgmt_cp_get_clock_info *cp = data;
7480 struct mgmt_rp_get_clock_info rp;
7481 struct mgmt_pending_cmd *cmd;
7482 struct hci_conn *conn;
7485 bt_dev_dbg(hdev, "sock %p", sk);
7487 memset(&rp, 0, sizeof(rp));
7488 bacpy(&rp.addr.bdaddr, &cp->addr.bdaddr);
7489 rp.addr.type = cp->addr.type;
7491 if (cp->addr.type != BDADDR_BREDR)
7492 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
7493 MGMT_STATUS_INVALID_PARAMS,
7498 if (!hdev_is_powered(hdev)) {
7499 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
7500 MGMT_STATUS_NOT_POWERED, &rp,
7505 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
7506 conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK,
7508 if (!conn || conn->state != BT_CONNECTED) {
7509 err = mgmt_cmd_complete(sk, hdev->id,
7510 MGMT_OP_GET_CLOCK_INFO,
7511 MGMT_STATUS_NOT_CONNECTED,
7519 cmd = mgmt_pending_new(sk, MGMT_OP_GET_CLOCK_INFO, hdev, data, len);
7523 err = hci_cmd_sync_queue(hdev, get_clock_info_sync, cmd,
7524 get_clock_info_complete);
7527 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_CLOCK_INFO,
7528 MGMT_STATUS_FAILED, &rp, sizeof(rp));
7531 mgmt_pending_free(cmd);
7536 hci_dev_unlock(hdev);
7540 static bool is_connected(struct hci_dev *hdev, bdaddr_t *addr, u8 type)
7542 struct hci_conn *conn;
7544 conn = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
7548 if (conn->dst_type != type)
7551 if (conn->state != BT_CONNECTED)
7557 /* This function requires the caller holds hdev->lock */
7558 static int hci_conn_params_set(struct hci_dev *hdev, bdaddr_t *addr,
7559 u8 addr_type, u8 auto_connect)
7561 struct hci_conn_params *params;
7563 params = hci_conn_params_add(hdev, addr, addr_type);
7567 if (params->auto_connect == auto_connect)
7570 list_del_init(¶ms->action);
7572 switch (auto_connect) {
7573 case HCI_AUTO_CONN_DISABLED:
7574 case HCI_AUTO_CONN_LINK_LOSS:
7575 /* If auto connect is being disabled when we're trying to
7576 * connect to device, keep connecting.
7578 if (params->explicit_connect)
7579 list_add(¶ms->action, &hdev->pend_le_conns);
7581 case HCI_AUTO_CONN_REPORT:
7582 if (params->explicit_connect)
7583 list_add(¶ms->action, &hdev->pend_le_conns);
7585 list_add(¶ms->action, &hdev->pend_le_reports);
7587 case HCI_AUTO_CONN_DIRECT:
7588 case HCI_AUTO_CONN_ALWAYS:
7589 if (!is_connected(hdev, addr, addr_type))
7590 list_add(¶ms->action, &hdev->pend_le_conns);
7594 params->auto_connect = auto_connect;
7596 bt_dev_dbg(hdev, "addr %pMR (type %u) auto_connect %u",
7597 addr, addr_type, auto_connect);
7602 static void device_added(struct sock *sk, struct hci_dev *hdev,
7603 bdaddr_t *bdaddr, u8 type, u8 action)
7605 struct mgmt_ev_device_added ev;
7607 bacpy(&ev.addr.bdaddr, bdaddr);
7608 ev.addr.type = type;
7611 mgmt_event(MGMT_EV_DEVICE_ADDED, hdev, &ev, sizeof(ev), sk);
7614 static int add_device_sync(struct hci_dev *hdev, void *data)
7616 return hci_update_passive_scan_sync(hdev);
7619 static int add_device(struct sock *sk, struct hci_dev *hdev,
7620 void *data, u16 len)
7622 struct mgmt_cp_add_device *cp = data;
7623 u8 auto_conn, addr_type;
7624 struct hci_conn_params *params;
7626 u32 current_flags = 0;
7627 u32 supported_flags;
7629 bt_dev_dbg(hdev, "sock %p", sk);
7631 if (!bdaddr_type_is_valid(cp->addr.type) ||
7632 !bacmp(&cp->addr.bdaddr, BDADDR_ANY))
7633 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7634 MGMT_STATUS_INVALID_PARAMS,
7635 &cp->addr, sizeof(cp->addr));
7637 if (cp->action != 0x00 && cp->action != 0x01 && cp->action != 0x02)
7638 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7639 MGMT_STATUS_INVALID_PARAMS,
7640 &cp->addr, sizeof(cp->addr));
7644 if (cp->addr.type == BDADDR_BREDR) {
7645 /* Only incoming connections action is supported for now */
7646 if (cp->action != 0x01) {
7647 err = mgmt_cmd_complete(sk, hdev->id,
7649 MGMT_STATUS_INVALID_PARAMS,
7650 &cp->addr, sizeof(cp->addr));
7654 err = hci_bdaddr_list_add_with_flags(&hdev->accept_list,
7660 hci_update_scan(hdev);
7665 addr_type = le_addr_type(cp->addr.type);
7667 if (cp->action == 0x02)
7668 auto_conn = HCI_AUTO_CONN_ALWAYS;
7669 else if (cp->action == 0x01)
7670 auto_conn = HCI_AUTO_CONN_DIRECT;
7672 auto_conn = HCI_AUTO_CONN_REPORT;
7674 /* Kernel internally uses conn_params with resolvable private
7675 * address, but Add Device allows only identity addresses.
7676 * Make sure it is enforced before calling
7677 * hci_conn_params_lookup.
7679 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
7680 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7681 MGMT_STATUS_INVALID_PARAMS,
7682 &cp->addr, sizeof(cp->addr));
7686 /* If the connection parameters don't exist for this device,
7687 * they will be created and configured with defaults.
7689 if (hci_conn_params_set(hdev, &cp->addr.bdaddr, addr_type,
7691 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7692 MGMT_STATUS_FAILED, &cp->addr,
7696 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
7699 current_flags = params->flags;
7702 err = hci_cmd_sync_queue(hdev, add_device_sync, NULL, NULL);
7707 device_added(sk, hdev, &cp->addr.bdaddr, cp->addr.type, cp->action);
7708 supported_flags = hdev->conn_flags;
7709 device_flags_changed(NULL, hdev, &cp->addr.bdaddr, cp->addr.type,
7710 supported_flags, current_flags);
7712 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_DEVICE,
7713 MGMT_STATUS_SUCCESS, &cp->addr,
7717 hci_dev_unlock(hdev);
7721 static void device_removed(struct sock *sk, struct hci_dev *hdev,
7722 bdaddr_t *bdaddr, u8 type)
7724 struct mgmt_ev_device_removed ev;
7726 bacpy(&ev.addr.bdaddr, bdaddr);
7727 ev.addr.type = type;
7729 mgmt_event(MGMT_EV_DEVICE_REMOVED, hdev, &ev, sizeof(ev), sk);
7732 static int remove_device_sync(struct hci_dev *hdev, void *data)
7734 return hci_update_passive_scan_sync(hdev);
7737 static int remove_device(struct sock *sk, struct hci_dev *hdev,
7738 void *data, u16 len)
7740 struct mgmt_cp_remove_device *cp = data;
7743 bt_dev_dbg(hdev, "sock %p", sk);
7747 if (bacmp(&cp->addr.bdaddr, BDADDR_ANY)) {
7748 struct hci_conn_params *params;
7751 if (!bdaddr_type_is_valid(cp->addr.type)) {
7752 err = mgmt_cmd_complete(sk, hdev->id,
7753 MGMT_OP_REMOVE_DEVICE,
7754 MGMT_STATUS_INVALID_PARAMS,
7755 &cp->addr, sizeof(cp->addr));
7759 if (cp->addr.type == BDADDR_BREDR) {
7760 err = hci_bdaddr_list_del(&hdev->accept_list,
7764 err = mgmt_cmd_complete(sk, hdev->id,
7765 MGMT_OP_REMOVE_DEVICE,
7766 MGMT_STATUS_INVALID_PARAMS,
7772 hci_update_scan(hdev);
7774 device_removed(sk, hdev, &cp->addr.bdaddr,
7779 addr_type = le_addr_type(cp->addr.type);
7781 /* Kernel internally uses conn_params with resolvable private
7782 * address, but Remove Device allows only identity addresses.
7783 * Make sure it is enforced before calling
7784 * hci_conn_params_lookup.
7786 if (!hci_is_identity_address(&cp->addr.bdaddr, addr_type)) {
7787 err = mgmt_cmd_complete(sk, hdev->id,
7788 MGMT_OP_REMOVE_DEVICE,
7789 MGMT_STATUS_INVALID_PARAMS,
7790 &cp->addr, sizeof(cp->addr));
7794 params = hci_conn_params_lookup(hdev, &cp->addr.bdaddr,
7797 err = mgmt_cmd_complete(sk, hdev->id,
7798 MGMT_OP_REMOVE_DEVICE,
7799 MGMT_STATUS_INVALID_PARAMS,
7800 &cp->addr, sizeof(cp->addr));
7804 if (params->auto_connect == HCI_AUTO_CONN_DISABLED ||
7805 params->auto_connect == HCI_AUTO_CONN_EXPLICIT) {
7806 err = mgmt_cmd_complete(sk, hdev->id,
7807 MGMT_OP_REMOVE_DEVICE,
7808 MGMT_STATUS_INVALID_PARAMS,
7809 &cp->addr, sizeof(cp->addr));
7813 list_del(¶ms->action);
7814 list_del(¶ms->list);
7817 device_removed(sk, hdev, &cp->addr.bdaddr, cp->addr.type);
7819 struct hci_conn_params *p, *tmp;
7820 struct bdaddr_list *b, *btmp;
7822 if (cp->addr.type) {
7823 err = mgmt_cmd_complete(sk, hdev->id,
7824 MGMT_OP_REMOVE_DEVICE,
7825 MGMT_STATUS_INVALID_PARAMS,
7826 &cp->addr, sizeof(cp->addr));
7830 list_for_each_entry_safe(b, btmp, &hdev->accept_list, list) {
7831 device_removed(sk, hdev, &b->bdaddr, b->bdaddr_type);
7836 hci_update_scan(hdev);
7838 list_for_each_entry_safe(p, tmp, &hdev->le_conn_params, list) {
7839 if (p->auto_connect == HCI_AUTO_CONN_DISABLED)
7841 device_removed(sk, hdev, &p->addr, p->addr_type);
7842 if (p->explicit_connect) {
7843 p->auto_connect = HCI_AUTO_CONN_EXPLICIT;
7846 list_del(&p->action);
7851 bt_dev_dbg(hdev, "All LE connection parameters were removed");
7854 hci_cmd_sync_queue(hdev, remove_device_sync, NULL, NULL);
7857 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_REMOVE_DEVICE,
7858 MGMT_STATUS_SUCCESS, &cp->addr,
7861 hci_dev_unlock(hdev);
7865 static int load_conn_param(struct sock *sk, struct hci_dev *hdev, void *data,
7868 struct mgmt_cp_load_conn_param *cp = data;
7869 const u16 max_param_count = ((U16_MAX - sizeof(*cp)) /
7870 sizeof(struct mgmt_conn_param));
7871 u16 param_count, expected_len;
7874 if (!lmp_le_capable(hdev))
7875 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
7876 MGMT_STATUS_NOT_SUPPORTED);
7878 param_count = __le16_to_cpu(cp->param_count);
7879 if (param_count > max_param_count) {
7880 bt_dev_err(hdev, "load_conn_param: too big param_count value %u",
7882 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
7883 MGMT_STATUS_INVALID_PARAMS);
7886 expected_len = struct_size(cp, params, param_count);
7887 if (expected_len != len) {
7888 bt_dev_err(hdev, "load_conn_param: expected %u bytes, got %u bytes",
7890 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM,
7891 MGMT_STATUS_INVALID_PARAMS);
7894 bt_dev_dbg(hdev, "param_count %u", param_count);
7898 hci_conn_params_clear_disabled(hdev);
7900 for (i = 0; i < param_count; i++) {
7901 struct mgmt_conn_param *param = &cp->params[i];
7902 struct hci_conn_params *hci_param;
7903 u16 min, max, latency, timeout;
7906 bt_dev_dbg(hdev, "Adding %pMR (type %u)", ¶m->addr.bdaddr,
7909 if (param->addr.type == BDADDR_LE_PUBLIC) {
7910 addr_type = ADDR_LE_DEV_PUBLIC;
7911 } else if (param->addr.type == BDADDR_LE_RANDOM) {
7912 addr_type = ADDR_LE_DEV_RANDOM;
7914 bt_dev_err(hdev, "ignoring invalid connection parameters");
7918 min = le16_to_cpu(param->min_interval);
7919 max = le16_to_cpu(param->max_interval);
7920 latency = le16_to_cpu(param->latency);
7921 timeout = le16_to_cpu(param->timeout);
7923 bt_dev_dbg(hdev, "min 0x%04x max 0x%04x latency 0x%04x timeout 0x%04x",
7924 min, max, latency, timeout);
7926 if (hci_check_conn_params(min, max, latency, timeout) < 0) {
7927 bt_dev_err(hdev, "ignoring invalid connection parameters");
7931 hci_param = hci_conn_params_add(hdev, ¶m->addr.bdaddr,
7934 bt_dev_err(hdev, "failed to add connection parameters");
7938 hci_param->conn_min_interval = min;
7939 hci_param->conn_max_interval = max;
7940 hci_param->conn_latency = latency;
7941 hci_param->supervision_timeout = timeout;
7944 hci_dev_unlock(hdev);
7946 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_LOAD_CONN_PARAM, 0,
7950 static int set_external_config(struct sock *sk, struct hci_dev *hdev,
7951 void *data, u16 len)
7953 struct mgmt_cp_set_external_config *cp = data;
7957 bt_dev_dbg(hdev, "sock %p", sk);
7959 if (hdev_is_powered(hdev))
7960 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
7961 MGMT_STATUS_REJECTED);
7963 if (cp->config != 0x00 && cp->config != 0x01)
7964 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
7965 MGMT_STATUS_INVALID_PARAMS);
7967 if (!test_bit(HCI_QUIRK_EXTERNAL_CONFIG, &hdev->quirks))
7968 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_EXTERNAL_CONFIG,
7969 MGMT_STATUS_NOT_SUPPORTED);
7974 changed = !hci_dev_test_and_set_flag(hdev, HCI_EXT_CONFIGURED);
7976 changed = hci_dev_test_and_clear_flag(hdev, HCI_EXT_CONFIGURED);
7978 err = send_options_rsp(sk, MGMT_OP_SET_EXTERNAL_CONFIG, hdev);
7985 err = new_options(hdev, sk);
7987 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED) == is_configured(hdev)) {
7988 mgmt_index_removed(hdev);
7990 if (hci_dev_test_and_change_flag(hdev, HCI_UNCONFIGURED)) {
7991 hci_dev_set_flag(hdev, HCI_CONFIG);
7992 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
7994 queue_work(hdev->req_workqueue, &hdev->power_on);
7996 set_bit(HCI_RAW, &hdev->flags);
7997 mgmt_index_added(hdev);
8002 hci_dev_unlock(hdev);
8006 static int set_public_address(struct sock *sk, struct hci_dev *hdev,
8007 void *data, u16 len)
8009 struct mgmt_cp_set_public_address *cp = data;
8013 bt_dev_dbg(hdev, "sock %p", sk);
8015 if (hdev_is_powered(hdev))
8016 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
8017 MGMT_STATUS_REJECTED);
8019 if (!bacmp(&cp->bdaddr, BDADDR_ANY))
8020 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
8021 MGMT_STATUS_INVALID_PARAMS);
8023 if (!hdev->set_bdaddr)
8024 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_SET_PUBLIC_ADDRESS,
8025 MGMT_STATUS_NOT_SUPPORTED);
8029 changed = !!bacmp(&hdev->public_addr, &cp->bdaddr);
8030 bacpy(&hdev->public_addr, &cp->bdaddr);
8032 err = send_options_rsp(sk, MGMT_OP_SET_PUBLIC_ADDRESS, hdev);
8039 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED))
8040 err = new_options(hdev, sk);
8042 if (is_configured(hdev)) {
8043 mgmt_index_removed(hdev);
8045 hci_dev_clear_flag(hdev, HCI_UNCONFIGURED);
8047 hci_dev_set_flag(hdev, HCI_CONFIG);
8048 hci_dev_set_flag(hdev, HCI_AUTO_OFF);
8050 queue_work(hdev->req_workqueue, &hdev->power_on);
8054 hci_dev_unlock(hdev);
8058 static void read_local_oob_ext_data_complete(struct hci_dev *hdev, void *data,
8061 const struct mgmt_cp_read_local_oob_ext_data *mgmt_cp;
8062 struct mgmt_rp_read_local_oob_ext_data *mgmt_rp;
8063 u8 *h192, *r192, *h256, *r256;
8064 struct mgmt_pending_cmd *cmd = data;
8065 struct sk_buff *skb = cmd->skb;
8066 u8 status = mgmt_status(err);
8069 if (cmd != pending_find(MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev))
8074 status = MGMT_STATUS_FAILED;
8075 else if (IS_ERR(skb))
8076 status = mgmt_status(PTR_ERR(skb));
8078 status = mgmt_status(skb->data[0]);
8081 bt_dev_dbg(hdev, "status %u", status);
8083 mgmt_cp = cmd->param;
8086 status = mgmt_status(status);
8093 } else if (!bredr_sc_enabled(hdev)) {
8094 struct hci_rp_read_local_oob_data *rp;
8096 if (skb->len != sizeof(*rp)) {
8097 status = MGMT_STATUS_FAILED;
8100 status = MGMT_STATUS_SUCCESS;
8101 rp = (void *)skb->data;
8103 eir_len = 5 + 18 + 18;
8110 struct hci_rp_read_local_oob_ext_data *rp;
8112 if (skb->len != sizeof(*rp)) {
8113 status = MGMT_STATUS_FAILED;
8116 status = MGMT_STATUS_SUCCESS;
8117 rp = (void *)skb->data;
8119 if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
8120 eir_len = 5 + 18 + 18;
8124 eir_len = 5 + 18 + 18 + 18 + 18;
8134 mgmt_rp = kmalloc(sizeof(*mgmt_rp) + eir_len, GFP_KERNEL);
8141 eir_len = eir_append_data(mgmt_rp->eir, 0, EIR_CLASS_OF_DEV,
8142 hdev->dev_class, 3);
8145 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
8146 EIR_SSP_HASH_C192, h192, 16);
8147 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
8148 EIR_SSP_RAND_R192, r192, 16);
8152 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
8153 EIR_SSP_HASH_C256, h256, 16);
8154 eir_len = eir_append_data(mgmt_rp->eir, eir_len,
8155 EIR_SSP_RAND_R256, r256, 16);
8159 mgmt_rp->type = mgmt_cp->type;
8160 mgmt_rp->eir_len = cpu_to_le16(eir_len);
8162 err = mgmt_cmd_complete(cmd->sk, hdev->id,
8163 MGMT_OP_READ_LOCAL_OOB_EXT_DATA, status,
8164 mgmt_rp, sizeof(*mgmt_rp) + eir_len);
8165 if (err < 0 || status)
8168 hci_sock_set_flag(cmd->sk, HCI_MGMT_OOB_DATA_EVENTS);
8170 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
8171 mgmt_rp, sizeof(*mgmt_rp) + eir_len,
8172 HCI_MGMT_OOB_DATA_EVENTS, cmd->sk);
8174 if (skb && !IS_ERR(skb))
8178 mgmt_pending_remove(cmd);
8181 static int read_local_ssp_oob_req(struct hci_dev *hdev, struct sock *sk,
8182 struct mgmt_cp_read_local_oob_ext_data *cp)
8184 struct mgmt_pending_cmd *cmd;
8187 cmd = mgmt_pending_add(sk, MGMT_OP_READ_LOCAL_OOB_EXT_DATA, hdev,
8192 err = hci_cmd_sync_queue(hdev, read_local_oob_data_sync, cmd,
8193 read_local_oob_ext_data_complete);
8196 mgmt_pending_remove(cmd);
8203 static int read_local_oob_ext_data(struct sock *sk, struct hci_dev *hdev,
8204 void *data, u16 data_len)
8206 struct mgmt_cp_read_local_oob_ext_data *cp = data;
8207 struct mgmt_rp_read_local_oob_ext_data *rp;
8210 u8 status, flags, role, addr[7], hash[16], rand[16];
8213 bt_dev_dbg(hdev, "sock %p", sk);
8215 if (hdev_is_powered(hdev)) {
8217 case BIT(BDADDR_BREDR):
8218 status = mgmt_bredr_support(hdev);
8224 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
8225 status = mgmt_le_support(hdev);
8229 eir_len = 9 + 3 + 18 + 18 + 3;
8232 status = MGMT_STATUS_INVALID_PARAMS;
8237 status = MGMT_STATUS_NOT_POWERED;
8241 rp_len = sizeof(*rp) + eir_len;
8242 rp = kmalloc(rp_len, GFP_ATOMIC);
8246 if (!status && !lmp_ssp_capable(hdev)) {
8247 status = MGMT_STATUS_NOT_SUPPORTED;
8258 case BIT(BDADDR_BREDR):
8259 if (hci_dev_test_flag(hdev, HCI_SSP_ENABLED)) {
8260 err = read_local_ssp_oob_req(hdev, sk, cp);
8261 hci_dev_unlock(hdev);
8265 status = MGMT_STATUS_FAILED;
8268 eir_len = eir_append_data(rp->eir, eir_len,
8270 hdev->dev_class, 3);
8273 case (BIT(BDADDR_LE_PUBLIC) | BIT(BDADDR_LE_RANDOM)):
8274 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED) &&
8275 smp_generate_oob(hdev, hash, rand) < 0) {
8276 hci_dev_unlock(hdev);
8277 status = MGMT_STATUS_FAILED;
8281 /* This should return the active RPA, but since the RPA
8282 * is only programmed on demand, it is really hard to fill
8283 * this in at the moment. For now disallow retrieving
8284 * local out-of-band data when privacy is in use.
8286 * Returning the identity address will not help here since
8287 * pairing happens before the identity resolving key is
8288 * known and thus the connection establishment happens
8289 * based on the RPA and not the identity address.
8291 if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
8292 hci_dev_unlock(hdev);
8293 status = MGMT_STATUS_REJECTED;
8297 if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
8298 !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
8299 (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
8300 bacmp(&hdev->static_addr, BDADDR_ANY))) {
8301 memcpy(addr, &hdev->static_addr, 6);
8304 memcpy(addr, &hdev->bdaddr, 6);
8308 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_BDADDR,
8309 addr, sizeof(addr));
8311 if (hci_dev_test_flag(hdev, HCI_ADVERTISING))
8316 eir_len = eir_append_data(rp->eir, eir_len, EIR_LE_ROLE,
8317 &role, sizeof(role));
8319 if (hci_dev_test_flag(hdev, HCI_SC_ENABLED)) {
8320 eir_len = eir_append_data(rp->eir, eir_len,
8322 hash, sizeof(hash));
8324 eir_len = eir_append_data(rp->eir, eir_len,
8326 rand, sizeof(rand));
8329 flags = mgmt_get_adv_discov_flags(hdev);
8331 if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
8332 flags |= LE_AD_NO_BREDR;
8334 eir_len = eir_append_data(rp->eir, eir_len, EIR_FLAGS,
8335 &flags, sizeof(flags));
8339 hci_dev_unlock(hdev);
8341 hci_sock_set_flag(sk, HCI_MGMT_OOB_DATA_EVENTS);
8343 status = MGMT_STATUS_SUCCESS;
8346 rp->type = cp->type;
8347 rp->eir_len = cpu_to_le16(eir_len);
8349 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_LOCAL_OOB_EXT_DATA,
8350 status, rp, sizeof(*rp) + eir_len);
8351 if (err < 0 || status)
8354 err = mgmt_limited_event(MGMT_EV_LOCAL_OOB_DATA_UPDATED, hdev,
8355 rp, sizeof(*rp) + eir_len,
8356 HCI_MGMT_OOB_DATA_EVENTS, sk);
8364 static u32 get_supported_adv_flags(struct hci_dev *hdev)
8368 flags |= MGMT_ADV_FLAG_CONNECTABLE;
8369 flags |= MGMT_ADV_FLAG_DISCOV;
8370 flags |= MGMT_ADV_FLAG_LIMITED_DISCOV;
8371 flags |= MGMT_ADV_FLAG_MANAGED_FLAGS;
8372 flags |= MGMT_ADV_FLAG_APPEARANCE;
8373 flags |= MGMT_ADV_FLAG_LOCAL_NAME;
8374 flags |= MGMT_ADV_PARAM_DURATION;
8375 flags |= MGMT_ADV_PARAM_TIMEOUT;
8376 flags |= MGMT_ADV_PARAM_INTERVALS;
8377 flags |= MGMT_ADV_PARAM_TX_POWER;
8378 flags |= MGMT_ADV_PARAM_SCAN_RSP;
8380 /* In extended adv TX_POWER returned from Set Adv Param
8381 * will be always valid.
8383 if (hdev->adv_tx_power != HCI_TX_POWER_INVALID || ext_adv_capable(hdev))
8384 flags |= MGMT_ADV_FLAG_TX_POWER;
8386 if (ext_adv_capable(hdev)) {
8387 flags |= MGMT_ADV_FLAG_SEC_1M;
8388 flags |= MGMT_ADV_FLAG_HW_OFFLOAD;
8389 flags |= MGMT_ADV_FLAG_CAN_SET_TX_POWER;
8391 if (hdev->le_features[1] & HCI_LE_PHY_2M)
8392 flags |= MGMT_ADV_FLAG_SEC_2M;
8394 if (hdev->le_features[1] & HCI_LE_PHY_CODED)
8395 flags |= MGMT_ADV_FLAG_SEC_CODED;
8401 static int read_adv_features(struct sock *sk, struct hci_dev *hdev,
8402 void *data, u16 data_len)
8404 struct mgmt_rp_read_adv_features *rp;
8407 struct adv_info *adv_instance;
8408 u32 supported_flags;
8411 bt_dev_dbg(hdev, "sock %p", sk);
8413 if (!lmp_le_capable(hdev))
8414 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
8415 MGMT_STATUS_REJECTED);
8419 rp_len = sizeof(*rp) + hdev->adv_instance_cnt;
8420 rp = kmalloc(rp_len, GFP_ATOMIC);
8422 hci_dev_unlock(hdev);
8426 supported_flags = get_supported_adv_flags(hdev);
8428 rp->supported_flags = cpu_to_le32(supported_flags);
8429 rp->max_adv_data_len = HCI_MAX_AD_LENGTH;
8430 rp->max_scan_rsp_len = HCI_MAX_AD_LENGTH;
8431 rp->max_instances = hdev->le_num_of_adv_sets;
8432 rp->num_instances = hdev->adv_instance_cnt;
8434 instance = rp->instance;
8435 list_for_each_entry(adv_instance, &hdev->adv_instances, list) {
8436 /* Only instances 1-le_num_of_adv_sets are externally visible */
8437 if (adv_instance->instance <= hdev->adv_instance_cnt) {
8438 *instance = adv_instance->instance;
8441 rp->num_instances--;
8446 hci_dev_unlock(hdev);
8448 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_READ_ADV_FEATURES,
8449 MGMT_STATUS_SUCCESS, rp, rp_len);
8456 static u8 calculate_name_len(struct hci_dev *hdev)
8458 u8 buf[HCI_MAX_SHORT_NAME_LENGTH + 3];
8460 return eir_append_local_name(hdev, buf, 0);
8463 static u8 tlv_data_max_len(struct hci_dev *hdev, u32 adv_flags,
8466 u8 max_len = HCI_MAX_AD_LENGTH;
8469 if (adv_flags & (MGMT_ADV_FLAG_DISCOV |
8470 MGMT_ADV_FLAG_LIMITED_DISCOV |
8471 MGMT_ADV_FLAG_MANAGED_FLAGS))
8474 if (adv_flags & MGMT_ADV_FLAG_TX_POWER)
8477 if (adv_flags & MGMT_ADV_FLAG_LOCAL_NAME)
8478 max_len -= calculate_name_len(hdev);
8480 if (adv_flags & (MGMT_ADV_FLAG_APPEARANCE))
8487 static bool flags_managed(u32 adv_flags)
8489 return adv_flags & (MGMT_ADV_FLAG_DISCOV |
8490 MGMT_ADV_FLAG_LIMITED_DISCOV |
8491 MGMT_ADV_FLAG_MANAGED_FLAGS);
8494 static bool tx_power_managed(u32 adv_flags)
8496 return adv_flags & MGMT_ADV_FLAG_TX_POWER;
8499 static bool name_managed(u32 adv_flags)
8501 return adv_flags & MGMT_ADV_FLAG_LOCAL_NAME;
8504 static bool appearance_managed(u32 adv_flags)
8506 return adv_flags & MGMT_ADV_FLAG_APPEARANCE;
8509 static bool tlv_data_is_valid(struct hci_dev *hdev, u32 adv_flags, u8 *data,
8510 u8 len, bool is_adv_data)
8515 max_len = tlv_data_max_len(hdev, adv_flags, is_adv_data);
8520 /* Make sure that the data is correctly formatted. */
8521 for (i = 0; i < len; i += (cur_len + 1)) {
8527 if (data[i + 1] == EIR_FLAGS &&
8528 (!is_adv_data || flags_managed(adv_flags)))
8531 if (data[i + 1] == EIR_TX_POWER && tx_power_managed(adv_flags))
8534 if (data[i + 1] == EIR_NAME_COMPLETE && name_managed(adv_flags))
8537 if (data[i + 1] == EIR_NAME_SHORT && name_managed(adv_flags))
8540 if (data[i + 1] == EIR_APPEARANCE &&
8541 appearance_managed(adv_flags))
8544 /* If the current field length would exceed the total data
8545 * length, then it's invalid.
8547 if (i + cur_len >= len)
8554 static bool requested_adv_flags_are_valid(struct hci_dev *hdev, u32 adv_flags)
8556 u32 supported_flags, phy_flags;
8558 /* The current implementation only supports a subset of the specified
8559 * flags. Also need to check mutual exclusiveness of sec flags.
8561 supported_flags = get_supported_adv_flags(hdev);
8562 phy_flags = adv_flags & MGMT_ADV_FLAG_SEC_MASK;
8563 if (adv_flags & ~supported_flags ||
8564 ((phy_flags && (phy_flags ^ (phy_flags & -phy_flags)))))
8570 static bool adv_busy(struct hci_dev *hdev)
8572 return pending_find(MGMT_OP_SET_LE, hdev);
8575 static void add_adv_complete(struct hci_dev *hdev, struct sock *sk, u8 instance,
8578 struct adv_info *adv, *n;
8580 bt_dev_dbg(hdev, "err %d", err);
8584 list_for_each_entry_safe(adv, n, &hdev->adv_instances, list) {
8591 adv->pending = false;
8595 instance = adv->instance;
8597 if (hdev->cur_adv_instance == instance)
8598 cancel_adv_timeout(hdev);
8600 hci_remove_adv_instance(hdev, instance);
8601 mgmt_advertising_removed(sk, hdev, instance);
8604 hci_dev_unlock(hdev);
8607 static void add_advertising_complete(struct hci_dev *hdev, void *data, int err)
8609 struct mgmt_pending_cmd *cmd = data;
8610 struct mgmt_cp_add_advertising *cp = cmd->param;
8611 struct mgmt_rp_add_advertising rp;
8613 memset(&rp, 0, sizeof(rp));
8615 rp.instance = cp->instance;
8618 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
8621 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
8622 mgmt_status(err), &rp, sizeof(rp));
8624 add_adv_complete(hdev, cmd->sk, cp->instance, err);
8626 mgmt_pending_free(cmd);
8629 static int add_advertising_sync(struct hci_dev *hdev, void *data)
8631 struct mgmt_pending_cmd *cmd = data;
8632 struct mgmt_cp_add_advertising *cp = cmd->param;
8634 return hci_schedule_adv_instance_sync(hdev, cp->instance, true);
8637 static int add_advertising(struct sock *sk, struct hci_dev *hdev,
8638 void *data, u16 data_len)
8640 struct mgmt_cp_add_advertising *cp = data;
8641 struct mgmt_rp_add_advertising rp;
8644 u16 timeout, duration;
8645 unsigned int prev_instance_cnt;
8646 u8 schedule_instance = 0;
8647 struct adv_info *adv, *next_instance;
8649 struct mgmt_pending_cmd *cmd;
8651 bt_dev_dbg(hdev, "sock %p", sk);
8653 status = mgmt_le_support(hdev);
8655 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8658 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
8659 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8660 MGMT_STATUS_INVALID_PARAMS);
8662 if (data_len != sizeof(*cp) + cp->adv_data_len + cp->scan_rsp_len)
8663 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8664 MGMT_STATUS_INVALID_PARAMS);
8666 flags = __le32_to_cpu(cp->flags);
8667 timeout = __le16_to_cpu(cp->timeout);
8668 duration = __le16_to_cpu(cp->duration);
8670 if (!requested_adv_flags_are_valid(hdev, flags))
8671 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8672 MGMT_STATUS_INVALID_PARAMS);
8676 if (timeout && !hdev_is_powered(hdev)) {
8677 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8678 MGMT_STATUS_REJECTED);
8682 if (adv_busy(hdev)) {
8683 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8688 if (!tlv_data_is_valid(hdev, flags, cp->data, cp->adv_data_len, true) ||
8689 !tlv_data_is_valid(hdev, flags, cp->data + cp->adv_data_len,
8690 cp->scan_rsp_len, false)) {
8691 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8692 MGMT_STATUS_INVALID_PARAMS);
8696 prev_instance_cnt = hdev->adv_instance_cnt;
8698 adv = hci_add_adv_instance(hdev, cp->instance, flags,
8699 cp->adv_data_len, cp->data,
8701 cp->data + cp->adv_data_len,
8703 HCI_ADV_TX_POWER_NO_PREFERENCE,
8704 hdev->le_adv_min_interval,
8705 hdev->le_adv_max_interval, 0);
8707 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8708 MGMT_STATUS_FAILED);
8712 /* Only trigger an advertising added event if a new instance was
8715 if (hdev->adv_instance_cnt > prev_instance_cnt)
8716 mgmt_advertising_added(sk, hdev, cp->instance);
8718 if (hdev->cur_adv_instance == cp->instance) {
8719 /* If the currently advertised instance is being changed then
8720 * cancel the current advertising and schedule the next
8721 * instance. If there is only one instance then the overridden
8722 * advertising data will be visible right away.
8724 cancel_adv_timeout(hdev);
8726 next_instance = hci_get_next_instance(hdev, cp->instance);
8728 schedule_instance = next_instance->instance;
8729 } else if (!hdev->adv_instance_timeout) {
8730 /* Immediately advertise the new instance if no other
8731 * instance is currently being advertised.
8733 schedule_instance = cp->instance;
8736 /* If the HCI_ADVERTISING flag is set or the device isn't powered or
8737 * there is no instance to be advertised then we have no HCI
8738 * communication to make. Simply return.
8740 if (!hdev_is_powered(hdev) ||
8741 hci_dev_test_flag(hdev, HCI_ADVERTISING) ||
8742 !schedule_instance) {
8743 rp.instance = cp->instance;
8744 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8745 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
8749 /* We're good to go, update advertising data, parameters, and start
8752 cmd = mgmt_pending_new(sk, MGMT_OP_ADD_ADVERTISING, hdev, data,
8759 cp->instance = schedule_instance;
8761 err = hci_cmd_sync_queue(hdev, add_advertising_sync, cmd,
8762 add_advertising_complete);
8764 mgmt_pending_free(cmd);
8767 hci_dev_unlock(hdev);
8772 static void add_ext_adv_params_complete(struct hci_dev *hdev, void *data,
8775 struct mgmt_pending_cmd *cmd = data;
8776 struct mgmt_cp_add_ext_adv_params *cp = cmd->param;
8777 struct mgmt_rp_add_ext_adv_params rp;
8778 struct adv_info *adv;
8781 BT_DBG("%s", hdev->name);
8785 adv = hci_find_adv_instance(hdev, cp->instance);
8789 rp.instance = cp->instance;
8790 rp.tx_power = adv->tx_power;
8792 /* While we're at it, inform userspace of the available space for this
8793 * advertisement, given the flags that will be used.
8795 flags = __le32_to_cpu(cp->flags);
8796 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
8797 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
8800 /* If this advertisement was previously advertising and we
8801 * failed to update it, we signal that it has been removed and
8802 * delete its structure
8805 mgmt_advertising_removed(cmd->sk, hdev, cp->instance);
8807 hci_remove_adv_instance(hdev, cp->instance);
8809 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
8812 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
8813 mgmt_status(err), &rp, sizeof(rp));
8818 mgmt_pending_free(cmd);
8820 hci_dev_unlock(hdev);
8823 static int add_ext_adv_params_sync(struct hci_dev *hdev, void *data)
8825 struct mgmt_pending_cmd *cmd = data;
8826 struct mgmt_cp_add_ext_adv_params *cp = cmd->param;
8828 return hci_setup_ext_adv_instance_sync(hdev, cp->instance);
8831 static int add_ext_adv_params(struct sock *sk, struct hci_dev *hdev,
8832 void *data, u16 data_len)
8834 struct mgmt_cp_add_ext_adv_params *cp = data;
8835 struct mgmt_rp_add_ext_adv_params rp;
8836 struct mgmt_pending_cmd *cmd = NULL;
8837 struct adv_info *adv;
8838 u32 flags, min_interval, max_interval;
8839 u16 timeout, duration;
8844 BT_DBG("%s", hdev->name);
8846 status = mgmt_le_support(hdev);
8848 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8851 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
8852 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8853 MGMT_STATUS_INVALID_PARAMS);
8855 /* The purpose of breaking add_advertising into two separate MGMT calls
8856 * for params and data is to allow more parameters to be added to this
8857 * structure in the future. For this reason, we verify that we have the
8858 * bare minimum structure we know of when the interface was defined. Any
8859 * extra parameters we don't know about will be ignored in this request.
8861 if (data_len < MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE)
8862 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_ADVERTISING,
8863 MGMT_STATUS_INVALID_PARAMS);
8865 flags = __le32_to_cpu(cp->flags);
8867 if (!requested_adv_flags_are_valid(hdev, flags))
8868 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8869 MGMT_STATUS_INVALID_PARAMS);
8873 /* In new interface, we require that we are powered to register */
8874 if (!hdev_is_powered(hdev)) {
8875 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8876 MGMT_STATUS_REJECTED);
8880 if (adv_busy(hdev)) {
8881 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8886 /* Parse defined parameters from request, use defaults otherwise */
8887 timeout = (flags & MGMT_ADV_PARAM_TIMEOUT) ?
8888 __le16_to_cpu(cp->timeout) : 0;
8890 duration = (flags & MGMT_ADV_PARAM_DURATION) ?
8891 __le16_to_cpu(cp->duration) :
8892 hdev->def_multi_adv_rotation_duration;
8894 min_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ?
8895 __le32_to_cpu(cp->min_interval) :
8896 hdev->le_adv_min_interval;
8898 max_interval = (flags & MGMT_ADV_PARAM_INTERVALS) ?
8899 __le32_to_cpu(cp->max_interval) :
8900 hdev->le_adv_max_interval;
8902 tx_power = (flags & MGMT_ADV_PARAM_TX_POWER) ?
8904 HCI_ADV_TX_POWER_NO_PREFERENCE;
8906 /* Create advertising instance with no advertising or response data */
8907 adv = hci_add_adv_instance(hdev, cp->instance, flags, 0, NULL, 0, NULL,
8908 timeout, duration, tx_power, min_interval,
8912 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_PARAMS,
8913 MGMT_STATUS_FAILED);
8917 /* Submit request for advertising params if ext adv available */
8918 if (ext_adv_capable(hdev)) {
8919 cmd = mgmt_pending_new(sk, MGMT_OP_ADD_EXT_ADV_PARAMS, hdev,
8923 hci_remove_adv_instance(hdev, cp->instance);
8927 err = hci_cmd_sync_queue(hdev, add_ext_adv_params_sync, cmd,
8928 add_ext_adv_params_complete);
8930 mgmt_pending_free(cmd);
8932 rp.instance = cp->instance;
8933 rp.tx_power = HCI_ADV_TX_POWER_NO_PREFERENCE;
8934 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
8935 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
8936 err = mgmt_cmd_complete(sk, hdev->id,
8937 MGMT_OP_ADD_EXT_ADV_PARAMS,
8938 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
8942 hci_dev_unlock(hdev);
8947 static void add_ext_adv_data_complete(struct hci_dev *hdev, void *data, int err)
8949 struct mgmt_pending_cmd *cmd = data;
8950 struct mgmt_cp_add_ext_adv_data *cp = cmd->param;
8951 struct mgmt_rp_add_advertising rp;
8953 add_adv_complete(hdev, cmd->sk, cp->instance, err);
8955 memset(&rp, 0, sizeof(rp));
8957 rp.instance = cp->instance;
8960 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
8963 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
8964 mgmt_status(err), &rp, sizeof(rp));
8966 mgmt_pending_free(cmd);
8969 static int add_ext_adv_data_sync(struct hci_dev *hdev, void *data)
8971 struct mgmt_pending_cmd *cmd = data;
8972 struct mgmt_cp_add_ext_adv_data *cp = cmd->param;
8975 if (ext_adv_capable(hdev)) {
8976 err = hci_update_adv_data_sync(hdev, cp->instance);
8980 err = hci_update_scan_rsp_data_sync(hdev, cp->instance);
8984 return hci_enable_ext_advertising_sync(hdev, cp->instance);
8987 return hci_schedule_adv_instance_sync(hdev, cp->instance, true);
8990 static int add_ext_adv_data(struct sock *sk, struct hci_dev *hdev, void *data,
8993 struct mgmt_cp_add_ext_adv_data *cp = data;
8994 struct mgmt_rp_add_ext_adv_data rp;
8995 u8 schedule_instance = 0;
8996 struct adv_info *next_instance;
8997 struct adv_info *adv_instance;
8999 struct mgmt_pending_cmd *cmd;
9001 BT_DBG("%s", hdev->name);
9005 adv_instance = hci_find_adv_instance(hdev, cp->instance);
9007 if (!adv_instance) {
9008 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9009 MGMT_STATUS_INVALID_PARAMS);
9013 /* In new interface, we require that we are powered to register */
9014 if (!hdev_is_powered(hdev)) {
9015 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9016 MGMT_STATUS_REJECTED);
9017 goto clear_new_instance;
9020 if (adv_busy(hdev)) {
9021 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9023 goto clear_new_instance;
9026 /* Validate new data */
9027 if (!tlv_data_is_valid(hdev, adv_instance->flags, cp->data,
9028 cp->adv_data_len, true) ||
9029 !tlv_data_is_valid(hdev, adv_instance->flags, cp->data +
9030 cp->adv_data_len, cp->scan_rsp_len, false)) {
9031 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9032 MGMT_STATUS_INVALID_PARAMS);
9033 goto clear_new_instance;
9036 /* Set the data in the advertising instance */
9037 hci_set_adv_instance_data(hdev, cp->instance, cp->adv_data_len,
9038 cp->data, cp->scan_rsp_len,
9039 cp->data + cp->adv_data_len);
9041 /* If using software rotation, determine next instance to use */
9042 if (hdev->cur_adv_instance == cp->instance) {
9043 /* If the currently advertised instance is being changed
9044 * then cancel the current advertising and schedule the
9045 * next instance. If there is only one instance then the
9046 * overridden advertising data will be visible right
9049 cancel_adv_timeout(hdev);
9051 next_instance = hci_get_next_instance(hdev, cp->instance);
9053 schedule_instance = next_instance->instance;
9054 } else if (!hdev->adv_instance_timeout) {
9055 /* Immediately advertise the new instance if no other
9056 * instance is currently being advertised.
9058 schedule_instance = cp->instance;
9061 /* If the HCI_ADVERTISING flag is set or there is no instance to
9062 * be advertised then we have no HCI communication to make.
9065 if (hci_dev_test_flag(hdev, HCI_ADVERTISING) || !schedule_instance) {
9066 if (adv_instance->pending) {
9067 mgmt_advertising_added(sk, hdev, cp->instance);
9068 adv_instance->pending = false;
9070 rp.instance = cp->instance;
9071 err = mgmt_cmd_complete(sk, hdev->id, MGMT_OP_ADD_EXT_ADV_DATA,
9072 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
9076 cmd = mgmt_pending_new(sk, MGMT_OP_ADD_EXT_ADV_DATA, hdev, data,
9080 goto clear_new_instance;
9083 err = hci_cmd_sync_queue(hdev, add_ext_adv_data_sync, cmd,
9084 add_ext_adv_data_complete);
9086 mgmt_pending_free(cmd);
9087 goto clear_new_instance;
9090 /* We were successful in updating data, so trigger advertising_added
9091 * event if this is an instance that wasn't previously advertising. If
9092 * a failure occurs in the requests we initiated, we will remove the
9093 * instance again in add_advertising_complete
9095 if (adv_instance->pending)
9096 mgmt_advertising_added(sk, hdev, cp->instance);
9101 hci_remove_adv_instance(hdev, cp->instance);
9104 hci_dev_unlock(hdev);
9109 static void remove_advertising_complete(struct hci_dev *hdev, void *data,
9112 struct mgmt_pending_cmd *cmd = data;
9113 struct mgmt_cp_remove_advertising *cp = cmd->param;
9114 struct mgmt_rp_remove_advertising rp;
9116 bt_dev_dbg(hdev, "err %d", err);
9118 memset(&rp, 0, sizeof(rp));
9119 rp.instance = cp->instance;
9122 mgmt_cmd_status(cmd->sk, cmd->index, cmd->opcode,
9125 mgmt_cmd_complete(cmd->sk, cmd->index, cmd->opcode,
9126 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
9128 mgmt_pending_free(cmd);
9131 static int remove_advertising_sync(struct hci_dev *hdev, void *data)
9133 struct mgmt_pending_cmd *cmd = data;
9134 struct mgmt_cp_remove_advertising *cp = cmd->param;
9137 err = hci_remove_advertising_sync(hdev, cmd->sk, cp->instance, true);
9141 if (list_empty(&hdev->adv_instances))
9142 err = hci_disable_advertising_sync(hdev);
9147 static int remove_advertising(struct sock *sk, struct hci_dev *hdev,
9148 void *data, u16 data_len)
9150 struct mgmt_cp_remove_advertising *cp = data;
9151 struct mgmt_pending_cmd *cmd;
9154 bt_dev_dbg(hdev, "sock %p", sk);
9158 if (cp->instance && !hci_find_adv_instance(hdev, cp->instance)) {
9159 err = mgmt_cmd_status(sk, hdev->id,
9160 MGMT_OP_REMOVE_ADVERTISING,
9161 MGMT_STATUS_INVALID_PARAMS);
9165 if (pending_find(MGMT_OP_SET_LE, hdev)) {
9166 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
9171 if (list_empty(&hdev->adv_instances)) {
9172 err = mgmt_cmd_status(sk, hdev->id, MGMT_OP_REMOVE_ADVERTISING,
9173 MGMT_STATUS_INVALID_PARAMS);
9177 cmd = mgmt_pending_new(sk, MGMT_OP_REMOVE_ADVERTISING, hdev, data,
9184 err = hci_cmd_sync_queue(hdev, remove_advertising_sync, cmd,
9185 remove_advertising_complete);
9187 mgmt_pending_free(cmd);
9190 hci_dev_unlock(hdev);
9195 static int get_adv_size_info(struct sock *sk, struct hci_dev *hdev,
9196 void *data, u16 data_len)
9198 struct mgmt_cp_get_adv_size_info *cp = data;
9199 struct mgmt_rp_get_adv_size_info rp;
9200 u32 flags, supported_flags;
9202 bt_dev_dbg(hdev, "sock %p", sk);
9204 if (!lmp_le_capable(hdev))
9205 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
9206 MGMT_STATUS_REJECTED);
9208 if (cp->instance < 1 || cp->instance > hdev->le_num_of_adv_sets)
9209 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
9210 MGMT_STATUS_INVALID_PARAMS);
9212 flags = __le32_to_cpu(cp->flags);
9214 /* The current implementation only supports a subset of the specified
9217 supported_flags = get_supported_adv_flags(hdev);
9218 if (flags & ~supported_flags)
9219 return mgmt_cmd_status(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
9220 MGMT_STATUS_INVALID_PARAMS);
9222 rp.instance = cp->instance;
9223 rp.flags = cp->flags;
9224 rp.max_adv_data_len = tlv_data_max_len(hdev, flags, true);
9225 rp.max_scan_rsp_len = tlv_data_max_len(hdev, flags, false);
9227 return mgmt_cmd_complete(sk, hdev->id, MGMT_OP_GET_ADV_SIZE_INFO,
9228 MGMT_STATUS_SUCCESS, &rp, sizeof(rp));
9231 static const struct hci_mgmt_handler mgmt_handlers[] = {
9232 { NULL }, /* 0x0000 (no command) */
9233 { read_version, MGMT_READ_VERSION_SIZE,
9235 HCI_MGMT_UNTRUSTED },
9236 { read_commands, MGMT_READ_COMMANDS_SIZE,
9238 HCI_MGMT_UNTRUSTED },
9239 { read_index_list, MGMT_READ_INDEX_LIST_SIZE,
9241 HCI_MGMT_UNTRUSTED },
9242 { read_controller_info, MGMT_READ_INFO_SIZE,
9243 HCI_MGMT_UNTRUSTED },
9244 { set_powered, MGMT_SETTING_SIZE },
9245 { set_discoverable, MGMT_SET_DISCOVERABLE_SIZE },
9246 { set_connectable, MGMT_SETTING_SIZE },
9247 { set_fast_connectable, MGMT_SETTING_SIZE },
9248 { set_bondable, MGMT_SETTING_SIZE },
9249 { set_link_security, MGMT_SETTING_SIZE },
9250 { set_ssp, MGMT_SETTING_SIZE },
9251 { set_hs, MGMT_SETTING_SIZE },
9252 { set_le, MGMT_SETTING_SIZE },
9253 { set_dev_class, MGMT_SET_DEV_CLASS_SIZE },
9254 { set_local_name, MGMT_SET_LOCAL_NAME_SIZE },
9255 { add_uuid, MGMT_ADD_UUID_SIZE },
9256 { remove_uuid, MGMT_REMOVE_UUID_SIZE },
9257 { load_link_keys, MGMT_LOAD_LINK_KEYS_SIZE,
9259 { load_long_term_keys, MGMT_LOAD_LONG_TERM_KEYS_SIZE,
9261 { disconnect, MGMT_DISCONNECT_SIZE },
9262 { get_connections, MGMT_GET_CONNECTIONS_SIZE },
9263 { pin_code_reply, MGMT_PIN_CODE_REPLY_SIZE },
9264 { pin_code_neg_reply, MGMT_PIN_CODE_NEG_REPLY_SIZE },
9265 { set_io_capability, MGMT_SET_IO_CAPABILITY_SIZE },
9266 { pair_device, MGMT_PAIR_DEVICE_SIZE },
9267 { cancel_pair_device, MGMT_CANCEL_PAIR_DEVICE_SIZE },
9268 { unpair_device, MGMT_UNPAIR_DEVICE_SIZE },
9269 { user_confirm_reply, MGMT_USER_CONFIRM_REPLY_SIZE },
9270 { user_confirm_neg_reply, MGMT_USER_CONFIRM_NEG_REPLY_SIZE },
9271 { user_passkey_reply, MGMT_USER_PASSKEY_REPLY_SIZE },
9272 { user_passkey_neg_reply, MGMT_USER_PASSKEY_NEG_REPLY_SIZE },
9273 { read_local_oob_data, MGMT_READ_LOCAL_OOB_DATA_SIZE },
9274 { add_remote_oob_data, MGMT_ADD_REMOTE_OOB_DATA_SIZE,
9276 { remove_remote_oob_data, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE },
9277 { start_discovery, MGMT_START_DISCOVERY_SIZE },
9278 { stop_discovery, MGMT_STOP_DISCOVERY_SIZE },
9279 { confirm_name, MGMT_CONFIRM_NAME_SIZE },
9280 { block_device, MGMT_BLOCK_DEVICE_SIZE },
9281 { unblock_device, MGMT_UNBLOCK_DEVICE_SIZE },
9282 { set_device_id, MGMT_SET_DEVICE_ID_SIZE },
9283 { set_advertising, MGMT_SETTING_SIZE },
9284 { set_bredr, MGMT_SETTING_SIZE },
9285 { set_static_address, MGMT_SET_STATIC_ADDRESS_SIZE },
9286 { set_scan_params, MGMT_SET_SCAN_PARAMS_SIZE },
9287 { set_secure_conn, MGMT_SETTING_SIZE },
9288 { set_debug_keys, MGMT_SETTING_SIZE },
9289 { set_privacy, MGMT_SET_PRIVACY_SIZE },
9290 { load_irks, MGMT_LOAD_IRKS_SIZE,
9292 { get_conn_info, MGMT_GET_CONN_INFO_SIZE },
9293 { get_clock_info, MGMT_GET_CLOCK_INFO_SIZE },
9294 { add_device, MGMT_ADD_DEVICE_SIZE },
9295 { remove_device, MGMT_REMOVE_DEVICE_SIZE },
9296 { load_conn_param, MGMT_LOAD_CONN_PARAM_SIZE,
9298 { read_unconf_index_list, MGMT_READ_UNCONF_INDEX_LIST_SIZE,
9300 HCI_MGMT_UNTRUSTED },
9301 { read_config_info, MGMT_READ_CONFIG_INFO_SIZE,
9302 HCI_MGMT_UNCONFIGURED |
9303 HCI_MGMT_UNTRUSTED },
9304 { set_external_config, MGMT_SET_EXTERNAL_CONFIG_SIZE,
9305 HCI_MGMT_UNCONFIGURED },
9306 { set_public_address, MGMT_SET_PUBLIC_ADDRESS_SIZE,
9307 HCI_MGMT_UNCONFIGURED },
9308 { start_service_discovery, MGMT_START_SERVICE_DISCOVERY_SIZE,
9310 { read_local_oob_ext_data, MGMT_READ_LOCAL_OOB_EXT_DATA_SIZE },
9311 { read_ext_index_list, MGMT_READ_EXT_INDEX_LIST_SIZE,
9313 HCI_MGMT_UNTRUSTED },
9314 { read_adv_features, MGMT_READ_ADV_FEATURES_SIZE },
9315 { add_advertising, MGMT_ADD_ADVERTISING_SIZE,
9317 { remove_advertising, MGMT_REMOVE_ADVERTISING_SIZE },
9318 { get_adv_size_info, MGMT_GET_ADV_SIZE_INFO_SIZE },
9319 { start_limited_discovery, MGMT_START_DISCOVERY_SIZE },
9320 { read_ext_controller_info,MGMT_READ_EXT_INFO_SIZE,
9321 HCI_MGMT_UNTRUSTED },
9322 { set_appearance, MGMT_SET_APPEARANCE_SIZE },
9323 { get_phy_configuration, MGMT_GET_PHY_CONFIGURATION_SIZE },
9324 { set_phy_configuration, MGMT_SET_PHY_CONFIGURATION_SIZE },
9325 { set_blocked_keys, MGMT_OP_SET_BLOCKED_KEYS_SIZE,
9327 { set_wideband_speech, MGMT_SETTING_SIZE },
9328 { read_controller_cap, MGMT_READ_CONTROLLER_CAP_SIZE,
9329 HCI_MGMT_UNTRUSTED },
9330 { read_exp_features_info, MGMT_READ_EXP_FEATURES_INFO_SIZE,
9331 HCI_MGMT_UNTRUSTED |
9332 HCI_MGMT_HDEV_OPTIONAL },
9333 { set_exp_feature, MGMT_SET_EXP_FEATURE_SIZE,
9335 HCI_MGMT_HDEV_OPTIONAL },
9336 { read_def_system_config, MGMT_READ_DEF_SYSTEM_CONFIG_SIZE,
9337 HCI_MGMT_UNTRUSTED },
9338 { set_def_system_config, MGMT_SET_DEF_SYSTEM_CONFIG_SIZE,
9340 { read_def_runtime_config, MGMT_READ_DEF_RUNTIME_CONFIG_SIZE,
9341 HCI_MGMT_UNTRUSTED },
9342 { set_def_runtime_config, MGMT_SET_DEF_RUNTIME_CONFIG_SIZE,
9344 { get_device_flags, MGMT_GET_DEVICE_FLAGS_SIZE },
9345 { set_device_flags, MGMT_SET_DEVICE_FLAGS_SIZE },
9346 { read_adv_mon_features, MGMT_READ_ADV_MONITOR_FEATURES_SIZE },
9347 { add_adv_patterns_monitor,MGMT_ADD_ADV_PATTERNS_MONITOR_SIZE,
9349 { remove_adv_monitor, MGMT_REMOVE_ADV_MONITOR_SIZE },
9350 { add_ext_adv_params, MGMT_ADD_EXT_ADV_PARAMS_MIN_SIZE,
9352 { add_ext_adv_data, MGMT_ADD_EXT_ADV_DATA_SIZE,
9354 { add_adv_patterns_monitor_rssi,
9355 MGMT_ADD_ADV_PATTERNS_MONITOR_RSSI_SIZE },
9356 { set_mesh, MGMT_SET_MESH_RECEIVER_SIZE,
9358 { mesh_features, MGMT_MESH_READ_FEATURES_SIZE },
9359 { mesh_send, MGMT_MESH_SEND_SIZE,
9361 { mesh_send_cancel, MGMT_MESH_SEND_CANCEL_SIZE },
9364 void mgmt_index_added(struct hci_dev *hdev)
9366 struct mgmt_ev_ext_index ev;
9368 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
9371 switch (hdev->dev_type) {
9373 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
9374 mgmt_index_event(MGMT_EV_UNCONF_INDEX_ADDED, hdev,
9375 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
9378 mgmt_index_event(MGMT_EV_INDEX_ADDED, hdev, NULL, 0,
9379 HCI_MGMT_INDEX_EVENTS);
9392 mgmt_index_event(MGMT_EV_EXT_INDEX_ADDED, hdev, &ev, sizeof(ev),
9393 HCI_MGMT_EXT_INDEX_EVENTS);
9396 void mgmt_index_removed(struct hci_dev *hdev)
9398 struct mgmt_ev_ext_index ev;
9399 u8 status = MGMT_STATUS_INVALID_INDEX;
9401 if (test_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks))
9404 switch (hdev->dev_type) {
9406 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
9408 if (hci_dev_test_flag(hdev, HCI_UNCONFIGURED)) {
9409 mgmt_index_event(MGMT_EV_UNCONF_INDEX_REMOVED, hdev,
9410 NULL, 0, HCI_MGMT_UNCONF_INDEX_EVENTS);
9413 mgmt_index_event(MGMT_EV_INDEX_REMOVED, hdev, NULL, 0,
9414 HCI_MGMT_INDEX_EVENTS);
9427 mgmt_index_event(MGMT_EV_EXT_INDEX_REMOVED, hdev, &ev, sizeof(ev),
9428 HCI_MGMT_EXT_INDEX_EVENTS);
9430 /* Cancel any remaining timed work */
9431 if (!hci_dev_test_flag(hdev, HCI_MGMT))
9433 cancel_delayed_work_sync(&hdev->discov_off);
9434 cancel_delayed_work_sync(&hdev->service_cache);
9435 cancel_delayed_work_sync(&hdev->rpa_expired);
9438 void mgmt_power_on(struct hci_dev *hdev, int err)
9440 struct cmd_lookup match = { NULL, hdev };
9442 bt_dev_dbg(hdev, "err %d", err);
9447 restart_le_actions(hdev);
9448 hci_update_passive_scan(hdev);
9451 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
9453 new_settings(hdev, match.sk);
9458 hci_dev_unlock(hdev);
9461 void __mgmt_power_off(struct hci_dev *hdev)
9463 struct cmd_lookup match = { NULL, hdev };
9464 u8 status, zero_cod[] = { 0, 0, 0 };
9466 mgmt_pending_foreach(MGMT_OP_SET_POWERED, hdev, settings_rsp, &match);
9468 /* If the power off is because of hdev unregistration let
9469 * use the appropriate INVALID_INDEX status. Otherwise use
9470 * NOT_POWERED. We cover both scenarios here since later in
9471 * mgmt_index_removed() any hci_conn callbacks will have already
9472 * been triggered, potentially causing misleading DISCONNECTED
9475 if (hci_dev_test_flag(hdev, HCI_UNREGISTER))
9476 status = MGMT_STATUS_INVALID_INDEX;
9478 status = MGMT_STATUS_NOT_POWERED;
9480 mgmt_pending_foreach(0, hdev, cmd_complete_rsp, &status);
9482 if (memcmp(hdev->dev_class, zero_cod, sizeof(zero_cod)) != 0) {
9483 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev,
9484 zero_cod, sizeof(zero_cod),
9485 HCI_MGMT_DEV_CLASS_EVENTS, NULL);
9486 ext_info_changed(hdev, NULL);
9489 new_settings(hdev, match.sk);
9495 void mgmt_set_powered_failed(struct hci_dev *hdev, int err)
9497 struct mgmt_pending_cmd *cmd;
9500 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
9504 if (err == -ERFKILL)
9505 status = MGMT_STATUS_RFKILLED;
9507 status = MGMT_STATUS_FAILED;
9509 mgmt_cmd_status(cmd->sk, hdev->id, MGMT_OP_SET_POWERED, status);
9511 mgmt_pending_remove(cmd);
9514 void mgmt_new_link_key(struct hci_dev *hdev, struct link_key *key,
9517 struct mgmt_ev_new_link_key ev;
9519 memset(&ev, 0, sizeof(ev));
9521 ev.store_hint = persistent;
9522 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
9523 ev.key.addr.type = BDADDR_BREDR;
9524 ev.key.type = key->type;
9525 memcpy(ev.key.val, key->val, HCI_LINK_KEY_SIZE);
9526 ev.key.pin_len = key->pin_len;
9528 mgmt_event(MGMT_EV_NEW_LINK_KEY, hdev, &ev, sizeof(ev), NULL);
9531 static u8 mgmt_ltk_type(struct smp_ltk *ltk)
9533 switch (ltk->type) {
9535 case SMP_LTK_RESPONDER:
9536 if (ltk->authenticated)
9537 return MGMT_LTK_AUTHENTICATED;
9538 return MGMT_LTK_UNAUTHENTICATED;
9540 if (ltk->authenticated)
9541 return MGMT_LTK_P256_AUTH;
9542 return MGMT_LTK_P256_UNAUTH;
9543 case SMP_LTK_P256_DEBUG:
9544 return MGMT_LTK_P256_DEBUG;
9547 return MGMT_LTK_UNAUTHENTICATED;
9550 void mgmt_new_ltk(struct hci_dev *hdev, struct smp_ltk *key, bool persistent)
9552 struct mgmt_ev_new_long_term_key ev;
9554 memset(&ev, 0, sizeof(ev));
9556 /* Devices using resolvable or non-resolvable random addresses
9557 * without providing an identity resolving key don't require
9558 * to store long term keys. Their addresses will change the
9561 * Only when a remote device provides an identity address
9562 * make sure the long term key is stored. If the remote
9563 * identity is known, the long term keys are internally
9564 * mapped to the identity address. So allow static random
9565 * and public addresses here.
9567 if (key->bdaddr_type == ADDR_LE_DEV_RANDOM &&
9568 (key->bdaddr.b[5] & 0xc0) != 0xc0)
9569 ev.store_hint = 0x00;
9571 ev.store_hint = persistent;
9573 bacpy(&ev.key.addr.bdaddr, &key->bdaddr);
9574 ev.key.addr.type = link_to_bdaddr(LE_LINK, key->bdaddr_type);
9575 ev.key.type = mgmt_ltk_type(key);
9576 ev.key.enc_size = key->enc_size;
9577 ev.key.ediv = key->ediv;
9578 ev.key.rand = key->rand;
9580 if (key->type == SMP_LTK)
9581 ev.key.initiator = 1;
9583 /* Make sure we copy only the significant bytes based on the
9584 * encryption key size, and set the rest of the value to zeroes.
9586 memcpy(ev.key.val, key->val, key->enc_size);
9587 memset(ev.key.val + key->enc_size, 0,
9588 sizeof(ev.key.val) - key->enc_size);
9590 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY, hdev, &ev, sizeof(ev), NULL);
9593 void mgmt_new_irk(struct hci_dev *hdev, struct smp_irk *irk, bool persistent)
9595 struct mgmt_ev_new_irk ev;
9597 memset(&ev, 0, sizeof(ev));
9599 ev.store_hint = persistent;
9601 bacpy(&ev.rpa, &irk->rpa);
9602 bacpy(&ev.irk.addr.bdaddr, &irk->bdaddr);
9603 ev.irk.addr.type = link_to_bdaddr(LE_LINK, irk->addr_type);
9604 memcpy(ev.irk.val, irk->val, sizeof(irk->val));
9606 mgmt_event(MGMT_EV_NEW_IRK, hdev, &ev, sizeof(ev), NULL);
9609 void mgmt_new_csrk(struct hci_dev *hdev, struct smp_csrk *csrk,
9612 struct mgmt_ev_new_csrk ev;
9614 memset(&ev, 0, sizeof(ev));
9616 /* Devices using resolvable or non-resolvable random addresses
9617 * without providing an identity resolving key don't require
9618 * to store signature resolving keys. Their addresses will change
9619 * the next time around.
9621 * Only when a remote device provides an identity address
9622 * make sure the signature resolving key is stored. So allow
9623 * static random and public addresses here.
9625 if (csrk->bdaddr_type == ADDR_LE_DEV_RANDOM &&
9626 (csrk->bdaddr.b[5] & 0xc0) != 0xc0)
9627 ev.store_hint = 0x00;
9629 ev.store_hint = persistent;
9631 bacpy(&ev.key.addr.bdaddr, &csrk->bdaddr);
9632 ev.key.addr.type = link_to_bdaddr(LE_LINK, csrk->bdaddr_type);
9633 ev.key.type = csrk->type;
9634 memcpy(ev.key.val, csrk->val, sizeof(csrk->val));
9636 mgmt_event(MGMT_EV_NEW_CSRK, hdev, &ev, sizeof(ev), NULL);
9639 void mgmt_new_conn_param(struct hci_dev *hdev, bdaddr_t *bdaddr,
9640 u8 bdaddr_type, u8 store_hint, u16 min_interval,
9641 u16 max_interval, u16 latency, u16 timeout)
9643 struct mgmt_ev_new_conn_param ev;
9645 if (!hci_is_identity_address(bdaddr, bdaddr_type))
9648 memset(&ev, 0, sizeof(ev));
9649 bacpy(&ev.addr.bdaddr, bdaddr);
9650 ev.addr.type = link_to_bdaddr(LE_LINK, bdaddr_type);
9651 ev.store_hint = store_hint;
9652 ev.min_interval = cpu_to_le16(min_interval);
9653 ev.max_interval = cpu_to_le16(max_interval);
9654 ev.latency = cpu_to_le16(latency);
9655 ev.timeout = cpu_to_le16(timeout);
9657 mgmt_event(MGMT_EV_NEW_CONN_PARAM, hdev, &ev, sizeof(ev), NULL);
9660 void mgmt_device_connected(struct hci_dev *hdev, struct hci_conn *conn,
9661 u8 *name, u8 name_len)
9663 struct sk_buff *skb;
9664 struct mgmt_ev_device_connected *ev;
9668 /* allocate buff for LE or BR/EDR adv */
9669 if (conn->le_adv_data_len > 0)
9670 skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_CONNECTED,
9671 sizeof(*ev) + conn->le_adv_data_len);
9673 skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_CONNECTED,
9674 sizeof(*ev) + (name ? eir_precalc_len(name_len) : 0) +
9675 eir_precalc_len(sizeof(conn->dev_class)));
9677 ev = skb_put(skb, sizeof(*ev));
9678 bacpy(&ev->addr.bdaddr, &conn->dst);
9679 ev->addr.type = link_to_bdaddr(conn->type, conn->dst_type);
9682 flags |= MGMT_DEV_FOUND_INITIATED_CONN;
9684 ev->flags = __cpu_to_le32(flags);
9686 /* We must ensure that the EIR Data fields are ordered and
9687 * unique. Keep it simple for now and avoid the problem by not
9688 * adding any BR/EDR data to the LE adv.
9690 if (conn->le_adv_data_len > 0) {
9691 skb_put_data(skb, conn->le_adv_data, conn->le_adv_data_len);
9692 eir_len = conn->le_adv_data_len;
9695 eir_len += eir_skb_put_data(skb, EIR_NAME_COMPLETE, name, name_len);
9697 if (memcmp(conn->dev_class, "\0\0\0", sizeof(conn->dev_class)))
9698 eir_len += eir_skb_put_data(skb, EIR_CLASS_OF_DEV,
9699 conn->dev_class, sizeof(conn->dev_class));
9702 ev->eir_len = cpu_to_le16(eir_len);
9704 mgmt_event_skb(skb, NULL);
9707 static void disconnect_rsp(struct mgmt_pending_cmd *cmd, void *data)
9709 struct sock **sk = data;
9711 cmd->cmd_complete(cmd, 0);
9716 mgmt_pending_remove(cmd);
9719 static void unpair_device_rsp(struct mgmt_pending_cmd *cmd, void *data)
9721 struct hci_dev *hdev = data;
9722 struct mgmt_cp_unpair_device *cp = cmd->param;
9724 device_unpaired(hdev, &cp->addr.bdaddr, cp->addr.type, cmd->sk);
9726 cmd->cmd_complete(cmd, 0);
9727 mgmt_pending_remove(cmd);
9730 bool mgmt_powering_down(struct hci_dev *hdev)
9732 struct mgmt_pending_cmd *cmd;
9733 struct mgmt_mode *cp;
9735 cmd = pending_find(MGMT_OP_SET_POWERED, hdev);
9746 void mgmt_device_disconnected(struct hci_dev *hdev, bdaddr_t *bdaddr,
9747 u8 link_type, u8 addr_type, u8 reason,
9748 bool mgmt_connected)
9750 struct mgmt_ev_device_disconnected ev;
9751 struct sock *sk = NULL;
9753 /* The connection is still in hci_conn_hash so test for 1
9754 * instead of 0 to know if this is the last one.
9756 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
9757 cancel_delayed_work(&hdev->power_off);
9758 queue_work(hdev->req_workqueue, &hdev->power_off.work);
9761 if (!mgmt_connected)
9764 if (link_type != ACL_LINK && link_type != LE_LINK)
9767 mgmt_pending_foreach(MGMT_OP_DISCONNECT, hdev, disconnect_rsp, &sk);
9769 bacpy(&ev.addr.bdaddr, bdaddr);
9770 ev.addr.type = link_to_bdaddr(link_type, addr_type);
9773 /* Report disconnects due to suspend */
9774 if (hdev->suspended)
9775 ev.reason = MGMT_DEV_DISCONN_LOCAL_HOST_SUSPEND;
9777 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED, hdev, &ev, sizeof(ev), sk);
9782 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
9786 void mgmt_disconnect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr,
9787 u8 link_type, u8 addr_type, u8 status)
9789 u8 bdaddr_type = link_to_bdaddr(link_type, addr_type);
9790 struct mgmt_cp_disconnect *cp;
9791 struct mgmt_pending_cmd *cmd;
9793 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE, hdev, unpair_device_rsp,
9796 cmd = pending_find(MGMT_OP_DISCONNECT, hdev);
9802 if (bacmp(bdaddr, &cp->addr.bdaddr))
9805 if (cp->addr.type != bdaddr_type)
9808 cmd->cmd_complete(cmd, mgmt_status(status));
9809 mgmt_pending_remove(cmd);
9812 void mgmt_connect_failed(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
9813 u8 addr_type, u8 status)
9815 struct mgmt_ev_connect_failed ev;
9817 /* The connection is still in hci_conn_hash so test for 1
9818 * instead of 0 to know if this is the last one.
9820 if (mgmt_powering_down(hdev) && hci_conn_count(hdev) == 1) {
9821 cancel_delayed_work(&hdev->power_off);
9822 queue_work(hdev->req_workqueue, &hdev->power_off.work);
9825 bacpy(&ev.addr.bdaddr, bdaddr);
9826 ev.addr.type = link_to_bdaddr(link_type, addr_type);
9827 ev.status = mgmt_status(status);
9829 mgmt_event(MGMT_EV_CONNECT_FAILED, hdev, &ev, sizeof(ev), NULL);
9832 void mgmt_pin_code_request(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 secure)
9834 struct mgmt_ev_pin_code_request ev;
9836 bacpy(&ev.addr.bdaddr, bdaddr);
9837 ev.addr.type = BDADDR_BREDR;
9840 mgmt_event(MGMT_EV_PIN_CODE_REQUEST, hdev, &ev, sizeof(ev), NULL);
9843 void mgmt_pin_code_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
9846 struct mgmt_pending_cmd *cmd;
9848 cmd = pending_find(MGMT_OP_PIN_CODE_REPLY, hdev);
9852 cmd->cmd_complete(cmd, mgmt_status(status));
9853 mgmt_pending_remove(cmd);
9856 void mgmt_pin_code_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
9859 struct mgmt_pending_cmd *cmd;
9861 cmd = pending_find(MGMT_OP_PIN_CODE_NEG_REPLY, hdev);
9865 cmd->cmd_complete(cmd, mgmt_status(status));
9866 mgmt_pending_remove(cmd);
9869 int mgmt_user_confirm_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
9870 u8 link_type, u8 addr_type, u32 value,
9873 struct mgmt_ev_user_confirm_request ev;
9875 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
9877 bacpy(&ev.addr.bdaddr, bdaddr);
9878 ev.addr.type = link_to_bdaddr(link_type, addr_type);
9879 ev.confirm_hint = confirm_hint;
9880 ev.value = cpu_to_le32(value);
9882 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST, hdev, &ev, sizeof(ev),
9886 int mgmt_user_passkey_request(struct hci_dev *hdev, bdaddr_t *bdaddr,
9887 u8 link_type, u8 addr_type)
9889 struct mgmt_ev_user_passkey_request ev;
9891 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
9893 bacpy(&ev.addr.bdaddr, bdaddr);
9894 ev.addr.type = link_to_bdaddr(link_type, addr_type);
9896 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST, hdev, &ev, sizeof(ev),
9900 static int user_pairing_resp_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
9901 u8 link_type, u8 addr_type, u8 status,
9904 struct mgmt_pending_cmd *cmd;
9906 cmd = pending_find(opcode, hdev);
9910 cmd->cmd_complete(cmd, mgmt_status(status));
9911 mgmt_pending_remove(cmd);
9916 int mgmt_user_confirm_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
9917 u8 link_type, u8 addr_type, u8 status)
9919 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
9920 status, MGMT_OP_USER_CONFIRM_REPLY);
9923 int mgmt_user_confirm_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
9924 u8 link_type, u8 addr_type, u8 status)
9926 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
9928 MGMT_OP_USER_CONFIRM_NEG_REPLY);
9931 int mgmt_user_passkey_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
9932 u8 link_type, u8 addr_type, u8 status)
9934 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
9935 status, MGMT_OP_USER_PASSKEY_REPLY);
9938 int mgmt_user_passkey_neg_reply_complete(struct hci_dev *hdev, bdaddr_t *bdaddr,
9939 u8 link_type, u8 addr_type, u8 status)
9941 return user_pairing_resp_complete(hdev, bdaddr, link_type, addr_type,
9943 MGMT_OP_USER_PASSKEY_NEG_REPLY);
9946 int mgmt_user_passkey_notify(struct hci_dev *hdev, bdaddr_t *bdaddr,
9947 u8 link_type, u8 addr_type, u32 passkey,
9950 struct mgmt_ev_passkey_notify ev;
9952 bt_dev_dbg(hdev, "bdaddr %pMR", bdaddr);
9954 bacpy(&ev.addr.bdaddr, bdaddr);
9955 ev.addr.type = link_to_bdaddr(link_type, addr_type);
9956 ev.passkey = __cpu_to_le32(passkey);
9957 ev.entered = entered;
9959 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY, hdev, &ev, sizeof(ev), NULL);
9962 void mgmt_auth_failed(struct hci_conn *conn, u8 hci_status)
9964 struct mgmt_ev_auth_failed ev;
9965 struct mgmt_pending_cmd *cmd;
9966 u8 status = mgmt_status(hci_status);
9968 bacpy(&ev.addr.bdaddr, &conn->dst);
9969 ev.addr.type = link_to_bdaddr(conn->type, conn->dst_type);
9972 cmd = find_pairing(conn);
9974 mgmt_event(MGMT_EV_AUTH_FAILED, conn->hdev, &ev, sizeof(ev),
9975 cmd ? cmd->sk : NULL);
9978 cmd->cmd_complete(cmd, status);
9979 mgmt_pending_remove(cmd);
9983 void mgmt_auth_enable_complete(struct hci_dev *hdev, u8 status)
9985 struct cmd_lookup match = { NULL, hdev };
9989 u8 mgmt_err = mgmt_status(status);
9990 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev,
9991 cmd_status_rsp, &mgmt_err);
9995 if (test_bit(HCI_AUTH, &hdev->flags))
9996 changed = !hci_dev_test_and_set_flag(hdev, HCI_LINK_SECURITY);
9998 changed = hci_dev_test_and_clear_flag(hdev, HCI_LINK_SECURITY);
10000 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY, hdev, settings_rsp,
10004 new_settings(hdev, match.sk);
10007 sock_put(match.sk);
10010 static void sk_lookup(struct mgmt_pending_cmd *cmd, void *data)
10012 struct cmd_lookup *match = data;
10014 if (match->sk == NULL) {
10015 match->sk = cmd->sk;
10016 sock_hold(match->sk);
10020 void mgmt_set_class_of_dev_complete(struct hci_dev *hdev, u8 *dev_class,
10023 struct cmd_lookup match = { NULL, hdev, mgmt_status(status) };
10025 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS, hdev, sk_lookup, &match);
10026 mgmt_pending_foreach(MGMT_OP_ADD_UUID, hdev, sk_lookup, &match);
10027 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID, hdev, sk_lookup, &match);
10030 mgmt_limited_event(MGMT_EV_CLASS_OF_DEV_CHANGED, hdev, dev_class,
10031 3, HCI_MGMT_DEV_CLASS_EVENTS, NULL);
10032 ext_info_changed(hdev, NULL);
10036 sock_put(match.sk);
10039 void mgmt_set_local_name_complete(struct hci_dev *hdev, u8 *name, u8 status)
10041 struct mgmt_cp_set_local_name ev;
10042 struct mgmt_pending_cmd *cmd;
10047 memset(&ev, 0, sizeof(ev));
10048 memcpy(ev.name, name, HCI_MAX_NAME_LENGTH);
10049 memcpy(ev.short_name, hdev->short_name, HCI_MAX_SHORT_NAME_LENGTH);
10051 cmd = pending_find(MGMT_OP_SET_LOCAL_NAME, hdev);
10053 memcpy(hdev->dev_name, name, sizeof(hdev->dev_name));
10055 /* If this is a HCI command related to powering on the
10056 * HCI dev don't send any mgmt signals.
10058 if (pending_find(MGMT_OP_SET_POWERED, hdev))
10062 mgmt_limited_event(MGMT_EV_LOCAL_NAME_CHANGED, hdev, &ev, sizeof(ev),
10063 HCI_MGMT_LOCAL_NAME_EVENTS, cmd ? cmd->sk : NULL);
10064 ext_info_changed(hdev, cmd ? cmd->sk : NULL);
10067 static inline bool has_uuid(u8 *uuid, u16 uuid_count, u8 (*uuids)[16])
10071 for (i = 0; i < uuid_count; i++) {
10072 if (!memcmp(uuid, uuids[i], 16))
10079 static bool eir_has_uuids(u8 *eir, u16 eir_len, u16 uuid_count, u8 (*uuids)[16])
10083 while (parsed < eir_len) {
10084 u8 field_len = eir[0];
10088 if (field_len == 0)
10091 if (eir_len - parsed < field_len + 1)
10095 case EIR_UUID16_ALL:
10096 case EIR_UUID16_SOME:
10097 for (i = 0; i + 3 <= field_len; i += 2) {
10098 memcpy(uuid, bluetooth_base_uuid, 16);
10099 uuid[13] = eir[i + 3];
10100 uuid[12] = eir[i + 2];
10101 if (has_uuid(uuid, uuid_count, uuids))
10105 case EIR_UUID32_ALL:
10106 case EIR_UUID32_SOME:
10107 for (i = 0; i + 5 <= field_len; i += 4) {
10108 memcpy(uuid, bluetooth_base_uuid, 16);
10109 uuid[15] = eir[i + 5];
10110 uuid[14] = eir[i + 4];
10111 uuid[13] = eir[i + 3];
10112 uuid[12] = eir[i + 2];
10113 if (has_uuid(uuid, uuid_count, uuids))
10117 case EIR_UUID128_ALL:
10118 case EIR_UUID128_SOME:
10119 for (i = 0; i + 17 <= field_len; i += 16) {
10120 memcpy(uuid, eir + i + 2, 16);
10121 if (has_uuid(uuid, uuid_count, uuids))
10127 parsed += field_len + 1;
10128 eir += field_len + 1;
10134 static void restart_le_scan(struct hci_dev *hdev)
10136 /* If controller is not scanning we are done. */
10137 if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
10140 if (time_after(jiffies + DISCOV_LE_RESTART_DELAY,
10141 hdev->discovery.scan_start +
10142 hdev->discovery.scan_duration))
10145 queue_delayed_work(hdev->req_workqueue, &hdev->le_scan_restart,
10146 DISCOV_LE_RESTART_DELAY);
10149 static bool is_filter_match(struct hci_dev *hdev, s8 rssi, u8 *eir,
10150 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len)
10152 /* If a RSSI threshold has been specified, and
10153 * HCI_QUIRK_STRICT_DUPLICATE_FILTER is not set, then all results with
10154 * a RSSI smaller than the RSSI threshold will be dropped. If the quirk
10155 * is set, let it through for further processing, as we might need to
10156 * restart the scan.
10158 * For BR/EDR devices (pre 1.2) providing no RSSI during inquiry,
10159 * the results are also dropped.
10161 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
10162 (rssi == HCI_RSSI_INVALID ||
10163 (rssi < hdev->discovery.rssi &&
10164 !test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks))))
10167 if (hdev->discovery.uuid_count != 0) {
10168 /* If a list of UUIDs is provided in filter, results with no
10169 * matching UUID should be dropped.
10171 if (!eir_has_uuids(eir, eir_len, hdev->discovery.uuid_count,
10172 hdev->discovery.uuids) &&
10173 !eir_has_uuids(scan_rsp, scan_rsp_len,
10174 hdev->discovery.uuid_count,
10175 hdev->discovery.uuids))
10179 /* If duplicate filtering does not report RSSI changes, then restart
10180 * scanning to ensure updated result with updated RSSI values.
10182 if (test_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks)) {
10183 restart_le_scan(hdev);
10185 /* Validate RSSI value against the RSSI threshold once more. */
10186 if (hdev->discovery.rssi != HCI_RSSI_INVALID &&
10187 rssi < hdev->discovery.rssi)
10194 void mgmt_adv_monitor_device_lost(struct hci_dev *hdev, u16 handle,
10195 bdaddr_t *bdaddr, u8 addr_type)
10197 struct mgmt_ev_adv_monitor_device_lost ev;
10199 ev.monitor_handle = cpu_to_le16(handle);
10200 bacpy(&ev.addr.bdaddr, bdaddr);
10201 ev.addr.type = addr_type;
10203 mgmt_event(MGMT_EV_ADV_MONITOR_DEVICE_LOST, hdev, &ev, sizeof(ev),
10207 static void mgmt_send_adv_monitor_device_found(struct hci_dev *hdev,
10208 struct sk_buff *skb,
10209 struct sock *skip_sk,
10212 struct sk_buff *advmon_skb;
10213 size_t advmon_skb_len;
10214 __le16 *monitor_handle;
10219 advmon_skb_len = (sizeof(struct mgmt_ev_adv_monitor_device_found) -
10220 sizeof(struct mgmt_ev_device_found)) + skb->len;
10221 advmon_skb = mgmt_alloc_skb(hdev, MGMT_EV_ADV_MONITOR_DEVICE_FOUND,
10226 /* ADV_MONITOR_DEVICE_FOUND is similar to DEVICE_FOUND event except
10227 * that it also has 'monitor_handle'. Make a copy of DEVICE_FOUND and
10228 * store monitor_handle of the matched monitor.
10230 monitor_handle = skb_put(advmon_skb, sizeof(*monitor_handle));
10231 *monitor_handle = cpu_to_le16(handle);
10232 skb_put_data(advmon_skb, skb->data, skb->len);
10234 mgmt_event_skb(advmon_skb, skip_sk);
10237 static void mgmt_adv_monitor_device_found(struct hci_dev *hdev,
10238 bdaddr_t *bdaddr, bool report_device,
10239 struct sk_buff *skb,
10240 struct sock *skip_sk)
10242 struct monitored_device *dev, *tmp;
10243 bool matched = false;
10244 bool notified = false;
10246 /* We have received the Advertisement Report because:
10247 * 1. the kernel has initiated active discovery
10248 * 2. if not, we have pend_le_reports > 0 in which case we are doing
10250 * 3. if none of the above is true, we have one or more active
10251 * Advertisement Monitor
10253 * For case 1 and 2, report all advertisements via MGMT_EV_DEVICE_FOUND
10254 * and report ONLY one advertisement per device for the matched Monitor
10255 * via MGMT_EV_ADV_MONITOR_DEVICE_FOUND event.
10257 * For case 3, since we are not active scanning and all advertisements
10258 * received are due to a matched Advertisement Monitor, report all
10259 * advertisements ONLY via MGMT_EV_ADV_MONITOR_DEVICE_FOUND event.
10261 if (report_device && !hdev->advmon_pend_notify) {
10262 mgmt_event_skb(skb, skip_sk);
10266 hdev->advmon_pend_notify = false;
10268 list_for_each_entry_safe(dev, tmp, &hdev->monitored_devices, list) {
10269 if (!bacmp(&dev->bdaddr, bdaddr)) {
10272 if (!dev->notified) {
10273 mgmt_send_adv_monitor_device_found(hdev, skb,
10277 dev->notified = true;
10281 if (!dev->notified)
10282 hdev->advmon_pend_notify = true;
10285 if (!report_device &&
10286 ((matched && !notified) || !msft_monitor_supported(hdev))) {
10287 /* Handle 0 indicates that we are not active scanning and this
10288 * is a subsequent advertisement report for an already matched
10289 * Advertisement Monitor or the controller offloading support
10290 * is not available.
10292 mgmt_send_adv_monitor_device_found(hdev, skb, skip_sk, 0);
10296 mgmt_event_skb(skb, skip_sk);
10301 static void mesh_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr,
10302 u8 addr_type, s8 rssi, u32 flags, u8 *eir,
10303 u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len,
10306 struct sk_buff *skb;
10307 struct mgmt_ev_mesh_device_found *ev;
10310 if (!hdev->mesh_ad_types[0])
10313 /* Scan for requested AD types */
10315 for (i = 0; i + 1 < eir_len; i += eir[i] + 1) {
10316 for (j = 0; j < sizeof(hdev->mesh_ad_types); j++) {
10317 if (!hdev->mesh_ad_types[j])
10320 if (hdev->mesh_ad_types[j] == eir[i + 1])
10326 if (scan_rsp_len > 0) {
10327 for (i = 0; i + 1 < scan_rsp_len; i += scan_rsp[i] + 1) {
10328 for (j = 0; j < sizeof(hdev->mesh_ad_types); j++) {
10329 if (!hdev->mesh_ad_types[j])
10332 if (hdev->mesh_ad_types[j] == scan_rsp[i + 1])
10341 skb = mgmt_alloc_skb(hdev, MGMT_EV_MESH_DEVICE_FOUND,
10342 sizeof(*ev) + eir_len + scan_rsp_len);
10346 ev = skb_put(skb, sizeof(*ev));
10348 bacpy(&ev->addr.bdaddr, bdaddr);
10349 ev->addr.type = link_to_bdaddr(LE_LINK, addr_type);
10351 ev->flags = cpu_to_le32(flags);
10352 ev->instant = cpu_to_le64(instant);
10355 /* Copy EIR or advertising data into event */
10356 skb_put_data(skb, eir, eir_len);
10358 if (scan_rsp_len > 0)
10359 /* Append scan response data to event */
10360 skb_put_data(skb, scan_rsp, scan_rsp_len);
10362 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
10364 mgmt_event_skb(skb, NULL);
10367 void mgmt_device_found(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
10368 u8 addr_type, u8 *dev_class, s8 rssi, u32 flags,
10369 u8 *eir, u16 eir_len, u8 *scan_rsp, u8 scan_rsp_len,
10372 struct sk_buff *skb;
10373 struct mgmt_ev_device_found *ev;
10374 bool report_device = hci_discovery_active(hdev);
10376 if (hci_dev_test_flag(hdev, HCI_MESH) && link_type == LE_LINK)
10377 mesh_device_found(hdev, bdaddr, addr_type, rssi, flags,
10378 eir, eir_len, scan_rsp, scan_rsp_len,
10381 /* Don't send events for a non-kernel initiated discovery. With
10382 * LE one exception is if we have pend_le_reports > 0 in which
10383 * case we're doing passive scanning and want these events.
10385 if (!hci_discovery_active(hdev)) {
10386 if (link_type == ACL_LINK)
10388 if (link_type == LE_LINK && !list_empty(&hdev->pend_le_reports))
10389 report_device = true;
10390 else if (!hci_is_adv_monitoring(hdev))
10394 if (hdev->discovery.result_filtering) {
10395 /* We are using service discovery */
10396 if (!is_filter_match(hdev, rssi, eir, eir_len, scan_rsp,
10401 if (hdev->discovery.limited) {
10402 /* Check for limited discoverable bit */
10404 if (!(dev_class[1] & 0x20))
10407 u8 *flags = eir_get_data(eir, eir_len, EIR_FLAGS, NULL);
10408 if (!flags || !(flags[0] & LE_AD_LIMITED))
10413 /* Allocate skb. The 5 extra bytes are for the potential CoD field */
10414 skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_FOUND,
10415 sizeof(*ev) + eir_len + scan_rsp_len + 5);
10419 ev = skb_put(skb, sizeof(*ev));
10421 /* In case of device discovery with BR/EDR devices (pre 1.2), the
10422 * RSSI value was reported as 0 when not available. This behavior
10423 * is kept when using device discovery. This is required for full
10424 * backwards compatibility with the API.
10426 * However when using service discovery, the value 127 will be
10427 * returned when the RSSI is not available.
10429 if (rssi == HCI_RSSI_INVALID && !hdev->discovery.report_invalid_rssi &&
10430 link_type == ACL_LINK)
10433 bacpy(&ev->addr.bdaddr, bdaddr);
10434 ev->addr.type = link_to_bdaddr(link_type, addr_type);
10436 ev->flags = cpu_to_le32(flags);
10439 /* Copy EIR or advertising data into event */
10440 skb_put_data(skb, eir, eir_len);
10442 if (dev_class && !eir_get_data(eir, eir_len, EIR_CLASS_OF_DEV, NULL)) {
10445 eir_len += eir_append_data(eir_cod, 0, EIR_CLASS_OF_DEV,
10447 skb_put_data(skb, eir_cod, sizeof(eir_cod));
10450 if (scan_rsp_len > 0)
10451 /* Append scan response data to event */
10452 skb_put_data(skb, scan_rsp, scan_rsp_len);
10454 ev->eir_len = cpu_to_le16(eir_len + scan_rsp_len);
10456 mgmt_adv_monitor_device_found(hdev, bdaddr, report_device, skb, NULL);
10459 void mgmt_remote_name(struct hci_dev *hdev, bdaddr_t *bdaddr, u8 link_type,
10460 u8 addr_type, s8 rssi, u8 *name, u8 name_len)
10462 struct sk_buff *skb;
10463 struct mgmt_ev_device_found *ev;
10467 skb = mgmt_alloc_skb(hdev, MGMT_EV_DEVICE_FOUND,
10468 sizeof(*ev) + (name ? eir_precalc_len(name_len) : 0));
10470 ev = skb_put(skb, sizeof(*ev));
10471 bacpy(&ev->addr.bdaddr, bdaddr);
10472 ev->addr.type = link_to_bdaddr(link_type, addr_type);
10476 eir_len += eir_skb_put_data(skb, EIR_NAME_COMPLETE, name, name_len);
10478 flags = MGMT_DEV_FOUND_NAME_REQUEST_FAILED;
10480 ev->eir_len = cpu_to_le16(eir_len);
10481 ev->flags = cpu_to_le32(flags);
10483 mgmt_event_skb(skb, NULL);
10486 void mgmt_discovering(struct hci_dev *hdev, u8 discovering)
10488 struct mgmt_ev_discovering ev;
10490 bt_dev_dbg(hdev, "discovering %u", discovering);
10492 memset(&ev, 0, sizeof(ev));
10493 ev.type = hdev->discovery.type;
10494 ev.discovering = discovering;
10496 mgmt_event(MGMT_EV_DISCOVERING, hdev, &ev, sizeof(ev), NULL);
10499 void mgmt_suspending(struct hci_dev *hdev, u8 state)
10501 struct mgmt_ev_controller_suspend ev;
10503 ev.suspend_state = state;
10504 mgmt_event(MGMT_EV_CONTROLLER_SUSPEND, hdev, &ev, sizeof(ev), NULL);
10507 void mgmt_resuming(struct hci_dev *hdev, u8 reason, bdaddr_t *bdaddr,
10510 struct mgmt_ev_controller_resume ev;
10512 ev.wake_reason = reason;
10514 bacpy(&ev.addr.bdaddr, bdaddr);
10515 ev.addr.type = addr_type;
10517 memset(&ev.addr, 0, sizeof(ev.addr));
10520 mgmt_event(MGMT_EV_CONTROLLER_RESUME, hdev, &ev, sizeof(ev), NULL);
10523 static struct hci_mgmt_chan chan = {
10524 .channel = HCI_CHANNEL_CONTROL,
10525 .handler_count = ARRAY_SIZE(mgmt_handlers),
10526 .handlers = mgmt_handlers,
10527 .hdev_init = mgmt_init_hdev,
10530 int mgmt_init(void)
10532 return hci_mgmt_chan_register(&chan);
10535 void mgmt_exit(void)
10537 hci_mgmt_chan_unregister(&chan);
10540 void mgmt_cleanup(struct sock *sk)
10542 struct mgmt_mesh_tx *mesh_tx;
10543 struct hci_dev *hdev;
10545 read_lock(&hci_dev_list_lock);
10547 list_for_each_entry(hdev, &hci_dev_list, list) {
10549 mesh_tx = mgmt_mesh_next(hdev, sk);
10552 mesh_send_complete(hdev, mesh_tx, true);
10556 read_unlock(&hci_dev_list_lock);