Skip to content

Commit

Permalink
Moved Wi-SUN Bootstrap Event handling to separate device handlers
Browse files Browse the repository at this point in the history
Moved Event handlers of different devices to own files and moved
functions related to those that are not used from other modes
to own modules

Added own flags for Wi-SUN Router, Wi-SUN Host and Wi-SUN Border router
To allow improved code flagging

Added wrappers for functions called from stack to use ws_common instead
of ws_bootstrap directly
  • Loading branch information
Mika Tervonen authored and Mika Tervonen committed Sep 2, 2021
1 parent 61cbdde commit 85358a6
Show file tree
Hide file tree
Showing 14 changed files with 756 additions and 566 deletions.
701 changes: 165 additions & 536 deletions source/6LoWPAN/ws/ws_bootstrap.c

Large diffs are not rendered by default.

69 changes: 52 additions & 17 deletions source/6LoWPAN/ws/ws_bootstrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,25 +82,10 @@ int ws_bootstrap_restart(int8_t interface_id);

int ws_bootstrap_restart_delayed(int8_t interface_id);

int ws_bootstrap_set_rf_config(protocol_interface_info_entry_t *cur, phy_rf_channel_configuration_s rf_configs);

int ws_bootstrap_neighbor_remove(protocol_interface_info_entry_t *cur, const uint8_t *ll_address);

int ws_bootstrap_aro_failure(protocol_interface_info_entry_t *cur, const uint8_t *ll_address);

