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