2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "net.hpp"
|
|
|
|
|
|
|
|
// #include <llarp/ev/ev.hpp>
|
|
|
|
#include <llarp/service/protocol_type.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/buffer.hpp>
|
|
|
|
#include <llarp/util/time.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
|
|
|
|
#include <oxenc/endian.h>
|
|
|
|
|
2023-10-19 21:59:57 +00:00
|
|
|
#include <memory>
|
2019-07-30 23:42:13 +00:00
|
|
|
#include <utility>
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
namespace llarp::net
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2022-05-18 21:06:32 +00:00
|
|
|
struct ip_header_le
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2022-09-10 17:44:55 +00:00
|
|
|
uint8_t ihl : 4;
|
|
|
|
uint8_t version : 4;
|
2022-05-18 21:06:32 +00:00
|
|
|
uint8_t tos;
|
|
|
|
uint16_t tot_len;
|
|
|
|
uint16_t id;
|
|
|
|
uint16_t frag_off;
|
|
|
|
uint8_t ttl;
|
|
|
|
uint8_t protocol;
|
|
|
|
uint16_t check;
|
|
|
|
uint32_t saddr;
|
|
|
|
uint32_t daddr;
|
|
|
|
};
|
|
|
|
|
2022-09-10 17:44:55 +00:00
|
|
|
struct ip_header_be
|
2022-05-18 21:06:32 +00:00
|
|
|
{
|
2022-09-10 17:44:55 +00:00
|
|
|
uint8_t version : 4;
|
|
|
|
uint8_t ihl : 4;
|
2022-05-18 21:06:32 +00:00
|
|
|
uint8_t tos;
|
|
|
|
uint16_t tot_len;
|
|
|
|
uint16_t id;
|
|
|
|
uint16_t frag_off;
|
|
|
|
uint8_t ttl;
|
|
|
|
uint8_t protocol;
|
|
|
|
uint16_t check;
|
|
|
|
uint32_t saddr;
|
|
|
|
uint32_t daddr;
|
|
|
|
};
|
|
|
|
|
2022-09-10 17:44:55 +00:00
|
|
|
using ip_header = std::conditional_t<oxenc::little_endian, ip_header_le, ip_header_be>;
|
|
|
|
|
|
|
|
static_assert(sizeof(ip_header) == 20);
|
2022-05-18 21:06:32 +00:00
|
|
|
|
|
|
|
struct ipv6_header_preamble_le
|
|
|
|
{
|
|
|
|
unsigned char pad_small : 4;
|
|
|
|
unsigned char version : 4;
|
|
|
|
uint8_t pad[3];
|
|
|
|
};
|
2021-04-14 15:07:06 +00:00
|
|
|
|
2022-09-10 17:44:55 +00:00
|
|
|
struct ipv6_header_preamble_be
|
2022-05-18 21:06:32 +00:00
|
|
|
{
|
|
|
|
unsigned char version : 4;
|
|
|
|
unsigned char pad_small : 4;
|
|
|
|
uint8_t pad[3];
|
|
|
|
};
|
2021-04-14 15:07:06 +00:00
|
|
|
|
2022-09-10 17:44:55 +00:00
|
|
|
using ipv6_header_preamble =
|
|
|
|
std::conditional_t<oxenc::little_endian, ipv6_header_preamble_le, ipv6_header_preamble_be>;
|
|
|
|
|
|
|
|
static_assert(sizeof(ipv6_header_preamble) == 4);
|
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
struct ipv6_header
|
|
|
|
{
|
|
|
|
union
|
2019-05-01 13:40:10 +00:00
|
|
|
{
|
2022-09-10 17:44:55 +00:00
|
|
|
ipv6_header_preamble preamble;
|
2022-05-18 21:06:32 +00:00
|
|
|
uint32_t flowlabel;
|
|
|
|
} preamble;
|
|
|
|
|
|
|
|
uint16_t payload_len;
|
2022-07-28 16:07:38 +00:00
|
|
|
uint8_t protocol;
|
2022-05-18 21:06:32 +00:00
|
|
|
uint8_t hoplimit;
|
|
|
|
in6_addr srcaddr;
|
|
|
|
in6_addr dstaddr;
|
|
|
|
llarp::nuint32_t
|
|
|
|
FlowLabel() const;
|
|
|
|
|
|
|
|
/// put 20 bit truncated flow label network order
|
|
|
|
void
|
|
|
|
FlowLabel(llarp::nuint32_t label);
|
|
|
|
};
|
|
|
|
|
2022-09-10 17:44:55 +00:00
|
|
|
static_assert(sizeof(ipv6_header) == 40);
|
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
/// "well known" ip protocols
|
|
|
|
/// TODO: extend this to non "well known values"
|
|
|
|
enum class IPProtocol : uint8_t
|
|
|
|
{
|
|
|
|
ICMP = 0x01,
|
|
|
|
IGMP = 0x02,
|
|
|
|
IPIP = 0x04,
|
|
|
|
TCP = 0x06,
|
|
|
|
UDP = 0x11,
|
|
|
|
GRE = 0x2F,
|
|
|
|
ICMP6 = 0x3A,
|
|
|
|
OSFP = 0x59,
|
|
|
|
PGM = 0x71,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// get string representation of this protocol
|
|
|
|
/// throws std::invalid_argument if we don't know the name of this ip protocol
|
|
|
|
std::string
|
|
|
|
IPProtocolName(IPProtocol proto);
|
|
|
|
|
|
|
|
/// parse a string to an ip protocol
|
|
|
|
/// throws std::invalid_argument if cannot be parsed
|
|
|
|
IPProtocol
|
|
|
|
ParseIPProtocol(std::string data);
|
|
|
|
|
|
|
|
/// an Packet
|
|
|
|
struct IPPacket
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
static constexpr size_t _max_size = 1500;
|
2022-05-18 21:06:32 +00:00
|
|
|
llarp_time_t timestamp;
|
2022-07-28 16:07:38 +00:00
|
|
|
std::vector<byte_t> _buf;
|
2022-05-18 22:02:57 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
public:
|
|
|
|
IPPacket() : IPPacket{size_t{}}
|
|
|
|
{}
|
|
|
|
/// create an ip packet buffer of all zeros of size sz
|
|
|
|
explicit IPPacket(size_t sz);
|
|
|
|
/// create an ip packet from a view
|
|
|
|
explicit IPPacket(byte_view_t);
|
|
|
|
/// create an ip packet from a vector we then own
|
|
|
|
IPPacket(std::vector<byte_t>&&);
|
2018-08-22 15:52:10 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
~IPPacket() = default;
|
|
|
|
|
|
|
|
static constexpr size_t MaxSize = _max_size;
|
|
|
|
static constexpr size_t MinSize = 20;
|
|
|
|
|
|
|
|
[[deprecated("deprecated because of llarp_buffer_t")]] static IPPacket
|
2022-05-18 21:06:32 +00:00
|
|
|
UDP(nuint32_t srcaddr,
|
|
|
|
nuint16_t srcport,
|
|
|
|
nuint32_t dstaddr,
|
|
|
|
nuint16_t dstport,
|
2022-07-28 16:07:38 +00:00
|
|
|
const llarp_buffer_t& data)
|
|
|
|
{
|
|
|
|
return make_udp(srcaddr, srcport, dstaddr, dstport, data.copy());
|
|
|
|
}
|
|
|
|
|
|
|
|
static IPPacket
|
|
|
|
make_udp(
|
|
|
|
net::ipaddr_t srcaddr,
|
|
|
|
net::port_t srcport,
|
|
|
|
net::ipaddr_t dstaddr,
|
|
|
|
net::port_t dstport,
|
|
|
|
std::vector<byte_t> udp_body);
|
|
|
|
|
|
|
|
static inline IPPacket
|
|
|
|
make_udp(SockAddr src, SockAddr dst, std::variant<OwnedBuffer, std::vector<byte_t>> udp_body)
|
|
|
|
{
|
|
|
|
if (auto* vec = std::get_if<std::vector<byte_t>>(&udp_body))
|
|
|
|
return make_udp(src.getIP(), src.port(), dst.getIP(), dst.port(), std::move(*vec));
|
2023-08-31 16:28:02 +00:00
|
|
|
if (auto* buf = std::get_if<OwnedBuffer>(&udp_body))
|
2022-07-28 16:07:38 +00:00
|
|
|
return make_udp(src, dst, buf->copy());
|
2023-08-31 16:28:02 +00:00
|
|
|
return net::IPPacket{size_t{}};
|
2022-07-28 16:07:38 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
inline bool
|
2022-07-28 16:07:38 +00:00
|
|
|
Load(const llarp_buffer_t& buf)
|
|
|
|
{
|
|
|
|
_buf = buf.copy();
|
|
|
|
if (size() >= MinSize)
|
|
|
|
return true;
|
|
|
|
_buf.resize(0);
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-29 16:31:55 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
inline llarp_buffer_t
|
2022-07-28 16:07:38 +00:00
|
|
|
ConstBuffer() const
|
|
|
|
{
|
|
|
|
return llarp_buffer_t{_buf};
|
|
|
|
}
|
2018-08-20 19:12:12 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
/// steal the underlying vector
|
|
|
|
inline std::vector<byte_t>
|
|
|
|
steal()
|
|
|
|
{
|
|
|
|
std::vector<byte_t> buf;
|
|
|
|
buf.resize(0);
|
|
|
|
std::swap(_buf, buf);
|
|
|
|
return buf;
|
|
|
|
}
|
2018-11-29 21:19:20 +00:00
|
|
|
|
2023-10-12 20:37:45 +00:00
|
|
|
inline std::string
|
|
|
|
to_string()
|
|
|
|
{
|
|
|
|
return {reinterpret_cast<const char*>(_buf.data()), _buf.size()};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::string_view
|
|
|
|
to_view()
|
|
|
|
{
|
|
|
|
return {reinterpret_cast<const char*>(_buf.data()), _buf.size()};
|
|
|
|
}
|
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
inline byte_t*
|
|
|
|
data()
|
|
|
|
{
|
|
|
|
return _buf.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const byte_t*
|
|
|
|
data() const
|
|
|
|
{
|
|
|
|
return _buf.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t
|
|
|
|
capacity() const
|
|
|
|
{
|
|
|
|
return _max_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline size_t
|
|
|
|
size() const
|
|
|
|
{
|
|
|
|
return _buf.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
empty() const
|
|
|
|
{
|
|
|
|
return _buf.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
byte_view_t
|
|
|
|
view() const;
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
struct CompareSize
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
operator()(const IPPacket& left, const IPPacket& right)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
return left.size() < right.size();
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
2022-05-18 21:06:32 +00:00
|
|
|
};
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
struct CompareOrder
|
|
|
|
{
|
|
|
|
bool
|
|
|
|
operator()(const IPPacket& left, const IPPacket& right)
|
2018-08-17 19:49:58 +00:00
|
|
|
{
|
2022-05-18 21:06:32 +00:00
|
|
|
return left.timestamp < right.timestamp;
|
2018-08-17 19:49:58 +00:00
|
|
|
}
|
2022-05-18 21:06:32 +00:00
|
|
|
};
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
inline ip_header*
|
|
|
|
Header()
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
return reinterpret_cast<ip_header*>(data());
|
2022-05-18 21:06:32 +00:00
|
|
|
}
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
inline const ip_header*
|
|
|
|
Header() const
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
return reinterpret_cast<const ip_header*>(data());
|
2022-05-18 21:06:32 +00:00
|
|
|
}
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
inline ipv6_header*
|
|
|
|
HeaderV6()
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
return reinterpret_cast<ipv6_header*>(data());
|
2022-05-18 21:06:32 +00:00
|
|
|
}
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
inline const ipv6_header*
|
|
|
|
HeaderV6() const
|
|
|
|
{
|
2022-07-28 16:07:38 +00:00
|
|
|
return reinterpret_cast<const ipv6_header*>(data());
|
2022-05-18 21:06:32 +00:00
|
|
|
}
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
inline int
|
|
|
|
Version() const
|
|
|
|
{
|
|
|
|
return Header()->version;
|
|
|
|
}
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
inline byte_t
|
|
|
|
protocol() const
|
|
|
|
{
|
|
|
|
if (IsV4())
|
|
|
|
return Header()->protocol;
|
2023-08-31 16:28:02 +00:00
|
|
|
return HeaderV6()->protocol;
|
2022-07-28 16:07:38 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
inline bool
|
|
|
|
IsV4() const
|
|
|
|
{
|
|
|
|
return Version() == 4;
|
|
|
|
}
|
2019-07-30 23:42:13 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
inline bool
|
|
|
|
IsV6() const
|
|
|
|
{
|
|
|
|
return Version() == 6;
|
|
|
|
}
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
inline service::ProtocolType
|
|
|
|
ServiceProtocol() const
|
|
|
|
{
|
|
|
|
if (IsV4())
|
|
|
|
return service::ProtocolType::TrafficV4;
|
|
|
|
if (IsV6())
|
|
|
|
return service::ProtocolType::TrafficV6;
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
return service::ProtocolType::Control;
|
|
|
|
}
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
huint128_t
|
|
|
|
srcv6() const;
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
huint128_t
|
|
|
|
dstv6() const;
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
huint32_t
|
|
|
|
srcv4() const;
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
huint32_t
|
|
|
|
dstv4() const;
|
2019-06-11 16:44:05 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
huint128_t
|
|
|
|
src4to6() const;
|
2021-01-11 23:13:22 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
huint128_t
|
|
|
|
dst4to6() const;
|
2021-01-11 23:13:22 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
huint128_t
|
|
|
|
src4to6Lan() const;
|
2021-04-14 15:07:06 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
huint128_t
|
|
|
|
dst4to6Lan() const;
|
2021-09-23 18:01:04 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
SockAddr
|
|
|
|
src() const;
|
|
|
|
|
|
|
|
SockAddr
|
|
|
|
dst() const;
|
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
/// get destination port if applicable
|
|
|
|
std::optional<nuint16_t>
|
|
|
|
DstPort() const;
|
2021-09-23 18:01:04 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
/// get source port if applicable
|
|
|
|
std::optional<nuint16_t>
|
|
|
|
SrcPort() const;
|
2018-10-09 14:09:03 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
/// get pointer and size of layer 4 data
|
|
|
|
std::optional<std::pair<const char*, size_t>>
|
|
|
|
L4Data() const;
|
2020-06-17 13:07:05 +00:00
|
|
|
|
2022-07-28 16:07:38 +00:00
|
|
|
inline std::optional<OwnedBuffer>
|
|
|
|
L4OwnedBuffer() const
|
|
|
|
{
|
|
|
|
if (auto data = L4Data())
|
|
|
|
return OwnedBuffer{reinterpret_cast<const byte_t*>(data->first), data->second};
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
void
|
|
|
|
UpdateIPv4Address(nuint32_t src, nuint32_t dst);
|
2020-05-21 14:18:23 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
void
|
|
|
|
UpdateIPv6Address(
|
|
|
|
huint128_t src, huint128_t dst, std::optional<nuint32_t> flowlabel = std::nullopt);
|
2020-05-21 14:18:23 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
/// set addresses to zero and recacluate checksums
|
|
|
|
void
|
|
|
|
ZeroAddresses(std::optional<nuint32_t> flowlabel = std::nullopt);
|
|
|
|
|
|
|
|
/// zero out source address
|
|
|
|
void
|
|
|
|
ZeroSourceAddress(std::optional<nuint32_t> flowlabel = std::nullopt);
|
|
|
|
|
|
|
|
/// make an icmp unreachable reply packet based of this ip packet
|
|
|
|
std::optional<IPPacket>
|
|
|
|
MakeICMPUnreachable() const;
|
2022-07-28 16:07:38 +00:00
|
|
|
|
|
|
|
std::function<void(net::IPPacket)> reply;
|
2022-05-18 21:06:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// generate ip checksum
|
|
|
|
uint16_t
|
|
|
|
ipchksum(const byte_t* buf, size_t sz, uint32_t sum = 0);
|
2018-08-17 19:49:58 +00:00
|
|
|
|
2022-05-18 21:06:32 +00:00
|
|
|
} // namespace llarp::net
|