2020-08-25 17:03:42 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ZEPHYR_SUBSYS_BLUETOOTH_MESH_SUBNET_H_
|
|
|
|
#define ZEPHYR_SUBSYS_BLUETOOTH_MESH_SUBNET_H_
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
2022-05-06 17:12:04 +08:00
|
|
|
#include <zephyr/net/buf.h>
|
includes: prefer <zephyr/kernel.h> over <zephyr/zephyr.h>
As of today <zephyr/zephyr.h> is 100% equivalent to <zephyr/kernel.h>.
This patch proposes to then include <zephyr/kernel.h> instead of
<zephyr/zephyr.h> since it is more clear that you are including the
Kernel APIs and (probably) nothing else. <zephyr/zephyr.h> sounds like a
catch-all header that may be confusing. Most applications need to
include a bunch of other things to compile, e.g. driver headers or
subsystem headers like BT, logging, etc.
The idea of a catch-all header in Zephyr is probably not feasible
anyway. Reason is that Zephyr is not a library, like it could be for
example `libpython`. Zephyr provides many utilities nowadays: a kernel,
drivers, subsystems, etc and things will likely grow. A catch-all header
would be massive, difficult to keep up-to-date. It is also likely that
an application will only build a small subset. Note that subsystem-level
headers may use a catch-all approach to make things easier, though.
NOTE: This patch is **NOT** removing the header, just removing its usage
in-tree. I'd advocate for its deprecation (add a #warning on it), but I
understand many people will have concerns.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-08-25 15:58:46 +08:00
|
|
|
#include <zephyr/kernel.h>
|
2023-05-15 21:50:28 +08:00
|
|
|
#include <zephyr/sys/iterable_sections.h>
|
2020-08-25 17:03:42 +08:00
|
|
|
|
|
|
|
#define BT_MESH_NET_FLAG_KR BIT(0)
|
|
|
|
#define BT_MESH_NET_FLAG_IVU BIT(1)
|
|
|
|
|
|
|
|
#define BT_MESH_KR_NORMAL 0x00
|
|
|
|
#define BT_MESH_KR_PHASE_1 0x01
|
|
|
|
#define BT_MESH_KR_PHASE_2 0x02
|
|
|
|
#define BT_MESH_KR_PHASE_3 0x03
|
|
|
|
|
|
|
|
/** Which of the two subnet.keys should be used for sending. */
|
|
|
|
#define SUBNET_KEY_TX_IDX(sub) ((sub)->kr_phase == BT_MESH_KR_PHASE_2)
|
|
|
|
|
|
|
|
struct bt_mesh_net_rx;
|
|
|
|
enum bt_mesh_key_evt;
|
|
|
|
|
|
|
|
/** Network message encryption credentials */
|
|
|
|
struct bt_mesh_net_cred {
|
2023-04-21 18:49:14 +08:00
|
|
|
uint8_t nid; /* NID */
|
|
|
|
struct bt_mesh_key enc; /* EncKey */
|
|
|
|
struct bt_mesh_key privacy; /* PrivacyKey */
|
2020-08-25 17:03:42 +08:00
|
|
|
};
|
|
|
|
|
2023-03-28 19:55:58 +08:00
|
|
|
struct bt_mesh_beacon {
|
|
|
|
uint32_t sent; /* Timestamp of last sent beacon */
|
|
|
|
uint32_t recv; /* Timestamp of last received beacon */
|
|
|
|
uint8_t last; /* Number of beacons during last
|
2020-08-25 17:03:42 +08:00
|
|
|
* observation window
|
|
|
|
*/
|
2023-03-28 19:55:58 +08:00
|
|
|
uint8_t cur; /* Number of beacons observed during
|
2020-08-25 17:03:42 +08:00
|
|
|
* currently ongoing window.
|
|
|
|
*/
|
2023-03-28 19:55:58 +08:00
|
|
|
uint8_t cache[8]; /* Cached last beacon auth value */
|
|
|
|
uint8_t auth[8]; /* Beacon Authentication Value */
|
|
|
|
};
|
2020-08-25 17:03:42 +08:00
|
|
|
|
2023-03-28 19:55:58 +08:00
|
|
|
/** Subnet instance. */
|
|
|
|
struct bt_mesh_subnet {
|
2020-08-25 17:03:42 +08:00
|
|
|
uint16_t net_idx; /* NetKeyIndex */
|
|
|
|
|
|
|
|
uint8_t kr_phase; /* Key Refresh Phase */
|
|
|
|
|
|
|
|
uint8_t node_id; /* Node Identity State */
|
|
|
|
uint32_t node_id_start; /* Node Identity started timestamp */
|
|
|
|
|
2023-03-28 19:55:58 +08:00
|
|
|
struct bt_mesh_beacon secure_beacon;
|
|
|
|
|
2020-06-02 18:04:53 +08:00
|
|
|
#if defined(CONFIG_BT_MESH_PRIV_BEACONS)
|
2023-03-28 19:55:58 +08:00
|
|
|
struct bt_mesh_beacon priv_beacon;
|
2020-06-02 18:04:53 +08:00
|
|
|
struct {
|
|
|
|
uint16_t idx; /* Private beacon random index */
|
|
|
|
bool node_id; /* Private Node Identity enabled */
|
|
|
|
uint8_t data[5]; /* Private Beacon data */
|
2023-03-28 19:55:58 +08:00
|
|
|
} priv_beacon_ctx;
|
2020-06-02 18:04:53 +08:00
|
|
|
#endif
|
2020-08-25 17:03:42 +08:00
|
|
|
|
|
|
|
struct bt_mesh_subnet_keys {
|
|
|
|
bool valid;
|
2023-04-21 18:49:14 +08:00
|
|
|
struct bt_mesh_key net; /* NetKey */
|
2020-08-25 17:03:42 +08:00
|
|
|
struct bt_mesh_net_cred msg;
|
2023-04-21 18:49:14 +08:00
|
|
|
uint8_t net_id[8]; /* Network ID */
|
2020-08-25 17:03:42 +08:00
|
|
|
#if defined(CONFIG_BT_MESH_GATT_PROXY)
|
2023-04-21 18:49:14 +08:00
|
|
|
struct bt_mesh_key identity; /* IdentityKey */
|
2020-08-25 17:03:42 +08:00
|
|
|
#endif
|
2023-04-21 18:49:14 +08:00
|
|
|
struct bt_mesh_key beacon; /* BeaconKey */
|
|
|
|
struct bt_mesh_key priv_beacon; /* PrivateBeaconKey */
|
2020-08-25 17:03:42 +08:00
|
|
|
} keys[2];
|
2022-10-19 15:42:02 +08:00
|
|
|
#if defined(CONFIG_BT_MESH_PROXY_SOLICITATION)
|
|
|
|
bool sol_tx;
|
|
|
|
#endif
|
|
|
|
#if defined(CONFIG_BT_MESH_OD_PRIV_PROXY_SRV)
|
|
|
|
uint32_t priv_net_id_sent; /* Timestamp for Private Network ID advertising
|
|
|
|
* started via Proxy Solicitation
|
|
|
|
*/
|
|
|
|
bool solicited; /* Subnet received valid Solicitation PDU */
|
|
|
|
#endif
|
2020-08-25 17:03:42 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** Subnet callback structure. Instantiate with @ref BT_MESH_SUBNET_CB */
|
|
|
|
struct bt_mesh_subnet_cb {
|
|
|
|
void (*evt_handler)(struct bt_mesh_subnet *subnet,
|
|
|
|
enum bt_mesh_key_evt evt);
|
|
|
|
};
|
|
|
|
|
2022-08-05 22:10:35 +08:00
|
|
|
/**
|
2020-08-25 17:03:42 +08:00
|
|
|
* @brief Register a subnet event callback.
|
|
|
|
*
|
2021-08-13 14:10:45 +08:00
|
|
|
* @param _name Handler name.
|
2020-08-25 17:03:42 +08:00
|
|
|
*/
|
2021-08-13 14:10:45 +08:00
|
|
|
#define BT_MESH_SUBNET_CB_DEFINE(_name) \
|
|
|
|
static const STRUCT_SECTION_ITERABLE( \
|
|
|
|
bt_mesh_subnet_cb, _CONCAT(bt_mesh_subnet_cb_, _name))
|
2020-08-25 17:03:42 +08:00
|
|
|
|
|
|
|
/** @brief Reset all Network keys. */
|
|
|
|
void bt_mesh_net_keys_reset(void);
|
|
|
|
|
|
|
|
/** @brief Call cb on every valid Subnet until it returns a non-zero value.
|
|
|
|
*
|
2022-01-13 14:48:53 +08:00
|
|
|
* @param cb Callback to call, or NULL to return first valid subnet. If the callback returns true,
|
|
|
|
* iteration stops, and the passed subnet is returned.
|
2020-08-25 17:03:42 +08:00
|
|
|
* @param cb_data Callback data to pass to callback.
|
|
|
|
*
|
|
|
|
* @return Subnet that returned non-zero value.
|
|
|
|
*/
|
2022-01-13 14:48:53 +08:00
|
|
|
struct bt_mesh_subnet *bt_mesh_subnet_find(bool (*cb)(struct bt_mesh_subnet *sub, void *cb_data),
|
2020-08-25 17:03:42 +08:00
|
|
|
void *cb_data);
|
|
|
|
|
|
|
|
/** @brief Iterate through all valid Subnets.
|
|
|
|
*
|
|
|
|
* @param cb Callback to call on every Subnet.
|
|
|
|
*
|
|
|
|
* @returns The number of valid subnets.
|
|
|
|
*/
|
|
|
|
size_t bt_mesh_subnet_foreach(void (*cb)(struct bt_mesh_subnet *sub));
|
|
|
|
|
|
|
|
/** @brief Get the next valid Subnet.
|
|
|
|
*
|
|
|
|
* If there's only one valid Subnet, this will be returned on every call.
|
|
|
|
*
|
|
|
|
* @param sub Previous Subnet, or NULL to get the first valid.
|
|
|
|
*
|
|
|
|
* @returns Gets the next valid Subnet after @c sub, or NULL if there are no
|
|
|
|
* valid Subnets.
|
|
|
|
*/
|
|
|
|
struct bt_mesh_subnet *bt_mesh_subnet_next(struct bt_mesh_subnet *sub);
|
|
|
|
|
|
|
|
/** @brief Get a pointer to the Subnet with the given index.
|
|
|
|
*
|
|
|
|
* @param net_idx Network index to look for.
|
|
|
|
*
|
|
|
|
* @returns Subnet with index @c net_idx, or NULL if no such Subnet is known.
|
|
|
|
*/
|
|
|
|
struct bt_mesh_subnet *bt_mesh_subnet_get(uint16_t net_idx);
|
|
|
|
|
|
|
|
/** @brief Initialize a new Subnet.
|
|
|
|
*
|
|
|
|
* @param net_idx Network index of the Subnet.
|
|
|
|
* @param kr_phase Key refresh phase the Subnet should be in.
|
|
|
|
* @param key The current network key for the Subnet.
|
|
|
|
* @param new_key New network key, if available.
|
|
|
|
*
|
|
|
|
* @returns 0 on success, or (negative) error code on failure.
|
|
|
|
*/
|
|
|
|
int bt_mesh_subnet_set(uint16_t net_idx, uint8_t kr_phase,
|
2023-04-21 18:49:14 +08:00
|
|
|
const struct bt_mesh_key *key, const struct bt_mesh_key *new_key);
|
2020-08-25 17:03:42 +08:00
|
|
|
|
|
|
|
/** @brief Create Friendship credentials.
|
|
|
|
*
|
|
|
|
* @param cred Credential object to create.
|
|
|
|
* @param lpn_addr Address of the LPN node in the friendship.
|
|
|
|
* @param frnd_addr Address of the Friend node in the friendship.
|
|
|
|
* @param lpn_counter The LPN's counter parameter.
|
|
|
|
* @param frnd_counter The Friend node's counter parameter.
|
|
|
|
* @param key Network key to create the Friendship credentials for.
|
|
|
|
*
|
|
|
|
* @returns 0 on success, or (negative) error code on failure.
|
|
|
|
*/
|
|
|
|
int bt_mesh_friend_cred_create(struct bt_mesh_net_cred *cred,
|
|
|
|
uint16_t lpn_addr, uint16_t frnd_addr,
|
|
|
|
uint16_t lpn_counter, uint16_t frnd_counter,
|
2023-04-21 18:49:14 +08:00
|
|
|
const struct bt_mesh_key *key);
|
|
|
|
|
|
|
|
/** @brief Destroy Friendship credentials.
|
|
|
|
*
|
|
|
|
* @param cred Credential object to destroy.
|
|
|
|
*/
|
|
|
|
void bt_mesh_friend_cred_destroy(struct bt_mesh_net_cred *cred);
|
2020-08-25 17:03:42 +08:00
|
|
|
|
|
|
|
/** @brief Iterate through all valid network credentials to decrypt a message.
|
|
|
|
*
|
|
|
|
* @param rx Network RX parameters, passed to the callback.
|
|
|
|
* @param in Input message buffer, passed to the callback.
|
|
|
|
* @param out Output message buffer, passed to the callback.
|
|
|
|
* @param cb Callback to call for each known network credential. Iteration
|
|
|
|
* stops when this callback returns @c true.
|
|
|
|
*
|
|
|
|
* @returns Whether any of the credentials got a @c true return from the
|
|
|
|
* callback.
|
|
|
|
*/
|
|
|
|
bool bt_mesh_net_cred_find(struct bt_mesh_net_rx *rx, struct net_buf_simple *in,
|
|
|
|
struct net_buf_simple *out,
|
|
|
|
bool (*cb)(struct bt_mesh_net_rx *rx,
|
|
|
|
struct net_buf_simple *in,
|
|
|
|
struct net_buf_simple *out,
|
|
|
|
const struct bt_mesh_net_cred *cred));
|
|
|
|
|
|
|
|
/** @brief Get the network flags of the given Subnet.
|
|
|
|
*
|
|
|
|
* @param sub Subnet to get the network flags of.
|
|
|
|
*
|
|
|
|
* @returns A bitmap of @ref BT_MESH_NET_FLAG_KR and @ref BT_MESH_NET_FLAG_IVU.
|
|
|
|
*/
|
|
|
|
uint8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub);
|
|
|
|
|
|
|
|
/** @brief Process a Key Refresh event from a beacon.
|
|
|
|
*
|
|
|
|
* @param sub Subnet the Key Refresh was received on.
|
|
|
|
* @param kr_flag Key Refresh flag.
|
|
|
|
* @param new_key Whether the Key Refresh event was received on the new key
|
|
|
|
* set.
|
2020-06-02 18:04:53 +08:00
|
|
|
*
|
|
|
|
* @returns Whether the Key Refresh event caused a change.
|
2020-08-25 17:03:42 +08:00
|
|
|
*/
|
|
|
|
void bt_mesh_kr_update(struct bt_mesh_subnet *sub, bool kr_flag, bool new_key);
|
|
|
|
|
|
|
|
/** @brief Check whether the Subnet has the refreshed keys.
|
|
|
|
*
|
|
|
|
* @param sub Subnet.
|
|
|
|
*
|
|
|
|
* @returns Whether the Subnet's second key is valid.
|
|
|
|
*/
|
|
|
|
static inline bool
|
|
|
|
bt_mesh_subnet_has_new_key(const struct bt_mesh_subnet *sub)
|
|
|
|
{
|
|
|
|
return sub->kr_phase != BT_MESH_KR_NORMAL;
|
|
|
|
}
|
|
|
|
|
2023-06-05 17:55:37 +08:00
|
|
|
/** Kind of currently enabled Node Identity state on one or more subnets. */
|
|
|
|
enum bt_mesh_subnets_node_id_state {
|
|
|
|
/* None node identity states are enabled on any subnets. */
|
|
|
|
BT_MESH_SUBNETS_NODE_ID_STATE_NONE,
|
|
|
|
/* Node Identity state is enabled on one or more subnets. */
|
|
|
|
BT_MESH_SUBNETS_NODE_ID_STATE_ENABLED,
|
|
|
|
/* Private Node Identity state is enabled on one or more subnets. */
|
|
|
|
BT_MESH_SUBNETS_NODE_ID_STATE_ENABLED_PRIVATE,
|
|
|
|
};
|
|
|
|
|
|
|
|
/** @brief Returns what kind of node identity state is currently enabled on one or more subnets.
|
|
|
|
*
|
|
|
|
* Only one kind (either non-private or private) can be enabled at the same time on all subnets.
|
|
|
|
*
|
|
|
|
* @returns Kind of node identity state that is currently enabled.
|
|
|
|
*/
|
|
|
|
enum bt_mesh_subnets_node_id_state bt_mesh_subnets_node_id_state_get(void);
|
|
|
|
|
2021-01-06 16:56:56 +08:00
|
|
|
/** @brief Store the Subnet information in persistent storage.
|
|
|
|
*
|
|
|
|
* @param net_idx Network index to store.
|
|
|
|
*/
|
|
|
|
void bt_mesh_subnet_store(uint16_t net_idx);
|
|
|
|
|
|
|
|
/** @brief Store the pending Subnets in persistent storage. */
|
|
|
|
void bt_mesh_subnet_pending_store(void);
|
|
|
|
|
2020-08-25 17:03:42 +08:00
|
|
|
#endif /* ZEPHYR_SUBSYS_BLUETOOTH_MESH_SUBNET_H_ */
|