2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-03-14 12:20:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
#include <AK/Error.h>
|
2019-08-06 23:40:38 +10:00
|
|
|
#include <AK/Function.h>
|
2019-09-08 17:38:08 +10:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/SinglyLinkedList.h>
|
2019-08-09 12:34:32 +10:00
|
|
|
#include <AK/WeakPtr.h>
|
2021-08-21 23:31:15 +02:00
|
|
|
#include <Kernel/Locking/MutexProtected.h>
|
2019-04-02 19:54:38 +02:00
|
|
|
#include <Kernel/Net/IPv4Socket.h>
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-09-06 18:46:46 +02:00
|
|
|
class TCPSocket final : public IPv4Socket {
|
2019-03-14 12:20:38 +01:00
|
|
|
public:
|
2020-04-18 12:50:35 +03:00
|
|
|
static void for_each(Function<void(const TCPSocket&)>);
|
2021-11-08 00:51:39 +01:00
|
|
|
static ErrorOr<NonnullRefPtr<TCPSocket>> try_create(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer);
|
2019-03-14 12:20:38 +01:00
|
|
|
virtual ~TCPSocket() override;
|
|
|
|
|
2019-08-09 12:48:28 +10:00
|
|
|
enum class Direction {
|
|
|
|
Unspecified,
|
|
|
|
Outgoing,
|
|
|
|
Incoming,
|
|
|
|
Passive,
|
|
|
|
};
|
|
|
|
|
2021-08-05 20:41:44 +02:00
|
|
|
static StringView to_string(Direction direction)
|
2019-08-09 12:48:28 +10:00
|
|
|
{
|
|
|
|
switch (direction) {
|
|
|
|
case Direction::Unspecified:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "Unspecified"sv;
|
2019-08-09 12:48:28 +10:00
|
|
|
case Direction::Outgoing:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "Outgoing"sv;
|
2019-08-09 12:48:28 +10:00
|
|
|
case Direction::Incoming:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "Incoming"sv;
|
2019-08-09 12:48:28 +10:00
|
|
|
case Direction::Passive:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "Passive"sv;
|
2019-08-09 12:48:28 +10:00
|
|
|
default:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "None"sv;
|
2019-08-09 12:48:28 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class State {
|
2019-08-06 23:40:38 +10:00
|
|
|
Closed,
|
|
|
|
Listen,
|
|
|
|
SynSent,
|
|
|
|
SynReceived,
|
|
|
|
Established,
|
|
|
|
CloseWait,
|
|
|
|
LastAck,
|
|
|
|
FinWait1,
|
|
|
|
FinWait2,
|
|
|
|
Closing,
|
|
|
|
TimeWait,
|
2019-03-14 12:20:38 +01:00
|
|
|
};
|
|
|
|
|
2021-08-05 20:41:44 +02:00
|
|
|
static StringView to_string(State state)
|
2019-08-06 23:40:38 +10:00
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case State::Closed:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "Closed"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
case State::Listen:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "Listen"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
case State::SynSent:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "SynSent"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
case State::SynReceived:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "SynReceived"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
case State::Established:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "Established"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
case State::CloseWait:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "CloseWait"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
case State::LastAck:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "LastAck"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
case State::FinWait1:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "FinWait1"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
case State::FinWait2:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "FinWait2"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
case State::Closing:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "Closing"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
case State::TimeWait:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "TimeWait"sv;
|
2019-08-06 23:40:38 +10:00
|
|
|
default:
|
|
|
|
return "None";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-10 13:18:18 +10:00
|
|
|
enum class Error {
|
|
|
|
None,
|
|
|
|
FINDuringConnect,
|
|
|
|
RSTDuringConnect,
|
|
|
|
UnexpectedFlagsDuringConnect,
|
2021-05-13 10:49:10 +02:00
|
|
|
RetransmitTimeout,
|
2019-08-10 13:18:18 +10:00
|
|
|
};
|
|
|
|
|
2021-08-05 20:41:44 +02:00
|
|
|
static StringView to_string(Error error)
|
2019-08-10 13:18:18 +10:00
|
|
|
{
|
|
|
|
switch (error) {
|
|
|
|
case Error::None:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "None"sv;
|
2019-08-10 13:18:18 +10:00
|
|
|
case Error::FINDuringConnect:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "FINDuringConnect"sv;
|
2019-08-10 13:18:18 +10:00
|
|
|
case Error::RSTDuringConnect:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "RSTDuringConnect"sv;
|
2019-08-10 13:18:18 +10:00
|
|
|
case Error::UnexpectedFlagsDuringConnect:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "UnexpectedFlagsDuringConnect"sv;
|
2019-08-10 13:18:18 +10:00
|
|
|
default:
|
2021-08-05 20:41:44 +02:00
|
|
|
return "Invalid"sv;
|
2019-08-10 13:18:18 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 12:20:38 +01:00
|
|
|
State state() const { return m_state; }
|
2019-08-10 13:14:00 +10:00
|
|
|
void set_state(State state);
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2019-08-09 12:48:28 +10:00
|
|
|
Direction direction() const { return m_direction; }
|
|
|
|
|
2019-08-10 13:18:18 +10:00
|
|
|
bool has_error() const { return m_error != Error::None; }
|
|
|
|
Error error() const { return m_error; }
|
|
|
|
void set_error(Error error) { m_error = error; }
|
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
void set_ack_number(u32 n) { m_ack_number = n; }
|
|
|
|
void set_sequence_number(u32 n) { m_sequence_number = n; }
|
|
|
|
u32 ack_number() const { return m_ack_number; }
|
|
|
|
u32 sequence_number() const { return m_sequence_number; }
|
2019-08-08 12:32:35 +10:00
|
|
|
u32 packets_in() const { return m_packets_in; }
|
|
|
|
u32 bytes_in() const { return m_bytes_in; }
|
|
|
|
u32 packets_out() const { return m_packets_out; }
|
|
|
|
u32 bytes_out() const { return m_bytes_out; }
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2021-05-12 08:13:53 +02:00
|
|
|
// FIXME: Make this configurable?
|
|
|
|
static constexpr u32 maximum_duplicate_acks = 5;
|
|
|
|
void set_duplicate_acks(u32 acks) { m_duplicate_acks = acks; }
|
|
|
|
u32 duplicate_acks() const { return m_duplicate_acks; }
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<void> send_ack(bool allow_duplicate = false);
|
|
|
|
ErrorOr<void> send_tcp_packet(u16 flags, const UserOrKernelBuffer* = nullptr, size_t = 0, RoutingDecision* = nullptr);
|
2019-09-08 17:38:08 +10:00
|
|
|
void receive_tcp_packet(const TCPPacket&, u16 size);
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2021-05-12 09:14:37 +02:00
|
|
|
bool should_delay_next_ack() const;
|
|
|
|
|
2021-08-21 23:31:15 +02:00
|
|
|
static MutexProtected<HashMap<IPv4SocketTuple, TCPSocket*>>& sockets_by_tuple();
|
2019-09-08 17:16:40 +10:00
|
|
|
static RefPtr<TCPSocket> from_tuple(const IPv4SocketTuple& tuple);
|
2019-03-14 12:28:30 +01:00
|
|
|
|
2021-08-21 23:31:15 +02:00
|
|
|
static MutexProtected<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& closing_sockets();
|
2020-02-08 15:52:32 +01:00
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<NonnullRefPtr<TCPSocket>> try_create_client(IPv4Address const& local_address, u16 local_port, IPv4Address const& peer_address, u16 peer_port);
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
void set_originator(TCPSocket& originator) { m_originator = originator; }
|
2019-09-08 19:35:41 +10:00
|
|
|
bool has_originator() { return !!m_originator; }
|
2019-09-08 17:18:28 +10:00
|
|
|
void release_to_originator();
|
|
|
|
void release_for_accept(RefPtr<TCPSocket>);
|
2019-08-09 12:48:28 +10:00
|
|
|
|
2021-05-13 10:49:10 +02:00
|
|
|
void retransmit_packets();
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> close() override;
|
2020-02-08 15:52:32 +01:00
|
|
|
|
2021-09-07 13:39:11 +02:00
|
|
|
virtual bool can_write(const OpenFileDescription&, size_t) const override;
|
2021-05-26 06:26:20 +02:00
|
|
|
|
2021-07-18 18:19:25 -04:00
|
|
|
static NetworkOrdered<u16> compute_tcp_checksum(IPv4Address const& source, IPv4Address const& destination, TCPPacket const&, u16 payload_size);
|
|
|
|
|
2019-08-09 12:48:28 +10:00
|
|
|
protected:
|
|
|
|
void set_direction(Direction direction) { m_direction = direction; }
|
|
|
|
|
2019-03-14 12:20:38 +01:00
|
|
|
private:
|
2021-09-07 15:11:49 +02:00
|
|
|
explicit TCPSocket(int protocol, NonnullOwnPtr<DoubleBuffer> receive_buffer, NonnullOwnPtr<KBuffer> scratch_buffer);
|
2021-10-02 15:24:00 -07:00
|
|
|
virtual StringView class_name() const override { return "TCPSocket"sv; }
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2020-02-08 15:52:32 +01:00
|
|
|
virtual void shut_down_for_writing() override;
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<size_t> protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, int flags) override;
|
|
|
|
virtual ErrorOr<size_t> protocol_send(const UserOrKernelBuffer&, size_t) override;
|
|
|
|
virtual ErrorOr<void> protocol_connect(OpenFileDescription&, ShouldBlock) override;
|
|
|
|
virtual ErrorOr<u16> protocol_allocate_local_port() override;
|
2019-03-14 15:23:32 +01:00
|
|
|
virtual bool protocol_is_disconnected() const override;
|
2021-11-08 00:51:39 +01:00
|
|
|
virtual ErrorOr<void> protocol_bind() override;
|
|
|
|
virtual ErrorOr<void> protocol_listen(bool did_allocate_port) override;
|
2019-03-14 12:20:38 +01:00
|
|
|
|
2021-05-13 10:49:10 +02:00
|
|
|
void enqueue_for_retransmit();
|
|
|
|
void dequeue_for_retransmit();
|
|
|
|
|
2019-09-08 19:35:41 +10:00
|
|
|
WeakPtr<TCPSocket> m_originator;
|
2019-09-08 17:18:28 +10:00
|
|
|
HashMap<IPv4SocketTuple, NonnullRefPtr<TCPSocket>> m_pending_release_for_accept;
|
2019-08-09 12:48:28 +10:00
|
|
|
Direction m_direction { Direction::Unspecified };
|
2019-08-10 13:18:18 +10:00
|
|
|
Error m_error { Error::None };
|
2020-02-08 00:19:46 +01:00
|
|
|
RefPtr<NetworkAdapter> m_adapter;
|
2019-07-03 21:17:35 +02:00
|
|
|
u32 m_sequence_number { 0 };
|
|
|
|
u32 m_ack_number { 0 };
|
2019-08-06 23:40:38 +10:00
|
|
|
State m_state { State::Closed };
|
2019-08-08 12:32:35 +10:00
|
|
|
u32 m_packets_in { 0 };
|
|
|
|
u32 m_bytes_in { 0 };
|
|
|
|
u32 m_packets_out { 0 };
|
|
|
|
u32 m_bytes_out { 0 };
|
2019-09-08 17:38:08 +10:00
|
|
|
|
|
|
|
struct OutgoingPacket {
|
2020-02-08 01:43:55 +01:00
|
|
|
u32 ack_number { 0 };
|
2021-05-26 05:35:05 +02:00
|
|
|
RefPtr<PacketWithTimestamp> buffer;
|
|
|
|
size_t ipv4_payload_offset;
|
|
|
|
WeakPtr<NetworkAdapter> adapter;
|
2019-09-08 17:38:08 +10:00
|
|
|
int tx_counter { 0 };
|
|
|
|
};
|
|
|
|
|
2021-08-07 15:42:11 +02:00
|
|
|
struct UnackedPackets {
|
|
|
|
SinglyLinkedList<OutgoingPacket> packets;
|
|
|
|
size_t size { 0 };
|
|
|
|
};
|
|
|
|
|
2021-08-21 23:31:15 +02:00
|
|
|
MutexProtected<UnackedPackets> m_unacked_packets;
|
2021-05-12 08:13:53 +02:00
|
|
|
|
|
|
|
u32 m_duplicate_acks { 0 };
|
2021-05-12 09:14:37 +02:00
|
|
|
|
|
|
|
u32 m_last_ack_number_sent { 0 };
|
|
|
|
Time m_last_ack_sent_time;
|
2021-05-13 10:49:10 +02:00
|
|
|
|
|
|
|
// FIXME: Make this configurable (sysctl)
|
|
|
|
static constexpr u32 maximum_retransmits = 5;
|
|
|
|
Time m_last_retransmit_time;
|
|
|
|
u32 m_retransmit_attempts { 0 };
|
2021-05-26 06:26:20 +02:00
|
|
|
|
|
|
|
// FIXME: Parse window size TCP option from the peer
|
|
|
|
u32 m_send_window_size { 64 * KiB };
|
2021-08-15 16:37:45 +02:00
|
|
|
|
|
|
|
IntrusiveListNode<TCPSocket> m_retransmit_list_node;
|
|
|
|
|
|
|
|
public:
|
2021-09-09 16:30:59 +04:30
|
|
|
using RetransmitList = IntrusiveList<&TCPSocket::m_retransmit_list_node>;
|
2021-08-21 23:31:15 +02:00
|
|
|
static MutexProtected<TCPSocket::RetransmitList>& sockets_for_retransmit();
|
2019-03-14 12:20:38 +01:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|