2018-12-12 02:52:51 +00:00
|
|
|
#include <buffer.hpp>
|
|
|
|
#include <nodedb.hpp>
|
|
|
|
#include <path.hpp>
|
|
|
|
#include <pathbuilder.hpp>
|
|
|
|
#include <router.hpp>
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-10-09 12:06:30 +00:00
|
|
|
#include <functional>
|
2018-06-18 22:03:50 +00:00
|
|
|
|
|
|
|
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-10-09 12:06:30 +00:00
|
|
|
typedef std::function< void(AsyncPathKeyExchangeContext< User >*) > Handler;
|
|
|
|
User* user = nullptr;
|
|
|
|
|
|
|
|
Handler result;
|
2018-06-21 12:55:02 +00:00
|
|
|
size_t idx = 0;
|
2018-12-10 23:29:58 +00:00
|
|
|
llarp::Router* router = nullptr;
|
2018-06-21 12:55:02 +00:00
|
|
|
llarp_threadpool* worker = nullptr;
|
2018-12-10 14:14:55 +00:00
|
|
|
llarp::Logic* logic = nullptr;
|
2018-12-13 16:14:44 +00:00
|
|
|
llarp::Crypto* crypto = nullptr;
|
2018-09-19 16:36:12 +00:00
|
|
|
LR_CommitMessage LRCM;
|
2018-06-21 12:55:02 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
HandleDone(void* u)
|
|
|
|
{
|
|
|
|
AsyncPathKeyExchangeContext< User >* ctx =
|
|
|
|
static_cast< AsyncPathKeyExchangeContext< User >* >(u);
|
|
|
|
ctx->result(ctx);
|
2018-10-09 12:06:30 +00:00
|
|
|
delete ctx;
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
2018-09-19 16:36:12 +00:00
|
|
|
auto& frame = ctx->LRCM.frames[ctx->idx];
|
2018-06-21 12:55:02 +00:00
|
|
|
// generate key
|
|
|
|
ctx->crypto->encryption_keygen(hop.commkey);
|
|
|
|
hop.nonce.Randomize();
|
|
|
|
// do key exchange
|
2018-08-30 18:48:43 +00:00
|
|
|
if(!ctx->crypto->dh_client(hop.shared, hop.rc.enckey, hop.commkey,
|
2018-06-21 12:55:02 +00:00
|
|
|
hop.nonce))
|
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("Failed to generate shared key for path build");
|
2018-09-13 11:30:21 +00:00
|
|
|
delete ctx;
|
2018-06-21 12:55:02 +00:00
|
|
|
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-12-10 17:22:59 +00:00
|
|
|
hop.upstream = hop.rc.pubkey.data();
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-10 17:22:59 +00:00
|
|
|
hop.upstream = ctx->path->hops[ctx->idx].rc.pubkey.data();
|
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-09-13 11:30:21 +00:00
|
|
|
delete ctx;
|
2018-06-21 12:55:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// use ephameral keypair for frame
|
|
|
|
SecretKey framekey;
|
|
|
|
ctx->crypto->encryption_keygen(framekey);
|
2018-08-30 18:48:43 +00:00
|
|
|
if(!frame.EncryptInPlace(framekey, hop.rc.enckey, ctx->crypto))
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
2018-07-05 15:44:06 +00:00
|
|
|
llarp::LogError("Failed to encrypt LRCR");
|
2018-09-13 11:30:21 +00:00
|
|
|
delete ctx;
|
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
|
2018-12-10 14:14:55 +00:00
|
|
|
ctx->logic->queue_job({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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 00:53:11 +00:00
|
|
|
AsyncPathKeyExchangeContext(llarp::Crypto* c) : crypto(c)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate all keys asynchronously and call hadler when done
|
|
|
|
void
|
2018-12-10 14:14:55 +00:00
|
|
|
AsyncGenerateKeys(Path_t* p, llarp::Logic* l, llarp_threadpool* pool,
|
2018-06-25 15:12:08 +00:00
|
|
|
User* u, Handler func)
|
2018-06-21 12:55:02 +00:00
|
|
|
{
|
|
|
|
path = p;
|
|
|
|
logic = l;
|
|
|
|
user = u;
|
|
|
|
result = func;
|
|
|
|
worker = pool;
|
|
|
|
|
|
|
|
for(size_t idx = 0; idx < MAXHOPS; ++idx)
|
|
|
|
{
|
2018-09-19 16:36:12 +00:00
|
|
|
LRCM.frames[idx].Randomize();
|
2018-06-21 12:55:02 +00:00
|
|
|
}
|
|
|
|
llarp_threadpool_queue_job(pool, {this, &GenerateNextKey});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-18 22:03:50 +00:00
|
|
|
void
|
2018-08-30 18:48:43 +00:00
|
|
|
pathbuilder_generated_keys(AsyncPathKeyExchangeContext< path::Builder >* ctx)
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2018-12-07 13:38:49 +00:00
|
|
|
RouterID remote = ctx->path->Upstream();
|
2018-11-08 15:15:02 +00:00
|
|
|
const ILinkMessage* msg = &ctx->LRCM;
|
2018-12-07 13:38:49 +00:00
|
|
|
if(!ctx->router->SendToOrQueue(remote, msg))
|
2018-06-20 12:34:48 +00:00
|
|
|
{
|
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
|
|
|
|
|
|
|
// persist session with router until this path is done
|
2018-12-07 13:38:49 +00:00
|
|
|
ctx->router->PersistSessionUntil(remote, ctx->path->ExpireTime());
|
2018-08-14 21:17:18 +00:00
|
|
|
// add own path
|
2018-12-07 13:38:49 +00:00
|
|
|
ctx->router->paths.AddOwnPath(ctx->pathset, ctx->path);
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
namespace path
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2018-12-10 16:26:46 +00:00
|
|
|
Builder::Builder(llarp::Router* p_router, struct llarp_dht_context* p_dht,
|
2018-08-30 18:48:43 +00:00
|
|
|
size_t pathNum, size_t hops)
|
|
|
|
: llarp::path::PathSet(pathNum)
|
|
|
|
, router(p_router)
|
|
|
|
, dht(p_dht)
|
|
|
|
, numHops(hops)
|
2018-06-18 22:03:50 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
p_router->paths.AddPathBuilder(this);
|
|
|
|
p_router->crypto.encryption_keygen(enckey);
|
2018-06-18 22:03:50 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
Builder::~Builder()
|
|
|
|
{
|
|
|
|
router->paths.RemovePathBuilder(this);
|
|
|
|
}
|
2018-08-02 00:48:43 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
bool
|
|
|
|
Builder::SelectHop(llarp_nodedb* db, const RouterContact& prev,
|
2018-11-14 18:02:27 +00:00
|
|
|
RouterContact& cur, size_t hop, PathRole roles)
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
2018-11-15 13:47:46 +00:00
|
|
|
(void)roles;
|
2018-11-29 14:18:53 +00:00
|
|
|
if(hop == 0)
|
2018-12-02 18:07:07 +00:00
|
|
|
return router->NumberOfConnectedRouters()
|
|
|
|
&& router->GetRandomConnectedRouter(cur);
|
2018-10-10 11:48:44 +00:00
|
|
|
|
2018-10-04 17:51:45 +00:00
|
|
|
size_t tries = 5;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
--tries;
|
2018-12-10 23:29:58 +00:00
|
|
|
if(db->select_random_hop(prev, cur, hop))
|
2018-11-29 14:18:53 +00:00
|
|
|
return true;
|
2018-10-04 17:51:45 +00:00
|
|
|
} while(router->routerProfiling.IsBad(cur.pubkey) && tries > 0);
|
2018-11-29 14:18:53 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
2018-08-23 15:19:16 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
const byte_t*
|
|
|
|
Builder::GetTunnelEncryptionSecretKey() const
|
|
|
|
{
|
|
|
|
return enckey;
|
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
bool
|
2018-10-29 16:48:36 +00:00
|
|
|
Builder::ShouldBuildMore(llarp_time_t now) const
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
2018-10-29 16:48:36 +00:00
|
|
|
return llarp::path::PathSet::ShouldBuildMore(now) && now > lastBuild
|
2018-09-26 13:04:25 +00:00
|
|
|
&& now - lastBuild > buildIntervalLimit;
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
2018-08-12 17:22:29 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
void
|
2018-11-14 18:02:27 +00:00
|
|
|
Builder::BuildOne(PathRole roles)
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
2018-08-31 14:41:04 +00:00
|
|
|
std::vector< RouterContact > hops;
|
2018-11-14 18:02:27 +00:00
|
|
|
if(SelectHops(router->nodedb, hops, roles))
|
|
|
|
Build(hops, roles);
|
2018-09-28 15:46:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Builder::SelectHops(llarp_nodedb* nodedb,
|
2018-11-14 18:02:27 +00:00
|
|
|
std::vector< RouterContact >& hops, PathRole roles)
|
2018-09-28 15:46:47 +00:00
|
|
|
{
|
2018-09-06 11:46:19 +00:00
|
|
|
hops.resize(numHops);
|
2018-08-30 18:48:43 +00:00
|
|
|
size_t idx = 0;
|
|
|
|
while(idx < numHops)
|
|
|
|
{
|
|
|
|
if(idx == 0)
|
|
|
|
{
|
2018-11-14 18:02:27 +00:00
|
|
|
if(!SelectHop(nodedb, hops[0], hops[0], 0, roles))
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("failed to select first hop");
|
2018-09-28 15:46:47 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-11-14 18:02:27 +00:00
|
|
|
if(!SelectHop(nodedb, hops[idx - 1], hops[idx], idx, roles))
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
|
|
|
/// TODO: handle this failure properly
|
|
|
|
llarp::LogWarn("Failed to select hop ", idx);
|
2018-09-28 15:46:47 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
++idx;
|
|
|
|
}
|
2018-09-28 15:46:47 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-29 16:48:36 +00:00
|
|
|
llarp_time_t
|
|
|
|
Builder::Now() const
|
|
|
|
{
|
|
|
|
return router->Now();
|
|
|
|
}
|
|
|
|
|
2018-09-28 15:46:47 +00:00
|
|
|
void
|
2018-11-14 18:02:27 +00:00
|
|
|
Builder::Build(const std::vector< RouterContact >& hops, PathRole roles)
|
2018-09-28 15:46:47 +00:00
|
|
|
{
|
2018-10-29 16:48:36 +00:00
|
|
|
lastBuild = Now();
|
2018-08-30 18:48:43 +00:00
|
|
|
// async generate keys
|
|
|
|
AsyncPathKeyExchangeContext< Builder >* ctx =
|
|
|
|
new AsyncPathKeyExchangeContext< Builder >(&router->crypto);
|
2018-12-07 13:38:49 +00:00
|
|
|
ctx->router = router;
|
2018-08-30 18:48:43 +00:00
|
|
|
ctx->pathset = this;
|
2018-11-14 18:02:27 +00:00
|
|
|
auto path = new llarp::path::Path(hops, this, roles);
|
2018-09-26 13:04:25 +00:00
|
|
|
path->SetBuildResultHook(std::bind(&llarp::path::Builder::HandlePathBuilt,
|
|
|
|
this, std::placeholders::_1));
|
2018-08-30 18:48:43 +00:00
|
|
|
ctx->AsyncGenerateKeys(path, router->logic, router->tp, this,
|
|
|
|
&pathbuilder_generated_keys);
|
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-09-26 13:04:25 +00:00
|
|
|
void
|
|
|
|
Builder::HandlePathBuilt(Path* p)
|
|
|
|
{
|
|
|
|
buildIntervalLimit = MIN_PATH_BUILD_INTERVAL;
|
|
|
|
PathSet::HandlePathBuilt(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Builder::HandlePathBuildTimeout(Path* p)
|
|
|
|
{
|
|
|
|
// linear backoff
|
|
|
|
buildIntervalLimit += 1000;
|
|
|
|
PathSet::HandlePathBuildTimeout(p);
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
void
|
2018-11-14 18:02:27 +00:00
|
|
|
Builder::ManualRebuild(size_t num, PathRole roles)
|
2018-08-30 18:48:43 +00:00
|
|
|
{
|
|
|
|
llarp::LogDebug("manual rebuild ", num);
|
|
|
|
while(num--)
|
2018-11-14 18:02:27 +00:00
|
|
|
BuildOne(roles);
|
2018-08-30 18:48:43 +00:00
|
|
|
}
|
2018-06-18 22:03:50 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
} // namespace path
|
|
|
|
} // namespace llarp
|