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