xref: /btstack/src/mesh/mesh_upper_transport.c (revision 6259b5df56598ec9aea6e413ea69083f1b2cbdbd)
1 /*
2  * Copyright (C) 2014 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_upper_transport.c"
39 
40 #include "mesh/mesh_upper_transport.h"
41 
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include "btstack_util.h"
47 #include "btstack_memory.h"
48 #include "btstack_debug.h"
49 
50 #include "mesh/beacon.h"
51 #include "mesh/mesh_iv_index_seq_number.h"
52 #include "mesh/mesh_keys.h"
53 #include "mesh/mesh_lower_transport.h"
54 #include "mesh/mesh_peer.h"
55 #include "mesh/mesh_virtual_addresses.h"
56 
57 static void (*higher_layer_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu);
58 
59 static void mesh_print_hex(const char * name, const uint8_t * data, uint16_t len){
60     printf("%-20s ", name);
61     printf_hexdump(data, len);
62 }
63 // static void mesh_print_x(const char * name, uint32_t value){
64 //     printf("%20s: 0x%x", name, (int) value);
65 // }
66 
67 
68 // combined key x address iterator for upper transport decryption
69 
70 typedef struct {
71     // state
72     mesh_transport_key_iterator_t  key_it;
73     mesh_virtual_address_iterator_t address_it;
74     // elements
75     const mesh_transport_key_t *   key;
76     const mesh_virtual_address_t * address;
77     // address - might be virtual
78     uint16_t dst;
79     // key info
80 } mesh_transport_key_and_virtual_address_iterator_t;
81 
82 static void mesh_transport_key_and_virtual_address_iterator_init(mesh_transport_key_and_virtual_address_iterator_t *it,
83                                                                  uint16_t dst, uint16_t netkey_index, uint8_t akf,
84                                                                  uint8_t aid) {
85     printf("KEY_INIT: dst %04x, akf %x, aid %x\n", dst, akf, aid);
86     // config
87     it->dst   = dst;
88     // init elements
89     it->key     = NULL;
90     it->address = NULL;
91     // init element iterators
92     mesh_transport_key_aid_iterator_init(&it->key_it, netkey_index, akf, aid);
93     // init address iterator
94     if (mesh_network_address_virtual(it->dst)){
95         mesh_virtual_address_iterator_init(&it->address_it, dst);
96         // get first key
97         if (mesh_transport_key_aid_iterator_has_more(&it->key_it)) {
98             it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it);
99         }
100     }
101 }
102 
103 // cartesian product: keys x addressses
104 static int mesh_transport_key_and_virtual_address_iterator_has_more(mesh_transport_key_and_virtual_address_iterator_t * it){
105     if (mesh_network_address_virtual(it->dst)) {
106         // find next valid entry
107         while (1){
108             if (mesh_virtual_address_iterator_has_more(&it->address_it)) return 1;
109             if (!mesh_transport_key_aid_iterator_has_more(&it->key_it)) return 0;
110             // get next key
111             it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it);
112             mesh_virtual_address_iterator_init(&it->address_it, it->dst);
113         }
114     } else {
115         return mesh_transport_key_aid_iterator_has_more(&it->key_it);
116     }
117 }
118 
119 static void mesh_transport_key_and_virtual_address_iterator_next(mesh_transport_key_and_virtual_address_iterator_t * it){
120     if (mesh_network_address_virtual(it->dst)) {
121         it->address = mesh_virtual_address_iterator_get_next(&it->address_it);
122     } else {
123         it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it);
124     }
125 }
126 
127 // UPPER TRANSPORT
128 
129 // stub lower transport
130 
131 static void mesh_upper_transport_validate_unsegmented_message(void);
132 static void mesh_upper_transport_validate_segmented_message(void);
133 
134 static void mesh_upper_transport_run(void);
135 
136 static int crypto_active;
137 static mesh_network_pdu_t   * incoming_network_pdu_raw;
138 static mesh_network_pdu_t   * incoming_network_pdu_decoded;
139 static mesh_transport_pdu_t * incoming_transport_pdu_raw;
140 static mesh_transport_pdu_t * incoming_transport_pdu_decoded;
141 static uint8_t application_nonce[13];
142 static btstack_crypto_ccm_t ccm;
143 static mesh_transport_key_and_virtual_address_iterator_t mesh_transport_key_it;
144 
145 // upper transport callbacks - in access layer
146 static void (*mesh_access_message_handler)(mesh_pdu_t * pdu);
147 static void (*mesh_control_message_handler)(mesh_pdu_t * pdu);
148 
149 // incoming unsegmented (network) and segmented (transport) control and access messages
150 static btstack_linked_list_t upper_transport_incoming;
151 
152 // outgoing unsegmented (network) and segmented (uppert_transport_outgoing) control and access messages
153 static btstack_linked_list_t upper_transport_outgoing;
154 
155 static void mesh_upper_transport_dump_pdus(const char *name, btstack_linked_list_t *list){
156     printf("List: %s:\n", name);
157     btstack_linked_list_iterator_t it;
158     btstack_linked_list_iterator_init(&it, list);
159     while (btstack_linked_list_iterator_has_next(&it)){
160         mesh_pdu_t * pdu = (mesh_pdu_t*) btstack_linked_list_iterator_next(&it);
161         printf("- %p\n", pdu);
162         // printf_hexdump( mesh_pdu_data(pdu), mesh_pdu_len(pdu));
163     }
164 }
165 
166 static void mesh_upper_transport_reset_pdus(btstack_linked_list_t *list){
167     while (!btstack_linked_list_empty(list)){
168         mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_pop(list);
169         switch (pdu->pdu_type){
170             case MESH_PDU_TYPE_NETWORK:
171                 btstack_memory_mesh_network_pdu_free((mesh_network_pdu_t *) pdu);
172                 break;
173             case MESH_PDU_TYPE_TRANSPORT:
174                 btstack_memory_mesh_transport_pdu_free((mesh_transport_pdu_t *) pdu);
175                 break;
176             default:
177                 break;
178         }
179     }
180 }
181 
182 void mesh_upper_transport_dump(void){
183     printf("incoming_network_pdu_raw: %p\n", incoming_network_pdu_raw);
184     printf("incoming_network_pdu_decoded: %p\n", incoming_network_pdu_decoded);
185     mesh_upper_transport_dump_pdus("upper_transport_incoming", &upper_transport_incoming);
186 }
187 
188 void mesh_upper_transport_reset(void){
189     crypto_active = 0;
190     if (incoming_network_pdu_raw){
191         mesh_network_pdu_free(incoming_network_pdu_raw);
192         incoming_network_pdu_raw = NULL;
193     }
194     // if (incoming_network_pdu_decoded){
195     //     mesh_network_pdu_free(incoming_network_pdu_decoded);
196     //     incoming_network_pdu_decoded = NULL;
197     // }
198     mesh_upper_transport_reset_pdus(&upper_transport_incoming);
199 }
200 
201 static void mesh_upper_unsegmented_control_message_received(mesh_network_pdu_t * network_pdu){
202     uint8_t * lower_transport_pdu     = mesh_network_pdu_data(network_pdu);
203     uint8_t  opcode = lower_transport_pdu[0];
204     if (mesh_control_message_handler){
205         mesh_control_message_handler((mesh_pdu_t*) network_pdu);
206     } else {
207         printf("[!] Unhandled Control message with opcode %02x\n", opcode);
208         // done
209         mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) network_pdu);
210     }
211 }
212 
213 static void mesh_upper_transport_process_unsegmented_message_done(mesh_network_pdu_t *network_pdu){
214     crypto_active = 0;
215     if (mesh_network_control(network_pdu)) {
216         mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) network_pdu);
217     } else {
218         mesh_network_pdu_free(network_pdu);
219         mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) incoming_network_pdu_raw);
220         incoming_network_pdu_raw = NULL;
221     }
222     mesh_upper_transport_run();
223 }
224 
225 static void mesh_upper_transport_process_segmented_message_done(mesh_transport_pdu_t *transport_pdu){
226     crypto_active = 0;
227     if (mesh_transport_ctl(transport_pdu)) {
228         mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *)transport_pdu);
229     } else {
230         mesh_transport_pdu_free(transport_pdu);
231         mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *)incoming_transport_pdu_raw);
232         incoming_transport_pdu_raw = NULL;
233     }
234     mesh_upper_transport_run();
235 }
236 
237 static uint32_t iv_index_for_ivi_nid(uint8_t ivi_nid){
238     // get IV Index and IVI
239     uint32_t iv_index = mesh_get_iv_index();
240     int ivi = ivi_nid >> 7;
241 
242     // if least significant bit differs, use previous IV Index
243     if ((iv_index & 1 ) ^ ivi){
244         iv_index--;
245     }
246     return iv_index;
247 }
248 
249 static void transport_unsegmented_setup_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){
250     nonce[1] = 0x00;    // SZMIC if a Segmented Access message or 0 for all other message formats
251     memcpy(&nonce[2], &network_pdu->data[2], 7);
252     big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(network_pdu->data[0]));
253 }
254 
255 static void transport_segmented_setup_nonce(uint8_t * nonce, const mesh_transport_pdu_t * transport_pdu){
256     nonce[1] = transport_pdu->transmic_len == 8 ? 0x80 : 0x00;
257     memcpy(&nonce[2], &transport_pdu->network_header[2], 7);
258     big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(transport_pdu->network_header[0]));
259 }
260 
261 static void transport_unsegmented_setup_application_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){
262     nonce[0] = 0x01;
263     transport_unsegmented_setup_nonce(nonce, network_pdu);
264     mesh_print_hex("AppNonce", nonce, 13);
265 }
266 
267 static void transport_unsegmented_setup_device_nonce(uint8_t * nonce, const mesh_network_pdu_t * network_pdu){
268     nonce[0] = 0x02;
269     transport_unsegmented_setup_nonce(nonce, network_pdu);
270     mesh_print_hex("DeviceNonce", nonce, 13);
271 }
272 
273 static void transport_segmented_setup_application_nonce(uint8_t * nonce, const mesh_transport_pdu_t * transport_pdu){
274     nonce[0] = 0x01;
275     transport_segmented_setup_nonce(nonce, transport_pdu);
276     mesh_print_hex("AppNonce", nonce, 13);
277 }
278 
279 static void transport_segmented_setup_device_nonce(uint8_t * nonce, const mesh_transport_pdu_t * transport_pdu){
280     nonce[0] = 0x02;
281     transport_segmented_setup_nonce(nonce, transport_pdu);
282     mesh_print_hex("DeviceNonce", nonce, 13);
283 }
284 
285 static void mesh_upper_transport_validate_unsegmented_message_ccm(void * arg){
286     UNUSED(arg);
287 
288     uint8_t * lower_transport_pdu = mesh_network_pdu_data(incoming_network_pdu_decoded);
289     uint8_t trans_mic_len = 4;
290 
291     // store TransMIC
292     uint8_t trans_mic[8];
293     btstack_crypto_ccm_get_authentication_value(&ccm, trans_mic);
294     mesh_print_hex("TransMIC", trans_mic, trans_mic_len);
295 
296     uint8_t * upper_transport_pdu     = mesh_network_pdu_data(incoming_network_pdu_decoded) + 1;
297     uint8_t   upper_transport_pdu_len = mesh_network_pdu_len(incoming_network_pdu_decoded)  - 1;
298 
299     mesh_print_hex("Decryted PDU", upper_transport_pdu, upper_transport_pdu_len - trans_mic_len);
300 
301     if (memcmp(trans_mic, &upper_transport_pdu[upper_transport_pdu_len - trans_mic_len], trans_mic_len) == 0){
302         printf("TransMIC matches\n");
303 
304         // remove TransMIC from payload
305         incoming_network_pdu_decoded->len -= trans_mic_len;
306 
307         // if virtual address, update dst to pseudo_dst
308         if (mesh_network_address_virtual(mesh_network_dst(incoming_network_pdu_decoded))){
309             big_endian_store_16(incoming_network_pdu_decoded->data, 7, mesh_transport_key_it.address->pseudo_dst);
310         }
311 
312         // pass to upper layer
313         if (mesh_access_message_handler){
314             mesh_pdu_t * pdu = (mesh_pdu_t*) incoming_network_pdu_decoded;
315             incoming_network_pdu_decoded = NULL;
316             mesh_access_message_handler(pdu);
317         } else {
318             printf("[!] Unhandled Unsegmented Access message\n");
319             // done
320             mesh_upper_transport_process_unsegmented_message_done(incoming_network_pdu_decoded);
321         }
322 
323         printf("\n");
324     } else {
325         uint8_t afk = lower_transport_pdu[0] & 0x40;
326         if (afk){
327             printf("TransMIC does not match, try next key\n");
328             mesh_upper_transport_validate_unsegmented_message();
329         } else {
330             printf("TransMIC does not match device key, done\n");
331             // done
332             mesh_upper_transport_process_unsegmented_message_done(incoming_network_pdu_decoded);
333         }
334     }
335 }
336 
337 static void mesh_upper_transport_validate_segmented_message_ccm(void * arg){
338     UNUSED(arg);
339 
340     uint8_t * upper_transport_pdu     =  incoming_transport_pdu_decoded->data;
341     uint8_t   upper_transport_pdu_len =  incoming_transport_pdu_decoded->len - incoming_transport_pdu_decoded->transmic_len;
342 
343     mesh_print_hex("Decrypted PDU", upper_transport_pdu, upper_transport_pdu_len);
344 
345     // store TransMIC
346     uint8_t trans_mic[8];
347     btstack_crypto_ccm_get_authentication_value(&ccm, trans_mic);
348     mesh_print_hex("TransMIC", trans_mic, incoming_transport_pdu_decoded->transmic_len);
349 
350     if (memcmp(trans_mic, &upper_transport_pdu[upper_transport_pdu_len], incoming_transport_pdu_decoded->transmic_len) == 0){
351         printf("TransMIC matches\n");
352 
353         // remove TransMIC from payload
354         incoming_transport_pdu_decoded->len -= incoming_transport_pdu_decoded->transmic_len;
355 
356         // if virtual address, update dst to pseudo_dst
357         if (mesh_network_address_virtual(mesh_transport_dst(incoming_transport_pdu_decoded))){
358             big_endian_store_16(incoming_transport_pdu_decoded->network_header, 7, mesh_transport_key_it.address->pseudo_dst);
359         }
360 
361         // pass to upper layer
362         if (mesh_access_message_handler){
363             mesh_pdu_t * pdu = (mesh_pdu_t*) incoming_transport_pdu_decoded;
364             incoming_network_pdu_decoded = NULL;
365             mesh_access_message_handler(pdu);
366         } else {
367             printf("[!] Unhandled Segmented Access/Control message\n");
368             // done
369             mesh_upper_transport_process_segmented_message_done(incoming_transport_pdu_decoded);
370         }
371 
372         printf("\n");
373 
374     } else {
375         uint8_t akf = incoming_transport_pdu_decoded->akf_aid_control & 0x40;
376         if (akf){
377             printf("TransMIC does not match, try next key\n");
378             mesh_upper_transport_validate_segmented_message();
379         } else {
380             printf("TransMIC does not match device key, done\n");
381             // done
382             mesh_upper_transport_process_segmented_message_done(incoming_transport_pdu_decoded);
383         }
384     }
385 }
386 
387 void mesh_upper_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){
388     crypto_active = 0;
389     switch (pdu->pdu_type){
390         case MESH_PDU_TYPE_NETWORK:
391             mesh_upper_transport_process_unsegmented_message_done((mesh_network_pdu_t *) pdu);
392             break;
393         case MESH_PDU_TYPE_TRANSPORT:
394             mesh_upper_transport_process_segmented_message_done((mesh_transport_pdu_t *) pdu);
395             break;
396         default:
397             break;
398     }
399 }
400 
401 static void mesh_upper_transport_validate_segmented_message_digest(void * arg){
402     UNUSED(arg);
403     uint8_t   upper_transport_pdu_len      = incoming_transport_pdu_raw->len - incoming_transport_pdu_raw->transmic_len;
404     uint8_t * upper_transport_pdu_data_in  = incoming_transport_pdu_raw->data;
405     uint8_t * upper_transport_pdu_data_out = incoming_transport_pdu_decoded->data;
406     btstack_crypto_ccm_decrypt_block(&ccm, upper_transport_pdu_len, upper_transport_pdu_data_in, upper_transport_pdu_data_out, &mesh_upper_transport_validate_segmented_message_ccm, NULL);
407 }
408 
409 static void mesh_upper_transport_validate_unsegmented_message_digest(void * arg){
410     UNUSED(arg);
411     uint8_t   trans_mic_len = 4;
412     uint8_t   lower_transport_pdu_len      = incoming_network_pdu_raw->len - 9;
413     uint8_t * upper_transport_pdu_data_in  = &incoming_network_pdu_raw->data[10];
414     uint8_t * upper_transport_pdu_data_out = &incoming_network_pdu_decoded->data[10];
415     uint8_t   upper_transport_pdu_len      = lower_transport_pdu_len - 1 - trans_mic_len;
416     btstack_crypto_ccm_decrypt_block(&ccm, upper_transport_pdu_len, upper_transport_pdu_data_in, upper_transport_pdu_data_out, &mesh_upper_transport_validate_unsegmented_message_ccm, NULL);
417 }
418 
419 static void mesh_upper_transport_validate_unsegmented_message(void){
420 
421     if (!mesh_transport_key_and_virtual_address_iterator_has_more(&mesh_transport_key_it)){
422         printf("No valid transport key found\n");
423         mesh_upper_transport_process_unsegmented_message_done(incoming_network_pdu_decoded);
424         return;
425     }
426     mesh_transport_key_and_virtual_address_iterator_next(&mesh_transport_key_it);
427     const mesh_transport_key_t * message_key = mesh_transport_key_it.key;
428 
429     if (message_key->akf){
430         transport_unsegmented_setup_application_nonce(application_nonce, incoming_network_pdu_raw);
431     } else {
432         transport_unsegmented_setup_device_nonce(application_nonce, incoming_network_pdu_raw);
433     }
434 
435     // store application / device key index
436     mesh_print_hex("AppOrDevKey", message_key->key, 16);
437     incoming_network_pdu_decoded->appkey_index = message_key->appkey_index;
438 
439     // unsegmented message have TransMIC of 32 bit
440     uint8_t trans_mic_len = 4;
441     printf("Unsegmented Access message with TransMIC len 4\n");
442 
443     uint8_t   lower_transport_pdu_len = incoming_network_pdu_raw->len - 9;
444     uint8_t * upper_transport_pdu_data = &incoming_network_pdu_raw->data[10];
445     uint8_t   upper_transport_pdu_len  = lower_transport_pdu_len - 1 - trans_mic_len;
446 
447     mesh_print_hex("EncAccessPayload", upper_transport_pdu_data, upper_transport_pdu_len);
448 
449     // decrypt ccm
450     crypto_active = 1;
451     uint16_t aad_len  = 0;
452     if (mesh_network_address_virtual(mesh_network_dst(incoming_network_pdu_decoded))){
453         aad_len  = 16;
454     }
455     btstack_crypto_ccm_init(&ccm, message_key->key, application_nonce, upper_transport_pdu_len, aad_len, trans_mic_len);
456     if (aad_len){
457         btstack_crypto_ccm_digest(&ccm, (uint8_t*) mesh_transport_key_it.address->label_uuid, aad_len, &mesh_upper_transport_validate_unsegmented_message_digest, NULL);
458     } else {
459         mesh_upper_transport_validate_unsegmented_message_digest(incoming_network_pdu_decoded);
460     }
461 }
462 
463 static void mesh_upper_transport_validate_segmented_message(void){
464     uint8_t * upper_transport_pdu_data =  incoming_transport_pdu_decoded->data;
465     uint8_t   upper_transport_pdu_len  =  incoming_transport_pdu_decoded->len - incoming_transport_pdu_decoded->transmic_len;
466 
467     if (!mesh_transport_key_and_virtual_address_iterator_has_more(&mesh_transport_key_it)){
468         printf("No valid transport key found\n");
469         mesh_upper_transport_process_segmented_message_done(incoming_transport_pdu_decoded);
470         return;
471     }
472     mesh_transport_key_and_virtual_address_iterator_next(&mesh_transport_key_it);
473     const mesh_transport_key_t * message_key = mesh_transport_key_it.key;
474 
475     if (message_key->akf){
476         transport_segmented_setup_application_nonce(application_nonce, incoming_transport_pdu_raw);
477     } else {
478         transport_segmented_setup_device_nonce(application_nonce, incoming_transport_pdu_raw);
479     }
480 
481     // store application / device key index
482     mesh_print_hex("AppOrDevKey", message_key->key, 16);
483     incoming_transport_pdu_decoded->appkey_index = message_key->appkey_index;
484 
485     mesh_print_hex("EncAccessPayload", upper_transport_pdu_data, upper_transport_pdu_len);
486 
487     // decrypt ccm
488     crypto_active = 1;
489     uint16_t aad_len  = 0;
490     if (mesh_network_address_virtual(mesh_transport_dst(incoming_transport_pdu_decoded))){
491         aad_len  = 16;
492     }
493     btstack_crypto_ccm_init(&ccm, message_key->key, application_nonce, upper_transport_pdu_len, aad_len, incoming_transport_pdu_decoded->transmic_len);
494 
495     if (aad_len){
496         btstack_crypto_ccm_digest(&ccm, (uint8_t *) mesh_transport_key_it.address->label_uuid, aad_len, &mesh_upper_transport_validate_segmented_message_digest, NULL);
497     } else {
498         mesh_upper_transport_validate_segmented_message_digest(NULL);
499     }
500 }
501 
502 static void mesh_upper_transport_process_unsegmented_access_message(void){
503     // copy original pdu
504     incoming_network_pdu_decoded->len = incoming_network_pdu_raw->len;
505     memcpy(incoming_network_pdu_decoded->data, incoming_network_pdu_raw->data, incoming_network_pdu_decoded->len);
506 
507     //
508     uint8_t * lower_transport_pdu     = &incoming_network_pdu_raw->data[9];
509     uint8_t   lower_transport_pdu_len = incoming_network_pdu_raw->len - 9;
510 
511     mesh_print_hex("Lower Transport network pdu", &incoming_network_pdu_raw->data[9], lower_transport_pdu_len);
512 
513     uint8_t aid =  lower_transport_pdu[0] & 0x3f;
514     uint8_t akf = (lower_transport_pdu[0] & 0x40) >> 6;
515     printf("AKF: %u\n",   akf);
516     printf("AID: %02x\n", aid);
517 
518     mesh_transport_key_and_virtual_address_iterator_init(&mesh_transport_key_it, mesh_network_dst(incoming_network_pdu_decoded),
519             incoming_network_pdu_decoded->netkey_index, akf, aid);
520     mesh_upper_transport_validate_unsegmented_message();
521 }
522 
523 static void mesh_upper_transport_process_message(void){
524     // copy original pdu
525     memcpy(incoming_transport_pdu_decoded, incoming_transport_pdu_raw, sizeof(mesh_transport_pdu_t));
526 
527     //
528     uint8_t * upper_transport_pdu     =  incoming_transport_pdu_decoded->data;
529     uint8_t   upper_transport_pdu_len =  incoming_transport_pdu_decoded->len - incoming_transport_pdu_decoded->transmic_len;
530     mesh_print_hex("Upper Transport pdu", upper_transport_pdu, upper_transport_pdu_len);
531 
532     uint8_t aid =  incoming_transport_pdu_decoded->akf_aid_control & 0x3f;
533     uint8_t akf = (incoming_transport_pdu_decoded->akf_aid_control & 0x40) >> 6;
534 
535     printf("AKF: %u\n",   akf);
536     printf("AID: %02x\n", aid);
537 
538     mesh_transport_key_and_virtual_address_iterator_init(&mesh_transport_key_it, mesh_transport_dst(incoming_transport_pdu_decoded),
539             incoming_transport_pdu_decoded->netkey_index, akf, aid);
540     mesh_upper_transport_validate_segmented_message();
541 }
542 
543 static void mesh_upper_transport_message_received(mesh_pdu_t * pdu){
544     btstack_linked_list_add_tail(&upper_transport_incoming, (btstack_linked_item_t*) pdu);
545     mesh_upper_transport_run();
546 }
547 
548 void mesh_upper_transport_pdu_free(mesh_pdu_t * pdu){
549     mesh_network_pdu_t * network_pdu;
550     mesh_transport_pdu_t * transport_pdu;
551     switch (pdu->pdu_type) {
552         case MESH_PDU_TYPE_NETWORK:
553             network_pdu = (mesh_network_pdu_t *) pdu;
554             mesh_network_pdu_free(network_pdu);
555             break;
556         case MESH_PDU_TYPE_TRANSPORT:
557             transport_pdu = (mesh_transport_pdu_t *) pdu;
558             mesh_transport_pdu_free(transport_pdu);
559             break;
560         default:
561             break;
562     }
563 }
564 
565 static void mesh_upper_transport_pdu_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu){
566     switch (callback_type){
567         case MESH_TRANSPORT_PDU_RECEIVED:
568             mesh_upper_transport_message_received(pdu);
569             break;
570         case MESH_TRANSPORT_PDU_SENT:
571             // notify upper layer (or just free pdu)
572             if (higher_layer_handler){
573                 higher_layer_handler(callback_type, status, pdu);
574             } else {
575                 mesh_upper_transport_pdu_free(pdu);
576             }
577             break;
578         default:
579             break;
580     }
581 }
582 static void mesh_upper_transport_send_unsegmented_access_pdu_ccm(void * arg){
583     crypto_active = 0;
584 
585     mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg;
586     uint8_t * upper_transport_pdu     = mesh_network_pdu_data(network_pdu) + 1;
587     uint8_t   upper_transport_pdu_len = mesh_network_pdu_len(network_pdu)  - 1;
588     mesh_print_hex("EncAccessPayload", upper_transport_pdu, upper_transport_pdu_len);
589     // store TransMIC
590     btstack_crypto_ccm_get_authentication_value(&ccm, &upper_transport_pdu[upper_transport_pdu_len]);
591     mesh_print_hex("TransMIC", &upper_transport_pdu[upper_transport_pdu_len], 4);
592     network_pdu->len += 4;
593     mesh_print_hex("UpperTransportPDU", upper_transport_pdu, network_pdu->len);
594     // send network pdu
595     mesh_lower_transport_send_pdu((mesh_pdu_t*) network_pdu);
596 }
597 
598 static void mesh_upper_transport_send_segmented_access_pdu_ccm(void * arg){
599     crypto_active = 0;
600 
601     mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg;
602     mesh_print_hex("EncAccessPayload", transport_pdu->data, transport_pdu->len);
603     // store TransMIC
604     btstack_crypto_ccm_get_authentication_value(&ccm, &transport_pdu->data[transport_pdu->len]);
605     mesh_print_hex("TransMIC", &transport_pdu->data[transport_pdu->len], transport_pdu->transmic_len);
606     transport_pdu->len += transport_pdu->transmic_len;
607     mesh_print_hex("UpperTransportPDU", transport_pdu->data, transport_pdu->len);
608     mesh_lower_transport_send_pdu((mesh_pdu_t*) transport_pdu);
609 }
610 
611 static uint8_t mesh_upper_transport_setup_unsegmented_control_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode,
612                           const uint8_t * control_pdu_data, uint16_t control_pdu_len){
613 
614     printf("[+] Upper transport, setup unsegmented Control PDU %p (opcode %02x): ", network_pdu, opcode);
615     printf_hexdump(control_pdu_data, control_pdu_len);
616 
617     if (control_pdu_len > 11) return 1;
618 
619     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
620     if (!network_key) return 1;
621 
622     uint8_t transport_pdu_data[12];
623     transport_pdu_data[0] = opcode;
624     memcpy(&transport_pdu_data[1], control_pdu_data, control_pdu_len);
625     uint16_t transport_pdu_len = control_pdu_len + 1;
626 
627     // setup network_pdu
628     mesh_network_setup_pdu(network_pdu, netkey_index, network_key->nid, 1, ttl, 0, src, dest, transport_pdu_data, transport_pdu_len);
629 
630     return 0;
631 }
632 
633 static uint8_t mesh_upper_transport_setup_segmented_control_pdu(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode,
634                           const uint8_t * control_pdu_data, uint16_t control_pdu_len){
635 
636     printf("[+] Upper transport, setup segmented Control PDU (opcode %02x): \n", opcode);
637     printf_hexdump(control_pdu_data, control_pdu_len);
638 
639     if (control_pdu_len > 256) return 1;
640 
641     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
642     if (!network_key) return 1;
643 
644     memcpy(transport_pdu->data, control_pdu_data, control_pdu_len);
645     transport_pdu->len = control_pdu_len;
646     transport_pdu->netkey_index = netkey_index;
647     transport_pdu->akf_aid_control = opcode;
648     transport_pdu->transmic_len = 0;    // no TransMIC for control
649     mesh_transport_set_nid_ivi(transport_pdu, network_key->nid);
650     mesh_transport_set_src(transport_pdu, src);
651     mesh_transport_set_dest(transport_pdu, dest);
652     mesh_transport_set_ctl_ttl(transport_pdu, 0x80 | ttl);
653 
654     return 0;
655 }
656 
657 uint8_t mesh_upper_transport_setup_control_pdu(mesh_pdu_t * pdu, uint16_t netkey_index,
658                                                uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode, const uint8_t * control_pdu_data, uint16_t control_pdu_len){
659     switch (pdu->pdu_type){
660         case MESH_PDU_TYPE_NETWORK:
661             return mesh_upper_transport_setup_unsegmented_control_pdu((mesh_network_pdu_t *) pdu, netkey_index, ttl, src, dest, opcode, control_pdu_data, control_pdu_len);
662         case MESH_PDU_TYPE_TRANSPORT:
663             return mesh_upper_transport_setup_segmented_control_pdu((mesh_transport_pdu_t *) pdu, netkey_index, ttl, src, dest, opcode, control_pdu_data, control_pdu_len);
664         default:
665             return 1;
666     }
667 }
668 
669 static uint8_t mesh_upper_transport_setup_unsegmented_access_pdu_header(mesh_network_pdu_t * network_pdu, uint16_t netkey_index,
670         uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest){
671 
672     // get app or device key
673     const mesh_transport_key_t * appkey;
674     appkey = mesh_transport_key_get(appkey_index);
675     if (appkey == NULL){
676         printf("appkey_index %x unknown\n", appkey_index);
677         return 1;
678     }
679     uint8_t akf_aid = (appkey->akf << 6) | appkey->aid;
680 
681     // lookup network by netkey_index
682     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
683     if (!network_key) return 1;
684 
685     network_pdu->data[9] = akf_aid;
686     // setup network_pdu
687     mesh_network_setup_pdu_header(network_pdu, netkey_index, network_key->nid, 0, ttl, 0, src, dest);
688     network_pdu->appkey_index = appkey_index;
689     return 0;
690 }
691 
692 static uint8_t mesh_upper_transport_setup_unsegmented_access_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest,
693                                                        const uint8_t * access_pdu_data, uint8_t access_pdu_len){
694 
695     int status = mesh_upper_transport_setup_unsegmented_access_pdu_header(network_pdu, netkey_index, appkey_index, ttl, src, dest);
696     if (status) return status;
697 
698     printf("[+] Upper transport, setup unsegmented Access PDU - seq %06x\n", mesh_network_seq(network_pdu));
699     mesh_print_hex("Access Payload", access_pdu_data, access_pdu_len);
700 
701     // store in transport pdu
702     memcpy(&network_pdu->data[10], access_pdu_data, access_pdu_len);
703     network_pdu->len = 10 + access_pdu_len;
704     return 0;
705 }
706 
707 static uint8_t mesh_upper_transport_setup_segmented_access_pdu_header(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index,
708     uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){
709 
710     // get app or device key
711     const mesh_transport_key_t *appkey;
712     appkey = mesh_transport_key_get(appkey_index);
713     if (appkey == NULL) {
714         printf("[!] Upper transport, setup segmented Access PDU - appkey_index %x unknown\n", appkey_index);
715         return 1;
716     }
717     uint8_t akf_aid = (appkey->akf << 6) | appkey->aid;
718 
719     // lookup network by netkey_index
720     const mesh_network_key_t *network_key = mesh_network_key_list_get(netkey_index);
721     if (!network_key) return 1;
722     if (network_key == NULL) {
723         printf("[!] Upper transport, setup segmented Access PDU - netkey_index %x unknown\n", appkey_index);
724         return 1;
725     }
726 
727     const uint8_t trans_mic_len = szmic ? 8 : 4;
728 
729     // lower transport will call next sequence number. Only peek here to use same seq for access payload encryption as well as for first network pdu (unit test)
730     uint32_t seq = mesh_sequence_number_peek();
731 
732     // store in transport pdu
733     transport_pdu->transmic_len = trans_mic_len;
734     transport_pdu->netkey_index = netkey_index;
735     transport_pdu->appkey_index = appkey_index;
736     transport_pdu->akf_aid_control = akf_aid;
737     mesh_transport_set_nid_ivi(transport_pdu, network_key->nid | ((mesh_get_iv_index_for_tx() & 1) << 7));
738     mesh_transport_set_seq(transport_pdu, seq);
739     mesh_transport_set_src(transport_pdu, src);
740     mesh_transport_set_dest(transport_pdu, dest);
741     mesh_transport_set_ctl_ttl(transport_pdu, ttl);
742     return 0;
743 }
744 
745 
746 static uint8_t mesh_upper_transport_setup_segmented_access_pdu(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest,
747                           uint8_t szmic, const uint8_t * access_pdu_data, uint8_t access_pdu_len){
748     int status = mesh_upper_transport_setup_segmented_access_pdu_header(transport_pdu, netkey_index, appkey_index, ttl, src, dest, szmic);
749     if (status) return status;
750 
751     // store in transport pdu
752     memcpy(transport_pdu->data, access_pdu_data, access_pdu_len);
753     transport_pdu->len = access_pdu_len;
754 
755     printf("[+] Upper transport, setup segmented Access PDU - seq %06x, szmic %u, iv_index %08x\n", mesh_transport_seq(transport_pdu), szmic, mesh_get_iv_index_for_tx());
756     mesh_print_hex("Access Payload", transport_pdu->data, transport_pdu->len);
757     return 0;
758 }
759 uint8_t mesh_upper_transport_setup_access_pdu_header(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index,
760                                               uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){
761     switch (pdu->pdu_type){
762         case MESH_PDU_TYPE_NETWORK:
763             return mesh_upper_transport_setup_unsegmented_access_pdu_header((mesh_network_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest);
764         case MESH_PDU_TYPE_TRANSPORT:
765             return mesh_upper_transport_setup_segmented_access_pdu_header((mesh_transport_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, szmic);
766         default:
767             return 1;
768     }
769 }
770 
771 uint8_t mesh_upper_transport_setup_access_pdu(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index,
772                                               uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic,
773                                               const uint8_t * access_pdu_data, uint8_t access_pdu_len){
774     switch (pdu->pdu_type){
775         case MESH_PDU_TYPE_NETWORK:
776             return mesh_upper_transport_setup_unsegmented_access_pdu((mesh_network_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, access_pdu_data, access_pdu_len);
777         case MESH_PDU_TYPE_TRANSPORT:
778             return mesh_upper_transport_setup_segmented_access_pdu((mesh_transport_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, szmic, access_pdu_data, access_pdu_len);
779         default:
780             return 1;
781     }
782 }
783 
784 void mesh_upper_transport_send_control_pdu(mesh_pdu_t * pdu){
785     mesh_lower_transport_send_pdu((mesh_pdu_t*) pdu);
786 }
787 
788 static void mesh_upper_transport_send_unsegmented_access_pdu_digest(void * arg){
789     mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg;
790     uint8_t * access_pdu_data = mesh_network_pdu_data(network_pdu) + 1;
791     uint16_t  access_pdu_len  = mesh_network_pdu_len(network_pdu)  - 1;
792     btstack_crypto_ccm_encrypt_block(&ccm, access_pdu_len, access_pdu_data, access_pdu_data, &mesh_upper_transport_send_unsegmented_access_pdu_ccm, network_pdu);
793 }
794 
795 static mesh_transport_key_t * mesh_upper_transport_get_outgoing_appkey(uint16_t netkey_index, uint16_t appkey_index){
796     // Device Key is fixed
797     if (appkey_index == MESH_DEVICE_KEY_INDEX) {
798         return mesh_transport_key_get(appkey_index);
799     }
800 
801     // Get key refresh state from subnet
802     mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index);
803     if (subnet == NULL) return NULL;
804 
805     // identify old and new app keys for given appkey_index
806     mesh_transport_key_t * old_key = NULL;
807     mesh_transport_key_t * new_key = NULL;
808     mesh_transport_key_iterator_t it;
809     mesh_transport_key_iterator_init(&it, netkey_index);
810     while (mesh_transport_key_iterator_has_more(&it)){
811         mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it);
812         if (transport_key->appkey_index != appkey_index) continue;
813         if (transport_key->old_key == 0) {
814             new_key = transport_key;
815         } else {
816             old_key = transport_key;
817         }
818     }
819 
820     // if no key is marked as old, just use the current one
821     if (old_key == NULL) return new_key;
822 
823     // use new key if it exists in phase two
824     if ((subnet->key_refresh == MESH_KEY_REFRESH_SECOND_PHASE) && (new_key != NULL)){
825         return new_key;
826     } else {
827         return old_key;
828     }
829 }
830 
831 static void mesh_upper_transport_send_unsegmented_access_pdu(mesh_network_pdu_t * network_pdu){
832 
833     // if dst is virtual address, lookup label uuid and hash
834     uint16_t aad_len = 0;
835     mesh_virtual_address_t * virtual_address = NULL;
836     uint16_t dst = mesh_network_dst(network_pdu);
837     if (mesh_network_address_virtual(dst)){
838         virtual_address = mesh_virtual_address_for_pseudo_dst(dst);
839         if (!virtual_address){
840             printf("No virtual address register for pseudo dst %4x\n", dst);
841             btstack_memory_mesh_network_pdu_free(network_pdu);
842             return;
843         }
844         aad_len = 16;
845         big_endian_store_16(network_pdu->data, 7, virtual_address->hash);
846     }
847 
848     // setup nonce
849     uint16_t appkey_index = network_pdu->appkey_index;
850     if (appkey_index == MESH_DEVICE_KEY_INDEX){
851         transport_unsegmented_setup_device_nonce(application_nonce, network_pdu);
852     } else {
853         transport_unsegmented_setup_application_nonce(application_nonce, network_pdu);
854     }
855 
856     // get app or device key
857     const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(network_pdu->netkey_index, appkey_index);
858     mesh_print_hex("AppOrDevKey", appkey->key, 16);
859 
860     // encrypt ccm
861     uint8_t   trans_mic_len = 4;
862     uint16_t  access_pdu_len  = mesh_network_pdu_len(network_pdu)  - 1;
863     crypto_active = 1;
864 
865     btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, trans_mic_len);
866     if (virtual_address){
867         mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16);
868         btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_unsegmented_access_pdu_digest, network_pdu);
869     } else {
870         mesh_upper_transport_send_unsegmented_access_pdu_digest(network_pdu);
871     }
872 }
873 
874 static void mesh_upper_transport_send_segmented_access_pdu_digest(void *arg){
875     mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg;
876     uint16_t  access_pdu_len  = transport_pdu->len;
877     uint8_t * access_pdu_data = transport_pdu->data;
878     btstack_crypto_ccm_encrypt_block(&ccm, access_pdu_len,access_pdu_data, access_pdu_data, &mesh_upper_transport_send_segmented_access_pdu_ccm, transport_pdu);
879 }
880 
881 static void mesh_upper_transport_send_segmented_access_pdu(mesh_transport_pdu_t * transport_pdu){
882 
883     // if dst is virtual address, lookup label uuid and hash
884     uint16_t aad_len = 0;
885     mesh_virtual_address_t * virtual_address = NULL;
886     uint16_t dst = mesh_transport_dst(transport_pdu);
887     if (mesh_network_address_virtual(dst)){
888         virtual_address = mesh_virtual_address_for_pseudo_dst(dst);
889         if (!virtual_address){
890             printf("No virtual address register for pseudo dst %4x\n", dst);
891             btstack_memory_mesh_transport_pdu_free(transport_pdu);
892             return;
893         }
894         // printf("Using hash %4x with LabelUUID: ", virtual_address->hash);
895         // printf_hexdump(virtual_address->label_uuid, 16);
896         aad_len = 16;
897         big_endian_store_16(transport_pdu->network_header, 7, virtual_address->hash);
898     }
899 
900     // setup nonce - uses dst, so after pseudo address translation
901     uint16_t appkey_index = transport_pdu->appkey_index;
902     if (appkey_index == MESH_DEVICE_KEY_INDEX){
903         transport_segmented_setup_device_nonce(application_nonce, transport_pdu);
904     } else {
905         transport_segmented_setup_application_nonce(application_nonce, transport_pdu);
906     }
907 
908     // get app or device key
909     const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(transport_pdu->netkey_index, appkey_index);
910     mesh_print_hex("AppOrDevKey", appkey->key, 16);
911 
912     // encrypt ccm
913     uint8_t   transmic_len    = transport_pdu->transmic_len;
914     uint16_t  access_pdu_len  = transport_pdu->len;
915     crypto_active = 1;
916     btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, transmic_len);
917     if (virtual_address){
918         mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16);
919         btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_segmented_access_pdu_digest, transport_pdu);
920     } else {
921         mesh_upper_transport_send_segmented_access_pdu_digest(transport_pdu);
922     }
923 }
924 
925 static void mesh_upper_transport_run(void){
926     while(!btstack_linked_list_empty(&upper_transport_incoming)){
927 
928         if (crypto_active) return;
929 
930         // peek at next message
931         mesh_pdu_t * pdu =  (mesh_pdu_t *) btstack_linked_list_get_first_item(&upper_transport_incoming);
932         mesh_transport_pdu_t * transport_pdu;
933         mesh_network_pdu_t   * network_pdu;
934         switch (pdu->pdu_type){
935             case MESH_PDU_TYPE_NETWORK:
936                 network_pdu = (mesh_network_pdu_t *) pdu;
937                 // control?
938                 if (mesh_network_control(network_pdu)) {
939                     (void) btstack_linked_list_pop(&upper_transport_incoming);
940                     mesh_upper_unsegmented_control_message_received(network_pdu);
941                 } else {
942                     incoming_network_pdu_decoded = mesh_network_pdu_get();
943                     if (!incoming_network_pdu_decoded) return;
944                     // get encoded network pdu and start processing
945                     incoming_network_pdu_raw = network_pdu;
946                     (void) btstack_linked_list_pop(&upper_transport_incoming);
947                     mesh_upper_transport_process_unsegmented_access_message();
948                 }
949                 break;
950             case MESH_PDU_TYPE_TRANSPORT:
951                 transport_pdu = (mesh_transport_pdu_t *) pdu;
952                 uint8_t ctl = mesh_transport_ctl(transport_pdu);
953                 if (ctl){
954                     printf("Ignoring Segmented Control Message\n");
955                     (void) btstack_linked_list_pop(&upper_transport_incoming);
956                     mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) transport_pdu);
957                 } else {
958                     incoming_transport_pdu_decoded = mesh_transport_pdu_get();
959                     if (!incoming_transport_pdu_decoded) return;
960                     // get encoded transport pdu and start processing
961                     incoming_transport_pdu_raw = transport_pdu;
962                     (void) btstack_linked_list_pop(&upper_transport_incoming);
963                     mesh_upper_transport_process_message();
964                 }
965                 break;
966             default:
967                 break;
968         }
969     }
970 
971     while (!btstack_linked_list_empty(&upper_transport_outgoing)){
972 
973         if (crypto_active) return;
974 
975         mesh_pdu_t * pdu =  (mesh_pdu_t *) btstack_linked_list_pop(&upper_transport_outgoing);
976         switch (pdu->pdu_type){
977             case MESH_PDU_TYPE_NETWORK:
978                 mesh_upper_transport_send_unsegmented_access_pdu((mesh_network_pdu_t *) pdu);
979                 break;
980             case MESH_PDU_TYPE_TRANSPORT:
981                 mesh_upper_transport_send_segmented_access_pdu((mesh_transport_pdu_t *) pdu);
982                 break;
983             default:
984                 break;
985         }
986     }
987 }
988 
989 void mesh_upper_transport_register_access_message_handler(void (*callback)(mesh_pdu_t *pdu)){
990     mesh_access_message_handler = callback;
991 }
992 
993 void mesh_upper_transport_register_control_message_handler(void (*callback)(mesh_pdu_t *pdu)){
994     mesh_control_message_handler = callback;
995 }
996 
997 void mesh_upper_transport_set_higher_layer_handler(void (*pdu_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu)){
998     higher_layer_handler = pdu_handler;
999 }
1000 
1001 void mesh_upper_transport_init(){
1002     mesh_lower_transport_set_higher_layer_handler(&mesh_upper_transport_pdu_handler);
1003 }
1004 
1005 void mesh_upper_transport_send_access_pdu(mesh_pdu_t *pdu){
1006     if (pdu->pdu_type == MESH_PDU_TYPE_NETWORK){
1007         btstack_assert(mesh_network_pdu_len((mesh_network_pdu_t *) pdu) >= 9);
1008     }
1009 
1010     btstack_linked_list_add_tail(&upper_transport_outgoing, (btstack_linked_item_t*) pdu);
1011     mesh_upper_transport_run();
1012 }
1013