From 05e6c3123831bf4ceb74cd03d2beb5e20529f7cc Mon Sep 17 00:00:00 2001 From: cathugger Date: Wed, 10 Oct 2018 12:06:28 +0000 Subject: [PATCH] ip: some fixes and tweaks --- include/llarp/ip.hpp | 12 ++++----- llarp/handlers/tun.cpp | 1 + llarp/ip.cpp | 57 +++++++++++++++++++++++++++--------------- 3 files changed, 44 insertions(+), 26 deletions(-) diff --git a/include/llarp/ip.hpp b/include/llarp/ip.hpp index 4e0e027d5..2e5fcf3ec 100644 --- a/include/llarp/ip.hpp +++ b/include/llarp/ip.hpp @@ -117,37 +117,37 @@ namespace llarp } }; - ip_header* + inline ip_header* Header() { return (ip_header*)&buf[0]; } - const ip_header* + inline const ip_header* Header() const { return (ip_header*)&buf[0]; } - uint32_t + inline uint32_t src() { return ntohl(Header()->saddr); } - uint32_t + inline uint32_t dst() { return ntohl(Header()->daddr); } - void + inline void src(uint32_t ip) { Header()->saddr = htonl(ip); } - void + inline void dst(uint32_t ip) { Header()->daddr = htonl(ip); diff --git a/llarp/handlers/tun.cpp b/llarp/handlers/tun.cpp index 80efebb1f..716e9a823 100644 --- a/llarp/handlers/tun.cpp +++ b/llarp/handlers/tun.cpp @@ -1,3 +1,4 @@ +#include // harmless on other platforms #define __USE_MINGW_ANSI_STDIO 1 #include diff --git a/llarp/ip.cpp b/llarp/ip.cpp index f725e3577..6472e143e 100644 --- a/llarp/ip.cpp +++ b/llarp/ip.cpp @@ -8,6 +8,7 @@ #endif #include #include +#include namespace llarp { @@ -57,14 +58,9 @@ namespace llarp } static uint16_t - deltachksum(uint16_t old_sum, uint32_t old_src_ip_n, uint32_t old_dst_ip_n, - uint32_t new_src_ip_n, uint32_t new_dst_ip_n) + deltachksum(uint16_t old_sum, uint32_t old_src_ip_h, uint32_t old_dst_ip_h, + uint32_t new_src_ip_h, uint32_t new_dst_ip_h) { - uint32_t old_src_ip_h = htonl(old_src_ip_n); - uint32_t old_dst_ip_h = htonl(old_dst_ip_n); - uint32_t new_src_ip_h = htonl(new_src_ip_n); - uint32_t new_dst_ip_h = htonl(new_dst_ip_n); - #define ADDIPCS(x) ((uint32_t)(x & 0xFFFF) + (uint32_t)(x >> 16)) #define SUBIPCS(x) ((uint32_t)((~x) & 0xFFFF) + (uint32_t)((~x) >> 16)) @@ -89,20 +85,26 @@ namespace llarp {// TCP 6, [](const ip_header *hdr, byte_t *pkt, size_t sz) { - auto hlen = size_t(hdr->ihl * 4); + auto hlen = size_t(hdr->ihl * 4); + uint16_t *check = (uint16_t *)(pkt + hlen + 16); - *check = deltachksum(*check, 0, 0, hdr->saddr, hdr->daddr); + + *check = deltachksum(*check, 0, 0, ntohl(hdr->saddr), + ntohl(hdr->daddr)); }}, {// UDP 17, [](const ip_header *hdr, byte_t *pkt, size_t sz) { + auto hlen = size_t(hdr->ihl * 4); + uint16_t *check = (uint16_t *)(pkt + hlen + 16); if(*check != 0xFFff) { if(*check == 0x0000) return; // don't change zero - *check = deltachksum(*check, 0, 0, hdr->saddr, hdr->daddr); + *check = deltachksum(*check, 0, 0, ntohl(hdr->saddr), + ntohl(hdr->daddr)); if(*check == 0x0000) *check = 0xFFff; } @@ -110,19 +112,23 @@ namespace llarp { // such checksum can mean 2 things: 0x0000 or 0xFFff // we can only know by looking at data :< - auto hlen = size_t(hdr->ihl * 4); if(hlen > sz) return; // malformed, bail out + auto oldcs = *check; + + *check = 0; // zero checksum before calculation + auto cs = ipchksum(pkt + hlen, sz - hlen, ipchksum_pseudoIPv4(0, 0, 17, sz - hlen)); - auto mod_cs = deltachksum(cs, 0, 0, hdr->saddr, hdr->daddr); + auto mod_cs = deltachksum(cs, 0, 0, ntohl(hdr->saddr), + ntohl(hdr->daddr)); if(cs != 0x0000 && cs != 0xFFff) { // packet was bad - sabotage new checksum - mod_cs += cs - *check; + mod_cs += cs - oldcs; } // 0x0000 is reserved for no checksum if(mod_cs == 0x0000) @@ -139,7 +145,8 @@ namespace llarp auto hdr = Header(); // IPv4 checksum - hdr->check = deltachksum(hdr->check, 0, 0, hdr->saddr, hdr->daddr); + hdr->check = + deltachksum(hdr->check, 0, 0, ntohl(hdr->saddr), ntohl(hdr->daddr)); // L4 checksum auto proto = hdr->protocol; @@ -159,18 +166,23 @@ namespace llarp auto hlen = size_t(hdr->ihl * 4); uint16_t *check = (uint16_t *)(pkt + hlen + 16); - *check = deltachksum(*check, hdr->saddr, hdr->daddr, 0, 0); + + *check = deltachksum(*check, ntohl(hdr->saddr), + ntohl(hdr->daddr), 0, 0); }}, {// UDP 17, [](const ip_header *hdr, byte_t *pkt, size_t sz) { + auto hlen = size_t(hdr->ihl * 4); + uint16_t *check = (uint16_t *)(pkt + hlen + 16); if(*check != 0xFFff) { if(*check == 0x0000) return; // don't change zero - *check = deltachksum(*check, hdr->saddr, hdr->daddr, 0, 0); + *check = deltachksum(*check, ntohl(hdr->saddr), + ntohl(hdr->daddr), 0, 0); if(*check == 0x0000) *check = 0xFFff; } @@ -178,20 +190,24 @@ namespace llarp { // such checksum can mean 2 things: 0x0000 or 0xFFff // we can only know by looking at data :< - auto hlen = size_t(hdr->ihl * 4); if(hlen > sz) return; // malformed, bail out + auto oldcs = *check; + + *check = 0; // zero checksum before calculation + auto cs = ipchksum(pkt + hlen, sz - hlen, ipchksum_pseudoIPv4(hdr->saddr, hdr->daddr, 17, sz - hlen)); - auto mod_cs = deltachksum(cs, hdr->saddr, hdr->daddr, 0, 0); + auto mod_cs = deltachksum(cs, ntohl(hdr->saddr), + ntohl(hdr->daddr), 0, 0); if(cs != 0x0000 && cs != 0xFFff) { // packet was bad - sabotage new checksum - mod_cs += cs - *check; + mod_cs += cs - oldcs; } // 0x0000 is reserved for no checksum if(mod_cs == 0x0000) @@ -215,7 +231,8 @@ namespace llarp } // IPv4 - hdr->check = deltachksum(hdr->check, hdr->saddr, hdr->daddr, 0, 0); + hdr->check = + deltachksum(hdr->check, ntohl(hdr->saddr), ntohl(hdr->daddr), 0, 0); } } // namespace net } // namespace llarp