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 #ifndef __MESH_NETWORK 39 #define __MESH_NETWORK 40 41 #include "btstack_linked_list.h" 42 #include "btstack_run_loop.h" 43 44 #include "mesh/provisioning.h" 45 #include "mesh/mesh_keys.h" 46 47 #if defined __cplusplus 48 extern "C" { 49 #endif 50 51 #define MESH_DEVICE_KEY_INDEX 0xffff 52 53 #define MESH_NETWORK_PAYLOAD_MAX 29 54 #define MESH_ACCESS_PAYLOAD_MAX 384 55 #define MESH_CONTROL_PAYLOAD_MAX 256 56 57 #define MESH_ADDRESS_UNSASSIGNED 0x0000u 58 #define MESH_ADDRESS_ALL_PROXIES 0xFFFCu 59 #define MESH_ADDRESS_ALL_FRIENDS 0xFFFDu 60 #define MESH_ADDRESS_ALL_RELAYS 0xFFFEu 61 #define MESH_ADDRESS_ALL_NODES 0xFFFFu 62 63 typedef enum { 64 MESH_NETWORK_PDU_RECEIVED, 65 MESH_NETWORK_PDU_SENT, 66 MESH_NETWORK_PDU_ENCRYPTED, 67 MESH_NETWORK_CAN_SEND_NOW, 68 } mesh_network_callback_type_t; 69 70 typedef enum { 71 MESH_PDU_TYPE_NETWORK = 0, 72 MESH_PDU_TYPE_TRANSPORT, 73 MESH_PDU_TYPE_SEGMENTED, 74 MESH_PDU_TYPE_UNSEGMENTED, 75 MESH_PDU_TYPE_ACCESS, 76 MESH_PDU_TYPE_CONTROL, 77 MESH_PDU_TYPE_UPPER_SEGMENTED_ACCESS, 78 MESH_PDU_TYPE_UPPER_UNSEGMENTED_ACCESS, 79 MESH_PDU_TYPE_UPPER_SEGMENTED_CONTROL, 80 MESH_PDU_TYPE_UPPER_UNSEGMENTED_CONTROL, 81 } mesh_pdu_type_t; 82 83 typedef struct mesh_pdu { 84 // allow for linked lists 85 btstack_linked_item_t item; 86 // type 87 mesh_pdu_type_t pdu_type; 88 89 } mesh_pdu_t; 90 91 // 92 #define MESH_NETWORK_PDU_FLAGS_PROXY_CONFIGURATION 1 93 #define MESH_NETWORK_PDU_FLAGS_GATT_BEARER 2 94 #define MESH_NETWORK_PDU_FLAGS_RELAY 4 95 96 typedef struct mesh_network_pdu { 97 mesh_pdu_t pdu_header; 98 99 // meta data network layer 100 uint16_t netkey_index; 101 // MESH_NETWORK_PDU_FLAGS 102 uint16_t flags; 103 104 // pdu 105 uint16_t len; 106 uint8_t data[MESH_NETWORK_PAYLOAD_MAX]; 107 } mesh_network_pdu_t; 108 109 #define MESH_TRANSPORT_FLAG_SEQ_RESERVED 1 110 #define MESH_TRANSPORT_FLAG_CONTROL 2 111 112 typedef struct { 113 // generic pdu header 114 mesh_pdu_t pdu_header; 115 // meta data transport layer 116 uint16_t appkey_index; 117 // MESH_TRANSPORT_FLAG 118 uint16_t flags; 119 // pdu segment 120 mesh_network_pdu_t * segment; 121 } mesh_unsegmented_pdu_t; 122 123 typedef struct { 124 mesh_pdu_t pdu_header; 125 126 // rx/tx: acknowledgement timer / segment transmission timer 127 btstack_timer_source_t acknowledgement_timer; 128 // rx: incomplete timer / tx: resend timer 129 btstack_timer_source_t incomplete_timer; 130 // block access 131 uint32_t block_ack; 132 // meta data network layer 133 uint16_t netkey_index; 134 // meta data transport layer 135 uint16_t appkey_index; 136 // transmic size 137 uint8_t transmic_len; 138 // akf - aid for access, opcode for control 139 uint8_t akf_aid_control; 140 // network pdu header 141 uint8_t network_header[9]; 142 // MESH_TRANSPORT_FLAG 143 uint16_t flags; 144 // acknowledgement timer active 145 uint8_t acknowledgement_timer_active; 146 // incomplete timer active 147 uint8_t incomplete_timer_active; 148 // message complete 149 uint8_t message_complete; 150 // seq_zero for segmented messages 151 uint16_t seq_zero; 152 // pdu segments 153 uint16_t len; 154 btstack_linked_list_t segments; 155 } mesh_segmented_pdu_t; 156 157 typedef struct { 158 mesh_pdu_t pdu_header; 159 160 // meta data network layer 161 uint16_t netkey_index; 162 // meta data transport layer 163 uint16_t appkey_index; 164 // transmic size 165 uint8_t transmic_len; 166 // akf - aid for access, opcode for control 167 uint8_t akf_aid_control; 168 // network pdu header 169 uint8_t network_header[9]; 170 // MESH_TRANSPORT_FLAG 171 uint16_t flags; 172 // pdu 173 uint16_t len; 174 uint8_t data[MESH_ACCESS_PAYLOAD_MAX]; 175 } mesh_transport_pdu_t; 176 177 typedef struct { 178 // generic pdu header 179 mesh_pdu_t pdu_header; 180 // network header 181 uint8_t ivi_nid; 182 uint8_t ctl_ttl; 183 uint16_t src; 184 uint16_t dst; 185 uint32_t seq; 186 // meta data network layer 187 uint16_t netkey_index; 188 // meta data transport layer 189 uint16_t appkey_index; 190 // transmic size 191 uint8_t transmic_len; 192 // akf - aid for access, opcode for control 193 uint8_t akf_aid_control; 194 // MESH_TRANSPORT_FLAG 195 uint16_t flags; 196 // payload 197 uint16_t len; 198 uint8_t data[MESH_ACCESS_PAYLOAD_MAX]; 199 200 } mesh_access_pdu_t; 201 202 // for unsegmented + segmented access + segmented control pdus 203 typedef struct { 204 // generic pdu header 205 mesh_pdu_t pdu_header; 206 // ivi_nid 207 uint8_t ivi_nid; 208 // ctl_ttl 209 uint8_t ctl_ttl; 210 // src 211 uint16_t src; 212 // dst 213 uint16_t dst; 214 // seq 215 uint32_t seq; 216 // meta data network layer 217 uint16_t netkey_index; 218 // meta data transport layer 219 uint16_t appkey_index; 220 uint8_t transmic_len; 221 // akf - aid for access, opcode for control 222 uint8_t akf_aid_control; 223 // MESH_TRANSPORT_FLAG 224 uint16_t flags; 225 // payload, single segmented or list of them 226 uint16_t len; 227 btstack_linked_list_t segments; 228 229 // access acknowledged message 230 uint16_t retransmit_count; 231 uint32_t retransmit_timeout_ms; 232 uint32_t ack_opcode; 233 234 // associated lower transport pdu 235 mesh_pdu_t * lower_pdu; 236 } mesh_upper_transport_pdu_t; 237 238 typedef struct { 239 // generic pdu header 240 mesh_pdu_t pdu_header; 241 // network header 242 uint8_t ivi_nid; 243 uint8_t ctl_ttl; 244 uint16_t src; 245 uint16_t dst; 246 uint32_t seq; 247 // meta data network layer 248 uint16_t netkey_index; 249 // akf - aid for access, opcode for control 250 uint8_t akf_aid_control; 251 // MESH_TRANSPORT_FLAG 252 uint16_t flags; 253 // payload 254 uint16_t len; 255 uint8_t data[MESH_CONTROL_PAYLOAD_MAX]; 256 } mesh_control_pdu_t; 257 258 typedef enum { 259 MESH_KEY_REFRESH_NOT_ACTIVE = 0, 260 MESH_KEY_REFRESH_FIRST_PHASE, 261 MESH_KEY_REFRESH_SECOND_PHASE 262 } mesh_key_refresh_state_t; 263 264 typedef enum { 265 MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE, 266 MESH_SECURE_NETWORK_BEACON_W4_AUTH_VALUE, 267 MESH_SECURE_NETWORK_BEACON_AUTH_VALUE, 268 MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV, 269 MESH_SECURE_NETWORK_BEACON_ADV_SENT, 270 MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT, 271 MESH_SECURE_NETWORK_BEACON_GATT_SENT, 272 MESH_SECURE_NETWORK_BEACON_W4_INTERVAL 273 } mesh_secure_network_beacon_state_t; 274 275 typedef struct { 276 btstack_linked_item_t item; 277 278 // netkey index 279 uint16_t netkey_index; 280 281 // current / old key 282 mesh_network_key_t * old_key; 283 284 // new key (only set during key refresh) 285 mesh_network_key_t * new_key; 286 287 // key refresh state 288 mesh_key_refresh_state_t key_refresh; 289 290 // advertisement using node id active 291 uint8_t node_id_advertisement_running; 292 293 294 // advertisement using network id (used by proxy) 295 adv_bearer_connectable_advertisement_data_item_t advertisement_with_network_id; 296 297 // advertising using node id (used by proxy) 298 adv_bearer_connectable_advertisement_data_item_t advertisement_with_node_id; 299 300 // secure network beacons 301 mesh_secure_network_beacon_state_t beacon_state; 302 uint32_t beacon_interval_ms; 303 uint32_t beacon_observation_start_ms; 304 uint16_t beacon_observation_counter; 305 306 } mesh_subnet_t; 307 308 typedef struct { 309 btstack_linked_list_iterator_t it; 310 } mesh_subnet_iterator_t; 311 312 /** 313 * @brief Init Mesh Network Layer 314 */ 315 void mesh_network_init(void); 316 317 /** 318 * @brief Set higher layer Network PDU handler 319 * @param packet_handler 320 */ 321 void mesh_network_set_higher_layer_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)); 322 323 /** 324 * @brief Set higher layer Proxy PDU handler 325 * @param packet_handler 326 */ 327 void mesh_network_set_proxy_message_handler(void (*packet_handler)(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu)); 328 329 /** 330 * @brief Mark packet as processed 331 * @param newtork_pdu received via call packet_handler 332 */ 333 void mesh_network_message_processed_by_higher_layer(mesh_network_pdu_t * network_pdu); 334 335 /** 336 * @brief Send network_pdu after encryption 337 * @param network_pdu 338 */ 339 void mesh_network_send_pdu(mesh_network_pdu_t * network_pdu); 340 341 /* 342 * @brief Setup network pdu header 343 * @param netkey_index 344 * @param nid 345 * @param ctl 346 * @param ttl 347 * @param seq 348 * @param dst 349 * @param transport_pdu_data 350 * @param transport_pdu_len 351 */ 352 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 dst, const uint8_t * transport_pdu_data, uint8_t transport_pdu_len); 353 354 /** 355 * Setup network pdu header without modifying len or payload 356 * @param network_pdu 357 * @param netkey_index 358 * @param nid 359 * @param ctl 360 * @param ttl 361 * @param seq 362 * @param src 363 * @param dest 364 */ 365 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); 366 367 /** 368 * @brief Validate network addresses 369 * @param ctl 370 * @param src 371 * @param dst 372 * @returns 1 if valid, 373 */ 374 int mesh_network_addresses_valid(uint8_t ctl, uint16_t src, uint16_t dst); 375 376 /** 377 * @brief Check if Unicast address 378 * @param addr 379 * @returns 1 if unicast 380 */ 381 int mesh_network_address_unicast(uint16_t addr); 382 383 /** 384 * @brief Check if Unicast address 385 * @param addr 386 * @returns 1 if unicast 387 */ 388 int mesh_network_address_group(uint16_t addr); 389 390 /** 391 * @brief Check if All Proxies address 392 * @param addr 393 * @returns 1 if all proxies 394 */ 395 int mesh_network_address_all_proxies(uint16_t addr); 396 397 /** 398 * @brief Check if All Nodes address 399 * @param addr 400 * @returns 1 if all nodes 401 */ 402 int mesh_network_address_all_nodes(uint16_t addr); 403 404 /** 405 * @brief Check if All Friends address 406 * @param addr 407 * @returns 1 if all friends 408 */ 409 int mesh_network_address_all_friends(uint16_t addr); 410 411 /** 412 * @brief Check if All Relays address 413 * @param addr 414 * @returns 1 if all relays 415 */ 416 int mesh_network_address_all_relays(uint16_t addr); 417 418 419 /** 420 * @brief Check if Virtual address 421 * @param addr 422 * @returns 1 if virtual 423 */ 424 int mesh_network_address_virtual(uint16_t addr); 425 426 427 /** 428 * @brief Add subnet to list 429 * @param subnet 430 */ 431 void mesh_subnet_add(mesh_subnet_t * subnet); 432 433 /** 434 * @brief Remove subnet from list 435 * @param subnet 436 */ 437 void mesh_subnet_remove(mesh_subnet_t * subnet); 438 439 /** 440 * @brief Get subnet for netkey_index 441 * @param netkey_index 442 * @returns mesh_subnet_t or NULL 443 */ 444 mesh_subnet_t * mesh_subnet_get_by_netkey_index(uint16_t netkey_index); 445 446 /** 447 * @brief Get number of stored subnets 448 * @returns count 449 */ 450 int mesh_subnet_list_count(void); 451 452 /** 453 * @brief Iterate over all subnets 454 * @param it 455 */ 456 void mesh_subnet_iterator_init(mesh_subnet_iterator_t *it); 457 458 /** 459 * @brief Check if another subnet is available 460 * @param it 461 * @return 462 */ 463 int mesh_subnet_iterator_has_more(mesh_subnet_iterator_t *it); 464 465 /** 466 * @brief Get next subnet 467 * @param it 468 * @return 469 */ 470 mesh_subnet_t * mesh_subnet_iterator_get_next(mesh_subnet_iterator_t *it); 471 472 /** 473 * @brief Setup subnet for given netkey index 474 */ 475 void mesh_subnet_setup_for_netkey_index(uint16_t netkey_index); 476 477 478 /** 479 * @brief Get outgoing network key for subnet based on key refresh phase 480 */ 481 mesh_network_key_t * mesh_subnet_get_outgoing_network_key(mesh_subnet_t * subnet); 482 483 // buffer pool 484 mesh_network_pdu_t * mesh_network_pdu_get(void); 485 void mesh_network_pdu_free(mesh_network_pdu_t * network_pdu); 486 487 // Mesh Network PDU Getter 488 uint16_t mesh_network_control(mesh_network_pdu_t * network_pdu); 489 uint8_t mesh_network_nid(mesh_network_pdu_t * network_pdu); 490 uint8_t mesh_network_ttl(mesh_network_pdu_t * network_pdu); 491 uint32_t mesh_network_seq(mesh_network_pdu_t * network_pdu); 492 uint16_t mesh_network_src(mesh_network_pdu_t * network_pdu); 493 uint16_t mesh_network_dst(mesh_network_pdu_t * network_pdu); 494 int mesh_network_segmented(mesh_network_pdu_t * network_pdu); 495 uint8_t mesh_network_control_opcode(mesh_network_pdu_t * network_pdu); 496 uint8_t * mesh_network_pdu_data(mesh_network_pdu_t * network_pdu); 497 uint8_t mesh_network_pdu_len(mesh_network_pdu_t * network_pdu); 498 499 // Mesh Network PDU Setter 500 void mesh_network_pdu_set_seq(mesh_network_pdu_t * network_pdu, uint32_t seq); 501 502 // Testing only 503 void mesh_network_received_message(const uint8_t * pdu_data, uint8_t pdu_len, uint8_t flags); 504 void mesh_network_process_proxy_configuration_message(const uint8_t * pdu_data, uint8_t pdu_len); 505 void mesh_network_encrypt_proxy_configuration_message(mesh_network_pdu_t * network_pdu); 506 void mesh_network_dump(void); 507 void mesh_network_reset(void); 508 509 #if defined __cplusplus 510 } 511 #endif 512 513 #endif 514