zephyrproject-rtos / zephyr

Primary Git Repository for the Zephyr Project. Zephyr is a new generation, scalable, optimized, secure RTOS for multiple hardware architectures.
https://docs.zephyrproject.org
Apache License 2.0
10.03k stars 6.17k forks source link

Bluetooth: Add `bt_addr_le_is_valid` #71801

Open Thalley opened 2 months ago

Thalley commented 2 months ago

Is your enhancement proposal related to a problem? Please describe. We have several public APIs where are receive a bt_addr_le_t *addr, but we never check if the addr is valid before sending it on air or to the controller.

Describe the solution you'd like Add a bool bt_addr_le_is_valid function that verifies if the address provided is valid. The easiest check to add for it is simply to verify that the type is valid, but the core spec may have additional requirements for valid addresses.

The check should then be added to all public functions where we have a bt_addr_le_t *addr as an argument like so

int foo(bt_addr_le_t *addr)
{
    CHECKIF(!bt_addr_le_is_valid(addr)) {
        return -EINVAL;
    }

    return 0; 
}

Describe alternatives you've considered N/A

Additional context Core spec, 5.4, vol 6, Part B adds additional requirements for some address. For example for random device addresses, certain bits shall be set based on the type: image

With additional requirements such as image

These requirements should also be added to the function.

Functions such as int bt_addr_le_create_nrpa(bt_addr_le_t *addr) and int bt_addr_le_create_static(bt_addr_le_t *addr) should also use this function to verify that the address they generate is valid.

jhedberg commented 2 months ago

No objections to the feature proposal, but the name should really be bt_addr_le_is_valid() if it takes a bt_addr_le_t * as input (since we also have a bt_addr_t type).