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.
|
|
|
|
*/
|
|
|
|
|
2019-08-10 19:10:36 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/Net/ARP.h>
|
|
|
|
#include <Kernel/Net/ICMP.h>
|
|
|
|
#include <Kernel/Net/IPv4.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/IPv4Socket.h>
|
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/Routing.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/TCP.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/TCPSocket.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/UDP.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/UDPSocket.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-03-12 17:51:42 +03:00
|
|
|
#include <LibC/errno_numbers.h>
|
2019-09-23 20:06:03 +03:00
|
|
|
#include <LibC/sys/ioctl_numbers.h>
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-08-04 11:04:06 +03:00
|
|
|
//#define IPV4_SOCKET_DEBUG
|
2019-03-13 16:22:27 +03:00
|
|
|
|
2019-03-12 19:27:07 +03:00
|
|
|
Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
|
|
|
|
{
|
|
|
|
static Lockable<HashTable<IPv4Socket*>>* s_table;
|
|
|
|
if (!s_table)
|
|
|
|
s_table = new Lockable<HashTable<IPv4Socket*>>;
|
|
|
|
return *s_table;
|
|
|
|
}
|
|
|
|
|
2020-01-23 20:11:14 +03:00
|
|
|
KResultOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2019-03-14 14:20:38 +03:00
|
|
|
if (type == SOCK_STREAM)
|
|
|
|
return TCPSocket::create(protocol);
|
2019-03-14 14:43:18 +03:00
|
|
|
if (type == SOCK_DGRAM)
|
|
|
|
return UDPSocket::create(protocol);
|
2020-01-23 20:11:14 +03:00
|
|
|
if (type == SOCK_RAW)
|
|
|
|
return adopt(*new IPv4Socket(type, protocol));
|
|
|
|
return KResult(-EINVAL);
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IPv4Socket::IPv4Socket(int type, int protocol)
|
|
|
|
: Socket(AF_INET, type, protocol)
|
|
|
|
{
|
2019-10-18 17:47:29 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-03-24 00:03:17 +03:00
|
|
|
kprintf("%s(%u) IPv4Socket{%p} created with type=%u, protocol=%d\n", current->process().name().characters(), current->pid(), this, type, protocol);
|
2019-10-18 17:47:29 +03:00
|
|
|
#endif
|
2019-12-14 11:43:31 +03:00
|
|
|
m_buffer_mode = type == SOCK_STREAM ? BufferMode::Bytes : BufferMode::Packets;
|
|
|
|
if (m_buffer_mode == BufferMode::Bytes) {
|
|
|
|
m_scratch_buffer = KBuffer::create_with_size(65536);
|
|
|
|
}
|
2019-03-12 19:27:07 +03:00
|
|
|
LOCKER(all_sockets().lock());
|
|
|
|
all_sockets().resource().set(this);
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IPv4Socket::~IPv4Socket()
|
|
|
|
{
|
2019-03-14 14:43:18 +03:00
|
|
|
LOCKER(all_sockets().lock());
|
|
|
|
all_sockets().resource().remove(this);
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2020-02-08 01:42:28 +03:00
|
|
|
void IPv4Socket::get_local_address(sockaddr* address, socklen_t* address_size)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2020-02-08 01:42:28 +03:00
|
|
|
sockaddr_in local_address = { AF_INET, htons(m_local_port), { m_local_address.to_in_addr_t() }, { 0 } };
|
|
|
|
memcpy(address, &local_address, min(static_cast<size_t>(*address_size), sizeof(sockaddr_in)));
|
2019-05-20 21:33:03 +03:00
|
|
|
*address_size = sizeof(sockaddr_in);
|
|
|
|
}
|
|
|
|
|
2020-02-08 01:42:28 +03:00
|
|
|
void IPv4Socket::get_peer_address(sockaddr* address, socklen_t* address_size)
|
2019-05-20 21:33:03 +03:00
|
|
|
{
|
2020-02-08 01:42:28 +03:00
|
|
|
sockaddr_in peer_address = { AF_INET, htons(m_peer_port), { m_peer_address.to_in_addr_t() }, { 0 } };
|
|
|
|
memcpy(address, &peer_address, min(static_cast<size_t>(*address_size), sizeof(sockaddr_in)));
|
2019-03-12 17:51:42 +03:00
|
|
|
*address_size = sizeof(sockaddr_in);
|
|
|
|
}
|
|
|
|
|
2020-01-11 14:07:45 +03:00
|
|
|
KResult IPv4Socket::bind(const sockaddr* user_address, socklen_t address_size)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2019-08-10 06:17:00 +03:00
|
|
|
ASSERT(setup_state() == SetupState::Unstarted);
|
2019-03-12 17:51:42 +03:00
|
|
|
if (address_size != sizeof(sockaddr_in))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
|
2020-01-11 14:07:45 +03:00
|
|
|
sockaddr_in address;
|
|
|
|
copy_from_user(&address, user_address, sizeof(sockaddr_in));
|
|
|
|
|
|
|
|
if (address.sin_family != AF_INET)
|
|
|
|
return KResult(-EINVAL);
|
2019-09-02 19:49:54 +03:00
|
|
|
|
2020-01-11 14:07:45 +03:00
|
|
|
auto requested_local_port = ntohs(address.sin_port);
|
2019-09-02 19:49:54 +03:00
|
|
|
if (!current->process().is_superuser()) {
|
|
|
|
if (requested_local_port < 1024) {
|
|
|
|
dbg() << current->process() << " (uid " << current->process().uid() << ") attempted to bind " << class_name() << " to port " << requested_local_port;
|
|
|
|
return KResult(-EACCES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 14:07:45 +03:00
|
|
|
m_local_address = IPv4Address((const u8*)&address.sin_addr.s_addr);
|
2019-09-02 19:49:54 +03:00
|
|
|
m_local_port = requested_local_port;
|
2019-05-03 22:51:40 +03:00
|
|
|
|
2019-10-18 17:47:29 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-08-09 05:35:56 +03:00
|
|
|
dbgprintf("IPv4Socket::bind %s{%p} to %s:%u\n", class_name(), this, m_local_address.to_string().characters(), m_local_port);
|
2019-10-18 17:47:29 +03:00
|
|
|
#endif
|
2019-05-03 22:51:40 +03:00
|
|
|
|
|
|
|
return protocol_bind();
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
KResult IPv4Socket::listen(int backlog)
|
|
|
|
{
|
|
|
|
int rc = allocate_local_port_if_needed();
|
|
|
|
if (rc < 0)
|
|
|
|
return KResult(-EADDRINUSE);
|
|
|
|
|
|
|
|
set_backlog(backlog);
|
2019-09-08 10:03:03 +03:00
|
|
|
m_role = Role::Listener;
|
2019-08-06 16:40:38 +03:00
|
|
|
|
2019-10-18 17:47:29 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-08-06 16:40:38 +03:00
|
|
|
kprintf("IPv4Socket{%p} listening with backlog=%d\n", this, backlog);
|
2019-10-18 17:47:29 +03:00
|
|
|
#endif
|
2019-08-06 16:40:38 +03:00
|
|
|
|
|
|
|
return protocol_listen();
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
KResult IPv4Socket::connect(FileDescription& description, const sockaddr* address, socklen_t address_size, ShouldBlock should_block)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
|
|
|
if (address_size != sizeof(sockaddr_in))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
if (address->sa_family != AF_INET)
|
|
|
|
return KResult(-EINVAL);
|
2019-08-11 16:38:20 +03:00
|
|
|
if (m_role == Role::Connected)
|
|
|
|
return KResult(-EISCONN);
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-03-13 19:17:07 +03:00
|
|
|
auto& ia = *(const sockaddr_in*)address;
|
2019-07-03 22:17:35 +03:00
|
|
|
m_peer_address = IPv4Address((const u8*)&ia.sin_addr.s_addr);
|
2019-05-04 17:40:34 +03:00
|
|
|
m_peer_port = ntohs(ia.sin_port);
|
2019-03-13 19:17:07 +03:00
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
return protocol_connect(description, should_block);
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
void IPv4Socket::attach(FileDescription&)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
void IPv4Socket::detach(FileDescription&)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-11-04 16:03:14 +03:00
|
|
|
bool IPv4Socket::can_read(const FileDescription&) const
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2019-08-11 16:38:20 +03:00
|
|
|
if (m_role == Role::Listener)
|
2019-05-03 22:51:40 +03:00
|
|
|
return can_accept();
|
2019-03-14 17:23:32 +03:00
|
|
|
if (protocol_is_disconnected())
|
|
|
|
return true;
|
2019-03-12 19:27:07 +03:00
|
|
|
return m_can_read;
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2019-11-04 16:03:14 +03:00
|
|
|
bool IPv4Socket::can_write(const FileDescription&) const
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2019-04-08 05:52:21 +03:00
|
|
|
return is_connected();
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2019-05-04 17:40:34 +03:00
|
|
|
int IPv4Socket::allocate_local_port_if_needed()
|
2019-03-13 18:05:56 +03:00
|
|
|
{
|
2019-05-04 17:40:34 +03:00
|
|
|
if (m_local_port)
|
|
|
|
return m_local_port;
|
|
|
|
int port = protocol_allocate_local_port();
|
2019-03-18 06:03:44 +03:00
|
|
|
if (port < 0)
|
|
|
|
return port;
|
2019-07-03 22:17:35 +03:00
|
|
|
m_local_port = (u16)port;
|
2019-03-18 06:03:44 +03:00
|
|
|
return port;
|
2019-03-13 18:05:56 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
ssize_t IPv4Socket::sendto(FileDescription&, const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
|
|
|
(void)flags;
|
2019-03-14 01:14:30 +03:00
|
|
|
if (addr && addr_length != sizeof(sockaddr_in))
|
2019-03-12 17:51:42 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-03-13 19:17:07 +03:00
|
|
|
if (addr) {
|
2019-03-14 01:14:30 +03:00
|
|
|
if (addr->sa_family != AF_INET) {
|
|
|
|
kprintf("sendto: Bad address family: %u is not AF_INET!\n", addr->sa_family);
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
2019-03-13 19:17:07 +03:00
|
|
|
auto& ia = *(const sockaddr_in*)addr;
|
2019-07-03 22:17:35 +03:00
|
|
|
m_peer_address = IPv4Address((const u8*)&ia.sin_addr.s_addr);
|
2019-05-04 17:40:34 +03:00
|
|
|
m_peer_port = ntohs(ia.sin_port);
|
2019-03-13 19:17:07 +03:00
|
|
|
}
|
2019-03-13 17:40:30 +03:00
|
|
|
|
2019-08-28 14:58:01 +03:00
|
|
|
auto routing_decision = route_to(m_peer_address, m_local_address);
|
2019-08-29 04:18:38 +03:00
|
|
|
if (routing_decision.is_zero())
|
2019-04-02 16:46:44 +03:00
|
|
|
return -EHOSTUNREACH;
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
if (m_local_address.to_u32() == 0)
|
2019-08-28 14:58:01 +03:00
|
|
|
m_local_address = routing_decision.adapter->ipv4_address();
|
2019-08-06 16:40:38 +03:00
|
|
|
|
2019-05-04 17:40:34 +03:00
|
|
|
int rc = allocate_local_port_if_needed();
|
2019-03-18 06:03:44 +03:00
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-09-08 12:13:50 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-05-04 17:40:34 +03:00
|
|
|
kprintf("sendto: destination=%s:%u\n", m_peer_address.to_string().characters(), m_peer_port);
|
2019-09-08 12:13:50 +03:00
|
|
|
#endif
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-03-13 17:40:30 +03:00
|
|
|
if (type() == SOCK_RAW) {
|
2019-09-19 22:40:06 +03:00
|
|
|
routing_decision.adapter->send_ipv4(routing_decision.next_hop, m_peer_address, (IPv4Protocol)protocol(), (const u8*)data, data_length, m_ttl);
|
2019-03-13 17:40:30 +03:00
|
|
|
return data_length;
|
|
|
|
}
|
|
|
|
|
2019-12-01 19:36:06 +03:00
|
|
|
int nsent = protocol_send(data, data_length);
|
|
|
|
if (nsent > 0)
|
|
|
|
current->did_ipv4_socket_write(nsent);
|
|
|
|
return nsent;
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2020-02-08 15:09:37 +03:00
|
|
|
ssize_t IPv4Socket::receive_byte_buffered(FileDescription& description, void* buffer, size_t buffer_length, int, sockaddr*, socklen_t*)
|
2019-03-12 19:27:07 +03:00
|
|
|
{
|
2020-02-08 15:09:37 +03:00
|
|
|
if (m_receive_buffer.is_empty()) {
|
|
|
|
if (protocol_is_disconnected())
|
|
|
|
return 0;
|
|
|
|
if (!description.is_blocking())
|
|
|
|
return -EAGAIN;
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2020-02-08 15:09:37 +03:00
|
|
|
auto res = current->block<Thread::ReadBlocker>(description);
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2020-02-08 15:09:37 +03:00
|
|
|
LOCKER(lock());
|
|
|
|
if (!m_can_read) {
|
|
|
|
if (res != Thread::BlockResult::WokeNormally)
|
|
|
|
return -EINTR;
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2020-02-08 15:09:37 +03:00
|
|
|
// Unblocked due to timeout.
|
|
|
|
return -EAGAIN;
|
2019-12-14 11:43:31 +03:00
|
|
|
}
|
2020-02-08 15:09:37 +03:00
|
|
|
}
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2020-02-08 15:09:37 +03:00
|
|
|
ASSERT(!m_receive_buffer.is_empty());
|
|
|
|
int nreceived = m_receive_buffer.read((u8*)buffer, buffer_length);
|
|
|
|
if (nreceived > 0)
|
|
|
|
current->did_ipv4_socket_read((size_t)nreceived);
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2020-02-08 15:09:37 +03:00
|
|
|
m_can_read = !m_receive_buffer.is_empty();
|
|
|
|
return nreceived;
|
|
|
|
}
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2020-02-08 15:09:37 +03:00
|
|
|
ssize_t IPv4Socket::receive_packet_buffered(FileDescription& description, void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
|
|
|
|
{
|
2019-05-04 17:40:34 +03:00
|
|
|
ReceivedPacket packet;
|
2019-03-12 19:27:07 +03:00
|
|
|
{
|
2019-03-14 02:20:44 +03:00
|
|
|
LOCKER(lock());
|
2019-11-18 19:30:45 +03:00
|
|
|
if (m_receive_queue.is_empty()) {
|
|
|
|
// FIXME: Shouldn't this return -ENOTCONN instead of EOF?
|
|
|
|
// But if so, we still need to deliver at least one EOF read to userspace.. right?
|
|
|
|
if (protocol_is_disconnected())
|
|
|
|
return 0;
|
|
|
|
if (!description.is_blocking())
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2019-11-04 15:41:36 +03:00
|
|
|
|
2019-03-12 19:27:07 +03:00
|
|
|
if (!m_receive_queue.is_empty()) {
|
2019-05-04 17:40:34 +03:00
|
|
|
packet = m_receive_queue.take_first();
|
2019-03-13 05:26:01 +03:00
|
|
|
m_can_read = !m_receive_queue.is_empty();
|
2019-03-14 17:23:32 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-12-09 19:48:58 +03:00
|
|
|
kprintf("IPv4Socket(%p): recvfrom without blocking %d bytes, packets in queue: %zu\n", this, packet.data.value().size(), m_receive_queue.size_slow());
|
2019-03-14 17:23:32 +03:00
|
|
|
#endif
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-05 12:06:21 +03:00
|
|
|
if (!packet.data.has_value()) {
|
2019-03-14 17:23:32 +03:00
|
|
|
if (protocol_is_disconnected()) {
|
|
|
|
kprintf("IPv4Socket{%p} is protocol-disconnected, returning 0 in recvfrom!\n", this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-01-26 19:54:23 +03:00
|
|
|
auto res = current->block<Thread::ReadBlocker>(description);
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2019-03-14 02:20:44 +03:00
|
|
|
LOCKER(lock());
|
2019-03-13 15:13:23 +03:00
|
|
|
if (!m_can_read) {
|
2020-01-10 21:15:01 +03:00
|
|
|
if (res != Thread::BlockResult::WokeNormally)
|
2019-07-20 12:05:52 +03:00
|
|
|
return -EINTR;
|
|
|
|
|
2019-03-13 15:13:23 +03:00
|
|
|
// Unblocked due to timeout.
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2019-03-12 19:27:07 +03:00
|
|
|
ASSERT(m_can_read);
|
|
|
|
ASSERT(!m_receive_queue.is_empty());
|
2019-05-04 17:40:34 +03:00
|
|
|
packet = m_receive_queue.take_first();
|
2019-03-13 05:26:01 +03:00
|
|
|
m_can_read = !m_receive_queue.is_empty();
|
2019-03-14 17:23:32 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-12-09 19:48:58 +03:00
|
|
|
kprintf("IPv4Socket(%p): recvfrom with blocking %d bytes, packets in queue: %zu\n", this, packet.data.value().size(), m_receive_queue.size_slow());
|
2019-03-14 17:23:32 +03:00
|
|
|
#endif
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|
2019-08-05 12:06:21 +03:00
|
|
|
ASSERT(packet.data.has_value());
|
|
|
|
auto& ipv4_packet = *(const IPv4Packet*)(packet.data.value().data());
|
2019-03-13 16:22:27 +03:00
|
|
|
|
2019-03-14 01:14:30 +03:00
|
|
|
if (addr) {
|
2019-10-18 17:47:29 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-05-04 17:40:34 +03:00
|
|
|
dbgprintf("Incoming packet is from: %s:%u\n", packet.peer_address.to_string().characters(), packet.peer_port);
|
2019-10-18 17:47:29 +03:00
|
|
|
#endif
|
2019-03-14 01:14:30 +03:00
|
|
|
auto& ia = *(sockaddr_in*)addr;
|
2019-05-04 17:40:34 +03:00
|
|
|
memcpy(&ia.sin_addr, &packet.peer_address, sizeof(IPv4Address));
|
|
|
|
ia.sin_port = htons(packet.peer_port);
|
2019-03-14 01:14:30 +03:00
|
|
|
ia.sin_family = AF_INET;
|
|
|
|
ASSERT(addr_length);
|
|
|
|
*addr_length = sizeof(sockaddr_in);
|
|
|
|
}
|
2019-03-13 16:47:21 +03:00
|
|
|
|
2019-03-13 16:22:27 +03:00
|
|
|
if (type() == SOCK_RAW) {
|
|
|
|
ASSERT(buffer_length >= ipv4_packet.payload_size());
|
|
|
|
memcpy(buffer, ipv4_packet.payload(), ipv4_packet.payload_size());
|
|
|
|
return ipv4_packet.payload_size();
|
|
|
|
}
|
|
|
|
|
2020-02-08 15:09:37 +03:00
|
|
|
return protocol_receive(packet.data.value(), buffer, buffer_length, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t IPv4Socket::recvfrom(FileDescription& description, void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
|
|
|
|
{
|
|
|
|
if (addr_length && *addr_length < sizeof(sockaddr_in))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
|
|
|
kprintf("recvfrom: type=%d, local_port=%u\n", type(), local_port());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
ssize_t nreceived = 0;
|
|
|
|
if (buffer_mode() == BufferMode::Bytes)
|
|
|
|
nreceived = receive_byte_buffered(description, buffer, buffer_length, flags, addr, addr_length);
|
|
|
|
else
|
|
|
|
nreceived = receive_packet_buffered(description, buffer, buffer_length, flags, addr, addr_length);
|
|
|
|
|
2019-12-01 19:36:06 +03:00
|
|
|
if (nreceived > 0)
|
|
|
|
current->did_ipv4_socket_read(nreceived);
|
|
|
|
return nreceived;
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|
|
|
|
|
2019-11-04 16:03:14 +03:00
|
|
|
bool IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port, KBuffer&& packet)
|
2019-03-12 19:27:07 +03:00
|
|
|
{
|
2019-03-14 02:20:44 +03:00
|
|
|
LOCKER(lock());
|
2020-02-08 02:58:11 +03:00
|
|
|
|
|
|
|
if (is_shut_down_for_reading())
|
|
|
|
return false;
|
|
|
|
|
2019-08-05 12:06:21 +03:00
|
|
|
auto packet_size = packet.size();
|
2019-12-14 11:43:31 +03:00
|
|
|
|
|
|
|
if (buffer_mode() == BufferMode::Bytes) {
|
2020-01-20 18:08:49 +03:00
|
|
|
size_t space_in_receive_buffer = m_receive_buffer.space_for_writing();
|
2019-12-14 11:43:31 +03:00
|
|
|
if (packet_size > space_in_receive_buffer) {
|
|
|
|
kprintf("IPv4Socket(%p): did_receive refusing packet since buffer is full.\n", this);
|
|
|
|
ASSERT(m_can_read);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int nreceived = protocol_receive(packet, m_scratch_buffer.value().data(), m_scratch_buffer.value().size(), 0);
|
|
|
|
m_receive_buffer.write(m_scratch_buffer.value().data(), nreceived);
|
|
|
|
m_can_read = !m_receive_buffer.is_empty();
|
|
|
|
} else {
|
|
|
|
// FIXME: Maybe track the number of packets so we don't have to walk the entire packet queue to count them..
|
|
|
|
if (m_receive_queue.size_slow() > 2000) {
|
|
|
|
kprintf("IPv4Socket(%p): did_receive refusing packet since queue is full.\n", this);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_receive_queue.append({ source_address, source_port, move(packet) });
|
|
|
|
m_can_read = true;
|
|
|
|
}
|
2019-03-14 17:28:23 +03:00
|
|
|
m_bytes_received += packet_size;
|
2019-03-13 15:13:23 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-12-14 11:43:31 +03:00
|
|
|
if (buffer_mode() == BufferMode::Bytes)
|
2020-01-26 16:42:44 +03:00
|
|
|
kprintf("IPv4Socket(%p): did_receive %d bytes, total_received=%u\n", this, packet_size, m_bytes_received);
|
2019-12-14 11:43:31 +03:00
|
|
|
else
|
|
|
|
kprintf("IPv4Socket(%p): did_receive %d bytes, total_received=%u, packets in queue: %zu\n", this, packet_size, m_bytes_received, m_receive_queue.size_slow());
|
2019-03-13 15:13:23 +03:00
|
|
|
#endif
|
2019-11-04 16:03:14 +03:00
|
|
|
return true;
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|
2019-08-10 19:10:36 +03:00
|
|
|
|
|
|
|
String IPv4Socket::absolute_path(const FileDescription&) const
|
|
|
|
{
|
|
|
|
if (m_role == Role::None)
|
|
|
|
return "socket";
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append("socket:");
|
|
|
|
|
|
|
|
builder.appendf("%s:%d", m_local_address.to_string().characters(), m_local_port);
|
|
|
|
if (m_role == Role::Accepted || m_role == Role::Connected)
|
|
|
|
builder.appendf(" / %s:%d", m_peer_address.to_string().characters(), m_peer_port);
|
|
|
|
|
|
|
|
switch (m_role) {
|
|
|
|
case Role::Listener:
|
|
|
|
builder.append(" (listening)");
|
|
|
|
break;
|
|
|
|
case Role::Accepted:
|
|
|
|
builder.append(" (accepted)");
|
|
|
|
break;
|
|
|
|
case Role::Connected:
|
|
|
|
builder.append(" (connected)");
|
|
|
|
break;
|
|
|
|
case Role::Connecting:
|
|
|
|
builder.append(" (connecting)");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
2019-09-19 22:40:06 +03:00
|
|
|
|
|
|
|
KResult IPv4Socket::setsockopt(int level, int option, const void* value, socklen_t value_size)
|
|
|
|
{
|
|
|
|
if (level != IPPROTO_IP)
|
|
|
|
return Socket::setsockopt(level, option, value, value_size);
|
|
|
|
|
|
|
|
switch (option) {
|
|
|
|
case IP_TTL:
|
|
|
|
if (value_size < sizeof(int))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
if (*(const int*)value < 0 || *(const int*)value > 255)
|
|
|
|
return KResult(-EINVAL);
|
2019-12-14 11:43:31 +03:00
|
|
|
m_ttl = (u8) * (const int*)value;
|
2019-09-19 22:40:06 +03:00
|
|
|
return KSuccess;
|
|
|
|
default:
|
|
|
|
return KResult(-ENOPROTOOPT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-06 20:38:36 +03:00
|
|
|
KResult IPv4Socket::getsockopt(FileDescription& description, int level, int option, void* value, socklen_t* value_size)
|
2019-09-19 22:40:06 +03:00
|
|
|
{
|
|
|
|
if (level != IPPROTO_IP)
|
2019-12-06 20:38:36 +03:00
|
|
|
return Socket::getsockopt(description, level, option, value, value_size);
|
2019-09-19 22:40:06 +03:00
|
|
|
|
|
|
|
switch (option) {
|
|
|
|
case IP_TTL:
|
|
|
|
if (*value_size < sizeof(int))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
*(int*)value = m_ttl;
|
|
|
|
return KSuccess;
|
|
|
|
default:
|
|
|
|
return KResult(-ENOPROTOOPT);
|
|
|
|
}
|
|
|
|
}
|
2019-09-23 20:06:03 +03:00
|
|
|
|
|
|
|
int IPv4Socket::ioctl(FileDescription&, unsigned request, unsigned arg)
|
|
|
|
{
|
2020-01-12 15:28:07 +03:00
|
|
|
REQUIRE_PROMISE(inet);
|
2019-09-23 20:06:03 +03:00
|
|
|
auto* ifr = (ifreq*)arg;
|
|
|
|
if (!current->process().validate_read_typed(ifr))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
char namebuf[IFNAMSIZ + 1];
|
|
|
|
memcpy(namebuf, ifr->ifr_name, IFNAMSIZ);
|
|
|
|
namebuf[sizeof(namebuf) - 1] = '\0';
|
|
|
|
auto adapter = NetworkAdapter::lookup_by_name(namebuf);
|
|
|
|
if (!adapter)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case SIOCSIFADDR:
|
|
|
|
if (!current->process().is_superuser())
|
|
|
|
return -EPERM;
|
|
|
|
if (ifr->ifr_addr.sa_family != AF_INET)
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
adapter->set_ipv4_address(IPv4Address(((sockaddr_in&)ifr->ifr_addr).sin_addr.s_addr));
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SIOCGIFADDR:
|
|
|
|
if (!current->process().validate_write_typed(ifr))
|
|
|
|
return -EFAULT;
|
|
|
|
ifr->ifr_addr.sa_family = AF_INET;
|
|
|
|
((sockaddr_in&)ifr->ifr_addr).sin_addr.s_addr = adapter->ipv4_address().to_u32();
|
|
|
|
return 0;
|
2019-10-02 19:20:11 +03:00
|
|
|
|
|
|
|
case SIOCGIFHWADDR:
|
|
|
|
if (!current->process().validate_write_typed(ifr))
|
|
|
|
return -EFAULT;
|
|
|
|
ifr->ifr_hwaddr.sa_family = AF_INET;
|
|
|
|
{
|
|
|
|
auto mac_address = adapter->mac_address();
|
|
|
|
memcpy(ifr->ifr_hwaddr.sa_data, &mac_address, sizeof(MACAddress));
|
|
|
|
}
|
|
|
|
return 0;
|
2019-09-23 20:06:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-02-08 17:52:32 +03:00
|
|
|
|
|
|
|
void IPv4Socket::close()
|
|
|
|
{
|
|
|
|
shutdown(SHUT_RDWR);
|
|
|
|
}
|
2020-02-08 17:59:21 +03:00
|
|
|
|
|
|
|
void IPv4Socket::shut_down_for_reading()
|
|
|
|
{
|
|
|
|
Socket::shut_down_for_reading();
|
|
|
|
m_can_read = true;
|
|
|
|
}
|