From b4af87aa426c0f27a71519f810a3c8b3495669ae Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 31 Jul 2019 08:26:11 -0400 Subject: [PATCH 1/8] have IPPacket::srcv6 and IPPacket::dstv6 check for ipv4 packets and expand them to ipv6 --- llarp/handlers/exit.cpp | 4 ++-- llarp/net/ip.cpp | 10 ++++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index 33b7970d4..18524ceec 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -242,12 +242,12 @@ namespace llarp m_InetToNetwork.Process([&](Pkt_t &pkt) { PubKey pk; { - auto itr = m_IPToKey.find(net::IPPacket::ExpandV4(pkt.dstv4())); + auto itr = m_IPToKey.find(pkt.dstv6()); if(itr == m_IPToKey.end()) { // drop LogWarn(Name(), " dropping packet, has no session at ", - pkt.dstv4()); + pkt.dstv6()); return; } pk = itr->second; diff --git a/llarp/net/ip.cpp b/llarp/net/ip.cpp index 0b04b8456..8723d7636 100644 --- a/llarp/net/ip.cpp +++ b/llarp/net/ip.cpp @@ -69,13 +69,19 @@ namespace llarp huint128_t IPPacket::srcv6() const { - return In6ToHUInt(HeaderV6()->srcaddr); + if(IsV6()) + return In6ToHUInt(HeaderV6()->srcaddr); + else + return ExpandV4(srcv4()); } huint128_t IPPacket::dstv6() const { - return In6ToHUInt(HeaderV6()->dstaddr); + if(IsV6()) + return In6ToHUInt(HeaderV6()->dstaddr); + else + return ExpandV4(dstv4()); } bool From aa0a79568902fb81996fd355a504a6eee3f354f3 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Wed, 31 Jul 2019 08:51:24 -0400 Subject: [PATCH 2/8] call path build fail stuff in logic thread --- llarp/path/path.cpp | 13 ++++++++----- llarp/path/pathbuilder.cpp | 18 ++++++++++++++++-- llarp/path/pathbuilder.hpp | 6 ++++++ llarp/path/pathset.cpp | 8 ++++++++ llarp/path/pathset.hpp | 3 +++ 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/llarp/path/path.cpp b/llarp/path/path.cpp index 43761a923..c3585cff1 100644 --- a/llarp/path/path.cpp +++ b/llarp/path/path.cpp @@ -106,7 +106,7 @@ namespace llarp Path::HandleLRSM(uint64_t status, std::array< EncryptedFrame, 8 >& frames, AbstractRouter* r) { - uint64_t currentStatus = LR_StatusRecord::SUCCESS; + uint64_t currentStatus = status; size_t index = 0; while(index < hops.size()) @@ -143,7 +143,7 @@ namespace llarp ++index; } - if((currentStatus & LR_StatusRecord::SUCCESS) == 1) + if(currentStatus & LR_StatusRecord::SUCCESS) { llarp::LogDebug("LR_Status message processed, path build successful"); auto self = shared_from_this(); @@ -153,7 +153,7 @@ namespace llarp { r->routerProfiling().MarkPathFail(this); - llarp::LogInfo("LR_Status message processed, path build failed"); + llarp::LogDebug("LR_Status message processed, path build failed"); if(currentStatus & LR_StatusRecord::FAIL_TIMEOUT) { @@ -197,8 +197,9 @@ namespace llarp { llarp::LogDebug("Path build failed for an unspecified reason"); } - - EnterState(ePathFailed, r->Now()); + auto self = shared_from_this(); + r->logic()->queue_func( + [=]() { self->EnterState(ePathFailed, r->Now()); }); } // TODO: meaningful return value? @@ -211,6 +212,8 @@ namespace llarp if(st == ePathFailed) { _status = st; + m_PathSet->HandlePathBuildFailed(shared_from_this()); + return; } else if(st == ePathExpired && _status == ePathBuilding) { diff --git a/llarp/path/pathbuilder.cpp b/llarp/path/pathbuilder.cpp index 6a94a356c..e0d1b4b11 100644 --- a/llarp/path/pathbuilder.cpp +++ b/llarp/path/pathbuilder.cpp @@ -442,15 +442,29 @@ namespace llarp } void - Builder::HandlePathBuildTimeout(Path_ptr p) + Builder::HandlePathBuildFailed(Path_ptr p) + { + router->routerProfiling().MarkPathFail(p.get()); + PathSet::HandlePathBuildFailed(p); + DoPathBuildBackoff(); + } + + void + Builder::DoPathBuildBackoff() { // linear backoff static constexpr llarp_time_t MaxBuildInterval = 30 * 1000; buildIntervalLimit = std::min( MIN_PATH_BUILD_INTERVAL + buildIntervalLimit, MaxBuildInterval); + LogWarn(Name(), " build interval is now ", buildIntervalLimit); + } + + void + Builder::HandlePathBuildTimeout(Path_ptr p) + { router->routerProfiling().MarkPathFail(p.get()); PathSet::HandlePathBuildTimeout(p); - LogWarn(Name(), " build interval is now ", buildIntervalLimit); + DoPathBuildBackoff(); } void diff --git a/llarp/path/pathbuilder.hpp b/llarp/path/pathbuilder.hpp index 5d47b3801..9d17ea561 100644 --- a/llarp/path/pathbuilder.hpp +++ b/llarp/path/pathbuilder.hpp @@ -27,6 +27,9 @@ namespace llarp UrgentBuild(llarp_time_t now) const; private: + void + DoPathBuildBackoff(); + bool DoUrgentBuildAlignedTo(const RouterID remote, std::vector< RouterContact >& hops); @@ -115,6 +118,9 @@ namespace llarp virtual void HandlePathBuildTimeout(Path_ptr p) override; + + virtual void + HandlePathBuildFailed(Path_ptr p) override; }; using Builder_ptr = std::shared_ptr< Builder >; diff --git a/llarp/path/pathset.cpp b/llarp/path/pathset.cpp index a752ed4f6..122948a4f 100644 --- a/llarp/path/pathset.cpp +++ b/llarp/path/pathset.cpp @@ -291,6 +291,14 @@ namespace llarp m_BuildStats.timeouts++; } + void + PathSet::HandlePathBuildFailed(Path_ptr p) + { + LogWarn(Name(), " path build ", p->HopsString(), " failed"); + m_BuildStats.fails ++; + } + + void PathSet::PathBuildStarted(Path_ptr p) { diff --git a/llarp/path/pathset.hpp b/llarp/path/pathset.hpp index e8ddd6e5c..1acb8e7ca 100644 --- a/llarp/path/pathset.hpp +++ b/llarp/path/pathset.hpp @@ -128,6 +128,9 @@ namespace llarp virtual void HandlePathBuildTimeout(Path_ptr path); + virtual void + HandlePathBuildFailed(Path_ptr path); + virtual void PathBuildStarted(Path_ptr path); From f37552bbd317cddad926523005f1232b0b78623f Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 1 Aug 2019 08:20:07 -0400 Subject: [PATCH 3/8] properly close dropped inbound sessions on utp --- llarp/utp/linklayer.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/llarp/utp/linklayer.cpp b/llarp/utp/linklayer.cpp index 591a7ce03..99dd17624 100644 --- a/llarp/utp/linklayer.cpp +++ b/llarp/utp/linklayer.cpp @@ -32,6 +32,8 @@ namespace llarp Session* session = static_cast< Session* >(utp_get_userdata(arg->socket)); if(session && l) session->OutboundLinkEstablished(l); + else + utp_close(arg->socket); return 0; } @@ -59,6 +61,10 @@ namespace llarp link->HandleTimeout(session); session->Close(); } + else + { + utp_close(arg->socket); + } return 0; } @@ -319,15 +325,18 @@ namespace llarp LinkLayer* self = static_cast< LinkLayer* >(utp_context_get_userdata(arg->context)); Addr remote(*arg->address); - LogDebug("utp accepted from ", remote); + std::shared_ptr< ILinkSession > session = std::make_shared< InboundSession >(self, arg->socket, remote); if(!self->PutSession(session)) { + LogWarn("dropping inbound utp session from ", remote); + utp_close(arg->socket); session->Close(); } else { + LogDebug("utp accepted from ", remote); session->OnLinkEstablished(self); } return 0; From a08f79aecdec1b807c26ad2ac752ed844eb836b4 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 1 Aug 2019 08:20:35 -0400 Subject: [PATCH 4/8] remove limit --- libutp/utp_internal.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libutp/utp_internal.cpp b/libutp/utp_internal.cpp index 9d6dee2aa..1529fce64 100644 --- a/libutp/utp_internal.cpp +++ b/libutp/utp_internal.cpp @@ -3416,7 +3416,7 @@ utp_process_udp(utp_context *ctx, const byte *buffer, size_t len, return 1; } - + /* if(ctx->utp_sockets->GetCount() > 3000) { #if UTP_DEBUG_LOGGING @@ -3427,6 +3427,7 @@ utp_process_udp(utp_context *ctx, const byte *buffer, size_t len, return 1; } + */ // true means yes, block connection. false means no, don't block. if(utp_call_on_firewall(ctx, to, tolen)) { From 2261885206b504ee47b19d6714ed9a535a310879 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 1 Aug 2019 08:20:51 -0400 Subject: [PATCH 5/8] mark addresses as active when we use them --- llarp/handlers/tun.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index f85d483b0..3a0b4f9ca 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -759,7 +759,10 @@ namespace llarp pkt.UpdateIPv6Address({0}, {0}); if(sendFunc && sendFunc(pkt.Buffer())) + { + MarkIPActive(dst); return; + } llarp::LogWarn(Name(), " did not flush packets"); }); } From 4cfc3481dc0a23037f192f80a27e0478df63f48c Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 1 Aug 2019 08:28:26 -0400 Subject: [PATCH 6/8] remove session->Close() --- llarp/utp/linklayer.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/llarp/utp/linklayer.cpp b/llarp/utp/linklayer.cpp index 99dd17624..267296cb2 100644 --- a/llarp/utp/linklayer.cpp +++ b/llarp/utp/linklayer.cpp @@ -332,7 +332,6 @@ namespace llarp { LogWarn("dropping inbound utp session from ", remote); utp_close(arg->socket); - session->Close(); } else { From a9f524383a9bb2ad72acd7e9de77be279082d780 Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 1 Aug 2019 08:33:54 -0400 Subject: [PATCH 7/8] close dropped session later in event loop --- llarp/utp/linklayer.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/llarp/utp/linklayer.cpp b/llarp/utp/linklayer.cpp index 267296cb2..577cf6b0f 100644 --- a/llarp/utp/linklayer.cpp +++ b/llarp/utp/linklayer.cpp @@ -331,7 +331,10 @@ namespace llarp if(!self->PutSession(session)) { LogWarn("dropping inbound utp session from ", remote); - utp_close(arg->socket); + // close later + self->m_Logic->call_later(50, [=]() { + session->Close(); + }); } else { From 8329aa0ee6904f919491212478f88bb8e34d1b6e Mon Sep 17 00:00:00 2001 From: Jeff Becker Date: Thu, 1 Aug 2019 14:18:36 -0400 Subject: [PATCH 8/8] always rewrite address --- llarp/handlers/tun.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 3a0b4f9ca..b575289d8 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -802,8 +802,6 @@ namespace llarp } else if(pkt.IsV6()) { - if(pkt.srcv6() != huint128_t{0} || pkt.dstv6() != huint128_t{0}) - return false; pkt.UpdateIPv6Address(themIP, usIP); } return true;