2018-06-18 22:03:50 +00:00
|
|
|
#include <llarp/nodedb.h>
|
|
|
|
#include <llarp/path.hpp>
|
|
|
|
|
2018-07-09 17:32:11 +00:00
|
|
|
#include <llarp/pathbuilder.hpp>
|
2018-07-28 22:20:32 +00:00
|
|
|
#include "buffer.hpp"
|
2018-06-18 22:03:50 +00:00
|
|
|
#include "router.hpp"
|
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2018-06-21 12:55:02 +00:00
|
|
|
template < typename User >
|
|
|
|
struct AsyncPathKeyExchangeContext
|
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
typedef llarp::path::Path Path_t;
|
|
|
|
typedef llarp::path::PathSet PathSet_t;
|
|
|
|
PathSet_t* pathset = nullptr;
|
|
|
|
Path_t* path = nullptr;
|
2018-06-21 12:55:02 +00:00
|
|
|
typedef void (*Handler)(AsyncPathKeyExchangeContext< User >*);
|
|
|
|
User* user = nullptr;
|
|
|
|
Handler result = nullptr;
|
|
|
|
size_t idx = 0;
|
|
|
|
llarp_threadpool* worker = nullptr;
|
|
|
|
llarp_logic* logic = nullptr;
|
|
|
|
llarp_crypto* crypto = nullptr;
|
|
|
|
LR_CommitMessage* LRCM = nullptr;
|
|
|
|
|
|
|
|
static void
|
|
|
|
HandleDone(void* u)
|
|
|
|
{
|
|
|
|
AsyncPathKeyExchangeContext< User >* ctx =
|
|
|
|
static_cast< AsyncPathKeyExchangeContext< User >* >(u);
|
|
|
|
ctx->result(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GenerateNextKey(void* u)
|
|
|
|
{
|
|
|
|
AsyncPathKeyExchangeContext< User >* ctx =
|
|
|
|
static_cast< AsyncPathKeyExchangeContext< User >* >(u);
|
|
|
|
|
2018-06-21 14:20:14 +00:00
|
|
|
// current hop
|
2018-06-21 12:55:02 +00:00
|
|
|
auto& hop = ctx->path->hops[ctx->idx];
|
|
|
|
auto& frame = ctx->LRCM->frames[ctx->idx];
|
|
|
|
// generate key
|
|
|
|
ctx->crypto->encryption_keygen(hop.commkey);
|
|
|
|
hop.nonce.Randomize();
|
|
|
|
// do key exchange
|
|
|
|
if(!ctx->crypto->dh_client(hop.shared, hop.router.enckey, hop.commkey,
|
|
|
|
hop.nonce))
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("Failed to generate shared key for path build");
|
2018-06-21 12:55:02 +00:00
|
|
|
abort();
|
|
|
|
return;
|
|
|
|
}
|
2018-07-28 22:20:32 +00:00
|
|
|
// generate nonceXOR valueself->hop->pathKey
|
|
|
|
ctx->crypto->shorthash(hop.nonceXOR, llarp::Buffer(hop.shared));
|
2018-06-21 12:55:02 +00:00
|
|
|
++ctx->idx;
|
2018-06-21 14:20:14 +00:00
|
|
|
|
2018-06-22 13:59:28 +00:00
|
|
|
bool isFarthestHop = ctx->idx == ctx->path->hops.size();
|
2018-06-21 14:20:14 +00:00
|
|
|
|
|
|
|
if(isFarthestHop)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
hop.upstream = hop.router.pubkey;
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
hop.upstream = ctx->path->hops[ctx->idx].router.pubkey;
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
2018-06-21 15:46:35 +00:00
|
|
|
|
|
|
|
// build record
|
|
|
|
LR_CommitRecord record;
|
|
|
|
record.version = LLARP_PROTO_VERSION;
|
2018-06-22 12:45:46 +00:00
|
|
|
record.txid = hop.txID;
|
|
|
|
record.rxid = hop.rxID;
|
2018-06-21 15:46:35 +00:00
|
|
|
record.tunnelNonce = hop.nonce;
|
|
|
|
record.nextHop = hop.upstream;
|
|
|
|
record.commkey = llarp::seckey_topublic(hop.commkey);
|
|
|
|
|
2018-06-21 12:55:02 +00:00
|
|
|
auto buf = frame.Buffer();
|
|
|
|
buf->cur = buf->base + EncryptedFrame::OverheadSize;
|
2018-06-21 15:46:35 +00:00
|
|
|
// encode record
|
2018-06-21 12:55:02 +00:00
|
|
|
if(!record.BEncode(buf))
|
|
|
|
{
|
|
|
|
// failed to encode?
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("Failed to generate Commit Record");
|
2018-06-21 12:55:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// use ephameral keypair for frame
|
|
|
|
SecretKey framekey;
|
|
|
|
ctx->crypto->encryption_keygen(framekey);
|
|
|
|
if(!frame.EncryptInPlace(framekey, hop.router.enckey, ctx->crypto))
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("Failed to encrypt LRCR");
|
2018-06-21 12:55:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-21 14:20:14 +00:00
|
|
|
if(isFarthestHop)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
// farthest hop
|
|
|
|
llarp_logic_queue_job(ctx->logic, {ctx, &HandleDone});
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-21 14:20:14 +00:00
|
|
|
// next hop
|
|
|
|
llarp_threadpool_queue_job(ctx->worker, {ctx, &GenerateNextKey});
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AsyncPathKeyExchangeContext(llarp_crypto* c) : crypto(c)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate all keys asynchronously and call hadler when done
|
|
|
|
void
|
2018-06-25 15:12:08 +00:00
|
|
|
AsyncGenerateKeys(Path_t* p, llarp_logic* l, llarp_threadpool* pool,
|
|
|
|
User* u, Handler func)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
|
|
|
path = p;
|
|
|
|
logic = l;
|
|
|
|
user = u;
|
|
|
|
result = func;
|
|
|
|
worker = pool;
|
2018-08-02 00:48:43 +00:00
|
|
|
LRCM = new LR_CommitMessage();
|
2018-06-21 12:55:02 +00:00
|
|
|
|
|
|
|
for(size_t idx = 0; idx < MAXHOPS; ++idx)
|
|
|
|
{
|
|
|
|
LRCM->frames.emplace_back();
|
|
|
|
LRCM->frames.back().Randomize();
|
|
|
|
}
|
|
|
|
llarp_threadpool_queue_job(pool, {this, &GenerateNextKey});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-18 22:03:50 +00:00
|
|
|
void
|
|
|
|
pathbuilder_generated_keys(
|
|
|
|
AsyncPathKeyExchangeContext< llarp_pathbuild_job >* ctx)
|
|
|
|
{
|
2018-06-20 12:34:48 +00:00
|
|
|
auto remote = ctx->path->Upstream();
|
2018-06-19 17:11:24 +00:00
|
|
|
auto router = ctx->user->router;
|
2018-06-20 12:34:48 +00:00
|
|
|
if(!router->SendToOrQueue(remote, ctx->LRCM))
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("failed to send LRCM");
|
2018-06-20 12:34:48 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-08-14 21:17:18 +00:00
|
|
|
|
2018-06-26 13:39:29 +00:00
|
|
|
ctx->path->status = llarp::path::ePathBuilding;
|
|
|
|
ctx->path->buildStarted = llarp_time_now_ms();
|
2018-08-14 21:17:18 +00:00
|
|
|
// persist session with router until this path is done
|
|
|
|
router->PersistSessionUntil(remote, ctx->path->ExpireTime());
|
|
|
|
// add own path
|
2018-06-25 15:12:08 +00:00
|
|
|
router->paths.AddOwnPath(ctx->pathset, ctx->path);
|
2018-06-19 17:11:24 +00:00
|
|
|
ctx->user->pathBuildStarted(ctx->user);
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
pathbuilder_start_build(void* user)
|
|
|
|
{
|
|
|
|
llarp_pathbuild_job* job = static_cast< llarp_pathbuild_job* >(user);
|
2018-06-19 17:11:24 +00:00
|
|
|
// select hops
|
2018-06-20 12:34:48 +00:00
|
|
|
size_t idx = 0;
|
|
|
|
llarp_rc* prev = nullptr;
|
2018-06-18 22:03:50 +00:00
|
|
|
while(idx < job->hops.numHops)
|
|
|
|
{
|
2018-06-20 12:34:48 +00:00
|
|
|
llarp_rc* rc = &job->hops.hops[idx].router;
|
2018-06-19 17:11:24 +00:00
|
|
|
llarp_rc_clear(rc);
|
2018-07-25 01:54:37 +00:00
|
|
|
if(!job->selectHop(job->user, job->router->nodedb, prev, rc, idx))
|
|
|
|
{
|
|
|
|
/// TODO: handle this failure properly
|
|
|
|
llarp::LogWarn("Failed to select hop ", idx);
|
|
|
|
return;
|
|
|
|
}
|
2018-06-20 12:34:48 +00:00
|
|
|
prev = rc;
|
2018-06-18 22:03:50 +00:00
|
|
|
++idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
// async generate keys
|
|
|
|
AsyncPathKeyExchangeContext< llarp_pathbuild_job >* ctx =
|
|
|
|
new AsyncPathKeyExchangeContext< llarp_pathbuild_job >(
|
|
|
|
&job->router->crypto);
|
2018-06-25 15:12:08 +00:00
|
|
|
ctx->pathset = job->context;
|
|
|
|
auto path = new llarp::path::Path(&job->hops);
|
|
|
|
path->SetBuildResultHook(std::bind(&llarp::path::PathSet::HandlePathBuilt,
|
|
|
|
ctx->pathset, std::placeholders::_1));
|
|
|
|
ctx->AsyncGenerateKeys(path, job->router->logic, job->router->tp, job,
|
|
|
|
&pathbuilder_generated_keys);
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
|
|
|
} // namespace llarp
|
|
|
|
|
|
|
|
llarp_pathbuilder_context::llarp_pathbuilder_context(
|
2018-07-19 21:08:11 +00:00
|
|
|
llarp_router* p_router, struct llarp_dht_context* p_dht, size_t pathNum,
|
|
|
|
size_t hops)
|
|
|
|
: llarp::path::PathSet(pathNum), router(p_router), dht(p_dht), numHops(hops)
|
2018-06-25 15:12:08 +00:00
|
|
|
{
|
|
|
|
p_router->paths.AddPathBuilder(this);
|
2018-08-02 00:48:43 +00:00
|
|
|
p_router->crypto.encryption_keygen(enckey);
|
2018-06-25 15:12:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-18 14:01:21 +00:00
|
|
|
llarp_pathbuilder_context::~llarp_pathbuilder_context()
|
|
|
|
{
|
|
|
|
router->paths.RemovePathBuilder(this);
|
|
|
|
}
|
|
|
|
|
2018-07-19 21:08:11 +00:00
|
|
|
bool
|
|
|
|
llarp_pathbuilder_context::SelectHop(llarp_nodedb* db, llarp_rc* prev,
|
|
|
|
llarp_rc* cur, size_t hop)
|
|
|
|
{
|
2018-07-25 01:36:22 +00:00
|
|
|
if(hop == 0)
|
|
|
|
return router->GetRandomConnectedRouter(cur);
|
|
|
|
else
|
|
|
|
llarp_nodedb_select_random_hop(db, prev, cur, hop);
|
2018-07-19 21:08:11 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-08-02 00:48:43 +00:00
|
|
|
byte_t*
|
|
|
|
llarp_pathbuilder_context::GetTunnelEncryptionSecretKey()
|
|
|
|
{
|
|
|
|
return enckey;
|
|
|
|
}
|
|
|
|
|
2018-06-25 15:12:08 +00:00
|
|
|
void
|
|
|
|
llarp_pathbuilder_context::BuildOne()
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
llarp_pathbuild_job* job = new llarp_pathbuild_job;
|
|
|
|
job->context = this;
|
2018-07-19 21:08:11 +00:00
|
|
|
job->selectHop = &PathSet::SelectHopCallback;
|
|
|
|
job->hops.numHops = numHops;
|
|
|
|
job->user = this;
|
2018-06-25 15:12:08 +00:00
|
|
|
job->pathBuildStarted = [](llarp_pathbuild_job* j) { delete j; };
|
|
|
|
llarp_pathbuilder_build_path(job);
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-12 17:22:29 +00:00
|
|
|
void
|
|
|
|
llarp_pathbuilder_context::ManualRebuild(size_t num)
|
|
|
|
{
|
|
|
|
while(num--)
|
|
|
|
BuildOne();
|
|
|
|
}
|
|
|
|
|
2018-06-22 00:25:51 +00:00
|
|
|
struct llarp_pathbuilder_context*
|
|
|
|
llarp_pathbuilder_context_new(struct llarp_router* router,
|
2018-07-19 21:08:11 +00:00
|
|
|
struct llarp_dht_context* dht, size_t sz,
|
|
|
|
size_t hops)
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2018-07-19 21:08:11 +00:00
|
|
|
return new llarp_pathbuilder_context(router, dht, sz, hops);
|
2018-06-22 00:25:51 +00:00
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-06-22 00:25:51 +00:00
|
|
|
void
|
|
|
|
llarp_pathbuilder_context_free(struct llarp_pathbuilder_context* ctx)
|
|
|
|
{
|
|
|
|
delete ctx;
|
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-06-22 00:25:51 +00:00
|
|
|
void
|
|
|
|
llarp_pathbuilder_build_path(struct llarp_pathbuild_job* job)
|
|
|
|
{
|
2018-06-25 15:12:08 +00:00
|
|
|
if(!job->context)
|
2018-06-19 09:19:23 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("failed to build path because no context is set in job");
|
2018-06-22 05:42:50 +00:00
|
|
|
return;
|
2018-06-19 09:19:23 +00:00
|
|
|
}
|
2018-06-22 00:25:51 +00:00
|
|
|
if(job->selectHop == nullptr)
|
2018-07-19 21:08:11 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("No callback provided for hop selection");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
job->router = job->context->router;
|
2018-06-22 00:25:51 +00:00
|
|
|
llarp_logic_queue_job(job->router->logic,
|
|
|
|
{job, &llarp::pathbuilder_start_build});
|
2018-06-19 09:19:23 +00:00
|
|
|
}
|