Skip to content

Commit

Permalink
eid&random mac moved to device conf struct (ARMmbed#1497)
Browse files Browse the repository at this point in the history
eid&random mac moved to device conf struct.
first phase or refactoring.
  • Loading branch information
Kari Severinkangas authored Dec 4, 2017
1 parent df18635 commit f60268f
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 29 deletions.
2 changes: 2 additions & 0 deletions nanostack/thread_management_if.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ typedef struct link_configuration {
*/
typedef struct {
uint8_t eui64[8];/**< eui64 of the device. This field is used to identify device when joining to network Mandatory*/
uint8_t mesh_local_eid[8]; /**< Mesh local extented id*/
uint8_t extended_random_mac[8]; /**< Extended random mac which is generated during commissioning*/
uint8_t *PSKd_ptr;/**< Device credentials used to authenticate device to commissioner Mandatory length 6-32*/
uint8_t PSKd_len;/**< Length of PSKd_ptr*/
char *provisioning_uri_ptr;/**< Provisioning url max 64 bytes*/
Expand Down
9 changes: 5 additions & 4 deletions source/6LoWPAN/Thread/thread_bbr_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,7 @@ int thread_bbr_start(int8_t interface_id, int8_t backbone_interface_id)
#ifdef HAVE_THREAD_BORDER_ROUTER
thread_bbr_t *this = thread_bbr_find_by_interface(interface_id);
link_configuration_s *link_configuration_ptr = thread_joiner_application_get_config(interface_id);
uint8_t *extended_random_mac = thread_joiner_application_random_mac_get(interface_id);
char service_name[30] = {0};
char *ptr;

Expand All @@ -1052,10 +1053,10 @@ int thread_bbr_start(int8_t interface_id, int8_t backbone_interface_id)

this->backbone_interface_id = backbone_interface_id;
ptr = service_name;
*ptr++ = 'a' + link_configuration_ptr->extended_random_mac[0] % 26;
*ptr++ = 'a' + link_configuration_ptr->extended_random_mac[1] % 26;
*ptr++ = 'a' + link_configuration_ptr->extended_random_mac[2] % 26;
*ptr++ = 'a' + link_configuration_ptr->extended_random_mac[3] % 26;
*ptr++ = 'a' + extended_random_mac[0] % 26;
*ptr++ = 'a' + extended_random_mac[1] % 26;
*ptr++ = 'a' + extended_random_mac[2] % 26;
*ptr++ = 'a' + extended_random_mac[3] % 26;
memcpy(ptr,"-ARM-",5);
ptr += 5;
memcpy(ptr,link_configuration_ptr->name,16);
Expand Down
8 changes: 5 additions & 3 deletions source/6LoWPAN/Thread/thread_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,9 @@ static void thread_bootstrap_ml_address_update(protocol_interface_info_entry_t *
}

// Set new ML-EID and ULA prefix
memcpy(cur->iid_slaac, conf->mesh_local_eid, 8);
uint8_t *ml_eid = thread_joiner_application_ml_eid_get(cur->id);
memcpy(cur->iid_slaac, ml_eid, 8);

arm_thread_private_ula_prefix_set(cur, conf->mesh_local_ula_prefix);

// Generate new ML64 address
Expand Down Expand Up @@ -864,7 +866,7 @@ int thread_link_configuration_activate(protocol_interface_info_entry_t *cur, lin
return -1;
}

if (thread_configuration_mac_activate(cur, linkConfiguration->rfChannel, linkConfiguration->panId,linkConfiguration->extended_random_mac)) {
if (thread_configuration_mac_activate(cur, linkConfiguration->rfChannel, linkConfiguration->panId,thread_joiner_application_random_mac_get(cur->id))) {
return -1;
}

Expand Down Expand Up @@ -2310,7 +2312,7 @@ void thread_bootstrap_state_machine(protocol_interface_info_entry_t *cur)

//SET Link by Static configuration
tr_info("thread network attach start");
if (thread_mle_service_register(cur->id,linkConfiguration->extended_random_mac) != 0 ||
if (thread_mle_service_register(cur->id,thread_joiner_application_random_mac_get(cur->id)) != 0 ||
thread_link_configuration_activate(cur, linkConfiguration) != 0) {
tr_error("Network Bootsrap Start Fail");
bootsrap_next_state_kick(ER_BOOTSTRAP_SCAN_FAIL, cur);
Expand Down
3 changes: 1 addition & 2 deletions source/6LoWPAN/Thread/thread_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,7 @@ void thread_extension_address_registration(struct protocol_interface_info_entry
tr_error("No link configuration.");
return;
}

ml_eid = link_config->mesh_local_eid;
ml_eid = thread_joiner_application_ml_eid_get(interface->id);
}

// Register the DUA address
Expand Down
64 changes: 44 additions & 20 deletions source/6LoWPAN/Thread/thread_joiner_application.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,14 +335,14 @@ static void link_configuration_copy(link_configuration_s *this, link_configurati
if (!this || !configuration_ptr) {
return;
}
//@TODO Totaly wrong place

memcpy(this->extended_random_mac, configuration_ptr->extended_random_mac, 8);
memcpy(this->mesh_local_eid, configuration_ptr->mesh_local_eid,5);
memcpy(this->name, configuration_ptr->name, 16);
memcpy(this->PSKc, configuration_ptr->PSKc, 16);
memcpy(this->master_key, configuration_ptr->master_key, 16);
memcpy(this->mesh_local_ula_prefix, configuration_ptr->mesh_local_ula_prefix, 8);
memcpy(this->mesh_local_eid, configuration_ptr->mesh_local_eid, 8);
memcpy(this->extented_pan_id, configuration_ptr->extented_pan_id, 8);
memcpy(this->extended_random_mac, configuration_ptr->extended_random_mac, 8);
memcpy(this->channel_mask, configuration_ptr->channel_mask,5);
this->key_rotation = configuration_ptr->key_rotation;
this->key_sequence = configuration_ptr->key_sequence;
Expand Down Expand Up @@ -416,8 +416,6 @@ static void link_configuration_trace(link_configuration_s *this)
}
tr_debug("NwkName: %s", trace_array(this->name, 16));
tr_debug("Mesh ula: %s", trace_array(this->mesh_local_ula_prefix, 8));
tr_debug("ML-EID: %s", trace_array(this->mesh_local_eid, 8));
tr_debug("Random MAC: %s", trace_array(this->extended_random_mac, 8));
tr_debug("Extendend PanId: %s", trace_array(this->extented_pan_id, 8));
tr_debug("panid: %"PRIu16", Channel:%"PRIu16", keyRot:%"PRIu16", keySeq:%"PRIu32, this->panId, this->rfChannel, this->key_rotation, this->key_sequence);
return;
Expand All @@ -432,6 +430,7 @@ static device_configuration_s *device_configuration_create(void)
memset(this, 0, sizeof(device_configuration_s));
return this;
}

static void device_configuration_delete(device_configuration_s *this)
{
if (!this) {
Expand Down Expand Up @@ -787,12 +786,13 @@ void thread_joiner_application_lc_nvm_read_callback(int status, void *context)
fast_data.mle_frame_counter += MLE_FRAME_COUNTER_LIMIT;
thread_nvm_store_fast_data_store(&fast_data);

thread_dynamic_storage_device_configuration_read(this->interface_id,this->configuration_ptr->extended_random_mac,this->configuration_ptr->mesh_local_eid);
thread_dynamic_storage_device_configuration_read(this->interface_id,this->device_configuration_ptr->extended_random_mac,this->device_configuration_ptr->mesh_local_eid);
thread_nvm_store_link_info_file_read();
thread_joiner_application_validate_settings(this);// Generate all random information
this->configuration_valid = true;
link_configuration_trace(this->configuration_ptr);

tr_debug("ML-EID: %s", trace_array(this->device_configuration_ptr->mesh_local_eid, 8));
tr_debug("Random MAC: %s", trace_array(this->device_configuration_ptr->extended_random_mac, 8));
//Add Security to MLE service
uint8_t key_material[32];
uint8_t key_index;
Expand Down Expand Up @@ -867,26 +867,32 @@ void thread_joiner_application_nvm_delete_callback(int status, void *context)

static void thread_joiner_application_validate_settings(thread_joiner_t *this)
{

if (memcmp(this->configuration_ptr->extended_random_mac,ADDR_UNSPECIFIED, 8) == 0) {
randLIB_get_n_bytes_random(this->configuration_ptr->extended_random_mac, 8);

this->configuration_ptr->extended_random_mac[0] |= 2; //Set Local Bit
this->configuration_ptr->extended_random_mac[0] &= ~1; //Clear multicast bit
if (memcmp(this->device_configuration_ptr->extended_random_mac,ADDR_UNSPECIFIED, 8) == 0) {
memcpy(this->device_configuration_ptr->extended_random_mac,this->configuration_ptr->extended_random_mac,8);
}
if (memcmp(this->device_configuration_ptr->extended_random_mac,ADDR_UNSPECIFIED, 8) == 0) {
randLIB_get_n_bytes_random(this->device_configuration_ptr->extended_random_mac, 8);
this->device_configuration_ptr->extended_random_mac[0] |= 2; //Set Local Bit
this->device_configuration_ptr->extended_random_mac[0] &= ~1; //Clear multicast bit
tr_info("Generating Random MAC");
memcpy(this->configuration_ptr->extended_random_mac,this->device_configuration_ptr->extended_random_mac,8);
}
if (memcmp(this->device_configuration_ptr->mesh_local_eid,ADDR_UNSPECIFIED,8)==0){
memcpy(this->device_configuration_ptr->mesh_local_eid,this->configuration_ptr->mesh_local_eid,8);
}
while (addr_iid_reserved(this->configuration_ptr->mesh_local_eid) ||
memcmp(this->configuration_ptr->mesh_local_eid, ADDR_SHORT_ADR_SUFFIC,6) == 0 ) {
while (addr_iid_reserved(this->device_configuration_ptr->mesh_local_eid) ||
memcmp(this->device_configuration_ptr->mesh_local_eid, ADDR_SHORT_ADR_SUFFIC,6) == 0 ) {
// addr_iid_reserved checks the all zeroes case.
randLIB_get_n_bytes_random(this->configuration_ptr->mesh_local_eid, 8);
randLIB_get_n_bytes_random(this->device_configuration_ptr->mesh_local_eid, 8);
tr_info("Generating Random ML-EID");
memcpy(this->configuration_ptr->mesh_local_eid,this->device_configuration_ptr->mesh_local_eid,8);
}

if (this->configuration_ptr->key_rotation < 3600) {
this->configuration_ptr->key_rotation = 3600;
}
}


int thread_joiner_application_init(int8_t interface_id, device_configuration_s *device_configuration_ptr, link_configuration_s *default_configuration_ptr)
{
thread_joiner_t *this;
Expand Down Expand Up @@ -1132,6 +1138,25 @@ uint64_t thread_joiner_application_active_timestamp_get(int8_t interface_id)
return this->configuration_ptr->timestamp;
}

uint8_t *thread_joiner_application_random_mac_get(int8_t interface_id)
{
thread_joiner_t *this = thread_joiner_find(interface_id);
if (!this || !this->device_configuration_ptr) {
tr_error("thread_joiner_application_random_mac_get NULL parameter ");
return (uint8_t*)ADDR_UNSPECIFIED;
}
return this->device_configuration_ptr->extended_random_mac;
}
uint8_t *thread_joiner_application_ml_eid_get(int8_t interface_id)
{
tr_info("thread_joiner_application_ml_eid_get");
thread_joiner_t *this = thread_joiner_find(interface_id);
if (!this || !this->device_configuration_ptr) {
tr_error("thread_joiner_application_ml_eid_get parameter NULL");
return (uint8_t*)ADDR_UNSPECIFIED;
}
return this->device_configuration_ptr->mesh_local_eid;
}
void thread_joiner_application_active_timestamp_set(int8_t interface_id, uint64_t timestamp)
{
thread_joiner_t *this = thread_joiner_find(interface_id);
Expand Down Expand Up @@ -1944,13 +1969,12 @@ int thread_joiner_application_pskd_commission_start(int8_t interface_id, uint8_t

int thread_joiner_application_configuration_nvm_save(int8_t interface_id)
{

tr_info("thread_joiner_application_configuration_nvm_save");
thread_joiner_t *this = thread_joiner_find(interface_id);
if (!this) {
return -1;
}

thread_dynamic_storage_device_configuration_store(interface_id,this->configuration_ptr->extended_random_mac,this->configuration_ptr->mesh_local_eid);
thread_dynamic_storage_device_configuration_store(interface_id,this->device_configuration_ptr->extended_random_mac,this->device_configuration_ptr->mesh_local_eid);
thread_dynamic_storage_pending_configuration_store(interface_id,this->pending_configuration_ptr, sizeof(configuration_set_t));
thread_nvm_store_seq_counter_store(this->configuration_ptr->key_sequence);
thread_joiner_application_nvm_link_config_write(this, this->active_configuration_ptr);
Expand Down
5 changes: 5 additions & 0 deletions source/6LoWPAN/Thread/thread_joiner_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ device_configuration_s *thread_joiner_application_get_device_config(int8_t inter
*\ return 0 if store request has been delivered to lower layers.
*\ return -1 if store failed (store request not made)
*/

uint8_t *thread_joiner_application_random_mac_get(int8_t interface_id);

uint8_t *thread_joiner_application_ml_eid_get(int8_t interface_id);

int thread_joiner_application_link_configuration_store(int8_t interface_id, link_configuration_s *link_config);

/** Delete link configuration from platform NVM
Expand Down
9 changes: 9 additions & 0 deletions test/nanostack/unittest/stub/thread_joiner_application_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,15 @@ void thread_joiner_application_deinit(int8_t interface_id)
{
}

uint8_t *thread_joiner_application_ml_eid_get(int8_t interface_id)
{
return NULL;
}

uint8_t *thread_joiner_application_random_mac_get(int8_t interface_id)
{
return NULL;
}
link_configuration_s *thread_joiner_application_get_config(int8_t interface_id)
{
return NULL;
Expand Down

0 comments on commit f60268f

Please sign in to comment.