summaryrefslogtreecommitdiff
path: root/Kernel/Net/TCPSocket.cpp
AgeCommit message (Collapse)Author
2021-12-16Kernel: Return the correct result for FIONREAD on datagram socketssin-ack
Before this commit, we only checked the receive buffer on the socket, which is unused on datagram streams. Now we return the actual size of the datagram without the protocol headers, which required the protocol to tell us what the size of the payload is.
2021-11-08Kernel: Replace KResult and KResultOr<T> with Error and ErrorOr<T>Andreas Kling
We now use AK::Error and AK::ErrorOr<T> in both kernel and userspace! This was a slightly tedious refactoring that took a long time, so it's not unlikely that some bugs crept in. Nevertheless, it does pass basic functionality testing, and it's just real nice to finally see the same pattern in all contexts. :^)
2021-10-28Kernel+LibC: Add support for the IPv4 TOS field via the IP_TOS sockoptIdan Horowitz
2021-09-16Kernel: Drop the receive buffer when socket enters the TimeWait statesin-ack
The TimeWait state is intended to prevent another socket from taking the address tuple in case any packets are still in transit after the final close. Since this state never delivers packets to userspace, it doesn't make sense to keep the receive buffer around.
2021-09-07Kernel: Make KBuffer::try_create_with_size() return KResultOrAndreas Kling
This allows us to use TRY() in a lot of new places.
2021-09-07Kernel: TCPSocket always has a scratch bufferAndreas Kling
Let's encode this in the constructor signature.
2021-09-07Kernel/Net: Add a special SOCKET_TRY() and use it in socket codeAndreas Kling
Sockets remember their last error code in the SO_ERROR field, so we need to take special care to remember this when returning an error. This patch adds a SOCKET_TRY() that works like TRY() but also calls set_so_error() on the failure path. There's probably a lot more code that should be using this, but that's outside the scope of this patch.
2021-09-07Kernel: Make TCPSocket client construction use KResultOr and TRY()Andreas Kling
We don't really have anywhere to propagate the error in NetworkTask at the moment, since it runs in its own kernel thread and has no direct userspace caller.
2021-09-07Kernel: Make DoubleBuffer::try() return KResultOrAndreas Kling
This tidies up error propagation in a number of places.
2021-09-07Kernel: Rename FileDescription => OpenFileDescriptionAndreas Kling
Dr. POSIX really calls these "open file description", not just "file description", so let's call them exactly that. :^)
2021-09-07Kernel: Make UserOrKernelBuffer return KResult from read/write/memsetAndreas Kling
This allows us to simplify a whole bunch of call sites with TRY(). :^)
2021-09-05Kernel: Use TRY() in TCPSocketAndreas Kling
2021-09-04Kernel: Tidy up TCPSocket creation a bitAndreas Kling
- Rename create() => try_create() - Use adopt_nonnull_ref_or_enomem()
2021-09-01Kernel: Don't cast to NetworkOrdered<u16>* from random dataBrian Gianforcaro
NetworkOrdered is a non trivial type, and it's undefined behavior to cast a random pointer to it and then pretend it's that type. Instead just call AK::convert_between_host_and_network_endian on the individual u16*. This suppresses static analysis warnings. I don't think there was a "bug" in the previous code, it worked, but it was very brittle.
2021-08-29Kernel: Rename Socket::lock() => Socket::mutex()Andreas Kling
"lock" is ambiguous (verb vs noun) while "mutex" is not.
2021-08-29Kernel: Add Socket::set_role() and use it everywhereAndreas Kling
Instead of having Socket subclasses write their role into Socket::m_role directly, add a setter to do this.
2021-08-22Kernel: Rename ProtectedValue<T> => MutexProtected<T>Andreas Kling
Let's make it obvious what we're protecting it with.
2021-08-15Kernel: Convert TCP retransmit queue from HashTable to IntrusiveListAndreas Kling
2021-08-14Kernel: Stop allowing implicit conversion from KResult to intAndreas Kling
This patch removes KResult::operator int() and deals with the fallout. This forces a lot of code to be more explicit in its handling of errors, greatly improving readability.
2021-08-13Kernel: Clear SO_ERROR on successful socket connectionbrapru
When TCP sockets successfully establish a connection, any SO_ERROR should be cleared back to success. For example, SO_ERROR gets set to EINPROGRESS on asynchronous connect calls and should be cleared when the socket moves to the Established state.
2021-08-10Kernel: Add so_error to keep track of the socket's error statebrapru
This sets the m_so_error variable every time the socket returns an error.
2021-08-08Everywhere: Replace AK::Singleton => SingletonAndreas Kling
2021-08-07Kernel/TCP: Port TCP retransmit queue to ProtectedValueAndreas Kling
I had to switch to exclusive locking since ProtectedValue rightly doesn't allow you to mutate protected data with only a shared lock.
2021-08-07Kernel: Migrate TCP socket tables locking to ProtectedValueJean-Baptiste Boric
Note: TCPSocket::create_client() has a dubious locking process where the sockets by tuple table is first shared lock to check if the socket exists and bail out if it does, then unlocks, then exclusively locks to add the tuple. There could be a race condition where two client creation requests for the same tuple happen at the same time and both cleared the shared lock check. When in doubt, lock exclusively the whole time.
2021-08-07Kernel: Move Lockable into its own headerJean-Baptiste Boric
2021-08-03Kernel: Handle OOM when allocating Packet KBuffersBrian Gianforcaro
2021-08-03Kernel: Handle OOM when allocating IPv4Socket optional scratch bufferBrian Gianforcaro
2021-08-03Kernel: Handle OOM from DoubleBuffer usage in IPv4SocketBrian Gianforcaro
The IPv4Socket requires a DoubleBuffer for storage of any data it received on the socket. However it was previously using the default constructor which can not observe allocation failure. Address this by plumbing the receive buffer through the various derived classes.
2021-07-18Kernel: Rename Locker => MutexLockerAndreas Kling
2021-07-17Kernel: Rename Lock to MutexAndreas Kling
Let's be explicit about what kind of lock this is meant to be.
2021-06-24Everywhere: Use nothrow new with `adopt_{ref,own}_if_nonnull`Daniel Bertalan
This commit converts naked `new`s to `AK::try_make` and `AK::try_create` wherever possible. If the called constructor is private, this can not be done, so we instead now use the standard-defined and compiler-agnostic `new (nothrow)`.
2021-06-11Kernel: Block writes while we're establishing the TCP connectionGunnar Beutner
Previously we would not block the caller until the connection was established and would instead return EPIPE for the first send() call which then likely caused the caller to abandon the socket. This was broken by 0625342.
2021-06-09Kernel: Introduce the NetworkingManagement singletonLiav A
Instead of initializing network adapters in init.cpp, let's move that logic into a separate class to handle this. Also, it seems like a good idea to shift responsiblity on enumeration of network adapters after the boot process, so this singleton will take care of finding the appropriate network adapter when asked to with an IPv4 address or interface name. With this change being merged, we simplify the creation logic of NetworkAdapter derived classes, so we enumerate the PCI bus only once, searching for driver candidates when doing so, and we let each driver to test if it is resposible for the specified PCI device.
2021-06-04Kernel: Make sure we increment the TX counterGunnar Beutner
This was broken by b436dd1.
2021-06-01Kernel: Dont try to register ephemeral TCP ports twicestelar7
2021-05-28Kernel: Release packet buffer in TCPSocket::send_tcp_packetGunnar Beutner
Previously we wouldn't release the buffer back to the network adapter in all cases. While this didn't leak the buffer it would cause the buffer to not be reused for other packets.
2021-05-26Kernel: Block when writing to TCP sockets when the send window is fullGunnar Beutner
Previously we'd just dump those packets into the network adapter's send queue and hope for the best. Instead we should wait until the peer has sent TCP ACK packets. Ideally this would parse the TCP window size option from the SYN or SYN|ACK packet, but for now we just assume the window size is 64 kB.
2021-05-26Kernel: Avoid allocations when sending IP packetsGunnar Beutner
Previously we'd allocate buffers when sending packets. This patch avoids these allocations by using the NetworkAdapter's packet queue. At the same time this also avoids copying partially constructed packets in order to prepend Ethernet and/or IPv4 headers. It also properly truncates UDP and raw IP packets.
2021-05-25Kernel: Don't try to send TCP packets larger than the MSSGunnar Beutner
Previously TCPSocket::send_tcp_packet() would try to send TCP packets which matched whatever size the userspace program specified. We'd try to break those packets up into smaller fragments, however a much better approach is to limit TCP packets to the maximum segment size and avoid fragmentation altogether.
2021-05-19Kernel: static vs non-static constexpr variablesLenny Maiorani
Problem: - `static` variables consume memory and sometimes are less optimizable. - `static const` variables can be `constexpr`, usually. - `static` function-local variables require an initialization check every time the function is run. Solution: - If a global `static` variable is only used in a single function then move it into the function and make it non-`static` and `constexpr`. - Make all global `static` variables `constexpr` instead of `const`. - Change function-local `static const[expr]` variables to be just `constexpr`.
2021-05-16Kernel: Avoid allocations when handling network packetsGunnar Beutner
2021-05-14Kernel: Merge do_retransmit_packets() into retransmit_packets()Gunnar Beutner
2021-05-14Kernel: Try to retransmit lost TCP packetsGunnar Beutner
Previously we didn't retransmit lost TCP packets which would cause connections to hang if packets were lost. Also we now time out TCP connections after a number of retransmission attempts.
2021-05-14Kernel: Avoid unnecessary time under lock in TCPSocket::createBrian Gianforcaro
Avoid holding the sockets_by_tuple lock while allocating the TCPSocket. While checking if the list contains the item we can also hold the lock in shared mode, as we are only reading the hash table. In addition the call to from_tuple appears to be superfluous, as we created the socket, so we should be able to just return it directly. This avoids the recursive lock acquisition, as well as the unnecessary hash table lookups.
2021-05-14Kernel: Remove dead TCPSocket::from_endpoints methodBrian Gianforcaro
2021-05-13Kernel: Make TCPSocket::create API OOM safeBrian Gianforcaro
Note that the changes to IPv4Socket::create are unfortunately needed as the return type of TCPSocket::create and IPv4Socket::create don't match. - KResultOr<NonnullRefPtr<TcpSocket>>> vs - KResultOr<NonnullRefPtr<Socket>>> To handle this we are forced to manually decompose the KResultOr<T> and return the value() and error() separately.
2021-05-12Kernel: Outbound packets should use the source address from the socketGunnar Beutner
Previously we'd use the adapter's address as the source address when sending packets. Instead we should use the socket's bound local address.
2021-05-12Kernel: Coalesce TCP ACKsGunnar Beutner
Previously we'd send a TCP ACK for each TCP packet we received. This changes NetworkTask so that we send fewer TCP ACKs.
2021-05-12Kernel: Set MSS option for outbound TCP SYN packetsGunnar Beutner
When the MSS option header is missing the default maximum segment size is 536 which results in lots of very small TCP packets that NetworkTask has to handle. This adds the MSS option header to outbound TCP SYN packets and sets it to an appropriate value depending on the interface's MTU. Note that we do not currently do path MTU discovery so this could cause problems when hops don't fragment packets properly.
2021-05-12Kernel: Increase the default TCP window sizeGunnar Beutner
This increases the default TCP window size to a more reasonable value of 64k. This allows TCP peers to send us more packets before waiting for corresponding ACKs.