From 6919ba130192c51b52d7b3b84320bc6cfedb73e1 Mon Sep 17 00:00:00 2001 From: Juha Heiskanen Date: Mon, 2 Dec 2019 09:28:22 +0200 Subject: [PATCH] Wi-sun New key index activate. Delayed default key index activate after first config message confirmation. Change-Id: Ibcfbe82ad651f6ad4570180cade192406d566d8d --- source/6LoWPAN/ws/ws_bootstrap.c | 33 ++++++++++++++++++++++++---- source/6LoWPAN/ws/ws_common.c | 3 +++ source/6LoWPAN/ws/ws_common.h | 10 +++++++++ source/NWK_INTERFACE/protocol_core.c | 1 + 4 files changed, 43 insertions(+), 4 deletions(-) diff --git a/source/6LoWPAN/ws/ws_bootstrap.c b/source/6LoWPAN/ws/ws_bootstrap.c index a3d554f719ae..535b018aba5c 100644 --- a/source/6LoWPAN/ws/ws_bootstrap.c +++ b/source/6LoWPAN/ws/ws_bootstrap.c @@ -1465,8 +1465,13 @@ static void ws_bootstrap_asynch_ind(struct protocol_interface_info_entry *cur, c static void ws_bootstrap_asynch_confirm(struct protocol_interface_info_entry *interface, uint8_t asynch_message) { ws_stats_update(interface, STATS_WS_ASYNCH_TX, 1); - (void)interface; - (void)asynch_message; + if (interface->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER) { + if (asynch_message == WS_FT_PAN_CONF && interface->ws_info->pending_key_index_info.state == PENDING_KEY_INDEX_ACTIVATE) { + interface->ws_info->pending_key_index_info.state = NO_PENDING_PROCESS; + tr_info("Activate new default key %u", interface->ws_info->pending_key_index_info.index + 1); + mac_helper_security_auto_request_key_index_set(interface, interface->ws_info->pending_key_index_info.index, interface->ws_info->pending_key_index_info.index + 1); + } + } } uint32_t ws_time_from_last_unicast_traffic(uint32_t current_time_stamp, ws_neighbor_class_entry_t *ws_neighbor) @@ -2410,7 +2415,16 @@ static void ws_bootstrap_nw_key_clear(protocol_interface_info_entry_t *cur, uint static void ws_bootstrap_nw_key_index_set(protocol_interface_info_entry_t *cur, uint8_t index) { - // Set send key + + if (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER) { + if (cur->mac_parameters->mac_default_key_index != 0 && cur->mac_parameters->mac_default_key_index != index + 1) { + tr_info("New Pending key Request %u", index + 1); + cur->ws_info->pending_key_index_info.state = PENDING_KEY_INDEX_ADVERTISMENT; + cur->ws_info->pending_key_index_info.index = index; + return; + } + } + mac_helper_security_auto_request_key_index_set(cur, index, index + 1); } @@ -2660,7 +2674,13 @@ static void ws_bootstrap_pan_config(protocol_interface_info_entry_t *cur) async_req.security.SecurityLevel = mac_helper_default_security_level_get(cur); async_req.security.KeyIdMode = mac_helper_default_security_key_id_mode_get(cur); - async_req.security.KeyIndex = mac_helper_default_key_index_get(cur); + if (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER && cur->ws_info->pending_key_index_info.state == PENDING_KEY_INDEX_ADVERTISMENT) { + async_req.security.KeyIndex = cur->ws_info->pending_key_index_info.index + 1; + cur->ws_info->pending_key_index_info.state = PENDING_KEY_INDEX_ACTIVATE; + } else { + async_req.security.KeyIndex = mac_helper_default_key_index_get(cur); + } + ws_llc_asynch_request(cur, &async_req); } @@ -2680,6 +2700,11 @@ static void ws_bootstrap_event_handler(arm_event_s *event) break; case WS_DISCOVERY_START: tr_info("Discovery start"); + + //Clear Pending Key Index State + cur->ws_info->pending_key_index_info.state = NO_PENDING_PROCESS; + cur->mac_parameters->mac_default_key_index = 0; + // All trickle timers stopped to allow entry from any state cur->ws_info->trickle_pa_running = false; cur->ws_info->trickle_pc_running = false; diff --git a/source/6LoWPAN/ws/ws_common.c b/source/6LoWPAN/ws/ws_common.c index 449a4d8e0d87..92792c393ab6 100644 --- a/source/6LoWPAN/ws/ws_common.c +++ b/source/6LoWPAN/ws/ws_common.c @@ -316,6 +316,9 @@ int8_t ws_common_allocate_and_init(protocol_interface_info_entry_t *cur) cur->ws_info->fhss_bc_dwell_interval = WS_FHSS_BC_DWELL_INTERVAL; cur->ws_info->fhss_uc_channel_function = WS_DH1CF; cur->ws_info->fhss_bc_channel_function = WS_DH1CF; + + cur->ws_info->pending_key_index_info.state = NO_PENDING_PROCESS; + for (uint8_t n = 0; n < 8; n++) { cur->ws_info->fhss_channel_mask[n] = 0xffffffff; } diff --git a/source/6LoWPAN/ws/ws_common.h b/source/6LoWPAN/ws/ws_common.h index a901d08b27d7..18cb9fe14407 100644 --- a/source/6LoWPAN/ws/ws_common.h +++ b/source/6LoWPAN/ws/ws_common.h @@ -60,6 +60,15 @@ typedef struct ws_nud_table_entry { ns_list_link_t link; } ws_nud_table_entry_t; +#define NO_PENDING_PROCESS 0 +#define PENDING_KEY_INDEX_ADVERTISMENT 1 +#define PENDING_KEY_INDEX_ACTIVATE 2 + +typedef struct { + unsigned state: 2; + uint8_t index; +} ws_pending_key_index_t; + typedef NS_LIST_HEAD(ws_nud_table_entry_t, link) ws_nud_table_list_t; typedef struct ws_info_s { @@ -88,6 +97,7 @@ typedef struct ws_info_s { bool trickle_pcs_running: 1; bool trickle_pc_running: 1; bool fhss_owner: 1; + ws_pending_key_index_t pending_key_index_info; // default fhss parameters for this device uint8_t fhss_uc_dwell_interval; uint8_t fhss_bc_dwell_interval; diff --git a/source/NWK_INTERFACE/protocol_core.c b/source/NWK_INTERFACE/protocol_core.c index f51599eb99e5..279bc7f0df61 100644 --- a/source/NWK_INTERFACE/protocol_core.c +++ b/source/NWK_INTERFACE/protocol_core.c @@ -603,6 +603,7 @@ static protocol_interface_info_entry_t *protocol_core_interface_6lowpan_entry_ge entry->mac_parameters->mac_prev_key_attribute_id = 0; entry->mac_parameters->mac_default_key_attribute_id = 1; entry->mac_parameters->mac_next_key_attribute_id = 2; + entry->mac_parameters->mac_default_key_index = 0; entry->beacon_cb = beacon_received;