Socket.cpp 9.0 KB

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