Skip to content

Commit

Permalink
LFN version and LGTK Hash IE advertisment and learn
Browse files Browse the repository at this point in the history
  • Loading branch information
Juha Heiskanen committed Aug 26, 2021
1 parent 8e07511 commit c94b306
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 13 deletions.
78 changes: 78 additions & 0 deletions source/6LoWPAN/ws/ws_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1708,6 +1708,65 @@ static void ws_bootstrap_pan_advertisement_solicit_analyse(struct protocol_inter
}
}
}
#ifdef HAVE_WS_VERSION_1_1
static void ws_bootstrap_pan_config_lfn_analyze(struct protocol_interface_info_entry *cur, const struct mcps_data_ie_list *ie_ext)
{
if (!ws_version_1_1(cur) || cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER) {
return;
}

ws_lfnver_ie_t lfn_version;
if (!ws_wp_nested_lfn_version_read(ie_ext->payloadIeList, ie_ext->payloadIeListLength, &lfn_version)) {
return; // LFN version
}

//Read LFNGTKHASH
ws_lgtkhash_ie_t ws_lgtkhash;
if (!ws_wp_nested_lgtk_hash_read(ie_ext->payloadIeList, ie_ext->payloadIeListLength, &ws_lgtkhash)) {
return;
}

if (!cur->ws_info->lfngtk.lfn_version_learned) {
if (!cur->ws_info->configuration_learned) {
trickle_inconsistent_heard(&cur->ws_info->trickle_pan_config, &cur->ws_info->trickle_params_pan_discovery);
}
} else {
if (cur->ws_info->lfngtk.lfn_version == lfn_version.lfn_version) {
return;
}

if (common_serial_number_greater_16(cur->ws_info->lfngtk.lfn_version, lfn_version.lfn_version)) {
// older version heard ignoring the message
return;
}
}

cur->ws_info->lfngtk.lfn_version = lfn_version.lfn_version;

//Clear HASH allways at new first or for first leaned one's
memset(cur->ws_info->lfngtk.lgtkhash, 0, 24);
cur->ws_info->lfngtk.lfn_version_learned = true;

//Set Active key index and hash inline bits
cur->ws_info->lfngtk.active_key_index = ws_lgtkhash.active_lgtk_index;
cur->ws_info->lfngtk.active_hash_1 = ws_lgtkhash.lgtk0;
cur->ws_info->lfngtk.active_hash_2 = ws_lgtkhash.lgtk1;
cur->ws_info->lfngtk.active_hash_3 = ws_lgtkhash.lgtk2;

if (cur->ws_info->lfngtk.active_hash_1) {
memcpy(cur->ws_info->lfngtk.lgtkhash, ws_lgtkhash.lgtk0_hash, 8);
}

if (cur->ws_info->lfngtk.active_hash_2) {
memcpy(cur->ws_info->lfngtk.lgtkhash + 8, ws_lgtkhash.lgtk1_hash, 8);
}

if (cur->ws_info->lfngtk.active_hash_3) {
memcpy(cur->ws_info->lfngtk.lgtkhash + 16, ws_lgtkhash.lgtk2_hash, 8);
}
//TODO Analyze HASH's and set LFN group key index
}
#endif


