IPv4Socket.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 <Kernel/FileSystem/FileDescription.h>
  28. #include <Kernel/Net/ARP.h>
  29. #include <Kernel/Net/ICMP.h>
  30. #include <Kernel/Net/IPv4.h>
  31. #include <Kernel/Net/IPv4Socket.h>
  32. #include <Kernel/Net/NetworkAdapter.h>
  33. #include <Kernel/Net/Routing.h>
  34. #include <Kernel/Net/TCP.h>
  35. #include <Kernel/Net/TCPSocket.h>
  36. #include <Kernel/Net/UDP.h>
  37. #include <Kernel/Net/UDPSocket.h>
  38. #include <Kernel/Process.h>
  39. #include <Kernel/UnixTypes.h>
  40. #include <LibC/errno_numbers.h>
  41. #include <LibC/sys/ioctl_numbers.h>
  42. //#define IPV4_SOCKET_DEBUG
  43. namespace Kernel {
  44. Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
  45. {
  46. static Lockable<HashTable<IPv4Socket*>>* s_table;
  47. if (!s_table)
  48. s_table = new Lockable<HashTable<IPv4Socket*>>;
  49. return *s_table;
  50. }
  51. KResultOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol)
  52. {
  53. if (type == SOCK_STREAM)
  54. return TCPSocket::create(protocol);
  55. if (type == SOCK_DGRAM)
  56. return UDPSocket::create(protocol);
  57. if (type == SOCK_RAW)
  58. return adopt(*new IPv4Socket(type, protocol));
  59. return KResult(-EINVAL);
  60. }
  61. IPv4Socket::IPv4Socket(int type, int protocol)
  62. : Socket(AF_INET, type, protocol)
  63. {
  64. #ifdef IPV4_SOCKET_DEBUG
  65. dbg() << "IPv4Socket{" << this << "} created with type=" << type << ", protocol=" << protocol;
  66. #endif
  67. m_buffer_mode = type == SOCK_STREAM ? BufferMode::Bytes : BufferMode::Packets;
  68. if (m_buffer_mode == BufferMode::Bytes) {
  69. m_scratch_buffer = KBuffer::create_with_size(65536);
  70. }
  71. LOCKER(all_sockets().lock());
  72. all_sockets().resource().set(this);
  73. }
  74. IPv4Socket::~IPv4Socket()
  75. {
  76. LOCKER(all_sockets().lock());
  77. all_sockets().resource().remove(this);
  78. }
  79. void IPv4Socket::get_local_address(sockaddr* address, socklen_t* address_size)
  80. {
  81. sockaddr_in local_address = { AF_INET, htons(m_local_port), { m_local_address.to_in_addr_t() }, { 0 } };
  82. memcpy(address, &local_address, min(static_cast<size_t>(*address_size), sizeof(sockaddr_in)));
  83. *address_size = sizeof(sockaddr_in);
  84. }
  85. void IPv4Socket::get_peer_address(sockaddr* address, socklen_t* address_size)
  86. {
  87. sockaddr_in peer_address = { AF_INET, htons(m_peer_port), { m_peer_address.to_in_addr_t() }, { 0 } };
  88. memcpy(address, &peer_address, min(static_cast<size_t>(*address_size), sizeof(sockaddr_in)));
  89. *address_size = sizeof(sockaddr_in);
  90. }
  91. KResult IPv4Socket::bind(const sockaddr* user_address, socklen_t address_size)
  92. {
  93. ASSERT(setup_state() == SetupState::Unstarted);
  94. if (address_size != sizeof(sockaddr_in))
  95. return KResult(-EINVAL);
  96. sockaddr_in address;
  97. copy_from_user(&address, user_address, sizeof(sockaddr_in));
  98. if (address.sin_family != AF_INET)
  99. return KResult(-EINVAL);
  100. auto requested_local_port = ntohs(address.sin_port);
  101. if (!Process::current->is_superuser()) {
  102. if (requested_local_port < 1024) {
  103. dbg() << "UID " << Process::current->uid() << " attempted to bind " << class_name() << " to port " << requested_local_port;
  104. return KResult(-EACCES);
  105. }
  106. }
  107. m_local_address = IPv4Address((const u8*)&address.sin_addr.s_addr);
  108. m_local_port = requested_local_port;
  109. #ifdef IPV4_SOCKET_DEBUG
  110. dbg() << "IPv4Socket::bind " << class_name() << "{" << this << "} to " << m_local_address << ":" << m_local_port;
  111. #endif
  112. return protocol_bind();
  113. }
  114. KResult IPv4Socket::listen(size_t backlog)
  115. {
  116. int rc = allocate_local_port_if_needed();
  117. if (rc < 0)
  118. return KResult(-EADDRINUSE);
  119. set_backlog(backlog);
  120. m_role = Role::Listener;
  121. #ifdef IPV4_SOCKET_DEBUG
  122. dbg() << "IPv4Socket{" << this << "} listening with backlog=" << backlog;
  123. #endif
  124. return protocol_listen();
  125. }
  126. KResult IPv4Socket::connect(FileDescription& description, const sockaddr* address, socklen_t address_size, ShouldBlock should_block)
  127. {
  128. if (address_size != sizeof(sockaddr_in))
  129. return KResult(-EINVAL);
  130. if (address->sa_family != AF_INET)
  131. return KResult(-EINVAL);
  132. if (m_role == Role::Connected)
  133. return KResult(-EISCONN);
  134. auto& ia = *(const sockaddr_in*)address;
  135. m_peer_address = IPv4Address((const u8*)&ia.sin_addr.s_addr);
  136. m_peer_port = ntohs(ia.sin_port);
  137. return protocol_connect(description, should_block);
  138. }
  139. void IPv4Socket::attach(FileDescription&)
  140. {
  141. }
  142. void IPv4Socket::detach(FileDescription&)
  143. {
  144. }
  145. bool IPv4Socket::can_read(const FileDescription&, size_t) const
  146. {
  147. if (m_role == Role::Listener)
  148. return can_accept();
  149. if (protocol_is_disconnected())
  150. return true;
  151. return m_can_read;
  152. }
  153. bool IPv4Socket::can_write(const FileDescription&, size_t) const
  154. {
  155. return is_connected();
  156. }
  157. int IPv4Socket::allocate_local_port_if_needed()
  158. {
  159. if (m_local_port)
  160. return m_local_port;
  161. int port = protocol_allocate_local_port();
  162. if (port < 0)
  163. return port;
  164. m_local_port = (u16)port;
  165. return port;
  166. }
  167. ssize_t IPv4Socket::sendto(FileDescription&, const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
  168. {
  169. (void)flags;
  170. if (addr && addr_length != sizeof(sockaddr_in))
  171. return -EINVAL;
  172. if (addr) {
  173. if (addr->sa_family != AF_INET) {
  174. klog() << "sendto: Bad address family: " << addr->sa_family << " is not AF_INET!";
  175. return -EAFNOSUPPORT;
  176. }
  177. auto& ia = *(const sockaddr_in*)addr;
  178. m_peer_address = IPv4Address((const u8*)&ia.sin_addr.s_addr);
  179. m_peer_port = ntohs(ia.sin_port);
  180. }
  181. auto routing_decision = route_to(m_peer_address, m_local_address, bound_interface());
  182. if (routing_decision.is_zero())
  183. return -EHOSTUNREACH;
  184. if (m_local_address.to_u32() == 0)
  185. m_local_address = routing_decision.adapter->ipv4_address();
  186. int rc = allocate_local_port_if_needed();
  187. if (rc < 0)
  188. return rc;
  189. #ifdef IPV4_SOCKET_DEBUG
  190. klog() << "sendto: destination=" << m_peer_address.to_string().characters() << ":" << m_peer_port;
  191. #endif
  192. if (type() == SOCK_RAW) {
  193. routing_decision.adapter->send_ipv4(routing_decision.next_hop, m_peer_address, (IPv4Protocol)protocol(), (const u8*)data, data_length, m_ttl);
  194. return data_length;
  195. }
  196. int nsent = protocol_send(data, data_length);
  197. if (nsent > 0)
  198. Thread::current->did_ipv4_socket_write(nsent);
  199. return nsent;
  200. }
  201. ssize_t IPv4Socket::receive_byte_buffered(FileDescription& description, void* buffer, size_t buffer_length, int, sockaddr*, socklen_t*)
  202. {
  203. Locker locker(lock());
  204. if (m_receive_buffer.is_empty()) {
  205. if (protocol_is_disconnected())
  206. return 0;
  207. if (!description.is_blocking())
  208. return -EAGAIN;
  209. locker.unlock();
  210. auto res = Thread::current->block<Thread::ReadBlocker>(description);
  211. locker.lock();
  212. if (!m_can_read) {
  213. if (res != Thread::BlockResult::WokeNormally)
  214. return -EINTR;
  215. // Unblocked due to timeout.
  216. return -EAGAIN;
  217. }
  218. }
  219. ASSERT(!m_receive_buffer.is_empty());
  220. int nreceived = m_receive_buffer.read((u8*)buffer, buffer_length);
  221. if (nreceived > 0)
  222. Thread::current->did_ipv4_socket_read((size_t)nreceived);
  223. m_can_read = !m_receive_buffer.is_empty();
  224. return nreceived;
  225. }
  226. ssize_t IPv4Socket::receive_packet_buffered(FileDescription& description, void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
  227. {
  228. Locker locker(lock());
  229. ReceivedPacket packet;
  230. {
  231. if (m_receive_queue.is_empty()) {
  232. // FIXME: Shouldn't this return -ENOTCONN instead of EOF?
  233. // But if so, we still need to deliver at least one EOF read to userspace.. right?
  234. if (protocol_is_disconnected())
  235. return 0;
  236. if (!description.is_blocking())
  237. return -EAGAIN;
  238. }
  239. if (!m_receive_queue.is_empty()) {
  240. packet = m_receive_queue.take_first();
  241. m_can_read = !m_receive_queue.is_empty();
  242. #ifdef IPV4_SOCKET_DEBUG
  243. dbg() << "IPv4Socket(" << this << "): recvfrom without blocking " << packet.data.value().size() << " bytes, packets in queue: " << m_receive_queue.size_slow();
  244. #endif
  245. }
  246. }
  247. if (!packet.data.has_value()) {
  248. if (protocol_is_disconnected()) {
  249. dbg() << "IPv4Socket{" << this << "} is protocol-disconnected, returning 0 in recvfrom!";
  250. return 0;
  251. }
  252. locker.unlock();
  253. auto res = Thread::current->block<Thread::ReadBlocker>(description);
  254. locker.lock();
  255. if (!m_can_read) {
  256. if (res != Thread::BlockResult::WokeNormally)
  257. return -EINTR;
  258. // Unblocked due to timeout.
  259. return -EAGAIN;
  260. }
  261. ASSERT(m_can_read);
  262. ASSERT(!m_receive_queue.is_empty());
  263. packet = m_receive_queue.take_first();
  264. m_can_read = !m_receive_queue.is_empty();
  265. #ifdef IPV4_SOCKET_DEBUG
  266. dbg() << "IPv4Socket(" << this << "): recvfrom with blocking " << packet.data.value().size() << " bytes, packets in queue: " << m_receive_queue.size_slow();
  267. #endif
  268. }
  269. ASSERT(packet.data.has_value());
  270. auto& ipv4_packet = *(const IPv4Packet*)(packet.data.value().data());
  271. if (addr) {
  272. #ifdef IPV4_SOCKET_DEBUG
  273. dbg() << "Incoming packet is from: " << packet.peer_address << ":" << packet.peer_port;
  274. #endif
  275. auto& ia = *(sockaddr_in*)addr;
  276. memcpy(&ia.sin_addr, &packet.peer_address, sizeof(IPv4Address));
  277. ia.sin_port = htons(packet.peer_port);
  278. ia.sin_family = AF_INET;
  279. ASSERT(addr_length);
  280. *addr_length = sizeof(sockaddr_in);
  281. }
  282. if (type() == SOCK_RAW) {
  283. ASSERT(buffer_length >= ipv4_packet.payload_size());
  284. memcpy(buffer, ipv4_packet.payload(), ipv4_packet.payload_size());
  285. return ipv4_packet.payload_size();
  286. }
  287. return protocol_receive(packet.data.value(), buffer, buffer_length, flags);
  288. }
  289. ssize_t IPv4Socket::recvfrom(FileDescription& description, void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
  290. {
  291. if (addr_length && *addr_length < sizeof(sockaddr_in))
  292. return -EINVAL;
  293. #ifdef IPV4_SOCKET_DEBUG
  294. klog() << "recvfrom: type=" << type() << ", local_port=" << local_port();
  295. #endif
  296. ssize_t nreceived = 0;
  297. if (buffer_mode() == BufferMode::Bytes)
  298. nreceived = receive_byte_buffered(description, buffer, buffer_length, flags, addr, addr_length);
  299. else
  300. nreceived = receive_packet_buffered(description, buffer, buffer_length, flags, addr, addr_length);
  301. if (nreceived > 0)
  302. Thread::current->did_ipv4_socket_read(nreceived);
  303. return nreceived;
  304. }
  305. bool IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port, KBuffer&& packet)
  306. {
  307. LOCKER(lock());
  308. if (is_shut_down_for_reading())
  309. return false;
  310. auto packet_size = packet.size();
  311. if (buffer_mode() == BufferMode::Bytes) {
  312. size_t space_in_receive_buffer = m_receive_buffer.space_for_writing();
  313. if (packet_size > space_in_receive_buffer) {
  314. dbg() << "IPv4Socket(" << this << "): did_receive refusing packet since buffer is full.";
  315. ASSERT(m_can_read);
  316. return false;
  317. }
  318. int nreceived = protocol_receive(packet, m_scratch_buffer.value().data(), m_scratch_buffer.value().size(), 0);
  319. m_receive_buffer.write(m_scratch_buffer.value().data(), nreceived);
  320. m_can_read = !m_receive_buffer.is_empty();
  321. } else {
  322. // FIXME: Maybe track the number of packets so we don't have to walk the entire packet queue to count them..
  323. if (m_receive_queue.size_slow() > 2000) {
  324. dbg() << "IPv4Socket(" << this << "): did_receive refusing packet since queue is full.";
  325. return false;
  326. }
  327. m_receive_queue.append({ source_address, source_port, move(packet) });
  328. m_can_read = true;
  329. }
  330. m_bytes_received += packet_size;
  331. #ifdef IPV4_SOCKET_DEBUG
  332. if (buffer_mode() == BufferMode::Bytes)
  333. dbg() << "IPv4Socket(" << this << "): did_receive " << packet_size << " bytes, total_received=" << m_bytes_received;
  334. else
  335. dbg() << "IPv4Socket(" << this << "): did_receive " << packet_size << " bytes, total_received=" << m_bytes_received << ", packets in queue: " << m_receive_queue.size_slow();
  336. #endif
  337. return true;
  338. }
  339. String IPv4Socket::absolute_path(const FileDescription&) const
  340. {
  341. if (m_role == Role::None)
  342. return "socket";
  343. StringBuilder builder;
  344. builder.append("socket:");
  345. builder.appendf("%s:%d", m_local_address.to_string().characters(), m_local_port);
  346. if (m_role == Role::Accepted || m_role == Role::Connected)
  347. builder.appendf(" / %s:%d", m_peer_address.to_string().characters(), m_peer_port);
  348. switch (m_role) {
  349. case Role::Listener:
  350. builder.append(" (listening)");
  351. break;
  352. case Role::Accepted:
  353. builder.append(" (accepted)");
  354. break;
  355. case Role::Connected:
  356. builder.append(" (connected)");
  357. break;
  358. case Role::Connecting:
  359. builder.append(" (connecting)");
  360. break;
  361. default:
  362. ASSERT_NOT_REACHED();
  363. }
  364. return builder.to_string();
  365. }
  366. KResult IPv4Socket::setsockopt(int level, int option, const void* value, socklen_t value_size)
  367. {
  368. if (level != IPPROTO_IP)
  369. return Socket::setsockopt(level, option, value, value_size);
  370. switch (option) {
  371. case IP_TTL:
  372. if (value_size < sizeof(int))
  373. return KResult(-EINVAL);
  374. if (*(const int*)value < 0 || *(const int*)value > 255)
  375. return KResult(-EINVAL);
  376. m_ttl = (u8) * (const int*)value;
  377. return KSuccess;
  378. default:
  379. return KResult(-ENOPROTOOPT);
  380. }
  381. }
  382. KResult IPv4Socket::getsockopt(FileDescription& description, int level, int option, void* value, socklen_t* value_size)
  383. {
  384. if (level != IPPROTO_IP)
  385. return Socket::getsockopt(description, level, option, value, value_size);
  386. switch (option) {
  387. case IP_TTL:
  388. if (*value_size < sizeof(int))
  389. return KResult(-EINVAL);
  390. *(int*)value = m_ttl;
  391. return KSuccess;
  392. default:
  393. return KResult(-ENOPROTOOPT);
  394. }
  395. }
  396. int IPv4Socket::ioctl(FileDescription&, unsigned request, FlatPtr arg)
  397. {
  398. REQUIRE_PROMISE(inet);
  399. auto ioctl_route = [request, arg]() {
  400. auto* route = (rtentry*)arg;
  401. if (!Process::current->validate_read_typed(route))
  402. return -EFAULT;
  403. char namebuf[IFNAMSIZ + 1];
  404. memcpy(namebuf, route->rt_dev, IFNAMSIZ);
  405. namebuf[sizeof(namebuf) - 1] = '\0';
  406. auto adapter = NetworkAdapter::lookup_by_name(namebuf);
  407. if (!adapter)
  408. return -ENODEV;
  409. switch (request) {
  410. case SIOCADDRT:
  411. if (!Process::current->is_superuser())
  412. return -EPERM;
  413. if (route->rt_gateway.sa_family != AF_INET)
  414. return -EAFNOSUPPORT;
  415. if ((route->rt_flags & (RTF_UP | RTF_GATEWAY)) != (RTF_UP | RTF_GATEWAY))
  416. return -EINVAL; // FIXME: Find the correct value to return
  417. adapter->set_ipv4_gateway(IPv4Address(((sockaddr_in&)route->rt_gateway).sin_addr.s_addr));
  418. return 0;
  419. case SIOCDELRT:
  420. // FIXME: Support gateway deletion
  421. return 0;
  422. }
  423. return -EINVAL;
  424. };
  425. auto ioctl_interface = [request, arg]() {
  426. auto* ifr = (ifreq*)arg;
  427. if (!Process::current->validate_read_typed(ifr))
  428. return -EFAULT;
  429. char namebuf[IFNAMSIZ + 1];
  430. memcpy(namebuf, ifr->ifr_name, IFNAMSIZ);
  431. namebuf[sizeof(namebuf) - 1] = '\0';
  432. auto adapter = NetworkAdapter::lookup_by_name(namebuf);
  433. if (!adapter)
  434. return -ENODEV;
  435. switch (request) {
  436. case SIOCSIFADDR:
  437. if (!Process::current->is_superuser())
  438. return -EPERM;
  439. if (ifr->ifr_addr.sa_family != AF_INET)
  440. return -EAFNOSUPPORT;
  441. adapter->set_ipv4_address(IPv4Address(((sockaddr_in&)ifr->ifr_addr).sin_addr.s_addr));
  442. return 0;
  443. case SIOCSIFNETMASK:
  444. if (!Process::current->is_superuser())
  445. return -EPERM;
  446. if (ifr->ifr_addr.sa_family != AF_INET)
  447. return -EAFNOSUPPORT;
  448. adapter->set_ipv4_netmask(IPv4Address(((sockaddr_in&)ifr->ifr_netmask).sin_addr.s_addr));
  449. return 0;
  450. case SIOCGIFADDR:
  451. if (!Process::current->validate_write_typed(ifr))
  452. return -EFAULT;
  453. ifr->ifr_addr.sa_family = AF_INET;
  454. ((sockaddr_in&)ifr->ifr_addr).sin_addr.s_addr = adapter->ipv4_address().to_u32();
  455. return 0;
  456. case SIOCGIFHWADDR:
  457. if (!Process::current->validate_write_typed(ifr))
  458. return -EFAULT;
  459. ifr->ifr_hwaddr.sa_family = AF_INET;
  460. {
  461. auto mac_address = adapter->mac_address();
  462. memcpy(ifr->ifr_hwaddr.sa_data, &mac_address, sizeof(MACAddress));
  463. }
  464. return 0;
  465. }
  466. return -EINVAL;
  467. };
  468. switch (request) {
  469. case SIOCSIFADDR:
  470. case SIOCSIFNETMASK:
  471. case SIOCGIFADDR:
  472. case SIOCGIFHWADDR:
  473. return ioctl_interface();
  474. case SIOCADDRT:
  475. case SIOCDELRT:
  476. return ioctl_route();
  477. }
  478. return -EINVAL;
  479. }
  480. void IPv4Socket::close()
  481. {
  482. shutdown(SHUT_RDWR);
  483. }
  484. void IPv4Socket::shut_down_for_reading()
  485. {
  486. Socket::shut_down_for_reading();
  487. m_can_read = true;
  488. }
  489. }