summaryrefslogtreecommitdiff
path: root/Kernel/Net
AgeCommit message (Collapse)Author
2023-04-14Kernel/Net: Iron out the locking mechanism across the subsystemLiav A
There is a big mix of LockRefPtrs all over the Networking subsystem, as well as lots of room for improvements with our locking patterns, which this commit will not pursue, but will give a good start for such work. To deal with this situation, we change the following things: - Creating instances of NetworkAdapter should always yield a non-locking NonnullRefPtr. Acquiring an instance from the NetworkingManagement should give a simple RefPtr,as giving LockRefPtr does not really protect from concurrency problems in such case. - Since NetworkingManagement works with normal RefPtrs we should protect all instances of RefPtr<NetworkAdapter> with SpinlockProtected to ensure references are gone unexpectedly. - Protect the so_error class member with a proper spinlock. This happens to be important because the clear_so_error() method lacked any proper locking measures. It also helps preventing a possible TOCTOU when we might do a more fine-grained locking in the Socket code, so this could be definitely a start for this. - Change unnecessary LockRefPtr<PacketWithTimestamp> in the structure of OutgoingPacket to a simple RefPtr<PacketWithTimestamp> as the whole list should be MutexProtected.
2023-04-14Kernel/Net: Make the LoopbackAdapter initializer to use ErrorOr patternLiav A
This looks much more nice, and also matches our pattern for other types of network adapters' initializers.
2023-04-14Kernel/Net: Convert initializers to return NonnullRefPtr<NetworkAdapter>Liav A
There's no need for using NonnullLockRefPtr here.
2023-04-14Kernel/Net: Implement SIOCGIFINDEX and SIOCGIFNAME for socketsArda Cinar
These ioctls exist on Linux and can be used to implement libc functions if_indextoname and if_nametoindex (without needing to parse any JSON).
2023-04-04Kernel: Simplify Process factory functionsAndreas Kling
- Instead of taking the first new thread as an out-parameter, we now bundle the process and its first thread in a struct and use that as the return value. - Make all Process factory functions return ErrorOr. Use this to convert some places to more TRY(). - Drop the "try_" prefix on Process factory functions.
2023-03-10Kernel: Use RefPtr instead of LockRefPtr for File and subclassesAndreas Kling
This was mostly straightforward, as all the storage locations are guarded by some related mutex. The use of old-school associated mutexes instead of MutexProtected is unfortunate, but the process to modernize such code is ongoing.
2023-03-09Kernel: Switch LockRefPtr<Inode> to RefPtr<Inode>Andreas Kling
The main place where this is a little iffy is in RAMFS where inodes have a LockWeakPtr to their parent inode. I've left that as a LockWeakPtr for now.
2023-03-07Kernel: Use non-locking {Nonnull,}RefPtr for OpenFileDescriptionAndreas Kling
This patch switches away from {Nonnull,}LockRefPtr to the non-locking smart pointers throughout the kernel. I've looked at the handful of places where these were being persisted and I don't see any race situations. Note that the process file descriptor table (Process::m_fds) was already guarded via MutexProtected.
2023-03-06Kernel: Stop using NonnullLockRefPtrVectorAndreas Kling
2023-03-06Everywhere: Remove NonnullOwnPtr.h includesAndreas Kling
2023-03-06Everywhere: Stop using NonnullOwnPtrVectorAndreas Kling
Same as NonnullRefPtrVector: weird semantics, questionable benefits.
2023-02-19Kernel: Support sending filedescriptors with sendmsg(2) and SCM_RIGHTSPeter Elliott
This is necessary to support the wayland protocol. I also moved the CMSG_* macros to the kernel API since they are used in both kernel and userspace. this does not break ntpquery/SCM_TIMESTAMP.
2023-02-19Kernel: Change polarity of weak ownership between Inode and LocalSocketPeter Elliott
There was a bug in which bound Inodes would lose all their references (because localsocket does not reference them), and they would be deallocated, and clients would get ECONNREFUSED as a result. now LocalSocket has a strong reference to inode so that the inode will live as long as the socket, and Inode has a weak reference to the socket, because if the socket stops being referenced anywhere it should not be bound. This still prevents the reference loop that 220b7dd77905b7d573ded093cf88d2dc51f57c69 was trying to fix.
2023-02-10Kernel/Net: Propagate proper errno codes from determine_network_deviceLiav A
Returning literal strings is not the proper action here, because we should always assume that error could be propagated back to userland, so we need to keep a valid errno when returning an Error.
2023-02-10AK+Everywhere: Do not implicitly copy variables in TRY macrosTimothy Flynn
For example, consider cases where we want to propagate errors only in specific instances: auto result = read_data(); // something like ErrorOr<ByteBuffer> if (result.is_error() && result.error().code() != EINTR) continue; auto bytes = TRY(result); The TRY invocation will currently copy the byte buffer when the expression (in this case, just a local variable) is stored into _temporary_result. This patch binds the expression to a reference to prevent such copies. In less trival invocations (such as TRY(some_function()), this will incur only temporary lifetime extensions, i.e. no functional change.
2023-02-10Everywhere: Remove needless copies of Error / ErrorOr instancesTimothy Flynn
Either take the underlying objects with release_* methods or move() the instances around.
2023-02-10Kernel: Store socket errors as errno codes rather than ErrorOr valuesTimothy Flynn
2023-02-05Kernel: Pass `ipv4_packet_size` to `ipv4.set_length()`Iman Seyed
Instead of `sizeof(IPv4Packet) + payload_size` expression, pass `ipv4_packet_size` to `ipv4.set_length()`
2023-01-27Kernel: Remove declarations for non-existent methodsSam Atkins
2023-01-26Kernel/PCI: Hold a reference to DeviceIdentifier in the Device classLiav A
There are now 2 separate classes for almost the same object type: - EnumerableDeviceIdentifier, which is used in the enumeration code for all PCI host controller classes. This is allowed to be moved and copied, as it doesn't support ref-counting. - DeviceIdentifier, which inherits from EnumerableDeviceIdentifier. This class uses ref-counting, and is not allowed to be copied. It has a spinlock member in its structure to allow safely executing complicated IO sequences on a PCI device and its space configuration. There's a static method that allows a quick conversion from EnumerableDeviceIdentifier to DeviceIdentifier while creating a NonnullRefPtr out of it. The reason for doing this is for the sake of integrity and reliablity of the system in 2 places: - Ensure that "complicated" tasks that rely on manipulating PCI device registers are done in a safe manner. For example, determining a PCI BAR space size requires multiple read and writes to the same register, and if another CPU tries to do something else with our selected register, then the result will be a catastrophe. - Allow the PCI API to have a united form around a shared object which actually holds much more data than the PCI::Address structure. This is fundamental if we want to do certain types of optimizations, and be able to support more features of the PCI bus in the foreseeable future. This patch already has several implications: - All PCI::Device(s) hold a reference to a DeviceIdentifier structure being given originally from the PCI::Access singleton. This means that all instances of DeviceIdentifier structures are located in one place, and all references are pointing to that location. This ensures that locking the operation spinlock will take effect in all the appropriate places. - We no longer support adding PCI host controllers and then immediately allow for enumerating it with a lambda function. It was found that this method is extremely broken and too much complicated to work reliably with the new paradigm being introduced in this patch. This means that for Volume Management Devices (Intel VMD devices), we simply first enumerate the PCI bus for such devices in the storage code, and if we find a device, we attach it in the PCI::Access method which will scan for devices behind that bridge and will add new DeviceIdentifier(s) objects to its internal Vector. Afterwards, we just continue as usual with scanning for actual storage controllers, so we will find a corresponding NVMe controllers if there were any behind that VMD bridge.
2023-01-21Kernel+Userland: Move LibC/sys/ioctl_numbers to Kernel/API/Ioctl.hAndrew Kaster
This header has always been fundamentally a Kernel API file. Move it where it belongs. Include it directly in Kernel files, and make Userland applications include it via sys/ioctl.h rather than directly.
2023-01-15Everywhere: Fully qualify IsLvalueReference in TRY() macrosAndrew Kaster
If USING_AK_GLOBALLY is not defined, the name IsLvalueReference might not be available in the global namespace. Follow the pattern established in LibTest to fully qualify AK types in macros to avoid this problem.
2023-01-13AK+Everywhere: Disallow returning a reference from a fallible expressionTimothy Flynn
This will silently make a copy. Rather than masking this behavior, let's explicitly disallow it.
2023-01-13Kernel/Net: Get the correct interface type in SIOCGIFHWADDR ioctlArda Cinar
When calling ioctl on a socket with SIOCGIFHWADDR, return the correct physical interface type. This value was previously hardcoded to ARPHRD_ETHER (Ethernet), and now can also return ARPHRD_LOOPBACK for the loopback adapter.
2023-01-12Kernel: AK: Fix ignored .to_string() errors in IPv4SocketMacDue
2023-01-08Kernel: Remove the NE2000 PCI network adapter driverLiav A
Nobody tests this network card as the person who added it, Jean-Baptiste Boric (known as boricj) is not an active contributor in the project now. After a discussion with him on the Discord server, we agreed it's for the best to remove the driver, as for two reasons: - The original author (boricj) agreed to do this, stating that he will not be able to test the driver anymore after his Athlon XP machine is no longer supported after the removal of the i686 port. - It was agreed that the NE2000 network card family is far from the ideal hardware we would want to support, similarly to the RTL8139 that got removed recently for almost the same reason.
2023-01-07Kernel: Remove the RTL8139 PCI network adapter driverLiav A
Nobody tests this network card, and the driver has bugs (see the issue https://github.com/SerenityOS/serenity/issues/10198 for more details), so it's almost certain that this happened due to code being rotting when there's simply no testing of it. Essentially this has been determined to be dead-code so this is the most important reason to drop this code. Another good reason to do so is because the RTL8139 only supports Fast Ethernet connections (10/100 Megabits per second), and is considered obsolete even for bare metal setups.
2023-01-07Kernel/Net: Introduce a new mechanism to initialize a PCI deviceLiav A
Instead of using a clunky if-statement paradigm, we now have all drivers being declaring two methods for their adapter class - create and probe. These methods are linked in each PCINetworkDriverInitializer structure, in a new s_initializers static list of them. Then, when we probe for a PCI device, we use each probe method and if there's a match, then the corresponding create method is called. After the adapter instance is created, we call the virtual initialize method on it, because many drivers actually require a sort of post-construction initialization sequence to ensure the network adapter can properly function. As a result of this change, it's much more easy to add more drivers and the initialization code is more readable and it's easier to understand when and where things could fail in the whole initialization sequence.
2023-01-07Kernel/Net: Allocate regions before invoking the RTL8139 constructorLiav A
Instead of allocating those regions in the constructor, which makes it impossible to fail in case of OOM condition, allocate them in the static factory method so we could propagate errors in case of failure.
2023-01-07Kernel/Net: Allocate regions before invoking Intel driver constructorsLiav A
Instead of allocating after the construction point ensure that all Intel drivers are allocating necessary buffer regions and then pass them to the constructors. This could let us fail early in case of OOM, so we don't touch a network adapter before we ensure we have all the appropriate mappings in place.
2023-01-05Kernel: Add dmesgln_pci logging for Kernel::PCIEvan Smal
A virtual method named device_name() was added to Kernel::PCI to support logging the PCI::Device name and address using dmesgln_pci. Previously, PCI::Device did not store the device name. All devices inheriting from PCI::Device now use dmesgln_pci where they previously used dmesgln.
2023-01-02Everywhere: Remove unused includes of AK/StdLibExtras.hBen Wiederhake
These instances were detected by searching for files that include AK/StdLibExtras.h, but don't match the regex: \\b(abs|AK_REPLACED_STD_NAMESPACE|array_size|ceil_div|clamp|exchange|for ward|is_constant_evaluated|is_power_of_two|max|min|mix|move|_RawPtr|RawP tr|round_up_to_power_of_two|swap|to_underlying)\\b (Without the linebreaks.) This regex is pessimistic, so there might be more files that don't actually use any "extra stdlib" functions. In theory, one might use LibCPP to detect things like this automatically, but let's do this one step after another.
2023-01-02Kernel: Turn lock ranks into template parameterskleines Filmröllchen
This step would ideally not have been necessary (increases amount of refactoring and templates necessary, which in turn increases build times), but it gives us a couple of nice properties: - SpinlockProtected inside Singleton (a very common combination) can now obtain any lock rank just via the template parameter. It was not previously possible to do this with SingletonInstanceCreator magic. - SpinlockProtected's lock rank is now mandatory; this is the majority of cases and allows us to see where we're still missing proper ranks. - The type already informs us what lock rank a lock has, which aids code readability and (possibly, if gdb cooperates) lock mismatch debugging. - The rank of a lock can no longer be dynamic, which is not something we wanted in the first place (or made use of). Locks randomly changing their rank sounds like a disaster waiting to happen. - In some places, we might be able to statically check that locks are taken in the right order (with the right lock rank checking implementation) as rank information is fully statically known. This refactoring even more exposes the fact that Mutex has no lock rank capabilites, which is not fixed here.
2023-01-02AK: Combine SinglyLinkedList and SinglyLinkedListWithCountLenny Maiorani
Using policy based design `SinglyLinkedList` and `SinglyLinkedListWithCount` can be combined into one class which takes a policy to determine how to keep track of the size of the list. The default policy is to use list iteration to count the items in the list each time. The `WithCount` form is a different policy which tracks the size, but comes with the overhead of storing the count and incrementing/decrementing on each modification. This model is extensible to have other forms of counting by implementing only a new policy instead of implementing a totally new type.
2022-12-17Kernel: Propagate errors in E1000NetworkAdapterBaitinq
We now move the ErrorOr returning functions in the constructor to the try_to_initialize() factory, which allows us to handle the errors and removes two FIXME's :))
2022-12-14Kernel: Check against TCP packet size overflows in checksum calculationTim Schumacher
2022-12-14Kernel: Convert TCP pseudo-headers through a unionTim Schumacher
This keeps us from tripping strict aliasing, which previously made TCP connections inoperable when building without `-fsanitize=undefined` or `-fno-strict-aliasing`.
2022-12-13Kernel: Propagate errors during network adapter detection/initializationAndreas Kling
When scanning for network adapters, we give each driver a chance to claim the PCI device and whoever claims it first gets to keep it. Before this patch, the driver API returned a LockRefPtr<AdapterType>, which made it impossible to propagate errors that occurred during detection and/or initialization. This patch changes the API so that errors can bubble all the way out the PCI enumeration in NetworkingManagement::initialize() where we perform all the network adapter auto-detection on boot. When we eventually start to support hot-plugging network adapter in the future, it will be even more important to propagate errors instead of swallowing them. Importantly, before this patch, some errors were "handled" by panicking the kernel. This is no longer the case. 7 FIXMEs were killed in the making of this commit. :^)
2022-11-01AK+Kernel: Handle some allocation failures in IPv4Socket and TCPSocketGunnar Beutner
This adds try_* methods to AK::SinglyLinkedList and AK::SinglyLinkedListWithCount and updates the network stack to use those to gracefully handle allocation failures. Refs #6369.
2022-10-26Kernel: Use generic functions to change interrupt state of ProcessorTimon Kruiper
This allows these files to be built for aarch64.
2022-10-17Kernel: Move InterruptDisabler out of Arch directoryTimon Kruiper
The code in this file is not architecture specific, so it can be moved to the base Kernel directory.
2022-09-23Kernel: Introduce the IOWindow classLiav A
This class is intended to replace all IOAddress usages in the Kernel codebase altogether. The idea is to ensure IO can be done in arch-specific manner that is determined mostly in compile-time, but to still be able to use most of the Kernel code in non-x86 builds. Specific devices that rely on x86-specific IO instructions are already placed in the Arch/x86 directory and are omitted for non-x86 builds. The reason this works so well is the fact that x86 IO space acts in a similar fashion to the traditional memory space being available in most CPU architectures - the x86 IO space is essentially just an array of bytes like the physical memory address space, but requires x86 IO instructions to load and store data. Therefore, many devices allow host software to interact with the hardware registers in both ways, with a noticeable trend even in the modern x86 hardware to move away from the old x86 IO space to exclusively using memory-mapped IO. Therefore, the IOWindow class encapsulates both methods for x86 builds. The idea is to allow PCI devices to be used in either way in x86 builds, so when trying to map an IOWindow on a PCI BAR, the Kernel will try to find the proper method being declared with the PCI BAR flags. For old PCI hardware on non-x86 builds this might turn into a problem as we can't use port mapped IO, so the Kernel will gracefully fail with ENOTSUP error code if that's the case, as there's really nothing we can do within such case. For general IO, the read{8,16,32} and write{8,16,32} methods are available as a convenient API for other places in the Kernel. There are simply no direct 64-bit IO API methods yet, as it's not needed right now and is not considered to be Arch-agnostic too - the x86 IO space doesn't support generating 64 bit cycle on IO bus and instead requires two 2 32-bit accesses. If for whatever reason it appears to be necessary to do IO in such manner, it could probably be added with some neat tricks to do so. It is recommended to use Memory::TypedMapping struct if direct 64 bit IO is actually needed.
2022-09-20Kernel/PCI: Convert PCI BAR number to a strong typed enum classLiav A
2022-08-22Kernel: Use Process::credentials() and remove user ID/group ID helpersAnthony Iacono
Move away from using the group ID/user ID helpers in the process to allow for us to take advantage of the immutable credentials instead.
2022-08-21Kernel: Make sys$recvfrom() with MSG_DONTWAIT not so racyAndreas Kling
Instead of temporary changing the open file description's "blocking" flag while doing a non-waiting recvfrom, we instead plumb the currently wanted blocking behavior all the way through to the underlying socket.
2022-08-21Kernel: Make Socket::connect() take credentials as inputAndreas Kling
2022-08-21Kernel: Make Socket::bind() take credentials as inputAndreas Kling
2022-08-21Kernel: Make LocalSocket do chown/chmod through VFSAndreas Kling
This ensures that all the permissions checks are made against the provided credentials. Previously we were just calling through directly to the inode setters, which did no security checks!
2022-08-21Kernel: Make File::{chown,chmod} take credentials as inputAndreas Kling
...instead of getting them from Process::current(). :^)
2022-08-21Kernel: Make VirtualFileSystem functions take credentials as inputAndreas Kling
Instead of getting credentials from Process::current(), we now require that they be provided as input to the various VFS functions. This ensures that an atomic set of credentials is used throughout an entire VFS operation.