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