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 u8
get_adv_type(struct hci_dev
*hdev
)
822 struct pending_cmd
*cmd
;
825 /* If there's a pending mgmt command the flag will not yet have
826 * it's final value, so check for this first.
828 cmd
= mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
);
830 struct mgmt_mode
*cp
= cmd
->param
;
831 connectable
= !!cp
->val
;
833 connectable
= test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
836 return connectable
? LE_ADV_IND
: LE_ADV_NONCONN_IND
;
839 static void enable_advertising(struct hci_request
*req
)
841 struct hci_dev
*hdev
= req
->hdev
;
842 struct hci_cp_le_set_adv_param cp
;
843 u8 own_addr_type
, enable
= 0x01;
845 memset(&cp
, 0, sizeof(cp
));
847 if (hci_update_random_address(req
, &own_addr_type
) < 0)
850 cp
.min_interval
= __constant_cpu_to_le16(0x0800);
851 cp
.max_interval
= __constant_cpu_to_le16(0x0800);
852 cp
.type
= get_adv_type(hdev
);
853 cp
.own_address_type
= own_addr_type
;
854 cp
.channel_map
= hdev
->le_adv_channel_map
;
856 hci_req_add(req
, HCI_OP_LE_SET_ADV_PARAM
, sizeof(cp
), &cp
);
858 hci_req_add(req
, HCI_OP_LE_SET_ADV_ENABLE
, sizeof(enable
), &enable
);
861 static void disable_advertising(struct hci_request
*req
)
865 hci_req_add(req
, HCI_OP_LE_SET_ADV_ENABLE
, sizeof(enable
), &enable
);
868 static void service_cache_off(struct work_struct
*work
)
870 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
872 struct hci_request req
;
874 if (!test_and_clear_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
))
877 hci_req_init(&req
, hdev
);
884 hci_dev_unlock(hdev
);
886 hci_req_run(&req
, NULL
);
889 static void rpa_expired(struct work_struct
*work
)
891 struct hci_dev
*hdev
= container_of(work
, struct hci_dev
,
893 struct hci_request req
;
897 set_bit(HCI_RPA_EXPIRED
, &hdev
->dev_flags
);
899 if (!test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
) ||
900 hci_conn_num(hdev
, LE_LINK
) > 0)
903 /* The generation of a new RPA and programming it into the
904 * controller happens in the enable_advertising() function.
907 hci_req_init(&req
, hdev
);
909 disable_advertising(&req
);
910 enable_advertising(&req
);
912 hci_req_run(&req
, NULL
);
915 static void mgmt_init_hdev(struct sock
*sk
, struct hci_dev
*hdev
)
917 if (test_and_set_bit(HCI_MGMT
, &hdev
->dev_flags
))
920 INIT_DELAYED_WORK(&hdev
->service_cache
, service_cache_off
);
921 INIT_DELAYED_WORK(&hdev
->rpa_expired
, rpa_expired
);
923 /* Non-mgmt controlled devices get this bit set
924 * implicitly so that pairing works for them, however
925 * for mgmt we require user-space to explicitly enable
928 clear_bit(HCI_PAIRABLE
, &hdev
->dev_flags
);
931 static int read_controller_info(struct sock
*sk
, struct hci_dev
*hdev
,
932 void *data
, u16 data_len
)
934 struct mgmt_rp_read_info rp
;
936 BT_DBG("sock %p %s", sk
, hdev
->name
);
940 memset(&rp
, 0, sizeof(rp
));
942 bacpy(&rp
.bdaddr
, &hdev
->bdaddr
);
944 rp
.version
= hdev
->hci_ver
;
945 rp
.manufacturer
= cpu_to_le16(hdev
->manufacturer
);
947 rp
.supported_settings
= cpu_to_le32(get_supported_settings(hdev
));
948 rp
.current_settings
= cpu_to_le32(get_current_settings(hdev
));
950 memcpy(rp
.dev_class
, hdev
->dev_class
, 3);
952 memcpy(rp
.name
, hdev
->dev_name
, sizeof(hdev
->dev_name
));
953 memcpy(rp
.short_name
, hdev
->short_name
, sizeof(hdev
->short_name
));
955 hci_dev_unlock(hdev
);
957 return cmd_complete(sk
, hdev
->id
, MGMT_OP_READ_INFO
, 0, &rp
,
961 static void mgmt_pending_free(struct pending_cmd
*cmd
)
968 static struct pending_cmd
*mgmt_pending_add(struct sock
*sk
, u16 opcode
,
969 struct hci_dev
*hdev
, void *data
,
972 struct pending_cmd
*cmd
;
974 cmd
= kmalloc(sizeof(*cmd
), GFP_KERNEL
);
978 cmd
->opcode
= opcode
;
979 cmd
->index
= hdev
->id
;
981 cmd
->param
= kmalloc(len
, GFP_KERNEL
);
988 memcpy(cmd
->param
, data
, len
);
993 list_add(&cmd
->list
, &hdev
->mgmt_pending
);
998 static void mgmt_pending_foreach(u16 opcode
, struct hci_dev
*hdev
,
999 void (*cb
)(struct pending_cmd
*cmd
,
1003 struct pending_cmd
*cmd
, *tmp
;
1005 list_for_each_entry_safe(cmd
, tmp
, &hdev
->mgmt_pending
, list
) {
1006 if (opcode
> 0 && cmd
->opcode
!= opcode
)
1013 static void mgmt_pending_remove(struct pending_cmd
*cmd
)
1015 list_del(&cmd
->list
);
1016 mgmt_pending_free(cmd
);
1019 static int send_settings_rsp(struct sock
*sk
, u16 opcode
, struct hci_dev
*hdev
)
1021 __le32 settings
= cpu_to_le32(get_current_settings(hdev
));
1023 return cmd_complete(sk
, hdev
->id
, opcode
, 0, &settings
,
1027 static int set_powered(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1030 struct mgmt_mode
*cp
= data
;
1031 struct pending_cmd
*cmd
;
1034 BT_DBG("request for %s", hdev
->name
);
1036 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1037 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_POWERED
,
1038 MGMT_STATUS_INVALID_PARAMS
);
1042 if (mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
)) {
1043 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_POWERED
,
1048 if (test_and_clear_bit(HCI_AUTO_OFF
, &hdev
->dev_flags
)) {
1049 cancel_delayed_work(&hdev
->power_off
);
1052 mgmt_pending_add(sk
, MGMT_OP_SET_POWERED
, hdev
,
1054 err
= mgmt_powered(hdev
, 1);
1059 if (!!cp
->val
== hdev_is_powered(hdev
)) {
1060 err
= send_settings_rsp(sk
, MGMT_OP_SET_POWERED
, hdev
);
1064 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_POWERED
, hdev
, data
, len
);
1071 queue_work(hdev
->req_workqueue
, &hdev
->power_on
);
1073 queue_work(hdev
->req_workqueue
, &hdev
->power_off
.work
);
1078 hci_dev_unlock(hdev
);
1082 static int mgmt_event(u16 event
, struct hci_dev
*hdev
, void *data
, u16 data_len
,
1083 struct sock
*skip_sk
)
1085 struct sk_buff
*skb
;
1086 struct mgmt_hdr
*hdr
;
1088 skb
= alloc_skb(sizeof(*hdr
) + data_len
, GFP_KERNEL
);
1092 hdr
= (void *) skb_put(skb
, sizeof(*hdr
));
1093 hdr
->opcode
= cpu_to_le16(event
);
1095 hdr
->index
= cpu_to_le16(hdev
->id
);
1097 hdr
->index
= __constant_cpu_to_le16(MGMT_INDEX_NONE
);
1098 hdr
->len
= cpu_to_le16(data_len
);
1101 memcpy(skb_put(skb
, data_len
), data
, data_len
);
1104 __net_timestamp(skb
);
1106 hci_send_to_control(skb
, skip_sk
);
1112 static int new_settings(struct hci_dev
*hdev
, struct sock
*skip
)
1116 ev
= cpu_to_le32(get_current_settings(hdev
));
1118 return mgmt_event(MGMT_EV_NEW_SETTINGS
, hdev
, &ev
, sizeof(ev
), skip
);
1123 struct hci_dev
*hdev
;
1127 static void settings_rsp(struct pending_cmd
*cmd
, void *data
)
1129 struct cmd_lookup
*match
= data
;
1131 send_settings_rsp(cmd
->sk
, cmd
->opcode
, match
->hdev
);
1133 list_del(&cmd
->list
);
1135 if (match
->sk
== NULL
) {
1136 match
->sk
= cmd
->sk
;
1137 sock_hold(match
->sk
);
1140 mgmt_pending_free(cmd
);
1143 static void cmd_status_rsp(struct pending_cmd
*cmd
, void *data
)
1147 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, *status
);
1148 mgmt_pending_remove(cmd
);
1151 static u8
mgmt_bredr_support(struct hci_dev
*hdev
)
1153 if (!lmp_bredr_capable(hdev
))
1154 return MGMT_STATUS_NOT_SUPPORTED
;
1155 else if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1156 return MGMT_STATUS_REJECTED
;
1158 return MGMT_STATUS_SUCCESS
;
1161 static u8
mgmt_le_support(struct hci_dev
*hdev
)
1163 if (!lmp_le_capable(hdev
))
1164 return MGMT_STATUS_NOT_SUPPORTED
;
1165 else if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
1166 return MGMT_STATUS_REJECTED
;
1168 return MGMT_STATUS_SUCCESS
;
1171 static void set_discoverable_complete(struct hci_dev
*hdev
, u8 status
)
1173 struct pending_cmd
*cmd
;
1174 struct mgmt_mode
*cp
;
1175 struct hci_request req
;
1178 BT_DBG("status 0x%02x", status
);
1182 cmd
= mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
);
1187 u8 mgmt_err
= mgmt_status(status
);
1188 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_err
);
1189 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1195 changed
= !test_and_set_bit(HCI_DISCOVERABLE
,
1198 if (hdev
->discov_timeout
> 0) {
1199 int to
= msecs_to_jiffies(hdev
->discov_timeout
* 1000);
1200 queue_delayed_work(hdev
->workqueue
, &hdev
->discov_off
,
1204 changed
= test_and_clear_bit(HCI_DISCOVERABLE
,
1208 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
);
1211 new_settings(hdev
, cmd
->sk
);
1213 /* When the discoverable mode gets changed, make sure
1214 * that class of device has the limited discoverable
1215 * bit correctly set.
1217 hci_req_init(&req
, hdev
);
1219 hci_req_run(&req
, NULL
);
1222 mgmt_pending_remove(cmd
);
1225 hci_dev_unlock(hdev
);
1228 static int set_discoverable(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1231 struct mgmt_cp_set_discoverable
*cp
= data
;
1232 struct pending_cmd
*cmd
;
1233 struct hci_request req
;
1238 BT_DBG("request for %s", hdev
->name
);
1240 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
) &&
1241 !test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1242 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1243 MGMT_STATUS_REJECTED
);
1245 if (cp
->val
!= 0x00 && cp
->val
!= 0x01 && cp
->val
!= 0x02)
1246 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1247 MGMT_STATUS_INVALID_PARAMS
);
1249 timeout
= __le16_to_cpu(cp
->timeout
);
1251 /* Disabling discoverable requires that no timeout is set,
1252 * and enabling limited discoverable requires a timeout.
1254 if ((cp
->val
== 0x00 && timeout
> 0) ||
1255 (cp
->val
== 0x02 && timeout
== 0))
1256 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1257 MGMT_STATUS_INVALID_PARAMS
);
1261 if (!hdev_is_powered(hdev
) && timeout
> 0) {
1262 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1263 MGMT_STATUS_NOT_POWERED
);
1267 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
) ||
1268 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
)) {
1269 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1274 if (!test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
)) {
1275 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DISCOVERABLE
,
1276 MGMT_STATUS_REJECTED
);
1280 if (!hdev_is_powered(hdev
)) {
1281 bool changed
= false;
1283 /* Setting limited discoverable when powered off is
1284 * not a valid operation since it requires a timeout
1285 * and so no need to check HCI_LIMITED_DISCOVERABLE.
1287 if (!!cp
->val
!= test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
)) {
1288 change_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
1292 err
= send_settings_rsp(sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
);
1297 err
= new_settings(hdev
, sk
);
1302 /* If the current mode is the same, then just update the timeout
1303 * value with the new value. And if only the timeout gets updated,
1304 * then no need for any HCI transactions.
1306 if (!!cp
->val
== test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
) &&
1307 (cp
->val
== 0x02) == test_bit(HCI_LIMITED_DISCOVERABLE
,
1308 &hdev
->dev_flags
)) {
1309 cancel_delayed_work(&hdev
->discov_off
);
1310 hdev
->discov_timeout
= timeout
;
1312 if (cp
->val
&& hdev
->discov_timeout
> 0) {
1313 int to
= msecs_to_jiffies(hdev
->discov_timeout
* 1000);
1314 queue_delayed_work(hdev
->workqueue
, &hdev
->discov_off
,
1318 err
= send_settings_rsp(sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
);
1322 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_DISCOVERABLE
, hdev
, data
, len
);
1328 /* Cancel any potential discoverable timeout that might be
1329 * still active and store new timeout value. The arming of
1330 * the timeout happens in the complete handler.
1332 cancel_delayed_work(&hdev
->discov_off
);
1333 hdev
->discov_timeout
= timeout
;
1335 /* Limited discoverable mode */
1336 if (cp
->val
== 0x02)
1337 set_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1339 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1341 hci_req_init(&req
, hdev
);
1343 /* The procedure for LE-only controllers is much simpler - just
1344 * update the advertising data.
1346 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1352 struct hci_cp_write_current_iac_lap hci_cp
;
1354 if (cp
->val
== 0x02) {
1355 /* Limited discoverable mode */
1356 hci_cp
.num_iac
= min_t(u8
, hdev
->num_iac
, 2);
1357 hci_cp
.iac_lap
[0] = 0x00; /* LIAC */
1358 hci_cp
.iac_lap
[1] = 0x8b;
1359 hci_cp
.iac_lap
[2] = 0x9e;
1360 hci_cp
.iac_lap
[3] = 0x33; /* GIAC */
1361 hci_cp
.iac_lap
[4] = 0x8b;
1362 hci_cp
.iac_lap
[5] = 0x9e;
1364 /* General discoverable mode */
1366 hci_cp
.iac_lap
[0] = 0x33; /* GIAC */
1367 hci_cp
.iac_lap
[1] = 0x8b;
1368 hci_cp
.iac_lap
[2] = 0x9e;
1371 hci_req_add(&req
, HCI_OP_WRITE_CURRENT_IAC_LAP
,
1372 (hci_cp
.num_iac
* 3) + 1, &hci_cp
);
1374 scan
|= SCAN_INQUIRY
;
1376 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1379 hci_req_add(&req
, HCI_OP_WRITE_SCAN_ENABLE
, sizeof(scan
), &scan
);
1382 update_adv_data(&req
);
1384 err
= hci_req_run(&req
, set_discoverable_complete
);
1386 mgmt_pending_remove(cmd
);
1389 hci_dev_unlock(hdev
);
1393 static void write_fast_connectable(struct hci_request
*req
, bool enable
)
1395 struct hci_dev
*hdev
= req
->hdev
;
1396 struct hci_cp_write_page_scan_activity acp
;
1399 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1402 if (hdev
->hci_ver
< BLUETOOTH_VER_1_2
)
1406 type
= PAGE_SCAN_TYPE_INTERLACED
;
1408 /* 160 msec page scan interval */
1409 acp
.interval
= __constant_cpu_to_le16(0x0100);
1411 type
= PAGE_SCAN_TYPE_STANDARD
; /* default */
1413 /* default 1.28 sec page scan */
1414 acp
.interval
= __constant_cpu_to_le16(0x0800);
1417 acp
.window
= __constant_cpu_to_le16(0x0012);
1419 if (__cpu_to_le16(hdev
->page_scan_interval
) != acp
.interval
||
1420 __cpu_to_le16(hdev
->page_scan_window
) != acp
.window
)
1421 hci_req_add(req
, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY
,
1424 if (hdev
->page_scan_type
!= type
)
1425 hci_req_add(req
, HCI_OP_WRITE_PAGE_SCAN_TYPE
, 1, &type
);
1428 static void set_connectable_complete(struct hci_dev
*hdev
, u8 status
)
1430 struct pending_cmd
*cmd
;
1431 struct mgmt_mode
*cp
;
1434 BT_DBG("status 0x%02x", status
);
1438 cmd
= mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
);
1443 u8 mgmt_err
= mgmt_status(status
);
1444 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_err
);
1450 changed
= !test_and_set_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1452 changed
= test_and_clear_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1454 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_CONNECTABLE
, hdev
);
1457 new_settings(hdev
, cmd
->sk
);
1460 mgmt_pending_remove(cmd
);
1463 hci_dev_unlock(hdev
);
1466 static int set_connectable_update_settings(struct hci_dev
*hdev
,
1467 struct sock
*sk
, u8 val
)
1469 bool changed
= false;
1472 if (!!val
!= test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
1476 set_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1478 clear_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
1479 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
1482 err
= send_settings_rsp(sk
, MGMT_OP_SET_CONNECTABLE
, hdev
);
1487 return new_settings(hdev
, sk
);
1492 static int set_connectable(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1495 struct mgmt_mode
*cp
= data
;
1496 struct pending_cmd
*cmd
;
1497 struct hci_request req
;
1501 BT_DBG("request for %s", hdev
->name
);
1503 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
) &&
1504 !test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1505 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_CONNECTABLE
,
1506 MGMT_STATUS_REJECTED
);
1508 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1509 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_CONNECTABLE
,
1510 MGMT_STATUS_INVALID_PARAMS
);
1514 if (!hdev_is_powered(hdev
)) {
1515 err
= set_connectable_update_settings(hdev
, sk
, cp
->val
);
1519 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
) ||
1520 mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
)) {
1521 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_CONNECTABLE
,
1526 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_CONNECTABLE
, hdev
, data
, len
);
1532 hci_req_init(&req
, hdev
);
1534 /* If BR/EDR is not enabled and we disable advertising as a
1535 * by-product of disabling connectable, we need to update the
1536 * advertising flags.
1538 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
1540 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
1541 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
1543 update_adv_data(&req
);
1544 } else if (cp
->val
!= test_bit(HCI_PSCAN
, &hdev
->flags
)) {
1550 if (test_bit(HCI_ISCAN
, &hdev
->flags
) &&
1551 hdev
->discov_timeout
> 0)
1552 cancel_delayed_work(&hdev
->discov_off
);
1555 hci_req_add(&req
, HCI_OP_WRITE_SCAN_ENABLE
, 1, &scan
);
1558 /* If we're going from non-connectable to connectable or
1559 * vice-versa when fast connectable is enabled ensure that fast
1560 * connectable gets disabled. write_fast_connectable won't do
1561 * anything if the page scan parameters are already what they
1564 if (cp
->val
|| test_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
))
1565 write_fast_connectable(&req
, false);
1567 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
) &&
1568 hci_conn_num(hdev
, LE_LINK
) == 0) {
1569 disable_advertising(&req
);
1570 enable_advertising(&req
);
1573 err
= hci_req_run(&req
, set_connectable_complete
);
1575 mgmt_pending_remove(cmd
);
1576 if (err
== -ENODATA
)
1577 err
= set_connectable_update_settings(hdev
, sk
,
1583 hci_dev_unlock(hdev
);
1587 static int set_pairable(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1590 struct mgmt_mode
*cp
= data
;
1594 BT_DBG("request for %s", hdev
->name
);
1596 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1597 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_PAIRABLE
,
1598 MGMT_STATUS_INVALID_PARAMS
);
1603 changed
= !test_and_set_bit(HCI_PAIRABLE
, &hdev
->dev_flags
);
1605 changed
= test_and_clear_bit(HCI_PAIRABLE
, &hdev
->dev_flags
);
1607 err
= send_settings_rsp(sk
, MGMT_OP_SET_PAIRABLE
, hdev
);
1612 err
= new_settings(hdev
, sk
);
1615 hci_dev_unlock(hdev
);
1619 static int set_link_security(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
1622 struct mgmt_mode
*cp
= data
;
1623 struct pending_cmd
*cmd
;
1627 BT_DBG("request for %s", hdev
->name
);
1629 status
= mgmt_bredr_support(hdev
);
1631 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LINK_SECURITY
,
1634 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1635 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LINK_SECURITY
,
1636 MGMT_STATUS_INVALID_PARAMS
);
1640 if (!hdev_is_powered(hdev
)) {
1641 bool changed
= false;
1643 if (!!cp
->val
!= test_bit(HCI_LINK_SECURITY
,
1644 &hdev
->dev_flags
)) {
1645 change_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
);
1649 err
= send_settings_rsp(sk
, MGMT_OP_SET_LINK_SECURITY
, hdev
);
1654 err
= new_settings(hdev
, sk
);
1659 if (mgmt_pending_find(MGMT_OP_SET_LINK_SECURITY
, hdev
)) {
1660 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LINK_SECURITY
,
1667 if (test_bit(HCI_AUTH
, &hdev
->flags
) == val
) {
1668 err
= send_settings_rsp(sk
, MGMT_OP_SET_LINK_SECURITY
, hdev
);
1672 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_LINK_SECURITY
, hdev
, data
, len
);
1678 err
= hci_send_cmd(hdev
, HCI_OP_WRITE_AUTH_ENABLE
, sizeof(val
), &val
);
1680 mgmt_pending_remove(cmd
);
1685 hci_dev_unlock(hdev
);
1689 static int set_ssp(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
1691 struct mgmt_mode
*cp
= data
;
1692 struct pending_cmd
*cmd
;
1696 BT_DBG("request for %s", hdev
->name
);
1698 status
= mgmt_bredr_support(hdev
);
1700 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
, status
);
1702 if (!lmp_ssp_capable(hdev
))
1703 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
,
1704 MGMT_STATUS_NOT_SUPPORTED
);
1706 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1707 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
,
1708 MGMT_STATUS_INVALID_PARAMS
);
1712 if (!hdev_is_powered(hdev
)) {
1716 changed
= !test_and_set_bit(HCI_SSP_ENABLED
,
1719 changed
= test_and_clear_bit(HCI_SSP_ENABLED
,
1722 changed
= test_and_clear_bit(HCI_HS_ENABLED
,
1725 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
1728 err
= send_settings_rsp(sk
, MGMT_OP_SET_SSP
, hdev
);
1733 err
= new_settings(hdev
, sk
);
1738 if (mgmt_pending_find(MGMT_OP_SET_SSP
, hdev
) ||
1739 mgmt_pending_find(MGMT_OP_SET_HS
, hdev
)) {
1740 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SSP
,
1745 if (!!cp
->val
== test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
)) {
1746 err
= send_settings_rsp(sk
, MGMT_OP_SET_SSP
, hdev
);
1750 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_SSP
, hdev
, data
, len
);
1756 err
= hci_send_cmd(hdev
, HCI_OP_WRITE_SSP_MODE
, 1, &cp
->val
);
1758 mgmt_pending_remove(cmd
);
1763 hci_dev_unlock(hdev
);
1767 static int set_hs(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
1769 struct mgmt_mode
*cp
= data
;
1774 BT_DBG("request for %s", hdev
->name
);
1776 status
= mgmt_bredr_support(hdev
);
1778 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
, status
);
1780 if (!lmp_ssp_capable(hdev
))
1781 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1782 MGMT_STATUS_NOT_SUPPORTED
);
1784 if (!test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
))
1785 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1786 MGMT_STATUS_REJECTED
);
1788 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1789 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1790 MGMT_STATUS_INVALID_PARAMS
);
1795 changed
= !test_and_set_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
1797 if (hdev_is_powered(hdev
)) {
1798 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_HS
,
1799 MGMT_STATUS_REJECTED
);
1803 changed
= test_and_clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
1806 err
= send_settings_rsp(sk
, MGMT_OP_SET_HS
, hdev
);
1811 err
= new_settings(hdev
, sk
);
1814 hci_dev_unlock(hdev
);
1818 static void le_enable_complete(struct hci_dev
*hdev
, u8 status
)
1820 struct cmd_lookup match
= { NULL
, hdev
};
1823 u8 mgmt_err
= mgmt_status(status
);
1825 mgmt_pending_foreach(MGMT_OP_SET_LE
, hdev
, cmd_status_rsp
,
1830 mgmt_pending_foreach(MGMT_OP_SET_LE
, hdev
, settings_rsp
, &match
);
1832 new_settings(hdev
, match
.sk
);
1837 /* Make sure the controller has a good default for
1838 * advertising data. Restrict the update to when LE
1839 * has actually been enabled. During power on, the
1840 * update in powered_update_hci will take care of it.
1842 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
)) {
1843 struct hci_request req
;
1847 hci_req_init(&req
, hdev
);
1848 update_adv_data(&req
);
1849 update_scan_rsp_data(&req
);
1850 hci_req_run(&req
, NULL
);
1852 hci_dev_unlock(hdev
);
1856 static int set_le(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
1858 struct mgmt_mode
*cp
= data
;
1859 struct hci_cp_write_le_host_supported hci_cp
;
1860 struct pending_cmd
*cmd
;
1861 struct hci_request req
;
1865 BT_DBG("request for %s", hdev
->name
);
1867 if (!lmp_le_capable(hdev
))
1868 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1869 MGMT_STATUS_NOT_SUPPORTED
);
1871 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
1872 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1873 MGMT_STATUS_INVALID_PARAMS
);
1875 /* LE-only devices do not allow toggling LE on/off */
1876 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
1877 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1878 MGMT_STATUS_REJECTED
);
1883 enabled
= lmp_host_le_capable(hdev
);
1885 if (!hdev_is_powered(hdev
) || val
== enabled
) {
1886 bool changed
= false;
1888 if (val
!= test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
)) {
1889 change_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
);
1893 if (!val
&& test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
)) {
1894 clear_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
1898 err
= send_settings_rsp(sk
, MGMT_OP_SET_LE
, hdev
);
1903 err
= new_settings(hdev
, sk
);
1908 if (mgmt_pending_find(MGMT_OP_SET_LE
, hdev
) ||
1909 mgmt_pending_find(MGMT_OP_SET_ADVERTISING
, hdev
)) {
1910 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_LE
,
1915 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_LE
, hdev
, data
, len
);
1921 hci_req_init(&req
, hdev
);
1923 memset(&hci_cp
, 0, sizeof(hci_cp
));
1927 hci_cp
.simul
= lmp_le_br_capable(hdev
);
1929 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
1930 disable_advertising(&req
);
1933 hci_req_add(&req
, HCI_OP_WRITE_LE_HOST_SUPPORTED
, sizeof(hci_cp
),
1936 err
= hci_req_run(&req
, le_enable_complete
);
1938 mgmt_pending_remove(cmd
);
1941 hci_dev_unlock(hdev
);
1945 /* This is a helper function to test for pending mgmt commands that can
1946 * cause CoD or EIR HCI commands. We can only allow one such pending
1947 * mgmt command at a time since otherwise we cannot easily track what
1948 * the current values are, will be, and based on that calculate if a new
1949 * HCI command needs to be sent and if yes with what value.
1951 static bool pending_eir_or_class(struct hci_dev
*hdev
)
1953 struct pending_cmd
*cmd
;
1955 list_for_each_entry(cmd
, &hdev
->mgmt_pending
, list
) {
1956 switch (cmd
->opcode
) {
1957 case MGMT_OP_ADD_UUID
:
1958 case MGMT_OP_REMOVE_UUID
:
1959 case MGMT_OP_SET_DEV_CLASS
:
1960 case MGMT_OP_SET_POWERED
:
1968 static const u8 bluetooth_base_uuid
[] = {
1969 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
1970 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1973 static u8
get_uuid_size(const u8
*uuid
)
1977 if (memcmp(uuid
, bluetooth_base_uuid
, 12))
1980 val
= get_unaligned_le32(&uuid
[12]);
1987 static void mgmt_class_complete(struct hci_dev
*hdev
, u16 mgmt_op
, u8 status
)
1989 struct pending_cmd
*cmd
;
1993 cmd
= mgmt_pending_find(mgmt_op
, hdev
);
1997 cmd_complete(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_status(status
),
1998 hdev
->dev_class
, 3);
2000 mgmt_pending_remove(cmd
);
2003 hci_dev_unlock(hdev
);
2006 static void add_uuid_complete(struct hci_dev
*hdev
, u8 status
)
2008 BT_DBG("status 0x%02x", status
);
2010 mgmt_class_complete(hdev
, MGMT_OP_ADD_UUID
, status
);
2013 static int add_uuid(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
2015 struct mgmt_cp_add_uuid
*cp
= data
;
2016 struct pending_cmd
*cmd
;
2017 struct hci_request req
;
2018 struct bt_uuid
*uuid
;
2021 BT_DBG("request for %s", hdev
->name
);
2025 if (pending_eir_or_class(hdev
)) {
2026 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_ADD_UUID
,
2031 uuid
= kmalloc(sizeof(*uuid
), GFP_KERNEL
);
2037 memcpy(uuid
->uuid
, cp
->uuid
, 16);
2038 uuid
->svc_hint
= cp
->svc_hint
;
2039 uuid
->size
= get_uuid_size(cp
->uuid
);
2041 list_add_tail(&uuid
->list
, &hdev
->uuids
);
2043 hci_req_init(&req
, hdev
);
2048 err
= hci_req_run(&req
, add_uuid_complete
);
2050 if (err
!= -ENODATA
)
2053 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_ADD_UUID
, 0,
2054 hdev
->dev_class
, 3);
2058 cmd
= mgmt_pending_add(sk
, MGMT_OP_ADD_UUID
, hdev
, data
, len
);
2067 hci_dev_unlock(hdev
);
2071 static bool enable_service_cache(struct hci_dev
*hdev
)
2073 if (!hdev_is_powered(hdev
))
2076 if (!test_and_set_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
)) {
2077 queue_delayed_work(hdev
->workqueue
, &hdev
->service_cache
,
2085 static void remove_uuid_complete(struct hci_dev
*hdev
, u8 status
)
2087 BT_DBG("status 0x%02x", status
);
2089 mgmt_class_complete(hdev
, MGMT_OP_REMOVE_UUID
, status
);
2092 static int remove_uuid(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2095 struct mgmt_cp_remove_uuid
*cp
= data
;
2096 struct pending_cmd
*cmd
;
2097 struct bt_uuid
*match
, *tmp
;
2098 u8 bt_uuid_any
[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
2099 struct hci_request req
;
2102 BT_DBG("request for %s", hdev
->name
);
2106 if (pending_eir_or_class(hdev
)) {
2107 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
,
2112 if (memcmp(cp
->uuid
, bt_uuid_any
, 16) == 0) {
2113 hci_uuids_clear(hdev
);
2115 if (enable_service_cache(hdev
)) {
2116 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
,
2117 0, hdev
->dev_class
, 3);
2126 list_for_each_entry_safe(match
, tmp
, &hdev
->uuids
, list
) {
2127 if (memcmp(match
->uuid
, cp
->uuid
, 16) != 0)
2130 list_del(&match
->list
);
2136 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
,
2137 MGMT_STATUS_INVALID_PARAMS
);
2142 hci_req_init(&req
, hdev
);
2147 err
= hci_req_run(&req
, remove_uuid_complete
);
2149 if (err
!= -ENODATA
)
2152 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_REMOVE_UUID
, 0,
2153 hdev
->dev_class
, 3);
2157 cmd
= mgmt_pending_add(sk
, MGMT_OP_REMOVE_UUID
, hdev
, data
, len
);
2166 hci_dev_unlock(hdev
);
2170 static void set_class_complete(struct hci_dev
*hdev
, u8 status
)
2172 BT_DBG("status 0x%02x", status
);
2174 mgmt_class_complete(hdev
, MGMT_OP_SET_DEV_CLASS
, status
);
2177 static int set_dev_class(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2180 struct mgmt_cp_set_dev_class
*cp
= data
;
2181 struct pending_cmd
*cmd
;
2182 struct hci_request req
;
2185 BT_DBG("request for %s", hdev
->name
);
2187 if (!lmp_bredr_capable(hdev
))
2188 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
,
2189 MGMT_STATUS_NOT_SUPPORTED
);
2193 if (pending_eir_or_class(hdev
)) {
2194 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
,
2199 if ((cp
->minor
& 0x03) != 0 || (cp
->major
& 0xe0) != 0) {
2200 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
,
2201 MGMT_STATUS_INVALID_PARAMS
);
2205 hdev
->major_class
= cp
->major
;
2206 hdev
->minor_class
= cp
->minor
;
2208 if (!hdev_is_powered(hdev
)) {
2209 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
, 0,
2210 hdev
->dev_class
, 3);
2214 hci_req_init(&req
, hdev
);
2216 if (test_and_clear_bit(HCI_SERVICE_CACHE
, &hdev
->dev_flags
)) {
2217 hci_dev_unlock(hdev
);
2218 cancel_delayed_work_sync(&hdev
->service_cache
);
2225 err
= hci_req_run(&req
, set_class_complete
);
2227 if (err
!= -ENODATA
)
2230 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_DEV_CLASS
, 0,
2231 hdev
->dev_class
, 3);
2235 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_DEV_CLASS
, hdev
, data
, len
);
2244 hci_dev_unlock(hdev
);
2248 static int load_link_keys(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2251 struct mgmt_cp_load_link_keys
*cp
= data
;
2252 u16 key_count
, expected_len
;
2256 BT_DBG("request for %s", hdev
->name
);
2258 if (!lmp_bredr_capable(hdev
))
2259 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2260 MGMT_STATUS_NOT_SUPPORTED
);
2262 key_count
= __le16_to_cpu(cp
->key_count
);
2264 expected_len
= sizeof(*cp
) + key_count
*
2265 sizeof(struct mgmt_link_key_info
);
2266 if (expected_len
!= len
) {
2267 BT_ERR("load_link_keys: expected %u bytes, got %u bytes",
2269 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2270 MGMT_STATUS_INVALID_PARAMS
);
2273 if (cp
->debug_keys
!= 0x00 && cp
->debug_keys
!= 0x01)
2274 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2275 MGMT_STATUS_INVALID_PARAMS
);
2277 BT_DBG("%s debug_keys %u key_count %u", hdev
->name
, cp
->debug_keys
,
2280 for (i
= 0; i
< key_count
; i
++) {
2281 struct mgmt_link_key_info
*key
= &cp
->keys
[i
];
2283 if (key
->addr
.type
!= BDADDR_BREDR
|| key
->type
> 0x08)
2284 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
,
2285 MGMT_STATUS_INVALID_PARAMS
);
2290 hci_link_keys_clear(hdev
);
2293 changed
= !test_and_set_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
2295 changed
= test_and_clear_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
2298 new_settings(hdev
, NULL
);
2300 for (i
= 0; i
< key_count
; i
++) {
2301 struct mgmt_link_key_info
*key
= &cp
->keys
[i
];
2303 hci_add_link_key(hdev
, NULL
, 0, &key
->addr
.bdaddr
, key
->val
,
2304 key
->type
, key
->pin_len
);
2307 cmd_complete(sk
, hdev
->id
, MGMT_OP_LOAD_LINK_KEYS
, 0, NULL
, 0);
2309 hci_dev_unlock(hdev
);
2314 static int device_unpaired(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
2315 u8 addr_type
, struct sock
*skip_sk
)
2317 struct mgmt_ev_device_unpaired ev
;
2319 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
2320 ev
.addr
.type
= addr_type
;
2322 return mgmt_event(MGMT_EV_DEVICE_UNPAIRED
, hdev
, &ev
, sizeof(ev
),
2326 static int unpair_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2329 struct mgmt_cp_unpair_device
*cp
= data
;
2330 struct mgmt_rp_unpair_device rp
;
2331 struct hci_cp_disconnect dc
;
2332 struct pending_cmd
*cmd
;
2333 struct hci_conn
*conn
;
2336 memset(&rp
, 0, sizeof(rp
));
2337 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
2338 rp
.addr
.type
= cp
->addr
.type
;
2340 if (!bdaddr_type_is_valid(cp
->addr
.type
))
2341 return cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2342 MGMT_STATUS_INVALID_PARAMS
,
2345 if (cp
->disconnect
!= 0x00 && cp
->disconnect
!= 0x01)
2346 return cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2347 MGMT_STATUS_INVALID_PARAMS
,
2352 if (!hdev_is_powered(hdev
)) {
2353 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2354 MGMT_STATUS_NOT_POWERED
, &rp
, sizeof(rp
));
2358 if (cp
->addr
.type
== BDADDR_BREDR
) {
2359 err
= hci_remove_link_key(hdev
, &cp
->addr
.bdaddr
);
2363 if (cp
->addr
.type
== BDADDR_LE_PUBLIC
)
2364 addr_type
= ADDR_LE_DEV_PUBLIC
;
2366 addr_type
= ADDR_LE_DEV_RANDOM
;
2368 hci_remove_irk(hdev
, &cp
->addr
.bdaddr
, addr_type
);
2370 err
= hci_remove_ltk(hdev
, &cp
->addr
.bdaddr
, addr_type
);
2374 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
,
2375 MGMT_STATUS_NOT_PAIRED
, &rp
, sizeof(rp
));
2379 if (cp
->disconnect
) {
2380 if (cp
->addr
.type
== BDADDR_BREDR
)
2381 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
,
2384 conn
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
,
2391 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNPAIR_DEVICE
, 0,
2393 device_unpaired(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
, sk
);
2397 cmd
= mgmt_pending_add(sk
, MGMT_OP_UNPAIR_DEVICE
, hdev
, cp
,
2404 dc
.handle
= cpu_to_le16(conn
->handle
);
2405 dc
.reason
= 0x13; /* Remote User Terminated Connection */
2406 err
= hci_send_cmd(hdev
, HCI_OP_DISCONNECT
, sizeof(dc
), &dc
);
2408 mgmt_pending_remove(cmd
);
2411 hci_dev_unlock(hdev
);
2415 static int disconnect(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2418 struct mgmt_cp_disconnect
*cp
= data
;
2419 struct mgmt_rp_disconnect rp
;
2420 struct hci_cp_disconnect dc
;
2421 struct pending_cmd
*cmd
;
2422 struct hci_conn
*conn
;
2427 memset(&rp
, 0, sizeof(rp
));
2428 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
2429 rp
.addr
.type
= cp
->addr
.type
;
2431 if (!bdaddr_type_is_valid(cp
->addr
.type
))
2432 return cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2433 MGMT_STATUS_INVALID_PARAMS
,
2438 if (!test_bit(HCI_UP
, &hdev
->flags
)) {
2439 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2440 MGMT_STATUS_NOT_POWERED
, &rp
, sizeof(rp
));
2444 if (mgmt_pending_find(MGMT_OP_DISCONNECT
, hdev
)) {
2445 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2446 MGMT_STATUS_BUSY
, &rp
, sizeof(rp
));
2450 if (cp
->addr
.type
== BDADDR_BREDR
)
2451 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
,
2454 conn
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
, &cp
->addr
.bdaddr
);
2456 if (!conn
|| conn
->state
== BT_OPEN
|| conn
->state
== BT_CLOSED
) {
2457 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_DISCONNECT
,
2458 MGMT_STATUS_NOT_CONNECTED
, &rp
, sizeof(rp
));
2462 cmd
= mgmt_pending_add(sk
, MGMT_OP_DISCONNECT
, hdev
, data
, len
);
2468 dc
.handle
= cpu_to_le16(conn
->handle
);
2469 dc
.reason
= HCI_ERROR_REMOTE_USER_TERM
;
2471 err
= hci_send_cmd(hdev
, HCI_OP_DISCONNECT
, sizeof(dc
), &dc
);
2473 mgmt_pending_remove(cmd
);
2476 hci_dev_unlock(hdev
);
2480 static u8
link_to_bdaddr(u8 link_type
, u8 addr_type
)
2482 switch (link_type
) {
2484 switch (addr_type
) {
2485 case ADDR_LE_DEV_PUBLIC
:
2486 return BDADDR_LE_PUBLIC
;
2489 /* Fallback to LE Random address type */
2490 return BDADDR_LE_RANDOM
;
2494 /* Fallback to BR/EDR type */
2495 return BDADDR_BREDR
;
2499 static int get_connections(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2502 struct mgmt_rp_get_connections
*rp
;
2512 if (!hdev_is_powered(hdev
)) {
2513 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_GET_CONNECTIONS
,
2514 MGMT_STATUS_NOT_POWERED
);
2519 list_for_each_entry(c
, &hdev
->conn_hash
.list
, list
) {
2520 if (test_bit(HCI_CONN_MGMT_CONNECTED
, &c
->flags
))
2524 rp_len
= sizeof(*rp
) + (i
* sizeof(struct mgmt_addr_info
));
2525 rp
= kmalloc(rp_len
, GFP_KERNEL
);
2532 list_for_each_entry(c
, &hdev
->conn_hash
.list
, list
) {
2533 if (!test_bit(HCI_CONN_MGMT_CONNECTED
, &c
->flags
))
2535 bacpy(&rp
->addr
[i
].bdaddr
, &c
->dst
);
2536 rp
->addr
[i
].type
= link_to_bdaddr(c
->type
, c
->dst_type
);
2537 if (c
->type
== SCO_LINK
|| c
->type
== ESCO_LINK
)
2542 rp
->conn_count
= cpu_to_le16(i
);
2544 /* Recalculate length in case of filtered SCO connections, etc */
2545 rp_len
= sizeof(*rp
) + (i
* sizeof(struct mgmt_addr_info
));
2547 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_GET_CONNECTIONS
, 0, rp
,
2553 hci_dev_unlock(hdev
);
2557 static int send_pin_code_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
2558 struct mgmt_cp_pin_code_neg_reply
*cp
)
2560 struct pending_cmd
*cmd
;
2563 cmd
= mgmt_pending_add(sk
, MGMT_OP_PIN_CODE_NEG_REPLY
, hdev
, cp
,
2568 err
= hci_send_cmd(hdev
, HCI_OP_PIN_CODE_NEG_REPLY
,
2569 sizeof(cp
->addr
.bdaddr
), &cp
->addr
.bdaddr
);
2571 mgmt_pending_remove(cmd
);
2576 static int pin_code_reply(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2579 struct hci_conn
*conn
;
2580 struct mgmt_cp_pin_code_reply
*cp
= data
;
2581 struct hci_cp_pin_code_reply reply
;
2582 struct pending_cmd
*cmd
;
2589 if (!hdev_is_powered(hdev
)) {
2590 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
2591 MGMT_STATUS_NOT_POWERED
);
2595 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &cp
->addr
.bdaddr
);
2597 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
2598 MGMT_STATUS_NOT_CONNECTED
);
2602 if (conn
->pending_sec_level
== BT_SECURITY_HIGH
&& cp
->pin_len
!= 16) {
2603 struct mgmt_cp_pin_code_neg_reply ncp
;
2605 memcpy(&ncp
.addr
, &cp
->addr
, sizeof(ncp
.addr
));
2607 BT_ERR("PIN code is not 16 bytes long");
2609 err
= send_pin_code_neg_reply(sk
, hdev
, &ncp
);
2611 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
2612 MGMT_STATUS_INVALID_PARAMS
);
2617 cmd
= mgmt_pending_add(sk
, MGMT_OP_PIN_CODE_REPLY
, hdev
, data
, len
);
2623 bacpy(&reply
.bdaddr
, &cp
->addr
.bdaddr
);
2624 reply
.pin_len
= cp
->pin_len
;
2625 memcpy(reply
.pin_code
, cp
->pin_code
, sizeof(reply
.pin_code
));
2627 err
= hci_send_cmd(hdev
, HCI_OP_PIN_CODE_REPLY
, sizeof(reply
), &reply
);
2629 mgmt_pending_remove(cmd
);
2632 hci_dev_unlock(hdev
);
2636 static int set_io_capability(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2639 struct mgmt_cp_set_io_capability
*cp
= data
;
2645 hdev
->io_capability
= cp
->io_capability
;
2647 BT_DBG("%s IO capability set to 0x%02x", hdev
->name
,
2648 hdev
->io_capability
);
2650 hci_dev_unlock(hdev
);
2652 return cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_IO_CAPABILITY
, 0, NULL
,
2656 static struct pending_cmd
*find_pairing(struct hci_conn
*conn
)
2658 struct hci_dev
*hdev
= conn
->hdev
;
2659 struct pending_cmd
*cmd
;
2661 list_for_each_entry(cmd
, &hdev
->mgmt_pending
, list
) {
2662 if (cmd
->opcode
!= MGMT_OP_PAIR_DEVICE
)
2665 if (cmd
->user_data
!= conn
)
2674 static void pairing_complete(struct pending_cmd
*cmd
, u8 status
)
2676 struct mgmt_rp_pair_device rp
;
2677 struct hci_conn
*conn
= cmd
->user_data
;
2679 bacpy(&rp
.addr
.bdaddr
, &conn
->dst
);
2680 rp
.addr
.type
= link_to_bdaddr(conn
->type
, conn
->dst_type
);
2682 cmd_complete(cmd
->sk
, cmd
->index
, MGMT_OP_PAIR_DEVICE
, status
,
2685 /* So we don't get further callbacks for this connection */
2686 conn
->connect_cfm_cb
= NULL
;
2687 conn
->security_cfm_cb
= NULL
;
2688 conn
->disconn_cfm_cb
= NULL
;
2690 hci_conn_drop(conn
);
2692 mgmt_pending_remove(cmd
);
2695 void mgmt_smp_complete(struct hci_conn
*conn
, bool complete
)
2697 u8 status
= complete
? MGMT_STATUS_SUCCESS
: MGMT_STATUS_FAILED
;
2698 struct pending_cmd
*cmd
;
2700 cmd
= find_pairing(conn
);
2702 pairing_complete(cmd
, status
);
2705 static void pairing_complete_cb(struct hci_conn
*conn
, u8 status
)
2707 struct pending_cmd
*cmd
;
2709 BT_DBG("status %u", status
);
2711 cmd
= find_pairing(conn
);
2713 BT_DBG("Unable to find a pending command");
2715 pairing_complete(cmd
, mgmt_status(status
));
2718 static void le_pairing_complete_cb(struct hci_conn
*conn
, u8 status
)
2720 struct pending_cmd
*cmd
;
2722 BT_DBG("status %u", status
);
2727 cmd
= find_pairing(conn
);
2729 BT_DBG("Unable to find a pending command");
2731 pairing_complete(cmd
, mgmt_status(status
));
2734 static int pair_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2737 struct mgmt_cp_pair_device
*cp
= data
;
2738 struct mgmt_rp_pair_device rp
;
2739 struct pending_cmd
*cmd
;
2740 u8 sec_level
, auth_type
;
2741 struct hci_conn
*conn
;
2746 memset(&rp
, 0, sizeof(rp
));
2747 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
2748 rp
.addr
.type
= cp
->addr
.type
;
2750 if (!bdaddr_type_is_valid(cp
->addr
.type
))
2751 return cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2752 MGMT_STATUS_INVALID_PARAMS
,
2757 if (!hdev_is_powered(hdev
)) {
2758 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2759 MGMT_STATUS_NOT_POWERED
, &rp
, sizeof(rp
));
2763 sec_level
= BT_SECURITY_MEDIUM
;
2764 if (cp
->io_cap
== 0x03)
2765 auth_type
= HCI_AT_DEDICATED_BONDING
;
2767 auth_type
= HCI_AT_DEDICATED_BONDING_MITM
;
2769 if (cp
->addr
.type
== BDADDR_BREDR
)
2770 conn
= hci_connect(hdev
, ACL_LINK
, &cp
->addr
.bdaddr
,
2771 cp
->addr
.type
, sec_level
, auth_type
);
2773 conn
= hci_connect(hdev
, LE_LINK
, &cp
->addr
.bdaddr
,
2774 cp
->addr
.type
, sec_level
, auth_type
);
2779 if (PTR_ERR(conn
) == -EBUSY
)
2780 status
= MGMT_STATUS_BUSY
;
2782 status
= MGMT_STATUS_CONNECT_FAILED
;
2784 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2790 if (conn
->connect_cfm_cb
) {
2791 hci_conn_drop(conn
);
2792 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_PAIR_DEVICE
,
2793 MGMT_STATUS_BUSY
, &rp
, sizeof(rp
));
2797 cmd
= mgmt_pending_add(sk
, MGMT_OP_PAIR_DEVICE
, hdev
, data
, len
);
2800 hci_conn_drop(conn
);
2804 /* For LE, just connecting isn't a proof that the pairing finished */
2805 if (cp
->addr
.type
== BDADDR_BREDR
) {
2806 conn
->connect_cfm_cb
= pairing_complete_cb
;
2807 conn
->security_cfm_cb
= pairing_complete_cb
;
2808 conn
->disconn_cfm_cb
= pairing_complete_cb
;
2810 conn
->connect_cfm_cb
= le_pairing_complete_cb
;
2811 conn
->security_cfm_cb
= le_pairing_complete_cb
;
2812 conn
->disconn_cfm_cb
= le_pairing_complete_cb
;
2815 conn
->io_capability
= cp
->io_cap
;
2816 cmd
->user_data
= conn
;
2818 if (conn
->state
== BT_CONNECTED
&&
2819 hci_conn_security(conn
, sec_level
, auth_type
))
2820 pairing_complete(cmd
, 0);
2825 hci_dev_unlock(hdev
);
2829 static int cancel_pair_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2832 struct mgmt_addr_info
*addr
= data
;
2833 struct pending_cmd
*cmd
;
2834 struct hci_conn
*conn
;
2841 if (!hdev_is_powered(hdev
)) {
2842 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
,
2843 MGMT_STATUS_NOT_POWERED
);
2847 cmd
= mgmt_pending_find(MGMT_OP_PAIR_DEVICE
, hdev
);
2849 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
,
2850 MGMT_STATUS_INVALID_PARAMS
);
2854 conn
= cmd
->user_data
;
2856 if (bacmp(&addr
->bdaddr
, &conn
->dst
) != 0) {
2857 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
,
2858 MGMT_STATUS_INVALID_PARAMS
);
2862 pairing_complete(cmd
, MGMT_STATUS_CANCELLED
);
2864 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_CANCEL_PAIR_DEVICE
, 0,
2865 addr
, sizeof(*addr
));
2867 hci_dev_unlock(hdev
);
2871 static int user_pairing_resp(struct sock
*sk
, struct hci_dev
*hdev
,
2872 struct mgmt_addr_info
*addr
, u16 mgmt_op
,
2873 u16 hci_op
, __le32 passkey
)
2875 struct pending_cmd
*cmd
;
2876 struct hci_conn
*conn
;
2881 if (!hdev_is_powered(hdev
)) {
2882 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2883 MGMT_STATUS_NOT_POWERED
, addr
,
2888 if (addr
->type
== BDADDR_BREDR
)
2889 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &addr
->bdaddr
);
2891 conn
= hci_conn_hash_lookup_ba(hdev
, LE_LINK
, &addr
->bdaddr
);
2894 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2895 MGMT_STATUS_NOT_CONNECTED
, addr
,
2900 if (addr
->type
== BDADDR_LE_PUBLIC
|| addr
->type
== BDADDR_LE_RANDOM
) {
2901 /* Continue with pairing via SMP */
2902 err
= smp_user_confirm_reply(conn
, mgmt_op
, passkey
);
2905 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2906 MGMT_STATUS_SUCCESS
, addr
,
2909 err
= cmd_complete(sk
, hdev
->id
, mgmt_op
,
2910 MGMT_STATUS_FAILED
, addr
,
2916 cmd
= mgmt_pending_add(sk
, mgmt_op
, hdev
, addr
, sizeof(*addr
));
2922 /* Continue with pairing via HCI */
2923 if (hci_op
== HCI_OP_USER_PASSKEY_REPLY
) {
2924 struct hci_cp_user_passkey_reply cp
;
2926 bacpy(&cp
.bdaddr
, &addr
->bdaddr
);
2927 cp
.passkey
= passkey
;
2928 err
= hci_send_cmd(hdev
, hci_op
, sizeof(cp
), &cp
);
2930 err
= hci_send_cmd(hdev
, hci_op
, sizeof(addr
->bdaddr
),
2934 mgmt_pending_remove(cmd
);
2937 hci_dev_unlock(hdev
);
2941 static int pin_code_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
2942 void *data
, u16 len
)
2944 struct mgmt_cp_pin_code_neg_reply
*cp
= data
;
2948 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
2949 MGMT_OP_PIN_CODE_NEG_REPLY
,
2950 HCI_OP_PIN_CODE_NEG_REPLY
, 0);
2953 static int user_confirm_reply(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2956 struct mgmt_cp_user_confirm_reply
*cp
= data
;
2960 if (len
!= sizeof(*cp
))
2961 return cmd_status(sk
, hdev
->id
, MGMT_OP_USER_CONFIRM_REPLY
,
2962 MGMT_STATUS_INVALID_PARAMS
);
2964 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
2965 MGMT_OP_USER_CONFIRM_REPLY
,
2966 HCI_OP_USER_CONFIRM_REPLY
, 0);
2969 static int user_confirm_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
2970 void *data
, u16 len
)
2972 struct mgmt_cp_user_confirm_neg_reply
*cp
= data
;
2976 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
2977 MGMT_OP_USER_CONFIRM_NEG_REPLY
,
2978 HCI_OP_USER_CONFIRM_NEG_REPLY
, 0);
2981 static int user_passkey_reply(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
2984 struct mgmt_cp_user_passkey_reply
*cp
= data
;
2988 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
2989 MGMT_OP_USER_PASSKEY_REPLY
,
2990 HCI_OP_USER_PASSKEY_REPLY
, cp
->passkey
);
2993 static int user_passkey_neg_reply(struct sock
*sk
, struct hci_dev
*hdev
,
2994 void *data
, u16 len
)
2996 struct mgmt_cp_user_passkey_neg_reply
*cp
= data
;
3000 return user_pairing_resp(sk
, hdev
, &cp
->addr
,
3001 MGMT_OP_USER_PASSKEY_NEG_REPLY
,
3002 HCI_OP_USER_PASSKEY_NEG_REPLY
, 0);
3005 static void update_name(struct hci_request
*req
)
3007 struct hci_dev
*hdev
= req
->hdev
;
3008 struct hci_cp_write_local_name cp
;
3010 memcpy(cp
.name
, hdev
->dev_name
, sizeof(cp
.name
));
3012 hci_req_add(req
, HCI_OP_WRITE_LOCAL_NAME
, sizeof(cp
), &cp
);
3015 static void set_name_complete(struct hci_dev
*hdev
, u8 status
)
3017 struct mgmt_cp_set_local_name
*cp
;
3018 struct pending_cmd
*cmd
;
3020 BT_DBG("status 0x%02x", status
);
3024 cmd
= mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME
, hdev
);
3031 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
,
3032 mgmt_status(status
));
3034 cmd_complete(cmd
->sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
, 0,
3037 mgmt_pending_remove(cmd
);
3040 hci_dev_unlock(hdev
);
3043 static int set_local_name(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3046 struct mgmt_cp_set_local_name
*cp
= data
;
3047 struct pending_cmd
*cmd
;
3048 struct hci_request req
;
3055 /* If the old values are the same as the new ones just return a
3056 * direct command complete event.
3058 if (!memcmp(hdev
->dev_name
, cp
->name
, sizeof(hdev
->dev_name
)) &&
3059 !memcmp(hdev
->short_name
, cp
->short_name
,
3060 sizeof(hdev
->short_name
))) {
3061 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
, 0,
3066 memcpy(hdev
->short_name
, cp
->short_name
, sizeof(hdev
->short_name
));
3068 if (!hdev_is_powered(hdev
)) {
3069 memcpy(hdev
->dev_name
, cp
->name
, sizeof(hdev
->dev_name
));
3071 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_LOCAL_NAME
, 0,
3076 err
= mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED
, hdev
, data
, len
,
3082 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_LOCAL_NAME
, hdev
, data
, len
);
3088 memcpy(hdev
->dev_name
, cp
->name
, sizeof(hdev
->dev_name
));
3090 hci_req_init(&req
, hdev
);
3092 if (lmp_bredr_capable(hdev
)) {
3097 /* The name is stored in the scan response data and so
3098 * no need to udpate the advertising data here.
3100 if (lmp_le_capable(hdev
))
3101 update_scan_rsp_data(&req
);
3103 err
= hci_req_run(&req
, set_name_complete
);
3105 mgmt_pending_remove(cmd
);
3108 hci_dev_unlock(hdev
);
3112 static int read_local_oob_data(struct sock
*sk
, struct hci_dev
*hdev
,
3113 void *data
, u16 data_len
)
3115 struct pending_cmd
*cmd
;
3118 BT_DBG("%s", hdev
->name
);
3122 if (!hdev_is_powered(hdev
)) {
3123 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
3124 MGMT_STATUS_NOT_POWERED
);
3128 if (!lmp_ssp_capable(hdev
)) {
3129 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
3130 MGMT_STATUS_NOT_SUPPORTED
);
3134 if (mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA
, hdev
)) {
3135 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
3140 cmd
= mgmt_pending_add(sk
, MGMT_OP_READ_LOCAL_OOB_DATA
, hdev
, NULL
, 0);
3146 if (test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
))
3147 err
= hci_send_cmd(hdev
, HCI_OP_READ_LOCAL_OOB_EXT_DATA
,
3150 err
= hci_send_cmd(hdev
, HCI_OP_READ_LOCAL_OOB_DATA
, 0, NULL
);
3153 mgmt_pending_remove(cmd
);
3156 hci_dev_unlock(hdev
);
3160 static int add_remote_oob_data(struct sock
*sk
, struct hci_dev
*hdev
,
3161 void *data
, u16 len
)
3165 BT_DBG("%s ", hdev
->name
);
3169 if (len
== MGMT_ADD_REMOTE_OOB_DATA_SIZE
) {
3170 struct mgmt_cp_add_remote_oob_data
*cp
= data
;
3173 err
= hci_add_remote_oob_data(hdev
, &cp
->addr
.bdaddr
,
3174 cp
->hash
, cp
->randomizer
);
3176 status
= MGMT_STATUS_FAILED
;
3178 status
= MGMT_STATUS_SUCCESS
;
3180 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_ADD_REMOTE_OOB_DATA
,
3181 status
, &cp
->addr
, sizeof(cp
->addr
));
3182 } else if (len
== MGMT_ADD_REMOTE_OOB_EXT_DATA_SIZE
) {
3183 struct mgmt_cp_add_remote_oob_ext_data
*cp
= data
;
3186 err
= hci_add_remote_oob_ext_data(hdev
, &cp
->addr
.bdaddr
,
3192 status
= MGMT_STATUS_FAILED
;
3194 status
= MGMT_STATUS_SUCCESS
;
3196 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_ADD_REMOTE_OOB_DATA
,
3197 status
, &cp
->addr
, sizeof(cp
->addr
));
3199 BT_ERR("add_remote_oob_data: invalid length of %u bytes", len
);
3200 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_ADD_REMOTE_OOB_DATA
,
3201 MGMT_STATUS_INVALID_PARAMS
);
3204 hci_dev_unlock(hdev
);
3208 static int remove_remote_oob_data(struct sock
*sk
, struct hci_dev
*hdev
,
3209 void *data
, u16 len
)
3211 struct mgmt_cp_remove_remote_oob_data
*cp
= data
;
3215 BT_DBG("%s", hdev
->name
);
3219 err
= hci_remove_remote_oob_data(hdev
, &cp
->addr
.bdaddr
);
3221 status
= MGMT_STATUS_INVALID_PARAMS
;
3223 status
= MGMT_STATUS_SUCCESS
;
3225 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_REMOVE_REMOTE_OOB_DATA
,
3226 status
, &cp
->addr
, sizeof(cp
->addr
));
3228 hci_dev_unlock(hdev
);
3232 static int mgmt_start_discovery_failed(struct hci_dev
*hdev
, u8 status
)
3234 struct pending_cmd
*cmd
;
3238 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
3240 cmd
= mgmt_pending_find(MGMT_OP_START_DISCOVERY
, hdev
);
3244 type
= hdev
->discovery
.type
;
3246 err
= cmd_complete(cmd
->sk
, hdev
->id
, cmd
->opcode
, mgmt_status(status
),
3247 &type
, sizeof(type
));
3248 mgmt_pending_remove(cmd
);
3253 static void start_discovery_complete(struct hci_dev
*hdev
, u8 status
)
3255 BT_DBG("status %d", status
);
3259 mgmt_start_discovery_failed(hdev
, status
);
3260 hci_dev_unlock(hdev
);
3265 hci_discovery_set_state(hdev
, DISCOVERY_FINDING
);
3266 hci_dev_unlock(hdev
);
3268 switch (hdev
->discovery
.type
) {
3269 case DISCOV_TYPE_LE
:
3270 queue_delayed_work(hdev
->workqueue
, &hdev
->le_scan_disable
,
3274 case DISCOV_TYPE_INTERLEAVED
:
3275 queue_delayed_work(hdev
->workqueue
, &hdev
->le_scan_disable
,
3276 DISCOV_INTERLEAVED_TIMEOUT
);
3279 case DISCOV_TYPE_BREDR
:
3283 BT_ERR("Invalid discovery type %d", hdev
->discovery
.type
);
3287 static int start_discovery(struct sock
*sk
, struct hci_dev
*hdev
,
3288 void *data
, u16 len
)
3290 struct mgmt_cp_start_discovery
*cp
= data
;
3291 struct pending_cmd
*cmd
;
3292 struct hci_cp_le_set_scan_param param_cp
;
3293 struct hci_cp_le_set_scan_enable enable_cp
;
3294 struct hci_cp_inquiry inq_cp
;
3295 struct hci_request req
;
3296 /* General inquiry access code (GIAC) */
3297 u8 lap
[3] = { 0x33, 0x8b, 0x9e };
3298 u8 status
, own_addr_type
;
3301 BT_DBG("%s", hdev
->name
);
3305 if (!hdev_is_powered(hdev
)) {
3306 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3307 MGMT_STATUS_NOT_POWERED
);
3311 if (test_bit(HCI_PERIODIC_INQ
, &hdev
->dev_flags
)) {
3312 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3317 if (hdev
->discovery
.state
!= DISCOVERY_STOPPED
) {
3318 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3323 cmd
= mgmt_pending_add(sk
, MGMT_OP_START_DISCOVERY
, hdev
, NULL
, 0);
3329 hdev
->discovery
.type
= cp
->type
;
3331 hci_req_init(&req
, hdev
);
3333 switch (hdev
->discovery
.type
) {
3334 case DISCOV_TYPE_BREDR
:
3335 status
= mgmt_bredr_support(hdev
);
3337 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3339 mgmt_pending_remove(cmd
);
3343 if (test_bit(HCI_INQUIRY
, &hdev
->flags
)) {
3344 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3346 mgmt_pending_remove(cmd
);
3350 hci_inquiry_cache_flush(hdev
);
3352 memset(&inq_cp
, 0, sizeof(inq_cp
));
3353 memcpy(&inq_cp
.lap
, lap
, sizeof(inq_cp
.lap
));
3354 inq_cp
.length
= DISCOV_BREDR_INQUIRY_LEN
;
3355 hci_req_add(&req
, HCI_OP_INQUIRY
, sizeof(inq_cp
), &inq_cp
);
3358 case DISCOV_TYPE_LE
:
3359 case DISCOV_TYPE_INTERLEAVED
:
3360 status
= mgmt_le_support(hdev
);
3362 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3364 mgmt_pending_remove(cmd
);
3368 if (hdev
->discovery
.type
== DISCOV_TYPE_INTERLEAVED
&&
3369 !test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
3370 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3371 MGMT_STATUS_NOT_SUPPORTED
);
3372 mgmt_pending_remove(cmd
);
3376 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
)) {
3377 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3378 MGMT_STATUS_REJECTED
);
3379 mgmt_pending_remove(cmd
);
3383 if (test_bit(HCI_LE_SCAN
, &hdev
->dev_flags
)) {
3384 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3386 mgmt_pending_remove(cmd
);
3390 memset(¶m_cp
, 0, sizeof(param_cp
));
3392 err
= hci_update_random_address(&req
, &own_addr_type
);
3394 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3395 MGMT_STATUS_FAILED
);
3396 mgmt_pending_remove(cmd
);
3400 param_cp
.type
= LE_SCAN_ACTIVE
;
3401 param_cp
.interval
= cpu_to_le16(DISCOV_LE_SCAN_INT
);
3402 param_cp
.window
= cpu_to_le16(DISCOV_LE_SCAN_WIN
);
3403 param_cp
.own_address_type
= own_addr_type
;
3404 hci_req_add(&req
, HCI_OP_LE_SET_SCAN_PARAM
, sizeof(param_cp
),
3407 memset(&enable_cp
, 0, sizeof(enable_cp
));
3408 enable_cp
.enable
= LE_SCAN_ENABLE
;
3409 enable_cp
.filter_dup
= LE_SCAN_FILTER_DUP_ENABLE
;
3410 hci_req_add(&req
, HCI_OP_LE_SET_SCAN_ENABLE
, sizeof(enable_cp
),
3415 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_START_DISCOVERY
,
3416 MGMT_STATUS_INVALID_PARAMS
);
3417 mgmt_pending_remove(cmd
);
3421 err
= hci_req_run(&req
, start_discovery_complete
);
3423 mgmt_pending_remove(cmd
);
3425 hci_discovery_set_state(hdev
, DISCOVERY_STARTING
);
3428 hci_dev_unlock(hdev
);
3432 static int mgmt_stop_discovery_failed(struct hci_dev
*hdev
, u8 status
)
3434 struct pending_cmd
*cmd
;
3437 cmd
= mgmt_pending_find(MGMT_OP_STOP_DISCOVERY
, hdev
);
3441 err
= cmd_complete(cmd
->sk
, hdev
->id
, cmd
->opcode
, mgmt_status(status
),
3442 &hdev
->discovery
.type
, sizeof(hdev
->discovery
.type
));
3443 mgmt_pending_remove(cmd
);
3448 static void stop_discovery_complete(struct hci_dev
*hdev
, u8 status
)
3450 BT_DBG("status %d", status
);
3455 mgmt_stop_discovery_failed(hdev
, status
);
3459 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
3462 hci_dev_unlock(hdev
);
3465 static int stop_discovery(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3468 struct mgmt_cp_stop_discovery
*mgmt_cp
= data
;
3469 struct pending_cmd
*cmd
;
3470 struct hci_cp_remote_name_req_cancel cp
;
3471 struct inquiry_entry
*e
;
3472 struct hci_request req
;
3473 struct hci_cp_le_set_scan_enable enable_cp
;
3476 BT_DBG("%s", hdev
->name
);
3480 if (!hci_discovery_active(hdev
)) {
3481 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_STOP_DISCOVERY
,
3482 MGMT_STATUS_REJECTED
, &mgmt_cp
->type
,
3483 sizeof(mgmt_cp
->type
));
3487 if (hdev
->discovery
.type
!= mgmt_cp
->type
) {
3488 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_STOP_DISCOVERY
,
3489 MGMT_STATUS_INVALID_PARAMS
, &mgmt_cp
->type
,
3490 sizeof(mgmt_cp
->type
));
3494 cmd
= mgmt_pending_add(sk
, MGMT_OP_STOP_DISCOVERY
, hdev
, NULL
, 0);
3500 hci_req_init(&req
, hdev
);
3502 switch (hdev
->discovery
.state
) {
3503 case DISCOVERY_FINDING
:
3504 if (test_bit(HCI_INQUIRY
, &hdev
->flags
)) {
3505 hci_req_add(&req
, HCI_OP_INQUIRY_CANCEL
, 0, NULL
);
3507 cancel_delayed_work(&hdev
->le_scan_disable
);
3509 memset(&enable_cp
, 0, sizeof(enable_cp
));
3510 enable_cp
.enable
= LE_SCAN_DISABLE
;
3511 hci_req_add(&req
, HCI_OP_LE_SET_SCAN_ENABLE
,
3512 sizeof(enable_cp
), &enable_cp
);
3517 case DISCOVERY_RESOLVING
:
3518 e
= hci_inquiry_cache_lookup_resolve(hdev
, BDADDR_ANY
,
3521 mgmt_pending_remove(cmd
);
3522 err
= cmd_complete(sk
, hdev
->id
,
3523 MGMT_OP_STOP_DISCOVERY
, 0,
3525 sizeof(mgmt_cp
->type
));
3526 hci_discovery_set_state(hdev
, DISCOVERY_STOPPED
);
3530 bacpy(&cp
.bdaddr
, &e
->data
.bdaddr
);
3531 hci_req_add(&req
, HCI_OP_REMOTE_NAME_REQ_CANCEL
, sizeof(cp
),
3537 BT_DBG("unknown discovery state %u", hdev
->discovery
.state
);
3539 mgmt_pending_remove(cmd
);
3540 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_STOP_DISCOVERY
,
3541 MGMT_STATUS_FAILED
, &mgmt_cp
->type
,
3542 sizeof(mgmt_cp
->type
));
3546 err
= hci_req_run(&req
, stop_discovery_complete
);
3548 mgmt_pending_remove(cmd
);
3550 hci_discovery_set_state(hdev
, DISCOVERY_STOPPING
);
3553 hci_dev_unlock(hdev
);
3557 static int confirm_name(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3560 struct mgmt_cp_confirm_name
*cp
= data
;
3561 struct inquiry_entry
*e
;
3564 BT_DBG("%s", hdev
->name
);
3568 if (!hci_discovery_active(hdev
)) {
3569 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CONFIRM_NAME
,
3570 MGMT_STATUS_FAILED
);
3574 e
= hci_inquiry_cache_lookup_unknown(hdev
, &cp
->addr
.bdaddr
);
3576 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_CONFIRM_NAME
,
3577 MGMT_STATUS_INVALID_PARAMS
);
3581 if (cp
->name_known
) {
3582 e
->name_state
= NAME_KNOWN
;
3585 e
->name_state
= NAME_NEEDED
;
3586 hci_inquiry_cache_update_resolve(hdev
, e
);
3589 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_CONFIRM_NAME
, 0, &cp
->addr
,
3593 hci_dev_unlock(hdev
);
3597 static int block_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3600 struct mgmt_cp_block_device
*cp
= data
;
3604 BT_DBG("%s", hdev
->name
);
3606 if (!bdaddr_type_is_valid(cp
->addr
.type
))
3607 return cmd_complete(sk
, hdev
->id
, MGMT_OP_BLOCK_DEVICE
,
3608 MGMT_STATUS_INVALID_PARAMS
,
3609 &cp
->addr
, sizeof(cp
->addr
));
3613 err
= hci_blacklist_add(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
);
3615 status
= MGMT_STATUS_FAILED
;
3617 status
= MGMT_STATUS_SUCCESS
;
3619 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_BLOCK_DEVICE
, status
,
3620 &cp
->addr
, sizeof(cp
->addr
));
3622 hci_dev_unlock(hdev
);
3627 static int unblock_device(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3630 struct mgmt_cp_unblock_device
*cp
= data
;
3634 BT_DBG("%s", hdev
->name
);
3636 if (!bdaddr_type_is_valid(cp
->addr
.type
))
3637 return cmd_complete(sk
, hdev
->id
, MGMT_OP_UNBLOCK_DEVICE
,
3638 MGMT_STATUS_INVALID_PARAMS
,
3639 &cp
->addr
, sizeof(cp
->addr
));
3643 err
= hci_blacklist_del(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
);
3645 status
= MGMT_STATUS_INVALID_PARAMS
;
3647 status
= MGMT_STATUS_SUCCESS
;
3649 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_UNBLOCK_DEVICE
, status
,
3650 &cp
->addr
, sizeof(cp
->addr
));
3652 hci_dev_unlock(hdev
);
3657 static int set_device_id(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3660 struct mgmt_cp_set_device_id
*cp
= data
;
3661 struct hci_request req
;
3665 BT_DBG("%s", hdev
->name
);
3667 source
= __le16_to_cpu(cp
->source
);
3669 if (source
> 0x0002)
3670 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEVICE_ID
,
3671 MGMT_STATUS_INVALID_PARAMS
);
3675 hdev
->devid_source
= source
;
3676 hdev
->devid_vendor
= __le16_to_cpu(cp
->vendor
);
3677 hdev
->devid_product
= __le16_to_cpu(cp
->product
);
3678 hdev
->devid_version
= __le16_to_cpu(cp
->version
);
3680 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_DEVICE_ID
, 0, NULL
, 0);
3682 hci_req_init(&req
, hdev
);
3684 hci_req_run(&req
, NULL
);
3686 hci_dev_unlock(hdev
);
3691 static void set_advertising_complete(struct hci_dev
*hdev
, u8 status
)
3693 struct cmd_lookup match
= { NULL
, hdev
};
3696 u8 mgmt_err
= mgmt_status(status
);
3698 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING
, hdev
,
3699 cmd_status_rsp
, &mgmt_err
);
3703 mgmt_pending_foreach(MGMT_OP_SET_ADVERTISING
, hdev
, settings_rsp
,
3706 new_settings(hdev
, match
.sk
);
3712 static int set_advertising(struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
3715 struct mgmt_mode
*cp
= data
;
3716 struct pending_cmd
*cmd
;
3717 struct hci_request req
;
3718 u8 val
, enabled
, status
;
3721 BT_DBG("request for %s", hdev
->name
);
3723 status
= mgmt_le_support(hdev
);
3725 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_ADVERTISING
,
3728 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
3729 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_ADVERTISING
,
3730 MGMT_STATUS_INVALID_PARAMS
);
3735 enabled
= test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
3737 /* The following conditions are ones which mean that we should
3738 * not do any HCI communication but directly send a mgmt
3739 * response to user space (after toggling the flag if
3742 if (!hdev_is_powered(hdev
) || val
== enabled
||
3743 hci_conn_num(hdev
, LE_LINK
) > 0) {
3744 bool changed
= false;
3746 if (val
!= test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
)) {
3747 change_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
3751 err
= send_settings_rsp(sk
, MGMT_OP_SET_ADVERTISING
, hdev
);
3756 err
= new_settings(hdev
, sk
);
3761 if (mgmt_pending_find(MGMT_OP_SET_ADVERTISING
, hdev
) ||
3762 mgmt_pending_find(MGMT_OP_SET_LE
, hdev
)) {
3763 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_ADVERTISING
,
3768 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_ADVERTISING
, hdev
, data
, len
);
3774 hci_req_init(&req
, hdev
);
3777 enable_advertising(&req
);
3779 disable_advertising(&req
);
3781 err
= hci_req_run(&req
, set_advertising_complete
);
3783 mgmt_pending_remove(cmd
);
3786 hci_dev_unlock(hdev
);
3790 static int set_static_address(struct sock
*sk
, struct hci_dev
*hdev
,
3791 void *data
, u16 len
)
3793 struct mgmt_cp_set_static_address
*cp
= data
;
3796 BT_DBG("%s", hdev
->name
);
3798 if (!lmp_le_capable(hdev
))
3799 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_STATIC_ADDRESS
,
3800 MGMT_STATUS_NOT_SUPPORTED
);
3802 if (hdev_is_powered(hdev
))
3803 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_STATIC_ADDRESS
,
3804 MGMT_STATUS_REJECTED
);
3806 if (bacmp(&cp
->bdaddr
, BDADDR_ANY
)) {
3807 if (!bacmp(&cp
->bdaddr
, BDADDR_NONE
))
3808 return cmd_status(sk
, hdev
->id
,
3809 MGMT_OP_SET_STATIC_ADDRESS
,
3810 MGMT_STATUS_INVALID_PARAMS
);
3812 /* Two most significant bits shall be set */
3813 if ((cp
->bdaddr
.b
[5] & 0xc0) != 0xc0)
3814 return cmd_status(sk
, hdev
->id
,
3815 MGMT_OP_SET_STATIC_ADDRESS
,
3816 MGMT_STATUS_INVALID_PARAMS
);
3821 bacpy(&hdev
->static_addr
, &cp
->bdaddr
);
3823 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_STATIC_ADDRESS
, 0, NULL
, 0);
3825 hci_dev_unlock(hdev
);
3830 static int set_scan_params(struct sock
*sk
, struct hci_dev
*hdev
,
3831 void *data
, u16 len
)
3833 struct mgmt_cp_set_scan_params
*cp
= data
;
3834 __u16 interval
, window
;
3837 BT_DBG("%s", hdev
->name
);
3839 if (!lmp_le_capable(hdev
))
3840 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3841 MGMT_STATUS_NOT_SUPPORTED
);
3843 interval
= __le16_to_cpu(cp
->interval
);
3845 if (interval
< 0x0004 || interval
> 0x4000)
3846 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3847 MGMT_STATUS_INVALID_PARAMS
);
3849 window
= __le16_to_cpu(cp
->window
);
3851 if (window
< 0x0004 || window
> 0x4000)
3852 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3853 MGMT_STATUS_INVALID_PARAMS
);
3855 if (window
> interval
)
3856 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
,
3857 MGMT_STATUS_INVALID_PARAMS
);
3861 hdev
->le_scan_interval
= interval
;
3862 hdev
->le_scan_window
= window
;
3864 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_SET_SCAN_PARAMS
, 0, NULL
, 0);
3866 hci_dev_unlock(hdev
);
3871 static void fast_connectable_complete(struct hci_dev
*hdev
, u8 status
)
3873 struct pending_cmd
*cmd
;
3875 BT_DBG("status 0x%02x", status
);
3879 cmd
= mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE
, hdev
);
3884 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3885 mgmt_status(status
));
3887 struct mgmt_mode
*cp
= cmd
->param
;
3890 set_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
);
3892 clear_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
);
3894 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_FAST_CONNECTABLE
, hdev
);
3895 new_settings(hdev
, cmd
->sk
);
3898 mgmt_pending_remove(cmd
);
3901 hci_dev_unlock(hdev
);
3904 static int set_fast_connectable(struct sock
*sk
, struct hci_dev
*hdev
,
3905 void *data
, u16 len
)
3907 struct mgmt_mode
*cp
= data
;
3908 struct pending_cmd
*cmd
;
3909 struct hci_request req
;
3912 BT_DBG("%s", hdev
->name
);
3914 if (!test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
) ||
3915 hdev
->hci_ver
< BLUETOOTH_VER_1_2
)
3916 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3917 MGMT_STATUS_NOT_SUPPORTED
);
3919 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
3920 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3921 MGMT_STATUS_INVALID_PARAMS
);
3923 if (!hdev_is_powered(hdev
))
3924 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3925 MGMT_STATUS_NOT_POWERED
);
3927 if (!test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
3928 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3929 MGMT_STATUS_REJECTED
);
3933 if (mgmt_pending_find(MGMT_OP_SET_FAST_CONNECTABLE
, hdev
)) {
3934 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3939 if (!!cp
->val
== test_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
)) {
3940 err
= send_settings_rsp(sk
, MGMT_OP_SET_FAST_CONNECTABLE
,
3945 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_FAST_CONNECTABLE
, hdev
,
3952 hci_req_init(&req
, hdev
);
3954 write_fast_connectable(&req
, cp
->val
);
3956 err
= hci_req_run(&req
, fast_connectable_complete
);
3958 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_FAST_CONNECTABLE
,
3959 MGMT_STATUS_FAILED
);
3960 mgmt_pending_remove(cmd
);
3964 hci_dev_unlock(hdev
);
3969 static void set_bredr_scan(struct hci_request
*req
)
3971 struct hci_dev
*hdev
= req
->hdev
;
3974 /* Ensure that fast connectable is disabled. This function will
3975 * not do anything if the page scan parameters are already what
3978 write_fast_connectable(req
, false);
3980 if (test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
3982 if (test_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
))
3983 scan
|= SCAN_INQUIRY
;
3986 hci_req_add(req
, HCI_OP_WRITE_SCAN_ENABLE
, 1, &scan
);
3989 static void set_bredr_complete(struct hci_dev
*hdev
, u8 status
)
3991 struct pending_cmd
*cmd
;
3993 BT_DBG("status 0x%02x", status
);
3997 cmd
= mgmt_pending_find(MGMT_OP_SET_BREDR
, hdev
);
4002 u8 mgmt_err
= mgmt_status(status
);
4004 /* We need to restore the flag if related HCI commands
4007 clear_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
);
4009 cmd_status(cmd
->sk
, cmd
->index
, cmd
->opcode
, mgmt_err
);
4011 send_settings_rsp(cmd
->sk
, MGMT_OP_SET_BREDR
, hdev
);
4012 new_settings(hdev
, cmd
->sk
);
4015 mgmt_pending_remove(cmd
);
4018 hci_dev_unlock(hdev
);
4021 static int set_bredr(struct sock
*sk
, struct hci_dev
*hdev
, void *data
, u16 len
)
4023 struct mgmt_mode
*cp
= data
;
4024 struct pending_cmd
*cmd
;
4025 struct hci_request req
;
4028 BT_DBG("request for %s", hdev
->name
);
4030 if (!lmp_bredr_capable(hdev
) || !lmp_le_capable(hdev
))
4031 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4032 MGMT_STATUS_NOT_SUPPORTED
);
4034 if (!test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
))
4035 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4036 MGMT_STATUS_REJECTED
);
4038 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
4039 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4040 MGMT_STATUS_INVALID_PARAMS
);
4044 if (cp
->val
== test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
4045 err
= send_settings_rsp(sk
, MGMT_OP_SET_BREDR
, hdev
);
4049 if (!hdev_is_powered(hdev
)) {
4051 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4052 clear_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
);
4053 clear_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
);
4054 clear_bit(HCI_FAST_CONNECTABLE
, &hdev
->dev_flags
);
4055 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
4058 change_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
);
4060 err
= send_settings_rsp(sk
, MGMT_OP_SET_BREDR
, hdev
);
4064 err
= new_settings(hdev
, sk
);
4068 /* Reject disabling when powered on */
4070 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4071 MGMT_STATUS_REJECTED
);
4075 if (mgmt_pending_find(MGMT_OP_SET_BREDR
, hdev
)) {
4076 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_BREDR
,
4081 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_BREDR
, hdev
, data
, len
);
4087 /* We need to flip the bit already here so that update_adv_data
4088 * generates the correct flags.
4090 set_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
);
4092 hci_req_init(&req
, hdev
);
4094 if (test_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
))
4095 set_bredr_scan(&req
);
4097 /* Since only the advertising data flags will change, there
4098 * is no need to update the scan response data.
4100 update_adv_data(&req
);
4102 err
= hci_req_run(&req
, set_bredr_complete
);
4104 mgmt_pending_remove(cmd
);
4107 hci_dev_unlock(hdev
);
4111 static int set_secure_conn(struct sock
*sk
, struct hci_dev
*hdev
,
4112 void *data
, u16 len
)
4114 struct mgmt_mode
*cp
= data
;
4115 struct pending_cmd
*cmd
;
4119 BT_DBG("request for %s", hdev
->name
);
4121 status
= mgmt_bredr_support(hdev
);
4123 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4126 if (!lmp_sc_capable(hdev
) &&
4127 !test_bit(HCI_FORCE_SC
, &hdev
->dev_flags
))
4128 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4129 MGMT_STATUS_NOT_SUPPORTED
);
4131 if (cp
->val
!= 0x00 && cp
->val
!= 0x01 && cp
->val
!= 0x02)
4132 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4133 MGMT_STATUS_INVALID_PARAMS
);
4137 if (!hdev_is_powered(hdev
)) {
4141 changed
= !test_and_set_bit(HCI_SC_ENABLED
,
4143 if (cp
->val
== 0x02)
4144 set_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4146 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4148 changed
= test_and_clear_bit(HCI_SC_ENABLED
,
4150 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4153 err
= send_settings_rsp(sk
, MGMT_OP_SET_SECURE_CONN
, hdev
);
4158 err
= new_settings(hdev
, sk
);
4163 if (mgmt_pending_find(MGMT_OP_SET_SECURE_CONN
, hdev
)) {
4164 err
= cmd_status(sk
, hdev
->id
, MGMT_OP_SET_SECURE_CONN
,
4171 if (val
== test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
) &&
4172 (cp
->val
== 0x02) == test_bit(HCI_SC_ONLY
, &hdev
->dev_flags
)) {
4173 err
= send_settings_rsp(sk
, MGMT_OP_SET_SECURE_CONN
, hdev
);
4177 cmd
= mgmt_pending_add(sk
, MGMT_OP_SET_SECURE_CONN
, hdev
, data
, len
);
4183 err
= hci_send_cmd(hdev
, HCI_OP_WRITE_SC_SUPPORT
, 1, &val
);
4185 mgmt_pending_remove(cmd
);
4189 if (cp
->val
== 0x02)
4190 set_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4192 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
4195 hci_dev_unlock(hdev
);
4199 static int set_debug_keys(struct sock
*sk
, struct hci_dev
*hdev
,
4200 void *data
, u16 len
)
4202 struct mgmt_mode
*cp
= data
;
4206 BT_DBG("request for %s", hdev
->name
);
4208 if (cp
->val
!= 0x00 && cp
->val
!= 0x01)
4209 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_DEBUG_KEYS
,
4210 MGMT_STATUS_INVALID_PARAMS
);
4215 changed
= !test_and_set_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
4217 changed
= test_and_clear_bit(HCI_DEBUG_KEYS
, &hdev
->dev_flags
);
4219 err
= send_settings_rsp(sk
, MGMT_OP_SET_DEBUG_KEYS
, hdev
);
4224 err
= new_settings(hdev
, sk
);
4227 hci_dev_unlock(hdev
);
4231 static int set_privacy(struct sock
*sk
, struct hci_dev
*hdev
, void *cp_data
,
4234 struct mgmt_cp_set_privacy
*cp
= cp_data
;
4238 BT_DBG("request for %s", hdev
->name
);
4240 if (!lmp_le_capable(hdev
))
4241 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_PRIVACY
,
4242 MGMT_STATUS_NOT_SUPPORTED
);
4244 if (cp
->privacy
!= 0x00 && cp
->privacy
!= 0x01)
4245 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_PRIVACY
,
4246 MGMT_STATUS_INVALID_PARAMS
);
4248 if (hdev_is_powered(hdev
))
4249 return cmd_status(sk
, hdev
->id
, MGMT_OP_SET_PRIVACY
,
4250 MGMT_STATUS_REJECTED
);
4255 changed
= !test_and_set_bit(HCI_PRIVACY
, &hdev
->dev_flags
);
4256 memcpy(hdev
->irk
, cp
->irk
, sizeof(hdev
->irk
));
4257 set_bit(HCI_RPA_EXPIRED
, &hdev
->dev_flags
);
4259 changed
= test_and_clear_bit(HCI_PRIVACY
, &hdev
->dev_flags
);
4260 memset(hdev
->irk
, 0, sizeof(hdev
->irk
));
4261 clear_bit(HCI_RPA_EXPIRED
, &hdev
->dev_flags
);
4264 err
= send_settings_rsp(sk
, MGMT_OP_SET_PRIVACY
, hdev
);
4269 err
= new_settings(hdev
, sk
);
4272 hci_dev_unlock(hdev
);
4276 static bool irk_is_valid(struct mgmt_irk_info
*irk
)
4278 switch (irk
->addr
.type
) {
4279 case BDADDR_LE_PUBLIC
:
4282 case BDADDR_LE_RANDOM
:
4283 /* Two most significant bits shall be set */
4284 if ((irk
->addr
.bdaddr
.b
[5] & 0xc0) != 0xc0)
4292 static int load_irks(struct sock
*sk
, struct hci_dev
*hdev
, void *cp_data
,
4295 struct mgmt_cp_load_irks
*cp
= cp_data
;
4296 u16 irk_count
, expected_len
;
4299 BT_DBG("request for %s", hdev
->name
);
4301 if (!lmp_le_capable(hdev
))
4302 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_IRKS
,
4303 MGMT_STATUS_NOT_SUPPORTED
);
4305 irk_count
= __le16_to_cpu(cp
->irk_count
);
4307 expected_len
= sizeof(*cp
) + irk_count
* sizeof(struct mgmt_irk_info
);
4308 if (expected_len
!= len
) {
4309 BT_ERR("load_irks: expected %u bytes, got %u bytes",
4311 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_IRKS
,
4312 MGMT_STATUS_INVALID_PARAMS
);
4315 BT_DBG("%s irk_count %u", hdev
->name
, irk_count
);
4317 for (i
= 0; i
< irk_count
; i
++) {
4318 struct mgmt_irk_info
*key
= &cp
->irks
[i
];
4320 if (!irk_is_valid(key
))
4321 return cmd_status(sk
, hdev
->id
,
4323 MGMT_STATUS_INVALID_PARAMS
);
4328 hci_smp_irks_clear(hdev
);
4330 for (i
= 0; i
< irk_count
; i
++) {
4331 struct mgmt_irk_info
*irk
= &cp
->irks
[i
];
4334 if (irk
->addr
.type
== BDADDR_LE_PUBLIC
)
4335 addr_type
= ADDR_LE_DEV_PUBLIC
;
4337 addr_type
= ADDR_LE_DEV_RANDOM
;
4339 hci_add_irk(hdev
, &irk
->addr
.bdaddr
, addr_type
, irk
->val
,
4343 set_bit(HCI_RPA_RESOLVING
, &hdev
->dev_flags
);
4345 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_LOAD_IRKS
, 0, NULL
, 0);
4347 hci_dev_unlock(hdev
);
4352 static bool ltk_is_valid(struct mgmt_ltk_info
*key
)
4354 if (key
->master
!= 0x00 && key
->master
!= 0x01)
4357 switch (key
->addr
.type
) {
4358 case BDADDR_LE_PUBLIC
:
4361 case BDADDR_LE_RANDOM
:
4362 /* Two most significant bits shall be set */
4363 if ((key
->addr
.bdaddr
.b
[5] & 0xc0) != 0xc0)
4371 static int load_long_term_keys(struct sock
*sk
, struct hci_dev
*hdev
,
4372 void *cp_data
, u16 len
)
4374 struct mgmt_cp_load_long_term_keys
*cp
= cp_data
;
4375 u16 key_count
, expected_len
;
4378 BT_DBG("request for %s", hdev
->name
);
4380 if (!lmp_le_capable(hdev
))
4381 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LONG_TERM_KEYS
,
4382 MGMT_STATUS_NOT_SUPPORTED
);
4384 key_count
= __le16_to_cpu(cp
->key_count
);
4386 expected_len
= sizeof(*cp
) + key_count
*
4387 sizeof(struct mgmt_ltk_info
);
4388 if (expected_len
!= len
) {
4389 BT_ERR("load_keys: expected %u bytes, got %u bytes",
4391 return cmd_status(sk
, hdev
->id
, MGMT_OP_LOAD_LONG_TERM_KEYS
,
4392 MGMT_STATUS_INVALID_PARAMS
);
4395 BT_DBG("%s key_count %u", hdev
->name
, key_count
);
4397 for (i
= 0; i
< key_count
; i
++) {
4398 struct mgmt_ltk_info
*key
= &cp
->keys
[i
];
4400 if (!ltk_is_valid(key
))
4401 return cmd_status(sk
, hdev
->id
,
4402 MGMT_OP_LOAD_LONG_TERM_KEYS
,
4403 MGMT_STATUS_INVALID_PARAMS
);
4408 hci_smp_ltks_clear(hdev
);
4410 for (i
= 0; i
< key_count
; i
++) {
4411 struct mgmt_ltk_info
*key
= &cp
->keys
[i
];
4414 if (key
->addr
.type
== BDADDR_LE_PUBLIC
)
4415 addr_type
= ADDR_LE_DEV_PUBLIC
;
4417 addr_type
= ADDR_LE_DEV_RANDOM
;
4422 type
= HCI_SMP_LTK_SLAVE
;
4424 hci_add_ltk(hdev
, &key
->addr
.bdaddr
, addr_type
, type
,
4425 key
->type
, key
->val
, key
->enc_size
, key
->ediv
,
4429 err
= cmd_complete(sk
, hdev
->id
, MGMT_OP_LOAD_LONG_TERM_KEYS
, 0,
4432 hci_dev_unlock(hdev
);
4437 static const struct mgmt_handler
{
4438 int (*func
) (struct sock
*sk
, struct hci_dev
*hdev
, void *data
,
4442 } mgmt_handlers
[] = {
4443 { NULL
}, /* 0x0000 (no command) */
4444 { read_version
, false, MGMT_READ_VERSION_SIZE
},
4445 { read_commands
, false, MGMT_READ_COMMANDS_SIZE
},
4446 { read_index_list
, false, MGMT_READ_INDEX_LIST_SIZE
},
4447 { read_controller_info
, false, MGMT_READ_INFO_SIZE
},
4448 { set_powered
, false, MGMT_SETTING_SIZE
},
4449 { set_discoverable
, false, MGMT_SET_DISCOVERABLE_SIZE
},
4450 { set_connectable
, false, MGMT_SETTING_SIZE
},
4451 { set_fast_connectable
, false, MGMT_SETTING_SIZE
},
4452 { set_pairable
, false, MGMT_SETTING_SIZE
},
4453 { set_link_security
, false, MGMT_SETTING_SIZE
},
4454 { set_ssp
, false, MGMT_SETTING_SIZE
},
4455 { set_hs
, false, MGMT_SETTING_SIZE
},
4456 { set_le
, false, MGMT_SETTING_SIZE
},
4457 { set_dev_class
, false, MGMT_SET_DEV_CLASS_SIZE
},
4458 { set_local_name
, false, MGMT_SET_LOCAL_NAME_SIZE
},
4459 { add_uuid
, false, MGMT_ADD_UUID_SIZE
},
4460 { remove_uuid
, false, MGMT_REMOVE_UUID_SIZE
},
4461 { load_link_keys
, true, MGMT_LOAD_LINK_KEYS_SIZE
},
4462 { load_long_term_keys
, true, MGMT_LOAD_LONG_TERM_KEYS_SIZE
},
4463 { disconnect
, false, MGMT_DISCONNECT_SIZE
},
4464 { get_connections
, false, MGMT_GET_CONNECTIONS_SIZE
},
4465 { pin_code_reply
, false, MGMT_PIN_CODE_REPLY_SIZE
},
4466 { pin_code_neg_reply
, false, MGMT_PIN_CODE_NEG_REPLY_SIZE
},
4467 { set_io_capability
, false, MGMT_SET_IO_CAPABILITY_SIZE
},
4468 { pair_device
, false, MGMT_PAIR_DEVICE_SIZE
},
4469 { cancel_pair_device
, false, MGMT_CANCEL_PAIR_DEVICE_SIZE
},
4470 { unpair_device
, false, MGMT_UNPAIR_DEVICE_SIZE
},
4471 { user_confirm_reply
, false, MGMT_USER_CONFIRM_REPLY_SIZE
},
4472 { user_confirm_neg_reply
, false, MGMT_USER_CONFIRM_NEG_REPLY_SIZE
},
4473 { user_passkey_reply
, false, MGMT_USER_PASSKEY_REPLY_SIZE
},
4474 { user_passkey_neg_reply
, false, MGMT_USER_PASSKEY_NEG_REPLY_SIZE
},
4475 { read_local_oob_data
, false, MGMT_READ_LOCAL_OOB_DATA_SIZE
},
4476 { add_remote_oob_data
, true, MGMT_ADD_REMOTE_OOB_DATA_SIZE
},
4477 { remove_remote_oob_data
, false, MGMT_REMOVE_REMOTE_OOB_DATA_SIZE
},
4478 { start_discovery
, false, MGMT_START_DISCOVERY_SIZE
},
4479 { stop_discovery
, false, MGMT_STOP_DISCOVERY_SIZE
},
4480 { confirm_name
, false, MGMT_CONFIRM_NAME_SIZE
},
4481 { block_device
, false, MGMT_BLOCK_DEVICE_SIZE
},
4482 { unblock_device
, false, MGMT_UNBLOCK_DEVICE_SIZE
},
4483 { set_device_id
, false, MGMT_SET_DEVICE_ID_SIZE
},
4484 { set_advertising
, false, MGMT_SETTING_SIZE
},
4485 { set_bredr
, false, MGMT_SETTING_SIZE
},
4486 { set_static_address
, false, MGMT_SET_STATIC_ADDRESS_SIZE
},
4487 { set_scan_params
, false, MGMT_SET_SCAN_PARAMS_SIZE
},
4488 { set_secure_conn
, false, MGMT_SETTING_SIZE
},
4489 { set_debug_keys
, false, MGMT_SETTING_SIZE
},
4490 { set_privacy
, false, MGMT_SET_PRIVACY_SIZE
},
4491 { load_irks
, true, MGMT_LOAD_IRKS_SIZE
},
4495 int mgmt_control(struct sock
*sk
, struct msghdr
*msg
, size_t msglen
)
4499 struct mgmt_hdr
*hdr
;
4500 u16 opcode
, index
, len
;
4501 struct hci_dev
*hdev
= NULL
;
4502 const struct mgmt_handler
*handler
;
4505 BT_DBG("got %zu bytes", msglen
);
4507 if (msglen
< sizeof(*hdr
))
4510 buf
= kmalloc(msglen
, GFP_KERNEL
);
4514 if (memcpy_fromiovec(buf
, msg
->msg_iov
, msglen
)) {
4520 opcode
= __le16_to_cpu(hdr
->opcode
);
4521 index
= __le16_to_cpu(hdr
->index
);
4522 len
= __le16_to_cpu(hdr
->len
);
4524 if (len
!= msglen
- sizeof(*hdr
)) {
4529 if (index
!= MGMT_INDEX_NONE
) {
4530 hdev
= hci_dev_get(index
);
4532 err
= cmd_status(sk
, index
, opcode
,
4533 MGMT_STATUS_INVALID_INDEX
);
4537 if (test_bit(HCI_SETUP
, &hdev
->dev_flags
) ||
4538 test_bit(HCI_USER_CHANNEL
, &hdev
->dev_flags
)) {
4539 err
= cmd_status(sk
, index
, opcode
,
4540 MGMT_STATUS_INVALID_INDEX
);
4545 if (opcode
>= ARRAY_SIZE(mgmt_handlers
) ||
4546 mgmt_handlers
[opcode
].func
== NULL
) {
4547 BT_DBG("Unknown op %u", opcode
);
4548 err
= cmd_status(sk
, index
, opcode
,
4549 MGMT_STATUS_UNKNOWN_COMMAND
);
4553 if ((hdev
&& opcode
< MGMT_OP_READ_INFO
) ||
4554 (!hdev
&& opcode
>= MGMT_OP_READ_INFO
)) {
4555 err
= cmd_status(sk
, index
, opcode
,
4556 MGMT_STATUS_INVALID_INDEX
);
4560 handler
= &mgmt_handlers
[opcode
];
4562 if ((handler
->var_len
&& len
< handler
->data_len
) ||
4563 (!handler
->var_len
&& len
!= handler
->data_len
)) {
4564 err
= cmd_status(sk
, index
, opcode
,
4565 MGMT_STATUS_INVALID_PARAMS
);
4570 mgmt_init_hdev(sk
, hdev
);
4572 cp
= buf
+ sizeof(*hdr
);
4574 err
= handler
->func(sk
, hdev
, cp
, len
);
4588 void mgmt_index_added(struct hci_dev
*hdev
)
4590 if (hdev
->dev_type
!= HCI_BREDR
)
4593 mgmt_event(MGMT_EV_INDEX_ADDED
, hdev
, NULL
, 0, NULL
);
4596 void mgmt_index_removed(struct hci_dev
*hdev
)
4598 u8 status
= MGMT_STATUS_INVALID_INDEX
;
4600 if (hdev
->dev_type
!= HCI_BREDR
)
4603 mgmt_pending_foreach(0, hdev
, cmd_status_rsp
, &status
);
4605 mgmt_event(MGMT_EV_INDEX_REMOVED
, hdev
, NULL
, 0, NULL
);
4608 static void powered_complete(struct hci_dev
*hdev
, u8 status
)
4610 struct cmd_lookup match
= { NULL
, hdev
};
4612 BT_DBG("status 0x%02x", status
);
4616 mgmt_pending_foreach(MGMT_OP_SET_POWERED
, hdev
, settings_rsp
, &match
);
4618 new_settings(hdev
, match
.sk
);
4620 hci_dev_unlock(hdev
);
4626 static int powered_update_hci(struct hci_dev
*hdev
)
4628 struct hci_request req
;
4631 hci_req_init(&req
, hdev
);
4633 if (test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
) &&
4634 !lmp_host_ssp_capable(hdev
)) {
4637 hci_req_add(&req
, HCI_OP_WRITE_SSP_MODE
, 1, &ssp
);
4640 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
) &&
4641 lmp_bredr_capable(hdev
)) {
4642 struct hci_cp_write_le_host_supported cp
;
4645 cp
.simul
= lmp_le_br_capable(hdev
);
4647 /* Check first if we already have the right
4648 * host state (host features set)
4650 if (cp
.le
!= lmp_host_le_capable(hdev
) ||
4651 cp
.simul
!= lmp_host_le_br_capable(hdev
))
4652 hci_req_add(&req
, HCI_OP_WRITE_LE_HOST_SUPPORTED
,
4656 if (lmp_le_capable(hdev
)) {
4657 /* Make sure the controller has a good default for
4658 * advertising data. This also applies to the case
4659 * where BR/EDR was toggled during the AUTO_OFF phase.
4661 if (test_bit(HCI_LE_ENABLED
, &hdev
->dev_flags
)) {
4662 update_adv_data(&req
);
4663 update_scan_rsp_data(&req
);
4666 if (test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
4667 enable_advertising(&req
);
4670 link_sec
= test_bit(HCI_LINK_SECURITY
, &hdev
->dev_flags
);
4671 if (link_sec
!= test_bit(HCI_AUTH
, &hdev
->flags
))
4672 hci_req_add(&req
, HCI_OP_WRITE_AUTH_ENABLE
,
4673 sizeof(link_sec
), &link_sec
);
4675 if (lmp_bredr_capable(hdev
)) {
4676 if (test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
))
4677 set_bredr_scan(&req
);
4683 return hci_req_run(&req
, powered_complete
);
4686 int mgmt_powered(struct hci_dev
*hdev
, u8 powered
)
4688 struct cmd_lookup match
= { NULL
, hdev
};
4689 u8 status_not_powered
= MGMT_STATUS_NOT_POWERED
;
4690 u8 zero_cod
[] = { 0, 0, 0 };
4693 if (!test_bit(HCI_MGMT
, &hdev
->dev_flags
))
4697 if (powered_update_hci(hdev
) == 0)
4700 mgmt_pending_foreach(MGMT_OP_SET_POWERED
, hdev
, settings_rsp
,
4705 mgmt_pending_foreach(MGMT_OP_SET_POWERED
, hdev
, settings_rsp
, &match
);
4706 mgmt_pending_foreach(0, hdev
, cmd_status_rsp
, &status_not_powered
);
4708 if (memcmp(hdev
->dev_class
, zero_cod
, sizeof(zero_cod
)) != 0)
4709 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED
, hdev
,
4710 zero_cod
, sizeof(zero_cod
), NULL
);
4713 err
= new_settings(hdev
, match
.sk
);
4721 void mgmt_set_powered_failed(struct hci_dev
*hdev
, int err
)
4723 struct pending_cmd
*cmd
;
4726 cmd
= mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
);
4730 if (err
== -ERFKILL
)
4731 status
= MGMT_STATUS_RFKILLED
;
4733 status
= MGMT_STATUS_FAILED
;
4735 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_SET_POWERED
, status
);
4737 mgmt_pending_remove(cmd
);
4740 void mgmt_discoverable_timeout(struct hci_dev
*hdev
)
4742 struct hci_request req
;
4746 /* When discoverable timeout triggers, then just make sure
4747 * the limited discoverable flag is cleared. Even in the case
4748 * of a timeout triggered from general discoverable, it is
4749 * safe to unconditionally clear the flag.
4751 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
4752 clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4754 hci_req_init(&req
, hdev
);
4755 if (test_bit(HCI_BREDR_ENABLED
, &hdev
->dev_flags
)) {
4756 u8 scan
= SCAN_PAGE
;
4757 hci_req_add(&req
, HCI_OP_WRITE_SCAN_ENABLE
,
4758 sizeof(scan
), &scan
);
4761 update_adv_data(&req
);
4762 hci_req_run(&req
, NULL
);
4764 hdev
->discov_timeout
= 0;
4766 new_settings(hdev
, NULL
);
4768 hci_dev_unlock(hdev
);
4771 void mgmt_discoverable(struct hci_dev
*hdev
, u8 discoverable
)
4775 /* Nothing needed here if there's a pending command since that
4776 * commands request completion callback takes care of everything
4779 if (mgmt_pending_find(MGMT_OP_SET_DISCOVERABLE
, hdev
))
4783 changed
= !test_and_set_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4785 clear_bit(HCI_LIMITED_DISCOVERABLE
, &hdev
->dev_flags
);
4786 changed
= test_and_clear_bit(HCI_DISCOVERABLE
, &hdev
->dev_flags
);
4790 struct hci_request req
;
4792 /* In case this change in discoverable was triggered by
4793 * a disabling of connectable there could be a need to
4794 * update the advertising flags.
4796 hci_req_init(&req
, hdev
);
4797 update_adv_data(&req
);
4798 hci_req_run(&req
, NULL
);
4800 new_settings(hdev
, NULL
);
4804 void mgmt_connectable(struct hci_dev
*hdev
, u8 connectable
)
4808 /* Nothing needed here if there's a pending command since that
4809 * commands request completion callback takes care of everything
4812 if (mgmt_pending_find(MGMT_OP_SET_CONNECTABLE
, hdev
))
4816 changed
= !test_and_set_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
4818 changed
= test_and_clear_bit(HCI_CONNECTABLE
, &hdev
->dev_flags
);
4821 new_settings(hdev
, NULL
);
4824 void mgmt_write_scan_failed(struct hci_dev
*hdev
, u8 scan
, u8 status
)
4826 u8 mgmt_err
= mgmt_status(status
);
4828 if (scan
& SCAN_PAGE
)
4829 mgmt_pending_foreach(MGMT_OP_SET_CONNECTABLE
, hdev
,
4830 cmd_status_rsp
, &mgmt_err
);
4832 if (scan
& SCAN_INQUIRY
)
4833 mgmt_pending_foreach(MGMT_OP_SET_DISCOVERABLE
, hdev
,
4834 cmd_status_rsp
, &mgmt_err
);
4837 void mgmt_new_link_key(struct hci_dev
*hdev
, struct link_key
*key
,
4840 struct mgmt_ev_new_link_key ev
;
4842 memset(&ev
, 0, sizeof(ev
));
4844 ev
.store_hint
= persistent
;
4845 bacpy(&ev
.key
.addr
.bdaddr
, &key
->bdaddr
);
4846 ev
.key
.addr
.type
= BDADDR_BREDR
;
4847 ev
.key
.type
= key
->type
;
4848 memcpy(ev
.key
.val
, key
->val
, HCI_LINK_KEY_SIZE
);
4849 ev
.key
.pin_len
= key
->pin_len
;
4851 mgmt_event(MGMT_EV_NEW_LINK_KEY
, hdev
, &ev
, sizeof(ev
), NULL
);
4854 void mgmt_new_ltk(struct hci_dev
*hdev
, struct smp_ltk
*key
)
4856 struct mgmt_ev_new_long_term_key ev
;
4858 memset(&ev
, 0, sizeof(ev
));
4860 /* Devices using resolvable or non-resolvable random addresses
4861 * without providing an indentity resolving key don't require
4862 * to store long term keys. Their addresses will change the
4865 * Only when a remote device provides an identity address
4866 * make sure the long term key is stored. If the remote
4867 * identity is known, the long term keys are internally
4868 * mapped to the identity address. So allow static random
4869 * and public addresses here.
4871 if (key
->bdaddr_type
== ADDR_LE_DEV_RANDOM
&&
4872 (key
->bdaddr
.b
[5] & 0xc0) != 0xc0)
4873 ev
.store_hint
= 0x00;
4875 ev
.store_hint
= 0x01;
4877 bacpy(&ev
.key
.addr
.bdaddr
, &key
->bdaddr
);
4878 ev
.key
.addr
.type
= link_to_bdaddr(LE_LINK
, key
->bdaddr_type
);
4879 ev
.key
.type
= key
->authenticated
;
4880 ev
.key
.enc_size
= key
->enc_size
;
4881 ev
.key
.ediv
= key
->ediv
;
4883 if (key
->type
== HCI_SMP_LTK
)
4886 memcpy(ev
.key
.rand
, key
->rand
, sizeof(key
->rand
));
4887 memcpy(ev
.key
.val
, key
->val
, sizeof(key
->val
));
4889 mgmt_event(MGMT_EV_NEW_LONG_TERM_KEY
, hdev
, &ev
, sizeof(ev
), NULL
);
4892 void mgmt_new_irk(struct hci_dev
*hdev
, struct smp_irk
*irk
)
4894 struct mgmt_ev_new_irk ev
;
4896 memset(&ev
, 0, sizeof(ev
));
4898 /* For identity resolving keys from devices that are already
4899 * using a public address or static random address, do not
4900 * ask for storing this key. The identity resolving key really
4901 * is only mandatory for devices using resovlable random
4904 * Storing all identity resolving keys has the downside that
4905 * they will be also loaded on next boot of they system. More
4906 * identity resolving keys, means more time during scanning is
4907 * needed to actually resolve these addresses.
4909 if (bacmp(&irk
->rpa
, BDADDR_ANY
))
4910 ev
.store_hint
= 0x01;
4912 ev
.store_hint
= 0x00;
4914 bacpy(&ev
.rpa
, &irk
->rpa
);
4915 bacpy(&ev
.irk
.addr
.bdaddr
, &irk
->bdaddr
);
4916 ev
.irk
.addr
.type
= link_to_bdaddr(LE_LINK
, irk
->addr_type
);
4917 memcpy(ev
.irk
.val
, irk
->val
, sizeof(irk
->val
));
4919 mgmt_event(MGMT_EV_NEW_IRK
, hdev
, &ev
, sizeof(ev
), NULL
);
4922 static inline u16
eir_append_data(u8
*eir
, u16 eir_len
, u8 type
, u8
*data
,
4925 eir
[eir_len
++] = sizeof(type
) + data_len
;
4926 eir
[eir_len
++] = type
;
4927 memcpy(&eir
[eir_len
], data
, data_len
);
4928 eir_len
+= data_len
;
4933 void mgmt_device_connected(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
4934 u8 addr_type
, u32 flags
, u8
*name
, u8 name_len
,
4938 struct mgmt_ev_device_connected
*ev
= (void *) buf
;
4941 bacpy(&ev
->addr
.bdaddr
, bdaddr
);
4942 ev
->addr
.type
= link_to_bdaddr(link_type
, addr_type
);
4944 ev
->flags
= __cpu_to_le32(flags
);
4947 eir_len
= eir_append_data(ev
->eir
, 0, EIR_NAME_COMPLETE
,
4950 if (dev_class
&& memcmp(dev_class
, "\0\0\0", 3) != 0)
4951 eir_len
= eir_append_data(ev
->eir
, eir_len
,
4952 EIR_CLASS_OF_DEV
, dev_class
, 3);
4954 ev
->eir_len
= cpu_to_le16(eir_len
);
4956 mgmt_event(MGMT_EV_DEVICE_CONNECTED
, hdev
, buf
,
4957 sizeof(*ev
) + eir_len
, NULL
);
4960 static void disconnect_rsp(struct pending_cmd
*cmd
, void *data
)
4962 struct mgmt_cp_disconnect
*cp
= cmd
->param
;
4963 struct sock
**sk
= data
;
4964 struct mgmt_rp_disconnect rp
;
4966 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
4967 rp
.addr
.type
= cp
->addr
.type
;
4969 cmd_complete(cmd
->sk
, cmd
->index
, MGMT_OP_DISCONNECT
, 0, &rp
,
4975 mgmt_pending_remove(cmd
);
4978 static void unpair_device_rsp(struct pending_cmd
*cmd
, void *data
)
4980 struct hci_dev
*hdev
= data
;
4981 struct mgmt_cp_unpair_device
*cp
= cmd
->param
;
4982 struct mgmt_rp_unpair_device rp
;
4984 memset(&rp
, 0, sizeof(rp
));
4985 bacpy(&rp
.addr
.bdaddr
, &cp
->addr
.bdaddr
);
4986 rp
.addr
.type
= cp
->addr
.type
;
4988 device_unpaired(hdev
, &cp
->addr
.bdaddr
, cp
->addr
.type
, cmd
->sk
);
4990 cmd_complete(cmd
->sk
, cmd
->index
, cmd
->opcode
, 0, &rp
, sizeof(rp
));
4992 mgmt_pending_remove(cmd
);
4995 void mgmt_device_disconnected(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
4996 u8 link_type
, u8 addr_type
, u8 reason
)
4998 struct mgmt_ev_device_disconnected ev
;
4999 struct sock
*sk
= NULL
;
5001 if (link_type
!= ACL_LINK
&& link_type
!= LE_LINK
)
5004 mgmt_pending_foreach(MGMT_OP_DISCONNECT
, hdev
, disconnect_rsp
, &sk
);
5006 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5007 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5010 mgmt_event(MGMT_EV_DEVICE_DISCONNECTED
, hdev
, &ev
, sizeof(ev
), sk
);
5015 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE
, hdev
, unpair_device_rsp
,
5019 void mgmt_disconnect_failed(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5020 u8 link_type
, u8 addr_type
, u8 status
)
5022 u8 bdaddr_type
= link_to_bdaddr(link_type
, addr_type
);
5023 struct mgmt_cp_disconnect
*cp
;
5024 struct mgmt_rp_disconnect rp
;
5025 struct pending_cmd
*cmd
;
5027 mgmt_pending_foreach(MGMT_OP_UNPAIR_DEVICE
, hdev
, unpair_device_rsp
,
5030 cmd
= mgmt_pending_find(MGMT_OP_DISCONNECT
, hdev
);
5036 if (bacmp(bdaddr
, &cp
->addr
.bdaddr
))
5039 if (cp
->addr
.type
!= bdaddr_type
)
5042 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5043 rp
.addr
.type
= bdaddr_type
;
5045 cmd_complete(cmd
->sk
, cmd
->index
, MGMT_OP_DISCONNECT
,
5046 mgmt_status(status
), &rp
, sizeof(rp
));
5048 mgmt_pending_remove(cmd
);
5051 void mgmt_connect_failed(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5052 u8 addr_type
, u8 status
)
5054 struct mgmt_ev_connect_failed ev
;
5056 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5057 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5058 ev
.status
= mgmt_status(status
);
5060 mgmt_event(MGMT_EV_CONNECT_FAILED
, hdev
, &ev
, sizeof(ev
), NULL
);
5063 void mgmt_pin_code_request(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 secure
)
5065 struct mgmt_ev_pin_code_request ev
;
5067 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5068 ev
.addr
.type
= BDADDR_BREDR
;
5071 mgmt_event(MGMT_EV_PIN_CODE_REQUEST
, hdev
, &ev
, sizeof(ev
), NULL
);
5074 void mgmt_pin_code_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5077 struct pending_cmd
*cmd
;
5078 struct mgmt_rp_pin_code_reply rp
;
5080 cmd
= mgmt_pending_find(MGMT_OP_PIN_CODE_REPLY
, hdev
);
5084 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5085 rp
.addr
.type
= BDADDR_BREDR
;
5087 cmd_complete(cmd
->sk
, hdev
->id
, MGMT_OP_PIN_CODE_REPLY
,
5088 mgmt_status(status
), &rp
, sizeof(rp
));
5090 mgmt_pending_remove(cmd
);
5093 void mgmt_pin_code_neg_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5096 struct pending_cmd
*cmd
;
5097 struct mgmt_rp_pin_code_reply rp
;
5099 cmd
= mgmt_pending_find(MGMT_OP_PIN_CODE_NEG_REPLY
, hdev
);
5103 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5104 rp
.addr
.type
= BDADDR_BREDR
;
5106 cmd_complete(cmd
->sk
, hdev
->id
, MGMT_OP_PIN_CODE_NEG_REPLY
,
5107 mgmt_status(status
), &rp
, sizeof(rp
));
5109 mgmt_pending_remove(cmd
);
5112 int mgmt_user_confirm_request(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5113 u8 link_type
, u8 addr_type
, __le32 value
,
5116 struct mgmt_ev_user_confirm_request ev
;
5118 BT_DBG("%s", hdev
->name
);
5120 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5121 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5122 ev
.confirm_hint
= confirm_hint
;
5125 return mgmt_event(MGMT_EV_USER_CONFIRM_REQUEST
, hdev
, &ev
, sizeof(ev
),
5129 int mgmt_user_passkey_request(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5130 u8 link_type
, u8 addr_type
)
5132 struct mgmt_ev_user_passkey_request ev
;
5134 BT_DBG("%s", hdev
->name
);
5136 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5137 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5139 return mgmt_event(MGMT_EV_USER_PASSKEY_REQUEST
, hdev
, &ev
, sizeof(ev
),
5143 static int user_pairing_resp_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5144 u8 link_type
, u8 addr_type
, u8 status
,
5147 struct pending_cmd
*cmd
;
5148 struct mgmt_rp_user_confirm_reply rp
;
5151 cmd
= mgmt_pending_find(opcode
, hdev
);
5155 bacpy(&rp
.addr
.bdaddr
, bdaddr
);
5156 rp
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5157 err
= cmd_complete(cmd
->sk
, hdev
->id
, opcode
, mgmt_status(status
),
5160 mgmt_pending_remove(cmd
);
5165 int mgmt_user_confirm_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5166 u8 link_type
, u8 addr_type
, u8 status
)
5168 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5169 status
, MGMT_OP_USER_CONFIRM_REPLY
);
5172 int mgmt_user_confirm_neg_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5173 u8 link_type
, u8 addr_type
, u8 status
)
5175 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5177 MGMT_OP_USER_CONFIRM_NEG_REPLY
);
5180 int mgmt_user_passkey_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5181 u8 link_type
, u8 addr_type
, u8 status
)
5183 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5184 status
, MGMT_OP_USER_PASSKEY_REPLY
);
5187 int mgmt_user_passkey_neg_reply_complete(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5188 u8 link_type
, u8 addr_type
, u8 status
)
5190 return user_pairing_resp_complete(hdev
, bdaddr
, link_type
, addr_type
,
5192 MGMT_OP_USER_PASSKEY_NEG_REPLY
);
5195 int mgmt_user_passkey_notify(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
,
5196 u8 link_type
, u8 addr_type
, u32 passkey
,
5199 struct mgmt_ev_passkey_notify ev
;
5201 BT_DBG("%s", hdev
->name
);
5203 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5204 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5205 ev
.passkey
= __cpu_to_le32(passkey
);
5206 ev
.entered
= entered
;
5208 return mgmt_event(MGMT_EV_PASSKEY_NOTIFY
, hdev
, &ev
, sizeof(ev
), NULL
);
5211 void mgmt_auth_failed(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5212 u8 addr_type
, u8 status
)
5214 struct mgmt_ev_auth_failed ev
;
5216 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5217 ev
.addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5218 ev
.status
= mgmt_status(status
);
5220 mgmt_event(MGMT_EV_AUTH_FAILED
, hdev
, &ev
, sizeof(ev
), NULL
);
5223 void mgmt_auth_enable_complete(struct hci_dev
*hdev
, u8 status
)
5225 struct cmd_lookup match
= { NULL
, hdev
};
5229 u8 mgmt_err
= mgmt_status(status
);
5230 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY
, hdev
,
5231 cmd_status_rsp
, &mgmt_err
);
5235 if (test_bit(HCI_AUTH
, &hdev
->flags
))
5236 changed
= !test_and_set_bit(HCI_LINK_SECURITY
,
5239 changed
= test_and_clear_bit(HCI_LINK_SECURITY
,
5242 mgmt_pending_foreach(MGMT_OP_SET_LINK_SECURITY
, hdev
, settings_rsp
,
5246 new_settings(hdev
, match
.sk
);
5252 static void clear_eir(struct hci_request
*req
)
5254 struct hci_dev
*hdev
= req
->hdev
;
5255 struct hci_cp_write_eir cp
;
5257 if (!lmp_ext_inq_capable(hdev
))
5260 memset(hdev
->eir
, 0, sizeof(hdev
->eir
));
5262 memset(&cp
, 0, sizeof(cp
));
5264 hci_req_add(req
, HCI_OP_WRITE_EIR
, sizeof(cp
), &cp
);
5267 void mgmt_ssp_enable_complete(struct hci_dev
*hdev
, u8 enable
, u8 status
)
5269 struct cmd_lookup match
= { NULL
, hdev
};
5270 struct hci_request req
;
5271 bool changed
= false;
5274 u8 mgmt_err
= mgmt_status(status
);
5276 if (enable
&& test_and_clear_bit(HCI_SSP_ENABLED
,
5277 &hdev
->dev_flags
)) {
5278 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
5279 new_settings(hdev
, NULL
);
5282 mgmt_pending_foreach(MGMT_OP_SET_SSP
, hdev
, cmd_status_rsp
,
5288 changed
= !test_and_set_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
);
5290 changed
= test_and_clear_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
);
5292 changed
= test_and_clear_bit(HCI_HS_ENABLED
,
5295 clear_bit(HCI_HS_ENABLED
, &hdev
->dev_flags
);
5298 mgmt_pending_foreach(MGMT_OP_SET_SSP
, hdev
, settings_rsp
, &match
);
5301 new_settings(hdev
, match
.sk
);
5306 hci_req_init(&req
, hdev
);
5308 if (test_bit(HCI_SSP_ENABLED
, &hdev
->dev_flags
))
5313 hci_req_run(&req
, NULL
);
5316 void mgmt_sc_enable_complete(struct hci_dev
*hdev
, u8 enable
, u8 status
)
5318 struct cmd_lookup match
= { NULL
, hdev
};
5319 bool changed
= false;
5322 u8 mgmt_err
= mgmt_status(status
);
5325 if (test_and_clear_bit(HCI_SC_ENABLED
,
5327 new_settings(hdev
, NULL
);
5328 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
5331 mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN
, hdev
,
5332 cmd_status_rsp
, &mgmt_err
);
5337 changed
= !test_and_set_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
);
5339 changed
= test_and_clear_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
);
5340 clear_bit(HCI_SC_ONLY
, &hdev
->dev_flags
);
5343 mgmt_pending_foreach(MGMT_OP_SET_SECURE_CONN
, hdev
,
5344 settings_rsp
, &match
);
5347 new_settings(hdev
, match
.sk
);
5353 static void sk_lookup(struct pending_cmd
*cmd
, void *data
)
5355 struct cmd_lookup
*match
= data
;
5357 if (match
->sk
== NULL
) {
5358 match
->sk
= cmd
->sk
;
5359 sock_hold(match
->sk
);
5363 void mgmt_set_class_of_dev_complete(struct hci_dev
*hdev
, u8
*dev_class
,
5366 struct cmd_lookup match
= { NULL
, hdev
, mgmt_status(status
) };
5368 mgmt_pending_foreach(MGMT_OP_SET_DEV_CLASS
, hdev
, sk_lookup
, &match
);
5369 mgmt_pending_foreach(MGMT_OP_ADD_UUID
, hdev
, sk_lookup
, &match
);
5370 mgmt_pending_foreach(MGMT_OP_REMOVE_UUID
, hdev
, sk_lookup
, &match
);
5373 mgmt_event(MGMT_EV_CLASS_OF_DEV_CHANGED
, hdev
, dev_class
, 3,
5380 void mgmt_set_local_name_complete(struct hci_dev
*hdev
, u8
*name
, u8 status
)
5382 struct mgmt_cp_set_local_name ev
;
5383 struct pending_cmd
*cmd
;
5388 memset(&ev
, 0, sizeof(ev
));
5389 memcpy(ev
.name
, name
, HCI_MAX_NAME_LENGTH
);
5390 memcpy(ev
.short_name
, hdev
->short_name
, HCI_MAX_SHORT_NAME_LENGTH
);
5392 cmd
= mgmt_pending_find(MGMT_OP_SET_LOCAL_NAME
, hdev
);
5394 memcpy(hdev
->dev_name
, name
, sizeof(hdev
->dev_name
));
5396 /* If this is a HCI command related to powering on the
5397 * HCI dev don't send any mgmt signals.
5399 if (mgmt_pending_find(MGMT_OP_SET_POWERED
, hdev
))
5403 mgmt_event(MGMT_EV_LOCAL_NAME_CHANGED
, hdev
, &ev
, sizeof(ev
),
5404 cmd
? cmd
->sk
: NULL
);
5407 void mgmt_read_local_oob_data_complete(struct hci_dev
*hdev
, u8
*hash192
,
5408 u8
*randomizer192
, u8
*hash256
,
5409 u8
*randomizer256
, u8 status
)
5411 struct pending_cmd
*cmd
;
5413 BT_DBG("%s status %u", hdev
->name
, status
);
5415 cmd
= mgmt_pending_find(MGMT_OP_READ_LOCAL_OOB_DATA
, hdev
);
5420 cmd_status(cmd
->sk
, hdev
->id
, MGMT_OP_READ_LOCAL_OOB_DATA
,
5421 mgmt_status(status
));
5423 if (test_bit(HCI_SC_ENABLED
, &hdev
->dev_flags
) &&
5424 hash256
&& randomizer256
) {
5425 struct mgmt_rp_read_local_oob_ext_data rp
;
5427 memcpy(rp
.hash192
, hash192
, sizeof(rp
.hash192
));
5428 memcpy(rp
.randomizer192
, randomizer192
,
5429 sizeof(rp
.randomizer192
));
5431 memcpy(rp
.hash256
, hash256
, sizeof(rp
.hash256
));
5432 memcpy(rp
.randomizer256
, randomizer256
,
5433 sizeof(rp
.randomizer256
));
5435 cmd_complete(cmd
->sk
, hdev
->id
,
5436 MGMT_OP_READ_LOCAL_OOB_DATA
, 0,
5439 struct mgmt_rp_read_local_oob_data rp
;
5441 memcpy(rp
.hash
, hash192
, sizeof(rp
.hash
));
5442 memcpy(rp
.randomizer
, randomizer192
,
5443 sizeof(rp
.randomizer
));
5445 cmd_complete(cmd
->sk
, hdev
->id
,
5446 MGMT_OP_READ_LOCAL_OOB_DATA
, 0,
5451 mgmt_pending_remove(cmd
);
5454 void mgmt_device_found(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5455 u8 addr_type
, u8
*dev_class
, s8 rssi
, u8 cfm_name
, u8
5456 ssp
, u8
*eir
, u16 eir_len
)
5459 struct mgmt_ev_device_found
*ev
= (void *) buf
;
5460 struct smp_irk
*irk
;
5463 if (!hci_discovery_active(hdev
))
5466 /* Leave 5 bytes for a potential CoD field */
5467 if (sizeof(*ev
) + eir_len
+ 5 > sizeof(buf
))
5470 memset(buf
, 0, sizeof(buf
));
5472 irk
= hci_get_irk(hdev
, bdaddr
, addr_type
);
5474 bacpy(&ev
->addr
.bdaddr
, &irk
->bdaddr
);
5475 ev
->addr
.type
= link_to_bdaddr(link_type
, irk
->addr_type
);
5477 bacpy(&ev
->addr
.bdaddr
, bdaddr
);
5478 ev
->addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5483 ev
->flags
|= __constant_cpu_to_le32(MGMT_DEV_FOUND_CONFIRM_NAME
);
5485 ev
->flags
|= __constant_cpu_to_le32(MGMT_DEV_FOUND_LEGACY_PAIRING
);
5488 memcpy(ev
->eir
, eir
, eir_len
);
5490 if (dev_class
&& !eir_has_data_type(ev
->eir
, eir_len
, EIR_CLASS_OF_DEV
))
5491 eir_len
= eir_append_data(ev
->eir
, eir_len
, EIR_CLASS_OF_DEV
,
5494 ev
->eir_len
= cpu_to_le16(eir_len
);
5495 ev_size
= sizeof(*ev
) + eir_len
;
5497 mgmt_event(MGMT_EV_DEVICE_FOUND
, hdev
, ev
, ev_size
, NULL
);
5500 void mgmt_remote_name(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 link_type
,
5501 u8 addr_type
, s8 rssi
, u8
*name
, u8 name_len
)
5503 struct mgmt_ev_device_found
*ev
;
5504 char buf
[sizeof(*ev
) + HCI_MAX_NAME_LENGTH
+ 2];
5507 ev
= (struct mgmt_ev_device_found
*) buf
;
5509 memset(buf
, 0, sizeof(buf
));
5511 bacpy(&ev
->addr
.bdaddr
, bdaddr
);
5512 ev
->addr
.type
= link_to_bdaddr(link_type
, addr_type
);
5515 eir_len
= eir_append_data(ev
->eir
, 0, EIR_NAME_COMPLETE
, name
,
5518 ev
->eir_len
= cpu_to_le16(eir_len
);
5520 mgmt_event(MGMT_EV_DEVICE_FOUND
, hdev
, ev
, sizeof(*ev
) + eir_len
, NULL
);
5523 void mgmt_discovering(struct hci_dev
*hdev
, u8 discovering
)
5525 struct mgmt_ev_discovering ev
;
5526 struct pending_cmd
*cmd
;
5528 BT_DBG("%s discovering %u", hdev
->name
, discovering
);
5531 cmd
= mgmt_pending_find(MGMT_OP_START_DISCOVERY
, hdev
);
5533 cmd
= mgmt_pending_find(MGMT_OP_STOP_DISCOVERY
, hdev
);
5536 u8 type
= hdev
->discovery
.type
;
5538 cmd_complete(cmd
->sk
, hdev
->id
, cmd
->opcode
, 0, &type
,
5540 mgmt_pending_remove(cmd
);
5543 memset(&ev
, 0, sizeof(ev
));
5544 ev
.type
= hdev
->discovery
.type
;
5545 ev
.discovering
= discovering
;
5547 mgmt_event(MGMT_EV_DISCOVERING
, hdev
, &ev
, sizeof(ev
), NULL
);
5550 int mgmt_device_blocked(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 type
)
5552 struct pending_cmd
*cmd
;
5553 struct mgmt_ev_device_blocked ev
;
5555 cmd
= mgmt_pending_find(MGMT_OP_BLOCK_DEVICE
, hdev
);
5557 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5558 ev
.addr
.type
= type
;
5560 return mgmt_event(MGMT_EV_DEVICE_BLOCKED
, hdev
, &ev
, sizeof(ev
),
5561 cmd
? cmd
->sk
: NULL
);
5564 int mgmt_device_unblocked(struct hci_dev
*hdev
, bdaddr_t
*bdaddr
, u8 type
)
5566 struct pending_cmd
*cmd
;
5567 struct mgmt_ev_device_unblocked ev
;
5569 cmd
= mgmt_pending_find(MGMT_OP_UNBLOCK_DEVICE
, hdev
);
5571 bacpy(&ev
.addr
.bdaddr
, bdaddr
);
5572 ev
.addr
.type
= type
;
5574 return mgmt_event(MGMT_EV_DEVICE_UNBLOCKED
, hdev
, &ev
, sizeof(ev
),
5575 cmd
? cmd
->sk
: NULL
);
5578 static void adv_enable_complete(struct hci_dev
*hdev
, u8 status
)
5580 BT_DBG("%s status %u", hdev
->name
, status
);
5582 /* Clear the advertising mgmt setting if we failed to re-enable it */
5584 clear_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
5585 new_settings(hdev
, NULL
);
5589 void mgmt_reenable_advertising(struct hci_dev
*hdev
)
5591 struct hci_request req
;
5593 if (hci_conn_num(hdev
, LE_LINK
) > 0)
5596 if (!test_bit(HCI_ADVERTISING
, &hdev
->dev_flags
))
5599 hci_req_init(&req
, hdev
);
5600 enable_advertising(&req
);
5602 /* If this fails we have no option but to let user space know
5603 * that we've disabled advertising.
5605 if (hci_req_run(&req
, adv_enable_complete
) < 0) {
5606 clear_bit(HCI_ADVERTISING
, &hdev
->dev_flags
);
5607 new_settings(hdev
, NULL
);