xref: /btstack/src/mesh/mesh_network.h (revision 8661c58f369daa8ff811bb10b8e1a3e47e37be40)
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 #define MESH_CONTROL_PAYLOAD_MAX     256
56 
57 #define MESH_ADDRESS_UNSASSIGNED     0x0000u
58 #define MESH_ADDRESS_ALL_PROXIES     0xFFFCu
59 #define MESH_ADDRESS_ALL_FRIENDS     0xFFFDu
60 #define MESH_ADDRESS_ALL_RELAYS      0xFFFEu
61 #define MESH_ADDRESS_ALL_NODES       0xFFFFu
62 
63 typedef enum {
64     MESH_NETWORK_PDU_RECEIVED,
65     MESH_NETWORK_PDU_SENT,
66     MESH_NETWORK_PDU_ENCRYPTED,
67     MESH_NETWORK_CAN_SEND_NOW,
68 } mesh_network_callback_type_t;
69 
70 typedef enum {
71     MESH_PDU_TYPE_INVALID,
72     MESH_PDU_TYPE_NETWORK,
73     MESH_PDU_TYPE_SEGMENTED,
74     MESH_PDU_TYPE_UNSEGMENTED,
75     MESH_PDU_TYPE_ACCESS,
76     MESH_PDU_TYPE_CONTROL,
77     MESH_PDU_TYPE_UPPER_SEGMENTED_ACCESS,
78     MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS,
79     MESH_PDU_TYPE_UPPER_SEGMENTED_CONTROL,
80     MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL,
81 } mesh_pdu_type_t;
82 
83 typedef struct mesh_pdu {
84     // allow for linked lists
85     btstack_linked_item_t item;
86     // type
87     mesh_pdu_type_t pdu_type;
88 
89 } mesh_pdu_t;
90 
91 //
92 #define MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION 1
93 #define MESH_NETWORK_PDU_FLAGS_GATT_BEARER         2
94 #define MESH_NETWORK_PDU_FLAGS_RELAY               4
95 
96 typedef struct mesh_network_pdu {
97     mesh_pdu_t pdu_header;
98 
99     // meta data network layer
100     uint16_t              netkey_index;
101     // MESH_NETWORK_PDU_FLAGS
102     uint16_t              flags;
103 
104     // pdu
105     uint16_t              len;
106     uint8_t               data[MESH_NETWORK_PAYLOAD_MAX];
107 } mesh_network_pdu_t;
108 
109 #define MESH_TRANSPORT_FLAG_SEQ_RESERVED      1
110 #define MESH_TRANSPORT_FLAG_CONTROL           2
111 #define MESH_TRANSPORT_FLAG_TRANSMIC_64       4
112 #define MESH_TRANSPORT_FLAG_ACK_TIMER         8
113 #define MESH_TRANSPORT_FLAG_INCOMPLETE_TIMER 16
114 
115 typedef struct {
116     mesh_pdu_t pdu_header;
117     // network header
118     uint8_t               ivi_nid;
119     uint8_t               ctl_ttl;
120     uint16_t              src;
121     uint16_t              dst;
122     uint32_t              seq;
123 
124     // incoming: acknowledgement timer / outgoing: segment transmission timer
125     btstack_timer_source_t acknowledgement_timer;
126     // incoming: incomplete timer / outgoing: not used
127     btstack_timer_source_t incomplete_timer;
128     // block access
129     uint32_t              block_ack;
130     // meta data network layer
131     uint16_t              netkey_index;
132     // akf - aid for access, opcode for control
133     uint8_t               akf_aid_control;
134     // MESH_TRANSPORT_FLAG
135     uint16_t              flags;
136     // retry count
137     uint8_t               retry_count;
138     // pdu segments
139     uint16_t              len;
140     btstack_linked_list_t segments;
141 } mesh_segmented_pdu_t;
142 
143 typedef struct {
144     // generic pdu header
145     mesh_pdu_t            pdu_header;
146     // network header
147     uint8_t               ivi_nid;
148     uint8_t               ctl_ttl;
149     uint16_t              src;
150     uint16_t              dst;
151     uint32_t              seq;
152     // meta data network layer
153     uint16_t              netkey_index;
154     // meta data transport layer
155     uint16_t              appkey_index;
156     // akf - aid for access, opcode for control
157     uint8_t               akf_aid_control;
158     // MESH_TRANSPORT_FLAG
159     uint16_t              flags;
160     // payload
161     uint16_t              len;
162     uint8_t               data[MESH_ACCESS_PAYLOAD_MAX];
163 
164 } mesh_access_pdu_t;
165 
166 // for unsegmented + segmented access + segmented control pdus
167 typedef struct {
168     // generic pdu header
169     mesh_pdu_t            pdu_header;
170     // network header
171     uint8_t               ivi_nid;
172     uint8_t               ctl_ttl;
173     uint16_t              src;
174     uint16_t              dst;
175     uint32_t              seq;
176     // meta data network layer
177     uint16_t              netkey_index;
178     // meta data transport layer
179     uint16_t              appkey_index;
180     // akf - aid for access, opcode for control
181     uint8_t               akf_aid_control;
182     // MESH_TRANSPORT_FLAG
183     uint16_t              flags;
184     // payload, single segmented or list of them
185     uint16_t              len;
186     btstack_linked_list_t segments;
187 
188     // access acknowledged message
189     uint16_t retransmit_count;
190     uint32_t retransmit_timeout_ms;
191     uint32_t ack_opcode;
192 
193     // associated lower transport pdu
194     mesh_pdu_t *          lower_pdu;
195 } mesh_upper_transport_pdu_t;
196 
197 typedef struct {
198     // generic pdu header
199     mesh_pdu_t            pdu_header;
200     // network header
201     uint8_t               ivi_nid;
202     uint8_t               ctl_ttl;
203     uint16_t              src;
204     uint16_t              dst;
205     uint32_t              seq;
206     // meta data network layer
207     uint16_t              netkey_index;
208     // akf - aid for access, opcode for control
209     uint8_t               akf_aid_control;
210     // MESH_TRANSPORT_FLAG
211     uint16_t              flags;
212     // payload
213     uint16_t              len;
214     uint8_t               data[MESH_CONTROL_PAYLOAD_MAX];
215 } mesh_control_pdu_t;
216 
217 typedef enum {
218     MESH_KEY_REFRESH_NOT_ACTIVE = 0,
219     MESH_KEY_REFRESH_FIRST_PHASE,
220     MESH_KEY_REFRESH_SECOND_PHASE
221 } mesh_key_refresh_state_t;
222 
223 typedef enum {
224     MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE,
225     MESH_SECURE_NETWORK_BEACON_W4_AUTH_VALUE,
226     MESH_SECURE_NETWORK_BEACON_AUTH_VALUE,
227     MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV,
228     MESH_SECURE_NETWORK_BEACON_ADV_SENT,
229     MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT,
230     MESH_SECURE_NETWORK_BEACON_GATT_SENT,
231     MESH_SECURE_NETWORK_BEACON_W4_INTERVAL
232 } mesh_secure_network_beacon_state_t;
233 
234 typedef struct {
235     btstack_linked_item_t item;
236 
237     // netkey index
238     uint16_t              netkey_index;
239 
240     // current / old key
241     mesh_network_key_t * old_key;
242 
243     // new key (only set during key refresh)
244     mesh_network_key_t * new_key;
245 
246     // key refresh state
247     mesh_key_refresh_state_t key_refresh;
248 
249     // advertisement using node id active
250     uint8_t node_id_advertisement_running;
251 
252 
253     // advertisement using network id (used by proxy)
254     adv_bearer_connectable_advertisement_data_item_t advertisement_with_network_id;
255 
256     // advertising using node id (used by proxy)
257     adv_bearer_connectable_advertisement_data_item_t advertisement_with_node_id;
258 
259     // secure network beacons
260     mesh_secure_network_beacon_state_t beacon_state;
261     uint32_t                           beacon_interval_ms;
262     uint32_t                           beacon_observation_start_ms;
263     uint16_t                           beacon_observation_counter;
264 
265 } mesh_subnet_t;
266 
267 typedef struct {
268     btstack_linked_list_iterator_t it;
269 } mesh_subnet_iterator_t;
270 
271 /**
272  * @brief Init Mesh Network Layer
273  */
274 void mesh_network_init(void);
275 
276 /**
277  * @brief Set higher layer Network PDU handler
278  * @param packet_handler
279  */
280 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
281 
282 /**
283  * @brief Set higher layer Proxy PDU handler
284  * @param packet_handler
285  */
286 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu));
287 
288 /**
289  * @brief Mark packet as processed
290  * @param newtork_pdu received via call packet_handler
291  */
292 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu);
293 
294 /**
295  * @brief Send network_pdu after encryption
296  * @param network_pdu
297  */
298 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu);
299 
300 /*
301  * @brief Setup network pdu header
302  * @param netkey_index
303  * @param nid
304  * @param ctl
305  * @param ttl
306  * @param seq
307  * @param dst
308  * @param transport_pdu_data
309  * @param transport_pdu_len
310  */
311 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);
312 
313 /**
314  * Setup network pdu header without modifying len or payload
315  * @param network_pdu
316  * @param netkey_index
317  * @param nid
318  * @param ctl
319  * @param ttl
320  * @param seq
321  * @param src
322  * @param dest
323  */
324 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);
325 
326 /**
327  * @brief Validate network addresses
328  * @param ctl
329  * @param src
330  * @param dst
331  * @returns 1 if valid,
332  */
333 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst);
334 
335 /**
336  * @brief Check if Unicast address
337  * @param addr
338  * @returns 1 if unicast
339  */
340 int mesh_network_address_unicast(uint16_t addr);
341 
342 /**
343  * @brief Check if Unicast address
344  * @param addr
345  * @returns 1 if unicast
346  */
347 int mesh_network_address_group(uint16_t addr);
348 
349 /**
350  * @brief Check if All Proxies address
351  * @param addr
352  * @returns 1 if all proxies
353  */
354 int mesh_network_address_all_proxies(uint16_t addr);
355 
356 /**
357  * @brief Check if All Nodes address
358  * @param addr
359  * @returns 1 if all nodes
360  */
361 int mesh_network_address_all_nodes(uint16_t addr);
362 
363 /**
364  * @brief Check if All Friends address
365  * @param addr
366  * @returns 1 if all friends
367  */
368 int mesh_network_address_all_friends(uint16_t addr);
369 
370 /**
371  * @brief Check if All Relays address
372  * @param addr
373  * @returns 1 if all relays
374  */
375 int mesh_network_address_all_relays(uint16_t addr);
376 
377 
378 /**
379  * @brief Check if Virtual address
380  * @param addr
381  * @returns 1 if virtual
382  */
383 int mesh_network_address_virtual(uint16_t addr);
384 
385 
386 /**
387  * @brief Add subnet to list
388  * @param subnet
389  */
390 void mesh_subnet_add(mesh_subnet_t * subnet);
391 
392 /**
393  * @brief Remove subnet from list
394  * @param subnet
395  */
396 void mesh_subnet_remove(mesh_subnet_t * subnet);
397 
398 /**
399  * @brief Get subnet for netkey_index
400  * @param netkey_index
401  * @returns mesh_subnet_t or NULL
402  */
403 mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index);
404 
405 /**
406  * @brief Get number of stored subnets
407  * @returns count
408  */
409 int mesh_subnet_list_count(void);
410 
411 /**
412  * @brief Iterate over all subnets
413  * @param it
414  */
415 void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it);
416 
417 /**
418  * @brief Check if another subnet is available
419  * @param it
420  * @return
421  */
422 int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it);
423 
424 /**
425  * @brief Get next subnet
426  * @param it
427  * @return
428  */
429 mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it);
430 
431 /**
432  * @brief Setup subnet for given netkey index
433  */
434 void mesh_subnet_setup_for_netkey_index(uint16_t netkey_index);
435 
436 
437 /**
438  * @brief Get outgoing network key for subnet based on key refresh phase
439  */
440 mesh_network_key_t * mesh_subnet_get_outgoing_network_key(mesh_subnet_t * subnet);
441 
442 // buffer pool
443 mesh_network_pdu_t * mesh_network_pdu_get(void);
444 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu);
445 
446 // Mesh Network PDU Getter
447 uint16_t  mesh_network_control(mesh_network_pdu_t * network_pdu);
448 uint8_t   mesh_network_nid(mesh_network_pdu_t * network_pdu);
449 uint8_t   mesh_network_ttl(mesh_network_pdu_t * network_pdu);
450 uint32_t  mesh_network_seq(mesh_network_pdu_t * network_pdu);
451 uint16_t  mesh_network_src(mesh_network_pdu_t * network_pdu);
452 uint16_t  mesh_network_dst(mesh_network_pdu_t * network_pdu);
453 int       mesh_network_segmented(mesh_network_pdu_t * network_pdu);
454 uint8_t   mesh_network_control_opcode(mesh_network_pdu_t * network_pdu);
455 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu);
456 uint8_t   mesh_network_pdu_len(mesh_network_pdu_t * network_pdu);
457 
458 // Mesh Network PDU Setter
459 void mesh_network_pdu_set_seq(mesh_network_pdu_t * network_pdu, uint32_t seq);
460 
461 // Testing only
462 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags);
463 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len);
464 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu);
465 void mesh_network_dump(void);
466 void mesh_network_reset(void);
467 
468 #if defined __cplusplus
469 }
470 #endif
471 
472 #endif
473