From 23ac4ee47b8029c639c65953aa42e9558c7815d4 Mon Sep 17 00:00:00 2001 From: Andrzej Kaczmarek Date: Tue, 21 Jun 2022 13:50:15 +0200 Subject: [PATCH] nimble/ll: Fix race between aux scan and scan disable It's possible that scheduled aux is being received while scan sm is being disabled. In such case aux should not be processed since it cannot be handled properly anyway. --- nimble/controller/src/ble_ll_scan.c | 8 ++++---- nimble/controller/src/ble_ll_scan_aux.c | 5 ++++- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/nimble/controller/src/ble_ll_scan.c b/nimble/controller/src/ble_ll_scan.c index d9d351786..27f1e65a7 100644 --- a/nimble/controller/src/ble_ll_scan.c +++ b/nimble/controller/src/ble_ll_scan.c @@ -880,10 +880,6 @@ ble_ll_scan_sm_stop(int chk_disable) OS_ENTER_CRITICAL(sr); -#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL) - scansm->connsm = NULL; -#endif - /* Disable scanning state machine */ scansm->scan_enabled = 0; scansm->restart_timer_needed = 0; @@ -895,6 +891,10 @@ ble_ll_scan_sm_stop(int chk_disable) } #endif +#if MYNEWT_VAL(BLE_LL_ROLE_CENTRAL) + scansm->connsm = NULL; +#endif + /* Update backoff if we failed to receive scan response */ if (scansm->scan_rsp_pending) { scansm->scan_rsp_pending = 0; diff --git a/nimble/controller/src/ble_ll_scan_aux.c b/nimble/controller/src/ble_ll_scan_aux.c index 2eb915d0e..2967083b2 100644 --- a/nimble/controller/src/ble_ll_scan_aux.c +++ b/nimble/controller/src/ble_ll_scan_aux.c @@ -1237,7 +1237,10 @@ ble_ll_scan_aux_rx_isr_end(struct os_mbuf *rxpdu, uint8_t crcok) rxinfo = &rxhdr->rxinfo; rxinfo->user_data = aux; - if (!crcok) { + /* It's possible that we received aux while scan was just being disabled in + * LL task. In such case simply ignore aux. + */ + if (!crcok || !ble_ll_scan_enabled()) { rxinfo->flags |= BLE_MBUF_HDR_F_IGNORED; goto done; }