From 33373e4028711d345c0917135062dade0b367c64 Mon Sep 17 00:00:00 2001 From: Matthias Ringwald Date: Thu, 10 Nov 2016 21:51:01 +0100 Subject: [PATCH] sm: use fake public addresss of nRf5 chipsets as random static address --- src/ble/sm.c | 21 +++++++++++++++++---- src/bluetooth.h | 1 + src/hci.c | 13 ++++++++++++- src/hci.h | 7 +++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/ble/sm.c b/src/ble/sm.c index f4fdcf5ab..a0c4e422d 100644 --- a/src/ble/sm.c +++ b/src/ble/sm.c @@ -178,7 +178,6 @@ static derived_key_generation_t dkg_state; // random address update static random_address_update_t rau_state; static bd_addr_t sm_random_address; -static uint8_t sm_random_address_set; // CMAC Calculation: General static cmac_state_t sm_cmac_state; @@ -3106,6 +3105,17 @@ static void sm_event_packet_handler (uint8_t packet_type, uint16_t channel, uint sm_handle_random_result(&packet[6]); break; } + if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)){ + // Hack for Nordic nRF5 series that doesn't have public address: + // - with patches from port/nrf5-zephyr, hci_read_bd_addr returns random static address + // - we use this as default for advertisements/connections + if (hci_get_manufacturer() == COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA){ + log_info("nRF5: using (fake) public address as random static address"); + bd_addr_t addr; + reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], addr); + gap_random_address_set(addr); + } + } break; default: break; @@ -3623,7 +3633,6 @@ void sm_init(void){ sm_address_resolution_general_queue = NULL; gap_random_adress_update_period = 15 * 60 * 1000L; - sm_random_address_set = 0; sm_active_connection = 0; test_use_fixed_local_csrk = 0; @@ -3886,11 +3895,16 @@ static int gap_random_address_type_requires_updates(void){ if (gap_random_adress_type == GAP_RANDOM_ADDRESS_TYPE_OFF) return 0; return 1; } +static uint8_t own_address_type(void){ + if (gap_random_adress_type == 0) return 0; + return 1; +} // GAP LE API void gap_random_address_set_mode(gap_random_address_type_t random_address_type){ gap_random_address_update_stop(); gap_random_adress_type = random_address_type; + hci_le_advertisements_set_own_address_type(own_address_type()); if (!gap_random_address_type_requires_updates()) return; gap_random_address_update_start(); gap_random_address_trigger(); @@ -3910,7 +3924,6 @@ void gap_random_address_set_update_period(int period_ms){ void gap_random_address_set(bd_addr_t addr){ gap_random_address_set_mode(GAP_RANDOM_ADDRESS_TYPE_STATIC); memcpy(sm_random_address, addr, 6); - sm_random_address_set = 1; if (rau_state == RAU_W4_WORKING) return; rau_state = RAU_SET_ADDRESS; sm_run(); @@ -3930,7 +3943,7 @@ void gap_random_address_set(bd_addr_t addr){ */ void gap_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max, uint8_t adv_type, uint8_t direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy){ - hci_le_advertisements_set_params(adv_int_min, adv_int_max, adv_type, gap_random_adress_type == 0 ? 0 : 1, + hci_le_advertisements_set_params(adv_int_min, adv_int_max, adv_type, own_address_type(), direct_address_typ, direct_address, channel_map, filter_policy); } diff --git a/src/bluetooth.h b/src/bluetooth.h index e40acffc4..b1db0e31b 100644 --- a/src/bluetooth.h +++ b/src/bluetooth.h @@ -1142,6 +1142,7 @@ typedef enum { #define COMPANY_ID_TEXAS_INSTRUMENTS_INC 0x000D #define COMPANY_ID_BROADCOM_CORPORATION 0x000F #define COMPANY_ID_ST_MICROELECTRONICS 0x0030 +#define COMPANY_ID_NORDIC_SEMICONDUCTOR_ASA 0x0059 #define COMPANY_ID_EM_MICROELECTRONICS_MARIN 0x005A diff --git a/src/hci.c b/src/hci.c index 7f7fd9f0f..d7c2503bb 100644 --- a/src/hci.c +++ b/src/hci.c @@ -1479,15 +1479,16 @@ static void event_handler(uint8_t *packet, int size){ log_info("hci_le_read_white_list_size: size %u", hci_stack->le_whitelist_capacity); } #endif - // Dump local address if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_read_bd_addr)) { reverse_bd_addr(&packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE + 1], hci_stack->local_bd_addr); log_info("Local Address, Status: 0x%02x: Addr: %s", packet[OFFSET_OF_DATA_IN_COMMAND_COMPLETE], bd_addr_to_str(hci_stack->local_bd_addr)); +#ifdef ENABLE_CLASSIC if (hci_stack->link_key_db){ hci_stack->link_key_db->set_local_bd_addr(hci_stack->local_bd_addr); } +#endif } #ifdef ENABLE_CLASSIC if (HCI_EVENT_IS_COMMAND_COMPLETE(packet, hci_write_scan_enable)){ @@ -3580,6 +3581,12 @@ void gap_scan_response_set_data(uint8_t scan_response_data_length, uint8_t * sca gap_advertisments_changed(); } +void hci_le_advertisements_set_own_address_type(uint8_t own_address_type){ + hci_stack->le_advertisements_own_address_type = own_address_type; + hci_stack->le_advertisements_todo |= LE_ADVERTISEMENT_TASKS_SET_PARAMS; + gap_advertisments_changed(); +} + /** * @brief Enable/Disable Advertisements * @param enabled @@ -3773,3 +3780,7 @@ void hci_disconnect_all(void){ } hci_run(); } + +uint16_t hci_get_manufacturer(void){ + return hci_stack->manufacturer; +} diff --git a/src/hci.h b/src/hci.h index ef76e7755..31dbde0ba 100644 --- a/src/hci.h +++ b/src/hci.h @@ -975,6 +975,13 @@ void hci_le_advertisements_set_params(uint16_t adv_int_min, uint16_t adv_int_max uint8_t own_address_type, uint8_t direct_address_typ, bd_addr_t direct_address, uint8_t channel_map, uint8_t filter_policy); +void hci_le_advertisements_set_own_address_type(uint8_t own_address_type); + +/** + * @brief Get Manufactured + * @return manufacturer id + */ +uint16_t hci_get_manufacturer(void); // Only for PTS testing