/*State machine transactions*/
void ws_bootstrap_event_discovery_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_configuration_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_authentication_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_operation_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_routing_ready(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_disconnect(protocol_interface_info_entry_t *cur, ws_bootsrap_event_type_e event_type);

void ws_bootstrap_configuration_trickle_reset(protocol_interface_info_entry_t *cur);

void ws_bootstrap_seconds_timer(protocol_interface_info_entry_t *cur, uint32_t seconds);
Expand Down Expand Up @@ -143,21 +128,71 @@ int ws_bootstrap_test_procedure_trigger(protocol_interface_info_entry_t *cur, ws

bool ws_bootstrap_network_name_matches(const struct mcps_data_ie_list *ie_ext, const char *network_name_ptr);

/*State machine transactions*/
void ws_bootstrap_event_discovery_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_configuration_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_authentication_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_operation_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_routing_ready(protocol_interface_info_entry_t *cur);

void ws_bootstrap_event_disconnect(protocol_interface_info_entry_t *cur, ws_bootsrap_event_type_e event_type);

void ws_bootstrap_test_procedure_trigger_exec(protocol_interface_info_entry_t *cur, ws_bootsrap_procedure_t procedure);

void ws_bootstrap_network_down(protocol_interface_info_entry_t *cur);


// Bootstrap state machine state Functions
bool ws_bootstrap_state_discovery(struct protocol_interface_info_entry *cur);
bool ws_bootstrap_state_authenticate(struct protocol_interface_info_entry *cur);
bool ws_bootstrap_state_configure(struct protocol_interface_info_entry *cur);
bool ws_bootstrap_state_wait_rpl(struct protocol_interface_info_entry *cur);
bool ws_bootstrap_state_active(struct protocol_interface_info_entry *cur);
void ws_bootstrap_state_disconnect(protocol_interface_info_entry_t *cur, ws_bootsrap_event_type_e event_type);
void ws_bootstrap_state_change(protocol_interface_info_entry_t *cur, icmp_state_t nwk_bootstrap_state);

void ws_bootstrap_pan_information_store(struct protocol_interface_info_entry *cur, const struct mcps_data_ind_s *data, ws_utt_ie_t *ws_utt, ws_us_ie_t *ws_us, ws_pan_information_t *pan_information);
void ws_bootstrap_candidate_list_clean(struct protocol_interface_info_entry *cur, uint8_t pan_max, uint32_t current_time, uint16_t pan_id);
void ws_bootstrap_candidate_parent_store(parent_info_t *parent, const struct mcps_data_ind_s *data, ws_utt_ie_t *ws_utt, ws_us_ie_t *ws_us, ws_pan_information_t *pan_information);
void ws_bootstrap_candidate_table_reset(protocol_interface_info_entry_t *cur);
parent_info_t *ws_bootstrap_candidate_parent_get(struct protocol_interface_info_entry *cur, const uint8_t *addr, bool create);
void ws_bootstrap_candidate_parent_sort(struct protocol_interface_info_entry *cur, parent_info_t *new_entry);

void ws_bootstrap_primary_parent_set(struct protocol_interface_info_entry *cur, struct llc_neighbour_req *neighbor_info, ws_parent_synch_e synch_req);
void ws_bootstrap_parent_confirm(protocol_interface_info_entry_t *cur, struct rpl_instance *instance);
bool ws_bootstrap_neighbor_info_request(struct protocol_interface_info_entry *interface, const uint8_t *mac_64, struct llc_neighbour_req *neighbor_buffer, bool request_new);
void ws_bootstrap_primary_parent_set(struct protocol_interface_info_entry *cur, struct llc_neighbour_req *neighbor_info, ws_parent_synch_e synch_req);
void ws_bootstrap_neighbor_list_clean(struct protocol_interface_info_entry *interface);
void ws_nud_table_reset(protocol_interface_info_entry_t *cur);


void ws_bootstrap_configure_csma_ca_backoffs(protocol_interface_info_entry_t *cur, uint8_t max_backoffs, uint8_t min_be, uint8_t max_be);
void ws_fhss_configure_channel_masks(protocol_interface_info_entry_t *cur, fhss_ws_configuration_t *fhss_configuration);
int8_t ws_fhss_set_defaults(protocol_interface_info_entry_t *cur, fhss_ws_configuration_t *fhss_configuration);
void ws_bootstrap_fhss_activate(protocol_interface_info_entry_t *cur);
uint16_t ws_randomize_fixed_channel(uint16_t configured_fixed_channel, uint8_t number_of_channels, uint32_t *channel_mask);
int ws_bootstrap_set_domain_rf_config(protocol_interface_info_entry_t *cur);
void ws_bootstrap_configure_max_retries(protocol_interface_info_entry_t *cur, uint8_t max_mac_retries);
void ws_bootstrap_configure_data_request_restart(protocol_interface_info_entry_t *cur, uint8_t cca_failure_restart_max, uint8_t tx_failure_restart_max, uint16_t blacklist_min_ms, uint16_t blacklist_max_ms);


void ws_bootstrap_llc_hopping_update(struct protocol_interface_info_entry *cur, const fhss_ws_configuration_t *fhss_configuration);

void ws_bootstrap_rpl_activate(protocol_interface_info_entry_t *cur);
void ws_bootstrap_rpl_scan_start(protocol_interface_info_entry_t *cur);

void ws_bootstrap_ip_stack_reset(protocol_interface_info_entry_t *cur);
void ws_bootstrap_ip_stack_activate(protocol_interface_info_entry_t *cur);

void ws_bootstrap_packet_congestion_init(protocol_interface_info_entry_t *cur);

void ws_bootstrap_asynch_trickle_stop(protocol_interface_info_entry_t *cur);
void ws_bootstrap_advertise_start(protocol_interface_info_entry_t *cur);
void ws_bootstrap_network_discovery_configure(protocol_interface_info_entry_t *cur);

void ws_bootstrap_network_start(protocol_interface_info_entry_t *cur);

#else

Expand Down
222 changes: 221 additions & 1 deletion source/6LoWPAN/ws/ws_bootstrap_6lbr.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

#include <string.h>
#include "nsconfig.h"
#ifdef HAVE_WS
#if defined(HAVE_WS) && defined(HAVE_WS_BORDER_ROUTER)
#include "ns_types.h"
#include "ns_trace.h"
#include "nsdynmemLIB.h"
Expand Down Expand Up @@ -82,8 +82,67 @@
#include "libNET/src/net_dns_internal.h"
#include "Service_Libs/random_early_detection/random_early_detection_api.h"


#define TRACE_GROUP "wsbs"

static int8_t ws_bootstrap_6lbr_fhss_configure(protocol_interface_info_entry_t *cur)
{
// Read configuration of existing FHSS and start using the default values for any network
fhss_ws_configuration_t fhss_configuration;
memset(&fhss_configuration, 0, sizeof(fhss_ws_configuration_t));

if (ns_fhss_ws_configuration_get(cur->ws_info->fhss_api)) {
memcpy(&fhss_configuration, ns_fhss_ws_configuration_get(cur->ws_info->fhss_api), sizeof(fhss_ws_configuration_t));
}

//GET BSI from BBR module
fhss_configuration.bsi = ws_bbr_bsi_generate(cur);
ws_fhss_configure_channel_masks(cur, &fhss_configuration);
// Randomize fixed channels. Only used if channel plan is fixed.
cur->ws_info->cfg->fhss.fhss_uc_fixed_channel = ws_randomize_fixed_channel(cur->ws_info->cfg->fhss.fhss_uc_fixed_channel, cur->ws_info->hopping_schdule.number_of_channels, fhss_configuration.channel_mask);
cur->ws_info->cfg->fhss.fhss_bc_fixed_channel = ws_randomize_fixed_channel(cur->ws_info->cfg->fhss.fhss_bc_fixed_channel, cur->ws_info->hopping_schdule.number_of_channels, fhss_configuration.channel_mask);
ws_fhss_set_defaults(cur, &fhss_configuration);
ns_fhss_ws_configuration_set(cur->ws_info->fhss_api, &fhss_configuration);
ws_bootstrap_llc_hopping_update(cur, &fhss_configuration);

return 0;
}

static int8_t ws_bootstrap_6lbr_backbone_ip_addr_get(protocol_interface_info_entry_t *interface_ptr, uint8_t *address)
{
(void) interface_ptr;
(void) address;

if (ws_bbr_backbone_address_get(address)) {
return 0;
}

return -1;
}

static void ws_bootstrap_6lbr_eapol_congestion_init(protocol_interface_info_entry_t *cur)
{
random_early_detection_free(cur->llc_random_early_detection);
cur->llc_random_early_detection = NULL;

if (cur->llc_random_early_detection == NULL) {
cur->llc_random_early_detection = random_early_detection_create(
cur->ws_info->cfg->sec_prot.max_simult_sec_neg_tx_queue_min,
cur->ws_info->cfg->sec_prot.max_simult_sec_neg_tx_queue_max,
100, RED_AVERAGE_WEIGHT_EIGHTH);
}

random_early_detection_free(cur->llc_eapol_random_early_detection);
cur->llc_eapol_random_early_detection = NULL;

if (cur->llc_eapol_random_early_detection == NULL) {
cur->llc_eapol_random_early_detection = random_early_detection_create(
cur->ws_info->cfg->sec_prot.max_simult_sec_neg_tx_queue_min,
cur->ws_info->cfg->sec_prot.max_simult_sec_neg_tx_queue_max,
100, RED_AVERAGE_WEIGHT_EIGHTH);
}
}

static void ws_bootstrap_6lbr_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)
{
ws_bs_ie_t ws_bs_ie;
Expand Down Expand Up @@ -218,5 +277,166 @@ void ws_bootstrap_6lbr_asynch_confirm(struct protocol_interface_info_entry *inte
}
}

void ws_bootstrap_6lbr_event_handler(protocol_interface_info_entry_t *cur, arm_event_s *event)
{
ws_bootsrap_event_type_e event_type;
event_type = (ws_bootsrap_event_type_e)event->event_type;

switch (event_type) {
case WS_INIT_EVENT:
tr_debug("tasklet init");
break;
case WS_DISCOVERY_START:
tr_info("Discovery start");
protocol_mac_reset(cur);
ws_llc_reset(cur);
lowpan_adaptation_interface_reset(cur->id);
//Clear Pending Key Index State
cur->ws_info->pending_key_index_info.state = NO_PENDING_PROCESS;
cur->mac_parameters->mac_default_key_index = 0;

ipv6_destination_cache_clean(cur->id);

// Clear parent blacklist
blacklist_clear();

// All trickle timers stopped to allow entry from any state
ws_bootstrap_asynch_trickle_stop(cur);
//Init Packet congestion
ws_bootstrap_packet_congestion_init(cur);

tr_info("Border router start network");


if (!ws_bbr_ready_to_start(cur)) {
// Wi-SUN not started yet we wait for Border router permission
ws_bootstrap_state_change(cur, ER_WAIT_RESTART);
cur->nwk_nd_re_scan_count = randLIB_get_random_in_range(40, 100);
return;
}
// Clear Old information from stack

ws_nud_table_reset(cur);
ws_bootstrap_neighbor_list_clean(cur);
ws_bootstrap_ip_stack_reset(cur);
ws_pae_controller_auth_init(cur);

uint16_t pan_id = ws_bbr_pan_id_get(cur);
if (pan_id != 0xffff) {
cur->ws_info->network_pan_id = pan_id;
} else {
if (cur->ws_info->network_pan_id == 0xffff) {
cur->ws_info->network_pan_id = randLIB_get_random_in_range(0, 0xfffd);
}
}
if (!cur->ws_info->pan_information.pan_version_set) {
cur->ws_info->pan_information.pan_version = randLIB_get_random_in_range(0, 0xffff);
cur->ws_info->pan_information.pan_version_set = true;
}
cur->ws_info->pan_information.pan_size = 0;
cur->ws_info->pan_information.routing_cost = 0;
cur->ws_info->pan_information.rpl_routing_method = true;
cur->ws_info->pan_information.use_parent_bs = true;
cur->ws_info->pan_information.version = WS_FAN_VERSION_1_0;
// 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);
ws_llc_set_gtkhash(cur, gtkhash);
ws_bbr_pan_version_increase(cur);

// Set default parameters for FHSS when starting a discovery
ws_common_regulatory_domain_config(cur, &cur->ws_info->hopping_schdule);
ws_bootstrap_6lbr_fhss_configure(cur);
ws_bootstrap_set_domain_rf_config(cur);
ws_bootstrap_fhss_activate(cur);

uint8_t ll_addr[16];
addr_interface_get_ll_address(cur, ll_addr, 1);

//SET EAPOL authenticator EUI64
ws_pae_controller_border_router_addr_write(cur, cur->mac);

// Set EAPOL relay to port 10255 and authenticator relay to 10253 (and to own ll address)
ws_eapol_relay_start(cur, BR_EAPOL_RELAY_SOCKET_PORT, ll_addr, EAPOL_RELAY_SOCKET_PORT);

// Set authenticator relay to port 10253 and PAE to 10254 (and to own ll address)
ws_eapol_auth_relay_start(cur, EAPOL_RELAY_SOCKET_PORT, ll_addr, PAE_AUTH_SOCKET_PORT);

// Set PAN ID and network name to controller
ws_pae_controller_nw_info_set(cur, cur->ws_info->network_pan_id, cur->ws_info->pan_information.pan_version, cur->ws_info->cfg->gen.network_name);

// Set backbone IP address get callback
ws_pae_controller_auth_cb_register(cur, ws_bootstrap_6lbr_backbone_ip_addr_get);

// Set PAE port to 10254 and authenticator relay to 10253 (and to own ll address)
ws_pae_controller_authenticator_start(cur, PAE_AUTH_SOCKET_PORT, ll_addr, EAPOL_RELAY_SOCKET_PORT);

// Initialize eapol congestion tracking
ws_bootstrap_6lbr_eapol_congestion_init(cur);

// Set retry configuration for bootstrap ready state
ws_bootstrap_configure_max_retries(cur, WS_MAX_FRAME_RETRIES);

// Set TX failure request restart configuration
ws_bootstrap_configure_data_request_restart(cur, WS_CCA_REQUEST_RESTART_MAX, WS_TX_REQUEST_RESTART_MAX, WS_REQUEST_RESTART_BLACKLIST_MIN, WS_REQUEST_RESTART_BLACKLIST_MAX);

// Set CSMA-CA backoff configuration
ws_bootstrap_configure_csma_ca_backoffs(cur, WS_MAX_CSMA_BACKOFFS, WS_MAC_MIN_BE, WS_MAC_MAX_BE);

ws_bootstrap_event_operation_start(cur);
break;

case WS_CONFIGURATION_START:
tr_info("6LBR Configuration start");
break;
case WS_OPERATION_START:
tr_info("operation start");
// Advertisements stopped during the RPL scan
ws_bootstrap_asynch_trickle_stop(cur);
// Activate RPL
// Activate IPv6 stack
ws_bootstrap_ip_stack_activate(cur);
ws_bootstrap_rpl_activate(cur);
ws_bootstrap_network_start(cur);
// Wait for RPL start
ws_bootstrap_event_routing_ready(cur);
break;
case WS_ROUTING_READY:
tr_info("Routing ready");
// stopped all to make sure we can enter here from any state
ws_bootstrap_asynch_trickle_stop(cur);

// Indicate PAE controller that bootstrap is ready
ws_pae_controller_bootstrap_done(cur);

ws_bootstrap_advertise_start(cur);
ws_bootstrap_state_change(cur, ER_BOOTSRAP_DONE);
break;
case WS_FAST_DISCONNECT:
ws_bootstrap_state_disconnect(cur, WS_FAST_DISCONNECT);
break;
case WS_NORMAL_DISCONNECT:
ws_bootstrap_state_disconnect(cur, WS_NORMAL_DISCONNECT);
break;

case WS_TEST_PROC_TRIGGER:
ws_bootstrap_test_procedure_trigger_exec(cur, (ws_bootsrap_procedure_t) event->data_ptr);
break;

default:
tr_err("Invalid event received");
break;
}
}

#endif //HAVE_WS_BORDER_ROUTER && HAVE_WS
11 changes: 10 additions & 1 deletion source/6LoWPAN/ws/ws_bootstrap_6lbr.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@
#ifndef WS_BOOTSTRAP_6LBR_H_
#define WS_BOOTSTRAP_6LBR_H_

#ifdef HAVE_WS
#if defined(HAVE_WS) && defined(HAVE_WS_BORDER_ROUTER)

void ws_bootstrap_6lbr_asynch_ind(struct protocol_interface_info_entry *cur, const struct mcps_data_ind_s *data, const struct mcps_data_ie_list *ie_ext, uint8_t message_type);
void ws_bootstrap_6lbr_asynch_confirm(struct protocol_interface_info_entry *interface, uint8_t asynch_message);
void ws_bootstrap_6lbr_event_handler(protocol_interface_info_entry_t *cur, arm_event_s *event);

#define wisun_mode_border_router(cur) (cur->bootsrap_mode == ARM_NWK_BOOTSRAP_MODE_6LoWPAN_BORDER_ROUTER)

#else

#define ws_bootstrap_6lbr_asynch_ind(cur, data, ie_ext, message_type) ((void) 0)
#define ws_bootstrap_6lbr_asynch_confirm(interface, asynch_message) ((void) 0)
#define ws_bootstrap_6lbr_event_handler(cur, event) ((void) 0)

#define wisun_mode_border_router(cur) (false)

#endif //HAVE_WS

#endif /* WS_BOOTSTRAP_H_ */
Loading

0 comments on commit 85358a6

Please sign in to comment.