Socket.cpp 8.0 KB

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