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