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 #include <stdarg.h> 39 #include "btstack_tlv.h" 40 #include "mesh/mesh_foundation.h" 41 #include "mesh_upper_transport.h" 42 #include "mesh/mesh.h" 43 #include "mesh/mesh_proxy.h" 44 #include "mesh/mesh_node.h" 45 46 #define BTSTACK_FILE__ "mesh_upper_transport.c" 47 48 #include "mesh/mesh_upper_transport.h" 49 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 54 #include "btstack_util.h" 55 #include "btstack_memory.h" 56 #include "btstack_debug.h" 57 #include "btstack_bool.h" 58 59 #include "mesh/beacon.h" 60 #include "mesh/mesh_iv_index_seq_number.h" 61 #include "mesh/mesh_keys.h" 62 #include "mesh/mesh_lower_transport.h" 63 #include "mesh/mesh_peer.h" 64 #include "mesh/mesh_virtual_addresses.h" 65 66 // TODO: extract mesh_pdu functions into lower transport or network 67 #include "mesh/mesh_access.h" 68 69 // combined key x address iterator for upper transport decryption 70 71 typedef struct { 72 // state 73 mesh_transport_key_iterator_t key_it; 74 mesh_virtual_address_iterator_t address_it; 75 // elements 76 const mesh_transport_key_t * key; 77 const mesh_virtual_address_t * address; 78 // address - might be virtual 79 uint16_t dst; 80 // key info 81 } mesh_transport_key_and_virtual_address_iterator_t; 82 83 static void mesh_upper_transport_validate_access_message(void); 84 static void mesh_upper_transport_run(void); 85 86 // upper transport callbacks - in access layer 87 static void (*mesh_access_message_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu); 88 static void (*mesh_control_message_handler)( mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu); 89 90 // 91 static int crypto_active; 92 static uint8_t application_nonce[13]; 93 static btstack_crypto_ccm_t ccm; 94 static mesh_transport_key_and_virtual_address_iterator_t mesh_transport_key_it; 95 96 // incoming segmented (mesh_segmented_pdu_t) or unsegmented (network_pdu_t) 97 static mesh_pdu_t * incoming_access_encrypted; 98 99 // multi-purpose union: segmented control reassembly, decrypted access pdu 100 static union { 101 mesh_control_pdu_t control; 102 mesh_access_pdu_t access; 103 } incoming_pdu_singleton; 104 105 // pointer to incoming_pdu_singleton.access 106 static mesh_access_pdu_t * incoming_access_decrypted; 107 108 // pointer to incoming_pdu_singleton.access 109 static mesh_control_pdu_t * incoming_control_pdu; 110 111 // incoming unsegmented (network) and segmented (transport) control and access messages 112 static btstack_linked_list_t upper_transport_incoming; 113 114 // outgoing unsegmented and segmented control and access messages 115 static btstack_linked_list_t upper_transport_outgoing; 116 117 // outgoing upper transport messages that have been sent to lower transport and wait for sent event 118 static btstack_linked_list_t upper_transport_outgoing_active; 119 120 // TODO: higher layer define used for assert 121 #define MESH_ACCESS_OPCODE_NOT_SET 0xFFFFFFFEu 122 123 static void mesh_print_hex(const char * name, const uint8_t * data, uint16_t len){ 124 printf("%-20s ", name); 125 printf_hexdump(data, len); 126 } 127 // static void mesh_print_x(const char * name, uint32_t value){ 128 // printf("%20s: 0x%x", name, (int) value); 129 // } 130 131 static void mesh_transport_key_and_virtual_address_iterator_init(mesh_transport_key_and_virtual_address_iterator_t *it, 132 uint16_t dst, uint16_t netkey_index, uint8_t akf, 133 uint8_t aid) { 134 printf("KEY_INIT: dst %04x, akf %x, aid %x\n", dst, akf, aid); 135 // config 136 it->dst = dst; 137 // init elements 138 it->key = NULL; 139 it->address = NULL; 140 // init element iterators 141 mesh_transport_key_aid_iterator_init(&it->key_it, netkey_index, akf, aid); 142 // init address iterator 143 if (mesh_network_address_virtual(it->dst)){ 144 mesh_virtual_address_iterator_init(&it->address_it, dst); 145 // get first key 146 if (mesh_transport_key_aid_iterator_has_more(&it->key_it)) { 147 it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it); 148 } 149 } 150 } 151 152 // cartesian product: keys x addressses 153 static int mesh_transport_key_and_virtual_address_iterator_has_more(mesh_transport_key_and_virtual_address_iterator_t * it){ 154 if (mesh_network_address_virtual(it->dst)) { 155 // find next valid entry 156 while (true){ 157 if (mesh_virtual_address_iterator_has_more(&it->address_it)) return 1; 158 if (!mesh_transport_key_aid_iterator_has_more(&it->key_it)) return 0; 159 // get next key 160 it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it); 161 mesh_virtual_address_iterator_init(&it->address_it, it->dst); 162 } 163 } else { 164 return mesh_transport_key_aid_iterator_has_more(&it->key_it); 165 } 166 } 167 168 static void mesh_transport_key_and_virtual_address_iterator_next(mesh_transport_key_and_virtual_address_iterator_t * it){ 169 if (mesh_network_address_virtual(it->dst)) { 170 it->address = mesh_virtual_address_iterator_get_next(&it->address_it); 171 } else { 172 it->key = mesh_transport_key_aid_iterator_get_next(&it->key_it); 173 } 174 } 175 176 // UPPER TRANSPORT 177 178 static void mesh_segmented_pdu_flatten(btstack_linked_list_t * segments, uint8_t segment_len, uint8_t * buffer) { 179 // assemble payload 180 btstack_linked_list_iterator_t it; 181 btstack_linked_list_iterator_init(&it, segments); 182 while (btstack_linked_list_iterator_has_next(&it)) { 183 mesh_network_pdu_t *segment = (mesh_network_pdu_t *) btstack_linked_list_iterator_next(&it); 184 btstack_assert(segment->pdu_header.pdu_type == MESH_PDU_TYPE_NETWORK); 185 // get segment n 186 uint8_t *lower_transport_pdu = mesh_network_pdu_data(segment); 187 uint8_t seg_o = (big_endian_read_16(lower_transport_pdu, 2) >> 5) & 0x001f; 188 uint8_t *segment_data = &lower_transport_pdu[4]; 189 (void) memcpy(&buffer[seg_o * segment_len], segment_data, segment_len); 190 } 191 } 192 193 static uint16_t mesh_upper_pdu_flatten(mesh_upper_transport_pdu_t * upper_pdu, uint8_t * buffer, uint16_t buffer_len) { 194 // assemble payload 195 btstack_linked_list_iterator_t it; 196 btstack_linked_list_iterator_init(&it, &upper_pdu->segments); 197 uint16_t offset = 0; 198 while (btstack_linked_list_iterator_has_next(&it)) { 199 mesh_network_pdu_t *segment = (mesh_network_pdu_t *) btstack_linked_list_iterator_next(&it); 200 btstack_assert(segment->pdu_header.pdu_type == MESH_PDU_TYPE_NETWORK); 201 btstack_assert((offset + segment->len) <= buffer_len); 202 (void) memcpy(&buffer[offset], segment->data, segment->len); 203 offset += segment->len; 204 } 205 return offset; 206 } 207 208 // store payload in provided list of network pdus 209 static void mesh_segmented_store_payload(const uint8_t * payload, uint16_t payload_len, btstack_linked_list_t * in_segments, btstack_linked_list_t * out_segments){ 210 uint16_t payload_offset = 0; 211 uint16_t bytes_current_segment = 0; 212 mesh_network_pdu_t * network_pdu = NULL; 213 while (payload_offset < payload_len){ 214 if (bytes_current_segment == 0){ 215 network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(in_segments); 216 btstack_assert(network_pdu != NULL); 217 btstack_linked_list_add_tail(out_segments, (btstack_linked_item_t *) network_pdu); 218 bytes_current_segment = MESH_NETWORK_PAYLOAD_MAX; 219 } 220 uint16_t bytes_to_copy = btstack_min(bytes_current_segment, payload_len - payload_offset); 221 (void) memcpy(&network_pdu->data[network_pdu->len], &payload[payload_offset], bytes_to_copy); 222 bytes_current_segment -= bytes_to_copy; 223 network_pdu->len += bytes_to_copy; 224 payload_offset += bytes_to_copy; 225 } 226 } 227 228 // tries allocate and add enough segments to store payload of given size 229 static bool mesh_segmented_allocate_segments(btstack_linked_list_t * segments, uint16_t payload_len){ 230 uint16_t storage_size = btstack_linked_list_count(segments) * MESH_NETWORK_PAYLOAD_MAX; 231 while (storage_size < payload_len){ 232 mesh_network_pdu_t * network_pdu = mesh_network_pdu_get(); 233 if (network_pdu == NULL) break; 234 storage_size += MESH_NETWORK_PAYLOAD_MAX; 235 btstack_linked_list_add(segments, (btstack_linked_item_t *) network_pdu); 236 } 237 return (storage_size >= payload_len); 238 } 239 240 // stub lower transport 241 242 static void mesh_upper_transport_dump_pdus(const char *name, btstack_linked_list_t *list){ 243 printf("List: %s:\n", name); 244 btstack_linked_list_iterator_t it; 245 btstack_linked_list_iterator_init(&it, list); 246 while (btstack_linked_list_iterator_has_next(&it)){ 247 mesh_pdu_t * pdu = (mesh_pdu_t*) btstack_linked_list_iterator_next(&it); 248 printf("- %p\n", pdu); 249 // printf_hexdump( mesh_pdu_data(pdu), mesh_pdu_len(pdu)); 250 } 251 } 252 253 static void mesh_upper_transport_reset_pdus(btstack_linked_list_t *list){ 254 while (!btstack_linked_list_empty(list)){ 255 mesh_upper_transport_pdu_free((mesh_pdu_t *) btstack_linked_list_pop(list)); 256 } 257 } 258 259 void mesh_upper_transport_dump(void){ 260 mesh_upper_transport_dump_pdus("upper_transport_incoming", &upper_transport_incoming); 261 } 262 263 void mesh_upper_transport_reset(void){ 264 crypto_active = 0; 265 mesh_upper_transport_reset_pdus(&upper_transport_incoming); 266 } 267 268 static mesh_transport_key_t * mesh_upper_transport_get_outgoing_appkey(uint16_t netkey_index, uint16_t appkey_index){ 269 // Device Key is fixed 270 if (appkey_index == MESH_DEVICE_KEY_INDEX) { 271 return mesh_transport_key_get(appkey_index); 272 } 273 274 // Get key refresh state from subnet 275 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); 276 if (subnet == NULL) return NULL; 277 278 // identify old and new app keys for given appkey_index 279 mesh_transport_key_t * old_key = NULL; 280 mesh_transport_key_t * new_key = NULL; 281 mesh_transport_key_iterator_t it; 282 mesh_transport_key_iterator_init(&it, netkey_index); 283 while (mesh_transport_key_iterator_has_more(&it)){ 284 mesh_transport_key_t * transport_key = mesh_transport_key_iterator_get_next(&it); 285 if (transport_key->appkey_index != appkey_index) continue; 286 if (transport_key->old_key == 0) { 287 new_key = transport_key; 288 } else { 289 old_key = transport_key; 290 } 291 } 292 293 // if no key is marked as old, just use the current one 294 if (old_key == NULL) return new_key; 295 296 // use new key if it exists in phase two 297 if ((subnet->key_refresh == MESH_KEY_REFRESH_SECOND_PHASE) && (new_key != NULL)){ 298 return new_key; 299 } else { 300 return old_key; 301 } 302 } 303 304 static uint32_t iv_index_for_ivi_nid(uint8_t ivi_nid){ 305 // get IV Index and IVI 306 uint32_t iv_index = mesh_get_iv_index(); 307 int ivi = ivi_nid >> 7; 308 309 // if least significant bit differs, use previous IV Index 310 if ((iv_index & 1 ) ^ ivi){ 311 iv_index--; 312 } 313 return iv_index; 314 } 315 316 static void transport_segmented_setup_nonce(uint8_t * nonce, const mesh_pdu_t * pdu){ 317 mesh_access_pdu_t * access_pdu; 318 mesh_upper_transport_pdu_t * upper_pdu; 319 switch (pdu->pdu_type){ 320 case MESH_PDU_TYPE_ACCESS: 321 access_pdu = (mesh_access_pdu_t *) pdu; 322 nonce[1] = access_pdu->transmic_len == 8 ? 0x80 : 0x00; 323 big_endian_store_24(nonce, 2, access_pdu->seq); 324 big_endian_store_16(nonce, 5, access_pdu->src); 325 big_endian_store_16(nonce, 7, access_pdu->dst); 326 big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(access_pdu->ivi_nid)); 327 break; 328 case MESH_PDU_TYPE_UPPER_SEGMENTED_ACCESS: 329 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS: 330 upper_pdu = (mesh_upper_transport_pdu_t *) pdu; 331 nonce[1] = upper_pdu->transmic_len == 8 ? 0x80 : 0x00; 332 // 'network header' 333 big_endian_store_24(nonce, 2, upper_pdu->seq); 334 big_endian_store_16(nonce, 5, upper_pdu->src); 335 big_endian_store_16(nonce, 7, upper_pdu->dst); 336 big_endian_store_32(nonce, 9, iv_index_for_ivi_nid(upper_pdu->ivi_nid)); 337 break; 338 default: 339 btstack_assert(0); 340 break; 341 } 342 } 343 344 static void transport_segmented_setup_application_nonce(uint8_t * nonce, const mesh_pdu_t * pdu){ 345 nonce[0] = 0x01; 346 transport_segmented_setup_nonce(nonce, pdu); 347 mesh_print_hex("AppNonce", nonce, 13); 348 } 349 350 static void transport_segmented_setup_device_nonce(uint8_t * nonce, const mesh_pdu_t * pdu){ 351 nonce[0] = 0x02; 352 transport_segmented_setup_nonce(nonce, pdu); 353 mesh_print_hex("DeviceNonce", nonce, 13); 354 } 355 356 static void mesh_upper_transport_process_access_message_done(mesh_access_pdu_t *access_pdu){ 357 crypto_active = 0; 358 btstack_assert((access_pdu->ctl_ttl & 0x80) == 0); 359 mesh_lower_transport_message_processed_by_higher_layer(incoming_access_encrypted); 360 incoming_access_encrypted = NULL; 361 incoming_access_decrypted = NULL; 362 mesh_upper_transport_run(); 363 } 364 365 static void mesh_upper_transport_process_control_message_done(mesh_control_pdu_t * control_pdu){ 366 crypto_active = 0; 367 incoming_control_pdu = NULL; 368 mesh_upper_transport_run(); 369 } 370 371 static void mesh_upper_transport_validate_access_message_ccm(void * arg){ 372 UNUSED(arg); 373 374 uint8_t * upper_transport_pdu = incoming_access_decrypted->data; 375 uint8_t upper_transport_pdu_len = incoming_access_decrypted->len - incoming_access_decrypted->transmic_len; 376 377 mesh_print_hex("Decrypted PDU", upper_transport_pdu, upper_transport_pdu_len); 378 379 // store TransMIC 380 uint8_t trans_mic[8]; 381 btstack_crypto_ccm_get_authentication_value(&ccm, trans_mic); 382 mesh_print_hex("TransMIC", trans_mic, incoming_access_decrypted->transmic_len); 383 384 if (memcmp(trans_mic, &upper_transport_pdu[upper_transport_pdu_len], incoming_access_decrypted->transmic_len) == 0){ 385 printf("TransMIC matches\n"); 386 387 // remove TransMIC from payload 388 incoming_access_decrypted->len -= incoming_access_decrypted->transmic_len; 389 390 // if virtual address, update dst to pseudo_dst 391 if (mesh_network_address_virtual(incoming_access_decrypted->dst)){ 392 incoming_access_decrypted->dst = mesh_transport_key_it.address->pseudo_dst; 393 } 394 395 // pass to upper layer 396 btstack_assert(mesh_access_message_handler != NULL); 397 mesh_pdu_t * pdu = (mesh_pdu_t*) incoming_access_decrypted; 398 mesh_access_message_handler(MESH_TRANSPORT_PDU_RECEIVED, MESH_TRANSPORT_STATUS_SUCCESS, pdu); 399 400 printf("\n"); 401 402 } else { 403 uint8_t akf = incoming_access_decrypted->akf_aid_control & 0x40; 404 if (akf){ 405 printf("TransMIC does not match, try next key\n"); 406 mesh_upper_transport_validate_access_message(); 407 } else { 408 printf("TransMIC does not match device key, done\n"); 409 // done 410 mesh_upper_transport_process_access_message_done(incoming_access_decrypted); 411 } 412 } 413 } 414 415 static void mesh_upper_transport_validate_access_message_digest(void * arg){ 416 UNUSED(arg); 417 uint8_t upper_transport_pdu_len = incoming_access_decrypted->len - incoming_access_decrypted->transmic_len; 418 uint8_t * upper_transport_pdu_data_in = incoming_access_decrypted->data; 419 uint8_t * upper_transport_pdu_data_out = incoming_access_decrypted->data; 420 421 mesh_network_pdu_t * unsegmented_pdu = NULL; 422 mesh_segmented_pdu_t * segmented_pdu = NULL; 423 switch (incoming_access_encrypted->pdu_type){ 424 case MESH_PDU_TYPE_SEGMENTED: 425 segmented_pdu = (mesh_segmented_pdu_t *) incoming_access_encrypted; 426 mesh_segmented_pdu_flatten(&segmented_pdu->segments, 12, upper_transport_pdu_data_out); 427 btstack_crypto_ccm_decrypt_block(&ccm, upper_transport_pdu_len, upper_transport_pdu_data_out, upper_transport_pdu_data_out, 428 &mesh_upper_transport_validate_access_message_ccm, NULL); 429 break; 430 case MESH_PDU_TYPE_UNSEGMENTED: 431 unsegmented_pdu = (mesh_network_pdu_t *) incoming_access_encrypted; 432 (void)memcpy(upper_transport_pdu_data_out, &unsegmented_pdu->data[10], incoming_access_decrypted->len); 433 btstack_crypto_ccm_decrypt_block(&ccm, upper_transport_pdu_len, upper_transport_pdu_data_out, upper_transport_pdu_data_out, 434 &mesh_upper_transport_validate_access_message_ccm, NULL); 435 break; 436 default: 437 btstack_assert(false); 438 break; 439 } 440 441 } 442 443 static void mesh_upper_transport_validate_access_message(void){ 444 uint8_t * upper_transport_pdu_data = incoming_access_decrypted->data; 445 uint8_t upper_transport_pdu_len = incoming_access_decrypted->len - incoming_access_decrypted->transmic_len; 446 447 if (!mesh_transport_key_and_virtual_address_iterator_has_more(&mesh_transport_key_it)){ 448 printf("No valid transport key found\n"); 449 mesh_upper_transport_process_access_message_done(incoming_access_decrypted); 450 return; 451 } 452 mesh_transport_key_and_virtual_address_iterator_next(&mesh_transport_key_it); 453 const mesh_transport_key_t * message_key = mesh_transport_key_it.key; 454 455 if (message_key->akf){ 456 transport_segmented_setup_application_nonce(application_nonce, (mesh_pdu_t *) incoming_access_decrypted); 457 } else { 458 transport_segmented_setup_device_nonce(application_nonce, (mesh_pdu_t *) incoming_access_decrypted); 459 } 460 461 // store application / device key index 462 mesh_print_hex("AppOrDevKey", message_key->key, 16); 463 incoming_access_decrypted->appkey_index = message_key->appkey_index; 464 465 mesh_print_hex("EncAccessPayload", upper_transport_pdu_data, upper_transport_pdu_len); 466 467 // decrypt ccm 468 crypto_active = 1; 469 uint16_t aad_len = 0; 470 if (mesh_network_address_virtual(incoming_access_decrypted->dst)){ 471 aad_len = 16; 472 } 473 btstack_crypto_ccm_init(&ccm, message_key->key, application_nonce, upper_transport_pdu_len, aad_len, incoming_access_decrypted->transmic_len); 474 475 if (aad_len){ 476 btstack_crypto_ccm_digest(&ccm, (uint8_t *) mesh_transport_key_it.address->label_uuid, aad_len, 477 &mesh_upper_transport_validate_access_message_digest, NULL); 478 } else { 479 mesh_upper_transport_validate_access_message_digest(NULL); 480 } 481 } 482 483 static void mesh_upper_transport_process_access_message(void){ 484 uint8_t * upper_transport_pdu = incoming_access_decrypted->data; 485 uint8_t upper_transport_pdu_len = incoming_access_decrypted->len - incoming_access_decrypted->transmic_len; 486 mesh_print_hex("Upper Transport pdu", upper_transport_pdu, upper_transport_pdu_len); 487 488 uint8_t aid = incoming_access_decrypted->akf_aid_control & 0x3f; 489 uint8_t akf = (incoming_access_decrypted->akf_aid_control & 0x40) >> 6; 490 491 printf("AKF: %u\n", akf); 492 printf("AID: %02x\n", aid); 493 494 mesh_transport_key_and_virtual_address_iterator_init(&mesh_transport_key_it, incoming_access_decrypted->dst, 495 incoming_access_decrypted->netkey_index, akf, aid); 496 mesh_upper_transport_validate_access_message(); 497 } 498 499 static void mesh_upper_transport_message_received(mesh_pdu_t * pdu){ 500 btstack_linked_list_add_tail(&upper_transport_incoming, (btstack_linked_item_t*) pdu); 501 mesh_upper_transport_run(); 502 } 503 504 static void mesh_upper_transport_send_access_segmented(mesh_upper_transport_pdu_t * upper_pdu){ 505 506 mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) upper_pdu->lower_pdu; 507 segmented_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_SEGMENTED; 508 509 // convert mesh_access_pdu_t into mesh_segmented_pdu_t 510 btstack_linked_list_t free_segments = segmented_pdu->segments; 511 segmented_pdu->segments = NULL; 512 mesh_segmented_store_payload(incoming_pdu_singleton.access.data, upper_pdu->len, &free_segments, &segmented_pdu->segments); 513 514 // copy meta 515 segmented_pdu->len = upper_pdu->len; 516 segmented_pdu->netkey_index = upper_pdu->netkey_index; 517 segmented_pdu->transmic_len = upper_pdu->transmic_len; 518 segmented_pdu->akf_aid_control = upper_pdu->akf_aid_control; 519 segmented_pdu->flags = upper_pdu->flags; 520 521 // setup segmented_pdu header 522 // (void)memcpy(segmented_pdu->network_header, upper_pdu->network_header, 9); 523 // TODO: use fields in mesh_segmented_pdu_t and setup network header in lower transport 524 segmented_pdu->ivi_nid = upper_pdu->ivi_nid; 525 segmented_pdu->ctl_ttl = upper_pdu->ctl_ttl; 526 segmented_pdu->seq = upper_pdu->seq; 527 segmented_pdu->src = upper_pdu->src; 528 segmented_pdu->dst = upper_pdu->dst; 529 530 // queue up 531 upper_pdu->lower_pdu = (mesh_pdu_t *) segmented_pdu; 532 btstack_linked_list_add(&upper_transport_outgoing_active, (btstack_linked_item_t *) upper_pdu); 533 534 mesh_lower_transport_send_pdu((mesh_pdu_t*) segmented_pdu); 535 } 536 537 static void mesh_upper_transport_send_access_unsegmented(mesh_upper_transport_pdu_t * upper_pdu){ 538 539 // provide segment 540 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) upper_pdu->lower_pdu; 541 542 // setup network pdu 543 network_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS; 544 network_pdu->data[0] = upper_pdu->ivi_nid; 545 network_pdu->data[1] = upper_pdu->ctl_ttl; 546 big_endian_store_24(network_pdu->data, 2, upper_pdu->seq); 547 big_endian_store_16(network_pdu->data, 5, upper_pdu->src); 548 big_endian_store_16(network_pdu->data, 7, upper_pdu->dst); 549 network_pdu->netkey_index = upper_pdu->netkey_index; 550 551 // setup access message 552 network_pdu->data[9] = upper_pdu->akf_aid_control; 553 btstack_assert(upper_pdu->len < 15); 554 (void)memcpy(&network_pdu->data[10], &incoming_pdu_singleton.access.data, upper_pdu->len); 555 network_pdu->len = 10 + upper_pdu->len; 556 network_pdu->flags = 0; 557 558 // queue up 559 btstack_linked_list_add(&upper_transport_outgoing_active, (btstack_linked_item_t *) upper_pdu); 560 561 mesh_lower_transport_send_pdu((mesh_pdu_t*) network_pdu); 562 } 563 564 static void mesh_upper_transport_send_access_ccm(void * arg){ 565 crypto_active = 0; 566 567 mesh_upper_transport_pdu_t * upper_pdu = (mesh_upper_transport_pdu_t *) arg; 568 mesh_print_hex("EncAccessPayload", incoming_pdu_singleton.access.data, upper_pdu->len); 569 // store TransMIC 570 btstack_crypto_ccm_get_authentication_value(&ccm, &incoming_pdu_singleton.access.data[upper_pdu->len]); 571 mesh_print_hex("TransMIC", &incoming_pdu_singleton.access.data[upper_pdu->len], upper_pdu->transmic_len); 572 upper_pdu->len += upper_pdu->transmic_len; 573 mesh_print_hex("UpperTransportPDU", incoming_pdu_singleton.access.data, upper_pdu->len); 574 switch (upper_pdu->pdu_header.pdu_type){ 575 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS: 576 mesh_upper_transport_send_access_unsegmented(upper_pdu); 577 break; 578 case MESH_PDU_TYPE_UPPER_SEGMENTED_ACCESS: 579 mesh_upper_transport_send_access_segmented(upper_pdu); 580 break; 581 default: 582 btstack_assert(false); 583 } 584 } 585 586 static void mesh_upper_transport_send_access_digest(void *arg){ 587 mesh_upper_transport_pdu_t * upper_pdu = (mesh_upper_transport_pdu_t *) arg; 588 uint16_t access_pdu_len = upper_pdu->len; 589 btstack_crypto_ccm_encrypt_block(&ccm, access_pdu_len, incoming_pdu_singleton.access.data, incoming_pdu_singleton.access.data, 590 &mesh_upper_transport_send_access_ccm, upper_pdu); 591 } 592 593 static void mesh_upper_transport_send_access(mesh_upper_transport_pdu_t * upper_pdu){ 594 595 // if dst is virtual address, lookup label uuid and hash 596 uint16_t aad_len = 0; 597 mesh_virtual_address_t * virtual_address = NULL; 598 if (mesh_network_address_virtual(upper_pdu->dst)){ 599 virtual_address = mesh_virtual_address_for_pseudo_dst(upper_pdu->dst); 600 if (!virtual_address){ 601 printf("No virtual address register for pseudo dst %4x\n", upper_pdu->dst); 602 mesh_access_message_handler(MESH_TRANSPORT_PDU_SENT, MESH_TRANSPORT_STATUS_SEND_FAILED, (mesh_pdu_t *) upper_pdu); 603 return; 604 } 605 // printf("Using hash %4x with LabelUUID: ", virtual_address->hash); 606 // printf_hexdump(virtual_address->label_uuid, 16); 607 aad_len = 16; 608 upper_pdu->dst = virtual_address->hash; 609 } 610 611 // get app or device key 612 uint16_t appkey_index = upper_pdu->appkey_index; 613 const mesh_transport_key_t * appkey = mesh_upper_transport_get_outgoing_appkey(upper_pdu->netkey_index, appkey_index); 614 if (appkey == NULL){ 615 printf("AppKey %04x not found, drop message\n", appkey_index); 616 mesh_access_message_handler(MESH_TRANSPORT_PDU_SENT, MESH_TRANSPORT_STATUS_SEND_FAILED, (mesh_pdu_t *) upper_pdu); 617 return; 618 } 619 620 // reserve slot 621 mesh_lower_transport_reserve_slot(); 622 623 // reserve one sequence number, which is also used to encrypt access payload 624 uint32_t seq = mesh_sequence_number_next(); 625 upper_pdu->flags |= MESH_TRANSPORT_FLAG_SEQ_RESERVED; 626 upper_pdu->seq = seq; 627 628 // also reserves crypto_buffer 629 crypto_active = 1; 630 631 // flatten segmented pdu into crypto buffer 632 uint16_t payload_len = mesh_upper_pdu_flatten(upper_pdu, incoming_pdu_singleton.access.data, sizeof(incoming_pdu_singleton.access.data)); 633 btstack_assert(payload_len == upper_pdu->len); 634 635 // Dump PDU 636 printf("[+] Upper transport, send upper (un)segmented Access PDU - dest %04x, seq %06x\n", upper_pdu->dst, upper_pdu->seq); 637 mesh_print_hex("Access Payload", incoming_pdu_singleton.access.data, upper_pdu->len); 638 639 // setup nonce - uses dst, so after pseudo address translation 640 if (appkey_index == MESH_DEVICE_KEY_INDEX){ 641 transport_segmented_setup_device_nonce(application_nonce, (mesh_pdu_t *) upper_pdu); 642 } else { 643 transport_segmented_setup_application_nonce(application_nonce, (mesh_pdu_t *) upper_pdu); 644 } 645 646 // Dump key 647 mesh_print_hex("AppOrDevKey", appkey->key, 16); 648 649 // encrypt ccm 650 uint8_t transmic_len = upper_pdu->transmic_len; 651 uint16_t access_pdu_len = upper_pdu->len; 652 btstack_crypto_ccm_init(&ccm, appkey->key, application_nonce, access_pdu_len, aad_len, transmic_len); 653 if (virtual_address){ 654 mesh_print_hex("LabelUUID", virtual_address->label_uuid, 16); 655 btstack_crypto_ccm_digest(&ccm, virtual_address->label_uuid, 16, 656 &mesh_upper_transport_send_access_digest, upper_pdu); 657 } else { 658 mesh_upper_transport_send_access_digest(upper_pdu); 659 } 660 } 661 662 static void mesh_upper_transport_send_unsegmented_control_pdu(mesh_network_pdu_t * network_pdu){ 663 // reserve slot 664 mesh_lower_transport_reserve_slot(); 665 // reserve sequence number 666 uint32_t seq = mesh_sequence_number_next(); 667 mesh_network_pdu_set_seq(network_pdu, seq); 668 // Dump PDU 669 uint8_t opcode = network_pdu->data[9]; 670 printf("[+] Upper transport, send unsegmented Control PDU %p - seq %06x opcode %02x\n", network_pdu, seq, opcode); 671 mesh_print_hex("Access Payload", &network_pdu->data[10], network_pdu->len - 10); 672 673 // send 674 mesh_lower_transport_send_pdu((mesh_pdu_t *) network_pdu); 675 } 676 677 static void mesh_upper_transport_send_segmented_control_pdu(mesh_upper_transport_pdu_t * upper_pdu){ 678 // reserve slot 679 mesh_lower_transport_reserve_slot(); 680 // reserve sequence number 681 uint32_t seq = mesh_sequence_number_next(); 682 upper_pdu->flags |= MESH_TRANSPORT_FLAG_SEQ_RESERVED; 683 upper_pdu->seq = seq; 684 // Dump PDU 685 // uint8_t opcode = upper_pdu->data[0]; 686 // printf("[+] Upper transport, send segmented Control PDU %p - seq %06x opcode %02x\n", upper_pdu, seq, opcode); 687 // mesh_print_hex("Access Payload", &upper_pdu->data[1], upper_pdu->len - 1); 688 // send 689 mesh_segmented_pdu_t * segmented_pdu = (mesh_segmented_pdu_t *) upper_pdu->lower_pdu; 690 segmented_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_SEGMENTED; 691 692 // lend segments to lower transport pdu 693 segmented_pdu->segments = upper_pdu->segments; 694 upper_pdu->segments = NULL; 695 696 // copy meta 697 segmented_pdu->len = upper_pdu->len; 698 segmented_pdu->netkey_index = upper_pdu->netkey_index; 699 segmented_pdu->transmic_len = 0; // no TransMIC for control 700 segmented_pdu->akf_aid_control = upper_pdu->akf_aid_control; 701 segmented_pdu->flags = upper_pdu->flags; 702 703 // setup segmented_pdu header 704 // TODO: use fields in mesh_segmented_pdu_t and setup network header in lower transport 705 segmented_pdu->ivi_nid = upper_pdu->ivi_nid; 706 segmented_pdu->ctl_ttl = upper_pdu->ctl_ttl; 707 segmented_pdu->seq = upper_pdu->seq; 708 segmented_pdu->src = upper_pdu->src; 709 segmented_pdu->dst = upper_pdu->dst; 710 711 // queue up 712 upper_pdu->lower_pdu = (mesh_pdu_t *) segmented_pdu; 713 btstack_linked_list_add(&upper_transport_outgoing_active, (btstack_linked_item_t *) upper_pdu); 714 715 mesh_lower_transport_send_pdu((mesh_pdu_t *) segmented_pdu); 716 } 717 718 static void mesh_upper_transport_run(void){ 719 720 while(!btstack_linked_list_empty(&upper_transport_incoming)){ 721 722 if (crypto_active) return; 723 724 // get next message 725 mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_pop(&upper_transport_incoming); 726 mesh_network_pdu_t * network_pdu; 727 mesh_segmented_pdu_t * message_pdu; 728 switch (pdu->pdu_type){ 729 case MESH_PDU_TYPE_UNSEGMENTED: 730 network_pdu = (mesh_network_pdu_t *) pdu; 731 // control? 732 if (mesh_network_control(network_pdu)) { 733 734 incoming_control_pdu = &incoming_pdu_singleton.control; 735 incoming_control_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_CONTROL; 736 incoming_control_pdu->len = network_pdu->len; 737 incoming_control_pdu->netkey_index = network_pdu->netkey_index; 738 739 uint8_t * lower_transport_pdu = mesh_network_pdu_data(network_pdu); 740 741 incoming_control_pdu->akf_aid_control = lower_transport_pdu[0]; 742 incoming_control_pdu->len = network_pdu->len - 10; // 9 header + 1 opcode 743 (void)memcpy(incoming_control_pdu->data, &lower_transport_pdu[1], incoming_control_pdu->len); 744 745 // copy meta data into encrypted pdu buffer 746 incoming_control_pdu->ivi_nid = network_pdu->data[0]; 747 incoming_control_pdu->ctl_ttl = network_pdu->data[1]; 748 incoming_control_pdu->seq = big_endian_read_24(network_pdu->data, 2); 749 incoming_control_pdu->src = big_endian_read_16(network_pdu->data, 5); 750 incoming_control_pdu->dst = big_endian_read_16(network_pdu->data, 7); 751 752 mesh_print_hex("Assembled payload", incoming_control_pdu->data, incoming_control_pdu->len); 753 754 // free mesh message 755 mesh_lower_transport_message_processed_by_higher_layer(pdu); 756 757 btstack_assert(mesh_control_message_handler != NULL); 758 mesh_pdu_t * pdu = (mesh_pdu_t*) incoming_control_pdu; 759 mesh_control_message_handler(MESH_TRANSPORT_PDU_RECEIVED, MESH_TRANSPORT_STATUS_SUCCESS, pdu); 760 761 } else { 762 763 incoming_access_encrypted = (mesh_pdu_t *) network_pdu; 764 765 incoming_access_decrypted = &incoming_pdu_singleton.access; 766 incoming_access_decrypted->pdu_header.pdu_type = MESH_PDU_TYPE_ACCESS; 767 incoming_access_decrypted->netkey_index = network_pdu->netkey_index; 768 incoming_access_decrypted->transmic_len = 4; 769 incoming_access_decrypted->akf_aid_control = network_pdu->data[9]; 770 incoming_access_decrypted->len = network_pdu->len - 10; // 9 header + 1 AID 771 incoming_access_decrypted->ivi_nid = network_pdu->data[0]; 772 incoming_access_decrypted->ctl_ttl = network_pdu->data[1]; 773 incoming_access_decrypted->seq = big_endian_read_24(network_pdu->data, 2); 774 incoming_access_decrypted->src = big_endian_read_16(network_pdu->data, 5); 775 incoming_access_decrypted->dst = big_endian_read_16(network_pdu->data, 7); 776 // (void)memcpy(incoming_access_decrypted->network_header, network_pdu->data, 9); 777 778 mesh_upper_transport_process_access_message(); 779 } 780 break; 781 case MESH_PDU_TYPE_SEGMENTED: 782 message_pdu = (mesh_segmented_pdu_t *) pdu; 783 uint8_t ctl = message_pdu->ctl_ttl >> 7; 784 if (ctl){ 785 incoming_control_pdu= &incoming_pdu_singleton.control; 786 incoming_control_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_CONTROL; 787 788 // flatten 789 mesh_segmented_pdu_flatten(&message_pdu->segments, 8, incoming_control_pdu->data); 790 791 // copy meta data into encrypted pdu buffer 792 incoming_control_pdu->flags = 0; 793 incoming_control_pdu->len = message_pdu->len; 794 incoming_control_pdu->netkey_index = message_pdu->netkey_index; 795 incoming_control_pdu->akf_aid_control = message_pdu->akf_aid_control; 796 incoming_access_decrypted->ivi_nid = message_pdu->ivi_nid; 797 incoming_access_decrypted->ctl_ttl = message_pdu->ctl_ttl; 798 incoming_access_decrypted->seq = message_pdu->seq; 799 incoming_access_decrypted->src = message_pdu->src; 800 incoming_access_decrypted->dst = message_pdu->dst; 801 802 mesh_print_hex("Assembled payload", incoming_control_pdu->data, incoming_control_pdu->len); 803 804 // free mesh message 805 mesh_lower_transport_message_processed_by_higher_layer((mesh_pdu_t *)message_pdu); 806 807 btstack_assert(mesh_control_message_handler != NULL); 808 mesh_pdu_t * pdu = (mesh_pdu_t*) incoming_control_pdu; 809 mesh_access_message_handler(MESH_TRANSPORT_PDU_RECEIVED, MESH_TRANSPORT_STATUS_SUCCESS, pdu); 810 811 } else { 812 813 incoming_access_encrypted = (mesh_pdu_t *) message_pdu; 814 815 incoming_access_decrypted = &incoming_pdu_singleton.access; 816 incoming_access_decrypted->pdu_header.pdu_type = MESH_PDU_TYPE_ACCESS; 817 incoming_access_decrypted->len = message_pdu->len; 818 incoming_access_decrypted->netkey_index = message_pdu->netkey_index; 819 incoming_access_decrypted->transmic_len = message_pdu->transmic_len; 820 incoming_access_decrypted->akf_aid_control = message_pdu->akf_aid_control; 821 incoming_access_decrypted->ivi_nid = message_pdu->ivi_nid; 822 incoming_access_decrypted->ctl_ttl = message_pdu->ctl_ttl; 823 incoming_access_decrypted->seq = message_pdu->seq; 824 incoming_access_decrypted->src = message_pdu->src; 825 incoming_access_decrypted->dst = message_pdu->dst; 826 827 mesh_upper_transport_process_access_message(); 828 } 829 break; 830 default: 831 btstack_assert(0); 832 break; 833 } 834 } 835 836 while (!btstack_linked_list_empty(&upper_transport_outgoing)){ 837 838 if (crypto_active) break; 839 840 mesh_pdu_t * pdu = (mesh_pdu_t *) btstack_linked_list_get_first_item(&upper_transport_outgoing); 841 if (mesh_lower_transport_can_send_to_dest(mesh_pdu_dst(pdu)) == 0) break; 842 843 mesh_upper_transport_pdu_t * upper_pdu; 844 mesh_segmented_pdu_t * segmented_pdu; 845 bool ok; 846 847 switch (pdu->pdu_type){ 848 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL: 849 // control pdus can go through directly 850 btstack_assert(mesh_pdu_ctl(pdu) != 0); 851 (void) btstack_linked_list_pop(&upper_transport_outgoing); 852 mesh_upper_transport_send_unsegmented_control_pdu((mesh_network_pdu_t *) pdu); 853 break; 854 case MESH_PDU_TYPE_UPPER_SEGMENTED_CONTROL: 855 // control pdus can go through directly 856 btstack_assert(mesh_pdu_ctl(pdu) != 0); 857 (void) btstack_linked_list_pop(&upper_transport_outgoing); 858 mesh_upper_transport_send_segmented_control_pdu((mesh_upper_transport_pdu_t *) pdu); 859 break; 860 case MESH_PDU_TYPE_UPPER_SEGMENTED_ACCESS: 861 // segmented access pdus required a mesh-segmented-pdu 862 upper_pdu = (mesh_upper_transport_pdu_t *) pdu; 863 if (upper_pdu->lower_pdu == NULL){ 864 segmented_pdu = btstack_memory_mesh_segmented_pdu_get(); 865 } 866 if (segmented_pdu == NULL) break; 867 upper_pdu->lower_pdu = (mesh_pdu_t *) segmented_pdu; 868 segmented_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_SEGMENTED; 869 // and a mesh-network-pdu for each segment in upper pdu 870 ok = mesh_segmented_allocate_segments(&segmented_pdu->segments, upper_pdu->len + upper_pdu->transmic_len); 871 if (!ok) break; 872 // all buffers available, get started 873 (void) btstack_linked_list_pop(&upper_transport_outgoing); 874 mesh_upper_transport_send_access(upper_pdu); 875 break; 876 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS: 877 // unsegmented access pdus require a single mesh-network-dpu 878 upper_pdu = (mesh_upper_transport_pdu_t *) pdu; 879 if (upper_pdu->lower_pdu == NULL){ 880 upper_pdu->lower_pdu = (mesh_pdu_t *) mesh_network_pdu_get(); 881 } 882 if (upper_pdu->lower_pdu == NULL) break; 883 (void) btstack_linked_list_pop(&upper_transport_outgoing); 884 mesh_upper_transport_send_access((mesh_upper_transport_pdu_t *) pdu); 885 break; 886 default: 887 btstack_assert(false); 888 break; 889 } 890 } 891 } 892 893 static mesh_upper_transport_pdu_t * mesh_upper_transport_find_pdu_for_lower(mesh_pdu_t * pdu_to_find){ 894 btstack_linked_list_iterator_t it; 895 btstack_linked_list_iterator_init(&it, &upper_transport_outgoing_active); 896 mesh_upper_transport_pdu_t * upper_pdu; 897 while (btstack_linked_list_iterator_has_next(&it)){ 898 mesh_pdu_t * mesh_pdu = (mesh_pdu_t *) btstack_linked_list_iterator_next(&it); 899 switch (mesh_pdu->pdu_type){ 900 case MESH_PDU_TYPE_UPPER_SEGMENTED_CONTROL: 901 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS: 902 case MESH_PDU_TYPE_UPPER_SEGMENTED_ACCESS: 903 upper_pdu = (mesh_upper_transport_pdu_t *) mesh_pdu; 904 if (upper_pdu->lower_pdu == pdu_to_find){ 905 btstack_linked_list_iterator_remove(&it); 906 return upper_pdu; 907 } 908 break; 909 default: 910 break; 911 } 912 } 913 return NULL; 914 } 915 916 static void mesh_upper_transport_pdu_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu){ 917 mesh_upper_transport_pdu_t * upper_pdu; 918 mesh_network_pdu_t * network_pdu; 919 mesh_segmented_pdu_t * segmented_pdu; 920 switch (callback_type){ 921 case MESH_TRANSPORT_PDU_RECEIVED: 922 mesh_upper_transport_message_received(pdu); 923 break; 924 case MESH_TRANSPORT_PDU_SENT: 925 switch (pdu->pdu_type){ 926 case MESH_PDU_TYPE_SEGMENTED: 927 // try to find in outgoing active 928 upper_pdu = mesh_upper_transport_find_pdu_for_lower(pdu); 929 btstack_assert(upper_pdu != NULL); 930 segmented_pdu = (mesh_segmented_pdu_t *) pdu; 931 // free chunks 932 while (!btstack_linked_list_empty(&segmented_pdu->segments)){ 933 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&segmented_pdu->segments); 934 mesh_network_pdu_free(network_pdu); 935 } 936 // free segmented pdu 937 btstack_memory_mesh_segmented_pdu_free(segmented_pdu); 938 // TODO: free segmented_pdu 939 upper_pdu->lower_pdu = NULL; 940 switch (upper_pdu->pdu_header.pdu_type){ 941 case MESH_PDU_TYPE_UPPER_SEGMENTED_CONTROL: 942 mesh_control_message_handler(callback_type, status, (mesh_pdu_t *) upper_pdu); 943 break; 944 case MESH_PDU_TYPE_UPPER_SEGMENTED_ACCESS: 945 mesh_access_message_handler(callback_type, status, (mesh_pdu_t *) upper_pdu); 946 break; 947 default: 948 btstack_assert(false); 949 break; 950 } 951 break; 952 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS: 953 // find corresponding upper transport pdu and free single segment 954 upper_pdu = mesh_upper_transport_find_pdu_for_lower(pdu); 955 btstack_assert(upper_pdu != NULL); 956 btstack_assert(upper_pdu->lower_pdu == (mesh_pdu_t *) pdu); 957 mesh_network_pdu_free((mesh_network_pdu_t *) pdu); 958 upper_pdu->lower_pdu = NULL; 959 mesh_access_message_handler(callback_type, status, (mesh_pdu_t*) upper_pdu); 960 break; 961 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL: 962 mesh_access_message_handler(callback_type, status, pdu); 963 break; 964 default: 965 btstack_assert(false); 966 break; 967 } 968 mesh_upper_transport_run(); 969 break; 970 default: 971 break; 972 } 973 } 974 975 void mesh_upper_transport_pdu_free(mesh_pdu_t * pdu){ 976 btstack_assert(pdu != NULL); 977 mesh_network_pdu_t * network_pdu; 978 mesh_segmented_pdu_t * message_pdu; 979 mesh_upper_transport_pdu_t * upper_pdu; 980 switch (pdu->pdu_type) { 981 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL: 982 case MESH_PDU_TYPE_NETWORK: 983 network_pdu = (mesh_network_pdu_t *) pdu; 984 mesh_network_pdu_free(network_pdu); 985 break; 986 case MESH_PDU_TYPE_SEGMENTED: 987 message_pdu = (mesh_segmented_pdu_t *) pdu; 988 mesh_message_pdu_free(message_pdu); 989 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS: 990 case MESH_PDU_TYPE_UPPER_SEGMENTED_ACCESS: 991 case MESH_PDU_TYPE_UPPER_SEGMENTED_CONTROL: 992 upper_pdu = (mesh_upper_transport_pdu_t *) pdu; 993 while (upper_pdu->segments) { 994 mesh_network_pdu_t *segment = (mesh_network_pdu_t *) btstack_linked_list_pop(&upper_pdu->segments); 995 mesh_network_pdu_free(segment); 996 } 997 btstack_memory_mesh_upper_transport_pdu_free(upper_pdu); 998 break; 999 default: 1000 btstack_assert(false); 1001 break; 1002 } 1003 } 1004 1005 void mesh_upper_transport_message_processed_by_higher_layer(mesh_pdu_t * pdu){ 1006 crypto_active = 0; 1007 switch (pdu->pdu_type){ 1008 case MESH_PDU_TYPE_ACCESS: 1009 mesh_upper_transport_process_access_message_done((mesh_access_pdu_t *) pdu); 1010 case MESH_PDU_TYPE_CONTROL: 1011 mesh_upper_transport_process_control_message_done((mesh_control_pdu_t *) pdu); 1012 break; 1013 default: 1014 btstack_assert(0); 1015 break; 1016 } 1017 } 1018 1019 void mesh_upper_transport_send_access_pdu(mesh_pdu_t *pdu){ 1020 switch (pdu->pdu_type){ 1021 case MESH_PDU_TYPE_UPPER_SEGMENTED_ACCESS: 1022 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS: 1023 break; 1024 default: 1025 btstack_assert(false); 1026 break; 1027 } 1028 1029 btstack_assert(((mesh_upper_transport_pdu_t *) pdu)->lower_pdu == NULL); 1030 1031 btstack_linked_list_add_tail(&upper_transport_outgoing, (btstack_linked_item_t*) pdu); 1032 mesh_upper_transport_run(); 1033 } 1034 1035 void mesh_upper_transport_send_control_pdu(mesh_pdu_t * pdu){ 1036 switch (pdu->pdu_type){ 1037 case MESH_PDU_TYPE_UPPER_SEGMENTED_CONTROL: 1038 break; 1039 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL: 1040 btstack_assert( ((mesh_network_pdu_t *) pdu)->len >= 9); 1041 break; 1042 default: 1043 btstack_assert(false); 1044 break; 1045 } 1046 1047 btstack_linked_list_add_tail(&upper_transport_outgoing, (btstack_linked_item_t*) pdu); 1048 mesh_upper_transport_run(); 1049 } 1050 1051 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, 1052 const uint8_t * control_pdu_data, uint16_t control_pdu_len){ 1053 1054 if (control_pdu_len > 11) return 1; 1055 1056 const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 1057 if (!network_key) return 1; 1058 1059 uint8_t transport_pdu_data[12]; 1060 transport_pdu_data[0] = opcode; 1061 (void)memcpy(&transport_pdu_data[1], control_pdu_data, control_pdu_len); 1062 uint16_t transport_pdu_len = control_pdu_len + 1; 1063 1064 // setup network_pdu 1065 mesh_network_setup_pdu(network_pdu, netkey_index, network_key->nid, 1, ttl, 0, src, dest, transport_pdu_data, transport_pdu_len); 1066 1067 return 0; 1068 } 1069 1070 static uint8_t mesh_upper_transport_setup_segmented_control_pdu(mesh_upper_transport_pdu_t * upper_pdu, uint16_t netkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode, 1071 const uint8_t * control_pdu_data, uint16_t control_pdu_len){ 1072 1073 if (control_pdu_len > 256) return 1; 1074 1075 const mesh_network_key_t * network_key = mesh_network_key_list_get(netkey_index); 1076 if (!network_key) return 1; 1077 1078 upper_pdu->ivi_nid = network_key->nid | ((mesh_get_iv_index_for_tx() & 1) << 7); 1079 upper_pdu->ctl_ttl = ttl; 1080 upper_pdu->src = src; 1081 upper_pdu->dst = dest; 1082 upper_pdu->transmic_len = 0; // no TransMIC for control 1083 upper_pdu->netkey_index = netkey_index; 1084 upper_pdu->akf_aid_control = opcode; 1085 1086 // allocate segments 1087 btstack_linked_list_t free_segments = NULL; 1088 bool ok = mesh_segmented_allocate_segments( &free_segments, control_pdu_len); 1089 if (!ok) return 1; 1090 // store control pdu 1091 mesh_segmented_store_payload(control_pdu_data, control_pdu_len, &free_segments, &upper_pdu->segments); 1092 upper_pdu->len = control_pdu_len; 1093 return 0; 1094 } 1095 1096 uint8_t mesh_upper_transport_setup_control_pdu(mesh_pdu_t * pdu, uint16_t netkey_index, 1097 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t opcode, const uint8_t * control_pdu_data, uint16_t control_pdu_len){ 1098 switch (pdu->pdu_type){ 1099 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL: 1100 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); 1101 case MESH_PDU_TYPE_UPPER_SEGMENTED_CONTROL: 1102 return mesh_upper_transport_setup_segmented_control_pdu((mesh_upper_transport_pdu_t *) pdu, netkey_index, ttl, src, dest, opcode, control_pdu_data, control_pdu_len); 1103 default: 1104 btstack_assert(0); 1105 return 1; 1106 } 1107 } 1108 1109 static uint8_t mesh_upper_transport_setup_segmented_access_pdu_header(mesh_access_pdu_t * access_pdu, uint16_t netkey_index, 1110 uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){ 1111 1112 // get app or device key 1113 const mesh_transport_key_t *appkey; 1114 appkey = mesh_transport_key_get(appkey_index); 1115 if (appkey == NULL) { 1116 printf("[!] Upper transport, setup segmented Access PDU - appkey_index %x unknown\n", appkey_index); 1117 return 1; 1118 } 1119 uint8_t akf_aid = (appkey->akf << 6) | appkey->aid; 1120 1121 // lookup network by netkey_index 1122 const mesh_network_key_t *network_key = mesh_network_key_list_get(netkey_index); 1123 if (!network_key) return 1; 1124 if (network_key == NULL) { 1125 printf("[!] Upper transport, setup segmented Access PDU - netkey_index %x unknown\n", appkey_index); 1126 return 1; 1127 } 1128 1129 const uint8_t trans_mic_len = szmic ? 8 : 4; 1130 1131 // store in transport pdu 1132 access_pdu->transmic_len = trans_mic_len; 1133 access_pdu->netkey_index = netkey_index; 1134 access_pdu->appkey_index = appkey_index; 1135 access_pdu->akf_aid_control = akf_aid; 1136 uint8_t iviNid = network_key->nid | ((mesh_get_iv_index_for_tx() & 1) << 7); 1137 access_pdu->ivi_nid = iviNid; 1138 access_pdu->src = src; 1139 access_pdu->dst = dest; 1140 access_pdu->ctl_ttl = ttl; 1141 return 0; 1142 } 1143 1144 static uint8_t mesh_upper_transport_setup_upper_access_pdu_header(mesh_upper_transport_pdu_t * upper_pdu, uint16_t netkey_index, 1145 uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){ 1146 1147 // get app or device key 1148 const mesh_transport_key_t *appkey; 1149 appkey = mesh_transport_key_get(appkey_index); 1150 if (appkey == NULL) { 1151 printf("[!] Upper transport, setup segmented Access PDU - appkey_index %x unknown\n", appkey_index); 1152 return 1; 1153 } 1154 uint8_t akf_aid = (appkey->akf << 6) | appkey->aid; 1155 1156 // lookup network by netkey_index 1157 const mesh_network_key_t *network_key = mesh_network_key_list_get(netkey_index); 1158 if (!network_key) return 1; 1159 if (network_key == NULL) { 1160 printf("[!] Upper transport, setup segmented Access PDU - netkey_index %x unknown\n", appkey_index); 1161 return 1; 1162 } 1163 1164 const uint8_t trans_mic_len = szmic ? 8 : 4; 1165 1166 // store in transport pdu 1167 upper_pdu->ivi_nid = network_key->nid | ((mesh_get_iv_index_for_tx() & 1) << 7); 1168 upper_pdu->ctl_ttl = ttl; 1169 upper_pdu->src = src; 1170 upper_pdu->dst = dest; 1171 upper_pdu->transmic_len = trans_mic_len; 1172 upper_pdu->netkey_index = netkey_index; 1173 upper_pdu->appkey_index = appkey_index; 1174 upper_pdu->akf_aid_control = akf_aid; 1175 return 0; 1176 } 1177 1178 static uint8_t mesh_upper_transport_setup_upper_access_pdu(mesh_upper_transport_pdu_t * upper_pdu, uint16_t netkey_index, uint16_t appkey_index, uint8_t ttl, uint16_t src, uint16_t dest, 1179 uint8_t szmic, const uint8_t * access_pdu_data, uint8_t access_pdu_len){ 1180 int status = mesh_upper_transport_setup_upper_access_pdu_header(upper_pdu, netkey_index, appkey_index, ttl, src, 1181 dest, szmic); 1182 if (status) return status; 1183 1184 // allocate segments 1185 btstack_linked_list_t free_segments = NULL; 1186 bool ok = mesh_segmented_allocate_segments( &free_segments, access_pdu_len); 1187 if (!ok) return 1; 1188 // store control pdu 1189 mesh_segmented_store_payload(access_pdu_data, access_pdu_len, &free_segments, &upper_pdu->segments); 1190 upper_pdu->len = access_pdu_len; 1191 return 0; 1192 } 1193 1194 1195 uint8_t mesh_upper_transport_setup_access_pdu_header(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index, 1196 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic){ 1197 switch (pdu->pdu_type){ 1198 case MESH_PDU_TYPE_ACCESS: 1199 return mesh_upper_transport_setup_segmented_access_pdu_header((mesh_access_pdu_t *) pdu, netkey_index, appkey_index, ttl, src, dest, szmic); 1200 default: 1201 btstack_assert(false); 1202 return 1; 1203 } 1204 } 1205 1206 uint8_t mesh_upper_transport_setup_access_pdu(mesh_pdu_t * pdu, uint16_t netkey_index, uint16_t appkey_index, 1207 uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic, 1208 const uint8_t * access_pdu_data, uint8_t access_pdu_len){ 1209 switch (pdu->pdu_type){ 1210 case MESH_PDU_TYPE_UPPER_SEGMENTED_ACCESS: 1211 case MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS: 1212 return mesh_upper_transport_setup_upper_access_pdu((mesh_upper_transport_pdu_t *) pdu, netkey_index, 1213 appkey_index, ttl, src, dest, szmic, access_pdu_data, 1214 access_pdu_len); 1215 default: 1216 btstack_assert(false); 1217 return 1; 1218 } 1219 } 1220 1221 void mesh_upper_transport_register_access_message_handler(void (*callback)(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu)) { 1222 mesh_access_message_handler = callback; 1223 } 1224 1225 void mesh_upper_transport_register_control_message_handler(void (*callback)(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu)){ 1226 mesh_control_message_handler = callback; 1227 } 1228 1229 void mesh_upper_transport_init(){ 1230 mesh_lower_transport_set_higher_layer_handler(&mesh_upper_transport_pdu_handler); 1231 } 1232 1233 1234 mesh_upper_transport_pdu_t * mesh_upper_transport_message_init(mesh_pdu_type_t pdu_type) { 1235 mesh_upper_transport_pdu_t * pdu = btstack_memory_mesh_upper_transport_pdu_get(); 1236 if (!pdu) return NULL; 1237 1238 pdu->pdu_header.pdu_type = pdu_type; 1239 pdu->transmic_len = 4; 1240 pdu->ack_opcode = MESH_ACCESS_OPCODE_NOT_SET; 1241 1242 return pdu; 1243 } 1244 1245 1246 void mesh_upper_transport_message_add_data(mesh_upper_transport_pdu_t * pdu, const uint8_t * data, uint16_t data_len){ 1247 if (pdu->pdu_header.pdu_type == MESH_PDU_TYPE_INVALID) return; 1248 1249 uint16_t bytes_current_segment = 0; 1250 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_get_last_item(&pdu->segments); 1251 if (network_pdu){ 1252 bytes_current_segment = MESH_NETWORK_PAYLOAD_MAX - network_pdu->len; 1253 } 1254 while (data_len > 0){ 1255 if (bytes_current_segment == 0){ 1256 network_pdu = (mesh_network_pdu_t *) mesh_network_pdu_get(); 1257 if (network_pdu == NULL) { 1258 pdu->pdu_header.pdu_type = MESH_PDU_TYPE_INVALID; 1259 return; 1260 } 1261 btstack_linked_list_add_tail(&pdu->segments, (btstack_linked_item_t *) network_pdu); 1262 bytes_current_segment = MESH_NETWORK_PAYLOAD_MAX; 1263 } 1264 uint16_t bytes_to_copy = btstack_min(bytes_current_segment, data_len); 1265 (void) memcpy(&network_pdu->data[network_pdu->len], data, bytes_to_copy); 1266 bytes_current_segment -= bytes_to_copy; 1267 network_pdu->len += bytes_to_copy; 1268 data += bytes_to_copy; 1269 data_len -= bytes_to_copy; 1270 } 1271 } 1272 1273 void mesh_upper_transport_message_add_uint8(mesh_upper_transport_pdu_t * pdu, uint8_t value){ 1274 mesh_upper_transport_message_add_data(pdu, &value, 1); 1275 } 1276 1277 void mesh_upper_transport_message_add_uint16(mesh_upper_transport_pdu_t * pdu, uint16_t value){ 1278 uint8_t buffer[2]; 1279 little_endian_store_16(buffer, 0, value); 1280 mesh_upper_transport_message_add_data(pdu, buffer, sizeof(buffer)); 1281 } 1282 1283 void mesh_upper_transport_message_add_uint24(mesh_upper_transport_pdu_t * pdu, uint16_t value){ 1284 uint8_t buffer[3]; 1285 little_endian_store_24(buffer, 0, value); 1286 mesh_upper_transport_message_add_data(pdu, buffer, sizeof(buffer)); 1287 } 1288 1289 void mesh_upper_transport_message_add_uint32(mesh_upper_transport_pdu_t * pdu, uint16_t value){ 1290 uint8_t buffer[4]; 1291 little_endian_store_32(buffer, 0, value); 1292 mesh_upper_transport_message_add_data(pdu, buffer, sizeof(buffer)); 1293 } 1294 1295 bool mesh_upper_transport_message_finalize(mesh_upper_transport_pdu_t * pdu){ 1296 return pdu->pdu_header.pdu_type != MESH_PDU_TYPE_INVALID; 1297 } 1298