2019-01-14 21:46:07 +00:00
|
|
|
#include <net/ip.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
|
|
|
#include <util/buffer.hpp>
|
|
|
|
#include <util/endian.hpp>
|
|
|
|
#include <util/mem.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-09-18 23:44:23 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
2018-12-12 00:33:54 +00:00
|
|
|
|
2018-10-10 12:06:28 +00:00
|
|
|
#include <algorithm>
|
2018-12-12 00:33:54 +00:00
|
|
|
#include <map>
|
2018-08-17 19:49:58 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace net
|
|
|
|
{
|
2018-08-20 19:12:12 +00:00
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
IPv4Packet::Load(const llarp_buffer_t &pkt)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2018-10-13 18:57:31 +00:00
|
|
|
if(pkt.sz > sizeof(buf))
|
|
|
|
return false;
|
|
|
|
sz = pkt.sz;
|
2018-08-22 15:52:10 +00:00
|
|
|
memcpy(buf, pkt.base, sz);
|
2018-08-20 19:12:12 +00:00
|
|
|
return true;
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
2018-08-20 19:12:12 +00:00
|
|
|
|
2018-11-29 21:19:20 +00:00
|
|
|
llarp_buffer_t
|
2018-12-02 18:07:07 +00:00
|
|
|
IPv4Packet::ConstBuffer() const
|
2018-11-29 21:19:20 +00:00
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
return {buf, sz};
|
2018-11-29 21:19:20 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 15:52:10 +00:00
|
|
|
llarp_buffer_t
|
|
|
|
IPv4Packet::Buffer()
|
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
return {buf, sz};
|
2018-08-22 15:52:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-13 15:42:47 +00:00
|
|
|
#if 0
|
2018-10-09 17:02:49 +00:00
|
|
|
static uint32_t
|
2018-10-10 15:14:45 +00:00
|
|
|
ipchksum_pseudoIPv4(nuint32_t src_ip, nuint32_t dst_ip, uint8_t proto,
|
2018-10-09 17:09:45 +00:00
|
|
|
uint16_t innerlen)
|
2018-10-09 17:02:49 +00:00
|
|
|
{
|
2018-10-13 15:42:47 +00:00
|
|
|
#define IPCS(x) ((uint32_t)(x & 0xFFff) + (uint32_t)(x >> 16))
|
2018-10-10 15:14:45 +00:00
|
|
|
uint32_t sum = IPCS(src_ip.n) + IPCS(dst_ip.n) + (uint32_t)proto
|
2018-10-09 21:56:20 +00:00
|
|
|
+ (uint32_t)htons(innerlen);
|
2018-10-09 17:09:45 +00:00
|
|
|
#undef IPCS
|
2018-10-09 17:02:49 +00:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2018-09-16 12:06:19 +00:00
|
|
|
static uint16_t
|
2018-09-18 11:08:47 +00:00
|
|
|
ipchksum(const byte_t *buf, size_t sz, uint32_t sum = 0)
|
2018-09-16 12:06:19 +00:00
|
|
|
{
|
|
|
|
while(sz > 1)
|
2018-08-20 19:12:12 +00:00
|
|
|
{
|
2018-09-16 12:06:19 +00:00
|
|
|
sum += *(const uint16_t *)buf;
|
|
|
|
sz -= sizeof(uint16_t);
|
|
|
|
buf += sizeof(uint16_t);
|
2018-08-20 19:12:12 +00:00
|
|
|
}
|
2018-10-13 15:42:47 +00:00
|
|
|
if(sz != 0)
|
|
|
|
{
|
2018-10-13 17:28:35 +00:00
|
|
|
uint16_t x = 0;
|
|
|
|
|
2018-10-13 15:42:47 +00:00
|
|
|
*(byte_t *)&x = *(const byte_t *)buf;
|
|
|
|
sum += x;
|
|
|
|
}
|
2018-08-20 19:12:12 +00:00
|
|
|
|
2018-10-13 17:28:35 +00:00
|
|
|
// only need to do it 2 times to be sure
|
|
|
|
// proof: 0xFFff + 0xFFff = 0x1FFfe -> 0xFFff
|
|
|
|
sum = (sum & 0xFFff) + (sum >> 16);
|
|
|
|
sum += sum >> 16;
|
2018-08-20 19:12:12 +00:00
|
|
|
|
2018-10-13 17:28:35 +00:00
|
|
|
return uint16_t((~sum) & 0xFFff);
|
2018-09-16 12:06:19 +00:00
|
|
|
}
|
2018-10-13 15:42:47 +00:00
|
|
|
#endif
|
2018-09-16 12:06:19 +00:00
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
static nuint16_t
|
|
|
|
deltaIPv4Checksum(nuint16_t old_sum, nuint32_t old_src_ip,
|
|
|
|
nuint32_t old_dst_ip, nuint32_t new_src_ip,
|
|
|
|
nuint32_t new_dst_ip)
|
2018-10-09 17:02:49 +00:00
|
|
|
{
|
2018-11-01 17:15:01 +00:00
|
|
|
#define ADDIPCS(x) ((uint32_t)(x.n & 0xFFff) + (uint32_t)(x.n >> 16))
|
|
|
|
#define SUBIPCS(x) ((uint32_t)((~x.n) & 0xFFff) + (uint32_t)((~x.n) >> 16))
|
2018-10-10 01:59:52 +00:00
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
uint32_t sum = uint32_t(old_sum.n) + ADDIPCS(old_src_ip)
|
|
|
|
+ ADDIPCS(old_dst_ip) + SUBIPCS(new_src_ip) + SUBIPCS(new_dst_ip);
|
2018-10-10 01:29:44 +00:00
|
|
|
|
2018-10-09 21:56:20 +00:00
|
|
|
#undef ADDIPCS
|
|
|
|
#undef SUBIPCS
|
2018-10-10 01:29:44 +00:00
|
|
|
|
2018-10-13 17:28:35 +00:00
|
|
|
// only need to do it 2 times to be sure
|
|
|
|
// proof: 0xFFff + 0xFFff = 0x1FFfe -> 0xFFff
|
|
|
|
sum = (sum & 0xFFff) + (sum >> 16);
|
|
|
|
sum += sum >> 16;
|
2018-10-10 01:29:44 +00:00
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
return nuint16_t{uint16_t(sum & 0xFFff)};
|
2018-10-09 17:02:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
static void
|
2018-11-07 15:30:22 +00:00
|
|
|
checksumDstIPv4TCP(byte_t *pld, __attribute__((unused)) size_t psz,
|
|
|
|
size_t fragoff, size_t chksumoff, nuint32_t oSrcIP,
|
|
|
|
nuint32_t oDstIP, nuint32_t nSrcIP, nuint32_t nDstIP)
|
2018-10-12 04:35:10 +00:00
|
|
|
{
|
2018-10-14 21:56:14 +00:00
|
|
|
if(fragoff > chksumoff)
|
2018-10-13 15:42:47 +00:00
|
|
|
return;
|
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
auto check = (nuint16_t *)(pld + chksumoff - fragoff);
|
2018-10-12 04:35:10 +00:00
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
*check = deltaIPv4Checksum(*check, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2018-10-14 21:56:14 +00:00
|
|
|
// usually, TCP checksum field cannot be 0xFFff,
|
|
|
|
// because one's complement addition cannot result in 0x0000,
|
|
|
|
// and there's inversion in the end;
|
|
|
|
// emulate that.
|
2018-11-01 17:15:01 +00:00
|
|
|
if(check->n == 0xFFff)
|
|
|
|
check->n = 0x0000;
|
2018-10-12 04:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-11-07 15:30:22 +00:00
|
|
|
checksumDstIPv4UDP(byte_t *pld, __attribute__((unused)) size_t psz,
|
|
|
|
size_t fragoff, nuint32_t oSrcIP, nuint32_t oDstIP,
|
|
|
|
nuint32_t nSrcIP, nuint32_t nDstIP)
|
2018-10-12 04:35:10 +00:00
|
|
|
{
|
2018-10-13 15:42:47 +00:00
|
|
|
if(fragoff > 6)
|
|
|
|
return;
|
2018-10-12 04:35:10 +00:00
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
auto check = (nuint16_t *)(pld + 6);
|
|
|
|
if(check->n == 0x0000)
|
2018-10-13 15:42:47 +00:00
|
|
|
return; // 0 is used to indicate "no checksum", don't change
|
2018-10-10 02:38:59 +00:00
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
*check = deltaIPv4Checksum(*check, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2018-10-13 15:42:47 +00:00
|
|
|
// 0 is used to indicate "no checksum"
|
|
|
|
// 0xFFff and 0 are equivalent in one's complement math
|
|
|
|
// 0xFFff + 1 = 0x10000 -> 0x0001 (same as 0 + 1)
|
2018-10-13 17:28:35 +00:00
|
|
|
// infact it's impossible to get 0 with such addition,
|
|
|
|
// when starting from non-0 value.
|
|
|
|
// inside deltachksum we don't invert so it's safe to skip check there
|
2018-11-01 17:15:01 +00:00
|
|
|
// if(check->n == 0x0000)
|
|
|
|
// check->n = 0xFFff;
|
2018-10-12 04:35:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-16 12:06:19 +00:00
|
|
|
void
|
2018-11-01 17:15:01 +00:00
|
|
|
IPv4Packet::UpdateIPv4PacketOnDst(huint32_t newSrcIP, huint32_t newDstIP)
|
2018-09-16 12:06:19 +00:00
|
|
|
{
|
2018-10-09 17:02:49 +00:00
|
|
|
auto hdr = Header();
|
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
auto oSrcIP = nuint32_t{hdr->saddr};
|
|
|
|
auto oDstIP = nuint32_t{hdr->daddr};
|
|
|
|
auto nSrcIP = xhtonl(newSrcIP);
|
|
|
|
auto nDstIP = xhtonl(newDstIP);
|
2018-10-10 21:29:49 +00:00
|
|
|
|
2018-10-09 14:09:03 +00:00
|
|
|
// IPv4 checksum
|
2018-11-01 17:15:01 +00:00
|
|
|
auto v4chk = (nuint16_t *)&(hdr->check);
|
|
|
|
*v4chk = deltaIPv4Checksum(*v4chk, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2018-10-09 14:09:03 +00:00
|
|
|
|
|
|
|
// L4 checksum
|
2018-10-13 15:42:47 +00:00
|
|
|
auto ihs = size_t(hdr->ihl * 4);
|
|
|
|
if(ihs <= sz)
|
2018-09-16 12:06:19 +00:00
|
|
|
{
|
2018-10-13 15:42:47 +00:00
|
|
|
auto pld = buf + ihs;
|
|
|
|
auto psz = sz - ihs;
|
|
|
|
|
|
|
|
auto fragoff = size_t((ntohs(hdr->frag_off) & 0x1Fff) * 8);
|
|
|
|
|
|
|
|
switch(hdr->protocol)
|
|
|
|
{
|
2018-10-14 21:56:14 +00:00
|
|
|
case 6: // TCP
|
2018-11-01 17:15:01 +00:00
|
|
|
checksumDstIPv4TCP(pld, psz, fragoff, 16, oSrcIP, oDstIP, nSrcIP,
|
|
|
|
nDstIP);
|
2018-10-13 15:42:47 +00:00
|
|
|
break;
|
2018-10-14 21:56:14 +00:00
|
|
|
case 17: // UDP
|
|
|
|
case 136: // UDP-Lite - same checksum place, same 0->0xFFff condition
|
2018-11-01 17:15:01 +00:00
|
|
|
checksumDstIPv4UDP(pld, psz, fragoff, oSrcIP, oDstIP, nSrcIP,
|
|
|
|
nDstIP);
|
2018-10-13 15:42:47 +00:00
|
|
|
break;
|
2018-10-14 21:56:14 +00:00
|
|
|
case 33: // DCCP
|
2018-11-01 17:15:01 +00:00
|
|
|
checksumDstIPv4TCP(pld, psz, fragoff, 6, oSrcIP, oDstIP, nSrcIP,
|
|
|
|
nDstIP);
|
2018-10-14 21:56:14 +00:00
|
|
|
break;
|
2018-10-13 15:42:47 +00:00
|
|
|
}
|
2018-09-16 12:06:19 +00:00
|
|
|
}
|
2018-10-10 21:29:49 +00:00
|
|
|
|
|
|
|
// write new IP addresses
|
2018-11-01 17:15:01 +00:00
|
|
|
hdr->saddr = nSrcIP.n;
|
|
|
|
hdr->daddr = nDstIP.n;
|
2018-08-20 19:12:12 +00:00
|
|
|
}
|
2018-10-09 14:09:03 +00:00
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
static void
|
2018-11-07 15:30:22 +00:00
|
|
|
checksumSrcIPv4TCP(byte_t *pld, __attribute__((unused)) size_t psz,
|
|
|
|
size_t fragoff, size_t chksumoff, nuint32_t oSrcIP,
|
|
|
|
nuint32_t oDstIP)
|
2018-10-12 04:35:10 +00:00
|
|
|
{
|
2018-10-14 21:56:14 +00:00
|
|
|
if(fragoff > chksumoff)
|
2018-10-13 15:42:47 +00:00
|
|
|
return;
|
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
auto check = (nuint16_t *)(pld + chksumoff - fragoff);
|
2018-10-12 04:35:10 +00:00
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
*check =
|
|
|
|
deltaIPv4Checksum(*check, oSrcIP, oDstIP, nuint32_t{0}, nuint32_t{0});
|
2018-10-14 21:56:14 +00:00
|
|
|
// usually, TCP checksum field cannot be 0xFFff,
|
|
|
|
// because one's complement addition cannot result in 0x0000,
|
|
|
|
// and there's inversion in the end;
|
|
|
|
// emulate that.
|
2018-11-01 17:15:01 +00:00
|
|
|
if(check->n == 0xFFff)
|
|
|
|
check->n = 0x0000;
|
2018-10-12 04:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-11-07 15:30:22 +00:00
|
|
|
checksumSrcIPv4UDP(byte_t *pld, __attribute__((unused)) size_t psz,
|
|
|
|
size_t fragoff, nuint32_t oSrcIP, nuint32_t oDstIP)
|
2018-10-12 04:35:10 +00:00
|
|
|
{
|
2018-10-13 15:42:47 +00:00
|
|
|
if(fragoff > 6)
|
|
|
|
return;
|
2018-10-12 04:35:10 +00:00
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
auto check = (nuint16_t *)(pld + 6);
|
|
|
|
if(check->n == 0x0000)
|
2018-10-13 15:42:47 +00:00
|
|
|
return; // 0 is used to indicate "no checksum", don't change
|
2018-10-10 02:38:59 +00:00
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
*check =
|
|
|
|
deltaIPv4Checksum(*check, oSrcIP, oDstIP, nuint32_t{0}, nuint32_t{0});
|
2018-10-13 15:42:47 +00:00
|
|
|
// 0 is used to indicate "no checksum"
|
|
|
|
// 0xFFff and 0 are equivalent in one's complement math
|
|
|
|
// 0xFFff + 1 = 0x10000 -> 0x0001 (same as 0 + 1)
|
2018-10-13 17:28:35 +00:00
|
|
|
// infact it's impossible to get 0 with such addition,
|
|
|
|
// when starting from non-0 value.
|
|
|
|
// inside deltachksum we don't invert so it's safe to skip check there
|
2018-11-01 17:15:01 +00:00
|
|
|
// if(check->n == 0x0000)
|
|
|
|
// check->n = 0xFFff;
|
2018-10-12 04:35:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:09:03 +00:00
|
|
|
void
|
2018-11-01 17:15:01 +00:00
|
|
|
IPv4Packet::UpdateIPv4PacketOnSrc()
|
2018-10-09 14:09:03 +00:00
|
|
|
{
|
2018-10-09 17:02:49 +00:00
|
|
|
auto hdr = Header();
|
|
|
|
|
2018-11-01 17:15:01 +00:00
|
|
|
auto oSrcIP = nuint32_t{hdr->saddr};
|
|
|
|
auto oDstIP = nuint32_t{hdr->daddr};
|
2018-10-10 21:29:49 +00:00
|
|
|
|
2018-10-09 17:02:49 +00:00
|
|
|
// L4
|
2018-10-13 15:42:47 +00:00
|
|
|
auto ihs = size_t(hdr->ihl * 4);
|
|
|
|
if(ihs <= sz)
|
2018-10-09 17:02:49 +00:00
|
|
|
{
|
2018-10-13 15:42:47 +00:00
|
|
|
auto pld = buf + ihs;
|
|
|
|
auto psz = sz - ihs;
|
|
|
|
|
|
|
|
auto fragoff = size_t((ntohs(hdr->frag_off) & 0x1Fff) * 8);
|
|
|
|
|
|
|
|
switch(hdr->protocol)
|
|
|
|
{
|
2018-10-14 21:56:14 +00:00
|
|
|
case 6: // TCP
|
2018-11-01 17:15:01 +00:00
|
|
|
checksumSrcIPv4TCP(pld, psz, fragoff, 16, oSrcIP, oDstIP);
|
2018-10-13 15:42:47 +00:00
|
|
|
break;
|
2018-10-14 21:56:14 +00:00
|
|
|
case 17: // UDP
|
|
|
|
case 136: // UDP-Lite
|
2018-11-01 17:15:01 +00:00
|
|
|
checksumSrcIPv4UDP(pld, psz, fragoff, oSrcIP, oDstIP);
|
2018-10-13 15:42:47 +00:00
|
|
|
break;
|
2018-10-14 21:56:14 +00:00
|
|
|
case 33: // DCCP
|
2018-11-01 17:15:01 +00:00
|
|
|
checksumSrcIPv4TCP(pld, psz, fragoff, 6, oSrcIP, oDstIP);
|
2018-10-14 21:56:14 +00:00
|
|
|
break;
|
2018-10-13 15:42:47 +00:00
|
|
|
}
|
2018-10-09 17:02:49 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 14:09:03 +00:00
|
|
|
// IPv4
|
2018-11-01 17:15:01 +00:00
|
|
|
auto v4chk = (nuint16_t *)&(hdr->check);
|
|
|
|
*v4chk =
|
|
|
|
deltaIPv4Checksum(*v4chk, oSrcIP, oDstIP, nuint32_t{0}, nuint32_t{0});
|
2018-10-10 21:29:49 +00:00
|
|
|
|
|
|
|
// clear addresses
|
|
|
|
hdr->saddr = 0;
|
|
|
|
hdr->daddr = 0;
|
2018-10-09 14:09:03 +00:00
|
|
|
}
|
2018-08-17 19:49:58 +00:00
|
|
|
} // namespace net
|
|
|
|
} // namespace llarp
|