Skip to content

Commit

Permalink
Fix PAN-id read bug when received 2015 frame version's.
Browse files Browse the repository at this point in the history
Change-Id: Ia1c120dd25a78b4ef0475248cd79b13b3b5b194c
  • Loading branch information
Juha Heiskanen committed Aug 15, 2018
1 parent 6bc9e00 commit 0ddff57
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 41 deletions.
49 changes: 38 additions & 11 deletions source/MAC/IEEE802_15_4/mac_header_helper_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,32 +337,59 @@ void mac_header_security_components_read(mac_pre_parsed_frame_t *buffer, mlme_se

}

uint16_t mac_header_get_src_panid(const mac_fcf_sequence_t *header, const uint8_t *ptr)
static bool mac_header_pan_full_compressed(const mac_fcf_sequence_t *header)
{

if (!header->SrcPanPresents) {
if (!header->DstPanPresents) {
return 0xffff;
}
return mac_header_get_dst_panid(header, ptr);
if (header->frameVersion == MAC_FRAME_VERSION_2015 && (!header->DstPanPresents && !header->SrcPanPresents) && header->intraPan) {
return true;
}
return false;
}

static uint16_t mac_header_read_src_pan(const mac_fcf_sequence_t *header, const uint8_t *ptr)
{
ptr += mac_fcf_lenght(header);//Skip FCF + DSN

ptr += mac_dst_address_length_with_panid(header); //Skip Dst panID & Address

return common_read_16_bit_inverse(ptr);
}

uint16_t mac_header_get_dst_panid(const mac_fcf_sequence_t *header, const uint8_t *ptr)
static uint16_t mac_header_read_dst_pan(const mac_fcf_sequence_t *header, const uint8_t *ptr)
{
ptr += mac_fcf_lenght(header);//Skip FCF + DSN

return common_read_16_bit_inverse(ptr);
}

uint16_t mac_header_get_src_panid(const mac_fcf_sequence_t *header, const uint8_t *ptr, uint16_t configured_pan_id)
{
if (mac_header_pan_full_compressed(header)) {
return configured_pan_id;
}

if (!header->SrcPanPresents) {
if (!header->DstPanPresents) {
return 0xffff;
}
return mac_header_read_dst_pan(header, ptr);
}

return mac_header_read_src_pan(header, ptr);
}

uint16_t mac_header_get_dst_panid(const mac_fcf_sequence_t *header, const uint8_t *ptr, uint16_t configured_pan_id)
{
if (mac_header_pan_full_compressed(header)) {
return configured_pan_id;
}
if (!header->DstPanPresents) {
if (header->SrcPanPresents && header->frameVersion == MAC_FRAME_VERSION_2015 && header->DstAddrMode == MAC_ADDR_MODE_NONE) {
return mac_header_read_src_pan(header, ptr);
}
return 0xffff;
}

ptr += mac_fcf_lenght(header);//Skip FCF + DSN

return common_read_16_bit_inverse(ptr);
return mac_header_read_dst_pan(header, ptr);
}

void mac_header_get_src_address(const mac_fcf_sequence_t *header, const uint8_t *ptr, uint8_t *address_ptr)
Expand Down
4 changes: 2 additions & 2 deletions source/MAC/IEEE802_15_4/mac_header_helper_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ uint16_t mac_buffer_total_payload_length(struct mac_pre_build_frame *buffer);
/**
* Next function should only call when address mode is not MAC_ADDR_MODE_NONE and parsed proper header!
*/
uint16_t mac_header_get_src_panid(const struct mac_fcf_sequence_s *header, const uint8_t *ptr);
uint16_t mac_header_get_dst_panid(const struct mac_fcf_sequence_s *header, const uint8_t *ptr);
uint16_t mac_header_get_src_panid(const struct mac_fcf_sequence_s *header, const uint8_t *ptr, uint16_t configured_pan_id);
uint16_t mac_header_get_dst_panid(const struct mac_fcf_sequence_s *header, const uint8_t *ptr, uint16_t configured_pan_id);
void mac_header_get_src_address(const struct mac_fcf_sequence_s *header, const uint8_t *ptr, uint8_t *address_ptr);
void mac_header_get_dst_address(const struct mac_fcf_sequence_s *header, const uint8_t *ptr, uint8_t *address_ptr);
uint8_t mac_address_length(uint8_t address_mode);
Expand Down
2 changes: 1 addition & 1 deletion source/MAC/IEEE802_15_4/mac_indirect_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ uint8_t mac_indirect_data_req_handle(mac_pre_parsed_frame_t *buf, protocol_inter

comm_status.status = MLME_DATA_POLL_NOTIFICATION;
//Call com status
comm_status.PANId = mac_header_get_dst_panid(&buf->fcf_dsn, mac_header_message_start_pointer(buf));
comm_status.PANId = mac_header_get_dst_panid(&buf->fcf_dsn, mac_header_message_start_pointer(buf), mac_ptr->pan_id);
comm_status.DstAddrMode = buf->fcf_dsn.DstAddrMode;;
mac_header_get_dst_address(&buf->fcf_dsn, mac_header_message_start_pointer(buf), comm_status.DstAddr);
comm_status.SrcAddrMode = buf->fcf_dsn.SrcAddrMode;
Expand Down
27 changes: 18 additions & 9 deletions source/MAC/IEEE802_15_4/mac_mcps_sap.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ static uint8_t mac_data_interface_decrypt_packet(mac_pre_parsed_frame_t *b, mlme

//READ SRC Address

uint16_t SrcPANId = mac_header_get_src_panid(&b->fcf_dsn, mac_header_message_start_pointer(b));
uint16_t SrcPANId = mac_header_get_src_panid(&b->fcf_dsn, mac_header_message_start_pointer(b), rf_mac_setup->pan_id);
mac_header_get_src_address(&b->fcf_dsn, mac_header_message_start_pointer(b), neighbour_validation.address);
neighbour_validation.addr_type = b->fcf_dsn.SrcAddrMode;
neighbour_validation.keyId = security_params->KeyIndex;
Expand Down Expand Up @@ -670,10 +670,11 @@ static uint8_t mac_data_interface_decrypt_packet(mac_pre_parsed_frame_t *b, mlme
static void mcps_comm_status_indication_generate(uint8_t status, mac_pre_parsed_frame_t *buf, mac_api_t * mac)
{
mlme_comm_status_t comm_status;
protocol_interface_rf_mac_setup_s *rf_ptr = buf->mac_class_ptr;
memset(&comm_status,0 ,sizeof(mlme_comm_status_t) );
comm_status.status = status;
//Call com status
comm_status.PANId = mac_header_get_dst_panid(&buf->fcf_dsn, mac_header_message_start_pointer(buf));
comm_status.PANId = mac_header_get_dst_panid(&buf->fcf_dsn, mac_header_message_start_pointer(buf), rf_ptr->pan_id);
comm_status.DstAddrMode = buf->fcf_dsn.DstAddrMode;;
mac_header_get_dst_address(&buf->fcf_dsn, mac_header_message_start_pointer(buf), comm_status.DstAddr);
comm_status.SrcAddrMode = buf->fcf_dsn.SrcAddrMode;
Expand Down Expand Up @@ -723,12 +724,14 @@ static int8_t mac_data_sap_rx_handler(mac_pre_parsed_frame_t *buf, protocol_inte
//Parse data
data_ind->DSN = buf->fcf_dsn.DSN;
data_ind->DstAddrMode = buf->fcf_dsn.DstAddrMode;
data_ind->DstPANId = mac_header_get_dst_panid(&buf->fcf_dsn, mac_header_message_start_pointer(buf));
mac_header_get_dst_address(&buf->fcf_dsn, mac_header_message_start_pointer(buf), data_ind->DstAddr);
data_ind->SrcAddrMode = buf->fcf_dsn.SrcAddrMode;
data_ind->SrcPANId = mac_header_get_src_panid(&buf->fcf_dsn, mac_header_message_start_pointer(buf));

mac_header_get_src_address(&buf->fcf_dsn, mac_header_message_start_pointer(buf), data_ind->SrcAddr);

data_ind->SrcPANId = mac_header_get_src_panid(&buf->fcf_dsn, mac_header_message_start_pointer(buf), rf_mac_setup->pan_id);
data_ind->DstPANId = mac_header_get_dst_panid(&buf->fcf_dsn, mac_header_message_start_pointer(buf), rf_mac_setup->pan_id);

data_ind->mpduLinkQuality = buf->LQI;
data_ind->signal_dbm = buf->dbm;
data_ind->timestamp = buf->timestamp;
Expand Down Expand Up @@ -772,6 +775,12 @@ static int8_t mac_data_sap_rx_handler(mac_pre_parsed_frame_t *buf, protocol_inte
ie_list.payloadIeListLength = buf->payloadsIeLength;
ie_list.headerIeList = buf->headerIePtr;
ie_list.headerIeListLength = buf->headerIeLength;
//Swap compressed address to broadcast when dst Address is elided
if (buf->fcf_dsn.DstAddrMode == MAC_ADDR_MODE_NONE) {
data_ind->DstAddrMode = MAC_ADDR_MODE_16_BIT;
data_ind->DstAddr[0] = 0xff;
data_ind->DstAddr[1] = 0xff;
}
mac->data_ind_ext_cb(mac, data_ind, &ie_list);

} else {
Expand All @@ -796,7 +805,7 @@ static void mac_lib_res_no_data_to_req(mac_pre_parsed_frame_t *buffer, protocol_
buf->fcf_dsn.SrcAddrMode = buffer->fcf_dsn.DstAddrMode;
buf->fcf_dsn.DstAddrMode = buffer->fcf_dsn.SrcAddrMode;
//SET PANID
buf->SrcPANId = mac_header_get_dst_panid(&buffer->fcf_dsn, mac_header_message_start_pointer(buffer));
buf->SrcPANId = mac_header_get_dst_panid(&buffer->fcf_dsn, mac_header_message_start_pointer(buffer), rf_mac_setup->pan_id);
buf->DstPANId = buf->SrcPANId;

mac_header_get_dst_address(&buffer->fcf_dsn, mac_header_message_start_pointer(buffer), buf->SrcAddr);
Expand Down Expand Up @@ -910,7 +919,7 @@ static void mac_data_interface_parse_beacon(mac_pre_parsed_frame_t *buf, protoco
uint8_t *pending_address_list = NULL;
uint8_t SuperframeSpec[2];

uint16_t src_pan_id = mac_header_get_src_panid(&buf->fcf_dsn, mac_header_message_start_pointer(buf));
uint16_t src_pan_id = mac_header_get_src_panid(&buf->fcf_dsn, mac_header_message_start_pointer(buf), rf_mac_setup->pan_id);

//validate beacon pan-id and filter other network out
if (rf_mac_setup->pan_id < 0xffff && (rf_mac_setup->pan_id != src_pan_id && !rf_mac_setup->macAcceptAnyBeacon)) {
Expand Down Expand Up @@ -1615,6 +1624,7 @@ int8_t mcps_generic_ack_build(protocol_interface_rf_mac_setup_s *rf_ptr, const m
buffer->fcf_dsn.frameVersion = fcf->frameVersion;
buffer->fcf_dsn.framePending = rf_ptr->mac_frame_pending;
buffer->fcf_dsn.DSN = fcf->DSN;
buffer->fcf_dsn.intraPan = fcf->intraPan;
buffer->fcf_dsn.sequenceNumberSuppress = fcf->sequenceNumberSuppress;
buffer->fcf_dsn.DstPanPresents = fcf->DstPanPresents;
buffer->fcf_dsn.SrcAddrMode = fcf->DstAddrMode;
Expand All @@ -1628,9 +1638,8 @@ int8_t mcps_generic_ack_build(protocol_interface_rf_mac_setup_s *rf_ptr, const m

buffer->mac_header_length_with_security += mac_header_address_length(&buffer->fcf_dsn);

buffer->DstPANId = mac_header_get_src_panid(fcf, data_ptr);
buffer->SrcPANId = mac_header_get_dst_panid(fcf, data_ptr);

buffer->DstPANId = mac_header_get_src_panid(fcf, data_ptr, rf_ptr->pan_id);
buffer->SrcPANId = mac_header_get_dst_panid(fcf, data_ptr, rf_ptr->pan_id);
mac_header_get_src_address(fcf, data_ptr, buffer->DstAddr);
mac_header_get_dst_address(fcf, data_ptr, buffer->SrcAddr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,21 +268,21 @@ bool test_mac_header_get_src_panid()
seq.SrcPanPresents = false;


if( 0xffff != mac_header_get_src_panid(&seq, &ptr) ){
if( 0xffff != mac_header_get_src_panid(&seq, &ptr, 0xffff) ){
return false;
}
seq.SrcPanPresents = false;
seq.DstPanPresents = true;

mac_header_get_src_panid(&seq, &ptr);
mac_header_get_src_panid(&seq, &ptr, 0xffff);
if (memcmp(common_functions_stub.argument_table, &ptr[3], 2)) {
return false;
}

seq.DstPanPresents = false;
seq.SrcPanPresents = true;

mac_header_get_src_panid(&seq, &ptr);
mac_header_get_src_panid(&seq, &ptr, 0xffff);
if (memcmp(common_functions_stub.argument_table, &ptr[3], 2)) {
return false;
}
Expand All @@ -291,15 +291,15 @@ bool test_mac_header_get_src_panid()
ptr[8] = 0;
seq.DstPanPresents = true;
seq.DstAddrMode = MAC_ADDR_MODE_16_BIT;
mac_header_get_src_panid(&seq, &ptr);
mac_header_get_src_panid(&seq, &ptr, 0xffff);
if (memcmp(common_functions_stub.argument_table, &ptr[7], 2)) {
return false;
}

ptr[13] = 5;
ptr[14] = 0;
seq.DstAddrMode = MAC_ADDR_MODE_64_BIT;
mac_header_get_src_panid(&seq, &ptr);
mac_header_get_src_panid(&seq, &ptr, 0xffff);
if (memcmp(common_functions_stub.argument_table, &ptr[13], 2)) {
return false;
}
Expand All @@ -316,12 +316,12 @@ bool test_mac_header_get_dst_panid()
ptr[3] = 3;
ptr[4] = 0;
seq.DstPanPresents = false;
uint16_t ret = mac_header_get_dst_panid(&seq, &ptr);
uint16_t ret = mac_header_get_dst_panid(&seq, &ptr, 0xffff);
if( 0xffff != ret ){
return false;
}
seq.DstPanPresents = true;
mac_header_get_dst_panid(&seq, &ptr);
mac_header_get_dst_panid(&seq, &ptr,0xffff);
if (memcmp(common_functions_stub.argument_table, &ptr[3], 2)) {
return false;
}
Expand Down
16 changes: 7 additions & 9 deletions test/nanostack/unittest/mac/mac_mcps_sap/test_mac_mcps_sap.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,23 +649,16 @@ bool test_mac_mcps_data_confirmation()
event_stub.int8_value = 0;
int8_t mac_tasklet_id = mac_mcps_sap_tasklet_init();
mcps_data_req_t data_req;
memset(&data_req, 0, sizeof(mcps_data_req_t));

ccm_globals_t ccm_ptr;
mac_api_t mac_api;
mlme_key_descriptor_t key_description;
mlme_device_descriptor_t device_description;
mac_aux_security_header_t secuirity_params;
mlme_key_device_descriptor_t key_device_description;
memset(&data_req, 0, sizeof(mcps_data_req_t));
memset(&mac_api, 0, sizeof(mac_api_t));

uint8_t tx_buf[127];
uint8_t tx_temp[200];
protocol_interface_rf_mac_setup_s *rf_mac_setup = test_mac_rf_mac_class_allocate();
memset(tx_buf, 1, 127);

memset(&key_description, 0, sizeof(mlme_key_descriptor_t));
memset(&device_description, 0, sizeof(mlme_device_descriptor_t));

mac_api.data_conf_cb = &test_mcps_data_confirm;
mac_api.mlme_conf_cb = &test_mlme_confirm;
mac_api.data_ind_cb = &test_mcps_data_indication;
Expand All @@ -675,6 +668,8 @@ bool test_mac_mcps_data_confirmation()

rf_mac_setup->macUpState = true;
sw_mac_stub.mac_api_ptr = &mac_api;
mac_header_helper_functions_stub.uint8_value = 0;
memset(&mac_header_helper_functions_stub.security_header, 0, sizeof(mac_header_helper_functions_stub.security_header));

data_req.msdu = tx_buf;
data_req.msduLength = 10;
Expand Down Expand Up @@ -2287,6 +2282,7 @@ bool test_mcps_sap_data_req_ext_handler()
memset(&secuirity_params, 0, sizeof(mac_aux_security_header_t));
mcps_data_req_t data_req;
memset(&data_req, 0, sizeof(mcps_data_req_t));
memset(&mac_header_helper_functions_stub.security_header, 0, sizeof(mac_header_helper_functions_stub.security_header));

uint8_t tx_buf[127];
uint8_t tx_buf_driver[127];
Expand Down Expand Up @@ -2364,6 +2360,7 @@ bool test_mcps_sap_data_req_handler()
memset(&secuirity_params, 0, sizeof(mac_aux_security_header_t));
mcps_data_req_t data_req;
memset(&data_req, 0, sizeof(mcps_data_req_t));
memset(&mac_header_helper_functions_stub.security_header, 0, sizeof(mac_header_helper_functions_stub.security_header));

uint8_t tx_buf[127];
uint8_t msdu_handle = 0;
Expand Down Expand Up @@ -3003,6 +3000,7 @@ bool test_mcps_sap_data_req_handler_ext()
memset(&ie_list, 0, sizeof(mcps_data_req_ie_list_t));
channel_list_s asynch_channel_list;
memset(&asynch_channel_list, 0, sizeof(channel_list_s));
memset(&mac_header_helper_functions_stub.security_header, 0, sizeof(mac_header_helper_functions_stub.security_header));

ns_ie_iovec_t vector_list[3];

Expand Down
2 changes: 2 additions & 0 deletions test/nanostack/unittest/mac/mac_pd_sap/test_mac_pd_sap.c
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ bool test_mac_pd_sap_data_cb()

// FHSS TESTS START
mac_pre_build_frame_t temp_buf;

memset(&temp_buf, 0, sizeof(mac_pre_build_frame_t));
rf_ptr.active_pd_data_request = &temp_buf;
mac_mcps_sap_stub.int8_value = 0;
nsdynmemlib_stub.returnCounter = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ uint16_t mac_header_off_set_to_aux_header(const mac_fcf_sequence_t *fcf)
return 0;
}

uint16_t mac_header_get_src_panid(const mac_fcf_sequence_t *header, const uint8_t *ptr)
uint16_t mac_header_get_src_panid(const mac_fcf_sequence_t *header, const uint8_t *ptr, uint16_t configured_pan_id)
{
return mac_header_helper_functions_stub.pan_src;
}

uint16_t mac_header_get_dst_panid(const mac_fcf_sequence_t *header, const uint8_t *ptr)
uint16_t mac_header_get_dst_panid(const mac_fcf_sequence_t *header, const uint8_t *ptr, uint16_t configured_pan_id)
{
return mac_header_helper_functions_stub.uint16_value;
}
Expand Down

0 comments on commit 0ddff57

Please sign in to comment.