2019-04-02 17:54:38 +00:00
|
|
|
#include <Kernel/Net/TCPSocket.h>
|
|
|
|
#include <Kernel/Net/TCP.h>
|
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2019-04-02 13:46:44 +00:00
|
|
|
#include <Kernel/Net/Routing.h>
|
2019-04-02 17:54:38 +00:00
|
|
|
#include <Kernel/Process.h>
|
2019-04-03 10:36:40 +00:00
|
|
|
#include <Kernel/Devices/RandomDevice.h>
|
2019-03-14 11:20:38 +00:00
|
|
|
|
2019-03-14 11:28:30 +00:00
|
|
|
Lockable<HashMap<word, TCPSocket*>>& TCPSocket::sockets_by_port()
|
|
|
|
{
|
|
|
|
static Lockable<HashMap<word, TCPSocket*>>* s_map;
|
|
|
|
if (!s_map)
|
|
|
|
s_map = new Lockable<HashMap<word, TCPSocket*>>;
|
|
|
|
return *s_map;
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocketHandle TCPSocket::from_port(word port)
|
|
|
|
{
|
|
|
|
RetainPtr<TCPSocket> socket;
|
|
|
|
{
|
|
|
|
LOCKER(sockets_by_port().lock());
|
|
|
|
auto it = sockets_by_port().resource().find(port);
|
|
|
|
if (it == sockets_by_port().resource().end())
|
|
|
|
return { };
|
|
|
|
socket = (*it).value;
|
|
|
|
ASSERT(socket);
|
|
|
|
}
|
|
|
|
return { move(socket) };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-14 11:20:38 +00:00
|
|
|
TCPSocket::TCPSocket(int protocol)
|
|
|
|
: IPv4Socket(SOCK_STREAM, protocol)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TCPSocket::~TCPSocket()
|
|
|
|
{
|
2019-03-14 11:28:30 +00:00
|
|
|
LOCKER(sockets_by_port().lock());
|
|
|
|
sockets_by_port().resource().remove(source_port());
|
2019-03-14 11:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Retained<TCPSocket> TCPSocket::create(int protocol)
|
|
|
|
{
|
|
|
|
return adopt(*new TCPSocket(protocol));
|
|
|
|
}
|
|
|
|
|
|
|
|
int TCPSocket::protocol_receive(const ByteBuffer& packet_buffer, void* buffer, size_t buffer_size, int flags, sockaddr* addr, socklen_t* addr_length)
|
|
|
|
{
|
|
|
|
(void)flags;
|
|
|
|
(void)addr_length;
|
|
|
|
ASSERT(!packet_buffer.is_null());
|
|
|
|
auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
|
|
|
|
auto& tcp_packet = *static_cast<const TCPPacket*>(ipv4_packet.payload());
|
|
|
|
size_t payload_size = packet_buffer.size() - sizeof(IPv4Packet) - tcp_packet.header_size();
|
2019-03-14 18:57:54 +00:00
|
|
|
kprintf("payload_size %u, will it fit in %u?\n", payload_size, buffer_size);
|
2019-03-14 11:20:38 +00:00
|
|
|
ASSERT(buffer_size >= payload_size);
|
|
|
|
if (addr) {
|
|
|
|
auto& ia = *(sockaddr_in*)addr;
|
|
|
|
ia.sin_port = htons(tcp_packet.destination_port());
|
|
|
|
}
|
|
|
|
memcpy(buffer, tcp_packet.payload(), payload_size);
|
|
|
|
return payload_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TCPSocket::protocol_send(const void* data, int data_length)
|
|
|
|
{
|
2019-04-02 13:46:44 +00:00
|
|
|
auto* adapter = adapter_for_route_to(destination_address());
|
2019-03-14 11:20:38 +00:00
|
|
|
if (!adapter)
|
2019-04-02 13:46:44 +00:00
|
|
|
return -EHOSTUNREACH;
|
2019-03-14 11:20:38 +00:00
|
|
|
send_tcp_packet(TCPFlags::PUSH | TCPFlags::ACK, data, data_length);
|
|
|
|
return data_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPSocket::send_tcp_packet(word flags, const void* payload, int payload_size)
|
|
|
|
{
|
2019-04-02 13:46:44 +00:00
|
|
|
// FIXME: Maybe the socket should be bound to an adapter instead of looking it up every time?
|
|
|
|
auto* adapter = adapter_for_route_to(destination_address());
|
|
|
|
ASSERT(adapter);
|
2019-03-14 11:20:38 +00:00
|
|
|
|
|
|
|
auto buffer = ByteBuffer::create_zeroed(sizeof(TCPPacket) + payload_size);
|
|
|
|
auto& tcp_packet = *(TCPPacket*)(buffer.pointer());
|
|
|
|
ASSERT(source_port());
|
|
|
|
tcp_packet.set_source_port(source_port());
|
|
|
|
tcp_packet.set_destination_port(destination_port());
|
|
|
|
tcp_packet.set_window_size(1024);
|
|
|
|
tcp_packet.set_sequence_number(m_sequence_number);
|
|
|
|
tcp_packet.set_data_offset(sizeof(TCPPacket) / sizeof(dword));
|
|
|
|
tcp_packet.set_flags(flags);
|
|
|
|
|
|
|
|
if (flags & TCPFlags::ACK)
|
|
|
|
tcp_packet.set_ack_number(m_ack_number);
|
|
|
|
|
|
|
|
if (flags == TCPFlags::SYN) {
|
|
|
|
++m_sequence_number;
|
|
|
|
} else {
|
|
|
|
m_sequence_number += payload_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(tcp_packet.payload(), payload, payload_size);
|
2019-04-02 13:46:44 +00:00
|
|
|
tcp_packet.set_checksum(compute_tcp_checksum(adapter->ipv4_address(), destination_address(), tcp_packet, payload_size));
|
2019-03-14 11:20:38 +00:00
|
|
|
kprintf("sending tcp packet from %s:%u to %s:%u with (%s %s) seq_no=%u, ack_no=%u\n",
|
2019-04-02 13:46:44 +00:00
|
|
|
adapter->ipv4_address().to_string().characters(),
|
2019-03-14 11:20:38 +00:00
|
|
|
source_port(),
|
|
|
|
destination_address().to_string().characters(),
|
|
|
|
destination_port(),
|
|
|
|
tcp_packet.has_syn() ? "SYN" : "",
|
|
|
|
tcp_packet.has_ack() ? "ACK" : "",
|
|
|
|
tcp_packet.sequence_number(),
|
|
|
|
tcp_packet.ack_number()
|
|
|
|
);
|
2019-04-02 13:46:44 +00:00
|
|
|
adapter->send_ipv4(MACAddress(), destination_address(), IPv4Protocol::TCP, move(buffer));
|
2019-03-14 11:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NetworkOrdered<word> TCPSocket::compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket& packet, word payload_size)
|
|
|
|
{
|
|
|
|
struct [[gnu::packed]] PseudoHeader {
|
|
|
|
IPv4Address source;
|
|
|
|
IPv4Address destination;
|
|
|
|
byte zero;
|
|
|
|
byte protocol;
|
|
|
|
NetworkOrdered<word> payload_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
PseudoHeader pseudo_header { source, destination, 0, (byte)IPv4Protocol::TCP, sizeof(TCPPacket) + payload_size };
|
|
|
|
|
|
|
|
dword checksum = 0;
|
|
|
|
auto* w = (const NetworkOrdered<word>*)&pseudo_header;
|
|
|
|
for (size_t i = 0; i < sizeof(pseudo_header) / sizeof(word); ++i) {
|
|
|
|
checksum += w[i];
|
|
|
|
if (checksum > 0xffff)
|
|
|
|
checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
}
|
|
|
|
w = (const NetworkOrdered<word>*)&packet;
|
|
|
|
for (size_t i = 0; i < sizeof(packet) / sizeof(word); ++i) {
|
|
|
|
checksum += w[i];
|
|
|
|
if (checksum > 0xffff)
|
|
|
|
checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
}
|
|
|
|
ASSERT(packet.data_offset() * 4 == sizeof(TCPPacket));
|
|
|
|
w = (const NetworkOrdered<word>*)packet.payload();
|
|
|
|
for (size_t i = 0; i < payload_size / sizeof(word); ++i) {
|
|
|
|
checksum += w[i];
|
|
|
|
if (checksum > 0xffff)
|
|
|
|
checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
}
|
|
|
|
if (payload_size & 1) {
|
2019-03-14 18:57:54 +00:00
|
|
|
word expanded_byte = ((const byte*)packet.payload())[payload_size - 1] << 8;
|
2019-03-14 11:20:38 +00:00
|
|
|
checksum += expanded_byte;
|
|
|
|
if (checksum > 0xffff)
|
|
|
|
checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
}
|
|
|
|
return ~(checksum & 0xffff);
|
|
|
|
}
|
|
|
|
|
2019-05-03 18:15:54 +00:00
|
|
|
KResult TCPSocket::protocol_connect(FileDescriptor& descriptor, ShouldBlock should_block)
|
2019-03-14 11:20:38 +00:00
|
|
|
{
|
2019-04-02 13:46:44 +00:00
|
|
|
auto* adapter = adapter_for_route_to(destination_address());
|
2019-03-14 11:20:38 +00:00
|
|
|
if (!adapter)
|
2019-04-02 13:46:44 +00:00
|
|
|
return KResult(-EHOSTUNREACH);
|
2019-03-14 11:20:38 +00:00
|
|
|
|
|
|
|
allocate_source_port_if_needed();
|
|
|
|
|
|
|
|
m_sequence_number = 0;
|
|
|
|
m_ack_number = 0;
|
|
|
|
|
|
|
|
send_tcp_packet(TCPFlags::SYN);
|
|
|
|
m_state = State::Connecting;
|
|
|
|
|
2019-04-08 02:52:21 +00:00
|
|
|
if (should_block == ShouldBlock::Yes) {
|
2019-05-03 18:15:54 +00:00
|
|
|
current->block(Thread::BlockedConnect, descriptor);
|
2019-04-08 02:52:21 +00:00
|
|
|
ASSERT(is_connected());
|
|
|
|
return KSuccess;
|
|
|
|
}
|
2019-03-14 11:20:38 +00:00
|
|
|
|
2019-04-08 02:52:21 +00:00
|
|
|
return KResult(-EINPROGRESS);
|
2019-03-14 11:20:38 +00:00
|
|
|
}
|
2019-03-14 11:28:30 +00:00
|
|
|
|
2019-03-18 03:03:44 +00:00
|
|
|
int TCPSocket::protocol_allocate_source_port()
|
2019-03-14 11:28:30 +00:00
|
|
|
{
|
2019-03-18 03:03:44 +00:00
|
|
|
static const word first_ephemeral_port = 32768;
|
|
|
|
static const word last_ephemeral_port = 60999;
|
|
|
|
static const word ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port;
|
2019-04-22 21:38:33 +00:00
|
|
|
word first_scan_port = first_ephemeral_port + RandomDevice::random_value() % ephemeral_port_range_size;
|
2019-03-18 03:03:44 +00:00
|
|
|
|
2019-03-14 11:28:30 +00:00
|
|
|
LOCKER(sockets_by_port().lock());
|
2019-03-18 03:03:44 +00:00
|
|
|
for (word port = first_scan_port;;) {
|
2019-03-14 11:28:30 +00:00
|
|
|
auto it = sockets_by_port().resource().find(port);
|
|
|
|
if (it == sockets_by_port().resource().end()) {
|
|
|
|
set_source_port(port);
|
|
|
|
sockets_by_port().resource().set(port, this);
|
2019-03-18 03:03:44 +00:00
|
|
|
return port;
|
2019-03-14 11:28:30 +00:00
|
|
|
}
|
2019-03-18 03:03:44 +00:00
|
|
|
++port;
|
|
|
|
if (port > last_ephemeral_port)
|
|
|
|
port = first_ephemeral_port;
|
|
|
|
if (port == first_scan_port)
|
|
|
|
break;
|
2019-03-14 11:28:30 +00:00
|
|
|
}
|
2019-03-18 03:03:44 +00:00
|
|
|
return -EADDRINUSE;
|
2019-03-14 11:28:30 +00:00
|
|
|
}
|
2019-03-14 14:23:32 +00:00
|
|
|
|
|
|
|
bool TCPSocket::protocol_is_disconnected() const
|
|
|
|
{
|
|
|
|
return m_state == State::Disconnecting || m_state == State::Disconnected;
|
|
|
|
}
|