From 5beef7405c89b3891236eeee244b909e757833d4 Mon Sep 17 00:00:00 2001 From: itziar Date: Mon, 18 Mar 2019 11:08:57 +0100 Subject: [PATCH 1/2] Bug related to downlink sequence counter rollover --- features/lorawan/lorastack/mac/LoRaMac.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/features/lorawan/lorastack/mac/LoRaMac.cpp b/features/lorawan/lorastack/mac/LoRaMac.cpp index a971fb9cd4..032094d9df 100644 --- a/features/lorawan/lorastack/mac/LoRaMac.cpp +++ b/features/lorawan/lorastack/mac/LoRaMac.cpp @@ -319,10 +319,13 @@ bool LoRaMac::message_integrity_check(const uint8_t *const payload, sequence_counter_prev = (uint16_t) * downlink_counter; sequence_counter_diff = sequence_counter - sequence_counter_prev; *downlink_counter += sequence_counter_diff; - if (sequence_counter < sequence_counter_prev) { - *downlink_counter += 0x10000; + + if (sequence_counter_diff >= _lora_phy->get_maximum_frame_counter_gap()) { + _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOST; + _mcps_indication.dl_frame_counter = *downlink_counter; + return false; } - + // sizeof nws_skey must be the same as _params.keys.nwk_skey, _lora_crypto.compute_mic(payload, size - LORAMAC_MFR_LEN, nwk_skey, @@ -332,13 +335,7 @@ bool LoRaMac::message_integrity_check(const uint8_t *const payload, if (mic_rx != mic) { _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_MIC_FAIL; return false; - } - - if (sequence_counter_diff >= _lora_phy->get_maximum_frame_counter_gap()) { - _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOST; - _mcps_indication.dl_frame_counter = *downlink_counter; - return false; - } + } return true; } From 4c1fedd72a84b9e87781a404a67912a740d5394f Mon Sep 17 00:00:00 2001 From: itziar Date: Tue, 19 Mar 2019 08:01:52 +0100 Subject: [PATCH 2/2] Applied suggested astyle fix(es) --- features/lorawan/lorastack/mac/LoRaMac.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/features/lorawan/lorastack/mac/LoRaMac.cpp b/features/lorawan/lorastack/mac/LoRaMac.cpp index 032094d9df..1b6768cd9e 100644 --- a/features/lorawan/lorastack/mac/LoRaMac.cpp +++ b/features/lorawan/lorastack/mac/LoRaMac.cpp @@ -319,13 +319,13 @@ bool LoRaMac::message_integrity_check(const uint8_t *const payload, sequence_counter_prev = (uint16_t) * downlink_counter; sequence_counter_diff = sequence_counter - sequence_counter_prev; *downlink_counter += sequence_counter_diff; - + if (sequence_counter_diff >= _lora_phy->get_maximum_frame_counter_gap()) { _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_DOWNLINK_TOO_MANY_FRAMES_LOST; _mcps_indication.dl_frame_counter = *downlink_counter; return false; } - + // sizeof nws_skey must be the same as _params.keys.nwk_skey, _lora_crypto.compute_mic(payload, size - LORAMAC_MFR_LEN, nwk_skey, @@ -335,7 +335,7 @@ bool LoRaMac::message_integrity_check(const uint8_t *const payload, if (mic_rx != mic) { _mcps_indication.status = LORAMAC_EVENT_INFO_STATUS_MIC_FAIL; return false; - } + } return true; }