diff --git a/configs/config.example.yml b/configs/config.example.yml index d3532504..24ca9cfc 100644 --- a/configs/config.example.yml +++ b/configs/config.example.yml @@ -243,6 +243,8 @@ protocols: # Internal data queue size (in DMR frames). This is the queue used internally to buffer frames before they # are sent to the air interface modem. queueSize: 31 + # Flag indicating whether or not network packet admission checks are ignored. + debugIgnPktAdmission: false # Flag indicating whether or not verbose logging is enabled. verbose: true # Flag indicating whether or not debug logging is enabled. @@ -377,6 +379,8 @@ protocols: # Internal data queue size (in P25 LDU frames). This is the queue used internally to buffer frames before they # are sent to the air interface modem. queueSize: 12 + # Flag indicating whether or not network packet admission checks are ignored. + debugIgnPktAdmission: false # Flag indicating whether or not verbose logging is enabled. verbose: true # Flag indicating whether or not debug logging is enabled. @@ -431,6 +435,8 @@ protocols: # Internal data queue size (in NXDN frames). This is the queue used internally to buffer frames before they # are sent to the air interface modem. queueSize: 31 + # Flag indicating whether or not network packet admission checks are ignored. + debugIgnPktAdmission: false # Flag indicating whether or not verbose logging is enabled. verbose: true # Flag indicating whether or not debug logging is enabled. diff --git a/src/common/network/Network.cpp b/src/common/network/Network.cpp index 341f26b6..17ca3cac 100644 --- a/src/common/network/Network.cpp +++ b/src/common/network/Network.cpp @@ -74,6 +74,11 @@ Network::Network(const std::string& address, uint16_t port, uint16_t localPort, m_passKeysWithNoPresharedKey(false), m_peerConnectedCallback(nullptr), m_peerDisconnectedCallback(nullptr), + m_dmrRxPacketPreprocessor(nullptr), + m_p25RxPacketPreprocessor(nullptr), + m_p25P2RxPacketPreprocessor(nullptr), + m_nxdnRxPacketPreprocessor(nullptr), + m_analogRxPacketPreprocessor(nullptr), m_dmrInCallCallback(nullptr), m_p25InCallCallback(nullptr), m_p25P2InCallCallback(nullptr), diff --git a/src/common/network/Network.h b/src/common/network/Network.h index 3797d626..1e4fccaf 100644 --- a/src/common/network/Network.h +++ b/src/common/network/Network.h @@ -145,6 +145,58 @@ namespace network */ class DVM_COMMON_API Network : public BaseNetwork { public: + /** + * @brief Receive packet preprocessing result. + * @ingroup network_core + */ + enum class RX_PACKET_PREPROCESS_RESULT : uint8_t { + ACCEPT, //!< Continue normal receive stream processing. + DROP //!< Discard the packet without changing receive stream state. + }; + + /** + * @brief Protocol receive packet preprocessor callback. + * @param data Pointer to the packet data. + * @param length Length of the packet data. + * @param streamId Stream ID. + * @param rxStreamId Receive stream ID. + */ + using RxPacketPreprocessor = std::function; + /** + * @brief Callback for ICC commands that include a slot number. + * @param command ICC command. + * @param dstId Destination ID. + * @param slotNo Slot number. + * @param peerId Peer ID. + * @param ssrc RTP SSRC. + * @param streamId Stream ID. + */ + using ICCSlotCallback = std::function; + /** + * @brief Callback for ICC commands that do not include a slot number. + * @param command ICC command. + * @param dstId Destination ID. + * @param peerId Peer ID. + * @param ssrc RTP SSRC. + * @param streamId Stream ID. + */ + using ICCCallback = std::function; + /** + * @brief Callback for key responses. + * @param ki Key item. + * @param algId Algorithm ID. + * @param keyLength Key length. + */ + using KeyResponseCallback = std::function; + /** + * @brief Callback for KEK key responses. + * @param rsi RSI. + * @param ki Key item. + * @param algId Algorithm ID. + * @param keyLength Key length. + */ + using KEKKeyResponseCallback = std::function; + /** * @brief Initializes a new instance of the Network class. * @param address Network Hostname/IP address to connect to. @@ -322,42 +374,68 @@ namespace network */ void setPeerDisconnectedCallback(std::function&& callback) { m_peerDisconnectedCallback = callback; } + /** + * @brief Sets the optional DMR receive packet preprocessor. + * @param callback Callback invoked before DMR receive stream selection, or an empty callback to disable preprocessing. + */ + void setDMRRxPacketPreprocessor(RxPacketPreprocessor&& callback) { m_dmrRxPacketPreprocessor = callback; } + /** + * @brief Sets the optional P25 receive packet preprocessor. + * @param callback Callback invoked before P25 receive stream selection, or an empty callback to disable preprocessing. + */ + void setP25RxPacketPreprocessor(RxPacketPreprocessor&& callback) { m_p25RxPacketPreprocessor = callback; } + /** + * @brief Sets the optional P25 Phase 2 receive packet preprocessor. + * @param callback Callback invoked before P25 Phase 2 receive stream selection, or an empty callback to disable preprocessing. + */ + void setP25P2RxPacketPreprocessor(RxPacketPreprocessor&& callback) { m_p25P2RxPacketPreprocessor = callback; } + /** + * @brief Sets the optional NXDN receive packet preprocessor. + * @param callback Callback invoked before NXDN receive stream selection, or an empty callback to disable preprocessing. + */ + void setNXDNRxPacketPreprocessor(RxPacketPreprocessor&& callback) { m_nxdnRxPacketPreprocessor = callback; } + /** + * @brief Sets the optional analog receive packet preprocessor. + * @param callback Callback invoked before analog receive stream selection, or an empty callback to disable preprocessing. + */ + void setAnalogRxPacketPreprocessor(RxPacketPreprocessor&& callback) { m_analogRxPacketPreprocessor = callback; } + /** * @brief Helper to set the DMR In-Call Control callback. * @param callback */ - void setDMRICCCallback(std::function&& callback) { m_dmrInCallCallback = callback; } + void setDMRICCCallback(ICCSlotCallback&& callback) { m_dmrInCallCallback = callback; } /** * @brief Helper to set the P25 In-Call Control callback. * @param callback */ - void setP25ICCCallback(std::function&& callback) { m_p25InCallCallback = callback; } + void setP25ICCCallback(ICCCallback&& callback) { m_p25InCallCallback = callback; } /** * @brief Helper to set the P25 Phase 2 In-Call Control callback. * @param callback */ - void setP25P2ICCCallback(std::function&& callback) { m_p25P2InCallCallback = callback; } + void setP25P2ICCCallback(ICCSlotCallback&& callback) { m_p25P2InCallCallback = callback; } /** * @brief Helper to set the NXDN In-Call Control callback. * @param callback */ - void setNXDNICCCallback(std::function&& callback) { m_nxdnInCallCallback = callback; } + void setNXDNICCCallback(ICCCallback&& callback) { m_nxdnInCallCallback = callback; } /** * @brief Helper to set the analog In-Call Control callback. * @param callback */ - void setAnalogICCCallback(std::function&& callback) { m_analogInCallCallback = callback; } + void setAnalogICCCallback(ICCCallback&& callback) { m_analogInCallCallback = callback; } /** * @brief Helper to set the enc. key response callback. * @param callback */ - void setKeyResponseCallback(std::function&& callback) { m_keyRespCallback = callback; } + void setKeyResponseCallback(KeyResponseCallback&& callback) { m_keyRespCallback = callback; } /** * @brief Helper to set the UKEK/LLA encryption key response callback. * @param callback */ - void setKEKKeyResponseCallback(std::function&& callback) { m_kekKeyRespCallback = callback; } + void setKEKKeyResponseCallback(KEKKeyResponseCallback&& callback) { m_kekKeyRespCallback = callback; } public: /** @@ -365,7 +443,7 @@ namespace network */ DECLARE_RO_PROPERTY_PLAIN(uint16_t, pktLastSeq); - /** + /** * @brief Flag indicating that network unit-to-unit traffic is discarded. */ DECLARE_RO_PROPERTY_PLAIN(bool, disallowUnitToUnit); @@ -457,48 +535,81 @@ namespace network */ std::function m_peerDisconnectedCallback; + /** + * @brief Optional DMR receive packet preprocessor. + * (This is called before DMR receive stream selection.) + */ + RxPacketPreprocessor m_dmrRxPacketPreprocessor; + /** + * @brief Optional P25 receive packet preprocessor. + * (This is called before P25 receive stream selection.) + */ + RxPacketPreprocessor m_p25RxPacketPreprocessor; + /** + * @brief Optional P25 Phase 2 receive packet preprocessor. + * (This is called before P25 Phase 2 receive stream selection.) + */ + RxPacketPreprocessor m_p25P2RxPacketPreprocessor; + /** + * @brief Optional NXDN receive packet preprocessor. + * (This is called before NXDN receive stream selection.) + */ + RxPacketPreprocessor m_nxdnRxPacketPreprocessor; + /** + * @brief Optional analog receive packet preprocessor. + * (This is called before analog receive stream selection.) + */ + RxPacketPreprocessor m_analogRxPacketPreprocessor; + /** * @brief DMR In-Call Control Function Callback. * (This is called when the master sends a In-Call Control request.) */ - std::function m_dmrInCallCallback; + ICCSlotCallback m_dmrInCallCallback; /** * @brief P25 In-Call Control Function Callback. * (This is called once the master sends a In-Call Control request.) */ - std::function m_p25InCallCallback; + ICCCallback m_p25InCallCallback; /** * @brief P25 Phase 2 In-Call Control Function Callback. * (This is called once the master sends a In-Call Control request.) */ - std::function m_p25P2InCallCallback; + ICCSlotCallback m_p25P2InCallCallback; /** * @brief NXDN In-Call Control Function Callback. * (This is called once the master sends a In-Call Control request.) */ - std::function m_nxdnInCallCallback; + ICCCallback m_nxdnInCallCallback; /** * @brief Analog In-Call Control Function Callback. * (This is called once the master sends a In-Call Control request.) */ - std::function m_analogInCallCallback; + ICCCallback m_analogInCallCallback; /** * @brief Encryption Key Response Function Callback. * (This is called once the master responds to a key request.) */ - std::function m_keyRespCallback; + KeyResponseCallback m_keyRespCallback; /** * @brief LLA/UKEK Encryption Key Response Function Callback. * (This is called once the master responds to a key KEK request.) */ - std::function m_kekKeyRespCallback; + KEKKeyResponseCallback m_kekKeyRespCallback; + /** + * @brief Function pointer type for packet handlers. + * @param network Instance of the Network class. + * @param peerId Peer ID. + * @param streamId Stream ID. + * @param now Current time in milliseconds. + * @param fneHeader RTP FNE Header. + * @param rtpHeader RTP Header. + * @param buffer Buffer containing the packet data. + * @param length Length of buffer. + * @returns bool True, if the packet was handled, otherwise false. + */ using PacketHandlerFunc = bool (*)(Network* network, uint32_t peerId, uint32_t streamId, uint64_t now, const frame::RTPFNEHeader& fneHeader, const frame::RTPHeader& rtpHeader, const uint8_t* buffer, int length); diff --git a/src/common/network/packet/Protocol.cpp b/src/common/network/packet/Protocol.cpp index 5ca2cb78..4d222ba3 100644 --- a/src/common/network/packet/Protocol.cpp +++ b/src/common/network/packet/Protocol.cpp @@ -50,6 +50,11 @@ bool Network::PacketHandler::protocol(Network* network, uint32_t peerId, uint32_ slotNo + 1U, peerId, length, rtpHeader.getSequence(), streamId); } + // allow the DMR preprocessor to discard packets before stream state is updated + if (network->m_dmrRxPacketPreprocessor && network->m_dmrRxPacketPreprocessor(buffer, length, streamId, network->m_rxDMRStreamId[slotNo]) == RX_PACKET_PREPROCESS_RESULT::DROP) { + break; + } + if (network->m_promiscuousPeer) { network->m_rxDMRStreamId[slotNo] = streamId; network->m_pktLastSeq = network->m_pktSeq; @@ -132,6 +137,11 @@ bool Network::PacketHandler::protocol(Network* network, uint32_t peerId, uint32_ peerId, length, rtpHeader.getSequence(), streamId); } + // allow the P25 preprocessor to discard packets before stream state is updated + if (network->m_p25RxPacketPreprocessor && network->m_p25RxPacketPreprocessor(buffer, length, streamId, network->m_rxP25StreamId) == RX_PACKET_PREPROCESS_RESULT::DROP) { + break; + } + if (network->m_promiscuousPeer) { network->m_rxP25StreamId = streamId; network->m_pktLastSeq = network->m_pktSeq; @@ -229,6 +239,11 @@ bool Network::PacketHandler::protocol(Network* network, uint32_t peerId, uint32_ slotNo + 1U, peerId, length, rtpHeader.getSequence(), streamId); } + // allow the P25 Phase 2 preprocessor to discard packets before stream state is updated + if (network->m_p25P2RxPacketPreprocessor && network->m_p25P2RxPacketPreprocessor(buffer, length, streamId, network->m_rxP25P2StreamId[slotNo]) == RX_PACKET_PREPROCESS_RESULT::DROP) { + break; + } + if (network->m_promiscuousPeer) { network->m_rxP25P2StreamId[slotNo] = streamId; network->m_pktLastSeq = network->m_pktSeq; @@ -320,6 +335,11 @@ bool Network::PacketHandler::protocol(Network* network, uint32_t peerId, uint32_ peerId, length, rtpHeader.getSequence(), streamId); } + // allow the NXDN preprocessor to discard packets before stream state is updated + if (network->m_nxdnRxPacketPreprocessor && network->m_nxdnRxPacketPreprocessor(buffer, length, streamId, network->m_rxNXDNStreamId) == RX_PACKET_PREPROCESS_RESULT::DROP) { + break; + } + if (network->m_promiscuousPeer) { network->m_rxNXDNStreamId = streamId; network->m_pktLastSeq = network->m_pktSeq; @@ -408,6 +428,11 @@ bool Network::PacketHandler::protocol(Network* network, uint32_t peerId, uint32_ peerId, length, rtpHeader.getSequence(), streamId); } + // allow the analog preprocessor to discard packets before stream state is updated + if (network->m_analogRxPacketPreprocessor && network->m_analogRxPacketPreprocessor(buffer, length, streamId, network->m_rxAnalogStreamId) == RX_PACKET_PREPROCESS_RESULT::DROP) { + break; + } + if (network->m_promiscuousPeer) { network->m_rxAnalogStreamId = streamId; network->m_pktLastSeq = network->m_pktSeq; diff --git a/src/fne/network/PeerNetwork.h b/src/fne/network/PeerNetwork.h index 05fd7946..7ed453a5 100644 --- a/src/fne/network/PeerNetwork.h +++ b/src/fne/network/PeerNetwork.h @@ -64,6 +64,17 @@ namespace network */ class HOST_SW_API PeerNetwork : public Network { public: + /** + * @brief Callback for protocol packet processing. + * @param peer Pointer to the PeerNetwork instance. + * @param data Pointer to the packet data. + * @param length Length of the packet data. + * @param peerId Peer ID associated with the packet. + * @param fneHeader RTP FNE header. + * @param rtpHeader RTP header. + */ + using ProtocolCallback = std::function; + /** * @brief Initializes a new instance of the PeerNetwork class. * @param address Network Hostname/IP address to connect to. @@ -121,27 +132,27 @@ namespace network * @brief Helper to set the DMR protocol callback. * @param callback */ - void setDMRCallback(std::function&& callback) { m_dmrCallback = callback; } + void setDMRCallback(ProtocolCallback&& callback) { m_dmrCallback = callback; } /** * @brief Helper to set the P25 protocol callback. * @param callback */ - void setP25Callback(std::function&& callback) { m_p25Callback = callback; } + void setP25Callback(ProtocolCallback&& callback) { m_p25Callback = callback; } /** * @brief Helper to set the P25 Phase 2 protocol callback. * @param callback */ - void setP25P2Callback(std::function&& callback) { m_p25P2Callback = callback; } + void setP25P2Callback(ProtocolCallback&& callback) { m_p25P2Callback = callback; } /** * @brief Helper to set the NXDN protocol callback. * @param callback */ - void setNXDNCallback(std::function&& callback) { m_nxdnCallback = callback; } + void setNXDNCallback(ProtocolCallback&& callback) { m_nxdnCallback = callback; } /** * @brief Helper to set the analog protocol callback. * @param callback */ - void setAnalogCallback(std::function&& callback) { m_analogCallback = callback; } + void setAnalogCallback(ProtocolCallback&& callback) { m_analogCallback = callback; } /** * @brief Helper to set the network tree disconnect callback. diff --git a/src/host/dmr/Control.cpp b/src/host/dmr/Control.cpp index b2797076..5d115150 100644 --- a/src/host/dmr/Control.cpp +++ b/src/host/dmr/Control.cpp @@ -77,6 +77,7 @@ Control::Control(bool authoritative, uint32_t colorCode, uint32_t callHang, uint m_ccRunning(false), m_ccHalted(false), m_dumpCSBKData(dumpCSBKData), + m_debugIgnPktAdmission(false), m_verbose(verbose), m_debug(debug) { @@ -88,6 +89,111 @@ Control::Control(bool authoritative, uint32_t colorCode, uint32_t callHang, uint assert(rssiMapper != nullptr); acl::AccessControl::init(m_ridLookup, m_tidLookup); + if (network != nullptr) { + network->setDMRRxPacketPreprocessor([this](const uint8_t* data, uint32_t length, uint32_t streamId, uint32_t rxStreamId) { + if (m_debugIgnPktAdmission) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + if (data == nullptr || length <= 15U) + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + + // allow packets matching the currently locked receive stream to bypass destination filtering + if (rxStreamId != 0U && streamId == rxStreamId) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + DataType::E dataType = DataType::VOICE; + if ((data[15U] & 0x20U) == 0x20U) + dataType = (DataType::E)(data[15U] & 0x0FU); + else if ((data[15U] & 0x10U) == 0x10U) + dataType = DataType::VOICE_SYNC; + + // only apply destination filtering to voice calls + if (dataType == DataType::TERMINATOR_WITH_LC || + (dataType != DataType::VOICE_LC_HEADER && dataType != DataType::VOICE_PI_HEADER && + dataType != DataType::VOICE_SYNC && dataType != DataType::VOICE)) { + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + } + + const uint32_t srcId = GET_UINT24(data, 5U); + const uint32_t dstId = GET_UINT24(data, 8U); + + // enforce source ID access control + if (!acl::AccessControl::validateSrcId(srcId)) { + if (m_debug) + LogDebug(LOG_NET, "DMR packet denial, RID rejection, srcId = %u", srcId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + const uint32_t slotNo = (data[15U] & 0x80U) == 0x80U ? 2U : 1U; + + //LogDebug(LOG_NET, "DMR packet, slot = %u, srcId = %u, dstId = %u, streamId = %u, rxStreamId = %u", slotNo, srcId, dstId, streamId, rxStreamId); + + // enforce the permitted destination for non-authoritative hosts + if (!m_authoritative) { + switch (slotNo) { + case 1U: + // allow packets on a dedicated control channel to bypass destination filtering + if (m_slot1->m_dedicatedTSCC) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + // reject packets when no destination is permitted for non-authoritative hosts + if (m_slot1->m_permittedDstId == 0U) { + if (m_debug) + LogDebug(LOG_NET, "[NON-AUTHORITATIVE] DMR Slot 1 packet denial, no permitted destination"); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + // enforce the permitted destination for non-authoritative hosts + if (dstId != m_slot1->m_permittedDstId) { + if (m_debug) + LogDebug(LOG_NET, "[NON-AUTHORITATIVE] DMR Slot 1 packet denial, destination rejection, dstId = %u, permittedDstId = %u", + dstId, m_slot1->m_permittedDstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + break; + case 2U: + // allow packets on a dedicated control channel to bypass destination filtering + if (m_slot2->m_dedicatedTSCC) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + // reject packets when no destination is permitted for non-authoritative hosts + if (m_slot2->m_permittedDstId == 0U) { + if (m_debug) + LogDebug(LOG_NET, "[NON-AUTHORITATIVE] DMR Slot 2 packet denial, no permitted destination"); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + // enforce the permitted destination for non-authoritative hosts + if (dstId != m_slot2->m_permittedDstId) { + if (m_debug) + LogDebug(LOG_NET, "[NON-AUTHORITATIVE] DMR Slot 2 packet denial, destination rejection, dstId = %u, permittedDstId = %u", + dstId, m_slot2->m_permittedDstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + break; + } + } + + // enforce talkgroup or source ID access control + const bool group = (data[15U] & 0x40U) != 0x40U; + if (group) { + if (!acl::AccessControl::validateTGId(slotNo, dstId) || !acl::AccessControl::validateTGAccess(slotNo, srcId, dstId)) { + if (m_debug) + LogDebug(LOG_NET, "DMR Slot %u packet denial, TGID/RID admission rejection, srcId = %u, dstId = %u", + slotNo, srcId, dstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + } + else if (!acl::AccessControl::validateSrcId(dstId)) { + if (m_debug) + LogDebug(LOG_NET, "DMR Slot %u packet denial, RID rejection, dstId = %u", slotNo, dstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + }); + } + Slot::init(this, authoritative, colorCode, SiteData(), embeddedLCOnly, dumpTAData, callHang, modem, network, duplex, chLookup, m_ridLookup, m_tidLookup, m_idenTable, rssiMapper, jitter, verbose); lc::CSBK::setVerbose(m_dumpCSBKData); @@ -107,6 +213,9 @@ Control::Control(bool authoritative, uint32_t colorCode, uint32_t callHang, uint Control::~Control() { + if (Slot::s_network != nullptr) + Slot::s_network->setDMRRxPacketPreprocessor({}); + delete m_slot2; delete m_slot1; } @@ -241,6 +350,9 @@ void Control::setOptions(yaml::Node& conf, ::lookups::VoiceChData controlChData, m_slot1->m_convNetGrantDemand = convNetGrantDemand; m_slot2->m_convNetGrantDemand = convNetGrantDemand; + m_debugIgnPktAdmission = dmrProtocol["debugIgnPktAdmission"].as(false); + if (m_debugIgnPktAdmission) + LogWarning(LOG_DMR, "Ignoring network packet admission checks. This may cause odd behavior, proceed with caution."); if (printOptions) { if (enableTSCC) { @@ -273,6 +385,10 @@ void Control::setOptions(yaml::Node& conf, ::lookups::VoiceChData controlChData, LogInfo(" Verify Registration: %s", Slot::s_verifyReg ? "yes" : "no"); LogInfo(" Conventional Network Grant Demand: %s", convNetGrantDemand ? "yes" : "no"); + + if (m_debugIgnPktAdmission) { + LogInfo(" Ignore Network Packet Admission: yes"); + } } } diff --git a/src/host/dmr/Control.h b/src/host/dmr/Control.h index 0bf02d64..332ecf23 100644 --- a/src/host/dmr/Control.h +++ b/src/host/dmr/Control.h @@ -333,6 +333,7 @@ namespace dmr bool m_ccHalted; bool m_dumpCSBKData; + bool m_debugIgnPktAdmission; bool m_verbose; bool m_debug; diff --git a/src/host/dmr/Slot.cpp b/src/host/dmr/Slot.cpp index efc4dd1a..fd6ebf06 100644 --- a/src/host/dmr/Slot.cpp +++ b/src/host/dmr/Slot.cpp @@ -800,6 +800,7 @@ void Slot::clock() if (m_verbose) { LogInfoEx(LOG_NET, "Slot %u, talkgroup hang has expired, lastDstId = %u", m_slotNo, m_netLastDstId); } + m_netLastDstId = 0U; m_netLastSrcId = 0U; } diff --git a/src/host/nxdn/Control.cpp b/src/host/nxdn/Control.cpp index 6d5ab51e..a95e9913 100644 --- a/src/host/nxdn/Control.cpp +++ b/src/host/nxdn/Control.cpp @@ -118,6 +118,7 @@ Control::Control(bool authoritative, uint32_t ran, uint32_t callHang, uint32_t q m_netTimeout(false), m_dumpRCCH(dumpRCCHData), m_notifyCC(true), + m_debugIgnPktAdmission(false), m_ccDebug(debug), m_verbose(verbose), m_debug(debug) @@ -133,6 +134,71 @@ Control::Control(bool authoritative, uint32_t ran, uint32_t callHang, uint32_t q m_interval.start(); acl::AccessControl::init(m_ridLookup, m_tidLookup); + if (m_network != nullptr) { + m_network->setNXDNRxPacketPreprocessor([this](const uint8_t* data, uint32_t length, uint32_t streamId, uint32_t rxStreamId) { + if (m_debugIgnPktAdmission) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + if (data == nullptr || length <= 15U) + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + + // allow packets on a dedicated control channel to bypass destination filtering + if (m_dedicatedControl) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + // allow packets matching the currently locked receive stream to bypass destination filtering + if (rxStreamId != 0U && streamId == rxStreamId) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + // only apply destination filtering to voice calls + if (data[4U] != MessageType::RTCH_VCALL) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + const uint32_t srcId = GET_UINT24(data, 5U); + const uint32_t dstId = GET_UINT24(data, 8U); + + //LogDebug(LOG_NET, "NXDN packet, srcId = %u, dstId = %u, streamId = %u, rxStreamId = %u", srcId, dstId, streamId, rxStreamId); + + // reject packets when no destination is permitted for non-authoritative hosts + if (!m_authoritative && m_permittedDstId == 0U) { + if (m_debug) + LogDebug(LOG_NET, "[NON-AUTHORITATIVE] NXDN packet denial, no permitted destination"); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + // enforce the permitted destination for non-authoritative hosts + if (!m_authoritative && m_permittedDstId != 0U && dstId != m_permittedDstId) { + if (m_debug) + LogDebug(LOG_NET, "[NON-AUTHORITATIVE] NXDN packet denial, destination rejection, dstId = %u, permittedDstId = %u", + dstId, m_permittedDstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + // enforce source ID access control + if (!acl::AccessControl::validateSrcId(srcId)) { + if (m_debug) + LogDebug(LOG_NET, "NXDN packet denial, RID rejection, srcId = %u", srcId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + // enforce talkgroup or source ID access control + const bool group = (data[15U] & 0x40U) != 0x40U; + if (group) { + if (!acl::AccessControl::validateTGId(dstId)) { + if (m_debug) + LogDebug(LOG_NET, "NXDN packet denial, TGID rejection, dstId = %u", dstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + } + else if (!acl::AccessControl::validateSrcId(dstId)) { + if (m_debug) + LogDebug(LOG_NET, "NXDN packet denial, RID rejection, dstId = %u", dstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + }); + } m_voice = new Voice(this, debug, verbose); m_control = new ControlSignaling(this, debug, verbose); @@ -151,6 +217,9 @@ Control::Control(bool authoritative, uint32_t ran, uint32_t callHang, uint32_t q Control::~Control() { + if (m_network != nullptr) + m_network->setNXDNRxPacketPreprocessor({}); + if (m_affiliations != nullptr) { delete m_affiliations; } @@ -325,6 +394,10 @@ void Control::setOptions(yaml::Node& conf, const std::string cwCallsign, lookups uint32_t peerId, uint32_t ssrc, uint32_t streamId) { processInCallCtrl(command, dstId); }); } + m_debugIgnPktAdmission = nxdnProtocol["debugIgnPktAdmission"].as(false); + if (m_debugIgnPktAdmission) + LogWarning(LOG_NXDN, "Ignoring network packet admission checks. This may cause odd behavior, proceed with caution."); + if (printOptions) { LogInfo(" Site Location ID: $%04X", m_siteData.locId()); LogInfo(" Silence Threshold: %u (%.1f%%)", m_voice->m_silenceThreshold, float(m_voice->m_silenceThreshold) / 12.33F); @@ -358,6 +431,10 @@ void Control::setOptions(yaml::Node& conf, const std::string cwCallsign, lookups if (m_ccDebug) { LogInfo(" Control Message Debug: yes"); } + + if (m_debugIgnPktAdmission) { + LogInfo(" Ignore Network Packet Admission: yes"); + } } if (m_voice != nullptr) { @@ -732,6 +809,27 @@ void Control::clock() if (m_verbose) { LogInfoEx(LOG_NET, "talkgroup hang has expired, lastDstId = %u", m_netLastDstId); } + + // if the group is still granted at this point -- forcibly release it + if (m_affiliations->isGranted(m_netLastDstId)) { + if (!m_dedicatedControl) { + m_affiliations->releaseGrant(m_netLastDstId, false); + if (m_network != nullptr) + m_network->resetNXDN(); + } + } + + if (m_network != nullptr) + m_network->resetNXDN(); + + m_netState = RS_NET_IDLE; + + m_voice->resetNet(); + + m_netTimeoutTimer.stop(); + m_netTimeout = false; + m_networkWatchdog.stop(); + m_netLastDstId = 0U; m_netLastSrcId = 0U; } diff --git a/src/host/nxdn/Control.h b/src/host/nxdn/Control.h index ecd1a913..776590aa 100644 --- a/src/host/nxdn/Control.h +++ b/src/host/nxdn/Control.h @@ -359,6 +359,7 @@ namespace nxdn bool m_notifyCC; + bool m_debugIgnPktAdmission; bool m_ccDebug; bool m_verbose; bool m_debug; diff --git a/src/host/p25/Control.cpp b/src/host/p25/Control.cpp index cb7c261b..7185c870 100644 --- a/src/host/p25/Control.cpp +++ b/src/host/p25/Control.cpp @@ -155,6 +155,7 @@ Control::Control(bool authoritative, uint32_t nac, uint32_t callHang, uint32_t q m_ccNotifyActiveTG(false), m_disableAdjSiteBroadcast(false), m_notifyCC(true), + m_debugIgnPktAdmission(false), m_ccDebug(debug), m_verbose(verbose), m_debug(debug) @@ -175,6 +176,71 @@ Control::Control(bool authoritative, uint32_t nac, uint32_t callHang, uint32_t q m_interval.start(); acl::AccessControl::init(m_ridLookup, m_tidLookup); + if (m_network != nullptr) { + m_network->setP25RxPacketPreprocessor([this](const uint8_t* data, uint32_t length, uint32_t streamId, uint32_t rxStreamId) { + if (m_debugIgnPktAdmission) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + if (data == nullptr || length <= 22U) + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + + // allow packets on a dedicated control channel to bypass destination filtering + if (m_dedicatedControl) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + // allow packets matching the currently locked receive stream to bypass destination filtering + if (rxStreamId != 0U && streamId == rxStreamId) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + const DUID::E duid = (DUID::E)data[22U]; + if (duid == DUID::TDU || duid == DUID::TDULC) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + const uint32_t srcId = GET_UINT24(data, 5U); + const uint32_t dstId = GET_UINT24(data, 8U); + + //LogDebug(LOG_NET, "P25 packet, srcId = %u, dstId = %u, streamId = %u, rxStreamId = %u", srcId, dstId, streamId, rxStreamId); + + // reject packets when no destination is permitted for non-authoritative hosts + if (!m_authoritative && m_permittedDstId == 0U) { + if (m_debug) + LogDebug(LOG_NET, "[NON-AUTHORITATIVE] P25 packet denial, no permitted destination"); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + // enforce the permitted destination for non-authoritative hosts + if (!m_authoritative && m_permittedDstId != 0U && dstId != m_permittedDstId) { + if (m_debug) + LogDebug(LOG_NET, "[NON-AUTHORITATIVE] P25 packet denial, destination rejection, dstId = %u, permittedDstId = %u", + dstId, m_permittedDstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + // enforce source ID access control + if (!acl::AccessControl::validateSrcId(srcId)) { + if (m_debug) + LogDebug(LOG_NET, "P25 packet denial, RID rejection, srcId = %u", srcId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + // enforce talkgroup or source ID access control + if (data[4U] == LCO::GROUP) { + if (!acl::AccessControl::validateTGId(dstId) || !acl::AccessControl::validateTGAccess(srcId, dstId)) { + if (m_debug) + LogDebug(LOG_NET, "P25 packet denial, TGID/RID admission rejection, srcId = %u, dstId = %u", + srcId, dstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + } + else if (!acl::AccessControl::validateSrcId(dstId)) { + if (m_debug) + LogDebug(LOG_NET, "P25 packet denial, RID rejection, dstId = %u", dstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + }); + } m_hangCount = callHang * 4U; @@ -198,6 +264,9 @@ Control::Control(bool authoritative, uint32_t nac, uint32_t callHang, uint32_t q Control::~Control() { + if (m_network != nullptr) + m_network->setP25RxPacketPreprocessor({}); + if (m_affiliations != nullptr) { delete m_affiliations; } @@ -497,6 +566,10 @@ void Control::setOptions(yaml::Node& conf, const std::string cwCallsign, const : LogWarning(LOG_P25, "We are configured as a dedicated trunked voice channel but, have control data enabled. Don't do this, control data for a dedicated trunked voice channel should be disabled. This can cause unintended behavior."); } + m_debugIgnPktAdmission = p25Protocol["debugIgnPktAdmission"].as(false); + if (m_debugIgnPktAdmission) + LogWarning(LOG_P25, "Ignoring network packet admission checks. This may cause odd behavior, proceed with caution."); + if (printOptions) { LogInfo(" Silence Threshold: %u (%.1f%%)", m_voice->m_silenceThreshold, float(m_voice->m_silenceThreshold) / 12.33F); LogInfo(" Frame Loss Threshold: %u", m_frameLossThreshold); @@ -591,6 +664,10 @@ void Control::setOptions(yaml::Node& conf, const std::string cwCallsign, const : if (m_ccDebug) { LogInfo(" Control Message Debug: yes"); } + + if (m_debugIgnPktAdmission) { + LogInfo(" Ignore Network Packet Admission: yes"); + } } // are we overriding the NAC for split NAC operations? diff --git a/src/host/p25/Control.h b/src/host/p25/Control.h index d6d2b12c..e2add8af 100644 --- a/src/host/p25/Control.h +++ b/src/host/p25/Control.h @@ -442,6 +442,7 @@ namespace p25 bool m_notifyCC; + bool m_debugIgnPktAdmission; bool m_ccDebug; bool m_verbose; bool m_debug; diff --git a/src/host/p25/phase2/Control.cpp b/src/host/p25/phase2/Control.cpp index 3f7692dc..2320e454 100644 --- a/src/host/p25/phase2/Control.cpp +++ b/src/host/p25/phase2/Control.cpp @@ -9,6 +9,7 @@ */ #include "Defines.h" #include "common/Log.h" +#include "common/p25/acl/AccessControl.h" #include "p25/phase2/Control.h" #include @@ -38,6 +39,48 @@ Control::Control(bool authoritative, uint32_t callHang, uint32_t timeout, uint32 m_debug(debug), m_verbose(verbose) { + if (m_network != nullptr) { + m_network->setP25P2RxPacketPreprocessor([this](const uint8_t* data, uint32_t length, uint32_t streamId, uint32_t rxStreamId) { + if (data == nullptr || length <= 19U) + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + + // allow packets matching the currently locked receive stream to bypass destination filtering + if (rxStreamId != 0U && streamId == rxStreamId) + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + + const uint32_t srcId = GET_UINT24(data, 5U); + const uint32_t dstId = GET_UINT24(data, 8U); + + //LogDebug(LOG_NET, "P25 Phase 2 packet, srcId = %u, dstId = %u, streamId = %u, rxStreamId = %u", srcId, dstId, streamId, rxStreamId); + + // enforce source ID access control + if (!acl::AccessControl::validateSrcId(srcId)) { + if (m_debug) + LogDebug(LOG_NET, "P25 Phase 2 packet denial, RID rejection, srcId = %u", srcId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + // enforce talkgroup or source ID access control + const bool group = (data[14U] & network::NET_CTRL_U2U) != network::NET_CTRL_U2U; + if (group) { + if (!acl::AccessControl::validateTGId(dstId) || + !acl::AccessControl::validateTGAccess(srcId, dstId)) { + if (m_debug) + LogDebug(LOG_NET, "P25 Phase 2 packet denial, TGID/RID admission rejection, srcId = %u, dstId = %u", + srcId, dstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + } + else if (!acl::AccessControl::validateSrcId(dstId)) { + if (m_debug) + LogDebug(LOG_NET, "P25 Phase 2 packet denial, RID rejection, dstId = %u", dstId); + return network::Network::RX_PACKET_PREPROCESS_RESULT::DROP; + } + + return network::Network::RX_PACKET_PREPROCESS_RESULT::ACCEPT; + }); + } + Slot::init(this, authoritative, callHang, modem, network, affiliations, ridLookup, tidLookup, colorCode); m_slot1 = new Slot(0U, timeout, tgHang, queueSize, debug, verbose); m_slot2 = new Slot(1U, timeout, tgHang, queueSize, debug, verbose); @@ -47,6 +90,9 @@ Control::Control(bool authoritative, uint32_t callHang, uint32_t timeout, uint32 Control::~Control() { + if (m_network != nullptr) + m_network->setP25P2RxPacketPreprocessor({}); + delete m_slot2; delete m_slot1; } diff --git a/src/host/p25/phase2/Control.h b/src/host/p25/phase2/Control.h index ee46665d..8a83e13a 100644 --- a/src/host/p25/phase2/Control.h +++ b/src/host/p25/phase2/Control.h @@ -183,25 +183,20 @@ namespace p25 friend class ::HostTestHooks; #endif - /* Interfaces */ modem::Modem* m_modem; network::Network* m_network; - /* TDMA Slots */ Slot* m_slot1; Slot* m_slot2; - /* Access and Grant Lookups */ ::lookups::RadioIdLookup* m_ridLookup; ::lookups::TalkgroupRulesLookup* m_tidLookup; lookups::P25AffiliationLookup* m_affiliations; - /* Call Timing */ uint32_t m_callHang; uint32_t m_timeout; uint32_t m_tgHang; - /* Logging */ bool m_debug; bool m_verbose; diff --git a/src/patch/HostPatch.cpp b/src/patch/HostPatch.cpp index 4c23cae5..55a78c49 100644 --- a/src/patch/HostPatch.cpp +++ b/src/patch/HostPatch.cpp @@ -1165,17 +1165,17 @@ void HostPatch::processP25Network(uint8_t* buffer, uint32_t length) lsd.setLSD2(lsd2); if ((duid == DUID::TDU) || (duid == DUID::TDULC)) { - // ensure destination ID matches - if (dstId != m_srcTGId && dstId != m_dstTGId) { - // ignore TDU's that are grant demands - if (grantDemand) { - m_network->resetP25(); - return; - } - - resetP25Call(srcId); + // Grant demands use a TDU envelope but do not terminate the active call. + if (grantDemand) { + m_network->resetP25(); return; } + + // A terminator may carry either configured TGID (or no IDs for a bare + // TDU). Handle it before group-call rewriting so it cannot restart the + // call-drop timer and bypass call termination. + resetP25Call(srcId); + return; } if (control.getLCO() == LCO::GROUP) { diff --git a/tests/dmr/HostControl_Tests.cpp b/tests/dmr/HostControl_Tests.cpp index 86cc17d3..ab366872 100644 --- a/tests/dmr/HostControl_Tests.cpp +++ b/tests/dmr/HostControl_Tests.cpp @@ -792,6 +792,96 @@ TEST_CASE("DMR host e2e loopback enforces stream lock until active stream termin REQUIRE(harness.network()->rxDMRStreamId(1U) == streamB); } +TEST_CASE("DMR host drops denied streams before receive stream selection", "[dmr][host][control][net][e2e][acl]") +{ + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); + + const uint32_t hostPeerId = 7013U; + const uint32_t deniedStream = 0x610301U; + const uint32_t allowedStream = 0x610302U; + const uint32_t srcId = 5199083U; + const uint32_t deniedDstId = 8384U; + const uint32_t allowedDstId = 212U; + + DMRHostHarness harness(true, true, hostPort, hostPeerId, false, true); + DMRTestNetwork sender(senderPort, 7014U); + harness.m_tidLookup.addEntry(admissionRule(allowedDstId, 1U, { srcId })); + + REQUIRE(harness.network() != nullptr); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + + REQUIRE(sender.sendDMRVoiceHeader(hostPeerId, deniedStream, 800U, 1U, srcId, deniedDstId)); + REQUIRE(sender.sendDMRVoiceHeader(hostPeerId, allowedStream, 801U, 1U, srcId, allowedDstId)); + + for (uint32_t i = 0U; i < 50U; i++) { + harness.network()->clock(1U); + harness.m_control->clock(); + if (HostTestHooks::dmrNetState(*HostTestHooks::dmrSlot1(*harness.m_control)) == RS_NET_AUDIO) + break; + + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + + REQUIRE(HostTestHooks::dmrNetState(*HostTestHooks::dmrSlot1(*harness.m_control)) == RS_NET_AUDIO); + REQUIRE(harness.m_control->getLastSrcId(1U) == srcId); + REQUIRE(harness.m_control->getLastDstId(1U) == allowedDstId); + REQUIRE(harness.network()->rxDMRStreamId(1U) == allowedStream); + REQUIRE(harness.network()->resetDMRCount() == 0U); +} + +TEST_CASE("DMR non-authoritative packet admission requires the matching slot permit", "[dmr][host][control][net][e2e][acl][non-authoritative]") +{ + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); + + const uint32_t hostPeerId = 7015U; + const uint32_t unpermittedStream = 0x610401U; + const uint32_t mismatchedStream = 0x610402U; + const uint32_t permittedStream = 0x610403U; + const uint32_t srcId = 5199083U; + const uint32_t dstId = 212U; + + DMRHostHarness harness(false, true, hostPort, hostPeerId, false, true); + DMRTestNetwork sender(senderPort, 7016U); + harness.m_tidLookup.addEntry(admissionRule(dstId, 1U, { srcId })); + + REQUIRE(harness.network() != nullptr); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + + REQUIRE(sender.sendDMRVoiceHeader(hostPeerId, unpermittedStream, 900U, 1U, srcId, dstId)); + for (uint32_t i = 0U; i < 10U; i++) + harness.network()->clock(1U); + REQUIRE(harness.network()->rxDMRStreamId(1U) == 0U); + + harness.m_control->permittedTG(313U, 1U); + REQUIRE(sender.sendDMRVoiceHeader(hostPeerId, mismatchedStream, 901U, 1U, srcId, dstId)); + for (uint32_t i = 0U; i < 10U; i++) + harness.network()->clock(1U); + REQUIRE(harness.network()->rxDMRStreamId(1U) == 0U); + + harness.m_control->permittedTG(dstId, 1U); + REQUIRE(sender.sendDMRVoiceHeader(hostPeerId, permittedStream, 902U, 1U, srcId, dstId)); + for (uint32_t i = 0U; i < 50U; i++) { + harness.network()->clock(1U); + harness.m_control->clock(); + if (HostTestHooks::dmrNetState(*HostTestHooks::dmrSlot1(*harness.m_control)) == RS_NET_AUDIO) + break; + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + + REQUIRE(HostTestHooks::dmrNetState(*HostTestHooks::dmrSlot1(*harness.m_control)) == RS_NET_AUDIO); + REQUIRE(harness.network()->rxDMRStreamId(1U) == permittedStream); +} + TEST_CASE("DMR slot network voice start arms watchdog on the targeted slot", "[dmr][host][control]") { DMRHostHarness harness; diff --git a/tests/nxdn/HostControl_Tests.cpp b/tests/nxdn/HostControl_Tests.cpp index 2714b3be..3ef89737 100644 --- a/tests/nxdn/HostControl_Tests.cpp +++ b/tests/nxdn/HostControl_Tests.cpp @@ -891,6 +891,98 @@ TEST_CASE("NXDN host e2e loopback enforces stream lock until active stream termi REQUIRE(harness.network()->rxNXDNStreamId() == streamB); } +TEST_CASE("NXDN host drops denied streams before receive stream selection", "[nxdn][host][control][net][e2e][acl]") +{ + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); + + const uint32_t hostPeerId = 8013U; + const uint32_t deniedStream = 0x620301U; + const uint32_t allowedStream = 0x620302U; + const uint16_t srcId = 5199U; + const uint16_t deniedDstId = 8384U; + const uint16_t allowedDstId = 212U; + + NXDNHostHarness harness(true, true, hostPort, hostPeerId, false, true); + NXDNTestNetwork sender(senderPort, 8014U); + harness.m_tidLookup.addEntry(admissionRule(allowedDstId, { srcId })); + + REQUIRE(harness.network() != nullptr); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + + REQUIRE(sender.sendNXDNCallHeader(hostPeerId, deniedStream, 800U, srcId, deniedDstId)); + REQUIRE(sender.sendNXDNCallHeader(hostPeerId, allowedStream, 801U, srcId, allowedDstId)); + + for (uint32_t i = 0U; i < 50U; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + if (HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO) + break; + + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + + REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO); + REQUIRE(HostTestHooks::nxdnNetLastSrcId(*harness.m_control) == srcId); + REQUIRE(HostTestHooks::nxdnNetLastDstId(*harness.m_control) == allowedDstId); + REQUIRE(harness.network()->rxNXDNStreamId() == allowedStream); + REQUIRE(harness.network()->resetNXDNCount() == 0U); +} + +TEST_CASE("NXDN non-authoritative packet admission requires a matching permit", "[nxdn][host][control][net][e2e][acl][non-authoritative]") +{ + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); + + const uint32_t hostPeerId = 8015U; + const uint32_t unpermittedStream = 0x620401U; + const uint32_t mismatchedStream = 0x620402U; + const uint32_t permittedStream = 0x620403U; + const uint16_t srcId = 5199U; + const uint16_t dstId = 212U; + + NXDNHostHarness harness(false, true, hostPort, hostPeerId, false, true); + NXDNTestNetwork sender(senderPort, 8016U); + harness.m_tidLookup.addEntry(admissionRule(dstId, { srcId })); + + REQUIRE(harness.network() != nullptr); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + + REQUIRE(sender.sendNXDNCallHeader(hostPeerId, unpermittedStream, 900U, srcId, dstId)); + for (uint32_t i = 0U; i < 10U; i++) + harness.network()->clock(1U); + REQUIRE(harness.network()->rxNXDNStreamId() == 0U); + + harness.m_control->permittedTG(313U); + REQUIRE(sender.sendNXDNCallHeader(hostPeerId, mismatchedStream, 901U, srcId, dstId)); + for (uint32_t i = 0U; i < 10U; i++) + harness.network()->clock(1U); + REQUIRE(harness.network()->rxNXDNStreamId() == 0U); + + harness.m_control->permittedTG(dstId); + REQUIRE(sender.sendNXDNCallHeader(hostPeerId, permittedStream, 902U, srcId, dstId)); + for (uint32_t i = 0U; i < 50U; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + if (HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO) + break; + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + + REQUIRE(HostTestHooks::nxdnNetState(*harness.m_control) == RS_NET_AUDIO); + REQUIRE(harness.network()->rxNXDNStreamId() == permittedStream); +} + TEST_CASE("NXDN host arms the network watchdog when network voice starts", "[nxdn][host][control]") { NXDNHostHarness harness; diff --git a/tests/p25/HostControl_Tests.cpp b/tests/p25/HostControl_Tests.cpp index 99525138..778219e7 100644 --- a/tests/p25/HostControl_Tests.cpp +++ b/tests/p25/HostControl_Tests.cpp @@ -679,7 +679,7 @@ TEST_CASE("P25 Harris radio aliases preserve scheduled link control", "[p25][hos } } -TEST_CASE("P25 Harris radio aliases follow the odd-even conveyance cadence", "[p25][host][radio-alias][harris]") +TEST_CASE("P25 Harris radio aliases follow the four-part conveyance cadence", "[p25][host][radio-alias][harris]") { P25HostHarness harness; harness.m_ridAliasLookup.addEntry(1001U, "TEST RADIO"); @@ -689,10 +689,8 @@ TEST_CASE("P25 Harris radio aliases follow the odd-even conveyance cadence", "[p const uint8_t expected[] = { p25::defines::LCO::HARRIS_USER_ALIAS_A_ODD, p25::defines::LCO::HARRIS_USER_ALIAS_B_ODD, - p25::defines::LCO::GROUP, p25::defines::LCO::HARRIS_USER_ALIAS_A_EVEN, - p25::defines::LCO::HARRIS_USER_ALIAS_B_EVEN, - p25::defines::LCO::GROUP + p25::defines::LCO::HARRIS_USER_ALIAS_B_EVEN }; for (uint8_t i = 0U; i < sizeof(expected); ++i) { @@ -703,12 +701,10 @@ TEST_CASE("P25 Harris radio aliases follow the odd-even conveyance cadence", "[p control.setDstId(2001U); const bool applied = HostTestHooks::p25ApplyRadioAlias(*harness.m_control, control, phase); - CHECK(applied == (i != 2U && i != 5U)); + CHECK(applied); CHECK(control.getLCO() == expected[i]); - if (applied) { - CHECK(control.getMFId() == p25::defines::MFG_HARRIS); - CHECK(control.getUserAlias() == "TEST RADIO"); - } + CHECK(control.getMFId() == p25::defines::MFG_HARRIS); + CHECK(control.getUserAlias() == "TEST RADIO"); } CHECK(phase == 0U); } @@ -1973,6 +1969,101 @@ TEST_CASE("P25 host e2e loopback enforces stream lock until active stream termin REQUIRE(harness.network()->rxP25StreamId() == streamB); } +TEST_CASE("P25 host drops denied streams before receive stream selection", "[p25][host][control][net][e2e][acl]") +{ + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); + + const uint32_t hostPeerId = 6013U; + const uint32_t deniedStream = 0x500301U; + const uint32_t allowedStream = 0x500302U; + const uint32_t srcId = 5199083U; + const uint32_t deniedDstId = 8384U; + const uint32_t allowedDstId = 212U; + + P25HostHarness harness(true, true, hostPort, hostPeerId, false, true); + P25TestNetwork sender(senderPort, 6014U); + harness.m_tidLookup.addEntry(admissionRule(allowedDstId, {})); + + REQUIRE(harness.network() != nullptr); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + + REQUIRE(sender.sendP25LDU1Frame(hostPeerId, deniedStream, 800U, srcId, deniedDstId)); + REQUIRE(sender.sendP25LDU2Frame(hostPeerId, deniedStream, 801U, srcId, deniedDstId)); + REQUIRE(sender.sendP25LDU1Frame(hostPeerId, allowedStream, 802U, srcId, allowedDstId)); + REQUIRE(sender.sendP25LDU2Frame(hostPeerId, allowedStream, 803U, srcId, allowedDstId)); + + for (uint32_t i = 0U; i < 50U; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + if (HostTestHooks::p25NetState(*harness.m_control) == RS_NET_AUDIO) + break; + + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + + REQUIRE(HostTestHooks::p25NetState(*harness.m_control) == RS_NET_AUDIO); + REQUIRE(HostTestHooks::p25NetLastSrcId(*harness.m_control) == srcId); + REQUIRE(HostTestHooks::p25NetLastDstId(*harness.m_control) == allowedDstId); + REQUIRE(harness.network()->rxP25StreamId() == allowedStream); + REQUIRE(harness.network()->resetP25Count() == 0U); +} + +TEST_CASE("P25 non-authoritative packet admission requires a matching permit", "[p25][host][control][net][e2e][acl][non-authoritative]") +{ + const uint16_t hostPort = reserveLoopbackPort(); + const uint16_t senderPort = reserveLoopbackPort(); + REQUIRE(hostPort != 0U); + REQUIRE(senderPort != 0U); + REQUIRE(hostPort != senderPort); + + const uint32_t hostPeerId = 6015U; + const uint32_t unpermittedStream = 0x500401U; + const uint32_t mismatchedStream = 0x500402U; + const uint32_t permittedStream = 0x500403U; + const uint32_t srcId = 5199083U; + const uint32_t dstId = 212U; + + P25HostHarness harness(false, true, hostPort, hostPeerId, false, true); + P25TestNetwork sender(senderPort, 6016U); + harness.m_tidLookup.addEntry(admissionRule(dstId, {})); + + REQUIRE(harness.network() != nullptr); + REQUIRE(harness.network()->activateLoopback("127.0.0.1", senderPort)); + REQUIRE(sender.activateLoopback("127.0.0.1", hostPort)); + + REQUIRE(sender.sendP25LDU1Frame(hostPeerId, unpermittedStream, 900U, srcId, dstId)); + for (uint32_t i = 0U; i < 10U; i++) + harness.network()->clock(1U); + REQUIRE(harness.network()->rxP25StreamId() == 0U); + + harness.m_control->permittedTG(313U, false, false, 0U, false); + REQUIRE(sender.sendP25LDU1Frame(hostPeerId, mismatchedStream, 901U, srcId, dstId)); + for (uint32_t i = 0U; i < 10U; i++) + harness.network()->clock(1U); + REQUIRE(harness.network()->rxP25StreamId() == 0U); + + harness.m_control->permittedTG(dstId, false, false, 0U, false); + REQUIRE(sender.sendP25LDU1Frame(hostPeerId, permittedStream, 902U, srcId, dstId)); + REQUIRE(sender.sendP25LDU2Frame(hostPeerId, permittedStream, 903U, srcId, dstId)); + for (uint32_t i = 0U; i < 50U; i++) { + sender.clock(1U); + harness.network()->clock(1U); + harness.m_control->clock(); + if (HostTestHooks::p25NetState(*harness.m_control) == RS_NET_AUDIO) + break; + std::this_thread::sleep_for(std::chrono::milliseconds(2)); + } + + REQUIRE(HostTestHooks::p25NetState(*harness.m_control) == RS_NET_AUDIO); + REQUIRE(harness.network()->rxP25StreamId() == permittedStream); +} + TEST_CASE("P25 host network terminator clears active network voice state", "[p25][host][control]") { P25HostHarness harness;