Socket.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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/API/POSIX/errno.h>
  8. #include <Kernel/Debug.h>
  9. #include <Kernel/FileSystem/OpenFileDescription.h>
  10. #include <Kernel/Net/IPv4Socket.h>
  11. #include <Kernel/Net/LocalSocket.h>
  12. #include <Kernel/Net/NetworkingManagement.h>
  13. #include <Kernel/Net/Socket.h>
  14. #include <Kernel/Process.h>
  15. #include <Kernel/UnixTypes.h>
  16. namespace Kernel {
  17. ErrorOr<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() = default;
  36. void Socket::set_setup_state(SetupState new_setup_state)
  37. {
  38. dbgln_if(SOCKET_DEBUG, "Socket({}) setup state moving from {} to {}", this, to_string(m_setup_state), to_string(new_setup_state));
  39. m_setup_state = new_setup_state;
  40. evaluate_block_conditions();
  41. }
  42. RefPtr<Socket> Socket::accept()
  43. {
  44. MutexLocker locker(mutex());
  45. if (m_pending.is_empty())
  46. return nullptr;
  47. dbgln_if(SOCKET_DEBUG, "Socket({}) de-queueing connection", this);
  48. auto client = m_pending.take_first();
  49. VERIFY(!client->is_connected());
  50. auto& process = Process::current();
  51. client->set_acceptor(process);
  52. client->m_connected = true;
  53. client->set_role(Role::Accepted);
  54. if (!m_pending.is_empty())
  55. evaluate_block_conditions();
  56. return client;
  57. }
  58. ErrorOr<void> Socket::queue_connection_from(NonnullRefPtr<Socket> peer)
  59. {
  60. dbgln_if(SOCKET_DEBUG, "Socket({}) queueing connection", this);
  61. MutexLocker locker(mutex());
  62. if (m_pending.size() >= m_backlog)
  63. return set_so_error(ECONNREFUSED);
  64. SOCKET_TRY(m_pending.try_append(peer));
  65. evaluate_block_conditions();
  66. return {};
  67. }
  68. ErrorOr<void> Socket::setsockopt(int level, int option, Userspace<const void*> user_value, socklen_t user_value_size)
  69. {
  70. MutexLocker locker(mutex());
  71. if (level != SOL_SOCKET)
  72. return ENOPROTOOPT;
  73. VERIFY(level == SOL_SOCKET);
  74. switch (option) {
  75. case SO_SNDTIMEO:
  76. if (user_value_size != sizeof(timeval))
  77. return EINVAL;
  78. m_send_timeout = TRY(copy_time_from_user(static_ptr_cast<timeval const*>(user_value)));
  79. return {};
  80. case SO_RCVTIMEO:
  81. if (user_value_size != sizeof(timeval))
  82. return EINVAL;
  83. m_receive_timeout = TRY(copy_time_from_user(static_ptr_cast<timeval const*>(user_value)));
  84. return {};
  85. case SO_BINDTODEVICE: {
  86. if (user_value_size != IFNAMSIZ)
  87. return EINVAL;
  88. auto user_string = static_ptr_cast<const char*>(user_value);
  89. auto ifname = TRY(try_copy_kstring_from_user(user_string, user_value_size));
  90. auto device = NetworkingManagement::the().lookup_by_name(ifname->view());
  91. if (!device)
  92. return ENODEV;
  93. m_bound_interface = move(device);
  94. return {};
  95. }
  96. case SO_DEBUG:
  97. // NOTE: This is supposed to toggle collection of debugging information on/off, we don't have any right now, so this is a no-op.
  98. return {};
  99. case SO_KEEPALIVE:
  100. // FIXME: Obviously, this is not a real keepalive.
  101. return {};
  102. case SO_TIMESTAMP:
  103. if (user_value_size != sizeof(int))
  104. return EINVAL;
  105. m_timestamp = TRY(copy_typed_from_user(static_ptr_cast<int const*>(user_value)));
  106. if (m_timestamp != 0 && (domain() != AF_INET || type() == SOCK_STREAM)) {
  107. // FIXME: Support SO_TIMESTAMP for more protocols?
  108. m_timestamp = 0;
  109. return ENOTSUP;
  110. }
  111. return {};
  112. case SO_DONTROUTE: {
  113. if (user_value_size != sizeof(int))
  114. return EINVAL;
  115. m_routing_disabled = TRY(copy_typed_from_user(static_ptr_cast<int const*>(user_value))) != 0;
  116. return {};
  117. }
  118. default:
  119. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  120. return ENOPROTOOPT;
  121. }
  122. }
  123. ErrorOr<void> Socket::getsockopt(OpenFileDescription&, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
  124. {
  125. MutexLocker locker(mutex());
  126. socklen_t size;
  127. TRY(copy_from_user(&size, value_size.unsafe_userspace_ptr()));
  128. // FIXME: Add TCP_NODELAY, IPPROTO_TCP and IPPROTO_IP (used in OpenSSH)
  129. if (level != SOL_SOCKET) {
  130. // Not sure if this is the correct error code, but it's only temporary until other levels are implemented.
  131. return ENOPROTOOPT;
  132. }
  133. switch (option) {
  134. case SO_SNDTIMEO:
  135. if (size < sizeof(timeval))
  136. return EINVAL;
  137. {
  138. timeval tv = m_send_timeout.to_timeval();
  139. TRY(copy_to_user(static_ptr_cast<timeval*>(value), &tv));
  140. }
  141. size = sizeof(timeval);
  142. return copy_to_user(value_size, &size);
  143. case SO_RCVTIMEO:
  144. if (size < sizeof(timeval))
  145. return EINVAL;
  146. {
  147. timeval tv = m_receive_timeout.to_timeval();
  148. TRY(copy_to_user(static_ptr_cast<timeval*>(value), &tv));
  149. }
  150. size = sizeof(timeval);
  151. return copy_to_user(value_size, &size);
  152. case SO_ERROR: {
  153. if (size < sizeof(int))
  154. return EINVAL;
  155. int errno;
  156. if (so_error().is_error())
  157. errno = so_error().error().code();
  158. else
  159. errno = 0;
  160. TRY(copy_to_user(static_ptr_cast<int*>(value), &errno));
  161. size = sizeof(int);
  162. TRY(copy_to_user(value_size, &size));
  163. clear_so_error();
  164. return {};
  165. }
  166. case SO_BINDTODEVICE:
  167. if (size < IFNAMSIZ)
  168. return EINVAL;
  169. if (m_bound_interface) {
  170. auto name = m_bound_interface->name();
  171. auto length = name.length() + 1;
  172. auto characters = name.characters_without_null_termination();
  173. TRY(copy_to_user(static_ptr_cast<char*>(value), characters, length));
  174. size = length;
  175. return copy_to_user(value_size, &size);
  176. } else {
  177. size = 0;
  178. TRY(copy_to_user(value_size, &size));
  179. // FIXME: This return value looks suspicious.
  180. return EFAULT;
  181. }
  182. case SO_TIMESTAMP:
  183. if (size < sizeof(int))
  184. return EINVAL;
  185. TRY(copy_to_user(static_ptr_cast<int*>(value), &m_timestamp));
  186. size = sizeof(int);
  187. return copy_to_user(value_size, &size);
  188. case SO_TYPE:
  189. if (size < sizeof(int))
  190. return EINVAL;
  191. TRY(copy_to_user(static_ptr_cast<int*>(value), &m_type));
  192. size = sizeof(int);
  193. return copy_to_user(value_size, &size);
  194. case SO_DEBUG:
  195. // NOTE: This is supposed to toggle collection of debugging information on/off, we don't have any right now, so we just claim it's always off.
  196. if (size < sizeof(int))
  197. return EINVAL;
  198. TRY(memset_user(value.unsafe_userspace_ptr(), 0, sizeof(int)));
  199. size = sizeof(int);
  200. return copy_to_user(value_size, &size);
  201. case SO_ACCEPTCONN: {
  202. int accepting_connections = (m_role == Role::Listener) ? 1 : 0;
  203. if (size < sizeof(accepting_connections))
  204. return EINVAL;
  205. TRY(copy_to_user(static_ptr_cast<int*>(value), &accepting_connections));
  206. size = sizeof(accepting_connections);
  207. return copy_to_user(value_size, &size);
  208. }
  209. case SO_DONTROUTE: {
  210. int routing_disabled = m_routing_disabled ? 1 : 0;
  211. if (size < sizeof(routing_disabled))
  212. return EINVAL;
  213. TRY(copy_to_user(static_ptr_cast<int*>(value), &routing_disabled));
  214. size = sizeof(routing_disabled);
  215. return copy_to_user(value_size, &size);
  216. }
  217. default:
  218. dbgln("getsockopt({}) at SOL_SOCKET not implemented.", option);
  219. return ENOPROTOOPT;
  220. }
  221. }
  222. ErrorOr<size_t> Socket::read(OpenFileDescription& description, u64, UserOrKernelBuffer& buffer, size_t size)
  223. {
  224. if (is_shut_down_for_reading())
  225. return 0;
  226. Time t {};
  227. return recvfrom(description, buffer, size, 0, {}, 0, t);
  228. }
  229. ErrorOr<size_t> Socket::write(OpenFileDescription& description, u64, const UserOrKernelBuffer& data, size_t size)
  230. {
  231. if (is_shut_down_for_writing())
  232. return set_so_error(EPIPE);
  233. return sendto(description, data, size, 0, {}, 0);
  234. }
  235. ErrorOr<void> Socket::shutdown(int how)
  236. {
  237. MutexLocker locker(mutex());
  238. if (type() == SOCK_STREAM && !is_connected())
  239. return set_so_error(ENOTCONN);
  240. if (m_role == Role::Listener)
  241. return set_so_error(ENOTCONN);
  242. if (!m_shut_down_for_writing && (how & SHUT_WR))
  243. shut_down_for_writing();
  244. if (!m_shut_down_for_reading && (how & SHUT_RD))
  245. shut_down_for_reading();
  246. m_shut_down_for_reading |= (how & SHUT_RD) != 0;
  247. m_shut_down_for_writing |= (how & SHUT_WR) != 0;
  248. return {};
  249. }
  250. ErrorOr<struct stat> Socket::stat() const
  251. {
  252. struct stat st = {};
  253. st.st_mode = S_IFSOCK;
  254. return st;
  255. }
  256. void Socket::set_connected(bool connected)
  257. {
  258. MutexLocker locker(mutex());
  259. if (m_connected == connected)
  260. return;
  261. m_connected = connected;
  262. evaluate_block_conditions();
  263. }
  264. void Socket::set_origin(Process const& process)
  265. {
  266. m_origin = { process.pid().value(), process.uid().value(), process.gid().value() };
  267. }
  268. void Socket::set_acceptor(Process const& process)
  269. {
  270. m_acceptor = { process.pid().value(), process.uid().value(), process.gid().value() };
  271. }
  272. }