Skip to content

Commit

Permalink
Merge e5f98aa into 08f8a10
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost authored Mar 5, 2021
2 parents 08f8a10 + e5f98aa commit 3859496
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/messaging/ExchangeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ CHIP_ERROR ExchangeContext::SendMessage(uint16_t protocolId, uint8_t msgType, Pa
VerifyOrReturnError(state != nullptr, CHIP_ERROR_NOT_CONNECTED);

// If a group message is to be transmitted to a destination node whose message counter is unknown.
if (ChipKeyId::IsAppGroupKey(state->GetLocalKeyID()) && !state->isPeerMsgCounterSynced())
if (ChipKeyId::IsAppGroupKey(state->GetLocalKeyID()) && !state->IsPeerMsgCounterSynced())
{
MessageCounterSyncMgr * messageCounterSyncMgr = mExchangeMgr->GetMessageCounterSyncMgr();
VerifyOrReturnError(messageCounterSyncMgr != nullptr, CHIP_ERROR_INTERNAL);

// Queue the message as need for sync with destination node.
// Queue the message as needed for sync with destination node.
err = messageCounterSyncMgr->AddToRetransmissionTable(protocolId, msgType, sendFlags, std::move(msgBuf), this);
ReturnErrorOnFailure(err);

Expand Down
5 changes: 5 additions & 0 deletions src/messaging/ExchangeContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ class DLL_EXPORT ExchangeContext : public ReferenceCounted<ExchangeContext, Exch
bool MatchExchange(SecureSessionHandle session, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader);

CHIP_ERROR StartResponseTimer();

/**
* A subset of SendMessage functionality that does not perform message
* counter sync for group keys.
*/
CHIP_ERROR SendMessageImpl(uint16_t protocolId, uint8_t msgType, System::PacketBufferHandle msgBuf, const SendFlags & sendFlags,
Transport::PeerConnectionState * state = nullptr);
void CancelResponseTimer();
Expand Down
8 changes: 4 additions & 4 deletions src/messaging/ExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ CHIP_ERROR ExchangeManager::UnregisterUMH(uint32_t protocolId, int16_t msgType)
return CHIP_ERROR_NO_UNSOLICITED_MESSAGE_HANDLER;
}

bool ExchangeManager::isMsgCounterSyncMessage(const PayloadHeader & payloadHeader) const
bool ExchangeManager::IsMsgCounterSyncMessage(const PayloadHeader & payloadHeader)
{
if (payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::MsgCounterSyncReq) ||
payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::MsgCounterSyncRsp))
Expand All @@ -231,12 +231,12 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const
UnsolicitedMessageHandler * matchingUMH = nullptr;
bool sendAckAndCloseExchange = false;

if (!isMsgCounterSyncMessage(payloadHeader) && packetHeader.IsPeerGroupMsgIdNotSynchronized())
if (!IsMsgCounterSyncMessage(payloadHeader) && packetHeader.IsPeerGroupMsgIdNotSynchronized())
{
Transport::PeerConnectionState * state = mSessionMgr->GetPeerConnectionState(session);
VerifyOrReturn(state != nullptr);

// Queue the message as need for sync with destination node.
// Queue the message as needed for sync with destination node.
err = mMessageCounterSyncMgr.AddToReceiveTable(packetHeader, payloadHeader, session, std::move(msgBuf));
VerifyOrReturn(err == CHIP_NO_ERROR);

Expand All @@ -254,7 +254,7 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const
}

// After the message that triggers message counter synchronization is stored, and a message counter
// synchronization exchange is initiated, we need to return immeidately and re-process the original message
// synchronization exchange is initiated, we need to return immediately and re-process the original message
// when the synchronization is completed.

return;
Expand Down
5 changes: 3 additions & 2 deletions src/messaging/ExchangeMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate

