Commit graph

167 commits

Author SHA1 Message Date
brapru 63a15ed19d Kernel: Do not send delayed ack in response to RST/ACK
In accordance with RFC 793, if the receiver is in the SYN-SENT state
it should respond to a RST by aborting the connection and immediately
move to the CLOSED state.

Previously the system would ACK all RST/ACKs, and the remote peer would
just respond with more RST packets.
2021-08-02 02:45:56 +02:00
brapru ea2abb3200 Kernel: Convert NetworkTask to east-const style 2021-08-02 00:32:55 +02:00
brapru bdaaff70cb Utilities: Support static assignment of the ARP table 2021-07-25 17:57:08 +02:00
brapru f8c104aaaf Kernel: Add update option to remove an entry from the ARP table
Allows for specifying whether to set/delete an entry from the table.
2021-07-25 17:57:08 +02:00
Andreas Kling 9457d83986 Kernel: Rename Locker => MutexLocker 2021-07-18 01:53:04 +02:00
Andreas Kling cee9528168 Kernel: Rename Lock to Mutex
Let's be explicit about what kind of lock this is meant to be.
2021-07-17 21:10:32 +02:00
Liav A 1c94b5e8eb Kernel: Introduce the NetworkingManagement singleton
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-09 22:44:09 +04:30
Gunnar Beutner 60298121d8 Kernel: Make sure we increment the TX counter
This was broken by b436dd1.
2021-06-04 19:06:47 +02:00
Gunnar Beutner 7f7897c900 Kernel: Make sure outgoing ICMP packets have the correct checksum
The internet_checksum() function relies on the buffer - or at least the
checksum field - to be all zeroes.
2021-06-03 20:59:30 +02:00
Gunnar Beutner 377b06c8ac Kernel: Ignore duplicate SYN packets
When receiving a SYN packet for a connection that's in the "SYN
received" state we should ignore the duplicate SYN packet instead of
closing the connection. This can happen when we didn't accept the
connection in time and our peer has sent us another SYN packet because
it thought that the initial SYN packet was lost.
2021-05-28 08:01:00 +02:00
Gunnar Beutner b436dd138b Kernel: Avoid allocations when sending IP packets
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-26 23:09:28 +02:00
Brian Gianforcaro f255993349 Kernel: Log unexpected TCP packet flags in NetworkTask handle_tcp()
Occasionally we'll see messages in the serial console like:

    handle_tcp: unexpected flags in FinWait1 state

In these cases it would be nice to know what flags we are receiving that
we aren't expecting.
2021-05-15 09:46:50 +02:00
Gunnar Beutner 08aa3a91e3 Kernel: Try to retransmit lost TCP packets
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-14 16:50:00 +02:00
Gunnar Beutner 9daec809b7 Kernel: Wake up NetworkTask every 500 milliseconds
This wakes up NetworkTask every 500 milliseconds so that it can send
pending delayed TCP ACKs and isn't forced to send all of them early
when it goes to sleep like it did before.
2021-05-14 16:50:00 +02:00
Gunnar Beutner 990b2d0989 Kernel: Don't use delayed ACKs when establishing the connection
When establishing the connection we should send ACKs right away so
as to not delay the connection process. This didn't previously
matter because we'd flush all delayed ACKs when NetworkTask waits
for incoming packets.
2021-05-14 16:50:00 +02:00
Gunnar Beutner 2b6aa571d1 Kernel: Outbound packets should use the source address from the socket
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-12 16:31:29 +02:00
Gunnar Beutner af59f64bc0 Kernel: Coalesce TCP ACKs
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-12 13:47:07 +02:00
Gunnar Beutner ffc6b714b0 Kernel: Trigger TCP fast retransmission when we encounter lost packets
When we receive a TCP packet with a sequence number that is not what
we expected we have lost one or more packets. We can signal this to
the sender by sending a TCP ACK with the previous ack number so that
they can resend the missing TCP fragments.
2021-05-12 13:47:07 +02:00
Gunnar Beutner 7272127927 Kernel: Don't process TCP packets out of order
Previously we'd process TCP packets in whatever order we received
them in. In the case where packets arrived out of order we'd end
up passing garbage to the userspace process.

