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/mgmt.h>
36 #define MGMT_VERSION 1
37 #define MGMT_REVISION 5
39 static const u16 mgmt_commands
[] = {
40 MGMT_OP_READ_INDEX_LIST
,
43 MGMT_OP_SET_DISCOVERABLE
,
44 MGMT_OP_SET_CONNECTABLE
,
45 MGMT_OP_SET_FAST_CONNECTABLE
,
47 MGMT_OP_SET_LINK_SECURITY
,
51 MGMT_OP_SET_DEV_CLASS
,
52 MGMT_OP_SET_LOCAL_NAME
,
55 MGMT_OP_LOAD_LINK_KEYS
,
56 MGMT_OP_LOAD_LONG_TERM_KEYS
,
58 MGMT_OP_GET_CONNECTIONS
,
59 MGMT_OP_PIN_CODE_REPLY
,
60 MGMT_OP_PIN_CODE_NEG_REPLY
,
61 MGMT_OP_SET_IO_CAPABILITY
,
63 MGMT_OP_CANCEL_PAIR_DEVICE
,
64 MGMT_OP_UNPAIR_DEVICE
,
65 MGMT_OP_USER_CONFIRM_REPLY
,
66 MGMT_OP_USER_CONFIRM_NEG_REPLY
,
67 MGMT_OP_USER_PASSKEY_REPLY
,
68 MGMT_OP_USER_PASSKEY_NEG_REPLY
,
69 MGMT_OP_READ_LOCAL_OOB_DATA
,
70 MGMT_OP_ADD_REMOTE_OOB_DATA
,
71 MGMT_OP_REMOVE_REMOTE_OOB_DATA
,
72 MGMT_OP_START_DISCOVERY
,
73 MGMT_OP_STOP_DISCOVERY
,
76 MGMT_OP_UNBLOCK_DEVICE
,
77 MGMT_OP_SET_DEVICE_ID
,
78 MGMT_OP_SET_ADVERTISING
,
80 MGMT_OP_SET_STATIC_ADDRESS
,
81 MGMT_OP_SET_SCAN_PARAMS
,
82 MGMT_OP_SET_SECURE_CONN
,
83 MGMT_OP_SET_DEBUG_KEYS
,
87 static const u16 mgmt_events
[] = {
88 MGMT_EV_CONTROLLER_ERROR
,
90 MGMT_EV_INDEX_REMOVED
,
92 MGMT_EV_CLASS_OF_DEV_CHANGED
,
93 MGMT_EV_LOCAL_NAME_CHANGED
,
95 MGMT_EV_NEW_LONG_TERM_KEY
,
96 MGMT_EV_DEVICE_CONNECTED
,
97 MGMT_EV_DEVICE_DISCONNECTED
,
98 MGMT_EV_CONNECT_FAILED
,
99 MGMT_EV_PIN_CODE_REQUEST
,
100 MGMT_EV_USER_CONFIRM_REQUEST
,
101 MGMT_EV_USER_PASSKEY_REQUEST
,
103 MGMT_EV_DEVICE_FOUND
,
105 MGMT_EV_DEVICE_BLOCKED
,
106 MGMT_EV_DEVICE_UNBLOCKED
,
107 MGMT_EV_DEVICE_UNPAIRED
,
108 MGMT_EV_PASSKEY_NOTIFY
,
112 #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
114 #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \
115 !test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
118 struct list_head list
;
126 /* HCI to MGMT error code conversion table */
127 static u8 mgmt_status_table
[] = {
129 MGMT_STATUS_UNKNOWN_COMMAND
, /* Unknown Command */
130 MGMT_STATUS_NOT_CONNECTED
, /* No Connection */
131 MGMT_STATUS_FAILED
, /* Hardware Failure */
132 MGMT_STATUS_CONNECT_FAILED
, /* Page Timeout */
133 MGMT_STATUS_AUTH_FAILED
, /* Authentication Failed */
134 MGMT_STATUS_AUTH_FAILED
, /* PIN or Key Missing */
135 MGMT_STATUS_NO_RESOURCES
, /* Memory Full */
136 MGMT_STATUS_TIMEOUT
, /* Connection Timeout */
137 MGMT_STATUS_NO_RESOURCES
, /* Max Number of Connections */
138 MGMT_STATUS_NO_RESOURCES
, /* Max Number of SCO Connections */
139 MGMT_STATUS_ALREADY_CONNECTED
, /* ACL Connection Exists */
140 MGMT_STATUS_BUSY
, /* Command Disallowed */
141 MGMT_STATUS_NO_RESOURCES
, /* Rejected Limited Resources */
142 MGMT_STATUS_REJECTED
, /* Rejected Security */
143 MGMT_STATUS_REJECTED
, /* Rejected Personal */
144 MGMT_STATUS_TIMEOUT
, /* Host Timeout */
145 MGMT_STATUS_NOT_SUPPORTED
, /* Unsupported Feature */
146 MGMT_STATUS_INVALID_PARAMS
, /* Invalid Parameters */
147 MGMT_STATUS_DISCONNECTED
, /* OE User Ended Connection */
148 MGMT_STATUS_NO_RESOURCES
, /* OE Low Resources */
149 MGMT_STATUS_DISCONNECTED
, /* OE Power Off */
150 MGMT_STATUS_DISCONNECTED
, /* Connection Terminated */
151 MGMT_STATUS_BUSY
, /* Repeated Attempts */
152 MGMT_STATUS_REJECTED
, /* Pairing Not Allowed */
153 MGMT_STATUS_FAILED
, /* Unknown LMP PDU */
154 MGMT_STATUS_NOT_SUPPORTED
, /* Unsupported Remote Feature */
155 MGMT_STATUS_REJECTED
, /* SCO Offset Rejected */
156 MGMT_STATUS_REJECTED
, /* SCO Interval Rejected */
157 MGMT_STATUS_REJECTED
, /* Air Mode Rejected */
158 MGMT_STATUS_INVALID_PARAMS
, /* Invalid LMP Parameters */
159 MGMT_STATUS_FAILED
, /* Unspecified Error */
160 MGMT_STATUS_NOT_SUPPORTED
, /* Unsupported LMP Parameter Value */
161 MGMT_STATUS_FAILED
, /* Role Change Not Allowed */
162 MGMT_STATUS_TIMEOUT
, /* LMP Response Timeout */
163 MGMT_STATUS_FAILED
, /* LMP Error Transaction Collision */
164 MGMT_STATUS_FAILED
, /* LMP PDU Not Allowed */
165 MGMT_STATUS_REJECTED
, /* Encryption Mode Not Accepted */
166 MGMT_STATUS_FAILED
, /* Unit Link Key Used */
167 MGMT_STATUS_NOT_SUPPORTED
, /* QoS Not Supported */
168 MGMT_STATUS_TIMEOUT
, /* Instant Passed */
169 MGMT_STATUS_NOT_SUPPORTED
, /* Pairing Not Supported */
170 MGMT_STATUS_FAILED
, /* Transaction Collision */
171 MGMT_STATUS_INVALID_PARAMS
, /* Unacceptable Parameter */
172 MGMT_STATUS_REJECTED
, /* QoS Rejected */
173 MGMT_STATUS_NOT_SUPPORTED
, /* Classification Not Supported */
174 MGMT_STATUS_REJECTED
, /* Insufficient Security */
175 MGMT_STATUS_INVALID_PARAMS
, /* Parameter Out Of Range */
176 MGMT_STATUS_BUSY
, /* Role Switch Pending */
177 MGMT_STATUS_FAILED
, /* Slot Violation */
178 MGMT_STATUS_FAILED
, /* Role Switch Failed */
179 MGMT_STATUS_INVALID_PARAMS
, /* EIR Too Large */
180 MGMT_STATUS_NOT_SUPPORTED
, /* Simple Pairing Not Supported */
181 MGMT_STATUS_BUSY
, /* Host Busy Pairing */
182 MGMT_STATUS_REJECTED
, /* Rejected, No Suitable Channel */
183 MGMT_STATUS_BUSY
, /* Controller Busy */
184 MGMT_STATUS_INVALID_PARAMS
, /* Unsuitable Connection Interval */
185 MGMT_STATUS_TIMEOUT
, /* Directed Advertising Timeout */
186 MGMT_STATUS_AUTH_FAILED
, /* Terminated Due to MIC Failure */
187 MGMT_STATUS_CONNECT_FAILED
, /* Connection Establishment Failed */
188 MGMT_STATUS_CONNECT_FAILED
, /* MAC Connection Failed */
191 static u8
mgmt_status(u8 hci_status
)
193 if (hci_status
< ARRAY_SIZE(mgmt_status_table
))
194 return mgmt_status_table
[hci_status
];
196 return MGMT_STATUS_FAILED
;
199 static int cmd_status(struct sock
*sk
, u16 index
, u16 cmd
, u8 status
)
202 struct mgmt_hdr
*hdr
;
203 struct mgmt_ev_cmd_status
*ev
;
206 BT_DBG("sock %p, index %u, cmd %u, status %u", sk
, index
, cmd
, status
);
208 skb
= alloc_skb(sizeof(*hdr
) + sizeof(*ev
), GFP_KERNEL
);
212 hdr
= (void *) skb_put(skb
, sizeof(*hdr
));
214 hdr
->opcode
= __constant_cpu_to_le16(MGMT_EV_CMD_STATUS
);
215 hdr
->index
= cpu_to_le16(index
);
216 hdr
->len
= cpu_to_le16(sizeof(*ev
));
218 ev
= (void *) skb_put(skb
, sizeof(*ev
));
220 ev
->opcode
= cpu_to_le16(cmd
);
222 err
= sock_queue_rcv_skb(sk
, skb
);
229 static int cmd_complete(struct sock
*sk
, u16 index
, u16 cmd
, u8 status
,
230 void *rp
, size_t rp_len
)
233 struct mgmt_hdr
*hdr
;
234 struct mgmt_ev_cmd_complete
*ev
;
237 BT_DBG("sock %p", sk
);
239 skb
= alloc_skb(sizeof(*hdr
) + sizeof(*ev
) + rp_len
, GFP_KERNEL
);
243 hdr
= (void *) skb_put(skb
, sizeof(*hdr
));
245 hdr
->opcode
= __constant_cpu_to_le16(MGMT_EV_CMD_COMPLETE
);
246 hdr
->index
= cpu_to_le16(index
);
247 hdr
->len
= cpu_to_le16(sizeof(*ev
) + rp_len
);
249 ev
= (void *) skb_put(skb
, sizeof(*ev
) + rp_len
);
250 ev
->opcode
= cpu_to_le16(cmd
);
254 memcpy(ev
->data
, rp
, rp_len
);
256 err
= sock_queue_rcv_skb(sk
, skb
);
263 static int read_version(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
266 struct mgmt_rp_read_version rp
;
268 BT_DBG("sock %p", sk
);
270 rp
.version
= MGMT_VERSION
;
271 rp
.revision
= __constant_cpu_to_le16(MGMT_REVISION
);
273 return cmd_complete(sk
, MGMT_INDEX_NONE
, MGMT_OP_READ_VERSION
, 0, &rp
,
277 static int read_commands(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
280 struct mgmt_rp_read_commands
*rp
;
281 const u16 num_commands
= ARRAY_SIZE(mgmt_commands
);
282 const u16 num_events
= ARRAY_SIZE(mgmt_events
);
287 BT_DBG("sock %p", sk
);
289 rp_size
= sizeof(*rp
) + ((num_commands
+ num_events
) * sizeof(u16
));
291 rp
= kmalloc(rp_size
, GFP_KERNEL
);
295 rp
->num_commands
= __constant_cpu_to_le16(num_commands
);
296 rp
->num_events
= __constant_cpu_to_le16(num_events
);
298 for (i
= 0, opcode
= rp
->opcodes
; i
< num_commands
; i
++, opcode
++)
299 put_unaligned_le16(mgmt_commands
[i
], opcode
);
301 for (i
= 0; i
< num_events
; i
++, opcode
++)
302 put_unaligned_le16(mgmt_events
[i
], opcode
);
304 err
= cmd_complete(sk
, MGMT_INDEX_NONE
, MGMT_OP_READ_COMMANDS
, 0, rp
,
311 static int read_index_list(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
314 struct mgmt_rp_read_index_list
*rp
;
320 BT_DBG("sock %p", sk
);
322 read_lock(&hci_dev_list_lock
);
325 list_for_each_entry(d
, &hci_dev_list
, list
) {
326 if (d
->dev_type
== HCI_BREDR
)
330 rp_len
= sizeof(*rp
) + (2 * count
);
331 rp
= kmalloc(rp_len
, GFP_ATOMIC
);
333 read_unlock(&hci_dev_list_lock
);
338 list_for_each_entry(d
, &hci_dev_list
, list
) {
339 if (test_bit(HCI_SETUP
, &d
->dev_flags
))
342 if (test_bit(HCI_USER_CHANNEL
, &d
->dev_flags
))
345 if (d
->dev_type
== HCI_BREDR
) {
346 rp
->index
[count
++] = cpu_to_le16(d
->id
);
347 BT_DBG("Added hci%u", d
->id
);
351 rp
->num_controllers
= cpu_to_le16(count
);
352 rp_len
= sizeof(*rp
) + (2 * count
);
354 read_unlock(&hci_dev_list_lock
);
356 err
= cmd_complete(sk
, MGMT_INDEX_NONE
, MGMT_OP_READ_INDEX_LIST
, 0, rp
,
364 static u32
get_supported_settings(struct hci_dev
*hdev
)
368 settings
|= MGMT_SETTING_POWERED
;
369 settings
|= MGMT_SETTING_PAIRABLE
;
370 settings
|= MGMT_SETTING_DEBUG_KEYS
;
372 if (lmp_bredr_capable(hdev
)) {
373 settings
|= MGMT_SETTING_CONNECTABLE
;
374 if (hdev
->hci_ver
>= BLUETOOTH_VER_1_2
)
375 settings
|= MGMT_SETTING_FAST_CONNECTABLE
;
376 settings
|= MGMT_SETTING_DISCOVERABLE
;
377 settings
|= MGMT_SETTING_BREDR
;
378 settings
|= MGMT_SETTING_LINK_SECURITY
;
380 if (lmp_ssp_capable(hdev
)) {
381 settings
|= MGMT_SETTING_SSP
;
382 settings
|= MGMT_SETTING_HS
;
385 if (lmp_sc_capable(hdev
) ||
386 test_bit(HCI_FORCE_SC
, &hdev
->dev_flags
))
387 settings
|= MGMT_SETTING_SECURE_CONN
;
390 if (lmp_le_capable(hdev
)) {
391 settings
|= MGMT_SETTING_LE
;
392 settings
|= MGMT_SETTING_ADVERTISING
;
393 settings
|= MGMT_SETTING_PRIVACY
;
399 static u32
get_current_settings(struct hci_dev
*hdev
)
403 if (hdev_is_powered(hdev
))
404 settings
|= MGMT_SETTING_POWERED
;
406 if (test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
407 settings
|= MGMT_SETTING_CONNECTABLE
;
409 if (test_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
))
410 settings
|= MGMT_SETTING_FAST_CONNECTABLE
;
412 if (test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
))
413 settings
|= MGMT_SETTING_DISCOVERABLE
;
415 if (test_bit(HCI_PAIRABLE
, &hdev
->dev_flags
))
416 settings
|= MGMT_SETTING_PAIRABLE
;
418 if (test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
419 settings
|= MGMT_SETTING_BREDR
;
421 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
422 settings
|= MGMT_SETTING_LE
;
424 if (test_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
))
425 settings
|= MGMT_SETTING_LINK_SECURITY
;
427 if (test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
))
428 settings
|= MGMT_SETTING_SSP
;
430 if (test_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
))
431 settings
|= MGMT_SETTING_HS
;
433 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
434 settings
|= MGMT_SETTING_ADVERTISING
;
436 if (test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
))
437 settings
|= MGMT_SETTING_SECURE_CONN
;
439 if (test_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
))
440 settings
|= MGMT_SETTING_DEBUG_KEYS
;
442 if (test_bit(HCI_PRIVACY
, &hdev
->dev_flags
))
443 settings
|= MGMT_SETTING_PRIVACY
;
448 #define PNP_INFO_SVCLASS_ID 0x1200
450 static u8
*create_uuid16_list(struct hci_dev
*hdev
, u8
*data
, ptrdiff_t len
)
452 u8
*ptr
= data
, *uuids_start
= NULL
;
453 struct bt_uuid
*uuid
;
458 list_for_each_entry(uuid
, &hdev
->uuids
, list
) {
461 if (uuid
->size
!= 16)
464 uuid16
= get_unaligned_le16(&uuid
->uuid
[12]);
468 if (uuid16
== PNP_INFO_SVCLASS_ID
)
474 uuids_start
[1] = EIR_UUID16_ALL
;
478 /* Stop if not enough space to put next UUID */
479 if ((ptr
- data
) + sizeof(u16
) > len
) {
480 uuids_start
[1] = EIR_UUID16_SOME
;
484 *ptr
++ = (uuid16
& 0x00ff);
485 *ptr
++ = (uuid16
& 0xff00) >> 8;
486 uuids_start
[0] += sizeof(uuid16
);
492 static u8
*create_uuid32_list(struct hci_dev
*hdev
, u8
*data
, ptrdiff_t len
)
494 u8
*ptr
= data
, *uuids_start
= NULL
;
495 struct bt_uuid
*uuid
;
500 list_for_each_entry(uuid
, &hdev
->uuids
, list
) {
501 if (uuid
->size
!= 32)
507 uuids_start
[1] = EIR_UUID32_ALL
;
511 /* Stop if not enough space to put next UUID */
512 if ((ptr
- data
) + sizeof(u32
) > len
) {
513 uuids_start
[1] = EIR_UUID32_SOME
;
517 memcpy(ptr
, &uuid
->uuid
[12], sizeof(u32
));
519 uuids_start
[0] += sizeof(u32
);
525 static u8
*create_uuid128_list(struct hci_dev
*hdev
, u8
*data
, ptrdiff_t len
)
527 u8
*ptr
= data
, *uuids_start
= NULL
;
528 struct bt_uuid
*uuid
;
533 list_for_each_entry(uuid
, &hdev
->uuids
, list
) {
534 if (uuid
->size
!= 128)
540 uuids_start
[1] = EIR_UUID128_ALL
;
544 /* Stop if not enough space to put next UUID */
545 if ((ptr
- data
) + 16 > len
) {
546 uuids_start
[1] = EIR_UUID128_SOME
;
550 memcpy(ptr
, uuid
->uuid
, 16);
552 uuids_start
[0] += 16;
558 static struct pending_cmd
*mgmt_pending_find(u16 opcode
, struct hci_dev
*hdev
)
560 struct pending_cmd
*cmd
;
562 list_for_each_entry(cmd
, &hdev
->mgmt_pending
, list
) {
563 if (cmd
->opcode
== opcode
)
570 static u8
create_scan_rsp_data(struct hci_dev
*hdev
, u8
*ptr
)
575 name_len
= strlen(hdev
->dev_name
);
577 size_t max_len
= HCI_MAX_AD_LENGTH
- ad_len
- 2;
579 if (name_len
> max_len
) {
581 ptr
[1] = EIR_NAME_SHORT
;
583 ptr
[1] = EIR_NAME_COMPLETE
;
585 ptr
[0] = name_len
+ 1;
587 memcpy(ptr
+ 2, hdev
->dev_name
, name_len
);
589 ad_len
+= (name_len
+ 2);
590 ptr
+= (name_len
+ 2);
596 static void update_scan_rsp_data(struct hci_request
*req
)
598 struct hci_dev
*hdev
= req
->hdev
;
599 struct hci_cp_le_set_scan_rsp_data cp
;
602 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
605 memset(&cp
, 0, sizeof(cp
));
607 len
= create_scan_rsp_data(hdev
, cp
.data
);
609 if (hdev
->scan_rsp_data_len
== len
&&
610 memcmp(cp
.data
, hdev
->scan_rsp_data
, len
) == 0)
613 memcpy(hdev
->scan_rsp_data
, cp
.data
, sizeof(cp
.data
));
614 hdev
->scan_rsp_data_len
= len
;
618 hci_req_add(req
, HCI_OP_LE_SET_SCAN_RSP_DATA
, sizeof(cp
), &cp
);
621 static u8
get_adv_discov_flags(struct hci_dev
*hdev
)
623 struct pending_cmd
*cmd
;
625 /* If there's a pending mgmt command the flags will not yet have
626 * their final values, so check for this first.
628 cmd
= mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
);
630 struct mgmt_mode
*cp
= cmd
->param
;
632 return LE_AD_GENERAL
;
633 else if (cp
->val
== 0x02)
634 return LE_AD_LIMITED
;
636 if (test_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
))
637 return LE_AD_LIMITED
;
638 else if (test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
))
639 return LE_AD_GENERAL
;
645 static u8
create_adv_data(struct hci_dev
*hdev
, u8
*ptr
)
647 u8 ad_len
= 0, flags
= 0;
649 flags
|= get_adv_discov_flags(hdev
);
651 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
652 flags
|= LE_AD_NO_BREDR
;
655 BT_DBG("adv flags 0x%02x", flags
);
665 if (hdev
->adv_tx_power
!= HCI_TX_POWER_INVALID
) {
667 ptr
[1] = EIR_TX_POWER
;
668 ptr
[2] = (u8
) hdev
->adv_tx_power
;
677 static void update_adv_data(struct hci_request
*req
)
679 struct hci_dev
*hdev
= req
->hdev
;
680 struct hci_cp_le_set_adv_data cp
;
683 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
686 memset(&cp
, 0, sizeof(cp
));
688 len
= create_adv_data(hdev
, cp
.data
);
690 if (hdev
->adv_data_len
== len
&&
691 memcmp(cp
.data
, hdev
->adv_data
, len
) == 0)
694 memcpy(hdev
->adv_data
, cp
.data
, sizeof(cp
.data
));
695 hdev
->adv_data_len
= len
;
699 hci_req_add(req
, HCI_OP_LE_SET_ADV_DATA
, sizeof(cp
), &cp
);
702 static void create_eir(struct hci_dev
*hdev
, u8
*data
)
707 name_len
= strlen(hdev
->dev_name
);
713 ptr
[1] = EIR_NAME_SHORT
;
715 ptr
[1] = EIR_NAME_COMPLETE
;
717 /* EIR Data length */
718 ptr
[0] = name_len
+ 1;
720 memcpy(ptr
+ 2, hdev
->dev_name
, name_len
);
722 ptr
+= (name_len
+ 2);
725 if (hdev
->inq_tx_power
!= HCI_TX_POWER_INVALID
) {
727 ptr
[1] = EIR_TX_POWER
;
728 ptr
[2] = (u8
) hdev
->inq_tx_power
;
733 if (hdev
->devid_source
> 0) {
735 ptr
[1] = EIR_DEVICE_ID
;
737 put_unaligned_le16(hdev
->devid_source
, ptr
+ 2);
738 put_unaligned_le16(hdev
->devid_vendor
, ptr
+ 4);
739 put_unaligned_le16(hdev
->devid_product
, ptr
+ 6);
740 put_unaligned_le16(hdev
->devid_version
, ptr
+ 8);
745 ptr
= create_uuid16_list(hdev
, ptr
, HCI_MAX_EIR_LENGTH
- (ptr
- data
));
746 ptr
= create_uuid32_list(hdev
, ptr
, HCI_MAX_EIR_LENGTH
- (ptr
- data
));
747 ptr
= create_uuid128_list(hdev
, ptr
, HCI_MAX_EIR_LENGTH
- (ptr
- data
));
750 static void update_eir(struct hci_request
*req
)
752 struct hci_dev
*hdev
= req
->hdev
;
753 struct hci_cp_write_eir cp
;
755 if (!hdev_is_powered(hdev
))
758 if (!lmp_ext_inq_capable(hdev
))
761 if (!test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
))
764 if (test_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
))
767 memset(&cp
, 0, sizeof(cp
));
769 create_eir(hdev
, cp
.data
);
771 if (memcmp(cp
.data
, hdev
->eir
, sizeof(cp
.data
)) == 0)
774 memcpy(hdev
->eir
, cp
.data
, sizeof(cp
.data
));
776 hci_req_add(req
, HCI_OP_WRITE_EIR
, sizeof(cp
), &cp
);
779 static u8
get_service_classes(struct hci_dev
*hdev
)
781 struct bt_uuid
*uuid
;
784 list_for_each_entry(uuid
, &hdev
->uuids
, list
)
785 val
|= uuid
->svc_hint
;
790 static void update_class(struct hci_request
*req
)
792 struct hci_dev
*hdev
= req
->hdev
;
795 BT_DBG("%s", hdev
->name
);
797 if (!hdev_is_powered(hdev
))
800 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
803 if (test_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
))
806 cod
[0] = hdev
->minor_class
;
807 cod
[1] = hdev
->major_class
;
808 cod
[2] = get_service_classes(hdev
);
810 if (test_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
))
813 if (memcmp(cod
, hdev
->dev_class
, 3) == 0)
816 hci_req_add(req
, HCI_OP_WRITE_CLASS_OF_DEV
, sizeof(cod
), cod
);
819 static u8
get_adv_type(struct hci_dev
*hdev
)
821 struct pending_cmd
*cmd
;
824 /* If there's a pending mgmt command the flag will not yet have
825 * it's final value, so check for this first.
827 cmd
= mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
);
829 struct mgmt_mode
*cp
= cmd
->param
;
830 connectable
= !!cp
->val
;
832 connectable
= test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
835 return connectable
? LE_ADV_IND
: LE_ADV_NONCONN_IND
;
838 static void enable_advertising(struct hci_request
*req
)
840 struct hci_dev
*hdev
= req
->hdev
;
841 struct hci_cp_le_set_adv_param cp
;
842 u8 own_addr_type
, enable
= 0x01;
844 memset(&cp
, 0, sizeof(cp
));
846 if (hci_update_random_address(req
, &own_addr_type
) < 0)
849 cp
.min_interval
= __constant_cpu_to_le16(0x0800);
850 cp
.max_interval
= __constant_cpu_to_le16(0x0800);
851 cp
.type
= get_adv_type(hdev
);
852 cp
.own_address_type
= own_addr_type
;
853 cp
.channel_map
= hdev
->le_adv_channel_map
;
855 hci_req_add(req
, HCI_OP_LE_SET_ADV_PARAM
, sizeof(cp
), &cp
);
857 hci_req_add(req
, HCI_OP_LE_SET_ADV_ENABLE
, sizeof(enable
), &enable
);
860 static void disable_advertising(struct hci_request
*req
)
864 hci_req_add(req
, HCI_OP_LE_SET_ADV_ENABLE
, sizeof(enable
), &enable
);
867 static void service_cache_off(struct work_struct
*work
)
869 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
871 struct hci_request req
;
873 if (!test_and_clear_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
))
876 hci_req_init(&req
, hdev
);
883 hci_dev_unlock(hdev
);
885 hci_req_run(&req
, NULL
);
888 static void rpa_expired(struct work_struct
*work
)
890 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
892 struct hci_request req
;
896 set_bit(HCI_RPA_EXPIRED
, &hdev
->dev_flags
);
898 if (!test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
) ||
899 hci_conn_num(hdev
, LE_LINK
) > 0)
902 /* The generation of a new RPA and programming it into the
903 * controller happens in the enable_advertising() function.
906 hci_req_init(&req
, hdev
);
908 disable_advertising(&req
);
909 enable_advertising(&req
);
911 hci_req_run(&req
, NULL
);
914 static void mgmt_init_hdev(struct sock
*sk
, struct hci_dev
*hdev
)
916 if (test_and_set_bit(HCI_MGMT
, &hdev
->dev_flags
))
919 INIT_DELAYED_WORK(&hdev
->service_cache
, service_cache_off
);
920 INIT_DELAYED_WORK(&hdev
->rpa_expired
, rpa_expired
);
922 /* Non-mgmt controlled devices get this bit set
923 * implicitly so that pairing works for them, however
924 * for mgmt we require user-space to explicitly enable
927 clear_bit(HCI_PAIRABLE
, &hdev
->dev_flags
);
930 static int read_controller_info(struct sock
*sk
, struct hci_dev
*hdev
,
931 void *data
, u16 data_len
)
933 struct mgmt_rp_read_info rp
;
935 BT_DBG("sock %p %s", sk
, hdev
->name
);
939 memset(&rp
, 0, sizeof(rp
));
941 bacpy(&rp
.bdaddr
, &hdev
->bdaddr
);
943 rp
.version
= hdev
->hci_ver
;
944 rp
.manufacturer
= cpu_to_le16(hdev
->manufacturer
);
946 rp
.supported_settings
= cpu_to_le32(get_supported_settings(hdev
));
947 rp
.current_settings
= cpu_to_le32(get_current_settings(hdev
));
949 memcpy(rp
.dev_class
, hdev
->dev_class
, 3);
951 memcpy(rp
.name
, hdev
->dev_name
, sizeof(hdev
->dev_name
));
952 memcpy(rp
.short_name
, hdev
->short_name
, sizeof(hdev
->short_name
));
954 hci_dev_unlock(hdev
);
956 return cmd_complete(sk
, hdev
->id
, MGMT_OP_READ_INFO
, 0, &rp
,
960 static void mgmt_pending_free(struct pending_cmd
*cmd
)
967 static struct pending_cmd
*mgmt_pending_add(struct sock
*sk
, u16 opcode
,
968 struct hci_dev
*hdev
, void *data
,
971 struct pending_cmd
*cmd
;
973 cmd
= kmalloc(sizeof(*cmd
), GFP_KERNEL
);
977 cmd
->opcode
= opcode
;
978 cmd
->index
= hdev
->id
;
980 cmd
->param
= kmalloc(len
, GFP_KERNEL
);
987 memcpy(cmd
->param
, data
, len
);
992 list_add(&cmd
->list
, &hdev
->mgmt_pending
);
997 static void mgmt_pending_foreach(u16 opcode
, struct hci_dev
*hdev
,
998 void (*cb
)(struct pending_cmd
*cmd
,
1002 struct pending_cmd
*cmd
, *tmp
;
1004 list_for_each_entry_safe(cmd
, tmp
, &hdev
->mgmt_pending
, list
) {
1005 if (opcode
> 0 && cmd
->opcode
!= opcode
)
1012 static void mgmt_pending_remove(struct pending_cmd
*cmd
)
1014 list_del(&cmd
->list
);
1015 mgmt_pending_free(cmd
);
1018 static int send_settings_rsp(struct sock
*sk
, u16 opcode
, struct hci_dev
*hdev
)
1020 __le32 settings
= cpu_to_le32(get_current_settings(hdev
));
1022 return cmd_complete(sk
, hdev
->id
, opcode
, 0, &settings
,
1026 static int set_powered(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1029 struct mgmt_mode
*cp
= data
;
1030 struct pending_cmd
*cmd
;
1033 BT_DBG("request for %s", hdev
->name
);
1035 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1036 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_POWERED
,
1037 MGMT_STATUS_INVALID_PARAMS
);
1041 if (mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
)) {
1042 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_POWERED
,
1047 if (test_and_clear_bit(HCI_AUTO_OFF
, &hdev
->dev_flags
)) {
1048 cancel_delayed_work(&hdev
->power_off
);
1051 mgmt_pending_add(sk
, MGMT_OP_SET_POWERED
, hdev
,
1053 err
= mgmt_powered(hdev
, 1);
1058 if (!!cp
->val
== hdev_is_powered(hdev
)) {
1059 err
= send_settings_rsp(sk
, MGMT_OP_SET_POWERED
, hdev
);
1063 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_POWERED
, hdev
, data
, len
);
1070 queue_work(hdev
->req_workqueue
, &hdev
->power_on
);
1072 queue_work(hdev
->req_workqueue
, &hdev
->power_off
.work
);
1077 hci_dev_unlock(hdev
);
1081 static int mgmt_event(u16 event
, struct hci_dev
*hdev
, void *data
, u16 data_len
,
1082 struct sock
*skip_sk
)
1084 struct sk_buff
*skb
;
1085 struct mgmt_hdr
*hdr
;
1087 skb
= alloc_skb(sizeof(*hdr
) + data_len
, GFP_KERNEL
);
1091 hdr
= (void *) skb_put(skb
, sizeof(*hdr
));
1092 hdr
->opcode
= cpu_to_le16(event
);
1094 hdr
->index
= cpu_to_le16(hdev
->id
);
1096 hdr
->index
= __constant_cpu_to_le16(MGMT_INDEX_NONE
);
1097 hdr
->len
= cpu_to_le16(data_len
);
1100 memcpy(skb_put(skb
, data_len
), data
, data_len
);
1103 __net_timestamp(skb
);
1105 hci_send_to_control(skb
, skip_sk
);
1111 static int new_settings(struct hci_dev
*hdev
, struct sock
*skip
)
1115 ev
= cpu_to_le32(get_current_settings(hdev
));
1117 return mgmt_event(MGMT_EV_NEW_SETTINGS
, hdev
, &ev
, sizeof(ev
), skip
);
1122 struct hci_dev
*hdev
;
1126 static void settings_rsp(struct pending_cmd
*cmd
, void *data
)
1128 struct cmd_lookup
*match
= data
;
1130 send_settings_rsp(cmd
->sk
, cmd
->opcode
, match
->hdev
);
1132 list_del(&cmd
->list
);
1134 if (match
->sk
== NULL
) {
1135 match
->sk
= cmd
->sk
;
1136 sock_hold(match
->sk
);
1139 mgmt_pending_free(cmd
);
1142 static void cmd_status_rsp(struct pending_cmd
*cmd
, void *data
)
1146 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, *status
);
1147 mgmt_pending_remove(cmd
);
1150 static u8
mgmt_bredr_support(struct hci_dev
*hdev
)
1152 if (!lmp_bredr_capable(hdev
))
1153 return MGMT_STATUS_NOT_SUPPORTED
;
1154 else if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1155 return MGMT_STATUS_REJECTED
;
1157 return MGMT_STATUS_SUCCESS
;
1160 static u8
mgmt_le_support(struct hci_dev
*hdev
)
1162 if (!lmp_le_capable(hdev
))
1163 return MGMT_STATUS_NOT_SUPPORTED
;
1164 else if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
1165 return MGMT_STATUS_REJECTED
;
1167 return MGMT_STATUS_SUCCESS
;
1170 static void set_discoverable_complete(struct hci_dev
*hdev
, u8 status
)
1172 struct pending_cmd
*cmd
;
1173 struct mgmt_mode
*cp
;
1174 struct hci_request req
;
1177 BT_DBG("status 0x%02x", status
);
1181 cmd
= mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
);
1186 u8 mgmt_err
= mgmt_status(status
);
1187 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_err
);
1188 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1194 changed
= !test_and_set_bit(HCI_DISCOVERABLE
,
1197 if (hdev
->discov_timeout
> 0) {
1198 int to
= msecs_to_jiffies(hdev
->discov_timeout
* 1000);
1199 queue_delayed_work(hdev
->workqueue
, &hdev
->discov_off
,
1203 changed
= test_and_clear_bit(HCI_DISCOVERABLE
,
1207 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
);
1210 new_settings(hdev
, cmd
->sk
);
1212 /* When the discoverable mode gets changed, make sure
1213 * that class of device has the limited discoverable
1214 * bit correctly set.
1216 hci_req_init(&req
, hdev
);
1218 hci_req_run(&req
, NULL
);
1221 mgmt_pending_remove(cmd
);
1224 hci_dev_unlock(hdev
);
1227 static int set_discoverable(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1230 struct mgmt_cp_set_discoverable
*cp
= data
;
1231 struct pending_cmd
*cmd
;
1232 struct hci_request req
;
1237 BT_DBG("request for %s", hdev
->name
);
1239 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
) &&
1240 !test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1241 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1242 MGMT_STATUS_REJECTED
);
1244 if (cp
->val
!= 0x00 && cp
->val
!= 0x01 && cp
->val
!= 0x02)
1245 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1246 MGMT_STATUS_INVALID_PARAMS
);
1248 timeout
= __le16_to_cpu(cp
->timeout
);
1250 /* Disabling discoverable requires that no timeout is set,
1251 * and enabling limited discoverable requires a timeout.
1253 if ((cp
->val
== 0x00 && timeout
> 0) ||
1254 (cp
->val
== 0x02 && timeout
== 0))
1255 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1256 MGMT_STATUS_INVALID_PARAMS
);
1260 if (!hdev_is_powered(hdev
) && timeout
> 0) {
1261 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1262 MGMT_STATUS_NOT_POWERED
);
1266 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
) ||
1267 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
)) {
1268 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1273 if (!test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
)) {
1274 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1275 MGMT_STATUS_REJECTED
);
1279 if (!hdev_is_powered(hdev
)) {
1280 bool changed
= false;
1282 /* Setting limited discoverable when powered off is
1283 * not a valid operation since it requires a timeout
1284 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1286 if (!!cp
->val
!= test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
)) {
1287 change_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
1291 err
= send_settings_rsp(sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
);
1296 err
= new_settings(hdev
, sk
);
1301 /* If the current mode is the same, then just update the timeout
1302 * value with the new value. And if only the timeout gets updated,
1303 * then no need for any HCI transactions.
1305 if (!!cp
->val
== test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
) &&
1306 (cp
->val
== 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE
,
1307 &hdev
->dev_flags
)) {
1308 cancel_delayed_work(&hdev
->discov_off
);
1309 hdev
->discov_timeout
= timeout
;
1311 if (cp
->val
&& hdev
->discov_timeout
> 0) {
1312 int to
= msecs_to_jiffies(hdev
->discov_timeout
* 1000);
1313 queue_delayed_work(hdev
->workqueue
, &hdev
->discov_off
,
1317 err
= send_settings_rsp(sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
);
1321 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
, data
, len
);
1327 /* Cancel any potential discoverable timeout that might be
1328 * still active and store new timeout value. The arming of
1329 * the timeout happens in the complete handler.
1331 cancel_delayed_work(&hdev
->discov_off
);
1332 hdev
->discov_timeout
= timeout
;
1334 /* Limited discoverable mode */
1335 if (cp
->val
== 0x02)
1336 set_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1338 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1340 hci_req_init(&req
, hdev
);
1342 /* The procedure for LE-only controllers is much simpler - just
1343 * update the advertising data.
1345 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1351 struct hci_cp_write_current_iac_lap hci_cp
;
1353 if (cp
->val
== 0x02) {
1354 /* Limited discoverable mode */
1355 hci_cp
.num_iac
= min_t(u8
, hdev
->num_iac
, 2);
1356 hci_cp
.iac_lap
[0] = 0x00; /* LIAC */
1357 hci_cp
.iac_lap
[1] = 0x8b;
1358 hci_cp
.iac_lap
[2] = 0x9e;
1359 hci_cp
.iac_lap
[3] = 0x33; /* GIAC */
1360 hci_cp
.iac_lap
[4] = 0x8b;
1361 hci_cp
.iac_lap
[5] = 0x9e;
1363 /* General discoverable mode */
1365 hci_cp
.iac_lap
[0] = 0x33; /* GIAC */
1366 hci_cp
.iac_lap
[1] = 0x8b;
1367 hci_cp
.iac_lap
[2] = 0x9e;
1370 hci_req_add(&req
, HCI_OP_WRITE_CURRENT_IAC_LAP
,
1371 (hci_cp
.num_iac
* 3) + 1, &hci_cp
);
1373 scan
|= SCAN_INQUIRY
;
1375 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1378 hci_req_add(&req
, HCI_OP_WRITE_SCAN_ENABLE
, sizeof(scan
), &scan
);
1381 update_adv_data(&req
);
1383 err
= hci_req_run(&req
, set_discoverable_complete
);
1385 mgmt_pending_remove(cmd
);
1388 hci_dev_unlock(hdev
);
1392 static void write_fast_connectable(struct hci_request
*req
, bool enable
)
1394 struct hci_dev
*hdev
= req
->hdev
;
1395 struct hci_cp_write_page_scan_activity acp
;
1398 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1401 if (hdev
->hci_ver
< BLUETOOTH_VER_1_2
)
1405 type
= PAGE_SCAN_TYPE_INTERLACED
;
1407 /* 160 msec page scan interval */
1408 acp
.interval
= __constant_cpu_to_le16(0x0100);
1410 type
= PAGE_SCAN_TYPE_STANDARD
; /* default */
1412 /* default 1.28 sec page scan */
1413 acp
.interval
= __constant_cpu_to_le16(0x0800);
1416 acp
.window
= __constant_cpu_to_le16(0x0012);
1418 if (__cpu_to_le16(hdev
->page_scan_interval
) != acp
.interval
||
1419 __cpu_to_le16(hdev
->page_scan_window
) != acp
.window
)
1420 hci_req_add(req
, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY
,
1423 if (hdev
->page_scan_type
!= type
)
1424 hci_req_add(req
, HCI_OP_WRITE_PAGE_SCAN_TYPE
, 1, &type
);
1427 static void set_connectable_complete(struct hci_dev
*hdev
, u8 status
)
1429 struct pending_cmd
*cmd
;
1430 struct mgmt_mode
*cp
;
1433 BT_DBG("status 0x%02x", status
);
1437 cmd
= mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
);
1442 u8 mgmt_err
= mgmt_status(status
);
1443 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_err
);
1449 changed
= !test_and_set_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1451 changed
= test_and_clear_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1453 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_CONNECTABLE
, hdev
);
1456 new_settings(hdev
, cmd
->sk
);
1459 mgmt_pending_remove(cmd
);
1462 hci_dev_unlock(hdev
);
1465 static int set_connectable_update_settings(struct hci_dev
*hdev
,
1466 struct sock
*sk
, u8 val
)
1468 bool changed
= false;
1471 if (!!val
!= test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
1475 set_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1477 clear_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1478 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
1481 err
= send_settings_rsp(sk
, MGMT_OP_SET_CONNECTABLE
, hdev
);
1486 return new_settings(hdev
, sk
);
1491 static int set_connectable(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1494 struct mgmt_mode
*cp
= data
;
1495 struct pending_cmd
*cmd
;
1496 struct hci_request req
;
1500 BT_DBG("request for %s", hdev
->name
);
1502 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
) &&
1503 !test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1504 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_CONNECTABLE
,
1505 MGMT_STATUS_REJECTED
);
1507 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1508 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_CONNECTABLE
,
1509 MGMT_STATUS_INVALID_PARAMS
);
1513 if (!hdev_is_powered(hdev
)) {
1514 err
= set_connectable_update_settings(hdev
, sk
, cp
->val
);
1518 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
) ||
1519 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
)) {
1520 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_CONNECTABLE
,
1525 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_CONNECTABLE
, hdev
, data
, len
);
1531 hci_req_init(&req
, hdev
);
1533 /* If BR/EDR is not enabled and we disable advertising as a
1534 * by-product of disabling connectable, we need to update the
1535 * advertising flags.
1537 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
1539 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1540 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
1542 update_adv_data(&req
);
1543 } else if (cp
->val
!= test_bit(HCI_PSCAN
, &hdev
->flags
)) {
1549 if (test_bit(HCI_ISCAN
, &hdev
->flags
) &&
1550 hdev
->discov_timeout
> 0)
1551 cancel_delayed_work(&hdev
->discov_off
);
1554 hci_req_add(&req
, HCI_OP_WRITE_SCAN_ENABLE
, 1, &scan
);
1557 /* If we're going from non-connectable to connectable or
1558 * vice-versa when fast connectable is enabled ensure that fast
1559 * connectable gets disabled. write_fast_connectable won't do
1560 * anything if the page scan parameters are already what they
1563 if (cp
->val
|| test_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
))
1564 write_fast_connectable(&req
, false);
1566 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
) &&
1567 hci_conn_num(hdev
, LE_LINK
) == 0) {
1568 disable_advertising(&req
);
1569 enable_advertising(&req
);
1572 err
= hci_req_run(&req
, set_connectable_complete
);
1574 mgmt_pending_remove(cmd
);
1575 if (err
== -ENODATA
)
1576 err
= set_connectable_update_settings(hdev
, sk
,
1582 hci_dev_unlock(hdev
);
1586 static int set_pairable(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1589 struct mgmt_mode
*cp
= data
;
1593 BT_DBG("request for %s", hdev
->name
);
1595 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1596 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_PAIRABLE
,
1597 MGMT_STATUS_INVALID_PARAMS
);
1602 changed
= !test_and_set_bit(HCI_PAIRABLE
, &hdev
->dev_flags
);
1604 changed
= test_and_clear_bit(HCI_PAIRABLE
, &hdev
->dev_flags
);
1606 err
= send_settings_rsp(sk
, MGMT_OP_SET_PAIRABLE
, hdev
);
1611 err
= new_settings(hdev
, sk
);
1614 hci_dev_unlock(hdev
);
1618 static int set_link_security(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1621 struct mgmt_mode
*cp
= data
;
1622 struct pending_cmd
*cmd
;
1626 BT_DBG("request for %s", hdev
->name
);
1628 status
= mgmt_bredr_support(hdev
);
1630 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LINK_SECURITY
,
1633 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1634 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LINK_SECURITY
,
1635 MGMT_STATUS_INVALID_PARAMS
);
1639 if (!hdev_is_powered(hdev
)) {
1640 bool changed
= false;
1642 if (!!cp
->val
!= test_bit(HCI_LINK_SECURITY
,
1643 &hdev
->dev_flags
)) {
1644 change_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
);
1648 err
= send_settings_rsp(sk
, MGMT_OP_SET_LINK_SECURITY
, hdev
);
1653 err
= new_settings(hdev
, sk
);
1658 if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY
, hdev
)) {
1659 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LINK_SECURITY
,
1666 if (test_bit(HCI_AUTH
, &hdev
->flags
) == val
) {
1667 err
= send_settings_rsp(sk
, MGMT_OP_SET_LINK_SECURITY
, hdev
);
1671 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_LINK_SECURITY
, hdev
, data
, len
);
1677 err
= hci_send_cmd(hdev
, HCI_OP_WRITE_AUTH_ENABLE
, sizeof(val
), &val
);
1679 mgmt_pending_remove(cmd
);
1684 hci_dev_unlock(hdev
);
1688 static int set_ssp(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
1690 struct mgmt_mode
*cp
= data
;
1691 struct pending_cmd
*cmd
;
1695 BT_DBG("request for %s", hdev
->name
);
1697 status
= mgmt_bredr_support(hdev
);
1699 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
, status
);
1701 if (!lmp_ssp_capable(hdev
))
1702 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
,
1703 MGMT_STATUS_NOT_SUPPORTED
);
1705 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1706 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
,
1707 MGMT_STATUS_INVALID_PARAMS
);
1711 if (!hdev_is_powered(hdev
)) {
1715 changed
= !test_and_set_bit(HCI_SSP_ENABLED
,
1718 changed
= test_and_clear_bit(HCI_SSP_ENABLED
,
1721 changed
= test_and_clear_bit(HCI_HS_ENABLED
,
1724 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
1727 err
= send_settings_rsp(sk
, MGMT_OP_SET_SSP
, hdev
);
1732 err
= new_settings(hdev
, sk
);
1737 if (mgmt_pending_find(MGMT_OP_SET_SSP
, hdev
) ||
1738 mgmt_pending_find(MGMT_OP_SET_HS
, hdev
)) {
1739 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
,
1744 if (!!cp
->val
== test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
)) {
1745 err
= send_settings_rsp(sk
, MGMT_OP_SET_SSP
, hdev
);
1749 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_SSP
, hdev
, data
, len
);
1755 err
= hci_send_cmd(hdev
, HCI_OP_WRITE_SSP_MODE
, 1, &cp
->val
);
1757 mgmt_pending_remove(cmd
);
1762 hci_dev_unlock(hdev
);
1766 static int set_hs(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
1768 struct mgmt_mode
*cp
= data
;
1773 BT_DBG("request for %s", hdev
->name
);
1775 status
= mgmt_bredr_support(hdev
);
1777 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
, status
);
1779 if (!lmp_ssp_capable(hdev
))
1780 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1781 MGMT_STATUS_NOT_SUPPORTED
);
1783 if (!test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
))
1784 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1785 MGMT_STATUS_REJECTED
);
1787 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1788 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1789 MGMT_STATUS_INVALID_PARAMS
);
1794 changed
= !test_and_set_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
1796 if (hdev_is_powered(hdev
)) {
1797 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1798 MGMT_STATUS_REJECTED
);
1802 changed
= test_and_clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
1805 err
= send_settings_rsp(sk
, MGMT_OP_SET_HS
, hdev
);
1810 err
= new_settings(hdev
, sk
);
1813 hci_dev_unlock(hdev
);
1817 static void le_enable_complete(struct hci_dev
*hdev
, u8 status
)
1819 struct cmd_lookup match
= { NULL
, hdev
};
1822 u8 mgmt_err
= mgmt_status(status
);
1824 mgmt_pending_foreach(MGMT_OP_SET_LE
, hdev
, cmd_status_rsp
,
1829 mgmt_pending_foreach(MGMT_OP_SET_LE
, hdev
, settings_rsp
, &match
);
1831 new_settings(hdev
, match
.sk
);
1836 /* Make sure the controller has a good default for
1837 * advertising data. Restrict the update to when LE
1838 * has actually been enabled. During power on, the
1839 * update in powered_update_hci will take care of it.
1841 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
)) {
1842 struct hci_request req
;
1846 hci_req_init(&req
, hdev
);
1847 update_adv_data(&req
);
1848 update_scan_rsp_data(&req
);
1849 hci_req_run(&req
, NULL
);
1851 hci_dev_unlock(hdev
);
1855 static int set_le(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
1857 struct mgmt_mode
*cp
= data
;
1858 struct hci_cp_write_le_host_supported hci_cp
;
1859 struct pending_cmd
*cmd
;
1860 struct hci_request req
;
1864 BT_DBG("request for %s", hdev
->name
);
1866 if (!lmp_le_capable(hdev
))
1867 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1868 MGMT_STATUS_NOT_SUPPORTED
);
1870 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1871 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1872 MGMT_STATUS_INVALID_PARAMS
);
1874 /* LE-only devices do not allow toggling LE on/off */
1875 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1876 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1877 MGMT_STATUS_REJECTED
);
1882 enabled
= lmp_host_le_capable(hdev
);
1884 if (!hdev_is_powered(hdev
) || val
== enabled
) {
1885 bool changed
= false;
1887 if (val
!= test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
)) {
1888 change_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
);
1892 if (!val
&& test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
)) {
1893 clear_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
1897 err
= send_settings_rsp(sk
, MGMT_OP_SET_LE
, hdev
);
1902 err
= new_settings(hdev
, sk
);
1907 if (mgmt_pending_find(MGMT_OP_SET_LE
, hdev
) ||
1908 mgmt_pending_find(MGMT_OP_SET_ADVERTISING
, hdev
)) {
1909 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1914 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_LE
, hdev
, data
, len
);
1920 hci_req_init(&req
, hdev
);
1922 memset(&hci_cp
, 0, sizeof(hci_cp
));
1926 hci_cp
.simul
= lmp_le_br_capable(hdev
);
1928 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
1929 disable_advertising(&req
);
1932 hci_req_add(&req
, HCI_OP_WRITE_LE_HOST_SUPPORTED
, sizeof(hci_cp
),
1935 err
= hci_req_run(&req
, le_enable_complete
);
1937 mgmt_pending_remove(cmd
);
1940 hci_dev_unlock(hdev
);
1944 /* This is a helper function to test for pending mgmt commands that can
1945 * cause CoD or EIR HCI commands. We can only allow one such pending
1946 * mgmt command at a time since otherwise we cannot easily track what
1947 * the current values are, will be, and based on that calculate if a new
1948 * HCI command needs to be sent and if yes with what value.
1950 static bool pending_eir_or_class(struct hci_dev
*hdev
)
1952 struct pending_cmd
*cmd
;
1954 list_for_each_entry(cmd
, &hdev
->mgmt_pending
, list
) {
1955 switch (cmd
->opcode
) {
1956 case MGMT_OP_ADD_UUID
:
1957 case MGMT_OP_REMOVE_UUID
:
1958 case MGMT_OP_SET_DEV_CLASS
:
1959 case MGMT_OP_SET_POWERED
:
1967 static const u8 bluetooth_base_uuid
[] = {
1968 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
1969 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1972 static u8
get_uuid_size(const u8
*uuid
)
1976 if (memcmp(uuid
, bluetooth_base_uuid
, 12))
1979 val
= get_unaligned_le32(&uuid
[12]);
1986 static void mgmt_class_complete(struct hci_dev
*hdev
, u16 mgmt_op
, u8 status
)
1988 struct pending_cmd
*cmd
;
1992 cmd
= mgmt_pending_find(mgmt_op
, hdev
);
1996 cmd_complete(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_status(status
),
1997 hdev
->dev_class
, 3);
1999 mgmt_pending_remove(cmd
);
2002 hci_dev_unlock(hdev
);
2005 static void add_uuid_complete(struct hci_dev
*hdev
, u8 status
)
2007 BT_DBG("status 0x%02x", status
);
2009 mgmt_class_complete(hdev
, MGMT_OP_ADD_UUID
, status
);
2012 static int add_uuid(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
2014 struct mgmt_cp_add_uuid
*cp
= data
;
2015 struct pending_cmd
*cmd
;
2016 struct hci_request req
;
2017 struct bt_uuid
*uuid
;
2020 BT_DBG("request for %s", hdev
->name
);
2024 if (pending_eir_or_class(hdev
)) {
2025 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_ADD_UUID
,
2030 uuid
= kmalloc(sizeof(*uuid
), GFP_KERNEL
);
2036 memcpy(uuid
->uuid
, cp
->uuid
, 16);
2037 uuid
->svc_hint
= cp
->svc_hint
;
2038 uuid
->size
= get_uuid_size(cp
->uuid
);
2040 list_add_tail(&uuid
->list
, &hdev
->uuids
);
2042 hci_req_init(&req
, hdev
);
2047 err
= hci_req_run(&req
, add_uuid_complete
);
2049 if (err
!= -ENODATA
)
2052 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_ADD_UUID
, 0,
2053 hdev
->dev_class
, 3);
2057 cmd
= mgmt_pending_add(sk
, MGMT_OP_ADD_UUID
, hdev
, data
, len
);
2066 hci_dev_unlock(hdev
);
2070 static bool enable_service_cache(struct hci_dev
*hdev
)
2072 if (!hdev_is_powered(hdev
))
2075 if (!test_and_set_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
)) {
2076 queue_delayed_work(hdev
->workqueue
, &hdev
->service_cache
,
2084 static void remove_uuid_complete(struct hci_dev
*hdev
, u8 status
)
2086 BT_DBG("status 0x%02x", status
);
2088 mgmt_class_complete(hdev
, MGMT_OP_REMOVE_UUID
, status
);
2091 static int remove_uuid(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2094 struct mgmt_cp_remove_uuid
*cp
= data
;
2095 struct pending_cmd
*cmd
;
2096 struct bt_uuid
*match
, *tmp
;
2097 u8 bt_uuid_any
[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2098 struct hci_request req
;
2101 BT_DBG("request for %s", hdev
->name
);
2105 if (pending_eir_or_class(hdev
)) {
2106 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
,
2111 if (memcmp(cp
->uuid
, bt_uuid_any
, 16) == 0) {
2112 hci_uuids_clear(hdev
);
2114 if (enable_service_cache(hdev
)) {
2115 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
,
2116 0, hdev
->dev_class
, 3);
2125 list_for_each_entry_safe(match
, tmp
, &hdev
->uuids
, list
) {
2126 if (memcmp(match
->uuid
, cp
->uuid
, 16) != 0)
2129 list_del(&match
->list
);
2135 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
,
2136 MGMT_STATUS_INVALID_PARAMS
);
2141 hci_req_init(&req
, hdev
);
2146 err
= hci_req_run(&req
, remove_uuid_complete
);
2148 if (err
!= -ENODATA
)
2151 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
, 0,
2152 hdev
->dev_class
, 3);
2156 cmd
= mgmt_pending_add(sk
, MGMT_OP_REMOVE_UUID
, hdev
, data
, len
);
2165 hci_dev_unlock(hdev
);
2169 static void set_class_complete(struct hci_dev
*hdev
, u8 status
)
2171 BT_DBG("status 0x%02x", status
);
2173 mgmt_class_complete(hdev
, MGMT_OP_SET_DEV_CLASS
, status
);
2176 static int set_dev_class(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2179 struct mgmt_cp_set_dev_class
*cp
= data
;
2180 struct pending_cmd
*cmd
;
2181 struct hci_request req
;
2184 BT_DBG("request for %s", hdev
->name
);
2186 if (!lmp_bredr_capable(hdev
))
2187 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
,
2188 MGMT_STATUS_NOT_SUPPORTED
);
2192 if (pending_eir_or_class(hdev
)) {
2193 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
,
2198 if ((cp
->minor
& 0x03) != 0 || (cp
->major
& 0xe0) != 0) {
2199 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
,
2200 MGMT_STATUS_INVALID_PARAMS
);
2204 hdev
->major_class
= cp
->major
;
2205 hdev
->minor_class
= cp
->minor
;
2207 if (!hdev_is_powered(hdev
)) {
2208 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
, 0,
2209 hdev
->dev_class
, 3);
2213 hci_req_init(&req
, hdev
);
2215 if (test_and_clear_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
)) {
2216 hci_dev_unlock(hdev
);
2217 cancel_delayed_work_sync(&hdev
->service_cache
);
2224 err
= hci_req_run(&req
, set_class_complete
);
2226 if (err
!= -ENODATA
)
2229 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
, 0,
2230 hdev
->dev_class
, 3);
2234 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_DEV_CLASS
, hdev
, data
, len
);
2243 hci_dev_unlock(hdev
);
2247 static int load_link_keys(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2250 struct mgmt_cp_load_link_keys
*cp
= data
;
2251 u16 key_count
, expected_len
;
2255 BT_DBG("request for %s", hdev
->name
);
2257 if (!lmp_bredr_capable(hdev
))
2258 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2259 MGMT_STATUS_NOT_SUPPORTED
);
2261 key_count
= __le16_to_cpu(cp
->key_count
);
2263 expected_len
= sizeof(*cp
) + key_count
*
2264 sizeof(struct mgmt_link_key_info
);
2265 if (expected_len
!= len
) {
2266 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
2268 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2269 MGMT_STATUS_INVALID_PARAMS
);
2272 if (cp
->debug_keys
!= 0x00 && cp
->debug_keys
!= 0x01)
2273 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2274 MGMT_STATUS_INVALID_PARAMS
);
2276 BT_DBG("%s debug_keys %u key_count %u", hdev
->name
, cp
->debug_keys
,
2279 for (i
= 0; i
< key_count
; i
++) {
2280 struct mgmt_link_key_info
*key
= &cp
->keys
[i
];
2282 if (key
->addr
.type
!= BDADDR_BREDR
|| key
->type
> 0x08)
2283 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2284 MGMT_STATUS_INVALID_PARAMS
);
2289 hci_link_keys_clear(hdev
);
2292 changed
= !test_and_set_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
2294 changed
= test_and_clear_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
2297 new_settings(hdev
, NULL
);
2299 for (i
= 0; i
< key_count
; i
++) {
2300 struct mgmt_link_key_info
*key
= &cp
->keys
[i
];
2302 hci_add_link_key(hdev
, NULL
, 0, &key
->addr
.bdaddr
, key
->val
,
2303 key
->type
, key
->pin_len
);
2306 cmd_complete(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
, 0, NULL
, 0);
2308 hci_dev_unlock(hdev
);
2313 static int device_unpaired(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
2314 u8 addr_type
, struct sock
*skip_sk
)
2316 struct mgmt_ev_device_unpaired ev
;
2318 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
2319 ev
.addr
.type
= addr_type
;
2321 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED
, hdev
, &ev
, sizeof(ev
),
2325 static int unpair_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2328 struct mgmt_cp_unpair_device
*cp
= data
;
2329 struct mgmt_rp_unpair_device rp
;
2330 struct hci_cp_disconnect dc
;
2331 struct pending_cmd
*cmd
;
2332 struct hci_conn
*conn
;
2335 memset(&rp
, 0, sizeof(rp
));
2336 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
2337 rp
.addr
.type
= cp
->addr
.type
;
2339 if (!bdaddr_type_is_valid(cp
->addr
.type
))
2340 return cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2341 MGMT_STATUS_INVALID_PARAMS
,
2344 if (cp
->disconnect
!= 0x00 && cp
->disconnect
!= 0x01)
2345 return cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2346 MGMT_STATUS_INVALID_PARAMS
,
2351 if (!hdev_is_powered(hdev
)) {
2352 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2353 MGMT_STATUS_NOT_POWERED
, &rp
, sizeof(rp
));
2357 if (cp
->addr
.type
== BDADDR_BREDR
) {
2358 err
= hci_remove_link_key(hdev
, &cp
->addr
.bdaddr
);
2362 if (cp
->addr
.type
== BDADDR_LE_PUBLIC
)
2363 addr_type
= ADDR_LE_DEV_PUBLIC
;
2365 addr_type
= ADDR_LE_DEV_RANDOM
;
2367 hci_remove_irk(hdev
, &cp
->addr
.bdaddr
, addr_type
);
2369 err
= hci_remove_ltk(hdev
, &cp
->addr
.bdaddr
, addr_type
);
2373 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2374 MGMT_STATUS_NOT_PAIRED
, &rp
, sizeof(rp
));
2378 if (cp
->disconnect
) {
2379 if (cp
->addr
.type
== BDADDR_BREDR
)
2380 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
,
2383 conn
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
,
2390 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
, 0,
2392 device_unpaired(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
, sk
);
2396 cmd
= mgmt_pending_add(sk
, MGMT_OP_UNPAIR_DEVICE
, hdev
, cp
,
2403 dc
.handle
= cpu_to_le16(conn
->handle
);
2404 dc
.reason
= 0x13; /* Remote User Terminated Connection */
2405 err
= hci_send_cmd(hdev
, HCI_OP_DISCONNECT
, sizeof(dc
), &dc
);
2407 mgmt_pending_remove(cmd
);
2410 hci_dev_unlock(hdev
);
2414 static int disconnect(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2417 struct mgmt_cp_disconnect
*cp
= data
;
2418 struct mgmt_rp_disconnect rp
;
2419 struct hci_cp_disconnect dc
;
2420 struct pending_cmd
*cmd
;
2421 struct hci_conn
*conn
;
2426 memset(&rp
, 0, sizeof(rp
));
2427 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
2428 rp
.addr
.type
= cp
->addr
.type
;
2430 if (!bdaddr_type_is_valid(cp
->addr
.type
))
2431 return cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2432 MGMT_STATUS_INVALID_PARAMS
,
2437 if (!test_bit(HCI_UP
, &hdev
->flags
)) {
2438 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2439 MGMT_STATUS_NOT_POWERED
, &rp
, sizeof(rp
));
2443 if (mgmt_pending_find(MGMT_OP_DISCONNECT
, hdev
)) {
2444 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2445 MGMT_STATUS_BUSY
, &rp
, sizeof(rp
));
2449 if (cp
->addr
.type
== BDADDR_BREDR
)
2450 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
,
2453 conn
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
, &cp
->addr
.bdaddr
);
2455 if (!conn
|| conn
->state
== BT_OPEN
|| conn
->state
== BT_CLOSED
) {
2456 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2457 MGMT_STATUS_NOT_CONNECTED
, &rp
, sizeof(rp
));
2461 cmd
= mgmt_pending_add(sk
, MGMT_OP_DISCONNECT
, hdev
, data
, len
);
2467 dc
.handle
= cpu_to_le16(conn
->handle
);
2468 dc
.reason
= HCI_ERROR_REMOTE_USER_TERM
;
2470 err
= hci_send_cmd(hdev
, HCI_OP_DISCONNECT
, sizeof(dc
), &dc
);
2472 mgmt_pending_remove(cmd
);
2475 hci_dev_unlock(hdev
);
2479 static u8
link_to_bdaddr(u8 link_type
, u8 addr_type
)
2481 switch (link_type
) {
2483 switch (addr_type
) {
2484 case ADDR_LE_DEV_PUBLIC
:
2485 return BDADDR_LE_PUBLIC
;
2488 /* Fallback to LE Random address type */
2489 return BDADDR_LE_RANDOM
;
2493 /* Fallback to BR/EDR type */
2494 return BDADDR_BREDR
;
2498 static int get_connections(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2501 struct mgmt_rp_get_connections
*rp
;
2511 if (!hdev_is_powered(hdev
)) {
2512 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_GET_CONNECTIONS
,
2513 MGMT_STATUS_NOT_POWERED
);
2518 list_for_each_entry(c
, &hdev
->conn_hash
.list
, list
) {
2519 if (test_bit(HCI_CONN_MGMT_CONNECTED
, &c
->flags
))
2523 rp_len
= sizeof(*rp
) + (i
* sizeof(struct mgmt_addr_info
));
2524 rp
= kmalloc(rp_len
, GFP_KERNEL
);
2531 list_for_each_entry(c
, &hdev
->conn_hash
.list
, list
) {
2532 if (!test_bit(HCI_CONN_MGMT_CONNECTED
, &c
->flags
))
2534 bacpy(&rp
->addr
[i
].bdaddr
, &c
->dst
);
2535 rp
->addr
[i
].type
= link_to_bdaddr(c
->type
, c
->dst_type
);
2536 if (c
->type
== SCO_LINK
|| c
->type
== ESCO_LINK
)
2541 rp
->conn_count
= cpu_to_le16(i
);
2543 /* Recalculate length in case of filtered SCO connections, etc */
2544 rp_len
= sizeof(*rp
) + (i
* sizeof(struct mgmt_addr_info
));
2546 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_GET_CONNECTIONS
, 0, rp
,
2552 hci_dev_unlock(hdev
);
2556 static int send_pin_code_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
2557 struct mgmt_cp_pin_code_neg_reply
*cp
)
2559 struct pending_cmd
*cmd
;
2562 cmd
= mgmt_pending_add(sk
, MGMT_OP_PIN_CODE_NEG_REPLY
, hdev
, cp
,
2567 err
= hci_send_cmd(hdev
, HCI_OP_PIN_CODE_NEG_REPLY
,
2568 sizeof(cp
->addr
.bdaddr
), &cp
->addr
.bdaddr
);
2570 mgmt_pending_remove(cmd
);
2575 static int pin_code_reply(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2578 struct hci_conn
*conn
;
2579 struct mgmt_cp_pin_code_reply
*cp
= data
;
2580 struct hci_cp_pin_code_reply reply
;
2581 struct pending_cmd
*cmd
;
2588 if (!hdev_is_powered(hdev
)) {
2589 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
2590 MGMT_STATUS_NOT_POWERED
);
2594 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &cp
->addr
.bdaddr
);
2596 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
2597 MGMT_STATUS_NOT_CONNECTED
);
2601 if (conn
->pending_sec_level
== BT_SECURITY_HIGH
&& cp
->pin_len
!= 16) {
2602 struct mgmt_cp_pin_code_neg_reply ncp
;
2604 memcpy(&ncp
.addr
, &cp
->addr
, sizeof(ncp
.addr
));
2606 BT_ERR("PIN code is not 16 bytes long");
2608 err
= send_pin_code_neg_reply(sk
, hdev
, &ncp
);
2610 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
2611 MGMT_STATUS_INVALID_PARAMS
);
2616 cmd
= mgmt_pending_add(sk
, MGMT_OP_PIN_CODE_REPLY
, hdev
, data
, len
);
2622 bacpy(&reply
.bdaddr
, &cp
->addr
.bdaddr
);
2623 reply
.pin_len
= cp
->pin_len
;
2624 memcpy(reply
.pin_code
, cp
->pin_code
, sizeof(reply
.pin_code
));
2626 err
= hci_send_cmd(hdev
, HCI_OP_PIN_CODE_REPLY
, sizeof(reply
), &reply
);
2628 mgmt_pending_remove(cmd
);
2631 hci_dev_unlock(hdev
);
2635 static int set_io_capability(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2638 struct mgmt_cp_set_io_capability
*cp
= data
;
2644 hdev
->io_capability
= cp
->io_capability
;
2646 BT_DBG("%s IO capability set to 0x%02x", hdev
->name
,
2647 hdev
->io_capability
);
2649 hci_dev_unlock(hdev
);
2651 return cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_IO_CAPABILITY
, 0, NULL
,
2655 static struct pending_cmd
*find_pairing(struct hci_conn
*conn
)
2657 struct hci_dev
*hdev
= conn
->hdev
;
2658 struct pending_cmd
*cmd
;
2660 list_for_each_entry(cmd
, &hdev
->mgmt_pending
, list
) {
2661 if (cmd
->opcode
!= MGMT_OP_PAIR_DEVICE
)
2664 if (cmd
->user_data
!= conn
)
2673 static void pairing_complete(struct pending_cmd
*cmd
, u8 status
)
2675 struct mgmt_rp_pair_device rp
;
2676 struct hci_conn
*conn
= cmd
->user_data
;
2678 bacpy(&rp
.addr
.bdaddr
, &conn
->dst
);
2679 rp
.addr
.type
= link_to_bdaddr(conn
->type
, conn
->dst_type
);
2681 cmd_complete(cmd
->sk
, cmd
->index
, MGMT_OP_PAIR_DEVICE
, status
,
2684 /* So we don't get further callbacks for this connection */
2685 conn
->connect_cfm_cb
= NULL
;
2686 conn
->security_cfm_cb
= NULL
;
2687 conn
->disconn_cfm_cb
= NULL
;
2689 hci_conn_drop(conn
);
2691 mgmt_pending_remove(cmd
);
2694 void mgmt_smp_complete(struct hci_conn
*conn
, bool complete
)
2696 u8 status
= complete
? MGMT_STATUS_SUCCESS
: MGMT_STATUS_FAILED
;
2697 struct pending_cmd
*cmd
;
2699 cmd
= find_pairing(conn
);
2701 pairing_complete(cmd
, status
);
2704 static void pairing_complete_cb(struct hci_conn
*conn
, u8 status
)
2706 struct pending_cmd
*cmd
;
2708 BT_DBG("status %u", status
);
2710 cmd
= find_pairing(conn
);
2712 BT_DBG("Unable to find a pending command");
2714 pairing_complete(cmd
, mgmt_status(status
));
2717 static void le_pairing_complete_cb(struct hci_conn
*conn
, u8 status
)
2719 struct pending_cmd
*cmd
;
2721 BT_DBG("status %u", status
);
2726 cmd
= find_pairing(conn
);
2728 BT_DBG("Unable to find a pending command");
2730 pairing_complete(cmd
, mgmt_status(status
));
2733 static int pair_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2736 struct mgmt_cp_pair_device
*cp
= data
;
2737 struct mgmt_rp_pair_device rp
;
2738 struct pending_cmd
*cmd
;
2739 u8 sec_level
, auth_type
;
2740 struct hci_conn
*conn
;
2745 memset(&rp
, 0, sizeof(rp
));
2746 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
2747 rp
.addr
.type
= cp
->addr
.type
;
2749 if (!bdaddr_type_is_valid(cp
->addr
.type
))
2750 return cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2751 MGMT_STATUS_INVALID_PARAMS
,
2756 if (!hdev_is_powered(hdev
)) {
2757 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2758 MGMT_STATUS_NOT_POWERED
, &rp
, sizeof(rp
));
2762 sec_level
= BT_SECURITY_MEDIUM
;
2763 if (cp
->io_cap
== 0x03)
2764 auth_type
= HCI_AT_DEDICATED_BONDING
;
2766 auth_type
= HCI_AT_DEDICATED_BONDING_MITM
;
2768 if (cp
->addr
.type
== BDADDR_BREDR
)
2769 conn
= hci_connect(hdev
, ACL_LINK
, &cp
->addr
.bdaddr
,
2770 cp
->addr
.type
, sec_level
, auth_type
);
2772 conn
= hci_connect(hdev
, LE_LINK
, &cp
->addr
.bdaddr
,
2773 cp
->addr
.type
, sec_level
, auth_type
);
2778 if (PTR_ERR(conn
) == -EBUSY
)
2779 status
= MGMT_STATUS_BUSY
;
2781 status
= MGMT_STATUS_CONNECT_FAILED
;
2783 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2789 if (conn
->connect_cfm_cb
) {
2790 hci_conn_drop(conn
);
2791 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2792 MGMT_STATUS_BUSY
, &rp
, sizeof(rp
));
2796 cmd
= mgmt_pending_add(sk
, MGMT_OP_PAIR_DEVICE
, hdev
, data
, len
);
2799 hci_conn_drop(conn
);
2803 /* For LE, just connecting isn't a proof that the pairing finished */
2804 if (cp
->addr
.type
== BDADDR_BREDR
) {
2805 conn
->connect_cfm_cb
= pairing_complete_cb
;
2806 conn
->security_cfm_cb
= pairing_complete_cb
;
2807 conn
->disconn_cfm_cb
= pairing_complete_cb
;
2809 conn
->connect_cfm_cb
= le_pairing_complete_cb
;
2810 conn
->security_cfm_cb
= le_pairing_complete_cb
;
2811 conn
->disconn_cfm_cb
= le_pairing_complete_cb
;
2814 conn
->io_capability
= cp
->io_cap
;
2815 cmd
->user_data
= conn
;
2817 if (conn
->state
== BT_CONNECTED
&&
2818 hci_conn_security(conn
, sec_level
, auth_type
))
2819 pairing_complete(cmd
, 0);
2824 hci_dev_unlock(hdev
);
2828 static int cancel_pair_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2831 struct mgmt_addr_info
*addr
= data
;
2832 struct pending_cmd
*cmd
;
2833 struct hci_conn
*conn
;
2840 if (!hdev_is_powered(hdev
)) {
2841 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
,
2842 MGMT_STATUS_NOT_POWERED
);
2846 cmd
= mgmt_pending_find(MGMT_OP_PAIR_DEVICE
, hdev
);
2848 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
,
2849 MGMT_STATUS_INVALID_PARAMS
);
2853 conn
= cmd
->user_data
;
2855 if (bacmp(&addr
->bdaddr
, &conn
->dst
) != 0) {
2856 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
,
2857 MGMT_STATUS_INVALID_PARAMS
);
2861 pairing_complete(cmd
, MGMT_STATUS_CANCELLED
);
2863 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
, 0,
2864 addr
, sizeof(*addr
));
2866 hci_dev_unlock(hdev
);
2870 static int user_pairing_resp(struct sock
*sk
, struct hci_dev
*hdev
,
2871 struct mgmt_addr_info
*addr
, u16 mgmt_op
,
2872 u16 hci_op
, __le32 passkey
)
2874 struct pending_cmd
*cmd
;
2875 struct hci_conn
*conn
;
2880 if (!hdev_is_powered(hdev
)) {
2881 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2882 MGMT_STATUS_NOT_POWERED
, addr
,
2887 if (addr
->type
== BDADDR_BREDR
)
2888 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &addr
->bdaddr
);
2890 conn
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
, &addr
->bdaddr
);
2893 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2894 MGMT_STATUS_NOT_CONNECTED
, addr
,
2899 if (addr
->type
== BDADDR_LE_PUBLIC
|| addr
->type
== BDADDR_LE_RANDOM
) {
2900 /* Continue with pairing via SMP */
2901 err
= smp_user_confirm_reply(conn
, mgmt_op
, passkey
);
2904 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2905 MGMT_STATUS_SUCCESS
, addr
,
2908 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2909 MGMT_STATUS_FAILED
, addr
,
2915 cmd
= mgmt_pending_add(sk
, mgmt_op
, hdev
, addr
, sizeof(*addr
));
2921 /* Continue with pairing via HCI */
2922 if (hci_op
== HCI_OP_USER_PASSKEY_REPLY
) {
2923 struct hci_cp_user_passkey_reply cp
;
2925 bacpy(&cp
.bdaddr
, &addr
->bdaddr
);
2926 cp
.passkey
= passkey
;
2927 err
= hci_send_cmd(hdev
, hci_op
, sizeof(cp
), &cp
);
2929 err
= hci_send_cmd(hdev
, hci_op
, sizeof(addr
->bdaddr
),
2933 mgmt_pending_remove(cmd
);
2936 hci_dev_unlock(hdev
);
2940 static int pin_code_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
2941 void *data
, u16 len
)
2943 struct mgmt_cp_pin_code_neg_reply
*cp
= data
;
2947 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
2948 MGMT_OP_PIN_CODE_NEG_REPLY
,
2949 HCI_OP_PIN_CODE_NEG_REPLY
, 0);
2952 static int user_confirm_reply(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2955 struct mgmt_cp_user_confirm_reply
*cp
= data
;
2959 if (len
!= sizeof(*cp
))
2960 return cmd_status(sk
, hdev
->id
, MGMT_OP_USER_CONFIRM_REPLY
,
2961 MGMT_STATUS_INVALID_PARAMS
);
2963 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
2964 MGMT_OP_USER_CONFIRM_REPLY
,
2965 HCI_OP_USER_CONFIRM_REPLY
, 0);
2968 static int user_confirm_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
2969 void *data
, u16 len
)
2971 struct mgmt_cp_user_confirm_neg_reply
*cp
= data
;
2975 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
2976 MGMT_OP_USER_CONFIRM_NEG_REPLY
,
2977 HCI_OP_USER_CONFIRM_NEG_REPLY
, 0);
2980 static int user_passkey_reply(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2983 struct mgmt_cp_user_passkey_reply
*cp
= data
;
2987 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
2988 MGMT_OP_USER_PASSKEY_REPLY
,
2989 HCI_OP_USER_PASSKEY_REPLY
, cp
->passkey
);
2992 static int user_passkey_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
2993 void *data
, u16 len
)
2995 struct mgmt_cp_user_passkey_neg_reply
*cp
= data
;
2999 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
3000 MGMT_OP_USER_PASSKEY_NEG_REPLY
,
3001 HCI_OP_USER_PASSKEY_NEG_REPLY
, 0);
3004 static void update_name(struct hci_request
*req
)
3006 struct hci_dev
*hdev
= req
->hdev
;
3007 struct hci_cp_write_local_name cp
;
3009 memcpy(cp
.name
, hdev
->dev_name
, sizeof(cp
.name
));
3011 hci_req_add(req
, HCI_OP_WRITE_LOCAL_NAME
, sizeof(cp
), &cp
);
3014 static void set_name_complete(struct hci_dev
*hdev
, u8 status
)
3016 struct mgmt_cp_set_local_name
*cp
;
3017 struct pending_cmd
*cmd
;
3019 BT_DBG("status 0x%02x", status
);
3023 cmd
= mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME
, hdev
);
3030 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
,
3031 mgmt_status(status
));
3033 cmd_complete(cmd
->sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
, 0,
3036 mgmt_pending_remove(cmd
);
3039 hci_dev_unlock(hdev
);
3042 static int set_local_name(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3045 struct mgmt_cp_set_local_name
*cp
= data
;
3046 struct pending_cmd
*cmd
;
3047 struct hci_request req
;
3054 /* If the old values are the same as the new ones just return a
3055 * direct command complete event.
3057 if (!memcmp(hdev
->dev_name
, cp
->name
, sizeof(hdev
->dev_name
)) &&
3058 !memcmp(hdev
->short_name
, cp
->short_name
,
3059 sizeof(hdev
->short_name
))) {
3060 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
, 0,
3065 memcpy(hdev
->short_name
, cp
->short_name
, sizeof(hdev
->short_name
));
3067 if (!hdev_is_powered(hdev
)) {
3068 memcpy(hdev
->dev_name
, cp
->name
, sizeof(hdev
->dev_name
));
3070 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
, 0,
3075 err
= mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED
, hdev
, data
, len
,
3081 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_LOCAL_NAME
, hdev
, data
, len
);
3087 memcpy(hdev
->dev_name
, cp
->name
, sizeof(hdev
->dev_name
));
3089 hci_req_init(&req
, hdev
);
3091 if (lmp_bredr_capable(hdev
)) {
3096 /* The name is stored in the scan response data and so
3097 * no need to udpate the advertising data here.
3099 if (lmp_le_capable(hdev
))
3100 update_scan_rsp_data(&req
);
3102 err
= hci_req_run(&req
, set_name_complete
);
3104 mgmt_pending_remove(cmd
);
3107 hci_dev_unlock(hdev
);
3111 static int read_local_oob_data(struct sock
*sk
, struct hci_dev
*hdev
,
3112 void *data
, u16 data_len
)
3114 struct pending_cmd
*cmd
;
3117 BT_DBG("%s", hdev
->name
);
3121 if (!hdev_is_powered(hdev
)) {
3122 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
3123 MGMT_STATUS_NOT_POWERED
);
3127 if (!lmp_ssp_capable(hdev
)) {
3128 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
3129 MGMT_STATUS_NOT_SUPPORTED
);
3133 if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA
, hdev
)) {
3134 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
3139 cmd
= mgmt_pending_add(sk
, MGMT_OP_READ_LOCAL_OOB_DATA
, hdev
, NULL
, 0);
3145 if (test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
))
3146 err
= hci_send_cmd(hdev
, HCI_OP_READ_LOCAL_OOB_EXT_DATA
,
3149 err
= hci_send_cmd(hdev
, HCI_OP_READ_LOCAL_OOB_DATA
, 0, NULL
);
3152 mgmt_pending_remove(cmd
);
3155 hci_dev_unlock(hdev
);
3159 static int add_remote_oob_data(struct sock
*sk
, struct hci_dev
*hdev
,
3160 void *data
, u16 len
)
3164 BT_DBG("%s ", hdev
->name
);
3168 if (len
== MGMT_ADD_REMOTE_OOB_DATA_SIZE
) {
3169 struct mgmt_cp_add_remote_oob_data
*cp
= data
;
3172 err
= hci_add_remote_oob_data(hdev
, &cp
->addr
.bdaddr
,
3173 cp
->hash
, cp
->randomizer
);
3175 status
= MGMT_STATUS_FAILED
;
3177 status
= MGMT_STATUS_SUCCESS
;
3179 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_ADD_REMOTE_OOB_DATA
,
3180 status
, &cp
->addr
, sizeof(cp
->addr
));
3181 } else if (len
== MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE
) {
3182 struct mgmt_cp_add_remote_oob_ext_data
*cp
= data
;
3185 err
= hci_add_remote_oob_ext_data(hdev
, &cp
->addr
.bdaddr
,
3191 status
= MGMT_STATUS_FAILED
;
3193 status
= MGMT_STATUS_SUCCESS
;
3195 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_ADD_REMOTE_OOB_DATA
,
3196 status
, &cp
->addr
, sizeof(cp
->addr
));
3198 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len
);
3199 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_ADD_REMOTE_OOB_DATA
,
3200 MGMT_STATUS_INVALID_PARAMS
);
3203 hci_dev_unlock(hdev
);
3207 static int remove_remote_oob_data(struct sock
*sk
, struct hci_dev
*hdev
,
3208 void *data
, u16 len
)
3210 struct mgmt_cp_remove_remote_oob_data
*cp
= data
;
3214 BT_DBG("%s", hdev
->name
);
3218 err
= hci_remove_remote_oob_data(hdev
, &cp
->addr
.bdaddr
);
3220 status
= MGMT_STATUS_INVALID_PARAMS
;
3222 status
= MGMT_STATUS_SUCCESS
;
3224 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_REMOVE_REMOTE_OOB_DATA
,
3225 status
, &cp
->addr
, sizeof(cp
->addr
));
3227 hci_dev_unlock(hdev
);
3231 static int mgmt_start_discovery_failed(struct hci_dev
*hdev
, u8 status
)
3233 struct pending_cmd
*cmd
;
3237 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
3239 cmd
= mgmt_pending_find(MGMT_OP_START_DISCOVERY
, hdev
);
3243 type
= hdev
->discovery
.type
;
3245 err
= cmd_complete(cmd
->sk
, hdev
->id
, cmd
->opcode
, mgmt_status(status
),
3246 &type
, sizeof(type
));
3247 mgmt_pending_remove(cmd
);
3252 static void start_discovery_complete(struct hci_dev
*hdev
, u8 status
)
3254 BT_DBG("status %d", status
);
3258 mgmt_start_discovery_failed(hdev
, status
);
3259 hci_dev_unlock(hdev
);
3264 hci_discovery_set_state(hdev
, DISCOVERY_FINDING
);
3265 hci_dev_unlock(hdev
);
3267 switch (hdev
->discovery
.type
) {
3268 case DISCOV_TYPE_LE
:
3269 queue_delayed_work(hdev
->workqueue
, &hdev
->le_scan_disable
,
3273 case DISCOV_TYPE_INTERLEAVED
:
3274 queue_delayed_work(hdev
->workqueue
, &hdev
->le_scan_disable
,
3275 DISCOV_INTERLEAVED_TIMEOUT
);
3278 case DISCOV_TYPE_BREDR
:
3282 BT_ERR("Invalid discovery type %d", hdev
->discovery
.type
);
3286 static int start_discovery(struct sock
*sk
, struct hci_dev
*hdev
,
3287 void *data
, u16 len
)
3289 struct mgmt_cp_start_discovery
*cp
= data
;
3290 struct pending_cmd
*cmd
;
3291 struct hci_cp_le_set_scan_param param_cp
;
3292 struct hci_cp_le_set_scan_enable enable_cp
;
3293 struct hci_cp_inquiry inq_cp
;
3294 struct hci_request req
;
3295 /* General inquiry access code (GIAC) */
3296 u8 lap
[3] = { 0x33, 0x8b, 0x9e };
3300 BT_DBG("%s", hdev
->name
);
3304 if (!hdev_is_powered(hdev
)) {
3305 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3306 MGMT_STATUS_NOT_POWERED
);
3310 if (test_bit(HCI_PERIODIC_INQ
, &hdev
->dev_flags
)) {
3311 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3316 if (hdev
->discovery
.state
!= DISCOVERY_STOPPED
) {
3317 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3322 cmd
= mgmt_pending_add(sk
, MGMT_OP_START_DISCOVERY
, hdev
, NULL
, 0);
3328 hdev
->discovery
.type
= cp
->type
;
3330 hci_req_init(&req
, hdev
);
3332 switch (hdev
->discovery
.type
) {
3333 case DISCOV_TYPE_BREDR
:
3334 status
= mgmt_bredr_support(hdev
);
3336 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3338 mgmt_pending_remove(cmd
);
3342 if (test_bit(HCI_INQUIRY
, &hdev
->flags
)) {
3343 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3345 mgmt_pending_remove(cmd
);
3349 hci_inquiry_cache_flush(hdev
);
3351 memset(&inq_cp
, 0, sizeof(inq_cp
));
3352 memcpy(&inq_cp
.lap
, lap
, sizeof(inq_cp
.lap
));
3353 inq_cp
.length
= DISCOV_BREDR_INQUIRY_LEN
;
3354 hci_req_add(&req
, HCI_OP_INQUIRY
, sizeof(inq_cp
), &inq_cp
);
3357 case DISCOV_TYPE_LE
:
3358 case DISCOV_TYPE_INTERLEAVED
:
3359 status
= mgmt_le_support(hdev
);
3361 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3363 mgmt_pending_remove(cmd
);
3367 if (hdev
->discovery
.type
== DISCOV_TYPE_INTERLEAVED
&&
3368 !test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
3369 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3370 MGMT_STATUS_NOT_SUPPORTED
);
3371 mgmt_pending_remove(cmd
);
3375 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
)) {
3376 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3377 MGMT_STATUS_REJECTED
);
3378 mgmt_pending_remove(cmd
);
3382 if (test_bit(HCI_LE_SCAN
, &hdev
->dev_flags
)) {
3383 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3385 mgmt_pending_remove(cmd
);
3389 memset(¶m_cp
, 0, sizeof(param_cp
));
3390 param_cp
.type
= LE_SCAN_ACTIVE
;
3391 param_cp
.interval
= cpu_to_le16(DISCOV_LE_SCAN_INT
);
3392 param_cp
.window
= cpu_to_le16(DISCOV_LE_SCAN_WIN
);
3393 param_cp
.own_address_type
= hdev
->own_addr_type
;
3394 hci_req_add(&req
, HCI_OP_LE_SET_SCAN_PARAM
, sizeof(param_cp
),
3397 memset(&enable_cp
, 0, sizeof(enable_cp
));
3398 enable_cp
.enable
= LE_SCAN_ENABLE
;
3399 enable_cp
.filter_dup
= LE_SCAN_FILTER_DUP_ENABLE
;
3400 hci_req_add(&req
, HCI_OP_LE_SET_SCAN_ENABLE
, sizeof(enable_cp
),
3405 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3406 MGMT_STATUS_INVALID_PARAMS
);
3407 mgmt_pending_remove(cmd
);
3411 err
= hci_req_run(&req
, start_discovery_complete
);
3413 mgmt_pending_remove(cmd
);
3415 hci_discovery_set_state(hdev
, DISCOVERY_STARTING
);
3418 hci_dev_unlock(hdev
);
3422 static int mgmt_stop_discovery_failed(struct hci_dev
*hdev
, u8 status
)
3424 struct pending_cmd
*cmd
;
3427 cmd
= mgmt_pending_find(MGMT_OP_STOP_DISCOVERY
, hdev
);
3431 err
= cmd_complete(cmd
->sk
, hdev
->id
, cmd
->opcode
, mgmt_status(status
),
3432 &hdev
->discovery
.type
, sizeof(hdev
->discovery
.type
));
3433 mgmt_pending_remove(cmd
);
3438 static void stop_discovery_complete(struct hci_dev
*hdev
, u8 status
)
3440 BT_DBG("status %d", status
);
3445 mgmt_stop_discovery_failed(hdev
, status
);
3449 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
3452 hci_dev_unlock(hdev
);
3455 static int stop_discovery(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3458 struct mgmt_cp_stop_discovery
*mgmt_cp
= data
;
3459 struct pending_cmd
*cmd
;
3460 struct hci_cp_remote_name_req_cancel cp
;
3461 struct inquiry_entry
*e
;
3462 struct hci_request req
;
3463 struct hci_cp_le_set_scan_enable enable_cp
;
3466 BT_DBG("%s", hdev
->name
);
3470 if (!hci_discovery_active(hdev
)) {
3471 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_STOP_DISCOVERY
,
3472 MGMT_STATUS_REJECTED
, &mgmt_cp
->type
,
3473 sizeof(mgmt_cp
->type
));
3477 if (hdev
->discovery
.type
!= mgmt_cp
->type
) {
3478 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_STOP_DISCOVERY
,
3479 MGMT_STATUS_INVALID_PARAMS
, &mgmt_cp
->type
,
3480 sizeof(mgmt_cp
->type
));
3484 cmd
= mgmt_pending_add(sk
, MGMT_OP_STOP_DISCOVERY
, hdev
, NULL
, 0);
3490 hci_req_init(&req
, hdev
);
3492 switch (hdev
->discovery
.state
) {
3493 case DISCOVERY_FINDING
:
3494 if (test_bit(HCI_INQUIRY
, &hdev
->flags
)) {
3495 hci_req_add(&req
, HCI_OP_INQUIRY_CANCEL
, 0, NULL
);
3497 cancel_delayed_work(&hdev
->le_scan_disable
);
3499 memset(&enable_cp
, 0, sizeof(enable_cp
));
3500 enable_cp
.enable
= LE_SCAN_DISABLE
;
3501 hci_req_add(&req
, HCI_OP_LE_SET_SCAN_ENABLE
,
3502 sizeof(enable_cp
), &enable_cp
);
3507 case DISCOVERY_RESOLVING
:
3508 e
= hci_inquiry_cache_lookup_resolve(hdev
, BDADDR_ANY
,
3511 mgmt_pending_remove(cmd
);
3512 err
= cmd_complete(sk
, hdev
->id
,
3513 MGMT_OP_STOP_DISCOVERY
, 0,
3515 sizeof(mgmt_cp
->type
));
3516 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
3520 bacpy(&cp
.bdaddr
, &e
->data
.bdaddr
);
3521 hci_req_add(&req
, HCI_OP_REMOTE_NAME_REQ_CANCEL
, sizeof(cp
),
3527 BT_DBG("unknown discovery state %u", hdev
->discovery
.state
);
3529 mgmt_pending_remove(cmd
);
3530 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_STOP_DISCOVERY
,
3531 MGMT_STATUS_FAILED
, &mgmt_cp
->type
,
3532 sizeof(mgmt_cp
->type
));
3536 err
= hci_req_run(&req
, stop_discovery_complete
);
3538 mgmt_pending_remove(cmd
);
3540 hci_discovery_set_state(hdev
, DISCOVERY_STOPPING
);
3543 hci_dev_unlock(hdev
);
3547 static int confirm_name(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3550 struct mgmt_cp_confirm_name
*cp
= data
;
3551 struct inquiry_entry
*e
;
3554 BT_DBG("%s", hdev
->name
);
3558 if (!hci_discovery_active(hdev
)) {
3559 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CONFIRM_NAME
,
3560 MGMT_STATUS_FAILED
);
3564 e
= hci_inquiry_cache_lookup_unknown(hdev
, &cp
->addr
.bdaddr
);
3566 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CONFIRM_NAME
,
3567 MGMT_STATUS_INVALID_PARAMS
);
3571 if (cp
->name_known
) {
3572 e
->name_state
= NAME_KNOWN
;
3575 e
->name_state
= NAME_NEEDED
;
3576 hci_inquiry_cache_update_resolve(hdev
, e
);
3579 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_CONFIRM_NAME
, 0, &cp
->addr
,
3583 hci_dev_unlock(hdev
);
3587 static int block_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3590 struct mgmt_cp_block_device
*cp
= data
;
3594 BT_DBG("%s", hdev
->name
);
3596 if (!bdaddr_type_is_valid(cp
->addr
.type
))
3597 return cmd_complete(sk
, hdev
->id
, MGMT_OP_BLOCK_DEVICE
,
3598 MGMT_STATUS_INVALID_PARAMS
,
3599 &cp
->addr
, sizeof(cp
->addr
));
3603 err
= hci_blacklist_add(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
);
3605 status
= MGMT_STATUS_FAILED
;
3607 status
= MGMT_STATUS_SUCCESS
;
3609 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_BLOCK_DEVICE
, status
,
3610 &cp
->addr
, sizeof(cp
->addr
));
3612 hci_dev_unlock(hdev
);
3617 static int unblock_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3620 struct mgmt_cp_unblock_device
*cp
= data
;
3624 BT_DBG("%s", hdev
->name
);
3626 if (!bdaddr_type_is_valid(cp
->addr
.type
))
3627 return cmd_complete(sk
, hdev
->id
, MGMT_OP_UNBLOCK_DEVICE
,
3628 MGMT_STATUS_INVALID_PARAMS
,
3629 &cp
->addr
, sizeof(cp
->addr
));
3633 err
= hci_blacklist_del(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
);
3635 status
= MGMT_STATUS_INVALID_PARAMS
;
3637 status
= MGMT_STATUS_SUCCESS
;
3639 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNBLOCK_DEVICE
, status
,
3640 &cp
->addr
, sizeof(cp
->addr
));
3642 hci_dev_unlock(hdev
);
3647 static int set_device_id(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3650 struct mgmt_cp_set_device_id
*cp
= data
;
3651 struct hci_request req
;
3655 BT_DBG("%s", hdev
->name
);
3657 source
= __le16_to_cpu(cp
->source
);
3659 if (source
> 0x0002)
3660 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEVICE_ID
,
3661 MGMT_STATUS_INVALID_PARAMS
);
3665 hdev
->devid_source
= source
;
3666 hdev
->devid_vendor
= __le16_to_cpu(cp
->vendor
);
3667 hdev
->devid_product
= __le16_to_cpu(cp
->product
);
3668 hdev
->devid_version
= __le16_to_cpu(cp
->version
);
3670 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_DEVICE_ID
, 0, NULL
, 0);
3672 hci_req_init(&req
, hdev
);
3674 hci_req_run(&req
, NULL
);
3676 hci_dev_unlock(hdev
);
3681 static void set_advertising_complete(struct hci_dev
*hdev
, u8 status
)
3683 struct cmd_lookup match
= { NULL
, hdev
};
3686 u8 mgmt_err
= mgmt_status(status
);
3688 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING
, hdev
,
3689 cmd_status_rsp
, &mgmt_err
);
3693 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING
, hdev
, settings_rsp
,
3696 new_settings(hdev
, match
.sk
);
3702 static int set_advertising(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3705 struct mgmt_mode
*cp
= data
;
3706 struct pending_cmd
*cmd
;
3707 struct hci_request req
;
3708 u8 val
, enabled
, status
;
3711 BT_DBG("request for %s", hdev
->name
);
3713 status
= mgmt_le_support(hdev
);
3715 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_ADVERTISING
,
3718 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
3719 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_ADVERTISING
,
3720 MGMT_STATUS_INVALID_PARAMS
);
3725 enabled
= test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
3727 /* The following conditions are ones which mean that we should
3728 * not do any HCI communication but directly send a mgmt
3729 * response to user space (after toggling the flag if
3732 if (!hdev_is_powered(hdev
) || val
== enabled
||
3733 hci_conn_num(hdev
, LE_LINK
) > 0) {
3734 bool changed
= false;
3736 if (val
!= test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
)) {
3737 change_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
3741 err
= send_settings_rsp(sk
, MGMT_OP_SET_ADVERTISING
, hdev
);
3746 err
= new_settings(hdev
, sk
);
3751 if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING
, hdev
) ||
3752 mgmt_pending_find(MGMT_OP_SET_LE
, hdev
)) {
3753 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_ADVERTISING
,
3758 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_ADVERTISING
, hdev
, data
, len
);
3764 hci_req_init(&req
, hdev
);
3767 enable_advertising(&req
);
3769 disable_advertising(&req
);
3771 err
= hci_req_run(&req
, set_advertising_complete
);
3773 mgmt_pending_remove(cmd
);
3776 hci_dev_unlock(hdev
);
3780 static int set_static_address(struct sock
*sk
, struct hci_dev
*hdev
,
3781 void *data
, u16 len
)
3783 struct mgmt_cp_set_static_address
*cp
= data
;
3786 BT_DBG("%s", hdev
->name
);
3788 if (!lmp_le_capable(hdev
))
3789 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_STATIC_ADDRESS
,
3790 MGMT_STATUS_NOT_SUPPORTED
);
3792 if (hdev_is_powered(hdev
))
3793 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_STATIC_ADDRESS
,
3794 MGMT_STATUS_REJECTED
);
3796 if (bacmp(&cp
->bdaddr
, BDADDR_ANY
)) {
3797 if (!bacmp(&cp
->bdaddr
, BDADDR_NONE
))
3798 return cmd_status(sk
, hdev
->id
,
3799 MGMT_OP_SET_STATIC_ADDRESS
,
3800 MGMT_STATUS_INVALID_PARAMS
);
3802 /* Two most significant bits shall be set */
3803 if ((cp
->bdaddr
.b
[5] & 0xc0) != 0xc0)
3804 return cmd_status(sk
, hdev
->id
,
3805 MGMT_OP_SET_STATIC_ADDRESS
,
3806 MGMT_STATUS_INVALID_PARAMS
);
3811 bacpy(&hdev
->static_addr
, &cp
->bdaddr
);
3813 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_STATIC_ADDRESS
, 0, NULL
, 0);
3815 hci_dev_unlock(hdev
);
3820 static int set_scan_params(struct sock
*sk
, struct hci_dev
*hdev
,
3821 void *data
, u16 len
)
3823 struct mgmt_cp_set_scan_params
*cp
= data
;
3824 __u16 interval
, window
;
3827 BT_DBG("%s", hdev
->name
);
3829 if (!lmp_le_capable(hdev
))
3830 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3831 MGMT_STATUS_NOT_SUPPORTED
);
3833 interval
= __le16_to_cpu(cp
->interval
);
3835 if (interval
< 0x0004 || interval
> 0x4000)
3836 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3837 MGMT_STATUS_INVALID_PARAMS
);
3839 window
= __le16_to_cpu(cp
->window
);
3841 if (window
< 0x0004 || window
> 0x4000)
3842 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3843 MGMT_STATUS_INVALID_PARAMS
);
3845 if (window
> interval
)
3846 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3847 MGMT_STATUS_INVALID_PARAMS
);
3851 hdev
->le_scan_interval
= interval
;
3852 hdev
->le_scan_window
= window
;
3854 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
, 0, NULL
, 0);
3856 hci_dev_unlock(hdev
);
3861 static void fast_connectable_complete(struct hci_dev
*hdev
, u8 status
)
3863 struct pending_cmd
*cmd
;
3865 BT_DBG("status 0x%02x", status
);
3869 cmd
= mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE
, hdev
);
3874 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3875 mgmt_status(status
));
3877 struct mgmt_mode
*cp
= cmd
->param
;
3880 set_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
);
3882 clear_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
);
3884 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_FAST_CONNECTABLE
, hdev
);
3885 new_settings(hdev
, cmd
->sk
);
3888 mgmt_pending_remove(cmd
);
3891 hci_dev_unlock(hdev
);
3894 static int set_fast_connectable(struct sock
*sk
, struct hci_dev
*hdev
,
3895 void *data
, u16 len
)
3897 struct mgmt_mode
*cp
= data
;
3898 struct pending_cmd
*cmd
;
3899 struct hci_request req
;
3902 BT_DBG("%s", hdev
->name
);
3904 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
) ||
3905 hdev
->hci_ver
< BLUETOOTH_VER_1_2
)
3906 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3907 MGMT_STATUS_NOT_SUPPORTED
);
3909 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
3910 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3911 MGMT_STATUS_INVALID_PARAMS
);
3913 if (!hdev_is_powered(hdev
))
3914 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3915 MGMT_STATUS_NOT_POWERED
);
3917 if (!test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
3918 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3919 MGMT_STATUS_REJECTED
);
3923 if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE
, hdev
)) {
3924 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3929 if (!!cp
->val
== test_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
)) {
3930 err
= send_settings_rsp(sk
, MGMT_OP_SET_FAST_CONNECTABLE
,
3935 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_FAST_CONNECTABLE
, hdev
,
3942 hci_req_init(&req
, hdev
);
3944 write_fast_connectable(&req
, cp
->val
);
3946 err
= hci_req_run(&req
, fast_connectable_complete
);
3948 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3949 MGMT_STATUS_FAILED
);
3950 mgmt_pending_remove(cmd
);
3954 hci_dev_unlock(hdev
);
3959 static void set_bredr_scan(struct hci_request
*req
)
3961 struct hci_dev
*hdev
= req
->hdev
;
3964 /* Ensure that fast connectable is disabled. This function will
3965 * not do anything if the page scan parameters are already what
3968 write_fast_connectable(req
, false);
3970 if (test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
3972 if (test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
))
3973 scan
|= SCAN_INQUIRY
;
3976 hci_req_add(req
, HCI_OP_WRITE_SCAN_ENABLE
, 1, &scan
);
3979 static void set_bredr_complete(struct hci_dev
*hdev
, u8 status
)
3981 struct pending_cmd
*cmd
;
3983 BT_DBG("status 0x%02x", status
);
3987 cmd
= mgmt_pending_find(MGMT_OP_SET_BREDR
, hdev
);
3992 u8 mgmt_err
= mgmt_status(status
);
3994 /* We need to restore the flag if related HCI commands
3997 clear_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
);
3999 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_err
);
4001 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_BREDR
, hdev
);
4002 new_settings(hdev
, cmd
->sk
);
4005 mgmt_pending_remove(cmd
);
4008 hci_dev_unlock(hdev
);
4011 static int set_bredr(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
4013 struct mgmt_mode
*cp
= data
;
4014 struct pending_cmd
*cmd
;
4015 struct hci_request req
;
4018 BT_DBG("request for %s", hdev
->name
);
4020 if (!lmp_bredr_capable(hdev
) || !lmp_le_capable(hdev
))
4021 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4022 MGMT_STATUS_NOT_SUPPORTED
);
4024 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
4025 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4026 MGMT_STATUS_REJECTED
);
4028 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
4029 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4030 MGMT_STATUS_INVALID_PARAMS
);
4034 if (cp
->val
== test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
4035 err
= send_settings_rsp(sk
, MGMT_OP_SET_BREDR
, hdev
);
4039 if (!hdev_is_powered(hdev
)) {
4041 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4042 clear_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
);
4043 clear_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
);
4044 clear_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
);
4045 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
4048 change_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
);
4050 err
= send_settings_rsp(sk
, MGMT_OP_SET_BREDR
, hdev
);
4054 err
= new_settings(hdev
, sk
);
4058 /* Reject disabling when powered on */
4060 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4061 MGMT_STATUS_REJECTED
);
4065 if (mgmt_pending_find(MGMT_OP_SET_BREDR
, hdev
)) {
4066 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4071 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_BREDR
, hdev
, data
, len
);
4077 /* We need to flip the bit already here so that update_adv_data
4078 * generates the correct flags.
4080 set_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
);
4082 hci_req_init(&req
, hdev
);
4084 if (test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
4085 set_bredr_scan(&req
);
4087 /* Since only the advertising data flags will change, there
4088 * is no need to update the scan response data.
4090 update_adv_data(&req
);
4092 err
= hci_req_run(&req
, set_bredr_complete
);
4094 mgmt_pending_remove(cmd
);
4097 hci_dev_unlock(hdev
);
4101 static int set_secure_conn(struct sock
*sk
, struct hci_dev
*hdev
,
4102 void *data
, u16 len
)
4104 struct mgmt_mode
*cp
= data
;
4105 struct pending_cmd
*cmd
;
4109 BT_DBG("request for %s", hdev
->name
);
4111 status
= mgmt_bredr_support(hdev
);
4113 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4116 if (!lmp_sc_capable(hdev
) &&
4117 !test_bit(HCI_FORCE_SC
, &hdev
->dev_flags
))
4118 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4119 MGMT_STATUS_NOT_SUPPORTED
);
4121 if (cp
->val
!= 0x00 && cp
->val
!= 0x01 && cp
->val
!= 0x02)
4122 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4123 MGMT_STATUS_INVALID_PARAMS
);
4127 if (!hdev_is_powered(hdev
)) {
4131 changed
= !test_and_set_bit(HCI_SC_ENABLED
,
4133 if (cp
->val
== 0x02)
4134 set_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4136 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4138 changed
= test_and_clear_bit(HCI_SC_ENABLED
,
4140 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4143 err
= send_settings_rsp(sk
, MGMT_OP_SET_SECURE_CONN
, hdev
);
4148 err
= new_settings(hdev
, sk
);
4153 if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN
, hdev
)) {
4154 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4161 if (val
== test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
) &&
4162 (cp
->val
== 0x02) == test_bit(HCI_SC_ONLY
, &hdev
->dev_flags
)) {
4163 err
= send_settings_rsp(sk
, MGMT_OP_SET_SECURE_CONN
, hdev
);
4167 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_SECURE_CONN
, hdev
, data
, len
);
4173 err
= hci_send_cmd(hdev
, HCI_OP_WRITE_SC_SUPPORT
, 1, &val
);
4175 mgmt_pending_remove(cmd
);
4179 if (cp
->val
== 0x02)
4180 set_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4182 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4185 hci_dev_unlock(hdev
);
4189 static int set_debug_keys(struct sock
*sk
, struct hci_dev
*hdev
,
4190 void *data
, u16 len
)
4192 struct mgmt_mode
*cp
= data
;
4196 BT_DBG("request for %s", hdev
->name
);
4198 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
4199 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEBUG_KEYS
,
4200 MGMT_STATUS_INVALID_PARAMS
);
4205 changed
= !test_and_set_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
4207 changed
= test_and_clear_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
4209 err
= send_settings_rsp(sk
, MGMT_OP_SET_DEBUG_KEYS
, hdev
);
4214 err
= new_settings(hdev
, sk
);
4217 hci_dev_unlock(hdev
);
4221 static bool irk_is_valid(struct mgmt_irk_info
*irk
)
4223 switch (irk
->addr
.type
) {
4224 case BDADDR_LE_PUBLIC
:
4227 case BDADDR_LE_RANDOM
:
4228 /* Two most significant bits shall be set */
4229 if ((irk
->addr
.bdaddr
.b
[5] & 0xc0) != 0xc0)
4237 static int load_irks(struct sock
*sk
, struct hci_dev
*hdev
, void *cp_data
,
4240 struct mgmt_cp_load_irks
*cp
= cp_data
;
4241 u16 irk_count
, expected_len
;
4244 BT_DBG("request for %s", hdev
->name
);
4246 if (!lmp_le_capable(hdev
))
4247 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_IRKS
,
4248 MGMT_STATUS_NOT_SUPPORTED
);
4250 irk_count
= __le16_to_cpu(cp
->irk_count
);
4252 expected_len
= sizeof(*cp
) + irk_count
* sizeof(struct mgmt_irk_info
);
4253 if (expected_len
!= len
) {
4254 BT_ERR("load_irks: expected %u bytes, got %u bytes",
4256 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_IRKS
,
4257 MGMT_STATUS_INVALID_PARAMS
);
4260 BT_DBG("%s irk_count %u", hdev
->name
, irk_count
);
4262 for (i
= 0; i
< irk_count
; i
++) {
4263 struct mgmt_irk_info
*key
= &cp
->irks
[i
];
4265 if (!irk_is_valid(key
))
4266 return cmd_status(sk
, hdev
->id
,
4268 MGMT_STATUS_INVALID_PARAMS
);
4273 hci_smp_irks_clear(hdev
);
4275 for (i
= 0; i
< irk_count
; i
++) {
4276 struct mgmt_irk_info
*irk
= &cp
->irks
[i
];
4279 if (irk
->addr
.type
== BDADDR_LE_PUBLIC
)
4280 addr_type
= ADDR_LE_DEV_PUBLIC
;
4282 addr_type
= ADDR_LE_DEV_RANDOM
;
4284 hci_add_irk(hdev
, &irk
->addr
.bdaddr
, addr_type
, irk
->val
,
4288 set_bit(HCI_RPA_RESOLVING
, &hdev
->dev_flags
);
4290 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_LOAD_IRKS
, 0, NULL
, 0);
4292 hci_dev_unlock(hdev
);
4297 static bool ltk_is_valid(struct mgmt_ltk_info
*key
)
4299 if (key
->master
!= 0x00 && key
->master
!= 0x01)
4302 switch (key
->addr
.type
) {
4303 case BDADDR_LE_PUBLIC
:
4306 case BDADDR_LE_RANDOM
:
4307 /* Two most significant bits shall be set */
4308 if ((key
->addr
.bdaddr
.b
[5] & 0xc0) != 0xc0)
4316 static int load_long_term_keys(struct sock
*sk
, struct hci_dev
*hdev
,
4317 void *cp_data
, u16 len
)
4319 struct mgmt_cp_load_long_term_keys
*cp
= cp_data
;
4320 u16 key_count
, expected_len
;
4323 BT_DBG("request for %s", hdev
->name
);
4325 if (!lmp_le_capable(hdev
))
4326 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LONG_TERM_KEYS
,
4327 MGMT_STATUS_NOT_SUPPORTED
);
4329 key_count
= __le16_to_cpu(cp
->key_count
);
4331 expected_len
= sizeof(*cp
) + key_count
*
4332 sizeof(struct mgmt_ltk_info
);
4333 if (expected_len
!= len
) {
4334 BT_ERR("load_keys: expected %u bytes, got %u bytes",
4336 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LONG_TERM_KEYS
,
4337 MGMT_STATUS_INVALID_PARAMS
);
4340 BT_DBG("%s key_count %u", hdev
->name
, key_count
);
4342 for (i
= 0; i
< key_count
; i
++) {
4343 struct mgmt_ltk_info
*key
= &cp
->keys
[i
];
4345 if (!ltk_is_valid(key
))
4346 return cmd_status(sk
, hdev
->id
,
4347 MGMT_OP_LOAD_LONG_TERM_KEYS
,
4348 MGMT_STATUS_INVALID_PARAMS
);
4353 hci_smp_ltks_clear(hdev
);
4355 for (i
= 0; i
< key_count
; i
++) {
4356 struct mgmt_ltk_info
*key
= &cp
->keys
[i
];
4359 if (key
->addr
.type
== BDADDR_LE_PUBLIC
)
4360 addr_type
= ADDR_LE_DEV_PUBLIC
;
4362 addr_type
= ADDR_LE_DEV_RANDOM
;
4367 type
= HCI_SMP_LTK_SLAVE
;
4369 hci_add_ltk(hdev
, &key
->addr
.bdaddr
, addr_type
, type
,
4370 key
->type
, key
->val
, key
->enc_size
, key
->ediv
,
4374 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_LOAD_LONG_TERM_KEYS
, 0,
4377 hci_dev_unlock(hdev
);
4382 static const struct mgmt_handler
{
4383 int (*func
) (struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
4387 } mgmt_handlers
[] = {
4388 { NULL
}, /* 0x0000 (no command) */
4389 { read_version
, false, MGMT_READ_VERSION_SIZE
},
4390 { read_commands
, false, MGMT_READ_COMMANDS_SIZE
},
4391 { read_index_list
, false, MGMT_READ_INDEX_LIST_SIZE
},
4392 { read_controller_info
, false, MGMT_READ_INFO_SIZE
},
4393 { set_powered
, false, MGMT_SETTING_SIZE
},
4394 { set_discoverable
, false, MGMT_SET_DISCOVERABLE_SIZE
},
4395 { set_connectable
, false, MGMT_SETTING_SIZE
},
4396 { set_fast_connectable
, false, MGMT_SETTING_SIZE
},
4397 { set_pairable
, false, MGMT_SETTING_SIZE
},
4398 { set_link_security
, false, MGMT_SETTING_SIZE
},
4399 { set_ssp
, false, MGMT_SETTING_SIZE
},
4400 { set_hs
, false, MGMT_SETTING_SIZE
},
4401 { set_le
, false, MGMT_SETTING_SIZE
},
4402 { set_dev_class
, false, MGMT_SET_DEV_CLASS_SIZE
},
4403 { set_local_name
, false, MGMT_SET_LOCAL_NAME_SIZE
},
4404 { add_uuid
, false, MGMT_ADD_UUID_SIZE
},
4405 { remove_uuid
, false, MGMT_REMOVE_UUID_SIZE
},
4406 { load_link_keys
, true, MGMT_LOAD_LINK_KEYS_SIZE
},
4407 { load_long_term_keys
, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE
},
4408 { disconnect
, false, MGMT_DISCONNECT_SIZE
},
4409 { get_connections
, false, MGMT_GET_CONNECTIONS_SIZE
},
4410 { pin_code_reply
, false, MGMT_PIN_CODE_REPLY_SIZE
},
4411 { pin_code_neg_reply
, false, MGMT_PIN_CODE_NEG_REPLY_SIZE
},
4412 { set_io_capability
, false, MGMT_SET_IO_CAPABILITY_SIZE
},
4413 { pair_device
, false, MGMT_PAIR_DEVICE_SIZE
},
4414 { cancel_pair_device
, false, MGMT_CANCEL_PAIR_DEVICE_SIZE
},
4415 { unpair_device
, false, MGMT_UNPAIR_DEVICE_SIZE
},
4416 { user_confirm_reply
, false, MGMT_USER_CONFIRM_REPLY_SIZE
},
4417 { user_confirm_neg_reply
, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE
},
4418 { user_passkey_reply
, false, MGMT_USER_PASSKEY_REPLY_SIZE
},
4419 { user_passkey_neg_reply
, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE
},
4420 { read_local_oob_data
, false, MGMT_READ_LOCAL_OOB_DATA_SIZE
},
4421 { add_remote_oob_data
, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE
},
4422 { remove_remote_oob_data
, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE
},
4423 { start_discovery
, false, MGMT_START_DISCOVERY_SIZE
},
4424 { stop_discovery
, false, MGMT_STOP_DISCOVERY_SIZE
},
4425 { confirm_name
, false, MGMT_CONFIRM_NAME_SIZE
},
4426 { block_device
, false, MGMT_BLOCK_DEVICE_SIZE
},
4427 { unblock_device
, false, MGMT_UNBLOCK_DEVICE_SIZE
},
4428 { set_device_id
, false, MGMT_SET_DEVICE_ID_SIZE
},
4429 { set_advertising
, false, MGMT_SETTING_SIZE
},
4430 { set_bredr
, false, MGMT_SETTING_SIZE
},
4431 { set_static_address
, false, MGMT_SET_STATIC_ADDRESS_SIZE
},
4432 { set_scan_params
, false, MGMT_SET_SCAN_PARAMS_SIZE
},
4433 { set_secure_conn
, false, MGMT_SETTING_SIZE
},
4434 { set_debug_keys
, false, MGMT_SETTING_SIZE
},
4436 { load_irks
, true, MGMT_LOAD_IRKS_SIZE
},
4440 int mgmt_control(struct sock
*sk
, struct msghdr
*msg
, size_t msglen
)
4444 struct mgmt_hdr
*hdr
;
4445 u16 opcode
, index
, len
;
4446 struct hci_dev
*hdev
= NULL
;
4447 const struct mgmt_handler
*handler
;
4450 BT_DBG("got %zu bytes", msglen
);
4452 if (msglen
< sizeof(*hdr
))
4455 buf
= kmalloc(msglen
, GFP_KERNEL
);
4459 if (memcpy_fromiovec(buf
, msg
->msg_iov
, msglen
)) {
4465 opcode
= __le16_to_cpu(hdr
->opcode
);
4466 index
= __le16_to_cpu(hdr
->index
);
4467 len
= __le16_to_cpu(hdr
->len
);
4469 if (len
!= msglen
- sizeof(*hdr
)) {
4474 if (index
!= MGMT_INDEX_NONE
) {
4475 hdev
= hci_dev_get(index
);
4477 err
= cmd_status(sk
, index
, opcode
,
4478 MGMT_STATUS_INVALID_INDEX
);
4482 if (test_bit(HCI_SETUP
, &hdev
->dev_flags
) ||
4483 test_bit(HCI_USER_CHANNEL
, &hdev
->dev_flags
)) {
4484 err
= cmd_status(sk
, index
, opcode
,
4485 MGMT_STATUS_INVALID_INDEX
);
4490 if (opcode
>= ARRAY_SIZE(mgmt_handlers
) ||
4491 mgmt_handlers
[opcode
].func
== NULL
) {
4492 BT_DBG("Unknown op %u", opcode
);
4493 err
= cmd_status(sk
, index
, opcode
,
4494 MGMT_STATUS_UNKNOWN_COMMAND
);
4498 if ((hdev
&& opcode
< MGMT_OP_READ_INFO
) ||
4499 (!hdev
&& opcode
>= MGMT_OP_READ_INFO
)) {
4500 err
= cmd_status(sk
, index
, opcode
,
4501 MGMT_STATUS_INVALID_INDEX
);
4505 handler
= &mgmt_handlers
[opcode
];
4507 if ((handler
->var_len
&& len
< handler
->data_len
) ||
4508 (!handler
->var_len
&& len
!= handler
->data_len
)) {
4509 err
= cmd_status(sk
, index
, opcode
,
4510 MGMT_STATUS_INVALID_PARAMS
);
4515 mgmt_init_hdev(sk
, hdev
);
4517 cp
= buf
+ sizeof(*hdr
);
4519 err
= handler
->func(sk
, hdev
, cp
, len
);
4533 void mgmt_index_added(struct hci_dev
*hdev
)
4535 if (hdev
->dev_type
!= HCI_BREDR
)
4538 mgmt_event(MGMT_EV_INDEX_ADDED
, hdev
, NULL
, 0, NULL
);
4541 void mgmt_index_removed(struct hci_dev
*hdev
)
4543 u8 status
= MGMT_STATUS_INVALID_INDEX
;
4545 if (hdev
->dev_type
!= HCI_BREDR
)
4548 mgmt_pending_foreach(0, hdev
, cmd_status_rsp
, &status
);
4550 mgmt_event(MGMT_EV_INDEX_REMOVED
, hdev
, NULL
, 0, NULL
);
4553 static void powered_complete(struct hci_dev
*hdev
, u8 status
)
4555 struct cmd_lookup match
= { NULL
, hdev
};
4557 BT_DBG("status 0x%02x", status
);
4561 mgmt_pending_foreach(MGMT_OP_SET_POWERED
, hdev
, settings_rsp
, &match
);
4563 new_settings(hdev
, match
.sk
);
4565 hci_dev_unlock(hdev
);
4571 static int powered_update_hci(struct hci_dev
*hdev
)
4573 struct hci_request req
;
4576 hci_req_init(&req
, hdev
);
4578 if (test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
) &&
4579 !lmp_host_ssp_capable(hdev
)) {
4582 hci_req_add(&req
, HCI_OP_WRITE_SSP_MODE
, 1, &ssp
);
4585 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
) &&
4586 lmp_bredr_capable(hdev
)) {
4587 struct hci_cp_write_le_host_supported cp
;
4590 cp
.simul
= lmp_le_br_capable(hdev
);
4592 /* Check first if we already have the right
4593 * host state (host features set)
4595 if (cp
.le
!= lmp_host_le_capable(hdev
) ||
4596 cp
.simul
!= lmp_host_le_br_capable(hdev
))
4597 hci_req_add(&req
, HCI_OP_WRITE_LE_HOST_SUPPORTED
,
4601 if (lmp_le_capable(hdev
)) {
4602 /* Set random address to static address if configured */
4603 if (bacmp(&hdev
->static_addr
, BDADDR_ANY
))
4604 hci_req_add(&req
, HCI_OP_LE_SET_RANDOM_ADDR
, 6,
4605 &hdev
->static_addr
);
4607 /* Make sure the controller has a good default for
4608 * advertising data. This also applies to the case
4609 * where BR/EDR was toggled during the AUTO_OFF phase.
4611 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
)) {
4612 update_adv_data(&req
);
4613 update_scan_rsp_data(&req
);
4616 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
4617 enable_advertising(&req
);
4620 link_sec
= test_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
);
4621 if (link_sec
!= test_bit(HCI_AUTH
, &hdev
->flags
))
4622 hci_req_add(&req
, HCI_OP_WRITE_AUTH_ENABLE
,
4623 sizeof(link_sec
), &link_sec
);
4625 if (lmp_bredr_capable(hdev
)) {
4626 if (test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
4627 set_bredr_scan(&req
);
4633 return hci_req_run(&req
, powered_complete
);
4636 int mgmt_powered(struct hci_dev
*hdev
, u8 powered
)
4638 struct cmd_lookup match
= { NULL
, hdev
};
4639 u8 status_not_powered
= MGMT_STATUS_NOT_POWERED
;
4640 u8 zero_cod
[] = { 0, 0, 0 };
4643 if (!test_bit(HCI_MGMT
, &hdev
->dev_flags
))
4647 if (powered_update_hci(hdev
) == 0)
4650 mgmt_pending_foreach(MGMT_OP_SET_POWERED
, hdev
, settings_rsp
,
4655 mgmt_pending_foreach(MGMT_OP_SET_POWERED
, hdev
, settings_rsp
, &match
);
4656 mgmt_pending_foreach(0, hdev
, cmd_status_rsp
, &status_not_powered
);
4658 if (memcmp(hdev
->dev_class
, zero_cod
, sizeof(zero_cod
)) != 0)
4659 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED
, hdev
,
4660 zero_cod
, sizeof(zero_cod
), NULL
);
4663 err
= new_settings(hdev
, match
.sk
);
4671 void mgmt_set_powered_failed(struct hci_dev
*hdev
, int err
)
4673 struct pending_cmd
*cmd
;
4676 cmd
= mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
);
4680 if (err
== -ERFKILL
)
4681 status
= MGMT_STATUS_RFKILLED
;
4683 status
= MGMT_STATUS_FAILED
;
4685 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_SET_POWERED
, status
);
4687 mgmt_pending_remove(cmd
);
4690 void mgmt_discoverable_timeout(struct hci_dev
*hdev
)
4692 struct hci_request req
;
4696 /* When discoverable timeout triggers, then just make sure
4697 * the limited discoverable flag is cleared. Even in the case
4698 * of a timeout triggered from general discoverable, it is
4699 * safe to unconditionally clear the flag.
4701 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
4702 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4704 hci_req_init(&req
, hdev
);
4705 if (test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
4706 u8 scan
= SCAN_PAGE
;
4707 hci_req_add(&req
, HCI_OP_WRITE_SCAN_ENABLE
,
4708 sizeof(scan
), &scan
);
4711 update_adv_data(&req
);
4712 hci_req_run(&req
, NULL
);
4714 hdev
->discov_timeout
= 0;
4716 new_settings(hdev
, NULL
);
4718 hci_dev_unlock(hdev
);
4721 void mgmt_discoverable(struct hci_dev
*hdev
, u8 discoverable
)
4725 /* Nothing needed here if there's a pending command since that
4726 * commands request completion callback takes care of everything
4729 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
))
4733 changed
= !test_and_set_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4735 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
4736 changed
= test_and_clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4740 struct hci_request req
;
4742 /* In case this change in discoverable was triggered by
4743 * a disabling of connectable there could be a need to
4744 * update the advertising flags.
4746 hci_req_init(&req
, hdev
);
4747 update_adv_data(&req
);
4748 hci_req_run(&req
, NULL
);
4750 new_settings(hdev
, NULL
);
4754 void mgmt_connectable(struct hci_dev
*hdev
, u8 connectable
)
4758 /* Nothing needed here if there's a pending command since that
4759 * commands request completion callback takes care of everything
4762 if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
))
4766 changed
= !test_and_set_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
4768 changed
= test_and_clear_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
4771 new_settings(hdev
, NULL
);
4774 void mgmt_write_scan_failed(struct hci_dev
*hdev
, u8 scan
, u8 status
)
4776 u8 mgmt_err
= mgmt_status(status
);
4778 if (scan
& SCAN_PAGE
)
4779 mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE
, hdev
,
4780 cmd_status_rsp
, &mgmt_err
);
4782 if (scan
& SCAN_INQUIRY
)
4783 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE
, hdev
,
4784 cmd_status_rsp
, &mgmt_err
);
4787 void mgmt_new_link_key(struct hci_dev
*hdev
, struct link_key
*key
,
4790 struct mgmt_ev_new_link_key ev
;
4792 memset(&ev
, 0, sizeof(ev
));
4794 ev
.store_hint
= persistent
;
4795 bacpy(&ev
.key
.addr
.bdaddr
, &key
->bdaddr
);
4796 ev
.key
.addr
.type
= BDADDR_BREDR
;
4797 ev
.key
.type
= key
->type
;
4798 memcpy(ev
.key
.val
, key
->val
, HCI_LINK_KEY_SIZE
);
4799 ev
.key
.pin_len
= key
->pin_len
;
4801 mgmt_event(MGMT_EV_NEW_LINK_KEY
, hdev
, &ev
, sizeof(ev
), NULL
);
4804 void mgmt_new_ltk(struct hci_dev
*hdev
, struct smp_ltk
*key
)
4806 struct mgmt_ev_new_long_term_key ev
;
4808 memset(&ev
, 0, sizeof(ev
));
4810 /* Devices using resolvable or non-resolvable random addresses
4811 * without providing an indentity resolving key don't require
4812 * to store long term keys. Their addresses will change the
4815 * Only when a remote device provides an identity address
4816 * make sure the long term key is stored. If the remote
4817 * identity is known, the long term keys are internally
4818 * mapped to the identity address. So allow static random
4819 * and public addresses here.
4821 if (key
->bdaddr_type
== ADDR_LE_DEV_RANDOM
&&
4822 (key
->bdaddr
.b
[5] & 0xc0) != 0xc0)
4823 ev
.store_hint
= 0x00;
4825 ev
.store_hint
= 0x01;
4827 bacpy(&ev
.key
.addr
.bdaddr
, &key
->bdaddr
);
4828 ev
.key
.addr
.type
= link_to_bdaddr(LE_LINK
, key
->bdaddr_type
);
4829 ev
.key
.type
= key
->authenticated
;
4830 ev
.key
.enc_size
= key
->enc_size
;
4831 ev
.key
.ediv
= key
->ediv
;
4833 if (key
->type
== HCI_SMP_LTK
)
4836 memcpy(ev
.key
.rand
, key
->rand
, sizeof(key
->rand
));
4837 memcpy(ev
.key
.val
, key
->val
, sizeof(key
->val
));
4839 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY
, hdev
, &ev
, sizeof(ev
), NULL
);
4842 void mgmt_new_irk(struct hci_dev
*hdev
, struct smp_irk
*irk
)
4844 struct mgmt_ev_new_irk ev
;
4846 memset(&ev
, 0, sizeof(ev
));
4848 /* For identity resolving keys from devices that are already
4849 * using a public address or static random address, do not
4850 * ask for storing this key. The identity resolving key really
4851 * is only mandatory for devices using resovlable random
4854 * Storing all identity resolving keys has the downside that
4855 * they will be also loaded on next boot of they system. More
4856 * identity resolving keys, means more time during scanning is
4857 * needed to actually resolve these addresses.
4859 if (bacmp(&irk
->rpa
, BDADDR_ANY
))
4860 ev
.store_hint
= 0x01;
4862 ev
.store_hint
= 0x00;
4864 bacpy(&ev
.rpa
, &irk
->rpa
);
4865 bacpy(&ev
.irk
.addr
.bdaddr
, &irk
->bdaddr
);
4866 ev
.irk
.addr
.type
= link_to_bdaddr(LE_LINK
, irk
->addr_type
);
4867 memcpy(ev
.irk
.val
, irk
->val
, sizeof(irk
->val
));
4869 mgmt_event(MGMT_EV_NEW_IRK
, hdev
, &ev
, sizeof(ev
), NULL
);
4872 static inline u16
eir_append_data(u8
*eir
, u16 eir_len
, u8 type
, u8
*data
,
4875 eir
[eir_len
++] = sizeof(type
) + data_len
;
4876 eir
[eir_len
++] = type
;
4877 memcpy(&eir
[eir_len
], data
, data_len
);
4878 eir_len
+= data_len
;
4883 void mgmt_device_connected(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
4884 u8 addr_type
, u32 flags
, u8
*name
, u8 name_len
,
4888 struct mgmt_ev_device_connected
*ev
= (void *) buf
;
4891 bacpy(&ev
->addr
.bdaddr
, bdaddr
);
4892 ev
->addr
.type
= link_to_bdaddr(link_type
, addr_type
);
4894 ev
->flags
= __cpu_to_le32(flags
);
4897 eir_len
= eir_append_data(ev
->eir
, 0, EIR_NAME_COMPLETE
,
4900 if (dev_class
&& memcmp(dev_class
, "\0\0\0", 3) != 0)
4901 eir_len
= eir_append_data(ev
->eir
, eir_len
,
4902 EIR_CLASS_OF_DEV
, dev_class
, 3);
4904 ev
->eir_len
= cpu_to_le16(eir_len
);
4906 mgmt_event(MGMT_EV_DEVICE_CONNECTED
, hdev
, buf
,
4907 sizeof(*ev
) + eir_len
, NULL
);
4910 static void disconnect_rsp(struct pending_cmd
*cmd
, void *data
)
4912 struct mgmt_cp_disconnect
*cp
= cmd
->param
;
4913 struct sock
**sk
= data
;
4914 struct mgmt_rp_disconnect rp
;
4916 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
4917 rp
.addr
.type
= cp
->addr
.type
;
4919 cmd_complete(cmd
->sk
, cmd
->index
, MGMT_OP_DISCONNECT
, 0, &rp
,
4925 mgmt_pending_remove(cmd
);
4928 static void unpair_device_rsp(struct pending_cmd
*cmd
, void *data
)
4930 struct hci_dev
*hdev
= data
;
4931 struct mgmt_cp_unpair_device
*cp
= cmd
->param
;
4932 struct mgmt_rp_unpair_device rp
;
4934 memset(&rp
, 0, sizeof(rp
));
4935 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
4936 rp
.addr
.type
= cp
->addr
.type
;
4938 device_unpaired(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
, cmd
->sk
);
4940 cmd_complete(cmd
->sk
, cmd
->index
, cmd
->opcode
, 0, &rp
, sizeof(rp
));
4942 mgmt_pending_remove(cmd
);
4945 void mgmt_device_disconnected(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
4946 u8 link_type
, u8 addr_type
, u8 reason
)
4948 struct mgmt_ev_device_disconnected ev
;
4949 struct sock
*sk
= NULL
;
4951 if (link_type
!= ACL_LINK
&& link_type
!= LE_LINK
)
4954 mgmt_pending_foreach(MGMT_OP_DISCONNECT
, hdev
, disconnect_rsp
, &sk
);
4956 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
4957 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
4960 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED
, hdev
, &ev
, sizeof(ev
), sk
);
4965 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE
, hdev
, unpair_device_rsp
,
4969 void mgmt_disconnect_failed(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
4970 u8 link_type
, u8 addr_type
, u8 status
)
4972 u8 bdaddr_type
= link_to_bdaddr(link_type
, addr_type
);
4973 struct mgmt_cp_disconnect
*cp
;
4974 struct mgmt_rp_disconnect rp
;
4975 struct pending_cmd
*cmd
;
4977 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE
, hdev
, unpair_device_rsp
,
4980 cmd
= mgmt_pending_find(MGMT_OP_DISCONNECT
, hdev
);
4986 if (bacmp(bdaddr
, &cp
->addr
.bdaddr
))
4989 if (cp
->addr
.type
!= bdaddr_type
)
4992 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
4993 rp
.addr
.type
= bdaddr_type
;
4995 cmd_complete(cmd
->sk
, cmd
->index
, MGMT_OP_DISCONNECT
,
4996 mgmt_status(status
), &rp
, sizeof(rp
));
4998 mgmt_pending_remove(cmd
);
5001 void mgmt_connect_failed(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5002 u8 addr_type
, u8 status
)
5004 struct mgmt_ev_connect_failed ev
;
5006 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5007 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5008 ev
.status
= mgmt_status(status
);
5010 mgmt_event(MGMT_EV_CONNECT_FAILED
, hdev
, &ev
, sizeof(ev
), NULL
);
5013 void mgmt_pin_code_request(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 secure
)
5015 struct mgmt_ev_pin_code_request ev
;
5017 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5018 ev
.addr
.type
= BDADDR_BREDR
;
5021 mgmt_event(MGMT_EV_PIN_CODE_REQUEST
, hdev
, &ev
, sizeof(ev
), NULL
);
5024 void mgmt_pin_code_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5027 struct pending_cmd
*cmd
;
5028 struct mgmt_rp_pin_code_reply rp
;
5030 cmd
= mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY
, hdev
);
5034 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5035 rp
.addr
.type
= BDADDR_BREDR
;
5037 cmd_complete(cmd
->sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
5038 mgmt_status(status
), &rp
, sizeof(rp
));
5040 mgmt_pending_remove(cmd
);
5043 void mgmt_pin_code_neg_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5046 struct pending_cmd
*cmd
;
5047 struct mgmt_rp_pin_code_reply rp
;
5049 cmd
= mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY
, hdev
);
5053 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5054 rp
.addr
.type
= BDADDR_BREDR
;
5056 cmd_complete(cmd
->sk
, hdev
->id
, MGMT_OP_PIN_CODE_NEG_REPLY
,
5057 mgmt_status(status
), &rp
, sizeof(rp
));
5059 mgmt_pending_remove(cmd
);
5062 int mgmt_user_confirm_request(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5063 u8 link_type
, u8 addr_type
, __le32 value
,
5066 struct mgmt_ev_user_confirm_request ev
;
5068 BT_DBG("%s", hdev
->name
);
5070 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5071 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5072 ev
.confirm_hint
= confirm_hint
;
5075 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST
, hdev
, &ev
, sizeof(ev
),
5079 int mgmt_user_passkey_request(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5080 u8 link_type
, u8 addr_type
)
5082 struct mgmt_ev_user_passkey_request ev
;
5084 BT_DBG("%s", hdev
->name
);
5086 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5087 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5089 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST
, hdev
, &ev
, sizeof(ev
),
5093 static int user_pairing_resp_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5094 u8 link_type
, u8 addr_type
, u8 status
,
5097 struct pending_cmd
*cmd
;
5098 struct mgmt_rp_user_confirm_reply rp
;
5101 cmd
= mgmt_pending_find(opcode
, hdev
);
5105 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5106 rp
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5107 err
= cmd_complete(cmd
->sk
, hdev
->id
, opcode
, mgmt_status(status
),
5110 mgmt_pending_remove(cmd
);
5115 int mgmt_user_confirm_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5116 u8 link_type
, u8 addr_type
, u8 status
)
5118 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5119 status
, MGMT_OP_USER_CONFIRM_REPLY
);
5122 int mgmt_user_confirm_neg_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5123 u8 link_type
, u8 addr_type
, u8 status
)
5125 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5127 MGMT_OP_USER_CONFIRM_NEG_REPLY
);
5130 int mgmt_user_passkey_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5131 u8 link_type
, u8 addr_type
, u8 status
)
5133 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5134 status
, MGMT_OP_USER_PASSKEY_REPLY
);
5137 int mgmt_user_passkey_neg_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5138 u8 link_type
, u8 addr_type
, u8 status
)
5140 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5142 MGMT_OP_USER_PASSKEY_NEG_REPLY
);
5145 int mgmt_user_passkey_notify(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5146 u8 link_type
, u8 addr_type
, u32 passkey
,
5149 struct mgmt_ev_passkey_notify ev
;
5151 BT_DBG("%s", hdev
->name
);
5153 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5154 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5155 ev
.passkey
= __cpu_to_le32(passkey
);
5156 ev
.entered
= entered
;
5158 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY
, hdev
, &ev
, sizeof(ev
), NULL
);
5161 void mgmt_auth_failed(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5162 u8 addr_type
, u8 status
)
5164 struct mgmt_ev_auth_failed ev
;
5166 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5167 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5168 ev
.status
= mgmt_status(status
);
5170 mgmt_event(MGMT_EV_AUTH_FAILED
, hdev
, &ev
, sizeof(ev
), NULL
);
5173 void mgmt_auth_enable_complete(struct hci_dev
*hdev
, u8 status
)
5175 struct cmd_lookup match
= { NULL
, hdev
};
5179 u8 mgmt_err
= mgmt_status(status
);
5180 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY
, hdev
,
5181 cmd_status_rsp
, &mgmt_err
);
5185 if (test_bit(HCI_AUTH
, &hdev
->flags
))
5186 changed
= !test_and_set_bit(HCI_LINK_SECURITY
,
5189 changed
= test_and_clear_bit(HCI_LINK_SECURITY
,
5192 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY
, hdev
, settings_rsp
,
5196 new_settings(hdev
, match
.sk
);
5202 static void clear_eir(struct hci_request
*req
)
5204 struct hci_dev
*hdev
= req
->hdev
;
5205 struct hci_cp_write_eir cp
;
5207 if (!lmp_ext_inq_capable(hdev
))
5210 memset(hdev
->eir
, 0, sizeof(hdev
->eir
));
5212 memset(&cp
, 0, sizeof(cp
));
5214 hci_req_add(req
, HCI_OP_WRITE_EIR
, sizeof(cp
), &cp
);
5217 void mgmt_ssp_enable_complete(struct hci_dev
*hdev
, u8 enable
, u8 status
)
5219 struct cmd_lookup match
= { NULL
, hdev
};
5220 struct hci_request req
;
5221 bool changed
= false;
5224 u8 mgmt_err
= mgmt_status(status
);
5226 if (enable
&& test_and_clear_bit(HCI_SSP_ENABLED
,
5227 &hdev
->dev_flags
)) {
5228 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
5229 new_settings(hdev
, NULL
);
5232 mgmt_pending_foreach(MGMT_OP_SET_SSP
, hdev
, cmd_status_rsp
,
5238 changed
= !test_and_set_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
);
5240 changed
= test_and_clear_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
);
5242 changed
= test_and_clear_bit(HCI_HS_ENABLED
,
5245 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
5248 mgmt_pending_foreach(MGMT_OP_SET_SSP
, hdev
, settings_rsp
, &match
);
5251 new_settings(hdev
, match
.sk
);
5256 hci_req_init(&req
, hdev
);
5258 if (test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
))
5263 hci_req_run(&req
, NULL
);
5266 void mgmt_sc_enable_complete(struct hci_dev
*hdev
, u8 enable
, u8 status
)
5268 struct cmd_lookup match
= { NULL
, hdev
};
5269 bool changed
= false;
5272 u8 mgmt_err
= mgmt_status(status
);
5275 if (test_and_clear_bit(HCI_SC_ENABLED
,
5277 new_settings(hdev
, NULL
);
5278 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
5281 mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN
, hdev
,
5282 cmd_status_rsp
, &mgmt_err
);
5287 changed
= !test_and_set_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
);
5289 changed
= test_and_clear_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
);
5290 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
5293 mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN
, hdev
,
5294 settings_rsp
, &match
);
5297 new_settings(hdev
, match
.sk
);
5303 static void sk_lookup(struct pending_cmd
*cmd
, void *data
)
5305 struct cmd_lookup
*match
= data
;
5307 if (match
->sk
== NULL
) {
5308 match
->sk
= cmd
->sk
;
5309 sock_hold(match
->sk
);
5313 void mgmt_set_class_of_dev_complete(struct hci_dev
*hdev
, u8
*dev_class
,
5316 struct cmd_lookup match
= { NULL
, hdev
, mgmt_status(status
) };
5318 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS
, hdev
, sk_lookup
, &match
);
5319 mgmt_pending_foreach(MGMT_OP_ADD_UUID
, hdev
, sk_lookup
, &match
);
5320 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID
, hdev
, sk_lookup
, &match
);
5323 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED
, hdev
, dev_class
, 3,
5330 void mgmt_set_local_name_complete(struct hci_dev
*hdev
, u8
*name
, u8 status
)
5332 struct mgmt_cp_set_local_name ev
;
5333 struct pending_cmd
*cmd
;
5338 memset(&ev
, 0, sizeof(ev
));
5339 memcpy(ev
.name
, name
, HCI_MAX_NAME_LENGTH
);
5340 memcpy(ev
.short_name
, hdev
->short_name
, HCI_MAX_SHORT_NAME_LENGTH
);
5342 cmd
= mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME
, hdev
);
5344 memcpy(hdev
->dev_name
, name
, sizeof(hdev
->dev_name
));
5346 /* If this is a HCI command related to powering on the
5347 * HCI dev don't send any mgmt signals.
5349 if (mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
))
5353 mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED
, hdev
, &ev
, sizeof(ev
),
5354 cmd
? cmd
->sk
: NULL
);
5357 void mgmt_read_local_oob_data_complete(struct hci_dev
*hdev
, u8
*hash192
,
5358 u8
*randomizer192
, u8
*hash256
,
5359 u8
*randomizer256
, u8 status
)
5361 struct pending_cmd
*cmd
;
5363 BT_DBG("%s status %u", hdev
->name
, status
);
5365 cmd
= mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA
, hdev
);
5370 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
5371 mgmt_status(status
));
5373 if (test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
) &&
5374 hash256
&& randomizer256
) {
5375 struct mgmt_rp_read_local_oob_ext_data rp
;
5377 memcpy(rp
.hash192
, hash192
, sizeof(rp
.hash192
));
5378 memcpy(rp
.randomizer192
, randomizer192
,
5379 sizeof(rp
.randomizer192
));
5381 memcpy(rp
.hash256
, hash256
, sizeof(rp
.hash256
));
5382 memcpy(rp
.randomizer256
, randomizer256
,
5383 sizeof(rp
.randomizer256
));
5385 cmd_complete(cmd
->sk
, hdev
->id
,
5386 MGMT_OP_READ_LOCAL_OOB_DATA
, 0,
5389 struct mgmt_rp_read_local_oob_data rp
;
5391 memcpy(rp
.hash
, hash192
, sizeof(rp
.hash
));
5392 memcpy(rp
.randomizer
, randomizer192
,
5393 sizeof(rp
.randomizer
));
5395 cmd_complete(cmd
->sk
, hdev
->id
,
5396 MGMT_OP_READ_LOCAL_OOB_DATA
, 0,
5401 mgmt_pending_remove(cmd
);
5404 void mgmt_device_found(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5405 u8 addr_type
, u8
*dev_class
, s8 rssi
, u8 cfm_name
, u8
5406 ssp
, u8
*eir
, u16 eir_len
)
5409 struct mgmt_ev_device_found
*ev
= (void *) buf
;
5410 struct smp_irk
*irk
;
5413 if (!hci_discovery_active(hdev
))
5416 /* Leave 5 bytes for a potential CoD field */
5417 if (sizeof(*ev
) + eir_len
+ 5 > sizeof(buf
))
5420 memset(buf
, 0, sizeof(buf
));
5422 irk
= hci_get_irk(hdev
, bdaddr
, addr_type
);
5424 bacpy(&ev
->addr
.bdaddr
, &irk
->bdaddr
);
5425 ev
->addr
.type
= link_to_bdaddr(link_type
, irk
->addr_type
);
5427 bacpy(&ev
->addr
.bdaddr
, bdaddr
);
5428 ev
->addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5433 ev
->flags
|= __constant_cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME
);
5435 ev
->flags
|= __constant_cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING
);
5438 memcpy(ev
->eir
, eir
, eir_len
);
5440 if (dev_class
&& !eir_has_data_type(ev
->eir
, eir_len
, EIR_CLASS_OF_DEV
))
5441 eir_len
= eir_append_data(ev
->eir
, eir_len
, EIR_CLASS_OF_DEV
,
5444 ev
->eir_len
= cpu_to_le16(eir_len
);
5445 ev_size
= sizeof(*ev
) + eir_len
;
5447 mgmt_event(MGMT_EV_DEVICE_FOUND
, hdev
, ev
, ev_size
, NULL
);
5450 void mgmt_remote_name(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5451 u8 addr_type
, s8 rssi
, u8
*name
, u8 name_len
)
5453 struct mgmt_ev_device_found
*ev
;
5454 char buf
[sizeof(*ev
) + HCI_MAX_NAME_LENGTH
+ 2];
5457 ev
= (struct mgmt_ev_device_found
*) buf
;
5459 memset(buf
, 0, sizeof(buf
));
5461 bacpy(&ev
->addr
.bdaddr
, bdaddr
);
5462 ev
->addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5465 eir_len
= eir_append_data(ev
->eir
, 0, EIR_NAME_COMPLETE
, name
,
5468 ev
->eir_len
= cpu_to_le16(eir_len
);
5470 mgmt_event(MGMT_EV_DEVICE_FOUND
, hdev
, ev
, sizeof(*ev
) + eir_len
, NULL
);
5473 void mgmt_discovering(struct hci_dev
*hdev
, u8 discovering
)
5475 struct mgmt_ev_discovering ev
;
5476 struct pending_cmd
*cmd
;
5478 BT_DBG("%s discovering %u", hdev
->name
, discovering
);
5481 cmd
= mgmt_pending_find(MGMT_OP_START_DISCOVERY
, hdev
);
5483 cmd
= mgmt_pending_find(MGMT_OP_STOP_DISCOVERY
, hdev
);
5486 u8 type
= hdev
->discovery
.type
;
5488 cmd_complete(cmd
->sk
, hdev
->id
, cmd
->opcode
, 0, &type
,
5490 mgmt_pending_remove(cmd
);
5493 memset(&ev
, 0, sizeof(ev
));
5494 ev
.type
= hdev
->discovery
.type
;
5495 ev
.discovering
= discovering
;
5497 mgmt_event(MGMT_EV_DISCOVERING
, hdev
, &ev
, sizeof(ev
), NULL
);
5500 int mgmt_device_blocked(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 type
)
5502 struct pending_cmd
*cmd
;
5503 struct mgmt_ev_device_blocked ev
;
5505 cmd
= mgmt_pending_find(MGMT_OP_BLOCK_DEVICE
, hdev
);
5507 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5508 ev
.addr
.type
= type
;
5510 return mgmt_event(MGMT_EV_DEVICE_BLOCKED
, hdev
, &ev
, sizeof(ev
),
5511 cmd
? cmd
->sk
: NULL
);
5514 int mgmt_device_unblocked(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 type
)
5516 struct pending_cmd
*cmd
;
5517 struct mgmt_ev_device_unblocked ev
;
5519 cmd
= mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE
, hdev
);
5521 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5522 ev
.addr
.type
= type
;
5524 return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED
, hdev
, &ev
, sizeof(ev
),
5525 cmd
? cmd
->sk
: NULL
);
5528 static void adv_enable_complete(struct hci_dev
*hdev
, u8 status
)
5530 BT_DBG("%s status %u", hdev
->name
, status
);
5532 /* Clear the advertising mgmt setting if we failed to re-enable it */
5534 clear_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
5535 new_settings(hdev
, NULL
);
5539 void mgmt_reenable_advertising(struct hci_dev
*hdev
)
5541 struct hci_request req
;
5543 if (hci_conn_num(hdev
, LE_LINK
) > 0)
5546 if (!test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
5549 hci_req_init(&req
, hdev
);
5550 enable_advertising(&req
);
5552 /* If this fails we have no option but to let user space know
5553 * that we've disabled advertising.
5555 if (hci_req_run(&req
, adv_enable_complete
) < 0) {
5556 clear_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
5557 new_settings(hdev
, NULL
);