Socket.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringView.h>
  7. #include <Kernel/Debug.h>
  8. #include <Kernel/FileSystem/OpenFileDescription.h>
  9. #include <Kernel/Net/IPv4Socket.h>
  10. #include <Kernel/Net/LocalSocket.h>
  11. #include <Kernel/Net/NetworkingManagement.h>
  12. #include <Kernel/Net/Socket.h>
  13. #include <Kernel/Process.h>
  14. #include <Kernel/UnixTypes.h>
  15. #include <LibC/errno_numbers.h>
  16. namespace Kernel {
  17. KResultOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol)
  18. {
  19. switch (domain) {
  20. case AF_LOCAL:
  21. return TRY(LocalSocket::try_create(type & SOCK_TYPE_MASK));
  22. case AF_INET:
  23. return IPv4Socket::create(type & SOCK_TYPE_MASK, protocol);
  24. default:
  25. return EAFNOSUPPORT;
  26. }
  27. }
  28. Socket::Socket(int domain, int type, int protocol)
  29. : m_domain(domain)
  30. , m_type(type)
  31. , m_protocol(protocol)
  32. {
  33. set_origin(Process::current());
  34. }
  35. Socket::~Socket()
  36. {
  37. }
  38. void Socket::set_setup_state(SetupState new_setup_state)
  39. {
  40. dbgln_if(SOCKET_DEBUG, "Socket({}) setup state moving from {} to {}", this, to_string(m_setup_state), to_string(new_setup_state));
  41. m_setup_state = new_setup_state;
  42. evaluate_block_conditions();
  43. }
  44. RefPtr<Socket> Socket::accept()
  45. {
  46. MutexLocker locker(mutex());
  47. if (m_pending.is_empty())
  48. return nullptr;
  49. dbgln_if(SOCKET_DEBUG, "Socket({}) de-queueing connection", this);
  50. auto client = m_pending.take_first();
  51. VERIFY(!client->is_connected());
  52. auto& process = Process::current();
  53. client->set_acceptor(process);
  54. client->m_connected = true;
  55. client->set_role(Role::Accepted);
  56. if (!m_pending.is_empty())
  57. evaluate_block_conditions();
  58. return client;
  59. }
  60. KResult Socket::queue_connection_from(NonnullRefPtr<Socket> peer)
  61. {
  62. dbgln_if(SOCKET_DEBUG, "Socket({}) queueing connection", this);
  63. MutexLocker locker(mutex());
  64. if (m_pending.size() >= m_backlog)
  65. return set_so_error(ECONNREFUSED);
  66. if (!m_pending.try_append(peer))
  67. return set_so_error(ENOMEM);
  68. evaluate_block_conditions();
  69. return KSuccess;
  70. }
  71. KResult Socket::setsockopt(int level, int option, Userspace<const void*> user_value, socklen_t user_value_size)
  72. {
  73. if (level != SOL_SOCKET)
  74. return ENOPROTOOPT;
  75. VERIFY(level == SOL_SOCKET);
  76. switch (option) {
  77. case SO_SNDTIMEO:
  78. if (user_value_size != sizeof(timeval))
  79. return EINVAL;
  80. m_send_timeout = TRY(copy_time_from_user(static_ptr_cast<timeval const*>(user_value)));
  81. return KSuccess;
  82. case SO_RCVTIMEO:
  83. if (user_value_size != sizeof(timeval))
  84. return EINVAL;
  85. m_receive_timeout = TRY(copy_time_from_user(static_ptr_cast<timeval const*>(user_value)));
  86. return KSuccess;
  87. case SO_BINDTODEVICE: {
  88. if (user_value_size != IFNAMSIZ)
  89. return EINVAL;
  90. auto user_string = static_ptr_cast<const char*>(user_value);
  91. auto ifname = TRY(try_copy_kstring_from_user(user_string, user_value_size));
  92. auto device = NetworkingManagement::the().lookup_by_name(ifname->view());
  93. if (!device)
  94. return ENODEV;
  95. m_bound_interface = move(device);
  96. return KSuccess;
  97. }
  98. case SO_KEEPALIVE:
  99. // FIXME: Obviously, this is not a real keepalive.
  100. return KSuccess;
  101. case SO_TIMESTAMP:
  102. if (user_value_size != sizeof(int))
  103. return EINVAL;
  104. {
  105. int timestamp;
  106. TRY(copy_from_user(&timestamp, static_ptr_cast<const int*>(user_value)));
  107. m_timestamp = timestamp;
  108. }
  109. if (m_timestamp && (domain() != AF_INET || type() == SOCK_STREAM)) {
  110. // FIXME: Support SO_TIMESTAMP for more protocols?
  111. m_timestamp = 0;
  112. return ENOTSUP;
  113. }
  114. return KSuccess;
  115. default:
  116. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  117. return ENOPROTOOPT;
  118. }
  119. }
  120. KResult Socket::getsockopt(OpenFileDescription&, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
  121. {
  122. socklen_t size;
  123. TRY(copy_from_user(&size, value_size.unsafe_userspace_ptr()));
  124. // FIXME: Add TCP_NODELAY, IPPROTO_TCP and IPPROTO_IP (used in OpenSSH)
  125. if (level != SOL_SOCKET) {
  126. // Not sure if this is the correct error code, but it's only temporary until other levels are implemented.
  127. return ENOPROTOOPT;
  128. }
  129. switch (option) {
  130. case SO_SNDTIMEO:
  131. if (size < sizeof(timeval))
  132. return EINVAL;
  133. {
  134. timeval tv = m_send_timeout.to_timeval();
  135. TRY(copy_to_user(static_ptr_cast<timeval*>(value), &tv));
  136. }
  137. size = sizeof(timeval);
  138. return copy_to_user(value_size, &size);
  139. case SO_RCVTIMEO:
  140. if (size < sizeof(timeval))
  141. return EINVAL;
  142. {
  143. timeval tv = m_send_timeout.to_timeval();
  144. TRY(copy_to_user(static_ptr_cast<timeval*>(value), &tv));
  145. }
  146. size = sizeof(timeval);
  147. return copy_to_user(value_size, &size);
  148. case SO_ERROR: {
  149. if (size < sizeof(int))
  150. return EINVAL;
  151. int errno = so_error().error();
  152. TRY(copy_to_user(static_ptr_cast<int*>(value), &errno));
  153. size = sizeof(int);
  154. TRY(copy_to_user(value_size, &size));
  155. return set_so_error(KSuccess);
  156. }
  157. case SO_BINDTODEVICE:
  158. if (size < IFNAMSIZ)
  159. return EINVAL;
  160. if (m_bound_interface) {
  161. const auto& name = m_bound_interface->name();
  162. auto length = name.length() + 1;
  163. TRY(copy_to_user(static_ptr_cast<char*>(value), name.characters(), length));
  164. size = length;
  165. return copy_to_user(value_size, &size);
  166. } else {
  167. size = 0;
  168. TRY(copy_to_user(value_size, &size));
  169. // FIXME: This return value looks suspicious.
  170. return EFAULT;
  171. }
  172. case SO_TIMESTAMP:
  173. if (size < sizeof(int))
  174. return EINVAL;
  175. TRY(copy_to_user(static_ptr_cast<int*>(value), &m_timestamp));
  176. size = sizeof(int);
  177. return copy_to_user(value_size, &size);
  178. default:
  179. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  180. return ENOPROTOOPT;
  181. }
  182. }
  183. KResultOr<size_t> Socket::read(OpenFileDescription& description, u64, UserOrKernelBuffer& buffer, size_t size)
  184. {
  185. if (is_shut_down_for_reading())
  186. return 0;
  187. Time t {};
  188. return recvfrom(description, buffer, size, 0, {}, 0, t);
  189. }
  190. KResultOr<size_t> Socket::write(OpenFileDescription& description, u64, const UserOrKernelBuffer& data, size_t size)
  191. {
  192. if (is_shut_down_for_writing())
  193. return set_so_error(EPIPE);
  194. return sendto(description, data, size, 0, {}, 0);
  195. }
  196. KResult Socket::shutdown(int how)
  197. {
  198. MutexLocker locker(mutex());
  199. if (type() == SOCK_STREAM && !is_connected())
  200. return set_so_error(ENOTCONN);
  201. if (m_role == Role::Listener)
  202. return set_so_error(ENOTCONN);
  203. if (!m_shut_down_for_writing && (how & SHUT_WR))
  204. shut_down_for_writing();
  205. if (!m_shut_down_for_reading && (how & SHUT_RD))
  206. shut_down_for_reading();
  207. m_shut_down_for_reading |= (how & SHUT_RD) != 0;
  208. m_shut_down_for_writing |= (how & SHUT_WR) != 0;
  209. return KSuccess;
  210. }
  211. KResult Socket::stat(::stat& st) const
  212. {
  213. memset(&st, 0, sizeof(st));
  214. st.st_mode = S_IFSOCK;
  215. return KSuccess;
  216. }
  217. void Socket::set_connected(bool connected)
  218. {
  219. MutexLocker locker(mutex());
  220. if (m_connected == connected)
  221. return;
  222. m_connected = connected;
  223. evaluate_block_conditions();
  224. }
  225. void Socket::set_origin(Process const& process)
  226. {
  227. m_origin = { process.pid().value(), process.uid().value(), process.gid().value() };
  228. }
  229. void Socket::set_acceptor(Process const& process)
  230. {
  231. m_acceptor = { process.pid().value(), process.uid().value(), process.gid().value() };
  232. }
  233. }