1 /* 2 * Copyright (C) 2018 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_network.c" 39 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 44 #include "btstack_debug.h" 45 #include "btstack_event.h" 46 #include "btstack_memory.h" 47 #include "btstack_util.h" 48 49 #include "mesh/beacon.h" 50 #include "mesh/mesh_foundation.h" 51 #include "mesh/mesh_iv_index_seq_number.h" 52 #include "mesh/mesh_keys.h" 53 #include "mesh/mesh_node.h" 54 #include "mesh/provisioning.h" 55 #include "mesh/provisioning_device.h" 56 57 #ifdef ENABLE_MESH_ADV_BEARER 58 #include "mesh/adv_bearer.h" 59 #endif 60 61 #ifdef ENABLE_MESH_GATT_BEARER 62 #include "mesh/gatt_bearer.h" 63 #endif 64 65 // configuration 66 #define MESH_NETWORK_CACHE_SIZE 2 67 68 // debug config 69 #define LOG_NETWORK 70 71 static void mesh_network_dump_network_pdus(const char * name, btstack_linked_list_t * list); 72 73 // structs 74 75 // globals 76 77 static void (*mesh_network_higher_layer_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu); 78 static void (*mesh_network_proxy_message_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu); 79 80 #ifdef ENABLE_MESH_GATT_BEARER 81 static hci_con_handle_t gatt_bearer_con_handle; 82 #endif 83 84 // shared send/receive crypto 85 static int mesh_crypto_active; 86 87 // crypto requests 88 static union { 89 btstack_crypto_ccm_t ccm; 90 btstack_crypto_aes128_t aes128; 91 } mesh_network_crypto_request; 92 93 static const mesh_network_key_t * current_network_key; 94 95 // PECB calculation 96 static uint8_t encryption_block[16]; 97 static uint8_t obfuscation_block[16]; 98 99 // Subnets 100 static btstack_linked_list_t subnets; 101 102 // Network Nonce 103 static uint8_t network_nonce[13]; 104 105 // INCOMING // 106 107 // unprocessed network pdu - added by mesh_network_pdus_received_message 108 static btstack_linked_list_t network_pdus_received; 109 110 // in validation 111 static mesh_network_pdu_t * incoming_pdu_raw; 112 static mesh_network_pdu_t * incoming_pdu_decoded; 113 static mesh_network_key_iterator_t validation_network_key_it; 114 115 // OUTGOING // 116 117 // Network PDUs queued by mesh_network_send 118 static btstack_linked_list_t network_pdus_queued; 119 120 // Network PDU about to get send via all bearers when encrypted 121 static mesh_network_pdu_t * outgoing_pdu; 122 123 // Network PDUs ready to send via GATT Bearer 124 static btstack_linked_list_t network_pdus_outgoing_gatt; 125 126 #ifdef ENABLE_MESH_GATT_BEARER 127 static mesh_network_pdu_t * gatt_bearer_network_pdu; 128 #endif 129 130 // Network PDUs ready to send via ADV Bearer 131 static btstack_linked_list_t network_pdus_outgoing_adv; 132 133 #ifdef ENABLE_MESH_ADV_BEARER 134 static mesh_network_pdu_t * adv_bearer_network_pdu; 135 #endif 136 137 138 // mesh network cache - we use 32-bit 'hashes' 139 static uint32_t mesh_network_cache[MESH_NETWORK_CACHE_SIZE]; 140 static int mesh_network_cache_index; 141 142 // register for freed network pdu 143 void (*mesh_network_free_pdu_callback)(void); 144 145 // prototypes 146 147 static void mesh_network_run(void); 148 static void process_network_pdu_validate(void); 149 150 // network caching 151 static uint32_t mesh_network_cache_hash(mesh_network_pdu_t * network_pdu){ 152 // - The SEQ field is a 24-bit integer that when combined with the IV Index, 153 // shall be a unique value for each new Network PDU originated by this node (=> SRC) 154 // - IV updates only rarely 155 // => 16 bit SRC, 1 bit IVI, 15 bit SEQ 156 uint8_t ivi = network_pdu->data[0] >> 7; 157 uint16_t seq = big_endian_read_16(network_pdu->data, 3); 158 uint16_t src = big_endian_read_16(network_pdu->data, 5); 159 return (src << 16) | (ivi << 15) | (seq & 0x7fff); 160 } 161 162 static int mesh_network_cache_find(uint32_t hash){ 163 int i; 164 for (i = 0; i < MESH_NETWORK_CACHE_SIZE; i++) { 165 if (mesh_network_cache[i] == hash) { 166 return 1; 167 } 168 } 169 return 0; 170 } 171 172 static void mesh_network_cache_add(uint32_t hash){ 173 mesh_network_cache[mesh_network_cache_index++] = hash; 174 if (mesh_network_cache_index >= MESH_NETWORK_CACHE_SIZE){ 175 mesh_network_cache_index = 0; 176 } 177 } 178 179 // common helper 180 int mesh_network_address_unicast(uint16_t addr){ 181 return addr != MESH_ADDRESS_UNSASSIGNED && (addr < 0x8000); 182 } 183 184 int mesh_network_address_virtual(uint16_t addr){ 185 return (addr & 0xC000) == 0x8000; // 0b10xx xxxx xxxx xxxx 186 } 187 188 int mesh_network_address_group(uint16_t addr){ 189 return (addr & 0xC000) == 0xC000; // 0b11xx xxxx xxxx xxxx 190 } 191 192 int mesh_network_address_all_proxies(uint16_t addr){ 193 return addr == MESH_ADDRESS_ALL_PROXIES; 194 } 195 196 int mesh_network_address_all_nodes(uint16_t addr){ 197 return addr == MESH_ADDRESS_ALL_NODES; 198 } 199 200 int mesh_network_address_all_friends(uint16_t addr){ 201 return addr == MESH_ADDRESS_ALL_FRIENDS; 202 } 203 204 int mesh_network_address_all_relays(uint16_t addr){ 205 return addr == MESH_ADDRESS_ALL_RELAYS; 206 } 207 208 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst){ 209 // printf("CTL: %u\n", ctl); 210 // printf("SRC: %04x\n", src); 211 // printf("DST: %04x\n", dst); 212 if (src == 0){ 213 // printf("SRC Unassigned Addr -> ignore\n"); 214 return 0; 215 } 216 if ((src & 0xC000) == 0x8000){ 217 // printf("SRC Virtual Addr -> ignore\n"); 218 return 0; 219 } 220 if ((src & 0xC000) == 0xC000){ 221 // printf("SRC Group Addr -> ignore\n"); 222 return 0; 223 } 224 if (dst == 0){ 225 // printf("DST Unassigned Addr -> ignore\n"); 226 return 0; 227 } 228 if ( ((dst & 0xC000) == 0x8000) && (ctl == 1)){ 229 // printf("DST Virtual Addr in CONTROL -> ignore\n"); 230 return 0; 231 } 232 if ( (0xFF00 <= dst) && (dst <= 0xfffb) && (ctl == 0) ){ 233 // printf("DST RFU Group Addr in MESSAGE -> ignore\n"); 234 return 0; 235 } 236 // printf("SRC + DST Addr valid\n"); 237 return 1; 238 } 239 240 static void mesh_network_create_nonce(uint8_t * nonce, const mesh_network_pdu_t * pdu, uint32_t iv_index){ 241 unsigned int pos = 0; 242 nonce[pos++] = 0x0; // Network Nonce 243 (void)memcpy(&nonce[pos], &pdu->data[1], 6); 244 pos += 6; 245 big_endian_store_16(nonce, pos, 0); 246 pos += 2; 247 big_endian_store_32(nonce, pos, iv_index); 248 } 249 250 static void mesh_proxy_create_nonce(uint8_t * nonce, const mesh_network_pdu_t * pdu, uint32_t iv_index){ 251 unsigned int pos = 0; 252 nonce[pos++] = 0x3; // Proxy Nonce 253 nonce[pos++] = 0; 254 (void)memcpy(&nonce[pos], &pdu->data[2], 5); 255 pos += 5; 256 big_endian_store_16(nonce, pos, 0); 257 pos += 2; 258 big_endian_store_32(nonce, pos, iv_index); 259 } 260 261 // NID/IVI | obfuscated (CTL/TTL, SEQ (24), SRC (16) ), encrypted ( DST(16), TransportPDU), MIC(32 or 64) 262 263 static void mesh_network_send_complete(mesh_network_pdu_t * network_pdu){ 264 if (network_pdu->flags & MESH_NETWORK_PDU_FLAGS_RELAY){ 265 #ifdef LOG_NETWORK 266 printf("TX-F-NetworkPDU (%p): relay -> free packet\n", network_pdu); 267 #endif 268 mesh_network_pdu_free(network_pdu); 269 } else { 270 #ifdef LOG_NETWORK 271 printf("TX-F-NetworkPDU (%p): notify lower transport\n", network_pdu); 272 #endif 273 // notify higher layer 274 (*mesh_network_higher_layer_handler)(MESH_NETWORK_PDU_SENT, network_pdu); 275 } 276 } 277 278 // new 279 static void mesh_network_send_c(void *arg){ 280 UNUSED(arg); 281 282 // obfuscate 283 unsigned int i; 284 for (i=0;i<6;i++){ 285 outgoing_pdu->data[1+i] ^= obfuscation_block[i]; 286 } 287 288 #ifdef LOG_NETWORK 289 printf("TX-C-NetworkPDU (%p): ", outgoing_pdu); 290 printf_hexdump(outgoing_pdu->data, outgoing_pdu->len); 291 #endif 292 293 // crypto done 294 mesh_crypto_active = 0; 295 296 // done 297 mesh_network_pdu_t * network_pdu = outgoing_pdu; 298 outgoing_pdu = NULL; 299 300 if ((network_pdu->flags & MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION) != 0){ 301 // encryption requested by mesh_network_encrypt_proxy_configuration_message 302 (*mesh_network_proxy_message_handler)(MESH_NETWORK_PDU_ENCRYPTED, network_pdu); 303 return; 304 } 305 306 #ifdef LOG_NETWORK 307 printf("TX-D-NetworkPDU (%p): ", network_pdu); 308 printf_hexdump(network_pdu->data, network_pdu->len); 309 #endif 310 311 // add to queue 312 btstack_linked_list_add_tail(&network_pdus_outgoing_gatt, (btstack_linked_item_t *) network_pdu); 313 314 // go 315 mesh_network_run(); 316 } 317 318 static void mesh_network_send_b(void *arg){ 319 UNUSED(arg); 320 321 uint32_t iv_index = mesh_get_iv_index_for_tx(); 322 323 // store NetMIC 324 uint8_t net_mic[8]; 325 btstack_crypto_ccm_get_authentication_value(&mesh_network_crypto_request.ccm, net_mic); 326 327 // store MIC 328 uint8_t net_mic_len = outgoing_pdu->data[1] & 0x80 ? 8 : 4; 329 (void)memcpy(&outgoing_pdu->data[outgoing_pdu->len], net_mic, net_mic_len); 330 outgoing_pdu->len += net_mic_len; 331 332 btstack_assert(outgoing_pdu->len <= 29); 333 334 #ifdef LOG_NETWORK 335 printf("TX-B-NetworkPDU (%p): ", outgoing_pdu); 336 printf_hexdump(outgoing_pdu->data, outgoing_pdu->len); 337 #endif 338 339 // calc PECB 340 memset(encryption_block, 0, 5); 341 big_endian_store_32(encryption_block, 5, iv_index); 342 (void)memcpy(&encryption_block[9], &outgoing_pdu->data[7], 7); 343 btstack_crypto_aes128_encrypt(&mesh_network_crypto_request.aes128, current_network_key->privacy_key, encryption_block, obfuscation_block, &mesh_network_send_c, NULL); 344 } 345 346 static void mesh_network_send_a(void){ 347 348 mesh_crypto_active = 1; 349 350 uint32_t iv_index = mesh_get_iv_index_for_tx(); 351 352 // lookup subnet by netkey_index 353 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(outgoing_pdu->netkey_index); 354 if (!subnet) { 355 mesh_crypto_active = 0; 356 mesh_network_pdu_t * network_pdu = outgoing_pdu; 357 outgoing_pdu = NULL; 358 // notify upper layer 359 mesh_network_send_complete(network_pdu); 360 // run again 361 mesh_network_run(); 362 return; 363 } 364 365 // get network key to use for sending 366 current_network_key = mesh_subnet_get_outgoing_network_key(subnet); 367 368 #ifdef LOG_NETWORK 369 printf("TX-A-NetworkPDU (%p): ", outgoing_pdu); 370 printf_hexdump(outgoing_pdu->data, outgoing_pdu->len); 371 #endif 372 373 // get network nonce 374 if (outgoing_pdu->flags & MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION){ 375 mesh_proxy_create_nonce(network_nonce, outgoing_pdu, iv_index); 376 #ifdef LOG_NETWORK 377 printf("TX-ProxyNonce: "); 378 printf_hexdump(network_nonce, 13); 379 #endif 380 } else { 381 mesh_network_create_nonce(network_nonce, outgoing_pdu, iv_index); 382 #ifdef LOG_NETWORK 383 printf("TX-NetworkNonce: "); 384 printf_hexdump(network_nonce, 13); 385 #endif 386 } 387 388 #ifdef LOG_NETWORK 389 printf("TX-EncryptionKey: "); 390 printf_hexdump(current_network_key->encryption_key, 16); 391 #endif 392 393 // start ccm 394 uint8_t cypher_len = outgoing_pdu->len - 7; 395 uint8_t net_mic_len = outgoing_pdu->data[1] & 0x80 ? 8 : 4; 396 btstack_crypto_ccm_init(&mesh_network_crypto_request.ccm, current_network_key->encryption_key, network_nonce, cypher_len, 0, net_mic_len); 397 btstack_crypto_ccm_encrypt_block(&mesh_network_crypto_request.ccm, cypher_len, &outgoing_pdu->data[7], &outgoing_pdu->data[7], &mesh_network_send_b, NULL); 398 } 399 400 #if defined(ENABLE_MESH_RELAY) || defined (ENABLE_MESH_PROXY_SERVER) 401 static void mesh_network_relay_message(mesh_network_pdu_t * network_pdu){ 402 403 uint8_t ctl_ttl = network_pdu->data[1]; 404 uint8_t ctl_in_bit_7 = ctl_ttl & 0x80; 405 uint8_t ttl = ctl_ttl & 0x7f; 406 407 // prepare pdu for resending 408 network_pdu->data[1] = ctl_in_bit_7 | (ttl - 1); 409 network_pdu->flags |= MESH_NETWORK_PDU_FLAGS_RELAY; 410 411 #ifdef LOG_NETWORK 412 printf("TX-Relay-NetworkPDU (%p): ", network_pdu); 413 printf_hexdump(network_pdu->data, network_pdu->len); 414 printf("^^ into network_pdus_queued\n"); 415 #endif 416 417 uint8_t net_mic_len = ctl_in_bit_7 ? 8 : 4; 418 btstack_assert((network_pdu->len + net_mic_len) <= 29); 419 420 // queue up 421 btstack_linked_list_add_tail(&network_pdus_queued, (btstack_linked_item_t *) network_pdu); 422 } 423 #endif 424 425 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu){ 426 427 #if defined(ENABLE_MESH_RELAY) || defined (ENABLE_MESH_PROXY_SERVER) 428 429 // check if address does not matches elements on our node and TTL >= 2 430 uint16_t src = mesh_network_src(network_pdu); 431 uint8_t ttl = mesh_network_ttl(network_pdu); 432 433 uint16_t mesh_network_primary_address = mesh_node_get_primary_element_address(); 434 435 if (((src < mesh_network_primary_address) || (src > (mesh_network_primary_address + mesh_node_element_count()))) && (ttl >= 2)){ 436 437 if ((network_pdu->flags & MESH_NETWORK_PDU_FLAGS_GATT_BEARER) == 0){ 438 439 // message received via ADV bearer are relayed: 440 441 #ifdef ENABLE_MESH_RELAY 442 if (mesh_foundation_relay_get() != 0){ 443 // - to ADV bearer, if Relay supported and enabledx 444 mesh_network_relay_message(network_pdu); 445 mesh_network_run(); 446 return; 447 } 448 #endif 449 450 #ifdef ENABLE_MESH_PROXY_SERVER 451 if (mesh_foundation_gatt_proxy_get() != 0){ 452 // - to GATT bearer, if Proxy supported and enabled 453 mesh_network_relay_message(network_pdu); 454 mesh_network_run(); 455 return; 456 } 457 #endif 458 459 } else { 460 461 // messages received via GATT bearer are relayed: 462 463 #ifdef ENABLE_MESH_PROXY_SERVER 464 if (mesh_foundation_gatt_proxy_get() != 0){ 465 // - to ADV bearer, if Proxy supported and enabled 466 mesh_network_relay_message(network_pdu); 467 mesh_network_run(); 468 return; 469 } 470 #endif 471 472 } 473 } 474 #endif 475 476 // otherwise, we're done 477 btstack_memory_mesh_network_pdu_free(network_pdu); 478 } 479 480 static void process_network_pdu_done(void){ 481 btstack_memory_mesh_network_pdu_free(incoming_pdu_raw); 482 incoming_pdu_raw = NULL; 483 mesh_crypto_active = 0; 484 485 mesh_network_run(); 486 } 487 488 static void process_network_pdu_validate_d(void * arg){ 489 UNUSED(arg); 490 // mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) arg; 491 492 uint8_t ctl_ttl = incoming_pdu_decoded->data[1]; 493 uint8_t ctl = ctl_ttl >> 7; 494 uint8_t net_mic_len = (ctl_ttl & 0x80) ? 8 : 4; 495 496 // store NetMIC 497 uint8_t net_mic[8]; 498 btstack_crypto_ccm_get_authentication_value(&mesh_network_crypto_request.ccm, net_mic); 499 #ifdef LOG_NETWORK 500 printf("RX-NetMIC (%p): ", incoming_pdu_decoded); 501 printf_hexdump(net_mic, net_mic_len); 502 #endif 503 // store in decoded pdu 504 (void)memcpy(&incoming_pdu_decoded->data[incoming_pdu_decoded->len - net_mic_len], 505 net_mic, net_mic_len); 506 507 #ifdef LOG_NETWORK 508 uint8_t cypher_len = incoming_pdu_decoded->len - 9 - net_mic_len; 509 printf("RX-Decrypted DST/TransportPDU (%p): ", incoming_pdu_decoded); 510 printf_hexdump(&incoming_pdu_decoded->data[7], 2 + cypher_len); 511 512 printf("RX-Decrypted: "); 513 printf_hexdump(incoming_pdu_decoded->data, incoming_pdu_decoded->len); 514 #endif 515 516 // validate network mic 517 if (memcmp(net_mic, &incoming_pdu_raw->data[incoming_pdu_decoded->len-net_mic_len], net_mic_len) != 0){ 518 // fail 519 printf("RX-NetMIC mismatch, try next key (%p)\n", incoming_pdu_decoded); 520 process_network_pdu_validate(); 521 return; 522 } 523 524 // remove NetMIC from payload 525 incoming_pdu_decoded->len -= net_mic_len; 526 527 #ifdef LOG_NETWORK 528 // match 529 printf("RX-NetMIC matches (%p)\n", incoming_pdu_decoded); 530 printf("RX-TTL (%p): 0x%02x\n", incoming_pdu_decoded, incoming_pdu_decoded->data[1] & 0x7f); 531 #endif 532 533 // set netkey_index 534 incoming_pdu_decoded->netkey_index = current_network_key->netkey_index; 535 536 if (incoming_pdu_decoded->flags & MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION){ 537 538 mesh_network_pdu_t * decoded_pdu = incoming_pdu_decoded; 539 incoming_pdu_decoded = NULL; 540 541 // no additional checks for proxy messages 542 (*mesh_network_proxy_message_handler)(MESH_NETWORK_PDU_RECEIVED, decoded_pdu); 543 544 } else { 545 546 // validate src/dest addresses 547 uint16_t src = big_endian_read_16(incoming_pdu_decoded->data, 5); 548 uint16_t dst = big_endian_read_16(incoming_pdu_decoded->data, 7); 549 int valid = mesh_network_addresses_valid(ctl, src, dst); 550 if (!valid){ 551 #ifdef LOG_NETWORK 552 printf("RX Address invalid (%p)\n", incoming_pdu_decoded); 553 #endif 554 btstack_memory_mesh_network_pdu_free(incoming_pdu_decoded); 555 incoming_pdu_decoded = NULL; 556 process_network_pdu_done(); 557 return; 558 } 559 560 // check cache 561 uint32_t hash = mesh_network_cache_hash(incoming_pdu_decoded); 562 #ifdef LOG_NETWORK 563 printf("RX-Hash (%p): %08x\n", incoming_pdu_decoded, hash); 564 #endif 565 if (mesh_network_cache_find(hash)){ 566 // found in cache, drop 567 #ifdef LOG_NETWORK 568 printf("Found in cache -> drop packet (%p)\n", incoming_pdu_decoded); 569 #endif 570 btstack_memory_mesh_network_pdu_free(incoming_pdu_decoded); 571 incoming_pdu_decoded = NULL; 572 process_network_pdu_done(); 573 return; 574 } 575 576 // store in network cache 577 mesh_network_cache_add(hash); 578 579 #ifdef LOG_NETWORK 580 printf("RX-Validated (%p) - forward to lower transport\n", incoming_pdu_decoded); 581 #endif 582 583 // forward to lower transport layer. message is freed by call to mesh_network_message_processed_by_upper_layer 584 mesh_network_pdu_t * decoded_pdu = incoming_pdu_decoded; 585 incoming_pdu_decoded = NULL; 586 (*mesh_network_higher_layer_handler)(MESH_NETWORK_PDU_RECEIVED, decoded_pdu); 587 } 588 589 // done 590 process_network_pdu_done(); 591 } 592 593 static uint32_t iv_index_for_pdu(const mesh_network_pdu_t * network_pdu){ 594 // get IV Index and IVI 595 uint32_t iv_index = mesh_get_iv_index(); 596 int ivi = network_pdu->data[0] >> 7; 597 598 // if least significant bit differs, use previous IV Index 599 if ((iv_index & 1 ) ^ ivi){ 600 iv_index--; 601 #ifdef LOG_NETWORK 602 printf("RX-IV: IVI indicates previous IV index, using 0x%08x\n", iv_index); 603 #endif 604 } 605 return iv_index; 606 } 607 608 static void process_network_pdu_validate_b(void * arg){ 609 UNUSED(arg); 610 611 #ifdef LOG_NETWORK 612 printf("RX-PECB: "); 613 printf_hexdump(obfuscation_block, 6); 614 #endif 615 616 // de-obfuscate 617 unsigned int i; 618 for (i=0;i<6;i++){ 619 incoming_pdu_decoded->data[1+i] = incoming_pdu_raw->data[1+i] ^ obfuscation_block[i]; 620 } 621 622 uint32_t iv_index = iv_index_for_pdu(incoming_pdu_raw); 623 624 if (incoming_pdu_decoded->flags & MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION){ 625 // create network nonce 626 mesh_proxy_create_nonce(network_nonce, incoming_pdu_decoded, iv_index); 627 #ifdef LOG_NETWORK 628 printf("RX-Proxy Nonce: "); 629 printf_hexdump(network_nonce, 13); 630 #endif 631 } else { 632 // create network nonce 633 mesh_network_create_nonce(network_nonce, incoming_pdu_decoded, iv_index); 634 #ifdef LOG_NETWORK 635 printf("RX-Network Nonce: "); 636 printf_hexdump(network_nonce, 13); 637 #endif 638 } 639 640 // 641 uint8_t ctl_ttl = incoming_pdu_decoded->data[1]; 642 uint8_t net_mic_len = (ctl_ttl & 0x80) ? 8 : 4; 643 uint8_t cypher_len = incoming_pdu_decoded->len - 7 - net_mic_len; 644 645 #ifdef LOG_NETWORK 646 printf("RX-Cyper len %u, mic len %u\n", cypher_len, net_mic_len); 647 648 printf("RX-Encryption Key: "); 649 printf_hexdump(current_network_key->encryption_key, 16); 650 651 #endif 652 653 btstack_crypto_ccm_init(&mesh_network_crypto_request.ccm, current_network_key->encryption_key, network_nonce, cypher_len, 0, net_mic_len); 654 btstack_crypto_ccm_decrypt_block(&mesh_network_crypto_request.ccm, cypher_len, &incoming_pdu_raw->data[7], &incoming_pdu_decoded->data[7], &process_network_pdu_validate_d, incoming_pdu_decoded); 655 } 656 657 static void process_network_pdu_validate(void){ 658 if (!mesh_network_key_nid_iterator_has_more(&validation_network_key_it)){ 659 printf("No valid network key found\n"); 660 btstack_memory_mesh_network_pdu_free(incoming_pdu_decoded); 661 incoming_pdu_decoded = NULL; 662 process_network_pdu_done(); 663 return; 664 } 665 666 current_network_key = mesh_network_key_nid_iterator_get_next(&validation_network_key_it); 667 668 // calc PECB 669 uint32_t iv_index = iv_index_for_pdu(incoming_pdu_raw); 670 memset(encryption_block, 0, 5); 671 big_endian_store_32(encryption_block, 5, iv_index); 672 (void)memcpy(&encryption_block[9], &incoming_pdu_raw->data[7], 7); 673 btstack_crypto_aes128_encrypt(&mesh_network_crypto_request.aes128, current_network_key->privacy_key, encryption_block, obfuscation_block, &process_network_pdu_validate_b, NULL); 674 } 675 676 677 static void process_network_pdu(void){ 678 // 679 uint8_t nid_ivi = incoming_pdu_raw->data[0]; 680 681 // setup pdu object 682 incoming_pdu_decoded->data[0] = nid_ivi; 683 incoming_pdu_decoded->len = incoming_pdu_raw->len; 684 incoming_pdu_decoded->flags = incoming_pdu_raw->flags; 685 686 // init provisioning data iterator 687 uint8_t nid = nid_ivi & 0x7f; 688 // uint8_t iv_index = network_pdu_data[0] >> 7; 689 mesh_network_key_nid_iterator_init(&validation_network_key_it, nid); 690 691 process_network_pdu_validate(); 692 } 693 694 // returns true if done 695 static bool mesh_network_run_gatt(void){ 696 if (btstack_linked_list_empty(&network_pdus_outgoing_gatt)){ 697 return true; 698 } 699 700 #ifdef ENABLE_MESH_GATT_BEARER 701 if (gatt_bearer_network_pdu != NULL){ 702 return true; 703 } 704 705 // move to 'gatt bearer queue' 706 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_outgoing_gatt); 707 708 #ifdef LOG_NETWORK 709 printf("network run 1: pop %p from network_pdus_outgoing_gatt\n", network_pdu); 710 #endif 711 // request to send via gatt if: 712 // proxy active and connected 713 // packet wasn't received via gatt bearer 714 int send_via_gatt = ((mesh_foundation_gatt_proxy_get() != 0) && 715 (gatt_bearer_con_handle != HCI_CON_HANDLE_INVALID) && 716 ((network_pdu->flags & MESH_NETWORK_PDU_FLAGS_GATT_BEARER) == 0)); 717 if (send_via_gatt){ 718 719 #ifdef LOG_NETWORK 720 printf("network run 2: set %p as gatt_bearer_network_pdu\n", network_pdu); 721 #endif 722 gatt_bearer_network_pdu = network_pdu; 723 gatt_bearer_request_can_send_now_for_network_pdu(); 724 725 } else { 726 727 #ifdef LOG_NETWORK 728 printf("network run 3: push %p to network_pdus_outgoing_adv\n", network_pdu); 729 #endif 730 btstack_linked_list_add_tail(&network_pdus_outgoing_adv, (btstack_linked_item_t *) network_pdu); 731 732 #ifdef LOG_NETWORK 733 mesh_network_dump_network_pdus("network_pdus_outgoing_adv (1)", &network_pdus_outgoing_adv); 734 #endif 735 } 736 #else 737 // directly move to 'outgoing adv bearer queue' 738 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_outgoing_gatt); 739 btstack_linked_list_add_tail(&network_pdus_outgoing_adv, (btstack_linked_item_t *) network_pdu); 740 #endif 741 return false; 742 } 743 744 // returns true if done 745 static bool mesh_network_run_adv(void){ 746 747 if (btstack_linked_list_empty(&network_pdus_outgoing_adv)){ 748 return true; 749 } 750 751 #ifdef ENABLE_MESH_ADV_BEARER 752 if (adv_bearer_network_pdu != NULL){ 753 return true; 754 } 755 756 // move to 'adv bearer queue' 757 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_outgoing_adv); 758 759 #ifdef LOG_NETWORK 760 printf("network run 4: pop %p from network_pdus_outgoing_adv\n", network_pdu); 761 mesh_network_dump_network_pdus("network_pdus_outgoing_adv (3)", &network_pdus_outgoing_adv); 762 printf("network run 5: %p -> flags 0x%02x, gatt_proxy %u, relay %u\n", network_pdu, network_pdu->flags, mesh_foundation_gatt_proxy_get(), mesh_foundation_relay_get()); 763 #endif 764 765 // send via adv if: 766 // packet was received via gatt bearer and proxy active, or, 767 // packet originated locally (== not relayed), or, 768 // packet was received via ADV bearer and relay is active, or, 769 int send_via_adv = (((network_pdu->flags & MESH_NETWORK_PDU_FLAGS_GATT_BEARER) != 0) && (mesh_foundation_gatt_proxy_get() == 1)) || 770 (((network_pdu->flags & MESH_NETWORK_PDU_FLAGS_GATT_BEARER) == 0) && (mesh_foundation_relay_get() == 1)) || 771 ((network_pdu->flags & MESH_NETWORK_PDU_FLAGS_RELAY) == 0); 772 773 if (send_via_adv){ 774 #ifdef LOG_NETWORK 775 printf("network run 6: set %p as to adv_bearer_network_pdu\n", network_pdu); 776 #endif 777 adv_bearer_network_pdu = network_pdu; 778 adv_bearer_request_can_send_now_for_network_pdu(); 779 } else { 780 #ifdef LOG_NETWORK 781 printf("network run 7: skip sending %p via adv bearer\n", network_pdu); 782 #endif 783 // directly notify upper layer 784 mesh_network_send_complete(network_pdu); 785 } 786 #else 787 // done 788 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_outgoing_adv); 789 // directly notify upper layer 790 mesh_network_send_complete(network_pdu); 791 #endif 792 return false; 793 } 794 795 // returns true if done 796 static bool mesh_network_run_received(void){ 797 if (mesh_crypto_active) { 798 return true; 799 } 800 801 if (btstack_linked_list_empty(&network_pdus_received)) { 802 return true; 803 } 804 805 incoming_pdu_decoded = mesh_network_pdu_get(); 806 if (incoming_pdu_decoded == NULL) return true; 807 808 // get encoded network pdu and start processing 809 mesh_crypto_active = 1; 810 incoming_pdu_raw = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_received); 811 process_network_pdu(); 812 return true; 813 } 814 815 // returns true if done 816 static bool mesh_network_run_queued(void){ 817 if (mesh_crypto_active) { 818 return true; 819 } 820 821 if (btstack_linked_list_empty(&network_pdus_queued)){ 822 return true; 823 } 824 825 // get queued network pdu and start processing 826 outgoing_pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(&network_pdus_queued); 827 828 #ifdef LOG_NETWORK 829 printf("network run 5: pop %p from network_pdus_queued\n", outgoing_pdu); 830 mesh_network_dump_network_pdus("network_pdus_queued (2)", &network_pdus_queued); 831 #endif 832 mesh_network_send_a(); 833 return true; 834 } 835 836 static void mesh_network_run(void){ 837 while (true){ 838 bool done = true; 839 done &= mesh_network_run_gatt(); 840 done &= mesh_network_run_adv(); 841 done &= mesh_network_run_received(); 842 done &= mesh_network_run_queued(); 843 if (done) break; 844 } 845 } 846 847 #ifdef ENABLE_MESH_ADV_BEARER 848 static void mesh_adv_bearer_handle_network_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 849 UNUSED(channel); 850 mesh_network_pdu_t * network_pdu; 851 uint8_t transmission_count; 852 uint16_t transmission_interval; 853 uint8_t transmit_config; 854 855 switch (packet_type){ 856 case MESH_NETWORK_PACKET: 857 // check len. minimal transport PDU len = 1, 32 bit NetMIC -> 13 bytes 858 if (size < 13) break; 859 860 #ifdef LOG_NETWORK 861 printf("received network pdu from adv (len %u): ", size); 862 printf_hexdump(packet, size); 863 #endif 864 mesh_network_received_message(packet, size, 0); 865 break; 866 867 case HCI_EVENT_PACKET: 868 switch(packet[0]){ 869 case HCI_EVENT_MESH_META: 870 switch(packet[2]){ 871 case MESH_SUBEVENT_CAN_SEND_NOW: 872 if (adv_bearer_network_pdu == NULL) break; 873 874 // Get Transmission config depending on relay flag 875 if (adv_bearer_network_pdu->flags & MESH_NETWORK_PDU_FLAGS_RELAY){ 876 transmit_config = mesh_foundation_relay_get(); 877 } else { 878 transmit_config = mesh_foundation_network_transmit_get(); 879 } 880 transmission_count = (transmit_config & 0x07) + 1; 881 transmission_interval = (transmit_config >> 3) * 10; 882 883 #ifdef LOG_NETWORK 884 printf("TX-E-NetworkPDU (%p) count %u, interval %u ms: ", adv_bearer_network_pdu, transmission_count, transmission_interval); 885 printf_hexdump(adv_bearer_network_pdu->data, adv_bearer_network_pdu->len); 886 #endif 887 888 adv_bearer_send_network_pdu(adv_bearer_network_pdu->data, adv_bearer_network_pdu->len, transmission_count, transmission_interval); 889 network_pdu = adv_bearer_network_pdu; 890 adv_bearer_network_pdu = NULL; 891 892 // notify upper layer 893 mesh_network_send_complete(network_pdu); 894 895 // check if more to send 896 mesh_network_run(); 897 break; 898 default: 899 break; 900 } 901 break; 902 default: 903 break; 904 } 905 break; 906 } 907 } 908 #endif 909 910 #ifdef ENABLE_MESH_GATT_BEARER 911 static void mesh_network_gatt_bearer_outgoing_complete(void){ 912 913 if (gatt_bearer_network_pdu == NULL) return; 914 915 // forward to adv bearer 916 btstack_linked_list_add_tail(&network_pdus_outgoing_adv, (btstack_linked_item_t*) gatt_bearer_network_pdu); 917 gatt_bearer_network_pdu = NULL; 918 919 mesh_network_run(); 920 return; 921 } 922 923 static void mesh_network_gatt_bearer_handle_network_event(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 924 UNUSED(channel); 925 switch (packet_type){ 926 case MESH_PROXY_DATA_PACKET: 927 if (mesh_foundation_gatt_proxy_get() == 0) break; 928 #ifdef LOG_NETWORK 929 printf("received network pdu from gatt (len %u): ", size); 930 printf_hexdump(packet, size); 931 #endif 932 mesh_network_received_message(packet, size, MESH_NETWORK_PDU_FLAGS_GATT_BEARER); 933 break; 934 case HCI_EVENT_PACKET: 935 switch (hci_event_packet_get_type(packet)){ 936 case HCI_EVENT_MESH_META: 937 switch (hci_event_mesh_meta_get_subevent_code(packet)){ 938 case MESH_SUBEVENT_PROXY_CONNECTED: 939 gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet); 940 break; 941 case MESH_SUBEVENT_PROXY_DISCONNECTED: 942 gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID; 943 mesh_network_gatt_bearer_outgoing_complete(); 944 break; 945 case MESH_SUBEVENT_CAN_SEND_NOW: 946 if (gatt_bearer_network_pdu == NULL) break; 947 #ifdef LOG_NETWORK 948 printf("G-TX-E-NetworkPDU (%p): ", gatt_bearer_network_pdu); 949 printf_hexdump(gatt_bearer_network_pdu->data, gatt_bearer_network_pdu->len); 950 #endif 951 gatt_bearer_send_network_pdu(gatt_bearer_network_pdu->data, gatt_bearer_network_pdu->len); 952 break; 953 954 case MESH_SUBEVENT_MESSAGE_SENT: 955 mesh_network_gatt_bearer_outgoing_complete(); 956 break; 957 default: 958 break; 959 } 960 break; 961 default: 962 break; 963 } 964 break; 965 default: 966 break; 967 } 968 } 969 #endif 970 971 #ifdef ENABLE_MESH_GATT_BEARER 972 static void mesh_netework_gatt_bearer_handle_proxy_configuration(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 973 UNUSED(channel); 974 switch (packet_type){ 975 case MESH_PROXY_DATA_PACKET: 976 mesh_network_process_proxy_configuration_message(packet, size); 977 break; 978 case HCI_EVENT_PACKET: 979 switch (hci_event_packet_get_type(packet)){ 980 case HCI_EVENT_MESH_META: 981 switch (hci_event_mesh_meta_get_subevent_code(packet)){ 982 case MESH_SUBEVENT_CAN_SEND_NOW: 983 // forward to higher layer 984 (*mesh_network_proxy_message_handler)(MESH_NETWORK_CAN_SEND_NOW, NULL); 985 break; 986 default: 987 break; 988 } 989 break; 990 default: 991 break; 992 } 993 break; 994 default: 995 break; 996 } 997 } 998 #endif 999 1000 void mesh_network_init(void){ 1001 #ifdef ENABLE_MESH_ADV_BEARER 1002 adv_bearer_register_for_network_pdu(&mesh_adv_bearer_handle_network_event); 1003 #endif 1004 #ifdef ENABLE_MESH_GATT_BEARER 1005 gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID; 1006 gatt_bearer_register_for_network_pdu(&mesh_network_gatt_bearer_handle_network_event); 1007 gatt_bearer_register_for_mesh_proxy_configuration(&mesh_netework_gatt_bearer_handle_proxy_configuration); 1008 #endif 1009 } 1010 1011 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)){ 1012 mesh_network_higher_layer_handler = packet_handler; 1013 } 1014 1015 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)){ 1016 mesh_network_proxy_message_handler = packet_handler; 1017 } 1018 1019 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags){ 1020 // verify len 1021 if (pdu_len > 29) return; 1022 1023 // allocate network_pdu 1024 mesh_network_pdu_t * network_pdu = mesh_network_pdu_get(); 1025 if (!network_pdu) return; 1026 1027 // store data 1028 (void)memcpy(network_pdu->data, pdu_data, pdu_len); 1029 network_pdu->len = pdu_len; 1030 network_pdu->flags = flags; 1031 1032 // add to list and go 1033 btstack_linked_list_add_tail(&network_pdus_received, (btstack_linked_item_t *) network_pdu); 1034 mesh_network_run(); 1035 1036 } 1037 1038 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len){ 1039 // verify len 1040 if (pdu_len > 29) return; 1041 1042 // allocate network_pdu 1043 mesh_network_pdu_t * network_pdu = mesh_network_pdu_get(); 1044 if (!network_pdu) return; 1045 1046 // store data 1047 (void)memcpy(network_pdu->data, pdu_data, pdu_len); 1048 network_pdu->len = pdu_len; 1049 network_pdu->flags = MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION; // Network PDU 1050 1051 // add to list and go 1052 btstack_linked_list_add_tail(&network_pdus_received, (btstack_linked_item_t *) network_pdu); 1053 mesh_network_run(); 1054 } 1055 1056 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu){ 1057 #ifdef LOG_NETWORK 1058 printf("TX-NetworkPDU (%p): ", network_pdu); 1059 printf_hexdump(network_pdu->data, network_pdu->len); 1060 printf("^^ into network_pdus_queued\n"); 1061 #endif 1062 1063 btstack_assert((network_pdu->len + (network_pdu->data[1] & 0x80 ? 8 : 4)) <= 29); 1064 btstack_assert(network_pdu->len >= 9); 1065 1066 // setup callback 1067 network_pdu->flags = 0; 1068 1069 // queue up 1070 btstack_linked_list_add_tail(&network_pdus_queued, (btstack_linked_item_t *) network_pdu); 1071 #ifdef LOG_NETWORK 1072 mesh_network_dump_network_pdus("network_pdus_queued", &network_pdus_queued); 1073 #endif 1074 1075 // go 1076 mesh_network_run(); 1077 } 1078 1079 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu){ 1080 printf("ProxyPDU(unencrypted): "); 1081 printf_hexdump(network_pdu->data, network_pdu->len); 1082 1083 // setup callback 1084 network_pdu->flags = MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION; 1085 1086 // queue up 1087 btstack_linked_list_add_tail(&network_pdus_queued, (btstack_linked_item_t *) network_pdu); 1088 1089 // go 1090 mesh_network_run(); 1091 } 1092 1093 /* 1094 * @brief Setup network pdu header 1095 * @param netkey_index 1096 * @param ctl 1097 * @param ttl 1098 * @param seq 1099 * @param dest 1100 */ 1101 void mesh_network_setup_pdu(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t nid, uint8_t ctl, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dest, const uint8_t * transport_pdu_data, uint8_t transport_pdu_len){ 1102 // set netkey_index 1103 network_pdu->netkey_index = netkey_index; 1104 // setup header 1105 network_pdu->len = 0; 1106 network_pdu->data[network_pdu->len++] = (mesh_get_iv_index_for_tx() << 7) | nid; 1107 uint8_t ctl_ttl = (ctl << 7) | (ttl & 0x7f); 1108 network_pdu->data[network_pdu->len++] = ctl_ttl; 1109 big_endian_store_24(network_pdu->data, 2, seq); 1110 network_pdu->len += 3; 1111 big_endian_store_16(network_pdu->data, network_pdu->len, src); 1112 network_pdu->len += 2; 1113 big_endian_store_16(network_pdu->data, network_pdu->len, dest); 1114 network_pdu->len += 2; 1115 btstack_assert((network_pdu->len + transport_pdu_len) <= MESH_NETWORK_PAYLOAD_MAX); 1116 (void)memcpy(&network_pdu->data[network_pdu->len], transport_pdu_data, 1117 transport_pdu_len); 1118 network_pdu->len += transport_pdu_len; 1119 // zero rest of packet 1120 memset(&network_pdu->data[network_pdu->len], 0, MESH_NETWORK_PAYLOAD_MAX - network_pdu->len); 1121 } 1122 1123 /* 1124 * @brief Setup network pdu header 1125 * @param netkey_index 1126 * @param ctl 1127 * @param ttl 1128 * @param seq 1129 * @param dest 1130 */ 1131 void mesh_network_setup_pdu_header(mesh_network_pdu_t * network_pdu, uint16_t netkey_index, uint8_t nid, uint8_t ctl, uint8_t ttl, uint32_t seq, uint16_t src, uint16_t dest){ 1132 // set netkey_index 1133 network_pdu->netkey_index = netkey_index; 1134 // setup header 1135 network_pdu->data[0] = (mesh_get_iv_index_for_tx() << 7) | nid; 1136 uint8_t ctl_ttl = (ctl << 7) | (ttl & 0x7f); 1137 network_pdu->data[1] = ctl_ttl; 1138 big_endian_store_24(network_pdu->data, 2, seq); 1139 big_endian_store_16(network_pdu->data, 5, src); 1140 big_endian_store_16(network_pdu->data, 7, dest); 1141 } 1142 1143 // Network PDU Getter 1144 uint8_t mesh_network_nid(mesh_network_pdu_t * network_pdu){ 1145 return network_pdu->data[0] & 0x7f; 1146 } 1147 uint16_t mesh_network_control(mesh_network_pdu_t * network_pdu){ 1148 return network_pdu->data[1] & 0x80; 1149 } 1150 uint8_t mesh_network_ttl(mesh_network_pdu_t * network_pdu){ 1151 return network_pdu->data[1] & 0x7f; 1152 } 1153 uint32_t mesh_network_seq(mesh_network_pdu_t * network_pdu){ 1154 return big_endian_read_24(network_pdu->data, 2); 1155 } 1156 uint16_t mesh_network_src(mesh_network_pdu_t * network_pdu){ 1157 return big_endian_read_16(network_pdu->data, 5); 1158 } 1159 uint16_t mesh_network_dst(mesh_network_pdu_t * network_pdu){ 1160 return big_endian_read_16(network_pdu->data, 7); 1161 } 1162 int mesh_network_segmented(mesh_network_pdu_t * network_pdu){ 1163 return network_pdu->data[9] & 0x80; 1164 } 1165 uint8_t mesh_network_control_opcode(mesh_network_pdu_t * network_pdu){ 1166 return network_pdu->data[9] & 0x7f; 1167 } 1168 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu){ 1169 return &network_pdu->data[9]; 1170 } 1171 uint8_t mesh_network_pdu_len(mesh_network_pdu_t * network_pdu){ 1172 return network_pdu->len - 9; 1173 } 1174 1175 void mesh_network_pdu_set_seq(mesh_network_pdu_t * network_pdu, uint32_t seq){ 1176 big_endian_store_24(network_pdu->data, 2, seq); 1177 } 1178 1179 static void mesh_network_dump_network_pdu(mesh_network_pdu_t * network_pdu){ 1180 if (network_pdu){ 1181 printf("- %p: ", network_pdu); printf_hexdump(network_pdu->data, network_pdu->len); 1182 } 1183 } 1184 static void mesh_network_dump_network_pdus(const char * name, btstack_linked_list_t * list){ 1185 printf("List: %s:\n", name); 1186 btstack_linked_list_iterator_t it; 1187 btstack_linked_list_iterator_init(&it, list); 1188 while (btstack_linked_list_iterator_has_next(&it)){ 1189 mesh_network_pdu_t * network_pdu = (mesh_network_pdu_t*) btstack_linked_list_iterator_next(&it); 1190 mesh_network_dump_network_pdu(network_pdu); 1191 } 1192 } 1193 static void mesh_network_reset_network_pdus(btstack_linked_list_t * list){ 1194 while (!btstack_linked_list_empty(list)){ 1195 mesh_network_pdu_t * pdu = (mesh_network_pdu_t *) btstack_linked_list_pop(list); 1196 btstack_memory_mesh_network_pdu_free(pdu); 1197 } 1198 } 1199 void mesh_network_dump(void){ 1200 mesh_network_dump_network_pdus("network_pdus_received", &network_pdus_received); 1201 mesh_network_dump_network_pdus("network_pdus_queued", &network_pdus_queued); 1202 mesh_network_dump_network_pdus("network_pdus_outgoing_gatt", &network_pdus_outgoing_gatt); 1203 mesh_network_dump_network_pdus("network_pdus_outgoing_adv", &network_pdus_outgoing_adv); 1204 printf("outgoing_pdu: \n"); 1205 mesh_network_dump_network_pdu(outgoing_pdu); 1206 printf("incoming_pdu_raw: \n"); 1207 mesh_network_dump_network_pdu(incoming_pdu_raw); 1208 #ifdef ENABLE_MESH_GATT_BEARER 1209 printf("gatt_bearer_network_pdu: \n"); 1210 mesh_network_dump_network_pdu(gatt_bearer_network_pdu); 1211 #endif 1212 #ifdef ENABLE_MESH_ADV_BEARER 1213 printf("adv_bearer_network_pdu: \n"); 1214 mesh_network_dump_network_pdu(adv_bearer_network_pdu); 1215 #endif 1216 1217 } 1218 void mesh_network_reset(void){ 1219 mesh_network_reset_network_pdus(&network_pdus_received); 1220 mesh_network_reset_network_pdus(&network_pdus_queued); 1221 mesh_network_reset_network_pdus(&network_pdus_outgoing_gatt); 1222 mesh_network_reset_network_pdus(&network_pdus_outgoing_adv); 1223 1224 // outgoing network pdus are owned by higher layer, so we don't free: 1225 // - adv_bearer_network_pdu 1226 // - gatt_bearer_network_pdu 1227 // - outoing_pdu 1228 #ifdef ENABLE_MESH_ADV_BEARER 1229 adv_bearer_network_pdu = NULL; 1230 #endif 1231 #ifdef ENABLE_MESH_GATT_BEARER 1232 gatt_bearer_network_pdu = NULL; 1233 #endif 1234 outgoing_pdu = NULL; 1235 1236 if (incoming_pdu_raw){ 1237 mesh_network_pdu_free(incoming_pdu_raw); 1238 incoming_pdu_raw = NULL; 1239 } 1240 if (incoming_pdu_decoded){ 1241 mesh_network_pdu_free(incoming_pdu_decoded); 1242 incoming_pdu_decoded = NULL; 1243 } 1244 mesh_crypto_active = 0; 1245 } 1246 1247 // buffer pool 1248 mesh_network_pdu_t * mesh_network_pdu_get(void){ 1249 mesh_network_pdu_t * network_pdu = btstack_memory_mesh_network_pdu_get(); 1250 if (network_pdu) { 1251 memset(network_pdu, 0, sizeof(mesh_network_pdu_t)); 1252 network_pdu->pdu_header.pdu_type = MESH_PDU_TYPE_NETWORK; 1253 } 1254 return network_pdu; 1255 } 1256 1257 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu){ 1258 btstack_memory_mesh_network_pdu_free(network_pdu); 1259 if (mesh_network_free_pdu_callback!=NULL){ 1260 void (*callback)(void) = mesh_network_free_pdu_callback; 1261 mesh_network_free_pdu_callback= NULL; 1262 (*callback)(); 1263 } 1264 } 1265 1266 void mesh_network_notify_on_freed_pdu(void (*callback)(void)){ 1267 btstack_assert(mesh_network_free_pdu_callback == NULL); 1268 mesh_network_free_pdu_callback = callback; 1269 } 1270 1271 1272 // Mesh Subnet Management 1273 1274 void mesh_subnet_add(mesh_subnet_t * subnet){ 1275 btstack_linked_list_add_tail(&subnets, (btstack_linked_item_t *) subnet); 1276 } 1277 1278 void mesh_subnet_remove(mesh_subnet_t * subnet){ 1279 btstack_linked_list_remove(&subnets, (btstack_linked_item_t *) subnet); 1280 } 1281 1282 mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index){ 1283 btstack_linked_list_iterator_t it; 1284 btstack_linked_list_iterator_init(&it, &subnets); 1285 while (btstack_linked_list_iterator_has_next(&it)){ 1286 mesh_subnet_t * item = (mesh_subnet_t *) btstack_linked_list_iterator_next(&it); 1287 if (item->netkey_index == netkey_index) return item; 1288 } 1289 return NULL; 1290 } 1291 1292 int mesh_subnet_list_count(void){ 1293 return btstack_linked_list_count(&subnets); 1294 } 1295 1296 // mesh network key iterator over all keys 1297 void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it){ 1298 btstack_linked_list_iterator_init(&it->it, &subnets); 1299 } 1300 1301 int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it){ 1302 return btstack_linked_list_iterator_has_next(&it->it); 1303 } 1304 1305 mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it){ 1306 return (mesh_subnet_t *) btstack_linked_list_iterator_next(&it->it); 1307 } 1308 1309 mesh_network_key_t * mesh_subnet_get_outgoing_network_key(mesh_subnet_t * subnet){ 1310 switch (subnet->key_refresh){ 1311 case MESH_KEY_REFRESH_SECOND_PHASE: 1312 return subnet->new_key; 1313 case MESH_KEY_REFRESH_NOT_ACTIVE: 1314 case MESH_KEY_REFRESH_FIRST_PHASE: 1315 default: 1316 return subnet->old_key; 1317 } 1318 } 1319 1320 /** 1321 * @brief Setup subnet for given netkey index 1322 */ 1323 void mesh_subnet_setup_for_netkey_index(uint16_t netkey_index){ 1324 mesh_subnet_t * subnet = mesh_subnet_get_by_netkey_index(netkey_index); 1325 if (subnet != NULL) return; 1326 1327 // find old / new keys 1328 mesh_network_key_t * old_key = NULL; 1329 mesh_network_key_t * new_key = NULL; 1330 mesh_network_key_iterator_t it; 1331 mesh_network_key_iterator_init(&it); 1332 while (mesh_network_key_iterator_has_more(&it)){ 1333 mesh_network_key_t * network_key = mesh_network_key_iterator_get_next(&it); 1334 if (network_key->netkey_index != netkey_index) continue; 1335 if (old_key == NULL){ 1336 old_key = network_key; 1337 continue; 1338 } 1339 // assign current key depending on key version 1340 if (((int8_t) (network_key->version - new_key->version)) > 0) { 1341 new_key = network_key; 1342 } else { 1343 new_key = old_key; 1344 old_key = network_key; 1345 } 1346 } 1347 1348 // create subnet for netkey index 1349 subnet = btstack_memory_mesh_subnet_get(); 1350 if (subnet == NULL) return; 1351 subnet->netkey_index = netkey_index; 1352 mesh_subnet_add(subnet); 1353 1354 // set keys 1355 subnet->old_key = old_key; 1356 subnet->new_key = new_key; 1357 1358 // key refresh 1359 if (new_key == NULL){ 1360 // single key -> key refresh not active 1361 subnet->key_refresh = MESH_KEY_REFRESH_NOT_ACTIVE; 1362 } 1363 else { 1364 // two keys -> at least phase 1 1365 subnet->key_refresh = MESH_KEY_REFRESH_FIRST_PHASE; 1366 } 1367 } 1368