hci_request.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (C) 2014 Intel Corporation
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License version 2 as
  6. published by the Free Software Foundation;
  7. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  8. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  10. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  11. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  12. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  16. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  17. SOFTWARE IS DISCLAIMED.
  18. */
  19. #include <linux/sched/signal.h>
  20. #include <net/bluetooth/bluetooth.h>
  21. #include <net/bluetooth/hci_core.h>
  22. #include <net/bluetooth/mgmt.h>
  23. #include "smp.h"
  24. #include "hci_request.h"
  25. #include "msft.h"
  26. #include "eir.h"
  27. void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
  28. {
  29. skb_queue_head_init(&req->cmd_q);
  30. req->hdev = hdev;
  31. req->err = 0;
  32. }
  33. void hci_req_purge(struct hci_request *req)
  34. {
  35. skb_queue_purge(&req->cmd_q);
  36. }
  37. bool hci_req_status_pend(struct hci_dev *hdev)
  38. {
  39. return hdev->req_status == HCI_REQ_PEND;
  40. }
  41. static int req_run(struct hci_request *req, hci_req_complete_t complete,
  42. hci_req_complete_skb_t complete_skb)
  43. {
  44. struct hci_dev *hdev = req->hdev;
  45. struct sk_buff *skb;
  46. unsigned long flags;
  47. bt_dev_dbg(hdev, "length %u", skb_queue_len(&req->cmd_q));
  48. /* If an error occurred during request building, remove all HCI
  49. * commands queued on the HCI request queue.
  50. */
  51. if (req->err) {
  52. skb_queue_purge(&req->cmd_q);
  53. return req->err;
  54. }
  55. /* Do not allow empty requests */
  56. if (skb_queue_empty(&req->cmd_q))
  57. return -ENODATA;
  58. skb = skb_peek_tail(&req->cmd_q);
  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. }
  65. spin_lock_irqsave(&hdev->cmd_q.lock, flags);
  66. skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
  67. spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
  68. queue_work(hdev->workqueue, &hdev->cmd_work);
  69. return 0;
  70. }
  71. int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
  72. {
  73. return req_run(req, complete, NULL);
  74. }
  75. int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete)
  76. {
  77. return req_run(req, NULL, complete);
  78. }
  79. void hci_req_sync_complete(struct hci_dev *hdev, u8 result, u16 opcode,
  80. struct sk_buff *skb)
  81. {
  82. bt_dev_dbg(hdev, "result 0x%2.2x", result);
  83. if (hdev->req_status == HCI_REQ_PEND) {
  84. hdev->req_result = result;
  85. hdev->req_status = HCI_REQ_DONE;
  86. if (skb)
  87. hdev->req_skb = skb_get(skb);
  88. wake_up_interruptible(&hdev->req_wait_q);
  89. }
  90. }
  91. /* Execute request and wait for completion. */
  92. int __hci_req_sync(struct hci_dev *hdev, int (*func)(struct hci_request *req,
  93. unsigned long opt),
  94. unsigned long opt, u32 timeout, u8 *hci_status)
  95. {
  96. struct hci_request req;
  97. int err = 0;
  98. bt_dev_dbg(hdev, "start");
  99. hci_req_init(&req, hdev);
  100. hdev->req_status = HCI_REQ_PEND;
  101. err = func(&req, opt);
  102. if (err) {
  103. if (hci_status)
  104. *hci_status = HCI_ERROR_UNSPECIFIED;
  105. return err;
  106. }
  107. err = hci_req_run_skb(&req, hci_req_sync_complete);
  108. if (err < 0) {
  109. hdev->req_status = 0;
  110. /* ENODATA means the HCI request command queue is empty.
  111. * This can happen when a request with conditionals doesn't
  112. * trigger any commands to be sent. This is normal behavior
  113. * and should not trigger an error return.
  114. */
  115. if (err == -ENODATA) {
  116. if (hci_status)
  117. *hci_status = 0;
  118. return 0;
  119. }
  120. if (hci_status)
  121. *hci_status = HCI_ERROR_UNSPECIFIED;
  122. return err;
  123. }
  124. err = wait_event_interruptible_timeout(hdev->req_wait_q,
  125. hdev->req_status != HCI_REQ_PEND, timeout);
  126. if (err == -ERESTARTSYS)
  127. return -EINTR;
  128. switch (hdev->req_status) {
  129. case HCI_REQ_DONE:
  130. err = -bt_to_errno(hdev->req_result);
  131. if (hci_status)
  132. *hci_status = hdev->req_result;
  133. break;
  134. case HCI_REQ_CANCELED:
  135. err = -hdev->req_result;
  136. if (hci_status)
  137. *hci_status = HCI_ERROR_UNSPECIFIED;
  138. break;
  139. default:
  140. err = -ETIMEDOUT;
  141. if (hci_status)
  142. *hci_status = HCI_ERROR_UNSPECIFIED;
  143. break;
  144. }
  145. kfree_skb(hdev->req_skb);
  146. hdev->req_skb = NULL;
  147. hdev->req_status = hdev->req_result = 0;
  148. bt_dev_dbg(hdev, "end: err %d", err);
  149. return err;
  150. }
  151. int hci_req_sync(struct hci_dev *hdev, int (*req)(struct hci_request *req,
  152. unsigned long opt),
  153. unsigned long opt, u32 timeout, u8 *hci_status)
  154. {
  155. int ret;
  156. /* Serialize all requests */
  157. hci_req_sync_lock(hdev);
  158. /* check the state after obtaing the lock to protect the HCI_UP
  159. * against any races from hci_dev_do_close when the controller
  160. * gets removed.
  161. */
  162. if (test_bit(HCI_UP, &hdev->flags))
  163. ret = __hci_req_sync(hdev, req, opt, timeout, hci_status);
  164. else
  165. ret = -ENETDOWN;
  166. hci_req_sync_unlock(hdev);
  167. return ret;
  168. }
  169. struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen,
  170. const void *param)
  171. {
  172. int len = HCI_COMMAND_HDR_SIZE + plen;
  173. struct hci_command_hdr *hdr;
  174. struct sk_buff *skb;
  175. skb = bt_skb_alloc(len, GFP_ATOMIC);
  176. if (!skb)
  177. return NULL;
  178. hdr = skb_put(skb, HCI_COMMAND_HDR_SIZE);
  179. hdr->opcode = cpu_to_le16(opcode);
  180. hdr->plen = plen;
  181. if (plen)
  182. skb_put_data(skb, param, plen);
  183. bt_dev_dbg(hdev, "skb len %d", skb->len);
  184. hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
  185. hci_skb_opcode(skb) = opcode;
  186. return skb;
  187. }
  188. /* Queue a command to an asynchronous HCI request */
  189. void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
  190. const void *param, u8 event)
  191. {
  192. struct hci_dev *hdev = req->hdev;
  193. struct sk_buff *skb;
  194. bt_dev_dbg(hdev, "opcode 0x%4.4x plen %d", opcode, plen);
  195. /* If an error occurred during request building, there is no point in
  196. * queueing the HCI command. We can simply return.
  197. */
  198. if (req->err)
  199. return;
  200. skb = hci_prepare_cmd(hdev, opcode, plen, param);
  201. if (!skb) {
  202. bt_dev_err(hdev, "no memory for command (opcode 0x%4.4x)",
  203. opcode);
  204. req->err = -ENOMEM;
  205. return;
  206. }
  207. if (skb_queue_empty(&req->cmd_q))
  208. bt_cb(skb)->hci.req_flags |= HCI_REQ_START;
  209. hci_skb_event(skb) = event;
  210. skb_queue_tail(&req->cmd_q, skb);
  211. }
  212. void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
  213. const void *param)
  214. {
  215. bt_dev_dbg(req->hdev, "HCI_REQ-0x%4.4x", opcode);
  216. hci_req_add_ev(req, opcode, plen, param, 0);
  217. }
  218. static void start_interleave_scan(struct hci_dev *hdev)
  219. {
  220. hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
  221. queue_delayed_work(hdev->req_workqueue,
  222. &hdev->interleave_scan, 0);
  223. }
  224. static bool is_interleave_scanning(struct hci_dev *hdev)
  225. {
  226. return hdev->interleave_scan_state != INTERLEAVE_SCAN_NONE;
  227. }
  228. static void cancel_interleave_scan(struct hci_dev *hdev)
  229. {
  230. bt_dev_dbg(hdev, "cancelling interleave scan");
  231. cancel_delayed_work_sync(&hdev->interleave_scan);
  232. hdev->interleave_scan_state = INTERLEAVE_SCAN_NONE;
  233. }
  234. /* Return true if interleave_scan wasn't started until exiting this function,
  235. * otherwise, return false
  236. */
  237. static bool __hci_update_interleaved_scan(struct hci_dev *hdev)
  238. {
  239. /* Do interleaved scan only if all of the following are true:
  240. * - There is at least one ADV monitor
  241. * - At least one pending LE connection or one device to be scanned for
  242. * - Monitor offloading is not supported
  243. * If so, we should alternate between allowlist scan and one without
  244. * any filters to save power.
  245. */
  246. bool use_interleaving = hci_is_adv_monitoring(hdev) &&
  247. !(list_empty(&hdev->pend_le_conns) &&
  248. list_empty(&hdev->pend_le_reports)) &&
  249. hci_get_adv_monitor_offload_ext(hdev) ==
  250. HCI_ADV_MONITOR_EXT_NONE;
  251. bool is_interleaving = is_interleave_scanning(hdev);
  252. if (use_interleaving && !is_interleaving) {
  253. start_interleave_scan(hdev);
  254. bt_dev_dbg(hdev, "starting interleave scan");
  255. return true;
  256. }
  257. if (!use_interleaving && is_interleaving)
  258. cancel_interleave_scan(hdev);
  259. return false;
  260. }
  261. void hci_req_add_le_scan_disable(struct hci_request *req, bool rpa_le_conn)
  262. {
  263. struct hci_dev *hdev = req->hdev;
  264. if (hdev->scanning_paused) {
  265. bt_dev_dbg(hdev, "Scanning is paused for suspend");
  266. return;
  267. }
  268. if (use_ext_scan(hdev)) {
  269. struct hci_cp_le_set_ext_scan_enable cp;
  270. memset(&cp, 0, sizeof(cp));
  271. cp.enable = LE_SCAN_DISABLE;
  272. hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE, sizeof(cp),
  273. &cp);
  274. } else {
  275. struct hci_cp_le_set_scan_enable cp;
  276. memset(&cp, 0, sizeof(cp));
  277. cp.enable = LE_SCAN_DISABLE;
  278. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
  279. }
  280. /* Disable address resolution */
  281. if (hci_dev_test_flag(hdev, HCI_LL_RPA_RESOLUTION) && !rpa_le_conn) {
  282. __u8 enable = 0x00;
  283. hci_req_add(req, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, 1, &enable);
  284. }
  285. }
  286. static void del_from_accept_list(struct hci_request *req, bdaddr_t *bdaddr,
  287. u8 bdaddr_type)
  288. {
  289. struct hci_cp_le_del_from_accept_list cp;
  290. cp.bdaddr_type = bdaddr_type;
  291. bacpy(&cp.bdaddr, bdaddr);
  292. bt_dev_dbg(req->hdev, "Remove %pMR (0x%x) from accept list", &cp.bdaddr,
  293. cp.bdaddr_type);
  294. hci_req_add(req, HCI_OP_LE_DEL_FROM_ACCEPT_LIST, sizeof(cp), &cp);
  295. if (use_ll_privacy(req->hdev)) {
  296. struct smp_irk *irk;
  297. irk = hci_find_irk_by_addr(req->hdev, bdaddr, bdaddr_type);
  298. if (irk) {
  299. struct hci_cp_le_del_from_resolv_list cp;
  300. cp.bdaddr_type = bdaddr_type;
  301. bacpy(&cp.bdaddr, bdaddr);
  302. hci_req_add(req, HCI_OP_LE_DEL_FROM_RESOLV_LIST,
  303. sizeof(cp), &cp);
  304. }
  305. }
  306. }
  307. /* Adds connection to accept list if needed. On error, returns -1. */
  308. static int add_to_accept_list(struct hci_request *req,
  309. struct hci_conn_params *params, u8 *num_entries,
  310. bool allow_rpa)
  311. {
  312. struct hci_cp_le_add_to_accept_list cp;
  313. struct hci_dev *hdev = req->hdev;
  314. /* Already in accept list */
  315. if (hci_bdaddr_list_lookup(&hdev->le_accept_list, &params->addr,
  316. params->addr_type))
  317. return 0;
  318. /* Select filter policy to accept all advertising */
  319. if (*num_entries >= hdev->le_accept_list_size)
  320. return -1;
  321. /* Accept list can not be used with RPAs */
  322. if (!allow_rpa &&
  323. !hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
  324. hci_find_irk_by_addr(hdev, &params->addr, params->addr_type)) {
  325. return -1;
  326. }
  327. /* During suspend, only wakeable devices can be in accept list */
  328. if (hdev->suspended &&
  329. !(params->flags & HCI_CONN_FLAG_REMOTE_WAKEUP))
  330. return 0;
  331. *num_entries += 1;
  332. cp.bdaddr_type = params->addr_type;
  333. bacpy(&cp.bdaddr, &params->addr);
  334. bt_dev_dbg(hdev, "Add %pMR (0x%x) to accept list", &cp.bdaddr,
  335. cp.bdaddr_type);
  336. hci_req_add(req, HCI_OP_LE_ADD_TO_ACCEPT_LIST, sizeof(cp), &cp);
  337. if (use_ll_privacy(hdev)) {
  338. struct smp_irk *irk;
  339. irk = hci_find_irk_by_addr(hdev, &params->addr,
  340. params->addr_type);
  341. if (irk) {
  342. struct hci_cp_le_add_to_resolv_list cp;
  343. cp.bdaddr_type = params->addr_type;
  344. bacpy(&cp.bdaddr, &params->addr);
  345. memcpy(cp.peer_irk, irk->val, 16);
  346. if (hci_dev_test_flag(hdev, HCI_PRIVACY))
  347. memcpy(cp.local_irk, hdev->irk, 16);
  348. else
  349. memset(cp.local_irk, 0, 16);
  350. hci_req_add(req, HCI_OP_LE_ADD_TO_RESOLV_LIST,
  351. sizeof(cp), &cp);
  352. }
  353. }
  354. return 0;
  355. }
  356. static u8 update_accept_list(struct hci_request *req)
  357. {
  358. struct hci_dev *hdev = req->hdev;
  359. struct hci_conn_params *params;
  360. struct bdaddr_list *b;
  361. u8 num_entries = 0;
  362. bool pend_conn, pend_report;
  363. /* We allow usage of accept list even with RPAs in suspend. In the worst
  364. * case, we won't be able to wake from devices that use the privacy1.2
  365. * features. Additionally, once we support privacy1.2 and IRK
  366. * offloading, we can update this to also check for those conditions.
  367. */
  368. bool allow_rpa = hdev->suspended;
  369. if (use_ll_privacy(hdev))
  370. allow_rpa = true;
  371. /* Go through the current accept list programmed into the
  372. * controller one by one and check if that address is still
  373. * in the list of pending connections or list of devices to
  374. * report. If not present in either list, then queue the
  375. * command to remove it from the controller.
  376. */
  377. list_for_each_entry(b, &hdev->le_accept_list, list) {
  378. pend_conn = hci_pend_le_action_lookup(&hdev->pend_le_conns,
  379. &b->bdaddr,
  380. b->bdaddr_type);
  381. pend_report = hci_pend_le_action_lookup(&hdev->pend_le_reports,
  382. &b->bdaddr,
  383. b->bdaddr_type);
  384. /* If the device is not likely to connect or report,
  385. * remove it from the accept list.
  386. */
  387. if (!pend_conn && !pend_report) {
  388. del_from_accept_list(req, &b->bdaddr, b->bdaddr_type);
  389. continue;
  390. }
  391. /* Accept list can not be used with RPAs */
  392. if (!allow_rpa &&
  393. !hci_dev_test_flag(hdev, HCI_ENABLE_LL_PRIVACY) &&
  394. hci_find_irk_by_addr(hdev, &b->bdaddr, b->bdaddr_type)) {
  395. return 0x00;
  396. }
  397. num_entries++;
  398. }
  399. /* Since all no longer valid accept list entries have been
  400. * removed, walk through the list of pending connections
  401. * and ensure that any new device gets programmed into
  402. * the controller.
  403. *
  404. * If the list of the devices is larger than the list of
  405. * available accept list entries in the controller, then
  406. * just abort and return filer policy value to not use the
  407. * accept list.
  408. */
  409. list_for_each_entry(params, &hdev->pend_le_conns, action) {
  410. if (add_to_accept_list(req, params, &num_entries, allow_rpa))
  411. return 0x00;
  412. }
  413. /* After adding all new pending connections, walk through
  414. * the list of pending reports and also add these to the
  415. * accept list if there is still space. Abort if space runs out.
  416. */
  417. list_for_each_entry(params, &hdev->pend_le_reports, action) {
  418. if (add_to_accept_list(req, params, &num_entries, allow_rpa))
  419. return 0x00;
  420. }
  421. /* Use the allowlist unless the following conditions are all true:
  422. * - We are not currently suspending
  423. * - There are 1 or more ADV monitors registered and it's not offloaded
  424. * - Interleaved scanning is not currently using the allowlist
  425. */
  426. if (!idr_is_empty(&hdev->adv_monitors_idr) && !hdev->suspended &&
  427. hci_get_adv_monitor_offload_ext(hdev) == HCI_ADV_MONITOR_EXT_NONE &&
  428. hdev->interleave_scan_state != INTERLEAVE_SCAN_ALLOWLIST)
  429. return 0x00;
  430. /* Select filter policy to use accept list */
  431. return 0x01;
  432. }
  433. static bool scan_use_rpa(struct hci_dev *hdev)
  434. {
  435. return hci_dev_test_flag(hdev, HCI_PRIVACY);
  436. }
  437. static void hci_req_start_scan(struct hci_request *req, u8 type, u16 interval,
  438. u16 window, u8 own_addr_type, u8 filter_policy,
  439. bool filter_dup, bool addr_resolv)
  440. {
  441. struct hci_dev *hdev = req->hdev;
  442. if (hdev->scanning_paused) {
  443. bt_dev_dbg(hdev, "Scanning is paused for suspend");
  444. return;
  445. }
  446. if (use_ll_privacy(hdev) && addr_resolv) {
  447. u8 enable = 0x01;
  448. hci_req_add(req, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE, 1, &enable);
  449. }
  450. /* Use ext scanning if set ext scan param and ext scan enable is
  451. * supported
  452. */
  453. if (use_ext_scan(hdev)) {
  454. struct hci_cp_le_set_ext_scan_params *ext_param_cp;
  455. struct hci_cp_le_set_ext_scan_enable ext_enable_cp;
  456. struct hci_cp_le_scan_phy_params *phy_params;
  457. u8 data[sizeof(*ext_param_cp) + sizeof(*phy_params) * 2];
  458. u32 plen;
  459. ext_param_cp = (void *)data;
  460. phy_params = (void *)ext_param_cp->data;
  461. memset(ext_param_cp, 0, sizeof(*ext_param_cp));
  462. ext_param_cp->own_addr_type = own_addr_type;
  463. ext_param_cp->filter_policy = filter_policy;
  464. plen = sizeof(*ext_param_cp);
  465. if (scan_1m(hdev) || scan_2m(hdev)) {
  466. ext_param_cp->scanning_phys |= LE_SCAN_PHY_1M;
  467. memset(phy_params, 0, sizeof(*phy_params));
  468. phy_params->type = type;
  469. phy_params->interval = cpu_to_le16(interval);
  470. phy_params->window = cpu_to_le16(window);
  471. plen += sizeof(*phy_params);
  472. phy_params++;
  473. }
  474. if (scan_coded(hdev)) {
  475. ext_param_cp->scanning_phys |= LE_SCAN_PHY_CODED;
  476. memset(phy_params, 0, sizeof(*phy_params));
  477. phy_params->type = type;
  478. phy_params->interval = cpu_to_le16(interval);
  479. phy_params->window = cpu_to_le16(window);
  480. plen += sizeof(*phy_params);
  481. phy_params++;
  482. }
  483. hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_PARAMS,
  484. plen, ext_param_cp);
  485. memset(&ext_enable_cp, 0, sizeof(ext_enable_cp));
  486. ext_enable_cp.enable = LE_SCAN_ENABLE;
  487. ext_enable_cp.filter_dup = filter_dup;
  488. hci_req_add(req, HCI_OP_LE_SET_EXT_SCAN_ENABLE,
  489. sizeof(ext_enable_cp), &ext_enable_cp);
  490. } else {
  491. struct hci_cp_le_set_scan_param param_cp;
  492. struct hci_cp_le_set_scan_enable enable_cp;
  493. memset(&param_cp, 0, sizeof(param_cp));
  494. param_cp.type = type;
  495. param_cp.interval = cpu_to_le16(interval);
  496. param_cp.window = cpu_to_le16(window);
  497. param_cp.own_address_type = own_addr_type;
  498. param_cp.filter_policy = filter_policy;
  499. hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
  500. &param_cp);
  501. memset(&enable_cp, 0, sizeof(enable_cp));
  502. enable_cp.enable = LE_SCAN_ENABLE;
  503. enable_cp.filter_dup = filter_dup;
  504. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
  505. &enable_cp);
  506. }
  507. }
  508. /* Returns true if an le connection is in the scanning state */
  509. static inline bool hci_is_le_conn_scanning(struct hci_dev *hdev)
  510. {
  511. struct hci_conn_hash *h = &hdev->conn_hash;
  512. struct hci_conn *c;
  513. rcu_read_lock();
  514. list_for_each_entry_rcu(c, &h->list, list) {
  515. if (c->type == LE_LINK && c->state == BT_CONNECT &&
  516. test_bit(HCI_CONN_SCANNING, &c->flags)) {
  517. rcu_read_unlock();
  518. return true;
  519. }
  520. }
  521. rcu_read_unlock();
  522. return false;
  523. }
  524. static void set_random_addr(struct hci_request *req, bdaddr_t *rpa);
  525. static int hci_update_random_address(struct hci_request *req,
  526. bool require_privacy, bool use_rpa,
  527. u8 *own_addr_type)
  528. {
  529. struct hci_dev *hdev = req->hdev;
  530. int err;
  531. /* If privacy is enabled use a resolvable private address. If
  532. * current RPA has expired or there is something else than
  533. * the current RPA in use, then generate a new one.
  534. */
  535. if (use_rpa) {
  536. /* If Controller supports LL Privacy use own address type is
  537. * 0x03
  538. */
  539. if (use_ll_privacy(hdev))
  540. *own_addr_type = ADDR_LE_DEV_RANDOM_RESOLVED;
  541. else
  542. *own_addr_type = ADDR_LE_DEV_RANDOM;
  543. if (rpa_valid(hdev))
  544. return 0;
  545. err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
  546. if (err < 0) {
  547. bt_dev_err(hdev, "failed to generate new RPA");
  548. return err;
  549. }
  550. set_random_addr(req, &hdev->rpa);
  551. return 0;
  552. }
  553. /* In case of required privacy without resolvable private address,
  554. * use an non-resolvable private address. This is useful for active
  555. * scanning and non-connectable advertising.
  556. */
  557. if (require_privacy) {
  558. bdaddr_t nrpa;
  559. while (true) {
  560. /* The non-resolvable private address is generated
  561. * from random six bytes with the two most significant
  562. * bits cleared.
  563. */
  564. get_random_bytes(&nrpa, 6);
  565. nrpa.b[5] &= 0x3f;
  566. /* The non-resolvable private address shall not be
  567. * equal to the public address.
  568. */
  569. if (bacmp(&hdev->bdaddr, &nrpa))
  570. break;
  571. }
  572. *own_addr_type = ADDR_LE_DEV_RANDOM;
  573. set_random_addr(req, &nrpa);
  574. return 0;
  575. }
  576. /* If forcing static address is in use or there is no public
  577. * address use the static address as random address (but skip
  578. * the HCI command if the current random address is already the
  579. * static one.
  580. *
  581. * In case BR/EDR has been disabled on a dual-mode controller
  582. * and a static address has been configured, then use that
  583. * address instead of the public BR/EDR address.
  584. */
  585. if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
  586. !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
  587. (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
  588. bacmp(&hdev->static_addr, BDADDR_ANY))) {
  589. *own_addr_type = ADDR_LE_DEV_RANDOM;
  590. if (bacmp(&hdev->static_addr, &hdev->random_addr))
  591. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
  592. &hdev->static_addr);
  593. return 0;
  594. }
  595. /* Neither privacy nor static address is being used so use a
  596. * public address.
  597. */
  598. *own_addr_type = ADDR_LE_DEV_PUBLIC;
  599. return 0;
  600. }
  601. /* Ensure to call hci_req_add_le_scan_disable() first to disable the
  602. * controller based address resolution to be able to reconfigure
  603. * resolving list.
  604. */
  605. void hci_req_add_le_passive_scan(struct hci_request *req)
  606. {
  607. struct hci_dev *hdev = req->hdev;
  608. u8 own_addr_type;
  609. u8 filter_policy;
  610. u16 window, interval;
  611. /* Default is to enable duplicates filter */
  612. u8 filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  613. /* Background scanning should run with address resolution */
  614. bool addr_resolv = true;
  615. if (hdev->scanning_paused) {
  616. bt_dev_dbg(hdev, "Scanning is paused for suspend");
  617. return;
  618. }
  619. /* Set require_privacy to false since no SCAN_REQ are send
  620. * during passive scanning. Not using an non-resolvable address
  621. * here is important so that peer devices using direct
  622. * advertising with our address will be correctly reported
  623. * by the controller.
  624. */
  625. if (hci_update_random_address(req, false, scan_use_rpa(hdev),
  626. &own_addr_type))
  627. return;
  628. if (hdev->enable_advmon_interleave_scan &&
  629. __hci_update_interleaved_scan(hdev))
  630. return;
  631. bt_dev_dbg(hdev, "interleave state %d", hdev->interleave_scan_state);
  632. /* Adding or removing entries from the accept list must
  633. * happen before enabling scanning. The controller does
  634. * not allow accept list modification while scanning.
  635. */
  636. filter_policy = update_accept_list(req);
  637. /* When the controller is using random resolvable addresses and
  638. * with that having LE privacy enabled, then controllers with
  639. * Extended Scanner Filter Policies support can now enable support
  640. * for handling directed advertising.
  641. *
  642. * So instead of using filter polices 0x00 (no accept list)
  643. * and 0x01 (accept list enabled) use the new filter policies
  644. * 0x02 (no accept list) and 0x03 (accept list enabled).
  645. */
  646. if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
  647. (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
  648. filter_policy |= 0x02;
  649. if (hdev->suspended) {
  650. window = hdev->le_scan_window_suspend;
  651. interval = hdev->le_scan_int_suspend;
  652. } else if (hci_is_le_conn_scanning(hdev)) {
  653. window = hdev->le_scan_window_connect;
  654. interval = hdev->le_scan_int_connect;
  655. } else if (hci_is_adv_monitoring(hdev)) {
  656. window = hdev->le_scan_window_adv_monitor;
  657. interval = hdev->le_scan_int_adv_monitor;
  658. /* Disable duplicates filter when scanning for advertisement
  659. * monitor for the following reasons.
  660. *
  661. * For HW pattern filtering (ex. MSFT), Realtek and Qualcomm
  662. * controllers ignore RSSI_Sampling_Period when the duplicates
  663. * filter is enabled.
  664. *
  665. * For SW pattern filtering, when we're not doing interleaved
  666. * scanning, it is necessary to disable duplicates filter,
  667. * otherwise hosts can only receive one advertisement and it's
  668. * impossible to know if a peer is still in range.
  669. */
  670. filter_dup = LE_SCAN_FILTER_DUP_DISABLE;
  671. } else {
  672. window = hdev->le_scan_window;
  673. interval = hdev->le_scan_interval;
  674. }
  675. bt_dev_dbg(hdev, "LE passive scan with accept list = %d",
  676. filter_policy);
  677. hci_req_start_scan(req, LE_SCAN_PASSIVE, interval, window,
  678. own_addr_type, filter_policy, filter_dup,
  679. addr_resolv);
  680. }
  681. static int hci_req_add_le_interleaved_scan(struct hci_request *req,
  682. unsigned long opt)
  683. {
  684. struct hci_dev *hdev = req->hdev;
  685. int ret = 0;
  686. hci_dev_lock(hdev);
  687. if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
  688. hci_req_add_le_scan_disable(req, false);
  689. hci_req_add_le_passive_scan(req);
  690. switch (hdev->interleave_scan_state) {
  691. case INTERLEAVE_SCAN_ALLOWLIST:
  692. bt_dev_dbg(hdev, "next state: allowlist");
  693. hdev->interleave_scan_state = INTERLEAVE_SCAN_NO_FILTER;
  694. break;
  695. case INTERLEAVE_SCAN_NO_FILTER:
  696. bt_dev_dbg(hdev, "next state: no filter");
  697. hdev->interleave_scan_state = INTERLEAVE_SCAN_ALLOWLIST;
  698. break;
  699. case INTERLEAVE_SCAN_NONE:
  700. BT_ERR("unexpected error");
  701. ret = -1;
  702. }
  703. hci_dev_unlock(hdev);
  704. return ret;
  705. }
  706. static void interleave_scan_work(struct work_struct *work)
  707. {
  708. struct hci_dev *hdev = container_of(work, struct hci_dev,
  709. interleave_scan.work);
  710. u8 status;
  711. unsigned long timeout;
  712. if (hdev->interleave_scan_state == INTERLEAVE_SCAN_ALLOWLIST) {
  713. timeout = msecs_to_jiffies(hdev->advmon_allowlist_duration);
  714. } else if (hdev->interleave_scan_state == INTERLEAVE_SCAN_NO_FILTER) {
  715. timeout = msecs_to_jiffies(hdev->advmon_no_filter_duration);
  716. } else {
  717. bt_dev_err(hdev, "unexpected error");
  718. return;
  719. }
  720. hci_req_sync(hdev, hci_req_add_le_interleaved_scan, 0,
  721. HCI_CMD_TIMEOUT, &status);
  722. /* Don't continue interleaving if it was canceled */
  723. if (is_interleave_scanning(hdev))
  724. queue_delayed_work(hdev->req_workqueue,
  725. &hdev->interleave_scan, timeout);
  726. }
  727. static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
  728. {
  729. struct hci_dev *hdev = req->hdev;
  730. /* If we're advertising or initiating an LE connection we can't
  731. * go ahead and change the random address at this time. This is
  732. * because the eventual initiator address used for the
  733. * subsequently created connection will be undefined (some
  734. * controllers use the new address and others the one we had
  735. * when the operation started).
  736. *
  737. * In this kind of scenario skip the update and let the random
  738. * address be updated at the next cycle.
  739. */
  740. if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
  741. hci_lookup_le_connect(hdev)) {
  742. bt_dev_dbg(hdev, "Deferring random address update");
  743. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  744. return;
  745. }
  746. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa);
  747. }
  748. void hci_request_setup(struct hci_dev *hdev)
  749. {
  750. INIT_DELAYED_WORK(&hdev->interleave_scan, interleave_scan_work);
  751. }
  752. void hci_request_cancel_all(struct hci_dev *hdev)
  753. {
  754. __hci_cmd_sync_cancel(hdev, ENODEV);
  755. cancel_interleave_scan(hdev);
  756. }