Skip to content

Commit

Permalink
NimBLE: Fix repeated pairing request failure in RPA feature
Browse files Browse the repository at this point in the history
  • Loading branch information
prasad-alatkar authored and ESPAbhinav committed Feb 14, 2024
1 parent 7f59d1f commit 31b5f30
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
59 changes: 50 additions & 9 deletions nimble/host/src/ble_hs_resolv.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,44 @@ is_rpa_resolvable_by_peer_rec(struct ble_hs_dev_records *p_dev_rec, uint8_t *pee
return false;
}

static void
ble_rpa_replace_id_with_rand_addr(uint8_t *addr_type, uint8_t *peer_addr)
{
struct ble_hs_dev_records *p_dev_rec;
ble_addr_t p_addr = {0};
struct ble_hs_conn *conn = NULL;

p_dev_rec = ble_rpa_find_peer_dev_rec(peer_addr);

if (p_dev_rec != NULL) {
if (memcmp(p_dev_rec->rand_addr, p_dev_rec->identity_addr, BLE_DEV_ADDR_LEN)) {
/* OTA address (before resolving) gets saved in RAND_ADDR when the peer
* record is fetched from resolving list. Replace peer address
* with rand_addr to maintain status quo for new pairing/encryption request. */
p_addr.type = *addr_type;
memcpy(&p_addr.val[0], peer_addr, BLE_DEV_ADDR_LEN);

ble_hs_lock();

conn = ble_hs_conn_find_by_addr(&p_addr);
/* Rewrite the peer address history in ble_hs_conn. Need to take
* this step to avoid taking wrong address during re-pairing
* process */
if (conn != NULL) {
conn->bhc_peer_rpa_addr.type = p_dev_rec->rand_addr_type;
memcpy(&conn->bhc_peer_rpa_addr.val[0], p_dev_rec->rand_addr, BLE_DEV_ADDR_LEN);
conn->bhc_peer_addr.type = p_dev_rec->rand_addr_type;
memcpy(&conn->bhc_peer_addr.val[0], p_dev_rec->rand_addr, BLE_DEV_ADDR_LEN);
BLE_HS_LOG(DEBUG, "\n Replace Identity addr with random addr received at"
" start of the connection\n");
}

ble_hs_unlock();
}
}
return;
}

/* Add peer to peer device records.
*
* @return 0 if added successfully,
Expand Down Expand Up @@ -227,6 +265,7 @@ ble_rpa_find_rl_from_peer_records(uint8_t *peer_addr, uint8_t *peer_addr_type)

if (is_rpa_resolvable_by_peer_rec(p_dev_rec, peer_addr)) {
memcpy(p_dev_rec->rand_addr, peer_addr, BLE_DEV_ADDR_LEN);
p_dev_rec->rand_addr_type = *peer_addr_type;
rl = ble_hs_resolv_list_find(p_dev_rec->identity_addr);
if (rl) {
memcpy(peer_addr, p_dev_rec->identity_addr, BLE_DEV_ADDR_LEN);
Expand Down Expand Up @@ -277,13 +316,10 @@ ble_rpa_replace_peer_params_with_rl(uint8_t *peer_addr, uint8_t *addr_type,

if (is_rpa) {
ble_hs_log_flat_buf(peer_addr, BLE_DEV_ADDR_LEN);
rl_tmp = ble_hs_resolv_list_find(peer_addr);
BLE_HS_LOG(DEBUG, "\n");

/* Try to find from your peer_device records, if RL doesn't
* exist */
if (rl_tmp == NULL) {
rl_tmp = ble_rpa_find_rl_from_peer_records(peer_addr, addr_type);
}
/* Try to find RL from your peer_device records */
rl_tmp = ble_rpa_find_rl_from_peer_records(peer_addr, addr_type);
}

if (rl != NULL) {
Expand Down Expand Up @@ -571,7 +607,7 @@ ble_hs_resolv_list_add(uint8_t *cmdbuf)
int
ble_hs_resolv_list_rmv(uint8_t addr_type, uint8_t *ident_addr)
{
int position;
int position, rc = BLE_HS_ENOENT;

/* Remove from IRK records */
position = ble_hs_is_on_resolv_list(ident_addr, addr_type);
Expand All @@ -583,10 +619,15 @@ ble_hs_resolv_list_rmv(uint8_t addr_type, uint8_t *ident_addr)
ble_hs_resolv_entry));
--g_ble_hs_resolv_data.rl_cnt;

return 0;
rc = 0;
}

return BLE_HS_ENOENT;
/* As we are removing the RL record, it is needed to change
* peer_address to its latest received OTA address, this helps when existing bond at
* peer side is removed */
ble_rpa_replace_id_with_rand_addr(&addr_type, ident_addr);

return rc;
}

/**
Expand Down
1 change: 1 addition & 0 deletions nimble/host/src/ble_hs_resolv_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct ble_hs_peer_sec {
*/
struct ble_hs_dev_records {
bool rec_used;
uint8_t rand_addr_type;
uint8_t pseudo_addr[BLE_DEV_ADDR_LEN];
uint8_t rand_addr[BLE_DEV_ADDR_LEN];
uint8_t identity_addr[BLE_DEV_ADDR_LEN];
Expand Down

0 comments on commit 31b5f30

Please sign in to comment.