1 /******************************************************************************
2  *
3  *  Copyright 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  *
21  *  This file contains functions for BLE controller based privacy.
22  *
23  ******************************************************************************/
24 #define LOG_TAG "ble_priv"
25 
26 #include "stack/include/btm_ble_privacy.h"
27 
28 #include <bluetooth/log.h>
29 
30 #include "btm_dev.h"
31 #include "btm_sec_cb.h"
32 #include "btm_sec_int_types.h"
33 #include "hci/controller_interface.h"
34 #include "main/shim/acl_api.h"
35 #include "main/shim/entry.h"
36 #include "os/log.h"
37 #include "osi/include/allocator.h"
38 #include "stack/btm/btm_int_types.h"
39 #include "stack/include/bt_octets.h"
40 #include "stack/include/bt_types.h"
41 #include "stack/include/btm_api.h"
42 #include "types/raw_address.h"
43 
44 using namespace bluetooth;
45 
46 extern tBTM_CB btm_cb;
47 
48 /* RPA offload VSC specifics */
49 #define HCI_VENDOR_BLE_RPA_VSC (0x0155 | HCI_GRP_VENDOR_SPECIFIC)
50 
51 #define BTM_BLE_META_IRK_ENABLE 0x01
52 #define BTM_BLE_META_ADD_IRK_ENTRY 0x02
53 #define BTM_BLE_META_REMOVE_IRK_ENTRY 0x03
54 #define BTM_BLE_META_CLEAR_IRK_LIST 0x04
55 #define BTM_BLE_META_READ_IRK_ENTRY 0x05
56 #define BTM_BLE_META_CS_RESOLVE_ADDR 0x00000001
57 #define BTM_BLE_IRK_ENABLE_LEN 2
58 
59 #define BTM_BLE_META_ADD_IRK_LEN 24
60 #define BTM_BLE_META_REMOVE_IRK_LEN 8
61 #define BTM_BLE_META_CLEAR_IRK_LEN 1
62 #define BTM_BLE_META_READ_IRK_LEN 2
63 #define BTM_BLE_META_ADD_WL_ATTR_LEN 9
64 
65 /*******************************************************************************
66  *         Functions implemented controller based privacy using Resolving List
67  ******************************************************************************/
68 /*******************************************************************************
69  *
70  * Function         btm_ble_enq_resolving_list_pending
71  *
72  * Description      add target address into resolving pending operation queue
73  *
74  * Parameters       target_bda: target device address
75  *                  add_entry: true for add entry, false for remove entry
76  *
77  * Returns          void
78  *
79  ******************************************************************************/
btm_ble_enq_resolving_list_pending(const RawAddress & pseudo_bda,uint8_t op_code)80 static void btm_ble_enq_resolving_list_pending(const RawAddress& pseudo_bda,
81                                                uint8_t op_code) {
82   tBTM_BLE_RESOLVE_Q* p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
83 
84   p_q->resolve_q_random_pseudo[p_q->q_next] = pseudo_bda;
85   p_q->resolve_q_action[p_q->q_next] = op_code;
86   p_q->q_next++;
87   p_q->q_next %= bluetooth::shim::GetController()->GetLeResolvingListSize();
88 }
89 
90 /*******************************************************************************
91  *
92  * Function         btm_ble_brcm_find_resolving_pending_entry
93  *
94  * Description      check to see if the action is in pending list
95  *
96  * Parameters       true: action pending;
97  *                  false: new action
98  *
99  * Returns          void
100  *
101  ******************************************************************************/
btm_ble_brcm_find_resolving_pending_entry(const RawAddress & pseudo_addr,uint8_t action)102 static bool btm_ble_brcm_find_resolving_pending_entry(
103     const RawAddress& pseudo_addr, uint8_t action) {
104   tBTM_BLE_RESOLVE_Q* p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
105 
106   for (uint8_t i = p_q->q_pending; i != p_q->q_next;) {
107     if (p_q->resolve_q_random_pseudo[i] == pseudo_addr &&
108         action == p_q->resolve_q_action[i])
109       return true;
110 
111     i++;
112     i %= bluetooth::shim::GetController()->GetLeResolvingListSize();
113   }
114   return false;
115 }
116 
117 /*******************************************************************************
118  *
119  * Function         btm_ble_deq_resolving_pending
120  *
121  * Description      dequeue target address from resolving pending operation
122  *                  queue
123  *
124  * Parameters       pseudo_addr: pseudo_addr device address
125  *
126  * Returns          void
127  *
128  ******************************************************************************/
btm_ble_deq_resolving_pending(RawAddress & pseudo_addr)129 static bool btm_ble_deq_resolving_pending(RawAddress& pseudo_addr) {
130   tBTM_BLE_RESOLVE_Q* p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
131 
132   if (p_q->q_next != p_q->q_pending) {
133     pseudo_addr = p_q->resolve_q_random_pseudo[p_q->q_pending];
134     p_q->resolve_q_random_pseudo[p_q->q_pending] = RawAddress::kEmpty;
135     p_q->q_pending++;
136     p_q->q_pending %=
137         bluetooth::shim::GetController()->GetLeResolvingListSize();
138     return true;
139   }
140 
141   return false;
142 }
143 
144 /*******************************************************************************
145  *
146  * Function         btm_ble_clear_irk_index
147  *
148  * Description      clear IRK list index mask for availability
149  *
150  * Returns          none
151  *
152  ******************************************************************************/
btm_ble_clear_irk_index(uint8_t index)153 static void btm_ble_clear_irk_index(uint8_t index) {
154   uint8_t byte;
155   uint8_t bit;
156 
157   if (index < bluetooth::shim::GetController()->GetLeResolvingListSize()) {
158     byte = index / 8;
159     bit = index % 8;
160     btm_cb.ble_ctr_cb.irk_list_mask[byte] &= (~(1 << bit));
161   }
162 }
163 
164 /*******************************************************************************
165  *
166  * Function         btm_ble_find_irk_index
167  *
168  * Description      find the first available IRK list index
169  *
170  * Returns          index from 0 ~ max (127 default)
171  *
172  ******************************************************************************/
btm_ble_find_irk_index(void)173 static uint8_t btm_ble_find_irk_index(void) {
174   uint8_t i = 0;
175   uint8_t byte;
176   uint8_t bit;
177 
178   while (i < bluetooth::shim::GetController()->GetLeResolvingListSize()) {
179     byte = i / 8;
180     bit = i % 8;
181 
182     if ((btm_cb.ble_ctr_cb.irk_list_mask[byte] & (1 << bit)) == 0) {
183       btm_cb.ble_ctr_cb.irk_list_mask[byte] |= (1 << bit);
184       return i;
185     }
186     i++;
187   }
188 
189   log::error("no index found");
190   return i;
191 }
192 
193 /*******************************************************************************
194  *
195  * Function         btm_ble_update_resolving_list
196  *
197  * Description      update resolving list entry in host maintained record
198  *
199  * Returns          void
200  *
201  ******************************************************************************/
btm_ble_update_resolving_list(const RawAddress & pseudo_bda,bool add)202 static void btm_ble_update_resolving_list(const RawAddress& pseudo_bda,
203                                           bool add) {
204   tBTM_SEC_DEV_REC* p_dev_rec = btm_find_dev(pseudo_bda);
205   if (p_dev_rec == NULL) return;
206 
207   if (add) {
208     p_dev_rec->ble.in_controller_list |= BTM_RESOLVING_LIST_BIT;
209     if (!bluetooth::shim::GetController()->SupportsBlePrivacy())
210       p_dev_rec->ble.resolving_list_index = btm_ble_find_irk_index();
211   } else {
212     p_dev_rec->ble.in_controller_list &= ~BTM_RESOLVING_LIST_BIT;
213     if (!bluetooth::shim::GetController()->SupportsBlePrivacy()) {
214       /* clear IRK list index mask */
215       btm_ble_clear_irk_index(p_dev_rec->ble.resolving_list_index);
216       p_dev_rec->ble.resolving_list_index = 0;
217     }
218   }
219 }
220 
clear_resolving_list_bit(void * data,void *)221 static bool clear_resolving_list_bit(void* data, void* /* context */) {
222   tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(data);
223   p_dev_rec->ble.in_controller_list &= ~BTM_RESOLVING_LIST_BIT;
224   return true;
225 }
226 
227 /*******************************************************************************
228  *
229  * Function         btm_ble_clear_resolving_list_complete
230  *
231  * Description      This function is called when command complete for
232  *                  clear resolving list
233  *
234  * Returns          void
235  *
236  ******************************************************************************/
btm_ble_clear_resolving_list_complete(uint8_t * p,uint16_t evt_len)237 void btm_ble_clear_resolving_list_complete(uint8_t* p, uint16_t evt_len) {
238   uint8_t status = 0;
239 
240   if (evt_len < 1) {
241     log::error("malformatted event packet: containing zero bytes");
242     return;
243   }
244 
245   STREAM_TO_UINT8(status, p);
246 
247   log::verbose("status={}", status);
248 
249   if (status == HCI_SUCCESS) {
250     if (evt_len >= 3) {
251       /* VSC complete has one extra byte for op code and list size, skip it here
252        */
253       p++;
254 
255       /* updated the available list size, and current list size */
256       uint8_t irk_list_sz_max = 0;
257       STREAM_TO_UINT8(irk_list_sz_max, p);
258 
259       if (bluetooth::shim::GetController()->GetLeResolvingListSize() == 0)
260         btm_ble_resolving_list_init(irk_list_sz_max);
261 
262       uint8_t irk_mask_size = (irk_list_sz_max % 8) ? (irk_list_sz_max / 8 + 1)
263                                                     : (irk_list_sz_max / 8);
264       memset(btm_cb.ble_ctr_cb.irk_list_mask, 0, irk_mask_size);
265     }
266 
267     btm_cb.ble_ctr_cb.resolving_list_avail_size =
268         bluetooth::shim::GetController()->GetLeResolvingListSize();
269 
270     log::verbose("resolving_list_avail_size={}",
271                  btm_cb.ble_ctr_cb.resolving_list_avail_size);
272 
273     list_foreach(btm_sec_cb.sec_dev_rec, clear_resolving_list_bit, NULL);
274   }
275 }
276 
277 /*******************************************************************************
278  *
279  * Function         btm_ble_add_resolving_list_entry_complete
280  *
281  * Description      This function is called when command complete for
282  *                  add resolving list entry
283  *
284  * Returns          void
285  *
286  ******************************************************************************/
btm_ble_add_resolving_list_entry_complete(uint8_t * p,uint16_t evt_len)287 void btm_ble_add_resolving_list_entry_complete(uint8_t* p, uint16_t evt_len) {
288   uint8_t status;
289 
290   if (evt_len < 1) {
291     log::error("malformatted event packet: containing zero byte");
292     return;
293   }
294 
295   STREAM_TO_UINT8(status, p);
296 
297   log::verbose("status={}", status);
298 
299   RawAddress pseudo_bda;
300   if (!btm_ble_deq_resolving_pending(pseudo_bda)) {
301     log::verbose("no pending resolving list operation");
302     return;
303   }
304 
305   if (status == HCI_SUCCESS) {
306     btm_ble_update_resolving_list(pseudo_bda, true);
307     /* privacy 1.2 command complete does not have these extra byte */
308     if (evt_len > 2) {
309       /* VSC complete has one extra byte for op code, skip it here */
310       p++;
311       STREAM_TO_UINT8(btm_cb.ble_ctr_cb.resolving_list_avail_size, p);
312     } else
313       btm_cb.ble_ctr_cb.resolving_list_avail_size--;
314   } else if (status ==
315              HCI_ERR_MEMORY_FULL) /* BT_ERROR_CODE_MEMORY_CAPACITY_EXCEEDED  */
316   {
317     btm_cb.ble_ctr_cb.resolving_list_avail_size = 0;
318     log::verbose("Resolving list Full");
319   }
320 }
321 
322 /*******************************************************************************
323  *
324  * Function         btm_ble_remove_resolving_list_entry_complete
325  *
326  * Description      This function is called when command complete for
327  *                  remove resolving list entry
328  *
329  * Returns          void
330  *
331  ******************************************************************************/
btm_ble_remove_resolving_list_entry_complete(uint8_t * p,uint16_t evt_len)332 void btm_ble_remove_resolving_list_entry_complete(uint8_t* p,
333                                                   uint16_t evt_len) {
334   RawAddress pseudo_bda;
335   uint8_t status;
336 
337   STREAM_TO_UINT8(status, p);
338 
339   log::verbose("status={}", status);
340 
341   if (!btm_ble_deq_resolving_pending(pseudo_bda)) {
342     log::error("no pending resolving list operation");
343     return;
344   }
345 
346   if (status == HCI_SUCCESS) {
347     /* proprietary: spec does not have these extra bytes */
348     if (evt_len > 2) {
349       p++; /* skip opcode */
350       STREAM_TO_UINT8(btm_cb.ble_ctr_cb.resolving_list_avail_size, p);
351     } else
352       btm_cb.ble_ctr_cb.resolving_list_avail_size++;
353   }
354 }
355 
356 /*******************************************************************************
357  *
358  * Function         btm_ble_read_resolving_list_entry_complete
359  *
360  * Description      This function is called when command complete for
361  *                  remove resolving list entry
362  *
363  * Returns          void
364  *
365  ******************************************************************************/
btm_ble_read_resolving_list_entry_complete(const uint8_t * p,uint16_t evt_len)366 void btm_ble_read_resolving_list_entry_complete(const uint8_t* p,
367                                                 uint16_t evt_len) {
368   uint8_t status;
369   RawAddress rra, pseudo_bda;
370 
371   STREAM_TO_UINT8(status, p);
372 
373   log::verbose("status={}", status);
374 
375   if (!btm_ble_deq_resolving_pending(pseudo_bda)) {
376     log::error("no pending resolving list operation");
377     return;
378   }
379 
380   if (status == HCI_SUCCESS) {
381     /* proprietary spec has extra bytes */
382     if (evt_len > 8) {
383       /* skip subcode, index, IRK value, address type, identity addr type */
384       p += (2 + 16 + 1 + 6);
385       STREAM_TO_BDADDR(rra, p);
386 
387       log::info("peer_addr:{}", rra);
388     } else {
389       STREAM_TO_BDADDR(rra, p);
390     }
391     btm_ble_refresh_peer_resolvable_private_addr(
392         pseudo_bda, rra, tBLE_RAND_ADDR_TYPE::BTM_BLE_ADDR_PSEUDO);
393   }
394 }
395 /*******************************************************************************
396                 VSC that implement controller based privacy
397  ******************************************************************************/
398 /*******************************************************************************
399  *
400  * Function         btm_ble_resolving_list_vsc_op_cmpl
401  *
402  * Description      IRK operation VSC complete handler
403  *
404  * Parameters
405  *
406  * Returns          void
407  *
408  ******************************************************************************/
btm_ble_resolving_list_vsc_op_cmpl(tBTM_VSC_CMPL * p_params)409 static void btm_ble_resolving_list_vsc_op_cmpl(tBTM_VSC_CMPL* p_params) {
410   uint8_t *p = p_params->p_param_buf, op_subcode;
411   uint16_t evt_len = p_params->param_len;
412 
413   op_subcode = *(p + 1);
414 
415   log::verbose("op_subcode={}", op_subcode);
416 
417   if (op_subcode == BTM_BLE_META_CLEAR_IRK_LIST) {
418     btm_ble_clear_resolving_list_complete(p, evt_len);
419   } else if (op_subcode == BTM_BLE_META_ADD_IRK_ENTRY) {
420     btm_ble_add_resolving_list_entry_complete(p, evt_len);
421   } else if (op_subcode == BTM_BLE_META_REMOVE_IRK_ENTRY) {
422     btm_ble_remove_resolving_list_entry_complete(p, evt_len);
423   } else if (op_subcode == BTM_BLE_META_READ_IRK_ENTRY) {
424     btm_ble_read_resolving_list_entry_complete(p, evt_len);
425   } else if (op_subcode == BTM_BLE_META_IRK_ENABLE) {
426     /* RPA offloading enable/disabled */
427   }
428 }
429 
430 /*******************************************************************************
431  *
432  * Function         btm_ble_remove_resolving_list_entry
433  *
434  * Description      This function to remove an IRK entry from the list
435  *
436  * Parameters       ble_addr_type: address type
437  *                  ble_addr: LE adddress
438  *
439  * Returns          status
440  *
441  ******************************************************************************/
btm_ble_remove_resolving_list_entry(tBTM_SEC_DEV_REC * p_dev_rec)442 static tBTM_STATUS btm_ble_remove_resolving_list_entry(
443     tBTM_SEC_DEV_REC* p_dev_rec) {
444   /* if controller does not support RPA offloading or privacy 1.2, skip */
445   if (bluetooth::shim::GetController()->GetLeResolvingListSize() == 0)
446     return BTM_WRONG_MODE;
447 
448   if (bluetooth::shim::GetController()->SupportsBlePrivacy()) {
449     bluetooth::shim::ACL_RemoveFromAddressResolution(
450         p_dev_rec->ble.identity_address_with_type);
451   } else {
452     uint8_t param[20] = {0};
453     uint8_t* p = param;
454 
455     UINT8_TO_STREAM(p, BTM_BLE_META_REMOVE_IRK_ENTRY);
456     UINT8_TO_STREAM(p, p_dev_rec->ble.identity_address_with_type.type);
457     BDADDR_TO_STREAM(p, p_dev_rec->ble.identity_address_with_type.bda);
458 
459     BTM_VendorSpecificCommand(HCI_VENDOR_BLE_RPA_VSC,
460                               BTM_BLE_META_REMOVE_IRK_LEN, param,
461                               btm_ble_resolving_list_vsc_op_cmpl);
462     btm_ble_enq_resolving_list_pending(p_dev_rec->bd_addr,
463                                        BTM_BLE_META_REMOVE_IRK_ENTRY);
464   }
465   return BTM_CMD_STARTED;
466 }
467 
468 /*******************************************************************************
469  *
470  * Function         btm_ble_clear_resolving_list
471  *
472  * Description      This function clears the resolving  list
473  *
474  * Parameters       None.
475  *
476  ******************************************************************************/
btm_ble_clear_resolving_list(void)477 static void btm_ble_clear_resolving_list(void) {
478   if (bluetooth::shim::GetController()->SupportsBlePrivacy()) {
479     bluetooth::shim::ACL_ClearAddressResolution();
480   } else {
481     uint8_t param[20] = {0};
482     uint8_t* p = param;
483 
484     UINT8_TO_STREAM(p, BTM_BLE_META_CLEAR_IRK_LIST);
485     BTM_VendorSpecificCommand(HCI_VENDOR_BLE_RPA_VSC,
486                               BTM_BLE_META_CLEAR_IRK_LEN, param,
487                               btm_ble_resolving_list_vsc_op_cmpl);
488   }
489 }
490 
491 /*******************************************************************************
492  *
493  * Function         btm_ble_read_resolving_list_entry
494  *
495  * Description      This function read an IRK entry by index
496  *
497  * Parameters       entry index.
498  *
499  * Returns          true if command successfully sent, false otherwise
500  *
501  ******************************************************************************/
btm_ble_read_resolving_list_entry(tBTM_SEC_DEV_REC * p_dev_rec)502 bool btm_ble_read_resolving_list_entry(tBTM_SEC_DEV_REC* p_dev_rec) {
503   if (btm_cb.ble_ctr_cb.privacy_mode < BTM_PRIVACY_1_2) {
504     log::debug("Privacy 1.2 is not enabled");
505     return false;
506   }
507   if (!(p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT)) {
508     log::info("Unable to read resolving list entry as resolving bit not set");
509     return false;
510   }
511 
512   if (bluetooth::shim::GetController()->SupportsBlePrivacy()) {
513     btsnd_hcic_ble_read_resolvable_addr_peer(
514         p_dev_rec->ble.identity_address_with_type.type,
515         p_dev_rec->ble.identity_address_with_type.bda);
516   } else {
517     uint8_t param[20] = {0};
518     uint8_t* p = param;
519 
520     UINT8_TO_STREAM(p, BTM_BLE_META_READ_IRK_ENTRY);
521     UINT8_TO_STREAM(p, p_dev_rec->ble.resolving_list_index);
522 
523     BTM_VendorSpecificCommand(HCI_VENDOR_BLE_RPA_VSC, BTM_BLE_META_READ_IRK_LEN,
524                               param, btm_ble_resolving_list_vsc_op_cmpl);
525 
526     btm_ble_enq_resolving_list_pending(p_dev_rec->bd_addr,
527                                        BTM_BLE_META_READ_IRK_ENTRY);
528   }
529   return true;
530 }
531 
btm_ble_ble_unsupported_resolving_list_load_dev(tBTM_SEC_DEV_REC * p_dev_rec)532 static void btm_ble_ble_unsupported_resolving_list_load_dev(
533     tBTM_SEC_DEV_REC* p_dev_rec) {
534   log::info("Controller does not support BLE privacy");
535   uint8_t param[40] = {0};
536   uint8_t* p = param;
537 
538   UINT8_TO_STREAM(p, BTM_BLE_META_ADD_IRK_ENTRY);
539   ARRAY_TO_STREAM(p, p_dev_rec->sec_rec.ble_keys.irk, OCTET16_LEN);
540   UINT8_TO_STREAM(p, p_dev_rec->ble.identity_address_with_type.type);
541   BDADDR_TO_STREAM(p, p_dev_rec->ble.identity_address_with_type.bda);
542 
543   BTM_VendorSpecificCommand(HCI_VENDOR_BLE_RPA_VSC, BTM_BLE_META_ADD_IRK_LEN,
544                             param, btm_ble_resolving_list_vsc_op_cmpl);
545 
546   btm_ble_enq_resolving_list_pending(p_dev_rec->bd_addr,
547                                      BTM_BLE_META_ADD_IRK_ENTRY);
548   return;
549 }
550 
is_peer_identity_key_valid(const tBTM_SEC_DEV_REC & dev_rec)551 static bool is_peer_identity_key_valid(const tBTM_SEC_DEV_REC& dev_rec) {
552   return dev_rec.sec_rec.ble_keys.key_type & BTM_LE_KEY_PID;
553 }
554 
get_local_irk()555 static Octet16 get_local_irk() { return btm_sec_cb.devcb.id_keys.irk; }
556 
btm_ble_resolving_list_load_dev(tBTM_SEC_DEV_REC & dev_rec)557 void btm_ble_resolving_list_load_dev(tBTM_SEC_DEV_REC& dev_rec) {
558   if (btm_cb.ble_ctr_cb.privacy_mode < BTM_PRIVACY_1_2) {
559     log::debug("Privacy 1.2 is not enabled");
560     return;
561   }
562   if (bluetooth::shim::GetController()->GetLeResolvingListSize() == 0) {
563     log::info("Controller does not support RPA offloading or privacy 1.2");
564     return;
565   }
566 
567   if (!bluetooth::shim::GetController()->SupportsBlePrivacy()) {
568     return btm_ble_ble_unsupported_resolving_list_load_dev(&dev_rec);
569   }
570 
571   // No need to check for local identity key validity. It remains unchanged.
572   if (!is_peer_identity_key_valid(dev_rec)) {
573     log::info("Peer is not an RPA enabled device:{}",
574               dev_rec.ble.identity_address_with_type);
575     return;
576   }
577 
578   if (dev_rec.ble.in_controller_list & BTM_RESOLVING_LIST_BIT) {
579     log::warn("Already in Address Resolving list device:{}",
580               dev_rec.ble.identity_address_with_type);
581     return;
582   }
583 
584   const Octet16& peer_irk = dev_rec.sec_rec.ble_keys.irk;
585   const Octet16& local_irk = get_local_irk();
586 
587   if (dev_rec.ble.identity_address_with_type.bda.IsEmpty()) {
588     dev_rec.ble.identity_address_with_type = {
589         .type = dev_rec.ble.AddressType(),
590         .bda = dev_rec.bd_addr,
591     };
592   }
593 
594   if (!is_ble_addr_type_known(dev_rec.ble.identity_address_with_type.type)) {
595     log::error("Adding unknown address type({}) to Address Resolving list.",
596                dev_rec.ble.identity_address_with_type.type);
597     return;
598   }
599 
600   bluetooth::shim::ACL_AddToAddressResolution(
601       dev_rec.ble.identity_address_with_type, peer_irk, local_irk);
602 
603   log::debug("Added to Address Resolving list device:{}",
604              dev_rec.ble.identity_address_with_type);
605 
606   dev_rec.ble.in_controller_list |= BTM_RESOLVING_LIST_BIT;
607 }
608 
609 /*******************************************************************************
610  *
611  * Function         btm_ble_resolving_list_remove_dev
612  *
613  * Description      This function removes the device from resolving list
614  *
615  * Parameters
616  *
617  * Returns          status
618  *
619  ******************************************************************************/
btm_ble_resolving_list_remove_dev(tBTM_SEC_DEV_REC * p_dev_rec)620 void btm_ble_resolving_list_remove_dev(tBTM_SEC_DEV_REC* p_dev_rec) {
621   if (btm_cb.ble_ctr_cb.privacy_mode < BTM_PRIVACY_1_2) {
622     log::debug("Privacy 1.2 is not enabled");
623     return;
624   }
625 
626   if ((p_dev_rec->ble.in_controller_list & BTM_RESOLVING_LIST_BIT) &&
627       !btm_ble_brcm_find_resolving_pending_entry(
628           p_dev_rec->bd_addr, BTM_BLE_META_REMOVE_IRK_ENTRY)) {
629     btm_ble_update_resolving_list(p_dev_rec->bd_addr, false);
630     btm_ble_remove_resolving_list_entry(p_dev_rec);
631   } else {
632     log::verbose("Device not in resolving list");
633   }
634 }
635 
636 /*******************************************************************************
637  *
638  * Function         btm_ble_resolving_list_init
639  *
640  * Description      Initialize resolving list in host stack
641  *
642  * Parameters       Max resolving list size
643  *
644  * Returns          void
645  *
646  ******************************************************************************/
btm_ble_resolving_list_init(uint8_t max_irk_list_sz)647 void btm_ble_resolving_list_init(uint8_t max_irk_list_sz) {
648   tBTM_BLE_RESOLVE_Q* p_q = &btm_cb.ble_ctr_cb.resolving_list_pend_q;
649   uint8_t irk_mask_size =
650       (max_irk_list_sz % 8) ? (max_irk_list_sz / 8 + 1) : (max_irk_list_sz / 8);
651 
652   if (max_irk_list_sz > 0 && p_q->resolve_q_random_pseudo == nullptr) {
653     // NOTE: This memory is never freed
654     p_q->resolve_q_random_pseudo =
655         (RawAddress*)osi_malloc(sizeof(RawAddress) * max_irk_list_sz);
656     // NOTE: This memory is never freed
657     p_q->resolve_q_action = (uint8_t*)osi_malloc(max_irk_list_sz);
658 
659     /* RPA offloading feature */
660     if (btm_cb.ble_ctr_cb.irk_list_mask == NULL)
661       // NOTE: This memory is never freed
662       btm_cb.ble_ctr_cb.irk_list_mask = (uint8_t*)osi_malloc(irk_mask_size);
663 
664     log::verbose("max_irk_list_sz={}", max_irk_list_sz);
665   }
666 
667   btm_ble_clear_resolving_list();
668   btm_cb.ble_ctr_cb.resolving_list_avail_size = max_irk_list_sz;
669 }
670