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
|
|
|
*/
|
|
|
|
|
2019-03-14 11:20:38 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-06 13:40:38 +00:00
|
|
|
#include <AK/Function.h>
|
2019-09-08 07:38:08 +00:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/SinglyLinkedList.h>
|
2019-08-09 02:34:32 +00:00
|
|
|
#include <AK/WeakPtr.h>
|
2021-05-13 08:01:38 +00:00
|
|
|
#include <Kernel/KResult.h>
|
2019-04-02 17:54:38 +00:00
|
|
|
#include <Kernel/Net/IPv4Socket.h>
|
2019-03-14 11:20:38 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-09-06 16:46:46 +00:00
|
|
|
class TCPSocket final : public IPv4Socket {
|
2019-03-14 11:20:38 +00:00
|
|
|
public:
|
2020-04-18 09:50:35 +00:00
|
|
|
static void for_each(Function<void(const TCPSocket&)>);
|
2021-05-13 08:01:38 +00:00
|
|
|
static KResultOr<NonnullRefPtr<TCPSocket>> create(int protocol);
|
2019-03-14 11:20:38 +00:00
|
|
|
virtual ~TCPSocket() override;
|
|
|
|
|
2019-08-09 02:48:28 +00:00
|
|
|
enum class Direction {
|
|
|
|
Unspecified,
|
|
|
|
Outgoing,
|
|
|
|
Incoming,
|
|
|
|
Passive,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* to_string(Direction direction)
|
|
|
|
{
|
|
|
|
switch (direction) {
|
|
|
|
case Direction::Unspecified:
|
|
|
|
return "Unspecified";
|
|
|
|
case Direction::Outgoing:
|
|
|
|
return "Outgoing";
|
|
|
|
case Direction::Incoming:
|
|
|
|
return "Incoming";
|
|
|
|
case Direction::Passive:
|
|
|
|
return "Passive";
|
|
|
|
default:
|
|
|
|
return "None";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 15:13:23 +00:00
|
|
|
enum class State {
|
2019-08-06 13:40:38 +00:00
|
|
|
Closed,
|
|
|
|
Listen,
|
|
|
|
SynSent,
|
|
|
|
SynReceived,
|
|
|
|
Established,
|
|
|
|
CloseWait,
|
|
|
|
LastAck,
|
|
|
|
FinWait1,
|
|
|
|
FinWait2,
|
|
|
|
Closing,
|
|
|
|
TimeWait,
|
2019-03-14 11:20:38 +00:00
|
|
|
};
|
|
|
|
|
2019-08-06 13:40:38 +00:00
|
|
|
static const char* to_string(State state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case State::Closed:
|
|
|
|
return "Closed";
|
|
|
|
case State::Listen:
|
|
|
|
return "Listen";
|
|
|
|
case State::SynSent:
|
|
|
|
return "SynSent";
|
|
|
|
case State::SynReceived:
|
|
|
|
return "SynReceived";
|
|
|
|
case State::Established:
|
|
|
|
return "Established";
|
|
|
|
case State::CloseWait:
|
|
|
|
return "CloseWait";
|
|
|
|
case State::LastAck:
|
|
|
|
return "LastAck";
|
|
|
|
case State::FinWait1:
|
|
|
|
return "FinWait1";
|
|
|
|
case State::FinWait2:
|
|
|
|
return "FinWait2";
|
|
|
|
case State::Closing:
|
|
|
|
return "Closing";
|
|
|
|
case State::TimeWait:
|
|
|
|
return "TimeWait";
|
|
|
|
default:
|
|
|
|
return "None";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-10 03:18:18 +00:00
|
|
|
enum class Error {
|
|
|
|
None,
|
|
|
|
FINDuringConnect,
|
|
|
|
RSTDuringConnect,
|
|
|
|
UnexpectedFlagsDuringConnect,
|
2021-05-13 08:49:10 +00:00
|
|
|
RetransmitTimeout,
|
2019-08-10 03:18:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char* to_string(Error error)
|
|
|
|
{
|
|
|
|
switch (error) {
|
|
|
|
case Error::None:
|
|
|
|
return "None";
|
|
|
|
case Error::FINDuringConnect:
|
|
|
|
return "FINDuringConnect";
|
|
|
|
case Error::RSTDuringConnect:
|
|
|
|
return "RSTDuringConnect";
|
|
|
|
case Error::UnexpectedFlagsDuringConnect:
|
|
|
|
return "UnexpectedFlagsDuringConnect";
|
|
|
|
default:
|
|
|
|
return "Invalid";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 11:20:38 +00:00
|
|
|
State state() const { return m_state; }
|
2019-08-10 03:14:00 +00:00
|
|
|
void set_state(State state);
|
2019-03-14 11:20:38 +00:00
|
|
|
|
2019-08-09 02:48:28 +00:00
|
|
|
Direction direction() const { return m_direction; }
|
|
|
|
|
2019-08-10 03:18:18 +00: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 19:17:35 +00: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 02:32:35 +00: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 11:20:38 +00:00
|
|
|
|
2021-05-12 06:13:53 +00: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-05-12 07:14:37 +00:00
|
|
|
KResult send_ack(bool allow_duplicate = false);
|
2021-05-25 19:29:37 +00:00
|
|
|
KResult send_tcp_packet(u16 flags, const UserOrKernelBuffer* = nullptr, size_t = 0, RoutingDecision* = nullptr);
|
2019-09-08 07:38:08 +00:00
|
|
|
void receive_tcp_packet(const TCPPacket&, u16 size);
|
2019-03-14 11:20:38 +00:00
|
|
|
|
2021-05-12 07:14:37 +00:00
|
|
|
bool should_delay_next_ack() const;
|
|
|
|
|
2019-08-06 13:40:38 +00:00
|
|
|
static Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>& sockets_by_tuple();
|
2019-09-08 07:16:40 +00:00
|
|
|
static RefPtr<TCPSocket> from_tuple(const IPv4SocketTuple& tuple);
|
2019-03-14 11:28:30 +00:00
|
|
|
|
2020-02-08 14:52:32 +00:00
|
|
|
static Lockable<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& closing_sockets();
|
|
|
|
|
2019-09-08 07:16:40 +00:00
|
|
|
RefPtr<TCPSocket> create_client(const IPv4Address& local_address, u16 local_port, const IPv4Address& 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 22:26:13 +00:00
|
|
|
void set_originator(TCPSocket& originator) { m_originator = originator; }
|
2019-09-08 09:35:41 +00:00
|
|
|
bool has_originator() { return !!m_originator; }
|
2019-09-08 07:18:28 +00:00
|
|
|
void release_to_originator();
|
|
|
|
void release_for_accept(RefPtr<TCPSocket>);
|
2019-08-09 02:48:28 +00:00
|
|
|
|
2021-05-13 08:49:10 +00:00
|
|
|
static Lockable<HashTable<TCPSocket*>>& sockets_for_retransmit();
|
|
|
|
void retransmit_packets();
|
|
|
|
|
2020-06-02 16:20:05 +00:00
|
|
|
virtual KResult close() override;
|
2020-02-08 14:52:32 +00:00
|
|
|
|
2019-08-09 02:48:28 +00:00
|
|
|
protected:
|
|
|
|
void set_direction(Direction direction) { m_direction = direction; }
|
|
|
|
|
2019-03-14 11:20:38 +00:00
|
|
|
private:
|
|
|
|
explicit TCPSocket(int protocol);
|
2019-05-03 18:42:43 +00:00
|
|
|
virtual const char* class_name() const override { return "TCPSocket"; }
|
2019-03-14 11:20:38 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
static NetworkOrdered<u16> compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket&, u16 payload_size);
|
2019-03-14 11:20:38 +00:00
|
|
|
|
2020-02-08 14:52:32 +00:00
|
|
|
virtual void shut_down_for_writing() override;
|
|
|
|
|
2020-12-18 15:13:23 +00:00
|
|
|
virtual KResultOr<size_t> protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, int flags) override;
|
2020-09-12 03:11:07 +00:00
|
|
|
virtual KResultOr<size_t> protocol_send(const UserOrKernelBuffer&, size_t) override;
|
2019-06-07 07:36:51 +00:00
|
|
|
virtual KResult protocol_connect(FileDescription&, ShouldBlock) override;
|
2021-04-30 10:26:25 +00:00
|
|
|
virtual KResultOr<u16> protocol_allocate_local_port() override;
|
2019-03-14 14:23:32 +00:00
|
|
|
virtual bool protocol_is_disconnected() const override;
|
2019-05-03 19:51:40 +00:00
|
|
|
virtual KResult protocol_bind() override;
|
2019-08-06 13:40:38 +00:00
|
|
|
virtual KResult protocol_listen() override;
|
2019-03-14 11:20:38 +00:00
|
|
|
|
2021-05-13 08:49:10 +00:00
|
|
|
void enqueue_for_retransmit();
|
|
|
|
void dequeue_for_retransmit();
|
|
|
|
|
2019-09-08 09:35:41 +00:00
|
|
|
WeakPtr<TCPSocket> m_originator;
|
2019-09-08 07:18:28 +00:00
|
|
|
HashMap<IPv4SocketTuple, NonnullRefPtr<TCPSocket>> m_pending_release_for_accept;
|
2019-08-09 02:48:28 +00:00
|
|
|
Direction m_direction { Direction::Unspecified };
|
2019-08-10 03:18:18 +00:00
|
|
|
Error m_error { Error::None };
|
2020-02-07 23:19:46 +00:00
|
|
|
RefPtr<NetworkAdapter> m_adapter;
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 m_sequence_number { 0 };
|
|
|
|
u32 m_ack_number { 0 };
|
2019-08-06 13:40:38 +00:00
|
|
|
State m_state { State::Closed };
|
2019-08-08 02:32:35 +00:00
|
|
|
u32 m_packets_in { 0 };
|
|
|
|
u32 m_bytes_in { 0 };
|
|
|
|
u32 m_packets_out { 0 };
|
|
|
|
u32 m_bytes_out { 0 };
|
2019-09-08 07:38:08 +00:00
|
|
|
|
|
|
|
struct OutgoingPacket {
|
2020-02-08 00:43:55 +00:00
|
|
|
u32 ack_number { 0 };
|
2021-05-15 20:45:22 +00:00
|
|
|
NetworkByteBuffer buffer;
|
2019-09-08 07:38:08 +00:00
|
|
|
int tx_counter { 0 };
|
|
|
|
};
|
|
|
|
|
2019-11-23 20:44:32 +00:00
|
|
|
Lock m_not_acked_lock { "TCPSocket unacked packets" };
|
2019-09-08 07:38:08 +00:00
|
|
|
SinglyLinkedList<OutgoingPacket> m_not_acked;
|
2021-05-12 06:13:53 +00:00
|
|
|
|
|
|
|
u32 m_duplicate_acks { 0 };
|
2021-05-12 07:14:37 +00:00
|
|
|
|
|
|
|
u32 m_last_ack_number_sent { 0 };
|
|
|
|
Time m_last_ack_sent_time;
|
2021-05-13 08:49:10 +00:00
|
|
|
|
|
|
|
// FIXME: Make this configurable (sysctl)
|
|
|
|
static constexpr u32 maximum_retransmits = 5;
|
|
|
|
Time m_last_retransmit_time;
|
|
|
|
u32 m_retransmit_attempts { 0 };
|
2019-03-14 11:20:38 +00:00
|
|
|
};
|
2020-02-16 00:27:42 +00:00
|
|
|
|
|
|
|
}
|