xref: /btstack/src/mesh/mesh_network.h (revision f7434c1fd684409c6de9680669866d3b3d8611e6)
1 /*
2  * Copyright (C) 2018 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #ifndef __MESH_NETWORK
39 #define __MESH_NETWORK
40 
41 #include "btstack_linked_list.h"
42 #include "btstack_run_loop.h"
43 
44 #include "mesh/provisioning.h"
45 #include "mesh/mesh_keys.h"
46 
47 #if defined __cplusplus
48 extern "C" {
49 #endif
50 
51 #define MESH_DEVICE_KEY_INDEX     0xffff
52 
53 #define MESH_NETWORK_PAYLOAD_MAX      29
54 #define MESH_ACCESS_PAYLOAD_MAX      384
55 
56 #define MESH_ADDRESS_UNSASSIGNED     0x0000u
57 #define MESH_ADDRESS_ALL_PROXIES     0xFFFCu
58 #define MESH_ADDRESS_ALL_FRIENDS     0xFFFDu
59 #define MESH_ADDRESS_ALL_RELAYS      0xFFFEu
60 #define MESH_ADDRESS_ALL_NODES       0xFFFFu
61 
62 typedef enum {
63     MESH_NETWORK_PDU_RECEIVED,
64     MESH_NETWORK_PDU_SENT,
65     MESH_NETWORK_CAN_SEND_NOW,
66 } mesh_network_callback_type_t;
67 
68 typedef enum {
69     MESH_PDU_TYPE_NETWORK = 0,
70     MESH_PDU_TYPE_TRANSPORT,
71     MESH_PDU_TYPE_MESSAGE,
72 } mesh_pdu_type_t;
73 
74 typedef struct mesh_pdu {
75     // allow for linked lists
76     btstack_linked_item_t item;
77     // type
78     mesh_pdu_type_t pdu_type;
79 
80     // access acknowledged message
81     uint16_t retransmit_count;
82     uint32_t retransmit_timeout_ms;
83     uint32_t ack_opcode;
84 
85 } mesh_pdu_t;
86 
87 //
88 #define MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION 1
89 #define MESH_NETWORK_PDU_FLAGS_GATT_BEARER         2
90 #define MESH_NETWORK_PDU_FLAGS_RELAY               4
91 
92 typedef struct mesh_network_pdu {
93     mesh_pdu_t pdu_header;
94 
95     // callback
96     void (*callback)(struct mesh_network_pdu * network_pdu);
97 
98     // meta data network layer
99     uint16_t              netkey_index;
100     // meta data transport layer
101     uint16_t              appkey_index;
102     // MESH_NETWORK_PDU_FLAGS
103     uint16_t              flags;
104 
105     // pdu
106     uint16_t              len;
107     uint8_t               data[MESH_NETWORK_PAYLOAD_MAX];
108 } mesh_network_pdu_t;
109 
110 #define MESH_TRANSPORT_FLAG_SEQ_RESERVED    1
111 
112 typedef struct {
113     mesh_pdu_t pdu_header;
114 
115     // rx/tx: acknowledgement timer / segment transmission timer
116     btstack_timer_source_t acknowledgement_timer;
117     // rx: incomplete timer / tx: resend timer
118     btstack_timer_source_t incomplete_timer;
119     // block access
120     uint32_t              block_ack;
121     // meta data network layer
122     uint16_t              netkey_index;
123     // meta data transport layer
124     uint16_t              appkey_index;
125     // transmic size
126     uint8_t               transmic_len;
127     // akf - aid for access, opcode for control
128     uint8_t               akf_aid_control;
129     // network pdu header
130     uint8_t               network_header[9];
131     // MESH_TRANSPORT_FLAG
132     uint16_t              flags;
133     // acknowledgement timer active
134     uint8_t               acknowledgement_timer_active;
135     // incomplete timer active
136     uint8_t               incomplete_timer_active;
137     // message complete
138     uint8_t               message_complete;
139     // seq_zero for segmented messages
140     uint16_t              seq_zero;
141     // pdu
142     uint16_t              len;
143     uint8_t               data[MESH_ACCESS_PAYLOAD_MAX];
144 } mesh_transport_pdu_t;
145 
146 typedef struct {
147     mesh_pdu_t pdu_header;
148 
149     // rx/tx: acknowledgement timer / segment transmission timer
150     btstack_timer_source_t acknowledgement_timer;
151     // rx: incomplete timer / tx: resend timer
152     btstack_timer_source_t incomplete_timer;
153     // block access
154     uint32_t              block_ack;
155     // meta data network layer
156     uint16_t              netkey_index;
157     // meta data transport layer
158     uint16_t              appkey_index;
159     // transmic size
160     uint8_t               transmic_len;
161     // akf - aid for access, opcode for control
162     uint8_t               akf_aid_control;
163     // network pdu header
164     uint8_t               network_header[9];
165     // MESH_TRANSPORT_FLAG
166     uint16_t              flags;
167     // acknowledgement timer active
168     uint8_t               acknowledgement_timer_active;
169     // incomplete timer active
170     uint8_t               incomplete_timer_active;
171     // message complete
172     uint8_t               message_complete;
173     // seq_zero for segmented messages
174     uint16_t              seq_zero;
175     // pdu segments
176     uint16_t              len;
177     btstack_linked_list_t segments;
178 } mesh_message_pdu_t;
179 
180 typedef enum {
181     MESH_KEY_REFRESH_NOT_ACTIVE = 0,
182     MESH_KEY_REFRESH_FIRST_PHASE,
183     MESH_KEY_REFRESH_SECOND_PHASE
184 } mesh_key_refresh_state_t;
185 
186 typedef enum {
187     MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE,
188     MESH_SECURE_NETWORK_BEACON_W4_AUTH_VALUE,
189     MESH_SECURE_NETWORK_BEACON_AUTH_VALUE,
190     MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV,
191     MESH_SECURE_NETWORK_BEACON_ADV_SENT,
192     MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT,
193     MESH_SECURE_NETWORK_BEACON_GATT_SENT,
194     MESH_SECURE_NETWORK_BEACON_W4_INTERVAL
195 } mesh_secure_network_beacon_state_t;
196 
197 typedef struct {
198     btstack_linked_item_t item;
199 
200     // netkey index
201     uint16_t              netkey_index;
202 
203     // current / old key
204     mesh_network_key_t * old_key;
205 
206     // new key (only set during key refresh)
207     mesh_network_key_t * new_key;
208 
209     // key refresh state
210     mesh_key_refresh_state_t key_refresh;
211 
212     // advertisement using node id active
213     uint8_t node_id_advertisement_running;
214 
215 
216     // advertisement using network id (used by proxy)
217     adv_bearer_connectable_advertisement_data_item_t advertisement_with_network_id;
218 
219     // advertising using node id (used by proxy)
220     adv_bearer_connectable_advertisement_data_item_t advertisement_with_node_id;
221 
222     // secure network beacons
223     mesh_secure_network_beacon_state_t beacon_state;
224     uint32_t                           beacon_interval_ms;
225     uint32_t                           beacon_observation_start_ms;
226     uint16_t                           beacon_observation_counter;
227 
228 } mesh_subnet_t;
229 
230 typedef struct {
231     btstack_linked_list_iterator_t it;
232 } mesh_subnet_iterator_t;
233 
234 /**
235  * @brief Init Mesh Network Layer
236  */
237 void mesh_network_init(void);
238 
239 /**
240  * @brief Set higher layer Network PDU handler
241  * @param packet_handler
242  */
243 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
244 
245 /**
246  * @brief Set higher layer Proxy PDU handler
247  * @param packet_handler
248  */
249 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
250 
251 /**
252  * @brief Mark packet as processed
253  * @param newtork_pdu received via call packet_handler
254  */
255 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu);
256 
257 /**
258  * @brief Send network_pdu after encryption
259  * @param network_pdu
260  */
261 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu);
262 
263 /*
264  * @brief Setup network pdu header
265  * @param netkey_index
266  * @param nid
267  * @param ctl
268  * @param ttl
269  * @param seq
270  * @param dst
271  * @param transport_pdu_data
272  * @param transport_pdu_len
273  */
274 void mesh_network_setup_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t nid, uint8_t ctl, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dst, const uint8_t * transport_pdu_data, uint8_t transport_pdu_len);
275 
276 /**
277  * Setup network pdu header without modifying len or payload
278  * @param network_pdu
279  * @param netkey_index
280  * @param nid
281  * @param ctl
282  * @param ttl
283  * @param seq
284  * @param src
285  * @param dest
286  */
287 void mesh_network_setup_pdu_header(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t nid, uint8_t ctl, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dest);
288 
289 /**
290  * @brief Validate network addresses
291  * @param ctl
292  * @param src
293  * @param dst
294  * @returns 1 if valid,
295  */
296 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst);
297 
298 /**
299  * @brief Check if Unicast address
300  * @param addr
301  * @returns 1 if unicast
302  */
303 int mesh_network_address_unicast(uint16_t addr);
304 
305 /**
306  * @brief Check if Unicast address
307  * @param addr
308  * @returns 1 if unicast
309  */
310 int mesh_network_address_group(uint16_t addr);
311 
312 /**
313  * @brief Check if All Proxies address
314  * @param addr
315  * @returns 1 if all proxies
316  */
317 int mesh_network_address_all_proxies(uint16_t addr);
318 
319 /**
320  * @brief Check if All Nodes address
321  * @param addr
322  * @returns 1 if all nodes
323  */
324 int mesh_network_address_all_nodes(uint16_t addr);
325 
326 /**
327  * @brief Check if All Friends address
328  * @param addr
329  * @returns 1 if all friends
330  */
331 int mesh_network_address_all_friends(uint16_t addr);
332 
333 /**
334  * @brief Check if All Relays address
335  * @param addr
336  * @returns 1 if all relays
337  */
338 int mesh_network_address_all_relays(uint16_t addr);
339 
340 
341 /**
342  * @brief Check if Virtual address
343  * @param addr
344  * @returns 1 if virtual
345  */
346 int mesh_network_address_virtual(uint16_t addr);
347 
348 
349 /**
350  * @brief Add subnet to list
351  * @param subnet
352  */
353 void mesh_subnet_add(mesh_subnet_t * subnet);
354 
355 /**
356  * @brief Remove subnet from list
357  * @param subnet
358  */
359 void mesh_subnet_remove(mesh_subnet_t * subnet);
360 
361 /**
362  * @brief Get subnet for netkey_index
363  * @param netkey_index
364  * @returns mesh_subnet_t or NULL
365  */
366 mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index);
367 
368 /**
369  * @brief Get number of stored subnets
370  * @returns count
371  */
372 int mesh_subnet_list_count(void);
373 
374 /**
375  * @brief Iterate over all subnets
376  * @param it
377  */
378 void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it);
379 
380 /**
381  * @brief Check if another subnet is available
382  * @param it
383  * @return
384  */
385 int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it);
386 
387 /**
388  * @brief Get next subnet
389  * @param it
390  * @return
391  */
392 mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it);
393 
394 /**
395  * @brief Setup subnet for given netkey index
396  */
397 void mesh_subnet_setup_for_netkey_index(uint16_t netkey_index);
398 
399 
400 /**
401  * @brief Get outgoing network key for subnet based on key refresh phase
402  */
403 mesh_network_key_t * mesh_subnet_get_outgoing_network_key(mesh_subnet_t * subnet);
404 
405 // buffer pool
406 mesh_network_pdu_t * mesh_network_pdu_get(void);
407 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu);
408 
409 // Mesh Network PDU Getter
410 uint16_t  mesh_network_control(mesh_network_pdu_t * network_pdu);
411 uint8_t   mesh_network_nid(mesh_network_pdu_t * network_pdu);
412 uint8_t   mesh_network_ttl(mesh_network_pdu_t * network_pdu);
413 uint32_t  mesh_network_seq(mesh_network_pdu_t * network_pdu);
414 uint16_t  mesh_network_src(mesh_network_pdu_t * network_pdu);
415 uint16_t  mesh_network_dst(mesh_network_pdu_t * network_pdu);
416 int       mesh_network_segmented(mesh_network_pdu_t * network_pdu);
417 uint8_t   mesh_network_control_opcode(mesh_network_pdu_t * network_pdu);
418 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu);
419 uint8_t   mesh_network_pdu_len(mesh_network_pdu_t * network_pdu);
420 
421 // Mesh Network PDU Setter
422 void mesh_network_pdu_set_seq(mesh_network_pdu_t * network_pdu, uint32_t seq);
423 
424 // Testing only
425 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags);
426 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len);
427 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback));
428 void mesh_network_dump(void);
429 void mesh_network_reset(void);
430 
431 #if defined __cplusplus
432 }
433 #endif
434 
435 #endif
436