123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(PEER_MANAGER) && NRF_MODULE_ENABLED(PM_RA_PROTECTION)
- #include "auth_status_tracker.h"
- #include "app_timer.h"
- #include "id_manager.h"
- #define NRF_LOG_MODULE_NAME peer_manager_ast
- #if PM_LOG_ENABLED
- #define NRF_LOG_LEVEL PM_LOG_LEVEL
- #define NRF_LOG_INFO_COLOR PM_LOG_INFO_COLOR
- #define NRF_LOG_DEBUG_COLOR PM_LOG_DEBUG_COLOR
- #else
- #define NRF_LOG_LEVEL 0
- #endif
- #include "nrf_log.h"
- NRF_LOG_MODULE_REGISTER();
- #include "nrf_strerror.h"
- #define PAIR_REWARD_TICKS APP_TIMER_TICKS(PM_RA_PROTECTION_REWARD_PERIOD)
- #define PENALITY_LVL_TO_PENALITY_MS(_lvl) (PM_RA_PROTECTION_MIN_WAIT_INTERVAL * (1 << _lvl))
- #define PENALITY_LVL_TO_PENALITY_TICKS(_lvl) APP_TIMER_TICKS(PENALITY_LVL_TO_PENALITY_MS(_lvl))
- #define PENALITY_LVL_NEXT_SET(_lvl) \
- _lvl = (PENALITY_LVL_TO_PENALITY_MS(_lvl) >= (PM_RA_PROTECTION_MAX_WAIT_INTERVAL)) ? \
- (_lvl) : (_lvl + 1)
- typedef struct
- {
- ble_gap_addr_t peer_addr;
- uint32_t reward_ticks;
- uint32_t penality_ticks;
- uint8_t penality_lvl;
- bool is_active;
- bool is_valid;
- } blacklisted_peer_t;
- APP_TIMER_DEF(m_pairing_attempt_timer);
- static blacklisted_peer_t m_blacklisted_peers[PM_RA_PROTECTION_TRACKED_PEERS_NUM];
- static uint32_t m_ticks_cnt;
- static uint32_t blacklisted_peers_state_update(uint32_t ticks_passed)
- {
- uint32_t minimal_ticks = UINT32_MAX;
- for (uint32_t id = 0; id < ARRAY_SIZE(m_blacklisted_peers); id++)
- {
- blacklisted_peer_t * p_bl_peer = &m_blacklisted_peers[id];
- if (p_bl_peer->is_valid)
- {
- if (p_bl_peer->is_active)
- {
- if (p_bl_peer->penality_ticks > ticks_passed)
- {
- p_bl_peer->penality_ticks -= ticks_passed;
- minimal_ticks = MIN(minimal_ticks, p_bl_peer->penality_ticks);
- }
- else
- {
- p_bl_peer->is_active = false;
- if (p_bl_peer->penality_lvl == 0)
- {
- p_bl_peer->is_valid = false;
- NRF_LOG_DEBUG("Peer has been removed from the blacklist, its address:");
- NRF_LOG_HEXDUMP_DEBUG(p_bl_peer->peer_addr.addr,
- sizeof(p_bl_peer->peer_addr.addr));
- }
- else
- {
- minimal_ticks = MIN(minimal_ticks, PAIR_REWARD_TICKS);
- }
- NRF_LOG_DEBUG("Pairing waiting interval has expired for:");
- NRF_LOG_HEXDUMP_DEBUG(p_bl_peer->peer_addr.addr,
- sizeof(p_bl_peer->peer_addr.addr));
- }
- }
- else
- {
- if (p_bl_peer->penality_lvl == 0)
- {
- p_bl_peer->is_valid = false;
- NRF_LOG_DEBUG("Peer has been removed from the blacklist, its address:");
- NRF_LOG_HEXDUMP_DEBUG(p_bl_peer->peer_addr.addr,
- sizeof(p_bl_peer->peer_addr.addr));
- }
- else
- {
- p_bl_peer->reward_ticks += ticks_passed;
- if (p_bl_peer->reward_ticks >= PAIR_REWARD_TICKS)
- {
- p_bl_peer->penality_lvl--;
- p_bl_peer->reward_ticks -= PAIR_REWARD_TICKS;
- NRF_LOG_DEBUG("Peer penality level has decreased to %d for device:",
- p_bl_peer->penality_lvl);
- NRF_LOG_HEXDUMP_DEBUG(p_bl_peer->peer_addr.addr,
- sizeof(p_bl_peer->peer_addr.addr));
- }
- minimal_ticks = MIN(minimal_ticks,
- (PAIR_REWARD_TICKS - p_bl_peer->reward_ticks));
- }
- }
- }
- }
- return minimal_ticks;
- }
- static void blacklisted_peers_state_transition_handle(void * context)
- {
- ret_code_t err_code;
- uint32_t minimal_ticks;
- uint32_t ticks_passed = (uint32_t) context;
- minimal_ticks = blacklisted_peers_state_update(ticks_passed);
- m_ticks_cnt = app_timer_cnt_get();
- if (minimal_ticks != UINT32_MAX)
- {
- err_code = app_timer_start(m_pairing_attempt_timer,
- minimal_ticks,
- (void *) minimal_ticks);
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_WARNING("app_timer_start() returned %s", nrf_strerror_get(err_code));
- }
- NRF_LOG_DEBUG("Restarting the timer");
- }
- }
- ret_code_t ast_init(void)
- {
- ret_code_t err_code = app_timer_create(&m_pairing_attempt_timer,
- APP_TIMER_MODE_SINGLE_SHOT,
- blacklisted_peers_state_transition_handle);
- return err_code;
- }
- void ast_auth_error_notify(uint16_t conn_handle)
- {
- ret_code_t err_code;
- ble_gap_addr_t peer_addr;
- uint32_t new_timeout;
- uint32_t free_id = ARRAY_SIZE(m_blacklisted_peers);
- bool new_bl_entry = true;
-
- err_code = im_ble_addr_get(conn_handle, &peer_addr);
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_WARNING("im_ble_addr_get() returned %s. conn_handle: %d. "
- "Link was likely disconnected.",
- nrf_strerror_get(err_code),
- conn_handle);
- return;
- }
-
- err_code = app_timer_stop(m_pairing_attempt_timer);
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_WARNING("app_timer_stop() returned %s", nrf_strerror_get(err_code));
- return;
- }
- new_timeout = blacklisted_peers_state_update(app_timer_cnt_diff_compute(app_timer_cnt_get(),
- m_ticks_cnt));
- m_ticks_cnt = app_timer_cnt_get();
-
- for (uint32_t id = 0; id < ARRAY_SIZE(m_blacklisted_peers); id++)
- {
- blacklisted_peer_t * p_bl_peer = &m_blacklisted_peers[id];
- if (p_bl_peer->is_valid)
- {
- if (memcmp(peer_addr.addr, p_bl_peer->peer_addr.addr, BLE_GAP_ADDR_LEN) == 0)
- {
- uint8_t lvl = p_bl_peer->penality_lvl;
- PENALITY_LVL_NEXT_SET(lvl);
- p_bl_peer->penality_lvl = lvl;
- p_bl_peer->reward_ticks = 0;
- p_bl_peer->penality_ticks = PENALITY_LVL_TO_PENALITY_TICKS(lvl);
- new_timeout = MIN(new_timeout, p_bl_peer->penality_ticks);
- p_bl_peer->is_active = true;
- new_bl_entry = false;
- NRF_LOG_DEBUG("Pairing waiting interval has been renewed. "
- "Penality level: %d for device:",
- lvl);
- NRF_LOG_HEXDUMP_DEBUG(p_bl_peer->peer_addr.addr,
- sizeof(p_bl_peer->peer_addr.addr));
- }
- }
- else
- {
- free_id = id;
- }
- }
-
- if (new_bl_entry)
- {
- if (free_id < ARRAY_SIZE(m_blacklisted_peers))
- {
- blacklisted_peer_t * p_bl_peer = &m_blacklisted_peers[free_id];
- memcpy(&p_bl_peer->peer_addr, &peer_addr, sizeof(peer_addr));
- p_bl_peer->penality_lvl = 0;
- p_bl_peer->reward_ticks = 0;
- p_bl_peer->penality_ticks = PENALITY_LVL_TO_PENALITY_TICKS(p_bl_peer->penality_lvl);
- new_timeout = MIN(new_timeout, p_bl_peer->penality_ticks);
- p_bl_peer->is_active = true;
- p_bl_peer->is_valid = true;
- NRF_LOG_DEBUG("New peer has been added to the blacklist:");
- NRF_LOG_HEXDUMP_DEBUG(p_bl_peer->peer_addr.addr, sizeof(p_bl_peer->peer_addr.addr));
- }
- else
- {
- NRF_LOG_WARNING("No space to blacklist another peer ID");
- }
- }
-
- if (new_timeout != UINT32_MAX)
- {
- err_code = app_timer_start(m_pairing_attempt_timer,
- new_timeout,
- (void *) new_timeout);
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_WARNING("app_timer_start() returned %s", nrf_strerror_get(err_code));
- }
- }
- }
- bool ast_peer_blacklisted(uint16_t conn_handle)
- {
- ret_code_t err_code;
- ble_gap_addr_t peer_addr;
- err_code = im_ble_addr_get(conn_handle, &peer_addr);
- if (err_code != NRF_SUCCESS)
- {
- NRF_LOG_WARNING("im_ble_addr_get() returned %s. conn_handle: %d. "
- "Link was likely disconnected.",
- nrf_strerror_get(err_code),
- conn_handle);
- return true;
- }
- for (uint32_t id = 0; id < ARRAY_SIZE(m_blacklisted_peers); id++)
- {
- blacklisted_peer_t * p_bl_peer = &m_blacklisted_peers[id];
- if (p_bl_peer->is_valid)
- {
- if ((memcmp(peer_addr.addr, p_bl_peer->peer_addr.addr, BLE_GAP_ADDR_LEN) == 0) &&
- (p_bl_peer->is_active))
- {
- return true;
- }
- }
- }
- return false;
- }
- #endif
|