correct V.24 confirmed PDU receive block extraction to the 18 byte wire block the transmit path already uses; (#141)

Inbound confirmed PDUs of more than one block failed the packet CRC-32 on
every attempt. The receive path stepped 19 bytes per block on the wire and
stored 18 byte chunks, while storeConvertedRxPDU() reads 16 bytes of user
data per block, so every block after the first was misaligned.

A confirmed block on the wire is a sequence/flag byte, 16 bytes of user data
and a reserved byte (DFSI_PDU_CONFIRMED_BLOCK_LENGTH_BYTES). The single,
header and continuation frame cases now copy the user data through one
bounded helper, and reject a frame that is too short for its block count.

The single frame layout is checked against a frame captured from a Quantar,
using the subscriber's own packet CRC-32 and the IPv4 header checksum. No
capture of a header or continuation frame was available; those follow the
layout the transmit path generates.
r05a06_dev
David Andrzejewski 11 hours ago committed by GitHub
parent ffda6d50a1
commit 4e3a34182b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -34,6 +34,13 @@ using namespace p25::dfsi::frames;
#include <cassert>
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/** @brief Usable length in bytes of the DFSICallData PDU user data buffer. */
const uint32_t RX_PDU_USER_DATA_LENGTH_BYTES = P25_MAX_PDU_BLOCKS * P25_PDU_CONFIRMED_LENGTH_BYTES;
// ---------------------------------------------------------------------------
// Public Class Members
// ---------------------------------------------------------------------------
@ -484,6 +491,34 @@ int ModemV24::write(const uint8_t* data, uint32_t length, bool imm)
}
}
/* Copies the user data of confirmed PDU blocks out of a V.24 PDU frame. */
bool ModemV24::copyConfirmedPDUBlocks(const uint8_t* frame, uint32_t frameLength, uint32_t frameOffset, uint32_t blockCnt,
uint8_t* userData, uint32_t userDataLength)
{
assert(frame != nullptr);
assert(userData != nullptr);
if (blockCnt == 0U)
return true;
// each block on the wire is a sequence/flag byte, the block user data and a reserved byte; the reserved
// byte trailing the last block is not required to be present
const uint64_t wireLength = (uint64_t)frameOffset + ((uint64_t)(blockCnt - 1U) * DFSI_PDU_CONFIRMED_BLOCK_LENGTH_BYTES) +
1U + P25_PDU_CONFIRMED_DATA_LENGTH_BYTES;
const uint64_t dataLength = (uint64_t)blockCnt * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES;
if (wireLength > frameLength || dataLength > userDataLength)
return false;
for (uint32_t i = 0U; i < blockCnt; i++) {
// skip the sequence/flag byte leading each block
const uint32_t blockOffset = frameOffset + (i * DFSI_PDU_CONFIRMED_BLOCK_LENGTH_BYTES) + 1U;
::memcpy(userData + (i * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES), frame + blockOffset, P25_PDU_CONFIRMED_DATA_LENGTH_BYTES);
}
return true;
}
// ---------------------------------------------------------------------------
// Private Class Members
// ---------------------------------------------------------------------------
@ -1277,24 +1312,18 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length)
m_rxCall->dataCall = true;
m_rxCall->dataHeader = pduHeader;
int skipBytes = 2U; // skip padding between PDU blocks -- and first block serial number
for (uint8_t i = 0U; i < pduHeader->getBlocksToFollow(); i++) {
uint32_t blockOffset = 9U + P25_PDU_HEADER_LENGTH_BYTES + (i * P25_PDU_CONFIRMED_LENGTH_BYTES) + skipBytes;
uint8_t dataBlock[P25_PDU_CONFIRMED_LENGTH_BYTES];
::memset(dataBlock, 0x00U, P25_PDU_CONFIRMED_LENGTH_BYTES);
::memcpy(dataBlock, dfsiData + blockOffset, P25_PDU_CONFIRMED_LENGTH_BYTES);
// blocks follow the embedded start of stream, the header and a reserved byte
const uint32_t blockCnt = pduHeader->getBlocksToFollow();
if (!copyConfirmedPDUBlocks(dfsiData, length - 1U, DFSI_MOT_START_LEN + P25_PDU_HEADER_LENGTH_BYTES + DFSI_PDU_RESERVED_LENGTH_BYTES,
blockCnt, m_rxCall->pduUserData, RX_PDU_USER_DATA_LENGTH_BYTES)) {
LogError(LOG_MODEM, P25_PDU_STR ", ISP, V.24 PDU frame too short for %u confirmed blocks, len = %u", blockCnt, length - 1U);
if (m_trace)
Utils::dump(1U, "ModemV24::convertToAirV24(), PDU Confirmed Data Block", dataBlock, P25_PDU_CONFIRMED_LENGTH_BYTES);
uint32_t offset = i * P25_PDU_CONFIRMED_LENGTH_BYTES;
::memcpy(m_rxCall->pduUserData + offset, dataBlock, P25_PDU_CONFIRMED_LENGTH_BYTES);
skipBytes++; // each block is separated by a padding byte
m_rxCall->resetCallData();
break;
}
if (m_trace)
Utils::dump(1U, "ModemV24::convertToAirV24(), Final PDU Confirmed Data", m_rxCall->pduUserData, (pduHeader->getBlocksToFollow() + 1U) * P25_PDU_CONFIRMED_LENGTH_BYTES);
Utils::dump(1U, "ModemV24::convertToAirV24(), Final PDU Confirmed Data", m_rxCall->pduUserData, blockCnt * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES);
storeConvertedRxPDU(pduHeader, m_rxCall->pduUserData);
}
@ -1335,23 +1364,21 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length)
m_rxCall->dataCall = true;
m_rxCall->dataHeader = pduHeader;
// PDU_CONF_HEADER only contains 3 blocks
int skipBytes = 2U; // skip padding between PDU blocks -- and first block serial number
for (uint8_t i = 0U; i < 3U; i++) {
uint32_t blockOffset = 9U + P25_PDU_HEADER_LENGTH_BYTES + (i * P25_PDU_CONFIRMED_LENGTH_BYTES) + skipBytes;
// PDU_CONF_HEADER only contains 3 blocks, following the embedded start of stream, the header and a reserved byte
const uint32_t blockCnt = DFSI_PDU_BLOCK_CNT - 1U;
if (!copyConfirmedPDUBlocks(dfsiData, length - 1U, DFSI_MOT_START_LEN + P25_PDU_HEADER_LENGTH_BYTES + DFSI_PDU_RESERVED_LENGTH_BYTES,
blockCnt, m_rxCall->pduUserData, RX_PDU_USER_DATA_LENGTH_BYTES)) {
LogError(LOG_MODEM, P25_PDU_STR ", ISP, V.24 PDU frame too short for %u confirmed blocks, len = %u", blockCnt, length - 1U);
uint8_t dataBlock[P25_PDU_CONFIRMED_LENGTH_BYTES];
::memset(dataBlock, 0x00U, P25_PDU_CONFIRMED_LENGTH_BYTES);
::memcpy(dataBlock, dfsiData + blockOffset, P25_PDU_CONFIRMED_LENGTH_BYTES);
m_rxCall->resetCallData();
break;
}
if (m_trace)
Utils::dump(1U, "ModemV24::convertToAirV24(), PDU Confirmed Data Block", dataBlock, P25_PDU_CONFIRMED_LENGTH_BYTES);
if (m_trace)
Utils::dump(1U, "ModemV24::convertToAirV24(), PDU Confirmed Data Blocks", m_rxCall->pduUserData, blockCnt * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES);
::memcpy(m_rxCall->pduUserData + m_rxCall->pduUserDataOffset, dataBlock, P25_PDU_CONFIRMED_LENGTH_BYTES);
m_rxCall->pduUserDataOffset += P25_PDU_CONFIRMED_LENGTH_BYTES;
m_rxCall->pduTotalBlocks++;
skipBytes++; // each block is separated by a padding byte
}
m_rxCall->pduUserDataOffset += blockCnt * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES;
m_rxCall->pduTotalBlocks += blockCnt;
}
break;
case DFSIFrameType::MOT_PDU_CONF_BLOCK_1:
@ -1375,27 +1402,25 @@ void ModemV24::convertToAirV24(const uint8_t *data, uint32_t length)
// 4 blocks of user data, and the PDU_CONF_END is always variable with at least the last data block
}
// PDU_CONF_BLOCK_X only contains 4 blocks each
int skipBytes = 0U; // skip padding between PDU blocks
for (uint8_t i = 0U; i < blockCnt; i++) {
uint32_t blockOffset = 1U + (i * P25_PDU_CONFIRMED_LENGTH_BYTES) + skipBytes;
// PDU_CONF_BLOCK_X only contains 4 blocks each, following the frame type byte
const uint32_t userDataSpace = (m_rxCall->pduUserDataOffset < RX_PDU_USER_DATA_LENGTH_BYTES) ?
RX_PDU_USER_DATA_LENGTH_BYTES - m_rxCall->pduUserDataOffset : 0U;
if (!copyConfirmedPDUBlocks(dfsiData, length - 1U, 1U, blockCnt, m_rxCall->pduUserData + m_rxCall->pduUserDataOffset, userDataSpace)) {
LogError(LOG_MODEM, P25_PDU_STR ", ISP, V.24 PDU frame too short for %u confirmed blocks, len = %u", blockCnt, length - 1U);
uint8_t dataBlock[P25_PDU_CONFIRMED_LENGTH_BYTES];
::memset(dataBlock, 0x00U, P25_PDU_CONFIRMED_LENGTH_BYTES);
::memcpy(dataBlock, dfsiData + blockOffset, P25_PDU_CONFIRMED_LENGTH_BYTES);
m_rxCall->resetCallData();
break;
}
if (m_trace)
Utils::dump(1U, "ModemV24::convertToAirV24(), PDU Confirmed Data Block", dataBlock, P25_PDU_CONFIRMED_LENGTH_BYTES);
if (m_trace)
Utils::dump(1U, "ModemV24::convertToAirV24(), PDU Confirmed Data Blocks", m_rxCall->pduUserData + m_rxCall->pduUserDataOffset, blockCnt * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES);
::memcpy(m_rxCall->pduUserData + m_rxCall->pduUserDataOffset, dataBlock, P25_PDU_CONFIRMED_LENGTH_BYTES);
m_rxCall->pduUserDataOffset += P25_PDU_CONFIRMED_LENGTH_BYTES;
m_rxCall->pduTotalBlocks++;
skipBytes++; // each block is separated by a padding byte
}
m_rxCall->pduUserDataOffset += blockCnt * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES;
m_rxCall->pduTotalBlocks += blockCnt;
if (frameType == DFSIFrameType::MOT_PDU_CONF_END) {
if (m_trace)
Utils::dump(1U, "ModemV24::convertToAirV24(), Final PDU Confirmed Data", m_rxCall->pduUserData, m_rxCall->pduTotalBlocks * P25_PDU_CONFIRMED_LENGTH_BYTES);
Utils::dump(1U, "ModemV24::convertToAirV24(), Final PDU Confirmed Data", m_rxCall->pduUserData, m_rxCall->pduTotalBlocks * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES);
storeConvertedRxPDU(m_rxCall->dataHeader, m_rxCall->pduUserData);
}

