1 /* Bluetooth Mesh */
2
3 /*
4 * Copyright (c) 2017 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #ifndef __NET_H__
10 #define __NET_H__
11
12 #define BT_MESH_NET_FLAG_KR BIT(0)
13 #define BT_MESH_NET_FLAG_IVU BIT(1)
14
15 #define BT_MESH_KR_NORMAL 0x00
16 #define BT_MESH_KR_PHASE_1 0x01
17 #define BT_MESH_KR_PHASE_2 0x02
18 #define BT_MESH_KR_PHASE_3 0x03
19
20 #define BT_MESH_IV_UPDATE(flags) (((flags) >> 1) & 0x01)
21 #define BT_MESH_KEY_REFRESH(flags) ((flags) & 0x01)
22
23 #include <stdbool.h>
24 #include "atomic.h"
25 #include "mesh/mesh.h"
26 #include "mesh/glue.h"
27
28 /* How many hours in between updating IVU duration */
29 #define BT_MESH_IVU_MIN_HOURS 96
30 #define BT_MESH_IVU_HOURS (BT_MESH_IVU_MIN_HOURS / \
31 CONFIG_BT_MESH_IVU_DIVIDER)
32 #define BT_MESH_IVU_TIMEOUT K_HOURS(BT_MESH_IVU_HOURS)
33
34 struct bt_mesh_app_key {
35 u16_t net_idx;
36 u16_t app_idx;
37 bool updated;
38 struct bt_mesh_app_keys {
39 u8_t id;
40 u8_t val[16];
41 } keys[2];
42 };
43
44 struct bt_mesh_node {
45 u16_t addr;
46 u16_t net_idx;
47 u8_t dev_key[16];
48 u8_t num_elem;
49 };
50
51 struct bt_mesh_subnet {
52 u32_t beacon_sent; /* Timestamp of last sent beacon */
53 u8_t beacons_last; /* Number of beacons during last
54 * observation window
55 */
56 u8_t beacons_cur; /* Number of beaconds observed during
57 * currently ongoing window.
58 */
59
60 u8_t beacon_cache[21]; /* Cached last authenticated beacon */
61
62 u16_t net_idx; /* NetKeyIndex */
63
64 bool kr_flag; /* Key Refresh Flag */
65 u8_t kr_phase; /* Key Refresh Phase */
66
67 u8_t node_id; /* Node Identity State */
68 u32_t node_id_start; /* Node Identity started timestamp */
69
70 u8_t auth[8]; /* Beacon Authentication Value */
71
72 struct bt_mesh_subnet_keys {
73 u8_t net[16]; /* NetKey */
74 u8_t nid; /* NID */
75 u8_t enc[16]; /* EncKey */
76 u8_t net_id[8]; /* Network ID */
77 #if (MYNEWT_VAL(BLE_MESH_GATT_PROXY))
78 u8_t identity[16]; /* IdentityKey */
79 #endif
80 u8_t privacy[16]; /* PrivacyKey */
81 u8_t beacon[16]; /* BeaconKey */
82 } keys[2];
83 };
84
85 struct bt_mesh_rpl {
86 u16_t src;
87 bool old_iv;
88 #if (MYNEWT_VAL(BLE_MESH_SETTINGS))
89 bool store;
90 #endif
91 u32_t seq;
92 };
93
94 #if MYNEWT_VAL(BLE_MESH_FRIEND)
95 #define FRIEND_SEG_RX MYNEWT_VAL(BLE_MESH_FRIEND_SEG_RX)
96 #define FRIEND_SUB_LIST_SIZE MYNEWT_VAL(BLE_MESH_FRIEND_SUB_LIST_SIZE)
97 #else
98 #define FRIEND_SEG_RX 0
99 #define FRIEND_SUB_LIST_SIZE 0
100 #endif
101
102 struct bt_mesh_friend {
103 u16_t lpn;
104 u8_t recv_delay;
105 u8_t fsn : 1,
106 send_last : 1,
107 pending_req : 1,
108 sec_update : 1,
109 pending_buf : 1,
110 valid : 1,
111 established : 1;
112 s32_t poll_to;
113 u8_t num_elem;
114 u16_t lpn_counter;
115 u16_t counter;
116
117 u16_t net_idx;
118
119 u16_t sub_list[FRIEND_SUB_LIST_SIZE];
120
121 struct k_delayed_work timer;
122
123 struct bt_mesh_friend_seg {
124 struct net_buf_slist_t queue;
125
126 /* The target number of segments, i.e. not necessarily
127 * the current number of segments, in the queue. This is
128 * used for Friend Queue free space calculations.
129 */
130 u8_t seg_count;
131 } seg[FRIEND_SEG_RX];
132
133 struct os_mbuf *last;
134
135 struct net_buf_slist_t queue;
136 u32_t queue_size;
137
138 /* Friend Clear Procedure */
139 struct {
140 u32_t start; /* Clear Procedure start */
141 u16_t frnd; /* Previous Friend's address */
142 u16_t repeat_sec; /* Repeat timeout in seconds */
143 struct k_delayed_work timer; /* Repeat timer */
144 } clear;
145 };
146
147 #if (MYNEWT_VAL(BLE_MESH_LOW_POWER))
148 #define LPN_GROUPS CONFIG_BT_MESH_LPN_GROUPS
149 #else
150 #define LPN_GROUPS 1
151 #endif
152
153 /* Low Power Node state */
154 struct bt_mesh_lpn {
155 enum __packed {
156 BT_MESH_LPN_DISABLED, /* LPN feature is disabled */
157 BT_MESH_LPN_CLEAR, /* Clear in progress */
158 BT_MESH_LPN_TIMER, /* Waiting for auto timer expiry */
159 BT_MESH_LPN_ENABLED, /* LPN enabled, but no Friend */
160 BT_MESH_LPN_REQ_WAIT, /* Wait before scanning for offers */
161 BT_MESH_LPN_WAIT_OFFER, /* Friend Req sent */
162 BT_MESH_LPN_ESTABLISHED, /* Friendship established */
163 BT_MESH_LPN_RECV_DELAY, /* Poll sent, waiting ReceiveDelay */
164 BT_MESH_LPN_WAIT_UPDATE, /* Waiting for Update or message */
165 } state;
166
167 /* Transaction Number (used for subscription list) */
168 u8_t xact_next;
169 u8_t xact_pending;
170 u8_t sent_req;
171
172 /* Address of our Friend when we're a LPN. Unassigned if we don't
173 * have a friend yet.
174 */
175 u16_t frnd;
176
177 /* Value from the friend offer */
178 u8_t recv_win;
179
180 u8_t req_attempts; /* Number of Request attempts */
181
182 s32_t poll_timeout;
183
184 u8_t groups_changed : 1, /* Friend Subscription List needs updating */
185 pending_poll : 1, /* Poll to be sent after subscription */
186 disable : 1, /* Disable LPN after clearing */
187 fsn : 1, /* Friend Sequence Number */
188 established : 1, /* Friendship established */
189 clear_success : 1; /* Friend Clear Confirm received */
190
191 /* Friend Queue Size */
192 u8_t queue_size;
193
194 /* LPNCounter */
195 u16_t counter;
196
197 /* Previous Friend of this LPN */
198 u16_t old_friend;
199
200 /* Duration reported for last advertising packet */
201 u16_t adv_duration;
202
203 /* Next LPN related action timer */
204 struct k_delayed_work timer;
205
206 /* Subscribed groups */
207 u16_t groups[LPN_GROUPS];
208
209 /* Bit fields for tracking which groups the Friend knows about */
210 ATOMIC_DEFINE(added, LPN_GROUPS);
211 ATOMIC_DEFINE(pending, LPN_GROUPS);
212 ATOMIC_DEFINE(to_remove, LPN_GROUPS);
213 };
214
215 /* bt_mesh_net.flags */
216 enum {
217 BT_MESH_VALID, /* We have been provisioned */
218 BT_MESH_SUSPENDED, /* Network is temporarily suspended */
219 BT_MESH_IVU_IN_PROGRESS, /* IV Update in Progress */
220 BT_MESH_IVU_INITIATOR, /* IV Update initiated by us */
221 BT_MESH_IVU_TEST, /* IV Update test mode */
222 BT_MESH_IVU_PENDING, /* Update blocked by SDU in progress */
223
224 /* pending storage actions, must reside within first 32 flags */
225 BT_MESH_RPL_PENDING,
226 BT_MESH_KEYS_PENDING,
227 BT_MESH_NET_PENDING,
228 BT_MESH_IV_PENDING,
229 BT_MESH_SEQ_PENDING,
230 BT_MESH_HB_PUB_PENDING,
231 BT_MESH_CFG_PENDING,
232 BT_MESH_MOD_PENDING,
233 BT_MESH_VA_PENDING,
234 BT_MESH_NODES_PENDING,
235
236 /* Don't touch - intentionally last */
237 BT_MESH_FLAG_COUNT,
238 };
239
240 struct bt_mesh_net {
241 u32_t iv_index; /* Current IV Index */
242 u32_t seq; /* Next outgoing sequence number (24 bits) */
243
244 ATOMIC_DEFINE(flags, BT_MESH_FLAG_COUNT);
245
246 /* Local network interface */
247 struct ble_npl_callout local_work;
248 struct net_buf_slist_t local_queue;
249
250 #if MYNEWT_VAL(BLE_MESH_FRIEND)
251 /* Friend state, unique for each LPN that we're Friends for */
252 struct bt_mesh_friend frnd[MYNEWT_VAL(BLE_MESH_FRIEND_LPN_COUNT)];
253 #endif
254
255 #if (MYNEWT_VAL(BLE_MESH_LOW_POWER))
256 struct bt_mesh_lpn lpn; /* Low Power Node state */
257 #endif
258
259 /* Number of hours in current IV Update state */
260 u8_t ivu_duration;
261
262 /* Timer to track duration in current IV Update state */
263 struct k_delayed_work ivu_timer;
264
265 u8_t dev_key[16];
266
267 #if MYNEWT_VAL(BLE_MESH_PROVISIONER)
268 struct bt_mesh_node nodes[MYNEWT_VAL(BLE_MESH_NODE_COUNT)];
269 #endif
270
271 struct bt_mesh_app_key app_keys[MYNEWT_VAL(BLE_MESH_APP_KEY_COUNT)];
272
273 struct bt_mesh_subnet sub[MYNEWT_VAL(BLE_MESH_SUBNET_COUNT)];
274
275 struct bt_mesh_rpl rpl[MYNEWT_VAL(BLE_MESH_CRPL)];
276 };
277
278 /* Network interface */
279 enum bt_mesh_net_if {
280 BT_MESH_NET_IF_ADV,
281 BT_MESH_NET_IF_LOCAL,
282 BT_MESH_NET_IF_PROXY,
283 BT_MESH_NET_IF_PROXY_CFG,
284 };
285
286 /* Decoding context for Network/Transport data */
287 struct bt_mesh_net_rx {
288 struct bt_mesh_subnet *sub;
289 struct bt_mesh_msg_ctx ctx;
290 u32_t seq; /* Sequence Number */
291 u8_t old_iv : 1, /* iv_index - 1 was used */
292 new_key : 1, /* Data was encrypted with updated key */
293 friend_cred : 1, /* Data was encrypted with friend cred */
294 ctl : 1, /* Network Control */
295 net_if : 2, /* Network interface */
296 local_match : 1, /* Matched a local element */
297 friend_match : 1; /* Matched an LPN we're friends for */
298 u16_t msg_cache_idx; /* Index of entry in message cache */
299 };
300
301 /* Encoding context for Network/Transport data */
302 struct bt_mesh_net_tx {
303 struct bt_mesh_subnet *sub;
304 struct bt_mesh_msg_ctx *ctx;
305 u16_t src;
306 u8_t xmit;
307 u8_t friend_cred : 1,
308 aszmic : 1,
309 aid : 6;
310 };
311
312 extern struct bt_mesh_net bt_mesh;
313
314 #define BT_MESH_NET_IVI_TX (bt_mesh.iv_index - \
315 atomic_test_bit(bt_mesh.flags, \
316 BT_MESH_IVU_IN_PROGRESS))
317 #define BT_MESH_NET_IVI_RX(rx) (bt_mesh.iv_index - (rx)->old_iv)
318
319 #define BT_MESH_NET_HDR_LEN 9
320
321 int bt_mesh_net_keys_create(struct bt_mesh_subnet_keys *keys,
322 const u8_t key[16]);
323
324 int bt_mesh_net_create(u16_t idx, u8_t flags, const u8_t key[16],
325 u32_t iv_index);
326
327 u8_t bt_mesh_net_flags(struct bt_mesh_subnet *sub);
328
329 bool bt_mesh_kr_update(struct bt_mesh_subnet *sub, u8_t new_kr, bool new_key);
330
331 void bt_mesh_net_revoke_keys(struct bt_mesh_subnet *sub);
332
333 int bt_mesh_net_beacon_update(struct bt_mesh_subnet *sub);
334
335 void bt_mesh_rpl_reset(void);
336
337 bool bt_mesh_net_iv_update(u32_t iv_index, bool iv_update);
338
339 void bt_mesh_net_sec_update(struct bt_mesh_subnet *sub);
340
341 struct bt_mesh_subnet *bt_mesh_subnet_get(u16_t net_idx);
342
343 struct bt_mesh_subnet *bt_mesh_subnet_find(const u8_t net_id[8], u8_t flags,
344 u32_t iv_index, const u8_t auth[8],
345 bool *new_key);
346
347 int bt_mesh_net_encode(struct bt_mesh_net_tx *tx, struct os_mbuf *buf,
348 bool proxy);
349
350 int bt_mesh_net_send(struct bt_mesh_net_tx *tx, struct os_mbuf *buf,
351 const struct bt_mesh_send_cb *cb, void *cb_data);
352
353 int bt_mesh_net_resend(struct bt_mesh_subnet *sub, struct os_mbuf *buf,
354 bool new_key, const struct bt_mesh_send_cb *cb,
355 void *cb_data);
356
357 int bt_mesh_net_decode(struct os_mbuf *data, enum bt_mesh_net_if net_if,
358 struct bt_mesh_net_rx *rx, struct os_mbuf *buf);
359
360 void bt_mesh_net_recv(struct os_mbuf *data, s8_t rssi,
361 enum bt_mesh_net_if net_if);
362
363 u32_t bt_mesh_next_seq(void);
364
365 void bt_mesh_net_start(void);
366
367 void bt_mesh_net_init(void);
368 void bt_mesh_net_header_parse(struct os_mbuf *buf,
369 struct bt_mesh_net_rx *rx);
370
371 /* Friendship Credential Management */
372 struct friend_cred {
373 u16_t net_idx;
374 u16_t addr;
375
376 u16_t lpn_counter;
377 u16_t frnd_counter;
378
379 struct {
380 u8_t nid; /* NID */
381 u8_t enc[16]; /* EncKey */
382 u8_t privacy[16]; /* PrivacyKey */
383 } cred[2];
384 };
385
386 int friend_cred_get(struct bt_mesh_subnet *sub, u16_t addr, u8_t *nid,
387 const u8_t **enc, const u8_t **priv);
388 int friend_cred_set(struct friend_cred *cred, u8_t idx, const u8_t net_key[16]);
389 void friend_cred_refresh(u16_t net_idx);
390 int friend_cred_update(struct bt_mesh_subnet *sub);
391 struct friend_cred *friend_cred_create(struct bt_mesh_subnet *sub, u16_t addr,
392 u16_t lpn_counter, u16_t frnd_counter);
393 void friend_cred_clear(struct friend_cred *cred);
394 int friend_cred_del(u16_t net_idx, u16_t addr);
395
send_cb_finalize(const struct bt_mesh_send_cb * cb,void * cb_data)396 static inline void send_cb_finalize(const struct bt_mesh_send_cb *cb,
397 void *cb_data)
398 {
399 if (!cb) {
400 return;
401 }
402
403 if (cb->start) {
404 cb->start(0, 0, cb_data);
405 }
406
407 if (cb->end) {
408 cb->end(0, cb_data);
409 }
410 }
411
412 #endif
413