Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | BlueZ - Bluetooth protocol stack for Linux |
| 3 | |
| 4 | Copyright (C) 2014 Intel Corporation |
| 5 | |
| 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; |
| 9 | |
| 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. |
| 18 | |
| 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. |
| 22 | */ |
| 23 | |
| 24 | #include <net/bluetooth/bluetooth.h> |
| 25 | #include <net/bluetooth/hci_core.h> |
| 26 | |
| 27 | #include "smp.h" |
| 28 | #include "hci_request.h" |
| 29 | |
| 30 | void hci_req_init(struct hci_request *req, struct hci_dev *hdev) |
| 31 | { |
| 32 | skb_queue_head_init(&req->cmd_q); |
| 33 | req->hdev = hdev; |
| 34 | req->err = 0; |
| 35 | } |
| 36 | |
Johan Hedberg | e6214487 | 2015-04-02 13:41:08 +0300 | [diff] [blame] | 37 | static int req_run(struct hci_request *req, hci_req_complete_t complete, |
| 38 | hci_req_complete_skb_t complete_skb) |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 39 | { |
| 40 | struct hci_dev *hdev = req->hdev; |
| 41 | struct sk_buff *skb; |
| 42 | unsigned long flags; |
| 43 | |
| 44 | BT_DBG("length %u", skb_queue_len(&req->cmd_q)); |
| 45 | |
| 46 | /* If an error occurred during request building, remove all HCI |
| 47 | * commands queued on the HCI request queue. |
| 48 | */ |
| 49 | if (req->err) { |
| 50 | skb_queue_purge(&req->cmd_q); |
| 51 | return req->err; |
| 52 | } |
| 53 | |
| 54 | /* Do not allow empty requests */ |
| 55 | if (skb_queue_empty(&req->cmd_q)) |
| 56 | return -ENODATA; |
| 57 | |
| 58 | skb = skb_peek_tail(&req->cmd_q); |
Johan Hedberg | 44d2713 | 2015-11-05 09:31:40 +0200 | [diff] [blame^] | 59 | if (complete) { |
| 60 | bt_cb(skb)->hci.req_complete = complete; |
| 61 | } else if (complete_skb) { |
| 62 | bt_cb(skb)->hci.req_complete_skb = complete_skb; |
| 63 | bt_cb(skb)->hci.req_flags |= HCI_REQ_SKB; |
| 64 | } |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 65 | |
| 66 | spin_lock_irqsave(&hdev->cmd_q.lock, flags); |
| 67 | skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q); |
| 68 | spin_unlock_irqrestore(&hdev->cmd_q.lock, flags); |
| 69 | |
| 70 | queue_work(hdev->workqueue, &hdev->cmd_work); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Johan Hedberg | e6214487 | 2015-04-02 13:41:08 +0300 | [diff] [blame] | 75 | int hci_req_run(struct hci_request *req, hci_req_complete_t complete) |
| 76 | { |
| 77 | return req_run(req, complete, NULL); |
| 78 | } |
| 79 | |
| 80 | int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete) |
| 81 | { |
| 82 | return req_run(req, NULL, complete); |
| 83 | } |
| 84 | |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 85 | struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen, |
| 86 | const void *param) |
| 87 | { |
| 88 | int len = HCI_COMMAND_HDR_SIZE + plen; |
| 89 | struct hci_command_hdr *hdr; |
| 90 | struct sk_buff *skb; |
| 91 | |
| 92 | skb = bt_skb_alloc(len, GFP_ATOMIC); |
| 93 | if (!skb) |
| 94 | return NULL; |
| 95 | |
| 96 | hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE); |
| 97 | hdr->opcode = cpu_to_le16(opcode); |
| 98 | hdr->plen = plen; |
| 99 | |
| 100 | if (plen) |
| 101 | memcpy(skb_put(skb, plen), param, plen); |
| 102 | |
| 103 | BT_DBG("skb len %d", skb->len); |
| 104 | |
Marcel Holtmann | d79f34e | 2015-11-05 07:10:00 +0100 | [diff] [blame] | 105 | hci_skb_pkt_type(skb) = HCI_COMMAND_PKT; |
| 106 | hci_skb_opcode(skb) = opcode; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 107 | |
| 108 | return skb; |
| 109 | } |
| 110 | |
| 111 | /* Queue a command to an asynchronous HCI request */ |
| 112 | void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen, |
| 113 | const void *param, u8 event) |
| 114 | { |
| 115 | struct hci_dev *hdev = req->hdev; |
| 116 | struct sk_buff *skb; |
| 117 | |
| 118 | BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen); |
| 119 | |
| 120 | /* If an error occurred during request building, there is no point in |
| 121 | * queueing the HCI command. We can simply return. |
| 122 | */ |
| 123 | if (req->err) |
| 124 | return; |
| 125 | |
| 126 | skb = hci_prepare_cmd(hdev, opcode, plen, param); |
| 127 | if (!skb) { |
| 128 | BT_ERR("%s no memory for command (opcode 0x%4.4x)", |
| 129 | hdev->name, opcode); |
| 130 | req->err = -ENOMEM; |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | if (skb_queue_empty(&req->cmd_q)) |
Johan Hedberg | 44d2713 | 2015-11-05 09:31:40 +0200 | [diff] [blame^] | 135 | bt_cb(skb)->hci.req_flags |= HCI_REQ_START; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 136 | |
Marcel Holtmann | 242c0eb | 2015-10-25 22:45:53 +0100 | [diff] [blame] | 137 | bt_cb(skb)->hci.req_event = event; |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 138 | |
| 139 | skb_queue_tail(&req->cmd_q, skb); |
| 140 | } |
| 141 | |
| 142 | void hci_req_add(struct hci_request *req, u16 opcode, u32 plen, |
| 143 | const void *param) |
| 144 | { |
| 145 | hci_req_add_ev(req, opcode, plen, param, 0); |
| 146 | } |
| 147 | |
| 148 | void hci_req_add_le_scan_disable(struct hci_request *req) |
| 149 | { |
| 150 | struct hci_cp_le_set_scan_enable cp; |
| 151 | |
| 152 | memset(&cp, 0, sizeof(cp)); |
| 153 | cp.enable = LE_SCAN_DISABLE; |
| 154 | hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp); |
| 155 | } |
| 156 | |
| 157 | static void add_to_white_list(struct hci_request *req, |
| 158 | struct hci_conn_params *params) |
| 159 | { |
| 160 | struct hci_cp_le_add_to_white_list cp; |
| 161 | |
| 162 | cp.bdaddr_type = params->addr_type; |
| 163 | bacpy(&cp.bdaddr, ¶ms->addr); |
| 164 | |
| 165 | hci_req_add(req, HCI_OP_LE_ADD_TO_WHITE_LIST, sizeof(cp), &cp); |
| 166 | } |
| 167 | |
| 168 | static u8 update_white_list(struct hci_request *req) |
| 169 | { |
| 170 | struct hci_dev *hdev = req->hdev; |
| 171 | struct hci_conn_params *params; |
| 172 | struct bdaddr_list *b; |
| 173 | uint8_t white_list_entries = 0; |
| 174 | |
| 175 | /* Go through the current white list programmed into the |
| 176 | * controller one by one and check if that address is still |
| 177 | * in the list of pending connections or list of devices to |
| 178 | * report. If not present in either list, then queue the |
| 179 | * command to remove it from the controller. |
| 180 | */ |
| 181 | list_for_each_entry(b, &hdev->le_white_list, list) { |
| 182 | struct hci_cp_le_del_from_white_list cp; |
| 183 | |
| 184 | if (hci_pend_le_action_lookup(&hdev->pend_le_conns, |
| 185 | &b->bdaddr, b->bdaddr_type) || |
| 186 | hci_pend_le_action_lookup(&hdev->pend_le_reports, |
| 187 | &b->bdaddr, b->bdaddr_type)) { |
| 188 | white_list_entries++; |
| 189 | continue; |
| 190 | } |
| 191 | |
| 192 | cp.bdaddr_type = b->bdaddr_type; |
| 193 | bacpy(&cp.bdaddr, &b->bdaddr); |
| 194 | |
| 195 | hci_req_add(req, HCI_OP_LE_DEL_FROM_WHITE_LIST, |
| 196 | sizeof(cp), &cp); |
| 197 | } |
| 198 | |
| 199 | /* Since all no longer valid white list entries have been |
| 200 | * removed, walk through the list of pending connections |
| 201 | * and ensure that any new device gets programmed into |
| 202 | * the controller. |
| 203 | * |
| 204 | * If the list of the devices is larger than the list of |
| 205 | * available white list entries in the controller, then |
| 206 | * just abort and return filer policy value to not use the |
| 207 | * white list. |
| 208 | */ |
| 209 | list_for_each_entry(params, &hdev->pend_le_conns, action) { |
| 210 | if (hci_bdaddr_list_lookup(&hdev->le_white_list, |
| 211 | ¶ms->addr, params->addr_type)) |
| 212 | continue; |
| 213 | |
| 214 | if (white_list_entries >= hdev->le_white_list_size) { |
| 215 | /* Select filter policy to accept all advertising */ |
| 216 | return 0x00; |
| 217 | } |
| 218 | |
| 219 | if (hci_find_irk_by_addr(hdev, ¶ms->addr, |
| 220 | params->addr_type)) { |
| 221 | /* White list can not be used with RPAs */ |
| 222 | return 0x00; |
| 223 | } |
| 224 | |
| 225 | white_list_entries++; |
| 226 | add_to_white_list(req, params); |
| 227 | } |
| 228 | |
| 229 | /* After adding all new pending connections, walk through |
| 230 | * the list of pending reports and also add these to the |
| 231 | * white list if there is still space. |
| 232 | */ |
| 233 | list_for_each_entry(params, &hdev->pend_le_reports, action) { |
| 234 | if (hci_bdaddr_list_lookup(&hdev->le_white_list, |
| 235 | ¶ms->addr, params->addr_type)) |
| 236 | continue; |
| 237 | |
| 238 | if (white_list_entries >= hdev->le_white_list_size) { |
| 239 | /* Select filter policy to accept all advertising */ |
| 240 | return 0x00; |
| 241 | } |
| 242 | |
| 243 | if (hci_find_irk_by_addr(hdev, ¶ms->addr, |
| 244 | params->addr_type)) { |
| 245 | /* White list can not be used with RPAs */ |
| 246 | return 0x00; |
| 247 | } |
| 248 | |
| 249 | white_list_entries++; |
| 250 | add_to_white_list(req, params); |
| 251 | } |
| 252 | |
| 253 | /* Select filter policy to use white list */ |
| 254 | return 0x01; |
| 255 | } |
| 256 | |
| 257 | void hci_req_add_le_passive_scan(struct hci_request *req) |
| 258 | { |
| 259 | struct hci_cp_le_set_scan_param param_cp; |
| 260 | struct hci_cp_le_set_scan_enable enable_cp; |
| 261 | struct hci_dev *hdev = req->hdev; |
| 262 | u8 own_addr_type; |
| 263 | u8 filter_policy; |
| 264 | |
| 265 | /* Set require_privacy to false since no SCAN_REQ are send |
| 266 | * during passive scanning. Not using an non-resolvable address |
| 267 | * here is important so that peer devices using direct |
| 268 | * advertising with our address will be correctly reported |
| 269 | * by the controller. |
| 270 | */ |
| 271 | if (hci_update_random_address(req, false, &own_addr_type)) |
| 272 | return; |
| 273 | |
| 274 | /* Adding or removing entries from the white list must |
| 275 | * happen before enabling scanning. The controller does |
| 276 | * not allow white list modification while scanning. |
| 277 | */ |
| 278 | filter_policy = update_white_list(req); |
| 279 | |
| 280 | /* When the controller is using random resolvable addresses and |
| 281 | * with that having LE privacy enabled, then controllers with |
| 282 | * Extended Scanner Filter Policies support can now enable support |
| 283 | * for handling directed advertising. |
| 284 | * |
| 285 | * So instead of using filter polices 0x00 (no whitelist) |
| 286 | * and 0x01 (whitelist enabled) use the new filter policies |
| 287 | * 0x02 (no whitelist) and 0x03 (whitelist enabled). |
| 288 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 289 | if (hci_dev_test_flag(hdev, HCI_PRIVACY) && |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 290 | (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY)) |
| 291 | filter_policy |= 0x02; |
| 292 | |
| 293 | memset(¶m_cp, 0, sizeof(param_cp)); |
| 294 | param_cp.type = LE_SCAN_PASSIVE; |
| 295 | param_cp.interval = cpu_to_le16(hdev->le_scan_interval); |
| 296 | param_cp.window = cpu_to_le16(hdev->le_scan_window); |
| 297 | param_cp.own_address_type = own_addr_type; |
| 298 | param_cp.filter_policy = filter_policy; |
| 299 | hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp), |
| 300 | ¶m_cp); |
| 301 | |
| 302 | memset(&enable_cp, 0, sizeof(enable_cp)); |
| 303 | enable_cp.enable = LE_SCAN_ENABLE; |
| 304 | enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE; |
| 305 | hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp), |
| 306 | &enable_cp); |
| 307 | } |
| 308 | |
| 309 | static void set_random_addr(struct hci_request *req, bdaddr_t *rpa) |
| 310 | { |
| 311 | struct hci_dev *hdev = req->hdev; |
| 312 | |
| 313 | /* If we're advertising or initiating an LE connection we can't |
| 314 | * go ahead and change the random address at this time. This is |
| 315 | * because the eventual initiator address used for the |
| 316 | * subsequently created connection will be undefined (some |
| 317 | * controllers use the new address and others the one we had |
| 318 | * when the operation started). |
| 319 | * |
| 320 | * In this kind of scenario skip the update and let the random |
| 321 | * address be updated at the next cycle. |
| 322 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 323 | if (hci_dev_test_flag(hdev, HCI_LE_ADV) || |
Jakub Pawlowski | e7d9ab7 | 2015-08-07 20:22:52 +0200 | [diff] [blame] | 324 | hci_lookup_le_connect(hdev)) { |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 325 | BT_DBG("Deferring random address update"); |
Marcel Holtmann | a1536da | 2015-03-13 02:11:01 -0700 | [diff] [blame] | 326 | hci_dev_set_flag(hdev, HCI_RPA_EXPIRED); |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 327 | return; |
| 328 | } |
| 329 | |
| 330 | hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa); |
| 331 | } |
| 332 | |
| 333 | int hci_update_random_address(struct hci_request *req, bool require_privacy, |
| 334 | u8 *own_addr_type) |
| 335 | { |
| 336 | struct hci_dev *hdev = req->hdev; |
| 337 | int err; |
| 338 | |
| 339 | /* If privacy is enabled use a resolvable private address. If |
| 340 | * current RPA has expired or there is something else than |
| 341 | * the current RPA in use, then generate a new one. |
| 342 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 343 | if (hci_dev_test_flag(hdev, HCI_PRIVACY)) { |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 344 | int to; |
| 345 | |
| 346 | *own_addr_type = ADDR_LE_DEV_RANDOM; |
| 347 | |
Marcel Holtmann | a69d892 | 2015-03-13 02:11:05 -0700 | [diff] [blame] | 348 | if (!hci_dev_test_and_clear_flag(hdev, HCI_RPA_EXPIRED) && |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 349 | !bacmp(&hdev->random_addr, &hdev->rpa)) |
| 350 | return 0; |
| 351 | |
| 352 | err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa); |
| 353 | if (err < 0) { |
| 354 | BT_ERR("%s failed to generate new RPA", hdev->name); |
| 355 | return err; |
| 356 | } |
| 357 | |
| 358 | set_random_addr(req, &hdev->rpa); |
| 359 | |
| 360 | to = msecs_to_jiffies(hdev->rpa_timeout * 1000); |
| 361 | queue_delayed_work(hdev->workqueue, &hdev->rpa_expired, to); |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | /* In case of required privacy without resolvable private address, |
| 367 | * use an non-resolvable private address. This is useful for active |
| 368 | * scanning and non-connectable advertising. |
| 369 | */ |
| 370 | if (require_privacy) { |
| 371 | bdaddr_t nrpa; |
| 372 | |
| 373 | while (true) { |
| 374 | /* The non-resolvable private address is generated |
| 375 | * from random six bytes with the two most significant |
| 376 | * bits cleared. |
| 377 | */ |
| 378 | get_random_bytes(&nrpa, 6); |
| 379 | nrpa.b[5] &= 0x3f; |
| 380 | |
| 381 | /* The non-resolvable private address shall not be |
| 382 | * equal to the public address. |
| 383 | */ |
| 384 | if (bacmp(&hdev->bdaddr, &nrpa)) |
| 385 | break; |
| 386 | } |
| 387 | |
| 388 | *own_addr_type = ADDR_LE_DEV_RANDOM; |
| 389 | set_random_addr(req, &nrpa); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | /* If forcing static address is in use or there is no public |
| 394 | * address use the static address as random address (but skip |
| 395 | * the HCI command if the current random address is already the |
| 396 | * static one. |
Marcel Holtmann | 50b5b95 | 2014-12-19 23:05:35 +0100 | [diff] [blame] | 397 | * |
| 398 | * In case BR/EDR has been disabled on a dual-mode controller |
| 399 | * and a static address has been configured, then use that |
| 400 | * address instead of the public BR/EDR address. |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 401 | */ |
Marcel Holtmann | b7cb93e | 2015-03-13 10:20:35 -0700 | [diff] [blame] | 402 | if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) || |
Marcel Holtmann | 50b5b95 | 2014-12-19 23:05:35 +0100 | [diff] [blame] | 403 | !bacmp(&hdev->bdaddr, BDADDR_ANY) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 404 | (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) && |
Marcel Holtmann | 50b5b95 | 2014-12-19 23:05:35 +0100 | [diff] [blame] | 405 | bacmp(&hdev->static_addr, BDADDR_ANY))) { |
Johan Hedberg | 0857dd3 | 2014-12-19 13:40:20 +0200 | [diff] [blame] | 406 | *own_addr_type = ADDR_LE_DEV_RANDOM; |
| 407 | if (bacmp(&hdev->static_addr, &hdev->random_addr)) |
| 408 | hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, |
| 409 | &hdev->static_addr); |
| 410 | return 0; |
| 411 | } |
| 412 | |
| 413 | /* Neither privacy nor static address is being used so use a |
| 414 | * public address. |
| 415 | */ |
| 416 | *own_addr_type = ADDR_LE_DEV_PUBLIC; |
| 417 | |
| 418 | return 0; |
| 419 | } |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 420 | |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 421 | static bool disconnected_whitelist_entries(struct hci_dev *hdev) |
| 422 | { |
| 423 | struct bdaddr_list *b; |
| 424 | |
| 425 | list_for_each_entry(b, &hdev->whitelist, list) { |
| 426 | struct hci_conn *conn; |
| 427 | |
| 428 | conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr); |
| 429 | if (!conn) |
| 430 | return true; |
| 431 | |
| 432 | if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG) |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | return false; |
| 437 | } |
| 438 | |
| 439 | void __hci_update_page_scan(struct hci_request *req) |
| 440 | { |
| 441 | struct hci_dev *hdev = req->hdev; |
| 442 | u8 scan; |
| 443 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 444 | if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED)) |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 445 | return; |
| 446 | |
| 447 | if (!hdev_is_powered(hdev)) |
| 448 | return; |
| 449 | |
| 450 | if (mgmt_powering_down(hdev)) |
| 451 | return; |
| 452 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 453 | if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) || |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 454 | disconnected_whitelist_entries(hdev)) |
| 455 | scan = SCAN_PAGE; |
| 456 | else |
| 457 | scan = SCAN_DISABLED; |
| 458 | |
| 459 | if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE)) |
| 460 | return; |
| 461 | |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 462 | if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE)) |
Johan Hedberg | 405a261 | 2014-12-19 23:18:22 +0200 | [diff] [blame] | 463 | scan |= SCAN_INQUIRY; |
| 464 | |
| 465 | hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan); |
| 466 | } |
| 467 | |
| 468 | void hci_update_page_scan(struct hci_dev *hdev) |
| 469 | { |
| 470 | struct hci_request req; |
| 471 | |
| 472 | hci_req_init(&req, hdev); |
| 473 | __hci_update_page_scan(&req); |
| 474 | hci_req_run(&req, NULL); |
| 475 | } |
| 476 | |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 477 | /* This function controls the background scanning based on hdev->pend_le_conns |
| 478 | * list. If there are pending LE connection we start the background scanning, |
| 479 | * otherwise we stop it. |
| 480 | * |
| 481 | * This function requires the caller holds hdev->lock. |
| 482 | */ |
| 483 | void __hci_update_background_scan(struct hci_request *req) |
| 484 | { |
| 485 | struct hci_dev *hdev = req->hdev; |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 486 | |
| 487 | if (!test_bit(HCI_UP, &hdev->flags) || |
| 488 | test_bit(HCI_INIT, &hdev->flags) || |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 489 | hci_dev_test_flag(hdev, HCI_SETUP) || |
| 490 | hci_dev_test_flag(hdev, HCI_CONFIG) || |
| 491 | hci_dev_test_flag(hdev, HCI_AUTO_OFF) || |
| 492 | hci_dev_test_flag(hdev, HCI_UNREGISTER)) |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 493 | return; |
| 494 | |
| 495 | /* No point in doing scanning if LE support hasn't been enabled */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 496 | if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED)) |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 497 | return; |
| 498 | |
| 499 | /* If discovery is active don't interfere with it */ |
| 500 | if (hdev->discovery.state != DISCOVERY_STOPPED) |
| 501 | return; |
| 502 | |
| 503 | /* Reset RSSI and UUID filters when starting background scanning |
| 504 | * since these filters are meant for service discovery only. |
| 505 | * |
| 506 | * The Start Discovery and Start Service Discovery operations |
| 507 | * ensure to set proper values for RSSI threshold and UUID |
| 508 | * filter list. So it is safe to just reset them here. |
| 509 | */ |
| 510 | hci_discovery_filter_clear(hdev); |
| 511 | |
| 512 | if (list_empty(&hdev->pend_le_conns) && |
| 513 | list_empty(&hdev->pend_le_reports)) { |
| 514 | /* If there is no pending LE connections or devices |
| 515 | * to be scanned for, we should stop the background |
| 516 | * scanning. |
| 517 | */ |
| 518 | |
| 519 | /* If controller is not scanning we are done. */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 520 | if (!hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 521 | return; |
| 522 | |
| 523 | hci_req_add_le_scan_disable(req); |
| 524 | |
| 525 | BT_DBG("%s stopping background scanning", hdev->name); |
| 526 | } else { |
| 527 | /* If there is at least one pending LE connection, we should |
| 528 | * keep the background scan running. |
| 529 | */ |
| 530 | |
| 531 | /* If controller is connecting, we should not start scanning |
| 532 | * since some controllers are not able to scan and connect at |
| 533 | * the same time. |
| 534 | */ |
Jakub Pawlowski | e7d9ab7 | 2015-08-07 20:22:52 +0200 | [diff] [blame] | 535 | if (hci_lookup_le_connect(hdev)) |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 536 | return; |
| 537 | |
| 538 | /* If controller is currently scanning, we stop it to ensure we |
| 539 | * don't miss any advertising (due to duplicates filter). |
| 540 | */ |
Marcel Holtmann | d7a5a11 | 2015-03-13 02:11:00 -0700 | [diff] [blame] | 541 | if (hci_dev_test_flag(hdev, HCI_LE_SCAN)) |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 542 | hci_req_add_le_scan_disable(req); |
| 543 | |
| 544 | hci_req_add_le_passive_scan(req); |
| 545 | |
| 546 | BT_DBG("%s starting background scanning", hdev->name); |
| 547 | } |
| 548 | } |
| 549 | |
Marcel Holtmann | 1904a85 | 2015-01-11 13:50:44 -0800 | [diff] [blame] | 550 | static void update_background_scan_complete(struct hci_dev *hdev, u8 status, |
| 551 | u16 opcode) |
Johan Hedberg | 2cf2221 | 2014-12-19 22:26:00 +0200 | [diff] [blame] | 552 | { |
| 553 | if (status) |
| 554 | BT_DBG("HCI request failed to update background scanning: " |
| 555 | "status 0x%2.2x", status); |
| 556 | } |
| 557 | |
| 558 | void hci_update_background_scan(struct hci_dev *hdev) |
| 559 | { |
| 560 | int err; |
| 561 | struct hci_request req; |
| 562 | |
| 563 | hci_req_init(&req, hdev); |
| 564 | |
| 565 | __hci_update_background_scan(&req); |
| 566 | |
| 567 | err = hci_req_run(&req, update_background_scan_complete); |
| 568 | if (err && err != -ENODATA) |
| 569 | BT_ERR("Failed to run HCI request: err %d", err); |
| 570 | } |
Johan Hedberg | dcc0f0d | 2015-10-22 10:49:37 +0300 | [diff] [blame] | 571 | |
| 572 | void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn, |
| 573 | u8 reason) |
| 574 | { |
| 575 | switch (conn->state) { |
| 576 | case BT_CONNECTED: |
| 577 | case BT_CONFIG: |
| 578 | if (conn->type == AMP_LINK) { |
| 579 | struct hci_cp_disconn_phy_link cp; |
| 580 | |
| 581 | cp.phy_handle = HCI_PHY_HANDLE(conn->handle); |
| 582 | cp.reason = reason; |
| 583 | hci_req_add(req, HCI_OP_DISCONN_PHY_LINK, sizeof(cp), |
| 584 | &cp); |
| 585 | } else { |
| 586 | struct hci_cp_disconnect dc; |
| 587 | |
| 588 | dc.handle = cpu_to_le16(conn->handle); |
| 589 | dc.reason = reason; |
| 590 | hci_req_add(req, HCI_OP_DISCONNECT, sizeof(dc), &dc); |
| 591 | } |
| 592 | |
| 593 | conn->state = BT_DISCONN; |
| 594 | |
| 595 | break; |
| 596 | case BT_CONNECT: |
| 597 | if (conn->type == LE_LINK) { |
| 598 | if (test_bit(HCI_CONN_SCANNING, &conn->flags)) |
| 599 | break; |
| 600 | hci_req_add(req, HCI_OP_LE_CREATE_CONN_CANCEL, |
| 601 | 0, NULL); |
| 602 | } else if (conn->type == ACL_LINK) { |
| 603 | if (req->hdev->hci_ver < BLUETOOTH_VER_1_2) |
| 604 | break; |
| 605 | hci_req_add(req, HCI_OP_CREATE_CONN_CANCEL, |
| 606 | 6, &conn->dst); |
| 607 | } |
| 608 | break; |
| 609 | case BT_CONNECT2: |
| 610 | if (conn->type == ACL_LINK) { |
| 611 | struct hci_cp_reject_conn_req rej; |
| 612 | |
| 613 | bacpy(&rej.bdaddr, &conn->dst); |
| 614 | rej.reason = reason; |
| 615 | |
| 616 | hci_req_add(req, HCI_OP_REJECT_CONN_REQ, |
| 617 | sizeof(rej), &rej); |
| 618 | } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) { |
| 619 | struct hci_cp_reject_sync_conn_req rej; |
| 620 | |
| 621 | bacpy(&rej.bdaddr, &conn->dst); |
| 622 | |
| 623 | /* SCO rejection has its own limited set of |
| 624 | * allowed error values (0x0D-0x0F) which isn't |
| 625 | * compatible with most values passed to this |
| 626 | * function. To be safe hard-code one of the |
| 627 | * values that's suitable for SCO. |
| 628 | */ |
| 629 | rej.reason = HCI_ERROR_REMOTE_LOW_RESOURCES; |
| 630 | |
| 631 | hci_req_add(req, HCI_OP_REJECT_SYNC_CONN_REQ, |
| 632 | sizeof(rej), &rej); |
| 633 | } |
| 634 | break; |
| 635 | default: |
| 636 | conn->state = BT_CLOSED; |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | static void abort_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode) |
| 642 | { |
| 643 | if (status) |
| 644 | BT_DBG("Failed to abort connection: status 0x%2.2x", status); |
| 645 | } |
| 646 | |
| 647 | int hci_abort_conn(struct hci_conn *conn, u8 reason) |
| 648 | { |
| 649 | struct hci_request req; |
| 650 | int err; |
| 651 | |
| 652 | hci_req_init(&req, conn->hdev); |
| 653 | |
| 654 | __hci_abort_conn(&req, conn, reason); |
| 655 | |
| 656 | err = hci_req_run(&req, abort_conn_complete); |
| 657 | if (err && err != -ENODATA) { |
| 658 | BT_ERR("Failed to run HCI request: err %d", err); |
| 659 | return err; |
| 660 | } |
| 661 | |
| 662 | return 0; |
| 663 | } |