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