hci_request.c 14 KB

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