Socket.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. 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. if (level != SOL_SOCKET)
  73. return ENOPROTOOPT;
  74. VERIFY(level == SOL_SOCKET);
  75. switch (option) {
  76. case SO_SNDTIMEO:
  77. if (user_value_size != sizeof(timeval))
  78. return EINVAL;
  79. m_send_timeout = TRY(copy_time_from_user(static_ptr_cast<timeval const*>(user_value)));
  80. return {};
  81. case SO_RCVTIMEO:
  82. if (user_value_size != sizeof(timeval))
  83. return EINVAL;
  84. m_receive_timeout = TRY(copy_time_from_user(static_ptr_cast<timeval const*>(user_value)));
  85. return {};
  86. case SO_BINDTODEVICE: {
  87. if (user_value_size != IFNAMSIZ)
  88. return EINVAL;
  89. auto user_string = static_ptr_cast<const char*>(user_value);
  90. auto ifname = TRY(try_copy_kstring_from_user(user_string, user_value_size));
  91. auto device = NetworkingManagement::the().lookup_by_name(ifname->view());
  92. if (!device)
  93. return ENODEV;
  94. m_bound_interface = move(device);
  95. return {};
  96. }
  97. case SO_KEEPALIVE:
  98. // FIXME: Obviously, this is not a real keepalive.
  99. return {};
  100. case SO_TIMESTAMP:
  101. if (user_value_size != sizeof(int))
  102. return EINVAL;
  103. {
  104. int timestamp;
  105. TRY(copy_from_user(&timestamp, static_ptr_cast<const int*>(user_value)));
  106. m_timestamp = timestamp;
  107. }
  108. if (m_timestamp && (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. default:
  115. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  116. return ENOPROTOOPT;
  117. }
  118. }
  119. ErrorOr<void> Socket::getsockopt(OpenFileDescription&, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
  120. {
  121. socklen_t size;
  122. TRY(copy_from_user(&size, value_size.unsafe_userspace_ptr()));
  123. // FIXME: Add TCP_NODELAY, IPPROTO_TCP and IPPROTO_IP (used in OpenSSH)
  124. if (level != SOL_SOCKET) {
  125. // Not sure if this is the correct error code, but it's only temporary until other levels are implemented.
  126. return ENOPROTOOPT;
  127. }
  128. switch (option) {
  129. case SO_SNDTIMEO:
  130. if (size < sizeof(timeval))
  131. return EINVAL;
  132. {
  133. timeval tv = m_send_timeout.to_timeval();
  134. TRY(copy_to_user(static_ptr_cast<timeval*>(value), &tv));
  135. }
  136. size = sizeof(timeval);
  137. return copy_to_user(value_size, &size);
  138. case SO_RCVTIMEO:
  139. if (size < sizeof(timeval))
  140. return EINVAL;
  141. {
  142. timeval tv = m_send_timeout.to_timeval();
  143. TRY(copy_to_user(static_ptr_cast<timeval*>(value), &tv));
  144. }
  145. size = sizeof(timeval);
  146. return copy_to_user(value_size, &size);
  147. case SO_ERROR: {
  148. if (size < sizeof(int))
  149. return EINVAL;
  150. int errno;
  151. if (so_error().is_error())
  152. errno = so_error().error().code();
  153. else
  154. errno = 0;
  155. TRY(copy_to_user(static_ptr_cast<int*>(value), &errno));
  156. size = sizeof(int);
  157. TRY(copy_to_user(value_size, &size));
  158. clear_so_error();
  159. return {};
  160. }
  161. case SO_BINDTODEVICE:
  162. if (size < IFNAMSIZ)
  163. return EINVAL;
  164. if (m_bound_interface) {
  165. auto name = m_bound_interface->name();
  166. auto length = name.length() + 1;
  167. auto characters = name.characters_without_null_termination();
  168. TRY(copy_to_user(static_ptr_cast<char*>(value), characters, length));
  169. size = length;
  170. return copy_to_user(value_size, &size);
  171. } else {
  172. size = 0;
  173. TRY(copy_to_user(value_size, &size));
  174. // FIXME: This return value looks suspicious.
  175. return EFAULT;
  176. }
  177. case SO_TIMESTAMP:
  178. if (size < sizeof(int))
  179. return EINVAL;
  180. TRY(copy_to_user(static_ptr_cast<int*>(value), &m_timestamp));
  181. size = sizeof(int);
  182. return copy_to_user(value_size, &size);
  183. case SO_TYPE:
  184. if (size < sizeof(int))
  185. return EINVAL;
  186. TRY(copy_to_user(static_ptr_cast<int*>(value), &m_type));
  187. size = sizeof(int);
  188. return copy_to_user(value_size, &size);
  189. default:
  190. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  191. return ENOPROTOOPT;
  192. }
  193. }
  194. ErrorOr<size_t> Socket::read(OpenFileDescription& description, u64, UserOrKernelBuffer& buffer, size_t size)
  195. {
  196. if (is_shut_down_for_reading())
  197. return 0;
  198. Time t {};
  199. return recvfrom(description, buffer, size, 0, {}, 0, t);
  200. }
  201. ErrorOr<size_t> Socket::write(OpenFileDescription& description, u64, const UserOrKernelBuffer& data, size_t size)
  202. {
  203. if (is_shut_down_for_writing())
  204. return set_so_error(EPIPE);
  205. return sendto(description, data, size, 0, {}, 0);
  206. }
  207. ErrorOr<void> Socket::shutdown(int how)
  208. {
  209. MutexLocker locker(mutex());
  210. if (type() == SOCK_STREAM && !is_connected())
  211. return set_so_error(ENOTCONN);
  212. if (m_role == Role::Listener)
  213. return set_so_error(ENOTCONN);
  214. if (!m_shut_down_for_writing && (how & SHUT_WR))
  215. shut_down_for_writing();
  216. if (!m_shut_down_for_reading && (how & SHUT_RD))
  217. shut_down_for_reading();
  218. m_shut_down_for_reading |= (how & SHUT_RD) != 0;
  219. m_shut_down_for_writing |= (how & SHUT_WR) != 0;
  220. return {};
  221. }
  222. ErrorOr<void> Socket::stat(::stat& st) const
  223. {
  224. memset(&st, 0, sizeof(st));
  225. st.st_mode = S_IFSOCK;
  226. return {};
  227. }
  228. void Socket::set_connected(bool connected)
  229. {
  230. MutexLocker locker(mutex());
  231. if (m_connected == connected)
  232. return;
  233. m_connected = connected;
  234. evaluate_block_conditions();
  235. }
  236. void Socket::set_origin(Process const& process)
  237. {
  238. m_origin = { process.pid().value(), process.uid().value(), process.gid().value() };
  239. }
  240. void Socket::set_acceptor(Process const& process)
  241. {
  242. m_acceptor = { process.pid().value(), process.uid().value(), process.gid().value() };
  243. }
  244. }