261 Commits

Author SHA1 Message Date
Albin Kerouanton
28afa75c39 d/libnet: TestIptabler: move golden files into subdir
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2025-08-25 10:48:41 +02:00
Albin Kerouanton
ce4a331287 d/libnet: TestNftabler: move golden files into subdir
Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2025-08-25 10:48:21 +02:00
Albin Kerouanton
18651d8819 Merge pull request #50691 from akerouanton/portallocator-socket-filter
libnet/pa: OSAllocator: retry allocations
2025-08-21 11:50:57 +02:00
Cory Snider
f445d26d68 Merge pull request #50724 from corhere/libn/bitmap-onescount
daemon/libnetwork/bitmap: add OnesCount method
2025-08-20 17:45:18 -04:00
Albin Kerouanton
14262696d7 libnet/pa: OSAllocator: retry allocations
Previous commit changed the OSAllocator to listen after binding a port,
such that we're 100% sure that the port is free. We can now make the
OSAllocator responsible for retrying port allocations when it tries to
find an ephemeral port, or a free port in a range.

Move the retry logic from the 'nat' portmapper to the OSAllocator.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2025-08-20 12:02:34 +02:00
Albin Kerouanton
201968cc03 libnet/pa: OSAllocator: listen after bind
Move the listen syscall to the `OSAllocator` such that when
`RequestPortsInRange` returns, callers are guaranteed that the allocated
port isn't used by another process.

Bind and listen syscalls were previously split because listening before
inserting DNAT rules could cause connections to be accepted by the
kernel, so packets would never be forwarded to the container.

But, pulling them apart has an undesirable drawback: if another process
is racing against the Engine, and starts listening on the same port,
the conflict wouldn't be detected until OSAllocator's callers issue a
'listen' syscall. This means that callers need to implement their own
retry logic.

To overcome both drawbacks, set a cBPF socket filter on the socket
before it's bound, and let callers call `DetachSocketFilter` to remove
it. Now, callers are guaranteed that the port is free to use, and no
connections will be accepted prematurely.

For TCP / SCTP clients, this means that they'll send the first handshake
packet (e.g. SYN), but the kernel won't reply (e.g. SYN-ACK), and they
will retry until DNAT rules are configured or the socket filter is
removed.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2025-08-20 12:02:04 +02:00
Sebastiaan van Stijn
48557f72f9 daemon/libnetwork: TestUserChain: rename golden files
- Use a subdirectory for all files used in the test
- Add a .golden file-extension for easier discovery of generated files

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-16 15:18:22 +02:00
Cory Snider
b68ff62ab7 daemon/libnetwork/bitmap: add OnesCount method
Signed-off-by: Cory Snider <csnider@mirantis.com>
2025-08-14 12:17:45 -04:00
Cory Snider
56c48e8708 d/libn/bitmap: tidy up constants, branchy math
The only references to blockLen type-assert the uint32 constant to other
widths. Make it an untyped int to cut down on unnecessary boilerplate.

Rewrite the genNumBlocks utility function to use the well-known
algorithm for rounding-up integer division instead of branching. Inline
it into the only call site.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2025-08-13 14:45:13 -04:00
Cory Snider
a27a2901b1 Merge pull request #50660 from corhere/libn/overlay-eventnotify-nilderef
libn/d/overlay: don't deref nil PeerRecord on error
2025-08-12 11:51:44 -04:00
Sebastiaan van Stijn
6505e8d605 daemon/libnetwork/types: rename StaticRoute.GetCopy to Copy
Just for consistency; it was the last `GetCopy` remaining.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:48:24 +02:00
Sebastiaan van Stijn
1e11e64c9c daemon/libnetwork/types: remove TransportPort.Equal()
The `TransporPort` type is comparable; it doesn't have fields that
require special handling. It's defined as;

    // TransportPort represents a local Layer 4 endpoint
    type TransportPort struct {
        Proto Protocol
        Port  uint16
    }

where `Protocol` is an int (with a stringer interface);

    type Protocol uint8