This was most evident for TLS connections:

courage:~ $ git clone https://github.com/SerenityOS/serenity
Cloning into 'serenity'...
remote: Enumerating objects: 178826, done.
remote: Counting objects: 100% (1880/1880), done.
remote: Compressing objects: 100% (907/907), done.
error: RPC failed; curl 56 OpenSSL SSL_read: error:1408F119:SSL
routines:SSL3_GET_RECORD:decryption failed or bad record mac, errno 0
error: 1918 bytes of body are still expected
fetch-pack: unexpected disconnect while reading sideband packet
fatal: early EOF
fatal: fetch-pack: invalid index-pack output
2021-05-12 13:47:07 +02:00
Gunnar Beutner 5feeb62843 Kernel: Avoid allocating KBuffers for TCP packets
This avoids allocating a KBuffer for each incoming TCP packet.
2021-05-12 13:47:07 +02:00
Sergey Bugaev 78459b92d5 Kernel: Implement IP multicast support
An IP socket can now join a multicast group by using the
IP_ADD_MEMBERSHIP sockopt, which will cause it to start receiving
packets sent to the multicast address, even though this address does
not belong to this host.
2021-05-05 21:16:17 +02:00
Gunnar Beutner e0ac611a08 Kernel: Tear down connections when we receive an RST packet 2021-04-30 23:11:56 +02:00
Gunnar Beutner c03cbf83ab Kernel: Record MAC addresses for incoming IPv4 packets
This way we don't have to do ARP just to send packets back to
an address which just sent us a packet.
2021-04-30 23:11:56 +02:00
Gunnar Beutner d8f92bdf96 Kernel: Avoid deadlock when trying to send packets from the NetworkTask
fixes #6758
2021-04-30 23:11:56 +02:00
Gunnar Beutner 4e6a26cbd2 Kernel: Silence a few more network dbgln()s 2021-04-27 08:59:02 +02:00
Brian Gianforcaro 8d6e9fad40 Kernel: Remove the now defunct LOCKER(..) macro. 2021-04-25 09:38:27 +02:00
Brian Gianforcaro 1682f0b760 Everything: Move to SPDX license identifiers in all files.
SPDX License Identifiers are a more compact / standardized
way of representing file license information.

See: https://spdx.dev/resources/use/#identifiers

This was done with the `ambr` search and replace tool.

 ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-04-22 11:22:27 +02:00
Brendan Coles 627cfe017c Kernel: NetworkTask: Remove 10.0.2.x as default IP for NIC interfaces 2021-04-03 11:25:18 +02:00
Andreas Kling f9aace29ec Kernel: Convert klog() => AK::Format in NetworkTask 2021-03-12 11:59:41 +01:00
Ben Wiederhake 5c15ca7b84 Kernel: Make sockets use AK::Time 2021-03-02 08:36:08 +01:00
Andreas Kling 5d180d1f99 Everywhere: Rename ASSERT => VERIFY
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED)

Since all of these checks are done in release builds as well,
let's rename them to VERIFY to prevent confusion, as everyone is
used to assertions being compiled out in release.

We can introduce a new ASSERT macro that is specifically for debug
checks, but I'm doing this wholesale conversion first since we've
accumulated thousands of these already, and it's not immediately
obvious which ones are suitable for ASSERT.
2021-02-23 20:56:54 +01:00
Brian Gianforcaro ddd79fe2cf Kernel: Add WaitQueue::wait_forever and it use it for all infinite waits.
In preparation for marking BlockingResult [[nodiscard]], there are a few
places that perform infinite waits, which we never observe the result of
the wait. Instead of suppressing them, add an alternate function which
returns void when performing and infinite wait.
2021-02-15 08:28:57 +01:00
Sergey Bugaev 4717009e3e Kernel: Hold less locks when receiving ICMP packets
* We don't have to lock the "all IPv4 sockets" in exclusive mode, shared mode is
  enough for just reading the list (as opposed to modifying it).
