2018-08-17 19:49:58 +00:00
|
|
|
#include <llarp/endian.h>
|
2018-08-21 13:02:05 +00:00
|
|
|
#include <algorithm>
|
2018-08-23 18:48:41 +00:00
|
|
|
#include <llarp/ip.hpp>
|
|
|
|
#include "llarp/buffer.hpp"
|
2018-08-22 15:52:10 +00:00
|
|
|
#include "mem.hpp"
|
2018-09-18 23:44:23 +00:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
2018-09-18 11:08:47 +00:00
|
|
|
#include <llarp/endian.h>
|
2018-09-16 12:06:19 +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
|
|
|
|
IPv4Packet::Load(llarp_buffer_t pkt)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2018-10-09 17:49:20 +00:00
|
|
|
sz = std::min(pkt.sz, sizeof(buf));
|
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-08-22 15:52:10 +00:00
|
|
|
llarp_buffer_t
|
|
|
|
IPv4Packet::Buffer()
|
|
|
|
{
|
|
|
|
return llarp::InitBuffer(buf, sz);
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:02:49 +00:00
|
|
|
static uint32_t
|
2018-10-09 17:09:45 +00:00
|
|
|
ipchksum_pseudoIPv4(uint32_t src_ip_n, uint32_t dst_ip_n, uint8_t proto,
|
|
|
|
uint16_t innerlen)
|
2018-10-09 17:02:49 +00:00
|
|
|
{
|
2018-10-09 21:56:20 +00:00
|
|
|
#define IPCS(x) ((uint32_t)(x & 0xFFFF) + (uint32_t)(x >> 16))
|
|
|
|
uint32_t sum = IPCS(src_ip_n) + IPCS(dst_ip_n) + (uint32_t)proto
|
|
|
|
+ (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-09-16 12:06:19 +00:00
|
|
|
if(sz > 0)
|
|
|
|
sum += *(const byte_t *)buf;
|
2018-08-20 19:12:12 +00:00
|
|
|
|
|
|
|
while(sum >> 16)
|
|
|
|
sum = (sum & 0xffff) + (sum >> 16);
|
|
|
|
|
2018-09-16 12:06:19 +00:00
|
|
|
return ~sum;
|
|
|
|
}
|
|
|
|
|
2018-10-09 17:02:49 +00:00
|
|
|
static uint16_t
|
2018-10-09 17:09:45 +00:00
|
|
|
deltachksum(uint16_t old_sum, uint32_t old_src_ip_n, uint32_t old_dst_ip_n,
|
2018-10-09 17:02:49 +00:00
|
|
|
uint32_t new_src_ip_n, uint32_t new_dst_ip_n)
|
|
|
|
{
|
2018-10-09 21:56:20 +00:00
|
|
|
#define ADDIPCS(x) ((uint32_t)(x & 0xFFFF) + (uint32_t)(x >> 16))
|
|
|
|
#define SUBIPCS(x) ((uint32_t)((~x) & 0xFFFF) + (uint32_t)((~x) >> 16))
|
|
|
|
uint32_t sum = ~old_sum + ADDIPCS(new_src_ip_n) + ADDIPCS(new_dst_ip_n)
|
|
|
|
+ SUBIPCS(old_src_ip_n) + SUBIPCS(old_dst_ip_n);
|
|
|
|
#undef ADDIPCS
|
|
|
|
#undef SUBIPCS
|
2018-10-09 17:02:49 +00:00
|
|
|
while(sum >> 16)
|
|
|
|
sum = (sum & 0xffff) + (sum >> 16);
|
|
|
|
return ~sum;
|
|
|
|
}
|
|
|
|
|
2018-09-18 11:08:47 +00:00
|
|
|
static std::map<
|
|
|
|
byte_t, std::function< void(const ip_header *, byte_t *, size_t) > >
|
2018-10-09 17:09:45 +00:00
|
|
|
protoDstCheckSummer = {
|
|
|
|
// is this even correct???
|
|
|
|
// {RFC3022} says that IPv4 hdr isn't included in ICMP checksum calc
|
|
|
|
// and that we don't need to modify it
|
2018-10-09 17:02:49 +00:00
|
|
|
#if 0
|
|
|
|
{
|
|
|
|
// ICMP
|
|
|
|
1,
|
|
|
|
[](const ip_header *hdr, byte_t *buf, size_t sz)
|
|
|
|
{
|
|
|
|
auto len = hdr->ihl * 4;
|
|
|
|
if(len + 2 + 2 > sz)
|
|
|
|
return;
|
|
|
|
uint16_t *check = (uint16_t *)(buf + len + 2);
|
|
|
|
|
|
|
|
*check = 0;
|
|
|
|
*check = ipchksum(buf, sz);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
#endif
|
2018-10-09 17:09:45 +00:00
|
|
|
{// TCP
|
|
|
|
6,
|
|
|
|
[](const ip_header *hdr, byte_t *pkt, size_t sz) {
|
2018-10-09 21:56:20 +00:00
|
|
|
auto hlen = size_t(hdr->ihl * 4);
|
2018-10-09 17:09:45 +00:00
|
|
|
uint16_t *check = (uint16_t *)(pkt + hlen + 16);
|
|
|
|
*check = deltachksum(*check, 0, 0, hdr->saddr, hdr->daddr);
|
|
|
|
}},
|
2018-10-09 17:02:49 +00:00
|
|
|
};
|
2018-09-16 12:06:19 +00:00
|
|
|
void
|
2018-10-09 14:09:03 +00:00
|
|
|
IPv4Packet::UpdateChecksumsOnDst()
|
2018-09-16 12:06:19 +00:00
|
|
|
{
|
2018-10-09 17:02:49 +00:00
|
|
|
auto hdr = Header();
|
|
|
|
|
2018-10-09 14:09:03 +00:00
|
|
|
// IPv4 checksum
|
2018-10-09 17:09:45 +00:00
|
|
|
auto hlen = size_t(hdr->ihl * 4);
|
2018-09-16 12:06:19 +00:00
|
|
|
hdr->check = 0;
|
2018-10-09 21:09:16 +00:00
|
|
|
hdr->check = ipchksum(buf, hlen);
|
2018-10-09 14:09:03 +00:00
|
|
|
|
|
|
|
// L4 checksum
|
2018-09-17 18:59:12 +00:00
|
|
|
auto proto = hdr->protocol;
|
2018-10-09 17:02:49 +00:00
|
|
|
auto itr = protoDstCheckSummer.find(proto);
|
|
|
|
if(itr != protoDstCheckSummer.end())
|
2018-09-16 12:06:19 +00:00
|
|
|
{
|
2018-09-18 11:08:47 +00:00
|
|
|
itr->second(hdr, buf, sz);
|
2018-09-16 12:06:19 +00:00
|
|
|
}
|
2018-08-20 19:12:12 +00:00
|
|
|
}
|
2018-10-09 14:09:03 +00:00
|
|
|
|
2018-10-09 17:02:49 +00:00
|
|
|
static std::map<
|
2018-10-09 17:09:45 +00:00
|
|
|
byte_t, std::function< void(const ip_header *, byte_t *, size_t) > >
|
|
|
|
protoSrcCheckSummer = {
|
|
|
|
{// TCP
|
|
|
|
6,
|
|
|
|
[](const ip_header *hdr, byte_t *pkt, size_t sz) {
|
2018-10-09 21:56:20 +00:00
|
|
|
auto hlen = size_t(hdr->ihl * 4);
|
2018-10-09 17:09:45 +00:00
|
|
|
uint16_t *check = (uint16_t *)(pkt + hlen + 16);
|
|
|
|
*check = deltachksum(*check, hdr->saddr, hdr->daddr, 0, 0);
|
|
|
|
}},
|
2018-10-09 17:02:49 +00:00
|
|
|
};
|
2018-10-09 14:09:03 +00:00
|
|
|
void
|
|
|
|
IPv4Packet::UpdateChecksumsOnSrc()
|
|
|
|
{
|
2018-10-09 17:02:49 +00:00
|
|
|
auto hdr = Header();
|
|
|
|
|
|
|
|
// L4
|
|
|
|
auto proto = hdr->protocol;
|
|
|
|
auto itr = protoSrcCheckSummer.find(proto);
|
|
|
|
if(itr != protoSrcCheckSummer.end())
|
|
|
|
{
|
|
|
|
itr->second(hdr, buf, sz);
|
|
|
|
}
|
|
|
|
|
2018-10-09 14:09:03 +00:00
|
|
|
// IPv4
|
2018-10-09 17:02:49 +00:00
|
|
|
hdr->check = 0;
|
2018-10-09 14:09:03 +00:00
|
|
|
}
|
2018-08-17 19:49:58 +00:00
|
|
|
} // namespace net
|
|
|
|
} // namespace llarp
|