2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
#include <AK/Singleton.h>
|
2019-09-08 12:40:26 +03:00
|
|
|
#include <AK/Time.h>
|
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>
|
2020-01-03 14:36:30 +03:00
|
|
|
#include <Kernel/Random.h>
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-08-04 11:04:06 +03:00
|
|
|
//#define TCP_SOCKET_DEBUG
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-04-18 12:50:35 +03:00
|
|
|
void TCPSocket::for_each(Function<void(const TCPSocket&)> callback)
|
2019-08-06 16:40:38 +03:00
|
|
|
{
|
2020-04-18 12:50:35 +03:00
|
|
|
LOCKER(sockets_by_tuple().lock(), Lock::Mode::Shared);
|
2019-08-06 16:40:38 +03:00
|
|
|
for (auto& it : sockets_by_tuple().resource())
|
2019-08-09 13:26:29 +03:00
|
|
|
callback(*it.value);
|
2019-08-06 16:40:38 +03:00
|
|
|
}
|
|
|
|
|
2019-08-10 06:14:00 +03:00
|
|
|
void TCPSocket::set_state(State new_state)
|
|
|
|
{
|
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
2020-03-01 22:45:39 +03:00
|
|
|
dbg() << "TCPSocket{" << this << "} state moving from " << to_string(m_state) << " to " << to_string(new_state);
|
2019-08-10 06:14:00 +03:00
|
|
|
#endif
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
auto was_disconnected = protocol_is_disconnected();
|
|
|
|
auto previous_role = m_role;
|
|
|
|
|
2019-08-10 06:14:00 +03:00
|
|
|
m_state = new_state;
|
2019-08-11 16:38:20 +03:00
|
|
|
|
|
|
|
if (new_state == State::Established && m_direction == Direction::Outgoing)
|
|
|
|
m_role = Role::Connected;
|
2020-02-08 17:52:32 +03:00
|
|
|
|
|
|
|
if (new_state == State::Closed) {
|
|
|
|
LOCKER(closing_sockets().lock());
|
|
|
|
closing_sockets().resource().remove(tuple());
|
|
|
|
}
|
2020-11-30 02:05:27 +03:00
|
|
|
|
|
|
|
if (previous_role != m_role || was_disconnected != protocol_is_disconnected())
|
|
|
|
evaluate_block_conditions();
|
2020-02-08 17:52:32 +03:00
|
|
|
}
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
static AK::Singleton<Lockable<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>> s_socket_closing;
|
|
|
|
|
2020-02-08 17:52:32 +03:00
|
|
|
Lockable<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& TCPSocket::closing_sockets()
|
|
|
|
{
|
2020-08-25 04:35:19 +03:00
|
|
|
return *s_socket_closing;
|
2019-08-10 06:14:00 +03:00
|
|
|
}
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
static AK::Singleton<Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>> s_socket_tuples;
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>& TCPSocket::sockets_by_tuple()
|
2019-03-14 14:28:30 +03:00
|
|
|
{
|
2020-08-25 04:35:19 +03:00
|
|
|
return *s_socket_tuples;
|
2019-03-14 14:28:30 +03:00
|
|
|
}
|
|
|
|
|
2019-09-08 10:16:40 +03:00
|
|
|
RefPtr<TCPSocket> TCPSocket::from_tuple(const IPv4SocketTuple& tuple)
|
2019-03-14 14:28:30 +03:00
|
|
|
{
|
2020-04-18 12:50:35 +03:00
|
|
|
LOCKER(sockets_by_tuple().lock(), Lock::Mode::Shared);
|
2019-08-09 05:48:28 +03:00
|
|
|
|
|
|
|
auto exact_match = sockets_by_tuple().resource().get(tuple);
|
|
|
|
if (exact_match.has_value())
|
2019-08-09 10:16:12 +03:00
|
|
|
return { *exact_match.value() };
|
2019-08-09 05:48:28 +03:00
|
|
|
|
|
|
|
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())
|
2019-08-09 10:16:12 +03:00
|
|
|
return { *address_match.value() };
|
2019-08-09 05:48:28 +03:00
|
|
|
|
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())
|
2019-08-09 10:16:12 +03:00
|
|
|
return { *wildcard_match.value() };
|
2019-08-09 05:38:56 +03:00
|
|
|
|
2019-08-09 05:48:28 +03:00
|
|
|
return {};
|
2019-03-14 14:28:30 +03:00
|
|
|
}
|
|
|
|
|
2019-09-08 10:16:40 +03:00
|
|
|
RefPtr<TCPSocket> TCPSocket::from_endpoints(const IPv4Address& local_address, u16 local_port, const IPv4Address& peer_address, u16 peer_port)
|
2019-08-06 16:40:38 +03:00
|
|
|
{
|
|
|
|
return from_tuple(IPv4SocketTuple(local_address, local_port, peer_address, peer_port));
|
|
|
|
}
|
|
|
|
|
2019-09-08 10:16:40 +03:00
|
|
|
RefPtr<TCPSocket> TCPSocket::create_client(const IPv4Address& new_local_address, u16 new_local_port, const IPv4Address& new_peer_address, u16 new_peer_port)
|
2019-08-09 05:48:28 +03:00
|
|
|
{
|
|
|
|
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());
|
|
|
|
|
2019-08-10 06:17:00 +03:00
|
|
|
client->set_setup_state(SetupState::InProgress);
|
2019-08-09 05:48:28 +03:00
|
|
|
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);
|
2019-09-08 10:18:28 +03:00
|
|
|
client->set_originator(*this);
|
2019-08-09 05:48:28 +03:00
|
|
|
|
2019-09-08 10:18:28 +03:00
|
|
|
m_pending_release_for_accept.set(tuple, client);
|
2019-08-09 05:48:28 +03:00
|
|
|
sockets_by_tuple().resource().set(tuple, client);
|
|
|
|
|
|
|
|
return from_tuple(tuple);
|
|
|
|
}
|
|
|
|
|
2019-09-08 10:18:28 +03:00
|
|
|
void TCPSocket::release_to_originator()
|
|
|
|
{
|
|
|
|
ASSERT(!!m_originator);
|
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-30 01:26:13 +03:00
|
|
|
m_originator.strong_ref()->release_for_accept(this);
|
2019-09-08 10:18:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void TCPSocket::release_for_accept(RefPtr<TCPSocket> socket)
|
|
|
|
{
|
|
|
|
ASSERT(m_pending_release_for_accept.contains(socket->tuple()));
|
|
|
|
m_pending_release_for_accept.remove(socket->tuple());
|
2020-08-05 12:13:30 +03:00
|
|
|
// FIXME: Should we observe this error somehow?
|
2020-12-21 02:09:48 +03:00
|
|
|
[[maybe_unused]] auto rc = queue_connection_from(*socket);
|
2019-09-08 10:18:28 +03:00
|
|
|
}
|
|
|
|
|
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());
|
2020-02-08 17:52:32 +03:00
|
|
|
|
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
|
|
|
dbg() << "~TCPSocket in state " << to_string(state());
|
|
|
|
#endif
|
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));
|
|
|
|
}
|
|
|
|
|
2020-12-21 02:09:48 +03:00
|
|
|
KResultOr<size_t> TCPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags)
|
2019-03-14 14:20:38 +03:00
|
|
|
{
|
2020-12-18 18:13:23 +03:00
|
|
|
auto& ipv4_packet = *reinterpret_cast<const IPv4Packet*>(raw_ipv4_packet.data());
|
2019-03-14 14:20:38 +03:00
|
|
|
auto& tcp_packet = *static_cast<const TCPPacket*>(ipv4_packet.payload());
|
2020-12-18 18:13:23 +03:00
|
|
|
size_t payload_size = raw_ipv4_packet.size() - sizeof(IPv4Packet) - tcp_packet.header_size();
|
2019-08-04 11:04:06 +03:00
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
2020-03-01 22:45:39 +03:00
|
|
|
klog() << "payload_size " << payload_size << ", will it fit in " << buffer_size << "?";
|
2019-08-04 11:04:06 +03:00
|
|
|
#endif
|
2019-03-14 14:20:38 +03:00
|
|
|
ASSERT(buffer_size >= payload_size);
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!buffer.write(tcp_packet.payload(), payload_size))
|
|
|
|
return KResult(-EFAULT);
|
2019-03-14 14:20:38 +03:00
|
|
|
return payload_size;
|
|
|
|
}
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
KResultOr<size_t> TCPSocket::protocol_send(const UserOrKernelBuffer& data, size_t data_length)
|
2019-03-14 14:20:38 +03:00
|
|
|
{
|
2020-09-12 06:11:07 +03:00
|
|
|
int err = send_tcp_packet(TCPFlags::PUSH | TCPFlags::ACK, &data, data_length);
|
|
|
|
if (err < 0)
|
|
|
|
return KResult(err);
|
2019-03-14 14:20:38 +03:00
|
|
|
return data_length;
|
|
|
|
}
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
int TCPSocket::send_tcp_packet(u16 flags, const UserOrKernelBuffer* payload, size_t payload_size)
|
2019-03-14 14:20:38 +03:00
|
|
|
{
|
2020-12-18 21:18:19 +03:00
|
|
|
const size_t buffer_size = sizeof(TCPPacket) + payload_size;
|
|
|
|
alignas(TCPPacket) u8 buffer[buffer_size];
|
|
|
|
new (buffer) TCPPacket;
|
|
|
|
auto& tcp_packet = *(TCPPacket*)(buffer);
|
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);
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
if (payload && !payload->read(tcp_packet.payload(), payload_size))
|
|
|
|
return -EFAULT;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
tcp_packet.set_checksum(compute_tcp_checksum(local_address(), peer_address(), tcp_packet, payload_size));
|
2019-09-08 10:38:08 +03:00
|
|
|
|
|
|
|
if (tcp_packet.has_syn() || payload_size > 0) {
|
2019-11-23 23:44:32 +03:00
|
|
|
LOCKER(m_not_acked_lock);
|
2020-12-18 21:18:19 +03:00
|
|
|
m_not_acked.append({ m_sequence_number, ByteBuffer::copy(buffer, buffer_size) });
|
2019-09-08 10:38:08 +03:00
|
|
|
send_outgoing_packets();
|
2020-09-12 06:11:07 +03:00
|
|
|
return 0;
|
2019-10-17 21:46:04 +03:00
|
|
|
}
|
|
|
|
|
2020-04-04 23:46:45 +03:00
|
|
|
auto routing_decision = route_to(peer_address(), local_address(), bound_interface());
|
2019-10-17 21:46:04 +03:00
|
|
|
ASSERT(!routing_decision.is_zero());
|
2019-09-08 10:38:08 +03:00
|
|
|
|
2020-12-18 21:18:19 +03:00
|
|
|
auto packet_buffer = UserOrKernelBuffer::for_kernel_buffer(buffer);
|
2020-09-12 06:11:07 +03:00
|
|
|
int err = routing_decision.adapter->send_ipv4(
|
2019-10-17 21:46:04 +03:00
|
|
|
routing_decision.next_hop, peer_address(), IPv4Protocol::TCP,
|
2020-12-18 21:18:19 +03:00
|
|
|
packet_buffer, buffer_size, ttl());
|
2020-09-12 06:11:07 +03:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
2019-09-08 10:38:08 +03:00
|
|
|
|
2019-10-17 21:46:04 +03:00
|
|
|
m_packets_out++;
|
2020-12-18 21:18:19 +03:00
|
|
|
m_bytes_out += buffer_size;
|
2020-09-12 06:11:07 +03:00
|
|
|
return 0;
|
2019-09-08 10:38:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void TCPSocket::send_outgoing_packets()
|
|
|
|
{
|
2020-04-04 23:46:45 +03:00
|
|
|
auto routing_decision = route_to(peer_address(), local_address(), bound_interface());
|
2019-09-08 10:38:08 +03:00
|
|
|
ASSERT(!routing_decision.is_zero());
|
|
|
|
|
|
|
|
auto now = kgettimeofday();
|
|
|
|
|
2020-04-18 12:50:35 +03:00
|
|
|
LOCKER(m_not_acked_lock, Lock::Mode::Shared);
|
2019-09-08 10:38:08 +03:00
|
|
|
for (auto& packet : m_not_acked) {
|
2019-09-08 12:40:26 +03:00
|
|
|
timeval diff;
|
|
|
|
timeval_sub(packet.tx_time, now, diff);
|
2020-02-08 03:43:55 +03:00
|
|
|
if (diff.tv_sec == 0 && diff.tv_usec <= 500000)
|
2019-09-08 10:38:08 +03:00
|
|
|
continue;
|
|
|
|
packet.tx_time = now;
|
|
|
|
packet.tx_counter++;
|
|
|
|
|
2019-08-04 11:04:06 +03:00
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
2019-09-30 09:57:01 +03:00
|
|
|
auto& tcp_packet = *(TCPPacket*)(packet.buffer.data());
|
2020-03-01 22:45:39 +03:00
|
|
|
klog() << "sending tcp packet from " << local_address().to_string().characters() << ":" << local_port() << " to " << peer_address().to_string().characters() << ":" << peer_port() << " with (" << (tcp_packet.has_syn() ? "SYN " : "") << (tcp_packet.has_ack() ? "ACK " : "") << (tcp_packet.has_fin() ? "FIN " : "") << (tcp_packet.has_rst() ? "RST " : "") << ") seq_no=" << tcp_packet.sequence_number() << ", ack_no=" << tcp_packet.ack_number() << ", tx_counter=" << packet.tx_counter;
|
2019-08-04 11:04:06 +03:00
|
|
|
#endif
|
2020-09-12 06:11:07 +03:00
|
|
|
auto packet_buffer = UserOrKernelBuffer::for_kernel_buffer(packet.buffer.data());
|
|
|
|
int err = routing_decision.adapter->send_ipv4(
|
2019-09-08 10:38:08 +03:00
|
|
|
routing_decision.next_hop, peer_address(), IPv4Protocol::TCP,
|
2020-09-12 06:11:07 +03:00
|
|
|
packet_buffer, packet.buffer.size(), ttl());
|
|
|
|
if (err < 0) {
|
|
|
|
auto& tcp_packet = *(TCPPacket*)(packet.buffer.data());
|
|
|
|
klog() << "Error (" << err << ") sending tcp packet from " << local_address().to_string().characters() << ":" << local_port() << " to " << peer_address().to_string().characters() << ":" << peer_port() << " with (" << (tcp_packet.has_syn() ? "SYN " : "") << (tcp_packet.has_ack() ? "ACK " : "") << (tcp_packet.has_fin() ? "FIN " : "") << (tcp_packet.has_rst() ? "RST " : "") << ") seq_no=" << tcp_packet.sequence_number() << ", ack_no=" << tcp_packet.ack_number() << ", tx_counter=" << packet.tx_counter;
|
|
|
|
} else {
|
|
|
|
m_packets_out++;
|
|
|
|
m_bytes_out += packet.buffer.size();
|
|
|
|
}
|
2019-09-08 10:38:08 +03:00
|
|
|
}
|
2019-08-08 05:32:35 +03:00
|
|
|
}
|
|
|
|
|
2019-09-08 10:38:08 +03:00
|
|
|
void TCPSocket::receive_tcp_packet(const TCPPacket& packet, u16 size)
|
2019-08-08 05:32:35 +03:00
|
|
|
{
|
2019-09-08 10:38:08 +03:00
|
|
|
if (packet.has_ack()) {
|
|
|
|
u32 ack_number = packet.ack_number();
|
|
|
|
|
2019-09-08 12:13:50 +03:00
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
2019-09-08 10:38:08 +03:00
|
|
|
dbg() << "TCPSocket: receive_tcp_packet: " << ack_number;
|
2019-09-08 12:13:50 +03:00
|
|
|
#endif
|
2019-09-08 10:38:08 +03:00
|
|
|
|
|
|
|
int removed = 0;
|
2019-11-23 23:44:32 +03:00
|
|
|
LOCKER(m_not_acked_lock);
|
2019-09-08 10:38:08 +03:00
|
|
|
while (!m_not_acked.is_empty()) {
|
|
|
|
auto& packet = m_not_acked.first();
|
|
|
|
|
2019-09-08 12:13:50 +03:00
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
2019-09-08 10:38:08 +03:00
|
|
|
dbg() << "TCPSocket: iterate: " << packet.ack_number;
|
2019-09-08 12:13:50 +03:00
|
|
|
#endif
|
2019-09-08 10:38:08 +03:00
|
|
|
|
|
|
|
if (packet.ack_number <= ack_number) {
|
|
|
|
m_not_acked.take_first();
|
|
|
|
removed++;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-08 12:13:50 +03:00
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
2019-09-08 10:38:08 +03:00
|
|
|
dbg() << "TCPSocket: receive_tcp_packet acknowledged " << removed << " packets";
|
2019-09-08 12:13:50 +03:00
|
|
|
#endif
|
2019-09-08 10:38:08 +03:00
|
|
|
}
|
|
|
|
|
2019-08-08 05:32:35 +03:00
|
|
|
m_packets_in++;
|
2019-09-08 10:38:08 +03:00
|
|
|
m_bytes_in += packet.header_size() + 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
|
|
|
{
|
2020-12-31 00:44:54 +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-10 06:17:00 +03:00
|
|
|
set_setup_state(SetupState::Completed);
|
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
|
|
|
{
|
2020-10-21 21:51:02 +03:00
|
|
|
Locker locker(lock());
|
|
|
|
|
2019-08-28 14:58:01 +03:00
|
|
|
auto routing_decision = route_to(peer_address(), local_address());
|
2019-08-29 04:18:38 +03:00
|
|
|
if (routing_decision.is_zero())
|
2019-08-28 14:58:01 +03:00
|
|
|
return KResult(-EHOSTUNREACH);
|
|
|
|
if (!has_specific_local_address())
|
|
|
|
set_local_address(routing_decision.adapter->ipv4_address());
|
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
|
|
|
|
2020-01-08 18:03:01 +03:00
|
|
|
m_sequence_number = get_good_random<u32>();
|
2019-03-14 14:20:38 +03:00
|
|
|
m_ack_number = 0;
|
|
|
|
|
2019-08-10 06:17:00 +03:00
|
|
|
set_setup_state(SetupState::InProgress);
|
2020-09-12 06:11:07 +03:00
|
|
|
int err = send_tcp_packet(TCPFlags::SYN);
|
|
|
|
if (err < 0)
|
|
|
|
return KResult(err);
|
2019-08-06 16:40:38 +03:00
|
|
|
m_state = State::SynSent;
|
2019-08-11 16:38:20 +03:00
|
|
|
m_role = Role::Connecting;
|
2019-08-09 05:48:28 +03:00
|
|
|
m_direction = Direction::Outgoing;
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
evaluate_block_conditions();
|
|
|
|
|
2019-04-08 05:52:21 +03:00
|
|
|
if (should_block == ShouldBlock::Yes) {
|
2020-10-21 21:51:02 +03:00
|
|
|
locker.unlock();
|
2020-11-30 02:05:27 +03:00
|
|
|
auto unblock_flags = Thread::FileBlocker::BlockFlags::None;
|
|
|
|
if (Thread::current()->block<Thread::ConnectBlocker>(nullptr, description, unblock_flags).was_interrupted())
|
2019-07-20 12:05:52 +03:00
|
|
|
return KResult(-EINTR);
|
2020-10-21 21:51:02 +03:00
|
|
|
locker.lock();
|
2019-08-10 06:17:00 +03:00
|
|
|
ASSERT(setup_state() == SetupState::Completed);
|
2020-11-30 02:05:27 +03:00
|
|
|
if (has_error()) { // TODO: check unblock_flags
|
2019-08-11 16:38:20 +03:00
|
|
|
m_role = Role::None;
|
2019-08-10 06:18:18 +03:00
|
|
|
return KResult(-ECONNREFUSED);
|
2019-08-11 16:38:20 +03:00
|
|
|
}
|
2019-04-08 05:52:21 +03:00
|
|
|
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;
|
2020-01-03 14:36:30 +03:00
|
|
|
u16 first_scan_port = first_ephemeral_port + get_good_random<u16>() % 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
|
|
|
}
|
2020-02-08 17:52:32 +03:00
|
|
|
|
|
|
|
void TCPSocket::shut_down_for_writing()
|
|
|
|
{
|
|
|
|
if (state() == State::Established) {
|
2020-02-08 18:00:31 +03:00
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
2021-01-09 20:51:44 +03:00
|
|
|
dbgln(" Sending FIN/ACK from Established and moving into FinWait1");
|
2020-02-08 18:00:31 +03:00
|
|
|
#endif
|
2020-12-21 02:09:48 +03:00
|
|
|
[[maybe_unused]] auto rc = send_tcp_packet(TCPFlags::FIN | TCPFlags::ACK);
|
2020-02-08 17:52:32 +03:00
|
|
|
set_state(State::FinWait1);
|
|
|
|
} else {
|
2021-01-10 17:43:09 +03:00
|
|
|
dbgln(" Shutting down TCPSocket for writing but not moving to FinWait1 since state is {}", to_string(state()));
|
2020-02-08 17:52:32 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 19:20:05 +03:00
|
|
|
KResult TCPSocket::close()
|
2020-02-08 17:52:32 +03:00
|
|
|
{
|
2020-10-21 21:51:02 +03:00
|
|
|
Locker socket_locker(lock());
|
2020-06-02 19:20:05 +03:00
|
|
|
auto result = IPv4Socket::close();
|
2020-02-08 17:52:32 +03:00
|
|
|
if (state() == State::CloseWait) {
|
2020-02-08 18:00:31 +03:00
|
|
|
#ifdef TCP_SOCKET_DEBUG
|
2021-01-09 20:51:44 +03:00
|
|
|
dbgln(" Sending FIN from CloseWait and moving into LastAck");
|
2020-02-08 18:00:31 +03:00
|
|
|
#endif
|
2020-12-21 02:09:48 +03:00
|
|
|
[[maybe_unused]] auto rc = send_tcp_packet(TCPFlags::FIN | TCPFlags::ACK);
|
2020-02-08 17:52:32 +03:00
|
|
|
set_state(State::LastAck);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOCKER(closing_sockets().lock());
|
|
|
|
closing_sockets().resource().set(tuple(), *this);
|
2020-06-02 19:20:05 +03:00
|
|
|
return result;
|
2020-02-08 17:52:32 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|