1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 16:18:12 +00:00
serenity/Kernel/Net
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
..
Intel Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
Realtek Everywhere: Move global Kernel pattern code to Kernel/Library directory 2023-06-04 21:32:34 +02:00
VirtIO Kernel: Add an initial implementation of virtio-net driver 2023-07-11 00:49:11 -06:00
ARP.h Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
EthernetFrameHeader.h Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
EtherType.h Everything: Move to SPDX license identifiers in all files. 2021-04-22 11:22:27 +02:00
ICMP.h Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
IPv4.h Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
IPv4Socket.cpp Kernel/Net: Rework ephemeral port allocation 2023-07-29 16:51:58 -06:00
IPv4Socket.h Kernel/Net: Rework ephemeral port allocation 2023-07-29 16:51:58 -06:00
IPv4SocketTuple.h Everywhere: Remove needless trailing semi-colons after functions 2023-07-08 10:32:56 +01:00
LocalSocket.cpp Everywhere: Move global Kernel pattern code to Kernel/Library directory 2023-06-04 21:32:34 +02:00
LocalSocket.h Everywhere: Move global Kernel pattern code to Kernel/Library directory 2023-06-04 21:32:34 +02:00
LoopbackAdapter.cpp Kernel: Put loopback adapter debug spam behind a flag 2023-06-18 08:50:33 +01:00
LoopbackAdapter.h Kernel/Net: Make the LoopbackAdapter initializer to use ErrorOr pattern 2023-04-14 19:27:56 +02:00
NetworkAdapter.cpp Kernel: Move InterruptDisabler to the Interrupts subdirectory 2023-06-04 21:32:34 +02:00
NetworkAdapter.h Everywhere: Move global Kernel pattern code to Kernel/Library directory 2023-06-04 21:32:34 +02:00
NetworkingManagement.cpp Kernel: Add an initial implementation of virtio-net driver 2023-07-11 00:49:11 -06:00
NetworkingManagement.h Kernel/Net: Iron out the locking mechanism across the subsystem 2023-04-14 19:27:56 +02:00
NetworkTask.cpp Kernel/Tasks: Allow Kernel processes to be shut down 2023-07-15 00:12:01 +02:00
NetworkTask.h Kernel: Avoid deadlock when trying to send packets from the NetworkTask 2021-04-30 23:11:56 +02:00
Routing.cpp Everywhere: Run clang-format 2023-07-08 10:32:56 +01:00
Routing.h Kernel: Move all tasks-related code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
Socket.cpp Kernel: Move all tasks-related code to the Tasks subdirectory 2023-06-04 21:32:34 +02:00
Socket.h Kernel: Use UnixDateTime wherever applicable 2023-05-24 23:18:07 +02:00
TCP.h Everywhere: Remove unused includes of AK/StdLibExtras.h 2023-01-02 20:27:20 -05:00
TCPSocket.cpp Kernel/Net: Rework ephemeral port allocation 2023-07-29 16:51:58 -06:00
TCPSocket.h Kernel/Net: Rework ephemeral port allocation 2023-07-29 16:51:58 -06:00
UDP.h Everywhere: Run clang-format 2022-04-01 21:24:45 +01:00
UDPSocket.cpp Kernel/Net: Rework ephemeral port allocation 2023-07-29 16:51:58 -06:00
UDPSocket.h Kernel/Net: Rework ephemeral port allocation 2023-07-29 16:51:58 -06:00