PageRenderTime 76ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/net/bluetooth/hci_event.c

http://github.com/mirrors/linux
C | 6167 lines | 4315 code | 1438 blank | 414 comment | 824 complexity | 20943b43737aa6ea0cb07baf7990615a MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. /*
  2. BlueZ - Bluetooth protocol stack for Linux
  3. Copyright (c) 2000-2001, 2010, Code Aurora Forum. All rights reserved.
  4. Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License version 2 as
  7. published by the Free Software Foundation;
  8. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  9. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  10. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  11. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  12. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  13. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  17. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  18. SOFTWARE IS DISCLAIMED.
  19. */
  20. /* Bluetooth HCI event handling. */
  21. #include <asm/unaligned.h>
  22. #include <net/bluetooth/bluetooth.h>
  23. #include <net/bluetooth/hci_core.h>
  24. #include <net/bluetooth/mgmt.h>
  25. #include "hci_request.h"
  26. #include "hci_debugfs.h"
  27. #include "a2mp.h"
  28. #include "amp.h"
  29. #include "smp.h"
  30. #define ZERO_KEY "\x00\x00\x00\x00\x00\x00\x00\x00" \
  31. "\x00\x00\x00\x00\x00\x00\x00\x00"
  32. /* Handle HCI Event packets */
  33. static void hci_cc_inquiry_cancel(struct hci_dev *hdev, struct sk_buff *skb)
  34. {
  35. __u8 status = *((__u8 *) skb->data);
  36. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  37. if (status)
  38. return;
  39. clear_bit(HCI_INQUIRY, &hdev->flags);
  40. smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
  41. wake_up_bit(&hdev->flags, HCI_INQUIRY);
  42. hci_dev_lock(hdev);
  43. /* Set discovery state to stopped if we're not doing LE active
  44. * scanning.
  45. */
  46. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
  47. hdev->le_scan_type != LE_SCAN_ACTIVE)
  48. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  49. hci_dev_unlock(hdev);
  50. hci_conn_check_pending(hdev);
  51. }
  52. static void hci_cc_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
  53. {
  54. __u8 status = *((__u8 *) skb->data);
  55. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  56. if (status)
  57. return;
  58. hci_dev_set_flag(hdev, HCI_PERIODIC_INQ);
  59. }
  60. static void hci_cc_exit_periodic_inq(struct hci_dev *hdev, struct sk_buff *skb)
  61. {
  62. __u8 status = *((__u8 *) skb->data);
  63. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  64. if (status)
  65. return;
  66. hci_dev_clear_flag(hdev, HCI_PERIODIC_INQ);
  67. hci_conn_check_pending(hdev);
  68. }
  69. static void hci_cc_remote_name_req_cancel(struct hci_dev *hdev,
  70. struct sk_buff *skb)
  71. {
  72. BT_DBG("%s", hdev->name);
  73. }
  74. static void hci_cc_role_discovery(struct hci_dev *hdev, struct sk_buff *skb)
  75. {
  76. struct hci_rp_role_discovery *rp = (void *) skb->data;
  77. struct hci_conn *conn;
  78. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  79. if (rp->status)
  80. return;
  81. hci_dev_lock(hdev);
  82. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  83. if (conn)
  84. conn->role = rp->role;
  85. hci_dev_unlock(hdev);
  86. }
  87. static void hci_cc_read_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
  88. {
  89. struct hci_rp_read_link_policy *rp = (void *) skb->data;
  90. struct hci_conn *conn;
  91. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  92. if (rp->status)
  93. return;
  94. hci_dev_lock(hdev);
  95. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  96. if (conn)
  97. conn->link_policy = __le16_to_cpu(rp->policy);
  98. hci_dev_unlock(hdev);
  99. }
  100. static void hci_cc_write_link_policy(struct hci_dev *hdev, struct sk_buff *skb)
  101. {
  102. struct hci_rp_write_link_policy *rp = (void *) skb->data;
  103. struct hci_conn *conn;
  104. void *sent;
  105. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  106. if (rp->status)
  107. return;
  108. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LINK_POLICY);
  109. if (!sent)
  110. return;
  111. hci_dev_lock(hdev);
  112. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  113. if (conn)
  114. conn->link_policy = get_unaligned_le16(sent + 2);
  115. hci_dev_unlock(hdev);
  116. }
  117. static void hci_cc_read_def_link_policy(struct hci_dev *hdev,
  118. struct sk_buff *skb)
  119. {
  120. struct hci_rp_read_def_link_policy *rp = (void *) skb->data;
  121. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  122. if (rp->status)
  123. return;
  124. hdev->link_policy = __le16_to_cpu(rp->policy);
  125. }
  126. static void hci_cc_write_def_link_policy(struct hci_dev *hdev,
  127. struct sk_buff *skb)
  128. {
  129. __u8 status = *((__u8 *) skb->data);
  130. void *sent;
  131. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  132. if (status)
  133. return;
  134. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_LINK_POLICY);
  135. if (!sent)
  136. return;
  137. hdev->link_policy = get_unaligned_le16(sent);
  138. }
  139. static void hci_cc_reset(struct hci_dev *hdev, struct sk_buff *skb)
  140. {
  141. __u8 status = *((__u8 *) skb->data);
  142. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  143. clear_bit(HCI_RESET, &hdev->flags);
  144. if (status)
  145. return;
  146. /* Reset all non-persistent flags */
  147. hci_dev_clear_volatile_flags(hdev);
  148. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  149. hdev->inq_tx_power = HCI_TX_POWER_INVALID;
  150. hdev->adv_tx_power = HCI_TX_POWER_INVALID;
  151. memset(hdev->adv_data, 0, sizeof(hdev->adv_data));
  152. hdev->adv_data_len = 0;
  153. memset(hdev->scan_rsp_data, 0, sizeof(hdev->scan_rsp_data));
  154. hdev->scan_rsp_data_len = 0;
  155. hdev->le_scan_type = LE_SCAN_PASSIVE;
  156. hdev->ssp_debug_mode = 0;
  157. hci_bdaddr_list_clear(&hdev->le_white_list);
  158. hci_bdaddr_list_clear(&hdev->le_resolv_list);
  159. }
  160. static void hci_cc_read_stored_link_key(struct hci_dev *hdev,
  161. struct sk_buff *skb)
  162. {
  163. struct hci_rp_read_stored_link_key *rp = (void *)skb->data;
  164. struct hci_cp_read_stored_link_key *sent;
  165. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  166. sent = hci_sent_cmd_data(hdev, HCI_OP_READ_STORED_LINK_KEY);
  167. if (!sent)
  168. return;
  169. if (!rp->status && sent->read_all == 0x01) {
  170. hdev->stored_max_keys = rp->max_keys;
  171. hdev->stored_num_keys = rp->num_keys;
  172. }
  173. }
  174. static void hci_cc_delete_stored_link_key(struct hci_dev *hdev,
  175. struct sk_buff *skb)
  176. {
  177. struct hci_rp_delete_stored_link_key *rp = (void *)skb->data;
  178. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  179. if (rp->status)
  180. return;
  181. if (rp->num_keys <= hdev->stored_num_keys)
  182. hdev->stored_num_keys -= rp->num_keys;
  183. else
  184. hdev->stored_num_keys = 0;
  185. }
  186. static void hci_cc_write_local_name(struct hci_dev *hdev, struct sk_buff *skb)
  187. {
  188. __u8 status = *((__u8 *) skb->data);
  189. void *sent;
  190. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  191. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LOCAL_NAME);
  192. if (!sent)
  193. return;
  194. hci_dev_lock(hdev);
  195. if (hci_dev_test_flag(hdev, HCI_MGMT))
  196. mgmt_set_local_name_complete(hdev, sent, status);
  197. else if (!status)
  198. memcpy(hdev->dev_name, sent, HCI_MAX_NAME_LENGTH);
  199. hci_dev_unlock(hdev);
  200. }
  201. static void hci_cc_read_local_name(struct hci_dev *hdev, struct sk_buff *skb)
  202. {
  203. struct hci_rp_read_local_name *rp = (void *) skb->data;
  204. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  205. if (rp->status)
  206. return;
  207. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  208. hci_dev_test_flag(hdev, HCI_CONFIG))
  209. memcpy(hdev->dev_name, rp->name, HCI_MAX_NAME_LENGTH);
  210. }
  211. static void hci_cc_write_auth_enable(struct hci_dev *hdev, struct sk_buff *skb)
  212. {
  213. __u8 status = *((__u8 *) skb->data);
  214. void *sent;
  215. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  216. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_ENABLE);
  217. if (!sent)
  218. return;
  219. hci_dev_lock(hdev);
  220. if (!status) {
  221. __u8 param = *((__u8 *) sent);
  222. if (param == AUTH_ENABLED)
  223. set_bit(HCI_AUTH, &hdev->flags);
  224. else
  225. clear_bit(HCI_AUTH, &hdev->flags);
  226. }
  227. if (hci_dev_test_flag(hdev, HCI_MGMT))
  228. mgmt_auth_enable_complete(hdev, status);
  229. hci_dev_unlock(hdev);
  230. }
  231. static void hci_cc_write_encrypt_mode(struct hci_dev *hdev, struct sk_buff *skb)
  232. {
  233. __u8 status = *((__u8 *) skb->data);
  234. __u8 param;
  235. void *sent;
  236. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  237. if (status)
  238. return;
  239. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_ENCRYPT_MODE);
  240. if (!sent)
  241. return;
  242. param = *((__u8 *) sent);
  243. if (param)
  244. set_bit(HCI_ENCRYPT, &hdev->flags);
  245. else
  246. clear_bit(HCI_ENCRYPT, &hdev->flags);
  247. }
  248. static void hci_cc_write_scan_enable(struct hci_dev *hdev, struct sk_buff *skb)
  249. {
  250. __u8 status = *((__u8 *) skb->data);
  251. __u8 param;
  252. void *sent;
  253. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  254. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SCAN_ENABLE);
  255. if (!sent)
  256. return;
  257. param = *((__u8 *) sent);
  258. hci_dev_lock(hdev);
  259. if (status) {
  260. hdev->discov_timeout = 0;
  261. goto done;
  262. }
  263. if (param & SCAN_INQUIRY)
  264. set_bit(HCI_ISCAN, &hdev->flags);
  265. else
  266. clear_bit(HCI_ISCAN, &hdev->flags);
  267. if (param & SCAN_PAGE)
  268. set_bit(HCI_PSCAN, &hdev->flags);
  269. else
  270. clear_bit(HCI_PSCAN, &hdev->flags);
  271. done:
  272. hci_dev_unlock(hdev);
  273. }
  274. static void hci_cc_read_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
  275. {
  276. struct hci_rp_read_class_of_dev *rp = (void *) skb->data;
  277. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  278. if (rp->status)
  279. return;
  280. memcpy(hdev->dev_class, rp->dev_class, 3);
  281. BT_DBG("%s class 0x%.2x%.2x%.2x", hdev->name,
  282. hdev->dev_class[2], hdev->dev_class[1], hdev->dev_class[0]);
  283. }
  284. static void hci_cc_write_class_of_dev(struct hci_dev *hdev, struct sk_buff *skb)
  285. {
  286. __u8 status = *((__u8 *) skb->data);
  287. void *sent;
  288. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  289. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_CLASS_OF_DEV);
  290. if (!sent)
  291. return;
  292. hci_dev_lock(hdev);
  293. if (status == 0)
  294. memcpy(hdev->dev_class, sent, 3);
  295. if (hci_dev_test_flag(hdev, HCI_MGMT))
  296. mgmt_set_class_of_dev_complete(hdev, sent, status);
  297. hci_dev_unlock(hdev);
  298. }
  299. static void hci_cc_read_voice_setting(struct hci_dev *hdev, struct sk_buff *skb)
  300. {
  301. struct hci_rp_read_voice_setting *rp = (void *) skb->data;
  302. __u16 setting;
  303. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  304. if (rp->status)
  305. return;
  306. setting = __le16_to_cpu(rp->voice_setting);
  307. if (hdev->voice_setting == setting)
  308. return;
  309. hdev->voice_setting = setting;
  310. BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
  311. if (hdev->notify)
  312. hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
  313. }
  314. static void hci_cc_write_voice_setting(struct hci_dev *hdev,
  315. struct sk_buff *skb)
  316. {
  317. __u8 status = *((__u8 *) skb->data);
  318. __u16 setting;
  319. void *sent;
  320. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  321. if (status)
  322. return;
  323. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_VOICE_SETTING);
  324. if (!sent)
  325. return;
  326. setting = get_unaligned_le16(sent);
  327. if (hdev->voice_setting == setting)
  328. return;
  329. hdev->voice_setting = setting;
  330. BT_DBG("%s voice setting 0x%4.4x", hdev->name, setting);
  331. if (hdev->notify)
  332. hdev->notify(hdev, HCI_NOTIFY_VOICE_SETTING);
  333. }
  334. static void hci_cc_read_num_supported_iac(struct hci_dev *hdev,
  335. struct sk_buff *skb)
  336. {
  337. struct hci_rp_read_num_supported_iac *rp = (void *) skb->data;
  338. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  339. if (rp->status)
  340. return;
  341. hdev->num_iac = rp->num_iac;
  342. BT_DBG("%s num iac %d", hdev->name, hdev->num_iac);
  343. }
  344. static void hci_cc_write_ssp_mode(struct hci_dev *hdev, struct sk_buff *skb)
  345. {
  346. __u8 status = *((__u8 *) skb->data);
  347. struct hci_cp_write_ssp_mode *sent;
  348. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  349. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_MODE);
  350. if (!sent)
  351. return;
  352. hci_dev_lock(hdev);
  353. if (!status) {
  354. if (sent->mode)
  355. hdev->features[1][0] |= LMP_HOST_SSP;
  356. else
  357. hdev->features[1][0] &= ~LMP_HOST_SSP;
  358. }
  359. if (hci_dev_test_flag(hdev, HCI_MGMT))
  360. mgmt_ssp_enable_complete(hdev, sent->mode, status);
  361. else if (!status) {
  362. if (sent->mode)
  363. hci_dev_set_flag(hdev, HCI_SSP_ENABLED);
  364. else
  365. hci_dev_clear_flag(hdev, HCI_SSP_ENABLED);
  366. }
  367. hci_dev_unlock(hdev);
  368. }
  369. static void hci_cc_write_sc_support(struct hci_dev *hdev, struct sk_buff *skb)
  370. {
  371. u8 status = *((u8 *) skb->data);
  372. struct hci_cp_write_sc_support *sent;
  373. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  374. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SC_SUPPORT);
  375. if (!sent)
  376. return;
  377. hci_dev_lock(hdev);
  378. if (!status) {
  379. if (sent->support)
  380. hdev->features[1][0] |= LMP_HOST_SC;
  381. else
  382. hdev->features[1][0] &= ~LMP_HOST_SC;
  383. }
  384. if (!hci_dev_test_flag(hdev, HCI_MGMT) && !status) {
  385. if (sent->support)
  386. hci_dev_set_flag(hdev, HCI_SC_ENABLED);
  387. else
  388. hci_dev_clear_flag(hdev, HCI_SC_ENABLED);
  389. }
  390. hci_dev_unlock(hdev);
  391. }
  392. static void hci_cc_read_local_version(struct hci_dev *hdev, struct sk_buff *skb)
  393. {
  394. struct hci_rp_read_local_version *rp = (void *) skb->data;
  395. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  396. if (rp->status)
  397. return;
  398. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  399. hci_dev_test_flag(hdev, HCI_CONFIG)) {
  400. hdev->hci_ver = rp->hci_ver;
  401. hdev->hci_rev = __le16_to_cpu(rp->hci_rev);
  402. hdev->lmp_ver = rp->lmp_ver;
  403. hdev->manufacturer = __le16_to_cpu(rp->manufacturer);
  404. hdev->lmp_subver = __le16_to_cpu(rp->lmp_subver);
  405. }
  406. }
  407. static void hci_cc_read_local_commands(struct hci_dev *hdev,
  408. struct sk_buff *skb)
  409. {
  410. struct hci_rp_read_local_commands *rp = (void *) skb->data;
  411. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  412. if (rp->status)
  413. return;
  414. if (hci_dev_test_flag(hdev, HCI_SETUP) ||
  415. hci_dev_test_flag(hdev, HCI_CONFIG))
  416. memcpy(hdev->commands, rp->commands, sizeof(hdev->commands));
  417. }
  418. static void hci_cc_read_auth_payload_timeout(struct hci_dev *hdev,
  419. struct sk_buff *skb)
  420. {
  421. struct hci_rp_read_auth_payload_to *rp = (void *)skb->data;
  422. struct hci_conn *conn;
  423. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  424. if (rp->status)
  425. return;
  426. hci_dev_lock(hdev);
  427. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  428. if (conn)
  429. conn->auth_payload_timeout = __le16_to_cpu(rp->timeout);
  430. hci_dev_unlock(hdev);
  431. }
  432. static void hci_cc_write_auth_payload_timeout(struct hci_dev *hdev,
  433. struct sk_buff *skb)
  434. {
  435. struct hci_rp_write_auth_payload_to *rp = (void *)skb->data;
  436. struct hci_conn *conn;
  437. void *sent;
  438. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  439. if (rp->status)
  440. return;
  441. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO);
  442. if (!sent)
  443. return;
  444. hci_dev_lock(hdev);
  445. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  446. if (conn)
  447. conn->auth_payload_timeout = get_unaligned_le16(sent + 2);
  448. hci_dev_unlock(hdev);
  449. }
  450. static void hci_cc_read_local_features(struct hci_dev *hdev,
  451. struct sk_buff *skb)
  452. {
  453. struct hci_rp_read_local_features *rp = (void *) skb->data;
  454. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  455. if (rp->status)
  456. return;
  457. memcpy(hdev->features, rp->features, 8);
  458. /* Adjust default settings according to features
  459. * supported by device. */
  460. if (hdev->features[0][0] & LMP_3SLOT)
  461. hdev->pkt_type |= (HCI_DM3 | HCI_DH3);
  462. if (hdev->features[0][0] & LMP_5SLOT)
  463. hdev->pkt_type |= (HCI_DM5 | HCI_DH5);
  464. if (hdev->features[0][1] & LMP_HV2) {
  465. hdev->pkt_type |= (HCI_HV2);
  466. hdev->esco_type |= (ESCO_HV2);
  467. }
  468. if (hdev->features[0][1] & LMP_HV3) {
  469. hdev->pkt_type |= (HCI_HV3);
  470. hdev->esco_type |= (ESCO_HV3);
  471. }
  472. if (lmp_esco_capable(hdev))
  473. hdev->esco_type |= (ESCO_EV3);
  474. if (hdev->features[0][4] & LMP_EV4)
  475. hdev->esco_type |= (ESCO_EV4);
  476. if (hdev->features[0][4] & LMP_EV5)
  477. hdev->esco_type |= (ESCO_EV5);
  478. if (hdev->features[0][5] & LMP_EDR_ESCO_2M)
  479. hdev->esco_type |= (ESCO_2EV3);
  480. if (hdev->features[0][5] & LMP_EDR_ESCO_3M)
  481. hdev->esco_type |= (ESCO_3EV3);
  482. if (hdev->features[0][5] & LMP_EDR_3S_ESCO)
  483. hdev->esco_type |= (ESCO_2EV5 | ESCO_3EV5);
  484. }
  485. static void hci_cc_read_local_ext_features(struct hci_dev *hdev,
  486. struct sk_buff *skb)
  487. {
  488. struct hci_rp_read_local_ext_features *rp = (void *) skb->data;
  489. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  490. if (rp->status)
  491. return;
  492. if (hdev->max_page < rp->max_page)
  493. hdev->max_page = rp->max_page;
  494. if (rp->page < HCI_MAX_PAGES)
  495. memcpy(hdev->features[rp->page], rp->features, 8);
  496. }
  497. static void hci_cc_read_flow_control_mode(struct hci_dev *hdev,
  498. struct sk_buff *skb)
  499. {
  500. struct hci_rp_read_flow_control_mode *rp = (void *) skb->data;
  501. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  502. if (rp->status)
  503. return;
  504. hdev->flow_ctl_mode = rp->mode;
  505. }
  506. static void hci_cc_read_buffer_size(struct hci_dev *hdev, struct sk_buff *skb)
  507. {
  508. struct hci_rp_read_buffer_size *rp = (void *) skb->data;
  509. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  510. if (rp->status)
  511. return;
  512. hdev->acl_mtu = __le16_to_cpu(rp->acl_mtu);
  513. hdev->sco_mtu = rp->sco_mtu;
  514. hdev->acl_pkts = __le16_to_cpu(rp->acl_max_pkt);
  515. hdev->sco_pkts = __le16_to_cpu(rp->sco_max_pkt);
  516. if (test_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks)) {
  517. hdev->sco_mtu = 64;
  518. hdev->sco_pkts = 8;
  519. }
  520. hdev->acl_cnt = hdev->acl_pkts;
  521. hdev->sco_cnt = hdev->sco_pkts;
  522. BT_DBG("%s acl mtu %d:%d sco mtu %d:%d", hdev->name, hdev->acl_mtu,
  523. hdev->acl_pkts, hdev->sco_mtu, hdev->sco_pkts);
  524. }
  525. static void hci_cc_read_bd_addr(struct hci_dev *hdev, struct sk_buff *skb)
  526. {
  527. struct hci_rp_read_bd_addr *rp = (void *) skb->data;
  528. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  529. if (rp->status)
  530. return;
  531. if (test_bit(HCI_INIT, &hdev->flags))
  532. bacpy(&hdev->bdaddr, &rp->bdaddr);
  533. if (hci_dev_test_flag(hdev, HCI_SETUP))
  534. bacpy(&hdev->setup_addr, &rp->bdaddr);
  535. }
  536. static void hci_cc_read_page_scan_activity(struct hci_dev *hdev,
  537. struct sk_buff *skb)
  538. {
  539. struct hci_rp_read_page_scan_activity *rp = (void *) skb->data;
  540. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  541. if (rp->status)
  542. return;
  543. if (test_bit(HCI_INIT, &hdev->flags)) {
  544. hdev->page_scan_interval = __le16_to_cpu(rp->interval);
  545. hdev->page_scan_window = __le16_to_cpu(rp->window);
  546. }
  547. }
  548. static void hci_cc_write_page_scan_activity(struct hci_dev *hdev,
  549. struct sk_buff *skb)
  550. {
  551. u8 status = *((u8 *) skb->data);
  552. struct hci_cp_write_page_scan_activity *sent;
  553. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  554. if (status)
  555. return;
  556. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_ACTIVITY);
  557. if (!sent)
  558. return;
  559. hdev->page_scan_interval = __le16_to_cpu(sent->interval);
  560. hdev->page_scan_window = __le16_to_cpu(sent->window);
  561. }
  562. static void hci_cc_read_page_scan_type(struct hci_dev *hdev,
  563. struct sk_buff *skb)
  564. {
  565. struct hci_rp_read_page_scan_type *rp = (void *) skb->data;
  566. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  567. if (rp->status)
  568. return;
  569. if (test_bit(HCI_INIT, &hdev->flags))
  570. hdev->page_scan_type = rp->type;
  571. }
  572. static void hci_cc_write_page_scan_type(struct hci_dev *hdev,
  573. struct sk_buff *skb)
  574. {
  575. u8 status = *((u8 *) skb->data);
  576. u8 *type;
  577. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  578. if (status)
  579. return;
  580. type = hci_sent_cmd_data(hdev, HCI_OP_WRITE_PAGE_SCAN_TYPE);
  581. if (type)
  582. hdev->page_scan_type = *type;
  583. }
  584. static void hci_cc_read_data_block_size(struct hci_dev *hdev,
  585. struct sk_buff *skb)
  586. {
  587. struct hci_rp_read_data_block_size *rp = (void *) skb->data;
  588. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  589. if (rp->status)
  590. return;
  591. hdev->block_mtu = __le16_to_cpu(rp->max_acl_len);
  592. hdev->block_len = __le16_to_cpu(rp->block_len);
  593. hdev->num_blocks = __le16_to_cpu(rp->num_blocks);
  594. hdev->block_cnt = hdev->num_blocks;
  595. BT_DBG("%s blk mtu %d cnt %d len %d", hdev->name, hdev->block_mtu,
  596. hdev->block_cnt, hdev->block_len);
  597. }
  598. static void hci_cc_read_clock(struct hci_dev *hdev, struct sk_buff *skb)
  599. {
  600. struct hci_rp_read_clock *rp = (void *) skb->data;
  601. struct hci_cp_read_clock *cp;
  602. struct hci_conn *conn;
  603. BT_DBG("%s", hdev->name);
  604. if (skb->len < sizeof(*rp))
  605. return;
  606. if (rp->status)
  607. return;
  608. hci_dev_lock(hdev);
  609. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_CLOCK);
  610. if (!cp)
  611. goto unlock;
  612. if (cp->which == 0x00) {
  613. hdev->clock = le32_to_cpu(rp->clock);
  614. goto unlock;
  615. }
  616. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  617. if (conn) {
  618. conn->clock = le32_to_cpu(rp->clock);
  619. conn->clock_accuracy = le16_to_cpu(rp->accuracy);
  620. }
  621. unlock:
  622. hci_dev_unlock(hdev);
  623. }
  624. static void hci_cc_read_local_amp_info(struct hci_dev *hdev,
  625. struct sk_buff *skb)
  626. {
  627. struct hci_rp_read_local_amp_info *rp = (void *) skb->data;
  628. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  629. if (rp->status)
  630. return;
  631. hdev->amp_status = rp->amp_status;
  632. hdev->amp_total_bw = __le32_to_cpu(rp->total_bw);
  633. hdev->amp_max_bw = __le32_to_cpu(rp->max_bw);
  634. hdev->amp_min_latency = __le32_to_cpu(rp->min_latency);
  635. hdev->amp_max_pdu = __le32_to_cpu(rp->max_pdu);
  636. hdev->amp_type = rp->amp_type;
  637. hdev->amp_pal_cap = __le16_to_cpu(rp->pal_cap);
  638. hdev->amp_assoc_size = __le16_to_cpu(rp->max_assoc_size);
  639. hdev->amp_be_flush_to = __le32_to_cpu(rp->be_flush_to);
  640. hdev->amp_max_flush_to = __le32_to_cpu(rp->max_flush_to);
  641. }
  642. static void hci_cc_read_inq_rsp_tx_power(struct hci_dev *hdev,
  643. struct sk_buff *skb)
  644. {
  645. struct hci_rp_read_inq_rsp_tx_power *rp = (void *) skb->data;
  646. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  647. if (rp->status)
  648. return;
  649. hdev->inq_tx_power = rp->tx_power;
  650. }
  651. static void hci_cc_read_def_err_data_reporting(struct hci_dev *hdev,
  652. struct sk_buff *skb)
  653. {
  654. struct hci_rp_read_def_err_data_reporting *rp = (void *)skb->data;
  655. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  656. if (rp->status)
  657. return;
  658. hdev->err_data_reporting = rp->err_data_reporting;
  659. }
  660. static void hci_cc_write_def_err_data_reporting(struct hci_dev *hdev,
  661. struct sk_buff *skb)
  662. {
  663. __u8 status = *((__u8 *)skb->data);
  664. struct hci_cp_write_def_err_data_reporting *cp;
  665. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  666. if (status)
  667. return;
  668. cp = hci_sent_cmd_data(hdev, HCI_OP_WRITE_DEF_ERR_DATA_REPORTING);
  669. if (!cp)
  670. return;
  671. hdev->err_data_reporting = cp->err_data_reporting;
  672. }
  673. static void hci_cc_pin_code_reply(struct hci_dev *hdev, struct sk_buff *skb)
  674. {
  675. struct hci_rp_pin_code_reply *rp = (void *) skb->data;
  676. struct hci_cp_pin_code_reply *cp;
  677. struct hci_conn *conn;
  678. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  679. hci_dev_lock(hdev);
  680. if (hci_dev_test_flag(hdev, HCI_MGMT))
  681. mgmt_pin_code_reply_complete(hdev, &rp->bdaddr, rp->status);
  682. if (rp->status)
  683. goto unlock;
  684. cp = hci_sent_cmd_data(hdev, HCI_OP_PIN_CODE_REPLY);
  685. if (!cp)
  686. goto unlock;
  687. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  688. if (conn)
  689. conn->pin_length = cp->pin_len;
  690. unlock:
  691. hci_dev_unlock(hdev);
  692. }
  693. static void hci_cc_pin_code_neg_reply(struct hci_dev *hdev, struct sk_buff *skb)
  694. {
  695. struct hci_rp_pin_code_neg_reply *rp = (void *) skb->data;
  696. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  697. hci_dev_lock(hdev);
  698. if (hci_dev_test_flag(hdev, HCI_MGMT))
  699. mgmt_pin_code_neg_reply_complete(hdev, &rp->bdaddr,
  700. rp->status);
  701. hci_dev_unlock(hdev);
  702. }
  703. static void hci_cc_le_read_buffer_size(struct hci_dev *hdev,
  704. struct sk_buff *skb)
  705. {
  706. struct hci_rp_le_read_buffer_size *rp = (void *) skb->data;
  707. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  708. if (rp->status)
  709. return;
  710. hdev->le_mtu = __le16_to_cpu(rp->le_mtu);
  711. hdev->le_pkts = rp->le_max_pkt;
  712. hdev->le_cnt = hdev->le_pkts;
  713. BT_DBG("%s le mtu %d:%d", hdev->name, hdev->le_mtu, hdev->le_pkts);
  714. }
  715. static void hci_cc_le_read_local_features(struct hci_dev *hdev,
  716. struct sk_buff *skb)
  717. {
  718. struct hci_rp_le_read_local_features *rp = (void *) skb->data;
  719. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  720. if (rp->status)
  721. return;
  722. memcpy(hdev->le_features, rp->features, 8);
  723. }
  724. static void hci_cc_le_read_adv_tx_power(struct hci_dev *hdev,
  725. struct sk_buff *skb)
  726. {
  727. struct hci_rp_le_read_adv_tx_power *rp = (void *) skb->data;
  728. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  729. if (rp->status)
  730. return;
  731. hdev->adv_tx_power = rp->tx_power;
  732. }
  733. static void hci_cc_user_confirm_reply(struct hci_dev *hdev, struct sk_buff *skb)
  734. {
  735. struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
  736. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  737. hci_dev_lock(hdev);
  738. if (hci_dev_test_flag(hdev, HCI_MGMT))
  739. mgmt_user_confirm_reply_complete(hdev, &rp->bdaddr, ACL_LINK, 0,
  740. rp->status);
  741. hci_dev_unlock(hdev);
  742. }
  743. static void hci_cc_user_confirm_neg_reply(struct hci_dev *hdev,
  744. struct sk_buff *skb)
  745. {
  746. struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
  747. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  748. hci_dev_lock(hdev);
  749. if (hci_dev_test_flag(hdev, HCI_MGMT))
  750. mgmt_user_confirm_neg_reply_complete(hdev, &rp->bdaddr,
  751. ACL_LINK, 0, rp->status);
  752. hci_dev_unlock(hdev);
  753. }
  754. static void hci_cc_user_passkey_reply(struct hci_dev *hdev, struct sk_buff *skb)
  755. {
  756. struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
  757. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  758. hci_dev_lock(hdev);
  759. if (hci_dev_test_flag(hdev, HCI_MGMT))
  760. mgmt_user_passkey_reply_complete(hdev, &rp->bdaddr, ACL_LINK,
  761. 0, rp->status);
  762. hci_dev_unlock(hdev);
  763. }
  764. static void hci_cc_user_passkey_neg_reply(struct hci_dev *hdev,
  765. struct sk_buff *skb)
  766. {
  767. struct hci_rp_user_confirm_reply *rp = (void *) skb->data;
  768. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  769. hci_dev_lock(hdev);
  770. if (hci_dev_test_flag(hdev, HCI_MGMT))
  771. mgmt_user_passkey_neg_reply_complete(hdev, &rp->bdaddr,
  772. ACL_LINK, 0, rp->status);
  773. hci_dev_unlock(hdev);
  774. }
  775. static void hci_cc_read_local_oob_data(struct hci_dev *hdev,
  776. struct sk_buff *skb)
  777. {
  778. struct hci_rp_read_local_oob_data *rp = (void *) skb->data;
  779. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  780. }
  781. static void hci_cc_read_local_oob_ext_data(struct hci_dev *hdev,
  782. struct sk_buff *skb)
  783. {
  784. struct hci_rp_read_local_oob_ext_data *rp = (void *) skb->data;
  785. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  786. }
  787. static void hci_cc_le_set_random_addr(struct hci_dev *hdev, struct sk_buff *skb)
  788. {
  789. __u8 status = *((__u8 *) skb->data);
  790. bdaddr_t *sent;
  791. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  792. if (status)
  793. return;
  794. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_RANDOM_ADDR);
  795. if (!sent)
  796. return;
  797. hci_dev_lock(hdev);
  798. bacpy(&hdev->random_addr, sent);
  799. hci_dev_unlock(hdev);
  800. }
  801. static void hci_cc_le_set_default_phy(struct hci_dev *hdev, struct sk_buff *skb)
  802. {
  803. __u8 status = *((__u8 *) skb->data);
  804. struct hci_cp_le_set_default_phy *cp;
  805. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  806. if (status)
  807. return;
  808. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_DEFAULT_PHY);
  809. if (!cp)
  810. return;
  811. hci_dev_lock(hdev);
  812. hdev->le_tx_def_phys = cp->tx_phys;
  813. hdev->le_rx_def_phys = cp->rx_phys;
  814. hci_dev_unlock(hdev);
  815. }
  816. static void hci_cc_le_set_adv_set_random_addr(struct hci_dev *hdev,
  817. struct sk_buff *skb)
  818. {
  819. __u8 status = *((__u8 *) skb->data);
  820. struct hci_cp_le_set_adv_set_rand_addr *cp;
  821. struct adv_info *adv_instance;
  822. if (status)
  823. return;
  824. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_SET_RAND_ADDR);
  825. if (!cp)
  826. return;
  827. hci_dev_lock(hdev);
  828. if (!hdev->cur_adv_instance) {
  829. /* Store in hdev for instance 0 (Set adv and Directed advs) */
  830. bacpy(&hdev->random_addr, &cp->bdaddr);
  831. } else {
  832. adv_instance = hci_find_adv_instance(hdev,
  833. hdev->cur_adv_instance);
  834. if (adv_instance)
  835. bacpy(&adv_instance->random_addr, &cp->bdaddr);
  836. }
  837. hci_dev_unlock(hdev);
  838. }
  839. static void hci_cc_le_set_adv_enable(struct hci_dev *hdev, struct sk_buff *skb)
  840. {
  841. __u8 *sent, status = *((__u8 *) skb->data);
  842. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  843. if (status)
  844. return;
  845. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_ENABLE);
  846. if (!sent)
  847. return;
  848. hci_dev_lock(hdev);
  849. /* If we're doing connection initiation as peripheral. Set a
  850. * timeout in case something goes wrong.
  851. */
  852. if (*sent) {
  853. struct hci_conn *conn;
  854. hci_dev_set_flag(hdev, HCI_LE_ADV);
  855. conn = hci_lookup_le_connect(hdev);
  856. if (conn)
  857. queue_delayed_work(hdev->workqueue,
  858. &conn->le_conn_timeout,
  859. conn->conn_timeout);
  860. } else {
  861. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  862. }
  863. hci_dev_unlock(hdev);
  864. }
  865. static void hci_cc_le_set_ext_adv_enable(struct hci_dev *hdev,
  866. struct sk_buff *skb)
  867. {
  868. struct hci_cp_le_set_ext_adv_enable *cp;
  869. __u8 status = *((__u8 *) skb->data);
  870. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  871. if (status)
  872. return;
  873. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_ENABLE);
  874. if (!cp)
  875. return;
  876. hci_dev_lock(hdev);
  877. if (cp->enable) {
  878. struct hci_conn *conn;
  879. hci_dev_set_flag(hdev, HCI_LE_ADV);
  880. conn = hci_lookup_le_connect(hdev);
  881. if (conn)
  882. queue_delayed_work(hdev->workqueue,
  883. &conn->le_conn_timeout,
  884. conn->conn_timeout);
  885. } else {
  886. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  887. }
  888. hci_dev_unlock(hdev);
  889. }
  890. static void hci_cc_le_set_scan_param(struct hci_dev *hdev, struct sk_buff *skb)
  891. {
  892. struct hci_cp_le_set_scan_param *cp;
  893. __u8 status = *((__u8 *) skb->data);
  894. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  895. if (status)
  896. return;
  897. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_PARAM);
  898. if (!cp)
  899. return;
  900. hci_dev_lock(hdev);
  901. hdev->le_scan_type = cp->type;
  902. hci_dev_unlock(hdev);
  903. }
  904. static void hci_cc_le_set_ext_scan_param(struct hci_dev *hdev,
  905. struct sk_buff *skb)
  906. {
  907. struct hci_cp_le_set_ext_scan_params *cp;
  908. __u8 status = *((__u8 *) skb->data);
  909. struct hci_cp_le_scan_phy_params *phy_param;
  910. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  911. if (status)
  912. return;
  913. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_PARAMS);
  914. if (!cp)
  915. return;
  916. phy_param = (void *)cp->data;
  917. hci_dev_lock(hdev);
  918. hdev->le_scan_type = phy_param->type;
  919. hci_dev_unlock(hdev);
  920. }
  921. static bool has_pending_adv_report(struct hci_dev *hdev)
  922. {
  923. struct discovery_state *d = &hdev->discovery;
  924. return bacmp(&d->last_adv_addr, BDADDR_ANY);
  925. }
  926. static void clear_pending_adv_report(struct hci_dev *hdev)
  927. {
  928. struct discovery_state *d = &hdev->discovery;
  929. bacpy(&d->last_adv_addr, BDADDR_ANY);
  930. d->last_adv_data_len = 0;
  931. }
  932. static void store_pending_adv_report(struct hci_dev *hdev, bdaddr_t *bdaddr,
  933. u8 bdaddr_type, s8 rssi, u32 flags,
  934. u8 *data, u8 len)
  935. {
  936. struct discovery_state *d = &hdev->discovery;
  937. bacpy(&d->last_adv_addr, bdaddr);
  938. d->last_adv_addr_type = bdaddr_type;
  939. d->last_adv_rssi = rssi;
  940. d->last_adv_flags = flags;
  941. memcpy(d->last_adv_data, data, len);
  942. d->last_adv_data_len = len;
  943. }
  944. static void le_set_scan_enable_complete(struct hci_dev *hdev, u8 enable)
  945. {
  946. hci_dev_lock(hdev);
  947. switch (enable) {
  948. case LE_SCAN_ENABLE:
  949. hci_dev_set_flag(hdev, HCI_LE_SCAN);
  950. if (hdev->le_scan_type == LE_SCAN_ACTIVE)
  951. clear_pending_adv_report(hdev);
  952. break;
  953. case LE_SCAN_DISABLE:
  954. /* We do this here instead of when setting DISCOVERY_STOPPED
  955. * since the latter would potentially require waiting for
  956. * inquiry to stop too.
  957. */
  958. if (has_pending_adv_report(hdev)) {
  959. struct discovery_state *d = &hdev->discovery;
  960. mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
  961. d->last_adv_addr_type, NULL,
  962. d->last_adv_rssi, d->last_adv_flags,
  963. d->last_adv_data,
  964. d->last_adv_data_len, NULL, 0);
  965. }
  966. /* Cancel this timer so that we don't try to disable scanning
  967. * when it's already disabled.
  968. */
  969. cancel_delayed_work(&hdev->le_scan_disable);
  970. hci_dev_clear_flag(hdev, HCI_LE_SCAN);
  971. /* The HCI_LE_SCAN_INTERRUPTED flag indicates that we
  972. * interrupted scanning due to a connect request. Mark
  973. * therefore discovery as stopped. If this was not
  974. * because of a connect request advertising might have
  975. * been disabled because of active scanning, so
  976. * re-enable it again if necessary.
  977. */
  978. if (hci_dev_test_and_clear_flag(hdev, HCI_LE_SCAN_INTERRUPTED))
  979. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  980. else if (!hci_dev_test_flag(hdev, HCI_LE_ADV) &&
  981. hdev->discovery.state == DISCOVERY_FINDING)
  982. hci_req_reenable_advertising(hdev);
  983. break;
  984. default:
  985. bt_dev_err(hdev, "use of reserved LE_Scan_Enable param %d",
  986. enable);
  987. break;
  988. }
  989. hci_dev_unlock(hdev);
  990. }
  991. static void hci_cc_le_set_scan_enable(struct hci_dev *hdev,
  992. struct sk_buff *skb)
  993. {
  994. struct hci_cp_le_set_scan_enable *cp;
  995. __u8 status = *((__u8 *) skb->data);
  996. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  997. if (status)
  998. return;
  999. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_SCAN_ENABLE);
  1000. if (!cp)
  1001. return;
  1002. le_set_scan_enable_complete(hdev, cp->enable);
  1003. }
  1004. static void hci_cc_le_set_ext_scan_enable(struct hci_dev *hdev,
  1005. struct sk_buff *skb)
  1006. {
  1007. struct hci_cp_le_set_ext_scan_enable *cp;
  1008. __u8 status = *((__u8 *) skb->data);
  1009. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1010. if (status)
  1011. return;
  1012. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_SCAN_ENABLE);
  1013. if (!cp)
  1014. return;
  1015. le_set_scan_enable_complete(hdev, cp->enable);
  1016. }
  1017. static void hci_cc_le_read_num_adv_sets(struct hci_dev *hdev,
  1018. struct sk_buff *skb)
  1019. {
  1020. struct hci_rp_le_read_num_supported_adv_sets *rp = (void *) skb->data;
  1021. BT_DBG("%s status 0x%2.2x No of Adv sets %u", hdev->name, rp->status,
  1022. rp->num_of_sets);
  1023. if (rp->status)
  1024. return;
  1025. hdev->le_num_of_adv_sets = rp->num_of_sets;
  1026. }
  1027. static void hci_cc_le_read_white_list_size(struct hci_dev *hdev,
  1028. struct sk_buff *skb)
  1029. {
  1030. struct hci_rp_le_read_white_list_size *rp = (void *) skb->data;
  1031. BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
  1032. if (rp->status)
  1033. return;
  1034. hdev->le_white_list_size = rp->size;
  1035. }
  1036. static void hci_cc_le_clear_white_list(struct hci_dev *hdev,
  1037. struct sk_buff *skb)
  1038. {
  1039. __u8 status = *((__u8 *) skb->data);
  1040. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1041. if (status)
  1042. return;
  1043. hci_bdaddr_list_clear(&hdev->le_white_list);
  1044. }
  1045. static void hci_cc_le_add_to_white_list(struct hci_dev *hdev,
  1046. struct sk_buff *skb)
  1047. {
  1048. struct hci_cp_le_add_to_white_list *sent;
  1049. __u8 status = *((__u8 *) skb->data);
  1050. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1051. if (status)
  1052. return;
  1053. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_WHITE_LIST);
  1054. if (!sent)
  1055. return;
  1056. hci_bdaddr_list_add(&hdev->le_white_list, &sent->bdaddr,
  1057. sent->bdaddr_type);
  1058. }
  1059. static void hci_cc_le_del_from_white_list(struct hci_dev *hdev,
  1060. struct sk_buff *skb)
  1061. {
  1062. struct hci_cp_le_del_from_white_list *sent;
  1063. __u8 status = *((__u8 *) skb->data);
  1064. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1065. if (status)
  1066. return;
  1067. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_WHITE_LIST);
  1068. if (!sent)
  1069. return;
  1070. hci_bdaddr_list_del(&hdev->le_white_list, &sent->bdaddr,
  1071. sent->bdaddr_type);
  1072. }
  1073. static void hci_cc_le_read_supported_states(struct hci_dev *hdev,
  1074. struct sk_buff *skb)
  1075. {
  1076. struct hci_rp_le_read_supported_states *rp = (void *) skb->data;
  1077. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1078. if (rp->status)
  1079. return;
  1080. memcpy(hdev->le_states, rp->le_states, 8);
  1081. }
  1082. static void hci_cc_le_read_def_data_len(struct hci_dev *hdev,
  1083. struct sk_buff *skb)
  1084. {
  1085. struct hci_rp_le_read_def_data_len *rp = (void *) skb->data;
  1086. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1087. if (rp->status)
  1088. return;
  1089. hdev->le_def_tx_len = le16_to_cpu(rp->tx_len);
  1090. hdev->le_def_tx_time = le16_to_cpu(rp->tx_time);
  1091. }
  1092. static void hci_cc_le_write_def_data_len(struct hci_dev *hdev,
  1093. struct sk_buff *skb)
  1094. {
  1095. struct hci_cp_le_write_def_data_len *sent;
  1096. __u8 status = *((__u8 *) skb->data);
  1097. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1098. if (status)
  1099. return;
  1100. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_WRITE_DEF_DATA_LEN);
  1101. if (!sent)
  1102. return;
  1103. hdev->le_def_tx_len = le16_to_cpu(sent->tx_len);
  1104. hdev->le_def_tx_time = le16_to_cpu(sent->tx_time);
  1105. }
  1106. static void hci_cc_le_add_to_resolv_list(struct hci_dev *hdev,
  1107. struct sk_buff *skb)
  1108. {
  1109. struct hci_cp_le_add_to_resolv_list *sent;
  1110. __u8 status = *((__u8 *) skb->data);
  1111. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1112. if (status)
  1113. return;
  1114. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_ADD_TO_RESOLV_LIST);
  1115. if (!sent)
  1116. return;
  1117. hci_bdaddr_list_add_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
  1118. sent->bdaddr_type, sent->peer_irk,
  1119. sent->local_irk);
  1120. }
  1121. static void hci_cc_le_del_from_resolv_list(struct hci_dev *hdev,
  1122. struct sk_buff *skb)
  1123. {
  1124. struct hci_cp_le_del_from_resolv_list *sent;
  1125. __u8 status = *((__u8 *) skb->data);
  1126. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1127. if (status)
  1128. return;
  1129. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_DEL_FROM_RESOLV_LIST);
  1130. if (!sent)
  1131. return;
  1132. hci_bdaddr_list_del_with_irk(&hdev->le_resolv_list, &sent->bdaddr,
  1133. sent->bdaddr_type);
  1134. }
  1135. static void hci_cc_le_clear_resolv_list(struct hci_dev *hdev,
  1136. struct sk_buff *skb)
  1137. {
  1138. __u8 status = *((__u8 *) skb->data);
  1139. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1140. if (status)
  1141. return;
  1142. hci_bdaddr_list_clear(&hdev->le_resolv_list);
  1143. }
  1144. static void hci_cc_le_read_resolv_list_size(struct hci_dev *hdev,
  1145. struct sk_buff *skb)
  1146. {
  1147. struct hci_rp_le_read_resolv_list_size *rp = (void *) skb->data;
  1148. BT_DBG("%s status 0x%2.2x size %u", hdev->name, rp->status, rp->size);
  1149. if (rp->status)
  1150. return;
  1151. hdev->le_resolv_list_size = rp->size;
  1152. }
  1153. static void hci_cc_le_set_addr_resolution_enable(struct hci_dev *hdev,
  1154. struct sk_buff *skb)
  1155. {
  1156. __u8 *sent, status = *((__u8 *) skb->data);
  1157. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1158. if (status)
  1159. return;
  1160. sent = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADDR_RESOLV_ENABLE);
  1161. if (!sent)
  1162. return;
  1163. hci_dev_lock(hdev);
  1164. if (*sent)
  1165. hci_dev_set_flag(hdev, HCI_LL_RPA_RESOLUTION);
  1166. else
  1167. hci_dev_clear_flag(hdev, HCI_LL_RPA_RESOLUTION);
  1168. hci_dev_unlock(hdev);
  1169. }
  1170. static void hci_cc_le_read_max_data_len(struct hci_dev *hdev,
  1171. struct sk_buff *skb)
  1172. {
  1173. struct hci_rp_le_read_max_data_len *rp = (void *) skb->data;
  1174. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1175. if (rp->status)
  1176. return;
  1177. hdev->le_max_tx_len = le16_to_cpu(rp->tx_len);
  1178. hdev->le_max_tx_time = le16_to_cpu(rp->tx_time);
  1179. hdev->le_max_rx_len = le16_to_cpu(rp->rx_len);
  1180. hdev->le_max_rx_time = le16_to_cpu(rp->rx_time);
  1181. }
  1182. static void hci_cc_write_le_host_supported(struct hci_dev *hdev,
  1183. struct sk_buff *skb)
  1184. {
  1185. struct hci_cp_write_le_host_supported *sent;
  1186. __u8 status = *((__u8 *) skb->data);
  1187. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1188. if (status)
  1189. return;
  1190. sent = hci_sent_cmd_data(hdev, HCI_OP_WRITE_LE_HOST_SUPPORTED);
  1191. if (!sent)
  1192. return;
  1193. hci_dev_lock(hdev);
  1194. if (sent->le) {
  1195. hdev->features[1][0] |= LMP_HOST_LE;
  1196. hci_dev_set_flag(hdev, HCI_LE_ENABLED);
  1197. } else {
  1198. hdev->features[1][0] &= ~LMP_HOST_LE;
  1199. hci_dev_clear_flag(hdev, HCI_LE_ENABLED);
  1200. hci_dev_clear_flag(hdev, HCI_ADVERTISING);
  1201. }
  1202. if (sent->simul)
  1203. hdev->features[1][0] |= LMP_HOST_LE_BREDR;
  1204. else
  1205. hdev->features[1][0] &= ~LMP_HOST_LE_BREDR;
  1206. hci_dev_unlock(hdev);
  1207. }
  1208. static void hci_cc_set_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
  1209. {
  1210. struct hci_cp_le_set_adv_param *cp;
  1211. u8 status = *((u8 *) skb->data);
  1212. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1213. if (status)
  1214. return;
  1215. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_ADV_PARAM);
  1216. if (!cp)
  1217. return;
  1218. hci_dev_lock(hdev);
  1219. hdev->adv_addr_type = cp->own_address_type;
  1220. hci_dev_unlock(hdev);
  1221. }
  1222. static void hci_cc_set_ext_adv_param(struct hci_dev *hdev, struct sk_buff *skb)
  1223. {
  1224. struct hci_rp_le_set_ext_adv_params *rp = (void *) skb->data;
  1225. struct hci_cp_le_set_ext_adv_params *cp;
  1226. struct adv_info *adv_instance;
  1227. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1228. if (rp->status)
  1229. return;
  1230. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_SET_EXT_ADV_PARAMS);
  1231. if (!cp)
  1232. return;
  1233. hci_dev_lock(hdev);
  1234. hdev->adv_addr_type = cp->own_addr_type;
  1235. if (!hdev->cur_adv_instance) {
  1236. /* Store in hdev for instance 0 */
  1237. hdev->adv_tx_power = rp->tx_power;
  1238. } else {
  1239. adv_instance = hci_find_adv_instance(hdev,
  1240. hdev->cur_adv_instance);
  1241. if (adv_instance)
  1242. adv_instance->tx_power = rp->tx_power;
  1243. }
  1244. /* Update adv data as tx power is known now */
  1245. hci_req_update_adv_data(hdev, hdev->cur_adv_instance);
  1246. hci_dev_unlock(hdev);
  1247. }
  1248. static void hci_cc_read_rssi(struct hci_dev *hdev, struct sk_buff *skb)
  1249. {
  1250. struct hci_rp_read_rssi *rp = (void *) skb->data;
  1251. struct hci_conn *conn;
  1252. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1253. if (rp->status)
  1254. return;
  1255. hci_dev_lock(hdev);
  1256. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  1257. if (conn)
  1258. conn->rssi = rp->rssi;
  1259. hci_dev_unlock(hdev);
  1260. }
  1261. static void hci_cc_read_tx_power(struct hci_dev *hdev, struct sk_buff *skb)
  1262. {
  1263. struct hci_cp_read_tx_power *sent;
  1264. struct hci_rp_read_tx_power *rp = (void *) skb->data;
  1265. struct hci_conn *conn;
  1266. BT_DBG("%s status 0x%2.2x", hdev->name, rp->status);
  1267. if (rp->status)
  1268. return;
  1269. sent = hci_sent_cmd_data(hdev, HCI_OP_READ_TX_POWER);
  1270. if (!sent)
  1271. return;
  1272. hci_dev_lock(hdev);
  1273. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(rp->handle));
  1274. if (!conn)
  1275. goto unlock;
  1276. switch (sent->type) {
  1277. case 0x00:
  1278. conn->tx_power = rp->tx_power;
  1279. break;
  1280. case 0x01:
  1281. conn->max_tx_power = rp->tx_power;
  1282. break;
  1283. }
  1284. unlock:
  1285. hci_dev_unlock(hdev);
  1286. }
  1287. static void hci_cc_write_ssp_debug_mode(struct hci_dev *hdev, struct sk_buff *skb)
  1288. {
  1289. u8 status = *((u8 *) skb->data);
  1290. u8 *mode;
  1291. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1292. if (status)
  1293. return;
  1294. mode = hci_sent_cmd_data(hdev, HCI_OP_WRITE_SSP_DEBUG_MODE);
  1295. if (mode)
  1296. hdev->ssp_debug_mode = *mode;
  1297. }
  1298. static void hci_cs_inquiry(struct hci_dev *hdev, __u8 status)
  1299. {
  1300. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1301. if (status) {
  1302. hci_conn_check_pending(hdev);
  1303. return;
  1304. }
  1305. set_bit(HCI_INQUIRY, &hdev->flags);
  1306. }
  1307. static void hci_cs_create_conn(struct hci_dev *hdev, __u8 status)
  1308. {
  1309. struct hci_cp_create_conn *cp;
  1310. struct hci_conn *conn;
  1311. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1312. cp = hci_sent_cmd_data(hdev, HCI_OP_CREATE_CONN);
  1313. if (!cp)
  1314. return;
  1315. hci_dev_lock(hdev);
  1316. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  1317. BT_DBG("%s bdaddr %pMR hcon %p", hdev->name, &cp->bdaddr, conn);
  1318. if (status) {
  1319. if (conn && conn->state == BT_CONNECT) {
  1320. if (status != 0x0c || conn->attempt > 2) {
  1321. conn->state = BT_CLOSED;
  1322. hci_connect_cfm(conn, status);
  1323. hci_conn_del(conn);
  1324. } else
  1325. conn->state = BT_CONNECT2;
  1326. }
  1327. } else {
  1328. if (!conn) {
  1329. conn = hci_conn_add(hdev, ACL_LINK, &cp->bdaddr,
  1330. HCI_ROLE_MASTER);
  1331. if (!conn)
  1332. bt_dev_err(hdev, "no memory for new connection");
  1333. }
  1334. }
  1335. hci_dev_unlock(hdev);
  1336. }
  1337. static void hci_cs_add_sco(struct hci_dev *hdev, __u8 status)
  1338. {
  1339. struct hci_cp_add_sco *cp;
  1340. struct hci_conn *acl, *sco;
  1341. __u16 handle;
  1342. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1343. if (!status)
  1344. return;
  1345. cp = hci_sent_cmd_data(hdev, HCI_OP_ADD_SCO);
  1346. if (!cp)
  1347. return;
  1348. handle = __le16_to_cpu(cp->handle);
  1349. BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
  1350. hci_dev_lock(hdev);
  1351. acl = hci_conn_hash_lookup_handle(hdev, handle);
  1352. if (acl) {
  1353. sco = acl->link;
  1354. if (sco) {
  1355. sco->state = BT_CLOSED;
  1356. hci_connect_cfm(sco, status);
  1357. hci_conn_del(sco);
  1358. }
  1359. }
  1360. hci_dev_unlock(hdev);
  1361. }
  1362. static void hci_cs_auth_requested(struct hci_dev *hdev, __u8 status)
  1363. {
  1364. struct hci_cp_auth_requested *cp;
  1365. struct hci_conn *conn;
  1366. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1367. if (!status)
  1368. return;
  1369. cp = hci_sent_cmd_data(hdev, HCI_OP_AUTH_REQUESTED);
  1370. if (!cp)
  1371. return;
  1372. hci_dev_lock(hdev);
  1373. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1374. if (conn) {
  1375. if (conn->state == BT_CONFIG) {
  1376. hci_connect_cfm(conn, status);
  1377. hci_conn_drop(conn);
  1378. }
  1379. }
  1380. hci_dev_unlock(hdev);
  1381. }
  1382. static void hci_cs_set_conn_encrypt(struct hci_dev *hdev, __u8 status)
  1383. {
  1384. struct hci_cp_set_conn_encrypt *cp;
  1385. struct hci_conn *conn;
  1386. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1387. if (!status)
  1388. return;
  1389. cp = hci_sent_cmd_data(hdev, HCI_OP_SET_CONN_ENCRYPT);
  1390. if (!cp)
  1391. return;
  1392. hci_dev_lock(hdev);
  1393. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1394. if (conn) {
  1395. if (conn->state == BT_CONFIG) {
  1396. hci_connect_cfm(conn, status);
  1397. hci_conn_drop(conn);
  1398. }
  1399. }
  1400. hci_dev_unlock(hdev);
  1401. }
  1402. static int hci_outgoing_auth_needed(struct hci_dev *hdev,
  1403. struct hci_conn *conn)
  1404. {
  1405. if (conn->state != BT_CONFIG || !conn->out)
  1406. return 0;
  1407. if (conn->pending_sec_level == BT_SECURITY_SDP)
  1408. return 0;
  1409. /* Only request authentication for SSP connections or non-SSP
  1410. * devices with sec_level MEDIUM or HIGH or if MITM protection
  1411. * is requested.
  1412. */
  1413. if (!hci_conn_ssp_enabled(conn) && !(conn->auth_type & 0x01) &&
  1414. conn->pending_sec_level != BT_SECURITY_FIPS &&
  1415. conn->pending_sec_level != BT_SECURITY_HIGH &&
  1416. conn->pending_sec_level != BT_SECURITY_MEDIUM)
  1417. return 0;
  1418. return 1;
  1419. }
  1420. static int hci_resolve_name(struct hci_dev *hdev,
  1421. struct inquiry_entry *e)
  1422. {
  1423. struct hci_cp_remote_name_req cp;
  1424. memset(&cp, 0, sizeof(cp));
  1425. bacpy(&cp.bdaddr, &e->data.bdaddr);
  1426. cp.pscan_rep_mode = e->data.pscan_rep_mode;
  1427. cp.pscan_mode = e->data.pscan_mode;
  1428. cp.clock_offset = e->data.clock_offset;
  1429. return hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
  1430. }
  1431. static bool hci_resolve_next_name(struct hci_dev *hdev)
  1432. {
  1433. struct discovery_state *discov = &hdev->discovery;
  1434. struct inquiry_entry *e;
  1435. if (list_empty(&discov->resolve))
  1436. return false;
  1437. e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
  1438. if (!e)
  1439. return false;
  1440. if (hci_resolve_name(hdev, e) == 0) {
  1441. e->name_state = NAME_PENDING;
  1442. return true;
  1443. }
  1444. return false;
  1445. }
  1446. static void hci_check_pending_name(struct hci_dev *hdev, struct hci_conn *conn,
  1447. bdaddr_t *bdaddr, u8 *name, u8 name_len)
  1448. {
  1449. struct discovery_state *discov = &hdev->discovery;
  1450. struct inquiry_entry *e;
  1451. /* Update the mgmt connected state if necessary. Be careful with
  1452. * conn objects that exist but are not (yet) connected however.
  1453. * Only those in BT_CONFIG or BT_CONNECTED states can be
  1454. * considered connected.
  1455. */
  1456. if (conn &&
  1457. (conn->state == BT_CONFIG || conn->state == BT_CONNECTED) &&
  1458. !test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
  1459. mgmt_device_connected(hdev, conn, 0, name, name_len);
  1460. if (discov->state == DISCOVERY_STOPPED)
  1461. return;
  1462. if (discov->state == DISCOVERY_STOPPING)
  1463. goto discov_complete;
  1464. if (discov->state != DISCOVERY_RESOLVING)
  1465. return;
  1466. e = hci_inquiry_cache_lookup_resolve(hdev, bdaddr, NAME_PENDING);
  1467. /* If the device was not found in a list of found devices names of which
  1468. * are pending. there is no need to continue resolving a next name as it
  1469. * will be done upon receiving another Remote Name Request Complete
  1470. * Event */
  1471. if (!e)
  1472. return;
  1473. list_del(&e->list);
  1474. if (name) {
  1475. e->name_state = NAME_KNOWN;
  1476. mgmt_remote_name(hdev, bdaddr, ACL_LINK, 0x00,
  1477. e->data.rssi, name, name_len);
  1478. } else {
  1479. e->name_state = NAME_NOT_KNOWN;
  1480. }
  1481. if (hci_resolve_next_name(hdev))
  1482. return;
  1483. discov_complete:
  1484. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1485. }
  1486. static void hci_cs_remote_name_req(struct hci_dev *hdev, __u8 status)
  1487. {
  1488. struct hci_cp_remote_name_req *cp;
  1489. struct hci_conn *conn;
  1490. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1491. /* If successful wait for the name req complete event before
  1492. * checking for the need to do authentication */
  1493. if (!status)
  1494. return;
  1495. cp = hci_sent_cmd_data(hdev, HCI_OP_REMOTE_NAME_REQ);
  1496. if (!cp)
  1497. return;
  1498. hci_dev_lock(hdev);
  1499. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  1500. if (hci_dev_test_flag(hdev, HCI_MGMT))
  1501. hci_check_pending_name(hdev, conn, &cp->bdaddr, NULL, 0);
  1502. if (!conn)
  1503. goto unlock;
  1504. if (!hci_outgoing_auth_needed(hdev, conn))
  1505. goto unlock;
  1506. if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
  1507. struct hci_cp_auth_requested auth_cp;
  1508. set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
  1509. auth_cp.handle = __cpu_to_le16(conn->handle);
  1510. hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED,
  1511. sizeof(auth_cp), &auth_cp);
  1512. }
  1513. unlock:
  1514. hci_dev_unlock(hdev);
  1515. }
  1516. static void hci_cs_read_remote_features(struct hci_dev *hdev, __u8 status)
  1517. {
  1518. struct hci_cp_read_remote_features *cp;
  1519. struct hci_conn *conn;
  1520. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1521. if (!status)
  1522. return;
  1523. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_FEATURES);
  1524. if (!cp)
  1525. return;
  1526. hci_dev_lock(hdev);
  1527. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1528. if (conn) {
  1529. if (conn->state == BT_CONFIG) {
  1530. hci_connect_cfm(conn, status);
  1531. hci_conn_drop(conn);
  1532. }
  1533. }
  1534. hci_dev_unlock(hdev);
  1535. }
  1536. static void hci_cs_read_remote_ext_features(struct hci_dev *hdev, __u8 status)
  1537. {
  1538. struct hci_cp_read_remote_ext_features *cp;
  1539. struct hci_conn *conn;
  1540. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1541. if (!status)
  1542. return;
  1543. cp = hci_sent_cmd_data(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES);
  1544. if (!cp)
  1545. return;
  1546. hci_dev_lock(hdev);
  1547. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1548. if (conn) {
  1549. if (conn->state == BT_CONFIG) {
  1550. hci_connect_cfm(conn, status);
  1551. hci_conn_drop(conn);
  1552. }
  1553. }
  1554. hci_dev_unlock(hdev);
  1555. }
  1556. static void hci_cs_setup_sync_conn(struct hci_dev *hdev, __u8 status)
  1557. {
  1558. struct hci_cp_setup_sync_conn *cp;
  1559. struct hci_conn *acl, *sco;
  1560. __u16 handle;
  1561. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1562. if (!status)
  1563. return;
  1564. cp = hci_sent_cmd_data(hdev, HCI_OP_SETUP_SYNC_CONN);
  1565. if (!cp)
  1566. return;
  1567. handle = __le16_to_cpu(cp->handle);
  1568. BT_DBG("%s handle 0x%4.4x", hdev->name, handle);
  1569. hci_dev_lock(hdev);
  1570. acl = hci_conn_hash_lookup_handle(hdev, handle);
  1571. if (acl) {
  1572. sco = acl->link;
  1573. if (sco) {
  1574. sco->state = BT_CLOSED;
  1575. hci_connect_cfm(sco, status);
  1576. hci_conn_del(sco);
  1577. }
  1578. }
  1579. hci_dev_unlock(hdev);
  1580. }
  1581. static void hci_cs_sniff_mode(struct hci_dev *hdev, __u8 status)
  1582. {
  1583. struct hci_cp_sniff_mode *cp;
  1584. struct hci_conn *conn;
  1585. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1586. if (!status)
  1587. return;
  1588. cp = hci_sent_cmd_data(hdev, HCI_OP_SNIFF_MODE);
  1589. if (!cp)
  1590. return;
  1591. hci_dev_lock(hdev);
  1592. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1593. if (conn) {
  1594. clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
  1595. if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
  1596. hci_sco_setup(conn, status);
  1597. }
  1598. hci_dev_unlock(hdev);
  1599. }
  1600. static void hci_cs_exit_sniff_mode(struct hci_dev *hdev, __u8 status)
  1601. {
  1602. struct hci_cp_exit_sniff_mode *cp;
  1603. struct hci_conn *conn;
  1604. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1605. if (!status)
  1606. return;
  1607. cp = hci_sent_cmd_data(hdev, HCI_OP_EXIT_SNIFF_MODE);
  1608. if (!cp)
  1609. return;
  1610. hci_dev_lock(hdev);
  1611. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1612. if (conn) {
  1613. clear_bit(HCI_CONN_MODE_CHANGE_PEND, &conn->flags);
  1614. if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
  1615. hci_sco_setup(conn, status);
  1616. }
  1617. hci_dev_unlock(hdev);
  1618. }
  1619. static void hci_cs_disconnect(struct hci_dev *hdev, u8 status)
  1620. {
  1621. struct hci_cp_disconnect *cp;
  1622. struct hci_conn *conn;
  1623. if (!status)
  1624. return;
  1625. cp = hci_sent_cmd_data(hdev, HCI_OP_DISCONNECT);
  1626. if (!cp)
  1627. return;
  1628. hci_dev_lock(hdev);
  1629. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1630. if (conn) {
  1631. u8 type = conn->type;
  1632. mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
  1633. conn->dst_type, status);
  1634. /* If the disconnection failed for any reason, the upper layer
  1635. * does not retry to disconnect in current implementation.
  1636. * Hence, we need to do some basic cleanup here and re-enable
  1637. * advertising if necessary.
  1638. */
  1639. hci_conn_del(conn);
  1640. if (type == LE_LINK)
  1641. hci_req_reenable_advertising(hdev);
  1642. }
  1643. hci_dev_unlock(hdev);
  1644. }
  1645. static void cs_le_create_conn(struct hci_dev *hdev, bdaddr_t *peer_addr,
  1646. u8 peer_addr_type, u8 own_address_type,
  1647. u8 filter_policy)
  1648. {
  1649. struct hci_conn *conn;
  1650. conn = hci_conn_hash_lookup_le(hdev, peer_addr,
  1651. peer_addr_type);
  1652. if (!conn)
  1653. return;
  1654. /* Store the initiator and responder address information which
  1655. * is needed for SMP. These values will not change during the
  1656. * lifetime of the connection.
  1657. */
  1658. conn->init_addr_type = own_address_type;
  1659. if (own_address_type == ADDR_LE_DEV_RANDOM)
  1660. bacpy(&conn->init_addr, &hdev->random_addr);
  1661. else
  1662. bacpy(&conn->init_addr, &hdev->bdaddr);
  1663. conn->resp_addr_type = peer_addr_type;
  1664. bacpy(&conn->resp_addr, peer_addr);
  1665. /* We don't want the connection attempt to stick around
  1666. * indefinitely since LE doesn't have a page timeout concept
  1667. * like BR/EDR. Set a timer for any connection that doesn't use
  1668. * the white list for connecting.
  1669. */
  1670. if (filter_policy == HCI_LE_USE_PEER_ADDR)
  1671. queue_delayed_work(conn->hdev->workqueue,
  1672. &conn->le_conn_timeout,
  1673. conn->conn_timeout);
  1674. }
  1675. static void hci_cs_le_create_conn(struct hci_dev *hdev, u8 status)
  1676. {
  1677. struct hci_cp_le_create_conn *cp;
  1678. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1679. /* All connection failure handling is taken care of by the
  1680. * hci_le_conn_failed function which is triggered by the HCI
  1681. * request completion callbacks used for connecting.
  1682. */
  1683. if (status)
  1684. return;
  1685. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_CREATE_CONN);
  1686. if (!cp)
  1687. return;
  1688. hci_dev_lock(hdev);
  1689. cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
  1690. cp->own_address_type, cp->filter_policy);
  1691. hci_dev_unlock(hdev);
  1692. }
  1693. static void hci_cs_le_ext_create_conn(struct hci_dev *hdev, u8 status)
  1694. {
  1695. struct hci_cp_le_ext_create_conn *cp;
  1696. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1697. /* All connection failure handling is taken care of by the
  1698. * hci_le_conn_failed function which is triggered by the HCI
  1699. * request completion callbacks used for connecting.
  1700. */
  1701. if (status)
  1702. return;
  1703. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_EXT_CREATE_CONN);
  1704. if (!cp)
  1705. return;
  1706. hci_dev_lock(hdev);
  1707. cs_le_create_conn(hdev, &cp->peer_addr, cp->peer_addr_type,
  1708. cp->own_addr_type, cp->filter_policy);
  1709. hci_dev_unlock(hdev);
  1710. }
  1711. static void hci_cs_le_read_remote_features(struct hci_dev *hdev, u8 status)
  1712. {
  1713. struct hci_cp_le_read_remote_features *cp;
  1714. struct hci_conn *conn;
  1715. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1716. if (!status)
  1717. return;
  1718. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_READ_REMOTE_FEATURES);
  1719. if (!cp)
  1720. return;
  1721. hci_dev_lock(hdev);
  1722. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1723. if (conn) {
  1724. if (conn->state == BT_CONFIG) {
  1725. hci_connect_cfm(conn, status);
  1726. hci_conn_drop(conn);
  1727. }
  1728. }
  1729. hci_dev_unlock(hdev);
  1730. }
  1731. static void hci_cs_le_start_enc(struct hci_dev *hdev, u8 status)
  1732. {
  1733. struct hci_cp_le_start_enc *cp;
  1734. struct hci_conn *conn;
  1735. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1736. if (!status)
  1737. return;
  1738. hci_dev_lock(hdev);
  1739. cp = hci_sent_cmd_data(hdev, HCI_OP_LE_START_ENC);
  1740. if (!cp)
  1741. goto unlock;
  1742. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(cp->handle));
  1743. if (!conn)
  1744. goto unlock;
  1745. if (conn->state != BT_CONNECTED)
  1746. goto unlock;
  1747. hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
  1748. hci_conn_drop(conn);
  1749. unlock:
  1750. hci_dev_unlock(hdev);
  1751. }
  1752. static void hci_cs_switch_role(struct hci_dev *hdev, u8 status)
  1753. {
  1754. struct hci_cp_switch_role *cp;
  1755. struct hci_conn *conn;
  1756. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1757. if (!status)
  1758. return;
  1759. cp = hci_sent_cmd_data(hdev, HCI_OP_SWITCH_ROLE);
  1760. if (!cp)
  1761. return;
  1762. hci_dev_lock(hdev);
  1763. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &cp->bdaddr);
  1764. if (conn)
  1765. clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
  1766. hci_dev_unlock(hdev);
  1767. }
  1768. static void hci_inquiry_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  1769. {
  1770. __u8 status = *((__u8 *) skb->data);
  1771. struct discovery_state *discov = &hdev->discovery;
  1772. struct inquiry_entry *e;
  1773. BT_DBG("%s status 0x%2.2x", hdev->name, status);
  1774. hci_conn_check_pending(hdev);
  1775. if (!test_and_clear_bit(HCI_INQUIRY, &hdev->flags))
  1776. return;
  1777. smp_mb__after_atomic(); /* wake_up_bit advises about this barrier */
  1778. wake_up_bit(&hdev->flags, HCI_INQUIRY);
  1779. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  1780. return;
  1781. hci_dev_lock(hdev);
  1782. if (discov->state != DISCOVERY_FINDING)
  1783. goto unlock;
  1784. if (list_empty(&discov->resolve)) {
  1785. /* When BR/EDR inquiry is active and no LE scanning is in
  1786. * progress, then change discovery state to indicate completion.
  1787. *
  1788. * When running LE scanning and BR/EDR inquiry simultaneously
  1789. * and the LE scan already finished, then change the discovery
  1790. * state to indicate completion.
  1791. */
  1792. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
  1793. !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
  1794. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1795. goto unlock;
  1796. }
  1797. e = hci_inquiry_cache_lookup_resolve(hdev, BDADDR_ANY, NAME_NEEDED);
  1798. if (e && hci_resolve_name(hdev, e) == 0) {
  1799. e->name_state = NAME_PENDING;
  1800. hci_discovery_set_state(hdev, DISCOVERY_RESOLVING);
  1801. } else {
  1802. /* When BR/EDR inquiry is active and no LE scanning is in
  1803. * progress, then change discovery state to indicate completion.
  1804. *
  1805. * When running LE scanning and BR/EDR inquiry simultaneously
  1806. * and the LE scan already finished, then change the discovery
  1807. * state to indicate completion.
  1808. */
  1809. if (!hci_dev_test_flag(hdev, HCI_LE_SCAN) ||
  1810. !test_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks))
  1811. hci_discovery_set_state(hdev, DISCOVERY_STOPPED);
  1812. }
  1813. unlock:
  1814. hci_dev_unlock(hdev);
  1815. }
  1816. static void hci_inquiry_result_evt(struct hci_dev *hdev, struct sk_buff *skb)
  1817. {
  1818. struct inquiry_data data;
  1819. struct inquiry_info *info = (void *) (skb->data + 1);
  1820. int num_rsp = *((__u8 *) skb->data);
  1821. BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
  1822. if (!num_rsp)
  1823. return;
  1824. if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
  1825. return;
  1826. hci_dev_lock(hdev);
  1827. for (; num_rsp; num_rsp--, info++) {
  1828. u32 flags;
  1829. bacpy(&data.bdaddr, &info->bdaddr);
  1830. data.pscan_rep_mode = info->pscan_rep_mode;
  1831. data.pscan_period_mode = info->pscan_period_mode;
  1832. data.pscan_mode = info->pscan_mode;
  1833. memcpy(data.dev_class, info->dev_class, 3);
  1834. data.clock_offset = info->clock_offset;
  1835. data.rssi = HCI_RSSI_INVALID;
  1836. data.ssp_mode = 0x00;
  1837. flags = hci_inquiry_cache_update(hdev, &data, false);
  1838. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  1839. info->dev_class, HCI_RSSI_INVALID,
  1840. flags, NULL, 0, NULL, 0);
  1841. }
  1842. hci_dev_unlock(hdev);
  1843. }
  1844. static void hci_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  1845. {
  1846. struct hci_ev_conn_complete *ev = (void *) skb->data;
  1847. struct inquiry_entry *ie;
  1848. struct hci_conn *conn;
  1849. BT_DBG("%s", hdev->name);
  1850. hci_dev_lock(hdev);
  1851. conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
  1852. if (!conn) {
  1853. /* Connection may not exist if auto-connected. Check the inquiry
  1854. * cache to see if we've already discovered this bdaddr before.
  1855. * If found and link is an ACL type, create a connection class
  1856. * automatically.
  1857. */
  1858. ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
  1859. if (ie && ev->link_type == ACL_LINK) {
  1860. conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
  1861. HCI_ROLE_SLAVE);
  1862. if (!conn) {
  1863. bt_dev_err(hdev, "no memory for new conn");
  1864. goto unlock;
  1865. }
  1866. } else {
  1867. if (ev->link_type != SCO_LINK)
  1868. goto unlock;
  1869. conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK,
  1870. &ev->bdaddr);
  1871. if (!conn)
  1872. goto unlock;
  1873. conn->type = SCO_LINK;
  1874. }
  1875. }
  1876. if (!ev->status) {
  1877. conn->handle = __le16_to_cpu(ev->handle);
  1878. if (conn->type == ACL_LINK) {
  1879. conn->state = BT_CONFIG;
  1880. hci_conn_hold(conn);
  1881. if (!conn->out && !hci_conn_ssp_enabled(conn) &&
  1882. !hci_find_link_key(hdev, &ev->bdaddr))
  1883. conn->disc_timeout = HCI_PAIRING_TIMEOUT;
  1884. else
  1885. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  1886. } else
  1887. conn->state = BT_CONNECTED;
  1888. hci_debugfs_create_conn(conn);
  1889. hci_conn_add_sysfs(conn);
  1890. if (test_bit(HCI_AUTH, &hdev->flags))
  1891. set_bit(HCI_CONN_AUTH, &conn->flags);
  1892. if (test_bit(HCI_ENCRYPT, &hdev->flags))
  1893. set_bit(HCI_CONN_ENCRYPT, &conn->flags);
  1894. /* Get remote features */
  1895. if (conn->type == ACL_LINK) {
  1896. struct hci_cp_read_remote_features cp;
  1897. cp.handle = ev->handle;
  1898. hci_send_cmd(hdev, HCI_OP_READ_REMOTE_FEATURES,
  1899. sizeof(cp), &cp);
  1900. hci_req_update_scan(hdev);
  1901. }
  1902. /* Set packet type for incoming connection */
  1903. if (!conn->out && hdev->hci_ver < BLUETOOTH_VER_2_0) {
  1904. struct hci_cp_change_conn_ptype cp;
  1905. cp.handle = ev->handle;
  1906. cp.pkt_type = cpu_to_le16(conn->pkt_type);
  1907. hci_send_cmd(hdev, HCI_OP_CHANGE_CONN_PTYPE, sizeof(cp),
  1908. &cp);
  1909. }
  1910. } else {
  1911. conn->state = BT_CLOSED;
  1912. if (conn->type == ACL_LINK)
  1913. mgmt_connect_failed(hdev, &conn->dst, conn->type,
  1914. conn->dst_type, ev->status);
  1915. }
  1916. if (conn->type == ACL_LINK)
  1917. hci_sco_setup(conn, ev->status);
  1918. if (ev->status) {
  1919. hci_connect_cfm(conn, ev->status);
  1920. hci_conn_del(conn);
  1921. } else if (ev->link_type != ACL_LINK)
  1922. hci_connect_cfm(conn, ev->status);
  1923. unlock:
  1924. hci_dev_unlock(hdev);
  1925. hci_conn_check_pending(hdev);
  1926. }
  1927. static void hci_reject_conn(struct hci_dev *hdev, bdaddr_t *bdaddr)
  1928. {
  1929. struct hci_cp_reject_conn_req cp;
  1930. bacpy(&cp.bdaddr, bdaddr);
  1931. cp.reason = HCI_ERROR_REJ_BAD_ADDR;
  1932. hci_send_cmd(hdev, HCI_OP_REJECT_CONN_REQ, sizeof(cp), &cp);
  1933. }
  1934. static void hci_conn_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
  1935. {
  1936. struct hci_ev_conn_request *ev = (void *) skb->data;
  1937. int mask = hdev->link_mode;
  1938. struct inquiry_entry *ie;
  1939. struct hci_conn *conn;
  1940. __u8 flags = 0;
  1941. BT_DBG("%s bdaddr %pMR type 0x%x", hdev->name, &ev->bdaddr,
  1942. ev->link_type);
  1943. mask |= hci_proto_connect_ind(hdev, &ev->bdaddr, ev->link_type,
  1944. &flags);
  1945. if (!(mask & HCI_LM_ACCEPT)) {
  1946. hci_reject_conn(hdev, &ev->bdaddr);
  1947. return;
  1948. }
  1949. if (hci_bdaddr_list_lookup(&hdev->blacklist, &ev->bdaddr,
  1950. BDADDR_BREDR)) {
  1951. hci_reject_conn(hdev, &ev->bdaddr);
  1952. return;
  1953. }
  1954. /* Require HCI_CONNECTABLE or a whitelist entry to accept the
  1955. * connection. These features are only touched through mgmt so
  1956. * only do the checks if HCI_MGMT is set.
  1957. */
  1958. if (hci_dev_test_flag(hdev, HCI_MGMT) &&
  1959. !hci_dev_test_flag(hdev, HCI_CONNECTABLE) &&
  1960. !hci_bdaddr_list_lookup(&hdev->whitelist, &ev->bdaddr,
  1961. BDADDR_BREDR)) {
  1962. hci_reject_conn(hdev, &ev->bdaddr);
  1963. return;
  1964. }
  1965. /* Connection accepted */
  1966. hci_dev_lock(hdev);
  1967. ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
  1968. if (ie)
  1969. memcpy(ie->data.dev_class, ev->dev_class, 3);
  1970. conn = hci_conn_hash_lookup_ba(hdev, ev->link_type,
  1971. &ev->bdaddr);
  1972. if (!conn) {
  1973. conn = hci_conn_add(hdev, ev->link_type, &ev->bdaddr,
  1974. HCI_ROLE_SLAVE);
  1975. if (!conn) {
  1976. bt_dev_err(hdev, "no memory for new connection");
  1977. hci_dev_unlock(hdev);
  1978. return;
  1979. }
  1980. }
  1981. memcpy(conn->dev_class, ev->dev_class, 3);
  1982. hci_dev_unlock(hdev);
  1983. if (ev->link_type == ACL_LINK ||
  1984. (!(flags & HCI_PROTO_DEFER) && !lmp_esco_capable(hdev))) {
  1985. struct hci_cp_accept_conn_req cp;
  1986. conn->state = BT_CONNECT;
  1987. bacpy(&cp.bdaddr, &ev->bdaddr);
  1988. if (lmp_rswitch_capable(hdev) && (mask & HCI_LM_MASTER))
  1989. cp.role = 0x00; /* Become master */
  1990. else
  1991. cp.role = 0x01; /* Remain slave */
  1992. hci_send_cmd(hdev, HCI_OP_ACCEPT_CONN_REQ, sizeof(cp), &cp);
  1993. } else if (!(flags & HCI_PROTO_DEFER)) {
  1994. struct hci_cp_accept_sync_conn_req cp;
  1995. conn->state = BT_CONNECT;
  1996. bacpy(&cp.bdaddr, &ev->bdaddr);
  1997. cp.pkt_type = cpu_to_le16(conn->pkt_type);
  1998. cp.tx_bandwidth = cpu_to_le32(0x00001f40);
  1999. cp.rx_bandwidth = cpu_to_le32(0x00001f40);
  2000. cp.max_latency = cpu_to_le16(0xffff);
  2001. cp.content_format = cpu_to_le16(hdev->voice_setting);
  2002. cp.retrans_effort = 0xff;
  2003. hci_send_cmd(hdev, HCI_OP_ACCEPT_SYNC_CONN_REQ, sizeof(cp),
  2004. &cp);
  2005. } else {
  2006. conn->state = BT_CONNECT2;
  2007. hci_connect_cfm(conn, 0);
  2008. }
  2009. }
  2010. static u8 hci_to_mgmt_reason(u8 err)
  2011. {
  2012. switch (err) {
  2013. case HCI_ERROR_CONNECTION_TIMEOUT:
  2014. return MGMT_DEV_DISCONN_TIMEOUT;
  2015. case HCI_ERROR_REMOTE_USER_TERM:
  2016. case HCI_ERROR_REMOTE_LOW_RESOURCES:
  2017. case HCI_ERROR_REMOTE_POWER_OFF:
  2018. return MGMT_DEV_DISCONN_REMOTE;
  2019. case HCI_ERROR_LOCAL_HOST_TERM:
  2020. return MGMT_DEV_DISCONN_LOCAL_HOST;
  2021. default:
  2022. return MGMT_DEV_DISCONN_UNKNOWN;
  2023. }
  2024. }
  2025. static void hci_disconn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2026. {
  2027. struct hci_ev_disconn_complete *ev = (void *) skb->data;
  2028. u8 reason;
  2029. struct hci_conn_params *params;
  2030. struct hci_conn *conn;
  2031. bool mgmt_connected;
  2032. u8 type;
  2033. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2034. hci_dev_lock(hdev);
  2035. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2036. if (!conn)
  2037. goto unlock;
  2038. if (ev->status) {
  2039. mgmt_disconnect_failed(hdev, &conn->dst, conn->type,
  2040. conn->dst_type, ev->status);
  2041. goto unlock;
  2042. }
  2043. conn->state = BT_CLOSED;
  2044. mgmt_connected = test_and_clear_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags);
  2045. if (test_bit(HCI_CONN_AUTH_FAILURE, &conn->flags))
  2046. reason = MGMT_DEV_DISCONN_AUTH_FAILURE;
  2047. else
  2048. reason = hci_to_mgmt_reason(ev->reason);
  2049. mgmt_device_disconnected(hdev, &conn->dst, conn->type, conn->dst_type,
  2050. reason, mgmt_connected);
  2051. if (conn->type == ACL_LINK) {
  2052. if (test_bit(HCI_CONN_FLUSH_KEY, &conn->flags))
  2053. hci_remove_link_key(hdev, &conn->dst);
  2054. hci_req_update_scan(hdev);
  2055. }
  2056. params = hci_conn_params_lookup(hdev, &conn->dst, conn->dst_type);
  2057. if (params) {
  2058. switch (params->auto_connect) {
  2059. case HCI_AUTO_CONN_LINK_LOSS:
  2060. if (ev->reason != HCI_ERROR_CONNECTION_TIMEOUT)
  2061. break;
  2062. /* Fall through */
  2063. case HCI_AUTO_CONN_DIRECT:
  2064. case HCI_AUTO_CONN_ALWAYS:
  2065. list_del_init(&params->action);
  2066. list_add(&params->action, &hdev->pend_le_conns);
  2067. hci_update_background_scan(hdev);
  2068. break;
  2069. default:
  2070. break;
  2071. }
  2072. }
  2073. type = conn->type;
  2074. hci_disconn_cfm(conn, ev->reason);
  2075. hci_conn_del(conn);
  2076. /* The suspend notifier is waiting for all devices to disconnect so
  2077. * clear the bit from pending tasks and inform the wait queue.
  2078. */
  2079. if (list_empty(&hdev->conn_hash.list) &&
  2080. test_and_clear_bit(SUSPEND_DISCONNECTING, hdev->suspend_tasks)) {
  2081. wake_up(&hdev->suspend_wait_q);
  2082. }
  2083. /* Re-enable advertising if necessary, since it might
  2084. * have been disabled by the connection. From the
  2085. * HCI_LE_Set_Advertise_Enable command description in
  2086. * the core specification (v4.0):
  2087. * "The Controller shall continue advertising until the Host
  2088. * issues an LE_Set_Advertise_Enable command with
  2089. * Advertising_Enable set to 0x00 (Advertising is disabled)
  2090. * or until a connection is created or until the Advertising
  2091. * is timed out due to Directed Advertising."
  2092. */
  2093. if (type == LE_LINK)
  2094. hci_req_reenable_advertising(hdev);
  2095. unlock:
  2096. hci_dev_unlock(hdev);
  2097. }
  2098. static void hci_auth_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2099. {
  2100. struct hci_ev_auth_complete *ev = (void *) skb->data;
  2101. struct hci_conn *conn;
  2102. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2103. hci_dev_lock(hdev);
  2104. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2105. if (!conn)
  2106. goto unlock;
  2107. if (!ev->status) {
  2108. clear_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
  2109. if (!hci_conn_ssp_enabled(conn) &&
  2110. test_bit(HCI_CONN_REAUTH_PEND, &conn->flags)) {
  2111. bt_dev_info(hdev, "re-auth of legacy device is not possible.");
  2112. } else {
  2113. set_bit(HCI_CONN_AUTH, &conn->flags);
  2114. conn->sec_level = conn->pending_sec_level;
  2115. }
  2116. } else {
  2117. if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
  2118. set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
  2119. mgmt_auth_failed(conn, ev->status);
  2120. }
  2121. clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
  2122. clear_bit(HCI_CONN_REAUTH_PEND, &conn->flags);
  2123. if (conn->state == BT_CONFIG) {
  2124. if (!ev->status && hci_conn_ssp_enabled(conn)) {
  2125. struct hci_cp_set_conn_encrypt cp;
  2126. cp.handle = ev->handle;
  2127. cp.encrypt = 0x01;
  2128. hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
  2129. &cp);
  2130. } else {
  2131. conn->state = BT_CONNECTED;
  2132. hci_connect_cfm(conn, ev->status);
  2133. hci_conn_drop(conn);
  2134. }
  2135. } else {
  2136. hci_auth_cfm(conn, ev->status);
  2137. hci_conn_hold(conn);
  2138. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  2139. hci_conn_drop(conn);
  2140. }
  2141. if (test_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags)) {
  2142. if (!ev->status) {
  2143. struct hci_cp_set_conn_encrypt cp;
  2144. cp.handle = ev->handle;
  2145. cp.encrypt = 0x01;
  2146. hci_send_cmd(hdev, HCI_OP_SET_CONN_ENCRYPT, sizeof(cp),
  2147. &cp);
  2148. } else {
  2149. clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  2150. hci_encrypt_cfm(conn, ev->status, 0x00);
  2151. }
  2152. }
  2153. unlock:
  2154. hci_dev_unlock(hdev);
  2155. }
  2156. static void hci_remote_name_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2157. {
  2158. struct hci_ev_remote_name *ev = (void *) skb->data;
  2159. struct hci_conn *conn;
  2160. BT_DBG("%s", hdev->name);
  2161. hci_conn_check_pending(hdev);
  2162. hci_dev_lock(hdev);
  2163. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  2164. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  2165. goto check_auth;
  2166. if (ev->status == 0)
  2167. hci_check_pending_name(hdev, conn, &ev->bdaddr, ev->name,
  2168. strnlen(ev->name, HCI_MAX_NAME_LENGTH));
  2169. else
  2170. hci_check_pending_name(hdev, conn, &ev->bdaddr, NULL, 0);
  2171. check_auth:
  2172. if (!conn)
  2173. goto unlock;
  2174. if (!hci_outgoing_auth_needed(hdev, conn))
  2175. goto unlock;
  2176. if (!test_and_set_bit(HCI_CONN_AUTH_PEND, &conn->flags)) {
  2177. struct hci_cp_auth_requested cp;
  2178. set_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags);
  2179. cp.handle = __cpu_to_le16(conn->handle);
  2180. hci_send_cmd(hdev, HCI_OP_AUTH_REQUESTED, sizeof(cp), &cp);
  2181. }
  2182. unlock:
  2183. hci_dev_unlock(hdev);
  2184. }
  2185. static void read_enc_key_size_complete(struct hci_dev *hdev, u8 status,
  2186. u16 opcode, struct sk_buff *skb)
  2187. {
  2188. const struct hci_rp_read_enc_key_size *rp;
  2189. struct hci_conn *conn;
  2190. u16 handle;
  2191. BT_DBG("%s status 0x%02x", hdev->name, status);
  2192. if (!skb || skb->len < sizeof(*rp)) {
  2193. bt_dev_err(hdev, "invalid read key size response");
  2194. return;
  2195. }
  2196. rp = (void *)skb->data;
  2197. handle = le16_to_cpu(rp->handle);
  2198. hci_dev_lock(hdev);
  2199. conn = hci_conn_hash_lookup_handle(hdev, handle);
  2200. if (!conn)
  2201. goto unlock;
  2202. /* While unexpected, the read_enc_key_size command may fail. The most
  2203. * secure approach is to then assume the key size is 0 to force a
  2204. * disconnection.
  2205. */
  2206. if (rp->status) {
  2207. bt_dev_err(hdev, "failed to read key size for handle %u",
  2208. handle);
  2209. conn->enc_key_size = 0;
  2210. } else {
  2211. conn->enc_key_size = rp->key_size;
  2212. }
  2213. if (conn->state == BT_CONFIG) {
  2214. conn->state = BT_CONNECTED;
  2215. hci_connect_cfm(conn, 0);
  2216. hci_conn_drop(conn);
  2217. } else {
  2218. u8 encrypt;
  2219. if (!test_bit(HCI_CONN_ENCRYPT, &conn->flags))
  2220. encrypt = 0x00;
  2221. else if (test_bit(HCI_CONN_AES_CCM, &conn->flags))
  2222. encrypt = 0x02;
  2223. else
  2224. encrypt = 0x01;
  2225. hci_encrypt_cfm(conn, 0, encrypt);
  2226. }
  2227. unlock:
  2228. hci_dev_unlock(hdev);
  2229. }
  2230. static void hci_encrypt_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2231. {
  2232. struct hci_ev_encrypt_change *ev = (void *) skb->data;
  2233. struct hci_conn *conn;
  2234. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2235. hci_dev_lock(hdev);
  2236. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2237. if (!conn)
  2238. goto unlock;
  2239. if (!ev->status) {
  2240. if (ev->encrypt) {
  2241. /* Encryption implies authentication */
  2242. set_bit(HCI_CONN_AUTH, &conn->flags);
  2243. set_bit(HCI_CONN_ENCRYPT, &conn->flags);
  2244. conn->sec_level = conn->pending_sec_level;
  2245. /* P-256 authentication key implies FIPS */
  2246. if (conn->key_type == HCI_LK_AUTH_COMBINATION_P256)
  2247. set_bit(HCI_CONN_FIPS, &conn->flags);
  2248. if ((conn->type == ACL_LINK && ev->encrypt == 0x02) ||
  2249. conn->type == LE_LINK)
  2250. set_bit(HCI_CONN_AES_CCM, &conn->flags);
  2251. } else {
  2252. clear_bit(HCI_CONN_ENCRYPT, &conn->flags);
  2253. clear_bit(HCI_CONN_AES_CCM, &conn->flags);
  2254. }
  2255. }
  2256. /* We should disregard the current RPA and generate a new one
  2257. * whenever the encryption procedure fails.
  2258. */
  2259. if (ev->status && conn->type == LE_LINK) {
  2260. hci_dev_set_flag(hdev, HCI_RPA_EXPIRED);
  2261. hci_adv_instances_set_rpa_expired(hdev, true);
  2262. }
  2263. clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  2264. if (ev->status && conn->state == BT_CONNECTED) {
  2265. if (ev->status == HCI_ERROR_PIN_OR_KEY_MISSING)
  2266. set_bit(HCI_CONN_AUTH_FAILURE, &conn->flags);
  2267. hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
  2268. hci_conn_drop(conn);
  2269. goto unlock;
  2270. }
  2271. /* In Secure Connections Only mode, do not allow any connections
  2272. * that are not encrypted with AES-CCM using a P-256 authenticated
  2273. * combination key.
  2274. */
  2275. if (hci_dev_test_flag(hdev, HCI_SC_ONLY) &&
  2276. (!test_bit(HCI_CONN_AES_CCM, &conn->flags) ||
  2277. conn->key_type != HCI_LK_AUTH_COMBINATION_P256)) {
  2278. hci_connect_cfm(conn, HCI_ERROR_AUTH_FAILURE);
  2279. hci_conn_drop(conn);
  2280. goto unlock;
  2281. }
  2282. /* Try reading the encryption key size for encrypted ACL links */
  2283. if (!ev->status && ev->encrypt && conn->type == ACL_LINK) {
  2284. struct hci_cp_read_enc_key_size cp;
  2285. struct hci_request req;
  2286. /* Only send HCI_Read_Encryption_Key_Size if the
  2287. * controller really supports it. If it doesn't, assume
  2288. * the default size (16).
  2289. */
  2290. if (!(hdev->commands[20] & 0x10)) {
  2291. conn->enc_key_size = HCI_LINK_KEY_SIZE;
  2292. goto notify;
  2293. }
  2294. hci_req_init(&req, hdev);
  2295. cp.handle = cpu_to_le16(conn->handle);
  2296. hci_req_add(&req, HCI_OP_READ_ENC_KEY_SIZE, sizeof(cp), &cp);
  2297. if (hci_req_run_skb(&req, read_enc_key_size_complete)) {
  2298. bt_dev_err(hdev, "sending read key size failed");
  2299. conn->enc_key_size = HCI_LINK_KEY_SIZE;
  2300. goto notify;
  2301. }
  2302. goto unlock;
  2303. }
  2304. /* Set the default Authenticated Payload Timeout after
  2305. * an LE Link is established. As per Core Spec v5.0, Vol 2, Part B
  2306. * Section 3.3, the HCI command WRITE_AUTH_PAYLOAD_TIMEOUT should be
  2307. * sent when the link is active and Encryption is enabled, the conn
  2308. * type can be either LE or ACL and controller must support LMP Ping.
  2309. * Ensure for AES-CCM encryption as well.
  2310. */
  2311. if (test_bit(HCI_CONN_ENCRYPT, &conn->flags) &&
  2312. test_bit(HCI_CONN_AES_CCM, &conn->flags) &&
  2313. ((conn->type == ACL_LINK && lmp_ping_capable(hdev)) ||
  2314. (conn->type == LE_LINK && (hdev->le_features[0] & HCI_LE_PING)))) {
  2315. struct hci_cp_write_auth_payload_to cp;
  2316. cp.handle = cpu_to_le16(conn->handle);
  2317. cp.timeout = cpu_to_le16(hdev->auth_payload_timeout);
  2318. hci_send_cmd(conn->hdev, HCI_OP_WRITE_AUTH_PAYLOAD_TO,
  2319. sizeof(cp), &cp);
  2320. }
  2321. notify:
  2322. if (conn->state == BT_CONFIG) {
  2323. if (!ev->status)
  2324. conn->state = BT_CONNECTED;
  2325. hci_connect_cfm(conn, ev->status);
  2326. hci_conn_drop(conn);
  2327. } else
  2328. hci_encrypt_cfm(conn, ev->status, ev->encrypt);
  2329. unlock:
  2330. hci_dev_unlock(hdev);
  2331. }
  2332. static void hci_change_link_key_complete_evt(struct hci_dev *hdev,
  2333. struct sk_buff *skb)
  2334. {
  2335. struct hci_ev_change_link_key_complete *ev = (void *) skb->data;
  2336. struct hci_conn *conn;
  2337. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2338. hci_dev_lock(hdev);
  2339. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2340. if (conn) {
  2341. if (!ev->status)
  2342. set_bit(HCI_CONN_SECURE, &conn->flags);
  2343. clear_bit(HCI_CONN_AUTH_PEND, &conn->flags);
  2344. hci_key_change_cfm(conn, ev->status);
  2345. }
  2346. hci_dev_unlock(hdev);
  2347. }
  2348. static void hci_remote_features_evt(struct hci_dev *hdev,
  2349. struct sk_buff *skb)
  2350. {
  2351. struct hci_ev_remote_features *ev = (void *) skb->data;
  2352. struct hci_conn *conn;
  2353. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2354. hci_dev_lock(hdev);
  2355. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2356. if (!conn)
  2357. goto unlock;
  2358. if (!ev->status)
  2359. memcpy(conn->features[0], ev->features, 8);
  2360. if (conn->state != BT_CONFIG)
  2361. goto unlock;
  2362. if (!ev->status && lmp_ext_feat_capable(hdev) &&
  2363. lmp_ext_feat_capable(conn)) {
  2364. struct hci_cp_read_remote_ext_features cp;
  2365. cp.handle = ev->handle;
  2366. cp.page = 0x01;
  2367. hci_send_cmd(hdev, HCI_OP_READ_REMOTE_EXT_FEATURES,
  2368. sizeof(cp), &cp);
  2369. goto unlock;
  2370. }
  2371. if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
  2372. struct hci_cp_remote_name_req cp;
  2373. memset(&cp, 0, sizeof(cp));
  2374. bacpy(&cp.bdaddr, &conn->dst);
  2375. cp.pscan_rep_mode = 0x02;
  2376. hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
  2377. } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
  2378. mgmt_device_connected(hdev, conn, 0, NULL, 0);
  2379. if (!hci_outgoing_auth_needed(hdev, conn)) {
  2380. conn->state = BT_CONNECTED;
  2381. hci_connect_cfm(conn, ev->status);
  2382. hci_conn_drop(conn);
  2383. }
  2384. unlock:
  2385. hci_dev_unlock(hdev);
  2386. }
  2387. static void hci_cmd_complete_evt(struct hci_dev *hdev, struct sk_buff *skb,
  2388. u16 *opcode, u8 *status,
  2389. hci_req_complete_t *req_complete,
  2390. hci_req_complete_skb_t *req_complete_skb)
  2391. {
  2392. struct hci_ev_cmd_complete *ev = (void *) skb->data;
  2393. *opcode = __le16_to_cpu(ev->opcode);
  2394. *status = skb->data[sizeof(*ev)];
  2395. skb_pull(skb, sizeof(*ev));
  2396. switch (*opcode) {
  2397. case HCI_OP_INQUIRY_CANCEL:
  2398. hci_cc_inquiry_cancel(hdev, skb);
  2399. break;
  2400. case HCI_OP_PERIODIC_INQ:
  2401. hci_cc_periodic_inq(hdev, skb);
  2402. break;
  2403. case HCI_OP_EXIT_PERIODIC_INQ:
  2404. hci_cc_exit_periodic_inq(hdev, skb);
  2405. break;
  2406. case HCI_OP_REMOTE_NAME_REQ_CANCEL:
  2407. hci_cc_remote_name_req_cancel(hdev, skb);
  2408. break;
  2409. case HCI_OP_ROLE_DISCOVERY:
  2410. hci_cc_role_discovery(hdev, skb);
  2411. break;
  2412. case HCI_OP_READ_LINK_POLICY:
  2413. hci_cc_read_link_policy(hdev, skb);
  2414. break;
  2415. case HCI_OP_WRITE_LINK_POLICY:
  2416. hci_cc_write_link_policy(hdev, skb);
  2417. break;
  2418. case HCI_OP_READ_DEF_LINK_POLICY:
  2419. hci_cc_read_def_link_policy(hdev, skb);
  2420. break;
  2421. case HCI_OP_WRITE_DEF_LINK_POLICY:
  2422. hci_cc_write_def_link_policy(hdev, skb);
  2423. break;
  2424. case HCI_OP_RESET:
  2425. hci_cc_reset(hdev, skb);
  2426. break;
  2427. case HCI_OP_READ_STORED_LINK_KEY:
  2428. hci_cc_read_stored_link_key(hdev, skb);
  2429. break;
  2430. case HCI_OP_DELETE_STORED_LINK_KEY:
  2431. hci_cc_delete_stored_link_key(hdev, skb);
  2432. break;
  2433. case HCI_OP_WRITE_LOCAL_NAME:
  2434. hci_cc_write_local_name(hdev, skb);
  2435. break;
  2436. case HCI_OP_READ_LOCAL_NAME:
  2437. hci_cc_read_local_name(hdev, skb);
  2438. break;
  2439. case HCI_OP_WRITE_AUTH_ENABLE:
  2440. hci_cc_write_auth_enable(hdev, skb);
  2441. break;
  2442. case HCI_OP_WRITE_ENCRYPT_MODE:
  2443. hci_cc_write_encrypt_mode(hdev, skb);
  2444. break;
  2445. case HCI_OP_WRITE_SCAN_ENABLE:
  2446. hci_cc_write_scan_enable(hdev, skb);
  2447. break;
  2448. case HCI_OP_READ_CLASS_OF_DEV:
  2449. hci_cc_read_class_of_dev(hdev, skb);
  2450. break;
  2451. case HCI_OP_WRITE_CLASS_OF_DEV:
  2452. hci_cc_write_class_of_dev(hdev, skb);
  2453. break;
  2454. case HCI_OP_READ_VOICE_SETTING:
  2455. hci_cc_read_voice_setting(hdev, skb);
  2456. break;
  2457. case HCI_OP_WRITE_VOICE_SETTING:
  2458. hci_cc_write_voice_setting(hdev, skb);
  2459. break;
  2460. case HCI_OP_READ_NUM_SUPPORTED_IAC:
  2461. hci_cc_read_num_supported_iac(hdev, skb);
  2462. break;
  2463. case HCI_OP_WRITE_SSP_MODE:
  2464. hci_cc_write_ssp_mode(hdev, skb);
  2465. break;
  2466. case HCI_OP_WRITE_SC_SUPPORT:
  2467. hci_cc_write_sc_support(hdev, skb);
  2468. break;
  2469. case HCI_OP_READ_AUTH_PAYLOAD_TO:
  2470. hci_cc_read_auth_payload_timeout(hdev, skb);
  2471. break;
  2472. case HCI_OP_WRITE_AUTH_PAYLOAD_TO:
  2473. hci_cc_write_auth_payload_timeout(hdev, skb);
  2474. break;
  2475. case HCI_OP_READ_LOCAL_VERSION:
  2476. hci_cc_read_local_version(hdev, skb);
  2477. break;
  2478. case HCI_OP_READ_LOCAL_COMMANDS:
  2479. hci_cc_read_local_commands(hdev, skb);
  2480. break;
  2481. case HCI_OP_READ_LOCAL_FEATURES:
  2482. hci_cc_read_local_features(hdev, skb);
  2483. break;
  2484. case HCI_OP_READ_LOCAL_EXT_FEATURES:
  2485. hci_cc_read_local_ext_features(hdev, skb);
  2486. break;
  2487. case HCI_OP_READ_BUFFER_SIZE:
  2488. hci_cc_read_buffer_size(hdev, skb);
  2489. break;
  2490. case HCI_OP_READ_BD_ADDR:
  2491. hci_cc_read_bd_addr(hdev, skb);
  2492. break;
  2493. case HCI_OP_READ_PAGE_SCAN_ACTIVITY:
  2494. hci_cc_read_page_scan_activity(hdev, skb);
  2495. break;
  2496. case HCI_OP_WRITE_PAGE_SCAN_ACTIVITY:
  2497. hci_cc_write_page_scan_activity(hdev, skb);
  2498. break;
  2499. case HCI_OP_READ_PAGE_SCAN_TYPE:
  2500. hci_cc_read_page_scan_type(hdev, skb);
  2501. break;
  2502. case HCI_OP_WRITE_PAGE_SCAN_TYPE:
  2503. hci_cc_write_page_scan_type(hdev, skb);
  2504. break;
  2505. case HCI_OP_READ_DATA_BLOCK_SIZE:
  2506. hci_cc_read_data_block_size(hdev, skb);
  2507. break;
  2508. case HCI_OP_READ_FLOW_CONTROL_MODE:
  2509. hci_cc_read_flow_control_mode(hdev, skb);
  2510. break;
  2511. case HCI_OP_READ_LOCAL_AMP_INFO:
  2512. hci_cc_read_local_amp_info(hdev, skb);
  2513. break;
  2514. case HCI_OP_READ_CLOCK:
  2515. hci_cc_read_clock(hdev, skb);
  2516. break;
  2517. case HCI_OP_READ_INQ_RSP_TX_POWER:
  2518. hci_cc_read_inq_rsp_tx_power(hdev, skb);
  2519. break;
  2520. case HCI_OP_READ_DEF_ERR_DATA_REPORTING:
  2521. hci_cc_read_def_err_data_reporting(hdev, skb);
  2522. break;
  2523. case HCI_OP_WRITE_DEF_ERR_DATA_REPORTING:
  2524. hci_cc_write_def_err_data_reporting(hdev, skb);
  2525. break;
  2526. case HCI_OP_PIN_CODE_REPLY:
  2527. hci_cc_pin_code_reply(hdev, skb);
  2528. break;
  2529. case HCI_OP_PIN_CODE_NEG_REPLY:
  2530. hci_cc_pin_code_neg_reply(hdev, skb);
  2531. break;
  2532. case HCI_OP_READ_LOCAL_OOB_DATA:
  2533. hci_cc_read_local_oob_data(hdev, skb);
  2534. break;
  2535. case HCI_OP_READ_LOCAL_OOB_EXT_DATA:
  2536. hci_cc_read_local_oob_ext_data(hdev, skb);
  2537. break;
  2538. case HCI_OP_LE_READ_BUFFER_SIZE:
  2539. hci_cc_le_read_buffer_size(hdev, skb);
  2540. break;
  2541. case HCI_OP_LE_READ_LOCAL_FEATURES:
  2542. hci_cc_le_read_local_features(hdev, skb);
  2543. break;
  2544. case HCI_OP_LE_READ_ADV_TX_POWER:
  2545. hci_cc_le_read_adv_tx_power(hdev, skb);
  2546. break;
  2547. case HCI_OP_USER_CONFIRM_REPLY:
  2548. hci_cc_user_confirm_reply(hdev, skb);
  2549. break;
  2550. case HCI_OP_USER_CONFIRM_NEG_REPLY:
  2551. hci_cc_user_confirm_neg_reply(hdev, skb);
  2552. break;
  2553. case HCI_OP_USER_PASSKEY_REPLY:
  2554. hci_cc_user_passkey_reply(hdev, skb);
  2555. break;
  2556. case HCI_OP_USER_PASSKEY_NEG_REPLY:
  2557. hci_cc_user_passkey_neg_reply(hdev, skb);
  2558. break;
  2559. case HCI_OP_LE_SET_RANDOM_ADDR:
  2560. hci_cc_le_set_random_addr(hdev, skb);
  2561. break;
  2562. case HCI_OP_LE_SET_ADV_ENABLE:
  2563. hci_cc_le_set_adv_enable(hdev, skb);
  2564. break;
  2565. case HCI_OP_LE_SET_SCAN_PARAM:
  2566. hci_cc_le_set_scan_param(hdev, skb);
  2567. break;
  2568. case HCI_OP_LE_SET_SCAN_ENABLE:
  2569. hci_cc_le_set_scan_enable(hdev, skb);
  2570. break;
  2571. case HCI_OP_LE_READ_WHITE_LIST_SIZE:
  2572. hci_cc_le_read_white_list_size(hdev, skb);
  2573. break;
  2574. case HCI_OP_LE_CLEAR_WHITE_LIST:
  2575. hci_cc_le_clear_white_list(hdev, skb);
  2576. break;
  2577. case HCI_OP_LE_ADD_TO_WHITE_LIST:
  2578. hci_cc_le_add_to_white_list(hdev, skb);
  2579. break;
  2580. case HCI_OP_LE_DEL_FROM_WHITE_LIST:
  2581. hci_cc_le_del_from_white_list(hdev, skb);
  2582. break;
  2583. case HCI_OP_LE_READ_SUPPORTED_STATES:
  2584. hci_cc_le_read_supported_states(hdev, skb);
  2585. break;
  2586. case HCI_OP_LE_READ_DEF_DATA_LEN:
  2587. hci_cc_le_read_def_data_len(hdev, skb);
  2588. break;
  2589. case HCI_OP_LE_WRITE_DEF_DATA_LEN:
  2590. hci_cc_le_write_def_data_len(hdev, skb);
  2591. break;
  2592. case HCI_OP_LE_ADD_TO_RESOLV_LIST:
  2593. hci_cc_le_add_to_resolv_list(hdev, skb);
  2594. break;
  2595. case HCI_OP_LE_DEL_FROM_RESOLV_LIST:
  2596. hci_cc_le_del_from_resolv_list(hdev, skb);
  2597. break;
  2598. case HCI_OP_LE_CLEAR_RESOLV_LIST:
  2599. hci_cc_le_clear_resolv_list(hdev, skb);
  2600. break;
  2601. case HCI_OP_LE_READ_RESOLV_LIST_SIZE:
  2602. hci_cc_le_read_resolv_list_size(hdev, skb);
  2603. break;
  2604. case HCI_OP_LE_SET_ADDR_RESOLV_ENABLE:
  2605. hci_cc_le_set_addr_resolution_enable(hdev, skb);
  2606. break;
  2607. case HCI_OP_LE_READ_MAX_DATA_LEN:
  2608. hci_cc_le_read_max_data_len(hdev, skb);
  2609. break;
  2610. case HCI_OP_WRITE_LE_HOST_SUPPORTED:
  2611. hci_cc_write_le_host_supported(hdev, skb);
  2612. break;
  2613. case HCI_OP_LE_SET_ADV_PARAM:
  2614. hci_cc_set_adv_param(hdev, skb);
  2615. break;
  2616. case HCI_OP_READ_RSSI:
  2617. hci_cc_read_rssi(hdev, skb);
  2618. break;
  2619. case HCI_OP_READ_TX_POWER:
  2620. hci_cc_read_tx_power(hdev, skb);
  2621. break;
  2622. case HCI_OP_WRITE_SSP_DEBUG_MODE:
  2623. hci_cc_write_ssp_debug_mode(hdev, skb);
  2624. break;
  2625. case HCI_OP_LE_SET_EXT_SCAN_PARAMS:
  2626. hci_cc_le_set_ext_scan_param(hdev, skb);
  2627. break;
  2628. case HCI_OP_LE_SET_EXT_SCAN_ENABLE:
  2629. hci_cc_le_set_ext_scan_enable(hdev, skb);
  2630. break;
  2631. case HCI_OP_LE_SET_DEFAULT_PHY:
  2632. hci_cc_le_set_default_phy(hdev, skb);
  2633. break;
  2634. case HCI_OP_LE_READ_NUM_SUPPORTED_ADV_SETS:
  2635. hci_cc_le_read_num_adv_sets(hdev, skb);
  2636. break;
  2637. case HCI_OP_LE_SET_EXT_ADV_PARAMS:
  2638. hci_cc_set_ext_adv_param(hdev, skb);
  2639. break;
  2640. case HCI_OP_LE_SET_EXT_ADV_ENABLE:
  2641. hci_cc_le_set_ext_adv_enable(hdev, skb);
  2642. break;
  2643. case HCI_OP_LE_SET_ADV_SET_RAND_ADDR:
  2644. hci_cc_le_set_adv_set_random_addr(hdev, skb);
  2645. break;
  2646. default:
  2647. BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
  2648. break;
  2649. }
  2650. if (*opcode != HCI_OP_NOP)
  2651. cancel_delayed_work(&hdev->cmd_timer);
  2652. if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags))
  2653. atomic_set(&hdev->cmd_cnt, 1);
  2654. hci_req_cmd_complete(hdev, *opcode, *status, req_complete,
  2655. req_complete_skb);
  2656. if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
  2657. bt_dev_err(hdev,
  2658. "unexpected event for opcode 0x%4.4x", *opcode);
  2659. return;
  2660. }
  2661. if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
  2662. queue_work(hdev->workqueue, &hdev->cmd_work);
  2663. }
  2664. static void hci_cmd_status_evt(struct hci_dev *hdev, struct sk_buff *skb,
  2665. u16 *opcode, u8 *status,
  2666. hci_req_complete_t *req_complete,
  2667. hci_req_complete_skb_t *req_complete_skb)
  2668. {
  2669. struct hci_ev_cmd_status *ev = (void *) skb->data;
  2670. skb_pull(skb, sizeof(*ev));
  2671. *opcode = __le16_to_cpu(ev->opcode);
  2672. *status = ev->status;
  2673. switch (*opcode) {
  2674. case HCI_OP_INQUIRY:
  2675. hci_cs_inquiry(hdev, ev->status);
  2676. break;
  2677. case HCI_OP_CREATE_CONN:
  2678. hci_cs_create_conn(hdev, ev->status);
  2679. break;
  2680. case HCI_OP_DISCONNECT:
  2681. hci_cs_disconnect(hdev, ev->status);
  2682. break;
  2683. case HCI_OP_ADD_SCO:
  2684. hci_cs_add_sco(hdev, ev->status);
  2685. break;
  2686. case HCI_OP_AUTH_REQUESTED:
  2687. hci_cs_auth_requested(hdev, ev->status);
  2688. break;
  2689. case HCI_OP_SET_CONN_ENCRYPT:
  2690. hci_cs_set_conn_encrypt(hdev, ev->status);
  2691. break;
  2692. case HCI_OP_REMOTE_NAME_REQ:
  2693. hci_cs_remote_name_req(hdev, ev->status);
  2694. break;
  2695. case HCI_OP_READ_REMOTE_FEATURES:
  2696. hci_cs_read_remote_features(hdev, ev->status);
  2697. break;
  2698. case HCI_OP_READ_REMOTE_EXT_FEATURES:
  2699. hci_cs_read_remote_ext_features(hdev, ev->status);
  2700. break;
  2701. case HCI_OP_SETUP_SYNC_CONN:
  2702. hci_cs_setup_sync_conn(hdev, ev->status);
  2703. break;
  2704. case HCI_OP_SNIFF_MODE:
  2705. hci_cs_sniff_mode(hdev, ev->status);
  2706. break;
  2707. case HCI_OP_EXIT_SNIFF_MODE:
  2708. hci_cs_exit_sniff_mode(hdev, ev->status);
  2709. break;
  2710. case HCI_OP_SWITCH_ROLE:
  2711. hci_cs_switch_role(hdev, ev->status);
  2712. break;
  2713. case HCI_OP_LE_CREATE_CONN:
  2714. hci_cs_le_create_conn(hdev, ev->status);
  2715. break;
  2716. case HCI_OP_LE_READ_REMOTE_FEATURES:
  2717. hci_cs_le_read_remote_features(hdev, ev->status);
  2718. break;
  2719. case HCI_OP_LE_START_ENC:
  2720. hci_cs_le_start_enc(hdev, ev->status);
  2721. break;
  2722. case HCI_OP_LE_EXT_CREATE_CONN:
  2723. hci_cs_le_ext_create_conn(hdev, ev->status);
  2724. break;
  2725. default:
  2726. BT_DBG("%s opcode 0x%4.4x", hdev->name, *opcode);
  2727. break;
  2728. }
  2729. if (*opcode != HCI_OP_NOP)
  2730. cancel_delayed_work(&hdev->cmd_timer);
  2731. if (ev->ncmd && !test_bit(HCI_RESET, &hdev->flags))
  2732. atomic_set(&hdev->cmd_cnt, 1);
  2733. /* Indicate request completion if the command failed. Also, if
  2734. * we're not waiting for a special event and we get a success
  2735. * command status we should try to flag the request as completed
  2736. * (since for this kind of commands there will not be a command
  2737. * complete event).
  2738. */
  2739. if (ev->status ||
  2740. (hdev->sent_cmd && !bt_cb(hdev->sent_cmd)->hci.req_event))
  2741. hci_req_cmd_complete(hdev, *opcode, ev->status, req_complete,
  2742. req_complete_skb);
  2743. if (hci_dev_test_flag(hdev, HCI_CMD_PENDING)) {
  2744. bt_dev_err(hdev,
  2745. "unexpected event for opcode 0x%4.4x", *opcode);
  2746. return;
  2747. }
  2748. if (atomic_read(&hdev->cmd_cnt) && !skb_queue_empty(&hdev->cmd_q))
  2749. queue_work(hdev->workqueue, &hdev->cmd_work);
  2750. }
  2751. static void hci_hardware_error_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2752. {
  2753. struct hci_ev_hardware_error *ev = (void *) skb->data;
  2754. hdev->hw_error_code = ev->code;
  2755. queue_work(hdev->req_workqueue, &hdev->error_reset);
  2756. }
  2757. static void hci_role_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2758. {
  2759. struct hci_ev_role_change *ev = (void *) skb->data;
  2760. struct hci_conn *conn;
  2761. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2762. hci_dev_lock(hdev);
  2763. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  2764. if (conn) {
  2765. if (!ev->status)
  2766. conn->role = ev->role;
  2767. clear_bit(HCI_CONN_RSWITCH_PEND, &conn->flags);
  2768. hci_role_switch_cfm(conn, ev->status, ev->role);
  2769. }
  2770. hci_dev_unlock(hdev);
  2771. }
  2772. static void hci_num_comp_pkts_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2773. {
  2774. struct hci_ev_num_comp_pkts *ev = (void *) skb->data;
  2775. int i;
  2776. if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_PACKET_BASED) {
  2777. bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
  2778. return;
  2779. }
  2780. if (skb->len < sizeof(*ev) ||
  2781. skb->len < struct_size(ev, handles, ev->num_hndl)) {
  2782. BT_DBG("%s bad parameters", hdev->name);
  2783. return;
  2784. }
  2785. BT_DBG("%s num_hndl %d", hdev->name, ev->num_hndl);
  2786. for (i = 0; i < ev->num_hndl; i++) {
  2787. struct hci_comp_pkts_info *info = &ev->handles[i];
  2788. struct hci_conn *conn;
  2789. __u16 handle, count;
  2790. handle = __le16_to_cpu(info->handle);
  2791. count = __le16_to_cpu(info->count);
  2792. conn = hci_conn_hash_lookup_handle(hdev, handle);
  2793. if (!conn)
  2794. continue;
  2795. conn->sent -= count;
  2796. switch (conn->type) {
  2797. case ACL_LINK:
  2798. hdev->acl_cnt += count;
  2799. if (hdev->acl_cnt > hdev->acl_pkts)
  2800. hdev->acl_cnt = hdev->acl_pkts;
  2801. break;
  2802. case LE_LINK:
  2803. if (hdev->le_pkts) {
  2804. hdev->le_cnt += count;
  2805. if (hdev->le_cnt > hdev->le_pkts)
  2806. hdev->le_cnt = hdev->le_pkts;
  2807. } else {
  2808. hdev->acl_cnt += count;
  2809. if (hdev->acl_cnt > hdev->acl_pkts)
  2810. hdev->acl_cnt = hdev->acl_pkts;
  2811. }
  2812. break;
  2813. case SCO_LINK:
  2814. hdev->sco_cnt += count;
  2815. if (hdev->sco_cnt > hdev->sco_pkts)
  2816. hdev->sco_cnt = hdev->sco_pkts;
  2817. break;
  2818. default:
  2819. bt_dev_err(hdev, "unknown type %d conn %p",
  2820. conn->type, conn);
  2821. break;
  2822. }
  2823. }
  2824. queue_work(hdev->workqueue, &hdev->tx_work);
  2825. }
  2826. static struct hci_conn *__hci_conn_lookup_handle(struct hci_dev *hdev,
  2827. __u16 handle)
  2828. {
  2829. struct hci_chan *chan;
  2830. switch (hdev->dev_type) {
  2831. case HCI_PRIMARY:
  2832. return hci_conn_hash_lookup_handle(hdev, handle);
  2833. case HCI_AMP:
  2834. chan = hci_chan_lookup_handle(hdev, handle);
  2835. if (chan)
  2836. return chan->conn;
  2837. break;
  2838. default:
  2839. bt_dev_err(hdev, "unknown dev_type %d", hdev->dev_type);
  2840. break;
  2841. }
  2842. return NULL;
  2843. }
  2844. static void hci_num_comp_blocks_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2845. {
  2846. struct hci_ev_num_comp_blocks *ev = (void *) skb->data;
  2847. int i;
  2848. if (hdev->flow_ctl_mode != HCI_FLOW_CTL_MODE_BLOCK_BASED) {
  2849. bt_dev_err(hdev, "wrong event for mode %d", hdev->flow_ctl_mode);
  2850. return;
  2851. }
  2852. if (skb->len < sizeof(*ev) ||
  2853. skb->len < struct_size(ev, handles, ev->num_hndl)) {
  2854. BT_DBG("%s bad parameters", hdev->name);
  2855. return;
  2856. }
  2857. BT_DBG("%s num_blocks %d num_hndl %d", hdev->name, ev->num_blocks,
  2858. ev->num_hndl);
  2859. for (i = 0; i < ev->num_hndl; i++) {
  2860. struct hci_comp_blocks_info *info = &ev->handles[i];
  2861. struct hci_conn *conn = NULL;
  2862. __u16 handle, block_count;
  2863. handle = __le16_to_cpu(info->handle);
  2864. block_count = __le16_to_cpu(info->blocks);
  2865. conn = __hci_conn_lookup_handle(hdev, handle);
  2866. if (!conn)
  2867. continue;
  2868. conn->sent -= block_count;
  2869. switch (conn->type) {
  2870. case ACL_LINK:
  2871. case AMP_LINK:
  2872. hdev->block_cnt += block_count;
  2873. if (hdev->block_cnt > hdev->num_blocks)
  2874. hdev->block_cnt = hdev->num_blocks;
  2875. break;
  2876. default:
  2877. bt_dev_err(hdev, "unknown type %d conn %p",
  2878. conn->type, conn);
  2879. break;
  2880. }
  2881. }
  2882. queue_work(hdev->workqueue, &hdev->tx_work);
  2883. }
  2884. static void hci_mode_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2885. {
  2886. struct hci_ev_mode_change *ev = (void *) skb->data;
  2887. struct hci_conn *conn;
  2888. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  2889. hci_dev_lock(hdev);
  2890. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  2891. if (conn) {
  2892. conn->mode = ev->mode;
  2893. if (!test_and_clear_bit(HCI_CONN_MODE_CHANGE_PEND,
  2894. &conn->flags)) {
  2895. if (conn->mode == HCI_CM_ACTIVE)
  2896. set_bit(HCI_CONN_POWER_SAVE, &conn->flags);
  2897. else
  2898. clear_bit(HCI_CONN_POWER_SAVE, &conn->flags);
  2899. }
  2900. if (test_and_clear_bit(HCI_CONN_SCO_SETUP_PEND, &conn->flags))
  2901. hci_sco_setup(conn, ev->status);
  2902. }
  2903. hci_dev_unlock(hdev);
  2904. }
  2905. static void hci_pin_code_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2906. {
  2907. struct hci_ev_pin_code_req *ev = (void *) skb->data;
  2908. struct hci_conn *conn;
  2909. BT_DBG("%s", hdev->name);
  2910. hci_dev_lock(hdev);
  2911. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  2912. if (!conn)
  2913. goto unlock;
  2914. if (conn->state == BT_CONNECTED) {
  2915. hci_conn_hold(conn);
  2916. conn->disc_timeout = HCI_PAIRING_TIMEOUT;
  2917. hci_conn_drop(conn);
  2918. }
  2919. if (!hci_dev_test_flag(hdev, HCI_BONDABLE) &&
  2920. !test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags)) {
  2921. hci_send_cmd(hdev, HCI_OP_PIN_CODE_NEG_REPLY,
  2922. sizeof(ev->bdaddr), &ev->bdaddr);
  2923. } else if (hci_dev_test_flag(hdev, HCI_MGMT)) {
  2924. u8 secure;
  2925. if (conn->pending_sec_level == BT_SECURITY_HIGH)
  2926. secure = 1;
  2927. else
  2928. secure = 0;
  2929. mgmt_pin_code_request(hdev, &ev->bdaddr, secure);
  2930. }
  2931. unlock:
  2932. hci_dev_unlock(hdev);
  2933. }
  2934. static void conn_set_key(struct hci_conn *conn, u8 key_type, u8 pin_len)
  2935. {
  2936. if (key_type == HCI_LK_CHANGED_COMBINATION)
  2937. return;
  2938. conn->pin_length = pin_len;
  2939. conn->key_type = key_type;
  2940. switch (key_type) {
  2941. case HCI_LK_LOCAL_UNIT:
  2942. case HCI_LK_REMOTE_UNIT:
  2943. case HCI_LK_DEBUG_COMBINATION:
  2944. return;
  2945. case HCI_LK_COMBINATION:
  2946. if (pin_len == 16)
  2947. conn->pending_sec_level = BT_SECURITY_HIGH;
  2948. else
  2949. conn->pending_sec_level = BT_SECURITY_MEDIUM;
  2950. break;
  2951. case HCI_LK_UNAUTH_COMBINATION_P192:
  2952. case HCI_LK_UNAUTH_COMBINATION_P256:
  2953. conn->pending_sec_level = BT_SECURITY_MEDIUM;
  2954. break;
  2955. case HCI_LK_AUTH_COMBINATION_P192:
  2956. conn->pending_sec_level = BT_SECURITY_HIGH;
  2957. break;
  2958. case HCI_LK_AUTH_COMBINATION_P256:
  2959. conn->pending_sec_level = BT_SECURITY_FIPS;
  2960. break;
  2961. }
  2962. }
  2963. static void hci_link_key_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
  2964. {
  2965. struct hci_ev_link_key_req *ev = (void *) skb->data;
  2966. struct hci_cp_link_key_reply cp;
  2967. struct hci_conn *conn;
  2968. struct link_key *key;
  2969. BT_DBG("%s", hdev->name);
  2970. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  2971. return;
  2972. hci_dev_lock(hdev);
  2973. key = hci_find_link_key(hdev, &ev->bdaddr);
  2974. if (!key) {
  2975. BT_DBG("%s link key not found for %pMR", hdev->name,
  2976. &ev->bdaddr);
  2977. goto not_found;
  2978. }
  2979. BT_DBG("%s found key type %u for %pMR", hdev->name, key->type,
  2980. &ev->bdaddr);
  2981. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  2982. if (conn) {
  2983. clear_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
  2984. if ((key->type == HCI_LK_UNAUTH_COMBINATION_P192 ||
  2985. key->type == HCI_LK_UNAUTH_COMBINATION_P256) &&
  2986. conn->auth_type != 0xff && (conn->auth_type & 0x01)) {
  2987. BT_DBG("%s ignoring unauthenticated key", hdev->name);
  2988. goto not_found;
  2989. }
  2990. if (key->type == HCI_LK_COMBINATION && key->pin_len < 16 &&
  2991. (conn->pending_sec_level == BT_SECURITY_HIGH ||
  2992. conn->pending_sec_level == BT_SECURITY_FIPS)) {
  2993. BT_DBG("%s ignoring key unauthenticated for high security",
  2994. hdev->name);
  2995. goto not_found;
  2996. }
  2997. conn_set_key(conn, key->type, key->pin_len);
  2998. }
  2999. bacpy(&cp.bdaddr, &ev->bdaddr);
  3000. memcpy(cp.link_key, key->val, HCI_LINK_KEY_SIZE);
  3001. hci_send_cmd(hdev, HCI_OP_LINK_KEY_REPLY, sizeof(cp), &cp);
  3002. hci_dev_unlock(hdev);
  3003. return;
  3004. not_found:
  3005. hci_send_cmd(hdev, HCI_OP_LINK_KEY_NEG_REPLY, 6, &ev->bdaddr);
  3006. hci_dev_unlock(hdev);
  3007. }
  3008. static void hci_link_key_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3009. {
  3010. struct hci_ev_link_key_notify *ev = (void *) skb->data;
  3011. struct hci_conn *conn;
  3012. struct link_key *key;
  3013. bool persistent;
  3014. u8 pin_len = 0;
  3015. BT_DBG("%s", hdev->name);
  3016. hci_dev_lock(hdev);
  3017. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3018. if (!conn)
  3019. goto unlock;
  3020. hci_conn_hold(conn);
  3021. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  3022. hci_conn_drop(conn);
  3023. set_bit(HCI_CONN_NEW_LINK_KEY, &conn->flags);
  3024. conn_set_key(conn, ev->key_type, conn->pin_length);
  3025. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  3026. goto unlock;
  3027. key = hci_add_link_key(hdev, conn, &ev->bdaddr, ev->link_key,
  3028. ev->key_type, pin_len, &persistent);
  3029. if (!key)
  3030. goto unlock;
  3031. /* Update connection information since adding the key will have
  3032. * fixed up the type in the case of changed combination keys.
  3033. */
  3034. if (ev->key_type == HCI_LK_CHANGED_COMBINATION)
  3035. conn_set_key(conn, key->type, key->pin_len);
  3036. mgmt_new_link_key(hdev, key, persistent);
  3037. /* Keep debug keys around only if the HCI_KEEP_DEBUG_KEYS flag
  3038. * is set. If it's not set simply remove the key from the kernel
  3039. * list (we've still notified user space about it but with
  3040. * store_hint being 0).
  3041. */
  3042. if (key->type == HCI_LK_DEBUG_COMBINATION &&
  3043. !hci_dev_test_flag(hdev, HCI_KEEP_DEBUG_KEYS)) {
  3044. list_del_rcu(&key->list);
  3045. kfree_rcu(key, rcu);
  3046. goto unlock;
  3047. }
  3048. if (persistent)
  3049. clear_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
  3050. else
  3051. set_bit(HCI_CONN_FLUSH_KEY, &conn->flags);
  3052. unlock:
  3053. hci_dev_unlock(hdev);
  3054. }
  3055. static void hci_clock_offset_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3056. {
  3057. struct hci_ev_clock_offset *ev = (void *) skb->data;
  3058. struct hci_conn *conn;
  3059. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3060. hci_dev_lock(hdev);
  3061. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3062. if (conn && !ev->status) {
  3063. struct inquiry_entry *ie;
  3064. ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
  3065. if (ie) {
  3066. ie->data.clock_offset = ev->clock_offset;
  3067. ie->timestamp = jiffies;
  3068. }
  3069. }
  3070. hci_dev_unlock(hdev);
  3071. }
  3072. static void hci_pkt_type_change_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3073. {
  3074. struct hci_ev_pkt_type_change *ev = (void *) skb->data;
  3075. struct hci_conn *conn;
  3076. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3077. hci_dev_lock(hdev);
  3078. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3079. if (conn && !ev->status)
  3080. conn->pkt_type = __le16_to_cpu(ev->pkt_type);
  3081. hci_dev_unlock(hdev);
  3082. }
  3083. static void hci_pscan_rep_mode_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3084. {
  3085. struct hci_ev_pscan_rep_mode *ev = (void *) skb->data;
  3086. struct inquiry_entry *ie;
  3087. BT_DBG("%s", hdev->name);
  3088. hci_dev_lock(hdev);
  3089. ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
  3090. if (ie) {
  3091. ie->data.pscan_rep_mode = ev->pscan_rep_mode;
  3092. ie->timestamp = jiffies;
  3093. }
  3094. hci_dev_unlock(hdev);
  3095. }
  3096. static void hci_inquiry_result_with_rssi_evt(struct hci_dev *hdev,
  3097. struct sk_buff *skb)
  3098. {
  3099. struct inquiry_data data;
  3100. int num_rsp = *((__u8 *) skb->data);
  3101. BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
  3102. if (!num_rsp)
  3103. return;
  3104. if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
  3105. return;
  3106. hci_dev_lock(hdev);
  3107. if ((skb->len - 1) / num_rsp != sizeof(struct inquiry_info_with_rssi)) {
  3108. struct inquiry_info_with_rssi_and_pscan_mode *info;
  3109. info = (void *) (skb->data + 1);
  3110. for (; num_rsp; num_rsp--, info++) {
  3111. u32 flags;
  3112. bacpy(&data.bdaddr, &info->bdaddr);
  3113. data.pscan_rep_mode = info->pscan_rep_mode;
  3114. data.pscan_period_mode = info->pscan_period_mode;
  3115. data.pscan_mode = info->pscan_mode;
  3116. memcpy(data.dev_class, info->dev_class, 3);
  3117. data.clock_offset = info->clock_offset;
  3118. data.rssi = info->rssi;
  3119. data.ssp_mode = 0x00;
  3120. flags = hci_inquiry_cache_update(hdev, &data, false);
  3121. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  3122. info->dev_class, info->rssi,
  3123. flags, NULL, 0, NULL, 0);
  3124. }
  3125. } else {
  3126. struct inquiry_info_with_rssi *info = (void *) (skb->data + 1);
  3127. for (; num_rsp; num_rsp--, info++) {
  3128. u32 flags;
  3129. bacpy(&data.bdaddr, &info->bdaddr);
  3130. data.pscan_rep_mode = info->pscan_rep_mode;
  3131. data.pscan_period_mode = info->pscan_period_mode;
  3132. data.pscan_mode = 0x00;
  3133. memcpy(data.dev_class, info->dev_class, 3);
  3134. data.clock_offset = info->clock_offset;
  3135. data.rssi = info->rssi;
  3136. data.ssp_mode = 0x00;
  3137. flags = hci_inquiry_cache_update(hdev, &data, false);
  3138. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  3139. info->dev_class, info->rssi,
  3140. flags, NULL, 0, NULL, 0);
  3141. }
  3142. }
  3143. hci_dev_unlock(hdev);
  3144. }
  3145. static void hci_remote_ext_features_evt(struct hci_dev *hdev,
  3146. struct sk_buff *skb)
  3147. {
  3148. struct hci_ev_remote_ext_features *ev = (void *) skb->data;
  3149. struct hci_conn *conn;
  3150. BT_DBG("%s", hdev->name);
  3151. hci_dev_lock(hdev);
  3152. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3153. if (!conn)
  3154. goto unlock;
  3155. if (ev->page < HCI_MAX_PAGES)
  3156. memcpy(conn->features[ev->page], ev->features, 8);
  3157. if (!ev->status && ev->page == 0x01) {
  3158. struct inquiry_entry *ie;
  3159. ie = hci_inquiry_cache_lookup(hdev, &conn->dst);
  3160. if (ie)
  3161. ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
  3162. if (ev->features[0] & LMP_HOST_SSP) {
  3163. set_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
  3164. } else {
  3165. /* It is mandatory by the Bluetooth specification that
  3166. * Extended Inquiry Results are only used when Secure
  3167. * Simple Pairing is enabled, but some devices violate
  3168. * this.
  3169. *
  3170. * To make these devices work, the internal SSP
  3171. * enabled flag needs to be cleared if the remote host
  3172. * features do not indicate SSP support */
  3173. clear_bit(HCI_CONN_SSP_ENABLED, &conn->flags);
  3174. }
  3175. if (ev->features[0] & LMP_HOST_SC)
  3176. set_bit(HCI_CONN_SC_ENABLED, &conn->flags);
  3177. }
  3178. if (conn->state != BT_CONFIG)
  3179. goto unlock;
  3180. if (!ev->status && !test_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags)) {
  3181. struct hci_cp_remote_name_req cp;
  3182. memset(&cp, 0, sizeof(cp));
  3183. bacpy(&cp.bdaddr, &conn->dst);
  3184. cp.pscan_rep_mode = 0x02;
  3185. hci_send_cmd(hdev, HCI_OP_REMOTE_NAME_REQ, sizeof(cp), &cp);
  3186. } else if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
  3187. mgmt_device_connected(hdev, conn, 0, NULL, 0);
  3188. if (!hci_outgoing_auth_needed(hdev, conn)) {
  3189. conn->state = BT_CONNECTED;
  3190. hci_connect_cfm(conn, ev->status);
  3191. hci_conn_drop(conn);
  3192. }
  3193. unlock:
  3194. hci_dev_unlock(hdev);
  3195. }
  3196. static void hci_sync_conn_complete_evt(struct hci_dev *hdev,
  3197. struct sk_buff *skb)
  3198. {
  3199. struct hci_ev_sync_conn_complete *ev = (void *) skb->data;
  3200. struct hci_conn *conn;
  3201. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3202. hci_dev_lock(hdev);
  3203. conn = hci_conn_hash_lookup_ba(hdev, ev->link_type, &ev->bdaddr);
  3204. if (!conn) {
  3205. if (ev->link_type == ESCO_LINK)
  3206. goto unlock;
  3207. /* When the link type in the event indicates SCO connection
  3208. * and lookup of the connection object fails, then check
  3209. * if an eSCO connection object exists.
  3210. *
  3211. * The core limits the synchronous connections to either
  3212. * SCO or eSCO. The eSCO connection is preferred and tried
  3213. * to be setup first and until successfully established,
  3214. * the link type will be hinted as eSCO.
  3215. */
  3216. conn = hci_conn_hash_lookup_ba(hdev, ESCO_LINK, &ev->bdaddr);
  3217. if (!conn)
  3218. goto unlock;
  3219. }
  3220. switch (ev->status) {
  3221. case 0x00:
  3222. conn->handle = __le16_to_cpu(ev->handle);
  3223. conn->state = BT_CONNECTED;
  3224. conn->type = ev->link_type;
  3225. hci_debugfs_create_conn(conn);
  3226. hci_conn_add_sysfs(conn);
  3227. break;
  3228. case 0x10: /* Connection Accept Timeout */
  3229. case 0x0d: /* Connection Rejected due to Limited Resources */
  3230. case 0x11: /* Unsupported Feature or Parameter Value */
  3231. case 0x1c: /* SCO interval rejected */
  3232. case 0x1a: /* Unsupported Remote Feature */
  3233. case 0x1f: /* Unspecified error */
  3234. case 0x20: /* Unsupported LMP Parameter value */
  3235. if (conn->out) {
  3236. conn->pkt_type = (hdev->esco_type & SCO_ESCO_MASK) |
  3237. (hdev->esco_type & EDR_ESCO_MASK);
  3238. if (hci_setup_sync(conn, conn->link->handle))
  3239. goto unlock;
  3240. }
  3241. /* fall through */
  3242. default:
  3243. conn->state = BT_CLOSED;
  3244. break;
  3245. }
  3246. hci_connect_cfm(conn, ev->status);
  3247. if (ev->status)
  3248. hci_conn_del(conn);
  3249. unlock:
  3250. hci_dev_unlock(hdev);
  3251. }
  3252. static inline size_t eir_get_length(u8 *eir, size_t eir_len)
  3253. {
  3254. size_t parsed = 0;
  3255. while (parsed < eir_len) {
  3256. u8 field_len = eir[0];
  3257. if (field_len == 0)
  3258. return parsed;
  3259. parsed += field_len + 1;
  3260. eir += field_len + 1;
  3261. }
  3262. return eir_len;
  3263. }
  3264. static void hci_extended_inquiry_result_evt(struct hci_dev *hdev,
  3265. struct sk_buff *skb)
  3266. {
  3267. struct inquiry_data data;
  3268. struct extended_inquiry_info *info = (void *) (skb->data + 1);
  3269. int num_rsp = *((__u8 *) skb->data);
  3270. size_t eir_len;
  3271. BT_DBG("%s num_rsp %d", hdev->name, num_rsp);
  3272. if (!num_rsp)
  3273. return;
  3274. if (hci_dev_test_flag(hdev, HCI_PERIODIC_INQ))
  3275. return;
  3276. hci_dev_lock(hdev);
  3277. for (; num_rsp; num_rsp--, info++) {
  3278. u32 flags;
  3279. bool name_known;
  3280. bacpy(&data.bdaddr, &info->bdaddr);
  3281. data.pscan_rep_mode = info->pscan_rep_mode;
  3282. data.pscan_period_mode = info->pscan_period_mode;
  3283. data.pscan_mode = 0x00;
  3284. memcpy(data.dev_class, info->dev_class, 3);
  3285. data.clock_offset = info->clock_offset;
  3286. data.rssi = info->rssi;
  3287. data.ssp_mode = 0x01;
  3288. if (hci_dev_test_flag(hdev, HCI_MGMT))
  3289. name_known = eir_get_data(info->data,
  3290. sizeof(info->data),
  3291. EIR_NAME_COMPLETE, NULL);
  3292. else
  3293. name_known = true;
  3294. flags = hci_inquiry_cache_update(hdev, &data, name_known);
  3295. eir_len = eir_get_length(info->data, sizeof(info->data));
  3296. mgmt_device_found(hdev, &info->bdaddr, ACL_LINK, 0x00,
  3297. info->dev_class, info->rssi,
  3298. flags, info->data, eir_len, NULL, 0);
  3299. }
  3300. hci_dev_unlock(hdev);
  3301. }
  3302. static void hci_key_refresh_complete_evt(struct hci_dev *hdev,
  3303. struct sk_buff *skb)
  3304. {
  3305. struct hci_ev_key_refresh_complete *ev = (void *) skb->data;
  3306. struct hci_conn *conn;
  3307. BT_DBG("%s status 0x%2.2x handle 0x%4.4x", hdev->name, ev->status,
  3308. __le16_to_cpu(ev->handle));
  3309. hci_dev_lock(hdev);
  3310. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3311. if (!conn)
  3312. goto unlock;
  3313. /* For BR/EDR the necessary steps are taken through the
  3314. * auth_complete event.
  3315. */
  3316. if (conn->type != LE_LINK)
  3317. goto unlock;
  3318. if (!ev->status)
  3319. conn->sec_level = conn->pending_sec_level;
  3320. clear_bit(HCI_CONN_ENCRYPT_PEND, &conn->flags);
  3321. if (ev->status && conn->state == BT_CONNECTED) {
  3322. hci_disconnect(conn, HCI_ERROR_AUTH_FAILURE);
  3323. hci_conn_drop(conn);
  3324. goto unlock;
  3325. }
  3326. if (conn->state == BT_CONFIG) {
  3327. if (!ev->status)
  3328. conn->state = BT_CONNECTED;
  3329. hci_connect_cfm(conn, ev->status);
  3330. hci_conn_drop(conn);
  3331. } else {
  3332. hci_auth_cfm(conn, ev->status);
  3333. hci_conn_hold(conn);
  3334. conn->disc_timeout = HCI_DISCONN_TIMEOUT;
  3335. hci_conn_drop(conn);
  3336. }
  3337. unlock:
  3338. hci_dev_unlock(hdev);
  3339. }
  3340. static u8 hci_get_auth_req(struct hci_conn *conn)
  3341. {
  3342. /* If remote requests no-bonding follow that lead */
  3343. if (conn->remote_auth == HCI_AT_NO_BONDING ||
  3344. conn->remote_auth == HCI_AT_NO_BONDING_MITM)
  3345. return conn->remote_auth | (conn->auth_type & 0x01);
  3346. /* If both remote and local have enough IO capabilities, require
  3347. * MITM protection
  3348. */
  3349. if (conn->remote_cap != HCI_IO_NO_INPUT_OUTPUT &&
  3350. conn->io_capability != HCI_IO_NO_INPUT_OUTPUT)
  3351. return conn->remote_auth | 0x01;
  3352. /* No MITM protection possible so ignore remote requirement */
  3353. return (conn->remote_auth & ~0x01) | (conn->auth_type & 0x01);
  3354. }
  3355. static u8 bredr_oob_data_present(struct hci_conn *conn)
  3356. {
  3357. struct hci_dev *hdev = conn->hdev;
  3358. struct oob_data *data;
  3359. data = hci_find_remote_oob_data(hdev, &conn->dst, BDADDR_BREDR);
  3360. if (!data)
  3361. return 0x00;
  3362. if (bredr_sc_enabled(hdev)) {
  3363. /* When Secure Connections is enabled, then just
  3364. * return the present value stored with the OOB
  3365. * data. The stored value contains the right present
  3366. * information. However it can only be trusted when
  3367. * not in Secure Connection Only mode.
  3368. */
  3369. if (!hci_dev_test_flag(hdev, HCI_SC_ONLY))
  3370. return data->present;
  3371. /* When Secure Connections Only mode is enabled, then
  3372. * the P-256 values are required. If they are not
  3373. * available, then do not declare that OOB data is
  3374. * present.
  3375. */
  3376. if (!memcmp(data->rand256, ZERO_KEY, 16) ||
  3377. !memcmp(data->hash256, ZERO_KEY, 16))
  3378. return 0x00;
  3379. return 0x02;
  3380. }
  3381. /* When Secure Connections is not enabled or actually
  3382. * not supported by the hardware, then check that if
  3383. * P-192 data values are present.
  3384. */
  3385. if (!memcmp(data->rand192, ZERO_KEY, 16) ||
  3386. !memcmp(data->hash192, ZERO_KEY, 16))
  3387. return 0x00;
  3388. return 0x01;
  3389. }
  3390. static void hci_io_capa_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3391. {
  3392. struct hci_ev_io_capa_request *ev = (void *) skb->data;
  3393. struct hci_conn *conn;
  3394. BT_DBG("%s", hdev->name);
  3395. hci_dev_lock(hdev);
  3396. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3397. if (!conn)
  3398. goto unlock;
  3399. hci_conn_hold(conn);
  3400. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  3401. goto unlock;
  3402. /* Allow pairing if we're pairable, the initiators of the
  3403. * pairing or if the remote is not requesting bonding.
  3404. */
  3405. if (hci_dev_test_flag(hdev, HCI_BONDABLE) ||
  3406. test_bit(HCI_CONN_AUTH_INITIATOR, &conn->flags) ||
  3407. (conn->remote_auth & ~0x01) == HCI_AT_NO_BONDING) {
  3408. struct hci_cp_io_capability_reply cp;
  3409. bacpy(&cp.bdaddr, &ev->bdaddr);
  3410. /* Change the IO capability from KeyboardDisplay
  3411. * to DisplayYesNo as it is not supported by BT spec. */
  3412. cp.capability = (conn->io_capability == 0x04) ?
  3413. HCI_IO_DISPLAY_YESNO : conn->io_capability;
  3414. /* If we are initiators, there is no remote information yet */
  3415. if (conn->remote_auth == 0xff) {
  3416. /* Request MITM protection if our IO caps allow it
  3417. * except for the no-bonding case.
  3418. */
  3419. if (conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
  3420. conn->auth_type != HCI_AT_NO_BONDING)
  3421. conn->auth_type |= 0x01;
  3422. } else {
  3423. conn->auth_type = hci_get_auth_req(conn);
  3424. }
  3425. /* If we're not bondable, force one of the non-bondable
  3426. * authentication requirement values.
  3427. */
  3428. if (!hci_dev_test_flag(hdev, HCI_BONDABLE))
  3429. conn->auth_type &= HCI_AT_NO_BONDING_MITM;
  3430. cp.authentication = conn->auth_type;
  3431. cp.oob_data = bredr_oob_data_present(conn);
  3432. hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_REPLY,
  3433. sizeof(cp), &cp);
  3434. } else {
  3435. struct hci_cp_io_capability_neg_reply cp;
  3436. bacpy(&cp.bdaddr, &ev->bdaddr);
  3437. cp.reason = HCI_ERROR_PAIRING_NOT_ALLOWED;
  3438. hci_send_cmd(hdev, HCI_OP_IO_CAPABILITY_NEG_REPLY,
  3439. sizeof(cp), &cp);
  3440. }
  3441. unlock:
  3442. hci_dev_unlock(hdev);
  3443. }
  3444. static void hci_io_capa_reply_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3445. {
  3446. struct hci_ev_io_capa_reply *ev = (void *) skb->data;
  3447. struct hci_conn *conn;
  3448. BT_DBG("%s", hdev->name);
  3449. hci_dev_lock(hdev);
  3450. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3451. if (!conn)
  3452. goto unlock;
  3453. conn->remote_cap = ev->capability;
  3454. conn->remote_auth = ev->authentication;
  3455. unlock:
  3456. hci_dev_unlock(hdev);
  3457. }
  3458. static void hci_user_confirm_request_evt(struct hci_dev *hdev,
  3459. struct sk_buff *skb)
  3460. {
  3461. struct hci_ev_user_confirm_req *ev = (void *) skb->data;
  3462. int loc_mitm, rem_mitm, confirm_hint = 0;
  3463. struct hci_conn *conn;
  3464. BT_DBG("%s", hdev->name);
  3465. hci_dev_lock(hdev);
  3466. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  3467. goto unlock;
  3468. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3469. if (!conn)
  3470. goto unlock;
  3471. loc_mitm = (conn->auth_type & 0x01);
  3472. rem_mitm = (conn->remote_auth & 0x01);
  3473. /* If we require MITM but the remote device can't provide that
  3474. * (it has NoInputNoOutput) then reject the confirmation
  3475. * request. We check the security level here since it doesn't
  3476. * necessarily match conn->auth_type.
  3477. */
  3478. if (conn->pending_sec_level > BT_SECURITY_MEDIUM &&
  3479. conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) {
  3480. BT_DBG("Rejecting request: remote device can't provide MITM");
  3481. hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_NEG_REPLY,
  3482. sizeof(ev->bdaddr), &ev->bdaddr);
  3483. goto unlock;
  3484. }
  3485. /* If no side requires MITM protection; auto-accept */
  3486. if ((!loc_mitm || conn->remote_cap == HCI_IO_NO_INPUT_OUTPUT) &&
  3487. (!rem_mitm || conn->io_capability == HCI_IO_NO_INPUT_OUTPUT)) {
  3488. /* If we're not the initiators request authorization to
  3489. * proceed from user space (mgmt_user_confirm with
  3490. * confirm_hint set to 1). The exception is if neither
  3491. * side had MITM or if the local IO capability is
  3492. * NoInputNoOutput, in which case we do auto-accept
  3493. */
  3494. if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) &&
  3495. conn->io_capability != HCI_IO_NO_INPUT_OUTPUT &&
  3496. (loc_mitm || rem_mitm)) {
  3497. BT_DBG("Confirming auto-accept as acceptor");
  3498. confirm_hint = 1;
  3499. goto confirm;
  3500. }
  3501. /* If there already exists link key in local host, leave the
  3502. * decision to user space since the remote device could be
  3503. * legitimate or malicious.
  3504. */
  3505. if (hci_find_link_key(hdev, &ev->bdaddr)) {
  3506. bt_dev_dbg(hdev, "Local host already has link key");
  3507. confirm_hint = 1;
  3508. goto confirm;
  3509. }
  3510. BT_DBG("Auto-accept of user confirmation with %ums delay",
  3511. hdev->auto_accept_delay);
  3512. if (hdev->auto_accept_delay > 0) {
  3513. int delay = msecs_to_jiffies(hdev->auto_accept_delay);
  3514. queue_delayed_work(conn->hdev->workqueue,
  3515. &conn->auto_accept_work, delay);
  3516. goto unlock;
  3517. }
  3518. hci_send_cmd(hdev, HCI_OP_USER_CONFIRM_REPLY,
  3519. sizeof(ev->bdaddr), &ev->bdaddr);
  3520. goto unlock;
  3521. }
  3522. confirm:
  3523. mgmt_user_confirm_request(hdev, &ev->bdaddr, ACL_LINK, 0,
  3524. le32_to_cpu(ev->passkey), confirm_hint);
  3525. unlock:
  3526. hci_dev_unlock(hdev);
  3527. }
  3528. static void hci_user_passkey_request_evt(struct hci_dev *hdev,
  3529. struct sk_buff *skb)
  3530. {
  3531. struct hci_ev_user_passkey_req *ev = (void *) skb->data;
  3532. BT_DBG("%s", hdev->name);
  3533. if (hci_dev_test_flag(hdev, HCI_MGMT))
  3534. mgmt_user_passkey_request(hdev, &ev->bdaddr, ACL_LINK, 0);
  3535. }
  3536. static void hci_user_passkey_notify_evt(struct hci_dev *hdev,
  3537. struct sk_buff *skb)
  3538. {
  3539. struct hci_ev_user_passkey_notify *ev = (void *) skb->data;
  3540. struct hci_conn *conn;
  3541. BT_DBG("%s", hdev->name);
  3542. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3543. if (!conn)
  3544. return;
  3545. conn->passkey_notify = __le32_to_cpu(ev->passkey);
  3546. conn->passkey_entered = 0;
  3547. if (hci_dev_test_flag(hdev, HCI_MGMT))
  3548. mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
  3549. conn->dst_type, conn->passkey_notify,
  3550. conn->passkey_entered);
  3551. }
  3552. static void hci_keypress_notify_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3553. {
  3554. struct hci_ev_keypress_notify *ev = (void *) skb->data;
  3555. struct hci_conn *conn;
  3556. BT_DBG("%s", hdev->name);
  3557. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3558. if (!conn)
  3559. return;
  3560. switch (ev->type) {
  3561. case HCI_KEYPRESS_STARTED:
  3562. conn->passkey_entered = 0;
  3563. return;
  3564. case HCI_KEYPRESS_ENTERED:
  3565. conn->passkey_entered++;
  3566. break;
  3567. case HCI_KEYPRESS_ERASED:
  3568. conn->passkey_entered--;
  3569. break;
  3570. case HCI_KEYPRESS_CLEARED:
  3571. conn->passkey_entered = 0;
  3572. break;
  3573. case HCI_KEYPRESS_COMPLETED:
  3574. return;
  3575. }
  3576. if (hci_dev_test_flag(hdev, HCI_MGMT))
  3577. mgmt_user_passkey_notify(hdev, &conn->dst, conn->type,
  3578. conn->dst_type, conn->passkey_notify,
  3579. conn->passkey_entered);
  3580. }
  3581. static void hci_simple_pair_complete_evt(struct hci_dev *hdev,
  3582. struct sk_buff *skb)
  3583. {
  3584. struct hci_ev_simple_pair_complete *ev = (void *) skb->data;
  3585. struct hci_conn *conn;
  3586. BT_DBG("%s", hdev->name);
  3587. hci_dev_lock(hdev);
  3588. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3589. if (!conn)
  3590. goto unlock;
  3591. /* Reset the authentication requirement to unknown */
  3592. conn->remote_auth = 0xff;
  3593. /* To avoid duplicate auth_failed events to user space we check
  3594. * the HCI_CONN_AUTH_PEND flag which will be set if we
  3595. * initiated the authentication. A traditional auth_complete
  3596. * event gets always produced as initiator and is also mapped to
  3597. * the mgmt_auth_failed event */
  3598. if (!test_bit(HCI_CONN_AUTH_PEND, &conn->flags) && ev->status)
  3599. mgmt_auth_failed(conn, ev->status);
  3600. hci_conn_drop(conn);
  3601. unlock:
  3602. hci_dev_unlock(hdev);
  3603. }
  3604. static void hci_remote_host_features_evt(struct hci_dev *hdev,
  3605. struct sk_buff *skb)
  3606. {
  3607. struct hci_ev_remote_host_features *ev = (void *) skb->data;
  3608. struct inquiry_entry *ie;
  3609. struct hci_conn *conn;
  3610. BT_DBG("%s", hdev->name);
  3611. hci_dev_lock(hdev);
  3612. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, &ev->bdaddr);
  3613. if (conn)
  3614. memcpy(conn->features[1], ev->features, 8);
  3615. ie = hci_inquiry_cache_lookup(hdev, &ev->bdaddr);
  3616. if (ie)
  3617. ie->data.ssp_mode = (ev->features[0] & LMP_HOST_SSP);
  3618. hci_dev_unlock(hdev);
  3619. }
  3620. static void hci_remote_oob_data_request_evt(struct hci_dev *hdev,
  3621. struct sk_buff *skb)
  3622. {
  3623. struct hci_ev_remote_oob_data_request *ev = (void *) skb->data;
  3624. struct oob_data *data;
  3625. BT_DBG("%s", hdev->name);
  3626. hci_dev_lock(hdev);
  3627. if (!hci_dev_test_flag(hdev, HCI_MGMT))
  3628. goto unlock;
  3629. data = hci_find_remote_oob_data(hdev, &ev->bdaddr, BDADDR_BREDR);
  3630. if (!data) {
  3631. struct hci_cp_remote_oob_data_neg_reply cp;
  3632. bacpy(&cp.bdaddr, &ev->bdaddr);
  3633. hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_NEG_REPLY,
  3634. sizeof(cp), &cp);
  3635. goto unlock;
  3636. }
  3637. if (bredr_sc_enabled(hdev)) {
  3638. struct hci_cp_remote_oob_ext_data_reply cp;
  3639. bacpy(&cp.bdaddr, &ev->bdaddr);
  3640. if (hci_dev_test_flag(hdev, HCI_SC_ONLY)) {
  3641. memset(cp.hash192, 0, sizeof(cp.hash192));
  3642. memset(cp.rand192, 0, sizeof(cp.rand192));
  3643. } else {
  3644. memcpy(cp.hash192, data->hash192, sizeof(cp.hash192));
  3645. memcpy(cp.rand192, data->rand192, sizeof(cp.rand192));
  3646. }
  3647. memcpy(cp.hash256, data->hash256, sizeof(cp.hash256));
  3648. memcpy(cp.rand256, data->rand256, sizeof(cp.rand256));
  3649. hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_EXT_DATA_REPLY,
  3650. sizeof(cp), &cp);
  3651. } else {
  3652. struct hci_cp_remote_oob_data_reply cp;
  3653. bacpy(&cp.bdaddr, &ev->bdaddr);
  3654. memcpy(cp.hash, data->hash192, sizeof(cp.hash));
  3655. memcpy(cp.rand, data->rand192, sizeof(cp.rand));
  3656. hci_send_cmd(hdev, HCI_OP_REMOTE_OOB_DATA_REPLY,
  3657. sizeof(cp), &cp);
  3658. }
  3659. unlock:
  3660. hci_dev_unlock(hdev);
  3661. }
  3662. #if IS_ENABLED(CONFIG_BT_HS)
  3663. static void hci_chan_selected_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3664. {
  3665. struct hci_ev_channel_selected *ev = (void *)skb->data;
  3666. struct hci_conn *hcon;
  3667. BT_DBG("%s handle 0x%2.2x", hdev->name, ev->phy_handle);
  3668. skb_pull(skb, sizeof(*ev));
  3669. hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
  3670. if (!hcon)
  3671. return;
  3672. amp_read_loc_assoc_final_data(hdev, hcon);
  3673. }
  3674. static void hci_phy_link_complete_evt(struct hci_dev *hdev,
  3675. struct sk_buff *skb)
  3676. {
  3677. struct hci_ev_phy_link_complete *ev = (void *) skb->data;
  3678. struct hci_conn *hcon, *bredr_hcon;
  3679. BT_DBG("%s handle 0x%2.2x status 0x%2.2x", hdev->name, ev->phy_handle,
  3680. ev->status);
  3681. hci_dev_lock(hdev);
  3682. hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
  3683. if (!hcon) {
  3684. hci_dev_unlock(hdev);
  3685. return;
  3686. }
  3687. if (ev->status) {
  3688. hci_conn_del(hcon);
  3689. hci_dev_unlock(hdev);
  3690. return;
  3691. }
  3692. bredr_hcon = hcon->amp_mgr->l2cap_conn->hcon;
  3693. hcon->state = BT_CONNECTED;
  3694. bacpy(&hcon->dst, &bredr_hcon->dst);
  3695. hci_conn_hold(hcon);
  3696. hcon->disc_timeout = HCI_DISCONN_TIMEOUT;
  3697. hci_conn_drop(hcon);
  3698. hci_debugfs_create_conn(hcon);
  3699. hci_conn_add_sysfs(hcon);
  3700. amp_physical_cfm(bredr_hcon, hcon);
  3701. hci_dev_unlock(hdev);
  3702. }
  3703. static void hci_loglink_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3704. {
  3705. struct hci_ev_logical_link_complete *ev = (void *) skb->data;
  3706. struct hci_conn *hcon;
  3707. struct hci_chan *hchan;
  3708. struct amp_mgr *mgr;
  3709. BT_DBG("%s log_handle 0x%4.4x phy_handle 0x%2.2x status 0x%2.2x",
  3710. hdev->name, le16_to_cpu(ev->handle), ev->phy_handle,
  3711. ev->status);
  3712. hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
  3713. if (!hcon)
  3714. return;
  3715. /* Create AMP hchan */
  3716. hchan = hci_chan_create(hcon);
  3717. if (!hchan)
  3718. return;
  3719. hchan->handle = le16_to_cpu(ev->handle);
  3720. BT_DBG("hcon %p mgr %p hchan %p", hcon, hcon->amp_mgr, hchan);
  3721. mgr = hcon->amp_mgr;
  3722. if (mgr && mgr->bredr_chan) {
  3723. struct l2cap_chan *bredr_chan = mgr->bredr_chan;
  3724. l2cap_chan_lock(bredr_chan);
  3725. bredr_chan->conn->mtu = hdev->block_mtu;
  3726. l2cap_logical_cfm(bredr_chan, hchan, 0);
  3727. hci_conn_hold(hcon);
  3728. l2cap_chan_unlock(bredr_chan);
  3729. }
  3730. }
  3731. static void hci_disconn_loglink_complete_evt(struct hci_dev *hdev,
  3732. struct sk_buff *skb)
  3733. {
  3734. struct hci_ev_disconn_logical_link_complete *ev = (void *) skb->data;
  3735. struct hci_chan *hchan;
  3736. BT_DBG("%s log handle 0x%4.4x status 0x%2.2x", hdev->name,
  3737. le16_to_cpu(ev->handle), ev->status);
  3738. if (ev->status)
  3739. return;
  3740. hci_dev_lock(hdev);
  3741. hchan = hci_chan_lookup_handle(hdev, le16_to_cpu(ev->handle));
  3742. if (!hchan)
  3743. goto unlock;
  3744. amp_destroy_logical_link(hchan, ev->reason);
  3745. unlock:
  3746. hci_dev_unlock(hdev);
  3747. }
  3748. static void hci_disconn_phylink_complete_evt(struct hci_dev *hdev,
  3749. struct sk_buff *skb)
  3750. {
  3751. struct hci_ev_disconn_phy_link_complete *ev = (void *) skb->data;
  3752. struct hci_conn *hcon;
  3753. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3754. if (ev->status)
  3755. return;
  3756. hci_dev_lock(hdev);
  3757. hcon = hci_conn_hash_lookup_handle(hdev, ev->phy_handle);
  3758. if (hcon) {
  3759. hcon->state = BT_CLOSED;
  3760. hci_conn_del(hcon);
  3761. }
  3762. hci_dev_unlock(hdev);
  3763. }
  3764. #endif
  3765. static void le_conn_complete_evt(struct hci_dev *hdev, u8 status,
  3766. bdaddr_t *bdaddr, u8 bdaddr_type, u8 role, u16 handle,
  3767. u16 interval, u16 latency, u16 supervision_timeout)
  3768. {
  3769. struct hci_conn_params *params;
  3770. struct hci_conn *conn;
  3771. struct smp_irk *irk;
  3772. u8 addr_type;
  3773. hci_dev_lock(hdev);
  3774. /* All controllers implicitly stop advertising in the event of a
  3775. * connection, so ensure that the state bit is cleared.
  3776. */
  3777. hci_dev_clear_flag(hdev, HCI_LE_ADV);
  3778. conn = hci_lookup_le_connect(hdev);
  3779. if (!conn) {
  3780. conn = hci_conn_add(hdev, LE_LINK, bdaddr, role);
  3781. if (!conn) {
  3782. bt_dev_err(hdev, "no memory for new connection");
  3783. goto unlock;
  3784. }
  3785. conn->dst_type = bdaddr_type;
  3786. /* If we didn't have a hci_conn object previously
  3787. * but we're in master role this must be something
  3788. * initiated using a white list. Since white list based
  3789. * connections are not "first class citizens" we don't
  3790. * have full tracking of them. Therefore, we go ahead
  3791. * with a "best effort" approach of determining the
  3792. * initiator address based on the HCI_PRIVACY flag.
  3793. */
  3794. if (conn->out) {
  3795. conn->resp_addr_type = bdaddr_type;
  3796. bacpy(&conn->resp_addr, bdaddr);
  3797. if (hci_dev_test_flag(hdev, HCI_PRIVACY)) {
  3798. conn->init_addr_type = ADDR_LE_DEV_RANDOM;
  3799. bacpy(&conn->init_addr, &hdev->rpa);
  3800. } else {
  3801. hci_copy_identity_address(hdev,
  3802. &conn->init_addr,
  3803. &conn->init_addr_type);
  3804. }
  3805. }
  3806. } else {
  3807. cancel_delayed_work(&conn->le_conn_timeout);
  3808. }
  3809. if (!conn->out) {
  3810. /* Set the responder (our side) address type based on
  3811. * the advertising address type.
  3812. */
  3813. conn->resp_addr_type = hdev->adv_addr_type;
  3814. if (hdev->adv_addr_type == ADDR_LE_DEV_RANDOM) {
  3815. /* In case of ext adv, resp_addr will be updated in
  3816. * Adv Terminated event.
  3817. */
  3818. if (!ext_adv_capable(hdev))
  3819. bacpy(&conn->resp_addr, &hdev->random_addr);
  3820. } else {
  3821. bacpy(&conn->resp_addr, &hdev->bdaddr);
  3822. }
  3823. conn->init_addr_type = bdaddr_type;
  3824. bacpy(&conn->init_addr, bdaddr);
  3825. /* For incoming connections, set the default minimum
  3826. * and maximum connection interval. They will be used
  3827. * to check if the parameters are in range and if not
  3828. * trigger the connection update procedure.
  3829. */
  3830. conn->le_conn_min_interval = hdev->le_conn_min_interval;
  3831. conn->le_conn_max_interval = hdev->le_conn_max_interval;
  3832. }
  3833. /* Lookup the identity address from the stored connection
  3834. * address and address type.
  3835. *
  3836. * When establishing connections to an identity address, the
  3837. * connection procedure will store the resolvable random
  3838. * address first. Now if it can be converted back into the
  3839. * identity address, start using the identity address from
  3840. * now on.
  3841. */
  3842. irk = hci_get_irk(hdev, &conn->dst, conn->dst_type);
  3843. if (irk) {
  3844. bacpy(&conn->dst, &irk->bdaddr);
  3845. conn->dst_type = irk->addr_type;
  3846. }
  3847. if (status) {
  3848. hci_le_conn_failed(conn, status);
  3849. goto unlock;
  3850. }
  3851. if (conn->dst_type == ADDR_LE_DEV_PUBLIC)
  3852. addr_type = BDADDR_LE_PUBLIC;
  3853. else
  3854. addr_type = BDADDR_LE_RANDOM;
  3855. /* Drop the connection if the device is blocked */
  3856. if (hci_bdaddr_list_lookup(&hdev->blacklist, &conn->dst, addr_type)) {
  3857. hci_conn_drop(conn);
  3858. goto unlock;
  3859. }
  3860. if (!test_and_set_bit(HCI_CONN_MGMT_CONNECTED, &conn->flags))
  3861. mgmt_device_connected(hdev, conn, 0, NULL, 0);
  3862. conn->sec_level = BT_SECURITY_LOW;
  3863. conn->handle = handle;
  3864. conn->state = BT_CONFIG;
  3865. conn->le_conn_interval = interval;
  3866. conn->le_conn_latency = latency;
  3867. conn->le_supv_timeout = supervision_timeout;
  3868. hci_debugfs_create_conn(conn);
  3869. hci_conn_add_sysfs(conn);
  3870. /* The remote features procedure is defined for master
  3871. * role only. So only in case of an initiated connection
  3872. * request the remote features.
  3873. *
  3874. * If the local controller supports slave-initiated features
  3875. * exchange, then requesting the remote features in slave
  3876. * role is possible. Otherwise just transition into the
  3877. * connected state without requesting the remote features.
  3878. */
  3879. if (conn->out ||
  3880. (hdev->le_features[0] & HCI_LE_SLAVE_FEATURES)) {
  3881. struct hci_cp_le_read_remote_features cp;
  3882. cp.handle = __cpu_to_le16(conn->handle);
  3883. hci_send_cmd(hdev, HCI_OP_LE_READ_REMOTE_FEATURES,
  3884. sizeof(cp), &cp);
  3885. hci_conn_hold(conn);
  3886. } else {
  3887. conn->state = BT_CONNECTED;
  3888. hci_connect_cfm(conn, status);
  3889. }
  3890. params = hci_pend_le_action_lookup(&hdev->pend_le_conns, &conn->dst,
  3891. conn->dst_type);
  3892. if (params) {
  3893. list_del_init(&params->action);
  3894. if (params->conn) {
  3895. hci_conn_drop(params->conn);
  3896. hci_conn_put(params->conn);
  3897. params->conn = NULL;
  3898. }
  3899. }
  3900. unlock:
  3901. hci_update_background_scan(hdev);
  3902. hci_dev_unlock(hdev);
  3903. }
  3904. static void hci_le_conn_complete_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3905. {
  3906. struct hci_ev_le_conn_complete *ev = (void *) skb->data;
  3907. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3908. le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
  3909. ev->role, le16_to_cpu(ev->handle),
  3910. le16_to_cpu(ev->interval),
  3911. le16_to_cpu(ev->latency),
  3912. le16_to_cpu(ev->supervision_timeout));
  3913. }
  3914. static void hci_le_enh_conn_complete_evt(struct hci_dev *hdev,
  3915. struct sk_buff *skb)
  3916. {
  3917. struct hci_ev_le_enh_conn_complete *ev = (void *) skb->data;
  3918. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3919. le_conn_complete_evt(hdev, ev->status, &ev->bdaddr, ev->bdaddr_type,
  3920. ev->role, le16_to_cpu(ev->handle),
  3921. le16_to_cpu(ev->interval),
  3922. le16_to_cpu(ev->latency),
  3923. le16_to_cpu(ev->supervision_timeout));
  3924. }
  3925. static void hci_le_ext_adv_term_evt(struct hci_dev *hdev, struct sk_buff *skb)
  3926. {
  3927. struct hci_evt_le_ext_adv_set_term *ev = (void *) skb->data;
  3928. struct hci_conn *conn;
  3929. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3930. if (ev->status)
  3931. return;
  3932. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->conn_handle));
  3933. if (conn) {
  3934. struct adv_info *adv_instance;
  3935. if (hdev->adv_addr_type != ADDR_LE_DEV_RANDOM)
  3936. return;
  3937. if (!hdev->cur_adv_instance) {
  3938. bacpy(&conn->resp_addr, &hdev->random_addr);
  3939. return;
  3940. }
  3941. adv_instance = hci_find_adv_instance(hdev, hdev->cur_adv_instance);
  3942. if (adv_instance)
  3943. bacpy(&conn->resp_addr, &adv_instance->random_addr);
  3944. }
  3945. }
  3946. static void hci_le_conn_update_complete_evt(struct hci_dev *hdev,
  3947. struct sk_buff *skb)
  3948. {
  3949. struct hci_ev_le_conn_update_complete *ev = (void *) skb->data;
  3950. struct hci_conn *conn;
  3951. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  3952. if (ev->status)
  3953. return;
  3954. hci_dev_lock(hdev);
  3955. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  3956. if (conn) {
  3957. conn->le_conn_interval = le16_to_cpu(ev->interval);
  3958. conn->le_conn_latency = le16_to_cpu(ev->latency);
  3959. conn->le_supv_timeout = le16_to_cpu(ev->supervision_timeout);
  3960. }
  3961. hci_dev_unlock(hdev);
  3962. }
  3963. /* This function requires the caller holds hdev->lock */
  3964. static struct hci_conn *check_pending_le_conn(struct hci_dev *hdev,
  3965. bdaddr_t *addr,
  3966. u8 addr_type, u8 adv_type,
  3967. bdaddr_t *direct_rpa)
  3968. {
  3969. struct hci_conn *conn;
  3970. struct hci_conn_params *params;
  3971. /* If the event is not connectable don't proceed further */
  3972. if (adv_type != LE_ADV_IND && adv_type != LE_ADV_DIRECT_IND)
  3973. return NULL;
  3974. /* Ignore if the device is blocked */
  3975. if (hci_bdaddr_list_lookup(&hdev->blacklist, addr, addr_type))
  3976. return NULL;
  3977. /* Most controller will fail if we try to create new connections
  3978. * while we have an existing one in slave role.
  3979. */
  3980. if (hdev->conn_hash.le_num_slave > 0)
  3981. return NULL;
  3982. /* If we're not connectable only connect devices that we have in
  3983. * our pend_le_conns list.
  3984. */
  3985. params = hci_pend_le_action_lookup(&hdev->pend_le_conns, addr,
  3986. addr_type);
  3987. if (!params)
  3988. return NULL;
  3989. if (!params->explicit_connect) {
  3990. switch (params->auto_connect) {
  3991. case HCI_AUTO_CONN_DIRECT:
  3992. /* Only devices advertising with ADV_DIRECT_IND are
  3993. * triggering a connection attempt. This is allowing
  3994. * incoming connections from slave devices.
  3995. */
  3996. if (adv_type != LE_ADV_DIRECT_IND)
  3997. return NULL;
  3998. break;
  3999. case HCI_AUTO_CONN_ALWAYS:
  4000. /* Devices advertising with ADV_IND or ADV_DIRECT_IND
  4001. * are triggering a connection attempt. This means
  4002. * that incoming connectioms from slave device are
  4003. * accepted and also outgoing connections to slave
  4004. * devices are established when found.
  4005. */
  4006. break;
  4007. default:
  4008. return NULL;
  4009. }
  4010. }
  4011. conn = hci_connect_le(hdev, addr, addr_type, BT_SECURITY_LOW,
  4012. HCI_LE_AUTOCONN_TIMEOUT, HCI_ROLE_MASTER,
  4013. direct_rpa);
  4014. if (!IS_ERR(conn)) {
  4015. /* If HCI_AUTO_CONN_EXPLICIT is set, conn is already owned
  4016. * by higher layer that tried to connect, if no then
  4017. * store the pointer since we don't really have any
  4018. * other owner of the object besides the params that
  4019. * triggered it. This way we can abort the connection if
  4020. * the parameters get removed and keep the reference
  4021. * count consistent once the connection is established.
  4022. */
  4023. if (!params->explicit_connect)
  4024. params->conn = hci_conn_get(conn);
  4025. return conn;
  4026. }
  4027. switch (PTR_ERR(conn)) {
  4028. case -EBUSY:
  4029. /* If hci_connect() returns -EBUSY it means there is already
  4030. * an LE connection attempt going on. Since controllers don't
  4031. * support more than one connection attempt at the time, we
  4032. * don't consider this an error case.
  4033. */
  4034. break;
  4035. default:
  4036. BT_DBG("Failed to connect: err %ld", PTR_ERR(conn));
  4037. return NULL;
  4038. }
  4039. return NULL;
  4040. }
  4041. static void process_adv_report(struct hci_dev *hdev, u8 type, bdaddr_t *bdaddr,
  4042. u8 bdaddr_type, bdaddr_t *direct_addr,
  4043. u8 direct_addr_type, s8 rssi, u8 *data, u8 len)
  4044. {
  4045. struct discovery_state *d = &hdev->discovery;
  4046. struct smp_irk *irk;
  4047. struct hci_conn *conn;
  4048. bool match;
  4049. u32 flags;
  4050. u8 *ptr, real_len;
  4051. switch (type) {
  4052. case LE_ADV_IND:
  4053. case LE_ADV_DIRECT_IND:
  4054. case LE_ADV_SCAN_IND:
  4055. case LE_ADV_NONCONN_IND:
  4056. case LE_ADV_SCAN_RSP:
  4057. break;
  4058. default:
  4059. bt_dev_err_ratelimited(hdev, "unknown advertising packet "
  4060. "type: 0x%02x", type);
  4061. return;
  4062. }
  4063. /* Find the end of the data in case the report contains padded zero
  4064. * bytes at the end causing an invalid length value.
  4065. *
  4066. * When data is NULL, len is 0 so there is no need for extra ptr
  4067. * check as 'ptr < data + 0' is already false in such case.
  4068. */
  4069. for (ptr = data; ptr < data + len && *ptr; ptr += *ptr + 1) {
  4070. if (ptr + 1 + *ptr > data + len)
  4071. break;
  4072. }
  4073. real_len = ptr - data;
  4074. /* Adjust for actual length */
  4075. if (len != real_len) {
  4076. bt_dev_err_ratelimited(hdev, "advertising data len corrected");
  4077. len = real_len;
  4078. }
  4079. /* If the direct address is present, then this report is from
  4080. * a LE Direct Advertising Report event. In that case it is
  4081. * important to see if the address is matching the local
  4082. * controller address.
  4083. */
  4084. if (direct_addr) {
  4085. /* Only resolvable random addresses are valid for these
  4086. * kind of reports and others can be ignored.
  4087. */
  4088. if (!hci_bdaddr_is_rpa(direct_addr, direct_addr_type))
  4089. return;
  4090. /* If the controller is not using resolvable random
  4091. * addresses, then this report can be ignored.
  4092. */
  4093. if (!hci_dev_test_flag(hdev, HCI_PRIVACY))
  4094. return;
  4095. /* If the local IRK of the controller does not match
  4096. * with the resolvable random address provided, then
  4097. * this report can be ignored.
  4098. */
  4099. if (!smp_irk_matches(hdev, hdev->irk, direct_addr))
  4100. return;
  4101. }
  4102. /* Check if we need to convert to identity address */
  4103. irk = hci_get_irk(hdev, bdaddr, bdaddr_type);
  4104. if (irk) {
  4105. bdaddr = &irk->bdaddr;
  4106. bdaddr_type = irk->addr_type;
  4107. }
  4108. /* Check if we have been requested to connect to this device.
  4109. *
  4110. * direct_addr is set only for directed advertising reports (it is NULL
  4111. * for advertising reports) and is already verified to be RPA above.
  4112. */
  4113. conn = check_pending_le_conn(hdev, bdaddr, bdaddr_type, type,
  4114. direct_addr);
  4115. if (conn && type == LE_ADV_IND) {
  4116. /* Store report for later inclusion by
  4117. * mgmt_device_connected
  4118. */
  4119. memcpy(conn->le_adv_data, data, len);
  4120. conn->le_adv_data_len = len;
  4121. }
  4122. /* Passive scanning shouldn't trigger any device found events,
  4123. * except for devices marked as CONN_REPORT for which we do send
  4124. * device found events.
  4125. */
  4126. if (hdev->le_scan_type == LE_SCAN_PASSIVE) {
  4127. if (type == LE_ADV_DIRECT_IND)
  4128. return;
  4129. if (!hci_pend_le_action_lookup(&hdev->pend_le_reports,
  4130. bdaddr, bdaddr_type))
  4131. return;
  4132. if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND)
  4133. flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
  4134. else
  4135. flags = 0;
  4136. mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
  4137. rssi, flags, data, len, NULL, 0);
  4138. return;
  4139. }
  4140. /* When receiving non-connectable or scannable undirected
  4141. * advertising reports, this means that the remote device is
  4142. * not connectable and then clearly indicate this in the
  4143. * device found event.
  4144. *
  4145. * When receiving a scan response, then there is no way to
  4146. * know if the remote device is connectable or not. However
  4147. * since scan responses are merged with a previously seen
  4148. * advertising report, the flags field from that report
  4149. * will be used.
  4150. *
  4151. * In the really unlikely case that a controller get confused
  4152. * and just sends a scan response event, then it is marked as
  4153. * not connectable as well.
  4154. */
  4155. if (type == LE_ADV_NONCONN_IND || type == LE_ADV_SCAN_IND ||
  4156. type == LE_ADV_SCAN_RSP)
  4157. flags = MGMT_DEV_FOUND_NOT_CONNECTABLE;
  4158. else
  4159. flags = 0;
  4160. /* If there's nothing pending either store the data from this
  4161. * event or send an immediate device found event if the data
  4162. * should not be stored for later.
  4163. */
  4164. if (!has_pending_adv_report(hdev)) {
  4165. /* If the report will trigger a SCAN_REQ store it for
  4166. * later merging.
  4167. */
  4168. if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
  4169. store_pending_adv_report(hdev, bdaddr, bdaddr_type,
  4170. rssi, flags, data, len);
  4171. return;
  4172. }
  4173. mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
  4174. rssi, flags, data, len, NULL, 0);
  4175. return;
  4176. }
  4177. /* Check if the pending report is for the same device as the new one */
  4178. match = (!bacmp(bdaddr, &d->last_adv_addr) &&
  4179. bdaddr_type == d->last_adv_addr_type);
  4180. /* If the pending data doesn't match this report or this isn't a
  4181. * scan response (e.g. we got a duplicate ADV_IND) then force
  4182. * sending of the pending data.
  4183. */
  4184. if (type != LE_ADV_SCAN_RSP || !match) {
  4185. /* Send out whatever is in the cache, but skip duplicates */
  4186. if (!match)
  4187. mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
  4188. d->last_adv_addr_type, NULL,
  4189. d->last_adv_rssi, d->last_adv_flags,
  4190. d->last_adv_data,
  4191. d->last_adv_data_len, NULL, 0);
  4192. /* If the new report will trigger a SCAN_REQ store it for
  4193. * later merging.
  4194. */
  4195. if (type == LE_ADV_IND || type == LE_ADV_SCAN_IND) {
  4196. store_pending_adv_report(hdev, bdaddr, bdaddr_type,
  4197. rssi, flags, data, len);
  4198. return;
  4199. }
  4200. /* The advertising reports cannot be merged, so clear
  4201. * the pending report and send out a device found event.
  4202. */
  4203. clear_pending_adv_report(hdev);
  4204. mgmt_device_found(hdev, bdaddr, LE_LINK, bdaddr_type, NULL,
  4205. rssi, flags, data, len, NULL, 0);
  4206. return;
  4207. }
  4208. /* If we get here we've got a pending ADV_IND or ADV_SCAN_IND and
  4209. * the new event is a SCAN_RSP. We can therefore proceed with
  4210. * sending a merged device found event.
  4211. */
  4212. mgmt_device_found(hdev, &d->last_adv_addr, LE_LINK,
  4213. d->last_adv_addr_type, NULL, rssi, d->last_adv_flags,
  4214. d->last_adv_data, d->last_adv_data_len, data, len);
  4215. clear_pending_adv_report(hdev);
  4216. }
  4217. static void hci_le_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
  4218. {
  4219. u8 num_reports = skb->data[0];
  4220. void *ptr = &skb->data[1];
  4221. hci_dev_lock(hdev);
  4222. while (num_reports--) {
  4223. struct hci_ev_le_advertising_info *ev = ptr;
  4224. s8 rssi;
  4225. if (ev->length <= HCI_MAX_AD_LENGTH) {
  4226. rssi = ev->data[ev->length];
  4227. process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
  4228. ev->bdaddr_type, NULL, 0, rssi,
  4229. ev->data, ev->length);
  4230. } else {
  4231. bt_dev_err(hdev, "Dropping invalid advertising data");
  4232. }
  4233. ptr += sizeof(*ev) + ev->length + 1;
  4234. }
  4235. hci_dev_unlock(hdev);
  4236. }
  4237. static u8 ext_evt_type_to_legacy(struct hci_dev *hdev, u16 evt_type)
  4238. {
  4239. if (evt_type & LE_EXT_ADV_LEGACY_PDU) {
  4240. switch (evt_type) {
  4241. case LE_LEGACY_ADV_IND:
  4242. return LE_ADV_IND;
  4243. case LE_LEGACY_ADV_DIRECT_IND:
  4244. return LE_ADV_DIRECT_IND;
  4245. case LE_LEGACY_ADV_SCAN_IND:
  4246. return LE_ADV_SCAN_IND;
  4247. case LE_LEGACY_NONCONN_IND:
  4248. return LE_ADV_NONCONN_IND;
  4249. case LE_LEGACY_SCAN_RSP_ADV:
  4250. case LE_LEGACY_SCAN_RSP_ADV_SCAN:
  4251. return LE_ADV_SCAN_RSP;
  4252. }
  4253. goto invalid;
  4254. }
  4255. if (evt_type & LE_EXT_ADV_CONN_IND) {
  4256. if (evt_type & LE_EXT_ADV_DIRECT_IND)
  4257. return LE_ADV_DIRECT_IND;
  4258. return LE_ADV_IND;
  4259. }
  4260. if (evt_type & LE_EXT_ADV_SCAN_RSP)
  4261. return LE_ADV_SCAN_RSP;
  4262. if (evt_type & LE_EXT_ADV_SCAN_IND)
  4263. return LE_ADV_SCAN_IND;
  4264. if (evt_type == LE_EXT_ADV_NON_CONN_IND ||
  4265. evt_type & LE_EXT_ADV_DIRECT_IND)
  4266. return LE_ADV_NONCONN_IND;
  4267. invalid:
  4268. bt_dev_err_ratelimited(hdev, "Unknown advertising packet type: 0x%02x",
  4269. evt_type);
  4270. return LE_ADV_INVALID;
  4271. }
  4272. static void hci_le_ext_adv_report_evt(struct hci_dev *hdev, struct sk_buff *skb)
  4273. {
  4274. u8 num_reports = skb->data[0];
  4275. void *ptr = &skb->data[1];
  4276. hci_dev_lock(hdev);
  4277. while (num_reports--) {
  4278. struct hci_ev_le_ext_adv_report *ev = ptr;
  4279. u8 legacy_evt_type;
  4280. u16 evt_type;
  4281. evt_type = __le16_to_cpu(ev->evt_type);
  4282. legacy_evt_type = ext_evt_type_to_legacy(hdev, evt_type);
  4283. if (legacy_evt_type != LE_ADV_INVALID) {
  4284. process_adv_report(hdev, legacy_evt_type, &ev->bdaddr,
  4285. ev->bdaddr_type, NULL, 0, ev->rssi,
  4286. ev->data, ev->length);
  4287. }
  4288. ptr += sizeof(*ev) + ev->length;
  4289. }
  4290. hci_dev_unlock(hdev);
  4291. }
  4292. static void hci_le_remote_feat_complete_evt(struct hci_dev *hdev,
  4293. struct sk_buff *skb)
  4294. {
  4295. struct hci_ev_le_remote_feat_complete *ev = (void *)skb->data;
  4296. struct hci_conn *conn;
  4297. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  4298. hci_dev_lock(hdev);
  4299. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  4300. if (conn) {
  4301. if (!ev->status)
  4302. memcpy(conn->features[0], ev->features, 8);
  4303. if (conn->state == BT_CONFIG) {
  4304. __u8 status;
  4305. /* If the local controller supports slave-initiated
  4306. * features exchange, but the remote controller does
  4307. * not, then it is possible that the error code 0x1a
  4308. * for unsupported remote feature gets returned.
  4309. *
  4310. * In this specific case, allow the connection to
  4311. * transition into connected state and mark it as
  4312. * successful.
  4313. */
  4314. if ((hdev->le_features[0] & HCI_LE_SLAVE_FEATURES) &&
  4315. !conn->out && ev->status == 0x1a)
  4316. status = 0x00;
  4317. else
  4318. status = ev->status;
  4319. conn->state = BT_CONNECTED;
  4320. hci_connect_cfm(conn, status);
  4321. hci_conn_drop(conn);
  4322. }
  4323. }
  4324. hci_dev_unlock(hdev);
  4325. }
  4326. static void hci_le_ltk_request_evt(struct hci_dev *hdev, struct sk_buff *skb)
  4327. {
  4328. struct hci_ev_le_ltk_req *ev = (void *) skb->data;
  4329. struct hci_cp_le_ltk_reply cp;
  4330. struct hci_cp_le_ltk_neg_reply neg;
  4331. struct hci_conn *conn;
  4332. struct smp_ltk *ltk;
  4333. BT_DBG("%s handle 0x%4.4x", hdev->name, __le16_to_cpu(ev->handle));
  4334. hci_dev_lock(hdev);
  4335. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  4336. if (conn == NULL)
  4337. goto not_found;
  4338. ltk = hci_find_ltk(hdev, &conn->dst, conn->dst_type, conn->role);
  4339. if (!ltk)
  4340. goto not_found;
  4341. if (smp_ltk_is_sc(ltk)) {
  4342. /* With SC both EDiv and Rand are set to zero */
  4343. if (ev->ediv || ev->rand)
  4344. goto not_found;
  4345. } else {
  4346. /* For non-SC keys check that EDiv and Rand match */
  4347. if (ev->ediv != ltk->ediv || ev->rand != ltk->rand)
  4348. goto not_found;
  4349. }
  4350. memcpy(cp.ltk, ltk->val, ltk->enc_size);
  4351. memset(cp.ltk + ltk->enc_size, 0, sizeof(cp.ltk) - ltk->enc_size);
  4352. cp.handle = cpu_to_le16(conn->handle);
  4353. conn->pending_sec_level = smp_ltk_sec_level(ltk);
  4354. conn->enc_key_size = ltk->enc_size;
  4355. hci_send_cmd(hdev, HCI_OP_LE_LTK_REPLY, sizeof(cp), &cp);
  4356. /* Ref. Bluetooth Core SPEC pages 1975 and 2004. STK is a
  4357. * temporary key used to encrypt a connection following
  4358. * pairing. It is used during the Encrypted Session Setup to
  4359. * distribute the keys. Later, security can be re-established
  4360. * using a distributed LTK.
  4361. */
  4362. if (ltk->type == SMP_STK) {
  4363. set_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
  4364. list_del_rcu(&ltk->list);
  4365. kfree_rcu(ltk, rcu);
  4366. } else {
  4367. clear_bit(HCI_CONN_STK_ENCRYPT, &conn->flags);
  4368. }
  4369. hci_dev_unlock(hdev);
  4370. return;
  4371. not_found:
  4372. neg.handle = ev->handle;
  4373. hci_send_cmd(hdev, HCI_OP_LE_LTK_NEG_REPLY, sizeof(neg), &neg);
  4374. hci_dev_unlock(hdev);
  4375. }
  4376. static void send_conn_param_neg_reply(struct hci_dev *hdev, u16 handle,
  4377. u8 reason)
  4378. {
  4379. struct hci_cp_le_conn_param_req_neg_reply cp;
  4380. cp.handle = cpu_to_le16(handle);
  4381. cp.reason = reason;
  4382. hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_NEG_REPLY, sizeof(cp),
  4383. &cp);
  4384. }
  4385. static void hci_le_remote_conn_param_req_evt(struct hci_dev *hdev,
  4386. struct sk_buff *skb)
  4387. {
  4388. struct hci_ev_le_remote_conn_param_req *ev = (void *) skb->data;
  4389. struct hci_cp_le_conn_param_req_reply cp;
  4390. struct hci_conn *hcon;
  4391. u16 handle, min, max, latency, timeout;
  4392. handle = le16_to_cpu(ev->handle);
  4393. min = le16_to_cpu(ev->interval_min);
  4394. max = le16_to_cpu(ev->interval_max);
  4395. latency = le16_to_cpu(ev->latency);
  4396. timeout = le16_to_cpu(ev->timeout);
  4397. hcon = hci_conn_hash_lookup_handle(hdev, handle);
  4398. if (!hcon || hcon->state != BT_CONNECTED)
  4399. return send_conn_param_neg_reply(hdev, handle,
  4400. HCI_ERROR_UNKNOWN_CONN_ID);
  4401. if (hci_check_conn_params(min, max, latency, timeout))
  4402. return send_conn_param_neg_reply(hdev, handle,
  4403. HCI_ERROR_INVALID_LL_PARAMS);
  4404. if (hcon->role == HCI_ROLE_MASTER) {
  4405. struct hci_conn_params *params;
  4406. u8 store_hint;
  4407. hci_dev_lock(hdev);
  4408. params = hci_conn_params_lookup(hdev, &hcon->dst,
  4409. hcon->dst_type);
  4410. if (params) {
  4411. params->conn_min_interval = min;
  4412. params->conn_max_interval = max;
  4413. params->conn_latency = latency;
  4414. params->supervision_timeout = timeout;
  4415. store_hint = 0x01;
  4416. } else{
  4417. store_hint = 0x00;
  4418. }
  4419. hci_dev_unlock(hdev);
  4420. mgmt_new_conn_param(hdev, &hcon->dst, hcon->dst_type,
  4421. store_hint, min, max, latency, timeout);
  4422. }
  4423. cp.handle = ev->handle;
  4424. cp.interval_min = ev->interval_min;
  4425. cp.interval_max = ev->interval_max;
  4426. cp.latency = ev->latency;
  4427. cp.timeout = ev->timeout;
  4428. cp.min_ce_len = 0;
  4429. cp.max_ce_len = 0;
  4430. hci_send_cmd(hdev, HCI_OP_LE_CONN_PARAM_REQ_REPLY, sizeof(cp), &cp);
  4431. }
  4432. static void hci_le_direct_adv_report_evt(struct hci_dev *hdev,
  4433. struct sk_buff *skb)
  4434. {
  4435. u8 num_reports = skb->data[0];
  4436. void *ptr = &skb->data[1];
  4437. hci_dev_lock(hdev);
  4438. while (num_reports--) {
  4439. struct hci_ev_le_direct_adv_info *ev = ptr;
  4440. process_adv_report(hdev, ev->evt_type, &ev->bdaddr,
  4441. ev->bdaddr_type, &ev->direct_addr,
  4442. ev->direct_addr_type, ev->rssi, NULL, 0);
  4443. ptr += sizeof(*ev);
  4444. }
  4445. hci_dev_unlock(hdev);
  4446. }
  4447. static void hci_le_phy_update_evt(struct hci_dev *hdev, struct sk_buff *skb)
  4448. {
  4449. struct hci_ev_le_phy_update_complete *ev = (void *) skb->data;
  4450. struct hci_conn *conn;
  4451. BT_DBG("%s status 0x%2.2x", hdev->name, ev->status);
  4452. if (!ev->status)
  4453. return;
  4454. hci_dev_lock(hdev);
  4455. conn = hci_conn_hash_lookup_handle(hdev, __le16_to_cpu(ev->handle));
  4456. if (!conn)
  4457. goto unlock;
  4458. conn->le_tx_phy = ev->tx_phy;
  4459. conn->le_rx_phy = ev->rx_phy;
  4460. unlock:
  4461. hci_dev_unlock(hdev);
  4462. }
  4463. static void hci_le_meta_evt(struct hci_dev *hdev, struct sk_buff *skb)
  4464. {
  4465. struct hci_ev_le_meta *le_ev = (void *) skb->data;
  4466. skb_pull(skb, sizeof(*le_ev));
  4467. switch (le_ev->subevent) {
  4468. case HCI_EV_LE_CONN_COMPLETE:
  4469. hci_le_conn_complete_evt(hdev, skb);
  4470. break;
  4471. case HCI_EV_LE_CONN_UPDATE_COMPLETE:
  4472. hci_le_conn_update_complete_evt(hdev, skb);
  4473. break;
  4474. case HCI_EV_LE_ADVERTISING_REPORT:
  4475. hci_le_adv_report_evt(hdev, skb);
  4476. break;
  4477. case HCI_EV_LE_REMOTE_FEAT_COMPLETE:
  4478. hci_le_remote_feat_complete_evt(hdev, skb);
  4479. break;
  4480. case HCI_EV_LE_LTK_REQ:
  4481. hci_le_ltk_request_evt(hdev, skb);
  4482. break;
  4483. case HCI_EV_LE_REMOTE_CONN_PARAM_REQ:
  4484. hci_le_remote_conn_param_req_evt(hdev, skb);
  4485. break;
  4486. case HCI_EV_LE_DIRECT_ADV_REPORT:
  4487. hci_le_direct_adv_report_evt(hdev, skb);
  4488. break;
  4489. case HCI_EV_LE_PHY_UPDATE_COMPLETE:
  4490. hci_le_phy_update_evt(hdev, skb);
  4491. break;
  4492. case HCI_EV_LE_EXT_ADV_REPORT:
  4493. hci_le_ext_adv_report_evt(hdev, skb);
  4494. break;
  4495. case HCI_EV_LE_ENHANCED_CONN_COMPLETE:
  4496. hci_le_enh_conn_complete_evt(hdev, skb);
  4497. break;
  4498. case HCI_EV_LE_EXT_ADV_SET_TERM:
  4499. hci_le_ext_adv_term_evt(hdev, skb);
  4500. break;
  4501. default:
  4502. break;
  4503. }
  4504. }
  4505. static bool hci_get_cmd_complete(struct hci_dev *hdev, u16 opcode,
  4506. u8 event, struct sk_buff *skb)
  4507. {
  4508. struct hci_ev_cmd_complete *ev;
  4509. struct hci_event_hdr *hdr;
  4510. if (!skb)
  4511. return false;
  4512. if (skb->len < sizeof(*hdr)) {
  4513. bt_dev_err(hdev, "too short HCI event");
  4514. return false;
  4515. }
  4516. hdr = (void *) skb->data;
  4517. skb_pull(skb, HCI_EVENT_HDR_SIZE);
  4518. if (event) {
  4519. if (hdr->evt != event)
  4520. return false;
  4521. return true;
  4522. }
  4523. /* Check if request ended in Command Status - no way to retreive
  4524. * any extra parameters in this case.
  4525. */
  4526. if (hdr->evt == HCI_EV_CMD_STATUS)
  4527. return false;
  4528. if (hdr->evt != HCI_EV_CMD_COMPLETE) {
  4529. bt_dev_err(hdev, "last event is not cmd complete (0x%2.2x)",
  4530. hdr->evt);
  4531. return false;
  4532. }
  4533. if (skb->len < sizeof(*ev)) {
  4534. bt_dev_err(hdev, "too short cmd_complete event");
  4535. return false;
  4536. }
  4537. ev = (void *) skb->data;
  4538. skb_pull(skb, sizeof(*ev));
  4539. if (opcode != __le16_to_cpu(ev->opcode)) {
  4540. BT_DBG("opcode doesn't match (0x%2.2x != 0x%2.2x)", opcode,
  4541. __le16_to_cpu(ev->opcode));
  4542. return false;
  4543. }
  4544. return true;
  4545. }
  4546. void hci_event_packet(struct hci_dev *hdev, struct sk_buff *skb)
  4547. {
  4548. struct hci_event_hdr *hdr = (void *) skb->data;
  4549. hci_req_complete_t req_complete = NULL;
  4550. hci_req_complete_skb_t req_complete_skb = NULL;
  4551. struct sk_buff *orig_skb = NULL;
  4552. u8 status = 0, event = hdr->evt, req_evt = 0;
  4553. u16 opcode = HCI_OP_NOP;
  4554. if (!event) {
  4555. bt_dev_warn(hdev, "Received unexpected HCI Event 00000000");
  4556. goto done;
  4557. }
  4558. if (hdev->sent_cmd && bt_cb(hdev->sent_cmd)->hci.req_event == event) {
  4559. struct hci_command_hdr *cmd_hdr = (void *) hdev->sent_cmd->data;
  4560. opcode = __le16_to_cpu(cmd_hdr->opcode);
  4561. hci_req_cmd_complete(hdev, opcode, status, &req_complete,
  4562. &req_complete_skb);
  4563. req_evt = event;
  4564. }
  4565. /* If it looks like we might end up having to call
  4566. * req_complete_skb, store a pristine copy of the skb since the
  4567. * various handlers may modify the original one through
  4568. * skb_pull() calls, etc.
  4569. */
  4570. if (req_complete_skb || event == HCI_EV_CMD_STATUS ||
  4571. event == HCI_EV_CMD_COMPLETE)
  4572. orig_skb = skb_clone(skb, GFP_KERNEL);
  4573. skb_pull(skb, HCI_EVENT_HDR_SIZE);
  4574. switch (event) {
  4575. case HCI_EV_INQUIRY_COMPLETE:
  4576. hci_inquiry_complete_evt(hdev, skb);
  4577. break;
  4578. case HCI_EV_INQUIRY_RESULT:
  4579. hci_inquiry_result_evt(hdev, skb);
  4580. break;
  4581. case HCI_EV_CONN_COMPLETE:
  4582. hci_conn_complete_evt(hdev, skb);
  4583. break;
  4584. case HCI_EV_CONN_REQUEST:
  4585. hci_conn_request_evt(hdev, skb);
  4586. break;
  4587. case HCI_EV_DISCONN_COMPLETE:
  4588. hci_disconn_complete_evt(hdev, skb);
  4589. break;
  4590. case HCI_EV_AUTH_COMPLETE:
  4591. hci_auth_complete_evt(hdev, skb);
  4592. break;
  4593. case HCI_EV_REMOTE_NAME:
  4594. hci_remote_name_evt(hdev, skb);
  4595. break;
  4596. case HCI_EV_ENCRYPT_CHANGE:
  4597. hci_encrypt_change_evt(hdev, skb);
  4598. break;
  4599. case HCI_EV_CHANGE_LINK_KEY_COMPLETE:
  4600. hci_change_link_key_complete_evt(hdev, skb);
  4601. break;
  4602. case HCI_EV_REMOTE_FEATURES:
  4603. hci_remote_features_evt(hdev, skb);
  4604. break;
  4605. case HCI_EV_CMD_COMPLETE:
  4606. hci_cmd_complete_evt(hdev, skb, &opcode, &status,
  4607. &req_complete, &req_complete_skb);
  4608. break;
  4609. case HCI_EV_CMD_STATUS:
  4610. hci_cmd_status_evt(hdev, skb, &opcode, &status, &req_complete,
  4611. &req_complete_skb);
  4612. break;
  4613. case HCI_EV_HARDWARE_ERROR:
  4614. hci_hardware_error_evt(hdev, skb);
  4615. break;
  4616. case HCI_EV_ROLE_CHANGE:
  4617. hci_role_change_evt(hdev, skb);
  4618. break;
  4619. case HCI_EV_NUM_COMP_PKTS:
  4620. hci_num_comp_pkts_evt(hdev, skb);
  4621. break;
  4622. case HCI_EV_MODE_CHANGE:
  4623. hci_mode_change_evt(hdev, skb);
  4624. break;
  4625. case HCI_EV_PIN_CODE_REQ:
  4626. hci_pin_code_request_evt(hdev, skb);
  4627. break;
  4628. case HCI_EV_LINK_KEY_REQ:
  4629. hci_link_key_request_evt(hdev, skb);
  4630. break;
  4631. case HCI_EV_LINK_KEY_NOTIFY:
  4632. hci_link_key_notify_evt(hdev, skb);
  4633. break;
  4634. case HCI_EV_CLOCK_OFFSET:
  4635. hci_clock_offset_evt(hdev, skb);
  4636. break;
  4637. case HCI_EV_PKT_TYPE_CHANGE:
  4638. hci_pkt_type_change_evt(hdev, skb);
  4639. break;
  4640. case HCI_EV_PSCAN_REP_MODE:
  4641. hci_pscan_rep_mode_evt(hdev, skb);
  4642. break;
  4643. case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
  4644. hci_inquiry_result_with_rssi_evt(hdev, skb);
  4645. break;
  4646. case HCI_EV_REMOTE_EXT_FEATURES:
  4647. hci_remote_ext_features_evt(hdev, skb);
  4648. break;
  4649. case HCI_EV_SYNC_CONN_COMPLETE:
  4650. hci_sync_conn_complete_evt(hdev, skb);
  4651. break;
  4652. case HCI_EV_EXTENDED_INQUIRY_RESULT:
  4653. hci_extended_inquiry_result_evt(hdev, skb);
  4654. break;
  4655. case HCI_EV_KEY_REFRESH_COMPLETE:
  4656. hci_key_refresh_complete_evt(hdev, skb);
  4657. break;
  4658. case HCI_EV_IO_CAPA_REQUEST:
  4659. hci_io_capa_request_evt(hdev, skb);
  4660. break;
  4661. case HCI_EV_IO_CAPA_REPLY:
  4662. hci_io_capa_reply_evt(hdev, skb);
  4663. break;
  4664. case HCI_EV_USER_CONFIRM_REQUEST:
  4665. hci_user_confirm_request_evt(hdev, skb);
  4666. break;
  4667. case HCI_EV_USER_PASSKEY_REQUEST:
  4668. hci_user_passkey_request_evt(hdev, skb);
  4669. break;
  4670. case HCI_EV_USER_PASSKEY_NOTIFY:
  4671. hci_user_passkey_notify_evt(hdev, skb);
  4672. break;
  4673. case HCI_EV_KEYPRESS_NOTIFY:
  4674. hci_keypress_notify_evt(hdev, skb);
  4675. break;
  4676. case HCI_EV_SIMPLE_PAIR_COMPLETE:
  4677. hci_simple_pair_complete_evt(hdev, skb);
  4678. break;
  4679. case HCI_EV_REMOTE_HOST_FEATURES:
  4680. hci_remote_host_features_evt(hdev, skb);
  4681. break;
  4682. case HCI_EV_LE_META:
  4683. hci_le_meta_evt(hdev, skb);
  4684. break;
  4685. case HCI_EV_REMOTE_OOB_DATA_REQUEST:
  4686. hci_remote_oob_data_request_evt(hdev, skb);
  4687. break;
  4688. #if IS_ENABLED(CONFIG_BT_HS)
  4689. case HCI_EV_CHANNEL_SELECTED:
  4690. hci_chan_selected_evt(hdev, skb);
  4691. break;
  4692. case HCI_EV_PHY_LINK_COMPLETE:
  4693. hci_phy_link_complete_evt(hdev, skb);
  4694. break;
  4695. case HCI_EV_LOGICAL_LINK_COMPLETE:
  4696. hci_loglink_complete_evt(hdev, skb);
  4697. break;
  4698. case HCI_EV_DISCONN_LOGICAL_LINK_COMPLETE:
  4699. hci_disconn_loglink_complete_evt(hdev, skb);
  4700. break;
  4701. case HCI_EV_DISCONN_PHY_LINK_COMPLETE:
  4702. hci_disconn_phylink_complete_evt(hdev, skb);
  4703. break;
  4704. #endif
  4705. case HCI_EV_NUM_COMP_BLOCKS:
  4706. hci_num_comp_blocks_evt(hdev, skb);
  4707. break;
  4708. default:
  4709. BT_DBG("%s event 0x%2.2x", hdev->name, event);
  4710. break;
  4711. }
  4712. if (req_complete) {
  4713. req_complete(hdev, status, opcode);
  4714. } else if (req_complete_skb) {
  4715. if (!hci_get_cmd_complete(hdev, opcode, req_evt, orig_skb)) {
  4716. kfree_skb(orig_skb);
  4717. orig_skb = NULL;
  4718. }
  4719. req_complete_skb(hdev, status, opcode, orig_skb);
  4720. }
  4721. done:
  4722. kfree_skb(orig_skb);
  4723. kfree_skb(skb);
  4724. hdev->stat.evt_rx++;
  4725. }