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
,
88 static const u16 mgmt_events
[] = {
89 MGMT_EV_CONTROLLER_ERROR
,
91 MGMT_EV_INDEX_REMOVED
,
93 MGMT_EV_CLASS_OF_DEV_CHANGED
,
94 MGMT_EV_LOCAL_NAME_CHANGED
,
96 MGMT_EV_NEW_LONG_TERM_KEY
,
97 MGMT_EV_DEVICE_CONNECTED
,
98 MGMT_EV_DEVICE_DISCONNECTED
,
99 MGMT_EV_CONNECT_FAILED
,
100 MGMT_EV_PIN_CODE_REQUEST
,
101 MGMT_EV_USER_CONFIRM_REQUEST
,
102 MGMT_EV_USER_PASSKEY_REQUEST
,
104 MGMT_EV_DEVICE_FOUND
,
106 MGMT_EV_DEVICE_BLOCKED
,
107 MGMT_EV_DEVICE_UNBLOCKED
,
108 MGMT_EV_DEVICE_UNPAIRED
,
109 MGMT_EV_PASSKEY_NOTIFY
,
113 #define CACHE_TIMEOUT msecs_to_jiffies(2 * 1000)
115 #define hdev_is_powered(hdev) (test_bit(HCI_UP, &hdev->flags) && \
116 !test_bit(HCI_AUTO_OFF, &hdev->dev_flags))
119 struct list_head list
;
127 /* HCI to MGMT error code conversion table */
128 static u8 mgmt_status_table
[] = {
130 MGMT_STATUS_UNKNOWN_COMMAND
, /* Unknown Command */
131 MGMT_STATUS_NOT_CONNECTED
, /* No Connection */
132 MGMT_STATUS_FAILED
, /* Hardware Failure */
133 MGMT_STATUS_CONNECT_FAILED
, /* Page Timeout */
134 MGMT_STATUS_AUTH_FAILED
, /* Authentication Failed */
135 MGMT_STATUS_AUTH_FAILED
, /* PIN or Key Missing */
136 MGMT_STATUS_NO_RESOURCES
, /* Memory Full */
137 MGMT_STATUS_TIMEOUT
, /* Connection Timeout */
138 MGMT_STATUS_NO_RESOURCES
, /* Max Number of Connections */
139 MGMT_STATUS_NO_RESOURCES
, /* Max Number of SCO Connections */
140 MGMT_STATUS_ALREADY_CONNECTED
, /* ACL Connection Exists */
141 MGMT_STATUS_BUSY
, /* Command Disallowed */
142 MGMT_STATUS_NO_RESOURCES
, /* Rejected Limited Resources */
143 MGMT_STATUS_REJECTED
, /* Rejected Security */
144 MGMT_STATUS_REJECTED
, /* Rejected Personal */
145 MGMT_STATUS_TIMEOUT
, /* Host Timeout */
146 MGMT_STATUS_NOT_SUPPORTED
, /* Unsupported Feature */
147 MGMT_STATUS_INVALID_PARAMS
, /* Invalid Parameters */
148 MGMT_STATUS_DISCONNECTED
, /* OE User Ended Connection */
149 MGMT_STATUS_NO_RESOURCES
, /* OE Low Resources */
150 MGMT_STATUS_DISCONNECTED
, /* OE Power Off */
151 MGMT_STATUS_DISCONNECTED
, /* Connection Terminated */
152 MGMT_STATUS_BUSY
, /* Repeated Attempts */
153 MGMT_STATUS_REJECTED
, /* Pairing Not Allowed */
154 MGMT_STATUS_FAILED
, /* Unknown LMP PDU */
155 MGMT_STATUS_NOT_SUPPORTED
, /* Unsupported Remote Feature */
156 MGMT_STATUS_REJECTED
, /* SCO Offset Rejected */
157 MGMT_STATUS_REJECTED
, /* SCO Interval Rejected */
158 MGMT_STATUS_REJECTED
, /* Air Mode Rejected */
159 MGMT_STATUS_INVALID_PARAMS
, /* Invalid LMP Parameters */
160 MGMT_STATUS_FAILED
, /* Unspecified Error */
161 MGMT_STATUS_NOT_SUPPORTED
, /* Unsupported LMP Parameter Value */
162 MGMT_STATUS_FAILED
, /* Role Change Not Allowed */
163 MGMT_STATUS_TIMEOUT
, /* LMP Response Timeout */
164 MGMT_STATUS_FAILED
, /* LMP Error Transaction Collision */
165 MGMT_STATUS_FAILED
, /* LMP PDU Not Allowed */
166 MGMT_STATUS_REJECTED
, /* Encryption Mode Not Accepted */
167 MGMT_STATUS_FAILED
, /* Unit Link Key Used */
168 MGMT_STATUS_NOT_SUPPORTED
, /* QoS Not Supported */
169 MGMT_STATUS_TIMEOUT
, /* Instant Passed */
170 MGMT_STATUS_NOT_SUPPORTED
, /* Pairing Not Supported */
171 MGMT_STATUS_FAILED
, /* Transaction Collision */
172 MGMT_STATUS_INVALID_PARAMS
, /* Unacceptable Parameter */
173 MGMT_STATUS_REJECTED
, /* QoS Rejected */
174 MGMT_STATUS_NOT_SUPPORTED
, /* Classification Not Supported */
175 MGMT_STATUS_REJECTED
, /* Insufficient Security */
176 MGMT_STATUS_INVALID_PARAMS
, /* Parameter Out Of Range */
177 MGMT_STATUS_BUSY
, /* Role Switch Pending */
178 MGMT_STATUS_FAILED
, /* Slot Violation */
179 MGMT_STATUS_FAILED
, /* Role Switch Failed */
180 MGMT_STATUS_INVALID_PARAMS
, /* EIR Too Large */
181 MGMT_STATUS_NOT_SUPPORTED
, /* Simple Pairing Not Supported */
182 MGMT_STATUS_BUSY
, /* Host Busy Pairing */
183 MGMT_STATUS_REJECTED
, /* Rejected, No Suitable Channel */
184 MGMT_STATUS_BUSY
, /* Controller Busy */
185 MGMT_STATUS_INVALID_PARAMS
, /* Unsuitable Connection Interval */
186 MGMT_STATUS_TIMEOUT
, /* Directed Advertising Timeout */
187 MGMT_STATUS_AUTH_FAILED
, /* Terminated Due to MIC Failure */
188 MGMT_STATUS_CONNECT_FAILED
, /* Connection Establishment Failed */
189 MGMT_STATUS_CONNECT_FAILED
, /* MAC Connection Failed */
192 static u8
mgmt_status(u8 hci_status
)
194 if (hci_status
< ARRAY_SIZE(mgmt_status_table
))
195 return mgmt_status_table
[hci_status
];
197 return MGMT_STATUS_FAILED
;
200 static int cmd_status(struct sock
*sk
, u16 index
, u16 cmd
, u8 status
)
203 struct mgmt_hdr
*hdr
;
204 struct mgmt_ev_cmd_status
*ev
;
207 BT_DBG("sock %p, index %u, cmd %u, status %u", sk
, index
, cmd
, status
);
209 skb
= alloc_skb(sizeof(*hdr
) + sizeof(*ev
), GFP_KERNEL
);
213 hdr
= (void *) skb_put(skb
, sizeof(*hdr
));
215 hdr
->opcode
= __constant_cpu_to_le16(MGMT_EV_CMD_STATUS
);
216 hdr
->index
= cpu_to_le16(index
);
217 hdr
->len
= cpu_to_le16(sizeof(*ev
));
219 ev
= (void *) skb_put(skb
, sizeof(*ev
));
221 ev
->opcode
= cpu_to_le16(cmd
);
223 err
= sock_queue_rcv_skb(sk
, skb
);
230 static int cmd_complete(struct sock
*sk
, u16 index
, u16 cmd
, u8 status
,
231 void *rp
, size_t rp_len
)
234 struct mgmt_hdr
*hdr
;
235 struct mgmt_ev_cmd_complete
*ev
;
238 BT_DBG("sock %p", sk
);
240 skb
= alloc_skb(sizeof(*hdr
) + sizeof(*ev
) + rp_len
, GFP_KERNEL
);
244 hdr
= (void *) skb_put(skb
, sizeof(*hdr
));
246 hdr
->opcode
= __constant_cpu_to_le16(MGMT_EV_CMD_COMPLETE
);
247 hdr
->index
= cpu_to_le16(index
);
248 hdr
->len
= cpu_to_le16(sizeof(*ev
) + rp_len
);
250 ev
= (void *) skb_put(skb
, sizeof(*ev
) + rp_len
);
251 ev
->opcode
= cpu_to_le16(cmd
);
255 memcpy(ev
->data
, rp
, rp_len
);
257 err
= sock_queue_rcv_skb(sk
, skb
);
264 static int read_version(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
267 struct mgmt_rp_read_version rp
;
269 BT_DBG("sock %p", sk
);
271 rp
.version
= MGMT_VERSION
;
272 rp
.revision
= __constant_cpu_to_le16(MGMT_REVISION
);
274 return cmd_complete(sk
, MGMT_INDEX_NONE
, MGMT_OP_READ_VERSION
, 0, &rp
,
278 static int read_commands(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
281 struct mgmt_rp_read_commands
*rp
;
282 const u16 num_commands
= ARRAY_SIZE(mgmt_commands
);
283 const u16 num_events
= ARRAY_SIZE(mgmt_events
);
288 BT_DBG("sock %p", sk
);
290 rp_size
= sizeof(*rp
) + ((num_commands
+ num_events
) * sizeof(u16
));
292 rp
= kmalloc(rp_size
, GFP_KERNEL
);
296 rp
->num_commands
= __constant_cpu_to_le16(num_commands
);
297 rp
->num_events
= __constant_cpu_to_le16(num_events
);
299 for (i
= 0, opcode
= rp
->opcodes
; i
< num_commands
; i
++, opcode
++)
300 put_unaligned_le16(mgmt_commands
[i
], opcode
);
302 for (i
= 0; i
< num_events
; i
++, opcode
++)
303 put_unaligned_le16(mgmt_events
[i
], opcode
);
305 err
= cmd_complete(sk
, MGMT_INDEX_NONE
, MGMT_OP_READ_COMMANDS
, 0, rp
,
312 static int read_index_list(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
315 struct mgmt_rp_read_index_list
*rp
;
321 BT_DBG("sock %p", sk
);
323 read_lock(&hci_dev_list_lock
);
326 list_for_each_entry(d
, &hci_dev_list
, list
) {
327 if (d
->dev_type
== HCI_BREDR
)
331 rp_len
= sizeof(*rp
) + (2 * count
);
332 rp
= kmalloc(rp_len
, GFP_ATOMIC
);
334 read_unlock(&hci_dev_list_lock
);
339 list_for_each_entry(d
, &hci_dev_list
, list
) {
340 if (test_bit(HCI_SETUP
, &d
->dev_flags
))
343 if (test_bit(HCI_USER_CHANNEL
, &d
->dev_flags
))
346 if (d
->dev_type
== HCI_BREDR
) {
347 rp
->index
[count
++] = cpu_to_le16(d
->id
);
348 BT_DBG("Added hci%u", d
->id
);
352 rp
->num_controllers
= cpu_to_le16(count
);
353 rp_len
= sizeof(*rp
) + (2 * count
);
355 read_unlock(&hci_dev_list_lock
);
357 err
= cmd_complete(sk
, MGMT_INDEX_NONE
, MGMT_OP_READ_INDEX_LIST
, 0, rp
,
365 static u32
get_supported_settings(struct hci_dev
*hdev
)
369 settings
|= MGMT_SETTING_POWERED
;
370 settings
|= MGMT_SETTING_PAIRABLE
;
371 settings
|= MGMT_SETTING_DEBUG_KEYS
;
373 if (lmp_bredr_capable(hdev
)) {
374 settings
|= MGMT_SETTING_CONNECTABLE
;
375 if (hdev
->hci_ver
>= BLUETOOTH_VER_1_2
)
376 settings
|= MGMT_SETTING_FAST_CONNECTABLE
;
377 settings
|= MGMT_SETTING_DISCOVERABLE
;
378 settings
|= MGMT_SETTING_BREDR
;
379 settings
|= MGMT_SETTING_LINK_SECURITY
;
381 if (lmp_ssp_capable(hdev
)) {
382 settings
|= MGMT_SETTING_SSP
;
383 settings
|= MGMT_SETTING_HS
;
386 if (lmp_sc_capable(hdev
) ||
387 test_bit(HCI_FORCE_SC
, &hdev
->dev_flags
))
388 settings
|= MGMT_SETTING_SECURE_CONN
;
391 if (lmp_le_capable(hdev
)) {
392 settings
|= MGMT_SETTING_LE
;
393 settings
|= MGMT_SETTING_ADVERTISING
;
394 settings
|= MGMT_SETTING_PRIVACY
;
400 static u32
get_current_settings(struct hci_dev
*hdev
)
404 if (hdev_is_powered(hdev
))
405 settings
|= MGMT_SETTING_POWERED
;
407 if (test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
408 settings
|= MGMT_SETTING_CONNECTABLE
;
410 if (test_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
))
411 settings
|= MGMT_SETTING_FAST_CONNECTABLE
;
413 if (test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
))
414 settings
|= MGMT_SETTING_DISCOVERABLE
;
416 if (test_bit(HCI_PAIRABLE
, &hdev
->dev_flags
))
417 settings
|= MGMT_SETTING_PAIRABLE
;
419 if (test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
420 settings
|= MGMT_SETTING_BREDR
;
422 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
423 settings
|= MGMT_SETTING_LE
;
425 if (test_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
))
426 settings
|= MGMT_SETTING_LINK_SECURITY
;
428 if (test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
))
429 settings
|= MGMT_SETTING_SSP
;
431 if (test_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
))
432 settings
|= MGMT_SETTING_HS
;
434 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
435 settings
|= MGMT_SETTING_ADVERTISING
;
437 if (test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
))
438 settings
|= MGMT_SETTING_SECURE_CONN
;
440 if (test_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
))
441 settings
|= MGMT_SETTING_DEBUG_KEYS
;
443 if (test_bit(HCI_PRIVACY
, &hdev
->dev_flags
))
444 settings
|= MGMT_SETTING_PRIVACY
;
449 #define PNP_INFO_SVCLASS_ID 0x1200
451 static u8
*create_uuid16_list(struct hci_dev
*hdev
, u8
*data
, ptrdiff_t len
)
453 u8
*ptr
= data
, *uuids_start
= NULL
;
454 struct bt_uuid
*uuid
;
459 list_for_each_entry(uuid
, &hdev
->uuids
, list
) {
462 if (uuid
->size
!= 16)
465 uuid16
= get_unaligned_le16(&uuid
->uuid
[12]);
469 if (uuid16
== PNP_INFO_SVCLASS_ID
)
475 uuids_start
[1] = EIR_UUID16_ALL
;
479 /* Stop if not enough space to put next UUID */
480 if ((ptr
- data
) + sizeof(u16
) > len
) {
481 uuids_start
[1] = EIR_UUID16_SOME
;
485 *ptr
++ = (uuid16
& 0x00ff);
486 *ptr
++ = (uuid16
& 0xff00) >> 8;
487 uuids_start
[0] += sizeof(uuid16
);
493 static u8
*create_uuid32_list(struct hci_dev
*hdev
, u8
*data
, ptrdiff_t len
)
495 u8
*ptr
= data
, *uuids_start
= NULL
;
496 struct bt_uuid
*uuid
;
501 list_for_each_entry(uuid
, &hdev
->uuids
, list
) {
502 if (uuid
->size
!= 32)
508 uuids_start
[1] = EIR_UUID32_ALL
;
512 /* Stop if not enough space to put next UUID */
513 if ((ptr
- data
) + sizeof(u32
) > len
) {
514 uuids_start
[1] = EIR_UUID32_SOME
;
518 memcpy(ptr
, &uuid
->uuid
[12], sizeof(u32
));
520 uuids_start
[0] += sizeof(u32
);
526 static u8
*create_uuid128_list(struct hci_dev
*hdev
, u8
*data
, ptrdiff_t len
)
528 u8
*ptr
= data
, *uuids_start
= NULL
;
529 struct bt_uuid
*uuid
;
534 list_for_each_entry(uuid
, &hdev
->uuids
, list
) {
535 if (uuid
->size
!= 128)
541 uuids_start
[1] = EIR_UUID128_ALL
;
545 /* Stop if not enough space to put next UUID */
546 if ((ptr
- data
) + 16 > len
) {
547 uuids_start
[1] = EIR_UUID128_SOME
;
551 memcpy(ptr
, uuid
->uuid
, 16);
553 uuids_start
[0] += 16;
559 static struct pending_cmd
*mgmt_pending_find(u16 opcode
, struct hci_dev
*hdev
)
561 struct pending_cmd
*cmd
;
563 list_for_each_entry(cmd
, &hdev
->mgmt_pending
, list
) {
564 if (cmd
->opcode
== opcode
)
571 static u8
create_scan_rsp_data(struct hci_dev
*hdev
, u8
*ptr
)
576 name_len
= strlen(hdev
->dev_name
);
578 size_t max_len
= HCI_MAX_AD_LENGTH
- ad_len
- 2;
580 if (name_len
> max_len
) {
582 ptr
[1] = EIR_NAME_SHORT
;
584 ptr
[1] = EIR_NAME_COMPLETE
;
586 ptr
[0] = name_len
+ 1;
588 memcpy(ptr
+ 2, hdev
->dev_name
, name_len
);
590 ad_len
+= (name_len
+ 2);
591 ptr
+= (name_len
+ 2);
597 static void update_scan_rsp_data(struct hci_request
*req
)
599 struct hci_dev
*hdev
= req
->hdev
;
600 struct hci_cp_le_set_scan_rsp_data cp
;
603 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
606 memset(&cp
, 0, sizeof(cp
));
608 len
= create_scan_rsp_data(hdev
, cp
.data
);
610 if (hdev
->scan_rsp_data_len
== len
&&
611 memcmp(cp
.data
, hdev
->scan_rsp_data
, len
) == 0)
614 memcpy(hdev
->scan_rsp_data
, cp
.data
, sizeof(cp
.data
));
615 hdev
->scan_rsp_data_len
= len
;
619 hci_req_add(req
, HCI_OP_LE_SET_SCAN_RSP_DATA
, sizeof(cp
), &cp
);
622 static u8
get_adv_discov_flags(struct hci_dev
*hdev
)
624 struct pending_cmd
*cmd
;
626 /* If there's a pending mgmt command the flags will not yet have
627 * their final values, so check for this first.
629 cmd
= mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
);
631 struct mgmt_mode
*cp
= cmd
->param
;
633 return LE_AD_GENERAL
;
634 else if (cp
->val
== 0x02)
635 return LE_AD_LIMITED
;
637 if (test_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
))
638 return LE_AD_LIMITED
;
639 else if (test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
))
640 return LE_AD_GENERAL
;
646 static u8
create_adv_data(struct hci_dev
*hdev
, u8
*ptr
)
648 u8 ad_len
= 0, flags
= 0;
650 flags
|= get_adv_discov_flags(hdev
);
652 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
653 flags
|= LE_AD_NO_BREDR
;
656 BT_DBG("adv flags 0x%02x", flags
);
666 if (hdev
->adv_tx_power
!= HCI_TX_POWER_INVALID
) {
668 ptr
[1] = EIR_TX_POWER
;
669 ptr
[2] = (u8
) hdev
->adv_tx_power
;
678 static void update_adv_data(struct hci_request
*req
)
680 struct hci_dev
*hdev
= req
->hdev
;
681 struct hci_cp_le_set_adv_data cp
;
684 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
687 memset(&cp
, 0, sizeof(cp
));
689 len
= create_adv_data(hdev
, cp
.data
);
691 if (hdev
->adv_data_len
== len
&&
692 memcmp(cp
.data
, hdev
->adv_data
, len
) == 0)
695 memcpy(hdev
->adv_data
, cp
.data
, sizeof(cp
.data
));
696 hdev
->adv_data_len
= len
;
700 hci_req_add(req
, HCI_OP_LE_SET_ADV_DATA
, sizeof(cp
), &cp
);
703 static void create_eir(struct hci_dev
*hdev
, u8
*data
)
708 name_len
= strlen(hdev
->dev_name
);
714 ptr
[1] = EIR_NAME_SHORT
;
716 ptr
[1] = EIR_NAME_COMPLETE
;
718 /* EIR Data length */
719 ptr
[0] = name_len
+ 1;
721 memcpy(ptr
+ 2, hdev
->dev_name
, name_len
);
723 ptr
+= (name_len
+ 2);
726 if (hdev
->inq_tx_power
!= HCI_TX_POWER_INVALID
) {
728 ptr
[1] = EIR_TX_POWER
;
729 ptr
[2] = (u8
) hdev
->inq_tx_power
;
734 if (hdev
->devid_source
> 0) {
736 ptr
[1] = EIR_DEVICE_ID
;
738 put_unaligned_le16(hdev
->devid_source
, ptr
+ 2);
739 put_unaligned_le16(hdev
->devid_vendor
, ptr
+ 4);
740 put_unaligned_le16(hdev
->devid_product
, ptr
+ 6);
741 put_unaligned_le16(hdev
->devid_version
, ptr
+ 8);
746 ptr
= create_uuid16_list(hdev
, ptr
, HCI_MAX_EIR_LENGTH
- (ptr
- data
));
747 ptr
= create_uuid32_list(hdev
, ptr
, HCI_MAX_EIR_LENGTH
- (ptr
- data
));
748 ptr
= create_uuid128_list(hdev
, ptr
, HCI_MAX_EIR_LENGTH
- (ptr
- data
));
751 static void update_eir(struct hci_request
*req
)
753 struct hci_dev
*hdev
= req
->hdev
;
754 struct hci_cp_write_eir cp
;
756 if (!hdev_is_powered(hdev
))
759 if (!lmp_ext_inq_capable(hdev
))
762 if (!test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
))
765 if (test_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
))
768 memset(&cp
, 0, sizeof(cp
));
770 create_eir(hdev
, cp
.data
);
772 if (memcmp(cp
.data
, hdev
->eir
, sizeof(cp
.data
)) == 0)
775 memcpy(hdev
->eir
, cp
.data
, sizeof(cp
.data
));
777 hci_req_add(req
, HCI_OP_WRITE_EIR
, sizeof(cp
), &cp
);
780 static u8
get_service_classes(struct hci_dev
*hdev
)
782 struct bt_uuid
*uuid
;
785 list_for_each_entry(uuid
, &hdev
->uuids
, list
)
786 val
|= uuid
->svc_hint
;
791 static void update_class(struct hci_request
*req
)
793 struct hci_dev
*hdev
= req
->hdev
;
796 BT_DBG("%s", hdev
->name
);
798 if (!hdev_is_powered(hdev
))
801 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
804 if (test_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
))
807 cod
[0] = hdev
->minor_class
;
808 cod
[1] = hdev
->major_class
;
809 cod
[2] = get_service_classes(hdev
);
811 if (test_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
))
814 if (memcmp(cod
, hdev
->dev_class
, 3) == 0)
817 hci_req_add(req
, HCI_OP_WRITE_CLASS_OF_DEV
, sizeof(cod
), cod
);
820 static bool get_connectable(struct hci_dev
*hdev
)
822 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
;
833 return test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
836 static void enable_advertising(struct hci_request
*req
)
838 struct hci_dev
*hdev
= req
->hdev
;
839 struct hci_cp_le_set_adv_param cp
;
840 u8 own_addr_type
, enable
= 0x01;
843 connectable
= get_connectable(hdev
);
845 /* Set require_privacy to true only when non-connectable
846 * advertising is used. In that case it is fine to use a
847 * non-resolvable private address.
849 if (hci_update_random_address(req
, !connectable
, &own_addr_type
) < 0)
852 memset(&cp
, 0, sizeof(cp
));
853 cp
.min_interval
= __constant_cpu_to_le16(0x0800);
854 cp
.max_interval
= __constant_cpu_to_le16(0x0800);
855 cp
.type
= connectable
? LE_ADV_IND
: LE_ADV_NONCONN_IND
;
856 cp
.own_address_type
= own_addr_type
;
857 cp
.channel_map
= hdev
->le_adv_channel_map
;
859 hci_req_add(req
, HCI_OP_LE_SET_ADV_PARAM
, sizeof(cp
), &cp
);
861 hci_req_add(req
, HCI_OP_LE_SET_ADV_ENABLE
, sizeof(enable
), &enable
);
864 static void disable_advertising(struct hci_request
*req
)
868 hci_req_add(req
, HCI_OP_LE_SET_ADV_ENABLE
, sizeof(enable
), &enable
);
871 static void service_cache_off(struct work_struct
*work
)
873 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
875 struct hci_request req
;
877 if (!test_and_clear_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
))
880 hci_req_init(&req
, hdev
);
887 hci_dev_unlock(hdev
);
889 hci_req_run(&req
, NULL
);
892 static void rpa_expired(struct work_struct
*work
)
894 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
896 struct hci_request req
;
900 set_bit(HCI_RPA_EXPIRED
, &hdev
->dev_flags
);
902 if (!test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
) ||
903 hci_conn_num(hdev
, LE_LINK
) > 0)
906 /* The generation of a new RPA and programming it into the
907 * controller happens in the enable_advertising() function.
910 hci_req_init(&req
, hdev
);
912 disable_advertising(&req
);
913 enable_advertising(&req
);
915 hci_req_run(&req
, NULL
);
918 static void mgmt_init_hdev(struct sock
*sk
, struct hci_dev
*hdev
)
920 if (test_and_set_bit(HCI_MGMT
, &hdev
->dev_flags
))
923 INIT_DELAYED_WORK(&hdev
->service_cache
, service_cache_off
);
924 INIT_DELAYED_WORK(&hdev
->rpa_expired
, rpa_expired
);
926 /* Non-mgmt controlled devices get this bit set
927 * implicitly so that pairing works for them, however
928 * for mgmt we require user-space to explicitly enable
931 clear_bit(HCI_PAIRABLE
, &hdev
->dev_flags
);
934 static int read_controller_info(struct sock
*sk
, struct hci_dev
*hdev
,
935 void *data
, u16 data_len
)
937 struct mgmt_rp_read_info rp
;
939 BT_DBG("sock %p %s", sk
, hdev
->name
);
943 memset(&rp
, 0, sizeof(rp
));
945 bacpy(&rp
.bdaddr
, &hdev
->bdaddr
);
947 rp
.version
= hdev
->hci_ver
;
948 rp
.manufacturer
= cpu_to_le16(hdev
->manufacturer
);
950 rp
.supported_settings
= cpu_to_le32(get_supported_settings(hdev
));
951 rp
.current_settings
= cpu_to_le32(get_current_settings(hdev
));
953 memcpy(rp
.dev_class
, hdev
->dev_class
, 3);
955 memcpy(rp
.name
, hdev
->dev_name
, sizeof(hdev
->dev_name
));
956 memcpy(rp
.short_name
, hdev
->short_name
, sizeof(hdev
->short_name
));
958 hci_dev_unlock(hdev
);
960 return cmd_complete(sk
, hdev
->id
, MGMT_OP_READ_INFO
, 0, &rp
,
964 static void mgmt_pending_free(struct pending_cmd
*cmd
)
971 static struct pending_cmd
*mgmt_pending_add(struct sock
*sk
, u16 opcode
,
972 struct hci_dev
*hdev
, void *data
,
975 struct pending_cmd
*cmd
;
977 cmd
= kmalloc(sizeof(*cmd
), GFP_KERNEL
);
981 cmd
->opcode
= opcode
;
982 cmd
->index
= hdev
->id
;
984 cmd
->param
= kmalloc(len
, GFP_KERNEL
);
991 memcpy(cmd
->param
, data
, len
);
996 list_add(&cmd
->list
, &hdev
->mgmt_pending
);
1001 static void mgmt_pending_foreach(u16 opcode
, struct hci_dev
*hdev
,
1002 void (*cb
)(struct pending_cmd
*cmd
,
1006 struct pending_cmd
*cmd
, *tmp
;
1008 list_for_each_entry_safe(cmd
, tmp
, &hdev
->mgmt_pending
, list
) {
1009 if (opcode
> 0 && cmd
->opcode
!= opcode
)
1016 static void mgmt_pending_remove(struct pending_cmd
*cmd
)
1018 list_del(&cmd
->list
);
1019 mgmt_pending_free(cmd
);
1022 static int send_settings_rsp(struct sock
*sk
, u16 opcode
, struct hci_dev
*hdev
)
1024 __le32 settings
= cpu_to_le32(get_current_settings(hdev
));
1026 return cmd_complete(sk
, hdev
->id
, opcode
, 0, &settings
,
1030 static void clean_up_hci_complete(struct hci_dev
*hdev
, u8 status
)
1032 BT_DBG("%s status 0x%02x", hdev
->name
, status
);
1034 if (hci_conn_count(hdev
) == 0)
1035 queue_work(hdev
->req_workqueue
, &hdev
->power_off
.work
);
1038 static int clean_up_hci_state(struct hci_dev
*hdev
)
1040 struct hci_request req
;
1041 struct hci_conn
*conn
;
1043 hci_req_init(&req
, hdev
);
1045 if (test_bit(HCI_ISCAN
, &hdev
->flags
) ||
1046 test_bit(HCI_PSCAN
, &hdev
->flags
)) {
1048 hci_req_add(&req
, HCI_OP_WRITE_SCAN_ENABLE
, 1, &scan
);
1051 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
1052 disable_advertising(&req
);
1054 if (test_bit(HCI_LE_SCAN
, &hdev
->dev_flags
)) {
1055 hci_req_add_le_scan_disable(&req
);
1058 list_for_each_entry(conn
, &hdev
->conn_hash
.list
, list
) {
1059 struct hci_cp_disconnect dc
;
1061 dc
.handle
= cpu_to_le16(conn
->handle
);
1062 dc
.reason
= 0x15; /* Terminated due to Power Off */
1063 hci_req_add(&req
, HCI_OP_DISCONNECT
, sizeof(dc
), &dc
);
1066 return hci_req_run(&req
, clean_up_hci_complete
);
1069 static int set_powered(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1072 struct mgmt_mode
*cp
= data
;
1073 struct pending_cmd
*cmd
;
1076 BT_DBG("request for %s", hdev
->name
);
1078 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1079 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_POWERED
,
1080 MGMT_STATUS_INVALID_PARAMS
);
1084 if (mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
)) {
1085 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_POWERED
,
1090 if (test_and_clear_bit(HCI_AUTO_OFF
, &hdev
->dev_flags
)) {
1091 cancel_delayed_work(&hdev
->power_off
);
1094 mgmt_pending_add(sk
, MGMT_OP_SET_POWERED
, hdev
,
1096 err
= mgmt_powered(hdev
, 1);
1101 if (!!cp
->val
== hdev_is_powered(hdev
)) {
1102 err
= send_settings_rsp(sk
, MGMT_OP_SET_POWERED
, hdev
);
1106 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_POWERED
, hdev
, data
, len
);
1113 queue_work(hdev
->req_workqueue
, &hdev
->power_on
);
1116 /* Disconnect connections, stop scans, etc */
1117 err
= clean_up_hci_state(hdev
);
1119 /* ENODATA means there were no HCI commands queued */
1120 if (err
== -ENODATA
) {
1121 queue_work(hdev
->req_workqueue
, &hdev
->power_off
.work
);
1127 hci_dev_unlock(hdev
);
1131 static int mgmt_event(u16 event
, struct hci_dev
*hdev
, void *data
, u16 data_len
,
1132 struct sock
*skip_sk
)
1134 struct sk_buff
*skb
;
1135 struct mgmt_hdr
*hdr
;
1137 skb
= alloc_skb(sizeof(*hdr
) + data_len
, GFP_KERNEL
);
1141 hdr
= (void *) skb_put(skb
, sizeof(*hdr
));
1142 hdr
->opcode
= cpu_to_le16(event
);
1144 hdr
->index
= cpu_to_le16(hdev
->id
);
1146 hdr
->index
= __constant_cpu_to_le16(MGMT_INDEX_NONE
);
1147 hdr
->len
= cpu_to_le16(data_len
);
1150 memcpy(skb_put(skb
, data_len
), data
, data_len
);
1153 __net_timestamp(skb
);
1155 hci_send_to_control(skb
, skip_sk
);
1161 static int new_settings(struct hci_dev
*hdev
, struct sock
*skip
)
1165 ev
= cpu_to_le32(get_current_settings(hdev
));
1167 return mgmt_event(MGMT_EV_NEW_SETTINGS
, hdev
, &ev
, sizeof(ev
), skip
);
1172 struct hci_dev
*hdev
;
1176 static void settings_rsp(struct pending_cmd
*cmd
, void *data
)
1178 struct cmd_lookup
*match
= data
;
1180 send_settings_rsp(cmd
->sk
, cmd
->opcode
, match
->hdev
);
1182 list_del(&cmd
->list
);
1184 if (match
->sk
== NULL
) {
1185 match
->sk
= cmd
->sk
;
1186 sock_hold(match
->sk
);
1189 mgmt_pending_free(cmd
);
1192 static void cmd_status_rsp(struct pending_cmd
*cmd
, void *data
)
1196 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, *status
);
1197 mgmt_pending_remove(cmd
);
1200 static u8
mgmt_bredr_support(struct hci_dev
*hdev
)
1202 if (!lmp_bredr_capable(hdev
))
1203 return MGMT_STATUS_NOT_SUPPORTED
;
1204 else if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1205 return MGMT_STATUS_REJECTED
;
1207 return MGMT_STATUS_SUCCESS
;
1210 static u8
mgmt_le_support(struct hci_dev
*hdev
)
1212 if (!lmp_le_capable(hdev
))
1213 return MGMT_STATUS_NOT_SUPPORTED
;
1214 else if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
1215 return MGMT_STATUS_REJECTED
;
1217 return MGMT_STATUS_SUCCESS
;
1220 static void set_discoverable_complete(struct hci_dev
*hdev
, u8 status
)
1222 struct pending_cmd
*cmd
;
1223 struct mgmt_mode
*cp
;
1224 struct hci_request req
;
1227 BT_DBG("status 0x%02x", status
);
1231 cmd
= mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
);
1236 u8 mgmt_err
= mgmt_status(status
);
1237 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_err
);
1238 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1244 changed
= !test_and_set_bit(HCI_DISCOVERABLE
,
1247 if (hdev
->discov_timeout
> 0) {
1248 int to
= msecs_to_jiffies(hdev
->discov_timeout
* 1000);
1249 queue_delayed_work(hdev
->workqueue
, &hdev
->discov_off
,
1253 changed
= test_and_clear_bit(HCI_DISCOVERABLE
,
1257 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
);
1260 new_settings(hdev
, cmd
->sk
);
1262 /* When the discoverable mode gets changed, make sure
1263 * that class of device has the limited discoverable
1264 * bit correctly set.
1266 hci_req_init(&req
, hdev
);
1268 hci_req_run(&req
, NULL
);
1271 mgmt_pending_remove(cmd
);
1274 hci_dev_unlock(hdev
);
1277 static int set_discoverable(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1280 struct mgmt_cp_set_discoverable
*cp
= data
;
1281 struct pending_cmd
*cmd
;
1282 struct hci_request req
;
1287 BT_DBG("request for %s", hdev
->name
);
1289 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
) &&
1290 !test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1291 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1292 MGMT_STATUS_REJECTED
);
1294 if (cp
->val
!= 0x00 && cp
->val
!= 0x01 && cp
->val
!= 0x02)
1295 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1296 MGMT_STATUS_INVALID_PARAMS
);
1298 timeout
= __le16_to_cpu(cp
->timeout
);
1300 /* Disabling discoverable requires that no timeout is set,
1301 * and enabling limited discoverable requires a timeout.
1303 if ((cp
->val
== 0x00 && timeout
> 0) ||
1304 (cp
->val
== 0x02 && timeout
== 0))
1305 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1306 MGMT_STATUS_INVALID_PARAMS
);
1310 if (!hdev_is_powered(hdev
) && timeout
> 0) {
1311 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1312 MGMT_STATUS_NOT_POWERED
);
1316 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
) ||
1317 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
)) {
1318 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1323 if (!test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
)) {
1324 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1325 MGMT_STATUS_REJECTED
);
1329 if (!hdev_is_powered(hdev
)) {
1330 bool changed
= false;
1332 /* Setting limited discoverable when powered off is
1333 * not a valid operation since it requires a timeout
1334 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1336 if (!!cp
->val
!= test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
)) {
1337 change_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
1341 err
= send_settings_rsp(sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
);
1346 err
= new_settings(hdev
, sk
);
1351 /* If the current mode is the same, then just update the timeout
1352 * value with the new value. And if only the timeout gets updated,
1353 * then no need for any HCI transactions.
1355 if (!!cp
->val
== test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
) &&
1356 (cp
->val
== 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE
,
1357 &hdev
->dev_flags
)) {
1358 cancel_delayed_work(&hdev
->discov_off
);
1359 hdev
->discov_timeout
= timeout
;
1361 if (cp
->val
&& hdev
->discov_timeout
> 0) {
1362 int to
= msecs_to_jiffies(hdev
->discov_timeout
* 1000);
1363 queue_delayed_work(hdev
->workqueue
, &hdev
->discov_off
,
1367 err
= send_settings_rsp(sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
);
1371 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
, data
, len
);
1377 /* Cancel any potential discoverable timeout that might be
1378 * still active and store new timeout value. The arming of
1379 * the timeout happens in the complete handler.
1381 cancel_delayed_work(&hdev
->discov_off
);
1382 hdev
->discov_timeout
= timeout
;
1384 /* Limited discoverable mode */
1385 if (cp
->val
== 0x02)
1386 set_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1388 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1390 hci_req_init(&req
, hdev
);
1392 /* The procedure for LE-only controllers is much simpler - just
1393 * update the advertising data.
1395 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1401 struct hci_cp_write_current_iac_lap hci_cp
;
1403 if (cp
->val
== 0x02) {
1404 /* Limited discoverable mode */
1405 hci_cp
.num_iac
= min_t(u8
, hdev
->num_iac
, 2);
1406 hci_cp
.iac_lap
[0] = 0x00; /* LIAC */
1407 hci_cp
.iac_lap
[1] = 0x8b;
1408 hci_cp
.iac_lap
[2] = 0x9e;
1409 hci_cp
.iac_lap
[3] = 0x33; /* GIAC */
1410 hci_cp
.iac_lap
[4] = 0x8b;
1411 hci_cp
.iac_lap
[5] = 0x9e;
1413 /* General discoverable mode */
1415 hci_cp
.iac_lap
[0] = 0x33; /* GIAC */
1416 hci_cp
.iac_lap
[1] = 0x8b;
1417 hci_cp
.iac_lap
[2] = 0x9e;
1420 hci_req_add(&req
, HCI_OP_WRITE_CURRENT_IAC_LAP
,
1421 (hci_cp
.num_iac
* 3) + 1, &hci_cp
);
1423 scan
|= SCAN_INQUIRY
;
1425 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1428 hci_req_add(&req
, HCI_OP_WRITE_SCAN_ENABLE
, sizeof(scan
), &scan
);
1431 update_adv_data(&req
);
1433 err
= hci_req_run(&req
, set_discoverable_complete
);
1435 mgmt_pending_remove(cmd
);
1438 hci_dev_unlock(hdev
);
1442 static void write_fast_connectable(struct hci_request
*req
, bool enable
)
1444 struct hci_dev
*hdev
= req
->hdev
;
1445 struct hci_cp_write_page_scan_activity acp
;
1448 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1451 if (hdev
->hci_ver
< BLUETOOTH_VER_1_2
)
1455 type
= PAGE_SCAN_TYPE_INTERLACED
;
1457 /* 160 msec page scan interval */
1458 acp
.interval
= __constant_cpu_to_le16(0x0100);
1460 type
= PAGE_SCAN_TYPE_STANDARD
; /* default */
1462 /* default 1.28 sec page scan */
1463 acp
.interval
= __constant_cpu_to_le16(0x0800);
1466 acp
.window
= __constant_cpu_to_le16(0x0012);
1468 if (__cpu_to_le16(hdev
->page_scan_interval
) != acp
.interval
||
1469 __cpu_to_le16(hdev
->page_scan_window
) != acp
.window
)
1470 hci_req_add(req
, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY
,
1473 if (hdev
->page_scan_type
!= type
)
1474 hci_req_add(req
, HCI_OP_WRITE_PAGE_SCAN_TYPE
, 1, &type
);
1477 static void set_connectable_complete(struct hci_dev
*hdev
, u8 status
)
1479 struct pending_cmd
*cmd
;
1480 struct mgmt_mode
*cp
;
1483 BT_DBG("status 0x%02x", status
);
1487 cmd
= mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
);
1492 u8 mgmt_err
= mgmt_status(status
);
1493 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_err
);
1499 changed
= !test_and_set_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1501 changed
= test_and_clear_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1503 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_CONNECTABLE
, hdev
);
1506 new_settings(hdev
, cmd
->sk
);
1509 mgmt_pending_remove(cmd
);
1512 hci_dev_unlock(hdev
);
1515 static int set_connectable_update_settings(struct hci_dev
*hdev
,
1516 struct sock
*sk
, u8 val
)
1518 bool changed
= false;
1521 if (!!val
!= test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
1525 set_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1527 clear_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1528 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
1531 err
= send_settings_rsp(sk
, MGMT_OP_SET_CONNECTABLE
, hdev
);
1536 return new_settings(hdev
, sk
);
1541 static int set_connectable(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1544 struct mgmt_mode
*cp
= data
;
1545 struct pending_cmd
*cmd
;
1546 struct hci_request req
;
1550 BT_DBG("request for %s", hdev
->name
);
1552 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
) &&
1553 !test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1554 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_CONNECTABLE
,
1555 MGMT_STATUS_REJECTED
);
1557 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1558 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_CONNECTABLE
,
1559 MGMT_STATUS_INVALID_PARAMS
);
1563 if (!hdev_is_powered(hdev
)) {
1564 err
= set_connectable_update_settings(hdev
, sk
, cp
->val
);
1568 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
) ||
1569 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
)) {
1570 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_CONNECTABLE
,
1575 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_CONNECTABLE
, hdev
, data
, len
);
1581 hci_req_init(&req
, hdev
);
1583 /* If BR/EDR is not enabled and we disable advertising as a
1584 * by-product of disabling connectable, we need to update the
1585 * advertising flags.
1587 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
1589 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1590 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
1592 update_adv_data(&req
);
1593 } else if (cp
->val
!= test_bit(HCI_PSCAN
, &hdev
->flags
)) {
1599 if (test_bit(HCI_ISCAN
, &hdev
->flags
) &&
1600 hdev
->discov_timeout
> 0)
1601 cancel_delayed_work(&hdev
->discov_off
);
1604 hci_req_add(&req
, HCI_OP_WRITE_SCAN_ENABLE
, 1, &scan
);
1607 /* If we're going from non-connectable to connectable or
1608 * vice-versa when fast connectable is enabled ensure that fast
1609 * connectable gets disabled. write_fast_connectable won't do
1610 * anything if the page scan parameters are already what they
1613 if (cp
->val
|| test_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
))
1614 write_fast_connectable(&req
, false);
1616 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
) &&
1617 hci_conn_num(hdev
, LE_LINK
) == 0) {
1618 disable_advertising(&req
);
1619 enable_advertising(&req
);
1622 err
= hci_req_run(&req
, set_connectable_complete
);
1624 mgmt_pending_remove(cmd
);
1625 if (err
== -ENODATA
)
1626 err
= set_connectable_update_settings(hdev
, sk
,
1632 hci_dev_unlock(hdev
);
1636 static int set_pairable(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1639 struct mgmt_mode
*cp
= data
;
1643 BT_DBG("request for %s", hdev
->name
);
1645 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1646 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_PAIRABLE
,
1647 MGMT_STATUS_INVALID_PARAMS
);
1652 changed
= !test_and_set_bit(HCI_PAIRABLE
, &hdev
->dev_flags
);
1654 changed
= test_and_clear_bit(HCI_PAIRABLE
, &hdev
->dev_flags
);
1656 err
= send_settings_rsp(sk
, MGMT_OP_SET_PAIRABLE
, hdev
);
1661 err
= new_settings(hdev
, sk
);
1664 hci_dev_unlock(hdev
);
1668 static int set_link_security(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1671 struct mgmt_mode
*cp
= data
;
1672 struct pending_cmd
*cmd
;
1676 BT_DBG("request for %s", hdev
->name
);
1678 status
= mgmt_bredr_support(hdev
);
1680 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LINK_SECURITY
,
1683 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1684 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LINK_SECURITY
,
1685 MGMT_STATUS_INVALID_PARAMS
);
1689 if (!hdev_is_powered(hdev
)) {
1690 bool changed
= false;
1692 if (!!cp
->val
!= test_bit(HCI_LINK_SECURITY
,
1693 &hdev
->dev_flags
)) {
1694 change_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
);
1698 err
= send_settings_rsp(sk
, MGMT_OP_SET_LINK_SECURITY
, hdev
);
1703 err
= new_settings(hdev
, sk
);
1708 if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY
, hdev
)) {
1709 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LINK_SECURITY
,
1716 if (test_bit(HCI_AUTH
, &hdev
->flags
) == val
) {
1717 err
= send_settings_rsp(sk
, MGMT_OP_SET_LINK_SECURITY
, hdev
);
1721 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_LINK_SECURITY
, hdev
, data
, len
);
1727 err
= hci_send_cmd(hdev
, HCI_OP_WRITE_AUTH_ENABLE
, sizeof(val
), &val
);
1729 mgmt_pending_remove(cmd
);
1734 hci_dev_unlock(hdev
);
1738 static int set_ssp(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
1740 struct mgmt_mode
*cp
= data
;
1741 struct pending_cmd
*cmd
;
1745 BT_DBG("request for %s", hdev
->name
);
1747 status
= mgmt_bredr_support(hdev
);
1749 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
, status
);
1751 if (!lmp_ssp_capable(hdev
))
1752 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
,
1753 MGMT_STATUS_NOT_SUPPORTED
);
1755 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1756 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
,
1757 MGMT_STATUS_INVALID_PARAMS
);
1761 if (!hdev_is_powered(hdev
)) {
1765 changed
= !test_and_set_bit(HCI_SSP_ENABLED
,
1768 changed
= test_and_clear_bit(HCI_SSP_ENABLED
,
1771 changed
= test_and_clear_bit(HCI_HS_ENABLED
,
1774 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
1777 err
= send_settings_rsp(sk
, MGMT_OP_SET_SSP
, hdev
);
1782 err
= new_settings(hdev
, sk
);
1787 if (mgmt_pending_find(MGMT_OP_SET_SSP
, hdev
) ||
1788 mgmt_pending_find(MGMT_OP_SET_HS
, hdev
)) {
1789 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
,
1794 if (!!cp
->val
== test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
)) {
1795 err
= send_settings_rsp(sk
, MGMT_OP_SET_SSP
, hdev
);
1799 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_SSP
, hdev
, data
, len
);
1805 err
= hci_send_cmd(hdev
, HCI_OP_WRITE_SSP_MODE
, 1, &cp
->val
);
1807 mgmt_pending_remove(cmd
);
1812 hci_dev_unlock(hdev
);
1816 static int set_hs(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
1818 struct mgmt_mode
*cp
= data
;
1823 BT_DBG("request for %s", hdev
->name
);
1825 status
= mgmt_bredr_support(hdev
);
1827 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
, status
);
1829 if (!lmp_ssp_capable(hdev
))
1830 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1831 MGMT_STATUS_NOT_SUPPORTED
);
1833 if (!test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
))
1834 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1835 MGMT_STATUS_REJECTED
);
1837 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1838 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1839 MGMT_STATUS_INVALID_PARAMS
);
1844 changed
= !test_and_set_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
1846 if (hdev_is_powered(hdev
)) {
1847 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1848 MGMT_STATUS_REJECTED
);
1852 changed
= test_and_clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
1855 err
= send_settings_rsp(sk
, MGMT_OP_SET_HS
, hdev
);
1860 err
= new_settings(hdev
, sk
);
1863 hci_dev_unlock(hdev
);
1867 static void le_enable_complete(struct hci_dev
*hdev
, u8 status
)
1869 struct cmd_lookup match
= { NULL
, hdev
};
1872 u8 mgmt_err
= mgmt_status(status
);
1874 mgmt_pending_foreach(MGMT_OP_SET_LE
, hdev
, cmd_status_rsp
,
1879 mgmt_pending_foreach(MGMT_OP_SET_LE
, hdev
, settings_rsp
, &match
);
1881 new_settings(hdev
, match
.sk
);
1886 /* Make sure the controller has a good default for
1887 * advertising data. Restrict the update to when LE
1888 * has actually been enabled. During power on, the
1889 * update in powered_update_hci will take care of it.
1891 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
)) {
1892 struct hci_request req
;
1896 hci_req_init(&req
, hdev
);
1897 update_adv_data(&req
);
1898 update_scan_rsp_data(&req
);
1899 hci_req_run(&req
, NULL
);
1901 hci_dev_unlock(hdev
);
1905 static int set_le(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
1907 struct mgmt_mode
*cp
= data
;
1908 struct hci_cp_write_le_host_supported hci_cp
;
1909 struct pending_cmd
*cmd
;
1910 struct hci_request req
;
1914 BT_DBG("request for %s", hdev
->name
);
1916 if (!lmp_le_capable(hdev
))
1917 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1918 MGMT_STATUS_NOT_SUPPORTED
);
1920 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1921 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1922 MGMT_STATUS_INVALID_PARAMS
);
1924 /* LE-only devices do not allow toggling LE on/off */
1925 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1926 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1927 MGMT_STATUS_REJECTED
);
1932 enabled
= lmp_host_le_capable(hdev
);
1934 if (!hdev_is_powered(hdev
) || val
== enabled
) {
1935 bool changed
= false;
1937 if (val
!= test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
)) {
1938 change_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
);
1942 if (!val
&& test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
)) {
1943 clear_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
1947 err
= send_settings_rsp(sk
, MGMT_OP_SET_LE
, hdev
);
1952 err
= new_settings(hdev
, sk
);
1957 if (mgmt_pending_find(MGMT_OP_SET_LE
, hdev
) ||
1958 mgmt_pending_find(MGMT_OP_SET_ADVERTISING
, hdev
)) {
1959 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1964 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_LE
, hdev
, data
, len
);
1970 hci_req_init(&req
, hdev
);
1972 memset(&hci_cp
, 0, sizeof(hci_cp
));
1976 hci_cp
.simul
= lmp_le_br_capable(hdev
);
1978 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
1979 disable_advertising(&req
);
1982 hci_req_add(&req
, HCI_OP_WRITE_LE_HOST_SUPPORTED
, sizeof(hci_cp
),
1985 err
= hci_req_run(&req
, le_enable_complete
);
1987 mgmt_pending_remove(cmd
);
1990 hci_dev_unlock(hdev
);
1994 /* This is a helper function to test for pending mgmt commands that can
1995 * cause CoD or EIR HCI commands. We can only allow one such pending
1996 * mgmt command at a time since otherwise we cannot easily track what
1997 * the current values are, will be, and based on that calculate if a new
1998 * HCI command needs to be sent and if yes with what value.
2000 static bool pending_eir_or_class(struct hci_dev
*hdev
)
2002 struct pending_cmd
*cmd
;
2004 list_for_each_entry(cmd
, &hdev
->mgmt_pending
, list
) {
2005 switch (cmd
->opcode
) {
2006 case MGMT_OP_ADD_UUID
:
2007 case MGMT_OP_REMOVE_UUID
:
2008 case MGMT_OP_SET_DEV_CLASS
:
2009 case MGMT_OP_SET_POWERED
:
2017 static const u8 bluetooth_base_uuid
[] = {
2018 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
2019 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2022 static u8
get_uuid_size(const u8
*uuid
)
2026 if (memcmp(uuid
, bluetooth_base_uuid
, 12))
2029 val
= get_unaligned_le32(&uuid
[12]);
2036 static void mgmt_class_complete(struct hci_dev
*hdev
, u16 mgmt_op
, u8 status
)
2038 struct pending_cmd
*cmd
;
2042 cmd
= mgmt_pending_find(mgmt_op
, hdev
);
2046 cmd_complete(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_status(status
),
2047 hdev
->dev_class
, 3);
2049 mgmt_pending_remove(cmd
);
2052 hci_dev_unlock(hdev
);
2055 static void add_uuid_complete(struct hci_dev
*hdev
, u8 status
)
2057 BT_DBG("status 0x%02x", status
);
2059 mgmt_class_complete(hdev
, MGMT_OP_ADD_UUID
, status
);
2062 static int add_uuid(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
2064 struct mgmt_cp_add_uuid
*cp
= data
;
2065 struct pending_cmd
*cmd
;
2066 struct hci_request req
;
2067 struct bt_uuid
*uuid
;
2070 BT_DBG("request for %s", hdev
->name
);
2074 if (pending_eir_or_class(hdev
)) {
2075 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_ADD_UUID
,
2080 uuid
= kmalloc(sizeof(*uuid
), GFP_KERNEL
);
2086 memcpy(uuid
->uuid
, cp
->uuid
, 16);
2087 uuid
->svc_hint
= cp
->svc_hint
;
2088 uuid
->size
= get_uuid_size(cp
->uuid
);
2090 list_add_tail(&uuid
->list
, &hdev
->uuids
);
2092 hci_req_init(&req
, hdev
);
2097 err
= hci_req_run(&req
, add_uuid_complete
);
2099 if (err
!= -ENODATA
)
2102 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_ADD_UUID
, 0,
2103 hdev
->dev_class
, 3);
2107 cmd
= mgmt_pending_add(sk
, MGMT_OP_ADD_UUID
, hdev
, data
, len
);
2116 hci_dev_unlock(hdev
);
2120 static bool enable_service_cache(struct hci_dev
*hdev
)
2122 if (!hdev_is_powered(hdev
))
2125 if (!test_and_set_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
)) {
2126 queue_delayed_work(hdev
->workqueue
, &hdev
->service_cache
,
2134 static void remove_uuid_complete(struct hci_dev
*hdev
, u8 status
)
2136 BT_DBG("status 0x%02x", status
);
2138 mgmt_class_complete(hdev
, MGMT_OP_REMOVE_UUID
, status
);
2141 static int remove_uuid(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2144 struct mgmt_cp_remove_uuid
*cp
= data
;
2145 struct pending_cmd
*cmd
;
2146 struct bt_uuid
*match
, *tmp
;
2147 u8 bt_uuid_any
[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2148 struct hci_request req
;
2151 BT_DBG("request for %s", hdev
->name
);
2155 if (pending_eir_or_class(hdev
)) {
2156 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
,
2161 if (memcmp(cp
->uuid
, bt_uuid_any
, 16) == 0) {
2162 hci_uuids_clear(hdev
);
2164 if (enable_service_cache(hdev
)) {
2165 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
,
2166 0, hdev
->dev_class
, 3);
2175 list_for_each_entry_safe(match
, tmp
, &hdev
->uuids
, list
) {
2176 if (memcmp(match
->uuid
, cp
->uuid
, 16) != 0)
2179 list_del(&match
->list
);
2185 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
,
2186 MGMT_STATUS_INVALID_PARAMS
);
2191 hci_req_init(&req
, hdev
);
2196 err
= hci_req_run(&req
, remove_uuid_complete
);
2198 if (err
!= -ENODATA
)
2201 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
, 0,
2202 hdev
->dev_class
, 3);
2206 cmd
= mgmt_pending_add(sk
, MGMT_OP_REMOVE_UUID
, hdev
, data
, len
);
2215 hci_dev_unlock(hdev
);
2219 static void set_class_complete(struct hci_dev
*hdev
, u8 status
)
2221 BT_DBG("status 0x%02x", status
);
2223 mgmt_class_complete(hdev
, MGMT_OP_SET_DEV_CLASS
, status
);
2226 static int set_dev_class(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2229 struct mgmt_cp_set_dev_class
*cp
= data
;
2230 struct pending_cmd
*cmd
;
2231 struct hci_request req
;
2234 BT_DBG("request for %s", hdev
->name
);
2236 if (!lmp_bredr_capable(hdev
))
2237 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
,
2238 MGMT_STATUS_NOT_SUPPORTED
);
2242 if (pending_eir_or_class(hdev
)) {
2243 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
,
2248 if ((cp
->minor
& 0x03) != 0 || (cp
->major
& 0xe0) != 0) {
2249 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
,
2250 MGMT_STATUS_INVALID_PARAMS
);
2254 hdev
->major_class
= cp
->major
;
2255 hdev
->minor_class
= cp
->minor
;
2257 if (!hdev_is_powered(hdev
)) {
2258 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
, 0,
2259 hdev
->dev_class
, 3);
2263 hci_req_init(&req
, hdev
);
2265 if (test_and_clear_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
)) {
2266 hci_dev_unlock(hdev
);
2267 cancel_delayed_work_sync(&hdev
->service_cache
);
2274 err
= hci_req_run(&req
, set_class_complete
);
2276 if (err
!= -ENODATA
)
2279 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
, 0,
2280 hdev
->dev_class
, 3);
2284 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_DEV_CLASS
, hdev
, data
, len
);
2293 hci_dev_unlock(hdev
);
2297 static int load_link_keys(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2300 struct mgmt_cp_load_link_keys
*cp
= data
;
2301 u16 key_count
, expected_len
;
2305 BT_DBG("request for %s", hdev
->name
);
2307 if (!lmp_bredr_capable(hdev
))
2308 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2309 MGMT_STATUS_NOT_SUPPORTED
);
2311 key_count
= __le16_to_cpu(cp
->key_count
);
2313 expected_len
= sizeof(*cp
) + key_count
*
2314 sizeof(struct mgmt_link_key_info
);
2315 if (expected_len
!= len
) {
2316 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
2318 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2319 MGMT_STATUS_INVALID_PARAMS
);
2322 if (cp
->debug_keys
!= 0x00 && cp
->debug_keys
!= 0x01)
2323 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2324 MGMT_STATUS_INVALID_PARAMS
);
2326 BT_DBG("%s debug_keys %u key_count %u", hdev
->name
, cp
->debug_keys
,
2329 for (i
= 0; i
< key_count
; i
++) {
2330 struct mgmt_link_key_info
*key
= &cp
->keys
[i
];
2332 if (key
->addr
.type
!= BDADDR_BREDR
|| key
->type
> 0x08)
2333 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2334 MGMT_STATUS_INVALID_PARAMS
);
2339 hci_link_keys_clear(hdev
);
2342 changed
= !test_and_set_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
2344 changed
= test_and_clear_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
2347 new_settings(hdev
, NULL
);
2349 for (i
= 0; i
< key_count
; i
++) {
2350 struct mgmt_link_key_info
*key
= &cp
->keys
[i
];
2352 hci_add_link_key(hdev
, NULL
, 0, &key
->addr
.bdaddr
, key
->val
,
2353 key
->type
, key
->pin_len
);
2356 cmd_complete(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
, 0, NULL
, 0);
2358 hci_dev_unlock(hdev
);
2363 static int device_unpaired(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
2364 u8 addr_type
, struct sock
*skip_sk
)
2366 struct mgmt_ev_device_unpaired ev
;
2368 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
2369 ev
.addr
.type
= addr_type
;
2371 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED
, hdev
, &ev
, sizeof(ev
),
2375 static int unpair_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2378 struct mgmt_cp_unpair_device
*cp
= data
;
2379 struct mgmt_rp_unpair_device rp
;
2380 struct hci_cp_disconnect dc
;
2381 struct pending_cmd
*cmd
;
2382 struct hci_conn
*conn
;
2385 memset(&rp
, 0, sizeof(rp
));
2386 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
2387 rp
.addr
.type
= cp
->addr
.type
;
2389 if (!bdaddr_type_is_valid(cp
->addr
.type
))
2390 return cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2391 MGMT_STATUS_INVALID_PARAMS
,
2394 if (cp
->disconnect
!= 0x00 && cp
->disconnect
!= 0x01)
2395 return cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2396 MGMT_STATUS_INVALID_PARAMS
,
2401 if (!hdev_is_powered(hdev
)) {
2402 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2403 MGMT_STATUS_NOT_POWERED
, &rp
, sizeof(rp
));
2407 if (cp
->addr
.type
== BDADDR_BREDR
) {
2408 err
= hci_remove_link_key(hdev
, &cp
->addr
.bdaddr
);
2412 if (cp
->addr
.type
== BDADDR_LE_PUBLIC
)
2413 addr_type
= ADDR_LE_DEV_PUBLIC
;
2415 addr_type
= ADDR_LE_DEV_RANDOM
;
2417 hci_remove_irk(hdev
, &cp
->addr
.bdaddr
, addr_type
);
2419 hci_conn_params_del(hdev
, &cp
->addr
.bdaddr
, addr_type
);
2421 err
= hci_remove_ltk(hdev
, &cp
->addr
.bdaddr
, addr_type
);
2425 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2426 MGMT_STATUS_NOT_PAIRED
, &rp
, sizeof(rp
));
2430 if (cp
->disconnect
) {
2431 if (cp
->addr
.type
== BDADDR_BREDR
)
2432 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
,
2435 conn
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
,
2442 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
, 0,
2444 device_unpaired(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
, sk
);
2448 cmd
= mgmt_pending_add(sk
, MGMT_OP_UNPAIR_DEVICE
, hdev
, cp
,
2455 dc
.handle
= cpu_to_le16(conn
->handle
);
2456 dc
.reason
= 0x13; /* Remote User Terminated Connection */
2457 err
= hci_send_cmd(hdev
, HCI_OP_DISCONNECT
, sizeof(dc
), &dc
);
2459 mgmt_pending_remove(cmd
);
2462 hci_dev_unlock(hdev
);
2466 static int disconnect(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2469 struct mgmt_cp_disconnect
*cp
= data
;
2470 struct mgmt_rp_disconnect rp
;
2471 struct hci_cp_disconnect dc
;
2472 struct pending_cmd
*cmd
;
2473 struct hci_conn
*conn
;
2478 memset(&rp
, 0, sizeof(rp
));
2479 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
2480 rp
.addr
.type
= cp
->addr
.type
;
2482 if (!bdaddr_type_is_valid(cp
->addr
.type
))
2483 return cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2484 MGMT_STATUS_INVALID_PARAMS
,
2489 if (!test_bit(HCI_UP
, &hdev
->flags
)) {
2490 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2491 MGMT_STATUS_NOT_POWERED
, &rp
, sizeof(rp
));
2495 if (mgmt_pending_find(MGMT_OP_DISCONNECT
, hdev
)) {
2496 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2497 MGMT_STATUS_BUSY
, &rp
, sizeof(rp
));
2501 if (cp
->addr
.type
== BDADDR_BREDR
)
2502 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
,
2505 conn
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
, &cp
->addr
.bdaddr
);
2507 if (!conn
|| conn
->state
== BT_OPEN
|| conn
->state
== BT_CLOSED
) {
2508 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2509 MGMT_STATUS_NOT_CONNECTED
, &rp
, sizeof(rp
));
2513 cmd
= mgmt_pending_add(sk
, MGMT_OP_DISCONNECT
, hdev
, data
, len
);
2519 dc
.handle
= cpu_to_le16(conn
->handle
);
2520 dc
.reason
= HCI_ERROR_REMOTE_USER_TERM
;
2522 err
= hci_send_cmd(hdev
, HCI_OP_DISCONNECT
, sizeof(dc
), &dc
);
2524 mgmt_pending_remove(cmd
);
2527 hci_dev_unlock(hdev
);
2531 static u8
link_to_bdaddr(u8 link_type
, u8 addr_type
)
2533 switch (link_type
) {
2535 switch (addr_type
) {
2536 case ADDR_LE_DEV_PUBLIC
:
2537 return BDADDR_LE_PUBLIC
;
2540 /* Fallback to LE Random address type */
2541 return BDADDR_LE_RANDOM
;
2545 /* Fallback to BR/EDR type */
2546 return BDADDR_BREDR
;
2550 static int get_connections(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2553 struct mgmt_rp_get_connections
*rp
;
2563 if (!hdev_is_powered(hdev
)) {
2564 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_GET_CONNECTIONS
,
2565 MGMT_STATUS_NOT_POWERED
);
2570 list_for_each_entry(c
, &hdev
->conn_hash
.list
, list
) {
2571 if (test_bit(HCI_CONN_MGMT_CONNECTED
, &c
->flags
))
2575 rp_len
= sizeof(*rp
) + (i
* sizeof(struct mgmt_addr_info
));
2576 rp
= kmalloc(rp_len
, GFP_KERNEL
);
2583 list_for_each_entry(c
, &hdev
->conn_hash
.list
, list
) {
2584 if (!test_bit(HCI_CONN_MGMT_CONNECTED
, &c
->flags
))
2586 bacpy(&rp
->addr
[i
].bdaddr
, &c
->dst
);
2587 rp
->addr
[i
].type
= link_to_bdaddr(c
->type
, c
->dst_type
);
2588 if (c
->type
== SCO_LINK
|| c
->type
== ESCO_LINK
)
2593 rp
->conn_count
= cpu_to_le16(i
);
2595 /* Recalculate length in case of filtered SCO connections, etc */
2596 rp_len
= sizeof(*rp
) + (i
* sizeof(struct mgmt_addr_info
));
2598 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_GET_CONNECTIONS
, 0, rp
,
2604 hci_dev_unlock(hdev
);
2608 static int send_pin_code_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
2609 struct mgmt_cp_pin_code_neg_reply
*cp
)
2611 struct pending_cmd
*cmd
;
2614 cmd
= mgmt_pending_add(sk
, MGMT_OP_PIN_CODE_NEG_REPLY
, hdev
, cp
,
2619 err
= hci_send_cmd(hdev
, HCI_OP_PIN_CODE_NEG_REPLY
,
2620 sizeof(cp
->addr
.bdaddr
), &cp
->addr
.bdaddr
);
2622 mgmt_pending_remove(cmd
);
2627 static int pin_code_reply(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2630 struct hci_conn
*conn
;
2631 struct mgmt_cp_pin_code_reply
*cp
= data
;
2632 struct hci_cp_pin_code_reply reply
;
2633 struct pending_cmd
*cmd
;
2640 if (!hdev_is_powered(hdev
)) {
2641 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
2642 MGMT_STATUS_NOT_POWERED
);
2646 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &cp
->addr
.bdaddr
);
2648 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
2649 MGMT_STATUS_NOT_CONNECTED
);
2653 if (conn
->pending_sec_level
== BT_SECURITY_HIGH
&& cp
->pin_len
!= 16) {
2654 struct mgmt_cp_pin_code_neg_reply ncp
;
2656 memcpy(&ncp
.addr
, &cp
->addr
, sizeof(ncp
.addr
));
2658 BT_ERR("PIN code is not 16 bytes long");
2660 err
= send_pin_code_neg_reply(sk
, hdev
, &ncp
);
2662 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
2663 MGMT_STATUS_INVALID_PARAMS
);
2668 cmd
= mgmt_pending_add(sk
, MGMT_OP_PIN_CODE_REPLY
, hdev
, data
, len
);
2674 bacpy(&reply
.bdaddr
, &cp
->addr
.bdaddr
);
2675 reply
.pin_len
= cp
->pin_len
;
2676 memcpy(reply
.pin_code
, cp
->pin_code
, sizeof(reply
.pin_code
));
2678 err
= hci_send_cmd(hdev
, HCI_OP_PIN_CODE_REPLY
, sizeof(reply
), &reply
);
2680 mgmt_pending_remove(cmd
);
2683 hci_dev_unlock(hdev
);
2687 static int set_io_capability(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2690 struct mgmt_cp_set_io_capability
*cp
= data
;
2696 hdev
->io_capability
= cp
->io_capability
;
2698 BT_DBG("%s IO capability set to 0x%02x", hdev
->name
,
2699 hdev
->io_capability
);
2701 hci_dev_unlock(hdev
);
2703 return cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_IO_CAPABILITY
, 0, NULL
,
2707 static struct pending_cmd
*find_pairing(struct hci_conn
*conn
)
2709 struct hci_dev
*hdev
= conn
->hdev
;
2710 struct pending_cmd
*cmd
;
2712 list_for_each_entry(cmd
, &hdev
->mgmt_pending
, list
) {
2713 if (cmd
->opcode
!= MGMT_OP_PAIR_DEVICE
)
2716 if (cmd
->user_data
!= conn
)
2725 static void pairing_complete(struct pending_cmd
*cmd
, u8 status
)
2727 struct mgmt_rp_pair_device rp
;
2728 struct hci_conn
*conn
= cmd
->user_data
;
2730 bacpy(&rp
.addr
.bdaddr
, &conn
->dst
);
2731 rp
.addr
.type
= link_to_bdaddr(conn
->type
, conn
->dst_type
);
2733 cmd_complete(cmd
->sk
, cmd
->index
, MGMT_OP_PAIR_DEVICE
, status
,
2736 /* So we don't get further callbacks for this connection */
2737 conn
->connect_cfm_cb
= NULL
;
2738 conn
->security_cfm_cb
= NULL
;
2739 conn
->disconn_cfm_cb
= NULL
;
2741 hci_conn_drop(conn
);
2743 mgmt_pending_remove(cmd
);
2746 void mgmt_smp_complete(struct hci_conn
*conn
, bool complete
)
2748 u8 status
= complete
? MGMT_STATUS_SUCCESS
: MGMT_STATUS_FAILED
;
2749 struct pending_cmd
*cmd
;
2751 cmd
= find_pairing(conn
);
2753 pairing_complete(cmd
, status
);
2756 static void pairing_complete_cb(struct hci_conn
*conn
, u8 status
)
2758 struct pending_cmd
*cmd
;
2760 BT_DBG("status %u", status
);
2762 cmd
= find_pairing(conn
);
2764 BT_DBG("Unable to find a pending command");
2766 pairing_complete(cmd
, mgmt_status(status
));
2769 static void le_pairing_complete_cb(struct hci_conn
*conn
, u8 status
)
2771 struct pending_cmd
*cmd
;
2773 BT_DBG("status %u", status
);
2778 cmd
= find_pairing(conn
);
2780 BT_DBG("Unable to find a pending command");
2782 pairing_complete(cmd
, mgmt_status(status
));
2785 static int pair_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2788 struct mgmt_cp_pair_device
*cp
= data
;
2789 struct mgmt_rp_pair_device rp
;
2790 struct pending_cmd
*cmd
;
2791 u8 sec_level
, auth_type
;
2792 struct hci_conn
*conn
;
2797 memset(&rp
, 0, sizeof(rp
));
2798 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
2799 rp
.addr
.type
= cp
->addr
.type
;
2801 if (!bdaddr_type_is_valid(cp
->addr
.type
))
2802 return cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2803 MGMT_STATUS_INVALID_PARAMS
,
2808 if (!hdev_is_powered(hdev
)) {
2809 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2810 MGMT_STATUS_NOT_POWERED
, &rp
, sizeof(rp
));
2814 sec_level
= BT_SECURITY_MEDIUM
;
2815 if (cp
->io_cap
== 0x03)
2816 auth_type
= HCI_AT_DEDICATED_BONDING
;
2818 auth_type
= HCI_AT_DEDICATED_BONDING_MITM
;
2820 if (cp
->addr
.type
== BDADDR_BREDR
) {
2821 conn
= hci_connect_acl(hdev
, &cp
->addr
.bdaddr
, sec_level
,
2826 /* Convert from L2CAP channel address type to HCI address type
2828 if (cp
->addr
.type
== BDADDR_LE_PUBLIC
)
2829 addr_type
= ADDR_LE_DEV_PUBLIC
;
2831 addr_type
= ADDR_LE_DEV_RANDOM
;
2833 conn
= hci_connect_le(hdev
, &cp
->addr
.bdaddr
, addr_type
,
2834 sec_level
, auth_type
);
2840 if (PTR_ERR(conn
) == -EBUSY
)
2841 status
= MGMT_STATUS_BUSY
;
2843 status
= MGMT_STATUS_CONNECT_FAILED
;
2845 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2851 if (conn
->connect_cfm_cb
) {
2852 hci_conn_drop(conn
);
2853 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2854 MGMT_STATUS_BUSY
, &rp
, sizeof(rp
));
2858 cmd
= mgmt_pending_add(sk
, MGMT_OP_PAIR_DEVICE
, hdev
, data
, len
);
2861 hci_conn_drop(conn
);
2865 /* For LE, just connecting isn't a proof that the pairing finished */
2866 if (cp
->addr
.type
== BDADDR_BREDR
) {
2867 conn
->connect_cfm_cb
= pairing_complete_cb
;
2868 conn
->security_cfm_cb
= pairing_complete_cb
;
2869 conn
->disconn_cfm_cb
= pairing_complete_cb
;
2871 conn
->connect_cfm_cb
= le_pairing_complete_cb
;
2872 conn
->security_cfm_cb
= le_pairing_complete_cb
;
2873 conn
->disconn_cfm_cb
= le_pairing_complete_cb
;
2876 conn
->io_capability
= cp
->io_cap
;
2877 cmd
->user_data
= conn
;
2879 if (conn
->state
== BT_CONNECTED
&&
2880 hci_conn_security(conn
, sec_level
, auth_type
))
2881 pairing_complete(cmd
, 0);
2886 hci_dev_unlock(hdev
);
2890 static int cancel_pair_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2893 struct mgmt_addr_info
*addr
= data
;
2894 struct pending_cmd
*cmd
;
2895 struct hci_conn
*conn
;
2902 if (!hdev_is_powered(hdev
)) {
2903 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
,
2904 MGMT_STATUS_NOT_POWERED
);
2908 cmd
= mgmt_pending_find(MGMT_OP_PAIR_DEVICE
, hdev
);
2910 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
,
2911 MGMT_STATUS_INVALID_PARAMS
);
2915 conn
= cmd
->user_data
;
2917 if (bacmp(&addr
->bdaddr
, &conn
->dst
) != 0) {
2918 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
,
2919 MGMT_STATUS_INVALID_PARAMS
);
2923 pairing_complete(cmd
, MGMT_STATUS_CANCELLED
);
2925 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
, 0,
2926 addr
, sizeof(*addr
));
2928 hci_dev_unlock(hdev
);
2932 static int user_pairing_resp(struct sock
*sk
, struct hci_dev
*hdev
,
2933 struct mgmt_addr_info
*addr
, u16 mgmt_op
,
2934 u16 hci_op
, __le32 passkey
)
2936 struct pending_cmd
*cmd
;
2937 struct hci_conn
*conn
;
2942 if (!hdev_is_powered(hdev
)) {
2943 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2944 MGMT_STATUS_NOT_POWERED
, addr
,
2949 if (addr
->type
== BDADDR_BREDR
)
2950 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &addr
->bdaddr
);
2952 conn
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
, &addr
->bdaddr
);
2955 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2956 MGMT_STATUS_NOT_CONNECTED
, addr
,
2961 if (addr
->type
== BDADDR_LE_PUBLIC
|| addr
->type
== BDADDR_LE_RANDOM
) {
2962 /* Continue with pairing via SMP */
2963 err
= smp_user_confirm_reply(conn
, mgmt_op
, passkey
);
2966 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2967 MGMT_STATUS_SUCCESS
, addr
,
2970 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2971 MGMT_STATUS_FAILED
, addr
,
2977 cmd
= mgmt_pending_add(sk
, mgmt_op
, hdev
, addr
, sizeof(*addr
));
2983 /* Continue with pairing via HCI */
2984 if (hci_op
== HCI_OP_USER_PASSKEY_REPLY
) {
2985 struct hci_cp_user_passkey_reply cp
;
2987 bacpy(&cp
.bdaddr
, &addr
->bdaddr
);
2988 cp
.passkey
= passkey
;
2989 err
= hci_send_cmd(hdev
, hci_op
, sizeof(cp
), &cp
);
2991 err
= hci_send_cmd(hdev
, hci_op
, sizeof(addr
->bdaddr
),
2995 mgmt_pending_remove(cmd
);
2998 hci_dev_unlock(hdev
);
3002 static int pin_code_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
3003 void *data
, u16 len
)
3005 struct mgmt_cp_pin_code_neg_reply
*cp
= data
;
3009 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
3010 MGMT_OP_PIN_CODE_NEG_REPLY
,
3011 HCI_OP_PIN_CODE_NEG_REPLY
, 0);
3014 static int user_confirm_reply(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3017 struct mgmt_cp_user_confirm_reply
*cp
= data
;
3021 if (len
!= sizeof(*cp
))
3022 return cmd_status(sk
, hdev
->id
, MGMT_OP_USER_CONFIRM_REPLY
,
3023 MGMT_STATUS_INVALID_PARAMS
);
3025 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
3026 MGMT_OP_USER_CONFIRM_REPLY
,
3027 HCI_OP_USER_CONFIRM_REPLY
, 0);
3030 static int user_confirm_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
3031 void *data
, u16 len
)
3033 struct mgmt_cp_user_confirm_neg_reply
*cp
= data
;
3037 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
3038 MGMT_OP_USER_CONFIRM_NEG_REPLY
,
3039 HCI_OP_USER_CONFIRM_NEG_REPLY
, 0);
3042 static int user_passkey_reply(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3045 struct mgmt_cp_user_passkey_reply
*cp
= data
;
3049 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
3050 MGMT_OP_USER_PASSKEY_REPLY
,
3051 HCI_OP_USER_PASSKEY_REPLY
, cp
->passkey
);
3054 static int user_passkey_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
3055 void *data
, u16 len
)
3057 struct mgmt_cp_user_passkey_neg_reply
*cp
= data
;
3061 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
3062 MGMT_OP_USER_PASSKEY_NEG_REPLY
,
3063 HCI_OP_USER_PASSKEY_NEG_REPLY
, 0);
3066 static void update_name(struct hci_request
*req
)
3068 struct hci_dev
*hdev
= req
->hdev
;
3069 struct hci_cp_write_local_name cp
;
3071 memcpy(cp
.name
, hdev
->dev_name
, sizeof(cp
.name
));
3073 hci_req_add(req
, HCI_OP_WRITE_LOCAL_NAME
, sizeof(cp
), &cp
);
3076 static void set_name_complete(struct hci_dev
*hdev
, u8 status
)
3078 struct mgmt_cp_set_local_name
*cp
;
3079 struct pending_cmd
*cmd
;
3081 BT_DBG("status 0x%02x", status
);
3085 cmd
= mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME
, hdev
);
3092 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
,
3093 mgmt_status(status
));
3095 cmd_complete(cmd
->sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
, 0,
3098 mgmt_pending_remove(cmd
);
3101 hci_dev_unlock(hdev
);
3104 static int set_local_name(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3107 struct mgmt_cp_set_local_name
*cp
= data
;
3108 struct pending_cmd
*cmd
;
3109 struct hci_request req
;
3116 /* If the old values are the same as the new ones just return a
3117 * direct command complete event.
3119 if (!memcmp(hdev
->dev_name
, cp
->name
, sizeof(hdev
->dev_name
)) &&
3120 !memcmp(hdev
->short_name
, cp
->short_name
,
3121 sizeof(hdev
->short_name
))) {
3122 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
, 0,
3127 memcpy(hdev
->short_name
, cp
->short_name
, sizeof(hdev
->short_name
));
3129 if (!hdev_is_powered(hdev
)) {
3130 memcpy(hdev
->dev_name
, cp
->name
, sizeof(hdev
->dev_name
));
3132 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
, 0,
3137 err
= mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED
, hdev
, data
, len
,
3143 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_LOCAL_NAME
, hdev
, data
, len
);
3149 memcpy(hdev
->dev_name
, cp
->name
, sizeof(hdev
->dev_name
));
3151 hci_req_init(&req
, hdev
);
3153 if (lmp_bredr_capable(hdev
)) {
3158 /* The name is stored in the scan response data and so
3159 * no need to udpate the advertising data here.
3161 if (lmp_le_capable(hdev
))
3162 update_scan_rsp_data(&req
);
3164 err
= hci_req_run(&req
, set_name_complete
);
3166 mgmt_pending_remove(cmd
);
3169 hci_dev_unlock(hdev
);
3173 static int read_local_oob_data(struct sock
*sk
, struct hci_dev
*hdev
,
3174 void *data
, u16 data_len
)
3176 struct pending_cmd
*cmd
;
3179 BT_DBG("%s", hdev
->name
);
3183 if (!hdev_is_powered(hdev
)) {
3184 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
3185 MGMT_STATUS_NOT_POWERED
);
3189 if (!lmp_ssp_capable(hdev
)) {
3190 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
3191 MGMT_STATUS_NOT_SUPPORTED
);
3195 if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA
, hdev
)) {
3196 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
3201 cmd
= mgmt_pending_add(sk
, MGMT_OP_READ_LOCAL_OOB_DATA
, hdev
, NULL
, 0);
3207 if (test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
))
3208 err
= hci_send_cmd(hdev
, HCI_OP_READ_LOCAL_OOB_EXT_DATA
,
3211 err
= hci_send_cmd(hdev
, HCI_OP_READ_LOCAL_OOB_DATA
, 0, NULL
);
3214 mgmt_pending_remove(cmd
);
3217 hci_dev_unlock(hdev
);
3221 static int add_remote_oob_data(struct sock
*sk
, struct hci_dev
*hdev
,
3222 void *data
, u16 len
)
3226 BT_DBG("%s ", hdev
->name
);
3230 if (len
== MGMT_ADD_REMOTE_OOB_DATA_SIZE
) {
3231 struct mgmt_cp_add_remote_oob_data
*cp
= data
;
3234 err
= hci_add_remote_oob_data(hdev
, &cp
->addr
.bdaddr
,
3235 cp
->hash
, cp
->randomizer
);
3237 status
= MGMT_STATUS_FAILED
;
3239 status
= MGMT_STATUS_SUCCESS
;
3241 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_ADD_REMOTE_OOB_DATA
,
3242 status
, &cp
->addr
, sizeof(cp
->addr
));
3243 } else if (len
== MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE
) {
3244 struct mgmt_cp_add_remote_oob_ext_data
*cp
= data
;
3247 err
= hci_add_remote_oob_ext_data(hdev
, &cp
->addr
.bdaddr
,
3253 status
= MGMT_STATUS_FAILED
;
3255 status
= MGMT_STATUS_SUCCESS
;
3257 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_ADD_REMOTE_OOB_DATA
,
3258 status
, &cp
->addr
, sizeof(cp
->addr
));
3260 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len
);
3261 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_ADD_REMOTE_OOB_DATA
,
3262 MGMT_STATUS_INVALID_PARAMS
);
3265 hci_dev_unlock(hdev
);
3269 static int remove_remote_oob_data(struct sock
*sk
, struct hci_dev
*hdev
,
3270 void *data
, u16 len
)
3272 struct mgmt_cp_remove_remote_oob_data
*cp
= data
;
3276 BT_DBG("%s", hdev
->name
);
3280 err
= hci_remove_remote_oob_data(hdev
, &cp
->addr
.bdaddr
);
3282 status
= MGMT_STATUS_INVALID_PARAMS
;
3284 status
= MGMT_STATUS_SUCCESS
;
3286 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_REMOVE_REMOTE_OOB_DATA
,
3287 status
, &cp
->addr
, sizeof(cp
->addr
));
3289 hci_dev_unlock(hdev
);
3293 static int mgmt_start_discovery_failed(struct hci_dev
*hdev
, u8 status
)
3295 struct pending_cmd
*cmd
;
3299 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
3301 cmd
= mgmt_pending_find(MGMT_OP_START_DISCOVERY
, hdev
);
3305 type
= hdev
->discovery
.type
;
3307 err
= cmd_complete(cmd
->sk
, hdev
->id
, cmd
->opcode
, mgmt_status(status
),
3308 &type
, sizeof(type
));
3309 mgmt_pending_remove(cmd
);
3314 static void start_discovery_complete(struct hci_dev
*hdev
, u8 status
)
3316 BT_DBG("status %d", status
);
3320 mgmt_start_discovery_failed(hdev
, status
);
3321 hci_dev_unlock(hdev
);
3326 hci_discovery_set_state(hdev
, DISCOVERY_FINDING
);
3327 hci_dev_unlock(hdev
);
3329 switch (hdev
->discovery
.type
) {
3330 case DISCOV_TYPE_LE
:
3331 queue_delayed_work(hdev
->workqueue
, &hdev
->le_scan_disable
,
3335 case DISCOV_TYPE_INTERLEAVED
:
3336 queue_delayed_work(hdev
->workqueue
, &hdev
->le_scan_disable
,
3337 DISCOV_INTERLEAVED_TIMEOUT
);
3340 case DISCOV_TYPE_BREDR
:
3344 BT_ERR("Invalid discovery type %d", hdev
->discovery
.type
);
3348 static int start_discovery(struct sock
*sk
, struct hci_dev
*hdev
,
3349 void *data
, u16 len
)
3351 struct mgmt_cp_start_discovery
*cp
= data
;
3352 struct pending_cmd
*cmd
;
3353 struct hci_cp_le_set_scan_param param_cp
;
3354 struct hci_cp_le_set_scan_enable enable_cp
;
3355 struct hci_cp_inquiry inq_cp
;
3356 struct hci_request req
;
3357 /* General inquiry access code (GIAC) */
3358 u8 lap
[3] = { 0x33, 0x8b, 0x9e };
3359 u8 status
, own_addr_type
;
3362 BT_DBG("%s", hdev
->name
);
3366 if (!hdev_is_powered(hdev
)) {
3367 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3368 MGMT_STATUS_NOT_POWERED
);
3372 if (test_bit(HCI_PERIODIC_INQ
, &hdev
->dev_flags
)) {
3373 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3378 if (hdev
->discovery
.state
!= DISCOVERY_STOPPED
) {
3379 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3384 cmd
= mgmt_pending_add(sk
, MGMT_OP_START_DISCOVERY
, hdev
, NULL
, 0);
3390 hdev
->discovery
.type
= cp
->type
;
3392 hci_req_init(&req
, hdev
);
3394 switch (hdev
->discovery
.type
) {
3395 case DISCOV_TYPE_BREDR
:
3396 status
= mgmt_bredr_support(hdev
);
3398 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3400 mgmt_pending_remove(cmd
);
3404 if (test_bit(HCI_INQUIRY
, &hdev
->flags
)) {
3405 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3407 mgmt_pending_remove(cmd
);
3411 hci_inquiry_cache_flush(hdev
);
3413 memset(&inq_cp
, 0, sizeof(inq_cp
));
3414 memcpy(&inq_cp
.lap
, lap
, sizeof(inq_cp
.lap
));
3415 inq_cp
.length
= DISCOV_BREDR_INQUIRY_LEN
;
3416 hci_req_add(&req
, HCI_OP_INQUIRY
, sizeof(inq_cp
), &inq_cp
);
3419 case DISCOV_TYPE_LE
:
3420 case DISCOV_TYPE_INTERLEAVED
:
3421 status
= mgmt_le_support(hdev
);
3423 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3425 mgmt_pending_remove(cmd
);
3429 if (hdev
->discovery
.type
== DISCOV_TYPE_INTERLEAVED
&&
3430 !test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
3431 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3432 MGMT_STATUS_NOT_SUPPORTED
);
3433 mgmt_pending_remove(cmd
);
3437 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
)) {
3438 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3439 MGMT_STATUS_REJECTED
);
3440 mgmt_pending_remove(cmd
);
3444 /* If controller is scanning, it means the background scanning
3445 * is running. Thus, we should temporarily stop it in order to
3446 * set the discovery scanning parameters.
3448 if (test_bit(HCI_LE_SCAN
, &hdev
->dev_flags
))
3449 hci_req_add_le_scan_disable(&req
);
3451 memset(¶m_cp
, 0, sizeof(param_cp
));
3453 /* All active scans will be done with either a resolvable
3454 * private address (when privacy feature has been enabled)
3455 * or unresolvable private address.
3457 err
= hci_update_random_address(&req
, true, &own_addr_type
);
3459 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3460 MGMT_STATUS_FAILED
);
3461 mgmt_pending_remove(cmd
);
3465 param_cp
.type
= LE_SCAN_ACTIVE
;
3466 param_cp
.interval
= cpu_to_le16(DISCOV_LE_SCAN_INT
);
3467 param_cp
.window
= cpu_to_le16(DISCOV_LE_SCAN_WIN
);
3468 param_cp
.own_address_type
= own_addr_type
;
3469 hci_req_add(&req
, HCI_OP_LE_SET_SCAN_PARAM
, sizeof(param_cp
),
3472 memset(&enable_cp
, 0, sizeof(enable_cp
));
3473 enable_cp
.enable
= LE_SCAN_ENABLE
;
3474 enable_cp
.filter_dup
= LE_SCAN_FILTER_DUP_ENABLE
;
3475 hci_req_add(&req
, HCI_OP_LE_SET_SCAN_ENABLE
, sizeof(enable_cp
),
3480 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3481 MGMT_STATUS_INVALID_PARAMS
);
3482 mgmt_pending_remove(cmd
);
3486 err
= hci_req_run(&req
, start_discovery_complete
);
3488 mgmt_pending_remove(cmd
);
3490 hci_discovery_set_state(hdev
, DISCOVERY_STARTING
);
3493 hci_dev_unlock(hdev
);
3497 static int mgmt_stop_discovery_failed(struct hci_dev
*hdev
, u8 status
)
3499 struct pending_cmd
*cmd
;
3502 cmd
= mgmt_pending_find(MGMT_OP_STOP_DISCOVERY
, hdev
);
3506 err
= cmd_complete(cmd
->sk
, hdev
->id
, cmd
->opcode
, mgmt_status(status
),
3507 &hdev
->discovery
.type
, sizeof(hdev
->discovery
.type
));
3508 mgmt_pending_remove(cmd
);
3513 static void stop_discovery_complete(struct hci_dev
*hdev
, u8 status
)
3515 BT_DBG("status %d", status
);
3520 mgmt_stop_discovery_failed(hdev
, status
);
3524 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
3527 hci_dev_unlock(hdev
);
3530 static int stop_discovery(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3533 struct mgmt_cp_stop_discovery
*mgmt_cp
= data
;
3534 struct pending_cmd
*cmd
;
3535 struct hci_cp_remote_name_req_cancel cp
;
3536 struct inquiry_entry
*e
;
3537 struct hci_request req
;
3540 BT_DBG("%s", hdev
->name
);
3544 if (!hci_discovery_active(hdev
)) {
3545 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_STOP_DISCOVERY
,
3546 MGMT_STATUS_REJECTED
, &mgmt_cp
->type
,
3547 sizeof(mgmt_cp
->type
));
3551 if (hdev
->discovery
.type
!= mgmt_cp
->type
) {
3552 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_STOP_DISCOVERY
,
3553 MGMT_STATUS_INVALID_PARAMS
, &mgmt_cp
->type
,
3554 sizeof(mgmt_cp
->type
));
3558 cmd
= mgmt_pending_add(sk
, MGMT_OP_STOP_DISCOVERY
, hdev
, NULL
, 0);
3564 hci_req_init(&req
, hdev
);
3566 switch (hdev
->discovery
.state
) {
3567 case DISCOVERY_FINDING
:
3568 if (test_bit(HCI_INQUIRY
, &hdev
->flags
)) {
3569 hci_req_add(&req
, HCI_OP_INQUIRY_CANCEL
, 0, NULL
);
3571 cancel_delayed_work(&hdev
->le_scan_disable
);
3573 hci_req_add_le_scan_disable(&req
);
3578 case DISCOVERY_RESOLVING
:
3579 e
= hci_inquiry_cache_lookup_resolve(hdev
, BDADDR_ANY
,
3582 mgmt_pending_remove(cmd
);
3583 err
= cmd_complete(sk
, hdev
->id
,
3584 MGMT_OP_STOP_DISCOVERY
, 0,
3586 sizeof(mgmt_cp
->type
));
3587 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
3591 bacpy(&cp
.bdaddr
, &e
->data
.bdaddr
);
3592 hci_req_add(&req
, HCI_OP_REMOTE_NAME_REQ_CANCEL
, sizeof(cp
),
3598 BT_DBG("unknown discovery state %u", hdev
->discovery
.state
);
3600 mgmt_pending_remove(cmd
);
3601 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_STOP_DISCOVERY
,
3602 MGMT_STATUS_FAILED
, &mgmt_cp
->type
,
3603 sizeof(mgmt_cp
->type
));
3607 err
= hci_req_run(&req
, stop_discovery_complete
);
3609 mgmt_pending_remove(cmd
);
3611 hci_discovery_set_state(hdev
, DISCOVERY_STOPPING
);
3614 hci_dev_unlock(hdev
);
3618 static int confirm_name(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3621 struct mgmt_cp_confirm_name
*cp
= data
;
3622 struct inquiry_entry
*e
;
3625 BT_DBG("%s", hdev
->name
);
3629 if (!hci_discovery_active(hdev
)) {
3630 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CONFIRM_NAME
,
3631 MGMT_STATUS_FAILED
);
3635 e
= hci_inquiry_cache_lookup_unknown(hdev
, &cp
->addr
.bdaddr
);
3637 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CONFIRM_NAME
,
3638 MGMT_STATUS_INVALID_PARAMS
);
3642 if (cp
->name_known
) {
3643 e
->name_state
= NAME_KNOWN
;
3646 e
->name_state
= NAME_NEEDED
;
3647 hci_inquiry_cache_update_resolve(hdev
, e
);
3650 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_CONFIRM_NAME
, 0, &cp
->addr
,
3654 hci_dev_unlock(hdev
);
3658 static int block_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3661 struct mgmt_cp_block_device
*cp
= data
;
3665 BT_DBG("%s", hdev
->name
);
3667 if (!bdaddr_type_is_valid(cp
->addr
.type
))
3668 return cmd_complete(sk
, hdev
->id
, MGMT_OP_BLOCK_DEVICE
,
3669 MGMT_STATUS_INVALID_PARAMS
,
3670 &cp
->addr
, sizeof(cp
->addr
));
3674 err
= hci_blacklist_add(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
);
3676 status
= MGMT_STATUS_FAILED
;
3678 status
= MGMT_STATUS_SUCCESS
;
3680 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_BLOCK_DEVICE
, status
,
3681 &cp
->addr
, sizeof(cp
->addr
));
3683 hci_dev_unlock(hdev
);
3688 static int unblock_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3691 struct mgmt_cp_unblock_device
*cp
= data
;
3695 BT_DBG("%s", hdev
->name
);
3697 if (!bdaddr_type_is_valid(cp
->addr
.type
))
3698 return cmd_complete(sk
, hdev
->id
, MGMT_OP_UNBLOCK_DEVICE
,
3699 MGMT_STATUS_INVALID_PARAMS
,
3700 &cp
->addr
, sizeof(cp
->addr
));
3704 err
= hci_blacklist_del(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
);
3706 status
= MGMT_STATUS_INVALID_PARAMS
;
3708 status
= MGMT_STATUS_SUCCESS
;
3710 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNBLOCK_DEVICE
, status
,
3711 &cp
->addr
, sizeof(cp
->addr
));
3713 hci_dev_unlock(hdev
);
3718 static int set_device_id(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3721 struct mgmt_cp_set_device_id
*cp
= data
;
3722 struct hci_request req
;
3726 BT_DBG("%s", hdev
->name
);
3728 source
= __le16_to_cpu(cp
->source
);
3730 if (source
> 0x0002)
3731 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEVICE_ID
,
3732 MGMT_STATUS_INVALID_PARAMS
);
3736 hdev
->devid_source
= source
;
3737 hdev
->devid_vendor
= __le16_to_cpu(cp
->vendor
);
3738 hdev
->devid_product
= __le16_to_cpu(cp
->product
);
3739 hdev
->devid_version
= __le16_to_cpu(cp
->version
);
3741 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_DEVICE_ID
, 0, NULL
, 0);
3743 hci_req_init(&req
, hdev
);
3745 hci_req_run(&req
, NULL
);
3747 hci_dev_unlock(hdev
);
3752 static void set_advertising_complete(struct hci_dev
*hdev
, u8 status
)
3754 struct cmd_lookup match
= { NULL
, hdev
};
3757 u8 mgmt_err
= mgmt_status(status
);
3759 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING
, hdev
,
3760 cmd_status_rsp
, &mgmt_err
);
3764 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING
, hdev
, settings_rsp
,
3767 new_settings(hdev
, match
.sk
);
3773 static int set_advertising(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3776 struct mgmt_mode
*cp
= data
;
3777 struct pending_cmd
*cmd
;
3778 struct hci_request req
;
3779 u8 val
, enabled
, status
;
3782 BT_DBG("request for %s", hdev
->name
);
3784 status
= mgmt_le_support(hdev
);
3786 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_ADVERTISING
,
3789 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
3790 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_ADVERTISING
,
3791 MGMT_STATUS_INVALID_PARAMS
);
3796 enabled
= test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
3798 /* The following conditions are ones which mean that we should
3799 * not do any HCI communication but directly send a mgmt
3800 * response to user space (after toggling the flag if
3803 if (!hdev_is_powered(hdev
) || val
== enabled
||
3804 hci_conn_num(hdev
, LE_LINK
) > 0) {
3805 bool changed
= false;
3807 if (val
!= test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
)) {
3808 change_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
3812 err
= send_settings_rsp(sk
, MGMT_OP_SET_ADVERTISING
, hdev
);
3817 err
= new_settings(hdev
, sk
);
3822 if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING
, hdev
) ||
3823 mgmt_pending_find(MGMT_OP_SET_LE
, hdev
)) {
3824 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_ADVERTISING
,
3829 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_ADVERTISING
, hdev
, data
, len
);
3835 hci_req_init(&req
, hdev
);
3838 enable_advertising(&req
);
3840 disable_advertising(&req
);
3842 err
= hci_req_run(&req
, set_advertising_complete
);
3844 mgmt_pending_remove(cmd
);
3847 hci_dev_unlock(hdev
);
3851 static int set_static_address(struct sock
*sk
, struct hci_dev
*hdev
,
3852 void *data
, u16 len
)
3854 struct mgmt_cp_set_static_address
*cp
= data
;
3857 BT_DBG("%s", hdev
->name
);
3859 if (!lmp_le_capable(hdev
))
3860 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_STATIC_ADDRESS
,
3861 MGMT_STATUS_NOT_SUPPORTED
);
3863 if (hdev_is_powered(hdev
))
3864 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_STATIC_ADDRESS
,
3865 MGMT_STATUS_REJECTED
);
3867 if (bacmp(&cp
->bdaddr
, BDADDR_ANY
)) {
3868 if (!bacmp(&cp
->bdaddr
, BDADDR_NONE
))
3869 return cmd_status(sk
, hdev
->id
,
3870 MGMT_OP_SET_STATIC_ADDRESS
,
3871 MGMT_STATUS_INVALID_PARAMS
);
3873 /* Two most significant bits shall be set */
3874 if ((cp
->bdaddr
.b
[5] & 0xc0) != 0xc0)
3875 return cmd_status(sk
, hdev
->id
,
3876 MGMT_OP_SET_STATIC_ADDRESS
,
3877 MGMT_STATUS_INVALID_PARAMS
);
3882 bacpy(&hdev
->static_addr
, &cp
->bdaddr
);
3884 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_STATIC_ADDRESS
, 0, NULL
, 0);
3886 hci_dev_unlock(hdev
);
3891 static int set_scan_params(struct sock
*sk
, struct hci_dev
*hdev
,
3892 void *data
, u16 len
)
3894 struct mgmt_cp_set_scan_params
*cp
= data
;
3895 __u16 interval
, window
;
3898 BT_DBG("%s", hdev
->name
);
3900 if (!lmp_le_capable(hdev
))
3901 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3902 MGMT_STATUS_NOT_SUPPORTED
);
3904 interval
= __le16_to_cpu(cp
->interval
);
3906 if (interval
< 0x0004 || interval
> 0x4000)
3907 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3908 MGMT_STATUS_INVALID_PARAMS
);
3910 window
= __le16_to_cpu(cp
->window
);
3912 if (window
< 0x0004 || window
> 0x4000)
3913 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3914 MGMT_STATUS_INVALID_PARAMS
);
3916 if (window
> interval
)
3917 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3918 MGMT_STATUS_INVALID_PARAMS
);
3922 hdev
->le_scan_interval
= interval
;
3923 hdev
->le_scan_window
= window
;
3925 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
, 0, NULL
, 0);
3927 hci_dev_unlock(hdev
);
3932 static void fast_connectable_complete(struct hci_dev
*hdev
, u8 status
)
3934 struct pending_cmd
*cmd
;
3936 BT_DBG("status 0x%02x", status
);
3940 cmd
= mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE
, hdev
);
3945 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3946 mgmt_status(status
));
3948 struct mgmt_mode
*cp
= cmd
->param
;
3951 set_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
);
3953 clear_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
);
3955 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_FAST_CONNECTABLE
, hdev
);
3956 new_settings(hdev
, cmd
->sk
);
3959 mgmt_pending_remove(cmd
);
3962 hci_dev_unlock(hdev
);
3965 static int set_fast_connectable(struct sock
*sk
, struct hci_dev
*hdev
,
3966 void *data
, u16 len
)
3968 struct mgmt_mode
*cp
= data
;
3969 struct pending_cmd
*cmd
;
3970 struct hci_request req
;
3973 BT_DBG("%s", hdev
->name
);
3975 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
) ||
3976 hdev
->hci_ver
< BLUETOOTH_VER_1_2
)
3977 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3978 MGMT_STATUS_NOT_SUPPORTED
);
3980 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
3981 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3982 MGMT_STATUS_INVALID_PARAMS
);
3984 if (!hdev_is_powered(hdev
))
3985 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3986 MGMT_STATUS_NOT_POWERED
);
3988 if (!test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
3989 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3990 MGMT_STATUS_REJECTED
);
3994 if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE
, hdev
)) {
3995 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
4000 if (!!cp
->val
== test_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
)) {
4001 err
= send_settings_rsp(sk
, MGMT_OP_SET_FAST_CONNECTABLE
,
4006 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_FAST_CONNECTABLE
, hdev
,
4013 hci_req_init(&req
, hdev
);
4015 write_fast_connectable(&req
, cp
->val
);
4017 err
= hci_req_run(&req
, fast_connectable_complete
);
4019 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
4020 MGMT_STATUS_FAILED
);
4021 mgmt_pending_remove(cmd
);
4025 hci_dev_unlock(hdev
);
4030 static void set_bredr_scan(struct hci_request
*req
)
4032 struct hci_dev
*hdev
= req
->hdev
;
4035 /* Ensure that fast connectable is disabled. This function will
4036 * not do anything if the page scan parameters are already what
4039 write_fast_connectable(req
, false);
4041 if (test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
4043 if (test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
))
4044 scan
|= SCAN_INQUIRY
;
4047 hci_req_add(req
, HCI_OP_WRITE_SCAN_ENABLE
, 1, &scan
);
4050 static void set_bredr_complete(struct hci_dev
*hdev
, u8 status
)
4052 struct pending_cmd
*cmd
;
4054 BT_DBG("status 0x%02x", status
);
4058 cmd
= mgmt_pending_find(MGMT_OP_SET_BREDR
, hdev
);
4063 u8 mgmt_err
= mgmt_status(status
);
4065 /* We need to restore the flag if related HCI commands
4068 clear_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
);
4070 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_err
);
4072 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_BREDR
, hdev
);
4073 new_settings(hdev
, cmd
->sk
);
4076 mgmt_pending_remove(cmd
);
4079 hci_dev_unlock(hdev
);
4082 static int set_bredr(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
4084 struct mgmt_mode
*cp
= data
;
4085 struct pending_cmd
*cmd
;
4086 struct hci_request req
;
4089 BT_DBG("request for %s", hdev
->name
);
4091 if (!lmp_bredr_capable(hdev
) || !lmp_le_capable(hdev
))
4092 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4093 MGMT_STATUS_NOT_SUPPORTED
);
4095 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
4096 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4097 MGMT_STATUS_REJECTED
);
4099 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
4100 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4101 MGMT_STATUS_INVALID_PARAMS
);
4105 if (cp
->val
== test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
4106 err
= send_settings_rsp(sk
, MGMT_OP_SET_BREDR
, hdev
);
4110 if (!hdev_is_powered(hdev
)) {
4112 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4113 clear_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
);
4114 clear_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
);
4115 clear_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
);
4116 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
4119 change_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
);
4121 err
= send_settings_rsp(sk
, MGMT_OP_SET_BREDR
, hdev
);
4125 err
= new_settings(hdev
, sk
);
4129 /* Reject disabling when powered on */
4131 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4132 MGMT_STATUS_REJECTED
);
4136 if (mgmt_pending_find(MGMT_OP_SET_BREDR
, hdev
)) {
4137 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4142 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_BREDR
, hdev
, data
, len
);
4148 /* We need to flip the bit already here so that update_adv_data
4149 * generates the correct flags.
4151 set_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
);
4153 hci_req_init(&req
, hdev
);
4155 if (test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
4156 set_bredr_scan(&req
);
4158 /* Since only the advertising data flags will change, there
4159 * is no need to update the scan response data.
4161 update_adv_data(&req
);
4163 err
= hci_req_run(&req
, set_bredr_complete
);
4165 mgmt_pending_remove(cmd
);
4168 hci_dev_unlock(hdev
);
4172 static int set_secure_conn(struct sock
*sk
, struct hci_dev
*hdev
,
4173 void *data
, u16 len
)
4175 struct mgmt_mode
*cp
= data
;
4176 struct pending_cmd
*cmd
;
4180 BT_DBG("request for %s", hdev
->name
);
4182 status
= mgmt_bredr_support(hdev
);
4184 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4187 if (!lmp_sc_capable(hdev
) &&
4188 !test_bit(HCI_FORCE_SC
, &hdev
->dev_flags
))
4189 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4190 MGMT_STATUS_NOT_SUPPORTED
);
4192 if (cp
->val
!= 0x00 && cp
->val
!= 0x01 && cp
->val
!= 0x02)
4193 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4194 MGMT_STATUS_INVALID_PARAMS
);
4198 if (!hdev_is_powered(hdev
)) {
4202 changed
= !test_and_set_bit(HCI_SC_ENABLED
,
4204 if (cp
->val
== 0x02)
4205 set_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4207 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4209 changed
= test_and_clear_bit(HCI_SC_ENABLED
,
4211 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4214 err
= send_settings_rsp(sk
, MGMT_OP_SET_SECURE_CONN
, hdev
);
4219 err
= new_settings(hdev
, sk
);
4224 if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN
, hdev
)) {
4225 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4232 if (val
== test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
) &&
4233 (cp
->val
== 0x02) == test_bit(HCI_SC_ONLY
, &hdev
->dev_flags
)) {
4234 err
= send_settings_rsp(sk
, MGMT_OP_SET_SECURE_CONN
, hdev
);
4238 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_SECURE_CONN
, hdev
, data
, len
);
4244 err
= hci_send_cmd(hdev
, HCI_OP_WRITE_SC_SUPPORT
, 1, &val
);
4246 mgmt_pending_remove(cmd
);
4250 if (cp
->val
== 0x02)
4251 set_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4253 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4256 hci_dev_unlock(hdev
);
4260 static int set_debug_keys(struct sock
*sk
, struct hci_dev
*hdev
,
4261 void *data
, u16 len
)
4263 struct mgmt_mode
*cp
= data
;
4267 BT_DBG("request for %s", hdev
->name
);
4269 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
4270 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEBUG_KEYS
,
4271 MGMT_STATUS_INVALID_PARAMS
);
4276 changed
= !test_and_set_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
4278 changed
= test_and_clear_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
4280 err
= send_settings_rsp(sk
, MGMT_OP_SET_DEBUG_KEYS
, hdev
);
4285 err
= new_settings(hdev
, sk
);
4288 hci_dev_unlock(hdev
);
4292 static int set_privacy(struct sock
*sk
, struct hci_dev
*hdev
, void *cp_data
,
4295 struct mgmt_cp_set_privacy
*cp
= cp_data
;
4299 BT_DBG("request for %s", hdev
->name
);
4301 if (!lmp_le_capable(hdev
))
4302 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_PRIVACY
,
4303 MGMT_STATUS_NOT_SUPPORTED
);
4305 if (cp
->privacy
!= 0x00 && cp
->privacy
!= 0x01)
4306 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_PRIVACY
,
4307 MGMT_STATUS_INVALID_PARAMS
);
4309 if (hdev_is_powered(hdev
))
4310 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_PRIVACY
,
4311 MGMT_STATUS_REJECTED
);
4315 /* If user space supports this command it is also expected to
4316 * handle IRKs. Therefore, set the HCI_RPA_RESOLVING flag.
4318 set_bit(HCI_RPA_RESOLVING
, &hdev
->dev_flags
);
4321 changed
= !test_and_set_bit(HCI_PRIVACY
, &hdev
->dev_flags
);
4322 memcpy(hdev
->irk
, cp
->irk
, sizeof(hdev
->irk
));
4323 set_bit(HCI_RPA_EXPIRED
, &hdev
->dev_flags
);
4325 changed
= test_and_clear_bit(HCI_PRIVACY
, &hdev
->dev_flags
);
4326 memset(hdev
->irk
, 0, sizeof(hdev
->irk
));
4327 clear_bit(HCI_RPA_EXPIRED
, &hdev
->dev_flags
);
4330 err
= send_settings_rsp(sk
, MGMT_OP_SET_PRIVACY
, hdev
);
4335 err
= new_settings(hdev
, sk
);
4338 hci_dev_unlock(hdev
);
4342 static bool irk_is_valid(struct mgmt_irk_info
*irk
)
4344 switch (irk
->addr
.type
) {
4345 case BDADDR_LE_PUBLIC
:
4348 case BDADDR_LE_RANDOM
:
4349 /* Two most significant bits shall be set */
4350 if ((irk
->addr
.bdaddr
.b
[5] & 0xc0) != 0xc0)
4358 static int load_irks(struct sock
*sk
, struct hci_dev
*hdev
, void *cp_data
,
4361 struct mgmt_cp_load_irks
*cp
= cp_data
;
4362 u16 irk_count
, expected_len
;
4365 BT_DBG("request for %s", hdev
->name
);
4367 if (!lmp_le_capable(hdev
))
4368 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_IRKS
,
4369 MGMT_STATUS_NOT_SUPPORTED
);
4371 irk_count
= __le16_to_cpu(cp
->irk_count
);
4373 expected_len
= sizeof(*cp
) + irk_count
* sizeof(struct mgmt_irk_info
);
4374 if (expected_len
!= len
) {
4375 BT_ERR("load_irks: expected %u bytes, got %u bytes",
4377 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_IRKS
,
4378 MGMT_STATUS_INVALID_PARAMS
);
4381 BT_DBG("%s irk_count %u", hdev
->name
, irk_count
);
4383 for (i
= 0; i
< irk_count
; i
++) {
4384 struct mgmt_irk_info
*key
= &cp
->irks
[i
];
4386 if (!irk_is_valid(key
))
4387 return cmd_status(sk
, hdev
->id
,
4389 MGMT_STATUS_INVALID_PARAMS
);
4394 hci_smp_irks_clear(hdev
);
4396 for (i
= 0; i
< irk_count
; i
++) {
4397 struct mgmt_irk_info
*irk
= &cp
->irks
[i
];
4400 if (irk
->addr
.type
== BDADDR_LE_PUBLIC
)
4401 addr_type
= ADDR_LE_DEV_PUBLIC
;
4403 addr_type
= ADDR_LE_DEV_RANDOM
;
4405 hci_add_irk(hdev
, &irk
->addr
.bdaddr
, addr_type
, irk
->val
,
4409 set_bit(HCI_RPA_RESOLVING
, &hdev
->dev_flags
);
4411 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_LOAD_IRKS
, 0, NULL
, 0);
4413 hci_dev_unlock(hdev
);
4418 static bool ltk_is_valid(struct mgmt_ltk_info
*key
)
4420 if (key
->master
!= 0x00 && key
->master
!= 0x01)
4423 switch (key
->addr
.type
) {
4424 case BDADDR_LE_PUBLIC
:
4427 case BDADDR_LE_RANDOM
:
4428 /* Two most significant bits shall be set */
4429 if ((key
->addr
.bdaddr
.b
[5] & 0xc0) != 0xc0)
4437 static int load_long_term_keys(struct sock
*sk
, struct hci_dev
*hdev
,
4438 void *cp_data
, u16 len
)
4440 struct mgmt_cp_load_long_term_keys
*cp
= cp_data
;
4441 u16 key_count
, expected_len
;
4444 BT_DBG("request for %s", hdev
->name
);
4446 if (!lmp_le_capable(hdev
))
4447 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LONG_TERM_KEYS
,
4448 MGMT_STATUS_NOT_SUPPORTED
);
4450 key_count
= __le16_to_cpu(cp
->key_count
);
4452 expected_len
= sizeof(*cp
) + key_count
*
4453 sizeof(struct mgmt_ltk_info
);
4454 if (expected_len
!= len
) {
4455 BT_ERR("load_keys: expected %u bytes, got %u bytes",
4457 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LONG_TERM_KEYS
,
4458 MGMT_STATUS_INVALID_PARAMS
);
4461 BT_DBG("%s key_count %u", hdev
->name
, key_count
);
4463 for (i
= 0; i
< key_count
; i
++) {
4464 struct mgmt_ltk_info
*key
= &cp
->keys
[i
];
4466 if (!ltk_is_valid(key
))
4467 return cmd_status(sk
, hdev
->id
,
4468 MGMT_OP_LOAD_LONG_TERM_KEYS
,
4469 MGMT_STATUS_INVALID_PARAMS
);
4474 hci_smp_ltks_clear(hdev
);
4476 for (i
= 0; i
< key_count
; i
++) {
4477 struct mgmt_ltk_info
*key
= &cp
->keys
[i
];
4480 if (key
->addr
.type
== BDADDR_LE_PUBLIC
)
4481 addr_type
= ADDR_LE_DEV_PUBLIC
;
4483 addr_type
= ADDR_LE_DEV_RANDOM
;
4488 type
= HCI_SMP_LTK_SLAVE
;
4490 hci_add_ltk(hdev
, &key
->addr
.bdaddr
, addr_type
, type
,
4491 key
->type
, key
->val
, key
->enc_size
, key
->ediv
,
4495 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_LOAD_LONG_TERM_KEYS
, 0,
4498 hci_dev_unlock(hdev
);
4503 static const struct mgmt_handler
{
4504 int (*func
) (struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
4508 } mgmt_handlers
[] = {
4509 { NULL
}, /* 0x0000 (no command) */
4510 { read_version
, false, MGMT_READ_VERSION_SIZE
},
4511 { read_commands
, false, MGMT_READ_COMMANDS_SIZE
},
4512 { read_index_list
, false, MGMT_READ_INDEX_LIST_SIZE
},
4513 { read_controller_info
, false, MGMT_READ_INFO_SIZE
},
4514 { set_powered
, false, MGMT_SETTING_SIZE
},
4515 { set_discoverable
, false, MGMT_SET_DISCOVERABLE_SIZE
},
4516 { set_connectable
, false, MGMT_SETTING_SIZE
},
4517 { set_fast_connectable
, false, MGMT_SETTING_SIZE
},
4518 { set_pairable
, false, MGMT_SETTING_SIZE
},
4519 { set_link_security
, false, MGMT_SETTING_SIZE
},
4520 { set_ssp
, false, MGMT_SETTING_SIZE
},
4521 { set_hs
, false, MGMT_SETTING_SIZE
},
4522 { set_le
, false, MGMT_SETTING_SIZE
},
4523 { set_dev_class
, false, MGMT_SET_DEV_CLASS_SIZE
},
4524 { set_local_name
, false, MGMT_SET_LOCAL_NAME_SIZE
},
4525 { add_uuid
, false, MGMT_ADD_UUID_SIZE
},
4526 { remove_uuid
, false, MGMT_REMOVE_UUID_SIZE
},
4527 { load_link_keys
, true, MGMT_LOAD_LINK_KEYS_SIZE
},
4528 { load_long_term_keys
, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE
},
4529 { disconnect
, false, MGMT_DISCONNECT_SIZE
},
4530 { get_connections
, false, MGMT_GET_CONNECTIONS_SIZE
},
4531 { pin_code_reply
, false, MGMT_PIN_CODE_REPLY_SIZE
},
4532 { pin_code_neg_reply
, false, MGMT_PIN_CODE_NEG_REPLY_SIZE
},
4533 { set_io_capability
, false, MGMT_SET_IO_CAPABILITY_SIZE
},
4534 { pair_device
, false, MGMT_PAIR_DEVICE_SIZE
},
4535 { cancel_pair_device
, false, MGMT_CANCEL_PAIR_DEVICE_SIZE
},
4536 { unpair_device
, false, MGMT_UNPAIR_DEVICE_SIZE
},
4537 { user_confirm_reply
, false, MGMT_USER_CONFIRM_REPLY_SIZE
},
4538 { user_confirm_neg_reply
, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE
},
4539 { user_passkey_reply
, false, MGMT_USER_PASSKEY_REPLY_SIZE
},
4540 { user_passkey_neg_reply
, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE
},
4541 { read_local_oob_data
, false, MGMT_READ_LOCAL_OOB_DATA_SIZE
},
4542 { add_remote_oob_data
, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE
},
4543 { remove_remote_oob_data
, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE
},
4544 { start_discovery
, false, MGMT_START_DISCOVERY_SIZE
},
4545 { stop_discovery
, false, MGMT_STOP_DISCOVERY_SIZE
},
4546 { confirm_name
, false, MGMT_CONFIRM_NAME_SIZE
},
4547 { block_device
, false, MGMT_BLOCK_DEVICE_SIZE
},
4548 { unblock_device
, false, MGMT_UNBLOCK_DEVICE_SIZE
},
4549 { set_device_id
, false, MGMT_SET_DEVICE_ID_SIZE
},
4550 { set_advertising
, false, MGMT_SETTING_SIZE
},
4551 { set_bredr
, false, MGMT_SETTING_SIZE
},
4552 { set_static_address
, false, MGMT_SET_STATIC_ADDRESS_SIZE
},
4553 { set_scan_params
, false, MGMT_SET_SCAN_PARAMS_SIZE
},
4554 { set_secure_conn
, false, MGMT_SETTING_SIZE
},
4555 { set_debug_keys
, false, MGMT_SETTING_SIZE
},
4556 { set_privacy
, false, MGMT_SET_PRIVACY_SIZE
},
4557 { load_irks
, true, MGMT_LOAD_IRKS_SIZE
},
4561 int mgmt_control(struct sock
*sk
, struct msghdr
*msg
, size_t msglen
)
4565 struct mgmt_hdr
*hdr
;
4566 u16 opcode
, index
, len
;
4567 struct hci_dev
*hdev
= NULL
;
4568 const struct mgmt_handler
*handler
;
4571 BT_DBG("got %zu bytes", msglen
);
4573 if (msglen
< sizeof(*hdr
))
4576 buf
= kmalloc(msglen
, GFP_KERNEL
);
4580 if (memcpy_fromiovec(buf
, msg
->msg_iov
, msglen
)) {
4586 opcode
= __le16_to_cpu(hdr
->opcode
);
4587 index
= __le16_to_cpu(hdr
->index
);
4588 len
= __le16_to_cpu(hdr
->len
);
4590 if (len
!= msglen
- sizeof(*hdr
)) {
4595 if (index
!= MGMT_INDEX_NONE
) {
4596 hdev
= hci_dev_get(index
);
4598 err
= cmd_status(sk
, index
, opcode
,
4599 MGMT_STATUS_INVALID_INDEX
);
4603 if (test_bit(HCI_SETUP
, &hdev
->dev_flags
) ||
4604 test_bit(HCI_USER_CHANNEL
, &hdev
->dev_flags
)) {
4605 err
= cmd_status(sk
, index
, opcode
,
4606 MGMT_STATUS_INVALID_INDEX
);
4611 if (opcode
>= ARRAY_SIZE(mgmt_handlers
) ||
4612 mgmt_handlers
[opcode
].func
== NULL
) {
4613 BT_DBG("Unknown op %u", opcode
);
4614 err
= cmd_status(sk
, index
, opcode
,
4615 MGMT_STATUS_UNKNOWN_COMMAND
);
4619 if ((hdev
&& opcode
< MGMT_OP_READ_INFO
) ||
4620 (!hdev
&& opcode
>= MGMT_OP_READ_INFO
)) {
4621 err
= cmd_status(sk
, index
, opcode
,
4622 MGMT_STATUS_INVALID_INDEX
);
4626 handler
= &mgmt_handlers
[opcode
];
4628 if ((handler
->var_len
&& len
< handler
->data_len
) ||
4629 (!handler
->var_len
&& len
!= handler
->data_len
)) {
4630 err
= cmd_status(sk
, index
, opcode
,
4631 MGMT_STATUS_INVALID_PARAMS
);
4636 mgmt_init_hdev(sk
, hdev
);
4638 cp
= buf
+ sizeof(*hdr
);
4640 err
= handler
->func(sk
, hdev
, cp
, len
);
4654 void mgmt_index_added(struct hci_dev
*hdev
)
4656 if (hdev
->dev_type
!= HCI_BREDR
)
4659 mgmt_event(MGMT_EV_INDEX_ADDED
, hdev
, NULL
, 0, NULL
);
4662 void mgmt_index_removed(struct hci_dev
*hdev
)
4664 u8 status
= MGMT_STATUS_INVALID_INDEX
;
4666 if (hdev
->dev_type
!= HCI_BREDR
)
4669 mgmt_pending_foreach(0, hdev
, cmd_status_rsp
, &status
);
4671 mgmt_event(MGMT_EV_INDEX_REMOVED
, hdev
, NULL
, 0, NULL
);
4674 /* This function requires the caller holds hdev->lock */
4675 static void restart_le_auto_conns(struct hci_dev
*hdev
)
4677 struct hci_conn_params
*p
;
4679 list_for_each_entry(p
, &hdev
->le_conn_params
, list
) {
4680 if (p
->auto_connect
== HCI_AUTO_CONN_ALWAYS
)
4681 hci_pend_le_conn_add(hdev
, &p
->addr
, p
->addr_type
);
4685 static void powered_complete(struct hci_dev
*hdev
, u8 status
)
4687 struct cmd_lookup match
= { NULL
, hdev
};
4689 BT_DBG("status 0x%02x", status
);
4693 restart_le_auto_conns(hdev
);
4695 mgmt_pending_foreach(MGMT_OP_SET_POWERED
, hdev
, settings_rsp
, &match
);
4697 new_settings(hdev
, match
.sk
);
4699 hci_dev_unlock(hdev
);
4705 static int powered_update_hci(struct hci_dev
*hdev
)
4707 struct hci_request req
;
4710 hci_req_init(&req
, hdev
);
4712 if (test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
) &&
4713 !lmp_host_ssp_capable(hdev
)) {
4716 hci_req_add(&req
, HCI_OP_WRITE_SSP_MODE
, 1, &ssp
);
4719 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
) &&
4720 lmp_bredr_capable(hdev
)) {
4721 struct hci_cp_write_le_host_supported cp
;
4724 cp
.simul
= lmp_le_br_capable(hdev
);
4726 /* Check first if we already have the right
4727 * host state (host features set)
4729 if (cp
.le
!= lmp_host_le_capable(hdev
) ||
4730 cp
.simul
!= lmp_host_le_br_capable(hdev
))
4731 hci_req_add(&req
, HCI_OP_WRITE_LE_HOST_SUPPORTED
,
4735 if (lmp_le_capable(hdev
)) {
4736 /* Make sure the controller has a good default for
4737 * advertising data. This also applies to the case
4738 * where BR/EDR was toggled during the AUTO_OFF phase.
4740 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
)) {
4741 update_adv_data(&req
);
4742 update_scan_rsp_data(&req
);
4745 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
4746 enable_advertising(&req
);
4749 link_sec
= test_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
);
4750 if (link_sec
!= test_bit(HCI_AUTH
, &hdev
->flags
))
4751 hci_req_add(&req
, HCI_OP_WRITE_AUTH_ENABLE
,
4752 sizeof(link_sec
), &link_sec
);
4754 if (lmp_bredr_capable(hdev
)) {
4755 if (test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
4756 set_bredr_scan(&req
);
4762 return hci_req_run(&req
, powered_complete
);
4765 int mgmt_powered(struct hci_dev
*hdev
, u8 powered
)
4767 struct cmd_lookup match
= { NULL
, hdev
};
4768 u8 status_not_powered
= MGMT_STATUS_NOT_POWERED
;
4769 u8 zero_cod
[] = { 0, 0, 0 };
4772 if (!test_bit(HCI_MGMT
, &hdev
->dev_flags
))
4776 if (powered_update_hci(hdev
) == 0)
4779 mgmt_pending_foreach(MGMT_OP_SET_POWERED
, hdev
, settings_rsp
,
4784 mgmt_pending_foreach(MGMT_OP_SET_POWERED
, hdev
, settings_rsp
, &match
);
4785 mgmt_pending_foreach(0, hdev
, cmd_status_rsp
, &status_not_powered
);
4787 if (memcmp(hdev
->dev_class
, zero_cod
, sizeof(zero_cod
)) != 0)
4788 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED
, hdev
,
4789 zero_cod
, sizeof(zero_cod
), NULL
);
4792 err
= new_settings(hdev
, match
.sk
);
4800 void mgmt_set_powered_failed(struct hci_dev
*hdev
, int err
)
4802 struct pending_cmd
*cmd
;
4805 cmd
= mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
);
4809 if (err
== -ERFKILL
)
4810 status
= MGMT_STATUS_RFKILLED
;
4812 status
= MGMT_STATUS_FAILED
;
4814 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_SET_POWERED
, status
);
4816 mgmt_pending_remove(cmd
);
4819 void mgmt_discoverable_timeout(struct hci_dev
*hdev
)
4821 struct hci_request req
;
4825 /* When discoverable timeout triggers, then just make sure
4826 * the limited discoverable flag is cleared. Even in the case
4827 * of a timeout triggered from general discoverable, it is
4828 * safe to unconditionally clear the flag.
4830 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
4831 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4833 hci_req_init(&req
, hdev
);
4834 if (test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
4835 u8 scan
= SCAN_PAGE
;
4836 hci_req_add(&req
, HCI_OP_WRITE_SCAN_ENABLE
,
4837 sizeof(scan
), &scan
);
4840 update_adv_data(&req
);
4841 hci_req_run(&req
, NULL
);
4843 hdev
->discov_timeout
= 0;
4845 new_settings(hdev
, NULL
);
4847 hci_dev_unlock(hdev
);
4850 void mgmt_discoverable(struct hci_dev
*hdev
, u8 discoverable
)
4854 /* Nothing needed here if there's a pending command since that
4855 * commands request completion callback takes care of everything
4858 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
))
4861 /* Powering off may clear the scan mode - don't let that interfere */
4862 if (!discoverable
&& mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
))
4866 changed
= !test_and_set_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4868 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
4869 changed
= test_and_clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4873 struct hci_request req
;
4875 /* In case this change in discoverable was triggered by
4876 * a disabling of connectable there could be a need to
4877 * update the advertising flags.
4879 hci_req_init(&req
, hdev
);
4880 update_adv_data(&req
);
4881 hci_req_run(&req
, NULL
);
4883 new_settings(hdev
, NULL
);
4887 void mgmt_connectable(struct hci_dev
*hdev
, u8 connectable
)
4891 /* Nothing needed here if there's a pending command since that
4892 * commands request completion callback takes care of everything
4895 if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
))
4898 /* Powering off may clear the scan mode - don't let that interfere */
4899 if (!connectable
&& mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
))
4903 changed
= !test_and_set_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
4905 changed
= test_and_clear_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
4908 new_settings(hdev
, NULL
);
4911 void mgmt_advertising(struct hci_dev
*hdev
, u8 advertising
)
4913 /* Powering off may stop advertising - don't let that interfere */
4914 if (!advertising
&& mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
))
4918 set_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
4920 clear_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
4923 void mgmt_write_scan_failed(struct hci_dev
*hdev
, u8 scan
, u8 status
)
4925 u8 mgmt_err
= mgmt_status(status
);
4927 if (scan
& SCAN_PAGE
)
4928 mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE
, hdev
,
4929 cmd_status_rsp
, &mgmt_err
);
4931 if (scan
& SCAN_INQUIRY
)
4932 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE
, hdev
,
4933 cmd_status_rsp
, &mgmt_err
);
4936 void mgmt_new_link_key(struct hci_dev
*hdev
, struct link_key
*key
,
4939 struct mgmt_ev_new_link_key ev
;
4941 memset(&ev
, 0, sizeof(ev
));
4943 ev
.store_hint
= persistent
;
4944 bacpy(&ev
.key
.addr
.bdaddr
, &key
->bdaddr
);
4945 ev
.key
.addr
.type
= BDADDR_BREDR
;
4946 ev
.key
.type
= key
->type
;
4947 memcpy(ev
.key
.val
, key
->val
, HCI_LINK_KEY_SIZE
);
4948 ev
.key
.pin_len
= key
->pin_len
;
4950 mgmt_event(MGMT_EV_NEW_LINK_KEY
, hdev
, &ev
, sizeof(ev
), NULL
);
4953 void mgmt_new_ltk(struct hci_dev
*hdev
, struct smp_ltk
*key
)
4955 struct mgmt_ev_new_long_term_key ev
;
4957 memset(&ev
, 0, sizeof(ev
));
4959 /* Devices using resolvable or non-resolvable random addresses
4960 * without providing an indentity resolving key don't require
4961 * to store long term keys. Their addresses will change the
4964 * Only when a remote device provides an identity address
4965 * make sure the long term key is stored. If the remote
4966 * identity is known, the long term keys are internally
4967 * mapped to the identity address. So allow static random
4968 * and public addresses here.
4970 if (key
->bdaddr_type
== ADDR_LE_DEV_RANDOM
&&
4971 (key
->bdaddr
.b
[5] & 0xc0) != 0xc0)
4972 ev
.store_hint
= 0x00;
4974 ev
.store_hint
= 0x01;
4976 bacpy(&ev
.key
.addr
.bdaddr
, &key
->bdaddr
);
4977 ev
.key
.addr
.type
= link_to_bdaddr(LE_LINK
, key
->bdaddr_type
);
4978 ev
.key
.type
= key
->authenticated
;
4979 ev
.key
.enc_size
= key
->enc_size
;
4980 ev
.key
.ediv
= key
->ediv
;
4982 if (key
->type
== HCI_SMP_LTK
)
4985 memcpy(ev
.key
.rand
, key
->rand
, sizeof(key
->rand
));
4986 memcpy(ev
.key
.val
, key
->val
, sizeof(key
->val
));
4988 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY
, hdev
, &ev
, sizeof(ev
), NULL
);
4991 void mgmt_new_irk(struct hci_dev
*hdev
, struct smp_irk
*irk
)
4993 struct mgmt_ev_new_irk ev
;
4995 memset(&ev
, 0, sizeof(ev
));
4997 /* For identity resolving keys from devices that are already
4998 * using a public address or static random address, do not
4999 * ask for storing this key. The identity resolving key really
5000 * is only mandatory for devices using resovlable random
5003 * Storing all identity resolving keys has the downside that
5004 * they will be also loaded on next boot of they system. More
5005 * identity resolving keys, means more time during scanning is
5006 * needed to actually resolve these addresses.
5008 if (bacmp(&irk
->rpa
, BDADDR_ANY
))
5009 ev
.store_hint
= 0x01;
5011 ev
.store_hint
= 0x00;
5013 bacpy(&ev
.rpa
, &irk
->rpa
);
5014 bacpy(&ev
.irk
.addr
.bdaddr
, &irk
->bdaddr
);
5015 ev
.irk
.addr
.type
= link_to_bdaddr(LE_LINK
, irk
->addr_type
);
5016 memcpy(ev
.irk
.val
, irk
->val
, sizeof(irk
->val
));
5018 mgmt_event(MGMT_EV_NEW_IRK
, hdev
, &ev
, sizeof(ev
), NULL
);
5021 static inline u16
eir_append_data(u8
*eir
, u16 eir_len
, u8 type
, u8
*data
,
5024 eir
[eir_len
++] = sizeof(type
) + data_len
;
5025 eir
[eir_len
++] = type
;
5026 memcpy(&eir
[eir_len
], data
, data_len
);
5027 eir_len
+= data_len
;
5032 void mgmt_device_connected(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5033 u8 addr_type
, u32 flags
, u8
*name
, u8 name_len
,
5037 struct mgmt_ev_device_connected
*ev
= (void *) buf
;
5040 bacpy(&ev
->addr
.bdaddr
, bdaddr
);
5041 ev
->addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5043 ev
->flags
= __cpu_to_le32(flags
);
5046 eir_len
= eir_append_data(ev
->eir
, 0, EIR_NAME_COMPLETE
,
5049 if (dev_class
&& memcmp(dev_class
, "\0\0\0", 3) != 0)
5050 eir_len
= eir_append_data(ev
->eir
, eir_len
,
5051 EIR_CLASS_OF_DEV
, dev_class
, 3);
5053 ev
->eir_len
= cpu_to_le16(eir_len
);
5055 mgmt_event(MGMT_EV_DEVICE_CONNECTED
, hdev
, buf
,
5056 sizeof(*ev
) + eir_len
, NULL
);
5059 static void disconnect_rsp(struct pending_cmd
*cmd
, void *data
)
5061 struct mgmt_cp_disconnect
*cp
= cmd
->param
;
5062 struct sock
**sk
= data
;
5063 struct mgmt_rp_disconnect rp
;
5065 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
5066 rp
.addr
.type
= cp
->addr
.type
;
5068 cmd_complete(cmd
->sk
, cmd
->index
, MGMT_OP_DISCONNECT
, 0, &rp
,
5074 mgmt_pending_remove(cmd
);
5077 static void unpair_device_rsp(struct pending_cmd
*cmd
, void *data
)
5079 struct hci_dev
*hdev
= data
;
5080 struct mgmt_cp_unpair_device
*cp
= cmd
->param
;
5081 struct mgmt_rp_unpair_device rp
;
5083 memset(&rp
, 0, sizeof(rp
));
5084 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
5085 rp
.addr
.type
= cp
->addr
.type
;
5087 device_unpaired(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
, cmd
->sk
);
5089 cmd_complete(cmd
->sk
, cmd
->index
, cmd
->opcode
, 0, &rp
, sizeof(rp
));
5091 mgmt_pending_remove(cmd
);
5094 void mgmt_device_disconnected(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5095 u8 link_type
, u8 addr_type
, u8 reason
,
5096 bool mgmt_connected
)
5098 struct mgmt_ev_device_disconnected ev
;
5099 struct pending_cmd
*power_off
;
5100 struct sock
*sk
= NULL
;
5102 power_off
= mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
);
5104 struct mgmt_mode
*cp
= power_off
->param
;
5106 /* The connection is still in hci_conn_hash so test for 1
5107 * instead of 0 to know if this is the last one.
5109 if (!cp
->val
&& hci_conn_count(hdev
) == 1)
5110 queue_work(hdev
->req_workqueue
, &hdev
->power_off
.work
);
5113 if (!mgmt_connected
)
5116 if (link_type
!= ACL_LINK
&& link_type
!= LE_LINK
)
5119 mgmt_pending_foreach(MGMT_OP_DISCONNECT
, hdev
, disconnect_rsp
, &sk
);
5121 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5122 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5125 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED
, hdev
, &ev
, sizeof(ev
), sk
);
5130 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE
, hdev
, unpair_device_rsp
,
5134 void mgmt_disconnect_failed(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5135 u8 link_type
, u8 addr_type
, u8 status
)
5137 u8 bdaddr_type
= link_to_bdaddr(link_type
, addr_type
);
5138 struct mgmt_cp_disconnect
*cp
;
5139 struct mgmt_rp_disconnect rp
;
5140 struct pending_cmd
*cmd
;
5142 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE
, hdev
, unpair_device_rsp
,
5145 cmd
= mgmt_pending_find(MGMT_OP_DISCONNECT
, hdev
);
5151 if (bacmp(bdaddr
, &cp
->addr
.bdaddr
))
5154 if (cp
->addr
.type
!= bdaddr_type
)
5157 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5158 rp
.addr
.type
= bdaddr_type
;
5160 cmd_complete(cmd
->sk
, cmd
->index
, MGMT_OP_DISCONNECT
,
5161 mgmt_status(status
), &rp
, sizeof(rp
));
5163 mgmt_pending_remove(cmd
);
5166 void mgmt_connect_failed(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5167 u8 addr_type
, u8 status
)
5169 struct mgmt_ev_connect_failed ev
;
5171 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5172 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5173 ev
.status
= mgmt_status(status
);
5175 mgmt_event(MGMT_EV_CONNECT_FAILED
, hdev
, &ev
, sizeof(ev
), NULL
);
5178 void mgmt_pin_code_request(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 secure
)
5180 struct mgmt_ev_pin_code_request ev
;
5182 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5183 ev
.addr
.type
= BDADDR_BREDR
;
5186 mgmt_event(MGMT_EV_PIN_CODE_REQUEST
, hdev
, &ev
, sizeof(ev
), NULL
);
5189 void mgmt_pin_code_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5192 struct pending_cmd
*cmd
;
5193 struct mgmt_rp_pin_code_reply rp
;
5195 cmd
= mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY
, hdev
);
5199 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5200 rp
.addr
.type
= BDADDR_BREDR
;
5202 cmd_complete(cmd
->sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
5203 mgmt_status(status
), &rp
, sizeof(rp
));
5205 mgmt_pending_remove(cmd
);
5208 void mgmt_pin_code_neg_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5211 struct pending_cmd
*cmd
;
5212 struct mgmt_rp_pin_code_reply rp
;
5214 cmd
= mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY
, hdev
);
5218 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5219 rp
.addr
.type
= BDADDR_BREDR
;
5221 cmd_complete(cmd
->sk
, hdev
->id
, MGMT_OP_PIN_CODE_NEG_REPLY
,
5222 mgmt_status(status
), &rp
, sizeof(rp
));
5224 mgmt_pending_remove(cmd
);
5227 int mgmt_user_confirm_request(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5228 u8 link_type
, u8 addr_type
, __le32 value
,
5231 struct mgmt_ev_user_confirm_request ev
;
5233 BT_DBG("%s", hdev
->name
);
5235 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5236 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5237 ev
.confirm_hint
= confirm_hint
;
5240 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST
, hdev
, &ev
, sizeof(ev
),
5244 int mgmt_user_passkey_request(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5245 u8 link_type
, u8 addr_type
)
5247 struct mgmt_ev_user_passkey_request ev
;
5249 BT_DBG("%s", hdev
->name
);
5251 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5252 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5254 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST
, hdev
, &ev
, sizeof(ev
),
5258 static int user_pairing_resp_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5259 u8 link_type
, u8 addr_type
, u8 status
,
5262 struct pending_cmd
*cmd
;
5263 struct mgmt_rp_user_confirm_reply rp
;
5266 cmd
= mgmt_pending_find(opcode
, hdev
);
5270 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5271 rp
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5272 err
= cmd_complete(cmd
->sk
, hdev
->id
, opcode
, mgmt_status(status
),
5275 mgmt_pending_remove(cmd
);
5280 int mgmt_user_confirm_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5281 u8 link_type
, u8 addr_type
, u8 status
)
5283 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5284 status
, MGMT_OP_USER_CONFIRM_REPLY
);
5287 int mgmt_user_confirm_neg_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5288 u8 link_type
, u8 addr_type
, u8 status
)
5290 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5292 MGMT_OP_USER_CONFIRM_NEG_REPLY
);
5295 int mgmt_user_passkey_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5296 u8 link_type
, u8 addr_type
, u8 status
)
5298 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5299 status
, MGMT_OP_USER_PASSKEY_REPLY
);
5302 int mgmt_user_passkey_neg_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5303 u8 link_type
, u8 addr_type
, u8 status
)
5305 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5307 MGMT_OP_USER_PASSKEY_NEG_REPLY
);
5310 int mgmt_user_passkey_notify(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5311 u8 link_type
, u8 addr_type
, u32 passkey
,
5314 struct mgmt_ev_passkey_notify ev
;
5316 BT_DBG("%s", hdev
->name
);
5318 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5319 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5320 ev
.passkey
= __cpu_to_le32(passkey
);
5321 ev
.entered
= entered
;
5323 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY
, hdev
, &ev
, sizeof(ev
), NULL
);
5326 void mgmt_auth_failed(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5327 u8 addr_type
, u8 status
)
5329 struct mgmt_ev_auth_failed ev
;
5331 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5332 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5333 ev
.status
= mgmt_status(status
);
5335 mgmt_event(MGMT_EV_AUTH_FAILED
, hdev
, &ev
, sizeof(ev
), NULL
);
5338 void mgmt_auth_enable_complete(struct hci_dev
*hdev
, u8 status
)
5340 struct cmd_lookup match
= { NULL
, hdev
};
5344 u8 mgmt_err
= mgmt_status(status
);
5345 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY
, hdev
,
5346 cmd_status_rsp
, &mgmt_err
);
5350 if (test_bit(HCI_AUTH
, &hdev
->flags
))
5351 changed
= !test_and_set_bit(HCI_LINK_SECURITY
,
5354 changed
= test_and_clear_bit(HCI_LINK_SECURITY
,
5357 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY
, hdev
, settings_rsp
,
5361 new_settings(hdev
, match
.sk
);
5367 static void clear_eir(struct hci_request
*req
)
5369 struct hci_dev
*hdev
= req
->hdev
;
5370 struct hci_cp_write_eir cp
;
5372 if (!lmp_ext_inq_capable(hdev
))
5375 memset(hdev
->eir
, 0, sizeof(hdev
->eir
));
5377 memset(&cp
, 0, sizeof(cp
));
5379 hci_req_add(req
, HCI_OP_WRITE_EIR
, sizeof(cp
), &cp
);
5382 void mgmt_ssp_enable_complete(struct hci_dev
*hdev
, u8 enable
, u8 status
)
5384 struct cmd_lookup match
= { NULL
, hdev
};
5385 struct hci_request req
;
5386 bool changed
= false;
5389 u8 mgmt_err
= mgmt_status(status
);
5391 if (enable
&& test_and_clear_bit(HCI_SSP_ENABLED
,
5392 &hdev
->dev_flags
)) {
5393 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
5394 new_settings(hdev
, NULL
);
5397 mgmt_pending_foreach(MGMT_OP_SET_SSP
, hdev
, cmd_status_rsp
,
5403 changed
= !test_and_set_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
);
5405 changed
= test_and_clear_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
);
5407 changed
= test_and_clear_bit(HCI_HS_ENABLED
,
5410 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
5413 mgmt_pending_foreach(MGMT_OP_SET_SSP
, hdev
, settings_rsp
, &match
);
5416 new_settings(hdev
, match
.sk
);
5421 hci_req_init(&req
, hdev
);
5423 if (test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
))
5428 hci_req_run(&req
, NULL
);
5431 void mgmt_sc_enable_complete(struct hci_dev
*hdev
, u8 enable
, u8 status
)
5433 struct cmd_lookup match
= { NULL
, hdev
};
5434 bool changed
= false;
5437 u8 mgmt_err
= mgmt_status(status
);
5440 if (test_and_clear_bit(HCI_SC_ENABLED
,
5442 new_settings(hdev
, NULL
);
5443 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
5446 mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN
, hdev
,
5447 cmd_status_rsp
, &mgmt_err
);
5452 changed
= !test_and_set_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
);
5454 changed
= test_and_clear_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
);
5455 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
5458 mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN
, hdev
,
5459 settings_rsp
, &match
);
5462 new_settings(hdev
, match
.sk
);
5468 static void sk_lookup(struct pending_cmd
*cmd
, void *data
)
5470 struct cmd_lookup
*match
= data
;
5472 if (match
->sk
== NULL
) {
5473 match
->sk
= cmd
->sk
;
5474 sock_hold(match
->sk
);
5478 void mgmt_set_class_of_dev_complete(struct hci_dev
*hdev
, u8
*dev_class
,
5481 struct cmd_lookup match
= { NULL
, hdev
, mgmt_status(status
) };
5483 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS
, hdev
, sk_lookup
, &match
);
5484 mgmt_pending_foreach(MGMT_OP_ADD_UUID
, hdev
, sk_lookup
, &match
);
5485 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID
, hdev
, sk_lookup
, &match
);
5488 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED
, hdev
, dev_class
, 3,
5495 void mgmt_set_local_name_complete(struct hci_dev
*hdev
, u8
*name
, u8 status
)
5497 struct mgmt_cp_set_local_name ev
;
5498 struct pending_cmd
*cmd
;
5503 memset(&ev
, 0, sizeof(ev
));
5504 memcpy(ev
.name
, name
, HCI_MAX_NAME_LENGTH
);
5505 memcpy(ev
.short_name
, hdev
->short_name
, HCI_MAX_SHORT_NAME_LENGTH
);
5507 cmd
= mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME
, hdev
);
5509 memcpy(hdev
->dev_name
, name
, sizeof(hdev
->dev_name
));
5511 /* If this is a HCI command related to powering on the
5512 * HCI dev don't send any mgmt signals.
5514 if (mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
))
5518 mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED
, hdev
, &ev
, sizeof(ev
),
5519 cmd
? cmd
->sk
: NULL
);
5522 void mgmt_read_local_oob_data_complete(struct hci_dev
*hdev
, u8
*hash192
,
5523 u8
*randomizer192
, u8
*hash256
,
5524 u8
*randomizer256
, u8 status
)
5526 struct pending_cmd
*cmd
;
5528 BT_DBG("%s status %u", hdev
->name
, status
);
5530 cmd
= mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA
, hdev
);
5535 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
5536 mgmt_status(status
));
5538 if (test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
) &&
5539 hash256
&& randomizer256
) {
5540 struct mgmt_rp_read_local_oob_ext_data rp
;
5542 memcpy(rp
.hash192
, hash192
, sizeof(rp
.hash192
));
5543 memcpy(rp
.randomizer192
, randomizer192
,
5544 sizeof(rp
.randomizer192
));
5546 memcpy(rp
.hash256
, hash256
, sizeof(rp
.hash256
));
5547 memcpy(rp
.randomizer256
, randomizer256
,
5548 sizeof(rp
.randomizer256
));
5550 cmd_complete(cmd
->sk
, hdev
->id
,
5551 MGMT_OP_READ_LOCAL_OOB_DATA
, 0,
5554 struct mgmt_rp_read_local_oob_data rp
;
5556 memcpy(rp
.hash
, hash192
, sizeof(rp
.hash
));
5557 memcpy(rp
.randomizer
, randomizer192
,
5558 sizeof(rp
.randomizer
));
5560 cmd_complete(cmd
->sk
, hdev
->id
,
5561 MGMT_OP_READ_LOCAL_OOB_DATA
, 0,
5566 mgmt_pending_remove(cmd
);
5569 void mgmt_device_found(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5570 u8 addr_type
, u8
*dev_class
, s8 rssi
, u8 cfm_name
, u8
5571 ssp
, u8
*eir
, u16 eir_len
)
5574 struct mgmt_ev_device_found
*ev
= (void *) buf
;
5575 struct smp_irk
*irk
;
5578 if (!hci_discovery_active(hdev
))
5581 /* Leave 5 bytes for a potential CoD field */
5582 if (sizeof(*ev
) + eir_len
+ 5 > sizeof(buf
))
5585 memset(buf
, 0, sizeof(buf
));
5587 irk
= hci_get_irk(hdev
, bdaddr
, addr_type
);
5589 bacpy(&ev
->addr
.bdaddr
, &irk
->bdaddr
);
5590 ev
->addr
.type
= link_to_bdaddr(link_type
, irk
->addr_type
);
5592 bacpy(&ev
->addr
.bdaddr
, bdaddr
);
5593 ev
->addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5598 ev
->flags
|= __constant_cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME
);
5600 ev
->flags
|= __constant_cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING
);
5603 memcpy(ev
->eir
, eir
, eir_len
);
5605 if (dev_class
&& !eir_has_data_type(ev
->eir
, eir_len
, EIR_CLASS_OF_DEV
))
5606 eir_len
= eir_append_data(ev
->eir
, eir_len
, EIR_CLASS_OF_DEV
,
5609 ev
->eir_len
= cpu_to_le16(eir_len
);
5610 ev_size
= sizeof(*ev
) + eir_len
;
5612 mgmt_event(MGMT_EV_DEVICE_FOUND
, hdev
, ev
, ev_size
, NULL
);
5615 void mgmt_remote_name(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5616 u8 addr_type
, s8 rssi
, u8
*name
, u8 name_len
)
5618 struct mgmt_ev_device_found
*ev
;
5619 char buf
[sizeof(*ev
) + HCI_MAX_NAME_LENGTH
+ 2];
5622 ev
= (struct mgmt_ev_device_found
*) buf
;
5624 memset(buf
, 0, sizeof(buf
));
5626 bacpy(&ev
->addr
.bdaddr
, bdaddr
);
5627 ev
->addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5630 eir_len
= eir_append_data(ev
->eir
, 0, EIR_NAME_COMPLETE
, name
,
5633 ev
->eir_len
= cpu_to_le16(eir_len
);
5635 mgmt_event(MGMT_EV_DEVICE_FOUND
, hdev
, ev
, sizeof(*ev
) + eir_len
, NULL
);
5638 void mgmt_discovering(struct hci_dev
*hdev
, u8 discovering
)
5640 struct mgmt_ev_discovering ev
;
5641 struct pending_cmd
*cmd
;
5643 BT_DBG("%s discovering %u", hdev
->name
, discovering
);
5646 cmd
= mgmt_pending_find(MGMT_OP_START_DISCOVERY
, hdev
);
5648 cmd
= mgmt_pending_find(MGMT_OP_STOP_DISCOVERY
, hdev
);
5651 u8 type
= hdev
->discovery
.type
;
5653 cmd_complete(cmd
->sk
, hdev
->id
, cmd
->opcode
, 0, &type
,
5655 mgmt_pending_remove(cmd
);
5658 memset(&ev
, 0, sizeof(ev
));
5659 ev
.type
= hdev
->discovery
.type
;
5660 ev
.discovering
= discovering
;
5662 mgmt_event(MGMT_EV_DISCOVERING
, hdev
, &ev
, sizeof(ev
), NULL
);
5665 int mgmt_device_blocked(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 type
)
5667 struct pending_cmd
*cmd
;
5668 struct mgmt_ev_device_blocked ev
;
5670 cmd
= mgmt_pending_find(MGMT_OP_BLOCK_DEVICE
, hdev
);
5672 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5673 ev
.addr
.type
= type
;
5675 return mgmt_event(MGMT_EV_DEVICE_BLOCKED
, hdev
, &ev
, sizeof(ev
),
5676 cmd
? cmd
->sk
: NULL
);
5679 int mgmt_device_unblocked(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 type
)
5681 struct pending_cmd
*cmd
;
5682 struct mgmt_ev_device_unblocked ev
;
5684 cmd
= mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE
, hdev
);
5686 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5687 ev
.addr
.type
= type
;
5689 return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED
, hdev
, &ev
, sizeof(ev
),
5690 cmd
? cmd
->sk
: NULL
);
5693 static void adv_enable_complete(struct hci_dev
*hdev
, u8 status
)
5695 BT_DBG("%s status %u", hdev
->name
, status
);
5697 /* Clear the advertising mgmt setting if we failed to re-enable it */
5699 clear_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
5700 new_settings(hdev
, NULL
);
5704 void mgmt_reenable_advertising(struct hci_dev
*hdev
)
5706 struct hci_request req
;
5708 if (hci_conn_num(hdev
, LE_LINK
) > 0)
5711 if (!test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
5714 hci_req_init(&req
, hdev
);
5715 enable_advertising(&req
);
5717 /* If this fails we have no option but to let user space know
5718 * that we've disabled advertising.
5720 if (hci_req_run(&req
, adv_enable_complete
) < 0) {
5721 clear_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
5722 new_settings(hdev
, NULL
);