Socket.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StringBuilder.h>
  27. #include <AK/StringView.h>
  28. #include <Kernel/FileSystem/FileDescription.h>
  29. #include <Kernel/Net/IPv4Socket.h>
  30. #include <Kernel/Net/LocalSocket.h>
  31. #include <Kernel/Net/Socket.h>
  32. #include <Kernel/Process.h>
  33. #include <Kernel/UnixTypes.h>
  34. #include <LibC/errno_numbers.h>
  35. //#define SOCKET_DEBUG
  36. namespace Kernel {
  37. KResultOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol)
  38. {
  39. switch (domain) {
  40. case AF_LOCAL:
  41. return LocalSocket::create(type & SOCK_TYPE_MASK);
  42. case AF_INET:
  43. return IPv4Socket::create(type & SOCK_TYPE_MASK, protocol);
  44. default:
  45. return KResult(-EAFNOSUPPORT);
  46. }
  47. }
  48. Socket::Socket(int domain, int type, int protocol)
  49. : m_domain(domain)
  50. , m_type(type)
  51. , m_protocol(protocol)
  52. {
  53. auto& process = *Process::current();
  54. m_origin = { process.pid().value(), process.uid(), process.gid() };
  55. }
  56. Socket::~Socket()
  57. {
  58. }
  59. void Socket::set_setup_state(SetupState new_setup_state)
  60. {
  61. #ifdef SOCKET_DEBUG
  62. dbg() << "Socket{" << this << "} setup state moving from " << to_string(m_setup_state) << " to " << to_string(new_setup_state);
  63. #endif
  64. m_setup_state = new_setup_state;
  65. evaluate_block_conditions();
  66. }
  67. RefPtr<Socket> Socket::accept()
  68. {
  69. LOCKER(m_lock);
  70. if (m_pending.is_empty())
  71. return nullptr;
  72. #ifdef SOCKET_DEBUG
  73. dbg() << "Socket{" << this << "} de-queueing connection";
  74. #endif
  75. auto client = m_pending.take_first();
  76. ASSERT(!client->is_connected());
  77. auto& process = *Process::current();
  78. client->m_acceptor = { process.pid().value(), process.uid(), process.gid() };
  79. client->m_connected = true;
  80. client->m_role = Role::Accepted;
  81. if (!m_pending.is_empty())
  82. evaluate_block_conditions();
  83. return client;
  84. }
  85. KResult Socket::queue_connection_from(NonnullRefPtr<Socket> peer)
  86. {
  87. #ifdef SOCKET_DEBUG
  88. dbg() << "Socket{" << this << "} queueing connection";
  89. #endif
  90. LOCKER(m_lock);
  91. if (m_pending.size() >= m_backlog)
  92. return KResult(-ECONNREFUSED);
  93. m_pending.append(peer);
  94. evaluate_block_conditions();
  95. return KSuccess;
  96. }
  97. KResult Socket::setsockopt(int level, int option, Userspace<const void*> user_value, socklen_t user_value_size)
  98. {
  99. ASSERT(level == SOL_SOCKET);
  100. switch (option) {
  101. case SO_SNDTIMEO:
  102. if (user_value_size != sizeof(timeval))
  103. return KResult(-EINVAL);
  104. if (!copy_from_user(&m_send_timeout, static_ptr_cast<const timeval*>(user_value)))
  105. return KResult(-EFAULT);
  106. return KSuccess;
  107. case SO_RCVTIMEO:
  108. if (user_value_size != sizeof(timeval))
  109. return KResult(-EINVAL);
  110. if (!copy_from_user(&m_receive_timeout, static_ptr_cast<const timeval*>(user_value)))
  111. return KResult(-EFAULT);
  112. return KSuccess;
  113. case SO_BINDTODEVICE: {
  114. if (user_value_size != IFNAMSIZ)
  115. return KResult(-EINVAL);
  116. auto user_string = static_ptr_cast<const char*>(user_value);
  117. auto ifname = copy_string_from_user(user_string, user_value_size);
  118. if (ifname.is_null())
  119. return KResult(-EFAULT);
  120. auto device = NetworkAdapter::lookup_by_name(ifname);
  121. if (!device)
  122. return KResult(-ENODEV);
  123. m_bound_interface = device;
  124. return KSuccess;
  125. }
  126. case SO_KEEPALIVE:
  127. // FIXME: Obviously, this is not a real keepalive.
  128. return KSuccess;
  129. case SO_TIMESTAMP:
  130. if (user_value_size != sizeof(int))
  131. return KResult(-EINVAL);
  132. if (!copy_from_user(&m_timestamp, static_ptr_cast<const int*>(user_value)))
  133. return KResult(-EFAULT);
  134. if (m_timestamp && (domain() != AF_INET || type() == SOCK_STREAM)) {
  135. // FIXME: Support SO_TIMESTAMP for more protocols?
  136. m_timestamp = 0;
  137. return KResult(-ENOTSUP);
  138. }
  139. return KSuccess;
  140. default:
  141. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  142. return KResult(-ENOPROTOOPT);
  143. }
  144. }
  145. KResult Socket::getsockopt(FileDescription&, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
  146. {
  147. socklen_t size;
  148. if (!copy_from_user(&size, value_size.unsafe_userspace_ptr()))
  149. return KResult(-EFAULT);
  150. // FIXME: Add TCP_NODELAY, IPPROTO_TCP and IPPROTO_IP (used in OpenSSH)
  151. if (level != SOL_SOCKET) {
  152. // Not sure if this is the correct error code, but it's only temporary until other levels are implemented.
  153. return KResult(-ENOPROTOOPT);
  154. }
  155. switch (option) {
  156. case SO_SNDTIMEO:
  157. if (size < sizeof(timeval))
  158. return KResult(-EINVAL);
  159. if (!copy_to_user(static_ptr_cast<timeval*>(value), &m_send_timeout))
  160. return KResult(-EFAULT);
  161. size = sizeof(timeval);
  162. if (!copy_to_user(value_size, &size))
  163. return KResult(-EFAULT);
  164. return KSuccess;
  165. case SO_RCVTIMEO:
  166. if (size < sizeof(timeval))
  167. return KResult(-EINVAL);
  168. if (!copy_to_user(static_ptr_cast<timeval*>(value), &m_receive_timeout))
  169. return KResult(-EFAULT);
  170. size = sizeof(timeval);
  171. if (!copy_to_user(value_size, &size))
  172. return KResult(-EFAULT);
  173. return KSuccess;
  174. case SO_ERROR: {
  175. if (size < sizeof(int))
  176. return KResult(-EINVAL);
  177. dbgln("getsockopt(SO_ERROR): FIXME!");
  178. int errno = 0;
  179. if (!copy_to_user(static_ptr_cast<int*>(value), &errno))
  180. return KResult(-EFAULT);
  181. size = sizeof(int);
  182. if (!copy_to_user(value_size, &size))
  183. return KResult(-EFAULT);
  184. return KSuccess;
  185. }
  186. case SO_BINDTODEVICE:
  187. if (size < IFNAMSIZ)
  188. return KResult(-EINVAL);
  189. if (m_bound_interface) {
  190. const auto& name = m_bound_interface->name();
  191. auto length = name.length() + 1;
  192. if (!copy_to_user(static_ptr_cast<char*>(value), name.characters(), length))
  193. return KResult(-EFAULT);
  194. size = length;
  195. if (!copy_to_user(value_size, &size))
  196. return KResult(-EFAULT);
  197. return KSuccess;
  198. } else {
  199. size = 0;
  200. if (!copy_to_user(value_size, &size))
  201. return KResult(-EFAULT);
  202. return KResult(-EFAULT);
  203. }
  204. case SO_TIMESTAMP:
  205. if (size < sizeof(int))
  206. return KResult(-EINVAL);
  207. if (!copy_to_user(static_ptr_cast<int*>(value), &m_timestamp))
  208. return KResult(-EFAULT);
  209. size = sizeof(int);
  210. if (!copy_to_user(value_size, &size))
  211. return KResult(-EFAULT);
  212. return KSuccess;
  213. default:
  214. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  215. return KResult(-ENOPROTOOPT);
  216. }
  217. }
  218. KResultOr<size_t> Socket::read(FileDescription& description, size_t, UserOrKernelBuffer& buffer, size_t size)
  219. {
  220. if (is_shut_down_for_reading())
  221. return 0;
  222. timeval tv;
  223. return recvfrom(description, buffer, size, 0, {}, 0, tv);
  224. }
  225. KResultOr<size_t> Socket::write(FileDescription& description, size_t, const UserOrKernelBuffer& data, size_t size)
  226. {
  227. if (is_shut_down_for_writing())
  228. return -EPIPE;
  229. return sendto(description, data, size, 0, {}, 0);
  230. }
  231. KResult Socket::shutdown(int how)
  232. {
  233. LOCKER(lock());
  234. if (type() == SOCK_STREAM && !is_connected())
  235. return KResult(-ENOTCONN);
  236. if (m_role == Role::Listener)
  237. return KResult(-ENOTCONN);
  238. if (!m_shut_down_for_writing && (how & SHUT_WR))
  239. shut_down_for_writing();
  240. if (!m_shut_down_for_reading && (how & SHUT_RD))
  241. shut_down_for_reading();
  242. m_shut_down_for_reading |= (how & SHUT_RD) != 0;
  243. m_shut_down_for_writing |= (how & SHUT_WR) != 0;
  244. return KSuccess;
  245. }
  246. KResult Socket::stat(::stat& st) const
  247. {
  248. memset(&st, 0, sizeof(st));
  249. st.st_mode = S_IFSOCK;
  250. return KSuccess;
  251. }
  252. void Socket::set_connected(bool connected)
  253. {
  254. LOCKER(lock());
  255. if (m_connected == connected)
  256. return;
  257. m_connected = connected;
  258. evaluate_block_conditions();
  259. }
  260. }