Socket.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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()
  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. ErrorOr<void> 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. SOCKET_TRY(m_pending.try_append(peer));
  67. evaluate_block_conditions();
  68. return {};
  69. }
  70. ErrorOr<void> Socket::setsockopt(int level, int option, Userspace<const void*> user_value, socklen_t user_value_size)
  71. {
  72. MutexLocker locker(mutex());
  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 {};
  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 {};
  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 {};
  97. }
  98. case SO_DEBUG:
  99. // 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.
  100. return {};
  101. case SO_KEEPALIVE:
  102. // FIXME: Obviously, this is not a real keepalive.
  103. return {};
  104. case SO_TIMESTAMP:
  105. if (user_value_size != sizeof(int))
  106. return EINVAL;
  107. m_timestamp = TRY(copy_typed_from_user(static_ptr_cast<int const*>(user_value)));
  108. if (m_timestamp != 0 && (domain() != AF_INET || type() == SOCK_STREAM)) {
  109. // FIXME: Support SO_TIMESTAMP for more protocols?
  110. m_timestamp = 0;
  111. return ENOTSUP;
  112. }
  113. return {};
  114. case SO_DONTROUTE: {
  115. if (user_value_size != sizeof(int))
  116. return EINVAL;
  117. m_routing_disabled = TRY(copy_typed_from_user(static_ptr_cast<int const*>(user_value))) != 0;
  118. return {};
  119. }
  120. default:
  121. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  122. return ENOPROTOOPT;
  123. }
  124. }
  125. ErrorOr<void> Socket::getsockopt(OpenFileDescription&, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
  126. {
  127. MutexLocker locker(mutex());
  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_receive_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;
  158. if (so_error().is_error())
  159. errno = so_error().error().code();
  160. else
  161. errno = 0;
  162. TRY(copy_to_user(static_ptr_cast<int*>(value), &errno));
  163. size = sizeof(int);
  164. TRY(copy_to_user(value_size, &size));
  165. clear_so_error();
  166. return {};
  167. }
  168. case SO_BINDTODEVICE:
  169. if (size < IFNAMSIZ)
  170. return EINVAL;
  171. if (m_bound_interface) {
  172. auto name = m_bound_interface->name();
  173. auto length = name.length() + 1;
  174. auto characters = name.characters_without_null_termination();
  175. TRY(copy_to_user(static_ptr_cast<char*>(value), characters, length));
  176. size = length;
  177. return copy_to_user(value_size, &size);
  178. } else {
  179. size = 0;
  180. TRY(copy_to_user(value_size, &size));
  181. // FIXME: This return value looks suspicious.
  182. return EFAULT;
  183. }
  184. case SO_TIMESTAMP:
  185. if (size < sizeof(int))
  186. return EINVAL;
  187. TRY(copy_to_user(static_ptr_cast<int*>(value), &m_timestamp));
  188. size = sizeof(int);
  189. return copy_to_user(value_size, &size);
  190. case SO_TYPE:
  191. if (size < sizeof(int))
  192. return EINVAL;
  193. TRY(copy_to_user(static_ptr_cast<int*>(value), &m_type));
  194. size = sizeof(int);
  195. return copy_to_user(value_size, &size);
  196. case SO_DEBUG:
  197. // 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.
  198. if (size < sizeof(int))
  199. return EINVAL;
  200. TRY(memset_user(value.unsafe_userspace_ptr(), 0, sizeof(int)));
  201. size = sizeof(int);
  202. return copy_to_user(value_size, &size);
  203. case SO_ACCEPTCONN: {
  204. int accepting_connections = (m_role == Role::Listener) ? 1 : 0;
  205. if (size < sizeof(accepting_connections))
  206. return EINVAL;
  207. TRY(copy_to_user(static_ptr_cast<int*>(value), &accepting_connections));
  208. size = sizeof(accepting_connections);
  209. return copy_to_user(value_size, &size);
  210. }
  211. case SO_DONTROUTE: {
  212. int routing_disabled = m_routing_disabled ? 1 : 0;
  213. if (size < sizeof(routing_disabled))
  214. return EINVAL;
  215. TRY(copy_to_user(static_ptr_cast<int*>(value), &routing_disabled));
  216. size = sizeof(routing_disabled);
  217. return copy_to_user(value_size, &size);
  218. }
  219. default:
  220. dbgln("getsockopt({}) at SOL_SOCKET not implemented.", option);
  221. return ENOPROTOOPT;
  222. }
  223. }
  224. ErrorOr<size_t> Socket::read(OpenFileDescription& description, u64, UserOrKernelBuffer& buffer, size_t size)
  225. {
  226. if (is_shut_down_for_reading())
  227. return 0;
  228. Time t {};
  229. return recvfrom(description, buffer, size, 0, {}, 0, t);
  230. }
  231. ErrorOr<size_t> Socket::write(OpenFileDescription& description, u64, const UserOrKernelBuffer& data, size_t size)
  232. {
  233. if (is_shut_down_for_writing())
  234. return set_so_error(EPIPE);
  235. return sendto(description, data, size, 0, {}, 0);
  236. }
  237. ErrorOr<void> Socket::shutdown(int how)
  238. {
  239. MutexLocker locker(mutex());
  240. if (type() == SOCK_STREAM && !is_connected())
  241. return set_so_error(ENOTCONN);
  242. if (m_role == Role::Listener)
  243. return set_so_error(ENOTCONN);
  244. if (!m_shut_down_for_writing && (how & SHUT_WR))
  245. shut_down_for_writing();
  246. if (!m_shut_down_for_reading && (how & SHUT_RD))
  247. shut_down_for_reading();
  248. m_shut_down_for_reading |= (how & SHUT_RD) != 0;
  249. m_shut_down_for_writing |= (how & SHUT_WR) != 0;
  250. return {};
  251. }
  252. ErrorOr<struct stat> Socket::stat() const
  253. {
  254. struct stat st = {};
  255. st.st_mode = S_IFSOCK;
  256. return st;
  257. }
  258. void Socket::set_connected(bool connected)
  259. {
  260. MutexLocker locker(mutex());
  261. if (m_connected == connected)
  262. return;
  263. m_connected = connected;
  264. evaluate_block_conditions();
  265. }
  266. void Socket::set_origin(Process const& process)
  267. {
  268. m_origin = { process.pid().value(), process.uid().value(), process.gid().value() };
  269. }
  270. void Socket::set_acceptor(Process const& process)
  271. {
  272. m_acceptor = { process.pid().value(), process.uid().value(), process.gid().value() };
  273. }
  274. }