btm_ble_privacy.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * This file contains functions for BLE controller based privacy.
  21. *
  22. ******************************************************************************/
  23. #include <string.h>
  24. #include "common/bt_target.h"
  25. #if (BLE_INCLUDED == TRUE && BLE_PRIVACY_SPT == TRUE)
  26. #include "stack/bt_types.h"
  27. #include "stack/hcimsgs.h"
  28. #include "stack/btu.h"
  29. //#include "vendor_hcidefs.h"
  30. #include "btm_int.h"
  31. #include "device/controller.h"
  32. #include "stack/hcidefs.h"
  33. #define HCI_VENDOR_BLE_RPA_VSC (0x0155 | HCI_GRP_VENDOR_SPECIFIC)
  34. /* RPA offload VSC specifics */
  35. #define BTM_BLE_META_IRK_ENABLE 0x01
  36. #define BTM_BLE_META_ADD_IRK_ENTRY 0x02
  37. #define BTM_BLE_META_REMOVE_IRK_ENTRY 0x03
  38. #define BTM_BLE_META_CLEAR_IRK_LIST 0x04
  39. #define BTM_BLE_META_READ_IRK_ENTRY 0x05
  40. #define BTM_BLE_META_CS_RESOLVE_ADDR 0x00000001
  41. #define BTM_BLE_IRK_ENABLE_LEN 2
  42. #define BTM_BLE_META_ADD_IRK_LEN 24
  43. #define BTM_BLE_META_REMOVE_IRK_LEN 8
  44. #define BTM_BLE_META_CLEAR_IRK_LEN 1
  45. #define BTM_BLE_META_READ_IRK_LEN 2
  46. #define BTM_BLE_META_ADD_WL_ATTR_LEN 9
  47. /*******************************************************************************
  48. ** Functions implemented controller based privacy using Resolving List
  49. *******************************************************************************/
  50. /*******************************************************************************
  51. **
  52. ** Function btm_ble_enq_resolving_list_pending
  53. **
  54. ** Description add target address into resolving pending operation queue
  55. **
  56. ** Parameters target_bda: target device address
  57. ** add_entry: TRUE for add entry, FALSE for remove entry
  58. **
  59. ** Returns void
  60. **
  61. *******************************************************************************/
  62. void btm_ble_enq_resolving_list_pending(BD_ADDR pseudo_bda, UINT8 op_code)
  63. {
  64. tBTM_BLE_RESOLVE_Q *p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
  65. memcpy(p_q->resolve_q_random_pseudo[p_q->q_next], pseudo_bda, BD_ADDR_LEN);
  66. p_q->resolve_q_action[p_q->q_next] = op_code;
  67. p_q->q_next ++;
  68. p_q->q_next %= controller_get_interface()->get_ble_resolving_list_max_size();
  69. }
  70. /*******************************************************************************
  71. **
  72. ** Function btm_ble_brcm_find_resolving_pending_entry
  73. **
  74. ** Description check to see if the action is in pending list
  75. **
  76. ** Parameters TRUE: action pending;
  77. ** FALSE: new action
  78. **
  79. ** Returns void
  80. **
  81. *******************************************************************************/
  82. BOOLEAN btm_ble_brcm_find_resolving_pending_entry(BD_ADDR pseudo_addr, UINT8 action)
  83. {
  84. tBTM_BLE_RESOLVE_Q *p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
  85. for (UINT8 i = p_q->q_pending; i != p_q->q_next;) {
  86. if (memcmp(p_q->resolve_q_random_pseudo[i], pseudo_addr, BD_ADDR_LEN) == 0 &&
  87. action == p_q->resolve_q_action[i]) {
  88. return TRUE;
  89. }
  90. i ++;
  91. i %= controller_get_interface()->get_ble_resolving_list_max_size();
  92. }
  93. return FALSE;
  94. }
  95. /*******************************************************************************
  96. **
  97. ** Function btm_ble_deq_resolving_pending
  98. **
  99. ** Description dequeue target address from resolving pending operation queue
  100. **
  101. ** Parameters pseudo_addr: pseudo_addr device address
  102. **
  103. ** Returns void
  104. **
  105. *******************************************************************************/
  106. BOOLEAN btm_ble_deq_resolving_pending(BD_ADDR pseudo_addr)
  107. {
  108. tBTM_BLE_RESOLVE_Q *p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
  109. if (p_q->q_next != p_q->q_pending) {
  110. memcpy(pseudo_addr, p_q->resolve_q_random_pseudo[p_q->q_pending], BD_ADDR_LEN);
  111. memset(p_q->resolve_q_random_pseudo[p_q->q_pending], 0, BD_ADDR_LEN);
  112. p_q->q_pending ++;
  113. p_q->q_pending %= controller_get_interface()->get_ble_resolving_list_max_size();
  114. return TRUE;
  115. }
  116. return FALSE;
  117. }
  118. /*******************************************************************************
  119. **
  120. ** Function btm_ble_clear_irk_index
  121. **
  122. ** Description clear IRK list index mask for availability
  123. **
  124. ** Returns none
  125. **
  126. *******************************************************************************/
  127. void btm_ble_clear_irk_index(UINT8 index)
  128. {
  129. UINT8 byte;
  130. UINT8 bit;
  131. if (index < controller_get_interface()->get_ble_resolving_list_max_size()) {
  132. byte = index / 8;
  133. bit = index % 8;
  134. btm_cb.ble_ctr_cb.irk_list_mask[byte] &= (~(1 << bit));
  135. }
  136. }
  137. /*******************************************************************************
  138. **
  139. ** Function btm_ble_find_irk_index
  140. **
  141. ** Description find the first available IRK list index
  142. **
  143. ** Returns index from 0 ~ max (127 default)
  144. **
  145. *******************************************************************************/
  146. UINT8 btm_ble_find_irk_index(void)
  147. {
  148. UINT8 i = 0;
  149. UINT8 byte;
  150. UINT8 bit;
  151. while (i < controller_get_interface()->get_ble_resolving_list_max_size()) {
  152. byte = i / 8;
  153. bit = i % 8;
  154. if ((btm_cb.ble_ctr_cb.irk_list_mask[byte] & (1 << bit)) == 0) {
  155. btm_cb.ble_ctr_cb.irk_list_mask[byte] |= (1 << bit);
  156. return i;
  157. }
  158. i++;
  159. }
  160. BTM_TRACE_ERROR ("%s failed, list full", __func__);
  161. return i;
  162. }
  163. /*******************************************************************************
  164. **
  165. ** Function btm_ble_update_resolving_list
  166. **
  167. ** Description update resolving list entry in host maintained record
  168. **
  169. ** Returns void
  170. **
  171. *******************************************************************************/
  172. void btm_ble_update_resolving_list(BD_ADDR pseudo_bda, BOOLEAN add)
  173. {
  174. tBTM_SEC_DEV_REC *p_dev_rec = btm_find_dev(pseudo_bda);
  175. if (p_dev_rec == NULL) {
  176. return;
  177. }
  178. if (add) {
  179. p_dev_rec->ble.in_controller_list |= BTM_RESOLVING_LIST_BIT;
  180. if (!controller_get_interface()->supports_ble_privacy()) {
  181. p_dev_rec->ble.resolving_list_index = btm_ble_find_irk_index();
  182. }
  183. } else {
  184. p_dev_rec->ble.in_controller_list &= ~BTM_RESOLVING_LIST_BIT;
  185. if (!controller_get_interface()->supports_ble_privacy()) {
  186. /* clear IRK list index mask */
  187. btm_ble_clear_irk_index(p_dev_rec->ble.resolving_list_index);
  188. p_dev_rec->ble.resolving_list_index = 0;
  189. }
  190. }
  191. }
  192. /*******************************************************************************
  193. **
  194. ** Function btm_ble_clear_resolving_list_complete
  195. **
  196. ** Description This function is called when command complete for
  197. ** clear resolving list
  198. **
  199. ** Returns void
  200. **
  201. *******************************************************************************/
  202. void btm_ble_clear_resolving_list_complete(UINT8 *p, UINT16 evt_len)
  203. {
  204. UINT8 status = 0;
  205. STREAM_TO_UINT8(status, p);
  206. BTM_TRACE_DEBUG("%s status=%d", __func__, status);
  207. if (status == HCI_SUCCESS) {
  208. if (evt_len >= 3) {
  209. /* VSC complete has one extra byte for op code and list size, skip it here */
  210. p ++;
  211. /* updated the available list size, and current list size */
  212. uint8_t irk_list_sz_max = 0;
  213. STREAM_TO_UINT8(irk_list_sz_max, p);
  214. if (controller_get_interface()->get_ble_resolving_list_max_size() == 0) {
  215. btm_ble_resolving_list_init(irk_list_sz_max);
  216. }
  217. uint8_t irk_mask_size = (irk_list_sz_max % 8) ?
  218. (irk_list_sz_max / 8 + 1) : (irk_list_sz_max / 8);
  219. memset(btm_cb.ble_ctr_cb.irk_list_mask, 0, irk_mask_size);
  220. }
  221. btm_cb.ble_ctr_cb.resolving_list_avail_size =
  222. controller_get_interface()->get_ble_resolving_list_max_size();
  223. BTM_TRACE_DEBUG("%s resolving_list_avail_size=%d",
  224. __func__, btm_cb.ble_ctr_cb.resolving_list_avail_size);
  225. list_node_t *p_node = NULL;
  226. tBTM_SEC_DEV_REC *p_dev_rec = NULL;
  227. for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
  228. p_dev_rec = list_node(p_node);
  229. p_dev_rec->ble.in_controller_list &= ~BTM_RESOLVING_LIST_BIT;
  230. }
  231. }
  232. }
  233. /*******************************************************************************
  234. **
  235. ** Function btm_ble_add_resolving_list_entry_complete
  236. **
  237. ** Description This function is called when command complete for
  238. ** add resolving list entry
  239. **
  240. ** Returns void
  241. **
  242. *******************************************************************************/
  243. void btm_ble_add_resolving_list_entry_complete(UINT8 *p, UINT16 evt_len)
  244. {
  245. UINT8 status;
  246. STREAM_TO_UINT8(status, p);
  247. BTM_TRACE_DEBUG("%s status = %d", __func__, status);
  248. BD_ADDR pseudo_bda;
  249. if (!btm_ble_deq_resolving_pending(pseudo_bda)) {
  250. BTM_TRACE_DEBUG("no pending resolving list operation");
  251. return;
  252. }
  253. if (status == HCI_SUCCESS) {
  254. /* privacy 1.2 command complete does not have these extra byte */
  255. if (evt_len > 2) {
  256. /* VSC complete has one extra byte for op code, skip it here */
  257. p ++;
  258. STREAM_TO_UINT8(btm_cb.ble_ctr_cb.resolving_list_avail_size, p);
  259. } else {
  260. btm_cb.ble_ctr_cb.resolving_list_avail_size --;
  261. }
  262. } else if (status == HCI_ERR_MEMORY_FULL) { /* BT_ERROR_CODE_MEMORY_CAPACITY_EXCEEDED */
  263. btm_cb.ble_ctr_cb.resolving_list_avail_size = 0;
  264. BTM_TRACE_ERROR("%s Resolving list Full ", __func__);
  265. } else {
  266. BTM_TRACE_ERROR("%s Add resolving list error %d ", __func__, status);
  267. }
  268. }
  269. /*******************************************************************************
  270. **
  271. ** Function btm_ble_remove_resolving_list_entry_complete
  272. **
  273. ** Description This function is called when command complete for
  274. ** remove resolving list entry
  275. **
  276. ** Returns void
  277. **
  278. *******************************************************************************/
  279. void btm_ble_remove_resolving_list_entry_complete(UINT8 *p, UINT16 evt_len)
  280. {
  281. BD_ADDR pseudo_bda;
  282. UINT8 status;
  283. STREAM_TO_UINT8(status, p);
  284. BTM_TRACE_DEBUG("%s status = %d", __func__, status);
  285. if (!btm_ble_deq_resolving_pending(pseudo_bda)) {
  286. BTM_TRACE_ERROR("%s no pending resolving list operation", __func__);
  287. return;
  288. }
  289. if (status == HCI_SUCCESS) {
  290. /* proprietary: spec does not have these extra bytes */
  291. if (evt_len > 2) {
  292. p ++; /* skip opcode */
  293. STREAM_TO_UINT8(btm_cb.ble_ctr_cb.resolving_list_avail_size, p);
  294. } else {
  295. btm_cb.ble_ctr_cb.resolving_list_avail_size++;
  296. }
  297. } else {
  298. BTM_TRACE_ERROR("%s remove resolving list error 0x%x", __func__, status);
  299. }
  300. }
  301. /*******************************************************************************
  302. **
  303. ** Function btm_ble_read_resolving_list_entry_complete
  304. **
  305. ** Description This function is called when command complete for
  306. ** remove resolving list entry
  307. **
  308. ** Returns void
  309. **
  310. *******************************************************************************/
  311. void btm_ble_read_resolving_list_entry_complete(UINT8 *p, UINT16 evt_len)
  312. {
  313. UINT8 status, rra_type = BTM_BLE_ADDR_PSEUDO;
  314. BD_ADDR rra, pseudo_bda;
  315. STREAM_TO_UINT8 (status, p);
  316. BTM_TRACE_DEBUG("%s status = %d", __func__, status);
  317. if (!btm_ble_deq_resolving_pending(pseudo_bda)) {
  318. BTM_TRACE_ERROR("no pending resolving list operation");
  319. return;
  320. }
  321. if (status == HCI_SUCCESS) {
  322. /* proprietary spec has extra bytes */
  323. if (evt_len > 8) {
  324. p += (2 + 16 + 1 + 6); /* skip subcode, index, IRK value, address type, identity addr type */
  325. STREAM_TO_BDADDR(rra, p);
  326. BTM_TRACE_ERROR("%s peer_addr: %02x:%02x:%02x:%02x:%02x:%02x",
  327. __func__, rra[0], rra[1], rra[2], rra[3], rra[4], rra[5]);
  328. } else {
  329. STREAM_TO_BDADDR(rra, p);
  330. }
  331. btm_ble_refresh_peer_resolvable_private_addr(pseudo_bda, rra, rra_type);
  332. }
  333. }
  334. /*******************************************************************************
  335. VSC that implement controller based privacy
  336. ********************************************************************************/
  337. /*******************************************************************************
  338. **
  339. ** Function btm_ble_resolving_list_vsc_op_cmpl
  340. **
  341. ** Description IRK operation VSC complete handler
  342. **
  343. ** Parameters
  344. **
  345. ** Returns void
  346. **
  347. *******************************************************************************/
  348. void btm_ble_resolving_list_vsc_op_cmpl (tBTM_VSC_CMPL *p_params)
  349. {
  350. UINT8 *p = p_params->p_param_buf, op_subcode;
  351. UINT16 evt_len = p_params->param_len;
  352. op_subcode = *(p + 1);
  353. BTM_TRACE_DEBUG("%s op_subcode = %d", __func__, op_subcode);
  354. if (op_subcode == BTM_BLE_META_CLEAR_IRK_LIST) {
  355. btm_ble_clear_resolving_list_complete(p, evt_len);
  356. } else if (op_subcode == BTM_BLE_META_ADD_IRK_ENTRY) {
  357. btm_ble_add_resolving_list_entry_complete(p, evt_len);
  358. } else if (op_subcode == BTM_BLE_META_REMOVE_IRK_ENTRY) {
  359. btm_ble_remove_resolving_list_entry_complete(p, evt_len);
  360. } else if (op_subcode == BTM_BLE_META_READ_IRK_ENTRY) {
  361. btm_ble_read_resolving_list_entry_complete(p, evt_len);
  362. } else if (op_subcode == BTM_BLE_META_IRK_ENABLE) {
  363. /* RPA offloading enable/disabled */
  364. }
  365. }
  366. /*******************************************************************************
  367. **
  368. ** Function btm_ble_remove_resolving_list_entry
  369. **
  370. ** Description This function to remove an IRK entry from the list
  371. **
  372. ** Parameters ble_addr_type: address type
  373. ** ble_addr: LE adddress
  374. **
  375. ** Returns status
  376. **
  377. *******************************************************************************/
  378. tBTM_STATUS btm_ble_remove_resolving_list_entry(tBTM_SEC_DEV_REC *p_dev_rec)
  379. {
  380. /* if controller does not support RPA offloading or privacy 1.2, skip */
  381. if (controller_get_interface()->get_ble_resolving_list_max_size() == 0) {
  382. return BTM_WRONG_MODE;
  383. }
  384. tBTM_STATUS st = BTM_NO_RESOURCES;
  385. if (controller_get_interface()->supports_ble_privacy()) {
  386. if (btsnd_hcic_ble_rm_device_resolving_list(p_dev_rec->ble.static_addr_type,
  387. p_dev_rec->ble.static_addr)) {
  388. st = BTM_CMD_STARTED;
  389. }
  390. } else {
  391. UINT8 param[20] = {0};
  392. UINT8 *p = param;
  393. UINT8_TO_STREAM(p, BTM_BLE_META_REMOVE_IRK_ENTRY);
  394. UINT8_TO_STREAM(p, p_dev_rec->ble.static_addr_type);
  395. BDADDR_TO_STREAM(p, p_dev_rec->ble.static_addr);
  396. st = BTM_VendorSpecificCommand(HCI_VENDOR_BLE_RPA_VSC,
  397. BTM_BLE_META_REMOVE_IRK_LEN,
  398. param,
  399. btm_ble_resolving_list_vsc_op_cmpl);
  400. }
  401. if (st == BTM_CMD_STARTED) {
  402. btm_ble_enq_resolving_list_pending( p_dev_rec->bd_addr, BTM_BLE_META_REMOVE_IRK_ENTRY);
  403. }
  404. return st;
  405. }
  406. /*******************************************************************************
  407. **
  408. ** Function btm_ble_clear_resolving_list
  409. **
  410. ** Description This function clears the resolving list
  411. **
  412. ** Parameters None.
  413. **
  414. ** Returns status
  415. **
  416. *******************************************************************************/
  417. tBTM_STATUS btm_ble_clear_resolving_list(void)
  418. {
  419. tBTM_STATUS st = BTM_NO_RESOURCES;
  420. if (controller_get_interface()->supports_ble_privacy()) {
  421. if (btsnd_hcic_ble_clear_resolving_list()) {
  422. st = BTM_SUCCESS;
  423. }
  424. } else {
  425. UINT8 param[20] = {0};
  426. UINT8 *p = param;
  427. UINT8_TO_STREAM(p, BTM_BLE_META_CLEAR_IRK_LIST);
  428. st = BTM_VendorSpecificCommand (HCI_VENDOR_BLE_RPA_VSC,
  429. BTM_BLE_META_CLEAR_IRK_LEN,
  430. param,
  431. btm_ble_resolving_list_vsc_op_cmpl);
  432. }
  433. return st;
  434. }
  435. /*******************************************************************************
  436. **
  437. ** Function btm_ble_read_resolving_list_entry
  438. **
  439. ** Description This function read an IRK entry by index
  440. **
  441. ** Parameters entry index.
  442. **
  443. ** Returns status
  444. **
  445. *******************************************************************************/
  446. tBTM_STATUS btm_ble_read_resolving_list_entry(tBTM_SEC_DEV_REC *p_dev_rec)
  447. {
  448. tBTM_STATUS st = BTM_NO_RESOURCES;
  449. if (!(p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT)) {
  450. return BTM_WRONG_MODE;
  451. }
  452. if (controller_get_interface()->supports_ble_privacy()) {
  453. if (btsnd_hcic_ble_read_resolvable_addr_peer(p_dev_rec->ble.static_addr_type,
  454. p_dev_rec->ble.static_addr)) {
  455. st = BTM_CMD_STARTED;
  456. }
  457. } else {
  458. UINT8 param[20] = {0};
  459. UINT8 *p = param;
  460. UINT8_TO_STREAM(p, BTM_BLE_META_READ_IRK_ENTRY);
  461. UINT8_TO_STREAM(p, p_dev_rec->ble.resolving_list_index);
  462. st = BTM_VendorSpecificCommand (HCI_VENDOR_BLE_RPA_VSC,
  463. BTM_BLE_META_READ_IRK_LEN,
  464. param,
  465. btm_ble_resolving_list_vsc_op_cmpl);
  466. }
  467. if (st == BTM_CMD_STARTED) {
  468. btm_ble_enq_resolving_list_pending(p_dev_rec->bd_addr,
  469. BTM_BLE_META_READ_IRK_ENTRY);
  470. }
  471. return st;
  472. }
  473. /*******************************************************************************
  474. **
  475. ** Function btm_ble_suspend_resolving_list_activity
  476. **
  477. ** Description This function suspends all resolving list activity, including
  478. ** scan, initiating, and advertising, if resolving list is being
  479. ** enabled.
  480. **
  481. ** Parameters
  482. **
  483. ** Returns TRUE if suspended; FALSE otherwise
  484. **
  485. *******************************************************************************/
  486. BOOLEAN btm_ble_suspend_resolving_list_activity(void)
  487. {
  488. tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
  489. /* if resolving list is not enabled, do not need to terminate any activity */
  490. /* if asking for stop all activity */
  491. /* if already suspended */
  492. if (p_ble_cb->suspended_rl_state != BTM_BLE_RL_IDLE) {
  493. return TRUE;
  494. }
  495. /* direct connection active, wait until it completed */
  496. if (btm_ble_get_conn_st() == BLE_DIR_CONN) {
  497. BTM_TRACE_ERROR("resolving list can not be edited, EnQ now");
  498. return FALSE;
  499. }
  500. p_ble_cb->suspended_rl_state = BTM_BLE_RL_IDLE;
  501. if (p_ble_cb->inq_var.adv_mode == BTM_BLE_ADV_ENABLE) {
  502. btm_ble_stop_adv();
  503. p_ble_cb->suspended_rl_state |= BTM_BLE_RL_ADV;
  504. }
  505. if (BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) {
  506. btm_ble_stop_scan();
  507. p_ble_cb->suspended_rl_state |= BTM_BLE_RL_SCAN;
  508. }
  509. if (btm_ble_suspend_bg_conn()) {
  510. p_ble_cb->suspended_rl_state |= BTM_BLE_RL_INIT;
  511. }
  512. return TRUE;
  513. }
  514. /*******************************************************************************
  515. **
  516. ** Function btm_ble_resume_resolving_list_activity
  517. **
  518. ** Description This function resumes the resolving list activity, including
  519. ** scanning, initiating, and advertising, if any of these
  520. ** activities has been suspended earlier.
  521. **
  522. ** Returns none
  523. **
  524. *******************************************************************************/
  525. void btm_ble_resume_resolving_list_activity(void)
  526. {
  527. tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
  528. if (p_ble_cb->suspended_rl_state & BTM_BLE_RL_ADV) {
  529. btm_ble_start_adv();
  530. }
  531. if (p_ble_cb->suspended_rl_state & BTM_BLE_RL_SCAN) {
  532. btm_ble_start_scan();
  533. }
  534. if (p_ble_cb->suspended_rl_state & BTM_BLE_RL_INIT) {
  535. btm_ble_resume_bg_conn();
  536. }
  537. p_ble_cb->suspended_rl_state = BTM_BLE_RL_IDLE;
  538. }
  539. /*******************************************************************************
  540. **
  541. ** Function btm_ble_vendor_enable_irk_feature
  542. **
  543. ** Description This function is called to enable or disable the RRA
  544. ** offloading feature.
  545. **
  546. ** Parameters enable: enable or disable the RRA offloading feature
  547. **
  548. ** Returns BTM_SUCCESS if successful
  549. **
  550. *******************************************************************************/
  551. tBTM_STATUS btm_ble_vendor_enable_irk_feature(BOOLEAN enable)
  552. {
  553. UINT8 param[20], *p;
  554. tBTM_STATUS st = BTM_MODE_UNSUPPORTED;
  555. p = param;
  556. memset(param, 0, 20);
  557. /* select feature based on control block settings */
  558. UINT8_TO_STREAM(p, BTM_BLE_META_IRK_ENABLE);
  559. UINT8_TO_STREAM(p, enable ? 0x01 : 0x00);
  560. st = BTM_VendorSpecificCommand (HCI_VENDOR_BLE_RPA_VSC, BTM_BLE_IRK_ENABLE_LEN,
  561. param, btm_ble_resolving_list_vsc_op_cmpl);
  562. return st;
  563. }
  564. /*******************************************************************************
  565. **
  566. ** Function btm_ble_exe_disable_resolving_list
  567. **
  568. ** Description execute resolving list disable
  569. **
  570. ** Returns none
  571. **
  572. *******************************************************************************/
  573. BOOLEAN btm_ble_exe_disable_resolving_list(void)
  574. {
  575. if (!btm_ble_suspend_resolving_list_activity()) {
  576. return FALSE;
  577. }
  578. if (!controller_get_interface()->supports_ble_privacy()) {
  579. btm_ble_vendor_enable_irk_feature(FALSE);
  580. } else {
  581. //btsnd_hcic_ble_set_addr_resolution_enable(FALSE);
  582. }
  583. return TRUE;
  584. }
  585. /*******************************************************************************
  586. **
  587. ** Function btm_ble_exe_enable_resolving_list
  588. **
  589. ** Description enable LE resolve address list
  590. **
  591. ** Returns none
  592. **
  593. *******************************************************************************/
  594. void btm_ble_exe_enable_resolving_list(void)
  595. {
  596. if (!btm_ble_suspend_resolving_list_activity()) {
  597. return;
  598. }
  599. if (!controller_get_interface()->supports_ble_privacy()) {
  600. btm_ble_vendor_enable_irk_feature(TRUE);
  601. } else {
  602. //btsnd_hcic_ble_set_addr_resolution_enable(TRUE);
  603. }
  604. }
  605. /*******************************************************************************
  606. **
  607. ** Function btm_ble_disable_resolving_list
  608. **
  609. ** Description Disable LE Address resolution
  610. **
  611. ** Returns none
  612. **
  613. *******************************************************************************/
  614. BOOLEAN btm_ble_disable_resolving_list(UINT8 rl_mask, BOOLEAN to_resume )
  615. {
  616. UINT8 rl_state = btm_cb.ble_ctr_cb.rl_state;
  617. /* if controller does not support RPA offloading or privacy 1.2, skip */
  618. if (controller_get_interface()->get_ble_resolving_list_max_size() == 0) {
  619. return FALSE;
  620. }
  621. btm_cb.ble_ctr_cb.rl_state &= ~rl_mask;
  622. if (rl_state != BTM_BLE_RL_IDLE && btm_cb.ble_ctr_cb.rl_state == BTM_BLE_RL_IDLE) {
  623. if (btm_ble_exe_disable_resolving_list()) {
  624. if (to_resume) {
  625. btm_ble_resume_resolving_list_activity();
  626. }
  627. return TRUE;
  628. } else {
  629. return FALSE;
  630. }
  631. }
  632. return TRUE;
  633. }
  634. /*******************************************************************************
  635. **
  636. ** Function btm_ble_resolving_list_load_dev
  637. **
  638. ** Description This function add a device which is using RPA into white list
  639. **
  640. ** Parameters pointer to device security record
  641. **
  642. ** Returns TRUE if device added, otherwise falase.
  643. **
  644. *******************************************************************************/
  645. BOOLEAN btm_ble_resolving_list_load_dev(tBTM_SEC_DEV_REC *p_dev_rec)
  646. {
  647. BOOLEAN rt = FALSE;
  648. #if (SMP_INCLUDED == TRUE)
  649. UINT8 rl_mask = btm_cb.ble_ctr_cb.rl_state;
  650. BTM_TRACE_DEBUG("%s btm_cb.ble_ctr_cb.privacy_mode = %d\n", __func__,
  651. btm_cb.ble_ctr_cb.privacy_mode);
  652. /* if controller does not support RPA offloading or privacy 1.2, skip */
  653. if (controller_get_interface()->get_ble_resolving_list_max_size() == 0) {
  654. return FALSE;
  655. }
  656. BTM_TRACE_DEBUG("%s btm_cb.ble_ctr_cb.privacy_mode = %d\n",
  657. __func__, btm_cb.ble_ctr_cb.privacy_mode);
  658. /* only add RPA enabled device into resolving list */
  659. if (p_dev_rec != NULL && /* RPA is being used and PID is known */
  660. (p_dev_rec->sec_flags & BTM_SEC_IN_USE) != 0 &&
  661. ((p_dev_rec->ble.key_type & BTM_LE_KEY_PID) != 0 ||
  662. (p_dev_rec->ble.key_type & BTM_LE_KEY_LID) != 0)) {
  663. if (!(p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) &&
  664. btm_ble_brcm_find_resolving_pending_entry(p_dev_rec->bd_addr,
  665. BTM_BLE_META_ADD_IRK_ENTRY) == FALSE) {
  666. if (btm_cb.ble_ctr_cb.resolving_list_avail_size > 0) {
  667. if (rl_mask) {
  668. if (!btm_ble_disable_resolving_list (rl_mask, FALSE)) {
  669. return FALSE;
  670. }
  671. }
  672. btm_ble_update_resolving_list(p_dev_rec->bd_addr, TRUE);
  673. if (controller_get_interface()->supports_ble_privacy()) {
  674. BD_ADDR dummy_bda = {0};
  675. if (memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) == 0) {
  676. memcpy(p_dev_rec->ble.static_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
  677. p_dev_rec->ble.static_addr_type = p_dev_rec->ble.ble_addr_type;
  678. }
  679. #if CONTROLLER_RPA_LIST_ENABLE
  680. BTM_TRACE_DEBUG("%s:adding device to controller resolving list\n", __func__);
  681. UINT8 *peer_irk = p_dev_rec->ble.keys.irk;
  682. UINT8 *local_irk = btm_cb.devcb.id_keys.irk;
  683. //use identical IRK for now
  684. rt = btsnd_hcic_ble_add_device_resolving_list(p_dev_rec->ble.static_addr_type,
  685. p_dev_rec->ble.static_addr, peer_irk, local_irk);
  686. #else
  687. // do nothing
  688. /* It will cause that scanner doesn't send scan request to advertiser
  689. * which has sent IRK to us and we have stored the IRK in controller.
  690. * It is a hardware limitation. The preliminary solution is not to
  691. * send key to the controller, but to resolve the random address in host. */
  692. #endif
  693. } else {
  694. UINT8 param[40] = {0};
  695. UINT8 *p = param;
  696. UINT8_TO_STREAM(p, BTM_BLE_META_ADD_IRK_ENTRY);
  697. ARRAY_TO_STREAM(p, p_dev_rec->ble.keys.irk, BT_OCTET16_LEN);
  698. UINT8_TO_STREAM(p, p_dev_rec->ble.static_addr_type);
  699. BDADDR_TO_STREAM(p, p_dev_rec->ble.static_addr);
  700. if (BTM_VendorSpecificCommand (HCI_VENDOR_BLE_RPA_VSC,
  701. BTM_BLE_META_ADD_IRK_LEN,
  702. param,
  703. btm_ble_resolving_list_vsc_op_cmpl)
  704. == BTM_CMD_STARTED) {
  705. rt = TRUE;
  706. }
  707. }
  708. if (rt) {
  709. btm_ble_enq_resolving_list_pending(p_dev_rec->bd_addr,
  710. BTM_BLE_META_ADD_IRK_ENTRY);
  711. }
  712. /* if resolving list has been turned on, re-enable it */
  713. // if (rl_mask) {
  714. // btm_ble_enable_resolving_list(rl_mask);
  715. // } else {
  716. // btm_ble_enable_resolving_list(BTM_BLE_RL_INIT);
  717. // }
  718. } else {
  719. BTM_TRACE_WARNING("%s Resolving list full ", __func__);
  720. }
  721. } else {
  722. BTM_TRACE_DEBUG("Device already in Resolving list\n");
  723. rt = TRUE;
  724. }
  725. } else {
  726. BTM_TRACE_DEBUG("Device not a RPA enabled device\n");
  727. }
  728. #endif ///SMP_INCLUDED == TRUE
  729. return rt;
  730. }
  731. /*******************************************************************************
  732. **
  733. ** Function btm_ble_resolving_list_remove_dev
  734. **
  735. ** Description This function removes the device from resolving list
  736. **
  737. ** Parameters
  738. **
  739. ** Returns status
  740. **
  741. *******************************************************************************/
  742. void btm_ble_resolving_list_remove_dev(tBTM_SEC_DEV_REC *p_dev_rec)
  743. {
  744. UINT8 rl_mask = btm_cb.ble_ctr_cb.rl_state;
  745. BTM_TRACE_EVENT ("%s\n", __func__);
  746. if (rl_mask) {
  747. if (!btm_ble_disable_resolving_list (rl_mask, FALSE)) {
  748. return;
  749. }
  750. }
  751. if ((p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) &&
  752. btm_ble_brcm_find_resolving_pending_entry(p_dev_rec->bd_addr,
  753. BTM_BLE_META_REMOVE_IRK_ENTRY) == FALSE) {
  754. btm_ble_update_resolving_list( p_dev_rec->bd_addr, FALSE);
  755. btm_ble_remove_resolving_list_entry(p_dev_rec);
  756. } else {
  757. BTM_TRACE_DEBUG("Device not in resolving list\n");
  758. }
  759. /* if resolving list has been turned on, re-enable it */
  760. // if (rl_mask) {
  761. // btm_ble_enable_resolving_list(rl_mask);
  762. // }
  763. }
  764. /*******************************************************************************
  765. **
  766. ** Function btm_ble_enable_resolving_list
  767. **
  768. ** Description enable LE resolve address list
  769. **
  770. ** Returns none
  771. **
  772. *******************************************************************************/
  773. void btm_ble_enable_resolving_list(UINT8 rl_mask)
  774. {
  775. UINT8 rl_state = btm_cb.ble_ctr_cb.rl_state;
  776. btm_cb.ble_ctr_cb.rl_state |= rl_mask;
  777. if (rl_state == BTM_BLE_RL_IDLE &&
  778. btm_cb.ble_ctr_cb.rl_state != BTM_BLE_RL_IDLE &&
  779. controller_get_interface()->get_ble_resolving_list_max_size() != 0) {
  780. btm_ble_exe_enable_resolving_list();
  781. btm_ble_resume_resolving_list_activity();
  782. }
  783. }
  784. #if 0 //Unused
  785. /*******************************************************************************
  786. **
  787. ** Function btm_ble_resolving_list_empty
  788. **
  789. ** Description check to see if resoving list is empty or not
  790. **
  791. ** Returns TRUE: empty; FALSE non-empty
  792. **
  793. *******************************************************************************/
  794. BOOLEAN btm_ble_resolving_list_empty(void)
  795. {
  796. return (controller_get_interface()->get_ble_resolving_list_max_size() ==
  797. btm_cb.ble_ctr_cb.resolving_list_avail_size);
  798. }
  799. #endif
  800. /*******************************************************************************
  801. **
  802. ** Function btm_ble_enable_resolving_list_for_platform
  803. **
  804. ** Description enable/disable resolving list feature depending on if any
  805. ** resolving list is empty and whitelist is involoved in the
  806. ** operation.
  807. **
  808. ** Returns none
  809. **
  810. *******************************************************************************/
  811. void btm_ble_enable_resolving_list_for_platform (UINT8 rl_mask)
  812. {
  813. /* if controller does not support, skip */
  814. if (controller_get_interface()->get_ble_resolving_list_max_size() == 0) {
  815. return;
  816. }
  817. if (btm_cb.ble_ctr_cb.wl_state == BTM_BLE_WL_IDLE) {
  818. if (controller_get_interface()->get_ble_resolving_list_max_size() >
  819. btm_cb.ble_ctr_cb.resolving_list_avail_size) {
  820. btm_ble_enable_resolving_list(rl_mask);
  821. } else {
  822. btm_ble_disable_resolving_list(rl_mask, TRUE);
  823. }
  824. return;
  825. }
  826. tBTM_SEC_DEV_REC *p_dev = NULL;
  827. list_node_t *p_node = NULL;
  828. for (p_node = list_begin(btm_cb.p_sec_dev_rec_list); p_node; p_node = list_next(p_node)) {
  829. p_dev = list_node(p_node);
  830. if ((p_dev->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) &&
  831. (p_dev->ble.in_controller_list & BTM_WHITE_LIST_BIT)) {
  832. btm_ble_enable_resolving_list(rl_mask);
  833. return;
  834. }
  835. }
  836. btm_ble_disable_resolving_list(rl_mask, TRUE);
  837. }
  838. /*******************************************************************************
  839. **
  840. ** Function btm_ble_resolving_list_init
  841. **
  842. ** Description Initialize resolving list in host stack
  843. **
  844. ** Parameters Max resolving list size
  845. **
  846. ** Returns void
  847. **
  848. *******************************************************************************/
  849. void btm_ble_resolving_list_init(UINT8 max_irk_list_sz)
  850. {
  851. tBTM_BLE_RESOLVE_Q *p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
  852. UINT8 irk_mask_size = (max_irk_list_sz % 8) ?
  853. (max_irk_list_sz / 8 + 1) : (max_irk_list_sz / 8);
  854. if (max_irk_list_sz > 0) {
  855. p_q->resolve_q_random_pseudo = (BD_ADDR *)osi_malloc(sizeof(BD_ADDR) * max_irk_list_sz);
  856. p_q->resolve_q_action = (UINT8 *)osi_malloc(max_irk_list_sz);
  857. /* RPA offloading feature */
  858. if (btm_cb.ble_ctr_cb.irk_list_mask == NULL) {
  859. btm_cb.ble_ctr_cb.irk_list_mask = (UINT8 *)osi_malloc(irk_mask_size);
  860. }
  861. BTM_TRACE_DEBUG ("%s max_irk_list_sz = %d", __func__, max_irk_list_sz);
  862. }
  863. controller_get_interface()->set_ble_resolving_list_max_size(max_irk_list_sz);
  864. btm_ble_clear_resolving_list();
  865. btm_cb.ble_ctr_cb.resolving_list_avail_size = max_irk_list_sz;
  866. }
  867. /*******************************************************************************
  868. **
  869. ** Function btm_ble_resolving_list_cleanup
  870. **
  871. ** Description Cleanup resolving list dynamic memory
  872. **
  873. ** Parameters
  874. **
  875. ** Returns void
  876. **
  877. *******************************************************************************/
  878. void btm_ble_resolving_list_cleanup(void)
  879. {
  880. tBTM_BLE_RESOLVE_Q *p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
  881. if (p_q->resolve_q_random_pseudo) {
  882. osi_free(p_q->resolve_q_random_pseudo);
  883. p_q->resolve_q_random_pseudo = NULL;
  884. }
  885. if (p_q->resolve_q_action) {
  886. osi_free(p_q->resolve_q_action);
  887. p_q->resolve_q_action = NULL;
  888. }
  889. controller_get_interface()->set_ble_resolving_list_max_size(0);
  890. if (btm_cb.ble_ctr_cb.irk_list_mask) {
  891. osi_free(btm_cb.ble_ctr_cb.irk_list_mask);
  892. btm_cb.ble_ctr_cb.irk_list_mask = NULL;
  893. }
  894. }
  895. #endif