hci_request.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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)->hci.req_complete = complete;
  48. bt_cb(skb)->hci.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)->hci.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)->hci.req_start = true;
  103. bt_cb(skb)->hci.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. /* If the device is neither in pend_le_conns nor
  140. * pend_le_reports then remove it from the whitelist.
  141. */
  142. if (!hci_pend_le_action_lookup(&hdev->pend_le_conns,
  143. &b->bdaddr, b->bdaddr_type) &&
  144. !hci_pend_le_action_lookup(&hdev->pend_le_reports,
  145. &b->bdaddr, b->bdaddr_type)) {
  146. struct hci_cp_le_del_from_white_list cp;
  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. continue;
  152. }
  153. if (hci_find_irk_by_addr(hdev, &b->bdaddr, b->bdaddr_type)) {
  154. /* White list can not be used with RPAs */
  155. return 0x00;
  156. }
  157. white_list_entries++;
  158. }
  159. /* Since all no longer valid white list entries have been
  160. * removed, walk through the list of pending connections
  161. * and ensure that any new device gets programmed into
  162. * the controller.
  163. *
  164. * If the list of the devices is larger than the list of
  165. * available white list entries in the controller, then
  166. * just abort and return filer policy value to not use the
  167. * white list.
  168. */
  169. list_for_each_entry(params, &hdev->pend_le_conns, action) {
  170. if (hci_bdaddr_list_lookup(&hdev->le_white_list,
  171. &params->addr, params->addr_type))
  172. continue;
  173. if (white_list_entries >= hdev->le_white_list_size) {
  174. /* Select filter policy to accept all advertising */
  175. return 0x00;
  176. }
  177. if (hci_find_irk_by_addr(hdev, &params->addr,
  178. params->addr_type)) {
  179. /* White list can not be used with RPAs */
  180. return 0x00;
  181. }
  182. white_list_entries++;
  183. add_to_white_list(req, params);
  184. }
  185. /* After adding all new pending connections, walk through
  186. * the list of pending reports and also add these to the
  187. * white list if there is still space.
  188. */
  189. list_for_each_entry(params, &hdev->pend_le_reports, action) {
  190. if (hci_bdaddr_list_lookup(&hdev->le_white_list,
  191. &params->addr, params->addr_type))
  192. continue;
  193. if (white_list_entries >= hdev->le_white_list_size) {
  194. /* Select filter policy to accept all advertising */
  195. return 0x00;
  196. }
  197. if (hci_find_irk_by_addr(hdev, &params->addr,
  198. params->addr_type)) {
  199. /* White list can not be used with RPAs */
  200. return 0x00;
  201. }
  202. white_list_entries++;
  203. add_to_white_list(req, params);
  204. }
  205. /* Select filter policy to use white list */
  206. return 0x01;
  207. }
  208. void hci_req_add_le_passive_scan(struct hci_request *req)
  209. {
  210. struct hci_cp_le_set_scan_param param_cp;
  211. struct hci_cp_le_set_scan_enable enable_cp;
  212. struct hci_dev *hdev = req->hdev;
  213. u8 own_addr_type;
  214. u8 filter_policy;
  215. /* Set require_privacy to false since no SCAN_REQ are send
  216. * during passive scanning. Not using an non-resolvable address
  217. * here is important so that peer devices using direct
  218. * advertising with our address will be correctly reported
  219. * by the controller.
  220. */
  221. if (hci_update_random_address(req, false, &own_addr_type))
  222. return;
  223. /* Adding or removing entries from the white list must
  224. * happen before enabling scanning. The controller does
  225. * not allow white list modification while scanning.
  226. */
  227. filter_policy = update_white_list(req);
  228. /* When the controller is using random resolvable addresses and
  229. * with that having LE privacy enabled, then controllers with
  230. * Extended Scanner Filter Policies support can now enable support
  231. * for handling directed advertising.
  232. *
  233. * So instead of using filter polices 0x00 (no whitelist)
  234. * and 0x01 (whitelist enabled) use the new filter policies
  235. * 0x02 (no whitelist) and 0x03 (whitelist enabled).
  236. */
  237. if (hci_dev_test_flag(hdev, HCI_PRIVACY) &&
  238. (hdev->le_features[0] & HCI_LE_EXT_SCAN_POLICY))
  239. filter_policy |= 0x02;
  240. memset(&param_cp, 0, sizeof(param_cp));
  241. param_cp.type = LE_SCAN_PASSIVE;
  242. param_cp.interval = cpu_to_le16(hdev->le_scan_interval);
  243. param_cp.window = cpu_to_le16(hdev->le_scan_window);
  244. param_cp.own_address_type = own_addr_type;
  245. param_cp.filter_policy = filter_policy;
  246. hci_req_add(req, HCI_OP_LE_SET_SCAN_PARAM, sizeof(param_cp),
  247. &param_cp);
  248. memset(&enable_cp, 0, sizeof(enable_cp));
  249. enable_cp.enable = LE_SCAN_ENABLE;
  250. enable_cp.filter_dup = LE_SCAN_FILTER_DUP_ENABLE;
  251. hci_req_add(req, HCI_OP_LE_SET_SCAN_ENABLE, sizeof(enable_cp),
  252. &enable_cp);
  253. }
  254. static void set_random_addr(struct hci_request *req, bdaddr_t *rpa)
  255. {
  256. struct hci_dev *hdev = req->hdev;
  257. /* If we're advertising or initiating an LE connection we can't
  258. * go ahead and change the random address at this time. This is
  259. * because the eventual initiator address used for the
  260. * subsequently created connection will be undefined (some
  261. * controllers use the new address and others the one we had
  262. * when the operation started).
  263. *
  264. * In this kind of scenario skip the update and let the random
  265. * address be updated at the next cycle.
  266. */
  267. if (hci_dev_test_flag(hdev, HCI_LE_ADV) ||
  268. hci_lookup_le_connect(hdev)) {
  269. BT_DBG("Deferring random address update");
  270. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  271. return;
  272. }
  273. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6, rpa);
  274. }
  275. int hci_update_random_address(struct hci_request *req, bool require_privacy,
  276. u8 *own_addr_type)
  277. {
  278. struct hci_dev *hdev = req->hdev;
  279. int err;
  280. /* If privacy is enabled use a resolvable private address. If
  281. * current RPA has expired or there is something else than
  282. * the current RPA in use, then generate a new one.
  283. */
  284. if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
  285. int to;
  286. *own_addr_type = ADDR_LE_DEV_RANDOM;
  287. if (!hci_dev_test_and_clear_flag(hdev, HCI_RPA_EXPIRED) &&
  288. !bacmp(&hdev->random_addr, &hdev->rpa))
  289. return 0;
  290. err = smp_generate_rpa(hdev, hdev->irk, &hdev->rpa);
  291. if (err < 0) {
  292. BT_ERR("%s failed to generate new RPA", hdev->name);
  293. return err;
  294. }
  295. set_random_addr(req, &hdev->rpa);
  296. to = msecs_to_jiffies(hdev->rpa_timeout * 1000);
  297. queue_delayed_work(hdev->workqueue, &hdev->rpa_expired, to);
  298. return 0;
  299. }
  300. /* In case of required privacy without resolvable private address,
  301. * use an non-resolvable private address. This is useful for active
  302. * scanning and non-connectable advertising.
  303. */
  304. if (require_privacy) {
  305. bdaddr_t nrpa;
  306. while (true) {
  307. /* The non-resolvable private address is generated
  308. * from random six bytes with the two most significant
  309. * bits cleared.
  310. */
  311. get_random_bytes(&nrpa, 6);
  312. nrpa.b[5] &= 0x3f;
  313. /* The non-resolvable private address shall not be
  314. * equal to the public address.
  315. */
  316. if (bacmp(&hdev->bdaddr, &nrpa))
  317. break;
  318. }
  319. *own_addr_type = ADDR_LE_DEV_RANDOM;
  320. set_random_addr(req, &nrpa);
  321. return 0;
  322. }
  323. /* If forcing static address is in use or there is no public
  324. * address use the static address as random address (but skip
  325. * the HCI command if the current random address is already the
  326. * static one.
  327. *
  328. * In case BR/EDR has been disabled on a dual-mode controller
  329. * and a static address has been configured, then use that
  330. * address instead of the public BR/EDR address.
  331. */
  332. if (hci_dev_test_flag(hdev, HCI_FORCE_STATIC_ADDR) ||
  333. !bacmp(&hdev->bdaddr, BDADDR_ANY) ||
  334. (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED) &&
  335. bacmp(&hdev->static_addr, BDADDR_ANY))) {
  336. *own_addr_type = ADDR_LE_DEV_RANDOM;
  337. if (bacmp(&hdev->static_addr, &hdev->random_addr))
  338. hci_req_add(req, HCI_OP_LE_SET_RANDOM_ADDR, 6,
  339. &hdev->static_addr);
  340. return 0;
  341. }
  342. /* Neither privacy nor static address is being used so use a
  343. * public address.
  344. */
  345. *own_addr_type = ADDR_LE_DEV_PUBLIC;
  346. return 0;
  347. }
  348. static bool disconnected_whitelist_entries(struct hci_dev *hdev)
  349. {
  350. struct bdaddr_list *b;
  351. list_for_each_entry(b, &hdev->whitelist, list) {
  352. struct hci_conn *conn;
  353. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &b->bdaddr);
  354. if (!conn)
  355. return true;
  356. if (conn->state != BT_CONNECTED && conn->state != BT_CONFIG)
  357. return true;
  358. }
  359. return false;
  360. }
  361. void __hci_update_page_scan(struct hci_request *req)
  362. {
  363. struct hci_dev *hdev = req->hdev;
  364. u8 scan;
  365. if (!hci_dev_test_flag(hdev, HCI_BREDR_ENABLED))
  366. return;
  367. if (!hdev_is_powered(hdev))
  368. return;
  369. if (mgmt_powering_down(hdev))
  370. return;
  371. if (hci_dev_test_flag(hdev, HCI_CONNECTABLE) ||
  372. disconnected_whitelist_entries(hdev))
  373. scan = SCAN_PAGE;
  374. else
  375. scan = SCAN_DISABLED;
  376. if (test_bit(HCI_PSCAN, &hdev->flags) == !!(scan & SCAN_PAGE))
  377. return;
  378. if (hci_dev_test_flag(hdev, HCI_DISCOVERABLE))
  379. scan |= SCAN_INQUIRY;
  380. hci_req_add(req, HCI_OP_WRITE_SCAN_ENABLE, 1, &scan);
  381. }
  382. void hci_update_page_scan(struct hci_dev *hdev)
  383. {
  384. struct hci_request req;
  385. hci_req_init(&req, hdev);
  386. __hci_update_page_scan(&req);
  387. hci_req_run(&req, NULL);
  388. }
  389. /* This function controls the background scanning based on hdev->pend_le_conns
  390. * list. If there are pending LE connection we start the background scanning,
  391. * otherwise we stop it.
  392. *
  393. * This function requires the caller holds hdev->lock.
  394. */
  395. void __hci_update_background_scan(struct hci_request *req)
  396. {
  397. struct hci_dev *hdev = req->hdev;
  398. if (!test_bit(HCI_UP, &hdev->flags) ||
  399. test_bit(HCI_INIT, &hdev->flags) ||
  400. hci_dev_test_flag(hdev, HCI_SETUP) ||
  401. hci_dev_test_flag(hdev, HCI_CONFIG) ||
  402. hci_dev_test_flag(hdev, HCI_AUTO_OFF) ||
  403. hci_dev_test_flag(hdev, HCI_UNREGISTER))
  404. return;
  405. /* No point in doing scanning if LE support hasn't been enabled */
  406. if (!hci_dev_test_flag(hdev, HCI_LE_ENABLED))
  407. return;
  408. /* If discovery is active don't interfere with it */
  409. if (hdev->discovery.state != DISCOVERY_STOPPED)
  410. return;
  411. /* Reset RSSI and UUID filters when starting background scanning
  412. * since these filters are meant for service discovery only.
  413. *
  414. * The Start Discovery and Start Service Discovery operations
  415. * ensure to set proper values for RSSI threshold and UUID
  416. * filter list. So it is safe to just reset them here.
  417. */
  418. hci_discovery_filter_clear(hdev);
  419. if (list_empty(&hdev->pend_le_conns) &&
  420. list_empty(&hdev->pend_le_reports)) {
  421. /* If there is no pending LE connections or devices
  422. * to be scanned for, we should stop the background
  423. * scanning.
  424. */
  425. /* If controller is not scanning we are done. */
  426. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN))
  427. return;
  428. hci_req_add_le_scan_disable(req);
  429. BT_DBG("%s stopping background scanning", hdev->name);
  430. } else {
  431. /* If there is at least one pending LE connection, we should
  432. * keep the background scan running.
  433. */
  434. /* If controller is connecting, we should not start scanning
  435. * since some controllers are not able to scan and connect at
  436. * the same time.
  437. */
  438. if (hci_lookup_le_connect(hdev))
  439. return;
  440. /* If controller is currently scanning, we stop it to ensure we
  441. * don't miss any advertising (due to duplicates filter).
  442. */
  443. if (hci_dev_test_flag(hdev, HCI_LE_SCAN))
  444. hci_req_add_le_scan_disable(req);
  445. hci_req_add_le_passive_scan(req);
  446. BT_DBG("%s starting background scanning", hdev->name);
  447. }
  448. }
  449. static void update_background_scan_complete(struct hci_dev *hdev, u8 status,
  450. u16 opcode)
  451. {
  452. if (status)
  453. BT_DBG("HCI request failed to update background scanning: "
  454. "status 0x%2.2x", status);
  455. }
  456. void hci_update_background_scan(struct hci_dev *hdev)
  457. {
  458. int err;
  459. struct hci_request req;
  460. hci_req_init(&req, hdev);
  461. __hci_update_background_scan(&req);
  462. err = hci_req_run(&req, update_background_scan_complete);
  463. if (err && err != -ENODATA)
  464. BT_ERR("Failed to run HCI request: err %d", err);
  465. }
  466. void __hci_abort_conn(struct hci_request *req, struct hci_conn *conn,
  467. u8 reason)
  468. {
  469. switch (conn->state) {
  470. case BT_CONNECTED:
  471. case BT_CONFIG:
  472. if (conn->type == AMP_LINK) {
  473. struct hci_cp_disconn_phy_link cp;
  474. cp.phy_handle = HCI_PHY_HANDLE(conn->handle);
  475. cp.reason = reason;
  476. hci_req_add(req, HCI_OP_DISCONN_PHY_LINK, sizeof(cp),
  477. &cp);
  478. } else {
  479. struct hci_cp_disconnect dc;
  480. dc.handle = cpu_to_le16(conn->handle);
  481. dc.reason = reason;
  482. hci_req_add(req, HCI_OP_DISCONNECT, sizeof(dc), &dc);
  483. }
  484. conn->state = BT_DISCONN;
  485. break;
  486. case BT_CONNECT:
  487. if (conn->type == LE_LINK) {
  488. if (test_bit(HCI_CONN_SCANNING, &conn->flags))
  489. break;
  490. hci_req_add(req, HCI_OP_LE_CREATE_CONN_CANCEL,
  491. 0, NULL);
  492. } else if (conn->type == ACL_LINK) {
  493. if (req->hdev->hci_ver < BLUETOOTH_VER_1_2)
  494. break;
  495. hci_req_add(req, HCI_OP_CREATE_CONN_CANCEL,
  496. 6, &conn->dst);
  497. }
  498. break;
  499. case BT_CONNECT2:
  500. if (conn->type == ACL_LINK) {
  501. struct hci_cp_reject_conn_req rej;
  502. bacpy(&rej.bdaddr, &conn->dst);
  503. rej.reason = reason;
  504. hci_req_add(req, HCI_OP_REJECT_CONN_REQ,
  505. sizeof(rej), &rej);
  506. } else if (conn->type == SCO_LINK || conn->type == ESCO_LINK) {
  507. struct hci_cp_reject_sync_conn_req rej;
  508. bacpy(&rej.bdaddr, &conn->dst);
  509. /* SCO rejection has its own limited set of
  510. * allowed error values (0x0D-0x0F) which isn't
  511. * compatible with most values passed to this
  512. * function. To be safe hard-code one of the
  513. * values that's suitable for SCO.
  514. */
  515. rej.reason = HCI_ERROR_REMOTE_LOW_RESOURCES;
  516. hci_req_add(req, HCI_OP_REJECT_SYNC_CONN_REQ,
  517. sizeof(rej), &rej);
  518. }
  519. break;
  520. default:
  521. conn->state = BT_CLOSED;
  522. break;
  523. }
  524. }
  525. static void abort_conn_complete(struct hci_dev *hdev, u8 status, u16 opcode)
  526. {
  527. if (status)
  528. BT_DBG("Failed to abort connection: status 0x%2.2x", status);
  529. }
  530. int hci_abort_conn(struct hci_conn *conn, u8 reason)
  531. {
  532. struct hci_request req;
  533. int err;
  534. hci_req_init(&req, conn->hdev);
  535. __hci_abort_conn(&req, conn, reason);
  536. err = hci_req_run(&req, abort_conn_complete);
  537. if (err && err != -ENODATA) {
  538. BT_ERR("Failed to run HCI request: err %d", err);
  539. return err;
  540. }
  541. return 0;
  542. }