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