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