ladybird/Kernel
Sergey Bugaev 95bcffd713 Kernel/Net: Rework ephemeral port allocation
Currently, ephemeral port allocation is handled by the
allocate_local_port_if_needed() and protocol_allocate_local_port()
methods. Actually binding the socket to an address (which means
inserting the socket/address pair into a global map) is performed either
in protocol_allocate_local_port() (for ephemeral ports) or in
protocol_listen() (for non-ephemeral ports); the latter will fail with
EADDRINUSE if the address is already used by an existing pair present in
the map.

There used to be a bug where for listen() without an explicit bind(),
the port allocation would conflict with itself: first an ephemeral port
would get allocated and inserted into the map, and then
protocol_listen() would check again for the port being free, find the
just-created map entry, and error out. This was fixed in commit
01e5af487f by passing an additional flag
did_allocate_port into protocol_listen() which specifies whether the
port was just allocated, and skipping the check in protocol_listen() if
the flag is set.

However, this only helps if the socket is bound to an ephemeral port
inside of this very listen() call. But calling bind(sin_port = 0) from
userspace should succeed and bind to an allocated ephemeral port, in the
same was as using an unbound socket for connect() does. The port number
can then be retrieved from userspace by calling getsockname (), and it
should be possible to either connect() or listen() on this socket,
keeping the allocated port number. Also, calling bind() when already
bound (either explicitly or implicitly) should always result in EINVAL.

To untangle this, introduce an explicit m_bound state in IPv4Socket,
just like LocalSocket has already. Once a socket is bound, further
attempt to bind it fail. Some operations cause the socket to implicitly
get bound to an (ephemeral) address; this is implemented by the new
ensure_bound() method. The protocol_allocate_local_port() method is
gone; it is now up to a protocol to assign a port to the socket inside
protocol_bind() if it finds that the socket has local_port() == 0.

protocol_bind() is now called in more cases, such as inside listen() if
the socket wasn't bound before that.
2023-07-29 16:51:58 -06:00
..
API Kernel: Add a key code modifier to detect the number pad 2023-07-09 06:32:20 +02:00
Arch Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
Boot Kernel: Move all boot-related code to the new Boot subdirectory 2023-06-04 21:32:34 +02:00
Bus Kernel/Tasks: Allow Kernel processes to be shut down 2023-07-15 00:12:01 +02:00
Devices Kernel: Add a key code modifier to detect the number pad 2023-07-09 06:32:20 +02:00
FileSystem Kernel/FileSystem: Rename block_size -> logical_block_size 2023-07-28 14:51:07 +02:00
Firmware/ACPI Kernel: Overhaul system shutdown procedure 2023-07-15 00:12:01 +02:00
Heap Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
Interrupts Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
Library Everywhere: Resolve conflicts with LibC and libc++ 2023-06-27 12:40:38 +02:00
Locking Kernel: Move all tasks-related code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
Memory Kernel: Allow relaxing cleanup task rules during system shutdown 2023-07-15 00:12:01 +02:00
Net Kernel/Net: Rework ephemeral port allocation 2023-07-29 16:51:58 -06:00
Prekernel Everywhere: Resolve conflicts with LibC and libc++ 2023-06-27 12:40:38 +02:00
Security Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
Syscalls Kernel/FileSystem: Rename block_size -> logical_block_size 2023-07-28 14:51:07 +02:00
Tasks Kernel: Overhaul system shutdown procedure 2023-07-15 00:12:01 +02:00
Time Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
TTY Kernel: Move all Graphics-related code into Devices/GPU directory 2023-06-06 00:40:32 +02:00
CMakeLists.txt Kernel: Overhaul system shutdown procedure 2023-07-15 00:12:01 +02:00
Debug.h.in Kernel: Put loopback adapter debug spam behind a flag 2023-06-18 08:50:33 +01:00
embedmap.sh Kernel: Make new kernel build process work on macOS 2021-07-15 11:04:30 +02:00
Forward.h Kernel+LibCore+LibC: Split the mount syscall into multiple syscalls 2023-07-02 01:04:51 +02:00
generate-version-file.sh Kernel: Bake version information into the Kernel 2022-10-14 13:45:33 +02:00
kprintf.cpp Kernel: Move all Graphics-related code into Devices/GPU directory 2023-06-06 00:40:32 +02:00
kstdio.h Kernel: Remove includes to LibC stdarg definitions 2023-03-01 19:36:53 -07:00
KSyms.cpp Kernel: Move all tasks-related code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
KSyms.h Kernel: Add ability to dump backtrace from provided frame pointer 2022-10-01 14:09:01 +02:00
mkmap.sh Kernel: Use the toolchain's nm in mkmap.sh 2021-12-30 18:10:51 +01:00
SanCov.cpp Kernel: Move all tasks-related code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
Sections.h Kernel: Move all boot-related code to the new Boot subdirectory 2023-06-04 21:32:34 +02:00
UnixTypes.h Kernel: Add support for SA_SIGINFO 2022-03-04 20:07:05 +01:00