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