2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-08-25 01:35:19 +00:00
|
|
|
#include <AK/Singleton.h>
|
2019-04-03 10:36:40 +00:00
|
|
|
#include <Kernel/Devices/RandomDevice.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2019-04-02 13:46:44 +00:00
|
|
|
#include <Kernel/Net/Routing.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/Net/UDP.h>
|
|
|
|
#include <Kernel/Net/UDPSocket.h>
|
|
|
|
#include <Kernel/Process.h>
|
2020-01-03 11:36:30 +00:00
|
|
|
#include <Kernel/Random.h>
|
2019-03-14 11:43:18 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
void UDPSocket::for_each(Function<void(UDPSocket const&)> callback)
|
2019-08-09 10:27:40 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
sockets_by_port().for_each_shared([&](auto const& socket) {
|
2021-07-18 10:24:34 +00:00
|
|
|
callback(*socket.value);
|
|
|
|
});
|
2019-08-09 10:27:40 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> UDPSocket::try_for_each(Function<ErrorOr<void>(UDPSocket const&)> callback)
|
2022-02-24 18:05:49 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
return sockets_by_port().with_shared([&](auto const& sockets) -> ErrorOr<void> {
|
2022-02-24 18:05:49 +00:00
|
|
|
for (auto& socket : sockets)
|
|
|
|
TRY(callback(*socket.value));
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-08-21 21:31:15 +00:00
|
|
|
static Singleton<MutexProtected<HashMap<u16, UDPSocket*>>> s_map;
|
2020-08-25 01:35:19 +00:00
|
|
|
|
2021-08-21 21:31:15 +00:00
|
|
|
MutexProtected<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port()
|
2019-03-14 11:43:18 +00:00
|
|
|
{
|
|
|
|
return *s_map;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:23:57 +00:00
|
|
|
RefPtr<UDPSocket> UDPSocket::from_port(u16 port)
|
2019-03-14 11:43:18 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
return sockets_by_port().with_shared([&](auto const& table) -> RefPtr<UDPSocket> {
|
2021-07-18 10:24:34 +00:00
|
|
|
auto it = table.find(port);
|
|
|
|
if (it == table.end())
|
2019-06-07 09:43:58 +00:00
|
|
|
return {};
|
2021-12-25 10:23:57 +00:00
|
|
|
return (*it).value;
|
2021-07-18 10:24:34 +00:00
|
|
|
});
|
2019-03-14 11:43:18 +00:00
|
|
|
}
|
|
|
|
|
2021-08-01 09:42:03 +00:00
|
|
|
UDPSocket::UDPSocket(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer)
|
2021-08-01 12:11:05 +00:00
|
|
|
: IPv4Socket(SOCK_DGRAM, protocol, move(receive_buffer), {})
|
2019-03-14 11:43:18 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UDPSocket::~UDPSocket()
|
|
|
|
{
|
2021-07-18 10:24:34 +00:00
|
|
|
sockets_by_port().with_exclusive([&](auto& table) {
|
|
|
|
table.remove(local_port());
|
|
|
|
});
|
2019-03-14 11:43:18 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<NonnullRefPtr<UDPSocket>> UDPSocket::try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer)
|
2019-03-14 11:43:18 +00:00
|
|
|
{
|
2021-09-04 21:04:06 +00:00
|
|
|
return adopt_nonnull_ref_or_enomem(new (nothrow) UDPSocket(protocol, move(receive_buffer)));
|
2019-03-14 11:43:18 +00:00
|
|
|
}
|
|
|
|
|
2021-09-11 19:38:05 +00:00
|
|
|
ErrorOr<size_t> UDPSocket::protocol_size(ReadonlyBytes raw_ipv4_packet)
|
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
auto& ipv4_packet = *(IPv4Packet const*)(raw_ipv4_packet.data());
|
|
|
|
auto& udp_packet = *static_cast<UDPPacket const*>(ipv4_packet.payload());
|
2021-09-11 19:38:05 +00:00
|
|
|
return udp_packet.length() - sizeof(UDPPacket);
|
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<size_t> UDPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags)
|
2019-03-14 11:43:18 +00:00
|
|
|
{
|
2022-04-01 17:58:27 +00:00
|
|
|
auto& ipv4_packet = *(IPv4Packet const*)(raw_ipv4_packet.data());
|
|
|
|
auto& udp_packet = *static_cast<UDPPacket const*>(ipv4_packet.payload());
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
|
2021-05-06 06:27:51 +00:00
|
|
|
size_t read_size = min(buffer_size, udp_packet.length() - sizeof(UDPPacket));
|
2021-09-07 13:05:51 +00:00
|
|
|
SOCKET_TRY(buffer.write(udp_packet.payload(), read_size));
|
2021-05-06 06:27:51 +00:00
|
|
|
return read_size;
|
2019-03-14 11:43:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<size_t> UDPSocket::protocol_send(UserOrKernelBuffer const& data, size_t data_length)
|
2019-03-14 11:43:18 +00:00
|
|
|
{
|
2020-04-04 20:46:45 +00:00
|
|
|
auto routing_decision = route_to(peer_address(), local_address(), bound_interface());
|
2019-08-29 01:18:38 +00:00
|
|
|
if (routing_decision.is_zero())
|
2021-08-01 15:27:23 +00:00
|
|
|
return set_so_error(EHOSTUNREACH);
|
2021-05-26 03:35:05 +00:00
|
|
|
auto ipv4_payload_offset = routing_decision.adapter->ipv4_payload_offset();
|
|
|
|
data_length = min(data_length, routing_decision.adapter->mtu() - ipv4_payload_offset - sizeof(UDPPacket));
|
|
|
|
const size_t udp_buffer_size = sizeof(UDPPacket) + data_length;
|
|
|
|
auto packet = routing_decision.adapter->acquire_packet_buffer(ipv4_payload_offset + udp_buffer_size);
|
|
|
|
if (!packet)
|
2021-08-01 15:27:23 +00:00
|
|
|
return set_so_error(ENOMEM);
|
2021-08-01 12:11:49 +00:00
|
|
|
memset(packet->buffer->data() + ipv4_payload_offset, 0, sizeof(UDPPacket));
|
|
|
|
auto& udp_packet = *reinterpret_cast<UDPPacket*>(packet->buffer->data() + ipv4_payload_offset);
|
2019-05-04 14:40:34 +00:00
|
|
|
udp_packet.set_source_port(local_port());
|
|
|
|
udp_packet.set_destination_port(peer_port());
|
2021-05-26 03:35:05 +00:00
|
|
|
udp_packet.set_length(udp_buffer_size);
|
2021-09-07 13:05:51 +00:00
|
|
|
SOCKET_TRY(data.read(udp_packet.payload(), data_length));
|
2021-05-26 03:35:05 +00:00
|
|
|
routing_decision.adapter->fill_in_ipv4_header(*packet, local_address(), routing_decision.next_hop,
|
2021-10-27 20:20:24 +00:00
|
|
|
peer_address(), IPv4Protocol::UDP, udp_buffer_size, type_of_service(), ttl());
|
2021-08-01 12:11:49 +00:00
|
|
|
routing_decision.adapter->send_packet(packet->bytes());
|
2019-03-14 11:43:18 +00:00
|
|
|
return data_length;
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:31:24 +00:00
|
|
|
ErrorOr<void> UDPSocket::protocol_connect(OpenFileDescription&)
|
2019-08-11 13:38:20 +00:00
|
|
|
{
|
2021-08-29 00:04:30 +00:00
|
|
|
set_role(Role::Connected);
|
2020-01-26 13:43:08 +00:00
|
|
|
set_connected(true);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2019-08-11 13:38:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<u16> UDPSocket::protocol_allocate_local_port()
|
2019-03-14 11:43:18 +00:00
|
|
|
{
|
2021-05-19 14:35:09 +00:00
|
|
|
constexpr u16 first_ephemeral_port = 32768;
|
|
|
|
constexpr u16 last_ephemeral_port = 60999;
|
|
|
|
constexpr u16 ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port;
|
2020-01-03 11:36:30 +00:00
|
|
|
u16 first_scan_port = first_ephemeral_port + get_good_random<u16>() % ephemeral_port_range_size;
|
2019-03-18 03:03:44 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
return sockets_by_port().with_exclusive([&](auto& table) -> ErrorOr<u16> {
|
2021-07-18 10:24:34 +00:00
|
|
|
for (u16 port = first_scan_port;;) {
|
|
|
|
auto it = table.find(port);
|
|
|
|
if (it == table.end()) {
|
|
|
|
set_local_port(port);
|
|
|
|
table.set(port, this);
|
|
|
|
return port;
|
|
|
|
}
|
|
|
|
++port;
|
|
|
|
if (port > last_ephemeral_port)
|
|
|
|
port = first_ephemeral_port;
|
|
|
|
if (port == first_scan_port)
|
|
|
|
break;
|
2019-03-14 11:43:18 +00:00
|
|
|
}
|
2021-08-01 15:27:23 +00:00
|
|
|
return set_so_error(EADDRINUSE);
|
2021-07-18 10:24:34 +00:00
|
|
|
});
|
2019-03-14 11:43:18 +00:00
|
|
|
}
|
2019-05-03 19:51:40 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> UDPSocket::protocol_bind()
|
2019-05-03 19:51:40 +00:00
|
|
|
{
|
2021-11-07 23:51:39 +00:00
|
|
|
return sockets_by_port().with_exclusive([&](auto& table) -> ErrorOr<void> {
|
2021-07-18 10:24:34 +00:00
|
|
|
if (table.contains(local_port()))
|
2021-08-01 15:27:23 +00:00
|
|
|
return set_so_error(EADDRINUSE);
|
2021-07-18 10:24:34 +00:00
|
|
|
table.set(local_port(), this);
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2021-07-18 10:24:34 +00:00
|
|
|
});
|
2019-05-03 19:51:40 +00:00
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|