2018-06-12 11:57:14 +00:00
|
|
|
#include <llarp/bencode.hpp>
|
2018-06-22 00:25:30 +00:00
|
|
|
#include <llarp/messages/path_confirm.hpp>
|
2018-06-01 14:08:54 +00:00
|
|
|
#include <llarp/messages/relay_commit.hpp>
|
2018-06-22 00:25:30 +00:00
|
|
|
#include "buffer.hpp"
|
2018-06-10 14:05:48 +00:00
|
|
|
#include "logger.hpp"
|
|
|
|
#include "router.hpp"
|
2018-06-01 14:08:54 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
LR_CommitMessage::~LR_CommitMessage()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_CommitMessage::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
|
|
|
|
{
|
2018-06-11 13:25:10 +00:00
|
|
|
if(llarp_buffer_eq(key, "c"))
|
|
|
|
{
|
2018-06-12 11:57:14 +00:00
|
|
|
return BEncodeReadList(frames, buf);
|
2018-06-11 13:25:10 +00:00
|
|
|
}
|
2018-06-12 11:57:14 +00:00
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadVersion("v", version, LLARP_PROTO_VERSION, read, key,
|
|
|
|
buf))
|
|
|
|
return false;
|
2018-06-11 13:25:10 +00:00
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
return read;
|
2018-06-11 13:25:10 +00:00
|
|
|
}
|
|
|
|
|
2018-06-01 14:08:54 +00:00
|
|
|
bool
|
|
|
|
LR_CommitMessage::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2018-06-11 13:25:10 +00:00
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
// msg type
|
2018-06-12 11:57:14 +00:00
|
|
|
if(!BEncodeWriteDictMsgType(buf, "a", "c"))
|
2018-06-11 13:25:10 +00:00
|
|
|
return false;
|
|
|
|
// frames
|
2018-06-12 11:57:14 +00:00
|
|
|
if(!BEncodeWriteDictList("c", frames, buf))
|
|
|
|
return false;
|
|
|
|
// version
|
|
|
|
if(!bencode_write_version_entry(buf))
|
2018-06-11 13:25:10 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
2018-06-01 14:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_CommitMessage::HandleMessage(llarp_router* router) const
|
|
|
|
{
|
2018-06-18 22:03:50 +00:00
|
|
|
if(frames.size() != MAXHOPS)
|
|
|
|
{
|
2018-07-17 04:37:50 +00:00
|
|
|
llarp::LogError("LRCM invalid number of records, ", frames.size(),
|
|
|
|
"!=", MAXHOPS);
|
2018-06-18 22:03:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!router->paths.AllowingTransit())
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("got an LRCM from ", remote,
|
|
|
|
" when we are not allowing transit");
|
2018-06-18 22:03:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return AsyncDecrypt(&router->paths);
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_CommitRecord::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2018-06-12 11:57:14 +00:00
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(!BEncodeWriteDictEntry("c", commkey, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("i", nextHop, buf))
|
|
|
|
return false;
|
2018-06-29 14:25:09 +00:00
|
|
|
if(lifetime > 10 && lifetime < 600)
|
|
|
|
{
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("i", lifetime, buf))
|
2018-06-29 14:25:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-06-12 11:57:14 +00:00
|
|
|
if(!BEncodeWriteDictEntry("n", tunnelNonce, buf))
|
|
|
|
return false;
|
2018-06-22 00:25:30 +00:00
|
|
|
if(!BEncodeWriteDictEntry("r", rxid, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("t", txid, buf))
|
2018-06-12 11:57:14 +00:00
|
|
|
return false;
|
|
|
|
if(!bencode_write_version_entry(buf))
|
|
|
|
return false;
|
2018-06-18 22:03:50 +00:00
|
|
|
if(work && !BEncodeWriteDictEntry("w", *work, buf))
|
|
|
|
return false;
|
2018-06-12 11:57:14 +00:00
|
|
|
|
|
|
|
return bencode_end(buf);
|
2018-06-01 14:08:54 +00:00
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-06-18 22:03:50 +00:00
|
|
|
LR_CommitRecord::~LR_CommitRecord()
|
|
|
|
{
|
|
|
|
if(work)
|
|
|
|
delete work;
|
|
|
|
}
|
|
|
|
|
2018-06-10 14:05:48 +00:00
|
|
|
bool
|
|
|
|
LR_CommitRecord::OnKey(dict_reader* r, llarp_buffer_t* key)
|
|
|
|
{
|
|
|
|
if(!key)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
LR_CommitRecord* self = static_cast< LR_CommitRecord* >(r->user);
|
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
bool read = false;
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-06-12 11:57:14 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("c", self->commkey, read, *key, r->buffer))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("i", self->nextHop, read, *key, r->buffer))
|
|
|
|
return false;
|
2018-06-29 14:25:09 +00:00
|
|
|
if(!BEncodeMaybeReadDictInt("l", self->lifetime, read, *key, r->buffer))
|
|
|
|
return false;
|
2018-06-21 12:55:02 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("n", self->tunnelNonce, read, *key,
|
|
|
|
r->buffer))
|
2018-06-12 11:57:14 +00:00
|
|
|
return false;
|
2018-06-22 00:25:30 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("r", self->rxid, read, *key, r->buffer))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("t", self->txid, read, *key, r->buffer))
|
2018-06-12 11:57:14 +00:00
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadVersion("v", self->version, LLARP_PROTO_VERSION, read,
|
|
|
|
*key, r->buffer))
|
|
|
|
return false;
|
2018-06-18 22:03:50 +00:00
|
|
|
if(llarp_buffer_eq(*key, "w"))
|
|
|
|
{
|
|
|
|
// check for duplicate
|
|
|
|
if(self->work)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogWarn("duplicate POW in LRCR");
|
2018-06-18 22:03:50 +00:00
|
|
|
return false;
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-06-18 22:03:50 +00:00
|
|
|
self->work = new PoW;
|
|
|
|
return self->work->BDecode(r->buffer);
|
|
|
|
}
|
2018-06-12 11:57:14 +00:00
|
|
|
return read;
|
2018-06-10 14:05:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LR_CommitRecord::BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
dict_reader r;
|
|
|
|
r.user = this;
|
|
|
|
r.on_key = &OnKey;
|
|
|
|
return bencode_read_dict(buf, &r);
|
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-06-21 12:55:02 +00:00
|
|
|
bool
|
|
|
|
LR_CommitRecord::operator==(const LR_CommitRecord& other) const
|
|
|
|
{
|
|
|
|
if(work && other.work)
|
|
|
|
{
|
|
|
|
if(*work != *other.work)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return nextHop == other.nextHop && commkey == other.commkey
|
2018-06-22 00:25:30 +00:00
|
|
|
&& txid == other.txid && rxid == other.rxid;
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 22:03:50 +00:00
|
|
|
struct LRCMFrameDecrypt
|
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
typedef llarp::path::PathContext Context;
|
|
|
|
typedef llarp::path::TransitHop Hop;
|
2018-06-18 22:03:50 +00:00
|
|
|
typedef AsyncFrameDecrypter< LRCMFrameDecrypt > Decrypter;
|
|
|
|
Decrypter* decrypter;
|
|
|
|
std::deque< EncryptedFrame > frames;
|
2018-06-25 15:12:08 +00:00
|
|
|
Context* context;
|
2018-06-18 22:03:50 +00:00
|
|
|
// decrypted record
|
|
|
|
LR_CommitRecord record;
|
|
|
|
// the actual hop
|
2018-06-25 15:12:08 +00:00
|
|
|
Hop* hop;
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
LRCMFrameDecrypt(Context* ctx, Decrypter* dec,
|
2018-06-18 22:03:50 +00:00
|
|
|
const LR_CommitMessage* commit)
|
2018-06-25 15:12:08 +00:00
|
|
|
: decrypter(dec), context(ctx), hop(new Hop)
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
|
|
|
for(const auto& f : commit->frames)
|
2018-06-21 12:55:02 +00:00
|
|
|
frames.push_back(f);
|
2018-06-22 00:25:30 +00:00
|
|
|
hop->info.downstream = commit->remote;
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~LRCMFrameDecrypt()
|
|
|
|
{
|
|
|
|
delete decrypter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// this must be done from logic thread
|
|
|
|
static void
|
|
|
|
SendLRCM(void* user)
|
|
|
|
{
|
|
|
|
LRCMFrameDecrypt* self = static_cast< LRCMFrameDecrypt* >(user);
|
2018-06-22 00:25:30 +00:00
|
|
|
self->context->ForwardLRCM(self->hop->info.upstream, self->frames);
|
2018-06-18 22:03:50 +00:00
|
|
|
delete self;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-06-22 00:25:30 +00:00
|
|
|
SendPathConfirm(void* user)
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2018-06-22 00:25:30 +00:00
|
|
|
LRCMFrameDecrypt* self = static_cast< LRCMFrameDecrypt* >(user);
|
|
|
|
llarp::routing::PathConfirmMessage confirm(self->hop->lifetime);
|
|
|
|
if(!self->hop->SendRoutingMessage(&confirm, self->context->Router()))
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("failed to send path confirmation for ",
|
|
|
|
self->hop->info);
|
2018-06-22 00:25:30 +00:00
|
|
|
}
|
|
|
|
delete self;
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
HandleDecrypted(llarp_buffer_t* buf, LRCMFrameDecrypt* self)
|
|
|
|
{
|
2018-06-22 00:25:30 +00:00
|
|
|
auto& info = self->hop->info;
|
2018-06-18 22:03:50 +00:00
|
|
|
if(!buf)
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("LRCM decrypt failed from ", info.downstream);
|
2018-06-18 22:03:50 +00:00
|
|
|
delete self;
|
|
|
|
return;
|
|
|
|
}
|
2018-06-21 12:55:02 +00:00
|
|
|
buf->cur = buf->base + EncryptedFrame::OverheadSize;
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogDebug("decrypted LRCM from ", info.downstream);
|
2018-06-18 22:03:50 +00:00
|
|
|
// successful decrypt
|
|
|
|
if(!self->record.BDecode(buf))
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("malformed frame inside LRCM from ", info.downstream);
|
2018-06-18 22:03:50 +00:00
|
|
|
delete self;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-22 00:25:30 +00:00
|
|
|
info.txID = self->record.txid;
|
|
|
|
info.rxID = self->record.rxid;
|
2018-06-18 22:03:50 +00:00
|
|
|
info.upstream = self->record.nextHop;
|
|
|
|
if(self->context->HasTransitHop(info))
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("duplicate transit hop ", info);
|
2018-06-18 22:03:50 +00:00
|
|
|
delete self;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// generate path key as we are in a worker thread
|
|
|
|
auto DH = self->context->Crypto()->dh_server;
|
2018-06-22 00:25:30 +00:00
|
|
|
if(!DH(self->hop->pathKey, self->record.commkey,
|
2018-06-18 22:03:50 +00:00
|
|
|
self->context->EncryptionSecretKey(), self->record.tunnelNonce))
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("LRCM DH Failed ", info);
|
2018-06-18 22:03:50 +00:00
|
|
|
delete self;
|
|
|
|
return;
|
|
|
|
}
|
2018-07-28 22:20:32 +00:00
|
|
|
// generate hash of hop key for nonce mutation
|
|
|
|
self->context->Crypto()->shorthash(self->hop->nonceXOR,
|
|
|
|
llarp::Buffer(self->hop->pathKey));
|
2018-06-18 22:03:50 +00:00
|
|
|
if(self->record.work
|
2018-07-09 17:32:11 +00:00
|
|
|
&& self->record.work->IsValid(self->context->Crypto()->shorthash))
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2018-07-20 04:50:28 +00:00
|
|
|
llarp::LogDebug("LRCM extended lifetime by ",
|
|
|
|
self->record.work->extendedLifetime, " seconds for ",
|
|
|
|
info);
|
2018-06-22 00:25:30 +00:00
|
|
|
self->hop->lifetime += 1000 * self->record.work->extendedLifetime;
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
2018-06-29 14:25:09 +00:00
|
|
|
else if(self->record.lifetime < 600 && self->record.lifetime > 10)
|
|
|
|
{
|
|
|
|
self->hop->lifetime = self->record.lifetime;
|
2018-07-20 04:50:28 +00:00
|
|
|
llarp::LogDebug("LRCM short lifespan set to ", self->hop->lifetime,
|
|
|
|
" seconds for ", info);
|
2018-06-29 14:25:09 +00:00
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
|
|
|
// TODO: check if we really want to accept it
|
2018-06-22 00:25:30 +00:00
|
|
|
self->hop->started = llarp_time_now_ms();
|
2018-07-20 04:50:28 +00:00
|
|
|
llarp::LogDebug("Accepted ", self->hop->info);
|
2018-06-18 22:03:50 +00:00
|
|
|
self->context->PutTransitHop(self->hop);
|
|
|
|
|
2018-06-19 17:11:24 +00:00
|
|
|
size_t sz = self->frames.front().size();
|
2018-06-18 22:03:50 +00:00
|
|
|
// we pop the front element it was ours
|
|
|
|
self->frames.pop_front();
|
|
|
|
// put our response on the end
|
2018-06-27 13:13:33 +00:00
|
|
|
self->frames.emplace_back(sz - EncryptedFrame::OverheadSize);
|
2018-06-21 12:55:02 +00:00
|
|
|
// random junk for now
|
|
|
|
self->frames.back().Randomize();
|
2018-06-18 22:03:50 +00:00
|
|
|
|
|
|
|
if(self->context->HopIsUs(info.upstream))
|
|
|
|
{
|
|
|
|
// we are the farthest hop
|
2018-07-20 04:50:28 +00:00
|
|
|
llarp::LogDebug("We are the farthest hop for ", info);
|
2018-06-18 22:03:50 +00:00
|
|
|
// send a LRAM down the path
|
2018-06-22 00:25:30 +00:00
|
|
|
llarp_logic_queue_job(self->context->Logic(), {self, &SendPathConfirm});
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// forward upstream
|
|
|
|
// we are still in the worker thread so post job to logic
|
|
|
|
llarp_logic_queue_job(self->context->Logic(), {self, &SendLRCM});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
2018-06-25 15:12:08 +00:00
|
|
|
LR_CommitMessage::AsyncDecrypt(llarp::path::PathContext* context) const
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
|
|
|
LRCMFrameDecrypt::Decrypter* decrypter = new LRCMFrameDecrypt::Decrypter(
|
|
|
|
context->Crypto(), context->EncryptionSecretKey(),
|
|
|
|
&LRCMFrameDecrypt::HandleDecrypted);
|
|
|
|
// copy frames so we own them
|
|
|
|
LRCMFrameDecrypt* frames = new LRCMFrameDecrypt(context, decrypter, this);
|
|
|
|
|
|
|
|
// decrypt frames async
|
|
|
|
decrypter->AsyncDecrypt(context->Worker(), &frames->frames.front(), frames);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} // namespace llarp
|