2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
#include <AK/Singleton.h>
|
2019-08-10 19:10:36 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-01-25 18:07:10 +03:00
|
|
|
#include <Kernel/Debug.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
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2020-08-25 04:35:19 +03:00
|
|
|
static AK::Singleton<Lockable<HashTable<IPv4Socket*>>> s_table;
|
|
|
|
|
2021-03-07 14:01:11 +03:00
|
|
|
using BlockFlags = Thread::FileDescriptionBlocker::BlockFlags;
|
|
|
|
|
2019-03-12 19:27:07 +03:00
|
|
|
Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
|
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2021-05-13 11:01:38 +03:00
|
|
|
if (type == SOCK_STREAM) {
|
|
|
|
auto tcp_socket = TCPSocket::create(protocol);
|
|
|
|
if (tcp_socket.is_error())
|
|
|
|
return tcp_socket.error();
|
|
|
|
return tcp_socket.release_value();
|
|
|
|
}
|
2021-05-13 11:34:04 +03:00
|
|
|
if (type == SOCK_DGRAM) {
|
|
|
|
auto udp_socket = UDPSocket::create(protocol);
|
|
|
|
if (udp_socket.is_error())
|
|
|
|
return udp_socket.error();
|
|
|
|
return udp_socket.release_value();
|
|
|
|
}
|
2021-05-13 11:24:44 +03:00
|
|
|
if (type == SOCK_RAW) {
|
|
|
|
auto raw_socket = adopt_ref_if_nonnull(new IPv4Socket(type, protocol));
|
|
|
|
if (raw_socket)
|
|
|
|
return raw_socket.release_nonnull();
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IPv4Socket::IPv4Socket(int type, int protocol)
|
|
|
|
: Socket(AF_INET, type, protocol)
|
|
|
|
{
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(IPV4_SOCKET_DEBUG, "IPv4Socket({}) created with type={}, protocol={}", this, type, protocol);
|
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);
|
|
|
|
}
|
2021-04-25 01:27:32 +03:00
|
|
|
Locker locker(all_sockets().lock());
|
2019-03-12 19:27:07 +03:00
|
|
|
all_sockets().resource().set(this);
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IPv4Socket::~IPv4Socket()
|
|
|
|
{
|
2021-04-25 01:27:32 +03:00
|
|
|
Locker locker(all_sockets().lock());
|
2019-03-14 14:43:18 +03:00
|
|
|
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-08-10 01:13:43 +03:00
|
|
|
KResult IPv4Socket::bind(Userspace<const sockaddr*> user_address, socklen_t address_size)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(setup_state() == SetupState::Unstarted);
|
2019-03-12 17:51:42 +03:00
|
|
|
if (address_size != sizeof(sockaddr_in))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2020-01-11 14:07:45 +03:00
|
|
|
sockaddr_in address;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_from_user(&address, user_address, sizeof(sockaddr_in)))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-01-11 14:07:45 +03:00
|
|
|
|
|
|
|
if (address.sin_family != AF_INET)
|
2021-01-21 01:11:17 +03:00
|
|
|
return 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);
|
2020-06-29 00:34:31 +03:00
|
|
|
if (!Process::current()->is_superuser()) {
|
2021-04-20 00:12:56 +03:00
|
|
|
if (requested_local_port > 0 && requested_local_port < 1024) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("UID {} attempted to bind {} to port {}", Process::current()->uid(), class_name(), requested_local_port);
|
2021-01-21 01:11:17 +03:00
|
|
|
return EACCES;
|
2019-09-02 19:49:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(IPV4_SOCKET_DEBUG, "IPv4Socket::bind {}({}) to {}:{}", class_name(), this, m_local_address, m_local_port);
|
2019-05-03 22:51:40 +03:00
|
|
|
|
|
|
|
return protocol_bind();
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
KResult IPv4Socket::listen(size_t backlog)
|
2019-08-06 16:40:38 +03:00
|
|
|
{
|
2021-04-25 01:27:32 +03:00
|
|
|
Locker locker(lock());
|
2021-05-01 01:03:33 +03:00
|
|
|
if (auto result = allocate_local_port_if_needed(); result.is_error() && result.error() != -ENOPROTOOPT)
|
2021-04-30 13:26:25 +03:00
|
|
|
return result.error();
|
2019-08-06 16:40:38 +03:00
|
|
|
|
|
|
|
set_backlog(backlog);
|
2019-09-08 10:03:03 +03:00
|
|
|
m_role = Role::Listener;
|
2020-11-30 02:05:27 +03:00
|
|
|
evaluate_block_conditions();
|
2019-08-06 16:40:38 +03:00
|
|
|
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(IPV4_SOCKET_DEBUG, "IPv4Socket({}) listening with backlog={}", this, backlog);
|
2019-08-06 16:40:38 +03:00
|
|
|
|
|
|
|
return protocol_listen();
|
|
|
|
}
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
KResult IPv4Socket::connect(FileDescription& description, Userspace<const sockaddr*> address, socklen_t address_size, ShouldBlock should_block)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
|
|
|
if (address_size != sizeof(sockaddr_in))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2020-09-12 06:11:07 +03:00
|
|
|
u16 sa_family_copy;
|
|
|
|
auto* user_address = reinterpret_cast<const sockaddr*>(address.unsafe_userspace_ptr());
|
|
|
|
if (!copy_from_user(&sa_family_copy, &user_address->sa_family, sizeof(u16)))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (sa_family_copy != AF_INET)
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2019-08-11 16:38:20 +03:00
|
|
|
if (m_role == Role::Connected)
|
2021-01-21 01:11:17 +03:00
|
|
|
return EISCONN;
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
sockaddr_in safe_address;
|
|
|
|
if (!copy_from_user(&safe_address, (const sockaddr_in*)user_address, sizeof(sockaddr_in)))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-09-12 06:11:07 +03:00
|
|
|
|
|
|
|
m_peer_address = IPv4Address((const u8*)&safe_address.sin_addr.s_addr);
|
2021-05-12 13:14:03 +03:00
|
|
|
if (m_peer_address == IPv4Address { 0, 0, 0, 0 })
|
|
|
|
m_peer_address = IPv4Address { 127, 0, 0, 1 };
|
2020-09-12 06:11:07 +03:00
|
|
|
m_peer_port = ntohs(safe_address.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
|
|
|
}
|
|
|
|
|
2020-04-10 12:44:42 +03:00
|
|
|
bool IPv4Socket::can_read(const FileDescription&, size_t) 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
|
|
|
}
|
|
|
|
|
2020-04-10 12:44:42 +03:00
|
|
|
bool IPv4Socket::can_write(const FileDescription&, size_t) 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
|
|
|
}
|
|
|
|
|
2021-04-30 13:26:25 +03:00
|
|
|
KResultOr<u16> IPv4Socket::allocate_local_port_if_needed()
|
2019-03-13 18:05:56 +03:00
|
|
|
{
|
2021-04-30 13:26:25 +03:00
|
|
|
Locker locker(lock());
|
2019-05-04 17:40:34 +03:00
|
|
|
if (m_local_port)
|
|
|
|
return m_local_port;
|
2021-04-30 13:26:25 +03:00
|
|
|
auto port_or_error = protocol_allocate_local_port();
|
|
|
|
if (port_or_error.is_error())
|
|
|
|
return port_or_error.error();
|
|
|
|
m_local_port = port_or_error.value();
|
|
|
|
return port_or_error.value();
|
2019-03-13 18:05:56 +03:00
|
|
|
}
|
|
|
|
|
2020-12-21 02:09:48 +03:00
|
|
|
KResultOr<size_t> IPv4Socket::sendto(FileDescription&, const UserOrKernelBuffer& data, size_t data_length, [[maybe_unused]] int flags, Userspace<const sockaddr*> addr, socklen_t addr_length)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2021-04-25 01:27:32 +03:00
|
|
|
Locker locker(lock());
|
2020-10-21 21:51:02 +03:00
|
|
|
|
2019-03-14 01:14:30 +03:00
|
|
|
if (addr && addr_length != sizeof(sockaddr_in))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-03-13 19:17:07 +03:00
|
|
|
if (addr) {
|
2020-08-18 09:49:35 +03:00
|
|
|
sockaddr_in ia;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_from_user(&ia, Userspace<const sockaddr_in*>(addr.ptr())))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-08-18 09:49:35 +03:00
|
|
|
|
|
|
|
if (ia.sin_family != AF_INET) {
|
2021-03-10 00:25:09 +03:00
|
|
|
dmesgln("sendto: Bad address family: {} is not AF_INET", ia.sin_family);
|
2021-01-21 01:11:17 +03:00
|
|
|
return EAFNOSUPPORT;
|
2019-03-14 01:14:30 +03:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-04-04 23:46:45 +03:00
|
|
|
auto routing_decision = route_to(m_peer_address, m_local_address, bound_interface());
|
2019-08-29 04:18:38 +03:00
|
|
|
if (routing_decision.is_zero())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EHOSTUNREACH;
|
2019-04-02 16:46:44 +03:00
|
|
|
|
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
|
|
|
|
2021-05-01 01:03:33 +03:00
|
|
|
if (auto result = allocate_local_port_if_needed(); result.is_error() && result.error() != -ENOPROTOOPT)
|
2021-04-30 13:26:25 +03:00
|
|
|
return result.error();
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2021-03-10 00:25:09 +03:00
|
|
|
dbgln_if(IPV4_SOCKET_DEBUG, "sendto: destination={}:{}", m_peer_address, m_peer_port);
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-03-13 17:40:30 +03:00
|
|
|
if (type() == SOCK_RAW) {
|
2021-05-12 15:36:09 +03:00
|
|
|
auto result = routing_decision.adapter->send_ipv4(local_address(), routing_decision.next_hop,
|
|
|
|
m_peer_address, (IPv4Protocol)protocol(), data, data_length, m_ttl);
|
2021-01-31 14:13:16 +03:00
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
2019-03-13 17:40:30 +03:00
|
|
|
return data_length;
|
|
|
|
}
|
|
|
|
|
2020-08-04 19:02:23 +03:00
|
|
|
auto nsent_or_error = protocol_send(data, data_length);
|
|
|
|
if (!nsent_or_error.is_error())
|
|
|
|
Thread::current()->did_ipv4_socket_write(nsent_or_error.value());
|
|
|
|
return nsent_or_error;
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2021-04-29 00:22:55 +03:00
|
|
|
KResultOr<size_t> IPv4Socket::receive_byte_buffered(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>)
|
2019-03-12 19:27:07 +03:00
|
|
|
{
|
2020-03-07 13:26:17 +03:00
|
|
|
Locker locker(lock());
|
2020-02-08 15:09:37 +03:00
|
|
|
if (m_receive_buffer.is_empty()) {
|
|
|
|
if (protocol_is_disconnected())
|
|
|
|
return 0;
|
|
|
|
if (!description.is_blocking())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EAGAIN;
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2020-03-07 13:26:17 +03:00
|
|
|
locker.unlock();
|
2021-03-07 14:01:11 +03:00
|
|
|
auto unblocked_flags = BlockFlags::None;
|
2021-01-11 02:29:28 +03:00
|
|
|
auto res = Thread::current()->block<Thread::ReadBlocker>({}, description, unblocked_flags);
|
2020-03-07 13:26:17 +03:00
|
|
|
locker.lock();
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2021-03-07 14:01:11 +03:00
|
|
|
if (!has_flag(unblocked_flags, BlockFlags::Read)) {
|
2020-07-07 02:10:52 +03:00
|
|
|
if (res.was_interrupted())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINTR;
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2020-02-08 15:09:37 +03:00
|
|
|
// Unblocked due to timeout.
|
2021-01-21 01:11:17 +03:00
|
|
|
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
|
|
|
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(!m_receive_buffer.is_empty());
|
2021-04-29 00:22:55 +03:00
|
|
|
|
|
|
|
int nreceived;
|
|
|
|
if (flags & MSG_PEEK)
|
|
|
|
nreceived = m_receive_buffer.peek(buffer, buffer_length);
|
|
|
|
else
|
|
|
|
nreceived = m_receive_buffer.read(buffer, buffer_length);
|
|
|
|
|
|
|
|
if (nreceived > 0 && !(flags & MSG_PEEK))
|
2020-06-29 00:34:31 +03:00
|
|
|
Thread::current()->did_ipv4_socket_read((size_t)nreceived);
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
set_can_read(!m_receive_buffer.is_empty());
|
2020-02-08 15:09:37 +03:00
|
|
|
return nreceived;
|
|
|
|
}
|
2019-12-14 11:43:31 +03:00
|
|
|
|
2021-02-28 04:48:45 +03:00
|
|
|
KResultOr<size_t> IPv4Socket::receive_packet_buffered(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*> addr, Userspace<socklen_t*> addr_length, Time& packet_timestamp)
|
2020-02-08 15:09:37 +03:00
|
|
|
{
|
2020-03-07 13:26:17 +03:00
|
|
|
Locker locker(lock());
|
2019-05-04 17:40:34 +03:00
|
|
|
ReceivedPacket packet;
|
2019-03-12 19:27:07 +03:00
|
|
|
{
|
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())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EAGAIN;
|
2019-11-18 19:30:45 +03:00
|
|
|
}
|
2019-11-04 15:41:36 +03:00
|
|
|
|
2019-03-12 19:27:07 +03:00
|
|
|
if (!m_receive_queue.is_empty()) {
|
2021-04-29 00:22:55 +03:00
|
|
|
if (flags & MSG_PEEK)
|
|
|
|
packet = m_receive_queue.first();
|
|
|
|
else
|
|
|
|
packet = m_receive_queue.take_first();
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
set_can_read(!m_receive_queue.is_empty());
|
2021-01-14 02:21:21 +03:00
|
|
|
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(IPV4_SOCKET_DEBUG, "IPv4Socket({}): recvfrom without blocking {} bytes, packets in queue: {}",
|
2021-01-14 02:21:21 +03:00
|
|
|
this,
|
|
|
|
packet.data.value().size(),
|
|
|
|
m_receive_queue.size());
|
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()) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("IPv4Socket({}) is protocol-disconnected, returning 0 in recvfrom!", this);
|
2019-03-14 17:23:32 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-07 13:26:17 +03:00
|
|
|
locker.unlock();
|
2021-03-07 14:01:11 +03:00
|
|
|
auto unblocked_flags = BlockFlags::None;
|
2021-01-11 02:29:28 +03:00
|
|
|
auto res = Thread::current()->block<Thread::ReadBlocker>({}, description, unblocked_flags);
|
2020-03-07 13:26:17 +03:00
|
|
|
locker.lock();
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2021-03-07 14:01:11 +03:00
|
|
|
if (!has_flag(unblocked_flags, BlockFlags::Read)) {
|
2020-07-07 02:10:52 +03:00
|
|
|
if (res.was_interrupted())
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINTR;
|
2019-07-20 12:05:52 +03:00
|
|
|
|
2019-03-13 15:13:23 +03:00
|
|
|
// Unblocked due to timeout.
|
2021-01-21 01:11:17 +03:00
|
|
|
return EAGAIN;
|
2019-03-13 15:13:23 +03:00
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_can_read);
|
|
|
|
VERIFY(!m_receive_queue.is_empty());
|
2021-04-29 00:22:55 +03:00
|
|
|
|
|
|
|
if (flags & MSG_PEEK)
|
|
|
|
packet = m_receive_queue.first();
|
|
|
|
else
|
|
|
|
packet = m_receive_queue.take_first();
|
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
set_can_read(!m_receive_queue.is_empty());
|
2021-01-14 02:21:21 +03:00
|
|
|
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(IPV4_SOCKET_DEBUG, "IPv4Socket({}): recvfrom with blocking {} bytes, packets in queue: {}",
|
2021-01-14 02:21:21 +03:00
|
|
|
this,
|
|
|
|
packet.data.value().size(),
|
|
|
|
m_receive_queue.size());
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(packet.data.has_value());
|
2019-03-13 16:22:27 +03:00
|
|
|
|
2020-09-16 19:25:06 +03:00
|
|
|
packet_timestamp = packet.timestamp;
|
|
|
|
|
2019-03-14 01:14:30 +03:00
|
|
|
if (addr) {
|
2021-02-07 15:03:24 +03:00
|
|
|
dbgln_if(IPV4_SOCKET_DEBUG, "Incoming packet is from: {}:{}", packet.peer_address, packet.peer_port);
|
2020-08-18 10:25:23 +03:00
|
|
|
|
|
|
|
sockaddr_in out_addr {};
|
|
|
|
memcpy(&out_addr.sin_addr, &packet.peer_address, sizeof(IPv4Address));
|
|
|
|
out_addr.sin_port = htons(packet.peer_port);
|
|
|
|
out_addr.sin_family = AF_INET;
|
|
|
|
Userspace<sockaddr_in*> dest_addr = addr.ptr();
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(dest_addr, &out_addr))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-08-18 10:25:23 +03:00
|
|
|
|
|
|
|
socklen_t out_length = sizeof(sockaddr_in);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(addr_length);
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(addr_length, &out_length))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2019-03-14 01:14:30 +03:00
|
|
|
}
|
2019-03-13 16:47:21 +03:00
|
|
|
|
2019-03-13 16:22:27 +03:00
|
|
|
if (type() == SOCK_RAW) {
|
2020-10-31 13:32:33 +03:00
|
|
|
size_t bytes_written = min(packet.data.value().size(), buffer_length);
|
|
|
|
if (!buffer.write(packet.data.value().data(), bytes_written))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-09-10 07:12:50 +03:00
|
|
|
return bytes_written;
|
2019-03-13 16:22:27 +03:00
|
|
|
}
|
|
|
|
|
2020-12-18 18:13:23 +03:00
|
|
|
return protocol_receive(ReadonlyBytes { packet.data.value().data(), packet.data.value().size() }, buffer, buffer_length, flags);
|
2020-02-08 15:09:37 +03:00
|
|
|
}
|
|
|
|
|
2021-02-28 04:48:45 +03:00
|
|
|
KResultOr<size_t> IPv4Socket::recvfrom(FileDescription& description, UserOrKernelBuffer& buffer, size_t buffer_length, int flags, Userspace<sockaddr*> user_addr, Userspace<socklen_t*> user_addr_length, Time& packet_timestamp)
|
2020-02-08 15:09:37 +03:00
|
|
|
{
|
2020-08-18 10:25:23 +03:00
|
|
|
if (user_addr_length) {
|
|
|
|
socklen_t addr_length;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_from_user(&addr_length, user_addr_length.unsafe_userspace_ptr()))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-08-18 10:25:23 +03:00
|
|
|
if (addr_length < sizeof(sockaddr_in))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2020-08-18 10:25:23 +03:00
|
|
|
}
|
2020-02-08 15:09:37 +03:00
|
|
|
|
2021-03-10 00:25:09 +03:00
|
|
|
dbgln_if(IPV4_SOCKET_DEBUG, "recvfrom: type={}, local_port={}", type(), local_port());
|
2020-02-08 15:09:37 +03:00
|
|
|
|
2020-08-04 19:02:23 +03:00
|
|
|
KResultOr<size_t> nreceived = 0;
|
2020-02-08 15:09:37 +03:00
|
|
|
if (buffer_mode() == BufferMode::Bytes)
|
2020-08-18 10:25:23 +03:00
|
|
|
nreceived = receive_byte_buffered(description, buffer, buffer_length, flags, user_addr, user_addr_length);
|
2020-02-08 15:09:37 +03:00
|
|
|
else
|
2020-09-16 19:25:06 +03:00
|
|
|
nreceived = receive_packet_buffered(description, buffer, buffer_length, flags, user_addr, user_addr_length, packet_timestamp);
|
2020-02-08 15:09:37 +03:00
|
|
|
|
2020-08-04 19:02:23 +03:00
|
|
|
if (!nreceived.is_error())
|
|
|
|
Thread::current()->did_ipv4_socket_read(nreceived.value());
|
2019-12-01 19:36:06 +03:00
|
|
|
return nreceived;
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|
|
|
|
|
2021-05-11 20:47:40 +03:00
|
|
|
bool IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port, ReadonlyBytes packet, const Time& packet_timestamp)
|
2019-03-12 19:27:07 +03:00
|
|
|
{
|
2021-04-25 01:27:32 +03:00
|
|
|
Locker 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) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("IPv4Socket({}): did_receive refusing packet since buffer is full.", this);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(m_can_read);
|
2019-12-14 11:43:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
2020-09-12 06:11:07 +03:00
|
|
|
auto scratch_buffer = UserOrKernelBuffer::for_kernel_buffer(m_scratch_buffer.value().data());
|
2020-12-18 18:13:23 +03:00
|
|
|
auto nreceived_or_error = protocol_receive(ReadonlyBytes { packet.data(), packet.size() }, scratch_buffer, m_scratch_buffer.value().size(), 0);
|
2020-08-04 19:02:23 +03:00
|
|
|
if (nreceived_or_error.is_error())
|
|
|
|
return false;
|
2020-09-12 06:11:07 +03:00
|
|
|
ssize_t nwritten = m_receive_buffer.write(scratch_buffer, nreceived_or_error.value());
|
|
|
|
if (nwritten < 0)
|
|
|
|
return false;
|
2020-11-30 02:05:27 +03:00
|
|
|
set_can_read(!m_receive_buffer.is_empty());
|
2019-12-14 11:43:31 +03:00
|
|
|
} else {
|
2020-08-05 07:35:30 +03:00
|
|
|
if (m_receive_queue.size() > 2000) {
|
2021-01-10 17:17:54 +03:00
|
|
|
dbgln("IPv4Socket({}): did_receive refusing packet since queue is full.", this);
|
2019-12-14 11:43:31 +03:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-11 20:47:40 +03:00
|
|
|
m_receive_queue.append({ source_address, source_port, packet_timestamp, KBuffer::copy(packet.data(), packet.size()) });
|
2020-11-30 02:05:27 +03:00
|
|
|
set_can_read(true);
|
2019-12-14 11:43:31 +03:00
|
|
|
}
|
2019-03-14 17:28:23 +03:00
|
|
|
m_bytes_received += packet_size;
|
2021-01-14 02:21:21 +03:00
|
|
|
|
2021-01-24 01:59:27 +03:00
|
|
|
if constexpr (IPV4_SOCKET_DEBUG) {
|
2021-01-14 02:21:21 +03:00
|
|
|
if (buffer_mode() == BufferMode::Bytes)
|
|
|
|
dbgln("IPv4Socket({}): did_receive {} bytes, total_received={}", this, packet_size, m_bytes_received);
|
|
|
|
else
|
|
|
|
dbgln("IPv4Socket({}): did_receive {} bytes, total_received={}, packets in queue: {}",
|
|
|
|
this,
|
|
|
|
packet_size,
|
|
|
|
m_bytes_received,
|
|
|
|
m_receive_queue.size());
|
|
|
|
}
|
|
|
|
|
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:");
|
|
|
|
|
2021-02-09 18:08:43 +03:00
|
|
|
builder.appendff("{}:{}", m_local_address.to_string(), m_local_port);
|
2019-08-10 19:10:36 +03:00
|
|
|
if (m_role == Role::Accepted || m_role == Role::Connected)
|
2021-02-09 18:08:43 +03:00
|
|
|
builder.appendff(" / {}:{}", m_peer_address.to_string(), m_peer_port);
|
2019-08-10 19:10:36 +03:00
|
|
|
|
|
|
|
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:
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY_NOT_REACHED();
|
2019-08-10 19:10:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
2019-09-19 22:40:06 +03:00
|
|
|
|
2020-08-07 10:18:20 +03:00
|
|
|
KResult IPv4Socket::setsockopt(int level, int option, Userspace<const void*> user_value, socklen_t user_value_size)
|
2019-09-19 22:40:06 +03:00
|
|
|
{
|
|
|
|
if (level != IPPROTO_IP)
|
2020-07-31 01:26:33 +03:00
|
|
|
return Socket::setsockopt(level, option, user_value, user_value_size);
|
2019-09-19 22:40:06 +03:00
|
|
|
|
|
|
|
switch (option) {
|
2020-07-31 01:26:33 +03:00
|
|
|
case IP_TTL: {
|
|
|
|
if (user_value_size < sizeof(int))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2020-07-31 01:26:33 +03:00
|
|
|
int value;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_from_user(&value, static_ptr_cast<const int*>(user_value)))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-07-31 01:26:33 +03:00
|
|
|
if (value < 0 || value > 255)
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2020-07-31 01:26:33 +03:00
|
|
|
m_ttl = value;
|
2019-09-19 22:40:06 +03:00
|
|
|
return KSuccess;
|
2020-07-31 01:26:33 +03:00
|
|
|
}
|
2021-05-04 14:42:47 +03:00
|
|
|
case IP_MULTICAST_LOOP: {
|
|
|
|
if (user_value_size != 1)
|
|
|
|
return EINVAL;
|
|
|
|
u8 value;
|
|
|
|
if (!copy_from_user(&value, static_ptr_cast<const u8*>(user_value)))
|
|
|
|
return EFAULT;
|
|
|
|
if (value != 0 && value != 1)
|
|
|
|
return EINVAL;
|
|
|
|
m_multicast_loop = value;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
case IP_ADD_MEMBERSHIP: {
|
|
|
|
if (user_value_size != sizeof(ip_mreq))
|
|
|
|
return EINVAL;
|
|
|
|
ip_mreq mreq;
|
|
|
|
if (!copy_from_user(&mreq, static_ptr_cast<const ip_mreq*>(user_value)))
|
|
|
|
return EFAULT;
|
|
|
|
if (mreq.imr_interface.s_addr != INADDR_ANY)
|
|
|
|
return ENOTSUP;
|
|
|
|
IPv4Address address { (const u8*)&mreq.imr_multiaddr.s_addr };
|
|
|
|
if (!m_multicast_memberships.contains_slow(address))
|
|
|
|
m_multicast_memberships.append(address);
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
case IP_DROP_MEMBERSHIP: {
|
|
|
|
if (user_value_size != sizeof(ip_mreq))
|
|
|
|
return EINVAL;
|
|
|
|
ip_mreq mreq;
|
|
|
|
if (!copy_from_user(&mreq, static_ptr_cast<const ip_mreq*>(user_value)))
|
|
|
|
return EFAULT;
|
|
|
|
if (mreq.imr_interface.s_addr != INADDR_ANY)
|
|
|
|
return ENOTSUP;
|
|
|
|
IPv4Address address { (const u8*)&mreq.imr_multiaddr.s_addr };
|
|
|
|
m_multicast_memberships.remove_first_matching([&address](auto& a) { return a == address; });
|
|
|
|
return KSuccess;
|
|
|
|
}
|
2019-09-19 22:40:06 +03:00
|
|
|
default:
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOPROTOOPT;
|
2019-09-19 22:40:06 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 12:29:05 +03:00
|
|
|
KResult IPv4Socket::getsockopt(FileDescription& description, int level, int option, Userspace<void*> value, Userspace<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
|
|
|
|
2020-08-07 12:29:05 +03:00
|
|
|
socklen_t size;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_from_user(&size, value_size.unsafe_userspace_ptr()))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-08-07 12:29:05 +03:00
|
|
|
|
2019-09-19 22:40:06 +03:00
|
|
|
switch (option) {
|
|
|
|
case IP_TTL:
|
2020-08-07 12:29:05 +03:00
|
|
|
if (size < sizeof(int))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EINVAL;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(static_ptr_cast<int*>(value), (int*)&m_ttl))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2020-08-07 12:29:05 +03:00
|
|
|
size = sizeof(int);
|
2020-09-12 06:11:07 +03:00
|
|
|
if (!copy_to_user(value_size, &size))
|
2021-01-21 01:11:17 +03:00
|
|
|
return EFAULT;
|
2019-09-19 22:40:06 +03:00
|
|
|
return KSuccess;
|
2021-05-04 14:42:47 +03:00
|
|
|
case IP_MULTICAST_LOOP: {
|
|
|
|
if (size < 1)
|
|
|
|
return EINVAL;
|
|
|
|
if (!copy_to_user(static_ptr_cast<u8*>(value), (const u8*)&m_multicast_loop))
|
|
|
|
return EFAULT;
|
|
|
|
size = 1;
|
|
|
|
if (!copy_to_user(value_size, &size))
|
|
|
|
return EFAULT;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
2019-09-19 22:40:06 +03:00
|
|
|
default:
|
2021-01-21 01:11:17 +03:00
|
|
|
return ENOPROTOOPT;
|
2019-09-19 22:40:06 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-23 20:06:03 +03:00
|
|
|
|
2020-05-23 14:17:58 +03:00
|
|
|
int IPv4Socket::ioctl(FileDescription&, unsigned request, FlatPtr arg)
|
2019-09-23 20:06:03 +03:00
|
|
|
{
|
2020-01-12 15:28:07 +03:00
|
|
|
REQUIRE_PROMISE(inet);
|
2019-09-23 20:06:03 +03:00
|
|
|
|
2020-03-14 22:00:49 +03:00
|
|
|
auto ioctl_route = [request, arg]() {
|
2020-09-12 06:11:07 +03:00
|
|
|
rtentry route;
|
|
|
|
if (!copy_from_user(&route, (rtentry*)arg))
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EFAULT;
|
2019-09-23 20:06:03 +03:00
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
auto copied_ifname = copy_string_from_user(route.rt_dev, IFNAMSIZ);
|
|
|
|
if (copied_ifname.is_null())
|
|
|
|
return -EFAULT;
|
2020-03-14 22:00:49 +03:00
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
auto adapter = NetworkAdapter::lookup_by_name(copied_ifname);
|
2020-03-14 22:00:49 +03:00
|
|
|
if (!adapter)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case SIOCADDRT:
|
2020-06-29 00:34:31 +03:00
|
|
|
if (!Process::current()->is_superuser())
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EPERM;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (route.rt_gateway.sa_family != AF_INET)
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EAFNOSUPPORT;
|
2020-09-12 06:11:07 +03:00
|
|
|
if ((route.rt_flags & (RTF_UP | RTF_GATEWAY)) != (RTF_UP | RTF_GATEWAY))
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EINVAL; // FIXME: Find the correct value to return
|
2020-09-12 06:11:07 +03:00
|
|
|
adapter->set_ipv4_gateway(IPv4Address(((sockaddr_in&)route.rt_gateway).sin_addr.s_addr));
|
2020-03-14 22:00:49 +03:00
|
|
|
return 0;
|
2019-09-23 20:06:03 +03:00
|
|
|
|
2020-03-14 22:00:49 +03:00
|
|
|
case SIOCDELRT:
|
|
|
|
// FIXME: Support gateway deletion
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-11 23:30:41 +03:00
|
|
|
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EINVAL;
|
|
|
|
};
|
2019-10-02 19:20:11 +03:00
|
|
|
|
2020-03-14 22:00:49 +03:00
|
|
|
auto ioctl_interface = [request, arg]() {
|
2020-09-12 06:11:07 +03:00
|
|
|
ifreq* user_ifr = (ifreq*)arg;
|
|
|
|
ifreq ifr;
|
|
|
|
if (!copy_from_user(&ifr, user_ifr))
|
2019-10-02 19:20:11 +03:00
|
|
|
return -EFAULT;
|
2020-03-14 22:00:49 +03:00
|
|
|
|
|
|
|
char namebuf[IFNAMSIZ + 1];
|
2020-09-12 06:11:07 +03:00
|
|
|
memcpy(namebuf, ifr.ifr_name, IFNAMSIZ);
|
2020-03-14 22:00:49 +03:00
|
|
|
namebuf[sizeof(namebuf) - 1] = '\0';
|
|
|
|
|
|
|
|
auto adapter = NetworkAdapter::lookup_by_name(namebuf);
|
|
|
|
if (!adapter)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case SIOCSIFADDR:
|
2020-06-29 00:34:31 +03:00
|
|
|
if (!Process::current()->is_superuser())
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EPERM;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (ifr.ifr_addr.sa_family != AF_INET)
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EAFNOSUPPORT;
|
2020-09-12 06:11:07 +03:00
|
|
|
adapter->set_ipv4_address(IPv4Address(((sockaddr_in&)ifr.ifr_addr).sin_addr.s_addr));
|
2020-03-14 22:00:49 +03:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
case SIOCSIFNETMASK:
|
2020-06-29 00:34:31 +03:00
|
|
|
if (!Process::current()->is_superuser())
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EPERM;
|
2020-09-12 06:11:07 +03:00
|
|
|
if (ifr.ifr_addr.sa_family != AF_INET)
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EAFNOSUPPORT;
|
2020-09-12 06:11:07 +03:00
|
|
|
adapter->set_ipv4_netmask(IPv4Address(((sockaddr_in&)ifr.ifr_netmask).sin_addr.s_addr));
|
2020-03-14 22:00:49 +03:00
|
|
|
return 0;
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
case SIOCGIFADDR: {
|
|
|
|
u16 sa_family = AF_INET;
|
|
|
|
if (!copy_to_user(&user_ifr->ifr_addr.sa_family, &sa_family))
|
|
|
|
return -EFAULT;
|
|
|
|
auto ip4_addr = adapter->ipv4_address().to_u32();
|
|
|
|
if (!copy_to_user(&((sockaddr_in&)user_ifr->ifr_addr).sin_addr.s_addr, &ip4_addr, sizeof(ip4_addr)))
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
2020-09-12 06:11:07 +03:00
|
|
|
}
|
2020-03-14 22:00:49 +03:00
|
|
|
|
2021-04-16 18:48:41 +03:00
|
|
|
case SIOCGIFNETMASK: {
|
|
|
|
u16 sa_family = AF_INET;
|
|
|
|
if (!copy_to_user(&user_ifr->ifr_addr.sa_family, &sa_family))
|
|
|
|
return -EFAULT;
|
|
|
|
auto ip4_netmask = adapter->ipv4_netmask().to_u32();
|
|
|
|
// NOTE: NOT ifr_netmask.
|
|
|
|
if (!copy_to_user(&((sockaddr_in&)user_ifr->ifr_addr).sin_addr.s_addr, &ip4_netmask, sizeof(ip4_netmask)))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-12 06:11:07 +03:00
|
|
|
case SIOCGIFHWADDR: {
|
|
|
|
u16 sa_family = AF_INET;
|
|
|
|
if (!copy_to_user(&user_ifr->ifr_hwaddr.sa_family, &sa_family))
|
|
|
|
return -EFAULT;
|
|
|
|
auto mac_address = adapter->mac_address();
|
|
|
|
if (!copy_to_user(ifr.ifr_hwaddr.sa_data, &mac_address, sizeof(MACAddress)))
|
2020-03-14 22:00:49 +03:00
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
2019-10-02 19:20:11 +03:00
|
|
|
}
|
2021-04-16 18:48:41 +03:00
|
|
|
|
|
|
|
case SIOCGIFBRDADDR: {
|
|
|
|
u16 sa_family = AF_INET;
|
|
|
|
if (!copy_to_user(&user_ifr->ifr_addr.sa_family, &sa_family))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
// Broadcast address is basically the reverse of the netmask, i.e.
|
|
|
|
// instead of zeroing out the end, you OR with 1 instead.
|
|
|
|
auto ip4_netmask = adapter->ipv4_netmask().to_u32();
|
|
|
|
auto broadcast_addr = adapter->ipv4_address().to_u32() | ~ip4_netmask;
|
|
|
|
|
|
|
|
if (!copy_to_user(&((sockaddr_in&)user_ifr->ifr_addr).sin_addr.s_addr, &broadcast_addr, sizeof(broadcast_addr)))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIOCGIFMTU: {
|
|
|
|
u16 sa_family = AF_INET;
|
|
|
|
if (!copy_to_user(&user_ifr->ifr_addr.sa_family, &sa_family))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
auto ip4_metric = adapter->mtu();
|
|
|
|
|
|
|
|
if (!copy_to_user(&user_ifr->ifr_metric, &ip4_metric, sizeof(ip4_metric)))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIOCGIFFLAGS: {
|
|
|
|
u16 sa_family = AF_INET;
|
|
|
|
if (!copy_to_user(&user_ifr->ifr_addr.sa_family, &sa_family))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
// FIXME: stub!
|
|
|
|
short flags = 1;
|
|
|
|
|
|
|
|
if (!copy_to_user(&user_ifr->ifr_flags, &flags, sizeof(flags)))
|
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
case SIOCGIFCONF: {
|
|
|
|
// FIXME: stub!
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-09-12 06:11:07 +03:00
|
|
|
}
|
2020-03-14 22:00:49 +03:00
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (request) {
|
|
|
|
case SIOCSIFADDR:
|
|
|
|
case SIOCSIFNETMASK:
|
|
|
|
case SIOCGIFADDR:
|
|
|
|
case SIOCGIFHWADDR:
|
2021-04-16 18:48:41 +03:00
|
|
|
case SIOCGIFNETMASK:
|
|
|
|
case SIOCGIFBRDADDR:
|
|
|
|
case SIOCGIFMTU:
|
|
|
|
case SIOCGIFFLAGS:
|
|
|
|
case SIOCGIFCONF:
|
2020-03-14 22:00:49 +03:00
|
|
|
return ioctl_interface();
|
|
|
|
|
|
|
|
case SIOCADDRT:
|
|
|
|
case SIOCDELRT:
|
|
|
|
return ioctl_route();
|
2019-09-23 20:06:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2020-02-08 17:52:32 +03:00
|
|
|
|
2020-06-02 19:20:05 +03:00
|
|
|
KResult IPv4Socket::close()
|
2020-02-08 17:52:32 +03:00
|
|
|
{
|
2020-12-21 02:09:48 +03:00
|
|
|
[[maybe_unused]] auto rc = shutdown(SHUT_RDWR);
|
2020-06-02 19:20:05 +03:00
|
|
|
return KSuccess;
|
2020-02-08 17:52:32 +03:00
|
|
|
}
|
2020-02-08 17:59:21 +03:00
|
|
|
|
|
|
|
void IPv4Socket::shut_down_for_reading()
|
|
|
|
{
|
|
|
|
Socket::shut_down_for_reading();
|
2020-11-30 02:05:27 +03:00
|
|
|
set_can_read(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPv4Socket::set_can_read(bool value)
|
|
|
|
{
|
|
|
|
m_can_read = value;
|
|
|
|
if (value)
|
|
|
|
evaluate_block_conditions();
|
2020-02-08 17:59:21 +03:00
|
|
|
}
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|