← Writing

One dead uplink, seven outages: a WireGuard rule-priority race

28 October 2026

Context

  • The media node reaches the internet only through a full-tunnel WireGuard VPN; it is also a k3s node running Cilium.
  • Its applications kept their own error messages (“all indexers unavailable”, timeouts), so the first hour went into blaming the remote sites.

Symptom

  • Every outbound request from the media node returned 000, including control targets that block nobody (example.com, registry.npmjs.org, api.telegram.org). Egress was dead; no site was blocking it.
  • wg show reported a recent handshake, but no traffic came back.
  • Seven outages in three days, each after a deploy that restarted the tunnel. A manual systemctl restart needed a power-cycle: ARP answered, nothing above layer 2.

Causes

  1. Stale private key after a rolled-back deploy. WireGuard handshakes with a mismatched key, then blackholes the payload. Four of the seven outages.
  2. Rule-priority race. The tunnel’s ip rules landed above local, so the node stopped answering its own address.
  3. ::/0 in allowedIPs on a peer without IPv6: a second blackhole, for v6 traffic.
healthy: tunnel up before Ciliumstranded: tunnel up after Cilium0local32764main, suppress /032765not fwmark → tunnel table32766main9fwmark → 2004 (Cilium)98main, suppress /099not fwmark → tunnel table100local (too late)packet to its own LAN address → local → deliveredpacket to its own LAN address → main → the /24→ sent out of the NIC; the node stops answering itselffix: pin the two tunnel rules at pref 32765 and 32764
Same interface, same routes. Only the priority numbers differ, and the kernel chose them.

Why 98 and 99

  • wg-quick adds its rules without pref. The kernel’s fib_default_rule_pref() then uses the priority of the second rule in the list, minus one.
  • Cilium’s rule sits at 9 and local at 100, so the kernel picks 99 and 98: above local. If the tunnel comes up before Cilium, it gets 32765 and works. On boot this is a race; on a running node a restart loses every time.
  • Reproduced on a staging KVM guest (same Cilium image, a tunnel to a dead endpoint): 3 strands in about 21 cold boots.
  • Same guest, same tables, seconds apart: explicit pref reachable 10/10, no pref 0/10.

Fix

ip -4 rule add pref 32765 not fwmark 51820 table 51820
ip -4 rule add pref 32764 table main suppress_prefixlength 0
  • nix flake check fails if either pref is removed or a wg-quick interface returns.
  • A reconciler converges the live tunnel with wg set; it never restarts it.
  • --node-ip and --advertise-address are pinned. With the tunnel as default route, k3s had advertised 10.2.0.2, and etcd could not rejoin.
  • Result on 2026-10-28: 3/3 restarts, rules at 32764/32765, a fresh handshake each time.

Monitoring added

  • A 30-second timer writes handshake age and transfer counters for node-exporter’s textfile collector.
  • Alerts: WireGuardNoHandshake (over 5 minutes), WireGuardHandshakeStale (over 3 minutes for 10 minutes), WireGuardMetricsMissing.
  • The metrics were absent at first: the file was 0600 and node-exporter ran as nobody. Only the “missing” alert fired.

What I took from it

  • The lock was the bug. “Never restart the tunnel” was written down as a rule; it described the symptom and hid the defect behind a habit. The two pref values are what actually keep the node up.
  • Boot is part of the acceptance test. Two more bugs only appeared after a power cut: the endpoint route was pinned before the NIC had an address, and k3s advertised the tunnel’s address to etcd.
  • Prove egress before blaming a site. One curl loop with control targets is the first line of the runbook page now.