So we can remove the `Equal` method, and simplify places where it's
compared.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:39:56 +02:00
Sebastiaan van Stijn
561e14ea3f daemon/libnetwork/types: remove TransportPort.GetCopy()
The `GetCopy()` function doesn't de-reference anything, as it's
all a straight copy. We can remove it as it's only making things
more complicated than needed.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:39:56 +02:00
Sebastiaan van Stijn
385297ee40 daemon/libnetwork/types: remove unused IsIPNetValid utility
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:39:55 +02:00
Sebastiaan van Stijn
7960f742a2 daemon/libnetwork: Endpoint.CopyTo: use maps/slices.Clone
Modernize using maps.Clone, slices.Clone. This method is needed to
satisfy the datastore.KVObject interface, so also assert it does.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:39:55 +02:00
Sebastiaan van Stijn
a82a68a445 daemon/libnetwork: replace endpointJoinInfo.CopyTo with Copy()
The endpointJoinInfo.CopyTo function expected the caller to construct an
EndpointInterface to copy to, but all callsites created an empty struct.
In addition, `CopyTo` would never return an error, so the error return
was redundant.

Replace it with a `Copy()` function, which makes it easier to
consume.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:39:55 +02:00
Sebastiaan van Stijn
2d5100749d daemon/libnetwork: replace EndpointInterface.CopyTo with Copy()
The EndpointInterface.CopyTo function expected the caller to construct an
EndpointInterface to copy to, but all callsites created an empty struct.
In addition, `CopyTo` would never return an error, so the error return
was redundant.

Replace it with a `Copy()` function, which makes it easier to
consume.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:39:48 +02:00
Sebastiaan van Stijn
80452e5d4a daemon/libnetwork/types: PortBinding.Equal: use non-pointer receiver
Change `PortBinding.Equal` to use a value receiver and parameter, this
allows us to use it directly with `slices.IndexFunc`, `DeleteFunc`,
without having to add a wrapper func.

The only exception currently is the `UnmapPorts` function (stub), which
takes portmapperapi.PortBinding as argument; the portmapperapi.PortBinding
type embeds `types.PortBinding`, and it's the only field that's compared
as part of `UnmapPorts`

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:38:09 +02:00
Sebastiaan van Stijn
7d5312ab56 daemon/libnetwork/types: rename PortBinding.GetCopy to Copy and non-pointer
- Rename `PortBinding.GetCopy()` to `PortBinding.Copy()`, which is more
  idiomatic, and aligns with other similar methods.
- Change it to a non-pointer receiver; `Copy` does not mutate state, and
  the type should still be reasonably small.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:38:09 +02:00
Sebastiaan van Stijn
e387dc977e daemon/libnetwork/types: cleanup GetIPNetCopy, GetIPNetCanonical
Rewrite both to use slices.Clone, and GetIPNetCanonical to not depend
on GetIPNetCopy. GetIPNetCopy only has a single consumer, so we should
consider moving it local to where it's used.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:38:09 +02:00
Sebastiaan van Stijn
68a94ecbb5 daemon/libnetwork/types: remove GetIPCopy; use slices.Clone
We can replace this utility with slices.Clone, which provides the
same functionality.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:38:09 +02:00
Sebastiaan van Stijn
115b801a3b daemon/libnetwork/types: remove GetMacCopy; use slices.Clone
We can replace this utility with slices.Clone, which provides the
same functionality.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:38:09 +02:00
Sebastiaan van Stijn
cfad3ed6b9 daemon/libnetwork: replace IpamInfo.CopyTo with IpamInfo.Copy()
The IpamInfo.CopyTo function expected the caller to construct an
IpamInfo to copy to, but all callsites created an empty struct.
In addition, `CopyTo` would never return an error, so the error
return was redundant.

Replace it with a `Copy()` function, which makes it easier to
consume.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:38:09 +02:00
Sebastiaan van Stijn
2f74f245b6 daemon/libnetwork: replace IpamConf.CopyTo with IpamConf.Copy()
The IpamConf.CopyTo function expected the caller to construct an
IpamConf to copy to, but all callsites created an empty struct.
In addition, `CopyTo` would never return an error, so the error
return was redundant.

