2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Devices/RandomDevice.h>
|
2019-08-06 16:40:38 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2019-04-02 16:46:44 +03:00
|
|
|
#include <Kernel/Net/Routing.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/TCP.h>
|
|
|
|
#include <Kernel/Net/TCPSocket.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Process.h>
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-08-04 11:04:06 +03:00
|
|
|
//#define TCP_SOCKET_DEBUG
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
void TCPSocket::for_each(Function<void(TCPSocket*&)> callback)
|
|
|
|
{
|
|
|
|
LOCKER(sockets_by_tuple().lock());
|
|
|
|
for (auto& it : sockets_by_tuple().resource())
|
|
|
|
callback(it.value);
|
|
|
|
}
|
|
|
|
|
|
|
|
Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>& TCPSocket::sockets_by_tuple()
|
2019-03-14 14:28:30 +03:00
|
|
|
{
|
2019-08-06 16:40:38 +03:00
|
|
|
static Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>* s_map;
|
2019-03-14 14:28:30 +03:00
|
|
|
if (!s_map)
|
2019-08-06 16:40:38 +03:00
|
|
|
s_map = new Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>;
|
2019-03-14 14:28:30 +03:00
|
|
|
return *s_map;
|
|
|
|
}
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
TCPSocketHandle TCPSocket::from_tuple(const IPv4SocketTuple& tuple)
|
2019-03-14 14:28:30 +03:00
|
|
|
{
|
2019-08-09 05:48:28 +03:00
|
|
|
LOCKER(sockets_by_tuple().lock());
|
|
|
|
|
|
|
|
auto exact_match = sockets_by_tuple().resource().get(tuple);
|
|
|
|
if (exact_match.has_value())
|
|
|
|
return { move(exact_match.value()) };
|
|
|
|
|
|
|
|
auto address_tuple = IPv4SocketTuple(tuple.local_address(), tuple.local_port(), IPv4Address(), 0);
|
|
|
|
auto address_match = sockets_by_tuple().resource().get(address_tuple);
|
|
|
|
if (address_match.has_value())
|
|
|
|
return { move(address_match.value()) };
|
|
|
|
|
2019-08-09 05:38:56 +03:00
|
|
|
auto wildcard_tuple = IPv4SocketTuple(IPv4Address(), tuple.local_port(), IPv4Address(), 0);
|
|
|
|
auto wildcard_match = sockets_by_tuple().resource().get(wildcard_tuple);
|
|
|
|
if (wildcard_match.has_value())
|
|
|
|
return { move(wildcard_match.value()) };
|
|
|
|
|
2019-08-09 05:48:28 +03:00
|
|
|
return {};
|
2019-03-14 14:28:30 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
TCPSocketHandle TCPSocket::from_endpoints(const IPv4Address& local_address, u16 local_port, const IPv4Address& peer_address, u16 peer_port)
|
|
|
|
{
|
|
|
|
return from_tuple(IPv4SocketTuple(local_address, local_port, peer_address, peer_port));
|
|
|
|
}
|
|
|
|
|
2019-08-09 05:48:28 +03:00
|
|
|
TCPSocketHandle TCPSocket::create_client(const IPv4Address& new_local_address, u16 new_local_port, const IPv4Address& new_peer_address, u16 new_peer_port)
|
|
|
|
{
|
|
|
|
auto tuple = IPv4SocketTuple(new_local_address, new_local_port, new_peer_address, new_peer_port);
|
|
|
|
|
|
|
|
LOCKER(sockets_by_tuple().lock());
|
|
|
|
if (sockets_by_tuple().resource().contains(tuple))
|
|
|
|
return {};
|
|
|
|
|
|
|
|
auto client = TCPSocket::create(protocol());
|
|
|
|
|
|
|
|
client->set_local_address(new_local_address);
|
|
|
|
client->set_local_port(new_local_port);
|
|
|
|
client->set_peer_address(new_peer_address);
|
|
|
|
client->set_peer_port(new_peer_port);
|
|
|
|
client->set_direction(Direction::Incoming);
|
|
|
|
|
|
|
|
queue_connection_from(client);
|
|
|
|
|
|
|
|
sockets_by_tuple().resource().set(tuple, client);
|
|
|
|
|
|
|
|
return from_tuple(tuple);
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:20:38 +03:00
|
|
|
TCPSocket::TCPSocket(int protocol)
|
|
|
|
: IPv4Socket(SOCK_STREAM, protocol)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocket::~TCPSocket()
|
|
|
|
{
|
2019-08-06 16:40:38 +03:00
|
|
|
LOCKER(sockets_by_tuple().lock());
|
|
|
|
sockets_by_tuple().resource().remove(tuple());
|
2019-03-14 14:20:38 +03:00
|
|
|
}
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
NonnullRefPtr<TCPSocket> TCPSocket::create(int protocol)
|
2019-03-14 14:20:38 +03:00
|
|
|
{
|
|
|
|
return adopt(*new TCPSocket(protocol));
|
|
|
|
}
|
|
|
|
|
2019-08-04 22:07:50 +03:00
|
|
|
int TCPSocket::protocol_receive(const KBuffer& packet_buffer, void* buffer, size_t buffer_size, int flags)
|
2019-03-14 14:20:38 +03:00
|
|
|
{
|
|
|
|
(void)flags;
|
2019-08-04 22:07:50 +03:00
|
|
|
auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.data());
|
2019-03-14 14:20:38 +03:00
|
|
|
auto& tcp_packet = *static_cast<const TCPPacket*>(ipv4_packet.payload());
|
|
|
|
size_t payload_size = packet_buffer.size() - sizeof(IPv4Packet) - tcp_packet.header_size();
|
2019-08-04 11:04:06 +03:00
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
2019-03-14 21:57:54 +03:00
|
|
|
kprintf("payload_size %u, will it fit in %u?\n", payload_size, buffer_size);
|
2019-08-04 11:04:06 +03:00
|
|
|
#endif
|
2019-03-14 14:20:38 +03:00
|
|
|
ASSERT(buffer_size >= payload_size);
|
|
|
|
memcpy(buffer, tcp_packet.payload(), payload_size);
|
|
|
|
return payload_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TCPSocket::protocol_send(const void* data, int data_length)
|
|
|
|
{
|
|
|
|
send_tcp_packet(TCPFlags::PUSH | TCPFlags::ACK, data, data_length);
|
|
|
|
return data_length;
|
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void TCPSocket::send_tcp_packet(u16 flags, const void* payload, int payload_size)
|
2019-03-14 14:20:38 +03:00
|
|
|
{
|
2019-08-09 05:34:32 +03:00
|
|
|
if (!m_adapter) {
|
|
|
|
if (has_specific_local_address()) {
|
|
|
|
m_adapter = NetworkAdapter::from_ipv4_address(local_address());
|
|
|
|
} else {
|
|
|
|
m_adapter = adapter_for_route_to(peer_address());
|
|
|
|
if (m_adapter)
|
|
|
|
set_local_address(m_adapter->ipv4_address());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ASSERT(!!m_adapter);
|
2019-03-14 14:20:38 +03:00
|
|
|
|
|
|
|
auto buffer = ByteBuffer::create_zeroed(sizeof(TCPPacket) + payload_size);
|
|
|
|
auto& tcp_packet = *(TCPPacket*)(buffer.pointer());
|
2019-05-04 17:40:34 +03:00
|
|
|
ASSERT(local_port());
|
|
|
|
tcp_packet.set_source_port(local_port());
|
|
|
|
tcp_packet.set_destination_port(peer_port());
|
2019-03-14 14:20:38 +03:00
|
|
|
tcp_packet.set_window_size(1024);
|
|
|
|
tcp_packet.set_sequence_number(m_sequence_number);
|
2019-07-03 22:17:35 +03:00
|
|
|
tcp_packet.set_data_offset(sizeof(TCPPacket) / sizeof(u32));
|
2019-03-14 14:20:38 +03:00
|
|
|
tcp_packet.set_flags(flags);
|
|
|
|
|
|
|
|
if (flags & TCPFlags::ACK)
|
|
|
|
tcp_packet.set_ack_number(m_ack_number);
|
|
|
|
|
2019-08-09 05:48:28 +03:00
|
|
|
if (flags & TCPFlags::SYN) {
|
2019-03-14 14:20:38 +03:00
|
|
|
++m_sequence_number;
|
|
|
|
} else {
|
|
|
|
m_sequence_number += payload_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(tcp_packet.payload(), payload, payload_size);
|
2019-08-06 16:40:38 +03:00
|
|
|
tcp_packet.set_checksum(compute_tcp_checksum(local_address(), peer_address(), tcp_packet, payload_size));
|
2019-08-04 11:04:06 +03:00
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
2019-08-06 16:40:38 +03:00
|
|
|
kprintf("sending tcp packet from %s:%u to %s:%u with (%s%s%s%s) seq_no=%u, ack_no=%u\n",
|
|
|
|
local_address().to_string().characters(),
|
2019-05-04 17:40:34 +03:00
|
|
|
local_port(),
|
|
|
|
peer_address().to_string().characters(),
|
|
|
|
peer_port(),
|
2019-08-09 05:35:56 +03:00
|
|
|
tcp_packet.has_syn() ? "SYN " : "",
|
|
|
|
tcp_packet.has_ack() ? "ACK " : "",
|
|
|
|
tcp_packet.has_fin() ? "FIN " : "",
|
|
|
|
tcp_packet.has_rst() ? "RST " : "",
|
2019-03-14 14:20:38 +03:00
|
|
|
tcp_packet.sequence_number(),
|
2019-06-07 12:43:58 +03:00
|
|
|
tcp_packet.ack_number());
|
2019-08-04 11:04:06 +03:00
|
|
|
#endif
|
2019-08-06 16:40:38 +03:00
|
|
|
m_adapter->send_ipv4(MACAddress(), peer_address(), IPv4Protocol::TCP, buffer.data(), buffer.size());
|
2019-08-08 05:32:35 +03:00
|
|
|
|
|
|
|
m_packets_out++;
|
|
|
|
m_bytes_out += buffer.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPSocket::record_incoming_data(int size)
|
|
|
|
{
|
|
|
|
m_packets_in++;
|
|
|
|
m_bytes_in += size;
|
2019-03-14 14:20:38 +03:00
|
|
|
}
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
NetworkOrdered<u16> TCPSocket::compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket& packet, u16 payload_size)
|
2019-03-14 14:20:38 +03:00
|
|
|
{
|
2019-06-07 12:43:58 +03:00
|
|
|
struct [[gnu::packed]] PseudoHeader
|
|
|
|
{
|
2019-03-14 14:20:38 +03:00
|
|
|
IPv4Address source;
|
|
|
|
IPv4Address destination;
|
2019-07-03 22:17:35 +03:00
|
|
|
u8 zero;
|
|
|
|
u8 protocol;
|
|
|
|
NetworkOrdered<u16> payload_size;
|
2019-03-14 14:20:38 +03:00
|
|
|
};
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
PseudoHeader pseudo_header { source, destination, 0, (u8)IPv4Protocol::TCP, sizeof(TCPPacket) + payload_size };
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 checksum = 0;
|
|
|
|
auto* w = (const NetworkOrdered<u16>*)&pseudo_header;
|
|
|
|
for (size_t i = 0; i < sizeof(pseudo_header) / sizeof(u16); ++i) {
|
2019-03-14 14:20:38 +03:00
|
|
|
checksum += w[i];
|
|
|
|
if (checksum > 0xffff)
|
|
|
|
checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
}
|
2019-07-03 22:17:35 +03:00
|
|
|
w = (const NetworkOrdered<u16>*)&packet;
|
|
|
|
for (size_t i = 0; i < sizeof(packet) / sizeof(u16); ++i) {
|
2019-03-14 14:20:38 +03:00
|
|
|
checksum += w[i];
|
|
|
|
if (checksum > 0xffff)
|
|
|
|
checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
}
|
|
|
|
ASSERT(packet.data_offset() * 4 == sizeof(TCPPacket));
|
2019-07-03 22:17:35 +03:00
|
|
|
w = (const NetworkOrdered<u16>*)packet.payload();
|
|
|
|
for (size_t i = 0; i < payload_size / sizeof(u16); ++i) {
|
2019-03-14 14:20:38 +03:00
|
|
|
checksum += w[i];
|
|
|
|
if (checksum > 0xffff)
|
|
|
|
checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
}
|
|
|
|
if (payload_size & 1) {
|
2019-07-03 22:17:35 +03:00
|
|
|
u16 expanded_byte = ((const u8*)packet.payload())[payload_size - 1] << 8;
|
2019-03-14 14:20:38 +03:00
|
|
|
checksum += expanded_byte;
|
|
|
|
if (checksum > 0xffff)
|
|
|
|
checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
}
|
|
|
|
return ~(checksum & 0xffff);
|
|
|
|
}
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
KResult TCPSocket::protocol_bind()
|
|
|
|
{
|
2019-08-09 05:38:56 +03:00
|
|
|
if (has_specific_local_address() && !m_adapter) {
|
2019-08-06 16:40:38 +03:00
|
|
|
m_adapter = NetworkAdapter::from_ipv4_address(local_address());
|
|
|
|
if (!m_adapter)
|
|
|
|
return KResult(-EADDRNOTAVAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
KResult TCPSocket::protocol_listen()
|
|
|
|
{
|
|
|
|
LOCKER(sockets_by_tuple().lock());
|
|
|
|
if (sockets_by_tuple().resource().contains(tuple()))
|
|
|
|
return KResult(-EADDRINUSE);
|
|
|
|
sockets_by_tuple().resource().set(tuple(), this);
|
2019-08-09 05:48:28 +03:00
|
|
|
set_direction(Direction::Passive);
|
2019-08-06 16:40:38 +03:00
|
|
|
set_state(State::Listen);
|
2019-08-09 05:48:28 +03:00
|
|
|
set_connected(true);
|
2019-08-06 16:40:38 +03:00
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
KResult TCPSocket::protocol_connect(FileDescription& description, ShouldBlock should_block)
|
2019-03-14 14:20:38 +03:00
|
|
|
{
|
2019-08-06 16:40:38 +03:00
|
|
|
if (!m_adapter) {
|
2019-08-09 05:38:56 +03:00
|
|
|
if (has_specific_local_address()) {
|
|
|
|
m_adapter = NetworkAdapter::from_ipv4_address(local_address());
|
|
|
|
if (!m_adapter)
|
|
|
|
return KResult(-EADDRNOTAVAIL);
|
|
|
|
} else {
|
|
|
|
m_adapter = adapter_for_route_to(peer_address());
|
|
|
|
if (!m_adapter)
|
|
|
|
return KResult(-EHOSTUNREACH);
|
2019-08-06 16:40:38 +03:00
|
|
|
|
2019-08-09 05:38:56 +03:00
|
|
|
set_local_address(m_adapter->ipv4_address());
|
|
|
|
}
|
2019-08-06 16:40:38 +03:00
|
|
|
}
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-05-04 17:40:34 +03:00
|
|
|
allocate_local_port_if_needed();
|
2019-03-14 14:20:38 +03:00
|
|
|
|
|
|
|
m_sequence_number = 0;
|
|
|
|
m_ack_number = 0;
|
|
|
|
|
|
|
|
send_tcp_packet(TCPFlags::SYN);
|
2019-08-06 16:40:38 +03:00
|
|
|
m_state = State::SynSent;
|
2019-08-09 05:48:28 +03:00
|
|
|
m_direction = Direction::Outgoing;
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-04-08 05:52:21 +03:00
|
|
|
if (should_block == ShouldBlock::Yes) {
|
2019-07-20 12:05:52 +03:00
|
|
|
if (current->block<Thread::ConnectBlocker>(description) == Thread::BlockResult::InterruptedBySignal)
|
|
|
|
return KResult(-EINTR);
|
2019-04-08 05:52:21 +03:00
|
|
|
ASSERT(is_connected());
|
|
|
|
return KSuccess;
|
|
|
|
}
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-04-08 05:52:21 +03:00
|
|
|
return KResult(-EINPROGRESS);
|
2019-03-14 14:20:38 +03:00
|
|
|
}
|
2019-03-14 14:28:30 +03:00
|
|
|
|
2019-05-04 17:40:34 +03:00
|
|
|
int TCPSocket::protocol_allocate_local_port()
|
2019-03-14 14:28:30 +03:00
|
|
|
{
|
2019-07-03 22:17:35 +03:00
|
|
|
static const u16 first_ephemeral_port = 32768;
|
|
|
|
static const u16 last_ephemeral_port = 60999;
|
|
|
|
static const u16 ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port;
|
|
|
|
u16 first_scan_port = first_ephemeral_port + RandomDevice::random_value() % ephemeral_port_range_size;
|
2019-03-18 06:03:44 +03:00
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
LOCKER(sockets_by_tuple().lock());
|
2019-07-03 22:17:35 +03:00
|
|
|
for (u16 port = first_scan_port;;) {
|
2019-08-06 16:40:38 +03:00
|
|
|
IPv4SocketTuple proposed_tuple(local_address(), port, peer_address(), peer_port());
|
|
|
|
|
|
|
|
auto it = sockets_by_tuple().resource().find(proposed_tuple);
|
|
|
|
if (it == sockets_by_tuple().resource().end()) {
|
2019-05-04 17:40:34 +03:00
|
|
|
set_local_port(port);
|
2019-08-06 16:40:38 +03:00
|
|
|
sockets_by_tuple().resource().set(proposed_tuple, this);
|
2019-03-18 06:03:44 +03:00
|
|
|
return port;
|
2019-03-14 14:28:30 +03:00
|
|
|
}
|
2019-03-18 06:03:44 +03:00
|
|
|
++port;
|
|
|
|
if (port > last_ephemeral_port)
|
|
|
|
port = first_ephemeral_port;
|
|
|
|
if (port == first_scan_port)
|
|
|
|
break;
|
2019-03-14 14:28:30 +03:00
|
|
|
}
|
2019-03-18 06:03:44 +03:00
|
|
|
return -EADDRINUSE;
|
2019-03-14 14:28:30 +03:00
|
|
|
}
|
2019-03-14 17:23:32 +03:00
|
|
|
|
|
|
|
bool TCPSocket::protocol_is_disconnected() const
|
|
|
|
{
|
2019-08-06 16:40:38 +03:00
|
|
|
switch (m_state) {
|
|
|
|
case State::Closed:
|
|
|
|
case State::CloseWait:
|
|
|
|
case State::LastAck:
|
|
|
|
case State::FinWait1:
|
|
|
|
case State::FinWait2:
|
|
|
|
case State::Closing:
|
|
|
|
case State::TimeWait:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-03 22:51:40 +03:00
|
|
|
}
|