i2pd/libi2pd/TunnelEndpoint.cpp

299 lines
9.4 KiB
C++
Raw Normal View History

#include "I2PEndian.h"
2013-11-11 03:19:49 +04:00
#include <string.h>
2016-05-11 22:12:38 +03:00
#include "Crypto.h"
2013-11-11 03:19:49 +04:00
#include "Log.h"
#include "NetDb.hpp"
2013-11-11 03:19:49 +04:00
#include "I2NPProtocol.h"
#include "Transports.h"
#include "RouterContext.h"
#include "Timestamp.h"
2013-11-11 03:19:49 +04:00
#include "TunnelEndpoint.h"
namespace i2p
{
namespace tunnel
{
2014-07-05 16:33:08 +04:00
TunnelEndpoint::~TunnelEndpoint ()
{
}
void TunnelEndpoint::HandleDecryptedTunnelDataMsg (std::shared_ptr<I2NPMessage> msg)
2013-11-11 03:19:49 +04:00
{
2013-12-10 17:10:49 +04:00
m_NumReceivedBytes += TUNNEL_DATA_MSG_SIZE;
2013-11-11 03:19:49 +04:00
uint8_t * decrypted = msg->GetPayload () + 20; // 4 + 16
2013-12-10 17:10:49 +04:00
uint8_t * zero = (uint8_t *)memchr (decrypted + 4, 0, TUNNEL_DATA_ENCRYPTED_SIZE - 4); // witout 4-byte checksum
2013-11-11 03:19:49 +04:00
if (zero)
{
uint8_t * fragment = zero + 1;
2014-06-28 04:11:21 +04:00
// verify checksum
memcpy (msg->GetPayload () + TUNNEL_DATA_MSG_SIZE, msg->GetPayload () + 4, 16); // copy iv to the end
uint8_t hash[32];
2015-11-03 17:15:49 +03:00
SHA256(fragment, TUNNEL_DATA_MSG_SIZE -(fragment - msg->GetPayload ()) + 16, hash); // payload + iv
2014-06-28 04:11:21 +04:00
if (memcmp (hash, decrypted, 4))
{
2015-02-05 06:05:09 +03:00
LogPrint (eLogError, "TunnelMessage: checksum verification failed");
2014-06-28 04:11:21 +04:00
return;
}
// process fragments
2013-12-10 17:10:49 +04:00
while (fragment < decrypted + TUNNEL_DATA_ENCRYPTED_SIZE)
2013-11-11 03:19:49 +04:00
{
uint8_t flag = fragment[0];
fragment++;
bool isFollowOnFragment = flag & 0x80, isLastFragment = true;
uint32_t msgID = 0;
2014-06-11 18:56:20 +04:00
int fragmentNum = 0;
TunnelMessageBlockEx m;
2013-11-11 03:19:49 +04:00
if (!isFollowOnFragment)
{
// first fragment
m.deliveryType = (TunnelDeliveryType)((flag >> 5) & 0x03);
switch (m.deliveryType)
{
case eDeliveryTypeLocal: // 0
break;
case eDeliveryTypeTunnel: // 1
m.tunnelID = bufbe32toh (fragment);
2013-11-11 03:19:49 +04:00
fragment += 4; // tunnelID
2014-01-21 03:37:51 +04:00
m.hash = i2p::data::IdentHash (fragment);
2013-11-11 03:19:49 +04:00
fragment += 32; // hash
break;
case eDeliveryTypeRouter: // 2
2014-01-21 03:37:51 +04:00
m.hash = i2p::data::IdentHash (fragment);
2013-11-11 03:19:49 +04:00
fragment += 32; // to hash
break;
default:
;
}
bool isFragmented = flag & 0x08;
if (isFragmented)
{
// Message ID
msgID = bufbe32toh (fragment);
2013-11-11 03:19:49 +04:00
fragment += 4;
isLastFragment = false;
}
}
else
{
// follow on
msgID = bufbe32toh (fragment); // MessageID
2013-11-11 03:19:49 +04:00
fragment += 4;
2014-06-11 18:56:20 +04:00
fragmentNum = (flag >> 1) & 0x3F; // 6 bits
2013-11-11 03:19:49 +04:00
isLastFragment = flag & 0x01;
}
uint16_t size = bufbe16toh (fragment);
2013-11-11 03:19:49 +04:00
fragment += 2;
msg->offset = fragment - msg->buf;
msg->len = msg->offset + size;
2016-01-25 22:31:51 +03:00
if (msg->len > msg->maxLen)
{
LogPrint (eLogError, "TunnelMessage: fragment is too long ", (int)size);
return;
}
2013-12-10 17:10:49 +04:00
if (fragment + size < decrypted + TUNNEL_DATA_ENCRYPTED_SIZE)
2013-11-11 03:19:49 +04:00
{
// this is not last message. we have to copy it
2017-04-14 17:19:26 +03:00
m.data = NewI2NPTunnelMessage ();
2015-01-02 02:53:44 +03:00
m.data->offset += TUNNEL_GATEWAY_HEADER_SIZE; // reserve room for TunnelGateway header
m.data->len += TUNNEL_GATEWAY_HEADER_SIZE;
2013-11-11 03:19:49 +04:00
*(m.data) = *msg;
}
else
m.data = msg;
if (!isFollowOnFragment && isLastFragment)
HandleNextMessage (m);
else
{
if (msgID) // msgID is presented, assume message is fragmented
{
if (!isFollowOnFragment) // create new incomlete message
2014-06-11 18:56:20 +04:00
{
m.nextFragmentNum = 1;
2016-12-07 00:23:52 +03:00
m.receiveTime = i2p::util::GetMillisecondsSinceEpoch ();
2015-02-05 06:16:44 +03:00
auto ret = m_IncompleteMessages.insert (std::pair<uint32_t, TunnelMessageBlockEx>(msgID, m));
if (ret.second)
HandleOutOfSequenceFragments (msgID, ret.first->second);
2015-02-05 06:16:44 +03:00
else
2016-04-13 18:22:08 +03:00
LogPrint (eLogError, "TunnelMessage: Incomplete message ", msgID, " already exists");
2014-06-11 18:56:20 +04:00
}
2013-11-11 03:19:49 +04:00
else
{
2014-07-05 04:54:03 +04:00
m.nextFragmentNum = fragmentNum;
HandleFollowOnFragment (msgID, isLastFragment, m);
2013-11-11 03:19:49 +04:00
}
}
2015-02-05 06:05:09 +03:00
else
2016-01-20 03:00:00 +03:00
LogPrint (eLogError, "TunnelMessage: Message is fragmented, but msgID is not presented");
2013-11-11 03:19:49 +04:00
}
fragment += size;
}
}
else
2015-02-05 06:05:09 +03:00
LogPrint (eLogError, "TunnelMessage: zero not found");
2013-11-11 03:19:49 +04:00
}
2014-07-05 04:54:03 +04:00
void TunnelEndpoint::HandleFollowOnFragment (uint32_t msgID, bool isLastFragment, const TunnelMessageBlockEx& m)
{
auto fragment = m.data->GetBuffer ();
auto size = m.data->GetLength ();
auto it = m_IncompleteMessages.find (msgID);
if (it != m_IncompleteMessages.end())
{
auto& msg = it->second;
if (m.nextFragmentNum == msg.nextFragmentNum)
2014-07-05 04:54:03 +04:00
{
2015-03-11 20:24:13 +03:00
if (msg.data->len + size < I2NP_MAX_MESSAGE_SIZE) // check if message is not too long
2014-07-05 04:54:03 +04:00
{
2015-03-11 20:24:13 +03:00
if (msg.data->len + size > msg.data->maxLen)
{
2017-04-14 17:19:26 +03:00
// LogPrint (eLogWarning, "TunnelMessage: I2NP message size ", msg.data->maxLen, " is not enough");
2015-11-24 21:09:12 +03:00
auto newMsg = NewI2NPMessage ();
2015-03-11 20:24:13 +03:00
*newMsg = *(msg.data);
msg.data = newMsg;
}
2016-01-05 22:29:18 +03:00
if (msg.data->Concat (fragment, size) < size) // concatenate fragment
2016-01-20 03:00:00 +03:00
LogPrint (eLogError, "TunnelMessage: I2NP buffer overflow ", msg.data->maxLen);
2014-07-05 04:54:03 +04:00
if (isLastFragment)
{
// message complete
HandleNextMessage (msg);
2014-07-05 04:54:03 +04:00
m_IncompleteMessages.erase (it);
}
else
{
msg.nextFragmentNum++;
HandleOutOfSequenceFragments (msgID, msg);
}
2014-07-05 04:54:03 +04:00
}
else
{
2016-01-20 03:00:00 +03:00
LogPrint (eLogError, "TunnelMessage: Fragment ", m.nextFragmentNum, " of message ", msgID, "exceeds max I2NP message size, message dropped");
2014-07-05 04:54:03 +04:00
m_IncompleteMessages.erase (it);
}
}
else
{
2016-01-20 03:00:00 +03:00
LogPrint (eLogWarning, "TunnelMessage: Unexpected fragment ", (int)m.nextFragmentNum, " instead ", (int)msg.nextFragmentNum, " of message ", msgID, ", saved");
AddOutOfSequenceFragment (msgID, m.nextFragmentNum, isLastFragment, m.data);
2014-07-05 04:54:03 +04:00
}
}
else
{
2016-01-20 03:00:00 +03:00
LogPrint (eLogWarning, "TunnelMessage: First fragment of message ", msgID, " not found, saved");
AddOutOfSequenceFragment (msgID, m.nextFragmentNum, isLastFragment, m.data);
}
}
void TunnelEndpoint::AddOutOfSequenceFragment (uint32_t msgID, uint8_t fragmentNum, bool isLastFragment, std::shared_ptr<I2NPMessage> data)
{
if (!m_OutOfSequenceFragments.insert ({{msgID, fragmentNum}, {isLastFragment, data, i2p::util::GetMillisecondsSinceEpoch () }}).second)
LogPrint (eLogInfo, "TunnelMessage: duplicate out-of-sequence fragment ", fragmentNum, " of message ", msgID);
}
2014-07-05 04:54:03 +04:00
void TunnelEndpoint::HandleOutOfSequenceFragments (uint32_t msgID, TunnelMessageBlockEx& msg)
{
while (ConcatNextOutOfSequenceFragment (msgID, msg))
{
if (!msg.nextFragmentNum) // message complete
{
HandleNextMessage (msg);
m_IncompleteMessages.erase (msgID);
break;
}
}
}
bool TunnelEndpoint::ConcatNextOutOfSequenceFragment (uint32_t msgID, TunnelMessageBlockEx& msg)
{
auto it = m_OutOfSequenceFragments.find ({msgID, msg.nextFragmentNum});
if (it != m_OutOfSequenceFragments.end ())
{
LogPrint (eLogDebug, "TunnelMessage: Out-of-sequence fragment ", (int)msg.nextFragmentNum, " of message ", msgID, " found");
size_t size = it->second.data->GetLength ();
if (msg.data->len + size > msg.data->maxLen)
{
LogPrint (eLogWarning, "TunnelMessage: Tunnel endpoint I2NP message size ", msg.data->maxLen, " is not enough");
auto newMsg = NewI2NPMessage ();
*newMsg = *(msg.data);
msg.data = newMsg;
}
if (msg.data->Concat (it->second.data->GetBuffer (), size) < size) // concatenate out-of-sync fragment
LogPrint (eLogError, "TunnelMessage: Tunnel endpoint I2NP buffer overflow ", msg.data->maxLen);
if (it->second.isLastFragment)
// message complete
msg.nextFragmentNum = 0;
else
msg.nextFragmentNum++;
m_OutOfSequenceFragments.erase (it);
return true;
}
return false;
2014-07-05 04:54:03 +04:00
}
2013-11-11 03:19:49 +04:00
void TunnelEndpoint::HandleNextMessage (const TunnelMessageBlock& msg)
{
2016-01-19 19:16:50 +03:00
if (!m_IsInbound && msg.data->IsExpired ())
2016-01-19 05:13:43 +03:00
{
LogPrint (eLogInfo, "TunnelMessage: message expired");
return;
}
2016-06-28 19:20:18 +03:00
uint8_t typeID = msg.data->GetTypeID ();
2016-01-18 03:00:00 +03:00
LogPrint (eLogDebug, "TunnelMessage: handle fragment of ", msg.data->GetLength (), " bytes, msg type ", (int)typeID);
2016-02-01 02:27:47 +03:00
// catch RI or reply with new list of routers
if ((IsRouterInfoMsg (msg.data) || typeID == eI2NPDatabaseSearchReply) &&
!m_IsInbound && msg.deliveryType != eDeliveryTypeLocal)
i2p::data::netdb.PostI2NPMsg (CopyI2NPMessage (msg.data));
2013-11-11 03:19:49 +04:00
switch (msg.deliveryType)
{
case eDeliveryTypeLocal:
i2p::HandleI2NPMessage (msg.data);
2013-11-11 03:19:49 +04:00
break;
case eDeliveryTypeTunnel:
2015-12-09 18:03:51 +03:00
if (!m_IsInbound) // outbound transit tunnel
i2p::transport::transports.SendMessage (msg.hash, i2p::CreateTunnelGatewayMsg (msg.tunnelID, msg.data));
else
2016-01-20 03:00:00 +03:00
LogPrint (eLogError, "TunnelMessage: Delivery type 'tunnel' arrived from an inbound tunnel, dropped");
2015-12-09 18:03:51 +03:00
break;
case eDeliveryTypeRouter:
if (!m_IsInbound) // outbound transit tunnel
i2p::transport::transports.SendMessage (msg.hash, msg.data);
else // we shouldn't send this message. possible leakage
2016-01-20 03:00:00 +03:00
LogPrint (eLogError, "TunnelMessage: Delivery type 'router' arrived from an inbound tunnel, dropped");
2013-11-11 03:19:49 +04:00
break;
default:
2015-02-05 06:05:09 +03:00
LogPrint (eLogError, "TunnelMessage: Unknown delivery type ", (int)msg.deliveryType);
2013-11-11 03:19:49 +04:00
};
}
void TunnelEndpoint::Cleanup ()
{
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
// out-of-sequence fragments
for (auto it = m_OutOfSequenceFragments.begin (); it != m_OutOfSequenceFragments.end ();)
{
if (ts > it->second.receiveTime + i2p::I2NP_MESSAGE_EXPIRATION_TIMEOUT)
it = m_OutOfSequenceFragments.erase (it);
else
++it;
}
2016-12-07 00:23:52 +03:00
// incomplete messages
for (auto it = m_IncompleteMessages.begin (); it != m_IncompleteMessages.end ();)
{
if (ts > it->second.receiveTime + i2p::I2NP_MESSAGE_EXPIRATION_TIMEOUT)
it = m_IncompleteMessages.erase (it);
else
++it;
}
2013-11-11 03:19:49 +04:00
}
}
}