Skip to content

Commit

Permalink
Merge pull request ARMmbed#1650 from ARMmbed/merge_MtoK2
Browse files Browse the repository at this point in the history
Merge mto k2
  • Loading branch information
Jarkko Paso authored Apr 11, 2018
2 parents 04797c2 + 5227398 commit 9fff108
Show file tree
Hide file tree
Showing 29 changed files with 394 additions and 69 deletions.
8 changes: 8 additions & 0 deletions nanostack/net_test_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#define NET_TEST_API_H_

#include "ns_types.h"
#include "Service_Libs/mle_service/mle_service_api.h"

/**
* \brief Makes TCP protocol drop given number of packets from a particular state (TX side).
Expand Down Expand Up @@ -56,4 +57,11 @@ int8_t arm_nwk_test_tcp_drop_rx(int state, uint8_t count);
*/
void arm_nwk_test_tcp_drop_reset(void);

/**
* \brief Set callback for MLE message receiving filter.
*
* Testing API for setting MLE receive callback for message filtering purposes.
*/
void arm_nwk_test_mle_receive_filter_set(mle_service_filter_cb *response_filter_cb);

#endif //NET_TEST_API_H_
13 changes: 13 additions & 0 deletions nanostack/net_thread_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,19 @@ int thread_test_increment_key_sequence_counter(int8_t interface_id);
*/
int thread_test_key_sequence_counter_update(int8_t interface_id, uint32_t thrKeySequenceCounter);

/**
* \brief Resets cached values from stack
*
Resets link configuration from cache and from NVM.
*
*
* \param interface_id Network Interface
*
* \return 0, OK
* \return <0 Error
*/
int thread_test_stack_cache_reset(int8_t interface_id);

/**
* \brief Set new Thread key rotation value
*
Expand Down
25 changes: 15 additions & 10 deletions source/6LoWPAN/Thread/thread_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,14 @@ int thread_bootstrap_partition_process(protocol_interface_info_entry_t *cur, uin
/*Rule 0: If we are going to form Higher partition than heard we dont try to attach to lower ones
*/
if (thread_extension_enabled(cur) &&
thread_info(cur)->thread_device_mode == THREAD_DEVICE_MODE_ROUTER &&
heard_partition_leader_data->weighting < thread_info(cur)->partition_weighting) {
return -2;
thread_info(cur)->thread_device_mode == THREAD_DEVICE_MODE_ROUTER) {
if (heard_partition_leader_data->weighting < thread_info(cur)->partition_weighting) {
tr_debug("Heard a lower weight partition");
return -2;
}
if (heard_partition_leader_data->weighting > thread_info(cur)->partition_weighting) {
return 2;
}
}

//Rule 1: A non-singleton Thread Network Partition always has higher priority than a singleton Thread Network Partition
Expand Down Expand Up @@ -1162,14 +1167,14 @@ void thread_bootstrap_ready(protocol_interface_info_entry_t *cur)
mac_data_poll_protocol_poll_mode_decrement(cur);
}

