1 /* 2 * Copyright (C) 2017 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 #define __BTSTACK_FILE__ "beacon.c" 39 40 #include <string.h> 41 42 #include "mesh/beacon.h" 43 #include "mesh/adv_bearer.h" 44 #include "mesh/gatt_bearer.h" 45 #include "mesh_foundation.h" 46 #include "ble/core.h" 47 #include "bluetooth.h" 48 #include "bluetooth_data_types.h" 49 #include "btstack_debug.h" 50 #include "btstack_util.h" 51 #include "btstack_run_loop.h" 52 #include "btstack_event.h" 53 #include "gap.h" 54 #include "mesh_keys.h" 55 56 #define BEACON_TYPE_UNPROVISIONED_DEVICE 0 57 #define BEACON_TYPE_SECURE_NETWORK 1 58 59 #define UNPROVISIONED_BEACON_INTERVAL_MS 5000 60 #define UNPROVISIONED_BEACON_LEN 23 61 62 #define SECURE_NETWORK_BEACON_INTERVAL_MIN_MS 10000 63 #define SECURE_NETWORK_BEACON_INTERVAL_MAX_MS 600000 64 #define SECURE_NETWORK_BEACON_LEN 22 65 66 // prototypes 67 static void mesh_secure_network_beacon_run(btstack_timer_source_t * ts); 68 69 // bearers 70 #ifdef ENABLE_MESH_GATT_BEARER 71 static hci_con_handle_t gatt_bearer_con_handle; 72 #endif 73 74 // beacon 75 static uint8_t mesh_beacon_data[29]; 76 static uint8_t mesh_beacon_len; 77 static btstack_timer_source_t beacon_timer; 78 79 // unprovisioned device beacon 80 #ifdef ENABLE_MESH_ADV_BEARER 81 static const uint8_t * beacon_device_uuid; 82 static uint16_t beacon_oob_information; 83 static uint32_t beacon_uri_hash; 84 static int beacon_send_device_beacon; 85 #endif 86 87 static btstack_packet_handler_t unprovisioned_device_beacon_handler; 88 89 // secure network beacon 90 static btstack_crypto_aes128_cmac_t mesh_secure_network_beacon_cmac_request; 91 static uint8_t mesh_secure_network_beacon_auth_value[16]; 92 static btstack_packet_handler_t mesh_secure_network_beacon_handler; 93 static int mesh_secure_network_beacon_active; 94 static uint8_t mesh_secure_network_beacon_validate_buffer[SECURE_NETWORK_BEACON_LEN]; 95 96 #ifdef ENABLE_MESH_ADV_BEARER 97 static void beacon_timer_handler(btstack_timer_source_t * ts){ 98 // restart timer 99 btstack_run_loop_set_timer(ts, UNPROVISIONED_BEACON_INTERVAL_MS); 100 btstack_run_loop_add_timer(ts); 101 102 // setup beacon 103 mesh_beacon_len = UNPROVISIONED_BEACON_LEN; 104 mesh_beacon_data[0] = BEACON_TYPE_UNPROVISIONED_DEVICE; 105 memcpy(&mesh_beacon_data[1], beacon_device_uuid, 16); 106 big_endian_store_16(mesh_beacon_data, 17, beacon_oob_information); 107 big_endian_store_32(mesh_beacon_data, 19, beacon_uri_hash); 108 109 // request to send 110 beacon_send_device_beacon = 1; 111 adv_bearer_request_can_send_now_for_beacon(); 112 } 113 #endif 114 115 static void mesh_secure_network_beacon_auth_value_calculated(void * arg){ 116 mesh_subnet_t * mesh_subnet = (mesh_subnet_t *) arg; 117 118 memcpy(&mesh_beacon_data[14], mesh_secure_network_beacon_auth_value, 8); 119 mesh_beacon_len = SECURE_NETWORK_BEACON_LEN; 120 121 printf("Secure Network Beacon\n"); 122 printf("- "); 123 printf_hexdump(mesh_beacon_data, mesh_beacon_len); 124 125 mesh_subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_AUTH_VALUE; 126 127 mesh_secure_network_beacon_run(NULL); 128 } 129 130 static uint8_t mesh_secure_network_beacon_get_flags(mesh_subnet_t * mesh_subnet){ 131 uint8_t mesh_flags = 0; 132 if (mesh_subnet->key_refresh != MESH_KEY_REFRESH_NOT_ACTIVE){ 133 mesh_flags |= 1; 134 } 135 136 // TODO: set bit 1 if IV Update is active 137 return mesh_flags; 138 } 139 140 static void mesh_secure_network_beacon_setup(mesh_subnet_t * mesh_subnet){ 141 mesh_beacon_data[0] = BEACON_TYPE_SECURE_NETWORK; 142 mesh_beacon_data[1] = mesh_secure_network_beacon_get_flags(mesh_subnet); 143 // TODO: pick correct key based on key refresh phase 144 145 memcpy(&mesh_beacon_data[2], mesh_subnet->old_key->network_id, 8); 146 big_endian_store_32(mesh_beacon_data, 10, mesh_get_iv_index()); 147 btstack_crypto_aes128_cmac_message(&mesh_secure_network_beacon_cmac_request, mesh_subnet->old_key->beacon_key, 13, 148 &mesh_beacon_data[1], mesh_secure_network_beacon_auth_value, &mesh_secure_network_beacon_auth_value_calculated, mesh_subnet); 149 } 150 151 static void mesh_secure_network_beacon_update_interval(mesh_subnet_t * subnet){ 152 uint32_t min_observation_period_ms = 2 * subnet->beacon_interval_ms; 153 uint32_t actual_observation_period = btstack_time_delta(btstack_run_loop_get_time_ms(), subnet->beacon_observation_start_ms); 154 155 // The Observation Period in seconds should typically be double the typical Beacon Interval. 156 if (actual_observation_period < min_observation_period_ms) return; 157 158 // Expected Number of Beacons (1 beacon per 10 seconds) 159 uint16_t expected_number_of_beacons = actual_observation_period / SECURE_NETWORK_BEACON_INTERVAL_MIN_MS; 160 161 // Beacon Interval = Observation Period * (Observed Number of Beacons + 1) / Expected Number of Beacons 162 uint32_t new_beacon_interval = actual_observation_period * (subnet->beacon_observation_counter + 1) / expected_number_of_beacons; 163 164 if (new_beacon_interval > SECURE_NETWORK_BEACON_INTERVAL_MAX_MS){ 165 new_beacon_interval = SECURE_NETWORK_BEACON_INTERVAL_MAX_MS; 166 } 167 else if (new_beacon_interval < SECURE_NETWORK_BEACON_INTERVAL_MIN_MS){ 168 new_beacon_interval = SECURE_NETWORK_BEACON_INTERVAL_MAX_MS; 169 } 170 subnet->beacon_interval_ms = new_beacon_interval; 171 log_info("New beacon interval %u seconds", (int) (subnet->beacon_interval_ms / 1000)); 172 } 173 174 static void mesh_secure_network_beacon_run(btstack_timer_source_t * ts){ 175 UNUSED(ts); 176 177 uint32_t next_timeout_ms = 0; 178 179 // iterate over all networks 180 mesh_subnet_iterator_t it; 181 mesh_subnet_iterator_init(&it); 182 while (mesh_subnet_iterator_has_more(&it)){ 183 mesh_subnet_t * subnet = mesh_subnet_iterator_get_next(&it); 184 switch (subnet->beacon_state){ 185 case MESH_SECURE_NETWORK_BEACON_W4_INTERVAL: 186 // update beacon interval 187 mesh_secure_network_beacon_update_interval(subnet); 188 189 // send new beacon 190 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE; 191 192 /** Explict Fall-through */ 193 194 case MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE: 195 if (mesh_secure_network_beacon_active){ 196 // just try again in 10 ms 197 next_timeout_ms = 10; 198 break; 199 } 200 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W4_AUTH_VALUE; 201 mesh_secure_network_beacon_active = 1; 202 mesh_secure_network_beacon_setup(subnet); 203 break; 204 205 case MESH_SECURE_NETWORK_BEACON_AUTH_VALUE: 206 207 #ifdef ENABLE_MESH_ADV_BEARER 208 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV; 209 adv_bearer_request_can_send_now_for_beacon(); 210 break; 211 #endif 212 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_ADV_SENT; 213 214 /** Explict Fall-through */ 215 216 case MESH_SECURE_NETWORK_BEACON_ADV_SENT: 217 218 #ifdef ENABLE_MESH_GATT_BEARER 219 if (gatt_bearer_con_handle != HCI_CON_HANDLE_INVALID && mesh_foundation_gatt_proxy_get() != 0){ 220 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT; 221 gatt_bearer_request_can_send_now_for_beacon(); 222 break; 223 } 224 #endif 225 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_GATT_SENT; 226 227 /** Explict Fall-through */ 228 229 case MESH_SECURE_NETWORK_BEACON_GATT_SENT: 230 // now, start listening for beacons 231 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W4_INTERVAL; 232 // and request timeout 233 if (next_timeout_ms == 0 || next_timeout_ms > subnet->beacon_interval_ms){ 234 next_timeout_ms = subnet->beacon_interval_ms; 235 } 236 break; 237 238 default: 239 break; 240 } 241 } 242 243 // setup next run 244 if (next_timeout_ms == 0) return; 245 246 btstack_run_loop_set_timer(&beacon_timer, next_timeout_ms); 247 btstack_run_loop_set_timer_handler(&beacon_timer, mesh_secure_network_beacon_run); 248 btstack_run_loop_add_timer(&beacon_timer); 249 } 250 251 static void beacon_handle_secure_beacon_auth_value_calculated(void * arg){ 252 UNUSED(arg); 253 254 // pass on, if auth value checks out 255 if (memcmp(&mesh_secure_network_beacon_validate_buffer[14], mesh_secure_network_beacon_auth_value, 8) == 0) { 256 if (mesh_secure_network_beacon_handler){ 257 (*mesh_secure_network_beacon_handler)(MESH_BEACON_PACKET, 0, mesh_secure_network_beacon_validate_buffer, SECURE_NETWORK_BEACON_LEN); 258 } 259 } 260 261 // done 262 mesh_secure_network_beacon_active = 0; 263 mesh_secure_network_beacon_run(NULL); 264 } 265 266 static void beacon_handle_secure_beacon(uint8_t * packet, uint16_t size){ 267 if (size != SECURE_NETWORK_BEACON_LEN) return; 268 269 // lookup subnet by network id 270 uint8_t * beacon_network_id = &packet[2]; 271 mesh_subnet_iterator_t it; 272 mesh_subnet_iterator_init(&it); 273 mesh_subnet_t * subnet = NULL; 274 while (mesh_subnet_iterator_has_more(&it)){ 275 mesh_subnet_t * item = mesh_subnet_iterator_get_next(&it); 276 // TODO: handle old/new keys 277 if (memcmp(item->old_key->network_id, beacon_network_id, 8) != 0 ) continue; 278 subnet = item; 279 break; 280 } 281 if (subnet == NULL) return; 282 283 // count beacon 284 subnet->beacon_observation_counter++; 285 286 // check if new flags are set 287 uint8_t current_flags = mesh_secure_network_beacon_get_flags(subnet); 288 uint8_t new_flags = packet[1] & (~current_flags); 289 290 if (new_flags == 0) return; 291 292 // validate beacon - if crytpo ready 293 if (mesh_secure_network_beacon_active) return; 294 295 mesh_secure_network_beacon_active = 1; 296 memcpy(mesh_secure_network_beacon_validate_buffer, &packet[0], SECURE_NETWORK_BEACON_LEN); 297 298 // TODO: handle odl/new keys 299 btstack_crypto_aes128_cmac_message(&mesh_secure_network_beacon_cmac_request, subnet->old_key->beacon_key, 13, 300 &mesh_secure_network_beacon_validate_buffer[1], mesh_secure_network_beacon_auth_value, &beacon_handle_secure_beacon_auth_value_calculated, subnet); 301 } 302 303 static void beacon_handle_beacon_packet(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 304 log_info("beacon type %u", packet[0]); 305 switch (packet[0]){ 306 case BEACON_TYPE_UNPROVISIONED_DEVICE: 307 if (unprovisioned_device_beacon_handler){ 308 (*unprovisioned_device_beacon_handler)(packet_type, channel, packet, size); 309 } 310 break; 311 case BEACON_TYPE_SECURE_NETWORK: 312 beacon_handle_secure_beacon(packet, size); 313 break; 314 default: 315 break; 316 } 317 } 318 319 #ifdef ENABLE_MESH_ADV_BEARER 320 static void beacon_adv_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 321 mesh_subnet_iterator_t it; 322 switch (packet_type){ 323 case HCI_EVENT_PACKET: 324 switch(packet[0]){ 325 case HCI_EVENT_MESH_META: 326 switch(packet[2]){ 327 case MESH_SUBEVENT_CAN_SEND_NOW: 328 if (beacon_send_device_beacon){ 329 beacon_send_device_beacon = 0; 330 adv_bearer_send_beacon(mesh_beacon_data, mesh_beacon_len); 331 break; 332 } 333 // secure beacon state machine 334 mesh_subnet_iterator_init(&it); 335 while (mesh_subnet_iterator_has_more(&it)){ 336 mesh_subnet_t * subnet = mesh_subnet_iterator_get_next(&it); 337 switch (subnet->beacon_state){ 338 case MESH_SECURE_NETWORK_BEACON_W2_SEND_ADV: 339 adv_bearer_send_beacon(mesh_beacon_data, mesh_beacon_len); 340 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_ADV_SENT; 341 mesh_secure_network_beacon_run(NULL); 342 break; 343 default: 344 break; 345 } 346 } 347 break; 348 default: 349 break; 350 } 351 break; 352 default: 353 break; 354 } 355 break; 356 case MESH_BEACON_PACKET: 357 beacon_handle_beacon_packet(packet_type, channel, packet, size); 358 break; 359 default: 360 break; 361 } 362 } 363 #endif 364 365 #ifdef ENABLE_MESH_GATT_BEARER 366 // handle MESH_SUBEVENT_PROXY_DISCONNECTED and MESH_SUBEVENT_CAN_SEND_NOW 367 static void beacon_gatt_handle_mesh_event(uint8_t mesh_subevent){ 368 mesh_subnet_iterator_t it; 369 mesh_subnet_iterator_init(&it); 370 while (mesh_subnet_iterator_has_more(&it)){ 371 mesh_subnet_t * subnet = mesh_subnet_iterator_get_next(&it); 372 switch (subnet->beacon_state){ 373 case MESH_SECURE_NETWORK_BEACON_W2_SEND_GATT: 374 // skip send on MESH_SUBEVENT_PROXY_DISCONNECTED 375 if (mesh_subevent == MESH_SUBEVENT_CAN_SEND_NOW){ 376 gatt_bearer_send_beacon(mesh_beacon_data, mesh_beacon_len); 377 } 378 subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_GATT_SENT; 379 mesh_secure_network_beacon_run(NULL); 380 break; 381 default: 382 break; 383 } 384 } 385 386 } 387 388 static void beacon_gatt_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 389 uint8_t mesh_subevent; 390 switch (packet_type){ 391 case HCI_EVENT_PACKET: 392 switch(packet[0]){ 393 case HCI_EVENT_MESH_META: 394 mesh_subevent = packet[2]; 395 switch(mesh_subevent){ 396 case MESH_SUBEVENT_PROXY_CONNECTED: 397 gatt_bearer_con_handle = mesh_subevent_proxy_connected_get_con_handle(packet); 398 break; 399 case MESH_SUBEVENT_PROXY_DISCONNECTED: 400 gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID; 401 beacon_gatt_handle_mesh_event(mesh_subevent); 402 break; 403 case MESH_SUBEVENT_CAN_SEND_NOW: 404 beacon_gatt_handle_mesh_event(mesh_subevent); 405 break; 406 default: 407 break; 408 } 409 break; 410 default: 411 break; 412 } 413 break; 414 case MESH_BEACON_PACKET: 415 beacon_handle_beacon_packet(packet_type, channel, packet, size); 416 break; 417 default: 418 break; 419 } 420 } 421 #endif 422 423 void beacon_init(void){ 424 #ifdef ENABLE_MESH_ADV_BEARER 425 adv_bearer_register_for_beacon(&beacon_adv_packet_handler); 426 #endif 427 #ifdef ENABLE_MESH_GATT_BEARER 428 gatt_bearer_con_handle = HCI_CON_HANDLE_INVALID; 429 gatt_bearer_register_for_beacon(&beacon_gatt_packet_handler); 430 #endif 431 } 432 433 /** 434 * Start Unprovisioned Device Beacon 435 */ 436 void beacon_unprovisioned_device_start(const uint8_t * device_uuid, uint16_t oob_information){ 437 #ifdef ENABLE_MESH_ADV_BEARER 438 beacon_oob_information = oob_information; 439 if (device_uuid){ 440 beacon_device_uuid = device_uuid; 441 beacon_timer.process = &beacon_timer_handler; 442 beacon_timer_handler(&beacon_timer); 443 } 444 #endif 445 } 446 447 /** 448 * Stop Unprovisioned Device Beacon 449 */ 450 void beacon_unprovisioned_device_stop(void){ 451 #ifdef ENABLE_MESH_ADV_BEARER 452 btstack_run_loop_remove_timer(&beacon_timer); 453 #endif 454 } 455 456 // secure network beacons 457 458 void beacon_secure_network_start(mesh_subnet_t * mesh_subnet){ 459 // default interval 460 mesh_subnet->beacon_interval_ms = SECURE_NETWORK_BEACON_INTERVAL_MIN_MS; 461 mesh_subnet->beacon_state = MESH_SECURE_NETWORK_BEACON_W2_AUTH_VALUE; 462 mesh_subnet->beacon_observation_start_ms = btstack_run_loop_get_time_ms(); 463 mesh_subnet->beacon_observation_counter = 0; 464 465 // start sending 466 mesh_secure_network_beacon_run(NULL); 467 } 468 469 // register handler 470 void beacon_register_for_unprovisioned_device_beacons(btstack_packet_handler_t packet_handler){ 471 unprovisioned_device_beacon_handler = packet_handler; 472 } 473 474 void beacon_register_for_secure_network_beacons(btstack_packet_handler_t packet_handler){ 475 mesh_secure_network_beacon_handler = packet_handler; 476 } 477