2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-03-12 17:51:42 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-13 18:05:56 +03:00
|
|
|
#include <AK/HashMap.h>
|
2022-12-18 04:06:29 +03:00
|
|
|
#include <AK/SinglyLinkedList.h>
|
2023-02-24 21:10:59 +03:00
|
|
|
#include <Kernel/Library/DoubleBuffer.h>
|
|
|
|
#include <Kernel/Library/KBuffer.h>
|
2021-08-22 00:31:15 +03:00
|
|
|
#include <Kernel/Locking/MutexProtected.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/Net/IPv4.h>
|
2019-08-06 16:40:38 +03:00
|
|
|
#include <Kernel/Net/IPv4SocketTuple.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-03-14 01:14:30 +03:00
|
|
|
class NetworkAdapter;
|
|
|
|
class TCPPacket;
|
2019-03-14 14:20:38 +03:00
|
|
|
class TCPSocket;
|
2019-03-14 01:14:30 +03:00
|
|
|
|
2019-03-14 14:20:38 +03:00
|
|
|
class IPv4Socket : public Socket {
|
2019-03-12 17:51:42 +03:00
|
|
|
public:
|
2023-03-10 09:53:02 +03:00
|
|
|
static ErrorOr<NonnullRefPtr<Socket>> create(int type, int protocol);
|
2019-03-12 17:51:42 +03:00
|
|
|
virtual ~IPv4Socket() override;
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> close() override;
|
2022-08-21 17:33:09 +03:00
|
|
|
virtual ErrorOr<void> bind(Credentials const&, Userspace<sockaddr const*>, socklen_t) override;
|
2022-08-21 17:35:03 +03:00
|
|
|
virtual ErrorOr<void> connect(Credentials const&, OpenFileDescription&, Userspace<sockaddr const*>, socklen_t) override;
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> listen(size_t) override;
|
2020-02-08 01:42:28 +03:00
|
|
|
virtual void get_local_address(sockaddr*, socklen_t*) override;
|
|
|
|
virtual void get_peer_address(sockaddr*, socklen_t*) override;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual bool can_read(OpenFileDescription const&, u64) const override;
|
|
|
|
virtual bool can_write(OpenFileDescription const&, u64) const override;
|
|
|
|
virtual ErrorOr<size_t> sendto(OpenFileDescription&, UserOrKernelBuffer const&, size_t, int, Userspace<sockaddr const*>, socklen_t) override;
|
2023-03-14 00:11:13 +03:00
|
|
|
virtual ErrorOr<size_t> recvfrom(OpenFileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, UnixDateTime&, bool blocking) override;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<void> setsockopt(int level, int option, Userspace<void const*>, socklen_t) override;
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> getsockopt(OpenFileDescription&, int level, int option, Userspace<void*>, Userspace<socklen_t*>) override;
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg) override;
|
2019-09-23 20:06:03 +03:00
|
|
|
|
2023-03-14 00:11:13 +03:00
|
|
|
bool did_receive(IPv4Address const& peer_address, u16 peer_port, ReadonlyBytes, UnixDateTime const&);
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
IPv4Address const& local_address() const { return m_local_address; }
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 local_port() const { return m_local_port; }
|
|
|
|
void set_local_port(u16 port) { m_local_port = port; }
|
2019-08-09 05:38:56 +03:00
|
|
|
bool has_specific_local_address() { return m_local_address.to_u32() != 0; }
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
IPv4Address const& peer_address() const { return m_peer_address; }
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 peer_port() const { return m_peer_port; }
|
|
|
|
void set_peer_port(u16 port) { m_peer_port = port; }
|
2019-03-13 16:22:27 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
Vector<IPv4Address> const& multicast_memberships() const { return m_multicast_memberships; }
|
2021-05-04 14:42:47 +03:00
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
IPv4SocketTuple tuple() const { return IPv4SocketTuple(m_local_address, m_local_port, m_peer_address, m_peer_port); }
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const& description) const override;
|
2019-08-10 19:10:36 +03:00
|
|
|
|
2021-10-27 23:20:24 +03:00
|
|
|
u8 type_of_service() const { return m_type_of_service; }
|
2019-09-19 22:40:06 +03:00
|
|
|
u8 ttl() const { return m_ttl; }
|
|
|
|
|
2019-12-14 11:43:31 +03:00
|
|
|
enum class BufferMode {
|
|
|
|
Packets,
|
|
|
|
Bytes,
|
|
|
|
};
|
|
|
|
BufferMode buffer_mode() const { return m_buffer_mode; }
|
|
|
|
|
2019-03-14 14:20:38 +03:00
|
|
|
protected:
|
2021-08-01 15:11:05 +03:00
|
|
|
IPv4Socket(int type, int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer, OwnPtr<KBuffer> optional_scratch_buffer);
|
2021-10-03 01:24:00 +03:00
|
|
|
virtual StringView class_name() const override { return "IPv4Socket"sv; }
|
2019-03-14 14:28:30 +03:00
|
|
|
|
Kernel/Net: Rework ephemeral port allocation
Currently, ephemeral port allocation is handled by the
allocate_local_port_if_needed() and protocol_allocate_local_port()
methods. Actually binding the socket to an address (which means
inserting the socket/address pair into a global map) is performed either
in protocol_allocate_local_port() (for ephemeral ports) or in
protocol_listen() (for non-ephemeral ports); the latter will fail with
EADDRINUSE if the address is already used by an existing pair present in
the map.
There used to be a bug where for listen() without an explicit bind(),
the port allocation would conflict with itself: first an ephemeral port
would get allocated and inserted into the map, and then
protocol_listen() would check again for the port being free, find the
just-created map entry, and error out. This was fixed in commit
01e5af487f9513696dbcacab15d3e0036446f586 by passing an additional flag
did_allocate_port into protocol_listen() which specifies whether the
port was just allocated, and skipping the check in protocol_listen() if
the flag is set.
However, this only helps if the socket is bound to an ephemeral port
inside of this very listen() call. But calling bind(sin_port = 0) from
userspace should succeed and bind to an allocated ephemeral port, in the
same was as using an unbound socket for connect() does. The port number
can then be retrieved from userspace by calling getsockname (), and it
should be possible to either connect() or listen() on this socket,
keeping the allocated port number. Also, calling bind() when already
bound (either explicitly or implicitly) should always result in EINVAL.
To untangle this, introduce an explicit m_bound state in IPv4Socket,
just like LocalSocket has already. Once a socket is bound, further
attempt to bind it fail. Some operations cause the socket to implicitly
get bound to an (ephemeral) address; this is implemented by the new
ensure_bound() method. The protocol_allocate_local_port() method is
gone; it is now up to a protocol to assign a port to the socket inside
protocol_bind() if it finds that the socket has local_port() == 0.
protocol_bind() is now called in more cases, such as inside listen() if
the socket wasn't bound before that.
2023-07-23 15:43:45 +03:00
|
|
|
void set_bound(bool bound) { m_bound = bound; }
|
|
|
|
ErrorOr<void> ensure_bound();
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> protocol_bind() { return {}; }
|
Kernel/Net: Rework ephemeral port allocation
Currently, ephemeral port allocation is handled by the
allocate_local_port_if_needed() and protocol_allocate_local_port()
methods. Actually binding the socket to an address (which means
inserting the socket/address pair into a global map) is performed either
in protocol_allocate_local_port() (for ephemeral ports) or in
protocol_listen() (for non-ephemeral ports); the latter will fail with
EADDRINUSE if the address is already used by an existing pair present in
the map.
There used to be a bug where for listen() without an explicit bind(),
the port allocation would conflict with itself: first an ephemeral port
would get allocated and inserted into the map, and then
protocol_listen() would check again for the port being free, find the
just-created map entry, and error out. This was fixed in commit
01e5af487f9513696dbcacab15d3e0036446f586 by passing an additional flag
did_allocate_port into protocol_listen() which specifies whether the
port was just allocated, and skipping the check in protocol_listen() if
the flag is set.
However, this only helps if the socket is bound to an ephemeral port
inside of this very listen() call. But calling bind(sin_port = 0) from
userspace should succeed and bind to an allocated ephemeral port, in the
same was as using an unbound socket for connect() does. The port number
can then be retrieved from userspace by calling getsockname (), and it
should be possible to either connect() or listen() on this socket,
keeping the allocated port number. Also, calling bind() when already
bound (either explicitly or implicitly) should always result in EINVAL.
To untangle this, introduce an explicit m_bound state in IPv4Socket,
just like LocalSocket has already. Once a socket is bound, further
attempt to bind it fail. Some operations cause the socket to implicitly
get bound to an (ephemeral) address; this is implemented by the new
ensure_bound() method. The protocol_allocate_local_port() method is
gone; it is now up to a protocol to assign a port to the socket inside
protocol_bind() if it finds that the socket has local_port() == 0.
protocol_bind() is now called in more cases, such as inside listen() if
the socket wasn't bound before that.
2023-07-23 15:43:45 +03:00
|
|
|
virtual ErrorOr<void> protocol_listen() { return {}; }
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<size_t> protocol_receive(ReadonlyBytes /* raw_ipv4_packet */, UserOrKernelBuffer&, size_t, int) { return ENOTIMPL; }
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<size_t> protocol_send(UserOrKernelBuffer const&, size_t) { return ENOTIMPL; }
|
2022-07-13 09:31:24 +03:00
|
|
|
virtual ErrorOr<void> protocol_connect(OpenFileDescription&) { return {}; }
|
2021-09-11 22:38:05 +03:00
|
|
|
virtual ErrorOr<size_t> protocol_size(ReadonlyBytes /* raw_ipv4_packet */) { return ENOTIMPL; }
|
2019-03-14 17:23:32 +03:00
|
|
|
virtual bool protocol_is_disconnected() const { return false; }
|
2019-03-14 01:14:30 +03:00
|
|
|
|
2020-02-08 17:59:21 +03:00
|
|
|
virtual void shut_down_for_reading() override;
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
void set_local_address(IPv4Address address) { m_local_address = address; }
|
|
|
|
void set_peer_address(IPv4Address address) { m_peer_address = address; }
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
static ErrorOr<NonnullOwnPtr<DoubleBuffer>> try_create_receive_buffer();
|
2021-09-16 03:15:36 +03:00
|
|
|
void drop_receive_buffer();
|
2021-08-01 12:42:03 +03:00
|
|
|
|
2023-12-26 20:04:11 +03:00
|
|
|
size_t available_space_in_receive_buffer() const { return m_receive_buffer ? m_receive_buffer->space_for_writing() : 0; }
|
|
|
|
|
2019-03-12 17:51:42 +03:00
|
|
|
private:
|
|
|
|
virtual bool is_ipv4() const override { return true; }
|
|
|
|
|
2022-08-21 17:45:42 +03:00
|
|
|
ErrorOr<size_t> receive_byte_buffered(OpenFileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, bool blocking);
|
2023-03-14 00:11:13 +03:00
|
|
|
ErrorOr<size_t> receive_packet_buffered(OpenFileDescription&, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, UnixDateTime&, bool blocking);
|
2020-02-08 15:09:37 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
void set_can_read(bool);
|
|
|
|
|
2019-05-04 17:40:34 +03:00
|
|
|
IPv4Address m_local_address;
|
|
|
|
IPv4Address m_peer_address;
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2021-05-04 14:42:47 +03:00
|
|
|
Vector<IPv4Address> m_multicast_memberships;
|
|
|
|
bool m_multicast_loop { true };
|
Kernel/Net: Rework ephemeral port allocation
Currently, ephemeral port allocation is handled by the
allocate_local_port_if_needed() and protocol_allocate_local_port()
methods. Actually binding the socket to an address (which means
inserting the socket/address pair into a global map) is performed either
in protocol_allocate_local_port() (for ephemeral ports) or in
protocol_listen() (for non-ephemeral ports); the latter will fail with
EADDRINUSE if the address is already used by an existing pair present in
the map.
There used to be a bug where for listen() without an explicit bind(),
the port allocation would conflict with itself: first an ephemeral port
would get allocated and inserted into the map, and then
protocol_listen() would check again for the port being free, find the
just-created map entry, and error out. This was fixed in commit
01e5af487f9513696dbcacab15d3e0036446f586 by passing an additional flag
did_allocate_port into protocol_listen() which specifies whether the
port was just allocated, and skipping the check in protocol_listen() if
the flag is set.
However, this only helps if the socket is bound to an ephemeral port
inside of this very listen() call. But calling bind(sin_port = 0) from
userspace should succeed and bind to an allocated ephemeral port, in the
same was as using an unbound socket for connect() does. The port number
can then be retrieved from userspace by calling getsockname (), and it
should be possible to either connect() or listen() on this socket,
keeping the allocated port number. Also, calling bind() when already
bound (either explicitly or implicitly) should always result in EINVAL.
To untangle this, introduce an explicit m_bound state in IPv4Socket,
just like LocalSocket has already. Once a socket is bound, further
attempt to bind it fail. Some operations cause the socket to implicitly
get bound to an (ephemeral) address; this is implemented by the new
ensure_bound() method. The protocol_allocate_local_port() method is
gone; it is now up to a protocol to assign a port to the socket inside
protocol_bind() if it finds that the socket has local_port() == 0.
protocol_bind() is now called in more cases, such as inside listen() if
the socket wasn't bound before that.
2023-07-23 15:43:45 +03:00
|
|
|
bool m_bound { false };
|
2021-05-04 14:42:47 +03:00
|
|
|
|
2019-05-04 04:27:50 +03:00
|
|
|
struct ReceivedPacket {
|
2019-05-04 17:40:34 +03:00
|
|
|
IPv4Address peer_address;
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 peer_port;
|
2023-03-14 00:11:13 +03:00
|
|
|
UnixDateTime timestamp;
|
2021-09-07 16:36:39 +03:00
|
|
|
OwnPtr<KBuffer> data;
|
2019-05-04 04:27:50 +03:00
|
|
|
};
|
|
|
|
|
2022-12-18 04:06:29 +03:00
|
|
|
SinglyLinkedList<ReceivedPacket, CountingSizeCalculationPolicy> m_receive_queue;
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2021-09-16 03:15:36 +03:00
|
|
|
OwnPtr<DoubleBuffer> m_receive_buffer;
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 m_local_port { 0 };
|
|
|
|
u16 m_peer_port { 0 };
|
2019-03-13 16:22:27 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 m_bytes_received { 0 };
|
2019-03-14 17:28:23 +03:00
|
|
|
|
2021-10-27 23:20:24 +03:00
|
|
|
u8 m_type_of_service { IPTOS_LOWDELAY };
|
2019-09-19 22:40:06 +03:00
|
|
|
u8 m_ttl { 64 };
|
|
|
|
|
2019-03-12 19:27:07 +03:00
|
|
|
bool m_can_read { false };
|
2019-12-14 11:43:31 +03:00
|
|
|
|
|
|
|
BufferMode m_buffer_mode { BufferMode::Packets };
|
|
|
|
|
2021-08-01 15:11:05 +03:00
|
|
|
OwnPtr<KBuffer> m_scratch_buffer;
|
2021-08-15 16:46:35 +03:00
|
|
|
|
|
|
|
IntrusiveListNode<IPv4Socket> m_list_node;
|
|
|
|
|
|
|
|
public:
|
2021-09-09 15:00:59 +03:00
|
|
|
using List = IntrusiveList<&IPv4Socket::m_list_node>;
|
2021-08-15 16:46:35 +03:00
|
|
|
|
2021-08-22 00:31:15 +03:00
|
|
|
static MutexProtected<IPv4Socket::List>& all_sockets();
|
2019-03-12 17:51:42 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|