diff --git a/include/llarp/ip.hpp b/include/llarp/ip.hpp index 8ab3e17b2..4e0e027d5 100644 --- a/include/llarp/ip.hpp +++ b/include/llarp/ip.hpp @@ -153,9 +153,13 @@ namespace llarp Header()->daddr = htonl(ip); } - // update ip packet checksum + // update ip packet checksum (after packet gets out of network) void - UpdateChecksum(); + UpdateChecksumsOnDst(); + + // update ip packet checksum (before packet gets inserted into network) + void + UpdateChecksumsOnSrc(); }; } // namespace net diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index ab7370db0..77c5e7306 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -329,7 +329,7 @@ namespace llarp memcpy(pkt.buf, buf.base, pkt.sz); pkt.src(themIP); pkt.dst(usIP); - pkt.UpdateChecksum(); + pkt.UpdateChecksumsOnDst(); return true; })) @@ -440,11 +440,12 @@ namespace llarp // called in the isolated network thread TunEndpoint *self = static_cast< TunEndpoint * >(tun->user); self->m_NetworkToUserPktQueue.Process([self, tun](net::IPv4Packet &pkt) { + // prepare packet for insertion into network + pkg.UpdateChecksumsOnSrc(); // clear addresses pkt.src(0); pkt.dst(0); - // clear checksum - pkt.Header()->check = 0; + if(!llarp_ev_tun_async_write(tun, pkt.buf, pkt.sz)) llarp::LogWarn("packet dropped"); }); diff --git a/llarp/ip.cpp b/llarp/ip.cpp index d6464617d..28bf0b676 100644 --- a/llarp/ip.cpp +++ b/llarp/ip.cpp @@ -78,12 +78,15 @@ namespace llarp *check = ipchksum(pktbuf, 12 + pktsz); }}}; void - IPv4Packet::UpdateChecksum() + IPv4Packet::UpdateChecksumsOnDst() { + // IPv4 checksum auto hdr = Header(); hdr->check = 0; auto len = hdr->ihl * 4; hdr->check = ipchksum(buf, len); + + // L4 checksum auto proto = hdr->protocol; auto itr = protoCheckSummer.find(proto); if(itr != protoCheckSummer.end()) @@ -91,5 +94,12 @@ namespace llarp itr->second(hdr, buf, sz); } } + + void + IPv4Packet::UpdateChecksumsOnSrc() + { + // IPv4 + Header()->check = 0; + } } // namespace net } // namespace llarp