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