This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git


The following commit(s) were added to refs/heads/master by this push:
     new 25e54af0a nimble/ll: Make NRPA generation in active scan optional
25e54af0a is described below

commit 25e54af0a26d6183516d6c6c0eb6be1246b6a02f
Author: Andrzej Kaczmarek <[email protected]>
AuthorDate: Thu Feb 20 13:51:01 2025 +0100

    nimble/ll: Make NRPA generation in active scan optional
    
    This adds a syscfg to disable NRPA generation by LL in active scan. This
    allows host to specify own NRPA as a random address and use it instead
    of the one generated by LL.
    
    Also this moves code to make SCAN_REQ/AUX_SCAN_REQ to common helper so
    we don't duplicate code.
---
 nimble/controller/include/controller/ble_ll_scan.h |  9 ++-
 nimble/controller/src/ble_ll_scan.c                | 68 +++++++++++-----------
 nimble/controller/src/ble_ll_scan_aux.c            | 57 +-----------------
 nimble/controller/syscfg.yml                       | 10 ++++
 4 files changed, 54 insertions(+), 90 deletions(-)

diff --git a/nimble/controller/include/controller/ble_ll_scan.h 
b/nimble/controller/include/controller/ble_ll_scan.h
index 19d9cdd9c..47c743741 100644
--- a/nimble/controller/include/controller/ble_ll_scan.h
+++ b/nimble/controller/include/controller/ble_ll_scan.h
@@ -132,7 +132,7 @@ struct ble_ll_scan_sm
     uint8_t scan_rsp_cons_fails;
     uint8_t scan_rsp_cons_ok;
     uint8_t scan_peer_rpa[BLE_DEV_ADDR_LEN];
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
+#if MYNEWT_VAL(BLE_LL_SCAN_ACTIVE_SCAN_NRPA)
     ble_npl_time_t scan_nrpa_timer;
     uint8_t scan_nrpa[BLE_DEV_ADDR_LEN];
 #endif
@@ -250,7 +250,6 @@ void ble_ll_scan_interrupted(struct ble_ll_scan_sm *scansm);
 /* Called to halt currently running scan */
 void ble_ll_scan_halt(void);
 
-uint8_t *ble_ll_get_scan_nrpa(void);
 uint8_t ble_ll_scan_get_own_addr_type(void);
 uint8_t ble_ll_scan_get_filt_policy(void);
 uint8_t ble_ll_scan_get_filt_dups(void);
@@ -266,6 +265,12 @@ int ble_ll_scan_have_rxd_scan_rsp(uint8_t *addr, uint8_t 
txadd, uint8_t ext_adv,
 void ble_ll_scan_add_scan_rsp_adv(uint8_t *addr, uint8_t txadd, uint8_t 
ext_adv,
                                   uint16_t adi);
 
+struct ble_ll_scan_sm *ble_ll_scan_sm_get(void);
+
+void ble_ll_scan_make_req_pdu(struct ble_ll_scan_sm *scansm, uint8_t *pdu,
+                              uint8_t *hdr_byte, uint8_t adva_type,
+                              const uint8_t *adva, int rpa_index);
+
 int
 ble_ll_scan_rx_filter(uint8_t own_addr_type, uint8_t scan_filt_policy,
                       struct ble_ll_scan_addr_data *addrd, uint8_t *scan_ok);
diff --git a/nimble/controller/src/ble_ll_scan.c 
b/nimble/controller/src/ble_ll_scan.c
index f2c52f19e..d758d899f 100644
--- a/nimble/controller/src/ble_ll_scan.c
+++ b/nimble/controller/src/ble_ll_scan.c
@@ -175,7 +175,7 @@ ble_ll_scan_req_backoff(struct ble_ll_scan_sm *scansm, int 
success)
     BLE_LL_ASSERT(scansm->backoff_count <= 256);
 }
 
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
+#if MYNEWT_VAL(BLE_LL_SCAN_ACTIVE_SCAN_NRPA)
 static void
 ble_ll_scan_refresh_nrpa(struct ble_ll_scan_sm *scansm)
 {
@@ -191,17 +191,13 @@ ble_ll_scan_refresh_nrpa(struct ble_ll_scan_sm *scansm)
         scansm->scan_nrpa_timer = now + ble_ll_resolv_get_rpa_tmo();
     }
 }
+#endif
 
-uint8_t *
-ble_ll_get_scan_nrpa(void)
+struct ble_ll_scan_sm *
+ble_ll_scan_sm_get(void)
 {
-    struct ble_ll_scan_sm *scansm = &g_ble_ll_scan_sm;
-
-    ble_ll_scan_refresh_nrpa(scansm);
-
-    return scansm->scan_nrpa;
+    return &g_ble_ll_scan_sm;
 }
