1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00
lokinet/llarp/ip.cpp

161 lines
3.9 KiB
C++
Raw Normal View History

#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
#include <llarp/endian.h>
#include <map>
namespace llarp
{
namespace net
{
2018-08-20 21:12:12 +02:00
bool
IPv4Packet::Load(llarp_buffer_t pkt)
{
2018-08-23 20:02:02 +02:00
#ifndef MIN
#define MIN(a, b) (a < b ? a : b)
sz = MIN(pkt.sz, sizeof(buf));
#undef MIN
#endif
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-20 21:12:12 +02:00
2018-08-22 17:52:10 +02:00
llarp_buffer_t
IPv4Packet::Buffer()
{
return llarp::InitBuffer(buf, sz);
}
static uint32_t
2018-10-09 19:09:45 +02:00
ipchksum_pseudoIPv4(uint32_t src_ip_n, uint32_t dst_ip_n, uint8_t proto,
uint16_t innerlen)
{
2018-10-09 19:09:45 +02:00
#define IPCS(x) ((x & 0xFFFF) + (x >> 16))
uint32_t sum = (uint32_t)IPCS(src_ip_n) + (uint32_t)IPCS(dst_ip_n)
+ (uint32_t)proto + (uint32_t)htons(innerlen);
#undef IPCS
return sum;
}
static uint16_t
ipchksum(const byte_t *buf, size_t sz, uint32_t sum = 0)
{
while(sz > 1)
2018-08-20 21:12:12 +02:00
{
sum += *(const uint16_t *)buf;
sz -= sizeof(uint16_t);
buf += sizeof(uint16_t);
2018-08-20 21:12:12 +02:00
}
if(sz > 0)
sum += *(const byte_t *)buf;
2018-08-20 21:12:12 +02:00
while(sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return ~sum;
}
static uint16_t
2018-10-09 19:09:45 +02:00
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)
{
2018-10-09 19:09:45 +02:00
#define IPCS(x) ((x & 0xFFFF) + (x >> 16))
uint32_t sum = ~old_sum + IPCS(new_src_ip_n) + IPCS(new_dst_ip_n)
- IPCS(old_src_ip_n) - IPCS(old_dst_ip_n);
#undef IPCS
while(sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return ~sum;
}
static std::map<
byte_t, std::function< void(const ip_header *, byte_t *, size_t) > >
2018-10-09 19:09:45 +02: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
#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 19:09:45 +02:00
{// TCP
6,
[](const ip_header *hdr, byte_t *pkt, size_t sz) {
auto hlen = size_t(hdr->ihl * 4);
if(hlen + 16 + 2 > sz)
return;
uint16_t *check = (uint16_t *)(pkt + hlen + 16);
*check = deltachksum(*check, 0, 0, hdr->saddr, hdr->daddr);
}},
};
void
2018-10-09 16:09:03 +02:00
IPv4Packet::UpdateChecksumsOnDst()
{
auto hdr = Header();
2018-10-09 16:09:03 +02:00
// IPv4 checksum
2018-10-09 19:09:45 +02:00
auto hlen = size_t(hdr->ihl * 4);
hdr->check = 0;
if(hlen <= sz)
hdr->check = ipchksum(buf, hlen);
2018-10-09 16:09:03 +02:00
// L4 checksum
2018-09-17 20:59:12 +02:00
auto proto = hdr->protocol;
auto itr = protoDstCheckSummer.find(proto);
if(itr != protoDstCheckSummer.end())
{
itr->second(hdr, buf, sz);
}
2018-08-20 21:12:12 +02:00
}
2018-10-09 16:09:03 +02:00
static std::map<
2018-10-09 19:09:45 +02: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) {
auto hlen = size_t(hdr->ihl * 4);
if(hlen + 16 + 2 > sz)
return;
uint16_t *check = (uint16_t *)(pkt + hlen + 16);
*check = deltachksum(*check, hdr->saddr, hdr->daddr, 0, 0);
}},
};
2018-10-09 16:09:03 +02:00
void
IPv4Packet::UpdateChecksumsOnSrc()
{
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 16:09:03 +02:00
// IPv4
hdr->check = 0;
2018-10-09 16:09:03 +02:00
}
} // namespace net
} // namespace llarp