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