Socket.cpp 8.9 KB

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