2020-01-18 08:38:21 +00: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 16:10:36 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
2020-04-04 20:46:45 +00:00
|
|
|
#include <AK/StringView.h>
|
2019-06-07 07:36:51 +00:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
2019-04-02 17:54:38 +00:00
|
|
|
#include <Kernel/Net/IPv4Socket.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-02-14 16:18:35 +00:00
|
|
|
#include <Kernel/Process.h>
|
2019-06-07 09:43:58 +00:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-02-14 13:17:38 +00:00
|
|
|
#include <LibC/errno_numbers.h>
|
|
|
|
|
2019-08-10 03:17:00 +00:00
|
|
|
//#define SOCKET_DEBUG
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
KResultOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol)
|
2019-02-14 13:17:38 +00:00
|
|
|
{
|
|
|
|
switch (domain) {
|
|
|
|
case AF_LOCAL:
|
2019-02-14 14:17:30 +00:00
|
|
|
return LocalSocket::create(type & SOCK_TYPE_MASK);
|
2019-03-12 14:51:42 +00:00
|
|
|
case AF_INET:
|
|
|
|
return IPv4Socket::create(type & SOCK_TYPE_MASK, protocol);
|
2019-02-14 13:17:38 +00:00
|
|
|
default:
|
2019-03-06 21:14:31 +00:00
|
|
|
return KResult(-EAFNOSUPPORT);
|
2019-02-14 13:17:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Socket::Socket(int domain, int type, int protocol)
|
2019-05-02 01:28:20 +00:00
|
|
|
: m_domain(domain)
|
2019-02-14 13:17:38 +00:00
|
|
|
, m_type(type)
|
|
|
|
, m_protocol(protocol)
|
|
|
|
{
|
2020-06-28 21:34:31 +00:00
|
|
|
auto& process = *Process::current();
|
2020-08-08 15:32:34 +00:00
|
|
|
m_origin = { process.pid().value(), process.uid(), process.gid() };
|
2019-02-14 13:17:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Socket::~Socket()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-10 03:17:00 +00:00
|
|
|
void Socket::set_setup_state(SetupState new_setup_state)
|
|
|
|
{
|
|
|
|
#ifdef SOCKET_DEBUG
|
2020-03-01 19:45:39 +00:00
|
|
|
dbg() << "Socket{" << this << "} setup state moving from " << to_string(m_setup_state) << " to " << to_string(new_setup_state);
|
2019-08-10 03:17:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
m_setup_state = new_setup_state;
|
2020-11-29 23:05:27 +00:00
|
|
|
evaluate_block_conditions();
|
2019-08-10 03:17:00 +00:00
|
|
|
}
|
|
|
|
|
2019-06-21 16:37:47 +00:00
|
|
|
RefPtr<Socket> Socket::accept()
|
2019-02-14 14:17:30 +00:00
|
|
|
{
|
|
|
|
LOCKER(m_lock);
|
|
|
|
if (m_pending.is_empty())
|
|
|
|
return nullptr;
|
2019-08-10 03:17:00 +00:00
|
|
|
#ifdef SOCKET_DEBUG
|
2020-03-01 19:45:39 +00:00
|
|
|
dbg() << "Socket{" << this << "} de-queueing connection";
|
2019-08-10 03:17:00 +00:00
|
|
|
#endif
|
2019-02-14 14:17:30 +00:00
|
|
|
auto client = m_pending.take_first();
|
2019-02-14 16:18:35 +00:00
|
|
|
ASSERT(!client->is_connected());
|
2020-06-28 21:34:31 +00:00
|
|
|
auto& process = *Process::current();
|
2020-08-08 15:32:34 +00:00
|
|
|
client->m_acceptor = { process.pid().value(), process.uid(), process.gid() };
|
2019-02-14 16:18:35 +00:00
|
|
|
client->m_connected = true;
|
2019-08-11 13:38:20 +00:00
|
|
|
client->m_role = Role::Accepted;
|
2020-11-29 23:05:27 +00:00
|
|
|
if (!m_pending.is_empty())
|
|
|
|
evaluate_block_conditions();
|
2019-02-14 14:17:30 +00:00
|
|
|
return client;
|
|
|
|
}
|
2019-02-14 16:18:35 +00:00
|
|
|
|
2019-08-09 02:41:06 +00:00
|
|
|
KResult Socket::queue_connection_from(NonnullRefPtr<Socket> peer)
|
2019-02-14 16:18:35 +00:00
|
|
|
{
|
2019-08-10 03:17:00 +00:00
|
|
|
#ifdef SOCKET_DEBUG
|
2020-03-01 19:45:39 +00:00
|
|
|
dbg() << "Socket{" << this << "} queueing connection";
|
2019-08-10 03:17:00 +00:00
|
|
|
#endif
|
2019-02-14 16:18:35 +00:00
|
|
|
LOCKER(m_lock);
|
2019-03-06 21:14:31 +00:00
|
|
|
if (m_pending.size() >= m_backlog)
|
|
|
|
return KResult(-ECONNREFUSED);
|
2019-02-14 16:18:35 +00:00
|
|
|
m_pending.append(peer);
|
2020-11-29 23:05:27 +00:00
|
|
|
evaluate_block_conditions();
|
2019-03-06 21:14:31 +00:00
|
|
|
return KSuccess;
|
2019-02-14 16:18:35 +00:00
|
|
|
}
|
2019-03-13 12:13:23 +00:00
|
|
|
|
2020-08-07 07:18:20 +00:00
|
|
|
KResult Socket::setsockopt(int level, int option, Userspace<const void*> user_value, socklen_t user_value_size)
|
2019-03-13 12:13:23 +00:00
|
|
|
{
|
|
|
|
ASSERT(level == SOL_SOCKET);
|
|
|
|
switch (option) {
|
|
|
|
case SO_SNDTIMEO:
|
2020-07-30 22:26:33 +00:00
|
|
|
if (user_value_size != sizeof(timeval))
|
2019-03-13 12:13:23 +00:00
|
|
|
return KResult(-EINVAL);
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(&m_send_timeout, static_ptr_cast<const timeval*>(user_value)))
|
|
|
|
return KResult(-EFAULT);
|
2019-03-13 12:13:23 +00:00
|
|
|
return KSuccess;
|
|
|
|
case SO_RCVTIMEO:
|
2020-07-30 22:26:33 +00:00
|
|
|
if (user_value_size != sizeof(timeval))
|
2019-03-13 12:13:23 +00:00
|
|
|
return KResult(-EINVAL);
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(&m_receive_timeout, static_ptr_cast<const timeval*>(user_value)))
|
|
|
|
return KResult(-EFAULT);
|
2019-03-13 12:13:23 +00:00
|
|
|
return KSuccess;
|
2020-04-04 20:46:45 +00:00
|
|
|
case SO_BINDTODEVICE: {
|
2020-07-30 22:26:33 +00:00
|
|
|
if (user_value_size != IFNAMSIZ)
|
2020-04-04 20:46:45 +00:00
|
|
|
return KResult(-EINVAL);
|
2020-08-07 07:18:20 +00:00
|
|
|
auto user_string = static_ptr_cast<const char*>(user_value);
|
2020-09-12 03:11:07 +00:00
|
|
|
auto ifname = copy_string_from_user(user_string, user_value_size);
|
2020-07-30 22:26:33 +00:00
|
|
|
if (ifname.is_null())
|
|
|
|
return KResult(-EFAULT);
|
2020-04-04 20:46:45 +00:00
|
|
|
auto device = NetworkAdapter::lookup_by_name(ifname);
|
|
|
|
if (!device)
|
|
|
|
return KResult(-ENODEV);
|
|
|
|
m_bound_interface = device;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
2020-04-05 16:10:19 +00:00
|
|
|
case SO_KEEPALIVE:
|
|
|
|
// FIXME: Obviously, this is not a real keepalive.
|
|
|
|
return KSuccess;
|
2020-09-16 16:32:45 +00:00
|
|
|
case SO_TIMESTAMP:
|
|
|
|
if (user_value_size != sizeof(int))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
if (!copy_from_user(&m_timestamp, static_ptr_cast<const int*>(user_value)))
|
|
|
|
return KResult(-EFAULT);
|
|
|
|
if (m_timestamp && (domain() != AF_INET || type() == SOCK_STREAM)) {
|
|
|
|
// FIXME: Support SO_TIMESTAMP for more protocols?
|
|
|
|
m_timestamp = 0;
|
|
|
|
return KResult(-ENOTSUP);
|
|
|
|
}
|
|
|
|
return KSuccess;
|
2019-03-13 12:13:23 +00:00
|
|
|
default:
|
2020-02-07 22:48:12 +00:00
|
|
|
dbg() << "setsockopt(" << option << ") at SOL_SOCKET not implemented.";
|
2019-03-13 12:13:23 +00:00
|
|
|
return KResult(-ENOPROTOOPT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-07 09:29:05 +00:00
|
|
|
KResult Socket::getsockopt(FileDescription&, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
|
2019-03-13 12:13:23 +00:00
|
|
|
{
|
2020-08-07 09:29:05 +00:00
|
|
|
socklen_t size;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_from_user(&size, value_size.unsafe_userspace_ptr()))
|
2020-08-07 09:29:05 +00:00
|
|
|
return KResult(-EFAULT);
|
|
|
|
|
2020-09-09 06:41:57 +00: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.
|
|
|
|
return KResult(-ENOPROTOOPT);
|
|
|
|
}
|
|
|
|
|
2019-03-13 12:13:23 +00:00
|
|
|
switch (option) {
|
|
|
|
case SO_SNDTIMEO:
|
2020-08-07 09:29:05 +00:00
|
|
|
if (size < sizeof(timeval))
|
2019-03-13 12:13:23 +00:00
|
|
|
return KResult(-EINVAL);
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(static_ptr_cast<timeval*>(value), &m_send_timeout))
|
|
|
|
return KResult(-EFAULT);
|
2020-08-07 09:29:05 +00:00
|
|
|
size = sizeof(timeval);
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(value_size, &size))
|
|
|
|
return KResult(-EFAULT);
|
2019-03-13 12:13:23 +00:00
|
|
|
return KSuccess;
|
|
|
|
case SO_RCVTIMEO:
|
2020-08-07 09:29:05 +00:00
|
|
|
if (size < sizeof(timeval))
|
2019-03-13 12:13:23 +00:00
|
|
|
return KResult(-EINVAL);
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(static_ptr_cast<timeval*>(value), &m_receive_timeout))
|
|
|
|
return KResult(-EFAULT);
|
2020-08-07 09:29:05 +00:00
|
|
|
size = sizeof(timeval);
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(value_size, &size))
|
|
|
|
return KResult(-EFAULT);
|
2019-03-13 12:13:23 +00:00
|
|
|
return KSuccess;
|
2020-08-07 09:29:05 +00:00
|
|
|
case SO_ERROR: {
|
|
|
|
if (size < sizeof(int))
|
2019-05-29 19:59:50 +00:00
|
|
|
return KResult(-EINVAL);
|
2021-01-09 17:51:44 +00:00
|
|
|
dbgln("getsockopt(SO_ERROR): FIXME!");
|
2020-08-07 09:29:05 +00:00
|
|
|
int errno = 0;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(static_ptr_cast<int*>(value), &errno))
|
|
|
|
return KResult(-EFAULT);
|
2020-08-07 09:29:05 +00:00
|
|
|
size = sizeof(int);
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(value_size, &size))
|
|
|
|
return KResult(-EFAULT);
|
2019-05-29 19:59:50 +00:00
|
|
|
return KSuccess;
|
2020-08-07 09:29:05 +00:00
|
|
|
}
|
2020-04-04 20:46:45 +00:00
|
|
|
case SO_BINDTODEVICE:
|
2020-08-07 09:29:05 +00:00
|
|
|
if (size < IFNAMSIZ)
|
2020-04-04 20:46:45 +00:00
|
|
|
return KResult(-EINVAL);
|
|
|
|
if (m_bound_interface) {
|
|
|
|
const auto& name = m_bound_interface->name();
|
|
|
|
auto length = name.length() + 1;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(static_ptr_cast<char*>(value), name.characters(), length))
|
|
|
|
return KResult(-EFAULT);
|
2020-08-07 09:29:05 +00:00
|
|
|
size = length;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(value_size, &size))
|
|
|
|
return KResult(-EFAULT);
|
2020-04-04 20:46:45 +00:00
|
|
|
return KSuccess;
|
|
|
|
} else {
|
2020-08-07 09:29:05 +00:00
|
|
|
size = 0;
|
2020-09-12 03:11:07 +00:00
|
|
|
if (!copy_to_user(value_size, &size))
|
|
|
|
return KResult(-EFAULT);
|
2020-08-07 09:29:05 +00:00
|
|
|
|
2020-04-04 20:46:45 +00:00
|
|
|
return KResult(-EFAULT);
|
|
|
|
}
|
2020-09-16 16:32:45 +00:00
|
|
|
case SO_TIMESTAMP:
|
|
|
|
if (size < sizeof(int))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
if (!copy_to_user(static_ptr_cast<int*>(value), &m_timestamp))
|
|
|
|
return KResult(-EFAULT);
|
|
|
|
size = sizeof(int);
|
|
|
|
if (!copy_to_user(value_size, &size))
|
|
|
|
return KResult(-EFAULT);
|
|
|
|
return KSuccess;
|
2019-03-13 12:13:23 +00:00
|
|
|
default:
|
2020-02-07 22:48:12 +00:00
|
|
|
dbg() << "getsockopt(" << option << ") at SOL_SOCKET not implemented.";
|
2019-03-13 12:13:23 +00:00
|
|
|
return KResult(-ENOPROTOOPT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
KResultOr<size_t> Socket::read(FileDescription& description, size_t, UserOrKernelBuffer& buffer, size_t size)
|
2019-08-05 08:03:19 +00:00
|
|
|
{
|
2020-02-07 23:52:33 +00:00
|
|
|
if (is_shut_down_for_reading())
|
|
|
|
return 0;
|
2020-09-16 16:25:06 +00:00
|
|
|
timeval tv;
|
|
|
|
return recvfrom(description, buffer, size, 0, {}, 0, tv);
|
2019-08-05 08:03:19 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 03:11:07 +00:00
|
|
|
KResultOr<size_t> Socket::write(FileDescription& description, size_t, const UserOrKernelBuffer& data, size_t size)
|
2019-08-05 08:03:19 +00:00
|
|
|
{
|
2020-02-07 23:52:33 +00:00
|
|
|
if (is_shut_down_for_writing())
|
|
|
|
return -EPIPE;
|
2020-08-18 06:49:35 +00:00
|
|
|
return sendto(description, data, size, 0, {}, 0);
|
2019-08-05 08:03:19 +00:00
|
|
|
}
|
2020-02-07 23:52:33 +00:00
|
|
|
|
|
|
|
KResult Socket::shutdown(int how)
|
|
|
|
{
|
2020-10-21 18:51:02 +00:00
|
|
|
LOCKER(lock());
|
2020-02-07 23:52:33 +00:00
|
|
|
if (type() == SOCK_STREAM && !is_connected())
|
|
|
|
return KResult(-ENOTCONN);
|
2020-02-08 14:52:32 +00:00
|
|
|
if (m_role == Role::Listener)
|
|
|
|
return KResult(-ENOTCONN);
|
|
|
|
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-07 23:52:33 +00:00
|
|
|
return KSuccess;
|
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2020-09-06 16:31:51 +00:00
|
|
|
KResult Socket::stat(::stat& st) const
|
|
|
|
{
|
|
|
|
memset(&st, 0, sizeof(st));
|
|
|
|
st.st_mode = S_IFSOCK;
|
|
|
|
return KSuccess;
|
|
|
|
}
|
|
|
|
|
2020-12-13 18:15:42 +00:00
|
|
|
void Socket::set_connected(bool connected)
|
|
|
|
{
|
|
|
|
LOCKER(lock());
|
|
|
|
if (m_connected == connected)
|
|
|
|
return;
|
|
|
|
m_connected = connected;
|
|
|
|
evaluate_block_conditions();
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|