Socket.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/Debug.h>
  27. #include <AK/StringBuilder.h>
  28. #include <AK/StringView.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. //#define SOCKET_DEBUG
  37. namespace Kernel {
  38. KResultOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol)
  39. {
  40. switch (domain) {
  41. case AF_LOCAL:
  42. return LocalSocket::create(type & SOCK_TYPE_MASK);
  43. case AF_INET:
  44. return IPv4Socket::create(type & SOCK_TYPE_MASK, protocol);
  45. default:
  46. return EAFNOSUPPORT;
  47. }
  48. }
  49. Socket::Socket(int domain, int type, int protocol)
  50. : m_domain(domain)
  51. , m_type(type)
  52. , m_protocol(protocol)
  53. {
  54. auto& process = *Process::current();
  55. m_origin = { process.pid().value(), process.uid(), process.gid() };
  56. }
  57. Socket::~Socket()
  58. {
  59. }
  60. void Socket::set_setup_state(SetupState new_setup_state)
  61. {
  62. dbgln<debug_socket>("Socket({}) setup state moving from {} to {}", this, to_string(m_setup_state), to_string(new_setup_state));
  63. m_setup_state = new_setup_state;
  64. evaluate_block_conditions();
  65. }
  66. RefPtr<Socket> Socket::accept()
  67. {
  68. LOCKER(m_lock);
  69. if (m_pending.is_empty())
  70. return nullptr;
  71. dbgln<debug_socket>("Socket({}) de-queueing connection", this);
  72. auto client = m_pending.take_first();
  73. ASSERT(!client->is_connected());
  74. auto& process = *Process::current();
  75. client->m_acceptor = { process.pid().value(), process.uid(), process.gid() };
  76. client->m_connected = true;
  77. client->m_role = Role::Accepted;
  78. if (!m_pending.is_empty())
  79. evaluate_block_conditions();
  80. return client;
  81. }
  82. KResult Socket::queue_connection_from(NonnullRefPtr<Socket> peer)
  83. {
  84. dbgln<debug_socket>("Socket({}) queueing connection", this);
  85. LOCKER(m_lock);
  86. if (m_pending.size() >= m_backlog)
  87. return ECONNREFUSED;
  88. m_pending.append(peer);
  89. evaluate_block_conditions();
  90. return KSuccess;
  91. }
  92. KResult Socket::setsockopt(int level, int option, Userspace<const void*> user_value, socklen_t user_value_size)
  93. {
  94. ASSERT(level == SOL_SOCKET);
  95. switch (option) {
  96. case SO_SNDTIMEO:
  97. if (user_value_size != sizeof(timeval))
  98. return EINVAL;
  99. if (!copy_from_user(&m_send_timeout, static_ptr_cast<const timeval*>(user_value)))
  100. return EFAULT;
  101. return KSuccess;
  102. case SO_RCVTIMEO:
  103. if (user_value_size != sizeof(timeval))
  104. return EINVAL;
  105. if (!copy_from_user(&m_receive_timeout, static_ptr_cast<const timeval*>(user_value)))
  106. return EFAULT;
  107. return KSuccess;
  108. case SO_BINDTODEVICE: {
  109. if (user_value_size != IFNAMSIZ)
  110. return EINVAL;
  111. auto user_string = static_ptr_cast<const char*>(user_value);
  112. auto ifname = copy_string_from_user(user_string, user_value_size);
  113. if (ifname.is_null())
  114. return EFAULT;
  115. auto device = NetworkAdapter::lookup_by_name(ifname);
  116. if (!device)
  117. return ENODEV;
  118. m_bound_interface = device;
  119. return KSuccess;
  120. }
  121. case SO_KEEPALIVE:
  122. // FIXME: Obviously, this is not a real keepalive.
  123. return KSuccess;
  124. case SO_TIMESTAMP:
  125. if (user_value_size != sizeof(int))
  126. return EINVAL;
  127. if (!copy_from_user(&m_timestamp, static_ptr_cast<const int*>(user_value)))
  128. return EFAULT;
  129. if (m_timestamp && (domain() != AF_INET || type() == SOCK_STREAM)) {
  130. // FIXME: Support SO_TIMESTAMP for more protocols?
  131. m_timestamp = 0;
  132. return ENOTSUP;
  133. }
  134. return KSuccess;
  135. default:
  136. dbgln("setsockopt({}) at SOL_SOCKET not implemented.", option);
  137. return ENOPROTOOPT;
  138. }
  139. }
  140. KResult Socket::getsockopt(FileDescription&, int level, int option, Userspace<void*> value, Userspace<socklen_t*> value_size)
  141. {
  142. socklen_t size;
  143. if (!copy_from_user(&size, value_size.unsafe_userspace_ptr()))
  144. return EFAULT;
  145. // FIXME: Add TCP_NODELAY, IPPROTO_TCP and IPPROTO_IP (used in OpenSSH)
  146. if (level != SOL_SOCKET) {
  147. // Not sure if this is the correct error code, but it's only temporary until other levels are implemented.
  148. return ENOPROTOOPT;
  149. }
  150. switch (option) {
  151. case SO_SNDTIMEO:
  152. if (size < sizeof(timeval))
  153. return EINVAL;
  154. if (!copy_to_user(static_ptr_cast<timeval*>(value), &m_send_timeout))
  155. return EFAULT;
  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. if (!copy_to_user(static_ptr_cast<timeval*>(value), &m_receive_timeout))
  164. return EFAULT;
  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, size_t, UserOrKernelBuffer& buffer, size_t size)
  214. {
  215. if (is_shut_down_for_reading())
  216. return 0;
  217. timeval tv;
  218. return recvfrom(description, buffer, size, 0, {}, 0, tv);
  219. }
  220. KResultOr<size_t> Socket::write(FileDescription& description, size_t, 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(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(lock());
  250. if (m_connected == connected)
  251. return;
  252. m_connected = connected;
  253. evaluate_block_conditions();
  254. }
  255. }