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