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_peek(); 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, uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, 706 uint8_t szmic){ 707 uint32_t seq = mesh_sequence_number_peek(); 708 709 printf("[+] Upper transport, setup segmented Access PDU - seq %06x, szmic %u, iv_index %08x\n", seq, szmic, 710 mesh_get_iv_index_for_tx()); 711 mesh_print_hex("Access Payload", transport_pdu->data, transport_pdu->len); 712 713 // get app or device key 714 const mesh_transport_key_t *appkey; 715 appkey = mesh_transport_key_get(appkey_index); 716 if (appkey == NULL) { 717 printf("appkey_index %x unknown\n", appkey_index); 718 return 1; 719 } 720 uint8_t akf_aid = (appkey->akf << 6) | appkey->aid; 721 722 // lookup network by netkey_index 723 const mesh_network_key_t *network_key = mesh_network_key_list_get(netkey_index); 724 if (!network_key) return 1; 725 726 const uint8_t trans_mic_len = szmic ? 8 : 4; 727 728 // store in transport pdu 729 transport_pdu->transmic_len = trans_mic_len; 730 transport_pdu->netkey_index = netkey_index; 731 transport_pdu->appkey_index = appkey_index; 732 transport_pdu->akf_aid_control = akf_aid; 733 mesh_transport_set_nid_ivi(transport_pdu, network_key->nid | ((mesh_get_iv_index_for_tx() & 1) << 7)); 734 mesh_transport_set_seq(transport_pdu, seq); 735 mesh_transport_set_src(transport_pdu, src); 736 mesh_transport_set_dest(transport_pdu, dest); 737 mesh_transport_set_ctl_ttl(transport_pdu, ttl); 738 return 0; 739 } 740 741 742 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, 743 uint8_t szmic, const uint8_t * access_pdu_data, uint8_t access_pdu_len){ 744 int status = mesh_upper_transport_setup_segmented_access_pdu_header(transport_pdu, netkey_index, appkey_index, ttl, src, dest, szmic); 745 if (status) return status; 746 747 // store in transport pdu 748 memcpy(transport_pdu->data, access_pdu_data, access_pdu_len); 749 transport_pdu->len = access_pdu_len; 750 return 0; 751 } 752 uint8_t mesh_upper_transport_setup_access_pdu_header(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index, 753 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){ 754 switch (pdu->pdu_type){ 755 case MESH_PDU_TYPE_NETWORK: 756 return mesh_upper_transport_setup_unsegmented_access_pdu_header((mesh_network_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest); 757 case MESH_PDU_TYPE_TRANSPORT: 758 return mesh_upper_transport_setup_segmented_access_pdu_header((mesh_transport_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, szmic); 759 default: 760 return 1; 761 } 762 } 763 764 uint8_t mesh_upper_transport_setup_access_pdu(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index, 765 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic, 766 const uint8_t * access_pdu_data, uint8_t access_pdu_len){ 767 switch (pdu->pdu_type){ 768 case MESH_PDU_TYPE_NETWORK: 769 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); 770 case MESH_PDU_TYPE_TRANSPORT: 771 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); 772 default: 773 return 1; 774 } 775 } 776 777 void mesh_upper_transport_send_control_pdu(mesh_pdu_t * pdu){ 778 mesh_lower_transport_send_pdu((mesh_pdu_t*) pdu); 779 } 780 781 static void mesh_upper_transport_send_unsegmented_access_pdu_digest(void * arg){ 782 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg; 783 uint8_t * access_pdu_data = mesh_network_pdu_data(network_pdu) + 1; 784 uint16_t access_pdu_len = mesh_network_pdu_len(network_pdu) - 1; 785 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); 786 } 787 788 static mesh_transport_key_t * mesh_upper_transport_get_outgoing_appkey(uint16_t netkey_index, uint16_t appkey_index){ 789 // Device Key is fixed 790 if (appkey_index == MESH_DEVICE_KEY_INDEX) { 791 return mesh_transport_key_get(appkey_index); 792 } 793 794 // Get key refresh state from subnet 795 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); 796 if (subnet == NULL) return NULL; 797 798 // identify old and new app keys for given appkey_index 799 mesh_transport_key_t * old_key = NULL; 800 mesh_transport_key_t * new_key = NULL; 801 mesh_transport_key_iterator_t it; 802 mesh_transport_key_iterator_init(&it, netkey_index); 803 while (mesh_transport_key_iterator_has_more(&it)){ 804 mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it); 805 if (transport_key->appkey_index != appkey_index) continue; 806 if (transport_key->old_key == 0) { 807 new_key = transport_key; 808 } else { 809 old_key = transport_key; 810 } 811 } 812 813 // if no key is marked as old, just use the current one 814 if (old_key == NULL) return new_key; 815 816 // use new key if it exists in phase two 817 if ((subnet->key_refresh == MESH_KEY_REFRESH_SECOND_PHASE) && (new_key != NULL)){ 818 return new_key; 819 } else { 820 return old_key; 821 } 822 } 823 824 static void mesh_upper_transport_send_unsegmented_access_pdu(mesh_network_pdu_t * network_pdu){ 825 826 // if dst is virtual address, lookup label uuid and hash 827 uint16_t aad_len = 0; 828 mesh_virtual_address_t * virtual_address = NULL; 829 uint16_t dst = mesh_network_dst(network_pdu); 830 if (mesh_network_address_virtual(dst)){ 831 virtual_address = mesh_virtual_address_for_pseudo_dst(dst); 832 if (!virtual_address){ 833 printf("No virtual address register for pseudo dst %4x\n", dst); 834 btstack_memory_mesh_network_pdu_free(network_pdu); 835 return; 836 } 837 aad_len = 16; 838 big_endian_store_16(network_pdu->data, 7, virtual_address->hash); 839 } 840 841 // setup nonce 842 uint16_t appkey_index = network_pdu->appkey_index; 843 if (appkey_index == MESH_DEVICE_KEY_INDEX){ 844 transport_unsegmented_setup_device_nonce(application_nonce, network_pdu); 845 } else { 846 transport_unsegmented_setup_application_nonce(application_nonce, network_pdu); 847 } 848 849 // get app or device key 850 const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(network_pdu->netkey_index, appkey_index); 851 mesh_print_hex("AppOrDevKey", appkey->key, 16); 852 853 // encrypt ccm 854 uint8_t trans_mic_len = 4; 855 uint16_t access_pdu_len = mesh_network_pdu_len(network_pdu) - 1; 856 crypto_active = 1; 857 858 btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, trans_mic_len); 859 if (virtual_address){ 860 mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16); 861 btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_unsegmented_access_pdu_digest, network_pdu); 862 } else { 863 mesh_upper_transport_send_unsegmented_access_pdu_digest(network_pdu); 864 } 865 } 866 867 static void mesh_upper_transport_send_segmented_access_pdu_digest(void *arg){ 868 mesh_transport_pdu_t * transport_pdu = (mesh_transport_pdu_t *) arg; 869 uint16_t access_pdu_len = transport_pdu->len; 870 uint8_t * access_pdu_data = transport_pdu->data; 871 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); 872 } 873 874 static void mesh_upper_transport_send_segmented_access_pdu(mesh_transport_pdu_t * transport_pdu){ 875 876 // if dst is virtual address, lookup label uuid and hash 877 uint16_t aad_len = 0; 878 mesh_virtual_address_t * virtual_address = NULL; 879 uint16_t dst = mesh_transport_dst(transport_pdu); 880 if (mesh_network_address_virtual(dst)){ 881 virtual_address = mesh_virtual_address_for_pseudo_dst(dst); 882 if (!virtual_address){ 883 printf("No virtual address register for pseudo dst %4x\n", dst); 884 btstack_memory_mesh_transport_pdu_free(transport_pdu); 885 return; 886 } 887 // printf("Using hash %4x with LabelUUID: ", virtual_address->hash); 888 // printf_hexdump(virtual_address->label_uuid, 16); 889 aad_len = 16; 890 big_endian_store_16(transport_pdu->network_header, 7, virtual_address->hash); 891 } 892 893 // setup nonce - uses dst, so after pseudo address translation 894 uint16_t appkey_index = transport_pdu->appkey_index; 895 if (appkey_index == MESH_DEVICE_KEY_INDEX){ 896 transport_segmented_setup_device_nonce(application_nonce, transport_pdu); 897 } else { 898 transport_segmented_setup_application_nonce(application_nonce, transport_pdu); 899 } 900 901 // get app or device key 902 const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(transport_pdu->netkey_index, appkey_index); 903 mesh_print_hex("AppOrDevKey", appkey->key, 16); 904 905 // encrypt ccm 906 uint8_t transmic_len = transport_pdu->transmic_len; 907 uint16_t access_pdu_len = transport_pdu->len; 908 crypto_active = 1; 909 btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, transmic_len); 910 if (virtual_address){ 911 mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16); 912 btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, &mesh_upper_transport_send_segmented_access_pdu_digest, transport_pdu); 913 } else { 914 mesh_upper_transport_send_segmented_access_pdu_digest(transport_pdu); 915 } 916 } 917 918 void mesh_upper_transport_send_access_pdu(mesh_pdu_t * pdu){ 919 switch (pdu->pdu_type){ 920 case MESH_PDU_TYPE_NETWORK: 921 mesh_upper_transport_send_unsegmented_access_pdu((mesh_network_pdu_t *) pdu); 922 break; 923 case MESH_PDU_TYPE_TRANSPORT: 924 mesh_upper_transport_send_segmented_access_pdu((mesh_transport_pdu_t *) pdu); 925 break; 926 default: 927 break; 928 } 929 } 930 931 void mesh_upper_transport_register_access_message_handler(void (*callback)(mesh_pdu_t *pdu)){ 932 mesh_access_message_handler = callback; 933 } 934 935 void mesh_upper_transport_register_control_message_handler(void (*callback)(mesh_pdu_t *pdu)){ 936 mesh_control_message_handler = callback; 937 } 938 939 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)){ 940 higher_layer_handler = pdu_handler; 941 } 942 943 void mesh_upper_transport_init(){ 944 mesh_lower_transport_set_higher_layer_handler(&mesh_upper_transport_pdu_handler); 945 } 946 947 static void mesh_transport_run(void){ 948 while(!btstack_linked_list_empty(&upper_transport_incoming)){ 949 950 if (crypto_active) return; 951 952 // peek at next message 953 mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_get_first_item(&upper_transport_incoming); 954 mesh_transport_pdu_t * transport_pdu; 955 mesh_network_pdu_t * network_pdu; 956 switch (pdu->pdu_type){ 957 case MESH_PDU_TYPE_NETWORK: 958 network_pdu = (mesh_network_pdu_t *) pdu; 959 // control? 960 if (mesh_network_control(network_pdu)) { 961 (void) btstack_linked_list_pop(&upper_transport_incoming); 962 mesh_upper_unsegmented_control_message_received(network_pdu); 963 } else { 964 incoming_network_pdu_decoded = mesh_network_pdu_get(); 965 if (!incoming_network_pdu_decoded) return; 966 // get encoded network pdu and start processing 967 incoming_network_pdu_raw = network_pdu; 968 (void) btstack_linked_list_pop(&upper_transport_incoming); 969 mesh_upper_transport_process_unsegmented_access_message(); 970 } 971 break; 972 case MESH_PDU_TYPE_TRANSPORT: 973 transport_pdu = (mesh_transport_pdu_t *) pdu; 974 uint8_t ctl = mesh_transport_ctl(transport_pdu); 975 if (ctl){ 976 printf("Ignoring Segmented Control Message\n"); 977 (void) btstack_linked_list_pop(&upper_transport_incoming); 978 mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *) transport_pdu); 979 } else { 980 incoming_transport_pdu_decoded = mesh_transport_pdu_get(); 981 if (!incoming_transport_pdu_decoded) return; 982 // get encoded transport pdu and start processing 983 incoming_transport_pdu_raw = transport_pdu; 984 (void) btstack_linked_list_pop(&upper_transport_incoming); 985 mesh_upper_transport_process_message(); 986 } 987 break; 988 default: 989 break; 990 } 991 } 992 } 993