-#endif
 
 uint8_t
 ble_ll_scan_get_own_addr_type(void)
@@ -241,30 +237,26 @@ ble_ll_scan_backoff_update(int success)
     ble_ll_scan_req_backoff(scansm, success);
 }
 
-static void
-ble_ll_scan_req_pdu_prepare(struct ble_ll_scan_sm *scansm,
-                            const uint8_t *adv_addr, uint8_t adv_addr_type,
-                            int8_t rpa_index)
+void
+ble_ll_scan_make_req_pdu(struct ble_ll_scan_sm *scansm, uint8_t *pdu,
+                         uint8_t *hdr_byte, uint8_t adva_type,
+                         const uint8_t *adva, int rpa_index)
 {
-    uint8_t hdr_byte;
-    struct ble_ll_scan_pdu_data *pdu_data;
     uint8_t *scana;
 #if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     struct ble_ll_resolv_entry *rl;
     uint8_t rpa[BLE_DEV_ADDR_LEN];
 #endif
 
-    pdu_data = &scansm->pdu_data;
-
     /* Construct first PDU header byte */
-    hdr_byte = BLE_ADV_PDU_TYPE_SCAN_REQ;
-    if (adv_addr_type) {
-        hdr_byte |= BLE_ADV_PDU_HDR_RXADD_RAND;
+    *hdr_byte = BLE_ADV_PDU_TYPE_SCAN_REQ;
+    if (adva_type) {
+        *hdr_byte |= BLE_ADV_PDU_HDR_RXADD_RAND;
     }
 
     /* Determine ScanA */
     if (scansm->own_addr_type & 0x01) {
-        hdr_byte |= BLE_ADV_PDU_HDR_TXADD_RAND;
+        *hdr_byte |= BLE_ADV_PDU_HDR_TXADD_RAND;
         scana = g_random_addr;
     } else {
         scana = g_dev_addr;
@@ -278,12 +270,12 @@ ble_ll_scan_req_pdu_prepare(struct ble_ll_scan_sm *scansm,
             rl = NULL;
         }
 
-        /*
-         * If device is on RL and we have local IRK, we use RPA generated using
-         * that IRK as ScanA. Otherwise we use NRPA as ScanA to prevent our
-         * device from being tracked when doing an active scan (Core 5.1, Vol 
6,
-         * Part B, section 6.3)
-         */
+        /* Check if we should use RPA/NRPA instead of public/random address:
+         * - use RPA if device is on RL and has local IRK set
+         * - use RPA generated from local IRK if set
+         * - use NRPA if allowed by configuration
+         * */
+
         if (rl && rl->rl_has_local) {
             ble_ll_resolv_get_priv_addr(rl, 1, rpa);
             scana = rpa;
@@ -291,19 +283,29 @@ ble_ll_scan_req_pdu_prepare(struct ble_ll_scan_sm *scansm,
             if (ble_ll_resolv_local_rpa_get(scansm->own_addr_type & 0x01, rpa) 
== 0) {
                 scana = rpa;
             } else {
+#if MYNEWT_VAL(BLE_LL_SCAN_ACTIVE_SCAN_NRPA)
                 ble_ll_scan_refresh_nrpa(scansm);
                 scana = scansm->scan_nrpa;
+#endif
             }
         }
 
-        hdr_byte |= BLE_ADV_PDU_HDR_TXADD_RAND;
+        *hdr_byte |= BLE_ADV_PDU_HDR_TXADD_RAND;
     }
 #endif
 
-    /* Save scan request data */
-    pdu_data->hdr_byte = hdr_byte;
-    memcpy(pdu_data->scana, scana, BLE_DEV_ADDR_LEN);
-    memcpy(pdu_data->adva, adv_addr, BLE_DEV_ADDR_LEN);
+    memcpy(pdu, scana, BLE_DEV_ADDR_LEN);
+    memcpy(pdu + 6, adva, BLE_DEV_ADDR_LEN);
+}
+
+static void
+ble_ll_scan_req_pdu_prepare(struct ble_ll_scan_sm *scansm,
+                            const uint8_t *adv_addr, uint8_t adv_addr_type,
+                            int8_t rpa_index)
+{
+    ble_ll_scan_make_req_pdu(scansm, scansm->pdu_data.scana,
+                             &scansm->pdu_data.hdr_byte, adv_addr_type, 
adv_addr,
+                             rpa_index);
 }
 
 static uint8_t
@@ -2751,7 +2753,7 @@ ble_ll_scan_common_init(void)
     scansm->scan_phys[PHY_CODED].phy = BLE_PHY_CODED;
 #endif
 
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
+#if MYNEWT_VAL(BLE_LL_SCAN_ACTIVE_SCAN_NRPA)
     /* Make sure we'll generate new NRPA if necessary */
     scansm->scan_nrpa_timer = ble_npl_time_get();
 #endif
diff --git a/nimble/controller/src/ble_ll_scan_aux.c 
b/nimble/controller/src/ble_ll_scan_aux.c
index b61f729db..0596cd6f9 100644
--- a/nimble/controller/src/ble_ll_scan_aux.c
+++ b/nimble/controller/src/ble_ll_scan_aux.c
@@ -1144,62 +1144,9 @@ static uint8_t
 ble_ll_scan_aux_scan_req_tx_pdu_cb(uint8_t *dptr, void *arg, uint8_t *hdr_byte)
 {
     struct ble_ll_scan_aux_data *aux = arg;
-    uint8_t *scana;
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    struct ble_ll_resolv_entry *rl;
-    uint8_t rpa[BLE_DEV_ADDR_LEN];
-#endif
-    uint8_t hb;
-    uint8_t own_addr_type = ble_ll_scan_get_own_addr_type();
-
-    hb = BLE_ADV_PDU_TYPE_SCAN_REQ;
-
-    /* ScanA */
-    if (own_addr_type & 0x01) {
-        hb |= BLE_ADV_PDU_HDR_TXADD_RAND;
-        scana = g_random_addr;
-    } else {
-        scana = g_dev_addr;
-    }
-
-#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
-    if (own_addr_type & 0x02) {
-        if (aux->rpa_index >=0) {
-            rl = &g_ble_ll_resolv_list[aux->rpa_index];
-        } else {
-            rl = NULL;
-        }
-
-        /*
-         * If device is on RL and we have local IRK, we use RPA generated using
-         * that IRK as ScanA. Otherwise we use NRPA or RPA from global local 
IRK
-         * as ScanA to prevent our device from being tracked when doing
-         * an active scan
-         * ref: Core 5.2, Vol 6, Part B, section 6.3)
-         */
-        if (rl && rl->rl_has_local) {
-            ble_ll_resolv_get_priv_addr(rl, 1, rpa);
-            scana = rpa;
-        } else {
-            if (ble_ll_resolv_local_rpa_get(own_addr_type & 0x01, rpa) == 0) {
-                scana = rpa;
-            } else {
-                scana = ble_ll_get_scan_nrpa();
-            }
-        }
-
-        hb |= BLE_ADV_PDU_HDR_TXADD_RAND;
-    }
-#endif
-    memcpy(dptr, scana, BLE_DEV_ADDR_LEN);
-
-    /* AdvA */
-    if (aux->adva_type) {
-        hb |= BLE_ADV_PDU_HDR_RXADD_RAND;
-    }
-    memcpy(dptr + BLE_DEV_ADDR_LEN, aux->adva, BLE_DEV_ADDR_LEN);
 
-    *hdr_byte = hb;
+    ble_ll_scan_make_req_pdu(ble_ll_scan_sm_get(), dptr, hdr_byte,
+                             aux->adva_type, aux->adva, aux->rpa_index);
 
     return BLE_DEV_ADDR_LEN * 2;
 }
diff --git a/nimble/controller/syscfg.yml b/nimble/controller/syscfg.yml
index bc4860c1b..d26f7006e 100644
--- a/nimble/controller/syscfg.yml
+++ b/nimble/controller/syscfg.yml
@@ -370,6 +370,16 @@ syscfg.defs:
             concurrently (Core 5.2, Vol 6, Part B, 4.4.2.2.2).
          value: MYNEWT_VAL(BLE_LL_EXT_ADV_AUX_PTR_CNT)
 
+    BLE_LL_SCAN_ACTIVE_SCAN_NRPA:
+        description: >
+            The controller will automatically generate NRPA for scan requests
+            if host requested to use privacy (i.e. 0x02 or 0x03 own address
+            type) but the peer is not on the resolving list.
+            If disabled, public or random address will be used.
+        value: 1
+        restrictions:
+            - BLE_LL_CFG_FEAT_LL_PRIVACY if 1
+
     BLE_LL_PUBLIC_DEV_ADDR:
         description: >
             Set public device address. Address is specified as 48-bit number.

Reply via email to