net: add methods required for WireGuard port

* Adds methods for copying raw data to the data field of Ethernet frames and
  UDP packets. This is used in the port to wrap the higher-layer packet data
  prepared by the contrib code with the additionally required headers before
  sending it at a network session.
* Adds a method to cast raw data to an IPv4 packet. This is required in the
  port in order to check values in stand-alone IP packets produced by the
  contrib code before sending them at a network session.
* Adds methods for setting UDP ports given big endian port values without
  having to convert to little endian in the app and then back to big endian in
  the net lib.

Ref #4397
This commit is contained in:
Martin Stein 2022-05-18 13:09:35 +02:00 committed by Christian Helmuth
parent 679be47def
commit bd501404db
3 changed files with 33 additions and 3 deletions

View File

@ -102,6 +102,15 @@ class Net::Ethernet_frame
return *Genode::construct_at<T>(_data);
}
template <typename SIZE_GUARD>
void memcpy_to_data(void const *src_base,
Genode::size_t src_size,
SIZE_GUARD &size_guard)
{
size_guard.consume_head(src_size);
Genode::memcpy(_data, src_base, src_size);
}
static Ethernet_frame &construct_at(void *base,
Size_guard &size_guard)
{

View File

@ -143,6 +143,13 @@ class Net::Ipv4_packet
UDP = 17,
};
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;
}
template <typename T>
T const &data(Size_guard &size_guard) const
{
@ -191,6 +198,7 @@ class Net::Ipv4_packet
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); }
void diff_service_ecn(Genode::uint8_t v) { _offset_1_u8 = v; }
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; }
@ -198,6 +206,8 @@ class Net::Ipv4_packet
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); }
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; }
void flags(Genode::uint8_t v)
{

View File

@ -72,6 +72,15 @@ class Net::Udp_packet
return *Genode::construct_at<T>(_data);
}
template <typename SIZE_GUARD>
void memcpy_to_data(void const *src_base,
Genode::size_t src_size,
SIZE_GUARD &size_guard)
{
size_guard.consume_head(src_size);
Genode::memcpy(_data, src_base, src_size);
}
void update_checksum(Ipv4_address ip_src,
Ipv4_address ip_dst);
@ -88,9 +97,11 @@ class Net::Udp_packet
Genode::uint16_t length() const { return host_to_big_endian(_length); }
Genode::uint16_t checksum() const { return host_to_big_endian(_checksum); }
void length(Genode::uint16_t v) { _length = host_to_big_endian(v); }
void src_port(Port p) { _src_port = host_to_big_endian(p.value); }
void dst_port(Port p) { _dst_port = host_to_big_endian(p.value); }
void length(Genode::uint16_t v) { _length = host_to_big_endian(v); }
void src_port(Port p) { _src_port = host_to_big_endian(p.value); }
void dst_port(Port p) { _dst_port = host_to_big_endian(p.value); }
void src_port_big_endian(Genode::uint16_t v) { _src_port = v; }
void dst_port_big_endian(Genode::uint16_t v) { _dst_port = v; }
/*********