* We don't have to lock socket's own lock at all, the IPv4Socket::did_receive()
  implementation takes care of this.
* Most importantly, we don't have to hold the "all IPv4 sockets" across the
  IPv4Socket::did_receive() call(s). We can copy the current ICMP socket list
  while holding the lock, then release the lock, and then call
  IPv4Socket::did_receive() on all the ICMP sockets in our list.

These changes fix a deadlock triggered by receiving ICMP messages when using tap
networking setup (as opposed to QEMU's default user/SLIRP networking) on the host.
2021-02-12 15:37:28 +01:00
Andreas Kling 9984201634 Kernel: Use KResult a bit more in the IPv4 networking code 2021-01-31 12:13:16 +01:00
asynts 7cf0c7cc0d Meta: Split debug defines into multiple headers.
The following script was used to make these changes:

    #!/bin/bash
    set -e

    tmp=$(mktemp -d)

    echo "tmp=$tmp"

    find Kernel \( -name '*.cpp' -o -name '*.h' \) | sort > $tmp/Kernel.files
    find . \( -path ./Toolchain -prune -o -path ./Build -prune -o -path ./Kernel -prune \) -o \( -name '*.cpp' -o -name '*.h' \) -print | sort > $tmp/EverythingExceptKernel.files

    cat $tmp/Kernel.files | xargs grep -Eho '[A-Z0-9_]+_DEBUG' | sort | uniq > $tmp/Kernel.macros
    cat $tmp/EverythingExceptKernel.files | xargs grep -Eho '[A-Z0-9_]+_DEBUG' | sort | uniq > $tmp/EverythingExceptKernel.macros

    comm -23 $tmp/Kernel.macros $tmp/EverythingExceptKernel.macros > $tmp/Kernel.unique
    comm -1 $tmp/Kernel.macros $tmp/EverythingExceptKernel.macros > $tmp/EverythingExceptKernel.unique

    cat $tmp/Kernel.unique | awk '{ print "#cmakedefine01 "$1 }' > $tmp/Kernel.header
    cat $tmp/EverythingExceptKernel.unique | awk '{ print "#cmakedefine01 "$1 }' > $tmp/EverythingExceptKernel.header

    for macro in $(cat $tmp/Kernel.unique)
    do
        cat $tmp/Kernel.files | xargs grep -l $macro >> $tmp/Kernel.new-includes ||:
    done
    cat $tmp/Kernel.new-includes | sort > $tmp/Kernel.new-includes.sorted

    for macro in $(cat $tmp/EverythingExceptKernel.unique)
    do
        cat $tmp/Kernel.files | xargs grep -l $macro >> $tmp/Kernel.old-includes ||:
    done
    cat $tmp/Kernel.old-includes | sort > $tmp/Kernel.old-includes.sorted

    comm -23 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.new
    comm -13 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.old
    comm -12 $tmp/Kernel.new-includes.sorted $tmp/Kernel.old-includes.sorted > $tmp/Kernel.includes.mixed

    for file in $(cat $tmp/Kernel.includes.new)
    do
        sed -i -E 's/#include <AK\/Debug\.h>/#include <Kernel\/Debug\.h>/' $file
    done

    for file in $(cat $tmp/Kernel.includes.mixed)
    do
        echo "mixed include in $file, requires manual editing."
    done
2021-01-26 21:20:00 +01:00
asynts eea72b9b5c Everywhere: Hook up remaining debug macros to Debug.h. 2021-01-25 09:47:36 +01:00
asynts acdcf59a33 Everywhere: Remove unnecessary debug comments.
It would be tempting to uncomment these statements, but that won't work
with the new changes.

This was done with the following commands:

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/#define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/#define/ { toggle = 1 }' {} \;

    find . \( -name '*.cpp' -o -name '*.h' -o -name '*.in' \) -not -path './Toolchain/*' -not -path './Build/*' -exec awk -i inplace '$0 !~ /\/\/ #define/ { if (!toggle) { print; } else { toggle = !toggle } } ; $0 ~/\/\/ #define/ { toggle = 1 }' {} \;
2021-01-25 09:47:36 +01:00
Lenny Maiorani e6f907a155 AK: Simplify constructors and conversions from nullptr_t
Problem:
- Many constructors are defined as `{}` rather than using the ` =
  default` compiler-provided constructor.
- Some types provide an implicit conversion operator from `nullptr_t`
  instead of requiring the caller to default construct. This violates
  the C++ Core Guidelines suggestion to declare single-argument
  constructors explicit
  (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c46-by-default-declare-single-argument-constructors-explicit).

Solution:
- Change default constructors to use the compiler-provided default
  constructor.
- Remove implicit conversion operators from `nullptr_t` and change
  usage to enforce type consistency without conversion.
2021-01-12 09:11:45 +01:00
Tom 476f17b3f1 Kernel: Merge PurgeableVMObject into AnonymousVMObject
This implements memory commitments and lazy-allocation of committed
memory.
2021-01-01 23:43:44 +01:00
Andreas Kling ed5c26d698 AK: Remove custom %w format string specifier
This was a non-standard specifier alias for %04x. This patch replaces
all uses of it with new-style formatting functions instead.
2020-12-25 17:05:05 +01:00
Andreas Kling cb2c8f71f4 AK: Remove custom %b format string specifier
This was a non-standard specifier alias for %02x. This patch replaces
all uses of it with new-style formatting functions instead.
2020-12-25 17:04:28 +01:00
Lenny Maiorani 765936ebae
Everywhere: Switch from (void) to [[maybe_unused]] (#4473)
Problem:
- `(void)` simply casts the expression to void. This is understood to
  indicate that it is ignored, but this is really a compiler trick to
  get the compiler to not generate a warning.

Solution:
- Use the `[[maybe_unused]]` attribute to indicate the value is unused.

Note:
- Functions taking a `(void)` argument list have also been changed to
  `()` because this is not needed and shows up in the same grep
  command.
2020-12-21 00:09:48 +01:00
Tom da5cc34ebb Kernel: Fix some issues related to fixes and block conditions
Fix some problems with join blocks where the joining thread block
condition was added twice, which lead to a crash when trying to
unblock that condition a second time.

Deferred block condition evaluation by File objects were also not
properly keeping the File object alive, which lead to some random
crashes and corruption problems.

Other problems were caused by the fact that the Queued state didn't
handle signals/interruptions consistently. To solve these issues we
remove this state entirely, along with Thread::wait_on and change
the WaitQueue into a BlockCondition instead.

Also, deliver signals even if there isn't going to be a context switch
to another thread.

Fixes #4336 and #4330
2020-12-12 21:28:12 +01:00
Tom 046d6855f5 Kernel: Move block condition evaluation out of the Scheduler
This makes the Scheduler a lot leaner by not having to evaluate
block conditions every time it is invoked. Instead evaluate them as
the states change, and unblock threads at that point.

This also implements some more waitid/waitpid/wait features and
behavior. For example, WUNTRACED and WNOWAIT are now supported. And
wait will now not return EINTR when SIGCHLD is delivered at the
same time.
2020-11-30 13:17:02 +01:00
Tom 6a620562cc Kernel: Allow passing a thread argument for new kernel threads
This adds the ability to pass a pointer to kernel thread/process.
Also add the ability to use a closure as thread function, which
allows passing information to a kernel thread more easily.
2020-11-30 13:17:02 +01:00
Andreas Kling a6aee0c097 IPv4: Take the socket lock more (fixes TCP connection to localhost)
This fixes an issue where making a TCP connection to localhost didn't
work correctly since the loopback interface is currently synchronous.
(Sending something to localhost would enqueue a packet on the same
interface and then immediately wake the network task to process that
packet.)

This was preventing the TCP handshake from working correctly with
localhost since we'd send out the SYN packet before moving to the
SynSent state. The lock is now held long enough for this operation
to be atomic.
2020-10-21 20:51:02 +02:00
Andreas Kling ce6ef54337 ICMP: Check that incoming ICMP echo requests are large enough
Otherwise, just ignore them.
2020-10-20 18:10:10 +02:00
Tom 838d9fa251 Kernel: Make Thread refcounted
Similar to Process, we need to make Thread refcounted. This will solve
problems that will appear once we schedule threads on more than one
processor. This allows us to hold onto threads without necessarily
holding the scheduler lock for the entire duration.
2020-09-27 19:46:04 +02:00
Nico Weber 416d470d07 Kernel: Plumb packet receive timestamp from NetworkAdapter to Socket::recvfrom
Since the receiving socket isn't yet known at packet receive time,
keep timestamps for all packets.

This is useful for keeping statistics about in-kernel queue latencies
in the future, and it can be used to implement SO_TIMESTAMP.
2020-09-17 17:23:01 +02:00
Tom c8d9f1b9c9 Kernel: Make copy_to/from_user safe and remove unnecessary checks
Since the CPU already does almost all necessary validation steps
for us, we don't really need to attempt to do this. Doing it
ourselves doesn't really work very reliably, because we'd have to
account for other processors modifying virtual memory, and we'd
have to account for e.g. pages not being able to be allocated
due to insufficient resources.

So change the copy_to/from_user (and associated helper functions)
to use the new safe_memcpy, which will return whether it succeeded
or not. The only manual validation step needed (which the CPU
can't perform for us) is making sure the pointers provided by user
mode aren't pointing to kernel mappings.

To make it easier to read/write from/to either kernel or user mode
data add the UserOrKernelBuffer helper class, which will internally
either use copy_from/to_user or directly memcpy, or pass the data
through directly using a temporary buffer on the stack.

Last but not least we need to keep syscall params trivial as we
need to copy them from/to user mode using copy_from/to_user.
2020-09-13 21:19:15 +02:00
Nico Weber 430b265cd4 AK: Rename KB, MB, GB to KiB, MiB, GiB
The SI prefixes "k", "M", "G" mean "10^3", "10^6", "10^9".
The IEC prefixes "Ki", "Mi", "Gi" mean "2^10", "2^20", "2^30".

Let's use the correct name, at least in code.

Only changes the name of the constants, no other behavior change.
2020-08-16 16:33:28 +02:00
asynts fff581cd72 AK: Rename span() to bytes() when appropriate.
I originally defined the bytes() method for the String class, because it
made it obvious that it's a span of bytes instead of span of characters.

This commit makes this more consistent by defining a bytes() method when
the type of the span is known to be u8.

Additionaly, the cast operator to Bytes is overloaded for ByteBuffer and
such.
2020-08-15 21:21:18 +02:00
Andreas Kling f5ac4da993 Kernel: Use AK::Span a bunch in the network adapter code 2020-07-28 20:19:22 +02:00
Tom 788b2d64c6 Kernel: Require a reason to be passed to Thread::wait_on
The Lock class still permits no reason, but for everything else
require a reason to be passed to Thread::wait_on. This makes it
easier to diagnose why a Thread is in Queued state.
2020-07-06 10:00:24 +02:00
Tom 16783bd14d Kernel: Turn Thread::current and Process::current into functions
This allows us to query the current thread and process on a
per processor basis
2020-07-01 12:07:01 +02:00
Andreas Kling f25d2f5518 Kernel: Move NetworkTask startup into NetworkTask::spawn() 2020-04-09 13:31:05 +02:00
AnotherTest 2ea934bcfd Kernel: Do not reject broadcast UDP packets right away
This patch relaxes how we think about UDP packets being "for us" a bit;
the proper way to handle this would be to also check if the matched
socket has SO_BROADCAST set, but we don't have that :)
2020-04-04 12:23:46 +02:00
Liav A 0fc60e41dd Kernel: Use klog() instead of kprintf()
Also, duplicate data in dbg() and klog() calls were removed.
In addition, leakage of virtual address to kernel log is prevented.
This is done by replacing kprintf() calls to dbg() calls with the
leaked data instead.
Also, other kprintf() calls were replaced with klog().
2020-03-02 22:23:39 +01:00
Andreas Kling 48f7c28a5c Kernel: Replace "current" with Thread::current and Process::current
Suggested by Sergey. The currently running Thread and Process are now
Thread::current and Process::current respectively. :^)
2020-02-17 15:04:27 +01:00
Andreas Kling a356e48150 Kernel: Move all code into the Kernel namespace 2020-02-16 01:27:42 +01:00
Andreas Kling 228a1e9099 IPv4: Basic implementation of TCP socket shutdown
We can now participate in the TCP connection closing handshake. :^)
This implementation is definitely not complete and needs to handle a
bunch of other cases. But it's a huge improvement over not being able
to close connections at all.

Note that we hold on to pending-close sockets indefinitely, until they
are moved into the Closed state. This should also have a timeout but
that's still a FIXME. :^)

Fixes #428.
2020-02-08 16:04:27 +01:00
Andreas Kling 1037a1d2ba IPv4: Don't ACK empty TCP packets
Wireshark was complaining about duplicate ACK's and this was why.
2020-02-08 14:09:02 +01:00
Andreas Kling 94ca55cefd Meta: Add license header to source files
As suggested by Joshua, this commit adds the 2-clause BSD license as a
comment block to the top of every source file.

For the first pass, I've just added myself for simplicity. I encourage
everyone to add themselves as copyright holders of any file they've
added or modified in some significant way. If I've added myself in
error somewhere, feel free to replace it with the appropriate copyright
holder instead.

Going forward, all new source files should include a license header.
2020-01-18 09:45:54 +01:00
Conrad Pankoff bbb536ebed Kernel: Fix code locked behind NETWORK_TASK_DEBUG 2019-12-28 02:03:49 +01:00
Conrad Pankoff 876323fd7a Kernel: Move incoming packet buffer off the NetworkTask stack 2019-12-28 00:24:43 +01:00
Andreas Kling 8874ad7946 Net: Shrink the NetworkTask packet buffer
This thing was overflowing the NetworkTask's kernel stack. Oops!
2019-12-22 14:54:37 +01:00
Andreas Kling 8de5a0be41 TCP: Log packet metadata when receiving a TCP packet with no socket 2019-12-14 13:03:26 +01:00
Andreas Kling ac215ca601 Net: Try to reuse incoming packet buffers to avoid allocation churn
The majority of the time in NetworkTask was being spent in allocating
and deallocating KBuffers for each incoming packet.

We'll now keep up to 100 buffers around and reuse them for new packets
if the next incoming packet fits in an old buffer. This is pretty
naively implemented but definitely cuts down on time spent here.
2019-12-14 11:07:37 +01:00
Andreas Kling a18aa8fd5f Kernel: Use a WaitQueue in NetworkTask 2019-12-01 16:47:49 +01:00
Andreas Kling 1b2ef8582c Kernel: Make File's can_read/can_write take a const FileDescription&
Asking a File if we could possibly read or write it will never mutate
the asking FileDescription&, so it should be const.
2019-11-04 14:03:14 +01:00
Andreas Kling 8f45a259fc ByteBuffer: Remove pointer() in favor of data()
We had two ways to get the data inside a ByteBuffer. That was silly.
2019-09-30 08:57:01 +02:00
Andreas Kling 8cfb859368 IPv4: Support overriding the default TTL (64)
Made getsockopt() and setsockopt() virtual so we can handle them in the
various Socket subclasses. The subclasses map kinda nicely to "levels".

This will allow us to implement things like "traceroute", although..
I spent some time trying to do that, but then hit a wall when it turned
out that the user-mode networking in QEMU doesn't preserve TTL in the
ICMP packets passing through.
2019-09-19 21:42:59 +02:00
Conrad Pankoff 3f1c3a341b Kernel: Handle listening socket disappearing during incoming handshake 2019-09-08 12:34:20 +02:00
Conrad Pankoff a2b61e30c5 Kernel: Put some network log messages behind debug flags 2019-09-08 12:34:20 +02:00
Conrad Pankoff 328d52b323 Kernel: Send ACK/FIN in response to FIN packets on active connections
This is to work around our lack of a shutdown() implementation.
2019-09-08 12:34:20 +02:00
Conrad Pankoff 117d8db2a2 Kernel: Implement outgoing TCP retransmission and better ACK handling
This approach is a bit naiive - whenever we send a packet out, we
check to see if there are any other packets we should try to send.
This works well enough for a busy connection but not very well for a
quiet one. Ideally we would check for not-acked packets on some kind
of timer, and use the length of this not-acked list as feedback to
throttle the writes coming from userspace.
2019-09-08 12:34:20 +02:00
Conrad Pankoff b8e3c7ef01 Kernel: Remember all ARP replies, even ones we didn't request
This allows us to take advantage of unsolicited ARP replies, such as
those that are emitted by many systems after their network interfaces
are enabled, or after their DHCP client sets their IP.

This also makes us a bit more vulnerable to ARP flooding, but we need
some kind of eviction strategy anyway, so we can deal with that later.
2019-09-08 12:34:20 +02:00
Conrad Pankoff b45cfae7f4 Kernel: Don't mark incoming sockets as connected in NetworkTask
An incoming socket should only be considered connected after a
program has received it from accept(). Before that point, it's only
"half" open, and it might not ever actually be served to a program.

Socket::accept is where m_connected is correctly set.
2019-09-08 12:34:20 +02:00
Conrad Pankoff 72f728b0d6 Kernel: Hold socket back from accept() until it's fully set up 2019-09-08 12:34:20 +02:00
Conrad Pankoff cfcb53fe77 Kernel: Don't set sequence manually; send_tcp_packet will do it 2019-09-08 12:34:20 +02:00
Conrad Pankoff 706e04d340 Kernel: Don't increment ACK number without SYN, FIN, or data 2019-09-08 12:34:20 +02:00
Conrad Pankoff 498f8c01a2 Kernel: Use a public member for NetworkAdapter on_receive 2019-08-29 06:25:06 +02:00
Conrad Pankoff 6d1418aa7a Kernel: Add simple ARP routing layer
This replaces the previous placeholder routing layer with a real one!
It's still very primitive, doesn't deal with things like timeouts very
well, and will probably need several more iterations to support more
normal networking things.

I haven't confirmed that this works with anything other than the QEMU
user networking layer, but I suspect that's what nearly everybody is
using at this point, so that's the important target to keep working.
2019-08-29 06:25:06 +02:00
Conrad Pankoff 93c16590f1 Kernel: Remove specific devices from network code
By setting up the devices in init() and looping over the registered
network adapters in NetworkTask_main, we can remove the remaining
hard-coded adapter references from the network code.

This also assigns IPs according to the default range supplied by QEMU
in its slirp networking mode.
2019-08-29 06:25:06 +02:00
Conrad Pankoff 5f86a979ea Kernel: Ignore IPv6 packets; log unknown Ethernet payload types 2019-08-29 06:25:06 +02:00
Conrad Pankoff 4afe9e4f2a Kernel: Implement rtl8139 network interface driver 2019-08-21 17:10:34 +02:00
Conrad Pankoff fc3667c026 Kernel: Detect some outgoing TCP connection failures 2019-08-10 09:07:11 +02:00
Conrad Pankoff bd6d2c0819 Kernel: Use a more detailed state machine for socket setup 2019-08-10 09:07:11 +02:00
Conrad Pankoff 638008da13 Kernel: Move TCP state logging into TCPSocket 2019-08-10 09:07:11 +02:00
Andreas Kling 7e309e27dd Kernel: Clean up and sanitize incoming packet handling a bit more
Once we've converted from an Ethernet frame to an IPv4 packet, we can
pass the IPv4Packet around instead of the EthernetFrameHeader.

Also add some more code to ignore invalid-looking packets.
2019-08-09 10:42:01 +02:00
Andreas Kling f9de264f88 Kernel: Actually, let's not hang() on bad incoming packets
That kinda defeats the purpose of being smart enough to ignore them :^)
2019-08-09 09:38:38 +02:00
Andreas Kling 9e2a00248e Kernel: Do some basic sanity checking on IPv4 packet headers
Ignore packets that are too small, or not as large as they claim to be.
2019-08-09 09:36:06 +02:00
Conrad Pankoff 5c66c67f32 Kernel: Improve some network-related log messages 2019-08-09 07:09:26 +02:00
Conrad Pankoff 3eb659a2bb Kernel: Implement TCP listening sockets and incoming connections 2019-08-09 07:09:26 +02:00
Conrad Pankoff 54ceabd48d Kernel: Use WeakPtr<NetworkAdapter> instead of NetworkAdapter* in net code 2019-08-09 07:09:26 +02:00
Conrad Pankoff 7ed54d86d5 Kernel: Record network statistics and expose as JSON
This is comprised of five small changes:

