xref: /btstack/src/mesh/mesh_network.c (revision c22a293dc0834397a433c50d5c4a2645e1ba060c)
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 #define __BTSTACK_FILE__ "mesh_network.c"
39 
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 
44 #include "btstack_debug.h"
45 #include "btstack_event.h"
46 #include "btstack_memory.h"
47 #include "btstack_util.h"
48 
49 #include "mesh/beacon.h"
50 #include "mesh/mesh_foundation.h"
51 #include "mesh/mesh_iv_index_seq_number.h"
52 #include "mesh/mesh_keys.h"
53 #include "mesh/mesh_node.h"
54 #include "mesh/provisioning.h"
55 #include "mesh/provisioning_device.h"
56 
57 #ifdef ENABLE_MESH_ADV_BEARER
58 #include "mesh/adv_bearer.h"
59 #endif
60 
61 #ifdef ENABLE_MESH_GATT_BEARER
62 #include "mesh/gatt_bearer.h"
63 #endif
64 
65 // configuration
66 #define MESH_NETWORK_CACHE_SIZE 2
67 
68 // debug config
69 // #define LOG_NETWORK
70 
71 // structs
72 
73 // globals
74 
75 static void (*mesh_network_higher_layer_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu);
76 static void (*mesh_network_proxy_message_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu);
77 
78 #ifdef ENABLE_MESH_GATT_BEARER
79 static hci_con_handle_t gatt_bearer_con_handle;
80 #endif
81 
82 // shared send/receive crypto
83 static int mesh_crypto_active;
84 
85 // crypto requests
86 static union {
87     btstack_crypto_ccm_t         ccm;
88     btstack_crypto_aes128_t      aes128;
89 } mesh_network_crypto_request;
90 
91 static const mesh_network_key_t *  current_network_key;
92 
93 // PECB calculation
94 static uint8_t encryption_block[16];
95 static uint8_t obfuscation_block[16];
96 
97 // Subnets
98 static btstack_linked_list_t subnets;
99 
100 // Network Nonce
101 static uint8_t network_nonce[13];
102 
103 // INCOMING //
104 
105 // unprocessed network pdu - added by mesh_network_pdus_received_message
106 static btstack_linked_list_t        network_pdus_received;
107 
108 // in validation
109 static mesh_network_pdu_t *         network_pdu_in_validation;
110 static mesh_network_key_iterator_t  validation_network_key_it;
111 
112 // OUTGOING //
113 
114 // Network PDUs queued by mesh_network_send
115 static btstack_linked_list_t network_pdus_queued;
116 
117 
118 // Network PDUs ready to send via GATT Bearer
119 static btstack_linked_list_t network_pdus_outgoing_gatt;
120 
121 #ifdef ENABLE_MESH_GATT_BEARER
122 static mesh_network_pdu_t * gatt_bearer_network_pdu;
123 #endif
124 
125 // Network PDUs ready to send via ADV Bearer
126 static btstack_linked_list_t network_pdus_outgoing_adv;
127 
128 #ifdef ENABLE_MESH_ADV_BEARER
129 static mesh_network_pdu_t * adv_bearer_network_pdu;
130 #endif
131 
132 
133 // mesh network cache - we use 32-bit 'hashes'
134 static uint32_t mesh_network_cache[MESH_NETWORK_CACHE_SIZE];
135 static int      mesh_network_cache_index;
136 
137 // prototypes
138 
139 static void mesh_network_run(void);
140 static void process_network_pdu_validate(mesh_network_pdu_t * network_pdu);
141 
142 // network caching
143 static uint32_t mesh_network_cache_hash(mesh_network_pdu_t * network_pdu){
144     // - The SEQ field is a 24-bit integer that when combined with the IV Index,
145     // shall be a unique value for each new Network PDU originated by this node (=> SRC)
146     // - IV updates only rarely
147     // => 16 bit SRC, 1 bit IVI, 15 bit SEQ
148     uint8_t  ivi = network_pdu->data[0] >> 7;
149     uint16_t seq = big_endian_read_16(network_pdu->data, 3);
150     uint16_t src = big_endian_read_16(network_pdu->data, 5);
151     return (src << 16) | (ivi << 15) | (seq & 0x7fff);
152 }
153 
154 static int mesh_network_cache_find(uint32_t hash){
155     int i;
156     for (i = 0; i < MESH_NETWORK_CACHE_SIZE; i++) {
157         if (mesh_network_cache[i] == hash) {
158             return 1;
159         }
160     }
161     return 0;
162 }
163 
164 static void mesh_network_cache_add(uint32_t hash){
165     mesh_network_cache[mesh_network_cache_index++] = hash;
166     if (mesh_network_cache_index >= MESH_NETWORK_CACHE_SIZE){
167         mesh_network_cache_index = 0;
168     }
169 }
170 
171 // common helper
172 int mesh_network_address_unicast(uint16_t addr){
173     return addr != MESH_ADDRESS_UNSASSIGNED && (addr < 0x8000);
174 }
175 
176 int mesh_network_address_virtual(uint16_t addr){
177     return (addr & 0xC000) == 0x8000;   // 0b10xx xxxx xxxx xxxx
178 }
179 
180 int mesh_network_address_group(uint16_t addr){
181     return (addr & 0xC000) == 0xC000;   // 0b11xx xxxx xxxx xxxx
182 }
183 
184 int mesh_network_address_all_proxies(uint16_t addr){
185     return addr == MESH_ADDRESS_ALL_PROXIES;
186 }
187 
188 int mesh_network_address_all_nodes(uint16_t addr){
189     return addr == MESH_ADDRESS_ALL_NODES;
190 }
191 
192 int mesh_network_address_all_friends(uint16_t addr){
193     return addr == MESH_ADDRESS_ALL_FRIENDS;
194 }
195 
196 int mesh_network_address_all_relays(uint16_t addr){
197     return addr == MESH_ADDRESS_ALL_RELAYS;
198 }
199 
200 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst){
201     // printf("CTL: %u\n", ctl);
202     // printf("SRC: %04x\n", src);
203     // printf("DST: %04x\n", dst);
204     if (src == 0){
205         // printf("SRC Unassigned Addr -> ignore\n");
206         return 0;
207     }
208     if ((src & 0xC000) == 0x8000){
209         // printf("SRC Virtual Addr -> ignore\n");
210         return 0;
211     }
212     if ((src & 0xC000) == 0xC000){
213         // printf("SRC Group Addr -> ignore\n");
214         return 0;
215     }
216     if (dst == 0){
217         // printf("DST Unassigned Addr -> ignore\n");
218         return 0;
219     }
220     if ( ((dst & 0xC000) == 0x8000) && (ctl == 1)){
221         // printf("DST Virtual Addr in CONTROL -> ignore\n");
222         return 0;
223     }
224     if ( (0xFF00 <= dst) && (dst <= 0xfffb) && (ctl == 0) ){
225         // printf("DST RFU Group Addr in MESSAGE -> ignore\n");
226         return 0;
227     }
228     // printf("SRC + DST Addr valid\n");
229     return 1;
230 }
231 
232 static void mesh_network_create_nonce(uint8_t * nonce, const mesh_network_pdu_t * pdu, uint32_t iv_index){
233     unsigned int pos = 0;
234     nonce[pos++] = 0x0;      // Network Nonce
235     memcpy(&nonce[pos], &pdu->data[1], 6);
236     pos += 6;
237     big_endian_store_16(nonce, pos, 0);
238     pos += 2;
239     big_endian_store_32(nonce, pos, iv_index);
240 }
241 
242 static void mesh_proxy_create_nonce(uint8_t * nonce, const mesh_network_pdu_t * pdu, uint32_t iv_index){
243     unsigned int pos = 0;
244     nonce[pos++] = 0x3;      // Proxy Nonce
245     nonce[pos++] = 0;
246     memcpy(&nonce[pos], &pdu->data[2], 5);
247     pos += 5;
248     big_endian_store_16(nonce, pos, 0);
249     pos += 2;
250     big_endian_store_32(nonce, pos, iv_index);
251 }
252 
253 // NID/IVI | obfuscated (CTL/TTL, SEQ (24), SRC (16) ), encrypted ( DST(16), TransportPDU), MIC(32 or 64)
254 
255 static void mesh_network_send_d(mesh_network_pdu_t * network_pdu){
256 
257 #ifdef LOG_NETWORK
258     printf("TX-D-NetworkPDU (%p): ", network_pdu);
259     printf_hexdump(network_pdu->data, network_pdu->len);
260 #endif
261 
262     // add to queue
263     btstack_linked_list_add_tail(&network_pdus_outgoing_gatt, (btstack_linked_item_t *) network_pdu);
264 
265     // go
266     mesh_network_run();
267 }
268 
269 // new
270 static void mesh_network_send_c(void *arg){
271     mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg;
272 
273     // obfuscate
274     unsigned int i;
275     for (i=0;i<6;i++){
276         network_pdu->data[1+i] ^= obfuscation_block[i];
277     }
278 
279 #ifdef LOG_NETWORK
280     printf("TX-C-NetworkPDU (%p): ", network_pdu);
281     printf_hexdump(network_pdu->data, network_pdu->len);
282 #endif
283 
284     // crypto done
285     mesh_crypto_active = 0;
286 
287     // done
288     (network_pdu->callback)(network_pdu);
289 }
290 
291 static void mesh_network_send_b(void *arg){
292     mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg;
293 
294     uint32_t iv_index = mesh_get_iv_index_for_tx();
295 
296     // store NetMIC
297     uint8_t net_mic[8];
298     btstack_crypto_ccm_get_authentication_value(&mesh_network_crypto_request.ccm, net_mic);
299 
300     // store MIC
301     uint8_t net_mic_len = network_pdu->data[1] & 0x80 ? 8 : 4;
302     memcpy(&network_pdu->data[network_pdu->len], net_mic, net_mic_len);
303     network_pdu->len += net_mic_len;
304 
305 #ifdef LOG_NETWORK
306     printf("TX-B-NetworkPDU (%p): ", network_pdu);
307     printf_hexdump(network_pdu->data, network_pdu->len);
308 #endif
309 
310     // calc PECB
311     memset(encryption_block, 0, 5);
312     big_endian_store_32(encryption_block, 5, iv_index);
313     memcpy(&encryption_block[9], &network_pdu->data[7], 7);
314     btstack_crypto_aes128_encrypt(&mesh_network_crypto_request.aes128, current_network_key->privacy_key, encryption_block, obfuscation_block, &mesh_network_send_c, network_pdu);
315 }
316 
317 static void mesh_network_send_a(mesh_network_pdu_t * network_pdu){
318 
319     mesh_crypto_active = 1;
320 
321     uint32_t iv_index = mesh_get_iv_index_for_tx();
322 
323     // lookup subnet by netkey_index
324     mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(network_pdu->netkey_index);
325     if (!subnet) {
326         mesh_crypto_active = 0;
327         // notify upper layer
328         (*mesh_network_higher_layer_handler)(MESH_NETWORK_PDU_SENT, network_pdu);
329         // run again
330         mesh_network_run();
331         return;
332     }
333 
334     // get network key to use for sending
335     current_network_key = mesh_subnet_get_outgoing_network_key(subnet);
336 
337 #ifdef LOG_NETWORK
338     printf("TX-A-NetworkPDU (%p): ", network_pdu);
339     printf_hexdump(network_pdu->data, network_pdu->len);
340 #endif
341 
342     // get network nonce
343     if (network_pdu->flags & MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION){
344         mesh_proxy_create_nonce(network_nonce, network_pdu, iv_index);
345 #ifdef LOG_NETWORK
346         printf("TX-ProxyNonce:  ");
347         printf_hexdump(network_nonce, 13);
348 #endif
349     } else {
350         mesh_network_create_nonce(network_nonce, network_pdu, iv_index);
351 #ifdef LOG_NETWORK
352         printf("TX-NetworkNonce:  ");
353         printf_hexdump(network_nonce, 13);
354 #endif
355     }
356 
357 #ifdef LOG_NETWORK
358    printf("TX-EncryptionKey: ");
359     printf_hexdump(current_network_key->encryption_key, 16);
360 #endif
361 
362     // start ccm
363     uint8_t cypher_len  = network_pdu->len - 7;
364     uint8_t net_mic_len = network_pdu->data[1] & 0x80 ? 8 : 4;
365     btstack_crypto_ccm_init(&mesh_network_crypto_request.ccm, current_network_key->encryption_key, network_nonce, cypher_len, 0, net_mic_len);
366     btstack_crypto_ccm_encrypt_block(&mesh_network_crypto_request.ccm, cypher_len, &network_pdu->data[7], &network_pdu->data[7], &mesh_network_send_b, network_pdu);
367 }
368 
369 #if defined(ENABLE_MESH_RELAY) || defined (ENABLE_MESH_PROXY_SERVER)
370 static void mesh_network_relay_message(mesh_network_pdu_t * network_pdu){
371 
372     uint8_t ctl_ttl     = network_pdu->data[1];
373     uint8_t ctl         = ctl_ttl & 0x80;
374     uint8_t ttl         = ctl_ttl & 0x7f;
375 
376 #ifdef LOG_NETWORK
377     printf("TX-Relay-NetworkPDU (%p): ", network_pdu);
378     printf_hexdump(network_pdu->data, network_pdu->len);
379 #endif
380 
381     // prepare pdu for resending
382     network_pdu->data[1] = (ctl << 7) | (ttl - 1);
383     network_pdu->flags |= MESH_NETWORK_PDU_FLAGS_RELAY;
384 
385     // queue up
386     network_pdu->callback = &mesh_network_send_d;
387     btstack_linked_list_add_tail(&network_pdus_queued, (btstack_linked_item_t *) network_pdu);
388 }
389 #endif
390 
391 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu){
392 
393 #if defined(ENABLE_MESH_RELAY) || defined (ENABLE_MESH_PROXY_SERVER)
394 
395     // check if address does not matches elements on our node and TTL >= 2
396     uint16_t src     = mesh_network_src(network_pdu);
397     uint8_t  ttl     = mesh_network_ttl(network_pdu);
398 
399     uint16_t mesh_network_primary_address = mesh_node_get_primary_element_address();
400 
401     if (((src < mesh_network_primary_address) || (src > (mesh_network_primary_address + mesh_node_element_count()))) && (ttl >= 2)){
402 
403         if ((network_pdu->flags & MESH_NETWORK_PDU_FLAGS_GATT_BEARER) == 0){
404 
405             // message received via ADV bearer are relayed:
406 
407 #ifdef ENABLE_MESH_RELAY
408             if (mesh_foundation_relay_get() != 0){
409                 // - to ADV bearer, if Relay supported and enabled
410                 mesh_network_relay_message(network_pdu);
411                 mesh_network_run();
412                 return;
413             }
414 #endif
415 
416 #ifdef ENABLE_MESH_PROXY_SERVER
417             if (mesh_foundation_gatt_proxy_get() != 0){
418                 // - to GATT bearer, if Proxy supported and enabled
419                 mesh_network_relay_message(network_pdu);
420                 mesh_network_run();
421                 return;
422             }
423 #endif
424 
425         } else {
426 
427             // messages received via GATT bearer are relayed:
428 
429 #ifdef ENABLE_MESH_PROXY_SERVER
430             if (mesh_foundation_gatt_proxy_get() != 0){
431                 // - to ADV bearer, if Proxy supported and enabled
432                 mesh_network_relay_message(network_pdu);
433                 mesh_network_run();
434                 return;
435             }
436 #endif
437 
438         }
439     }
440 #endif
441 
442     // otherwise, we're done
443     btstack_memory_mesh_network_pdu_free(network_pdu);
444 }
445 
446 static void process_network_pdu_done(void){
447     btstack_memory_mesh_network_pdu_free(network_pdu_in_validation);
448     network_pdu_in_validation = NULL;
449     mesh_crypto_active = 0;
450 
451     mesh_network_run();
452 }
453 
454 static void process_network_pdu_validate_d(void * arg){
455     mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg;
456 
457     uint8_t ctl_ttl     = network_pdu->data[1];
458     uint8_t ctl         = ctl_ttl >> 7;
459     uint8_t net_mic_len = (ctl_ttl & 0x80) ? 8 : 4;
460 
461     // store NetMIC
462     uint8_t net_mic[8];
463     btstack_crypto_ccm_get_authentication_value(&mesh_network_crypto_request.ccm, net_mic);
464 #ifdef LOG_NETWORK
465     printf("RX-NetMIC: ");
466     printf_hexdump(net_mic, net_mic_len);
467 #endif
468     // store in pdu
469     memcpy(&network_pdu->data[network_pdu->len-net_mic_len], net_mic, net_mic_len);
470 
471 #ifdef LOG_NETWORK
472     uint8_t cypher_len  = network_pdu->len - 9 - net_mic_len;
473     printf("RX-Decrypted DST/TransportPDU: ");
474     printf_hexdump(&network_pdu->data[7], 2 + cypher_len);
475 
476     printf("RX-Decrypted: ");
477     printf_hexdump(network_pdu->data, network_pdu->len);
478 #endif
479 
480     // validate network mic
481     if (memcmp(net_mic, &network_pdu_in_validation->data[network_pdu->len-net_mic_len], net_mic_len) != 0){
482         // fail
483         printf("RX-NetMIC mismatch, try next key\n");
484         process_network_pdu_validate(network_pdu);
485         return;
486     }
487 
488     // remove NetMIC from payload
489     network_pdu->len -= net_mic_len;
490 
491 #ifdef LOG_NETWORK
492     // match
493     printf("RX-NetMIC matches\n");
494     printf("RX-TTL: 0x%02x\n", network_pdu->data[1] & 0x7f);
495 #endif
496 
497     // set netkey_index
498     network_pdu->netkey_index = current_network_key->netkey_index;
499 
500     if (network_pdu->flags & MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION){
501 
502         // no additional checks for proxy messages
503         (*mesh_network_proxy_message_handler)(MESH_NETWORK_PDU_RECEIVED, network_pdu);
504 
505     } else {
506 
507         // validate src/dest addresses
508         uint16_t src = big_endian_read_16(network_pdu->data, 5);
509         uint16_t dst = big_endian_read_16(network_pdu->data, 7);
510         int valid = mesh_network_addresses_valid(ctl, src, dst);
511         if (!valid){
512             printf("RX Address invalid\n");
513             btstack_memory_mesh_network_pdu_free(network_pdu);
514             process_network_pdu_done();
515             return;
516         }
517 
518         // check cache
519         uint32_t hash = mesh_network_cache_hash(network_pdu);
520 #ifdef LOG_NETWORK
521         printf("RX-Hash: %08x\n", hash);
522 #endif
523         if (mesh_network_cache_find(hash)){
524             // found in cache, drop
525             printf("Found in cache -> drop packet\n");
526             btstack_memory_mesh_network_pdu_free(network_pdu);
527             process_network_pdu_done();
528             return;
529         }
530 
531         // store in network cache
532         mesh_network_cache_add(hash);
533 
534         // forward to lower transport layer. message is freed by call to mesh_network_message_processed_by_upper_layer
535         (*mesh_network_higher_layer_handler)(MESH_NETWORK_PDU_RECEIVED, network_pdu);
536     }
537 
538     // done
539     process_network_pdu_done();
540 }
541 
542 static uint32_t iv_index_for_pdu(const mesh_network_pdu_t * network_pdu){
543     // get IV Index and IVI
544     uint32_t iv_index = mesh_get_iv_index();
545     int ivi = network_pdu->data[0] >> 7;
546 
547     // if least significant bit differs, use previous IV Index
548     if ((iv_index & 1 ) ^ ivi){
549         iv_index--;
550 #ifdef LOG_NETWORK
551         printf("RX-IV: IVI indicates previous IV index, using 0x%08x\n", iv_index);
552 #endif
553     }
554     return iv_index;
555 }
556 
557 static void process_network_pdu_validate_b(void * arg){
558     mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg;
559 
560 #ifdef LOG_NETWORK
561     printf("RX-PECB: ");
562     printf_hexdump(obfuscation_block, 6);
563 #endif
564 
565     // de-obfuscate
566     unsigned int i;
567     for (i=0;i<6;i++){
568         network_pdu->data[1+i] = network_pdu_in_validation->data[1+i] ^ obfuscation_block[i];
569     }
570 
571     uint32_t iv_index = iv_index_for_pdu(network_pdu);
572 
573     if (network_pdu->flags & MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION){
574         // create network nonce
575         mesh_proxy_create_nonce(network_nonce, network_pdu, iv_index);
576 #ifdef LOG_NETWORK
577         printf("RX-Proxy Nonce: ");
578         printf_hexdump(network_nonce, 13);
579 #endif
580     } else {
581         // create network nonce
582         mesh_network_create_nonce(network_nonce, network_pdu, iv_index);
583 #ifdef LOG_NETWORK
584         printf("RX-Network Nonce: ");
585         printf_hexdump(network_nonce, 13);
586 #endif
587     }
588 
589     //
590     uint8_t ctl_ttl     = network_pdu->data[1];
591     uint8_t net_mic_len = (ctl_ttl & 0x80) ? 8 : 4;
592     uint8_t cypher_len  = network_pdu->len - 7 - net_mic_len;
593 
594 #ifdef LOG_NETWORK
595     printf("RX-Cyper len %u, mic len %u\n", cypher_len, net_mic_len);
596 
597     printf("RX-Encryption Key: ");
598     printf_hexdump(current_network_key->encryption_key, 16);
599 
600 #endif
601 
602     btstack_crypto_ccm_init(&mesh_network_crypto_request.ccm, current_network_key->encryption_key, network_nonce, cypher_len, 0, net_mic_len);
603     btstack_crypto_ccm_decrypt_block(&mesh_network_crypto_request.ccm, cypher_len, &network_pdu_in_validation->data[7], &network_pdu->data[7], &process_network_pdu_validate_d, network_pdu);
604 }
605 
606 static void process_network_pdu_validate(mesh_network_pdu_t * network_pdu){
607     if (!mesh_network_key_nid_iterator_has_more(&validation_network_key_it)){
608         printf("No valid network key found\n");
609         btstack_memory_mesh_network_pdu_free(network_pdu);
610         process_network_pdu_done();
611         return;
612     }
613 
614     current_network_key = mesh_network_key_nid_iterator_get_next(&validation_network_key_it);
615 
616     // calc PECB
617     uint32_t iv_index = iv_index_for_pdu(network_pdu);
618     memset(encryption_block, 0, 5);
619     big_endian_store_32(encryption_block, 5, iv_index);
620     memcpy(&encryption_block[9], &network_pdu_in_validation->data[7], 7);
621     btstack_crypto_aes128_encrypt(&mesh_network_crypto_request.aes128, current_network_key->privacy_key, encryption_block, obfuscation_block, &process_network_pdu_validate_b, network_pdu);
622 }
623 
624 
625 static void process_network_pdu(mesh_network_pdu_t * network_pdu){
626     //
627     uint8_t nid_ivi = network_pdu_in_validation->data[0];
628 
629     // setup pdu object
630     network_pdu->data[0] = nid_ivi;
631     network_pdu->len     = network_pdu_in_validation->len;
632     network_pdu->flags   = network_pdu_in_validation->flags;
633 
634     // init provisioning data iterator
635     uint8_t nid = nid_ivi & 0x7f;
636     // uint8_t iv_index = network_pdu_data[0] >> 7;
637     mesh_network_key_nid_iterator_init(&validation_network_key_it, nid);
638 
639     process_network_pdu_validate(network_pdu);
640 }
641 
642 // static void mesh_network_encrypt_and_obfuscate(mesh_network_pdu_t * network_pdu, void (*callback)(mesh_network_pdu_t * network_pdu)){
643 //     network_pdu->callback = callback;
644 // }
645 
646 static void mesh_network_run(void){
647     if (!btstack_linked_list_empty(&network_pdus_outgoing_gatt)){
648 
649 #ifdef ENABLE_MESH_GATT_BEARER
650         if (gatt_bearer_network_pdu == NULL){
651             // move to 'gatt bearer queue'
652             mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_outgoing_gatt);
653         // request to send via gatt if:
654         // proxy active and connected
655         // packet wasn't received via gatt bearer
656             int send_via_gatt = ((mesh_foundation_gatt_proxy_get() != 0) &&
657             (gatt_bearer_con_handle != HCI_CON_HANDLE_INVALID) &&
658                                  ((network_pdu->flags & MESH_NETWORK_PDU_FLAGS_GATT_BEARER) == 0));
659             if (send_via_gatt){
660                 gatt_bearer_network_pdu = network_pdu;
661                 gatt_bearer_request_can_send_now_for_network_pdu();
662             } else {
663                 btstack_linked_list_add_tail(&network_pdus_outgoing_adv, (btstack_linked_item_t *) network_pdu);
664             }
665         }
666 #else
667         // directly move to 'outgoing adv bearer queue'
668         mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_outgoing_gatt);
669         btstack_linked_list_add_tail(&network_pdus_outgoing_adv, (btstack_linked_item_t *) network_pdu);
670 #endif
671     }
672 
673     if (!btstack_linked_list_empty(&network_pdus_outgoing_adv)){
674 #ifdef ENABLE_MESH_ADV_BEARER
675             if (adv_bearer_network_pdu == NULL){
676             // move to 'adv bearer queue'
677             mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_outgoing_adv);
678                 adv_bearer_network_pdu = network_pdu;
679                 adv_bearer_request_can_send_now_for_network_pdu();
680             }
681 #else
682         // done
683         mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_outgoing_adv);
684         // directly notify upper layer
685         (*mesh_network_higher_layer_handler)(MESH_NETWORK_PDU_SENT, network_pdu);
686 #endif
687     }
688 
689     if (mesh_crypto_active) return;
690 
691     if (!btstack_linked_list_empty(&network_pdus_received)){
692         mesh_network_pdu_t * decode_pdu = mesh_network_pdu_get();
693         if (!decode_pdu) return;
694         // get encoded network pdu and start processing
695         mesh_crypto_active = 1;
696         network_pdu_in_validation = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_received);
697         process_network_pdu(decode_pdu);
698         return;
699     }
700 
701     if (!btstack_linked_list_empty(&network_pdus_queued)){
702         // get queued network pdu and start processing
703         mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_queued);
704         mesh_network_send_a(network_pdu);
705         return;
706     }
707 }
708 
709 #ifdef ENABLE_MESH_ADV_BEARER
710 static void mesh_adv_bearer_handle_network_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
711     UNUSED(channel);
712     mesh_network_pdu_t * network_pdu;
713     uint8_t  transmission_count;
714     uint16_t transmission_interval;
715     uint8_t  transmit_config;
716 
717     switch (packet_type){
718         case MESH_NETWORK_PACKET:
719             // check len. minimal transport PDU len = 1, 32 bit NetMIC -> 13 bytes
720             if (size < 13) break;
721 
722 #ifdef LOG_NETWORK
723             printf("received network pdu from adv (len %u): ", size);
724             printf_hexdump(packet, size);
725 #endif
726             mesh_network_received_message(packet, size, 0);
727             break;
728 
729         case HCI_EVENT_PACKET:
730             switch(packet[0]){
731                 case HCI_EVENT_MESH_META:
732                     switch(packet[2]){
733                         case MESH_SUBEVENT_CAN_SEND_NOW:
734                             if (adv_bearer_network_pdu == NULL) break;
735 
736                             // Get Transmission config depending on relay flag
737                             if (adv_bearer_network_pdu->flags & MESH_NETWORK_PDU_FLAGS_RELAY){
738                                 transmit_config = mesh_foundation_relay_get();
739                             } else {
740                                 transmit_config = mesh_foundation_network_transmit_get();
741                             }
742                             transmission_count     = (transmit_config & 0x07) + 1;
743                             transmission_interval = (transmit_config >> 3) * 10;
744 
745 #ifdef LOG_NETWORK
746                             printf("TX-E-NetworkPDU count %u, interval %u ms (%p): ", transmission_count, transmission_interval, adv_bearer_network_pdu);
747                             printf_hexdump(adv_bearer_network_pdu->data, adv_bearer_network_pdu->len);
748 #endif
749 
750                             adv_bearer_send_network_pdu(adv_bearer_network_pdu->data, adv_bearer_network_pdu->len, transmission_count, transmission_interval);
751                             network_pdu = adv_bearer_network_pdu;
752                             adv_bearer_network_pdu = NULL;
753 
754                             // notify upper layer
755                             (*mesh_network_higher_layer_handler)(MESH_NETWORK_PDU_SENT, network_pdu);
756 
757                             // check if more to send
758                             mesh_network_run();
759                             break;
760                         default:
761                             break;
762                     }
763                     break;
764                 default:
765                     break;
766             }
767             break;
768     }
769 }
770 #endif
771 
772 #ifdef ENABLE_MESH_GATT_BEARER
773 static void mesh_network_gatt_bearer_outgoing_complete(void){
774 
775     if (gatt_bearer_network_pdu == NULL) return;
776 
777 #ifdef ENABLE_MESH_ADV_BEARER
778     // forward to adv bearer
779     btstack_linked_list_add_tail(&network_pdus_outgoing_adv, (btstack_linked_item_t*) gatt_bearer_network_pdu);
780     gatt_bearer_network_pdu = NULL;
781 #endif
782 
783     mesh_network_run();
784     return;
785 }
786 
787 static void mesh_network_gatt_bearer_handle_network_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
788     UNUSED(channel);
789     switch (packet_type){
790         case MESH_PROXY_DATA_PACKET:
791             if (mesh_foundation_gatt_proxy_get() == 0) break;
792 #ifdef LOG_NETWORK
793             printf("received network pdu from gatt (len %u): ", size);
794             printf_hexdump(packet, size);
795 #endif
796             mesh_network_received_message(packet, size, MESH_NETWORK_PDU_FLAGS_GATT_BEARER);
797             break;
798         case HCI_EVENT_PACKET:
799             switch (hci_event_packet_get_type(packet)){
800                 case HCI_EVENT_MESH_META:
801                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
802                         case MESH_SUBEVENT_PROXY_CONNECTED:
803                             gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet);
804                             break;
805                         case MESH_SUBEVENT_PROXY_DISCONNECTED:
806                             gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID;
807                             mesh_network_gatt_bearer_outgoing_complete();
808                             break;
809                         case MESH_SUBEVENT_CAN_SEND_NOW:
810                             if (gatt_bearer_network_pdu == NULL) break;
811 #ifdef LOG_NETWORK
812                             printf("G-TX-E-NetworkPDU (%p): ", gatt_bearer_network_pdu);
813                             printf_hexdump(gatt_bearer_network_pdu->data, gatt_bearer_network_pdu->len);
814 #endif
815                             gatt_bearer_send_network_pdu(gatt_bearer_network_pdu->data, gatt_bearer_network_pdu->len);
816                             break;
817 
818                         case MESH_SUBEVENT_MESSAGE_SENT:
819                             mesh_network_gatt_bearer_outgoing_complete();
820                             break;
821                         default:
822                             break;
823                     }
824                     break;
825                 default:
826                     break;
827             }
828             break;
829         default:
830             break;
831     }
832 }
833 #endif
834 
835 #ifdef ENABLE_MESH_GATT_BEARER
836 static void mesh_netework_gatt_bearer_handle_proxy_configuration(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
837     UNUSED(channel);
838     switch (packet_type){
839         case MESH_PROXY_DATA_PACKET:
840             mesh_network_process_proxy_configuration_message(packet, size);
841             break;
842         case HCI_EVENT_PACKET:
843             switch (hci_event_packet_get_type(packet)){
844                 case HCI_EVENT_MESH_META:
845                     switch (hci_event_mesh_meta_get_subevent_code(packet)){
846                         case MESH_SUBEVENT_CAN_SEND_NOW:
847                             // forward to higher layer
848                             (*mesh_network_proxy_message_handler)(MESH_NETWORK_CAN_SEND_NOW, NULL);
849                             break;
850                         default:
851                             break;
852                     }
853                     break;
854                 default:
855                     break;
856             }
857             break;
858         default:
859             break;
860     }
861 }
862 #endif
863 
864 void mesh_network_init(void){
865 #ifdef ENABLE_MESH_ADV_BEARER
866     adv_bearer_register_for_network_pdu(&mesh_adv_bearer_handle_network_event);
867 #endif
868 #ifdef ENABLE_MESH_GATT_BEARER
869     gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID;
870     gatt_bearer_register_for_network_pdu(&mesh_network_gatt_bearer_handle_network_event);
871     gatt_bearer_register_for_mesh_proxy_configuration(&mesh_netework_gatt_bearer_handle_proxy_configuration);
872 #endif
873 }
874 
875 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)){
876     mesh_network_higher_layer_handler = packet_handler;
877 }
878 
879 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)){
880     mesh_network_proxy_message_handler = packet_handler;
881 }
882 
883 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags){
884     // verify len
885     if (pdu_len > 29) return;
886 
887     // allocate network_pdu
888     mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
889     if (!network_pdu) return;
890 
891     // store data
892     memcpy(network_pdu->data, pdu_data, pdu_len);
893     network_pdu->len = pdu_len;
894     network_pdu->flags = flags;
895 
896     // add to list and go
897     btstack_linked_list_add_tail(&network_pdus_received, (btstack_linked_item_t *) network_pdu);
898     mesh_network_run();
899 
900 }
901 
902 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len){
903     // verify len
904     if (pdu_len > 29) return;
905 
906     // allocate network_pdu
907     mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
908     if (!network_pdu) return;
909 
910     // store data
911     memcpy(network_pdu->data, pdu_data, pdu_len);
912     network_pdu->len = pdu_len;
913     network_pdu->flags = MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION; // Network PDU
914 
915     // add to list and go
916     btstack_linked_list_add_tail(&network_pdus_received, (btstack_linked_item_t *) network_pdu);
917     mesh_network_run();
918 }
919 
920 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu){
921 #ifdef LOG_NETWORK
922     printf("TX-NetworkPDU (%p):   ", network_pdu);
923     printf_hexdump(network_pdu->data, network_pdu->len);
924 #endif
925 
926     if (network_pdu->len > 29){
927         printf("too long, %u\n", network_pdu->len);
928         return;
929     }
930 
931     // setup callback
932     network_pdu->callback = &mesh_network_send_d;
933     network_pdu->flags    = 0;
934 
935     // queue up
936     btstack_linked_list_add_tail(&network_pdus_queued, (btstack_linked_item_t *) network_pdu);
937 
938     // go
939     mesh_network_run();
940 }
941 
942 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu, void (* callback)(mesh_network_pdu_t * callback)){
943     printf("ProxyPDU(unencrypted): ");
944     printf_hexdump(network_pdu->data, network_pdu->len);
945 
946     // setup callback
947     network_pdu->callback = callback;
948     network_pdu->flags    = MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION;
949 
950     // queue up
951     btstack_linked_list_add_tail(&network_pdus_queued, (btstack_linked_item_t *) network_pdu);
952 
953     // go
954     mesh_network_run();
955 }
956 
957 /*
958  * @brief Setup network pdu header
959  * @param netkey_index
960  * @param ctl
961  * @param ttl
962  * @param seq
963  * @param dest
964  */
965 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 dest, const uint8_t * transport_pdu_data, uint8_t transport_pdu_len){
966     memset(network_pdu, 0, sizeof(mesh_network_pdu_t));
967     // set netkey_index
968     network_pdu->netkey_index = netkey_index;
969     // setup header
970     network_pdu->data[network_pdu->len++] = (mesh_get_iv_index_for_tx() << 7) |  nid;
971     uint8_t ctl_ttl = (ctl << 7) | (ttl & 0x7f);
972     network_pdu->data[network_pdu->len++] = ctl_ttl;
973     big_endian_store_24(network_pdu->data, 2, seq);
974     network_pdu->len += 3;
975     big_endian_store_16(network_pdu->data, network_pdu->len, src);
976     network_pdu->len += 2;
977     big_endian_store_16(network_pdu->data, network_pdu->len, dest);
978     network_pdu->len += 2;
979     memcpy(&network_pdu->data[network_pdu->len], transport_pdu_data, transport_pdu_len);
980     network_pdu->len += transport_pdu_len;
981 }
982 
983 /*
984  * @brief Setup network pdu header
985  * @param netkey_index
986  * @param ctl
987  * @param ttl
988  * @param seq
989  * @param dest
990  */
991 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){
992     // set netkey_index
993     network_pdu->netkey_index = netkey_index;
994     // setup header
995     network_pdu->data[0] = (mesh_get_iv_index_for_tx() << 7) |  nid;
996     uint8_t ctl_ttl = (ctl << 7) | (ttl & 0x7f);
997     network_pdu->data[1] = ctl_ttl;
998     big_endian_store_24(network_pdu->data, 2, seq);
999     big_endian_store_16(network_pdu->data, 5, src);
1000     big_endian_store_16(network_pdu->data, 7, dest);
1001 }
1002 
1003 // Network PDU Getter
1004 uint8_t  mesh_network_nid(mesh_network_pdu_t * network_pdu){
1005     return network_pdu->data[0] & 0x7f;
1006 }
1007 uint16_t mesh_network_control(mesh_network_pdu_t * network_pdu){
1008     return network_pdu->data[1] & 0x80;
1009 }
1010 uint8_t mesh_network_ttl(mesh_network_pdu_t * network_pdu){
1011     return network_pdu->data[1] & 0x7f;
1012 }
1013 uint32_t mesh_network_seq(mesh_network_pdu_t * network_pdu){
1014     return big_endian_read_24(network_pdu->data, 2);
1015 }
1016 uint16_t mesh_network_src(mesh_network_pdu_t * network_pdu){
1017     return big_endian_read_16(network_pdu->data, 5);
1018 }
1019 uint16_t mesh_network_dst(mesh_network_pdu_t * network_pdu){
1020     return big_endian_read_16(network_pdu->data, 7);
1021 }
1022 int mesh_network_segmented(mesh_network_pdu_t * network_pdu){
1023     return network_pdu->data[9] & 0x80;
1024 }
1025 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu){
1026     return &network_pdu->data[9];
1027 }
1028 uint8_t   mesh_network_pdu_len(mesh_network_pdu_t * network_pdu){
1029     return network_pdu->len - 9;
1030 }
1031 
1032 static void mesh_network_dump_network_pdu(mesh_network_pdu_t * network_pdu){
1033     if (network_pdu){
1034         printf("- %p: ", network_pdu); printf_hexdump(network_pdu->data, network_pdu->len);
1035     }
1036 }
1037 static void mesh_network_dump_network_pdus(const char * name, btstack_linked_list_t * list){
1038     printf("List: %s:\n", name);
1039     btstack_linked_list_iterator_t it;
1040     btstack_linked_list_iterator_init(&it, list);
1041     while (btstack_linked_list_iterator_has_next(&it)){
1042         mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t*) btstack_linked_list_iterator_next(&it);
1043         mesh_network_dump_network_pdu(network_pdu);
1044     }
1045 }
1046 static void mesh_network_reset_network_pdus(btstack_linked_list_t * list){
1047     while (!btstack_linked_list_empty(list)){
1048         mesh_network_pdu_t * pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(list);
1049         btstack_memory_mesh_network_pdu_free(pdu);
1050     }
1051 }
1052 void mesh_network_dump(void){
1053     mesh_network_dump_network_pdus("network_pdus_received", &network_pdus_received);
1054     mesh_network_dump_network_pdus("network_pdus_queued", &network_pdus_queued);
1055     mesh_network_dump_network_pdus("network_pdus_outgoing_gatt", &network_pdus_outgoing_gatt);
1056     mesh_network_dump_network_pdus("network_pdus_outgoing_adv", &network_pdus_outgoing_adv);
1057     printf("network_pdu_in_validation: \n");
1058     mesh_network_dump_network_pdu(network_pdu_in_validation);
1059 }
1060 void mesh_network_reset(void){
1061     mesh_network_reset_network_pdus(&network_pdus_received);
1062     mesh_network_reset_network_pdus(&network_pdus_queued);
1063     mesh_network_reset_network_pdus(&network_pdus_outgoing_gatt);
1064     mesh_network_reset_network_pdus(&network_pdus_outgoing_adv);
1065 }
1066 
1067 // buffer pool
1068 mesh_network_pdu_t * mesh_network_pdu_get(void){
1069     mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get();
1070     if (network_pdu) {
1071         memset(network_pdu, 0, sizeof(mesh_network_pdu_t));
1072         network_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_NETWORK;
1073     }
1074     return network_pdu;
1075 }
1076 
1077 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu){
1078     btstack_memory_mesh_network_pdu_free(network_pdu);
1079 }
1080 
1081 // Mesh Subnet Management
1082 
1083 void mesh_subnet_add(mesh_subnet_t * subnet){
1084     btstack_linked_list_add_tail(&subnets, (btstack_linked_item_t *) subnet);
1085 }
1086 
1087 void mesh_subnet_remove(mesh_subnet_t * subnet){
1088     btstack_linked_list_remove(&subnets, (btstack_linked_item_t *) subnet);
1089 }
1090 
1091 mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index){
1092     btstack_linked_list_iterator_t it;
1093     btstack_linked_list_iterator_init(&it, &subnets);
1094     while (btstack_linked_list_iterator_has_next(&it)){
1095         mesh_subnet_t * item = (mesh_subnet_t *) btstack_linked_list_iterator_next(&it);
1096         if (item->netkey_index == netkey_index) return item;
1097     }
1098     return NULL;
1099 }
1100 
1101 int mesh_subnet_list_count(void){
1102     return btstack_linked_list_count(&subnets);
1103 }
1104 
1105 // mesh network key iterator over all keys
1106 void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it){
1107     btstack_linked_list_iterator_init(&it->it, &subnets);
1108 }
1109 
1110 int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it){
1111     return btstack_linked_list_iterator_has_next(&it->it);
1112 }
1113 
1114 mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it){
1115     return (mesh_subnet_t *) btstack_linked_list_iterator_next(&it->it);
1116 }
1117 
1118 mesh_network_key_t * mesh_subnet_get_outgoing_network_key(mesh_subnet_t * subnet){
1119     switch (subnet->key_refresh){
1120         case MESH_KEY_REFRESH_SECOND_PHASE:
1121             return subnet->new_key;
1122         case MESH_KEY_REFRESH_NOT_ACTIVE:
1123         case MESH_KEY_REFRESH_FIRST_PHASE:
1124         default:
1125             return subnet->old_key;
1126     }
1127 }
1128 
1129 /**
1130  * @brief Setup subnet for given netkey index
1131  */
1132 void mesh_subnet_setup_for_netkey_index(uint16_t netkey_index){
1133     mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index);
1134     if (subnet != NULL) return;
1135 
1136     // find old / new keys
1137     mesh_network_key_t * old_key = NULL;
1138     mesh_network_key_t * new_key = NULL;
1139     mesh_network_key_iterator_t it;
1140     mesh_network_key_iterator_init(&it);
1141     while (mesh_network_key_iterator_has_more(&it)){
1142         mesh_network_key_t * network_key = mesh_network_key_iterator_get_next(&it);
1143         if (network_key->netkey_index != netkey_index) continue;
1144         if (old_key == NULL){
1145             old_key = network_key;
1146             continue;
1147         }
1148         // assign current key depending on key version
1149         if (((int8_t) (network_key->version - new_key->version)) > 0) {
1150             new_key = network_key;
1151         } else {
1152             new_key = old_key;
1153             old_key = network_key;
1154         }
1155     }
1156 
1157     // create subnet for netkey index
1158     subnet = btstack_memory_mesh_subnet_get();
1159     if (subnet == NULL) return;
1160     subnet->netkey_index = netkey_index;
1161     mesh_subnet_add(subnet);
1162 
1163     // set keys
1164     subnet->old_key = old_key;
1165     subnet->new_key = new_key;
1166 
1167     // key refresh
1168     if (new_key == NULL){
1169         // single key -> key refresh not active
1170         subnet->key_refresh = MESH_KEY_REFRESH_NOT_ACTIVE;
1171     }
1172     else {
1173         // two keys -> at least phase 1
1174         subnet->key_refresh = MESH_KEY_REFRESH_FIRST_PHASE;
1175     }
1176 }
1177