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