From a4f8829b45716af133ba2a61b35195ce6a5a8f73 Mon Sep 17 00:00:00 2001 From: Tim WD6AWP Date: Wed, 12 Aug 2026 07:54:47 -0700 Subject: [PATCH] R05a06 dev (#126) * fix(p25): guard mid-call LDU1 identity against marginal FEC miscorrection RS634717::decode241213/decode24169 already computed a corrected-symbol count and threw it away, so a decode() call taking the RS(24,12,13) LDU1 LC field to the edge of its correction guarantee (miscorrection of a still-CRC-valid-looking codeword) was as trusted as a clean decode. Voice::process() also never re-checked a mid-call LDU1's srcId/dstId against anything once a call was established. Thread the RS error count up through LC::decodeLDU1/decodeLDU2 (LC::FECErrs), and in Voice::process() treat a mid-call LDU1 as suspect if either: - it required RS correction and disagrees with the call's last known-good LDU1, or - its dstId disagrees with the dstId from this call's HDU, which is far more strongly protected (Golay(18,6,8)+RS(36,20,17)) and fixed for the life of the call, regardless of the LDU1 decode's own error count. On either, keep the established identity for that frame instead of adopting the new one; audio is untouched. Neither check runs at call grant/establishment, so this doesn't affect ACL or ad hoc/unregistered srcIds originating a call -- it only stops an already-established call's identity from being silently redirected mid-transmission. * fix(modem): use RS-corrected LC content instead of pre-correction DFSI bytes In ModemV24's DFSI parsing (both convertToAirV24 and convertToAirTIA, LDU1 and LDU2), srcId/dstId/lco/mfId and algId/kId/MI were latched directly from the raw wire bytes as they arrived, before the RS decode of that same data (m_rs.decode241213/decode24169 on m_rxCall->LDULC) ran. When the decoder found and corrected symbol errors, the correction was computed and discarded -- the pre- correction, still-wrong raw values were what actually got used to build the outgoing frame. Only a hard decode failure (ret == false) triggered any recovery. Thread the RS decoder's corrected-error count out via the errs out-param added to RS634717::decode241213/decode24169, and on a successful decode with errs > 0, re-derive the affected fields from the now-corrected LDULC buffer: LDU1 via the existing LC::decodeLC(), LDU2 via inline extraction matching LC::decodeLDU2()'s own algId/kId/MI handling. Logs only when the correction actually changed one of these fields. Scoped entirely to ModemV24.cpp (the DFSI/V.24 route); LC.cpp, RS634717.*, Voice.cpp, and Modem.cpp (the direct RF "air" route) are unchanged. * Update FEC Waring Log Message Include LC content for better indication of what changed. * Revert Control.cpp and test files to match upstream r05a06_dev exactly per review --- src/common/edac/RS634717.cpp | 10 +++- src/common/edac/RS634717.h | 6 +- src/common/p25/lc/LC.cpp | 12 +++- src/common/p25/lc/LC.h | 9 +++ src/host/modem/ModemV24.cpp | 103 ++++++++++++++++++++++++++++++++-- src/host/p25/packet/Voice.cpp | 32 +++++++++++ 6 files changed, 162 insertions(+), 10 deletions(-) diff --git a/src/common/edac/RS634717.cpp b/src/common/edac/RS634717.cpp index f560192d..a7351104 100644 --- a/src/common/edac/RS634717.cpp +++ b/src/common/edac/RS634717.cpp @@ -300,7 +300,7 @@ RS634717::~RS634717() = default; /* Decode RS (24,12,13) FEC. */ -bool RS634717::decode241213(uint8_t* data) +bool RS634717::decode241213(uint8_t* data, int8_t* errs) { assert(data != nullptr); @@ -318,6 +318,9 @@ bool RS634717::decode241213(uint8_t* data) for (uint32_t i = 0U; i < 12U; i++, offset += 6) Utils::hex2Bin(codeword[39 + i], data, offset); + if (errs != nullptr) + *errs = (int8_t)ec; + if ((ec == -1) || (ec > 6)) { return false; } @@ -350,7 +353,7 @@ void RS634717::encode241213(uint8_t* data) /* Decode RS (24,16,9) FEC. */ -bool RS634717::decode24169(uint8_t* data) +bool RS634717::decode24169(uint8_t* data, int8_t* errs) { assert(data != nullptr); @@ -368,6 +371,9 @@ bool RS634717::decode24169(uint8_t* data) for (uint32_t i = 0U; i < 16U; i++, offset += 6) Utils::hex2Bin(codeword[39 + i], data, offset); + if (errs != nullptr) + *errs = (int8_t)ec; + if ((ec == -1) || (ec > 4)) { return false; } diff --git a/src/common/edac/RS634717.h b/src/common/edac/RS634717.h index 35739679..37488a72 100644 --- a/src/common/edac/RS634717.h +++ b/src/common/edac/RS634717.h @@ -47,9 +47,10 @@ namespace edac /** * @brief Decode RS (24,12,13) FEC. * @param data Reed-Solomon FEC encoded data to decode. + * @param[out] errs Optional, number of symbol errors corrected (0-6), or -1 if uncorrectable. * @returns bool True, if data was decoded, otherwise false. */ - bool decode241213(uint8_t* data); + bool decode241213(uint8_t* data, int8_t* errs = nullptr); /** * @brief Encode RS (24,12,13) FEC. * @param data Raw data to encode with Reed-Solomon FEC. @@ -59,9 +60,10 @@ namespace edac /** * @brief Decode RS (24,16,9) FEC. * @param data Reed-Solomon FEC encoded data to decode. + * @param[out] errs Optional, number of symbol errors corrected (0-4), or -1 if uncorrectable. * @returns bool True, if data was decoded, otherwise false. */ - bool decode24169(uint8_t* data); + bool decode24169(uint8_t* data, int8_t* errs = nullptr); /** * @brief Encode RS (24,16,9) FEC. * @param data Raw data to encode with Reed-Solomon FEC. diff --git a/src/common/p25/lc/LC.cpp b/src/common/p25/lc/LC.cpp index 18571c00..d51bbd9b 100644 --- a/src/common/p25/lc/LC.cpp +++ b/src/common/p25/lc/LC.cpp @@ -187,6 +187,7 @@ LC::LC() : m_macPduOffset(P2_MAC_HEADER_OFFSET::NO_VOICE_OR_UNK), m_macPartition(P2_MAC_MCO_PARTITION::ABBREVIATED), m_rsValue(0U), + m_fecErrs(-1), p2MCOData(nullptr), m_rs(), m_encryptOverride(false), @@ -399,13 +400,16 @@ bool LC::decodeLDU1(const uint8_t* data, bool rawOnly) // decode RS (24,12,13) FEC try { - bool ret = m_rs.decode241213(rs); + int8_t errs = -1; + bool ret = m_rs.decode241213(rs, &errs); + m_fecErrs = errs; if (!ret) { LogError(LOG_P25, "LC::decodeLDU1(), failed to decode RS (24,12,13) FEC"); return false; } } catch (...) { + m_fecErrs = -1; Utils::dump(2U, "P25, LC::decodeLDU1(), RS excepted with input data", rs, P25_LDU_LC_FEC_LENGTH_BYTES); return false; } @@ -498,13 +502,16 @@ bool LC::decodeLDU2(const uint8_t* data) // decode RS (24,16,9) FEC try { - bool ret = m_rs.decode24169(rs); + int8_t errs = -1; + bool ret = m_rs.decode24169(rs, &errs); + m_fecErrs = errs; if (!ret) { LogError(LOG_P25, "LC::decodeLDU2(), failed to decode RS (24,16,9) FEC"); return false; } } catch (...) { + m_fecErrs = -1; Utils::dump(2U, "P25, LC::decodeLDU2(), RS excepted with input data", rs, P25_LDU_LC_FEC_LENGTH_BYTES); return false; } @@ -1536,6 +1543,7 @@ void LC::copy(const LC& data) m_p2ScrambleOffsetValid = data.m_p2ScrambleOffsetValid; m_rsValue = data.m_rsValue; + m_fecErrs = data.m_fecErrs; m_algId = data.m_algId; if (m_algId != ALGO_UNENCRYPT) { diff --git a/src/common/p25/lc/LC.h b/src/common/p25/lc/LC.h index bf7b9d89..b7c0dbef 100644 --- a/src/common/p25/lc/LC.h +++ b/src/common/p25/lc/LC.h @@ -340,6 +340,15 @@ namespace p25 DECLARE_PROPERTY(ulong64_t, rsValue, RS); /** @} */ + /** @name FEC Decode Confidence */ + /** + * @brief Number of Reed-Solomon symbol errors corrected decoding the most recent + * LDU1 Link Control or LDU2 Encryption Sync word. -1 if not yet decoded, or if the + * last decode attempt was uncorrectable. + */ + DECLARE_PROPERTY(int8_t, fecErrs, FECErrs); + /** @} */ + /** @name Phase 2 Raw MCO Data */ uint8_t* p2MCOData; // ?? - this should probably be private with getters/setters /** @} */ diff --git a/src/host/modem/ModemV24.cpp b/src/host/modem/ModemV24.cpp index d76ec04c..1c07da46 100644 --- a/src/host/modem/ModemV24.cpp +++ b/src/host/modem/ModemV24.cpp @@ -1680,7 +1680,8 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length) // decode RS (24,12,13) FEC try { - bool ret = m_rs.decode241213(m_rxCall->LDULC); + int8_t rsErrs = -1; + bool ret = m_rs.decode241213(m_rxCall->LDULC, &rsErrs); if (!ret) { LogError(LOG_MODEM, "V.24/DFSI LDU1, failed to decode RS (24,12,13) FEC"); @@ -1724,6 +1725,29 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length) } } else { valid = true; + + // srcId/dstId/lco/mfId were latched above directly from the raw, pre-correction DFSI + // wire bytes (mirrored into LDULC as they arrived). If the RS decoder just corrected + // symbol errors, re-derive those fields from the now-corrected buffer instead of + // trusting the raw bytes read before correction. + if (rsErrs > 0) { + lc::LC correctedLC = lc::LC(); + if (correctedLC.decodeLC(m_rxCall->LDULC, false)) { + if (m_rxCall->lco != correctedLC.getLCO() || m_rxCall->mfId != correctedLC.getMFId() || + (correctedLC.isStandardMFId() && (m_rxCall->dstId != correctedLC.getDstId() || m_rxCall->srcId != correctedLC.getSrcId()))) { + LogWarning(LOG_MODEM, "V.24/DFSI LDU1, %d FEC corrections changed LC content, lco = $%02X (was $%02X), mfId = $%02X (was $%02X), srcId = %u (was %u), dstId = %u (was %u)", + rsErrs, correctedLC.getLCO(), m_rxCall->lco, correctedLC.getMFId(), m_rxCall->mfId, + correctedLC.getSrcId(), m_rxCall->srcId, correctedLC.getDstId(), m_rxCall->dstId); + } + + m_rxCall->lco = correctedLC.getLCO(); + m_rxCall->mfId = correctedLC.getMFId(); + if (correctedLC.isStandardMFId()) { + m_rxCall->dstId = correctedLC.getDstId(); + m_rxCall->srcId = correctedLC.getSrcId(); + } + } + } } } catch (...) { @@ -1832,7 +1856,8 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length) // decode RS (24,16,9) FEC try { - bool ret = m_rs.decode24169(m_rxCall->LDULC); + int8_t rsErrs = -1; + bool ret = m_rs.decode24169(m_rxCall->LDULC, &rsErrs); if (!ret) { LogError(LOG_MODEM, "V.24/DFSI LDU2, failed to decode RS (24,16,9) FEC"); @@ -1853,6 +1878,29 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length) } } else { valid = true; + + // algId/kId/MI were latched above directly from the raw, pre-correction DFSI wire + // bytes. If the RS decoder just corrected symbol errors, re-derive those fields from + // the now-corrected buffer instead of trusting the raw bytes read before correction. + if (rsErrs > 0) { + uint8_t correctedAlgId = m_rxCall->LDULC[9U]; + uint32_t correctedKId = (m_rxCall->LDULC[10U] << 8) + m_rxCall->LDULC[11U]; + + if (correctedAlgId != m_rxCall->algoId || correctedKId != m_rxCall->kId || + ::memcmp(m_rxCall->LDULC, m_rxCall->MI, MI_LENGTH_BYTES) != 0) { + LogWarning(LOG_MODEM, "V.24/DFSI LDU2, %d FEC corrections changed Enc Sync content, algId = $%02X (was $%02X), kId = $%04X (was $%04X)", + rsErrs, correctedAlgId, m_rxCall->algoId, correctedKId, m_rxCall->kId); + } + + m_rxCall->algoId = correctedAlgId; + if (m_rxCall->algoId != ALGO_UNENCRYPT) { + ::memcpy(m_rxCall->MI, m_rxCall->LDULC, MI_LENGTH_BYTES); + m_rxCall->kId = correctedKId; + } else { + ::memset(m_rxCall->MI, 0x00U, MI_LENGTH_BYTES); + m_rxCall->kId = 0x0000U; + } + } } } catch (...) { @@ -2386,7 +2434,8 @@ void ModemV24::convertToAirTIA(const uint8_t *data, uint32_t length) // decode RS (24,12,13) FEC try { - bool ret = m_rs.decode241213(m_rxCall->LDULC); + int8_t rsErrs = -1; + bool ret = m_rs.decode241213(m_rxCall->LDULC, &rsErrs); if (!ret) { LogError(LOG_MODEM, "TIA/DFSI LDU1, failed to decode RS (24,12,13) FEC"); @@ -2430,6 +2479,28 @@ void ModemV24::convertToAirTIA(const uint8_t *data, uint32_t length) } } else { valid = true; + + // srcId/dstId/lco/mfId were latched above directly from the raw, pre-correction DFSI + // wire bytes. If the RS decoder just corrected symbol errors, re-derive those fields + // from the now-corrected buffer instead of trusting the raw bytes read before correction. + if (rsErrs > 0) { + lc::LC correctedLC = lc::LC(); + if (correctedLC.decodeLC(m_rxCall->LDULC, false)) { + if (m_rxCall->lco != correctedLC.getLCO() || m_rxCall->mfId != correctedLC.getMFId() || + (correctedLC.isStandardMFId() && (m_rxCall->dstId != correctedLC.getDstId() || m_rxCall->srcId != correctedLC.getSrcId()))) { + LogWarning(LOG_MODEM, "TIA/DFSI LDU1, %d FEC corrections changed LC content, lco = $%02X (was $%02X), mfId = $%02X (was $%02X), srcId = %u (was %u), dstId = %u (was %u)", + rsErrs, correctedLC.getLCO(), m_rxCall->lco, correctedLC.getMFId(), m_rxCall->mfId, + correctedLC.getSrcId(), m_rxCall->srcId, correctedLC.getDstId(), m_rxCall->dstId); + } + + m_rxCall->lco = correctedLC.getLCO(); + m_rxCall->mfId = correctedLC.getMFId(); + if (correctedLC.isStandardMFId()) { + m_rxCall->dstId = correctedLC.getDstId(); + m_rxCall->srcId = correctedLC.getSrcId(); + } + } + } } } catch (...) { @@ -2538,7 +2609,8 @@ void ModemV24::convertToAirTIA(const uint8_t *data, uint32_t length) // decode RS (24,16,9) FEC try { - bool ret = m_rs.decode24169(m_rxCall->LDULC); + int8_t rsErrs = -1; + bool ret = m_rs.decode24169(m_rxCall->LDULC, &rsErrs); if (!ret) { LogError(LOG_MODEM, "TIA/DFSI LDU2, failed to decode RS (24,16,9) FEC"); @@ -2559,6 +2631,29 @@ void ModemV24::convertToAirTIA(const uint8_t *data, uint32_t length) } } else { valid = true; + + // algId/kId/MI were latched above directly from the raw, pre-correction DFSI wire + // bytes. If the RS decoder just corrected symbol errors, re-derive those fields from + // the now-corrected buffer instead of trusting the raw bytes read before correction. + if (rsErrs > 0) { + uint8_t correctedAlgId = m_rxCall->LDULC[9U]; + uint32_t correctedKId = (m_rxCall->LDULC[10U] << 8) + m_rxCall->LDULC[11U]; + + if (correctedAlgId != m_rxCall->algoId || correctedKId != m_rxCall->kId || + ::memcmp(m_rxCall->LDULC, m_rxCall->MI, MI_LENGTH_BYTES) != 0) { + LogWarning(LOG_MODEM, "TIA/DFSI LDU2, %d FEC corrections changed Enc Sync content, algId = $%02X (was $%02X), kId = $%04X (was $%04X)", + rsErrs, correctedAlgId, m_rxCall->algoId, correctedKId, m_rxCall->kId); + } + + m_rxCall->algoId = correctedAlgId; + if (m_rxCall->algoId != ALGO_UNENCRYPT) { + ::memcpy(m_rxCall->MI, m_rxCall->LDULC, MI_LENGTH_BYTES); + m_rxCall->kId = correctedKId; + } else { + ::memset(m_rxCall->MI, 0x00U, MI_LENGTH_BYTES); + m_rxCall->kId = 0x0000U; + } + } } } catch (...) { diff --git a/src/host/p25/packet/Voice.cpp b/src/host/p25/packet/Voice.cpp index 74e2bfc9..f09a1826 100644 --- a/src/host/p25/packet/Voice.cpp +++ b/src/host/p25/packet/Voice.cpp @@ -675,6 +675,38 @@ bool Voice::process(uint8_t* data, uint32_t len) m_rfUndecodableLC++; } else { + // a P25 voice call does not change its srcId/dstId mid-transmission. Two independent + // corroboration checks guard against a marginal-RF miscorrection silently redirecting + // a call. Neither runs at call grant/establishment -- only once a call's identity is + // already set -- so this never blocks a new (including unregistered/ad hoc) srcId from + // originating a call: + // 1) does this decode disagree with the call's last known-good LDU1, despite needing + // RS correction to get here (weaker signal -- the LDU1 LC is only protected by + // Hamming(10,6,3) + RS(24,12,13)); + // 2) does this decode's dstId disagree with the dstId carried in this call's HDU, which + // is far more strongly protected (Golay(18,6,8) + RS(36,20,17)) and, unlike the LDU1 + // LC, is fixed for the life of the call -- so disagreement here is a protocol + // violation, not just a low-confidence guess, regardless of how "clean" (FEC-wise) + // this LDU1's decode was. + bool fecMismatch = m_rfLC.getFECErrs() > 0 && + ((m_rfLastLDU1.getDstId() != 0U && m_rfLC.getDstId() != m_rfLastLDU1.getDstId()) || + (m_rfLastLDU1.getSrcId() != 0U && m_rfLC.getSrcId() != m_rfLastLDU1.getSrcId())); + + bool hduDstMismatch = m_rfLastHDUValid && m_rfLastHDU.getDstId() != 0U && + m_rfLC.getDstId() != m_rfLastHDU.getDstId(); + + if (fecMismatch || hduDstMismatch) { + uint32_t goodSrcId = m_rfLastLDU1.getSrcId(); + uint32_t goodDstId = hduDstMismatch ? m_rfLastHDU.getDstId() : m_rfLastLDU1.getDstId(); + + LogWarning(LOG_RF, P25_LDU1_STR ", marginal LC decode disagrees with established call identity (%d FEC corrections%s), srcId = %u (was %u), dstId = %u (was %u), keeping established identity", + m_rfLC.getFECErrs(), hduDstMismatch ? ", HDU dstId mismatch" : "", + m_rfLC.getSrcId(), goodSrcId, m_rfLC.getDstId(), goodDstId); + + m_rfLC.setSrcId(goodSrcId); + m_rfLC.setDstId(goodDstId); + } + m_rfLastLDU1 = m_rfLC; } }