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