Socket.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/StringBuilder.h>
  7. #include <AK/StringView.h>
  8. #include <Kernel/Debug.h>
  9. #include <Kernel/FileSystem/FileDescription.h>
  10. #include <Kernel/Net/IPv4Socket.h>
  11. #include <Kernel/Net/LocalSocket.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. return LocalSocket::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. auto& process = *Process::current();
  34. m_origin = { process.pid().value(), process.uid(), process.gid() };
  35. }
  36. Socket::~Socket()
  37. {
  38. }
  39. void Socket::set_setup_state(SetupState new_setup_state)
  40. {
  41. dbgln_if(SOCKET_DEBUG, "Socket({}) setup state moving from {} to {}", this, to_string(m_setup_state), to_string(new_setup_state));
  42. m_setup_state = new_setup_state;
  43. evaluate_block_conditions();
  44. }
  45. RefPtr<Socket> Socket::accept()
  46. {
  47. Locker locker(m_lock);
  48. if (m_pending.is_empty())
  49. return nullptr;
  50. dbgln_if(SOCKET_DEBUG, "Socket({}) de-queueing connection", this);
  51. auto client = m_pending.take_first();
  52. VERIFY(!client->is_connected());
  53. auto& process = *Process::current();
  54. client->m_acceptor = { process.pid().value(), process.uid(), process.gid() };
  55. client->m_connected = true;
  56. client->m_role = Role::Accepted;
  57. if (!m_pending.is_empty())
  58. evaluate_block_conditions();
  59. return client;
  60. }
  61. KResult Socket::queue_connection_from(NonnullRefPtr<Socket> peer)
  62. {
  63. dbgln_if(SOCKET_DEBUG, "Socket({}) queueing connection", this);
  64. Locker locker(m_lock);
  65. if (m_pending.size() >= m_backlog)
  66. return ECONNREFUSED;
  67. if (!m_pending.try_append(peer))
  68. return ENOMEM;
  69. evaluate_block_conditions();
  70. return KSuccess;
  71. }
  72. KResult Socket::setsockopt(int level, int option, Userspace<const void*> user_value, socklen_t user_value_size)
  73. {
  74. if (level != SOL_SOCKET)
  75. return ENOPROTOOPT;
  76. VERIFY(level == SOL_SOCKET);
  77. switch (option) {
  78. case SO_SNDTIMEO:
  79. if (user_value_size != sizeof(timeval))
  80. return EINVAL;
  81. {
  82. auto timeout = copy_time_from_user(static_ptr_cast<const timeval*>(user_value));
  83. if (!timeout.has_value())
  84. return EFAULT;
  85. m_send_timeout = timeout.value();
  86. }
  87. return KSuccess;
  88. case SO_RCVTIMEO:
  89. if (user_value_size != sizeof(timeval))
  90. return EINVAL;
  91. {
  92. auto timeout = copy_time_from_user(static_ptr_cast<const timeval*>(user_value));
  93. if (!timeout.has_value())
  94. return EFAULT;
  95. m_receive_timeout = timeout.value();
  96. }
  97. return KSuccess;
  98. case SO_BINDTODEVICE: {
  99. if (user_value_size != IFNAMSIZ)
  100. return EINVAL;
  101. auto user_string = static_ptr_cast<const char*>(user_value);
  102. auto ifname = copy_string_from_user(user_string, user_value_size);
  103. if (ifname.is_null())
  104. return EFAULT;
  105. auto device = NetworkAdapter::lookup_by_name(ifname);
  106. if (!device)
  107. return ENODEV;
  108. m_bound_interface = device;
  109. return KSuccess;
  110. }
  111. case SO_KEEPALIVE:
  112. // FIXME: Obviously, this is not a real keepalive.
  113. return KSuccess;
  114. case SO_TIMESTAMP:
  115. if (user_value_size != sizeof(int))
  116. return EINVAL;
  117. {
  118. int timestamp;
  119. if (!copy_from_user(&timestamp, static_ptr_cast<const int*>(user_value)))
  120. return EFAULT;
  121. m_timestamp = timestamp;
  122. }
  123. if (m_timestamp && (domain() != AF_INET || type() == SOCK_STREAM)) {
  124. // FIXME: Support SO_TIMESTAMP for more protocols?
  125. m_timestamp = 0;
  126. return ENOTSUP;
  127. }
  128. return KSuccess;
  129. default:
  130. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  131. return ENOPROTOOPT;
  132. }
  133. }
  134. KResult Socket::getsockopt(FileDescription&, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
  135. {
  136. socklen_t size;
  137. if (!copy_from_user(&size, value_size.unsafe_userspace_ptr()))
  138. return EFAULT;
  139. // FIXME: Add TCP_NODELAY, IPPROTO_TCP and IPPROTO_IP (used in OpenSSH)
  140. if (level != SOL_SOCKET) {
  141. // Not sure if this is the correct error code, but it's only temporary until other levels are implemented.
  142. return ENOPROTOOPT;
  143. }
  144. switch (option) {
  145. case SO_SNDTIMEO:
  146. if (size < sizeof(timeval))
  147. return EINVAL;
  148. {
  149. timeval tv = m_send_timeout.to_timeval();
  150. if (!copy_to_user(static_ptr_cast<timeval*>(value), &tv))
  151. return EFAULT;
  152. }
  153. size = sizeof(timeval);
  154. if (!copy_to_user(value_size, &size))
  155. return EFAULT;
  156. return KSuccess;
  157. case SO_RCVTIMEO:
  158. if (size < sizeof(timeval))
  159. return EINVAL;
  160. {
  161. timeval tv = m_send_timeout.to_timeval();
  162. if (!copy_to_user(static_ptr_cast<timeval*>(value), &tv))
  163. return EFAULT;
  164. }
  165. size = sizeof(timeval);
  166. if (!copy_to_user(value_size, &size))
  167. return EFAULT;
  168. return KSuccess;
  169. case SO_ERROR: {
  170. if (size < sizeof(int))
  171. return EINVAL;
  172. dbgln("getsockopt(SO_ERROR): FIXME!");
  173. int errno = 0;
  174. if (!copy_to_user(static_ptr_cast<int*>(value), &errno))
  175. return EFAULT;
  176. size = sizeof(int);
  177. if (!copy_to_user(value_size, &size))
  178. return EFAULT;
  179. return KSuccess;
  180. }
  181. case SO_BINDTODEVICE:
  182. if (size < IFNAMSIZ)
  183. return EINVAL;
  184. if (m_bound_interface) {
  185. const auto& name = m_bound_interface->name();
  186. auto length = name.length() + 1;
  187. if (!copy_to_user(static_ptr_cast<char*>(value), name.characters(), length))
  188. return EFAULT;
  189. size = length;
  190. if (!copy_to_user(value_size, &size))
  191. return EFAULT;
  192. return KSuccess;
  193. } else {
  194. size = 0;
  195. if (!copy_to_user(value_size, &size))
  196. return EFAULT;
  197. return EFAULT;
  198. }
  199. case SO_TIMESTAMP:
  200. if (size < sizeof(int))
  201. return EINVAL;
  202. if (!copy_to_user(static_ptr_cast<int*>(value), &m_timestamp))
  203. return EFAULT;
  204. size = sizeof(int);
  205. if (!copy_to_user(value_size, &size))
  206. return EFAULT;
  207. return KSuccess;
  208. default:
  209. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  210. return ENOPROTOOPT;
  211. }
  212. }
  213. KResultOr<size_t> Socket::read(FileDescription& description, u64, UserOrKernelBuffer& buffer, size_t size)
  214. {
  215. if (is_shut_down_for_reading())
  216. return 0;
  217. Time t {};
  218. return recvfrom(description, buffer, size, 0, {}, 0, t);
  219. }
  220. KResultOr<size_t> Socket::write(FileDescription& description, u64, const UserOrKernelBuffer& data, size_t size)
  221. {
  222. if (is_shut_down_for_writing())
  223. return EPIPE;
  224. return sendto(description, data, size, 0, {}, 0);
  225. }
  226. KResult Socket::shutdown(int how)
  227. {
  228. Locker locker(lock());
  229. if (type() == SOCK_STREAM && !is_connected())
  230. return ENOTCONN;
  231. if (m_role == Role::Listener)
  232. return ENOTCONN;
  233. if (!m_shut_down_for_writing && (how & SHUT_WR))
  234. shut_down_for_writing();
  235. if (!m_shut_down_for_reading && (how & SHUT_RD))
  236. shut_down_for_reading();
  237. m_shut_down_for_reading |= (how & SHUT_RD) != 0;
  238. m_shut_down_for_writing |= (how & SHUT_WR) != 0;
  239. return KSuccess;
  240. }
  241. KResult Socket::stat(::stat& st) const
  242. {
  243. memset(&st, 0, sizeof(st));
  244. st.st_mode = S_IFSOCK;
  245. return KSuccess;
  246. }
  247. void Socket::set_connected(bool connected)
  248. {
  249. Locker locker(lock());
  250. if (m_connected == connected)
  251. return;
  252. m_connected = connected;
  253. evaluate_block_conditions();
  254. }
  255. }