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