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