2 BlueZ - Bluetooth protocol stack for Linux
4 Copyright (C) 2014 Intel Corporation
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License version 2 as
8 published by the Free Software Foundation;
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
11 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
13 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
14 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
15 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
20 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
21 SOFTWARE IS DISCLAIMED.
24 #include <net/bluetooth/bluetooth.h>
25 #include <net/bluetooth/hci_core.h>
28 #include "hci_request.h"
30 void hci_req_init(struct hci_request
*req
, struct hci_dev
*hdev
)
32 skb_queue_head_init(&req
->cmd_q
);
37 static int req_run(struct hci_request
*req
, hci_req_complete_t complete
,
38 hci_req_complete_skb_t complete_skb
)
40 struct hci_dev
*hdev
= req
->hdev
;
44 BT_DBG("length %u", skb_queue_len(&req
->cmd_q
));
46 /* If an error occurred during request building, remove all HCI
47 * commands queued on the HCI request queue.
50 skb_queue_purge(&req
->cmd_q
);
54 /* Do not allow empty requests */
55 if (skb_queue_empty(&req
->cmd_q
))
58 skb
= skb_peek_tail(&req
->cmd_q
);
59 bt_cb(skb
)->hci
.req_complete
= complete
;
60 bt_cb(skb
)->hci
.req_complete_skb
= complete_skb
;
62 spin_lock_irqsave(&hdev
->cmd_q
.lock
, flags
);
63 skb_queue_splice_tail(&req
->cmd_q
, &hdev
->cmd_q
);
64 spin_unlock_irqrestore(&hdev
->cmd_q
.lock
, flags
);
66 queue_work(hdev
->workqueue
, &hdev
->cmd_work
);
71 int hci_req_run(struct hci_request
*req
, hci_req_complete_t complete
)
73 return req_run(req
, complete
, NULL
);
76 int hci_req_run_skb(struct hci_request
*req
, hci_req_complete_skb_t complete
)
78 return req_run(req
, NULL
, complete
);
81 struct sk_buff
*hci_prepare_cmd(struct hci_dev
*hdev
, u16 opcode
, u32 plen
,
84 int len
= HCI_COMMAND_HDR_SIZE
+ plen
;
85 struct hci_command_hdr
*hdr
;
88 skb
= bt_skb_alloc(len
, GFP_ATOMIC
);
92 hdr
= (struct hci_command_hdr
*) skb_put(skb
, HCI_COMMAND_HDR_SIZE
);
93 hdr
->opcode
= cpu_to_le16(opcode
);
97 memcpy(skb_put(skb
, plen
), param
, plen
);
99 BT_DBG("skb len %d", skb
->len
);
101 bt_cb(skb
)->pkt_type
= HCI_COMMAND_PKT
;
102 bt_cb(skb
)->hci
.opcode
= opcode
;
107 /* Queue a command to an asynchronous HCI request */
108 void hci_req_add_ev(struct hci_request
*req
, u16 opcode
, u32 plen
,
109 const void *param
, u8 event
)
111 struct hci_dev
*hdev
= req
->hdev
;
114 BT_DBG("%s opcode 0x%4.4x plen %d", hdev
->name
, opcode
, plen
);
116 /* If an error occurred during request building, there is no point in
117 * queueing the HCI command. We can simply return.
122 skb
= hci_prepare_cmd(hdev
, opcode
, plen
, param
);
124 BT_ERR("%s no memory for command (opcode 0x%4.4x)",
130 if (skb_queue_empty(&req
->cmd_q
))
131 bt_cb(skb
)->hci
.req_start
= true;
133 bt_cb(skb
)->hci
.req_event
= event
;
135 skb_queue_tail(&req
->cmd_q
, skb
);
138 void hci_req_add(struct hci_request
*req
, u16 opcode
, u32 plen
,
141 hci_req_add_ev(req
, opcode
, plen
, param
, 0);
144 void hci_req_add_le_scan_disable(struct hci_request
*req
)
146 struct hci_cp_le_set_scan_enable cp
;
148 memset(&cp
, 0, sizeof(cp
));
149 cp
.enable
= LE_SCAN_DISABLE
;
150 hci_req_add(req
, HCI_OP_LE_SET_SCAN_ENABLE
, sizeof(cp
), &cp
);
153 static void add_to_white_list(struct hci_request
*req
,
154 struct hci_conn_params
*params
)
156 struct hci_cp_le_add_to_white_list cp
;
158 cp
.bdaddr_type
= params
->addr_type
;
159 bacpy(&cp
.bdaddr
, ¶ms
->addr
);
161 hci_req_add(req
, HCI_OP_LE_ADD_TO_WHITE_LIST
, sizeof(cp
), &cp
);
164 static u8
update_white_list(struct hci_request
*req
)
166 struct hci_dev
*hdev
= req
->hdev
;
167 struct hci_conn_params
*params
;
168 struct bdaddr_list
*b
;
169 uint8_t white_list_entries
= 0;
171 /* Go through the current white list programmed into the
172 * controller one by one and check if that address is still
173 * in the list of pending connections or list of devices to
174 * report. If not present in either list, then queue the
175 * command to remove it from the controller.
177 list_for_each_entry(b
, &hdev
->le_white_list
, list
) {
178 /* If the device is neither in pend_le_conns nor
179 * pend_le_reports then remove it from the whitelist.
181 if (!hci_pend_le_action_lookup(&hdev
->pend_le_conns
,
182 &b
->bdaddr
, b
->bdaddr_type
) &&
183 !hci_pend_le_action_lookup(&hdev
->pend_le_reports
,
184 &b
->bdaddr
, b
->bdaddr_type
)) {
185 struct hci_cp_le_del_from_white_list cp
;
187 cp
.bdaddr_type
= b
->bdaddr_type
;
188 bacpy(&cp
.bdaddr
, &b
->bdaddr
);
190 hci_req_add(req
, HCI_OP_LE_DEL_FROM_WHITE_LIST
,
195 if (hci_find_irk_by_addr(hdev
, &b
->bdaddr
, b
->bdaddr_type
)) {
196 /* White list can not be used with RPAs */
200 white_list_entries
++;
203 /* Since all no longer valid white list entries have been
204 * removed, walk through the list of pending connections
205 * and ensure that any new device gets programmed into
208 * If the list of the devices is larger than the list of
209 * available white list entries in the controller, then
210 * just abort and return filer policy value to not use the
213 list_for_each_entry(params
, &hdev
->pend_le_conns
, action
) {
214 if (hci_bdaddr_list_lookup(&hdev
->le_white_list
,
215 ¶ms
->addr
, params
->addr_type
))
218 if (white_list_entries
>= hdev
->le_white_list_size
) {
219 /* Select filter policy to accept all advertising */
223 if (hci_find_irk_by_addr(hdev
, ¶ms
->addr
,
224 params
->addr_type
)) {
225 /* White list can not be used with RPAs */
229 white_list_entries
++;
230 add_to_white_list(req
, params
);
233 /* After adding all new pending connections, walk through
234 * the list of pending reports and also add these to the
235 * white list if there is still space.
237 list_for_each_entry(params
, &hdev
->pend_le_reports
, action
) {
238 if (hci_bdaddr_list_lookup(&hdev
->le_white_list
,
239 ¶ms
->addr
, params
->addr_type
))
242 if (white_list_entries
>= hdev
->le_white_list_size
) {
243 /* Select filter policy to accept all advertising */
247 if (hci_find_irk_by_addr(hdev
, ¶ms
->addr
,
248 params
->addr_type
)) {
249 /* White list can not be used with RPAs */
253 white_list_entries
++;
254 add_to_white_list(req
, params
);
257 /* Select filter policy to use white list */
261 void hci_req_add_le_passive_scan(struct hci_request
*req
)
263 struct hci_cp_le_set_scan_param param_cp
;
264 struct hci_cp_le_set_scan_enable enable_cp
;
265 struct hci_dev
*hdev
= req
->hdev
;
269 /* Set require_privacy to false since no SCAN_REQ are send
270 * during passive scanning. Not using an non-resolvable address
271 * here is important so that peer devices using direct
272 * advertising with our address will be correctly reported
275 if (hci_update_random_address(req
, false, &own_addr_type
))
278 /* Adding or removing entries from the white list must
279 * happen before enabling scanning. The controller does
280 * not allow white list modification while scanning.
282 filter_policy
= update_white_list(req
);
284 /* When the controller is using random resolvable addresses and
285 * with that having LE privacy enabled, then controllers with
286 * Extended Scanner Filter Policies support can now enable support
287 * for handling directed advertising.
289 * So instead of using filter polices 0x00 (no whitelist)
290 * and 0x01 (whitelist enabled) use the new filter policies
291 * 0x02 (no whitelist) and 0x03 (whitelist enabled).
293 if (hci_dev_test_flag(hdev
, HCI_PRIVACY
) &&
294 (hdev
->le_features
[0] & HCI_LE_EXT_SCAN_POLICY
))
295 filter_policy
|= 0x02;
297 memset(¶m_cp
, 0, sizeof(param_cp
));
298 param_cp
.type
= LE_SCAN_PASSIVE
;
299 param_cp
.interval
= cpu_to_le16(hdev
->le_scan_interval
);
300 param_cp
.window
= cpu_to_le16(hdev
->le_scan_window
);
301 param_cp
.own_address_type
= own_addr_type
;
302 param_cp
.filter_policy
= filter_policy
;
303 hci_req_add(req
, HCI_OP_LE_SET_SCAN_PARAM
, sizeof(param_cp
),
306 memset(&enable_cp
, 0, sizeof(enable_cp
));
307 enable_cp
.enable
= LE_SCAN_ENABLE
;
308 enable_cp
.filter_dup
= LE_SCAN_FILTER_DUP_ENABLE
;
309 hci_req_add(req
, HCI_OP_LE_SET_SCAN_ENABLE
, sizeof(enable_cp
),
313 static void set_random_addr(struct hci_request
*req
, bdaddr_t
*rpa
)
315 struct hci_dev
*hdev
= req
->hdev
;
317 /* If we're advertising or initiating an LE connection we can't
318 * go ahead and change the random address at this time. This is
319 * because the eventual initiator address used for the
320 * subsequently created connection will be undefined (some
321 * controllers use the new address and others the one we had
322 * when the operation started).
324 * In this kind of scenario skip the update and let the random
325 * address be updated at the next cycle.
327 if (hci_dev_test_flag(hdev
, HCI_LE_ADV
) ||
328 hci_lookup_le_connect(hdev
)) {
329 BT_DBG("Deferring random address update");
330 hci_dev_set_flag(hdev
, HCI_RPA_EXPIRED
);
334 hci_req_add(req
, HCI_OP_LE_SET_RANDOM_ADDR
, 6, rpa
);
337 int hci_update_random_address(struct hci_request
*req
, bool require_privacy
,
340 struct hci_dev
*hdev
= req
->hdev
;
343 /* If privacy is enabled use a resolvable private address. If
344 * current RPA has expired or there is something else than
345 * the current RPA in use, then generate a new one.
347 if (hci_dev_test_flag(hdev
, HCI_PRIVACY
)) {
350 *own_addr_type
= ADDR_LE_DEV_RANDOM
;
352 if (!hci_dev_test_and_clear_flag(hdev
, HCI_RPA_EXPIRED
) &&
353 !bacmp(&hdev
->random_addr
, &hdev
->rpa
))
356 err
= smp_generate_rpa(hdev
, hdev
->irk
, &hdev
->rpa
);
358 BT_ERR("%s failed to generate new RPA", hdev
->name
);
362 set_random_addr(req
, &hdev
->rpa
);
364 to
= msecs_to_jiffies(hdev
->rpa_timeout
* 1000);
365 queue_delayed_work(hdev
->workqueue
, &hdev
->rpa_expired
, to
);
370 /* In case of required privacy without resolvable private address,
371 * use an non-resolvable private address. This is useful for active
372 * scanning and non-connectable advertising.
374 if (require_privacy
) {
378 /* The non-resolvable private address is generated
379 * from random six bytes with the two most significant
382 get_random_bytes(&nrpa
, 6);
385 /* The non-resolvable private address shall not be
386 * equal to the public address.
388 if (bacmp(&hdev
->bdaddr
, &nrpa
))
392 *own_addr_type
= ADDR_LE_DEV_RANDOM
;
393 set_random_addr(req
, &nrpa
);
397 /* If forcing static address is in use or there is no public
398 * address use the static address as random address (but skip
399 * the HCI command if the current random address is already the
402 * In case BR/EDR has been disabled on a dual-mode controller
403 * and a static address has been configured, then use that
404 * address instead of the public BR/EDR address.
406 if (hci_dev_test_flag(hdev
, HCI_FORCE_STATIC_ADDR
) ||
407 !bacmp(&hdev
->bdaddr
, BDADDR_ANY
) ||
408 (!hci_dev_test_flag(hdev
, HCI_BREDR_ENABLED
) &&
409 bacmp(&hdev
->static_addr
, BDADDR_ANY
))) {
410 *own_addr_type
= ADDR_LE_DEV_RANDOM
;
411 if (bacmp(&hdev
->static_addr
, &hdev
->random_addr
))
412 hci_req_add(req
, HCI_OP_LE_SET_RANDOM_ADDR
, 6,
417 /* Neither privacy nor static address is being used so use a
420 *own_addr_type
= ADDR_LE_DEV_PUBLIC
;
425 static bool disconnected_whitelist_entries(struct hci_dev
*hdev
)
427 struct bdaddr_list
*b
;
429 list_for_each_entry(b
, &hdev
->whitelist
, list
) {
430 struct hci_conn
*conn
;
432 conn
= hci_conn_hash_lookup_ba(hdev
, ACL_LINK
, &b
->bdaddr
);
436 if (conn
->state
!= BT_CONNECTED
&& conn
->state
!= BT_CONFIG
)
443 void __hci_update_page_scan(struct hci_request
*req
)
445 struct hci_dev
*hdev
= req
->hdev
;
448 if (!hci_dev_test_flag(hdev
, HCI_BREDR_ENABLED
))
451 if (!hdev_is_powered(hdev
))
454 if (mgmt_powering_down(hdev
))
457 if (hci_dev_test_flag(hdev
, HCI_CONNECTABLE
) ||
458 disconnected_whitelist_entries(hdev
))
461 scan
= SCAN_DISABLED
;
463 if (test_bit(HCI_PSCAN
, &hdev
->flags
) == !!(scan
& SCAN_PAGE
))
466 if (hci_dev_test_flag(hdev
, HCI_DISCOVERABLE
))
467 scan
|= SCAN_INQUIRY
;
469 hci_req_add(req
, HCI_OP_WRITE_SCAN_ENABLE
, 1, &scan
);
472 void hci_update_page_scan(struct hci_dev
*hdev
)
474 struct hci_request req
;
476 hci_req_init(&req
, hdev
);
477 __hci_update_page_scan(&req
);
478 hci_req_run(&req
, NULL
);
481 /* This function controls the background scanning based on hdev->pend_le_conns
482 * list. If there are pending LE connection we start the background scanning,
483 * otherwise we stop it.
485 * This function requires the caller holds hdev->lock.
487 void __hci_update_background_scan(struct hci_request
*req
)
489 struct hci_dev
*hdev
= req
->hdev
;
491 if (!test_bit(HCI_UP
, &hdev
->flags
) ||
492 test_bit(HCI_INIT
, &hdev
->flags
) ||
493 hci_dev_test_flag(hdev
, HCI_SETUP
) ||
494 hci_dev_test_flag(hdev
, HCI_CONFIG
) ||
495 hci_dev_test_flag(hdev
, HCI_AUTO_OFF
) ||
496 hci_dev_test_flag(hdev
, HCI_UNREGISTER
))
499 /* No point in doing scanning if LE support hasn't been enabled */
500 if (!hci_dev_test_flag(hdev
, HCI_LE_ENABLED
))
503 /* If discovery is active don't interfere with it */
504 if (hdev
->discovery
.state
!= DISCOVERY_STOPPED
)
507 /* Reset RSSI and UUID filters when starting background scanning
508 * since these filters are meant for service discovery only.
510 * The Start Discovery and Start Service Discovery operations
511 * ensure to set proper values for RSSI threshold and UUID
512 * filter list. So it is safe to just reset them here.
514 hci_discovery_filter_clear(hdev
);
516 if (list_empty(&hdev
->pend_le_conns
) &&
517 list_empty(&hdev
->pend_le_reports
)) {
518 /* If there is no pending LE connections or devices
519 * to be scanned for, we should stop the background
523 /* If controller is not scanning we are done. */
524 if (!hci_dev_test_flag(hdev
, HCI_LE_SCAN
))
527 hci_req_add_le_scan_disable(req
);
529 BT_DBG("%s stopping background scanning", hdev
->name
);
531 /* If there is at least one pending LE connection, we should
532 * keep the background scan running.
535 /* If controller is connecting, we should not start scanning
536 * since some controllers are not able to scan and connect at
539 if (hci_lookup_le_connect(hdev
))
542 /* If controller is currently scanning, we stop it to ensure we
543 * don't miss any advertising (due to duplicates filter).
545 if (hci_dev_test_flag(hdev
, HCI_LE_SCAN
))
546 hci_req_add_le_scan_disable(req
);
548 hci_req_add_le_passive_scan(req
);
550 BT_DBG("%s starting background scanning", hdev
->name
);
554 static void update_background_scan_complete(struct hci_dev
*hdev
, u8 status
,
558 BT_DBG("HCI request failed to update background scanning: "
559 "status 0x%2.2x", status
);
562 void hci_update_background_scan(struct hci_dev
*hdev
)
565 struct hci_request req
;
567 hci_req_init(&req
, hdev
);
569 __hci_update_background_scan(&req
);
571 err
= hci_req_run(&req
, update_background_scan_complete
);
572 if (err
&& err
!= -ENODATA
)
573 BT_ERR("Failed to run HCI request: err %d", err
);
576 void __hci_abort_conn(struct hci_request
*req
, struct hci_conn
*conn
,
579 switch (conn
->state
) {
582 if (conn
->type
== AMP_LINK
) {
583 struct hci_cp_disconn_phy_link cp
;
585 cp
.phy_handle
= HCI_PHY_HANDLE(conn
->handle
);
587 hci_req_add(req
, HCI_OP_DISCONN_PHY_LINK
, sizeof(cp
),
590 struct hci_cp_disconnect dc
;
592 dc
.handle
= cpu_to_le16(conn
->handle
);
594 hci_req_add(req
, HCI_OP_DISCONNECT
, sizeof(dc
), &dc
);
597 conn
->state
= BT_DISCONN
;
601 if (conn
->type
== LE_LINK
) {
602 if (test_bit(HCI_CONN_SCANNING
, &conn
->flags
))
604 hci_req_add(req
, HCI_OP_LE_CREATE_CONN_CANCEL
,
606 } else if (conn
->type
== ACL_LINK
) {
607 if (req
->hdev
->hci_ver
< BLUETOOTH_VER_1_2
)
609 hci_req_add(req
, HCI_OP_CREATE_CONN_CANCEL
,
614 if (conn
->type
== ACL_LINK
) {
615 struct hci_cp_reject_conn_req rej
;
617 bacpy(&rej
.bdaddr
, &conn
->dst
);
620 hci_req_add(req
, HCI_OP_REJECT_CONN_REQ
,
622 } else if (conn
->type
== SCO_LINK
|| conn
->type
== ESCO_LINK
) {
623 struct hci_cp_reject_sync_conn_req rej
;
625 bacpy(&rej
.bdaddr
, &conn
->dst
);
627 /* SCO rejection has its own limited set of
628 * allowed error values (0x0D-0x0F) which isn't
629 * compatible with most values passed to this
630 * function. To be safe hard-code one of the
631 * values that's suitable for SCO.
633 rej
.reason
= HCI_ERROR_REMOTE_LOW_RESOURCES
;
635 hci_req_add(req
, HCI_OP_REJECT_SYNC_CONN_REQ
,
640 conn
->state
= BT_CLOSED
;
645 static void abort_conn_complete(struct hci_dev
*hdev
, u8 status
, u16 opcode
)
648 BT_DBG("Failed to abort connection: status 0x%2.2x", status
);
651 int hci_abort_conn(struct hci_conn
*conn
, u8 reason
)
653 struct hci_request req
;
656 hci_req_init(&req
, conn
->hdev
);
658 __hci_abort_conn(&req
, conn
, reason
);
660 err
= hci_req_run(&req
, abort_conn_complete
);
661 if (err
&& err
!= -ENODATA
) {
662 BT_ERR("Failed to run HCI request: err %d", err
);