From a394d4bf6b169971df272f216ced84fa0a909583 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sun, 1 Nov 2026 16:53:01 +0100 Subject: [PATCH] fix(svc-net-wireguard): dial every endpoint at the IPv4 address it resolves to The mesh is IPv4 only (60-byte header, IPv4 pools), yet wg.conf handed wg the endpoint name, and wg dials the first getaddrinfo answer, an AAAA on a dual-stack name. A host with broken IPv6 then never handshook (alphaserver, 2026-11-01). Resolve every dialled endpoint with getent ahostsv4 on the dialling host, render that literal, measure the path MTU against it, and fail before rendering when a name has no IPv4 address. Also set the tunnel MTU on a live interface, since wg syncconf drops it. Co-Authored-By: Claude Opus 5.5 --- roles/svc-net-wireguard/README.md | 13 ++++- .../files/test/lib/checks.sh | 5 +- .../files/test/lib/probes.sh | 4 ++ .../filter_plugins/mesh_peers.py | 9 +++- roles/svc-net-wireguard/tasks/00_core.yml | 30 +++++++++++ .../tasks/utils/interface_up.yml | 28 ++++++++++ roles/svc-net-wireguard/templates/wg.conf.j2 | 2 +- roles/svc-net-wireguard/vars/main.yml | 4 +- .../filter_plugins/test_mesh_peers.py | 9 +++- .../roles/svc-net-wireguard/test_rendering.py | 20 +++++-- .../python/utils/networks/test_mesh_peers.py | 53 +++++++++++++++++++ utils/networks/mesh.py | 23 ++++++++ 12 files changed, 188 insertions(+), 12 deletions(-) diff --git a/roles/svc-net-wireguard/README.md b/roles/svc-net-wireguard/README.md index b895580f50..fc4fb8946b 100644 --- a/roles/svc-net-wireguard/README.md +++ b/roles/svc-net-wireguard/README.md @@ -151,7 +151,18 @@ python3 -m cli.administration.inventory.mesh \ `networks.interfaces: [{name: wg0, host: , address:
}]`. - A host deployed with its own vault password takes `--host-vault-password-file =`, so its private key opens there. -- A mesh is IPv4 only. +- A mesh is IPv4 only, on the underlay as inside the tunnel. Each deploy + resolves every endpoint a host dials with `getent ahostsv4` on that host, + writes the first IPv4 address into `Endpoint`, measures the path MTU against + that address and subtracts the 60-byte IPv4 tunnel header. On an interface + that is already up, the deploy sets that MTU on the live link as well. An + endpoint name with both an A and an AAAA record therefore always dials over + IPv4. A name without an A record, an unreachable resolver, an IPv6 literal + endpoint or a host whose only IPv4 address is `127.0.0.1` fails the deploy + before the interface is rendered; pass the peer's IPv4 address as + `--endpoint` instead. A host with an IPv4 address but no IPv4 route to the + peer fails at the handshake gate. When the hub's address changes, redeploy + every spoke. Inspect a live interface on any member: diff --git a/roles/svc-net-wireguard/files/test/lib/checks.sh b/roles/svc-net-wireguard/files/test/lib/checks.sh index f9673ed84a..7b44f1a9b1 100644 --- a/roles/svc-net-wireguard/files/test/lib/checks.sh +++ b/roles/svc-net-wireguard/files/test/lib/checks.sh @@ -33,10 +33,11 @@ expect_reach() { } expect_roaming() { - local hub="$1" roamer="$2" mesh="$3" port="$4" + local hub="$1" roamer="$2" mesh="$3" port="$4" hub_ipv4 + hub_ipv4="$(underlay_ipv4 "${roamer}" "${hub}")" check "${hub} listens on ${mesh} port ${port}" conf_has "${hub}" "${mesh}" "^ListenPort = ${port}$" check "${hub} dials no spoke on ${mesh}" not conf_has "${hub}" "${mesh}" '^Endpoint' - check "${roamer} dials ${hub} on ${mesh}" conf_has "${roamer}" "${mesh}" "^Endpoint = ${hub}:${port}$" + check "${roamer} dials ${hub} at its IPv4 address ${hub_ipv4} on ${mesh}" conf_has "${roamer}" "${mesh}" "^Endpoint = ${hub_ipv4//./\\.}:${port}$" check "${roamer} keeps its path to ${hub} open" conf_has "${roamer}" "${mesh}" '^PersistentKeepalive = 25$' check "${roamer} does not listen on ${mesh}" not conf_has "${roamer}" "${mesh}" '^ListenPort' } diff --git a/roles/svc-net-wireguard/files/test/lib/probes.sh b/roles/svc-net-wireguard/files/test/lib/probes.sh index d1c360f26d..49bb209216 100644 --- a/roles/svc-net-wireguard/files/test/lib/probes.sh +++ b/roles/svc-net-wireguard/files/test/lib/probes.sh @@ -28,6 +28,10 @@ conf_has() { on "$1" grep -q -E "$3" "/etc/wireguard/$2.conf" } +underlay_ipv4() { + on "$1" getent ahostsv4 "$2" | awk 'NR == 1 { print $1 }' +} + pings() { on "$1" ping -c "${PING_COUNT}" -W 2 "$2" >/dev/null 2>&1 } diff --git a/roles/svc-net-wireguard/filter_plugins/mesh_peers.py b/roles/svc-net-wireguard/filter_plugins/mesh_peers.py index 6ecf05a037..bef36d5888 100644 --- a/roles/svc-net-wireguard/filter_plugins/mesh_peers.py +++ b/roles/svc-net-wireguard/filter_plugins/mesh_peers.py @@ -1,6 +1,12 @@ from __future__ import annotations -from utils.networks.mesh import awaited_keys, dialled_peers, handshaken, listens +from utils.networks.mesh import ( + awaited_keys, + dialled_peers, + handshaken, + listens, + underlay, +) class FilterModule: @@ -10,4 +16,5 @@ class FilterModule: "mesh_dialled_peers": dialled_peers, "mesh_handshaken": handshaken, "mesh_listens": listens, + "mesh_underlay": underlay, } diff --git a/roles/svc-net-wireguard/tasks/00_core.yml b/roles/svc-net-wireguard/tasks/00_core.yml index c99c74b524..f36b1a3211 100644 --- a/roles/svc-net-wireguard/tasks/00_core.yml +++ b/roles/svc-net-wireguard/tasks/00_core.yml @@ -31,6 +31,36 @@ package_name: "automtu==1.1.1" global_install: true +- name: "🔍 Resolve every endpoint this host dials to its IPv4 address" + ansible.builtin.command: + argv: + - getent + - ahostsv4 + - "{{ endpoint }}" + loop: "{{ WIREGUARD_DIALLED_ENDPOINTS }}" + loop_control: + loop_var: endpoint + register: wireguard_underlay_lookup + changed_when: false + failed_when: wireguard_underlay_lookup.rc not in [0, 2] + +- name: "🔧 Pin every dialled endpoint to the IPv4 address it resolved to" + ansible.builtin.set_fact: + wireguard_underlay: "{{ wireguard_underlay_lookup.results | mesh_underlay }}" + changed_when: false + +- name: "🛑 Fail when a dialled endpoint has no IPv4 address on this host" + ansible.builtin.fail: + msg: >- + {{ unresolved | join(', ') }} resolved to no IPv4 address on + {{ inventory_hostname }}. The mesh underlay is IPv4: give the name an A + record this host's resolver answers, give the host an IPv4 address, or + pass the peer's IPv4 address as --endpoint to + cli.administration.inventory.mesh. + vars: + unresolved: "{{ WIREGUARD_DIALLED_ENDPOINTS | reject('in', wireguard_underlay) | list }}" + when: unresolved | length > 0 + - name: "🔍 Detect the path MTU the tunnel has to fit inside" ansible.builtin.command: argv: diff --git a/roles/svc-net-wireguard/tasks/utils/interface_up.yml b/roles/svc-net-wireguard/tasks/utils/interface_up.yml index 4e1142bf32..dc9a323e39 100644 --- a/roles/svc-net-wireguard/tasks/utils/interface_up.yml +++ b/roles/svc-net-wireguard/tasks/utils/interface_up.yml @@ -20,3 +20,31 @@ register: wireguard_synced changed_when: wireguard_synced.rc == 0 when: wireguard_present.rc == 0 + +- name: "🔍 Read the link MTU of the live '{{ mesh_name }}'" + ansible.builtin.command: + argv: + - ip + - -j + - link + - show + - dev + - "{{ mesh_name }}" + register: wireguard_link + changed_when: false + when: wireguard_present.rc == 0 + +- name: "📏 Apply the tunnel MTU to the live '{{ mesh_name }}'" + ansible.builtin.command: + argv: + - ip + - link + - set + - dev + - "{{ mesh_name }}" + - mtu + - "{{ wireguard_mtu | int }}" + changed_when: true + when: + - wireguard_present.rc == 0 + - (wireguard_link.stdout | from_json | first).mtu | int != wireguard_mtu | int diff --git a/roles/svc-net-wireguard/templates/wg.conf.j2 b/roles/svc-net-wireguard/templates/wg.conf.j2 index 0e0151b191..0f905bcaf2 100644 --- a/roles/svc-net-wireguard/templates/wg.conf.j2 +++ b/roles/svc-net-wireguard/templates/wg.conf.j2 @@ -12,7 +12,7 @@ ListenPort = {{ mesh.listen_port | int }} PublicKey = {{ peer.public_key }} AllowedIPs = {{ peer.allowed_ips }} {% if peer.host in dialled %} -Endpoint = {{ peer.endpoint | default(peer.host, true) }}:{{ mesh.listen_port | int }} +Endpoint = {{ wireguard_underlay[peer.endpoint] }}:{{ mesh.listen_port | int }} PersistentKeepalive = 25 {% endif %} diff --git a/roles/svc-net-wireguard/vars/main.yml b/roles/svc-net-wireguard/vars/main.yml index a53abedd2b..ac89d8aa78 100644 --- a/roles/svc-net-wireguard/vars/main.yml +++ b/roles/svc-net-wireguard/vars/main.yml @@ -32,9 +32,11 @@ WIREGUARD_DIALLED_PEERS: >- WIREGUARD_EXPECTED_PEERS: "{{ WIREGUARD_DIALLED_PEERS | map(attribute='address') | list }}" +WIREGUARD_DIALLED_ENDPOINTS: "{{ WIREGUARD_DIALLED_PEERS | map(attribute='endpoint') | unique | list }}" + WIREGUARD_HEADER_BYTES: 60 WIREGUARD_VXLAN_HEADER_BYTES: 50 WIREGUARD_PMTU_TARGETS: >- - {{ (WIREGUARD_DIALLED_PEERS | map(attribute='endpoint') | list) + {{ (wireguard_underlay.values() | list) | default(NETWORK_PUBLIC_DNS_RESOLVERS, true) }} diff --git a/tests/unit/python/roles/svc-net-wireguard/filter_plugins/test_mesh_peers.py b/tests/unit/python/roles/svc-net-wireguard/filter_plugins/test_mesh_peers.py index 8f536b28bb..95066d0a0f 100644 --- a/tests/unit/python/roles/svc-net-wireguard/filter_plugins/test_mesh_peers.py +++ b/tests/unit/python/roles/svc-net-wireguard/filter_plugins/test_mesh_peers.py @@ -1,7 +1,13 @@ import importlib.util import unittest -from utils.networks.mesh import awaited_keys, dialled_peers, handshaken, listens +from utils.networks.mesh import ( + awaited_keys, + dialled_peers, + handshaken, + listens, + underlay, +) from . import PROJECT_ROOT @@ -26,6 +32,7 @@ class TestMeshPeersFilters(unittest.TestCase): "mesh_dialled_peers": dialled_peers, "mesh_handshaken": handshaken, "mesh_listens": listens, + "mesh_underlay": underlay, }, _filters(), ) diff --git a/tests/unit/python/roles/svc-net-wireguard/test_rendering.py b/tests/unit/python/roles/svc-net-wireguard/test_rendering.py index 3e5e1926e4..049d066aa7 100644 --- a/tests/unit/python/roles/svc-net-wireguard/test_rendering.py +++ b/tests/unit/python/roles/svc-net-wireguard/test_rendering.py @@ -70,6 +70,13 @@ FULL_MEMBER = { } +UNDERLAY = { + "vpn.example.org": "192.0.2.1", + "host-2": "192.0.2.12", + "host-3": "192.0.2.13", +} + + def _filters() -> dict: spec = importlib.util.spec_from_file_location( "mesh_peers_plugin", ROLE / "filter_plugins" / "mesh_peers.py" @@ -98,7 +105,10 @@ def render_interface(mesh: dict) -> str: ) env.filters.update(_filters()) return env.get_template("wg.conf.j2").render( - mesh=mesh, WIREGUARD_PRIVATE_KEY="private-key-stub", wireguard_mtu=1360 + mesh=mesh, + WIREGUARD_PRIVATE_KEY="private-key-stub", + wireguard_mtu=1360, + wireguard_underlay=UNDERLAY, ) @@ -138,8 +148,8 @@ class TestTheRoamingSpoke(unittest.TestCase): def test_it_does_not_listen(self): self.assertNotIn("ListenPort", self.rendered) - def test_it_dials_the_hub_on_the_mesh_port(self): - self.assertIn("Endpoint = vpn.example.org:51871", self.rendered) + def test_it_dials_the_hub_at_its_resolved_ipv4_address(self): + self.assertIn("Endpoint = 192.0.2.1:51871", self.rendered) def test_it_keeps_the_path_open_from_behind_nat(self): self.assertIn("PersistentKeepalive = 25", self.rendered) @@ -157,8 +167,8 @@ class TestTheFullMeshMember(unittest.TestCase): def test_it_dials_every_peer(self): peers = _peer_blocks(self.rendered) - self.assertIn("Endpoint = host-2:51900", peers["k2"]) - self.assertIn("Endpoint = host-3:51900", peers["k3"]) + self.assertIn("Endpoint = 192.0.2.12:51900", peers["k2"]) + self.assertIn("Endpoint = 192.0.2.13:51900", peers["k3"]) class TestTheGateWiring(unittest.TestCase): diff --git a/tests/unit/python/utils/networks/test_mesh_peers.py b/tests/unit/python/utils/networks/test_mesh_peers.py index 1a78e59bf0..e9066a81be 100644 --- a/tests/unit/python/utils/networks/test_mesh_peers.py +++ b/tests/unit/python/utils/networks/test_mesh_peers.py @@ -8,6 +8,7 @@ from utils.networks.mesh import ( handshaken, is_full, listens, + underlay, ) HUB_PEER = { @@ -112,6 +113,58 @@ class TestAwaitedKeys(unittest.TestCase): self.assertEqual({}, awaited_keys({"wg0": lonely}, swarm_manager=False)) +class TestUnderlay(unittest.TestCase): + DUAL_STACK = ( + "178.63.190.220 STREAM veen.world\n" + "178.63.190.220 DGRAM \n" + "178.63.190.220 RAW \n" + ) + + def test_a_dual_stack_name_dials_its_ipv4_address(self): + self.assertEqual( + {"veen.world": "178.63.190.220"}, + underlay([{"endpoint": "veen.world", "stdout": self.DUAL_STACK}]), + ) + + def test_the_first_of_several_a_records_wins(self): + state = "192.0.2.1 STREAM hub\n192.0.2.1 DGRAM \n192.0.2.2 STREAM \n" + self.assertEqual( + {"hub": "192.0.2.1"}, underlay([{"endpoint": "hub", "stdout": state}]) + ) + + def test_an_ipv4_literal_dials_itself(self): + state = "192.0.2.10 STREAM 192.0.2.10\n" + self.assertEqual( + {"192.0.2.10": "192.0.2.10"}, + underlay([{"endpoint": "192.0.2.10", "stdout": state}]), + ) + + def test_a_name_without_an_ipv4_answer_is_left_out(self): + self.assertEqual({}, underlay([{"endpoint": "v6only.example", "stdout": ""}])) + + def test_an_unparsable_answer_is_left_out(self): + state = " STREAM hub\n" + self.assertEqual({}, underlay([{"endpoint": "hub", "stdout": state}])) + + def test_an_ipv6_answer_never_becomes_an_endpoint(self): + state = "2a01:4f8:2201:4695::2 STREAM veen.world\n" + self.assertEqual({}, underlay([{"endpoint": "veen.world", "stdout": state}])) + + def test_every_endpoint_maps_to_its_own_address(self): + self.assertEqual( + {"mgr": "192.0.2.1", "nfs": "192.0.2.3"}, + underlay( + [ + {"endpoint": "mgr", "stdout": "192.0.2.1 STREAM mgr\n"}, + {"endpoint": "nfs", "stdout": "192.0.2.3 STREAM nfs\n"}, + ] + ), + ) + + def test_no_dialled_endpoint_resolves_to_nothing(self): + self.assertEqual({}, underlay([])) + + class TestHandshaken(unittest.TestCase): NOW = 1760000100 STATE = f"{NOW}\nkey-wrk\t1760000000\nkey-nfs\t0\nkey-mgr\t1760000005\n" diff --git a/utils/networks/mesh.py b/utils/networks/mesh.py index f3492bd60c..192c8231d4 100644 --- a/utils/networks/mesh.py +++ b/utils/networks/mesh.py @@ -1,5 +1,6 @@ from __future__ import annotations +import ipaddress from collections.abc import Iterable, Mapping from typing import Any @@ -128,6 +129,28 @@ def dialled_peers(interface: Mapping[str, Any]) -> list[Mapping[str, Any]]: return [peer for peer in peers if peer.get("is_hub")] +def underlay(lookups: Iterable[Mapping[str, Any]]) -> dict[str, str]: + """Endpoint name to the IPv4 address the dialling host resolved it to. + + Args: + lookups: one result per dialled endpoint, each holding the + ``endpoint`` it looked up and the ``stdout`` of + ``getent ahostsv4 `` on the dialling host; a name whose + first answer is no IPv4 address is left out. + """ + resolved: dict[str, str] = {} + for lookup in lookups: + fields = str(lookup["stdout"]).split() + if not fields: + continue + try: + address = ipaddress.IPv4Address(fields[0]) + except ValueError: + continue + resolved[str(lookup["endpoint"])] = str(address) + return resolved + + def awaited_peers( interface: Mapping[str, Any], swarm_manager: bool ) -> list[Mapping[str, Any]]: