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