net.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /* Bluetooth Mesh */
  2. /*
  3. * SPDX-FileCopyrightText: 2017 Intel Corporation
  4. * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. */
  8. #ifndef _NET_H_
  9. #define _NET_H_
  10. #include "mesh/access.h"
  11. #include "mesh/mutex.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #define BLE_MESH_NET_FLAG_KR BIT(0)
  16. #define BLE_MESH_NET_FLAG_IVU BIT(1)
  17. #define BLE_MESH_KR_NORMAL 0x00
  18. #define BLE_MESH_KR_PHASE_1 0x01
  19. #define BLE_MESH_KR_PHASE_2 0x02
  20. #define BLE_MESH_KR_PHASE_3 0x03
  21. #define BLE_MESH_IV_UPDATE(flags) ((flags >> 1) & 0x01)
  22. #define BLE_MESH_KEY_REFRESH(flags) (flags & 0x01)
  23. /* How many hours in between updating IVU duration */
  24. #define BLE_MESH_IVU_MIN_HOURS 96
  25. #define BLE_MESH_IVU_HOURS (BLE_MESH_IVU_MIN_HOURS / CONFIG_BLE_MESH_IVU_DIVIDER)
  26. #define BLE_MESH_IVU_TIMEOUT K_HOURS(BLE_MESH_IVU_HOURS)
  27. struct bt_mesh_app_key {
  28. uint16_t net_idx;
  29. uint16_t app_idx;
  30. bool updated;
  31. struct bt_mesh_app_keys {
  32. uint8_t id;
  33. uint8_t val[16];
  34. } keys[2];
  35. };
  36. struct bt_mesh_subnet {
  37. uint32_t snb_sent; /* Timestamp of last sent secure network beacon */
  38. uint8_t snb_last; /* Number of secure network beacons during last observation window */
  39. uint8_t snb_cur; /* Number of secure network beacons observed during currently ongoing window.*/
  40. uint8_t snb_cache[21]; /* Cached last receive authenticated secure beacon */
  41. #if CONFIG_BLE_MESH_PRIVATE_BEACON
  42. uint32_t mpb_sent; /* Timestamp of last sent private beacon */
  43. uint8_t mpb_last; /* Number of private beacons during last observation window */
  44. uint8_t mpb_cur; /* Number of private beacons observed during currently ongoing window. */
  45. uint8_t mpb_cache[21]; /* Cached last receive private beacon (Random + Authentication Tag) */
  46. uint8_t mpb_flags_last; /* Flags of last sent private beacon */
  47. uint8_t mpb_ivi_last: 1; /* IV Index of last sent private beacon */
  48. uint8_t mpb_random[13]; /* Random of current private beacon */
  49. uint8_t private_node_id; /* Private Node Identity State */
  50. #endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */
  51. uint16_t net_idx; /* NetKeyIndex */
  52. #if CONFIG_BLE_MESH_BRC_SRV
  53. uint16_t sbr_net_idx; /* NetKeyIndex of bridged subnet */
  54. #endif
  55. bool kr_flag; /* Key Refresh Flag */
  56. uint8_t kr_phase; /* Key Refresh Phase */
  57. uint8_t node_id; /* Node Identity State */
  58. uint32_t node_id_start; /* Node Identity started timestamp */
  59. uint8_t auth[8]; /* Beacon Authentication Value */
  60. struct bt_mesh_subnet_keys {
  61. uint8_t net[16]; /* NetKey */
  62. uint8_t nid; /* NID */
  63. uint8_t enc[16]; /* EncKey */
  64. uint8_t net_id[8]; /* Network ID */
  65. #if CONFIG_BLE_MESH_GATT_PROXY_SERVER
  66. uint8_t identity[16]; /* IdentityKey */
  67. #endif /* CONFIG_BLE_MESH_GATT_PROXY_SERVER */
  68. uint8_t privacy[16]; /* PrivacyKey */
  69. uint8_t beacon[16]; /* BeaconKey */
  70. #if CONFIG_BLE_MESH_PRIVATE_BEACON
  71. uint8_t private_beacon[16]; /* Private BeaconKey */
  72. #endif /* CONFIG_BLE_MESH_PRIVATE_BEACON */
  73. #if CONFIG_BLE_MESH_DF_SRV
  74. uint8_t direct_nid; /* Directed NID */
  75. uint8_t direct_enc[16]; /* Directed EncKey */
  76. uint8_t direct_privacy[16]; /* Directed PrivacyKey */
  77. #endif /* CONFIG_BLE_MESH_DF_SRV */
  78. } keys[2];
  79. /* Indicate if proxy privacy is enabled (i.e. sending Mesh Private Beacons
  80. * or Secure Network Beacons) to proxy client.
  81. *
  82. * Note: in Mesh Spec, it describes as "the Proxy Privacy parameter for the
  83. * connection". Here we put the parameter in the subnet, since when sending
  84. * mesh beacon, and for the existing subnets, proxy server should send mesh
  85. * beacon for each of the subnets.
  86. */
  87. uint8_t proxy_privacy;
  88. #if CONFIG_BLE_MESH_DF_SRV
  89. uint8_t directed_forwarding;
  90. uint8_t directed_relay; /* Binding with Directed Forwarding state */
  91. uint8_t directed_proxy; /* Binding with Directed Forwarding state & GATT Proxy state */
  92. uint8_t directed_proxy_use_default; /* Binding with Directed Proxy state */
  93. uint8_t directed_friend;
  94. uint8_t use_directed;
  95. struct {
  96. uint16_t len_present:1,
  97. range_start:15;
  98. uint8_t range_length;
  99. } proxy_client_uar;
  100. uint8_t path_metric_type:3,
  101. path_lifetime_type:2,
  102. two_way_path:1;
  103. uint8_t forward_number;
  104. /* The Discovery Table initially is empty. A Path Origin updates
  105. * its Discovery Table when a Directed Forwarding Initialization
  106. * procedure is executed. A Path Target or a Directed Relay node
  107. * updates its Discovery Table when a PATH_REQUEST message is
  108. * received and processed.
  109. */
  110. struct bt_mesh_discovery_table {
  111. uint8_t max_disc_entries;
  112. uint8_t max_concurr_init; /* default is 0x02 */
  113. uint8_t concurr_init;
  114. bt_mesh_mutex_t mutex;
  115. sys_slist_t entries;
  116. } discovery_table;
  117. struct bt_mesh_forward_table {
  118. uint8_t max_ford_entries;
  119. uint8_t max_deps_nodes;
  120. uint16_t update_id;
  121. bt_mesh_mutex_t mutex;
  122. sys_slist_t entries;
  123. } forward_table;
  124. uint8_t wanted_lanes;
  125. uint8_t unicast_echo_interval;
  126. uint8_t multicast_echo_interval;
  127. #endif /* CONFIG_BLE_MESH_DF_SRV */
  128. };
  129. struct bt_mesh_rpl {
  130. uint16_t src;
  131. bool old_iv;
  132. #if CONFIG_BLE_MESH_SETTINGS
  133. bool store;
  134. #endif
  135. uint32_t seq;
  136. };
  137. #if CONFIG_BLE_MESH_FRIEND
  138. #define FRIEND_SEG_RX CONFIG_BLE_MESH_FRIEND_SEG_RX
  139. #define FRIEND_SUB_LIST_SIZE CONFIG_BLE_MESH_FRIEND_SUB_LIST_SIZE
  140. #else
  141. #define FRIEND_SEG_RX 0
  142. #define FRIEND_SUB_LIST_SIZE 0
  143. #endif
  144. struct bt_mesh_friend {
  145. uint16_t lpn;
  146. uint8_t recv_delay;
  147. uint8_t fsn:1,
  148. send_last:1,
  149. pending_req:1,
  150. pending_buf:1,
  151. valid:1,
  152. established:1;
  153. int32_t poll_to;
  154. uint8_t num_elem;
  155. uint16_t lpn_counter;
  156. uint16_t counter;
  157. uint16_t net_idx;
  158. uint16_t sub_list[FRIEND_SUB_LIST_SIZE];
  159. struct k_delayed_work timer;
  160. struct bt_mesh_friend_seg {
  161. sys_slist_t queue;
  162. /* The target number of segments, i.e. not necessarily
  163. * the current number of segments, in the queue. This is
  164. * used for Friend Queue free space calculations.
  165. */
  166. uint8_t seg_count;
  167. } seg[FRIEND_SEG_RX];
  168. struct net_buf *last;
  169. sys_slist_t queue;
  170. uint32_t queue_size;
  171. /* Friend Clear Procedure */
  172. struct {
  173. uint32_t start; /* Clear Procedure start */
  174. uint16_t frnd; /* Previous Friend's address */
  175. uint16_t repeat_sec; /* Repeat timeout in seconds */
  176. struct k_delayed_work timer; /* Repeat timer */
  177. } clear;
  178. };
  179. #if CONFIG_BLE_MESH_LOW_POWER
  180. #define LPN_GROUPS CONFIG_BLE_MESH_LPN_GROUPS
  181. #else
  182. #define LPN_GROUPS 0
  183. #endif
  184. /* Low Power Node state */
  185. struct bt_mesh_lpn {
  186. enum __attribute__((packed)) {
  187. BLE_MESH_LPN_DISABLED, /* LPN feature is disabled */
  188. BLE_MESH_LPN_CLEAR, /* Clear in progress */
  189. BLE_MESH_LPN_TIMER, /* Waiting for auto timer expiry */
  190. BLE_MESH_LPN_ENABLED, /* LPN enabled, but no Friend */
  191. BLE_MESH_LPN_REQ_WAIT, /* Wait before scanning for offers */
  192. BLE_MESH_LPN_WAIT_OFFER, /* Friend Req sent */
  193. BLE_MESH_LPN_ESTABLISHED, /* Friendship established */
  194. BLE_MESH_LPN_RECV_DELAY, /* Poll sent, waiting ReceiveDelay */
  195. BLE_MESH_LPN_WAIT_UPDATE, /* Waiting for Update or message */
  196. BLE_MESH_LPN_OFFER_RECV, /* Friend offer received */
  197. } state;
  198. /* Transaction Number (used for subscription list) */
  199. uint8_t xact_next;
  200. uint8_t xact_pending;
  201. uint8_t sent_req;
  202. /* Address of our Friend when we're a LPN. Unassigned if we don't
  203. * have a friend yet.
  204. */
  205. uint16_t frnd;
  206. /* Value from the friend offer */
  207. uint8_t recv_win;
  208. uint8_t req_attempts; /* Number of Request attempts */
  209. int32_t poll_timeout;
  210. uint8_t groups_changed: 1, /* Friend Subscription List needs updating */
  211. pending_poll: 1, /* Poll to be sent after subscription */
  212. disable: 1, /* Disable LPN after clearing */
  213. fsn: 1, /* Friend Sequence Number */
  214. established: 1, /* Friendship established */
  215. clear_success: 1; /* Friend Clear Confirm received */
  216. /* Friend Queue Size */
  217. uint8_t queue_size;
  218. /* LPNCounter */
  219. uint16_t counter;
  220. /* Previous Friend of this LPN */
  221. uint16_t old_friend;
  222. #if CONFIG_BLE_MESH_DF_SRV
  223. uint8_t old_directed_forwarding;
  224. #endif
  225. /* Duration reported for last advertising packet */
  226. uint16_t adv_duration;
  227. /* Next LPN related action timer */
  228. struct k_delayed_work timer;
  229. /* Subscribed groups */
  230. uint16_t groups[LPN_GROUPS];
  231. /* Bit fields for tracking which groups the Friend knows about */
  232. BLE_MESH_ATOMIC_DEFINE(added, LPN_GROUPS);
  233. BLE_MESH_ATOMIC_DEFINE(pending, LPN_GROUPS);
  234. BLE_MESH_ATOMIC_DEFINE(to_remove, LPN_GROUPS);
  235. };
  236. /* bt_mesh_net.flags */
  237. enum {
  238. BLE_MESH_NODE, /* Device is a node */
  239. BLE_MESH_PROVISIONER, /* Device is a Provisioner */
  240. BLE_MESH_VALID, /* We have been provisioned */
  241. BLE_MESH_VALID_PROV, /* Provisioner has been enabled */
  242. BLE_MESH_SUSPENDED, /* Network is temporarily suspended */
  243. BLE_MESH_IVU_IN_PROGRESS, /* IV Update in Progress */
  244. BLE_MESH_IVU_INITIATOR, /* IV Update initiated by us */
  245. BLE_MESH_IVU_TEST, /* IV Update test mode */
  246. BLE_MESH_IVU_PENDING, /* Update blocked by SDU in progress */
  247. /* pending storage actions, must reside within first 32 flags */
  248. BLE_MESH_RPL_PENDING,
  249. BLE_MESH_KEYS_PENDING,
  250. BLE_MESH_NET_PENDING,
  251. BLE_MESH_IV_PENDING,
  252. BLE_MESH_SEQ_PENDING,
  253. BLE_MESH_HB_PUB_PENDING,
  254. BLE_MESH_CFG_PENDING,
  255. BLE_MESH_MOD_PENDING,
  256. BLE_MESH_VA_PENDING,
  257. /* Don't touch - intentionally last */
  258. BLE_MESH_FLAG_COUNT,
  259. };
  260. struct bt_mesh_net {
  261. uint32_t iv_index; /* Current IV Index */
  262. uint32_t seq; /* Next outgoing sequence number (24 bits) */
  263. BLE_MESH_ATOMIC_DEFINE(flags, BLE_MESH_FLAG_COUNT);
  264. /* Local network interface */
  265. sys_slist_t local_queue;
  266. #if CONFIG_BLE_MESH_FRIEND
  267. /* Friend state, unique for each LPN that we're Friends for */
  268. struct bt_mesh_friend frnd[CONFIG_BLE_MESH_FRIEND_LPN_COUNT];
  269. #endif
  270. #if CONFIG_BLE_MESH_LOW_POWER
  271. struct bt_mesh_lpn lpn; /* Low Power Node state */
  272. #endif
  273. /* Number of hours in current IV Update state */
  274. uint8_t ivu_duration;
  275. /* Timer to track duration in current IV Update state */
  276. struct k_delayed_work ivu_timer;
  277. uint8_t dev_key[16]; /* Device Key */
  278. uint8_t dev_key_ca[16]; /* Device Key Candidate */
  279. struct bt_mesh_app_key app_keys[CONFIG_BLE_MESH_APP_KEY_COUNT];
  280. struct bt_mesh_subnet sub[CONFIG_BLE_MESH_SUBNET_COUNT];
  281. struct bt_mesh_rpl rpl[CONFIG_BLE_MESH_CRPL];
  282. #if CONFIG_BLE_MESH_PROVISIONER
  283. /* Application keys stored by provisioner */
  284. struct bt_mesh_app_key *p_app_keys[CONFIG_BLE_MESH_PROVISIONER_APP_KEY_COUNT];
  285. /* Next app_idx can be assigned */
  286. uint16_t p_app_idx_next;
  287. /* Network keys stored by provisioner */
  288. struct bt_mesh_subnet *p_sub[CONFIG_BLE_MESH_PROVISIONER_SUBNET_COUNT];
  289. /* Next net_idx can be assigned */
  290. uint16_t p_net_idx_next;
  291. #endif
  292. };
  293. /* Network interface */
  294. enum bt_mesh_net_if {
  295. BLE_MESH_NET_IF_ADV,
  296. BLE_MESH_NET_IF_LOCAL,
  297. BLE_MESH_NET_IF_PROXY,
  298. BLE_MESH_NET_IF_PROXY_CFG,
  299. };
  300. #define BLE_MESH_NONE_BEARER 0
  301. #define BLE_MESH_ADV_BEARER BIT(0)
  302. #define BLE_MESH_GATT_BEARER BIT(1)
  303. #define BLE_MESH_LOCAL_BEARER BIT(2)
  304. #define BLE_MESH_ALL_BEARERS (BLE_MESH_ADV_BEARER | BLE_MESH_GATT_BEARER)
  305. /* Decoding context for Network/Transport data */
  306. struct bt_mesh_net_rx {
  307. struct bt_mesh_subnet *sub;
  308. struct bt_mesh_msg_ctx ctx;
  309. uint32_t seq; /* Sequence Number */
  310. uint16_t old_iv:1, /* iv_index - 1 was used */
  311. new_key:1, /* Data was encrypted with updated key */
  312. friend_cred:1 __attribute__((deprecated)), /* Data was encrypted with friend cred */
  313. ctl:1, /* Network Control */
  314. net_if:2, /* Network interface */
  315. local_match:1, /* Matched a local element */
  316. friend_match:1, /* Matched an LPN we're friends for */
  317. sbr_rpl:1; /* Bridge RPL attacker */
  318. uint16_t msg_cache_idx; /* Index of entry in message cache */
  319. };
  320. /* Encoding context for Network/Transport data */
  321. struct bt_mesh_net_tx {
  322. struct bt_mesh_subnet *sub;
  323. struct bt_mesh_msg_ctx *ctx;
  324. uint16_t src;
  325. uint8_t xmit;
  326. uint8_t friend_cred:1 __attribute__((deprecated)),
  327. aszmic:1,
  328. aid:6;
  329. };
  330. extern struct bt_mesh_net bt_mesh;
  331. #define BLE_MESH_NET_IVI_TX (bt_mesh.iv_index - \
  332. bt_mesh_atomic_test_bit(bt_mesh.flags, \
  333. BLE_MESH_IVU_IN_PROGRESS))
  334. #define BLE_MESH_NET_IVI_RX(rx) (bt_mesh.iv_index - (rx)->old_iv)
  335. #define BLE_MESH_NET_HDR_LEN 9
  336. #define BLE_MESH_NET_HDR_IVI(pdu) ((pdu)[0] >> 7)
  337. #define BLE_MESH_NET_HDR_NID(pdu) ((pdu)[0] & 0x7F)
  338. #define BLE_MESH_NET_HDR_CTL(pdu) ((pdu)[1] >> 7)
  339. #define BLE_MESH_NET_HDR_TTL(pdu) ((pdu)[1] & 0x7F)
  340. #define BLE_MESH_NET_HDR_SEQ(pdu) (sys_get_be24(&(pdu)[2]))
  341. #define BLE_MESH_NET_HDR_SRC(pdu) (sys_get_be16(&(pdu)[5]))
  342. #define BLE_MESH_NET_HDR_DST(pdu) (sys_get_be16(&(pdu)[7]))
  343. void bt_mesh_msg_cache_clear(uint16_t unicast_addr, uint8_t elem_num);
  344. int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys,
  345. const uint8_t key[16]);
  346. int bt_mesh_net_create(uint16_t idx, uint8_t flags, const uint8_t key[16],
  347. uint32_t iv_index);
  348. uint8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub);
  349. bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, uint8_t new_kr, bool new_key);
  350. void bt_mesh_net_revoke_keys(struct bt_mesh_subnet *sub);
  351. int bt_mesh_net_secure_beacon_update(struct bt_mesh_subnet *sub);
  352. bool bt_mesh_net_iv_update(uint32_t iv_index, bool iv_update);
  353. void bt_mesh_net_sec_update(struct bt_mesh_subnet *sub);
  354. struct bt_mesh_subnet *bt_mesh_subnet_get(uint16_t net_idx);
  355. struct bt_mesh_subnet *bt_mesh_subnet_find_with_snb(const uint8_t net_id[8], uint8_t flags,
  356. uint32_t iv_index, const uint8_t auth[8],
  357. bool *new_key);
  358. int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct net_buf_simple *buf,
  359. bool proxy);
  360. int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct net_buf *buf,
  361. const struct bt_mesh_send_cb *cb, void *cb_data);
  362. int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct net_buf *buf,
  363. bool new_key, uint8_t *tx_cred, uint8_t tx_tag,
  364. const struct bt_mesh_send_cb *cb, void *cb_data);
  365. int bt_mesh_net_decode(struct net_buf_simple *data, enum bt_mesh_net_if net_if,
  366. struct bt_mesh_net_rx *rx, struct net_buf_simple *buf);
  367. void bt_mesh_net_recv(struct net_buf_simple *data, int8_t rssi,
  368. enum bt_mesh_net_if net_if);
  369. bool bt_mesh_primary_subnet_exist(void);
  370. uint32_t bt_mesh_next_seq(void);
  371. void bt_mesh_net_start(void);
  372. void bt_mesh_net_init(void);
  373. void bt_mesh_net_reset(void);
  374. void bt_mesh_net_deinit(void);
  375. void bt_mesh_net_header_parse(struct net_buf_simple *buf,
  376. struct bt_mesh_net_rx *rx);
  377. /* Friendship Credential Management */
  378. struct friend_cred {
  379. uint16_t net_idx;
  380. uint16_t addr;
  381. uint16_t lpn_counter;
  382. uint16_t frnd_counter;
  383. struct {
  384. uint8_t nid; /* NID */
  385. uint8_t enc[16]; /* EncKey */
  386. uint8_t privacy[16]; /* PrivacyKey */
  387. } cred[2];
  388. };
  389. int friend_cred_get(struct bt_mesh_subnet *sub, uint16_t addr, uint8_t *nid,
  390. const uint8_t **enc, const uint8_t **priv);
  391. int friend_cred_set(struct friend_cred *cred, uint8_t idx, const uint8_t net_key[16]);
  392. void friend_cred_refresh(uint16_t net_idx);
  393. int friend_cred_update(struct bt_mesh_subnet *sub);
  394. struct friend_cred *friend_cred_create(struct bt_mesh_subnet *sub, uint16_t addr,
  395. uint16_t lpn_counter, uint16_t frnd_counter);
  396. void friend_cred_clear(struct friend_cred *cred);
  397. int friend_cred_del(uint16_t net_idx, uint16_t addr);
  398. static inline void send_cb_finalize(const struct bt_mesh_send_cb *cb,
  399. void *cb_data)
  400. {
  401. if (!cb) {
  402. return;
  403. }
  404. if (cb->start) {
  405. cb->start(0, 0, cb_data);
  406. }
  407. if (cb->end) {
  408. cb->end(0, cb_data);
  409. }
  410. }
  411. #ifdef __cplusplus
  412. }
  413. #endif
  414. #endif /* _NET_H_ */