Socket.cpp 8.7 KB

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