2018-08-17 21:49:58 +02:00
|
|
|
#include <llarp/endian.h>
|
2018-08-21 15:02:05 +02:00
|
|
|
#include <algorithm>
|
2018-08-23 20:48:41 +02:00
|
|
|
#include <llarp/ip.hpp>
|
|
|
|
#include "llarp/buffer.hpp"
|
2018-08-22 17:52:10 +02:00
|
|
|
#include "mem.hpp"
|
2018-09-19 01:44:23 +02:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
2018-09-18 13:08:47 +02:00
|
|
|
#include <llarp/endian.h>
|
2018-09-16 14:06:19 +02:00
|
|
|
#include <map>
|
2018-10-10 14:06:28 +02:00
|
|
|
#include <algorithm>
|
2018-08-17 21:49:58 +02:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace net
|
|
|
|
{
|
2018-08-20 21:12:12 +02:00
|
|
|
bool
|
|
|
|
IPv4Packet::Load(llarp_buffer_t pkt)
|
2018-08-17 21:49:58 +02:00
|
|
|
{
|
2018-10-09 19:49:20 +02:00
|
|
|
sz = std::min(pkt.sz, sizeof(buf));
|
2018-08-22 17:52:10 +02:00
|
|
|
memcpy(buf, pkt.base, sz);
|
2018-08-20 21:12:12 +02:00
|
|
|
return true;
|
2018-08-17 21:49:58 +02:00
|
|
|
}
|
2018-08-20 21:12:12 +02:00
|
|
|
|
2018-08-22 17:52:10 +02:00
|
|
|
llarp_buffer_t
|
|
|
|
IPv4Packet::Buffer()
|
|
|
|
{
|
|
|
|
return llarp::InitBuffer(buf, sz);
|
|
|
|
}
|
|
|
|
|
2018-10-13 17:42:47 +02:00
|
|
|
#if 0
|
2018-10-09 19:02:49 +02:00
|
|
|
static uint32_t
|
2018-10-10 17:14:45 +02:00
|
|
|
ipchksum_pseudoIPv4(nuint32_t src_ip, nuint32_t dst_ip, uint8_t proto,
|
2018-10-09 19:09:45 +02:00
|
|
|
uint16_t innerlen)
|
2018-10-09 19:02:49 +02:00
|
|
|
{
|
2018-10-13 17:42:47 +02:00
|
|
|
#define IPCS(x) ((uint32_t)(x & 0xFFff) + (uint32_t)(x >> 16))
|
2018-10-10 17:14:45 +02:00
|
|
|
uint32_t sum = IPCS(src_ip.n) + IPCS(dst_ip.n) + (uint32_t)proto
|
2018-10-09 23:56:20 +02:00
|
|
|
+ (uint32_t)htons(innerlen);
|
2018-10-09 19:09:45 +02:00
|
|
|
#undef IPCS
|
2018-10-09 19:02:49 +02:00
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2018-09-16 14:06:19 +02:00
|
|
|
static uint16_t
|
2018-09-18 13:08:47 +02:00
|
|
|
ipchksum(const byte_t *buf, size_t sz, uint32_t sum = 0)
|
2018-09-16 14:06:19 +02:00
|
|
|
{
|
|
|
|
while(sz > 1)
|
2018-08-20 21:12:12 +02:00
|
|
|
{
|
2018-09-16 14:06:19 +02:00
|
|
|
sum += *(const uint16_t *)buf;
|
|
|
|
sz -= sizeof(uint16_t);
|
|
|
|
buf += sizeof(uint16_t);
|
2018-08-20 21:12:12 +02:00
|
|
|
}
|
2018-10-13 17:42:47 +02:00
|
|
|
if(sz != 0)
|
|
|
|
{
|
|
|
|
uint16_t x = 0;
|
|
|
|
*(byte_t *)&x = *(const byte_t *)buf;
|
|
|
|
sum += x;
|
|
|
|
}
|
2018-08-20 21:12:12 +02:00
|
|
|
|
|
|
|
while(sum >> 16)
|
2018-10-13 17:42:47 +02:00
|
|
|
sum = (sum & 0xFFff) + (sum >> 16);
|
2018-08-20 21:12:12 +02:00
|
|
|
|
2018-09-16 14:06:19 +02:00
|
|
|
return ~sum;
|
|
|
|
}
|
2018-10-13 17:42:47 +02:00
|
|
|
#endif
|
2018-09-16 14:06:19 +02:00
|
|
|
|
2018-10-09 19:02:49 +02:00
|
|
|
static uint16_t
|
2018-10-10 17:14:45 +02:00
|
|
|
deltachksum(uint16_t old_sum, huint32_t old_src_ip, huint32_t old_dst_ip,
|
|
|
|
huint32_t new_src_ip, huint32_t new_dst_ip)
|
2018-10-09 19:02:49 +02:00
|
|
|
{
|
2018-10-13 17:42:47 +02:00
|
|
|
#define ADDIPCS(x) ((uint32_t)(x.h & 0xFFff) + (uint32_t)(x.h >> 16))
|
|
|
|
#define SUBIPCS(x) ((uint32_t)((~x.h) & 0xFFff) + (uint32_t)((~x.h) >> 16))
|
2018-10-10 03:59:52 +02:00
|
|
|
|
2018-10-10 17:14:45 +02:00
|
|
|
uint32_t sum = ntohs(old_sum) + ADDIPCS(old_src_ip) + ADDIPCS(old_dst_ip)
|
|
|
|
+ SUBIPCS(new_src_ip) + SUBIPCS(new_dst_ip);
|
2018-10-10 03:29:44 +02:00
|
|
|
|
2018-10-09 23:56:20 +02:00
|
|
|
#undef ADDIPCS
|
|
|
|
#undef SUBIPCS
|
2018-10-10 03:29:44 +02:00
|
|
|
|
2018-10-09 19:02:49 +02:00
|
|
|
while(sum >> 16)
|
2018-10-13 17:42:47 +02:00
|
|
|
sum = (sum & 0xFFff) + (sum >> 16);
|
2018-10-10 03:29:44 +02:00
|
|
|
|
|
|
|
return htons(sum);
|
2018-10-09 19:02:49 +02:00
|
|
|
}
|
|
|
|
|
2018-10-12 06:35:10 +02:00
|
|
|
static void
|
2018-10-13 17:42:47 +02:00
|
|
|
checksumDstTCP(byte_t *pld, size_t psz, size_t fragoff, huint32_t oSrcIP,
|
|
|
|
huint32_t oDstIP, huint32_t nSrcIP, huint32_t nDstIP)
|
2018-10-12 06:35:10 +02:00
|
|
|
{
|
2018-10-13 17:42:47 +02:00
|
|
|
if(fragoff > 16)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint16_t *check = (uint16_t *)(pld + 16 - fragoff);
|
2018-10-12 06:35:10 +02:00
|
|
|
|
|
|
|
*check = deltachksum(*check, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
checksumDstUDP(const ip_header *ohdr, byte_t *pld, size_t psz,
|
2018-10-13 17:42:47 +02:00
|
|
|
size_t fragoff, huint32_t oSrcIP, huint32_t oDstIP,
|
|
|
|
huint32_t nSrcIP, huint32_t nDstIP)
|
2018-10-12 06:35:10 +02:00
|
|
|
{
|
2018-10-13 17:42:47 +02:00
|
|
|
if(fragoff > 6)
|
|
|
|
return;
|
2018-10-12 06:35:10 +02:00
|
|
|
|
2018-10-13 17:42:47 +02:00
|
|
|
uint16_t *check = (uint16_t *)(pld + 6);
|
|
|
|
if(*check == 0x0000)
|
|
|
|
return; // 0 is used to indicate "no checksum", don't change
|
2018-10-10 04:38:59 +02:00
|
|
|
|
2018-10-13 17:42:47 +02:00
|
|
|
*check = deltachksum(*check, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2018-10-12 06:35:10 +02:00
|
|
|
|
2018-10-13 17:42:47 +02: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)
|
|
|
|
// infact it's impossible to get 0 with such addition
|
|
|
|
// when starting from non-0 value
|
|
|
|
// but it's possible to get 0xFFff and we invert after that
|
|
|
|
// so we still need this fixup check
|
|
|
|
if(*check == 0x0000)
|
|
|
|
*check = 0xFFff;
|
2018-10-12 06:35:10 +02:00
|
|
|
}
|
|
|
|
|
2018-09-16 14:06:19 +02:00
|
|
|
void
|
2018-10-10 23:29:49 +02:00
|
|
|
IPv4Packet::UpdatePacketOnDst(huint32_t nSrcIP, huint32_t nDstIP)
|
2018-09-16 14:06:19 +02:00
|
|
|
{
|
2018-10-09 19:02:49 +02:00
|
|
|
auto hdr = Header();
|
|
|
|
|
2018-10-10 23:29:49 +02:00
|
|
|
auto oSrcIP = xntohl(nuint32_t{hdr->saddr});
|
|
|
|
auto oDstIP = xntohl(nuint32_t{hdr->daddr});
|
|
|
|
|
2018-10-09 16:09:03 +02:00
|
|
|
// IPv4 checksum
|
2018-10-10 23:29:49 +02:00
|
|
|
hdr->check = deltachksum(hdr->check, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2018-10-09 16:09:03 +02:00
|
|
|
|
|
|
|
// L4 checksum
|
2018-10-13 17:42:47 +02:00
|
|
|
auto ihs = size_t(hdr->ihl * 4);
|
|
|
|
if(ihs <= sz)
|
2018-09-16 14:06:19 +02:00
|
|
|
{
|
2018-10-13 17:42:47 +02:00
|
|
|
auto pld = buf + ihs;
|
|
|
|
auto psz = sz - ihs;
|
|
|
|
|
|
|
|
auto fragoff = size_t((ntohs(hdr->frag_off) & 0x1Fff) * 8);
|
|
|
|
|
|
|
|
switch(hdr->protocol)
|
|
|
|
{
|
|
|
|
case 6:
|
|
|
|
checksumDstTCP(pld, psz, fragoff, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
|
|
|
break;
|
|
|
|
case 17:
|
|
|
|
checksumDstUDP(hdr, pld, psz, fragoff, oSrcIP, oDstIP, nSrcIP,
|
|
|
|
nDstIP);
|
|
|
|
break;
|
|
|
|
}
|
2018-09-16 14:06:19 +02:00
|
|
|
}
|
2018-10-10 23:29:49 +02:00
|
|
|
|
|
|
|
// write new IP addresses
|
|
|
|
hdr->saddr = xhtonl(nSrcIP).n;
|
|
|
|
hdr->daddr = xhtonl(nDstIP).n;
|
2018-08-20 21:12:12 +02:00
|
|
|
}
|
2018-10-09 16:09:03 +02:00
|
|
|
|
2018-10-12 06:35:10 +02:00
|
|
|
static void
|
2018-10-13 17:42:47 +02:00
|
|
|
checksumSrcTCP(byte_t *pld, size_t psz, size_t fragoff, huint32_t oSrcIP,
|
|
|
|
huint32_t oDstIP)
|
2018-10-12 06:35:10 +02:00
|
|
|
{
|
2018-10-13 17:42:47 +02:00
|
|
|
if(fragoff > 16)
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint16_t *check = (uint16_t *)(pld + 16 - fragoff);
|
2018-10-12 06:35:10 +02:00
|
|
|
|
|
|
|
*check = deltachksum(*check, oSrcIP, oDstIP, huint32_t{0}, huint32_t{0});
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
checksumSrcUDP(const ip_header *ohdr, byte_t *pld, size_t psz,
|
2018-10-13 17:42:47 +02:00
|
|
|
size_t fragoff, huint32_t oSrcIP, huint32_t oDstIP)
|
2018-10-12 06:35:10 +02:00
|
|
|
{
|
2018-10-13 17:42:47 +02:00
|
|
|
if(fragoff > 6)
|
|
|
|
return;
|
2018-10-12 06:35:10 +02:00
|
|
|
|
2018-10-13 17:42:47 +02:00
|
|
|
uint16_t *check = (uint16_t *)(pld + 6);
|
|
|
|
if(*check == 0x0000)
|
|
|
|
return; // 0 is used to indicate "no checksum", don't change
|
2018-10-10 04:38:59 +02:00
|
|
|
|
2018-10-13 17:42:47 +02:00
|
|
|
*check = deltachksum(*check, oSrcIP, oDstIP, huint32_t{0}, huint32_t{0});
|
2018-10-12 06:35:10 +02:00
|
|
|
|
2018-10-13 17:42:47 +02: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)
|
|
|
|
// infact it's impossible to get 0 with such addition
|
|
|
|
// when starting from non-0 value
|
|
|
|
// but it's possible to get 0xFFff and we invert after that
|
|
|
|
// so we still need this fixup check
|
|
|
|
if(*check == 0x0000)
|
|
|
|
*check = 0xFFff;
|
2018-10-12 06:35:10 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:09:03 +02:00
|
|
|
void
|
2018-10-10 23:29:49 +02:00
|
|
|
IPv4Packet::UpdatePacketOnSrc()
|
2018-10-09 16:09:03 +02:00
|
|
|
{
|
2018-10-09 19:02:49 +02:00
|
|
|
auto hdr = Header();
|
|
|
|
|
2018-10-10 23:29:49 +02:00
|
|
|
auto oSrcIP = xntohl(nuint32_t{hdr->saddr});
|
|
|
|
auto oDstIP = xntohl(nuint32_t{hdr->daddr});
|
|
|
|
|
2018-10-09 19:02:49 +02:00
|
|
|
// L4
|
2018-10-13 17:42:47 +02:00
|
|
|
auto ihs = size_t(hdr->ihl * 4);
|
|
|
|
if(ihs <= sz)
|
2018-10-09 19:02:49 +02:00
|
|
|
{
|
2018-10-13 17:42:47 +02:00
|
|
|
auto pld = buf + ihs;
|
|
|
|
auto psz = sz - ihs;
|
|
|
|
|
|
|
|
auto fragoff = size_t((ntohs(hdr->frag_off) & 0x1Fff) * 8);
|
|
|
|
|
|
|
|
switch(hdr->protocol)
|
|
|
|
{
|
|
|
|
case 6:
|
|
|
|
checksumSrcTCP(pld, psz, fragoff, oSrcIP, oDstIP);
|
|
|
|
break;
|
|
|
|
case 17:
|
|
|
|
checksumSrcUDP(hdr, pld, psz, fragoff, oSrcIP, oDstIP);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-09 19:02:49 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 16:09:03 +02:00
|
|
|
// IPv4
|
2018-10-10 23:29:49 +02:00
|
|
|
hdr->check =
|
|
|
|
deltachksum(hdr->check, oSrcIP, oDstIP, huint32_t{0}, huint32_t{0});
|
|
|
|
|
|
|
|
// clear addresses
|
|
|
|
hdr->saddr = 0;
|
|
|
|
hdr->daddr = 0;
|
2018-10-09 16:09:03 +02:00
|
|
|
}
|
2018-08-17 21:49:58 +02:00
|
|
|
} // namespace net
|
|
|
|
} // namespace llarp
|