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