2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Internet protocol version 4.
|
|
|
|
* \author Stefan Kalkowski
|
|
|
|
* \date 2010-08-19
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2010-2017 Genode Labs GmbH
|
2011-12-22 15:19:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _IPV4_H_
|
|
|
|
#define _IPV4_H_
|
|
|
|
|
|
|
|
/* Genode */
|
|
|
|
#include <base/exception.h>
|
|
|
|
#include <util/string.h>
|
2016-08-25 13:47:44 +00:00
|
|
|
#include <util/token.h>
|
2018-04-09 17:58:08 +00:00
|
|
|
#include <util/construct_at.h>
|
2011-12-22 15:19:25 +00:00
|
|
|
#include <util/endian.h>
|
2021-07-23 18:01:42 +00:00
|
|
|
#include <util/register.h>
|
2011-12-22 15:19:25 +00:00
|
|
|
#include <net/netaddress.h>
|
2018-04-26 12:53:29 +00:00
|
|
|
#include <net/size_guard.h>
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2016-09-22 10:33:45 +00:00
|
|
|
namespace Genode { class Output; }
|
|
|
|
|
2020-09-11 08:44:44 +00:00
|
|
|
namespace Net {
|
2016-08-15 10:50:33 +00:00
|
|
|
enum { IPV4_ADDR_LEN = 4 };
|
2016-09-22 10:33:45 +00:00
|
|
|
|
|
|
|
class Ipv4_address;
|
2016-08-15 10:50:33 +00:00
|
|
|
class Ipv4_packet;
|
2020-09-11 08:44:44 +00:00
|
|
|
|
|
|
|
static inline Genode::size_t ascii_to(char const *, Net::Ipv4_address &);
|
2016-08-15 10:50:33 +00:00
|
|
|
}
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2016-09-22 10:33:45 +00:00
|
|
|
struct Net::Ipv4_address : Network_address<IPV4_ADDR_LEN, '.', false>
|
|
|
|
{
|
|
|
|
Ipv4_address(Genode::uint8_t value = 0) : Network_address(value) { }
|
|
|
|
|
|
|
|
Ipv4_address(void *src) : Network_address(src) { }
|
|
|
|
|
|
|
|
bool valid() const { return *this != Ipv4_address(); }
|
2017-09-27 11:54:58 +00:00
|
|
|
|
|
|
|
Genode::uint32_t to_uint32_big_endian() const;
|
|
|
|
|
|
|
|
static Ipv4_address from_uint32_big_endian(Genode::uint32_t ip_raw);
|
|
|
|
|
|
|
|
Genode::uint32_t to_uint32_little_endian() const;
|
|
|
|
|
|
|
|
static Ipv4_address from_uint32_little_endian(Genode::uint32_t ip_raw);
|
2017-10-06 11:00:05 +00:00
|
|
|
|
|
|
|
bool is_in_range(Ipv4_address const &first,
|
|
|
|
Ipv4_address const &last) const;
|
2016-10-24 09:46:14 +00:00
|
|
|
}
|
|
|
|
__attribute__((packed));
|
2016-09-22 10:33:45 +00:00
|
|
|
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
/**
|
|
|
|
* Data layout of this class conforms to an IPv4 packet (RFC 791)
|
|
|
|
*
|
|
|
|
* IPv4-header-format:
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
* | 0-3 | 4-7 | 8-11 | 12-15 | 16-18 | 19-23 | 24-27 | 28-31 |
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
* | version | IHL | service-type | total-length |
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
* | identifikation | flags | fragment-offset |
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
* | ttl | protocol | header-checksum |
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
* | source-ip-address |
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
* | destination-ip-address |
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
* | options ... |
|
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
class Net::Ipv4_packet
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum Size {
|
2016-08-15 10:50:33 +00:00
|
|
|
ADDR_LEN = IPV4_ADDR_LEN, /* Ip address length in bytes */
|
2016-06-03 14:21:45 +00:00
|
|
|
};
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-04 13:41:04 +00:00
|
|
|
static Ipv4_address current() { return Ipv4_address((Genode::uint8_t)0x00); }
|
|
|
|
static Ipv4_address broadcast() { return Ipv4_address((Genode::uint8_t)0xff); }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
static Ipv4_address ip_from_string(const char *ip);
|
|
|
|
|
2018-04-16 22:35:16 +00:00
|
|
|
void update_checksum();
|
|
|
|
|
|
|
|
bool checksum_error() const;
|
2016-03-18 18:41:00 +00:00
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
/************************
|
|
|
|
** IPv4 header fields **
|
|
|
|
************************/
|
|
|
|
|
2021-07-23 18:01:42 +00:00
|
|
|
struct Offset_6_u16 : Genode::Register<16>
|
|
|
|
{
|
|
|
|
struct Fragment_offset : Bitfield<0, 13> { };
|
|
|
|
struct Flags : Bitfield<13, 3> { };
|
|
|
|
struct More_fragments : Bitfield<13, 1> { };
|
|
|
|
struct Dont_fragment : Bitfield<14, 1> { };
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Offset_0_u8 : Genode::Register<8>
|
|
|
|
{
|
|
|
|
struct Ihl : Bitfield<0, 4> { }; /* Internet Header Length */
|
|
|
|
struct Version : Bitfield<4, 4> { };
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Offset_1_u8 : Genode::Register<8>
|
|
|
|
{
|
|
|
|
struct Ecn : Bitfield<0, 2> { }; /* Explicit Congestion Notification */
|
|
|
|
struct Dscp : Bitfield<2, 6> { }; /* Differentiated Services Code Point */
|
|
|
|
};
|
|
|
|
|
|
|
|
Genode::uint8_t _offset_0_u8;
|
|
|
|
Genode::uint8_t _offset_1_u8;
|
2015-03-04 20:12:14 +00:00
|
|
|
Genode::uint16_t _total_length;
|
|
|
|
Genode::uint16_t _identification;
|
2021-07-23 18:01:42 +00:00
|
|
|
Genode::uint16_t _offset_6_u16;
|
2015-03-04 20:12:14 +00:00
|
|
|
Genode::uint8_t _time_to_live;
|
|
|
|
Genode::uint8_t _protocol;
|
2017-09-14 13:10:32 +00:00
|
|
|
Genode::uint16_t _checksum;
|
|
|
|
Genode::uint8_t _src[ADDR_LEN];
|
|
|
|
Genode::uint8_t _dst[ADDR_LEN];
|
2021-07-23 18:01:42 +00:00
|
|
|
unsigned _data[0];
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-09-14 13:10:32 +00:00
|
|
|
enum class Protocol : Genode::uint8_t
|
|
|
|
{
|
2018-03-23 16:33:31 +00:00
|
|
|
ICMP = 1,
|
|
|
|
TCP = 6,
|
|
|
|
UDP = 17,
|
2017-09-14 13:10:32 +00:00
|
|
|
};
|
|
|
|
|
2022-05-18 11:09:35 +00:00
|
|
|
static Ipv4_packet const &cast_from(void const *base,
|
|
|
|
Size_guard &size_guard)
|
|
|
|
{
|
|
|
|
size_guard.consume_head(sizeof(Ipv4_packet));
|
|
|
|
return *(Ipv4_packet const *)base;
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:58:08 +00:00
|
|
|
template <typename T>
|
2018-04-26 12:53:29 +00:00
|
|
|
T const &data(Size_guard &size_guard) const
|
2018-01-08 13:11:29 +00:00
|
|
|
{
|
2018-04-26 12:53:29 +00:00
|
|
|
size_guard.consume_head(sizeof(T));
|
2018-04-09 17:58:08 +00:00
|
|
|
return *(T const *)(_data);
|
2018-01-08 13:11:29 +00:00
|
|
|
}
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-09 17:58:08 +00:00
|
|
|
template <typename T>
|
2018-04-26 12:53:29 +00:00
|
|
|
T &data(Size_guard &size_guard)
|
2018-01-08 13:11:29 +00:00
|
|
|
{
|
2018-04-26 12:53:29 +00:00
|
|
|
size_guard.consume_head(sizeof(T));
|
2018-04-09 17:58:08 +00:00
|
|
|
return *(T *)(_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename SIZE_GUARD>
|
|
|
|
T &construct_at_data(SIZE_GUARD &size_guard)
|
|
|
|
{
|
2018-04-26 12:53:29 +00:00
|
|
|
size_guard.consume_head(sizeof(T));
|
2018-04-09 17:58:08 +00:00
|
|
|
return *Genode::construct_at<T>(_data);
|
2015-03-04 20:12:14 +00:00
|
|
|
}
|
|
|
|
|
2018-04-18 17:38:43 +00:00
|
|
|
Genode::size_t size(Genode::size_t max_size) const;
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2017-09-14 13:10:32 +00:00
|
|
|
/***************
|
|
|
|
** Accessors **
|
|
|
|
***************/
|
|
|
|
|
2021-07-23 18:01:42 +00:00
|
|
|
Genode::size_t header_length() const { return Offset_0_u8::Ihl::get(_offset_0_u8); }
|
|
|
|
Genode::uint8_t version() const { return Offset_0_u8::Version::get(_offset_0_u8); }
|
|
|
|
Genode::uint8_t diff_service() const { return Offset_1_u8::Dscp::get(_offset_1_u8); }
|
|
|
|
Genode::uint8_t ecn() const { return Offset_1_u8::Ecn::get(_offset_1_u8); }
|
|
|
|
Genode::size_t total_length() const { return host_to_big_endian(_total_length); }
|
|
|
|
Genode::uint16_t identification() const { return host_to_big_endian(_identification); }
|
|
|
|
Genode::uint8_t flags() const { return (Genode::uint8_t)Offset_6_u16::Flags::get(host_to_big_endian(_offset_6_u16)); }
|
|
|
|
bool dont_fragment() const { return Offset_6_u16::Dont_fragment::get(host_to_big_endian(_offset_6_u16)); }
|
|
|
|
bool more_fragments() const { return Offset_6_u16::More_fragments::get(host_to_big_endian(_offset_6_u16)); }
|
|
|
|
Genode::size_t fragment_offset() const { return Offset_6_u16::Fragment_offset::get(host_to_big_endian(_offset_6_u16)); }
|
|
|
|
Genode::uint8_t time_to_live() const { return _time_to_live; }
|
|
|
|
Protocol protocol() const { return (Protocol)_protocol; }
|
|
|
|
Genode::uint16_t checksum() const { return host_to_big_endian(_checksum); }
|
|
|
|
Ipv4_address src() const { return Ipv4_address((void *)&_src); }
|
|
|
|
Ipv4_address dst() const { return Ipv4_address((void *)&_dst); }
|
|
|
|
|
2022-01-11 15:32:14 +00:00
|
|
|
void header_length(Genode::size_t v) { Offset_0_u8::Ihl::set(_offset_0_u8, (Offset_0_u8::access_t)v); }
|
2021-07-23 18:01:42 +00:00
|
|
|
void version(Genode::uint8_t v) { Offset_0_u8::Version::set(_offset_0_u8, v); }
|
|
|
|
void diff_service(Genode::uint8_t v) { Offset_1_u8::Dscp::set(_offset_1_u8, v); }
|
|
|
|
void ecn(Genode::uint8_t v) { Offset_1_u8::Ecn::set(_offset_1_u8, v); }
|
2022-05-18 11:09:35 +00:00
|
|
|
void diff_service_ecn(Genode::uint8_t v) { _offset_1_u8 = v; }
|
2017-09-14 13:10:32 +00:00
|
|
|
void total_length(Genode::size_t v) { _total_length = host_to_big_endian((Genode::uint16_t)v); }
|
|
|
|
void identification(Genode::uint16_t v) { _identification = host_to_big_endian(v); }
|
|
|
|
void time_to_live(Genode::uint8_t v) { _time_to_live = v; }
|
|
|
|
void protocol(Protocol v) { _protocol = (Genode::uint8_t)v; }
|
|
|
|
void checksum(Genode::uint16_t checksum) { _checksum = host_to_big_endian(checksum); }
|
|
|
|
void src(Ipv4_address v) { v.copy(&_src); }
|
|
|
|
void dst(Ipv4_address v) { v.copy(&_dst); }
|
2022-05-18 11:09:35 +00:00
|
|
|
void src_big_endian(Genode::uint32_t v) { *(Genode::uint32_t *)&_src = v; }
|
|
|
|
void dst_big_endian(Genode::uint32_t v) { *(Genode::uint32_t *)&_dst = v; }
|
2016-03-18 18:41:00 +00:00
|
|
|
|
2021-07-23 18:01:42 +00:00
|
|
|
void flags(Genode::uint8_t v)
|
|
|
|
{
|
|
|
|
Genode::uint16_t be = host_to_big_endian(_offset_6_u16);
|
|
|
|
Offset_6_u16::Flags::set(be, v);
|
|
|
|
_offset_6_u16 = host_to_big_endian(be);
|
|
|
|
}
|
|
|
|
|
|
|
|
void fragment_offset(Genode::size_t v)
|
|
|
|
{
|
|
|
|
Genode::uint16_t be = host_to_big_endian(_offset_6_u16);
|
2022-01-11 15:32:14 +00:00
|
|
|
Offset_6_u16::Fragment_offset::set(be, (Offset_6_u16::access_t)v);
|
2021-07-23 18:01:42 +00:00
|
|
|
_offset_6_u16 = host_to_big_endian(be);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dont_fragment(bool v)
|
|
|
|
{
|
|
|
|
Genode::uint16_t be = host_to_big_endian(_offset_6_u16);
|
|
|
|
Offset_6_u16::Dont_fragment::set(be, v);
|
|
|
|
_offset_6_u16 = host_to_big_endian(be);
|
|
|
|
}
|
|
|
|
|
|
|
|
void more_fragments(bool v)
|
|
|
|
{
|
|
|
|
Genode::uint16_t be = host_to_big_endian(_offset_6_u16);
|
|
|
|
Offset_6_u16::More_fragments::set(be, v);
|
|
|
|
_offset_6_u16 = host_to_big_endian(be);
|
|
|
|
}
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2016-11-02 00:00:59 +00:00
|
|
|
/*********
|
|
|
|
** log **
|
|
|
|
*********/
|
|
|
|
|
|
|
|
void print(Genode::Output &output) const;
|
|
|
|
|
2015-03-04 20:12:14 +00:00
|
|
|
} __attribute__((packed));
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2016-08-25 13:47:44 +00:00
|
|
|
|
2020-09-11 08:44:44 +00:00
|
|
|
Genode::size_t Net::ascii_to(char const *s, Net::Ipv4_address &result)
|
2016-08-25 13:47:44 +00:00
|
|
|
{
|
2020-09-11 08:44:44 +00:00
|
|
|
using namespace Genode;
|
|
|
|
|
2016-12-22 02:08:12 +00:00
|
|
|
Net::Ipv4_address buf;
|
|
|
|
size_t number_idx = 0;
|
|
|
|
size_t read_len = 0;
|
2020-09-11 08:44:44 +00:00
|
|
|
|
2016-12-22 02:08:12 +00:00
|
|
|
while (1) {
|
|
|
|
|
|
|
|
/* read the current number, fail if there's no number */
|
|
|
|
size_t number_len = ascii_to_unsigned(s, buf.addr[number_idx], 10);
|
|
|
|
if (!number_len) {
|
|
|
|
return 0; }
|
|
|
|
|
|
|
|
/* update read length and number index */
|
|
|
|
read_len += number_len;
|
|
|
|
number_idx++;
|
|
|
|
|
|
|
|
/* if we have all numbers, fill result and return read length */
|
|
|
|
if (number_idx == sizeof(buf.addr) / sizeof(buf.addr[0])) {
|
|
|
|
result = buf;
|
|
|
|
return read_len;
|
2016-08-25 13:47:44 +00:00
|
|
|
}
|
2016-12-22 02:08:12 +00:00
|
|
|
/* as it was not the last number, check for the following dot */
|
|
|
|
s += number_len;
|
|
|
|
if (*s != '.') {
|
|
|
|
return 0; }
|
|
|
|
read_len++;
|
|
|
|
s++;
|
2016-08-25 13:47:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 15:19:25 +00:00
|
|
|
#endif /* _IPV4_H_ */
|