xref: /btstack/src/mesh/mesh_upper_transport.c (revision b16fda24f6ea24ed027c2cbd3534ad4347e9eeae)
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     upper_transport_pdu_len += 4;
594     mesh_print_hex("UpperTransportPDU", upper_transport_pdu, upper_transport_pdu_len);
595     // send network pdu
596     mesh_lower_transport_send_pdu((mesh_pdu_t*) network_pdu);
597 }
598 
599 static void mesh_upper_transport_send_segmented_access_pdu_ccm(void * arg){
600     crypto_active = 0;
601 
602     mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg;
603     mesh_print_hex("EncAccessPayload", transport_pdu->data, transport_pdu->len);
604     // store TransMIC
605     btstack_crypto_ccm_get_authentication_value(&ccm, &transport_pdu->data[transport_pdu->len]);
606     mesh_print_hex("TransMIC", &transport_pdu->data[transport_pdu->len], transport_pdu->transmic_len);
607     transport_pdu->len += transport_pdu->transmic_len;
608     mesh_print_hex("UpperTransportPDU", transport_pdu->data, transport_pdu->len);
609     mesh_lower_transport_send_pdu((mesh_pdu_t*) transport_pdu);
610 }
611 
612 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,
613                           const uint8_t * control_pdu_data, uint16_t control_pdu_len){
614 
615     printf("[+] Upper transport, setup unsegmented Control PDU %p (opcode %02x): ", network_pdu, opcode);
616     printf_hexdump(control_pdu_data, control_pdu_len);
617 
618     if (control_pdu_len > 11) return 1;
619 
620     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
621     if (!network_key) return 1;
622 
623     uint8_t transport_pdu_data[12];
624     transport_pdu_data[0] = opcode;
625     memcpy(&transport_pdu_data[1], control_pdu_data, control_pdu_len);
626     uint16_t transport_pdu_len = control_pdu_len + 1;
627 
628     // setup network_pdu
629     mesh_network_setup_pdu(network_pdu, netkey_index, network_key->nid, 1, ttl, 0, src, dest, transport_pdu_data, transport_pdu_len);
630 
631     return 0;
632 }
633 
634 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,
635                           const uint8_t * control_pdu_data, uint16_t control_pdu_len){
636 
637     printf("[+] Upper transport, setup segmented Control PDU (opcode %02x): \n", opcode);
638     printf_hexdump(control_pdu_data, control_pdu_len);
639 
640     if (control_pdu_len > 256) return 1;
641 
642     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
643     if (!network_key) return 1;
644 
645     memcpy(transport_pdu->data, control_pdu_data, control_pdu_len);
646     transport_pdu->len = control_pdu_len;
647     transport_pdu->netkey_index = netkey_index;
648     transport_pdu->akf_aid_control = opcode;
649     transport_pdu->transmic_len = 0;    // no TransMIC for control
650     mesh_transport_set_nid_ivi(transport_pdu, network_key->nid);
651     mesh_transport_set_src(transport_pdu, src);
652     mesh_transport_set_dest(transport_pdu, dest);
653     mesh_transport_set_ctl_ttl(transport_pdu, 0x80 | ttl);
654 
655     return 0;
656 }
657 
658 uint8_t mesh_upper_transport_setup_control_pdu(mesh_pdu_t * pdu, uint16_t netkey_index,
659                                                uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode, const uint8_t * control_pdu_data, uint16_t control_pdu_len){
660     switch (pdu->pdu_type){
661         case MESH_PDU_TYPE_NETWORK:
662             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);
663         case MESH_PDU_TYPE_TRANSPORT:
664             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);
665         default:
666             return 1;
667     }
668 }
669 
670 static uint8_t mesh_upper_transport_setup_unsegmented_access_pdu_header(mesh_network_pdu_t * network_pdu, uint16_t netkey_index,
671         uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest){
672 
673     // get app or device key
674     const mesh_transport_key_t * appkey;
675     appkey = mesh_transport_key_get(appkey_index);
676     if (appkey == NULL){
677         printf("appkey_index %x unknown\n", appkey_index);
678         return 1;
679     }
680     uint8_t akf_aid = (appkey->akf << 6) | appkey->aid;
681 
682     // lookup network by netkey_index
683     const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index);
684     if (!network_key) return 1;
685 
686     // Nonce for Access Payload based on Network Sequence number: needs to be fixed now and lower layers need to send packet in right order
687     uint32_t seq = mesh_sequence_number_next();
688 
689     network_pdu->data[9] = akf_aid;
690     // setup network_pdu
691     mesh_network_setup_pdu_header(network_pdu, netkey_index, network_key->nid, 0, ttl, seq, src, dest);
692     network_pdu->appkey_index = appkey_index;
693     return 0;
694 }
695 
696 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,
697                                                        const uint8_t * access_pdu_data, uint8_t access_pdu_len){
698 
699     int status = mesh_upper_transport_setup_unsegmented_access_pdu_header(network_pdu, netkey_index, appkey_index, ttl, src, dest);
700     if (status) return status;
701 
702     printf("[+] Upper transport, setup unsegmented Access PDU - seq %06x\n", mesh_network_seq(network_pdu));
703     mesh_print_hex("Access Payload", access_pdu_data, access_pdu_len);
704 
705     // store in transport pdu
706     memcpy(&network_pdu->data[10], access_pdu_data, access_pdu_len);
707     network_pdu->len = 10 + access_pdu_len;
708     return 0;
709 }
710 
711 static uint8_t mesh_upper_transport_setup_segmented_access_pdu_header(mesh_transport_pdu_t * transport_pdu, uint16_t netkey_index,
712     uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){
713 
714     // get app or device key
715     const mesh_transport_key_t *appkey;
716     appkey = mesh_transport_key_get(appkey_index);
717     if (appkey == NULL) {
718         printf("[!] Upper transport, setup segmented Access PDU - appkey_index %x unknown\n", appkey_index);
719         return 1;
720     }
721     uint8_t akf_aid = (appkey->akf << 6) | appkey->aid;
722 
723     // lookup network by netkey_index
724     const mesh_network_key_t *network_key = mesh_network_key_list_get(netkey_index);
725     if (!network_key) return 1;
726     if (network_key == NULL) {
727         printf("[!] Upper transport, setup segmented Access PDU - netkey_index %x unknown\n", appkey_index);
728         return 1;
729     }
730 
731     const uint8_t trans_mic_len = szmic ? 8 : 4;
732 
733     // reserve one sequence number, which is also used to encrypt access payload
734     uint32_t seq = mesh_sequence_number_next();
735     transport_pdu->flags |= MESH_TRANSPORT_FLAG_SEQ_RESERVED;
736 
737     // store in transport pdu
738     transport_pdu->transmic_len = trans_mic_len;
739     transport_pdu->netkey_index = netkey_index;
740     transport_pdu->appkey_index = appkey_index;
741     transport_pdu->akf_aid_control = akf_aid;
742     mesh_transport_set_nid_ivi(transport_pdu, network_key->nid | ((mesh_get_iv_index_for_tx() & 1) << 7));
743     mesh_transport_set_seq(transport_pdu, seq);
744     mesh_transport_set_src(transport_pdu, src);
745     mesh_transport_set_dest(transport_pdu, dest);
746     mesh_transport_set_ctl_ttl(transport_pdu, ttl);
747     return 0;
748 }
749 
750 
751 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,
752                           uint8_t szmic, const uint8_t * access_pdu_data, uint8_t access_pdu_len){
753     int status = mesh_upper_transport_setup_segmented_access_pdu_header(transport_pdu, netkey_index, appkey_index, ttl, src, dest, szmic);
754     if (status) return status;
755 
756     // store in transport pdu
757     memcpy(transport_pdu->data, access_pdu_data, access_pdu_len);
758     transport_pdu->len = access_pdu_len;
759 
760     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());
761     mesh_print_hex("Access Payload", transport_pdu->data, transport_pdu->len);
762     return 0;
763 }
764 uint8_t mesh_upper_transport_setup_access_pdu_header(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index,
765                                               uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){
766     switch (pdu->pdu_type){
767         case MESH_PDU_TYPE_NETWORK:
768             return mesh_upper_transport_setup_unsegmented_access_pdu_header((mesh_network_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest);
769         case MESH_PDU_TYPE_TRANSPORT:
770             return mesh_upper_transport_setup_segmented_access_pdu_header((mesh_transport_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, szmic);
771         default:
772             return 1;
773     }
774 }
775 
776 uint8_t mesh_upper_transport_setup_access_pdu(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index,
777                                               uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic,
778                                               const uint8_t * access_pdu_data, uint8_t access_pdu_len){
779     switch (pdu->pdu_type){
780         case MESH_PDU_TYPE_NETWORK:
781             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);
782         case MESH_PDU_TYPE_TRANSPORT:
783             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);
784         default:
785             return 1;
786     }
787 }
788 
789 void mesh_upper_transport_send_control_pdu(mesh_pdu_t * pdu){
790     mesh_lower_transport_send_pdu((mesh_pdu_t*) pdu);
791 }
792 
793 static void mesh_upper_transport_send_unsegmented_access_pdu_digest(void * arg){
794     mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg;
795     uint8_t * access_pdu_data = mesh_network_pdu_data(network_pdu) + 1;
796     uint16_t  access_pdu_len  = mesh_network_pdu_len(network_pdu)  - 1;
797     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);
798 }
799 
800 static mesh_transport_key_t * mesh_upper_transport_get_outgoing_appkey(uint16_t netkey_index, uint16_t appkey_index){
801     // Device Key is fixed
802     if (appkey_index == MESH_DEVICE_KEY_INDEX) {
803         return mesh_transport_key_get(appkey_index);
804     }
805 
806     // Get key refresh state from subnet
807     mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index);
808     if (subnet == NULL) return NULL;
809 
810     // identify old and new app keys for given appkey_index
811     mesh_transport_key_t * old_key = NULL;
812     mesh_transport_key_t * new_key = NULL;
813     mesh_transport_key_iterator_t it;
814     mesh_transport_key_iterator_init(&it, netkey_index);
815     while (mesh_transport_key_iterator_has_more(&it)){
816         mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it);
817         if (transport_key->appkey_index != appkey_index) continue;
818         if (transport_key->old_key == 0) {
819             new_key = transport_key;
820         } else {
821             old_key = transport_key;
822         }
823     }
824 
825     // if no key is marked as old, just use the current one
826     if (old_key == NULL) return new_key;
827 
828     // use new key if it exists in phase two
829     if ((subnet->key_refresh == MESH_KEY_REFRESH_SECOND_PHASE) && (new_key != NULL)){
830         return new_key;
831     } else {
832         return old_key;
833     }
834 }
835 
836 static void mesh_upper_transport_send_unsegmented_access_pdu(mesh_network_pdu_t * network_pdu){
837 
838     // if dst is virtual address, lookup label uuid and hash
839     uint16_t aad_len = 0;
840     mesh_virtual_address_t * virtual_address = NULL;
841     uint16_t dst = mesh_network_dst(network_pdu);
842     if (mesh_network_address_virtual(dst)){
843         virtual_address = mesh_virtual_address_for_pseudo_dst(dst);
844         if (!virtual_address){
845             printf("No virtual address register for pseudo dst %4x\n", dst);
846             btstack_memory_mesh_network_pdu_free(network_pdu);
847             return;
848         }
849         aad_len = 16;
850         big_endian_store_16(network_pdu->data, 7, virtual_address->hash);
851     }
852 
853     // setup nonce
854     uint16_t appkey_index = network_pdu->appkey_index;
855     if (appkey_index == MESH_DEVICE_KEY_INDEX){
856         transport_unsegmented_setup_device_nonce(application_nonce, network_pdu);
857     } else {
858         transport_unsegmented_setup_application_nonce(application_nonce, network_pdu);
859     }
860 
861     // get app or device key
862     const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(network_pdu->netkey_index, appkey_index);
863     mesh_print_hex("AppOrDevKey", appkey->key, 16);
864 
865     // encrypt ccm
866     uint8_t   trans_mic_len = 4;
867     uint16_t  access_pdu_len  = mesh_network_pdu_len(network_pdu)  - 1;
868     crypto_active = 1;
869 
870     btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, trans_mic_len);
871     if (virtual_address){
872         mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16);
873         btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_unsegmented_access_pdu_digest, network_pdu);
874     } else {
875         mesh_upper_transport_send_unsegmented_access_pdu_digest(network_pdu);
876     }
877 }
878 
879 static void mesh_upper_transport_send_segmented_access_pdu_digest(void *arg){
880     mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg;
881     uint16_t  access_pdu_len  = transport_pdu->len;
882     uint8_t * access_pdu_data = transport_pdu->data;
883     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);
884 }
885 
886 static void mesh_upper_transport_send_segmented_access_pdu(mesh_transport_pdu_t * transport_pdu){
887 
888     // if dst is virtual address, lookup label uuid and hash
889     uint16_t aad_len = 0;
890     mesh_virtual_address_t * virtual_address = NULL;
891     uint16_t dst = mesh_transport_dst(transport_pdu);
892     if (mesh_network_address_virtual(dst)){
893         virtual_address = mesh_virtual_address_for_pseudo_dst(dst);
894         if (!virtual_address){
895             printf("No virtual address register for pseudo dst %4x\n", dst);
896             btstack_memory_mesh_transport_pdu_free(transport_pdu);
897             return;
898         }
899         // printf("Using hash %4x with LabelUUID: ", virtual_address->hash);
900         // printf_hexdump(virtual_address->label_uuid, 16);
901         aad_len = 16;
902         big_endian_store_16(transport_pdu->network_header, 7, virtual_address->hash);
903     }
904 
905     // setup nonce - uses dst, so after pseudo address translation
906     uint16_t appkey_index = transport_pdu->appkey_index;
907     if (appkey_index == MESH_DEVICE_KEY_INDEX){
908         transport_segmented_setup_device_nonce(application_nonce, transport_pdu);
909     } else {
910         transport_segmented_setup_application_nonce(application_nonce, transport_pdu);
911     }
912 
913     // get app or device key
914     const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(transport_pdu->netkey_index, appkey_index);
915     mesh_print_hex("AppOrDevKey", appkey->key, 16);
916 
917     // encrypt ccm
918     uint8_t   transmic_len    = transport_pdu->transmic_len;
919     uint16_t  access_pdu_len  = transport_pdu->len;
920     crypto_active = 1;
921     btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, transmic_len);
922     if (virtual_address){
923         mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16);
924         btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_segmented_access_pdu_digest, transport_pdu);
925     } else {
926         mesh_upper_transport_send_segmented_access_pdu_digest(transport_pdu);
927     }
928 }
929 
930 static void mesh_upper_transport_run(void){
931     while(!btstack_linked_list_empty(&upper_transport_incoming)){
932 
933         if (crypto_active) return;
934 
935         // peek at next message
936         mesh_pdu_t * pdu =  (mesh_pdu_t *) btstack_linked_list_get_first_item(&upper_transport_incoming);
937         mesh_transport_pdu_t * transport_pdu;
938         mesh_network_pdu_t   * network_pdu;
939         switch (pdu->pdu_type){
940             case MESH_PDU_TYPE_NETWORK:
941                 network_pdu = (mesh_network_pdu_t *) pdu;
942                 // control?
943                 if (mesh_network_control(network_pdu)) {
944                     (void) btstack_linked_list_pop(&upper_transport_incoming);
945                     mesh_upper_unsegmented_control_message_received(network_pdu);
946                 } else {
947                     incoming_network_pdu_decoded = mesh_network_pdu_get();
948                     if (!incoming_network_pdu_decoded) return;
949                     // get encoded network pdu and start processing
950                     incoming_network_pdu_raw = network_pdu;
951                     (void) btstack_linked_list_pop(&upper_transport_incoming);
952                     mesh_upper_transport_process_unsegmented_access_message();
953                 }
954                 break;
955             case MESH_PDU_TYPE_TRANSPORT:
956                 transport_pdu = (mesh_transport_pdu_t *) pdu;
957                 uint8_t ctl = mesh_transport_ctl(transport_pdu);
958                 if (ctl){
959                     printf("Ignoring Segmented Control Message\n");
960                     (void) btstack_linked_list_pop(&upper_transport_incoming);
961                     mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) transport_pdu);
962                 } else {
963                     incoming_transport_pdu_decoded = mesh_transport_pdu_get();
964                     if (!incoming_transport_pdu_decoded) return;
965                     // get encoded transport pdu and start processing
966                     incoming_transport_pdu_raw = transport_pdu;
967                     (void) btstack_linked_list_pop(&upper_transport_incoming);
968                     mesh_upper_transport_process_message();
969                 }
970                 break;
971             default:
972                 break;
973         }
974     }
975 
976     while (!btstack_linked_list_empty(&upper_transport_outgoing)){
977 
978         if (crypto_active) return;
979 
980         mesh_pdu_t * pdu =  (mesh_pdu_t *) btstack_linked_list_pop(&upper_transport_outgoing);
981         switch (pdu->pdu_type){
982             case MESH_PDU_TYPE_NETWORK:
983                 mesh_upper_transport_send_unsegmented_access_pdu((mesh_network_pdu_t *) pdu);
984                 break;
985             case MESH_PDU_TYPE_TRANSPORT:
986                 mesh_upper_transport_send_segmented_access_pdu((mesh_transport_pdu_t *) pdu);
987                 break;
988             default:
989                 break;
990         }
991     }
992 }
993 
994 void mesh_upper_transport_register_access_message_handler(void (*callback)(mesh_pdu_t *pdu)){
995     mesh_access_message_handler = callback;
996 }
997 
998 void mesh_upper_transport_register_control_message_handler(void (*callback)(mesh_pdu_t *pdu)){
999     mesh_control_message_handler = callback;
1000 }
1001 
1002 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)){
1003     higher_layer_handler = pdu_handler;
1004 }
1005 
1006 void mesh_upper_transport_init(){
1007     mesh_lower_transport_set_higher_layer_handler(&mesh_upper_transport_pdu_handler);
1008 }
1009 
1010 void mesh_upper_transport_send_access_pdu(mesh_pdu_t *pdu){
1011     if (pdu->pdu_type == MESH_PDU_TYPE_NETWORK){
1012         mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) pdu;
1013         btstack_assert(network_pdu->len >= 9);
1014     }
1015 
1016     btstack_linked_list_add_tail(&upper_transport_outgoing, (btstack_linked_item_t*) pdu);
1017     mesh_upper_transport_run();
1018 }
1019