static void ws_bootstrap_pan_config_analyse(struct protocol_interface_info_entry *cur, const struct mcps_data_ind_s *data, const struct mcps_data_ie_list *ie_ext, ws_utt_ie_t *ws_utt, ws_us_ie_t *ws_us)
Expand Down Expand Up @@ -1821,6 +1880,9 @@ static void ws_bootstrap_pan_config_analyse(struct protocol_interface_info_entry
ws_bootstrap_primary_parent_set(cur, &neighbor_info, WS_PARENT_SOFT_SYNCH);
}
// no need to process more
#ifdef HAVE_WS_VERSION_1_1
ws_bootstrap_pan_config_lfn_analyze(cur, ie_ext);
#endif
return;
} else {
// received version is different so we need to reset the trickle
Expand Down Expand Up @@ -1858,6 +1920,10 @@ static void ws_bootstrap_pan_config_analyse(struct protocol_interface_info_entry

ws_pae_controller_nw_key_index_update(cur, data->Key.KeyIndex - 1);

#ifdef HAVE_WS_VERSION_1_1
ws_bootstrap_pan_config_lfn_analyze(cur, ie_ext);
#endif

if (!cur->ws_info->configuration_learned) {
// Generate own hopping schedules Follow first parent broadcast and plans and also use same unicast dwell
tr_info("learn network configuration");
Expand Down Expand Up @@ -3679,6 +3745,11 @@ static void ws_bootstrap_pan_config(protocol_interface_info_entry_t *cur)
async_req.wp_requested_nested_ie_list.pan_version_ie = true;
async_req.wp_requested_nested_ie_list.gtkhash_ie = true;
async_req.wp_requested_nested_ie_list.vp_ie = true;
#ifdef HAVE_WS_VERSION_1_1
if (ws_version_1_1(cur)) {
async_req.wp_requested_nested_ie_list.lfn_gtk_version_ie = cur->ws_info->lfngtk.lfn_version_learned;
}
#endif

ws_set_asynch_channel_list(cur, &async_req);

Expand Down Expand Up @@ -3775,6 +3846,13 @@ static void ws_bootstrap_event_handler(arm_event_s *event)
// initialize for FAN 1.1 defaults
if (ws_version_1_1(cur)) {
cur->ws_info->pan_information.version = WS_FAN_VERSION_1_1;
#ifdef HAVE_WS_VERSION_1_1
if (!cur->ws_info->lfngtk.lfn_version_learned) {
//Randomize LFN version
cur->ws_info->lfngtk.lfn_version = randLIB_get_random_in_range(0, 0xffff);
cur->ws_info->lfngtk.lfn_version_learned = true;
}
#endif
}

uint8_t *gtkhash = ws_pae_controller_gtk_hash_ptr_get(cur);
Expand Down
19 changes: 19 additions & 0 deletions source/6LoWPAN/ws/ws_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ typedef struct {
bool auto_trg_enabled;
} ws_test_proc_trg_t;

typedef struct {
uint16_t lfn_version;
bool lfn_version_learned: 1;
bool active_hash_1: 1;
bool active_hash_2: 1;
bool active_hash_3: 1;
unsigned active_key_index: 2;
uint8_t lgtkhash[24];
} ws_lfn_lgtk_t;

typedef struct {
unsigned length_of_list: 3;
ws_pcab_ie_t pcab[7];
} ws_phy_cap_info_t;

typedef NS_LIST_HEAD(ws_nud_table_entry_t, link) ws_nud_table_list_t;

typedef struct ws_info_s {
Expand Down Expand Up @@ -129,6 +144,10 @@ typedef struct ws_info_s {
ws_nud_table_list_t active_nud_process;
ws_nud_table_list_t free_nud_entries;
ws_test_proc_trg_t test_proc_trg;
#ifdef HAVE_WS_VERSION_1_1
ws_lfn_lgtk_t lfngtk;
ws_phy_cap_info_t phy_cap_info;
#endif
struct ws_cfg_s *cfg; /**< Wi-SUN configuration */
struct ws_pan_information_s pan_information;
ws_hopping_schedule_t hopping_schdule;
Expand Down
2 changes: 1 addition & 1 deletion source/6LoWPAN/ws/ws_common_defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#define WP_PAYLOAD_IE_NETNAME_TYPE 5 /**< Network Name information */
#define WP_PAYLOAD_IE_PAN_VER_TYPE 6 /**< Pan configuration version */
#define WP_PAYLOAD_IE_GTKHASH_TYPE 7 /**< GTK Hash information */
/* Wi-SUN FAN dfinition 1.1 */
/* Wi-SUN FAN definition 1.1 */
#define WP_PAYLOAD_IE_PCAP_TYPE 8 /**< PHY Capability information */
#define WP_PAYLOAD_IE_LFNVER_TYPE 9 /**< LFN Version information */
#define WP_PAYLOAD_IE_LGTKHASH_TYPE 10 /**< LFN GTK Hash Information */
Expand Down
15 changes: 8 additions & 7 deletions source/6LoWPAN/ws/ws_llc.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ typedef struct wh_ie_sub_list_s {
* @brief wp_nested_ie_sub_list_t ws asynch Nested Payload sub IE element request list
*/
typedef struct wp_nested_ie_sub_list_s {
bool us_ie: 1; /**< Unicast Schedule information */
bool bs_ie: 1; /**< Broadcast Schedule information */
bool vp_ie: 1; /**< Vendor Payload information */
bool pan_ie: 1; /**< PAN Information */
bool net_name_ie: 1; /**< Network Name information */
bool pan_version_ie: 1; /**< Pan configuration version */
bool gtkhash_ie: 1; /**< GTK Hash information */
bool us_ie: 1; /**< Unicast Schedule information */
bool bs_ie: 1; /**< Broadcast Schedule information */
bool vp_ie: 1; /**< Vendor Payload information */
bool pan_ie: 1; /**< PAN Information */
bool net_name_ie: 1; /**< Network Name information */
bool pan_version_ie: 1; /**< Pan configuration version */
bool gtkhash_ie: 1; /**< GTK Hash information */
bool lfn_gtk_version_ie: 1; /**< LFN Version & GTK Hash */
} wp_nested_ie_sub_list_t;

/**
Expand Down
44 changes: 39 additions & 5 deletions source/6LoWPAN/ws/ws_llc_data_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ typedef struct {

static NS_LIST_DEFINE(llc_data_base_list, llc_data_base_t, link);

static uint16_t ws_wp_nested_message_length(wp_nested_ie_sub_list_t requested_list, llc_ie_params_t *params);
static uint16_t ws_wp_nested_message_length(wp_nested_ie_sub_list_t requested_list, llc_data_base_t *llc_base);
static uint16_t ws_wh_headers_length(wh_ie_sub_list_t requested_list, llc_ie_params_t *params);

/** LLC message local functions */
Expand Down Expand Up @@ -378,9 +378,10 @@ static uint16_t ws_wh_headers_length(wh_ie_sub_list_t requested_list, llc_ie_par
return length;
}

static uint16_t ws_wp_nested_message_length(wp_nested_ie_sub_list_t requested_list, llc_ie_params_t *params)
static uint16_t ws_wp_nested_message_length(wp_nested_ie_sub_list_t requested_list, llc_data_base_t *llc_base)
{
uint16_t length = 0;
llc_ie_params_t *params = &llc_base->ie_params;
if (requested_list.gtkhash_ie) {
//Static 32 bytes allways
length += WS_WP_SUB_IE_ELEMENT_HEADER_LENGTH + params->gtkhash_length;
Expand Down Expand Up @@ -411,6 +412,17 @@ static uint16_t ws_wp_nested_message_length(wp_nested_ie_sub_list_t requested_li
length += 2;
}
}
#ifdef HAVE_WS_VERSION_1_1
if (requested_list.lfn_gtk_version_ie) {

length += WS_WP_SUB_IE_ELEMENT_HEADER_LENGTH + ws_wp_nested_lfn_version_length();
ws_lgtkhash_ie_t ws_lgtkhash;
ws_lgtkhash.lgtk0 = llc_base->interface_ptr->ws_info->lfngtk.active_hash_1;
ws_lgtkhash.lgtk1 = llc_base->interface_ptr->ws_info->lfngtk.active_hash_2;
ws_lgtkhash.lgtk2 = llc_base->interface_ptr->ws_info->lfngtk.active_hash_3;
length += WS_WP_SUB_IE_ELEMENT_HEADER_LENGTH + ws_wp_lgtk_hash_length(&ws_lgtkhash);
}
#endif

if (requested_list.bs_ie) {
///Dynamic length
Expand Down Expand Up @@ -1038,7 +1050,7 @@ static void ws_llc_lowpan_mpx_data_request(llc_data_base_t *base, mpx_user_t *us
nested_wp_id.us_ie = true;

uint16_t ie_header_length = ws_wh_headers_length(ie_header_mask, &base->ie_params);
uint16_t nested_ie_length = ws_wp_nested_message_length(nested_wp_id, &base->ie_params);
uint16_t nested_ie_length = ws_wp_nested_message_length(nested_wp_id, base);

uint16_t over_head_size = ie_header_length;
if (nested_ie_length) {
Expand Down Expand Up @@ -1207,7 +1219,7 @@ static void ws_llc_mpx_eapol_request(llc_data_base_t *base, mpx_user_t *user_cb,
nested_wp_id.us_ie = true;

uint16_t ie_header_length = ws_wh_headers_length(ie_header_mask, &base->ie_params);
uint16_t nested_ie_length = ws_wp_nested_message_length(nested_wp_id, &base->ie_params);
uint16_t nested_ie_length = ws_wp_nested_message_length(nested_wp_id, base);

uint16_t over_head_size = ie_header_length;
if (nested_ie_length) {
Expand Down Expand Up @@ -1783,7 +1795,7 @@ int8_t ws_llc_asynch_request(struct protocol_interface_info_entry *interface, as
request->wh_requested_ie_list.rsl_ie = false; //Never should not be a part Asynch message
request->wh_requested_ie_list.vh_ie = false;
uint16_t header_buffer_length = ws_wh_headers_length(request->wh_requested_ie_list, &base->ie_params);
uint16_t wp_nested_payload_length = ws_wp_nested_message_length(request->wp_requested_nested_ie_list, &base->ie_params);
uint16_t wp_nested_payload_length = ws_wp_nested_message_length(request->wp_requested_nested_ie_list, base);

//Allocated
uint16_t total_length = header_buffer_length;
Expand Down Expand Up @@ -1880,6 +1892,28 @@ int8_t ws_llc_asynch_request(struct protocol_interface_info_entry *interface, as
//Write Vendor spesific payload
ptr = ws_wp_nested_vp_write(ptr, base->ie_params.vendor_payload, base->ie_params.vendor_payload_length);
}

#ifdef HAVE_WS_VERSION_1_1
if (ws_version_1_1(interface)) {
if (request->wp_requested_nested_ie_list.lfn_gtk_version_ie) {
ws_lfnver_ie_t lfn_ver;
ws_lgtkhash_ie_t ws_lgtkhash;
//Write LFN Version
lfn_ver.lfn_version = interface->ws_info->lfngtk.lfn_version;
ptr = ws_wp_nested_lfn_version_write(ptr, &lfn_ver);
//Write LFN GTK Hash info
ws_lgtkhash.lgtk0 = base->interface_ptr->ws_info->lfngtk.active_hash_1;
ws_lgtkhash.lgtk1 = base->interface_ptr->ws_info->lfngtk.active_hash_2;
ws_lgtkhash.lgtk2 = base->interface_ptr->ws_info->lfngtk.active_hash_3;
ws_lgtkhash.active_lgtk_index = base->interface_ptr->ws_info->lfngtk.active_key_index;
ws_lgtkhash.lgtk0_hash = base->interface_ptr->ws_info->lfngtk.lgtkhash;
ws_lgtkhash.lgtk1_hash = base->interface_ptr->ws_info->lfngtk.lgtkhash + 8;
ws_lgtkhash.lgtk2_hash = base->interface_ptr->ws_info->lfngtk.lgtkhash + 16;
ptr = ws_wp_nested_lgtk_hash_write(ptr, &ws_lgtkhash);
}
}
#endif

}

base->interface_ptr->mac_api->mcps_data_req_ext(base->interface_ptr->mac_api, &data_req, &message->ie_ext, &request->channel_list, message->priority);
Expand Down
18 changes: 18 additions & 0 deletions test/nanostack/unittest/stub/ws_ie_lib_stub.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,21 @@ bool ws_wp_nested_network_name_read(uint8_t *data, uint16_t length, ws_wp_networ
{
return false;
}


uint16_t ws_wp_lgtk_hash_length(struct ws_lgtkhash_ie *ws_lgtkhash)
{
return 1;
}

uint8_t *ws_wp_nested_lgtk_hash_write(uint8_t *ptr, struct ws_lgtkhash_ie *ws_lgtkhash)
{
ptr += 2 + 1;
return ptr;
}

uint8_t *ws_wp_nested_lfn_version_write(uint8_t *ptr, struct ws_lfnver_ie *ws_lfnver)
{
ptr += 2 + 2;
return ptr;
}

0 comments on commit c94b306

Please sign in to comment.