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