Replace it with a `Copy()` function, which makes it easier to
consume.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 11:38:08 +02:00
Sebastiaan van Stijn
796a4ce952 daemon/libnet/drv/bridge: stubPortMapper.UnmapPorts: fix slices.Delete
This stub was introduced in 4e246efcd1,
which currently is only in the master branch.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-12 09:27:29 +02:00
Sebastiaan van Stijn
162e6fae53 Merge pull request #50688 from thaJeztah/remove_errdefs_aliases
daemon/libnetwork/types: remove errdefs aliases
2025-08-11 19:01:55 +02:00
Sebastiaan van Stijn
79ca608d15 Merge pull request #50685 from thaJeztah/windows_cleanup_err
daemon/libnet/drivers/windows: small cleanup in error-handling
2025-08-11 17:53:27 +02:00
Rob Murray
2d0bc97eb9 Merge pull request #50646 from robmry/nftables_no_enable_ip_forwarding
nftables: never enable IP forwarding on the host
2025-08-11 09:44:00 +01:00
Sebastiaan van Stijn
0a89d98bad daemon/libnetwork/types: remove errdefs aliases
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-10 20:18:02 +02:00
Sebastiaan van Stijn
892ebd2f17 daemon/libnet/drivers/windows: small cleanup in error-handling
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-10 16:43:36 +02:00
Rob Murray
4b97831992 Merge pull request #50671 from mmorel-35/github.com/hashicorp/go-multierror
chore: use errors.Join instead of github.com/hashicorp/go-multierror
2025-08-08 19:40:36 +01:00
Rob Murray
f802d8a08e When cleaning iptables rules, warn on filter-FORWARD DROP
Signed-off-by: Rob Murray <rob.murray@docker.com>
2025-08-08 18:43:35 +01:00
Rob Murray
67ffa47090 nftables: don't enable IP forwarding
For nftables only, never enable IP forwarding on the host. Instead,
return an error on network creation if forwarding is not enabled,
required by a bridge network, and --ip-forward=true.

If IPv4 forwarding is not enabled when the daemon is started with
nftables enabled and other config at defaults, the daemon will
exit when it tries to create the default bridge.

Otherwise, network creation will fail with an error if IPv4/IPv6
forwarding is not enabled when a network is created with IPv4/IPv6.

It's the user's responsibility to configure and secure their host
when they run Docker with nftables.

Signed-off-by: Rob Murray <rob.murray@docker.com>
2025-08-08 18:43:35 +01:00
Rob Murray
7dfeee8460 Drop FirewallCleanerSetter interface
Signed-off-by: Rob Murray <rob.murray@docker.com>
2025-08-08 18:43:35 +01:00
Rob Murray
200a75b34a Return concrete types from NewNftabler/NewIptabler
Signed-off-by: Rob Murray <rob.murray@docker.com>
2025-08-08 18:43:35 +01:00
Matthieu MOREL
050fbbccca chore: use errors.Join instead of github.com/hashicorp/go-multierror
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-08-08 19:19:21 +02:00
Matthieu MOREL
96f8c6395e chore: enable use-any rule from revive
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2025-08-08 17:07:07 +02:00
Albin Kerouanton
c6be4ad999 libnet/pa: don't set SO_REUSEADDR on UDP sockets
The userland proxy uses unconnected UDP sockets to receive packets from
anywhere, so enabling SO_REUSEADDR means that multiple sockets can bind
the same port. This defeats the purpose of the portallocator, which is
supposed to ensure that the port is free and not already in use (either
by us, or by another process). So, do not enable SO_REUSEADDR for UDP
sockets.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2025-08-08 13:46:04 +02:00
Cory Snider
12c6345d3a libn/d/overlay: don't deref nil PeerRecord on error
If unmarshaling the peer record fails, there is no need to check if it's
a record for a local peer. Attempting to do so anyway will result in a
nil-dereference panic. Don't do that.

