From a9dac85c2873281277dedc4ace381c9ef7170fd9 Mon Sep 17 00:00:00 2001 From: cathugger Date: Wed, 12 Jun 2019 00:27:06 +0300 Subject: [PATCH] fix stuff --- llarp/exit/endpoint.cpp | 4 +- llarp/handlers/exit.cpp | 2 +- llarp/handlers/tun.cpp | 12 +-- llarp/handlers/tun.hpp | 8 +- llarp/net/ip.cpp | 186 ++++++++++++++++++++-------------------- 5 files changed, 106 insertions(+), 106 deletions(-) diff --git a/llarp/exit/endpoint.cpp b/llarp/exit/endpoint.cpp index 78fb8b44a..68f1362ea 100644 --- a/llarp/exit/endpoint.cpp +++ b/llarp/exit/endpoint.cpp @@ -117,7 +117,7 @@ namespace llarp dst = net::IPPacket::TruncateV6(m_Parent->GetIfAddr()); else dst = pkt.dstv4(); - pkt.UpdateV4Address(net::IPPacket::TruncateV6(m_IP), dst); + pkt.UpdateIPv4Address(xhtonl(net::IPPacket::TruncateV6(m_IP)), xhtonl(dst)); m_UpstreamQueue.emplace(pkt, counter); m_TxRate += buf.underlying.sz; m_LastActive = m_Parent->Now(); @@ -136,7 +136,7 @@ namespace llarp src = m_Parent->GetIfAddr(); else src = pkt.srcv6(); - pkt.UpdateV6Address(src, m_IP); + pkt.UpdateIPv6Address(src, m_IP); const llarp_buffer_t& pktbuf = pkt.Buffer(); // life time extension const uint8_t queue_idx = pktbuf.sz / llarp::routing::ExitPadSize; if(m_DownstreamQueues.find(queue_idx) == m_DownstreamQueues.end()) diff --git a/llarp/handlers/exit.cpp b/llarp/handlers/exit.cpp index 371b88930..197bbc55b 100644 --- a/llarp/handlers/exit.cpp +++ b/llarp/handlers/exit.cpp @@ -440,7 +440,7 @@ namespace llarp if(!pkt.Load(buf)) return false; // rewrite ip - pkt.UpdateV6Address(from, m_IfAddr); + pkt.UpdateIPv6Address(from, m_IfAddr); return llarp_ev_tun_async_write(&m_Tun, pkt.Buffer()); } diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 26fd604ef..a87976d73 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -712,7 +712,7 @@ namespace llarp { if(m_Exit && pkt.IsV4() && !llarp::IsIPv4Bogon(pkt.dstv4())) { - pkt.UpdateV4Address({0}, pkt.dstv4()); + pkt.UpdateIPv4Address({0}, xhtonl(pkt.dstv4())); m_Exit->QueueUpstreamTraffic(std::move(pkt), llarp::routing::ExitPadSize); } @@ -738,9 +738,9 @@ namespace llarp // prepare packet for insertion into network // this includes clearing IP addresses, recalculating checksums, etc if(pkt.IsV4()) - pkt.UpdateV4Address({0}, {0}); + pkt.UpdateIPv4Address({0}, {0}); else - pkt.UpdateV6Address({0}, {0}); + pkt.UpdateIPv6Address({0}, {0}); if(sendFunc && sendFunc(pkt.Buffer())) return; @@ -778,13 +778,15 @@ namespace llarp { return false; } - pkt.UpdateV4Address(net::IPPacket::TruncateV6(themIP), net::IPPacket::TruncateV6(usIP)); + pkt.UpdateIPv4Address( + xhtonl(net::IPPacket::TruncateV6(themIP)), + xhtonl(net::IPPacket::TruncateV6(usIP))); } else if(pkt.IsV6()) { if(pkt.srcv6() != huint128_t{0} || pkt.dstv6() != huint128_t{0}) return false; - pkt.UpdateV6Address(themIP, usIP); + pkt.UpdateIPv6Address(themIP, usIP); } return true; }); diff --git a/llarp/handlers/tun.hpp b/llarp/handlers/tun.hpp index 48d462b50..b2e131688 100644 --- a/llarp/handlers/tun.hpp +++ b/llarp/handlers/tun.hpp @@ -209,19 +209,19 @@ namespace llarp { if(pkt.IsV4()) { - pkt.UpdateV6Address(net::IPPacket::ExpandV4(pkt.srcv4()), + pkt.UpdateIPv6Address(net::IPPacket::ExpandV4(pkt.srcv4()), m_OurIP); } else { - pkt.UpdateV6Address(pkt.srcv6(), m_OurIP); + pkt.UpdateIPv6Address(pkt.srcv6(), m_OurIP); } } else { if(pkt.IsV4()) - pkt.UpdateV4Address(pkt.srcv4(), - net::IPPacket::TruncateV6(m_OurIP)); + pkt.UpdateIPv4Address(xhtonl(pkt.srcv4()), + xhtonl(net::IPPacket::TruncateV6(m_OurIP))); else return false; } diff --git a/llarp/net/ip.cpp b/llarp/net/ip.cpp index 788dd3d5a..6f132cf57 100644 --- a/llarp/net/ip.cpp +++ b/llarp/net/ip.cpp @@ -142,97 +142,6 @@ namespace llarp #endif - void - IPPacket::UpdateIPv4Address(nuint32_t nSrcIP, nuint32_t nDstIP) - { - llarp::LogDebug("set src=", nSrcIP, " dst=", nDstIP); - - auto hdr = Header(); - - auto oSrcIP = nuint32_t{hdr->saddr}; - auto oDstIP = nuint32_t{hdr->daddr}; - - // L4 checksum - auto ihs = size_t(hdr->ihl * 4); - if(ihs <= sz) - { - auto pld = buf + ihs; - auto psz = sz - ihs; - - auto fragoff = size_t((ntohs(hdr->frag_off) & 0x1Fff) * 8); - - switch(hdr->protocol) - { - case 6: // TCP - deltaChecksumIPv4TCP(pld, psz, fragoff, 16, oSrcIP, oDstIP, nSrcIP, - nDstIP); - break; - case 17: // UDP - case 136: // UDP-Lite - same checksum place, same 0->0xFFff condition - deltaChecksumIPv4UDP(pld, psz, fragoff, oSrcIP, oDstIP, nSrcIP, - nDstIP); - break; - case 33: // DCCP - deltaChecksumIPv4TCP(pld, psz, fragoff, 6, oSrcIP, oDstIP, nSrcIP, - nDstIP); - break; - } - } - - // IPv4 checksum - auto v4chk = (nuint16_t *)&(hdr->check); - *v4chk = deltaIPv4Checksum(*v4chk, oSrcIP, oDstIP, nSrcIP, nDstIP); - - // write new IP addresses - hdr->saddr = nSrcIP.n; - hdr->daddr = nDstIP.n; - } - - void - IPPacket::UpdateIPv6Address(huint128_t src, huint128_t dst) - { - const size_t ihs = 40; - - // XXX should've been checked at upper level? - if(sz <= ihs) - return; - - auto hdr = HeaderV6(); - - const uint32_t oSrcIP = hdr->srcaddr; - const uint32_t oDstIP = hdr->dstaddr; - - // IPv6 address - hdr->srcaddr = HUIntToIn6(src); - hdr->dstaddr = HUIntToIn6(dst); - - // TODO IPv6 header options - auto pld = buf + ihs; - auto psz = sz - ihs; - - switch(hdr->proto) - { - case 6: // TCP - deltaChecksumIPv6TCP(pld, psz, fragoff, 16, &oSrcIP.s6_addr32, - &oDstIP.s6_addr32, &hdr->srcaddr.s6_addr32, - &hdr->dstaddr.s6_addr32); - break; - case 17: // UDP - case 136: // UDP-Lite - same checksum place, same 0->0xFFff condition - deltaChecksumIPv6UDP(pld, psz, fragoff, &oSrcIP.s6_addr32, - &oDstIP.s6_addr32, &hdr->srcaddr.s6_addr32, - &hdr->dstaddr.s6_addr32); - break; - case 33: // DCCP - deltaChecksumIPv6TCP(pld, psz, fragoff, 6, &oSrcIP.s6_addr32, - &oDstIP.s6_addr32, &hdr->srcaddr.s6_addr32, - &hdr->dstaddr.s6_addr32); - break; - } - } - - - #define ADD32CS(x) ((uint32_t)(x & 0xFFff) + (uint32_t)(x >> 16)) #define SUB32CS(x) ((uint32_t)((~x) & 0xFFff) + (uint32_t)((~x) >> 16)) @@ -256,8 +165,8 @@ namespace llarp static nuint16_t deltaIPv6Checksum(nuint16_t old_sum, - const uint32 old_src_ip[4], const uint32 old_dst_ip[4], - const uint32 new_src_ip[4], const uint32 new_dst_ip[4]) + const uint32_t old_src_ip[4], const uint32_t old_dst_ip[4], + const uint32_t new_src_ip[4], const uint32_t new_dst_ip[4]) { /* we don't actually care in what way integers are arranged in memory internally */ /* as long as uint16 pairs are swapped in correct direction, result will be correct (assuming there are no gaps in structure) */ @@ -265,7 +174,7 @@ namespace llarp /* we could do 64bit ints too but then we couldn't reuse 32bit macros and that'd suck for 32bit cpus */ #define ADDN128CS(x) (ADD32CS(x[0]) + ADD32CS(x[1]) + ADD32CS(x[2]) + ADD32CS(x[3])) #define SUBN128CS(x) (SUB32CS(x[0]) + SUB32CS(x[1]) + SUB32CS(x[2]) + SUB32CS(x[3])) - uint32_t sum = uint32_t(old_sum) + + uint32_t sum = uint32_t(old_sum.n) + ADDN128CS(old_src_ip) + ADDN128CS(old_dst_ip) + SUBN128CS(new_src_ip) + SUBN128CS(new_dst_ip); #undef ADDN128CS @@ -376,5 +285,94 @@ namespace llarp // if(check->n == 0x0000) // check->n = 0xFFff; } + + + void + IPPacket::UpdateIPv4Address(nuint32_t nSrcIP, nuint32_t nDstIP) + { + llarp::LogDebug("set src=", nSrcIP, " dst=", nDstIP); + + auto hdr = Header(); + + auto oSrcIP = nuint32_t{hdr->saddr}; + auto oDstIP = nuint32_t{hdr->daddr}; + + // L4 checksum + auto ihs = size_t(hdr->ihl * 4); + if(ihs <= sz) + { + auto pld = buf + ihs; + auto psz = sz - ihs; + + auto fragoff = size_t((ntohs(hdr->frag_off) & 0x1Fff) * 8); + + switch(hdr->protocol) + { + case 6: // TCP + deltaChecksumIPv4TCP(pld, psz, fragoff, 16, oSrcIP, oDstIP, nSrcIP, + nDstIP); + break; + case 17: // UDP + case 136: // UDP-Lite - same checksum place, same 0->0xFFff condition + deltaChecksumIPv4UDP(pld, psz, fragoff, oSrcIP, oDstIP, nSrcIP, + nDstIP); + break; + case 33: // DCCP + deltaChecksumIPv4TCP(pld, psz, fragoff, 6, oSrcIP, oDstIP, nSrcIP, + nDstIP); + break; + } + } + + // IPv4 checksum + auto v4chk = (nuint16_t *)&(hdr->check); + *v4chk = deltaIPv4Checksum(*v4chk, oSrcIP, oDstIP, nSrcIP, nDstIP); + + // write new IP addresses + hdr->saddr = nSrcIP.n; + hdr->daddr = nDstIP.n; + } + + void + IPPacket::UpdateIPv6Address(huint128_t src, huint128_t dst) + { + const size_t ihs = 40; + + // XXX should've been checked at upper level? + if(sz <= ihs) + return; + + auto hdr = HeaderV6(); + + const auto oldSrcIP = hdr->srcaddr; + const auto oldDstIP = hdr->dstaddr; + const uint32_t *oSrcIP = oldSrcIP.s6_addr32; + const uint32_t *oDstIP = oldDstIP.s6_addr32; + + // IPv6 address + hdr->srcaddr = HUIntToIn6(src); + hdr->dstaddr = HUIntToIn6(dst); + const uint32_t *nSrcIP = hdr->srcaddr.s6_addr32; + const uint32_t *nDstIP = hdr->dstaddr.s6_addr32; + + // TODO IPv6 header options + auto pld = buf + ihs; + auto psz = sz - ihs; + const size_t fragoff = 0; + + switch(hdr->proto) + { + case 6: // TCP + deltaChecksumIPv6TCP(pld, psz, fragoff, 16, oSrcIP, oDstIP, nSrcIP, nDstIP); + break; + case 17: // UDP + case 136: // UDP-Lite - same checksum place, same 0->0xFFff condition + deltaChecksumIPv6UDP(pld, psz, fragoff, oSrcIP, oDstIP, nSrcIP, nDstIP); + break; + case 33: // DCCP + deltaChecksumIPv6TCP(pld, psz, fragoff, 6, oSrcIP, oDstIP, nSrcIP, nDstIP); + break; + } + } } // namespace net } // namespace llarp