Skip to content

Commit 3bb089b

Browse files
author
Jarkko Paso
authored
Validate randomized fixed channel (#2592)
1 parent 70743a1 commit 3bb089b

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

source/6LoWPAN/ws/ws_bootstrap.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ static void ws_bootstrap_asynch_trickle_stop(protocol_interface_info_entry_t *cu
124124
static void ws_bootstrap_advertise_start(protocol_interface_info_entry_t *cur);
125125
static void ws_bootstrap_rpl_scan_start(protocol_interface_info_entry_t *cur);
126126

127+
static uint16_t ws_randomize_fixed_channel(uint16_t configured_fixed_channel, uint8_t number_of_channels, uint32_t *channel_mask);
128+
127129
typedef enum {
128130
WS_PARENT_SOFT_SYNCH = 0, /**< let FHSS make decision if synchronization is needed*/
129131
WS_PARENT_HARD_SYNCH, /**< Synch FHSS with latest synch information*/
@@ -728,18 +730,33 @@ static int8_t ws_fhss_border_router_configure(protocol_interface_info_entry_t *c
728730

729731
//GET BSI from BBR module
730732
fhss_configuration.bsi = ws_bbr_bsi_generate(cur);
731-
ws_fhss_set_defaults(cur, &fhss_configuration);
732733
ws_fhss_configure_channel_masks(cur, &fhss_configuration);
734+
// Randomize fixed channels. Only used if channel plan is fixed.
735+
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);
736+
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);
737+
ws_fhss_set_defaults(cur, &fhss_configuration);
733738
ns_fhss_ws_configuration_set(cur->ws_info->fhss_api, &fhss_configuration);
734739
ws_bootstrap_llc_hopping_update(cur, &fhss_configuration);
735740

736741
return 0;
737742
}
738743

739-
static uint16_t ws_randomize_fixed_channel(uint16_t configured_fixed_channel, uint8_t number_of_channels)
744+
static bool ws_channel_allowed(uint8_t channel, uint32_t *channel_mask)
745+
{
746+
if ((1 << (channel % 32)) & (channel_mask[channel / 32])) {
747+
return true;
748+
}
749+
return false;
750+
}
751+
752+
static uint16_t ws_randomize_fixed_channel(uint16_t configured_fixed_channel, uint8_t number_of_channels, uint32_t *channel_mask)
740753
{
741754
if (configured_fixed_channel == 0xFFFF) {
742-
return randLIB_get_random_in_range(0, number_of_channels - 1);
755+
uint16_t random_channel = randLIB_get_random_in_range(0, number_of_channels - 1);
756+
while (ws_channel_allowed(random_channel, channel_mask) == false) {
757+
random_channel = randLIB_get_random_in_range(0, number_of_channels - 1);
758+
}
759+
return random_channel;
743760
} else {
744761
return configured_fixed_channel;
745762
}
@@ -764,8 +781,8 @@ static int8_t ws_fhss_configure(protocol_interface_info_entry_t *cur, bool disco
764781
}
765782
fhss_configuration.ws_bc_channel_function = WS_FIXED_CHANNEL;
766783
fhss_configuration.fhss_broadcast_interval = 0;
767-
uint8_t tmp_uc_fixed_channel = ws_randomize_fixed_channel(cur->ws_info->cfg->fhss.fhss_uc_fixed_channel, cur->ws_info->hopping_schdule.number_of_channels);
768-
uint8_t tmp_bc_fixed_channel = ws_randomize_fixed_channel(cur->ws_info->cfg->fhss.fhss_bc_fixed_channel, cur->ws_info->hopping_schdule.number_of_channels);
784+
uint8_t tmp_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);
785+
uint8_t tmp_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);
769786
fhss_configuration.unicast_fixed_channel = tmp_uc_fixed_channel;
770787
fhss_configuration.broadcast_fixed_channel = tmp_bc_fixed_channel;
771788
ns_fhss_ws_configuration_set(cur->ws_info->fhss_api, &fhss_configuration);
@@ -3655,9 +3672,6 @@ static void ws_bootstrap_event_handler(arm_event_s *event)
36553672
ws_bootstrap_ip_stack_reset(cur);
36563673
ws_pae_controller_auth_init(cur);
36573674

3658-
// Randomize fixed channels. Only used if channel plan is fixed.
3659-
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);
3660-
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);
36613675
if (cur->ws_info->cfg->gen.network_pan_id == 0xffff) {
36623676
cur->ws_info->network_pan_id = randLIB_get_random_in_range(0, 0xfffd);
36633677
} else {

0 commit comments

Comments
 (0)