2020-05-20 19:02:48 +00:00
|
|
|
#include <net/ip_packet.hpp>
|
2020-05-20 19:14:05 +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
|
|
|
|
2020-07-01 14:14:45 +00:00
|
|
|
constexpr uint32_t ipv6_flowlabel_mask = 0b0000'0000'0000'1111'1111'1111'1111'1111;
|
2020-06-17 13:07:05 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ipv6_header::FlowLabel(llarp::nuint32_t label)
|
|
|
|
{
|
|
|
|
// the ipv6 flow label is the last 20 bits in the first 32 bits of the header
|
|
|
|
preamble.flowlabel =
|
|
|
|
(htonl(ipv6_flowlabel_mask) & label.n) | (preamble.flowlabel & htonl(~ipv6_flowlabel_mask));
|
|
|
|
}
|
|
|
|
|
|
|
|
llarp::nuint32_t
|
|
|
|
ipv6_header::FlowLabel() const
|
|
|
|
{
|
|
|
|
return llarp::nuint32_t{preamble.flowlabel & htonl(ipv6_flowlabel_mask)};
|
|
|
|
}
|
|
|
|
|
2018-08-17 19:49:58 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace net
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
inline static uint32_t*
|
|
|
|
in6_uint32_ptr(in6_addr& addr)
|
2019-06-18 13:10:44 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
return (uint32_t*)addr.s6_addr;
|
2019-06-18 13:10:44 +00:00
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
inline static const uint32_t*
|
|
|
|
in6_uint32_ptr(const in6_addr& addr)
|
2019-06-18 13:10:44 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
return (uint32_t*)addr.s6_addr;
|
2019-06-18 13:10:44 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint128_t
|
|
|
|
IPPacket::srcv6() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (IsV6())
|
2019-07-31 12:26:11 +00:00
|
|
|
return In6ToHUInt(HeaderV6()->srcaddr);
|
2019-07-30 23:42:13 +00:00
|
|
|
|
|
|
|
return ExpandV4(srcv4());
|
2019-06-11 16:44:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
huint128_t
|
|
|
|
IPPacket::dstv6() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (IsV6())
|
2019-07-31 12:26:11 +00:00
|
|
|
return In6ToHUInt(HeaderV6()->dstaddr);
|
2019-07-30 23:42:13 +00:00
|
|
|
|
|
|
|
return ExpandV4(dstv4());
|
2019-06-11 16:44:05 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 19:12:12 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
IPPacket::Load(const llarp_buffer_t& pkt)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (pkt.sz > sizeof(buf) or pkt.sz == 0)
|
2018-10-13 18:57:31 +00:00
|
|
|
return false;
|
|
|
|
sz = pkt.sz;
|
2019-10-04 18:10:58 +00:00
|
|
|
std::copy_n(pkt.base, sz, buf);
|
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
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
ManagedBuffer
|
2019-06-11 16:44:05 +00:00
|
|
|
IPPacket::ConstBuffer() const
|
2018-11-29 21:19:20 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
const byte_t* ptr = buf;
|
2019-10-04 18:10:58 +00:00
|
|
|
llarp_buffer_t b(ptr, sz);
|
|
|
|
return ManagedBuffer(b);
|
2018-11-29 21:19:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 18:10:58 +00:00
|
|
|
ManagedBuffer
|
2019-06-11 16:44:05 +00:00
|
|
|
IPPacket::Buffer()
|
2018-08-22 15:52:10 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
byte_t* ptr = buf;
|
2019-10-04 18:10:58 +00:00
|
|
|
llarp_buffer_t b(ptr, sz);
|
|
|
|
return ManagedBuffer(b);
|
2018-08-22 15:52:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:44:05 +00:00
|
|
|
huint32_t
|
|
|
|
IPPacket::srcv4() const
|
|
|
|
{
|
|
|
|
return huint32_t{ntohl(Header()->saddr)};
|
|
|
|
}
|
|
|
|
|
|
|
|
huint32_t
|
|
|
|
IPPacket::dstv4() const
|
|
|
|
{
|
|
|
|
return huint32_t{ntohl(Header()->daddr)};
|
|
|
|
}
|
|
|
|
|
2020-05-21 14:18:23 +00:00
|
|
|
huint128_t
|
|
|
|
IPPacket::dst4to6() const
|
2018-10-09 17:02:49 +00:00
|
|
|
{
|
2020-05-21 14:18:23 +00:00
|
|
|
return ExpandV4(dstv4());
|
|
|
|
}
|
|
|
|
|
|
|
|
huint128_t
|
|
|
|
IPPacket::src4to6() const
|
|
|
|
{
|
|
|
|
return ExpandV4(srcv4());
|
2018-10-09 17:02:49 +00:00
|
|
|
}
|
|
|
|
|
2018-09-16 12:06:19 +00:00
|
|
|
static uint16_t
|
2020-05-21 14:18:23 +00:00
|
|
|
ipchksum(const byte_t* buf, size_t sz, uint32_t sum = 0)
|
2018-09-16 12:06:19 +00:00
|
|
|
{
|
2020-05-21 14:18:23 +00:00
|
|
|
while (sz > 1)
|
2018-08-20 19:12:12 +00:00
|
|
|
{
|
2020-05-21 14:18:23 +00:00
|
|
|
sum += *(const uint16_t*)buf;
|
2018-09-16 12:06:19 +00:00
|
|
|
sz -= sizeof(uint16_t);
|
|
|
|
buf += sizeof(uint16_t);
|
2018-08-20 19:12:12 +00:00
|
|
|
}
|
2020-05-21 14:18:23 +00:00
|
|
|
if (sz != 0)
|
2018-10-13 15:42:47 +00:00
|
|
|
{
|
2018-10-13 17:28:35 +00:00
|
|
|
uint16_t x = 0;
|
|
|
|
|
2020-05-21 14:18:23 +00:00
|
|
|
*(byte_t*)&x = *(const byte_t*)buf;
|
2018-10-13 15:42:47 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-11 20:17:59 +00:00
|
|
|
#define ADD32CS(x) ((uint32_t)(x & 0xFFff) + (uint32_t)(x >> 16))
|
|
|
|
#define SUB32CS(x) ((uint32_t)((~x) & 0xFFff) + (uint32_t)((~x) >> 16))
|
|
|
|
|
|
|
|
static nuint16_t
|
2020-04-07 18:38:56 +00:00
|
|
|
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)
|
2019-06-11 20:17:59 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
uint32_t sum = uint32_t(old_sum.n) + ADD32CS(old_src_ip.n) + ADD32CS(old_dst_ip.n)
|
|
|
|
+ SUB32CS(new_src_ip.n) + SUB32CS(new_dst_ip.n);
|
2019-06-11 20:17:59 +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;
|
|
|
|
|
|
|
|
return nuint16_t{uint16_t(sum & 0xFFff)};
|
|
|
|
}
|
|
|
|
|
|
|
|
static nuint16_t
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaIPv6Checksum(
|
|
|
|
nuint16_t old_sum,
|
|
|
|
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])
|
2019-06-11 20:17:59 +00:00
|
|
|
{
|
2019-06-11 21:28:55 +00:00
|
|
|
/* 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) */
|
|
|
|
/* we represent 128bit stuff there as 4 32bit ints, that should be more or
|
|
|
|
* less correct */
|
|
|
|
/* we could do 64bit ints too but then we couldn't reuse 32bit macros and
|
|
|
|
* that'd suck for 32bit cpus */
|
2020-04-07 18:38:56 +00:00
|
|
|
#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.n) + ADDN128CS(old_src_ip) + ADDN128CS(old_dst_ip)
|
|
|
|
+ SUBN128CS(new_src_ip) + SUBN128CS(new_dst_ip);
|
2019-06-11 20:17:59 +00:00
|
|
|
#undef ADDN128CS
|
|
|
|
#undef SUBN128CS
|
|
|
|
|
|
|
|
// only need to do it 2 times to be sure
|
|
|
|
// proof: 0xFFff + 0xFFff = 0x1FFfe -> 0xFFff
|
|
|
|
sum = (sum & 0xFFff) + (sum >> 16);
|
|
|
|
sum += sum >> 16;
|
|
|
|
|
|
|
|
return nuint16_t{uint16_t(sum & 0xFFff)};
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef ADD32CS
|
|
|
|
#undef SUB32CS
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaChecksumIPv4TCP(
|
|
|
|
byte_t* pld,
|
|
|
|
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
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fragoff > chksumoff || psz < chksumoff - fragoff + 2)
|
2018-10-13 15:42:47 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
auto check = (nuint16_t*)(pld + chksumoff - fragoff);
|
2018-10-12 04:35:10 +00:00
|
|
|
|
2019-06-11 20:17:59 +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.
|
2020-04-07 18:38:56 +00:00
|
|
|
if (check->n == 0xFFff)
|
2018-11-01 17:15:01 +00:00
|
|
|
check->n = 0x0000;
|
2018-10-12 04:35:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:30:59 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaChecksumIPv6TCP(
|
|
|
|
byte_t* pld,
|
|
|
|
size_t psz,
|
|
|
|
size_t fragoff,
|
|
|
|
size_t chksumoff,
|
|
|
|
const uint32_t oSrcIP[4],
|
|
|
|
const uint32_t oDstIP[4],
|
|
|
|
const uint32_t nSrcIP[4],
|
|
|
|
const uint32_t nDstIP[4])
|
2019-06-11 20:30:59 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fragoff > chksumoff || psz < chksumoff - fragoff + 2)
|
2019-06-11 20:30:59 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
auto check = (nuint16_t*)(pld + chksumoff - fragoff);
|
2019-06-11 20:30:59 +00:00
|
|
|
|
|
|
|
*check = deltaIPv6Checksum(*check, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
|
|
|
// 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.
|
2020-04-07 18:38:56 +00:00
|
|
|
if (check->n == 0xFFff)
|
2019-06-11 20:30:59 +00:00
|
|
|
check->n = 0x0000;
|
|
|
|
}
|
|
|
|
|
2018-10-12 04:35:10 +00:00
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaChecksumIPv4UDP(
|
|
|
|
byte_t* pld,
|
|
|
|
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
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fragoff > 6 || psz < 6 + 2)
|
2018-10-13 15:42:47 +00:00
|
|
|
return;
|
2018-10-12 04:35:10 +00:00
|
|
|
|
2020-04-07 18:38:56 +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
|
|
|
|
2019-06-11 20:17:59 +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
|
|
|
}
|
2019-06-11 20:30:59 +00:00
|
|
|
|
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaChecksumIPv6UDP(
|
|
|
|
byte_t* pld,
|
|
|
|
size_t psz,
|
|
|
|
size_t fragoff,
|
|
|
|
const uint32_t oSrcIP[4],
|
|
|
|
const uint32_t oDstIP[4],
|
|
|
|
const uint32_t nSrcIP[4],
|
|
|
|
const uint32_t nDstIP[4])
|
2019-06-11 20:30:59 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fragoff > 6 || psz < 6 + 2)
|
2019-06-11 20:30:59 +00:00
|
|
|
return;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
auto check = (nuint16_t*)(pld + 6);
|
2019-06-11 20:30:59 +00:00
|
|
|
// 0 is used to indicate "no checksum", don't change
|
|
|
|
// even tho this shouldn't happen for IPv6, handle it properly
|
|
|
|
// we actually should drop/log 0-checksum packets per spec
|
|
|
|
// but that should be done at upper level than this function
|
|
|
|
// it's better to do correct thing there regardless
|
2019-06-11 20:52:13 +00:00
|
|
|
// XXX or maybe we should change this function to be able to return error?
|
|
|
|
// either way that's not a priority
|
2020-04-07 18:38:56 +00:00
|
|
|
if (check->n == 0x0000)
|
2019-06-11 20:30:59 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
*check = deltaIPv6Checksum(*check, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
|
|
|
// 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.
|
|
|
|
// inside deltachksum we don't invert so it's safe to skip check there
|
|
|
|
// if(check->n == 0x0000)
|
|
|
|
// check->n = 0xFFff;
|
|
|
|
}
|
2019-06-11 21:27:06 +00:00
|
|
|
|
|
|
|
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);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (ihs <= sz)
|
2019-06-11 21:27:06 +00:00
|
|
|
{
|
|
|
|
auto pld = buf + ihs;
|
|
|
|
auto psz = sz - ihs;
|
|
|
|
|
|
|
|
auto fragoff = size_t((ntohs(hdr->frag_off) & 0x1Fff) * 8);
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
switch (hdr->protocol)
|
2019-06-11 21:27:06 +00:00
|
|
|
{
|
|
|
|
case 6: // TCP
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaChecksumIPv4TCP(pld, psz, fragoff, 16, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2019-06-11 21:27:06 +00:00
|
|
|
break;
|
|
|
|
case 17: // UDP
|
|
|
|
case 136: // UDP-Lite - same checksum place, same 0->0xFFff condition
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaChecksumIPv4UDP(pld, psz, fragoff, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2019-06-11 21:27:06 +00:00
|
|
|
break;
|
|
|
|
case 33: // DCCP
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaChecksumIPv4TCP(pld, psz, fragoff, 6, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2019-06-11 21:27:06 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IPv4 checksum
|
2020-04-07 18:38:56 +00:00
|
|
|
auto v4chk = (nuint16_t*)&(hdr->check);
|
|
|
|
*v4chk = deltaIPv4Checksum(*v4chk, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2019-06-11 21:27:06 +00:00
|
|
|
|
|
|
|
// write new IP addresses
|
|
|
|
hdr->saddr = nSrcIP.n;
|
|
|
|
hdr->daddr = nDstIP.n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-06-17 13:07:05 +00:00
|
|
|
IPPacket::UpdateIPv6Address(huint128_t src, huint128_t dst, std::optional<nuint32_t> flowlabel)
|
2019-06-11 21:27:06 +00:00
|
|
|
{
|
2019-06-11 23:42:05 +00:00
|
|
|
const size_t ihs = 4 + 4 + 16 + 16;
|
2019-06-11 21:27:06 +00:00
|
|
|
|
|
|
|
// XXX should've been checked at upper level?
|
2020-04-07 18:38:56 +00:00
|
|
|
if (sz <= ihs)
|
2019-06-11 21:27:06 +00:00
|
|
|
return;
|
|
|
|
|
2019-06-11 21:28:55 +00:00
|
|
|
auto hdr = HeaderV6();
|
2020-06-17 13:07:05 +00:00
|
|
|
if (flowlabel.has_value())
|
|
|
|
{
|
|
|
|
// set flow label if desired
|
|
|
|
hdr->FlowLabel(*flowlabel);
|
|
|
|
}
|
2019-06-11 21:27:06 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
const auto oldSrcIP = hdr->srcaddr;
|
|
|
|
const auto oldDstIP = hdr->dstaddr;
|
|
|
|
const uint32_t* oSrcIP = in6_uint32_ptr(oldSrcIP);
|
|
|
|
const uint32_t* oDstIP = in6_uint32_ptr(oldDstIP);
|
2019-06-11 21:27:06 +00:00
|
|
|
|
|
|
|
// IPv6 address
|
2020-04-07 18:38:56 +00:00
|
|
|
hdr->srcaddr = HUIntToIn6(src);
|
|
|
|
hdr->dstaddr = HUIntToIn6(dst);
|
|
|
|
const uint32_t* nSrcIP = in6_uint32_ptr(hdr->srcaddr);
|
|
|
|
const uint32_t* nDstIP = in6_uint32_ptr(hdr->dstaddr);
|
2019-06-11 21:27:06 +00:00
|
|
|
|
|
|
|
// TODO IPv6 header options
|
2019-06-11 23:42:05 +00:00
|
|
|
auto pld = buf + ihs;
|
|
|
|
auto psz = sz - ihs;
|
2019-06-11 21:27:06 +00:00
|
|
|
|
2019-06-11 23:42:05 +00:00
|
|
|
size_t fragoff = 0;
|
|
|
|
auto nextproto = hdr->proto;
|
2020-04-07 18:38:56 +00:00
|
|
|
for (;;)
|
2019-06-11 23:42:05 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
switch (nextproto)
|
2019-06-11 23:42:05 +00:00
|
|
|
{
|
|
|
|
case 0: // Hop-by-Hop Options
|
|
|
|
case 43: // Routing Header
|
|
|
|
case 60: // Destination Options
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
nextproto = pld[0];
|
2019-06-11 23:42:05 +00:00
|
|
|
auto addlen = (size_t(pld[1]) + 1) * 8;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (psz < addlen)
|
2019-06-11 23:42:05 +00:00
|
|
|
return;
|
|
|
|
pld += addlen;
|
|
|
|
psz -= addlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 44: // Fragment Header
|
|
|
|
/*
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
| Next Header | Reserved | Fragment Offset |Res|M|
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
| Identification |
|
|
|
|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
*/
|
|
|
|
nextproto = pld[0];
|
2020-04-07 18:38:56 +00:00
|
|
|
fragoff = (uint16_t(pld[2]) << 8) | (uint16_t(pld[3]) & 0xFC);
|
|
|
|
if (psz < 8)
|
2019-06-11 23:42:05 +00:00
|
|
|
return;
|
|
|
|
pld += 8;
|
|
|
|
psz -= 8;
|
|
|
|
|
|
|
|
// jump straight to payload processing
|
2020-04-07 18:38:56 +00:00
|
|
|
if (fragoff != 0)
|
2019-06-11 23:42:05 +00:00
|
|
|
goto endprotohdrs;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto endprotohdrs;
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 00:04:05 +00:00
|
|
|
endprotohdrs:
|
2019-06-11 23:42:05 +00:00
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
switch (nextproto)
|
2019-06-11 21:27:06 +00:00
|
|
|
{
|
2019-06-11 21:28:55 +00:00
|
|
|
case 6: // TCP
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaChecksumIPv6TCP(pld, psz, fragoff, 16, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2019-06-11 21:28:55 +00:00
|
|
|
break;
|
|
|
|
case 17: // UDP
|
|
|
|
case 136: // UDP-Lite - same checksum place, same 0->0xFFff condition
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaChecksumIPv6UDP(pld, psz, fragoff, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2019-06-11 21:28:55 +00:00
|
|
|
break;
|
|
|
|
case 33: // DCCP
|
2020-04-07 18:38:56 +00:00
|
|
|
deltaChecksumIPv6TCP(pld, psz, fragoff, 6, oSrcIP, oDstIP, nSrcIP, nDstIP);
|
2019-06-11 21:28:55 +00:00
|
|
|
break;
|
2019-06-11 21:27:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-21 14:18:23 +00:00
|
|
|
|
|
|
|
void
|
2020-06-17 13:07:05 +00:00
|
|
|
IPPacket::ZeroAddresses(std::optional<nuint32_t> flowlabel)
|
2020-05-21 14:18:23 +00:00
|
|
|
{
|
|
|
|
if (IsV4())
|
|
|
|
{
|
|
|
|
UpdateIPv4Address({0}, {0});
|
|
|
|
}
|
|
|
|
else if (IsV6())
|
|
|
|
{
|
2020-06-17 13:07:05 +00:00
|
|
|
UpdateIPv6Address({0}, {0}, flowlabel);
|
2020-05-21 14:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-06-17 13:07:05 +00:00
|
|
|
IPPacket::ZeroSourceAddress(std::optional<nuint32_t> flowlabel)
|
2020-05-21 14:18:23 +00:00
|
|
|
{
|
|
|
|
if (IsV4())
|
|
|
|
{
|
|
|
|
UpdateIPv4Address({0}, xhtonl(dstv4()));
|
|
|
|
}
|
|
|
|
else if (IsV6())
|
|
|
|
{
|
2020-06-17 13:07:05 +00:00
|
|
|
UpdateIPv6Address({0}, {ntoh128(dstv6().h)}, flowlabel);
|
2020-05-21 14:18:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-17 13:07:05 +00:00
|
|
|
|
2020-05-21 14:18:23 +00:00
|
|
|
std::optional<IPPacket>
|
|
|
|
IPPacket::MakeICMPUnreachable() const
|
|
|
|
{
|
|
|
|
if (IsV4())
|
|
|
|
{
|
|
|
|
constexpr auto icmp_Header_size = 8;
|
|
|
|
constexpr auto ip_Header_size = 20;
|
|
|
|
net::IPPacket pkt{};
|
|
|
|
auto* pkt_Header = pkt.Header();
|
|
|
|
|
|
|
|
pkt_Header->version = 4;
|
|
|
|
pkt_Header->ihl = 0x05;
|
|
|
|
pkt_Header->tos = 0;
|
|
|
|
pkt_Header->check = 0;
|
|
|
|
pkt_Header->tot_len = ntohs(icmp_Header_size + ip_Header_size);
|
|
|
|
pkt_Header->saddr = Header()->daddr;
|
|
|
|
pkt_Header->daddr = Header()->saddr;
|
|
|
|
pkt_Header->protocol = 1; // ICMP
|
|
|
|
pkt_Header->ttl = 1;
|
|
|
|
pkt_Header->frag_off = htons(0b0100000000000000);
|
|
|
|
// size pf ip header
|
|
|
|
const size_t l3_HeaderSize = Header()->ihl * 4;
|
|
|
|
// size of l4 packet to reflect back
|
|
|
|
const size_t l4_PacketSize = 8;
|
|
|
|
pkt_Header->tot_len += ntohs(l4_PacketSize + l3_HeaderSize);
|
|
|
|
|
|
|
|
uint16_t* checksum;
|
|
|
|
uint8_t* itr = pkt.buf + (pkt_Header->ihl * 4);
|
|
|
|
uint8_t* icmp_begin = itr; // type 'destination unreachable'
|
|
|
|
*itr++ = 3;
|
|
|
|
// code 'Destination host unknown error'
|
|
|
|
*itr++ = 7;
|
|
|
|
// checksum + unused
|
|
|
|
htobe32buf(itr, 0);
|
|
|
|
checksum = (uint16_t*)itr;
|
|
|
|
itr += 4;
|
|
|
|
// next hop mtu is ignored but let's put something here anyways just in case tm
|
|
|
|
htobe16buf(itr, 1500);
|
|
|
|
itr += 2;
|
|
|
|
// copy ip header and first 8 bytes of datagram for icmp rject
|
|
|
|
std::copy_n(buf, l4_PacketSize + l3_HeaderSize, itr);
|
|
|
|
itr += l4_PacketSize + l3_HeaderSize;
|
|
|
|
// calculate checksum of ip header
|
|
|
|
pkt_Header->check = ipchksum(pkt.buf, pkt_Header->ihl * 4);
|
|
|
|
const auto icmp_size = std::distance(icmp_begin, itr);
|
|
|
|
// calculate icmp checksum
|
|
|
|
*checksum = ipchksum(icmp_begin, icmp_size);
|
|
|
|
pkt.sz = ntohs(pkt_Header->tot_len);
|
|
|
|
return pkt;
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2018-08-17 19:49:58 +00:00
|
|
|
} // namespace net
|
|
|
|
} // namespace llarp
|