Socket.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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<NonnullLockRefPtr<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. LockRefPtr<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(NonnullLockRefPtr<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(move(peer)));
  65. evaluate_block_conditions();
  66. return {};
  67. }
  68. ErrorOr<void> Socket::setsockopt(int level, int option, Userspace<void const*> 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<char const*>(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. case SO_REUSEADDR:
  119. dbgln("FIXME: SO_REUSEADDR requested, but not implemented.");
  120. return {};
  121. default:
  122. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  123. return ENOPROTOOPT;
  124. }
  125. }
  126. ErrorOr<void> Socket::getsockopt(OpenFileDescription&, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
  127. {
  128. MutexLocker locker(mutex());
  129. socklen_t size;
  130. TRY(copy_from_user(&size, value_size.unsafe_userspace_ptr()));
  131. // FIXME: Add TCP_NODELAY, IPPROTO_TCP and IPPROTO_IP (used in OpenSSH)
  132. if (level != SOL_SOCKET) {
  133. // Not sure if this is the correct error code, but it's only temporary until other levels are implemented.
  134. return ENOPROTOOPT;
  135. }
  136. switch (option) {
  137. case SO_SNDTIMEO:
  138. if (size < sizeof(timeval))
  139. return EINVAL;
  140. {
  141. timeval tv = m_send_timeout.to_timeval();
  142. TRY(copy_to_user(static_ptr_cast<timeval*>(value), &tv));
  143. }
  144. size = sizeof(timeval);
  145. return copy_to_user(value_size, &size);
  146. case SO_RCVTIMEO:
  147. if (size < sizeof(timeval))
  148. return EINVAL;
  149. {
  150. timeval tv = m_receive_timeout.to_timeval();
  151. TRY(copy_to_user(static_ptr_cast<timeval*>(value), &tv));
  152. }
  153. size = sizeof(timeval);
  154. return copy_to_user(value_size, &size);
  155. case SO_ERROR: {
  156. if (size < sizeof(int))
  157. return EINVAL;
  158. int errno;
  159. if (so_error().is_error())
  160. errno = so_error().error().code();
  161. else
  162. errno = 0;
  163. TRY(copy_to_user(static_ptr_cast<int*>(value), &errno));
  164. size = sizeof(int);
  165. TRY(copy_to_user(value_size, &size));
  166. clear_so_error();
  167. return {};
  168. }
  169. case SO_BINDTODEVICE:
  170. if (size < IFNAMSIZ)
  171. return EINVAL;
  172. if (m_bound_interface) {
  173. auto name = m_bound_interface->name();
  174. auto length = name.length() + 1;
  175. auto characters = name.characters_without_null_termination();
  176. TRY(copy_to_user(static_ptr_cast<char*>(value), characters, length));
  177. size = length;
  178. return copy_to_user(value_size, &size);
  179. } else {
  180. size = 0;
  181. TRY(copy_to_user(value_size, &size));
  182. // FIXME: This return value looks suspicious.
  183. return EFAULT;
  184. }
  185. case SO_TIMESTAMP:
  186. if (size < sizeof(int))
  187. return EINVAL;
  188. TRY(copy_to_user(static_ptr_cast<int*>(value), &m_timestamp));
  189. size = sizeof(int);
  190. return copy_to_user(value_size, &size);
  191. case SO_TYPE:
  192. if (size < sizeof(int))
  193. return EINVAL;
  194. TRY(copy_to_user(static_ptr_cast<int*>(value), &m_type));
  195. size = sizeof(int);
  196. return copy_to_user(value_size, &size);
  197. case SO_DEBUG:
  198. // 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.
  199. if (size < sizeof(int))
  200. return EINVAL;
  201. TRY(memset_user(value.unsafe_userspace_ptr(), 0, sizeof(int)));
  202. size = sizeof(int);
  203. return copy_to_user(value_size, &size);
  204. case SO_ACCEPTCONN: {
  205. int accepting_connections = (m_role == Role::Listener) ? 1 : 0;
  206. if (size < sizeof(accepting_connections))
  207. return EINVAL;
  208. TRY(copy_to_user(static_ptr_cast<int*>(value), &accepting_connections));
  209. size = sizeof(accepting_connections);
  210. return copy_to_user(value_size, &size);
  211. }
  212. case SO_DONTROUTE: {
  213. int routing_disabled = m_routing_disabled ? 1 : 0;
  214. if (size < sizeof(routing_disabled))
  215. return EINVAL;
  216. TRY(copy_to_user(static_ptr_cast<int*>(value), &routing_disabled));
  217. size = sizeof(routing_disabled);
  218. return copy_to_user(value_size, &size);
  219. }
  220. default:
  221. dbgln("getsockopt({}) at SOL_SOCKET not implemented.", option);
  222. return ENOPROTOOPT;
  223. }
  224. }
  225. ErrorOr<size_t> Socket::read(OpenFileDescription& description, u64, UserOrKernelBuffer& buffer, size_t size)
  226. {
  227. if (is_shut_down_for_reading())
  228. return 0;
  229. Time t {};
  230. return recvfrom(description, buffer, size, 0, {}, 0, t);
  231. }
  232. ErrorOr<size_t> Socket::write(OpenFileDescription& description, u64, UserOrKernelBuffer const& data, size_t size)
  233. {
  234. if (is_shut_down_for_writing())
  235. return set_so_error(EPIPE);
  236. return sendto(description, data, size, 0, {}, 0);
  237. }
  238. ErrorOr<void> Socket::shutdown(int how)
  239. {
  240. MutexLocker locker(mutex());
  241. if (type() == SOCK_STREAM && !is_connected())
  242. return set_so_error(ENOTCONN);
  243. if (m_role == Role::Listener)
  244. return set_so_error(ENOTCONN);
  245. if (!m_shut_down_for_writing && (how == SHUT_WR || how == SHUT_RDWR)) {
  246. shut_down_for_writing();
  247. m_shut_down_for_writing = true;
  248. }
  249. if (!m_shut_down_for_reading && (how == SHUT_RD || how == SHUT_RDWR)) {
  250. shut_down_for_reading();
  251. m_shut_down_for_reading = true;
  252. }
  253. return {};
  254. }
  255. ErrorOr<struct stat> Socket::stat() const
  256. {
  257. struct stat st = {};
  258. st.st_mode = S_IFSOCK;
  259. return st;
  260. }
  261. void Socket::set_connected(bool connected)
  262. {
  263. MutexLocker locker(mutex());
  264. if (m_connected == connected)
  265. return;
  266. m_connected = connected;
  267. evaluate_block_conditions();
  268. }
  269. void Socket::set_origin(Process const& process)
  270. {
  271. auto credentials = process.credentials();
  272. m_origin = { process.pid().value(), credentials->uid().value(), credentials->gid().value() };
  273. }
  274. void Socket::set_acceptor(Process const& process)
  275. {
  276. auto credentials = process.credentials();
  277. m_acceptor = { process.pid().value(), credentials->uid().value(), credentials->gid().value() };
  278. }
  279. }