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