fix SNDCP context lifecycle and packet validation; add length-aware control PDU decoding, install authorized static and dynamic trunked bindings only after successful activation response dispatch, revoke bindings on deactivation, require active matching NSAPI state for SN-Data, permit SNDCP bindings through IP convergence routing, and add focused codec, truncation, authorization, routing, and teardown tests;

pull/138/head
Bryan Biedenkapp 3 days ago
parent 4399e46b9d
commit ea069b3528

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
#include "Defines.h"
@ -39,9 +39,10 @@ SNDCPCtxActAccept::SNDCPCtxActAccept() : SNDCPPacket(),
/* Decode a SNDCP context activation response. */
bool SNDCPCtxActAccept::decode(const uint8_t* data)
bool SNDCPCtxActAccept::decode(const uint8_t* data, uint32_t length)
{
assert(data != nullptr);
if (data == nullptr || length < 13U)
return false;
/* stub */

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
/**
@ -44,9 +44,10 @@ namespace p25
/**
* @brief Decode a SNDCP context activation response.
* @param[in] data Buffer containing SNDCP packet data to decode.
* @param length Length of the packet data in bytes.
* @returns bool True, if decoded, otherwise false.
*/
bool decode(const uint8_t* data) override;
bool decode(const uint8_t* data, uint32_t length) override;
/**
* @brief Encode a SNDCP context activation response.
* @param[out] data Buffer to encode SNDCP packet data to.

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
#include "Defines.h"
@ -32,9 +32,10 @@ SNDCPCtxActReject::SNDCPCtxActReject() : SNDCPPacket(),
/* Decode a SNDCP context activation reject packet. */
bool SNDCPCtxActReject::decode(const uint8_t* data)
bool SNDCPCtxActReject::decode(const uint8_t* data, uint32_t length)
{
assert(data != nullptr);
if (data == nullptr || length < 2U)
return false;
/* stub */

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
/**
@ -44,9 +44,10 @@ namespace p25
/**
* @brief Decode a SNDCP context activation reject packet.
* @param[in] data Buffer containing SNDCP packet data to decode.
* @param length Length of the packet data in bytes.
* @returns bool True, if decoded, otherwise false.
*/
bool decode(const uint8_t* data) override;
bool decode(const uint8_t* data, uint32_t length) override;
/**
* @brief Encode a SNDCP context activation reject packet.
* @param[out] data Buffer to encode SNDCP packet data to.

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
#include "Defines.h"
@ -35,9 +35,10 @@ SNDCPCtxActRequest::SNDCPCtxActRequest() : SNDCPPacket(),
/* Decode a SNDCP context activation request. */
bool SNDCPCtxActRequest::decode(const uint8_t* data)
bool SNDCPCtxActRequest::decode(const uint8_t* data, uint32_t length)
{
assert(data != nullptr);
if (data == nullptr || length < 10U)
return false;
SNDCPPacket::decodeHeader(data, false);

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
/**
@ -44,9 +44,10 @@ namespace p25
/**
* @brief Decode a SNDCP context activation request.
* @param[in] data Buffer containing SNDCP packet data to decode.
* @param length Length of the packet data in bytes.
* @returns bool True, if decoded, otherwise false.
*/
bool decode(const uint8_t* data) override;
bool decode(const uint8_t* data, uint32_t length) override;
/**
* @brief Encode a SNDCP context activation request.
* @param[out] data Buffer to encode SNDCP packet data to.

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
#include "Defines.h"
@ -32,9 +32,10 @@ SNDCPCtxDeactivation::SNDCPCtxDeactivation() : SNDCPPacket(),
/* Decode a SNDCP context deactivation packet. */
bool SNDCPCtxDeactivation::decode(const uint8_t* data)
bool SNDCPCtxDeactivation::decode(const uint8_t* data, uint32_t length)
{
assert(data != nullptr);
if (data == nullptr || length < 2U)
return false;
SNDCPPacket::decodeHeader(data, false);

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
/**
@ -44,9 +44,10 @@ namespace p25
/**
* @brief Decode a SNDCP context deactivation packet.
* @param[in] data Buffer containing SNDCP packet data to decode.
* @param length Length of the packet data in bytes.
* @returns bool True, if decoded, otherwise false.
*/
bool decode(const uint8_t* data) override;
bool decode(const uint8_t* data, uint32_t length) override;
/**
* @brief Encode a SNDCP context deactivation packet.
* @param[out] data Buffer to encode SNDCP packet data to.

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
#include "Defines.h"
@ -33,18 +33,19 @@ SNDCPFactory::~SNDCPFactory() = default;
/* Create an instance of a SNDCPPacket. */
std::unique_ptr<SNDCPPacket> SNDCPFactory::create(const uint8_t* data)
std::unique_ptr<SNDCPPacket> SNDCPFactory::create(const uint8_t* data, uint32_t length)
{
assert(data != nullptr);
if (data == nullptr || length == 0U)
return nullptr;
uint8_t pduType = (uint8_t)((data[0U] >> 4) & 0x0FU); // SNDCP PDU Message Type
switch (pduType) {
case SNDCP_PDUType::ACT_TDS_CTX:
return decode(new SNDCPCtxActRequest(), data);
return decode(new SNDCPCtxActRequest(), data, length);
break;
case SNDCP_PDUType::DEACT_TDS_CTX_REQ:
return decode(new SNDCPCtxDeactivation(), data);
return decode(new SNDCPCtxDeactivation(), data, length);
break;
case SNDCP_PDUType::RF_CONFIRMED:
break;
@ -64,14 +65,15 @@ std::unique_ptr<SNDCPPacket> SNDCPFactory::create(const uint8_t* data)
/* Decode a SNDCP packet. */
std::unique_ptr<SNDCPPacket> SNDCPFactory::decode(SNDCPPacket* packet, const uint8_t* data)
std::unique_ptr<SNDCPPacket> SNDCPFactory::decode(SNDCPPacket* packet, const uint8_t* data, uint32_t length)
{
assert(packet != nullptr);
assert(data != nullptr);
if (!packet->decode(data)) {
std::unique_ptr<SNDCPPacket> result(packet);
if (!result->decode(data, length)) {
return nullptr;
}
return std::unique_ptr<SNDCPPacket>(packet);
return result;
}

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
/**
@ -50,18 +50,20 @@ namespace p25
/**
* @brief Create an instance of a SNDCPPacket.
* @param[in] data Buffer containing SNDCP packet data to decode.
* @param length Length of the packet data in bytes.
* @returns SNDCPPacket* Instance of a SNDCPPacket representing the decoded data.
*/
static std::unique_ptr<SNDCPPacket> create(const uint8_t* data);
static std::unique_ptr<SNDCPPacket> create(const uint8_t* data, uint32_t length);
private:
/**
* @brief Decode a SNDCP packet.
* @param packet Instance of SNDCPPacket.
* @param[in] data Buffer containing SNDCP packet data to decode.
* @param length Length of the packet data in bytes.
* @returns SNDCPPacket* Instance of a SNDCPPacket representing the decoded data.
*/
static std::unique_ptr<SNDCPPacket> decode(SNDCPPacket* packet, const uint8_t* data);
static std::unique_ptr<SNDCPPacket> decode(SNDCPPacket* packet, const uint8_t* data, uint32_t length);
};
} // namespace sndcp
} // namespace p25

@ -4,7 +4,7 @@
* GPLv2 Open Source. Use is subject to license terms.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright (C) 2024 Bryan Biedenkapp, N2PLL
* Copyright (C) 2024,2026 Bryan Biedenkapp, N2PLL
*
*/
/**
@ -56,9 +56,10 @@ namespace p25
/**
* @brief Decode a SNDCP packet.
* @param[in] data Buffer containing SNDCP packet data to decode.
* @param length Length of the packet data in bytes.
* @returns bool True, if decoded, otherwise false.
*/
virtual bool decode(const uint8_t* data) = 0;
virtual bool decode(const uint8_t* data, uint32_t length) = 0;
/**
* @brief Encode a SNDCP packet.
* @param[out] data Buffer to encode SNDCP packet data to.

@ -1457,22 +1457,23 @@ bool P25PacketData::processSNDCPUserData(RxStatus* status, uint32_t peerId)
return false;
}
LogInfoEx(LOG_P25, P25_PDU_STR ", SNDCP user data, llId = %u, pduType = $%02X, nsapi = %u, length = %u",
llId, sndcpType, nsapi, status->pduUserDataLength - 2U);
LogInfoEx(LOG_P25, P25_PDU_STR ", SNDCP user data, llId = %u, pduType = $%02X, nsapi = %u, length = %u", llId, sndcpType,
nsapi, status->pduUserDataLength - 2U);
// adopt the context of an SU the FNE has no record of (the FNE restarted while the
// SU kept its context), so the reply is framed as SN-Data and can be routed
// SN-Data is valid only for a context accepted by this FNE; requiring the
// negotiated NSAPI also prevents a deactivated context from being recreated by data
{
const uint64_t now = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
std::lock_guard<std::mutex> guard(m_sndcpContextLock);
SNDCPContext& ctx = m_sndcpContexts[llId];
if (!ctx.active) {
ctx.active = true;
ctx.nsapi = nsapi;
ctx.vcPeerId = peerId;
ctx.onChannelUntil = now + SNDCP_ON_CHANNEL_MS;
auto it = m_sndcpContexts.find(llId);
if (it == m_sndcpContexts.end() || !it->second.active || it->second.nsapi != nsapi) {
LogWarning(LOG_P25, P25_PDU_STR ", SNDCP user data without an active matching context, llId = %u, nsapi = %u", llId, nsapi);
return false;
}
it->second.vcPeerId = peerId;
it->second.onChannelUntil = now + SNDCP_ON_CHANNEL_MS;
}
// strip the SNDCP header, leaving a bare IPv4 datagram
@ -1482,6 +1483,58 @@ bool P25PacketData::processSNDCPUserData(RxStatus* status, uint32_t peerId)
return true;
}
/* Dispatches an activation response and publishes the accepted SNDCP context. */
bool P25PacketData::commitSNDCPActivation(DataHeader& responseHeader, uint8_t* responseData,
uint32_t llId, uint8_t nsapi, uint32_t ipAddress, DataBindingOrigin origin)
{
if (llId == 0U || llId == WUID_ALL || ipAddress == 0U)
return false;
// reject an address already assigned to another logical link ID
const IPBinding* owner = m_bindingRegistry.findByIPAddress(ipAddress);
if (owner != nullptr && owner->link.llId != llId) {
LogWarning(LOG_P25, P25_PDU_STR ", SNDCP address already assigned, llId = %u, ipAddr = %s, owner = %u", llId, __IP_FROM_UINT(ipAddress).c_str(), owner->link.llId);
return false;
}
// dispatch the activation response before publishing the accepted context
if (!dispatchUserFrameToFNE(responseHeader, false, false, responseData)) {
LogWarning(LOG_P25, P25_PDU_STR ", failed to dispatch SNDCP context activation response, llId = %u", llId);
return false;
}
IPBinding binding;
binding.link.llId = llId;
binding.ipAddress = ipAddress;
binding.accessMode = DataAccessMode::TRUNKED;
binding.convergenceMode = DataConvergenceMode::SNDCP;
binding.origin = origin;
binding.nsapi = nsapi;
binding.hasNSAPI = true;
binding.authorized = true;
if (!m_bindingRegistry.upsert(binding)) {
LogError(LOG_P25, P25_PDU_STR ", failed to commit SNDCP context binding, llId = %u, ipAddr = %s",
llId, __IP_FROM_UINT(ipAddress).c_str());
return false;
}
m_neighborCache.observe(llId, ipAddress);
m_dataLinkManager.setReady(llId, true);
// scope is intentional
{
std::lock_guard<std::mutex> guard(m_sndcpContextLock);
SNDCPContext& ctx = m_sndcpContexts[llId];
ctx.active = true;
ctx.nsapi = nsapi;
}
clearSNDCPPendingAck(llId);
return true;
}
/* Helper used to process conventional data registration from PDU data. */
bool P25PacketData::processConvDataReg(RxStatus* status)
@ -1558,7 +1611,13 @@ bool P25PacketData::processConvDataReg(RxStatus* status)
bool P25PacketData::processSNDCPControl(RxStatus* status)
{
std::unique_ptr<sndcp::SNDCPPacket> packet = SNDCPFactory::create(status->pduUserData);
if (status == nullptr || status->pduUserData == nullptr) {
LogWarning(LOG_P25, P25_PDU_STR ", truncated SNDCP control packet");
return false;
}
std::unique_ptr<sndcp::SNDCPPacket> packet = SNDCPFactory::create(
status->pduUserData, status->pduUserDataLength);
if (packet == nullptr) {
LogWarning(LOG_P25, P25_PDU_STR ", undecodable SNDCP packet");
return false;
@ -1643,7 +1702,7 @@ bool P25PacketData::processSNDCPControl(RxStatus* status)
return true;
}
// Accept with static IP
// accept with static IP
uint8_t txPduUserData[P25_MAX_PDU_BLOCKS * P25_PDU_UNCONFIRMED_LENGTH_BYTES];
::memset(txPduUserData, 0x00U, P25_MAX_PDU_BLOCKS * P25_PDU_UNCONFIRMED_LENGTH_BYTES);
@ -1668,18 +1727,9 @@ bool P25PacketData::processSNDCPControl(RxStatus* status)
rspHeader.setBlocksToFollow(1U);
rspHeader.calculateLength(13U);
m_neighborCache.observe(llId, staticIP);
m_dataLinkManager.setReady(llId, true);
{
std::lock_guard<std::mutex> guard(m_sndcpContextLock);
SNDCPContext& ctx = m_sndcpContexts[llId];
ctx.active = true;
ctx.nsapi = isp->getNSAPI();
}
// a downlink still awaiting its ACK belongs to the context this one replaces
clearSNDCPPendingAck(llId);
dispatchUserFrameToFNE(rspHeader, false, false, txPduUserData);
// dispatch the activation response before publishing the accepted context
if (!commitSNDCPActivation(rspHeader, txPduUserData, llId, isp->getNSAPI(), staticIP, DataBindingOrigin::SNDCP_STATIC))
return false;
LogInfoEx(LOG_P25, P25_PDU_STR ", SNDCP context activation accept, llId = %u, ipAddr = %s (static)",
llId, __IP_FROM_UINT(staticIP).c_str());
@ -1741,18 +1791,9 @@ bool P25PacketData::processSNDCPControl(RxStatus* status)
rspHeader.setBlocksToFollow(1U);
rspHeader.calculateLength(13U);
m_neighborCache.observe(llId, dynamicIP);
m_dataLinkManager.setReady(llId, true);
{
std::lock_guard<std::mutex> guard(m_sndcpContextLock);
SNDCPContext& ctx = m_sndcpContexts[llId];
ctx.active = true;
ctx.nsapi = isp->getNSAPI();
}
// a downlink still awaiting its ACK belongs to the context this one replaces
clearSNDCPPendingAck(llId);
dispatchUserFrameToFNE(rspHeader, false, false, txPduUserData);
// dispatch the activation response before publishing the accepted context
if (!commitSNDCPActivation(rspHeader, txPduUserData, llId, isp->getNSAPI(), dynamicIP, DataBindingOrigin::SNDCP_DYNAMIC))
return false;
LogInfoEx(LOG_P25, P25_PDU_STR ", SNDCP context activation accept, llId = %u, ipAddr = %s (dynamic)",
llId, __IP_FROM_UINT(dynamicIP).c_str());
@ -1796,13 +1837,20 @@ bool P25PacketData::processSNDCPControl(RxStatus* status)
isp->getDeactType());
m_neighborCache.erase(llId);
const IPBinding* binding = m_bindingRegistry.findByLLId(llId);
if (binding != nullptr && binding->convergenceMode == DataConvergenceMode::SNDCP)
m_bindingRegistry.erase(llId);
m_dataLinkManager.erase(llId);
// scope is intentional
{
std::lock_guard<std::mutex> guard(m_sndcpContextLock);
auto ctx = m_sndcpContexts.find(llId);
if (ctx != m_sndcpContexts.end())
ctx->second.active = false;
}
clearSNDCPPendingAck(llId);
// send ACK response
@ -2088,7 +2136,6 @@ uint32_t P25PacketData::allocateIPAddress(uint32_t llId)
nextIP = m_network->m_sndcpStartAddr;
}
m_neighborCache.observe(llId, allocatedIP);
LogInfoEx(LOG_P25, P25_PDU_STR ", SNDCP allocated dynamic IP %s to llId = %u (pool: %s - %s)",
__IP_FROM_UINT(allocatedIP).c_str(), llId, __IP_FROM_UINT(m_network->m_sndcpStartAddr).c_str(), __IP_FROM_UINT(m_network->m_sndcpEndAddr).c_str());

@ -371,6 +371,18 @@ namespace network
* @returns bool True, if the SNDCP header was stripped leaving an IPv4 datagram, otherwise false.
*/
bool processSNDCPUserData(RxStatus* status, uint32_t peerId);
/**
* @brief Dispatches an activation response and publishes the accepted SNDCP context.
* @param responseHeader SNDCP activation response header.
* @param responseData Encoded SNDCP activation response.
* @param llId Logical Link ID of the SU.
* @param nsapi Network Service Access Point Identifier.
* @param ipAddress Assigned IPv4 address.
* @param origin Origin of the assigned address.
* @returns bool True if the response was dispatched and the context was committed.
*/
bool commitSNDCPActivation(::p25::data::DataHeader& responseHeader, uint8_t* responseData, uint32_t llId,
uint8_t nsapi, uint32_t ipAddress, p25data::DataBindingOrigin origin);
/**
* @brief Helper write ARP request to the network.

@ -89,10 +89,11 @@ ConvergenceResult SCEPService::authorizeUplink(uint32_t llId, const IPv4Packet&
(isMulticast(packet.destinationAddress) && !m_policy.allowMulticast))
return ConvergenceResult::POLICY_REJECTED;
// check if the logical link ID corresponds to an authorized SCEP binding
// both conventional SCEP and trunked SNDCP carry the same validated IPv4
// datagram at this boundary; their bearer-specific framing is applied later
const IPBinding* binding = m_bindings.findByLLId(llId);
if (binding == nullptr || !binding->authorized ||
binding->convergenceMode != DataConvergenceMode::SCEP)
if (binding == nullptr || !binding->authorized ||
(binding->convergenceMode != DataConvergenceMode::SCEP && binding->convergenceMode != DataConvergenceMode::SNDCP))
return ConvergenceResult::NO_BINDING;
// check if the source IP address of the packet matches the authorized binding
@ -108,7 +109,7 @@ uint32_t SCEPService::routeDownlink(uint32_t destinationAddress) const
{
const IPBinding* binding = m_bindings.findByIPAddress(destinationAddress);
if (binding == nullptr || !binding->authorized ||
binding->convergenceMode != DataConvergenceMode::SCEP)
(binding->convergenceMode != DataConvergenceMode::SCEP && binding->convergenceMode != DataConvergenceMode::SNDCP))
return 0U;
return binding->link.llId;

@ -327,7 +327,7 @@ bool Data::process(uint8_t* data, uint32_t len)
m_rfAssembler->dataHeader.getBlocksToFollow());
}
processSNDCPControl(m_rfPduUserData);
processSNDCPControl(m_rfPduUserData, m_rfPduUserDataLength);
writeNet_PDU_User(m_rfAssembler->dataHeader, m_rfAssembler->getExtendedAddress(), m_rfAssembler->getAuxiliaryES(),
m_rfPduUserData);
}
@ -943,13 +943,13 @@ bool Data::processConvDataReg(const uint8_t* pduUserData, uint32_t length)
/* Helper used to process SNDCP control data from PDU data. */
bool Data::processSNDCPControl(const uint8_t* pduUserData)
bool Data::processSNDCPControl(const uint8_t* pduUserData, uint32_t length)
{
if (!m_p25->m_sndcpSupport) {
return false;
}
std::unique_ptr<sndcp::SNDCPPacket> packet = SNDCPFactory::create(pduUserData);
std::unique_ptr<sndcp::SNDCPPacket> packet = SNDCPFactory::create(pduUserData, length);
if (packet == nullptr) {
LogWarning(LOG_RF, P25_PDU_STR ", undecodable SNDCP packet");
return false;

@ -209,9 +209,10 @@ namespace p25
/**
* @brief Helper used to process SNDCP control data from PDU data.
* @param pduUserData Buffer containing user data to transmit.
* @param length Length of the SNDCP control data in bytes.
* @returns bool True, if SNDCP control data was processed, otherwise false.
*/
bool processSNDCPControl(const uint8_t* pduUserData);
bool processSNDCPControl(const uint8_t* pduUserData, uint32_t length);
/**
* @brief Write data processed from RF to the network.

Loading…
Cancel
Save

Powered by TurnKey Linux.