hci_request.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 <net/bluetooth/bluetooth.h>
  20. #include <net/bluetooth/hci_core.h>
  21. #include "smp.h"
  22. #include "hci_request.h"
  23. void hci_req_init(struct hci_request *req, struct hci_dev *hdev)
  24. {
  25. skb_queue_head_init(&req->cmd_q);
  26. req->hdev = hdev;
  27. req->err = 0;
  28. }
  29. static int req_run(struct hci_request *req, hci_req_complete_t complete,
  30. hci_req_complete_skb_t complete_skb)
  31. {
  32. struct hci_dev *hdev = req->hdev;
  33. struct sk_buff *skb;
  34. unsigned long flags;
  35. BT_DBG("length %u", skb_queue_len(&req->cmd_q));
  36. /* If an error occurred during request building, remove all HCI
  37. * commands queued on the HCI request queue.
  38. */
  39. if (req->err) {
  40. skb_queue_purge(&req->cmd_q);
  41. return req->err;
  42. }
  43. /* Do not allow empty requests */
  44. if (skb_queue_empty(&req->cmd_q))
  45. return -ENODATA;
  46. skb = skb_peek_tail(&req->cmd_q);
  47. bt_cb(skb)->req.complete = complete;
  48. bt_cb(skb)->req.complete_skb = complete_skb;
  49. spin_lock_irqsave(&hdev->cmd_q.lock, flags);
  50. skb_queue_splice_tail(&req->cmd_q, &hdev->cmd_q);
  51. spin_unlock_irqrestore(&hdev->cmd_q.lock, flags);
  52. queue_work(hdev->workqueue, &hdev->cmd_work);
  53. return 0;
  54. }
  55. int hci_req_run(struct hci_request *req, hci_req_complete_t complete)
  56. {
  57. return req_run(req, complete, NULL);
  58. }
  59. int hci_req_run_skb(struct hci_request *req, hci_req_complete_skb_t complete)
  60. {
  61. return req_run(req, NULL, complete);
  62. }
  63. struct sk_buff *hci_prepare_cmd(struct hci_dev *hdev, u16 opcode, u32 plen,
  64. const void *param)
  65. {
  66. int len = HCI_COMMAND_HDR_SIZE + plen;
  67. struct hci_command_hdr *hdr;
  68. struct sk_buff *skb;
  69. skb = bt_skb_alloc(len, GFP_ATOMIC);
  70. if (!skb)
  71. return NULL;
  72. hdr = (struct hci_command_hdr *) skb_put(skb, HCI_COMMAND_HDR_SIZE);
  73. hdr->opcode = cpu_to_le16(opcode);
  74. hdr->plen = plen;
  75. if (plen)
  76. memcpy(skb_put(skb, plen), param, plen);
  77. BT_DBG("skb len %d", skb->len);
  78. bt_cb(skb)->pkt_type = HCI_COMMAND_PKT;
  79. bt_cb(skb)->opcode = opcode;
  80. return skb;
  81. }
  82. /* Queue a command to an asynchronous HCI request */
  83. void hci_req_add_ev(struct hci_request *req, u16 opcode, u32 plen,
  84. const void *param, u8 event)
  85. {
  86. struct hci_dev *hdev = req->hdev;
  87. struct sk_buff *skb;
  88. BT_DBG("%s opcode 0x%4.4x plen %d", hdev->name, opcode, plen);
  89. /* If an error occurred during request building, there is no point in
  90. * queueing the HCI command. We can simply return.
  91. */
  92. if (req->err)
  93. return;
  94. skb = hci_prepare_cmd(hdev, opcode, plen, param);
  95. if (!skb) {
  96. BT_ERR("%s no memory for command (opcode 0x%4.4x)",
  97. hdev->name, opcode);
  98. req->err = -ENOMEM;
  99. return;
  100. }
  101. if (skb_queue_empty(&req->cmd_q))
  102. bt_cb(skb)->req.start = true;
  103. bt_cb(skb)->req.event = event;
  104. skb_queue_tail(&req->cmd_q, skb);
  105. }
  106. void hci_req_add(struct hci_request *req, u16 opcode, u32 plen,
  107. const void *param)
  108. {
  109. hci_req_add_ev(req, opcode, plen, param, 0);
  110. }
  111. void hci_req_add_le_scan_disable(struct hci_request *req)
  112. {
  113. struct hci_cp_le_set_scan_enable cp;
  114. memset(&cp, 0, sizeof(cp));
  115. cp.enable = LE_SCAN_DISABLE;
  116. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(cp), &cp);
  117. }
  118. static void add_to_white_list(struct hci_request *req,
  119. struct hci_conn_params *params)
  120. {
  121. struct hci_cp_le_add_to_white_list cp;
  122. cp.bdaddr_type = params->addr_type;
  123. bacpy(&cp.bdaddr, &params->addr);
  124. hci_req_add(req, HCI_OP_LE_ADD_TO_WHITE_LIST, sizeof(cp), &cp);
  125. }
  126. static u8 update_white_list(struct hci_request *req)
  127. {
  128. struct hci_dev *hdev = req->hdev;
  129. struct hci_conn_params *params;
  130. struct bdaddr_list *b;
  131. uint8_t white_list_entries = 0;
  132. /* Go through the current white list programmed into the
  133. * controller one by one and check if that address is still
  134. * in the list of pending connections or list of devices to
  135. * report. If not present in either list, then queue the
  136. * command to remove it from the controller.
  137. */
  138. list_for_each_entry(b, &hdev->le_white_list, list) {
  139. struct hci_cp_le_del_from_white_list cp;
  140. if (hci_pend_le_action_lookup(&hdev->pend_le_conns,
  141. &b->bdaddr, b->bdaddr_type) ||
  142. hci_pend_le_action_lookup(&hdev->pend_le_reports,
  143. &b->bdaddr, b->bdaddr_type)) {
  144. white_list_entries++;
  145. continue;
  146. }
  147. cp.bdaddr_type = b->bdaddr_type;
  148. bacpy(&cp.bdaddr, &b->bdaddr);
  149. hci_req_add(req, HCI_OP_LE_DEL_FROM_WHITE_LIST,
  150. sizeof(cp), &cp);
  151. }
  152. /* Since all no longer valid white list entries have been
  153. * removed, walk through the list of pending connections
  154. * and ensure that any new device gets programmed into
  155. * the controller.
  156. *
  157. * If the list of the devices is larger than the list of
  158. * available white list entries in the controller, then
  159. * just abort and return filer policy value to not use the
  160. * white list.
  161. */
  162. list_for_each_entry(params, &hdev->pend_le_conns, action) {
  163. if (hci_bdaddr_list_lookup(&hdev->le_white_list,
  164. &params->addr, params->addr_type))
  165. continue;
  166. if (white_list_entries >= hdev->le_white_list_size) {
  167. /* Select filter policy to accept all advertising */
  168. return 0x00;
  169. }
  170. if (hci_find_irk_by_addr(hdev, &params->addr,
  171. params->addr_type)) {
  172. /* White list can not be used with RPAs */
  173. return 0x00;
  174. }
  175. white_list_entries++;
  176. add_to_white_list(req, params);
  177. }
  178. /* After adding all new pending connections, walk through
  179. * the list of pending reports and also add these to the
  180. * white list if there is still space.
  181. */
  182. list_for_each_entry(params, &hdev->pend_le_reports, action) {
  183. if (hci_bdaddr_list_lookup(&hdev->le_white_list,
  184. &params->addr, params->addr_type))
  185. continue;
  186. if (white_list_entries >= hdev->le_white_list_size) {
  187. /* Select filter policy to accept all advertising */
  188. return 0x00;
  189. }
  190. if (hci_find_irk_by_addr(hdev, &params->addr,
  191. params->addr_type)) {
  192. /* White list can not be used with RPAs */
  193. return 0x00;
  194. }
  195. white_list_entries++;
  196. add_to_white_list(req, params);
  197. }
  198. /* Select filter policy to use white list */
  199. return 0x01;
  200. }
  201. void hci_req_add_le_passive_scan(struct hci_request *req)
  202. {
  203. struct hci_cp_le_set_scan_param param_cp;
  204. struct hci_cp_le_set_scan_enable enable_cp;
  205. struct hci_dev *hdev = req->hdev;
  206. u8 own_addr_type;
  207. u8 filter_policy;
  208. /* Set require_privacy to false since no SCAN_REQ are send
  209. * during passive scanning. Not using an non-resolvable address
  210. * here is important so that peer devices using direct
  211. * advertising with our address will be correctly reported
  212. * by the controller.
  213. */
  214. if (hci_update_random_address(req, false, &own_addr_type))
  215. return;
  216. /* Adding or removing entries from the white list must
  217. * happen before enabling scanning. The controller does
  218. * not allow white list modification while scanning.
  219. */
  220. filter_policy = update_white_list(req);
  221. /* When the controller is using random resolvable addresses and
  222. * with that having LE privacy enabled, then controllers with
  223. * Extended Scanner Filter Policies support can now enable support
  224. * for handling directed advertising.
  225. *
  226. * So instead of using filter polices 0x00 (no whitelist)
  227. * and 0x01 (whitelist enabled) use the new filter policies
  228. * 0x02 (no whitelist) and 0x03 (whitelist enabled).
  229. */
  230. if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
  231. (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
  232. filter_policy |= 0x02;
  233. memset(&param_cp, 0, sizeof(param_cp));
  234. param_cp.type = LE_SCAN_PASSIVE;
  235. param_cp.interval = cpu_to_le16(hdev->le_scan_interval);
  236. param_cp.window = cpu_to_le16(hdev->le_scan_window);
  237. param_cp.own_address_type = own_addr_type;
  238. param_cp.filter_policy = filter_policy;
  239. hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
  240. &param_cp);
  241. memset(&enable_cp, 0, sizeof(enable_cp));
  242. enable_cp.enable = LE_SCAN_ENABLE;
  243. enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  244. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
  245. &enable_cp);
  246. }
  247. static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
  248. {
  249. struct hci_dev *hdev = req->hdev;
  250. /* If we're advertising or initiating an LE connection we can't
  251. * go ahead and change the random address at this time. This is
  252. * because the eventual initiator address used for the
  253. * subsequently created connection will be undefined (some
  254. * controllers use the new address and others the one we had
  255. * when the operation started).
  256. *
  257. * In this kind of scenario skip the update and let the random
  258. * address be updated at the next cycle.
  259. */
  260. if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
  261. hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT)) {
  262. BT_DBG("Deferring random address update");
  263. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  264. return;
  265. }
  266. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa);
  267. }
  268. int hci_update_random_address(struct hci_request *req, bool require_privacy,
  269. u8 *own_addr_type)
  270. {
  271. struct hci_dev *hdev = req->hdev;
  272. int err;
  273. /* If privacy is enabled use a resolvable private address. If
  274. * current RPA has expired or there is something else than
  275. * the current RPA in use, then generate a new one.
  276. */
  277. if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
  278. int to;
  279. *own_addr_type = ADDR_LE_DEV_RANDOM;
  280. if (!hci_dev_test_and_clear_flag(hdev, HCI_RPA_EXPIRED) &&
  281. !bacmp(&hdev->random_addr, &hdev->rpa))
  282. return 0;
  283. err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
  284. if (err < 0) {
  285. BT_ERR("%s failed to generate new RPA", hdev->name);
  286. return err;
  287. }
  288. set_random_addr(req, &hdev->rpa);
  289. to = msecs_to_jiffies(hdev->rpa_timeout * 1000);
  290. queue_delayed_work(hdev->workqueue, &hdev->rpa_expired, to);
  291. return 0;
  292. }
  293. /* In case of required privacy without resolvable private address,
  294. * use an non-resolvable private address. This is useful for active
  295. * scanning and non-connectable advertising.
  296. */
  297. if (require_privacy) {
  298. bdaddr_t nrpa;
  299. while (true) {
  300. /* The non-resolvable private address is generated
  301. * from random six bytes with the two most significant
  302. * bits cleared.
  303. */
  304. get_random_bytes(&nrpa, 6);
  305. nrpa.b[5] &= 0x3f;
  306. /* The non-resolvable private address shall not be
  307. * equal to the public address.
  308. */
  309. if (bacmp(&hdev->bdaddr, &nrpa))
  310. break;
  311. }
  312. *own_addr_type = ADDR_LE_DEV_RANDOM;
  313. set_random_addr(req, &nrpa);
  314. return 0;
  315. }
  316. /* If forcing static address is in use or there is no public
  317. * address use the static address as random address (but skip
  318. * the HCI command if the current random address is already the
  319. * static one.
  320. *
  321. * In case BR/EDR has been disabled on a dual-mode controller
  322. * and a static address has been configured, then use that
  323. * address instead of the public BR/EDR address.
  324. */
  325. if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
  326. !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
  327. (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
  328. bacmp(&hdev->static_addr, BDADDR_ANY))) {
  329. *own_addr_type = ADDR_LE_DEV_RANDOM;
  330. if (bacmp(&hdev->static_addr, &hdev->random_addr))
  331. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
  332. &hdev->static_addr);
  333. return 0;
  334. }
  335. /* Neither privacy nor static address is being used so use a
  336. * public address.
  337. */
  338. *own_addr_type = ADDR_LE_DEV_PUBLIC;
  339. return 0;
  340. }
  341. static bool disconnected_whitelist_entries(struct hci_dev *hdev)
  342. {
  343. struct bdaddr_list *b;
  344. list_for_each_entry(b, &hdev->whitelist, list) {
  345. struct hci_conn *conn;
  346. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
  347. if (!conn)
  348. return true;
  349. if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
  350. return true;
  351. }
  352. return false;
  353. }
  354. void __hci_update_page_scan(struct hci_request *req)
  355. {
  356. struct hci_dev *hdev = req->hdev;
  357. u8 scan;
  358. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  359. return;
  360. if (!hdev_is_powered(hdev))
  361. return;
  362. if (mgmt_powering_down(hdev))
  363. return;
  364. if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
  365. disconnected_whitelist_entries(hdev))
  366. scan = SCAN_PAGE;
  367. else
  368. scan = SCAN_DISABLED;
  369. if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE))
  370. return;
  371. if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  372. scan |= SCAN_INQUIRY;
  373. hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
  374. }
  375. void hci_update_page_scan(struct hci_dev *hdev)
  376. {
  377. struct hci_request req;
  378. hci_req_init(&req, hdev);
  379. __hci_update_page_scan(&req);
  380. hci_req_run(&req, NULL);
  381. }
  382. /* This function controls the background scanning based on hdev->pend_le_conns
  383. * list. If there are pending LE connection we start the background scanning,
  384. * otherwise we stop it.
  385. *
  386. * This function requires the caller holds hdev->lock.
  387. */
  388. void __hci_update_background_scan(struct hci_request *req)
  389. {
  390. struct hci_dev *hdev = req->hdev;
  391. struct hci_conn *conn;
  392. if (!test_bit(HCI_UP, &hdev->flags) ||
  393. test_bit(HCI_INIT, &hdev->flags) ||
  394. hci_dev_test_flag(hdev, HCI_SETUP) ||
  395. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  396. hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
  397. hci_dev_test_flag(hdev, HCI_UNREGISTER))
  398. return;
  399. /* No point in doing scanning if LE support hasn't been enabled */
  400. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  401. return;
  402. /* If discovery is active don't interfere with it */
  403. if (hdev->discovery.state != DISCOVERY_STOPPED)
  404. return;
  405. /* Reset RSSI and UUID filters when starting background scanning
  406. * since these filters are meant for service discovery only.
  407. *
  408. * The Start Discovery and Start Service Discovery operations
  409. * ensure to set proper values for RSSI threshold and UUID
  410. * filter list. So it is safe to just reset them here.
  411. */
  412. hci_discovery_filter_clear(hdev);
  413. if (list_empty(&hdev->pend_le_conns) &&
  414. list_empty(&hdev->pend_le_reports)) {
  415. /* If there is no pending LE connections or devices
  416. * to be scanned for, we should stop the background
  417. * scanning.
  418. */
  419. /* If controller is not scanning we are done. */
  420. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  421. return;
  422. hci_req_add_le_scan_disable(req);
  423. BT_DBG("%s stopping background scanning", hdev->name);
  424. } else {
  425. /* If there is at least one pending LE connection, we should
  426. * keep the background scan running.
  427. */
  428. /* If controller is connecting, we should not start scanning
  429. * since some controllers are not able to scan and connect at
  430. * the same time.
  431. */
  432. conn = hci_conn_hash_lookup_state(hdev, LE_LINK, BT_CONNECT);
  433. if (conn)
  434. return;
  435. /* If controller is currently scanning, we stop it to ensure we
  436. * don't miss any advertising (due to duplicates filter).
  437. */
  438. if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
  439. hci_req_add_le_scan_disable(req);
  440. hci_req_add_le_passive_scan(req);
  441. BT_DBG("%s starting background scanning", hdev->name);
  442. }
  443. }
  444. static void update_background_scan_complete(struct hci_dev *hdev, u8 status,
  445. u16 opcode)
  446. {
  447. if (status)
  448. BT_DBG("HCI request failed to update background scanning: "
  449. "status 0x%2.2x", status);
  450. }
  451. void hci_update_background_scan(struct hci_dev *hdev)
  452. {
  453. int err;
  454. struct hci_request req;
  455. hci_req_init(&req, hdev);
  456. __hci_update_background_scan(&req);
  457. err = hci_req_run(&req, update_background_scan_complete);
  458. if (err && err != -ENODATA)
  459. BT_ERR("Failed to run HCI request: err %d", err);
  460. }