Skip to content

Networks: gateway_mode_ipv4=isolated leaves the host off an internal network - #1010

Open
masihmoloodian wants to merge 1 commit into
libracoder:mainfrom
masihmoloodian:feat/gateway-mode-isolated
Open

masihmoloodian wants to merge 1 commit into
libracoder:mainfrom
masihmoloodian:feat/gateway-mode-isolated

Conversation

@masihmoloodian

Copy link
Copy Markdown
Contributor

What this changes and why

An --internal network's bridge still has the gateway address, so its containers reach every service listening on all of the host's addresses through it (#1001): a database, a dev server, an admin endpoint. Docker 28 closes that path with -o com.docker.network.bridge.gateway_mode_ipv4=isolated on an internal network, and inhibit_ipv4=true does the same there. Kiri accepted both and did nothing with them:

$ docker run -d --network host busybox:1.36 httpd -f -p 18997 -h /tmp      # a server on every host address
$ docker network create --internal -o com.docker.network.bridge.gateway_mode_ipv4=isolated isonet
$ docker run --rm --network isonet alpine sh -c 'nc -z -w 3 172.20.0.1 18997 && echo reached'
reached                       # main

In the daemon (daemon/network.rs, daemon/engine.rs):

  • network create checks the gateway options as Docker's bridge driver does (parseNetworkOptions, newGwMode), with its messages:
    • a mode must be nat, nat-unprotected, routed or isolated, else failed to parse … (unknown gateway mode …);
    • inhibit_ipv4 must be a boolean;
    • isolated is refused unless the network is internal, with gateway mode 'isolated' can only be used for an internal network.
  • The modes other than isolated work as before.
  • On an internal network with gateway_mode_ipv4=isolated or inhibit_ipv4=true, each container that starts on it, or joins it with network connect, has nogw4=1 among its network options for the guest.
  • gateway_mode_ipv6=isolated on a network with IPv6 is refused for now, rather than accepted with the bridge's IPv6 address left in place. On an IPv4-only network there is nothing for it to do, so it's accepted.

In the guest (kiri-init/src/linux/net.rs, container.rs, agent.rs), with nogw4:

  • The bridge is made without its IPv4 address, so the host has none on the network.
  • Containers keep their own addresses, and still reach each other, by name too. Kiri's resolver can't listen on a gateway that isn't there, so containers ask it at 127.0.0.11 in their own network, as they already do when the gateway won't take the listener (WSL 2 Linux host: user-defined bridge networks fail at gateway DNS bind (EINVAL) #477).
  • No IPv4 default route is given by that network (an internal one never had one).

This works the same on a Linux host and in the Mac's VM, where the same guest code makes the bridges. inhibit_ipv4 on a network that isn't internal is kept as before: Docker then leaves the bridge to be set up by hand, which Kiri doesn't do.

One difference from Docker remains: network inspect still shows the subnet's first address as the gateway in IPAM.Config, where Docker shows none.

Fixes #1001

How you tested it

On this branch, the same steps:

$ docker run --rm --network plainint alpine sh -c 'nc -z -w 3 172.19.0.1 18997 && echo reached || echo unreached'
reached                       # a plain internal network, as before
$ docker run --rm --network isonet alpine sh -c 'nc -z -w 3 172.20.0.1 18997 && echo reached || echo unreached'
unreached
$ docker run --rm --network isonet alpine wget -qO- -T 5 http://isoweb | grep -m1 '<title>'
<title>Welcome to nginx!</title>
$ docker run --rm --network isonet alpine grep nameserver /etc/resolv.conf
nameserver 127.0.0.11
$ docker network create -o com.docker.network.bridge.gateway_mode_ipv4=isolated isoext
Error response from daemon: gateway mode 'isolated' can only be used for an internal network

On a Linux host (tests/linux-host.sh), the gateway's address is on none of the host's interfaces with the option, and a server on every host address isn't reached through the gateway.

Tests:

  • cargo test -p kiri: 1054 passed. New test: an_isolated_gateway_is_for_internal_networks_only. It covers the options that isolate, the ones that don't, Docker's refusals and their messages, and the IPv6 case.

  • cargo test -p kiri-init (in a Linux container): 283 passed, 4 failed. The 4 fail on main too, because the container may not mknod, mount or redirect: whiteout_becomes_char_device, an_overlay_of_more_layers_than_one_page_names, an_imported_writable_layer_becomes_the_upper_dir_whole and egress_redirect_relays_vpn_tcp_with_original_dst_and_exempts_bridge_peers. New test: an_isolated_bridge_has_no_ipv4_gateway.

  • tests/e2e.sh (macOS): 7 new checks. Three of them fail on a main build and pass here:

    • isolated-host-unreached
    • isolated-connect-unreached, through network connect
    • isolated-needs-internal

    The others pass on both: isolated-plain-internal-reaches-host (the same server reached without the option, so the check sees a real listener), isolated-names, isolated-resolver and isolated-option-shown. The internal-network checks around them pass.

    The full suite: 945 passed, 19 failed, 1 skipped.

    • 18 failures are checks that fail on main on this Mac too:
      • Ten are DNS checks, and this Mac's VPN answers every DNS query: default-bridge-no-names, dns-failed-start-gone, disconnect-dns-gone, network-recreate-same-subnet-old-name-gone, dns-user-net-three-silent-servfail-in-time and the five rename-dns-* checks.
      • The other eight are about bind mounts, the overlay's birth times, loop-mounted images, network disconnect and Rosetta's vDSO.
    • The 19th, guest-kernel-nfs-dmcrypt, is my older guest kernel.
  • tests/linux-host.sh: 6 new checks. On a main build, isolated-host-unreached and isolated-bridge-no-ipv4 fail; here all pass. The suite: 239 passed, 1 failed. failed-start-no-dns fails on main too, because this Mac's VPN answers every DNS query. On one earlier run fwdrop-rules and fwdrop-ufw-rules failed too, and passed on the next run.

  • cargo clippy: no warnings in the changed lines.

Tested on: macOS 27.2.1, Apple M4 Pro, and the Linux host build in tests/linux-host.sh. Windows (WSL) runs the same Linux engine.

How you found it

  • I use Kiri and ran into this myself
  • I found it by reading the code or comparing with Docker
  • Someone else reported it (link the issue above)

AI tools used to write this change:

Checklist

  • I have at most 3 pull requests open on Kiri, this one included
  • cargo fmt, cargo clippy and the tests pass
  • It works on macOS, Windows and Linux, or the change only touches one of them

…network

An internal network's bridge still had the gateway address, so its
containers reached every service listening on all of the host's
addresses through it (libracoder#1001): a database, a dev server, an admin
endpoint. Docker 28 closes that path with
`-o com.docker.network.bridge.gateway_mode_ipv4=isolated` on an internal
network, and `inhibit_ipv4=true` does the same there. Kiri accepted both
and did nothing with them.

- With either on an internal network, the bridge gets no IPv4 address, so
  the host has none on the network. Containers keep their own addresses
  and reach each other, by name too: with nothing listening on the
  gateway, they ask Kiri's resolver at 127.0.0.11 in their own network.
  The same holds for a container that joins with `network connect`, on a
  Linux host and in the Mac's VM.
- The gateway options are checked as Docker checks them: a known mode
  (`nat`, `nat-unprotected`, `routed`, `isolated`), a boolean
  `inhibit_ipv4`, and `isolated` only on an internal network, with
  Docker's messages. The modes other than `isolated` work as before.
- `gateway_mode_ipv6=isolated` on a network with IPv6 is refused for now,
  rather than accepted with the bridge's IPv6 address left in place.

Fixes libracoder#1001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support com.docker.network.bridge.gateway_mode_ipv4=isolated (or inhibit_ipv4) on internal networks

1 participant