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