2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-08-10 19:10:36 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-04-05 01:16:45 +04:30
|
|
|
#include <AK/StringView.h>
|
2021-01-25 16:07:10 +01:00
|
|
|
#include <Kernel/Debug.h>
|
2019-06-07 09:36:51 +02:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-04-02 19:54:38 +02:00
|
|
|
#include <Kernel/Net/IPv4Socket.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-02-14 17:18:35 +01:00
|
|
|
#include <Kernel/Process.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-02-14 14:17:38 +01:00
|
|
|
#include <LibC/errno_numbers.h>
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
KResultOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol)
|
2019-02-14 14:17:38 +01:00
|
|
|
{
|
|
|
|
switch (domain) {
|
|
|
|
case AF_LOCAL:
|
2019-02-14 15:17:30 +01:00
|
|
|
return LocalSocket::create(type & SOCK_TYPE_MASK);
|
2019-03-12 15:51:42 +01:00
|
|
|
case AF_INET:
|
|
|
|
return IPv4Socket::create(type & SOCK_TYPE_MASK, protocol);
|
2019-02-14 14:17:38 +01:00
|
|
|
default:
|
2021-01-20 23:11:17 +01:00
|
|
|
return EAFNOSUPPORT;
|
2019-02-14 14:17:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::Socket(int domain, int type, int protocol)
|
2019-05-02 03:28:20 +02:00
|
|
|
: m_domain(domain)
|
2019-02-14 14:17:38 +01:00
|
|
|
, m_type(type)
|
|
|
|
, m_protocol(protocol)
|
|
|
|
{
|
2020-06-28 15:34:31 -06:00
|
|
|
auto& process = *Process::current();
|
2020-08-08 17:32:34 +02:00
|
|
|
m_origin = { process.pid().value(), process.uid(), process.gid() };
|
2019-02-14 14:17:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Socket::~Socket()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-10 13:17:00 +10:00
|
|
|
void Socket::set_setup_state(SetupState new_setup_state)
|
|
|
|
{
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(SOCKET_DEBUG, "Socket({}) setup state moving from {} to {}", this, to_string(m_setup_state), to_string(new_setup_state));
|
2019-08-10 13:17:00 +10:00
|
|
|
m_setup_state = new_setup_state;
|
2020-11-29 16:05:27 -07:00
|
|
|
evaluate_block_conditions();
|
2019-08-10 13:17:00 +10:00
|
|
|
}
|
|
|
|
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<Socket> Socket::accept()
|
2019-02-14 15:17:30 +01:00
|
|
|
{
|
2021-04-24 15:27:32 -07:00
|
|
|
Locker locker(m_lock);
|
2019-02-14 15:17:30 +01:00
|
|
|
if (m_pending.is_empty())
|
|
|
|
return nullptr;
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(SOCKET_DEBUG, "Socket({}) de-queueing connection", this);
|
2019-02-14 15:17:30 +01:00
|
|
|
auto client = m_pending.take_first();
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!client->is_connected());
|
2020-06-28 15:34:31 -06:00
|
|
|
auto& process = *Process::current();
|
2020-08-08 17:32:34 +02:00
|
|
|
client->m_acceptor = { process.pid().value(), process.uid(), process.gid() };
|
2019-02-14 17:18:35 +01:00
|
|
|
client->m_connected = true;
|
2019-08-11 16:38:20 +03:00
|
|
|
client->m_role = Role::Accepted;
|
2020-11-29 16:05:27 -07:00
|
|
|
if (!m_pending.is_empty())
|
|
|
|
evaluate_block_conditions();
|
2019-02-14 15:17:30 +01:00
|
|
|
return client;
|
|
|
|
}
|
2019-02-14 17:18:35 +01:00
|
|
|
|
2019-08-09 12:41:06 +10:00
|
|
|
KResult Socket::queue_connection_from(NonnullRefPtr<Socket> peer)
|
2019-02-14 17:18:35 +01:00
|
|
|
{
|
2021-02-07 15:33:24 +03:30
|
|
|
dbgln_if(SOCKET_DEBUG, "Socket({}) queueing connection", this);
|
2021-04-24 15:27:32 -07:00
|
|
|
Locker locker(m_lock);
|
2019-03-06 22:14:31 +01:00
|
|
|
if (m_pending.size() >= m_backlog)
|
2021-01-20 23:11:17 +01:00
|
|
|
return ECONNREFUSED;
|
2019-02-14 17:18:35 +01:00
|
|
|
m_pending.append(peer);
|
2020-11-29 16:05:27 -07:00
|
|
|
evaluate_block_conditions();
|
2019-03-06 22:14:31 +01:00
|
|
|
return KSuccess;
|
2019-02-14 17:18:35 +01:00
|
|
|
}
|
2019-03-13 13:13:23 +01:00
|
|
|
|
2020-08-07 00:18:20 -07:00
|
|
|
KResult Socket::setsockopt(int level, int option, Userspace<const void*> user_value, socklen_t user_value_size)
|
2019-03-13 13:13:23 +01:00
|
|
|
{
|
2021-02-13 01:29:28 +01:00
|
|
|
if (level != SOL_SOCKET)
|
|
|
|
return ENOPROTOOPT;
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(level == SOL_SOCKET);
|
2019-03-13 13:13:23 +01:00
|
|
|
switch (option) {
|
|
|
|
case SO_SNDTIMEO:
|
2020-07-31 00:26:33 +02:00
|
|
|
if (user_value_size != sizeof(timeval))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2021-02-21 20:28:20 +01:00
|
|
|
{
|
|
|
|
auto timeout = copy_time_from_user(static_ptr_cast<const timeval*>(user_value));
|
|
|
|
if (!timeout.has_value())
|
|
|
|
return EFAULT;
|
2021-02-28 02:48:45 +01:00
|
|
|
m_send_timeout = timeout.value();
|
2021-02-21 20:28:20 +01:00
|
|
|
}
|
2019-03-13 13:13:23 +01:00
|
|
|
return KSuccess;
|
|
|
|
case SO_RCVTIMEO:
|
2020-07-31 00:26:33 +02:00
|
|
|
if (user_value_size != sizeof(timeval))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2021-02-21 20:28:20 +01:00
|
|
|
{
|
|
|
|
auto timeout = copy_time_from_user(static_ptr_cast<const timeval*>(user_value));
|
|
|
|
if (!timeout.has_value())
|
|
|
|
return EFAULT;
|
2021-02-28 02:48:45 +01:00
|
|
|
m_receive_timeout = timeout.value();
|
2021-02-21 20:28:20 +01:00
|
|
|
}
|
2019-03-13 13:13:23 +01:00
|
|
|
return KSuccess;
|
2020-04-05 01:16:45 +04:30
|
|
|
case SO_BINDTODEVICE: {
|
2020-07-31 00:26:33 +02:00
|
|
|
if (user_value_size != IFNAMSIZ)
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2020-08-07 00:18:20 -07:00
|
|
|
auto user_string = static_ptr_cast<const char*>(user_value);
|
2020-09-11 21:11:07 -06:00
|
|
|
auto ifname = copy_string_from_user(user_string, user_value_size);
|
2020-07-31 00:26:33 +02:00
|
|
|
if (ifname.is_null())
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-04-05 01:16:45 +04:30
|
|
|
auto device = NetworkAdapter::lookup_by_name(ifname);
|
|
|
|
if (!device)
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENODEV;
|
2020-04-05 01:16:45 +04:30
|
|
|
m_bound_interface = device;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
2020-04-05 18:10:19 +02:00
|
|
|
case SO_KEEPALIVE:
|
|
|
|
// FIXME: Obviously, this is not a real keepalive.
|
|
|
|
return KSuccess;
|
2020-09-16 12:32:45 -04:00
|
|
|
case SO_TIMESTAMP:
|
|
|
|
if (user_value_size != sizeof(int))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2021-02-21 20:03:44 +01:00
|
|
|
{
|
|
|
|
int timestamp;
|
|
|
|
if (!copy_from_user(×tamp, static_ptr_cast<const int*>(user_value)))
|
|
|
|
return EFAULT;
|
|
|
|
m_timestamp = timestamp;
|
|
|
|
}
|
2020-09-16 12:32:45 -04:00
|
|
|
if (m_timestamp && (domain() != AF_INET || type() == SOCK_STREAM)) {
|
|
|
|
// FIXME: Support SO_TIMESTAMP for more protocols?
|
|
|
|
m_timestamp = 0;
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOTSUP;
|
2020-09-16 12:32:45 -04:00
|
|
|
}
|
|
|
|
return KSuccess;
|
2019-03-13 13:13:23 +01:00
|
|
|
default:
|
2021-01-10 15:43:09 +01:00
|
|
|
dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOPROTOOPT;
|
2019-03-13 13:13:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 02:29:05 -07:00
|
|
|
KResult Socket::getsockopt(FileDescription&, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
|
2019-03-13 13:13:23 +01:00
|
|
|
{
|
2020-08-07 02:29:05 -07:00
|
|
|
socklen_t size;
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!copy_from_user(&size, value_size.unsafe_userspace_ptr()))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-08-07 02:29:05 -07:00
|
|
|
|
2020-09-09 07:41:57 +01:00
|
|
|
// FIXME: Add TCP_NODELAY, IPPROTO_TCP and IPPROTO_IP (used in OpenSSH)
|
|
|
|
if (level != SOL_SOCKET) {
|
|
|
|
// Not sure if this is the correct error code, but it's only temporary until other levels are implemented.
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOPROTOOPT;
|
2020-09-09 07:41:57 +01:00
|
|
|
}
|
|
|
|
|
2019-03-13 13:13:23 +01:00
|
|
|
switch (option) {
|
|
|
|
case SO_SNDTIMEO:
|
2020-08-07 02:29:05 -07:00
|
|
|
if (size < sizeof(timeval))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2021-02-28 02:48:45 +01:00
|
|
|
{
|
|
|
|
timeval tv = m_send_timeout.to_timeval();
|
|
|
|
if (!copy_to_user(static_ptr_cast<timeval*>(value), &tv))
|
|
|
|
return EFAULT;
|
|
|
|
}
|
2020-08-07 02:29:05 -07:00
|
|
|
size = sizeof(timeval);
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!copy_to_user(value_size, &size))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2019-03-13 13:13:23 +01:00
|
|
|
return KSuccess;
|
|
|
|
case SO_RCVTIMEO:
|
2020-08-07 02:29:05 -07:00
|
|
|
if (size < sizeof(timeval))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2021-02-28 02:48:45 +01:00
|
|
|
{
|
|
|
|
timeval tv = m_send_timeout.to_timeval();
|
|
|
|
if (!copy_to_user(static_ptr_cast<timeval*>(value), &tv))
|
|
|
|
return EFAULT;
|
|
|
|
}
|
2020-08-07 02:29:05 -07:00
|
|
|
size = sizeof(timeval);
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!copy_to_user(value_size, &size))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2019-03-13 13:13:23 +01:00
|
|
|
return KSuccess;
|
2020-08-07 02:29:05 -07:00
|
|
|
case SO_ERROR: {
|
|
|
|
if (size < sizeof(int))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2021-01-09 18:51:44 +01:00
|
|
|
dbgln("getsockopt(SO_ERROR): FIXME!");
|
2020-08-07 02:29:05 -07:00
|
|
|
int errno = 0;
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!copy_to_user(static_ptr_cast<int*>(value), &errno))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-08-07 02:29:05 -07:00
|
|
|
size = sizeof(int);
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!copy_to_user(value_size, &size))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2019-05-29 21:59:50 +02:00
|
|
|
return KSuccess;
|
2020-08-07 02:29:05 -07:00
|
|
|
}
|
2020-04-05 01:16:45 +04:30
|
|
|
case SO_BINDTODEVICE:
|
2020-08-07 02:29:05 -07:00
|
|
|
if (size < IFNAMSIZ)
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2020-04-05 01:16:45 +04:30
|
|
|
if (m_bound_interface) {
|
|
|
|
const auto& name = m_bound_interface->name();
|
|
|
|
auto length = name.length() + 1;
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!copy_to_user(static_ptr_cast<char*>(value), name.characters(), length))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-08-07 02:29:05 -07:00
|
|
|
size = length;
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!copy_to_user(value_size, &size))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-04-05 01:16:45 +04:30
|
|
|
return KSuccess;
|
|
|
|
} else {
|
2020-08-07 02:29:05 -07:00
|
|
|
size = 0;
|
2020-09-11 21:11:07 -06:00
|
|
|
if (!copy_to_user(value_size, &size))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-08-07 02:29:05 -07:00
|
|
|
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-04-05 01:16:45 +04:30
|
|
|
}
|
2020-09-16 12:32:45 -04:00
|
|
|
case SO_TIMESTAMP:
|
|
|
|
if (size < sizeof(int))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EINVAL;
|
2020-09-16 12:32:45 -04:00
|
|
|
if (!copy_to_user(static_ptr_cast<int*>(value), &m_timestamp))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-09-16 12:32:45 -04:00
|
|
|
size = sizeof(int);
|
|
|
|
if (!copy_to_user(value_size, &size))
|
2021-01-20 23:11:17 +01:00
|
|
|
return EFAULT;
|
2020-09-16 12:32:45 -04:00
|
|
|
return KSuccess;
|
2019-03-13 13:13:23 +01:00
|
|
|
default:
|
2021-01-10 15:43:09 +01:00
|
|
|
dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOPROTOOPT;
|
2019-03-13 13:13:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 13:18:51 +01:00
|
|
|
KResultOr<size_t> Socket::read(FileDescription& description, u64, UserOrKernelBuffer& buffer, size_t size)
|
2019-08-05 10:03:19 +02:00
|
|
|
{
|
2020-02-08 00:52:33 +01:00
|
|
|
if (is_shut_down_for_reading())
|
|
|
|
return 0;
|
2021-02-28 02:48:45 +01:00
|
|
|
Time t {};
|
|
|
|
return recvfrom(description, buffer, size, 0, {}, 0, t);
|
2019-08-05 10:03:19 +02:00
|
|
|
}
|
|
|
|
|
2021-03-17 13:18:51 +01:00
|
|
|
KResultOr<size_t> Socket::write(FileDescription& description, u64, const UserOrKernelBuffer& data, size_t size)
|
2019-08-05 10:03:19 +02:00
|
|
|
{
|
2020-02-08 00:52:33 +01:00
|
|
|
if (is_shut_down_for_writing())
|
2021-03-15 09:04:04 +01:00
|
|
|
return EPIPE;
|
2020-08-17 23:49:35 -07:00
|
|
|
return sendto(description, data, size, 0, {}, 0);
|
2019-08-05 10:03:19 +02:00
|
|
|
}
|
2020-02-08 00:52:33 +01:00
|
|
|
|
|
|
|
KResult Socket::shutdown(int how)
|
|
|
|
{
|
2021-04-24 15:27:32 -07:00
|
|
|
Locker locker(lock());
|
2020-02-08 00:52:33 +01:00
|
|
|
if (type() == SOCK_STREAM && !is_connected())
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOTCONN;
|
2020-02-08 15:52:32 +01:00
|
|
|
if (m_role == Role::Listener)
|
2021-01-20 23:11:17 +01:00
|
|
|
return ENOTCONN;
|
2020-02-08 15:52:32 +01:00
|
|
|
if (!m_shut_down_for_writing && (how & SHUT_WR))
|
|
|
|
shut_down_for_writing();
|
|
|
|
if (!m_shut_down_for_reading && (how & SHUT_RD))
|
|
|
|
shut_down_for_reading();
|
|
|
|
m_shut_down_for_reading |= (how & SHUT_RD) != 0;
|
|
|
|
m_shut_down_for_writing |= (how & SHUT_WR) != 0;
|
2020-02-08 00:52:33 +01:00
|
|
|
return KSuccess;
|
|
|
|
}
|
2020-02-16 01:27:42 +01:00
|
|
|
|
2020-09-06 18:31:51 +02:00
|
|
|
KResult Socket::stat(::stat& st) const
|
|
|
|
{
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
|
|
st.st_mode = S_IFSOCK;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2020-12-13 19:15:42 +01:00
|
|
|
void Socket::set_connected(bool connected)
|
|
|
|
{
|
2021-04-24 15:27:32 -07:00
|
|
|
Locker locker(lock());
|
2020-12-13 19:15:42 +01:00
|
|
|
if (m_connected == connected)
|
|
|
|
return;
|
|
|
|
m_connected = connected;
|
|
|
|
evaluate_block_conditions();
|
|
|
|
}
|
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
}
|