void thread_clean_all_routers_from_neighbor_list(int8_t interface_id)
void thread_neighbor_list_clean(struct protocol_interface_info_entry *cur)
{
mle_neigh_table_list_t *neig_list = mle_class_active_list_get(interface_id);
/* Init Double linked Routing Table */
ns_list_foreach_safe(mle_neigh_table_entry_t, cur, neig_list) {
if (thread_is_router_addr(cur->short_adr)) {
tr_debug("Free Router %x", cur->short_adr);
mle_class_remove_entry(interface_id, cur);
mle_neigh_table_list_t *neig_list = mle_class_active_list_get(cur->id);

ns_list_foreach_safe(mle_neigh_table_entry_t, cur_entry, neig_list) {
if (!thread_addr_is_equal_or_child(cur->thread_info->routerShortAddress, cur_entry->short_adr)) {
tr_debug("Free ID %x", cur_entry->short_adr);
mle_class_remove_entry(cur->id, cur_entry);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/6LoWPAN/Thread/thread_bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void thread_general_mle_receive_cb(int8_t interface_id, mle_message_t *mle_msg,
int thread_bootstrap_reset_child_info(protocol_interface_info_entry_t *cur, struct mle_neigh_table_entry_t *child);
void thread_bootstrap_ready(struct protocol_interface_info_entry *cur);
int thread_bootstrap_reset(struct protocol_interface_info_entry *cur);
void thread_clean_all_routers_from_neighbor_list(int8_t interface_id);
void thread_neighbor_list_clean(struct protocol_interface_info_entry *cur);
bool thread_bootstrap_request_network_data(struct protocol_interface_info_entry *cur, struct thread_leader_data_s *leaderData, uint16_t short_address);
bool thread_check_is_this_my_parent(struct protocol_interface_info_entry *cur, struct mle_neigh_table_entry_t *entry_temp);
void thread_clean_old_16_bit_address_based_addresses(struct protocol_interface_info_entry *cur);
Expand Down
10 changes: 6 additions & 4 deletions source/6LoWPAN/Thread/thread_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,8 @@ int8_t thread_bootstrap_down(protocol_interface_info_entry_t *cur)
tr_debug("SET thread Idle");
//stop polling
mac_data_poll_disable(cur);
//Clean routers from mle table
thread_clean_all_routers_from_neighbor_list(cur->id);
//Clean mle table
thread_neighbor_list_clean(cur);
// store frame counters
if (cur->thread_info) {
thread_nvm_fast_data_t fast_data;
Expand All @@ -249,6 +249,7 @@ int8_t thread_bootstrap_down(protocol_interface_info_entry_t *cur)
thread_joiner_application_configuration_nvm_save(cur->id);
mac_pairwise_key_flush_list(cur->id);
thread_discovery_reset(cur->id);
thread_leader_mleid_rloc_map_to_nvm_write(cur);
thread_bootstrap_stop(cur);
mle_service_interface_unregister(cur->id);
thread_management_server_delete(cur->id);
Expand Down Expand Up @@ -524,6 +525,7 @@ thread_leader_info_t *thread_allocate_and_init_leader_private_data(void)
thread_leader_info_t *leader_info = ns_dyn_mem_alloc(sizeof(thread_leader_info_t));
if (leader_info) {
leader_info->leader_id_seq_timer = ID_SEQUENCE_PERIOD;
leader_info->leader_nvm_sync_timer = 0;
}
return leader_info;
}
Expand Down Expand Up @@ -1843,7 +1845,7 @@ static void thread_tx_failure_handler(int8_t nwk_id, uint8_t accumulated_failure
}

if (accumulated_failures >= THREAD_MAC_TRANSMISSIONS*THREAD_FAILED_CHILD_TRANSMISSIONS) {
thread_reset_neighbour_info(cur, neighbor);
mle_class_remove_entry(cur->id, neighbor);
}
}

Expand All @@ -1853,8 +1855,8 @@ void thread_reset_neighbour_info(protocol_interface_info_entry_t *cur, mle_neigh
thread_parent_info_t *thread_endnode_parent = thread_info(cur)->thread_endnode_parent;

if (!thread_i_am_router(cur) && thread_endnode_parent && thread_endnode_parent->shortAddress == neighbour->short_adr) {
tr_warn("End device lost Parent!\n");
if(cur->nwk_bootstrap_state != ER_CHILD_ID_REQ) {
tr_warn("End device lost parent, reset!\n");
thread_bootstrap_connection_error(cur->id, CON_PARENT_CONNECT_DOWN, NULL);
}
}
Expand Down
4 changes: 4 additions & 0 deletions source/6LoWPAN/Thread/thread_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
*/
#define ROUTER_ID_REUSE_DELAY 100 //Seconds

#define LEADER_NVM_SYNC_DELAY 30 // Leader router ids write delay to NVM

#define ROUTER_ID_INFINITY_DELAY 90 //Seconds

#define NETWORK_ID_TIMEOUT 120 //seconds
Expand Down Expand Up @@ -145,6 +147,7 @@ typedef struct thread_leader_info_s {
uint8_t leader_id_seq_timer;
uint8_t master_router_id_mask[8];
uint8_t maskSeq;
uint8_t leader_nvm_sync_timer;
} thread_leader_info_t;

typedef struct thread_leader_data_s {
Expand Down Expand Up @@ -286,6 +289,7 @@ typedef struct thread_info_s {
uint16_t native_commissioner_port;
uint16_t routerShortAddress;
uint16_t reedJitterTimer;
uint16_t reedMergeAdvTimer;
uint16_t routerIdReqCoapID; // COAP msg id of RouterID request
int16_t childUpdateReqTimer;
uint16_t childUpdateReqMsgId;
Expand Down
6 changes: 5 additions & 1 deletion source/6LoWPAN/Thread/thread_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@

#define THREAD_ENTERPRISE_NUMBER 44970

#define THREAD_ADDR_REG_TIMEOUT_BASE 300
#define THREAD_ADDR_REG_RETRY_INTERVAL 300
#define THREAD_PROACTIVE_AN_INTERVAL 3600

// Router defines
Expand All @@ -174,6 +174,10 @@
#define MIN_DOWNGRADE_NEIGHBORS 7
#define THREAD_REED_ADVERTISEMENT_DELAY 5000

// Interval after which REED can send an advertisement to help others merge to higher partition.
// This is not related to default REED advertisements.
#define THREAD_REED_MERGE_ADVERTISEMENT_INTERVAL 120

/** Default Threshold for router Selection */
#define ROUTER_DOWNGRADE_THRESHOLD 23 // Define downGrade Threshold when active router is higher than this
#define ROUTER_UPGRADE_THRESHOLD 16 // Define upgrade Threshold fort REED when Active Router Count is smaller than this upgrade is possible
Expand Down
6 changes: 6 additions & 0 deletions source/6LoWPAN/Thread/thread_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,12 @@ static void thread_discovery_request_msg_handler(thread_discovery_class_t * disc
}
tr_debug("Thread discovery request message RX");

// Check if we have room for new neighbor
if (mle_class_free_entry_count_get(discovery_class->interface_id) < 1) {
tr_debug("MLE table full, skip request");
return;
}

//validate message
mle_tlv_info_t discovery_tlv;
//Parse Message
Expand Down
23 changes: 13 additions & 10 deletions source/6LoWPAN/Thread/thread_extension.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

#define TRACE_GROUP "comm"

#define SECURITY_POLICY_CCM_ENABLED 0x04 /* Thread Commercial Commissioning Mode is enabled when this bit is set. This is Thread 1.2 feature. */
#define SECURITY_POLICY_CCM_DISABLED 0x04 /* Thread Commercial Commissioning Mode is enabled when this bit is set. This is Thread 1.2 feature. */

typedef struct thread_extension_info {
int8_t coap_service_id;
Expand Down Expand Up @@ -271,6 +271,9 @@ int thread_extension_primary_bbr_get(struct protocol_interface_info_entry *cur,
}
if (mlr_timer_ptr) {
*mlr_timer_ptr = common_read_32_bit(&found_tlv[7]);
if (*mlr_timer_ptr < THREAD_DEFAULT_MIN_MLR_TIMEOUT) {
*mlr_timer_ptr = THREAD_DEFAULT_MIN_MLR_TIMEOUT;
}
}
return 0;
}
Expand Down Expand Up @@ -496,14 +499,16 @@ void thread_extension_mcast_subscrition_change(protocol_interface_info_entry_t *
if (addr_ipv6_multicast_scope(group->group) < IPV6_SCOPE_ADMIN_LOCAL) {
return;
}
// Default timer added.
group->mld_timer = added ? THREAD_ADDR_REG_TIMEOUT_BASE + randLIB_get_random_in_range(1, 30) : 0;

if (0 !=thread_extension_primary_bbr_get(interface, br_ml_addr, NULL, &mlr_timer, NULL )) {
if (0 != thread_extension_primary_bbr_get(interface, br_ml_addr, NULL, &mlr_timer, NULL )) {
// No BBR, try again later if adding address.
group->mld_timer = added ? THREAD_ADDR_REG_RETRY_INTERVAL + randLIB_get_random_in_range(1, 30) : 0;
return;
}
// follow the Primary BBR set timeout + Small jitter added
group->mld_timer = added ? mlr_timer: 0;

// follow the Primary BBR set timeout.
group->mld_timer = added ? (mlr_timer - randLIB_get_random_in_range(30, 40)) : 0;

// MLR is sent only for primary BBR for now, but this might change
thread_extension_mlr_req_send(interface, br_ml_addr, group->group);

Expand Down Expand Up @@ -576,7 +581,7 @@ bool thread_extension_enabled(protocol_interface_info_entry_t *cur)
}
uint16_t securityPolicy = thread_joiner_application_security_policy_get(cur->id);

if (securityPolicy & SECURITY_POLICY_CCM_ENABLED) {
if (!(securityPolicy & SECURITY_POLICY_CCM_DISABLED)) {
return true;
}

Expand Down Expand Up @@ -615,11 +620,9 @@ void thread_extension_discover_response_read(discovery_response_list_t *nwk_info

void thread_extension_discover_response_tlv_write(uint16_t *data, uint8_t version, uint16_t securityPolicy)
{

if (version == 3 && !(securityPolicy & SECURITY_POLICY_CCM_ENABLED)) {
if (version == 3 && !(securityPolicy & SECURITY_POLICY_CCM_DISABLED)) {
*data |= (uint16_t) (1 << 10);
}
return;
}

#ifdef HAVE_THREAD_ROUTER
Expand Down
12 changes: 7 additions & 5 deletions source/6LoWPAN/Thread/thread_extension_bbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,10 @@ typedef struct {
static NS_LIST_DEFINE(pbbr_instance_list, thread_pbbr_t, link);
static NS_LIST_DEFINE(duplicate_dua_tr_list, duplicate_dua_tr_t, link);


#define THREAD_BBR_MLR_REGISTRATION_TIMEOUT 120
#define THREAD_BBR_MLR_REGISTRATION_TIMEOUT 600 //<* Default MLR timeout in seconds
#define THREAD_BBR_DUA_REGISTRATION_TIMEOUT 3600
#define THREAD_BBR_DUA_REGISTRATION_DELAY 5000 // 5 seconds in ms
#define THREAD_BBR_BACKBONE_PORT 5683 //<* Backbone border router
#define THREAD_BBR_DUA_REGISTRATION_DELAY 5000 // 5 seconds in ms
#define THREAD_BBR_BACKBONE_PORT 5683 //<* Backbone border router

/*
0 – Successful registration
Expand Down Expand Up @@ -608,8 +607,9 @@ static int thread_extension_bbr_mlr_cb(int8_t service_id, uint8_t source_address
bbr_status = THREAD_BBR_STATUS_NOT_PRIMARY_BBR;
goto send_response;
}

//check if commissioner
if (2 <= thread_meshcop_tlv_data_get_uint16(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_COMMISSIONER_SESSION_ID, &session_id)) {
//check if commissioner
// Session id present must be valid
tr_info("message from commissioner");
if (cur->thread_info->registered_commissioner.session_id != session_id) {
Expand All @@ -630,8 +630,10 @@ static int thread_extension_bbr_mlr_cb(int8_t service_id, uint8_t source_address
response_code = COAP_MSG_CODE_RESPONSE_BAD_REQUEST;
goto send_response;
}

int16_t remaining = addr_len;
uint8_t *addr_ptr = addr_data_ptr;

while (remaining > 0) {
//Go through all addresses
if (timeout_value == 0) {
Expand Down
5 changes: 5 additions & 0 deletions source/6LoWPAN/Thread/thread_extension_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ typedef struct discovery_additional_info {
bool ccm_supported:1;
}discovery_additional_info_t;

/**
* Minimum specified MLR timeout in seconds
*/
#define THREAD_DEFAULT_MIN_MLR_TIMEOUT 300

/**
* Default Autonomous enrollment port.
*/
Expand Down
32 changes: 24 additions & 8 deletions source/6LoWPAN/Thread/thread_host_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ static void thread_child_synch_receive_cb(int8_t interface_id, mle_message_t *ml
messageId = mle_tlv_validate_response(mle_msg->data_ptr, mle_msg->data_length);

if (messageId == 0) {
tr_debug("Not for me");
tr_debug("No matching challenge");
return;
}

Expand Down Expand Up @@ -476,7 +476,7 @@ void thread_mle_parent_discover_receive_cb(int8_t interface_id, mle_message_t *m
return;
}

tr_debug("Thread MLE Parent request response Handler");
tr_debug("MLE Parent response handler");
//State machine What packet should accept in this case
switch (mle_msg->message_type) {
case MLE_COMMAND_PARENT_RESPONSE: {
Expand All @@ -501,7 +501,7 @@ void thread_mle_parent_discover_receive_cb(int8_t interface_id, mle_message_t *m
messageId = mle_tlv_validate_response(mle_msg->data_ptr, mle_msg->data_length);

if (messageId == 0) {
tr_debug("Not for me");
tr_debug("No matching challenge");
return;
}

Expand Down Expand Up @@ -752,11 +752,12 @@ static void thread_mle_child_request_receive_cb(int8_t interface_id, mle_message
return;
}

tr_debug("Thread MLE Child request response Handler");
tr_debug("Thread MLE Child ID response handler");

switch (mle_msg->message_type) {

case MLE_COMMAND_CHILD_ID_RESPONSE: {
uint8_t src_mac64[8];
uint8_t shortAddress[2];
uint16_t childId;
mle_tlv_info_t routeTlv, addressRegisteredTlv, networkDataTlv;
Expand All @@ -766,15 +767,24 @@ static void thread_mle_child_request_receive_cb(int8_t interface_id, mle_message
thread_scanned_parent_t *scan_result = thread_info(cur)->thread_attach_scanned_parent;
bool new_entry_created;

tr_info("Received Child ID Response");
tr_info("Recv Child ID Response");

// Validate that response is coming from the scanned parent candidate
memcpy(src_mac64, (mle_msg->packet_src_address + 8), 8);
src_mac64[0] ^= 2;
if (memcmp(src_mac64, scan_result->mac64, 8) != 0) {
tr_debug("Drop Child ID response from previous request");
return;
}

// Clear old data
if (cur->thread_info->releaseRouterId) {
thread_bootstrap_clear_neighbor_entries(cur);
cur->thread_info->localServerDataBase.release_old_address = true;
}

thread_clean_all_routers_from_neighbor_list(cur->id);
cur->thread_info->localServerDataBase.release_old_address = true;

thread_neighbor_list_clean(cur);
thread_leader_service_stop(interface_id);
thread_leader_service_leader_data_free(cur->thread_info);
thread_merge_prepare(cur);
Expand Down Expand Up @@ -838,6 +848,11 @@ static void thread_mle_child_request_receive_cb(int8_t interface_id, mle_message
mac_helper_coordinator_address_set(cur, ADDR_802_15_4_SHORT, shortAddress);
mle_entry_timeout_update(entry_temp, thread_info(cur)->host_link_timeout);

if (scan_result->security_key_index != security_headers->KeyIndex) {
// KeyIndex has been changed between parent_response and child_id_response, reset link layer frame counter
scan_result->linLayerFrameCounter = 0;
scan_result->security_key_index = security_headers->KeyIndex;
}
mlme_device_descriptor_t device_desc;
mac_helper_device_description_write(cur, &device_desc, entry_temp->mac64, entry_temp->short_adr,scan_result->linLayerFrameCounter, false);
mac_helper_devicetable_set(&device_desc, cur, entry_temp->attribute_index, security_headers->KeyIndex, new_entry_created);
Expand Down Expand Up @@ -1030,9 +1045,10 @@ static bool thread_child_id_req_timeout(int8_t interface_id, uint16_t msgId, boo
cur->thread_info->releaseRouterId = false;

cur->nwk_bootstrap_state = ER_BOOTSRAP_DONE;
mle_service_interface_receiver_handler_update(cur->id, thread_general_mle_receive_cb);

exit:
mle_service_interface_receiver_handler_update(cur->id, thread_general_mle_receive_cb);

ns_dyn_mem_free(cur->thread_info->thread_attach_scanned_parent);
cur->thread_info->thread_attach_scanned_parent = NULL;

Expand Down
Loading

0 comments on commit 9fff108

Please sign in to comment.