Skip to content

Commit

Permalink
Mark session for eviction on TCP Client side when disconnecting with …
Browse files Browse the repository at this point in the history
…peer. (project-chip#34451)

The TCP server on receiving a TCPDisconnect from the client marks
the corresponding secure session for eviction.
The TCP client should also mark its session for eviction when proactively
closing the connection with peer.
  • Loading branch information
pidarped authored Jul 24, 2024
1 parent 8e32ce7 commit 138fb4f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/transport/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -714,27 +714,7 @@ void SessionManager::HandleConnectionClosed(Transport::ActiveTCPConnectionState
{
VerifyOrReturn(conn != nullptr);

// Mark the corresponding secure sessions as defunct
mSecureSessions.ForEachSession([&](auto session) {
if (session->IsActiveSession() && session->GetTCPConnection() == conn)
{
SessionHandle handle(*session);
// Notify the SessionConnection delegate of the connection
// closure.
if (mConnDelegate != nullptr)
{
mConnDelegate->OnTCPConnectionClosed(handle, conErr);
}

// Dis-associate the connection from session by setting it to a
// nullptr.
session->SetTCPConnection(nullptr);
// Mark session as defunct
session->MarkAsDefunct();
}

return Loop::Continue;
});
MarkSecureSessionOverTCPForEviction(conn, conErr);

// TODO: A mechanism to mark an unauthenticated session as unusable when
// the underlying connection is broken. Issue #32323
Expand Down Expand Up @@ -785,6 +765,8 @@ void SessionManager::TCPDisconnect(Transport::ActiveTCPConnectionState * conn, b
conn->mPeerAddr.ToString(peerAddrBuf);
ChipLogProgress(Inet, "Disconnecting TCP connection from peer at %s.", peerAddrBuf);
mTransportMgr->TCPDisconnect(conn, shouldAbort);

MarkSecureSessionOverTCPForEviction(conn, CHIP_NO_ERROR);
}
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT
Expand Down Expand Up @@ -1336,6 +1318,33 @@ Optional<SessionHandle> SessionManager::FindSecureSessionForNode(ScopedNodeId pe
return mrpSession != nullptr ? MakeOptional<SessionHandle>(*mrpSession) : Optional<SessionHandle>::Missing();
}

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
void SessionManager::MarkSecureSessionOverTCPForEviction(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr)
{
// Mark the corresponding secure sessions for eviction
mSecureSessions.ForEachSession([&](auto session) {
if (session->IsActiveSession() && session->GetTCPConnection() == conn)
{
SessionHandle handle(*session);
// Notify the SessionConnection delegate of the connection
// closure.
if (mConnDelegate != nullptr)
{
mConnDelegate->OnTCPConnectionClosed(handle, conErr);
}

// Dis-associate the connection from session by setting it to a
// nullptr.
session->SetTCPConnection(nullptr);
// Mark session for eviction.
session->MarkForEviction();
}

return Loop::Continue;
});
}
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

/**
* Provides a means to get diagnostic information such as number of sessions.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/transport/SessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,10 @@ class DLL_EXPORT SessionManager : public TransportMgrDelegate, public FabricTabl

void OnReceiveError(CHIP_ERROR error, const Transport::PeerAddress & source);

#if INET_CONFIG_ENABLE_TCP_ENDPOINT
void MarkSecureSessionOverTCPForEviction(Transport::ActiveTCPConnectionState * conn, CHIP_ERROR conErr);
#endif // INET_CONFIG_ENABLE_TCP_ENDPOINT

static bool IsControlMessage(PayloadHeader & payloadHeader)
{
return payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::MsgCounterSyncReq) ||
Expand Down

0 comments on commit 138fb4f

Please sign in to comment.