@ -647,6 +647,19 @@ namespace modem
*/
int write(const uint8_t* data, uint32_t length, bool imm = false) override;
/**
* @brief Copies the user data of confirmed PDU blocks out of a V.24 PDU frame.
* @param[in] frame Buffer containing the V.24 PDU frame.
* @param frameLength Length of the V.24 PDU frame in bytes.
* @param frameOffset Offset in bytes of the first confirmed block within the frame.
* @param blockCnt Number of confirmed blocks to copy.
* @param[out] userData Buffer receiving 16 bytes of user data per block; not owned.
* @param userDataLength Space available in the user data buffer in bytes.
* @returns bool True, if every block was copied, otherwise false when the frame or the buffer is too short.
*/
static bool copyConfirmedPDUBlocks(const uint8_t* frame, uint32_t frameLength, uint32_t frameOffset, uint32_t blockCnt,
uint8_t* userData, uint32_t userDataLength);
private:
bool m_rtrt;

@ -0,0 +1,164 @@
// SPDX-License-Identifier: GPL-2.0-only
/*
* Digital Voice Modem - Test Suite
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2026 David Andrzejewski
*
*/
#include "host/Defines.h"
#include "common/edac/CRC.h"
#include "common/p25/P25Defines.h"
#include "common/p25/dfsi/DFSIDefines.h"
#include "host/modem/ModemV24.h"
#include <catch2/catch_test_macros.hpp>
#include <cstring>
using namespace modem;
using namespace p25::defines;
using namespace p25::dfsi::defines;
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/** @brief Offset in bytes of the first block in a V.24 PDU frame carrying an embedded start of stream and header. */
static const uint32_t HEADER_FRAME_BLOCK_OFFSET = DFSI_MOT_START_LEN + P25_PDU_HEADER_LENGTH_BYTES + DFSI_PDU_RESERVED_LENGTH_BYTES;
/** @brief Number of confirmed blocks in the captured frame. */
static const uint32_t CAPTURED_BLOCK_CNT = 3U;
/** @brief Length in bytes of the user data carried by the captured frame, including the packet CRC-32. */
static const uint32_t CAPTURED_USER_DATA_LENGTH = CAPTURED_BLOCK_CNT * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES;
/**
* @brief Single confirmed PDU frame (frame type $8F) captured inbound from a Quantar over V.24.
* A 44 byte IPv4/UDP packet in three confirmed blocks, followed by the packet CRC-32. Each block on the wire is a
* sequence/flag byte ($00, $02, $04), 16 bytes of user data and a trailing byte.
*/
static const uint8_t CAPTURED_SINGLE_CONF_FRAME[] = {
0x8FU, 0x02U, 0x04U, 0x0CU, 0x06U, 0x00U, 0x00U, 0x00U, 0x00U, // embedded start of stream
0x56U, 0xC4U, 0x00U, 0x00U, 0x13U, 0x57U, 0x03U, 0x00U, 0x58U, 0x00U, 0xACU, 0x00U, // PDU header
0x02U, // reserved
0x00U, 0x45U, 0x00U, 0x00U, 0x2CU, 0x40U, 0xC5U, 0x00U, 0x00U, 0x40U, 0x11U, 0x24U, 0xC3U, 0x0AU, 0x19U, 0x00U, 0x0AU, 0x03U,
0x02U, 0x0AU, 0x19U, 0x00U, 0xFEU, 0x0FU, 0xA5U, 0x0FU, 0xA5U, 0x00U, 0x18U, 0xFDU, 0x0BU, 0x00U, 0x0EU, 0xF0U, 0x20U, 0x03U,
0x04U, 0x04U, 0x34U, 0x39U, 0x35U, 0x31U, 0x04U, 0x39U, 0x30U, 0x35U, 0x31U, 0x01U, 0x31U, 0x5EU, 0x9EU, 0x53U, 0x53U, 0x02U,
0xC0U, 0x01U, 0x67U, 0x02U // end metadata
};
/** @brief User data carried by the captured frame; a 44 byte IPv4/UDP packet and its CRC-32. */
static const uint8_t CAPTURED_USER_DATA[] = {
0x45U, 0x00U, 0x00U, 0x2CU, 0x40U, 0xC5U, 0x00U, 0x00U, 0x40U, 0x11U, 0x24U, 0xC3U, 0x0AU, 0x19U, 0x00U, 0x0AU,
0x0AU, 0x19U, 0x00U, 0xFEU, 0x0FU, 0xA5U, 0x0FU, 0xA5U, 0x00U, 0x18U, 0xFDU, 0x0BU, 0x00U, 0x0EU, 0xF0U, 0x20U,
0x04U, 0x34U, 0x39U, 0x35U, 0x31U, 0x04U, 0x39U, 0x30U, 0x35U, 0x31U, 0x01U, 0x31U, 0x5EU, 0x9EU, 0x53U, 0x53U
};
TEST_CASE("V.24 confirmed PDU blocks from a captured single frame carry a valid packet", "[p25][dfsi][pdu][v24]")
{
REQUIRE(sizeof(CAPTURED_SINGLE_CONF_FRAME) == 80U);
REQUIRE(sizeof(CAPTURED_USER_DATA) == CAPTURED_USER_DATA_LENGTH);
uint8_t userData[CAPTURED_USER_DATA_LENGTH];
::memset(userData, 0xFFU, CAPTURED_USER_DATA_LENGTH);
REQUIRE(ModemV24::copyConfirmedPDUBlocks(CAPTURED_SINGLE_CONF_FRAME, sizeof(CAPTURED_SINGLE_CONF_FRAME), HEADER_FRAME_BLOCK_OFFSET,
CAPTURED_BLOCK_CNT, userData, CAPTURED_USER_DATA_LENGTH));
REQUIRE(::memcmp(userData, CAPTURED_USER_DATA, CAPTURED_USER_DATA_LENGTH) == 0);
// the packet CRC-32 was generated by the subscriber, so it verifies the block boundaries independently
REQUIRE(edac::CRC::checkCRC32(userData, CAPTURED_USER_DATA_LENGTH));
// the IPv4 header checksum covers bytes on both sides of the first block boundary
uint32_t sum = 0U;
for (uint32_t i = 0U; i < 20U; i += 2U)
sum += (uint32_t)((userData[i] << 8) | userData[i + 1U]);
sum = (sum & 0xFFFFU) + (sum >> 16);
REQUIRE(sum == 0xFFFFU);
}
TEST_CASE("V.24 confirmed PDU block copy is bounded by the frame and the user data buffer", "[p25][dfsi][pdu][v24]")
{
// the reserved byte trailing the last block is not required
const uint32_t minFrameLength = HEADER_FRAME_BLOCK_OFFSET + ((CAPTURED_BLOCK_CNT - 1U) * DFSI_PDU_CONFIRMED_BLOCK_LENGTH_BYTES) +
1U + P25_PDU_CONFIRMED_DATA_LENGTH_BYTES;
uint8_t userData[CAPTURED_USER_DATA_LENGTH + 1U];
SECTION("minimum frame length is accepted") {
::memset(userData, 0xFFU, sizeof(userData));
REQUIRE(ModemV24::copyConfirmedPDUBlocks(CAPTURED_SINGLE_CONF_FRAME, minFrameLength, HEADER_FRAME_BLOCK_OFFSET,
CAPTURED_BLOCK_CNT, userData, CAPTURED_USER_DATA_LENGTH));
REQUIRE(::memcmp(userData, CAPTURED_USER_DATA, CAPTURED_USER_DATA_LENGTH) == 0);
// nothing is written past the copied blocks
REQUIRE(userData[CAPTURED_USER_DATA_LENGTH] == 0xFFU);
}
SECTION("frame one byte short is rejected without writing") {
::memset(userData, 0xFFU, sizeof(userData));
REQUIRE_FALSE(ModemV24::copyConfirmedPDUBlocks(CAPTURED_SINGLE_CONF_FRAME, minFrameLength - 1U, HEADER_FRAME_BLOCK_OFFSET,
CAPTURED_BLOCK_CNT, userData, CAPTURED_USER_DATA_LENGTH));
REQUIRE(userData[0U] == 0xFFU);
}
SECTION("user data buffer one byte short is rejected without writing") {
::memset(userData, 0xFFU, sizeof(userData));
REQUIRE_FALSE(ModemV24::copyConfirmedPDUBlocks(CAPTURED_SINGLE_CONF_FRAME, sizeof(CAPTURED_SINGLE_CONF_FRAME), HEADER_FRAME_BLOCK_OFFSET,
CAPTURED_BLOCK_CNT, userData, CAPTURED_USER_DATA_LENGTH - 1U));
REQUIRE(userData[0U] == 0xFFU);
}
SECTION("zero blocks copies nothing") {
::memset(userData, 0xFFU, sizeof(userData));
REQUIRE(ModemV24::copyConfirmedPDUBlocks(CAPTURED_SINGLE_CONF_FRAME, sizeof(CAPTURED_SINGLE_CONF_FRAME), HEADER_FRAME_BLOCK_OFFSET,
0U, userData, CAPTURED_USER_DATA_LENGTH));
REQUIRE(userData[0U] == 0xFFU);
}
SECTION("block count that overflows 32-bit length arithmetic is rejected") {
::memset(userData, 0xFFU, sizeof(userData));
REQUIRE_FALSE(ModemV24::copyConfirmedPDUBlocks(CAPTURED_SINGLE_CONF_FRAME, sizeof(CAPTURED_SINGLE_CONF_FRAME), HEADER_FRAME_BLOCK_OFFSET,
0xFFFFFFFFU, userData, CAPTURED_USER_DATA_LENGTH));
REQUIRE(userData[0U] == 0xFFU);
}
}
TEST_CASE("V.24 confirmed PDU continuation frame blocks follow the frame type byte", "[p25][dfsi][pdu][v24]")
{
// no continuation frame capture is available; this frame is built to the layout the transmit path
// generates: frame type, then for each block a sequence/flag byte, 16 bytes of user data and a reserved byte
const uint32_t frameLength = 1U + (DFSI_PDU_BLOCK_CNT * DFSI_PDU_CONFIRMED_BLOCK_LENGTH_BYTES) + 1U;
const uint32_t userDataLength = DFSI_PDU_BLOCK_CNT * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES;
uint8_t frame[1U + (DFSI_PDU_BLOCK_CNT * DFSI_PDU_CONFIRMED_BLOCK_LENGTH_BYTES) + 1U];
::memset(frame, 0x00U, frameLength);
frame[0U] = DFSIFrameType::MOT_PDU_CONF_BLOCK_1;
uint8_t expected[DFSI_PDU_BLOCK_CNT * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES];
for (uint32_t i = 0U; i < DFSI_PDU_BLOCK_CNT; i++) {
const uint32_t blockOffset = 1U + (i * DFSI_PDU_CONFIRMED_BLOCK_LENGTH_BYTES);
frame[blockOffset] = 0xEEU; // sequence/flag byte
for (uint32_t j = 0U; j < P25_PDU_CONFIRMED_DATA_LENGTH_BYTES; j++) {
const uint8_t value = (uint8_t)((i << 4) | j);
frame[blockOffset + 1U + j] = value;
expected[(i * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES) + j] = value;
}
frame[blockOffset + 1U + P25_PDU_CONFIRMED_DATA_LENGTH_BYTES] = 0xDDU; // reserved byte
}
uint8_t userData[DFSI_PDU_BLOCK_CNT * P25_PDU_CONFIRMED_DATA_LENGTH_BYTES];
::memset(userData, 0xFFU, userDataLength);
REQUIRE(ModemV24::copyConfirmedPDUBlocks(frame, frameLength, 1U, DFSI_PDU_BLOCK_CNT, userData, userDataLength));
REQUIRE(::memcmp(userData, expected, userDataLength) == 0);
}
Loading…
Cancel
Save

Powered by TurnKey Linux.