* Keep a counter for tx/rx packets/bytes per TCP socket
* Keep a counter for tx/rx packets/bytes per network adapter
* Expose that data in /proc/net_tcp and /proc/netadapters
* Convert /proc/netadapters to JSON
* Fix up ifconfig to read the JSON from netadapters
2019-08-08 06:44:49 +02:00
Conrad Pankoff 061c092fae Kernel: Prevent RST spam when we get an unexpected packet 2019-08-08 06:42:41 +02:00
Conrad Pankoff 73c998dbfc Kernel: Refactor TCP/IP stack
This has several significant changes to the networking stack.

* Significant refactoring of the TCP state machine. Right now it's
  probably more fragile than it used to be, but handles quite a lot
  more of the handshake process.
* `TCPSocket` holds a `NetworkAdapter*`, assigned during `connect()` or
  `bind()`, whichever comes first.
* `listen()` is now virtual in `Socket` and intended to be implemented
  in its child classes
* `listen()` no longer works without `bind()` - this is a bit of a
  regression, but listening sockets didn't work at all before, so it's
  not possible to observe the regression.
* A file is exposed at `/proc/net_tcp`, which is a JSON document listing
  the current TCP sockets with a bit of metadata.
* There's an `ETHERNET_VERY_DEBUG` flag for dumping packet's content out
  to `kprintf`. It is, indeed, _very debug_.
2019-08-06 16:21:17 +02:00
Andreas Kling 605975adb5 Kernel: Make KBuffer a value-type wrapper around a KBufferImpl
A KBuffer always contains a valid KBufferImpl. If you need a "null"
state buffer, use Optional<KBuffer>.

This makes KBuffer very easy to work with and pass around, just like
ByteBuffer before it.
2019-08-05 11:07:45 +02:00
Andreas Kling 52cfe9ebae IPv4: Remove an unnecessary copy of each outgoing IPv4 payload
There's no need for send_ipv4() to take a ByteBuffer&&, the data is
immediately cooked into a packet and transmitted. Instead, just pass
it the address+length of whatever buffer we've been using locally.

The more we can reduce the pressure on kmalloc the better. :^)
2019-08-05 10:43:22 +02:00