You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by ja...@apache.org on 2020/03/09 11:04:53 UTC

[mynewt-nimble] branch master updated: nimble/host: Fix check for valid static random address

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

janc 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 11f16a0  nimble/host: Fix check for valid static random address
11f16a0 is described below

commit 11f16a0ee2c1cbfb0db96a5e2e7c5a0b9cb331a0
Author: Prasad Alatkar <pr...@espressif.com>
AuthorDate: Mon Mar 2 16:29:45 2020 +0530

    nimble/host: Fix check for valid static random address
---
 nimble/host/src/ble_hs_id.c | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/nimble/host/src/ble_hs_id.c b/nimble/host/src/ble_hs_id.c
index 176ad20..e8b6c8b 100644
--- a/nimble/host/src/ble_hs_id.c
+++ b/nimble/host/src/ble_hs_id.c
@@ -58,14 +58,24 @@ ble_hs_id_set_rnd(const uint8_t *rnd_addr)
 {
     uint8_t addr_type_byte;
     int rc;
-    uint8_t all_zeros[BLE_DEV_ADDR_LEN] = {0}, all_ones[BLE_DEV_ADDR_LEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
+    int ones;
 
     ble_hs_lock();
 
-    /* Make sure all bits are neither one nor zero */
+    /* Make sure random part of rnd_addr is not all ones or zeros. Reference:
+     * Core v5.0, Vol 6, Part B, section 1.3.2.1 */
     addr_type_byte = rnd_addr[5] & 0xc0;
+
+    /* count bits set to 1 in random part of address */
+    ones = __builtin_popcount(rnd_addr[0]);
+    ones += __builtin_popcount(rnd_addr[1]);
+    ones += __builtin_popcount(rnd_addr[2]);
+    ones += __builtin_popcount(rnd_addr[3]);
+    ones += __builtin_popcount(rnd_addr[4]);
+    ones += __builtin_popcount(rnd_addr[5] & 0x3f);
+
     if ((addr_type_byte != 0x00 && addr_type_byte != 0xc0) ||
-        !memcmp(rnd_addr, all_zeros, BLE_DEV_ADDR_LEN) || !memcmp(rnd_addr, all_ones, BLE_DEV_ADDR_LEN)) {
+            (ones == 0 || ones == 46)) {
         rc = BLE_HS_EINVAL;
         goto done;
     }
@@ -77,8 +87,6 @@ ble_hs_id_set_rnd(const uint8_t *rnd_addr)
 
     memcpy(ble_hs_id_rnd, rnd_addr, 6);
 
-    rc = 0;
-
 done:
     ble_hs_unlock();
     return rc;