btm_ble_privacy.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. for (UINT8 i = 0; i < BTM_SEC_MAX_DEVICE_RECORDS; ++i) {
  226. btm_cb.sec_dev_rec[i].ble.in_controller_list &= ~BTM_RESOLVING_LIST_BIT;
  227. }
  228. }
  229. }
  230. /*******************************************************************************
  231. **
  232. ** Function btm_ble_add_resolving_list_entry_complete
  233. **
  234. ** Description This function is called when command complete for
  235. ** add resolving list entry
  236. **
  237. ** Returns void
  238. **
  239. *******************************************************************************/
  240. void btm_ble_add_resolving_list_entry_complete(UINT8 *p, UINT16 evt_len)
  241. {
  242. UINT8 status;
  243. STREAM_TO_UINT8(status, p);
  244. BTM_TRACE_DEBUG("%s status = %d", __func__, status);
  245. BD_ADDR pseudo_bda;
  246. if (!btm_ble_deq_resolving_pending(pseudo_bda)) {
  247. BTM_TRACE_DEBUG("no pending resolving list operation");
  248. return;
  249. }
  250. if (status == HCI_SUCCESS) {
  251. /* privacy 1.2 command complete does not have these extra byte */
  252. if (evt_len > 2) {
  253. /* VSC complete has one extra byte for op code, skip it here */
  254. p ++;
  255. STREAM_TO_UINT8(btm_cb.ble_ctr_cb.resolving_list_avail_size, p);
  256. } else {
  257. btm_cb.ble_ctr_cb.resolving_list_avail_size --;
  258. }
  259. } else if (status == HCI_ERR_MEMORY_FULL) { /* BT_ERROR_CODE_MEMORY_CAPACITY_EXCEEDED */
  260. btm_cb.ble_ctr_cb.resolving_list_avail_size = 0;
  261. BTM_TRACE_WARNING("%s Resolving list Full ", __func__);
  262. } else {
  263. BTM_TRACE_ERROR("%s Add resolving list error %d ", __func__, status);
  264. }
  265. }
  266. /*******************************************************************************
  267. **
  268. ** Function btm_ble_remove_resolving_list_entry_complete
  269. **
  270. ** Description This function is called when command complete for
  271. ** remove resolving list entry
  272. **
  273. ** Returns void
  274. **
  275. *******************************************************************************/
  276. void btm_ble_remove_resolving_list_entry_complete(UINT8 *p, UINT16 evt_len)
  277. {
  278. BD_ADDR pseudo_bda;
  279. UINT8 status;
  280. STREAM_TO_UINT8(status, p);
  281. BTM_TRACE_DEBUG("%s status = %d", __func__, status);
  282. if (!btm_ble_deq_resolving_pending(pseudo_bda)) {
  283. BTM_TRACE_ERROR("%s no pending resolving list operation", __func__);
  284. return;
  285. }
  286. if (status == HCI_SUCCESS) {
  287. /* proprietary: spec does not have these extra bytes */
  288. if (evt_len > 2) {
  289. p ++; /* skip opcode */
  290. STREAM_TO_UINT8(btm_cb.ble_ctr_cb.resolving_list_avail_size, p);
  291. } else {
  292. btm_cb.ble_ctr_cb.resolving_list_avail_size++;
  293. }
  294. }
  295. }
  296. /*******************************************************************************
  297. **
  298. ** Function btm_ble_read_resolving_list_entry_complete
  299. **
  300. ** Description This function is called when command complete for
  301. ** remove resolving list entry
  302. **
  303. ** Returns void
  304. **
  305. *******************************************************************************/
  306. void btm_ble_read_resolving_list_entry_complete(UINT8 *p, UINT16 evt_len)
  307. {
  308. UINT8 status, rra_type = BTM_BLE_ADDR_PSEUDO;
  309. BD_ADDR rra, pseudo_bda;
  310. STREAM_TO_UINT8 (status, p);
  311. BTM_TRACE_DEBUG("%s status = %d", __func__, status);
  312. if (!btm_ble_deq_resolving_pending(pseudo_bda)) {
  313. BTM_TRACE_ERROR("no pending resolving list operation");
  314. return;
  315. }
  316. if (status == HCI_SUCCESS) {
  317. /* proprietary spec has extra bytes */
  318. if (evt_len > 8) {
  319. p += (2 + 16 + 1 + 6); /* skip subcode, index, IRK value, address type, identity addr type */
  320. STREAM_TO_BDADDR(rra, p);
  321. BTM_TRACE_ERROR("%s peer_addr: %02x:%02x:%02x:%02x:%02x:%02x",
  322. __func__, rra[0], rra[1], rra[2], rra[3], rra[4], rra[5]);
  323. } else {
  324. STREAM_TO_BDADDR(rra, p);
  325. }
  326. btm_ble_refresh_peer_resolvable_private_addr(pseudo_bda, rra, rra_type);
  327. }
  328. }
  329. /*******************************************************************************
  330. VSC that implement controller based privacy
  331. ********************************************************************************/
  332. /*******************************************************************************
  333. **
  334. ** Function btm_ble_resolving_list_vsc_op_cmpl
  335. **
  336. ** Description IRK operation VSC complete handler
  337. **
  338. ** Parameters
  339. **
  340. ** Returns void
  341. **
  342. *******************************************************************************/
  343. void btm_ble_resolving_list_vsc_op_cmpl (tBTM_VSC_CMPL *p_params)
  344. {
  345. UINT8 *p = p_params->p_param_buf, op_subcode;
  346. UINT16 evt_len = p_params->param_len;
  347. op_subcode = *(p + 1);
  348. BTM_TRACE_DEBUG("%s op_subcode = %d", __func__, op_subcode);
  349. if (op_subcode == BTM_BLE_META_CLEAR_IRK_LIST) {
  350. btm_ble_clear_resolving_list_complete(p, evt_len);
  351. } else if (op_subcode == BTM_BLE_META_ADD_IRK_ENTRY) {
  352. btm_ble_add_resolving_list_entry_complete(p, evt_len);
  353. } else if (op_subcode == BTM_BLE_META_REMOVE_IRK_ENTRY) {
  354. btm_ble_remove_resolving_list_entry_complete(p, evt_len);
  355. } else if (op_subcode == BTM_BLE_META_READ_IRK_ENTRY) {
  356. btm_ble_read_resolving_list_entry_complete(p, evt_len);
  357. } else if (op_subcode == BTM_BLE_META_IRK_ENABLE) {
  358. /* RPA offloading enable/disabled */
  359. }
  360. }
  361. /*******************************************************************************
  362. **
  363. ** Function btm_ble_remove_resolving_list_entry
  364. **
  365. ** Description This function to remove an IRK entry from the list
  366. **
  367. ** Parameters ble_addr_type: address type
  368. ** ble_addr: LE adddress
  369. **
  370. ** Returns status
  371. **
  372. *******************************************************************************/
  373. tBTM_STATUS btm_ble_remove_resolving_list_entry(tBTM_SEC_DEV_REC *p_dev_rec)
  374. {
  375. /* if controller does not support RPA offloading or privacy 1.2, skip */
  376. if (controller_get_interface()->get_ble_resolving_list_max_size() == 0) {
  377. return BTM_WRONG_MODE;
  378. }
  379. tBTM_STATUS st = BTM_NO_RESOURCES;
  380. if (controller_get_interface()->supports_ble_privacy()) {
  381. if (btsnd_hcic_ble_rm_device_resolving_list(p_dev_rec->ble.static_addr_type,
  382. p_dev_rec->ble.static_addr)) {
  383. st = BTM_CMD_STARTED;
  384. }
  385. } else {
  386. UINT8 param[20] = {0};
  387. UINT8 *p = param;
  388. UINT8_TO_STREAM(p, BTM_BLE_META_REMOVE_IRK_ENTRY);
  389. UINT8_TO_STREAM(p, p_dev_rec->ble.static_addr_type);
  390. BDADDR_TO_STREAM(p, p_dev_rec->ble.static_addr);
  391. st = BTM_VendorSpecificCommand(HCI_VENDOR_BLE_RPA_VSC,
  392. BTM_BLE_META_REMOVE_IRK_LEN,
  393. param,
  394. btm_ble_resolving_list_vsc_op_cmpl);
  395. }
  396. if (st == BTM_CMD_STARTED) {
  397. btm_ble_enq_resolving_list_pending( p_dev_rec->bd_addr, BTM_BLE_META_REMOVE_IRK_ENTRY);
  398. }
  399. return st;
  400. }
  401. /*******************************************************************************
  402. **
  403. ** Function btm_ble_clear_resolving_list
  404. **
  405. ** Description This function clears the resolving list
  406. **
  407. ** Parameters None.
  408. **
  409. ** Returns status
  410. **
  411. *******************************************************************************/
  412. tBTM_STATUS btm_ble_clear_resolving_list(void)
  413. {
  414. tBTM_STATUS st = BTM_NO_RESOURCES;
  415. if (controller_get_interface()->supports_ble_privacy()) {
  416. if (btsnd_hcic_ble_clear_resolving_list()) {
  417. st = BTM_SUCCESS;
  418. }
  419. } else {
  420. UINT8 param[20] = {0};
  421. UINT8 *p = param;
  422. UINT8_TO_STREAM(p, BTM_BLE_META_CLEAR_IRK_LIST);
  423. st = BTM_VendorSpecificCommand (HCI_VENDOR_BLE_RPA_VSC,
  424. BTM_BLE_META_CLEAR_IRK_LEN,
  425. param,
  426. btm_ble_resolving_list_vsc_op_cmpl);
  427. }
  428. return st;
  429. }
  430. /*******************************************************************************
  431. **
  432. ** Function btm_ble_read_resolving_list_entry
  433. **
  434. ** Description This function read an IRK entry by index
  435. **
  436. ** Parameters entry index.
  437. **
  438. ** Returns status
  439. **
  440. *******************************************************************************/
  441. tBTM_STATUS btm_ble_read_resolving_list_entry(tBTM_SEC_DEV_REC *p_dev_rec)
  442. {
  443. tBTM_STATUS st = BTM_NO_RESOURCES;
  444. if (!(p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT)) {
  445. return BTM_WRONG_MODE;
  446. }
  447. if (controller_get_interface()->supports_ble_privacy()) {
  448. if (btsnd_hcic_ble_read_resolvable_addr_peer(p_dev_rec->ble.static_addr_type,
  449. p_dev_rec->ble.static_addr)) {
  450. st = BTM_CMD_STARTED;
  451. }
  452. } else {
  453. UINT8 param[20] = {0};
  454. UINT8 *p = param;
  455. UINT8_TO_STREAM(p, BTM_BLE_META_READ_IRK_ENTRY);
  456. UINT8_TO_STREAM(p, p_dev_rec->ble.resolving_list_index);
  457. st = BTM_VendorSpecificCommand (HCI_VENDOR_BLE_RPA_VSC,
  458. BTM_BLE_META_READ_IRK_LEN,
  459. param,
  460. btm_ble_resolving_list_vsc_op_cmpl);
  461. }
  462. if (st == BTM_CMD_STARTED) {
  463. btm_ble_enq_resolving_list_pending(p_dev_rec->bd_addr,
  464. BTM_BLE_META_READ_IRK_ENTRY);
  465. }
  466. return st;
  467. }
  468. /*******************************************************************************
  469. **
  470. ** Function btm_ble_suspend_resolving_list_activity
  471. **
  472. ** Description This function suspends all resolving list activity, including
  473. ** scan, initiating, and advertising, if resolving list is being
  474. ** enabled.
  475. **
  476. ** Parameters
  477. **
  478. ** Returns TRUE if suspended; FALSE otherwise
  479. **
  480. *******************************************************************************/
  481. BOOLEAN btm_ble_suspend_resolving_list_activity(void)
  482. {
  483. tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
  484. /* if resolving list is not enabled, do not need to terminate any activity */
  485. /* if asking for stop all activity */
  486. /* if already suspended */
  487. if (p_ble_cb->suspended_rl_state != BTM_BLE_RL_IDLE) {
  488. return TRUE;
  489. }
  490. /* direct connection active, wait until it completed */
  491. if (btm_ble_get_conn_st() == BLE_DIR_CONN) {
  492. BTM_TRACE_ERROR("resolving list can not be edited, EnQ now");
  493. return FALSE;
  494. }
  495. p_ble_cb->suspended_rl_state = BTM_BLE_RL_IDLE;
  496. if (p_ble_cb->inq_var.adv_mode == BTM_BLE_ADV_ENABLE) {
  497. btm_ble_stop_adv();
  498. p_ble_cb->suspended_rl_state |= BTM_BLE_RL_ADV;
  499. }
  500. if (BTM_BLE_IS_SCAN_ACTIVE(p_ble_cb->scan_activity)) {
  501. btm_ble_stop_scan();
  502. p_ble_cb->suspended_rl_state |= BTM_BLE_RL_SCAN;
  503. }
  504. if (btm_ble_suspend_bg_conn()) {
  505. p_ble_cb->suspended_rl_state |= BTM_BLE_RL_INIT;
  506. }
  507. return TRUE;
  508. }
  509. /*******************************************************************************
  510. **
  511. ** Function btm_ble_resume_resolving_list_activity
  512. **
  513. ** Description This function resumes the resolving list activity, including
  514. ** scanning, initiating, and advertising, if any of these
  515. ** activities has been suspended earlier.
  516. **
  517. ** Returns none
  518. **
  519. *******************************************************************************/
  520. void btm_ble_resume_resolving_list_activity(void)
  521. {
  522. tBTM_BLE_CB *p_ble_cb = &btm_cb.ble_ctr_cb;
  523. if (p_ble_cb->suspended_rl_state & BTM_BLE_RL_ADV) {
  524. btm_ble_start_adv();
  525. }
  526. if (p_ble_cb->suspended_rl_state & BTM_BLE_RL_SCAN) {
  527. btm_ble_start_scan();
  528. }
  529. if (p_ble_cb->suspended_rl_state & BTM_BLE_RL_INIT) {
  530. btm_ble_resume_bg_conn();
  531. }
  532. p_ble_cb->suspended_rl_state = BTM_BLE_RL_IDLE;
  533. }
  534. /*******************************************************************************
  535. **
  536. ** Function btm_ble_vendor_enable_irk_feature
  537. **
  538. ** Description This function is called to enable or disable the RRA
  539. ** offloading feature.
  540. **
  541. ** Parameters enable: enable or disable the RRA offloading feature
  542. **
  543. ** Returns BTM_SUCCESS if successful
  544. **
  545. *******************************************************************************/
  546. tBTM_STATUS btm_ble_vendor_enable_irk_feature(BOOLEAN enable)
  547. {
  548. UINT8 param[20], *p;
  549. tBTM_STATUS st = BTM_MODE_UNSUPPORTED;
  550. p = param;
  551. memset(param, 0, 20);
  552. /* select feature based on control block settings */
  553. UINT8_TO_STREAM(p, BTM_BLE_META_IRK_ENABLE);
  554. UINT8_TO_STREAM(p, enable ? 0x01 : 0x00);
  555. st = BTM_VendorSpecificCommand (HCI_VENDOR_BLE_RPA_VSC, BTM_BLE_IRK_ENABLE_LEN,
  556. param, btm_ble_resolving_list_vsc_op_cmpl);
  557. return st;
  558. }
  559. /*******************************************************************************
  560. **
  561. ** Function btm_ble_exe_disable_resolving_list
  562. **
  563. ** Description execute resolving list disable
  564. **
  565. ** Returns none
  566. **
  567. *******************************************************************************/
  568. BOOLEAN btm_ble_exe_disable_resolving_list(void)
  569. {
  570. if (!btm_ble_suspend_resolving_list_activity()) {
  571. return FALSE;
  572. }
  573. if (!controller_get_interface()->supports_ble_privacy()) {
  574. btm_ble_vendor_enable_irk_feature(FALSE);
  575. } else {
  576. btsnd_hcic_ble_set_addr_resolution_enable(FALSE);
  577. }
  578. return TRUE;
  579. }
  580. /*******************************************************************************
  581. **
  582. ** Function btm_ble_exe_enable_resolving_list
  583. **
  584. ** Description enable LE resolve address list
  585. **
  586. ** Returns none
  587. **
  588. *******************************************************************************/
  589. void btm_ble_exe_enable_resolving_list(void)
  590. {
  591. if (!btm_ble_suspend_resolving_list_activity()) {
  592. return;
  593. }
  594. if (!controller_get_interface()->supports_ble_privacy()) {
  595. btm_ble_vendor_enable_irk_feature(TRUE);
  596. } else {
  597. btsnd_hcic_ble_set_addr_resolution_enable(TRUE);
  598. }
  599. }
  600. /*******************************************************************************
  601. **
  602. ** Function btm_ble_disable_resolving_list
  603. **
  604. ** Description Disable LE Address resolution
  605. **
  606. ** Returns none
  607. **
  608. *******************************************************************************/
  609. BOOLEAN btm_ble_disable_resolving_list(UINT8 rl_mask, BOOLEAN to_resume )
  610. {
  611. UINT8 rl_state = btm_cb.ble_ctr_cb.rl_state;
  612. /* if controller does not support RPA offloading or privacy 1.2, skip */
  613. if (controller_get_interface()->get_ble_resolving_list_max_size() == 0) {
  614. return FALSE;
  615. }
  616. btm_cb.ble_ctr_cb.rl_state &= ~rl_mask;
  617. if (rl_state != BTM_BLE_RL_IDLE && btm_cb.ble_ctr_cb.rl_state == BTM_BLE_RL_IDLE) {
  618. if (btm_ble_exe_disable_resolving_list()) {
  619. if (to_resume) {
  620. btm_ble_resume_resolving_list_activity();
  621. }
  622. return TRUE;
  623. } else {
  624. return FALSE;
  625. }
  626. }
  627. return TRUE;
  628. }
  629. /*******************************************************************************
  630. **
  631. ** Function btm_ble_resolving_list_load_dev
  632. **
  633. ** Description This function add a device which is using RPA into white list
  634. **
  635. ** Parameters pointer to device security record
  636. **
  637. ** Returns TRUE if device added, otherwise falase.
  638. **
  639. *******************************************************************************/
  640. BOOLEAN btm_ble_resolving_list_load_dev(tBTM_SEC_DEV_REC *p_dev_rec)
  641. {
  642. BOOLEAN rt = FALSE;
  643. #if (SMP_INCLUDED == TRUE)
  644. UINT8 rl_mask = btm_cb.ble_ctr_cb.rl_state;
  645. BTM_TRACE_DEBUG("%s btm_cb.ble_ctr_cb.privacy_mode = %d\n", __func__,
  646. btm_cb.ble_ctr_cb.privacy_mode);
  647. /* if controller does not support RPA offloading or privacy 1.2, skip */
  648. if (controller_get_interface()->get_ble_resolving_list_max_size() == 0) {
  649. return FALSE;
  650. }
  651. BTM_TRACE_DEBUG("%s btm_cb.ble_ctr_cb.privacy_mode = %d\n",
  652. __func__, btm_cb.ble_ctr_cb.privacy_mode);
  653. /* only add RPA enabled device into resolving list */
  654. if (p_dev_rec != NULL && /* RPA is being used and PID is known */
  655. (p_dev_rec->sec_flags & BTM_SEC_IN_USE) != 0 &&
  656. ((p_dev_rec->ble.key_type & BTM_LE_KEY_PID) != 0 ||
  657. (p_dev_rec->ble.key_type & BTM_LE_KEY_LID) != 0)) {
  658. if (!(p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) &&
  659. btm_ble_brcm_find_resolving_pending_entry(p_dev_rec->bd_addr,
  660. BTM_BLE_META_ADD_IRK_ENTRY) == FALSE) {
  661. if (btm_cb.ble_ctr_cb.resolving_list_avail_size > 0) {
  662. if (rl_mask) {
  663. if (!btm_ble_disable_resolving_list (rl_mask, FALSE)) {
  664. return FALSE;
  665. }
  666. }
  667. btm_ble_update_resolving_list(p_dev_rec->bd_addr, TRUE);
  668. if (controller_get_interface()->supports_ble_privacy()) {
  669. BD_ADDR dummy_bda = {0};
  670. if (memcmp(p_dev_rec->ble.static_addr, dummy_bda, BD_ADDR_LEN) == 0) {
  671. memcpy(p_dev_rec->ble.static_addr, p_dev_rec->bd_addr, BD_ADDR_LEN);
  672. p_dev_rec->ble.static_addr_type = p_dev_rec->ble.ble_addr_type;
  673. }
  674. /* It will cause that scanner doesn't send scan request to advertiser
  675. * which has sent IRK to us and we have stored the IRK in controller.
  676. * It is a hardware limitation. The preliminary solution is not to
  677. * send key to the controller, but to resolve the random address in host. */
  678. /*
  679. BTM_TRACE_DEBUG("%s:adding device to controller resolving list\n", __func__);
  680. UINT8 *peer_irk = p_dev_rec->ble.keys.irk;
  681. UINT8 *local_irk = btm_cb.devcb.id_keys.irk;
  682. //use identical IRK for now
  683. rt = btsnd_hcic_ble_add_device_resolving_list(p_dev_rec->ble.static_addr_type,
  684. p_dev_rec->ble.static_addr, peer_irk, local_irk);
  685. */
  686. } else {
  687. UINT8 param[40] = {0};
  688. UINT8 *p = param;
  689. UINT8_TO_STREAM(p, BTM_BLE_META_ADD_IRK_ENTRY);
  690. ARRAY_TO_STREAM(p, p_dev_rec->ble.keys.irk, BT_OCTET16_LEN);
  691. UINT8_TO_STREAM(p, p_dev_rec->ble.static_addr_type);
  692. BDADDR_TO_STREAM(p, p_dev_rec->ble.static_addr);
  693. if (BTM_VendorSpecificCommand (HCI_VENDOR_BLE_RPA_VSC,
  694. BTM_BLE_META_ADD_IRK_LEN,
  695. param,
  696. btm_ble_resolving_list_vsc_op_cmpl)
  697. == BTM_CMD_STARTED) {
  698. rt = TRUE;
  699. }
  700. }
  701. if (rt) {
  702. btm_ble_enq_resolving_list_pending(p_dev_rec->bd_addr,
  703. BTM_BLE_META_ADD_IRK_ENTRY);
  704. }
  705. /* if resolving list has been turned on, re-enable it */
  706. if (rl_mask) {
  707. btm_ble_enable_resolving_list(rl_mask);
  708. } else {
  709. btm_ble_enable_resolving_list(BTM_BLE_RL_INIT);
  710. }
  711. } else {
  712. BTM_TRACE_WARNING("%s Resolving list full ", __func__);
  713. }
  714. } else {
  715. BTM_TRACE_DEBUG("Device already in Resolving list\n");
  716. rt = TRUE;
  717. }
  718. } else {
  719. BTM_TRACE_DEBUG("Device not a RPA enabled device\n");
  720. }
  721. #endif ///SMP_INCLUDED == TRUE
  722. return rt;
  723. }
  724. /*******************************************************************************
  725. **
  726. ** Function btm_ble_resolving_list_remove_dev
  727. **
  728. ** Description This function removes the device from resolving list
  729. **
  730. ** Parameters
  731. **
  732. ** Returns status
  733. **
  734. *******************************************************************************/
  735. void btm_ble_resolving_list_remove_dev(tBTM_SEC_DEV_REC *p_dev_rec)
  736. {
  737. UINT8 rl_mask = btm_cb.ble_ctr_cb.rl_state;
  738. BTM_TRACE_EVENT ("%s\n", __func__);
  739. if (rl_mask) {
  740. if (!btm_ble_disable_resolving_list (rl_mask, FALSE)) {
  741. return;
  742. }
  743. }
  744. if ((p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) &&
  745. btm_ble_brcm_find_resolving_pending_entry(p_dev_rec->bd_addr,
  746. BTM_BLE_META_REMOVE_IRK_ENTRY) == FALSE) {
  747. btm_ble_update_resolving_list( p_dev_rec->bd_addr, FALSE);
  748. btm_ble_remove_resolving_list_entry(p_dev_rec);
  749. } else {
  750. BTM_TRACE_DEBUG("Device not in resolving list\n");
  751. }
  752. /* if resolving list has been turned on, re-enable it */
  753. if (rl_mask) {
  754. btm_ble_enable_resolving_list(rl_mask);
  755. }
  756. }
  757. /*******************************************************************************
  758. **
  759. ** Function btm_ble_enable_resolving_list
  760. **
  761. ** Description enable LE resolve address list
  762. **
  763. ** Returns none
  764. **
  765. *******************************************************************************/
  766. void btm_ble_enable_resolving_list(UINT8 rl_mask)
  767. {
  768. UINT8 rl_state = btm_cb.ble_ctr_cb.rl_state;
  769. btm_cb.ble_ctr_cb.rl_state |= rl_mask;
  770. if (rl_state == BTM_BLE_RL_IDLE &&
  771. btm_cb.ble_ctr_cb.rl_state != BTM_BLE_RL_IDLE &&
  772. controller_get_interface()->get_ble_resolving_list_max_size() != 0) {
  773. btm_ble_exe_enable_resolving_list();
  774. btm_ble_resume_resolving_list_activity();
  775. }
  776. }
  777. #if 0 //Unused
  778. /*******************************************************************************
  779. **
  780. ** Function btm_ble_resolving_list_empty
  781. **
  782. ** Description check to see if resoving list is empty or not
  783. **
  784. ** Returns TRUE: empty; FALSE non-empty
  785. **
  786. *******************************************************************************/
  787. BOOLEAN btm_ble_resolving_list_empty(void)
  788. {
  789. return (controller_get_interface()->get_ble_resolving_list_max_size() ==
  790. btm_cb.ble_ctr_cb.resolving_list_avail_size);
  791. }
  792. #endif
  793. /*******************************************************************************
  794. **
  795. ** Function btm_ble_enable_resolving_list_for_platform
  796. **
  797. ** Description enable/disable resolving list feature depending on if any
  798. ** resolving list is empty and whitelist is involoved in the
  799. ** operation.
  800. **
  801. ** Returns none
  802. **
  803. *******************************************************************************/
  804. void btm_ble_enable_resolving_list_for_platform (UINT8 rl_mask)
  805. {
  806. /* if controller does not support, skip */
  807. if (controller_get_interface()->get_ble_resolving_list_max_size() == 0) {
  808. return;
  809. }
  810. if (btm_cb.ble_ctr_cb.wl_state == BTM_BLE_WL_IDLE) {
  811. if (controller_get_interface()->get_ble_resolving_list_max_size() >
  812. btm_cb.ble_ctr_cb.resolving_list_avail_size) {
  813. btm_ble_enable_resolving_list(rl_mask);
  814. } else {
  815. btm_ble_disable_resolving_list(rl_mask, TRUE);
  816. }
  817. return;
  818. }
  819. tBTM_SEC_DEV_REC *p_dev = &btm_cb.sec_dev_rec[0];
  820. for (UINT8 i = 0; i < BTM_SEC_MAX_DEVICE_RECORDS; i ++, p_dev ++) {
  821. if ((p_dev->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) &&
  822. (p_dev->ble.in_controller_list & BTM_WHITE_LIST_BIT)) {
  823. btm_ble_enable_resolving_list(rl_mask);
  824. return;
  825. }
  826. }
  827. btm_ble_disable_resolving_list(rl_mask, TRUE);
  828. }
  829. /*******************************************************************************
  830. **
  831. ** Function btm_ble_resolving_list_init
  832. **
  833. ** Description Initialize resolving list in host stack
  834. **
  835. ** Parameters Max resolving list size
  836. **
  837. ** Returns void
  838. **
  839. *******************************************************************************/
  840. void btm_ble_resolving_list_init(UINT8 max_irk_list_sz)
  841. {
  842. tBTM_BLE_RESOLVE_Q *p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
  843. UINT8 irk_mask_size = (max_irk_list_sz % 8) ?
  844. (max_irk_list_sz / 8 + 1) : (max_irk_list_sz / 8);
  845. if (max_irk_list_sz > 0) {
  846. p_q->resolve_q_random_pseudo = (BD_ADDR *)osi_malloc(sizeof(BD_ADDR) * max_irk_list_sz);
  847. p_q->resolve_q_action = (UINT8 *)osi_malloc(max_irk_list_sz);
  848. /* RPA offloading feature */
  849. if (btm_cb.ble_ctr_cb.irk_list_mask == NULL) {
  850. btm_cb.ble_ctr_cb.irk_list_mask = (UINT8 *)osi_malloc(irk_mask_size);
  851. }
  852. BTM_TRACE_DEBUG ("%s max_irk_list_sz = %d", __func__, max_irk_list_sz);
  853. }
  854. controller_get_interface()->set_ble_resolving_list_max_size(max_irk_list_sz);
  855. btm_ble_clear_resolving_list();
  856. btm_cb.ble_ctr_cb.resolving_list_avail_size = max_irk_list_sz;
  857. }
  858. /*******************************************************************************
  859. **
  860. ** Function btm_ble_resolving_list_cleanup
  861. **
  862. ** Description Cleanup resolving list dynamic memory
  863. **
  864. ** Parameters
  865. **
  866. ** Returns void
  867. **
  868. *******************************************************************************/
  869. void btm_ble_resolving_list_cleanup(void)
  870. {
  871. tBTM_BLE_RESOLVE_Q *p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
  872. if (p_q->resolve_q_random_pseudo) {
  873. osi_free(p_q->resolve_q_random_pseudo);
  874. p_q->resolve_q_random_pseudo = NULL;
  875. }
  876. if (p_q->resolve_q_action) {
  877. osi_free(p_q->resolve_q_action);
  878. p_q->resolve_q_action = NULL;
  879. }
  880. controller_get_interface()->set_ble_resolving_list_max_size(0);
  881. if (btm_cb.ble_ctr_cb.irk_list_mask) {
  882. osi_free(btm_cb.ble_ctr_cb.irk_list_mask);
  883. btm_cb.ble_ctr_cb.irk_list_mask = NULL;
  884. }
  885. }
  886. #endif