/**
* @brief
* Called when a cached group message is received.
* Called when a cached group message that was waiting for message counter
* sync shold be reprocessed.
*
* @param packetHeader The message header
* @param payloadHeader The payload header
Expand Down Expand Up @@ -225,7 +226,7 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate
CHIP_ERROR RegisterUMH(uint32_t protocolId, int16_t msgType, ExchangeDelegate * delegate);
CHIP_ERROR UnregisterUMH(uint32_t protocolId, int16_t msgType);

bool isMsgCounterSyncMessage(const PayloadHeader & payloadHeader) const;
static bool IsMsgCounterSyncMessage(const PayloadHeader & payloadHeader);

void OnReceiveError(CHIP_ERROR error, const Transport::PeerAddress & source, SecureSessionMgr * msgLayer) override;

Expand Down
17 changes: 11 additions & 6 deletions src/messaging/MessageCounterSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ CHIP_ERROR MessageCounterSyncMgr::AddToRetransmissionTable(uint16_t protocolId,

for (RetransTableEntry & entry : mRetransTable)
{
// Check the exchContext pointer for finding an empty slot in Table
// Entries are in use if they have an exchangeContext.
if (entry.exchangeContext == nullptr)
{
entry.protocolId = protocolId;
Expand Down Expand Up @@ -136,7 +136,8 @@ CHIP_ERROR MessageCounterSyncMgr::AddToRetransmissionTable(uint16_t protocolId,
*/
void MessageCounterSyncMgr::RetransPendingGroupMsgs(NodeId peerNodeId)
{
// Find all retransmit entries (re) matching peerNodeId and using application group key.
// Find all retransmit entries matching peerNodeId. Note that everything in
// this table was using an application group key; that's why it was added.
for (RetransTableEntry & entry : mRetransTable)
{
if (entry.exchangeContext != nullptr && entry.exchangeContext->GetSecureSession().GetPeerNodeId() == peerNodeId)
Expand Down Expand Up @@ -165,7 +166,7 @@ CHIP_ERROR MessageCounterSyncMgr::AddToReceiveTable(const PacketHeader & packetH

for (ReceiveTableEntry & entry : mReceiveTable)
{
// Check the exchContext pointer for finding an empty slot in Table
// Entries are in use if they have a message buffer.
if (entry.msgBuf.IsNull())
{
entry.packetHeader = packetHeader;
Expand All @@ -189,14 +190,15 @@ CHIP_ERROR MessageCounterSyncMgr::AddToReceiveTable(const PacketHeader & packetH

/**
* Reprocess all pending messages that were encrypted with application
* group key and were addressed to the specified node.
* group key and were addressed to the specified node id.
*
* @param[in] peerNodeId Node ID of the destination node.
*
*/
void MessageCounterSyncMgr::ProcessPendingGroupMsgs(NodeId peerNodeId)
{
// Find all retransmit entries (re) matching peerNodeId and using application group key.
// Find all receive entries matching peerNodeId. Note that everything in
// this table was using an application group key; that's why it was added.
for (ReceiveTableEntry & entry : mReceiveTable)
{
if (!entry.msgBuf.IsNull() && entry.session.GetPeerNodeId() == peerNodeId)
Expand All @@ -205,7 +207,10 @@ void MessageCounterSyncMgr::ProcessPendingGroupMsgs(NodeId peerNodeId)
mExchangeMgr->HandleGroupMessageReceived(entry.packetHeader, entry.payloadHeader, entry.session,
std::move(entry.msgBuf));

// Explicitly free any buffer owned by this handle.
// Explicitly free any buffer owned by this handle. The
// HandleGroupMessageReceived() call should really handle this, but
// just in case it messes up we don't want to get confused about
// wheter the entry is in use.
entry.msgBuf = nullptr;
}
}
Expand Down
25 changes: 14 additions & 11 deletions src/messaging/MessageCounterSync.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class MessageCounterSyncMgr : public Messaging::ExchangeDelegate
System::PacketBufferHandle msgBuf, Messaging::ExchangeContext * exchangeContext);

/**
* Add a CHIP message into the cache table to queue the incomming messages that trigger message counter synchronization
* Add a CHIP message into the cache table to queue the incoming messages that trigger message counter synchronization
* protocol for re-processing.
*
* @param[in] packetHeader The message header for the received message.
Expand All @@ -98,12 +98,13 @@ class MessageCounterSyncMgr : public Messaging::ExchangeDelegate
* This class is part of the CHIP Message Counter Synchronization Protocol and is used
* to keep track of a CHIP messages to be transmitted to a destination node whose message
* counter is unknown. The message would be retransmitted from this table after message
* synchronization is completed.
* counter synchronization is completed.
*
*/
struct RetransTableEntry
{
ExchangeContext * exchangeContext; /**< The ExchangeContext for the stored CHIP message. */
ExchangeContext * exchangeContext; /**< The ExchangeContext for the stored CHIP message.
Non-null if and only if this entry is in use. */
System::PacketBufferHandle msgBuf; /**< A handle to the PacketBuffer object holding the CHIP message. */
SendFlags sendFlags; /**< Flags set by the application for the CHIP message being sent. */
uint16_t protocolId; /**< The protocol identifier of the CHIP message to be sent. */
Expand All @@ -115,25 +116,27 @@ class MessageCounterSyncMgr : public Messaging::ExchangeDelegate
*
* @brief
* This class is part of the CHIP Message Counter Synchronization Protocol and is used
* to keep track of a CHIP messages to be transmitted to a destination node whose message
* counter is unknown. The message would be retransmitted from this table after message
* synchronization is completed.
* to keep track of a CHIP messages to be reprocessed whose source's
* message counter is unknown. The message is reprocessed after message
* counter synchronization is completed.
*
*/
struct ReceiveTableEntry
{
PacketHeader packetHeader; /**< The ExchangeContext for the stored CHIP message. */
PayloadHeader payloadHeader; /**< Flags set by the application for the CHIP message being sent. */
SecureSessionHandle session; /**< The protocol identifier of the CHIP message to be sent. */
System::PacketBufferHandle msgBuf; /**< A handle to the PacketBuffer object holding the CHIP message. */
PacketHeader packetHeader; /**< The packet header for the message. */
PayloadHeader payloadHeader; /**< The payload header for the message. */
SecureSessionHandle session; /**< The secure session the message was received on. */
System::PacketBufferHandle msgBuf; /**< A handle to the PacketBuffer object holding
the message data. This is non-null if and only
if this entry is in use. */
};

Messaging::ExchangeManager * mExchangeMgr; // [READ ONLY] Associated Exchange Manager object.

// MessageCounterSyncProtocol cache table to queue the outging messages that trigger message counter synchronization protocol.
RetransTableEntry mRetransTable[CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS];

// MessageCounterSyncProtocol cache table to queue the incomming messages that trigger message counter synchronization protocol.
// MessageCounterSyncProtocol cache table to queue the incoming messages that trigger message counter synchronization protocol.
ReceiveTableEntry mReceiveTable[CHIP_CONFIG_MAX_EXCHANGE_CONTEXTS];

void RetransPendingGroupMsgs(NodeId peerNodeId);
Expand Down
2 changes: 1 addition & 1 deletion src/transport/PeerConnectionState.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class PeerConnectionState
void SetTransport(Transport::Base * transport) { mTransport = transport; }
Transport::Base * GetTransport() { return mTransport; }

bool isPeerMsgCounterSynced() { return (mPeerMessageIndex != kUndefinedMessageIndex); }
bool IsPeerMsgCounterSynced() { return (mPeerMessageIndex != kUndefinedMessageIndex); }
void SetPeerMessageIndex(uint32_t id) { mPeerMessageIndex = id; }

NodeId GetPeerNodeId() const { return mPeerNodeId; }
Expand Down
2 changes: 1 addition & 1 deletion src/transport/SecureSessionMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ void SecureSessionMgr::OnMessageReceived(const PacketHeader & packetHeader, cons
state->SetPeerAddress(peerAddress);
}

if (!state->isPeerMsgCounterSynced())
if (!state->IsPeerMsgCounterSynced())
{
// For all control messages, the first authenticated message counter from an unsynchronized peer is trusted
// and used to seed subsequent message counter based replay protection.
Expand Down

0 comments on commit 3859496

Please sign in to comment.