The Windows overlay driver has a typo: prevPeer is being checked twice
for whether it was a local-peer record. Check prevPeer once and newPeer
once each, as intended.

Signed-off-by: Cory Snider <csnider@mirantis.com>
2025-08-07 13:25:37 -04:00
Sebastiaan van Stijn
78ccee32b3 update various "doc" links in code
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-05 20:49:57 +02:00
Sebastiaan van Stijn
77c2e3279d daemon/libnetwork: Sandbox.EnableService/DisableService slight cleanup
- remove/rename named error-return
- remove redundant defer
- use "continue" to reduce nesting
- use structured logs

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-05 11:56:00 +02:00
Rob Murray
b789a8ca96 Merge pull request #50600 from thaJeztah/DNSConfig_netip
daemon/config: make DNSConfig.DNS a netip.Addr
2025-08-04 18:20:59 +01:00
Albin Kerouanton
778d8e3a26 libnet/d/bridge: handleFirewalldReloadNw: fix deadlock
handleFirewalldReloadNw locks `d.mu` and then `d.configNetworks`.
However, the rest of the driver locks `d.configNetworks` first and then
`d.mu`.

This could result in deadlocks if `handleFirewalldReloadNw` is called
while the bridge driver is already holding `d.configNetworks` lock.

Other code paths were checked to ensure that they all follow the same
locking order.

This bug was introduced by commit a527e5a.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2025-08-04 12:04:47 +02:00
Albin Kerouanton
519adb26c3 libnet/d/bridge: driver: un-embed mutex
The bridge driver was embedding `sync.Mutex` which is unconventional and
makes it harder to analyze locks ordering.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
2025-08-04 11:32:34 +02:00
Sebastiaan van Stijn
1205a15db1 libnetwork/types: define RouteType type
Define a `RouteType` type, type the related consts, and update the
`JoinInfo.AddStaticRoute` signature in the interface.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-01 22:33:10 +02:00
Sebastiaan van Stijn
6aab881534 libnetwork/types: make Protocol consts strong-typed
These use the Linux-specific values as convention, so unfortunately,
the syscall package doesn't define consts for these on Windows, so
keeping our own definition (values are not really relevant here).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-01 22:33:08 +02:00
Sebastiaan van Stijn
ec5dbaf233 libnetwork/types: define IPFamily options using syscall.AF_XXX consts
Use the consts defined in syscall that basically match our intent here.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-01 22:33:03 +02:00
Sebastiaan van Stijn
95eeb0b3b0 libnetwork/types: define IPFamily type for IP-family consts
Define a type to help discovery, and update the signatures of
`ResolveName`, `Network.ResolveName`, and `Sandbox.ResolveName`
accordingly.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-01 22:32:57 +02:00
Sebastiaan van Stijn
5365f08ae2 daemon/config: make DNSConfig.DNS a netip.Addr
Modernize the field and allow using it as-is in some places, or
convert it to a string (which won't produce an error down the line).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-01 18:48:10 +02:00
Sebastiaan van Stijn
0156a42200 daemon/libnetwork/resolvconf: remove unused code
This package is a wrapper for the libnetwork/internal/resolvconf package,
which is a modernized, more performant rewrite of the original parsing
code.

The libnetwork/resolvconf package was still maintained because it was
used by BuildKit, but since [moby/buildkit@3d43066], BuildKit maintains
its own copy of the internal package.

The only remaining uses of this package was as part of some tests (which
would also benefit of using the internal pacakge's implementation directly),
and a _single_ use of `resolvconf.Path` in the daemon, which cannot use
the internal package currently because it's internal to libnetwork.

This patch:

- Removes all functions that were not used.
- Rewrites some tests in libnetwork to use the internal/resolvconf package
  directly, instead of depending on the wrapper.
- Add TODOs to consider moving the "Path" function separate (which could
  be in daemon/config if we consider it to be the default for the daemon's
  resolvconf path configuration).

[moby/buildkit@3d43066]: 3d43066f2e

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-08-01 14:48:05 +02:00