wg-quick fails with RTNETLINK: Invalid argument
frank.martino1
frank.martino1 at protonmail.com
Wed Sep 30 17:04:42 UTC 2026
### Description of the Bug
When switching WireGuard servers using `wg-quick` on a configuration file that specifies `Table = off`, the initialization fails during the IP address assignment phase, throwing the following error:
`RTNETLINK answers: Invalid argument`
### Steps to Reproduce
1. Create two WireGuard configuration files (`serverA.conf` and `serverB.conf`) with `Table = off` and different `Endpoint` IPs.
2. Connect to the first server: `sudo wg-quick up serverA`
3. Implement manual host routes to the endpoint via your physical gateway.
4. Switch to the second server in a single sequence:
```bash
sudo wg-quick down serverA && sudo wg-quick up serverB
```
5. Observe the `RTNETLINK answers: Invalid argument` failure printing out during the execution trace of `serverB`.
### Expected Behavior
`wg-quick` should cleanly tear down all interface states from the previous execution, allowing the kernel network namespace to settle so that the newly spawned interface can bind its IP address and configuration parameters successfully on the first attempt.
### Actual Behavior / Execution Trace
The script wrapper executes its hardcoded command sequence:
1. `ip link add wg0 type wireguard`
2. `wg setconf wg0 /dev/fd/63`
3. `ip -4 address add <ip>/32 dev wg0` <-- **CRASHES HERE with EINVAL**
### Root Cause Analysis
This bug is a timing and state synchronization race condition between `iproute2`, the `wireguard` kernel module, and Netfilter:
1. **Asynchronous Deletion Delay:** When `wg-quick down` deletes the virtual device link, the namespace unregistration and hook flushes happen asynchronously in the background.
2. **Socket Cache Pollution:** Because `wg-quick up` instantly creates a new interface container (`add_if`) and populates keys/endpoints (`set_config`), the module instantiates UDP sockets while the background cleanup is still running. The new socket structure inherits a dirty socket destination cache (`sk_dst_cache`) pointing to the *old* server IP.
3. **Netlink Veto:** When `wg-quick` subsequently tries to execute `ip address add`, the Netlink engine evaluates the active routing layout, detects an structural paradox (a new interface instance whose sockets are tied to an un-flushed legacy routing table index), and rejects the address assignment with `EINVAL`.
4. **Cascading Deadlock:** The failed state causes the kernel to loop handshake traffic destined for the old server into `OUT=wg0`, deadlocking local socket queues and freezing local DNS proxies.
### Environment & Configuration
* **OS / Kernel:** Linux Kernel (Stateful tracking via `nf_conntrack` enabled)
* **Tools:** `wg-quick` from `wireguard-tools`
* **Relevant Config Snippet:**
```ini
[Interface]
Address = 10.67.57.124/32
Table = off
```
### Workaround / Possible Fix
The failure can be avoided entirely by performing an explicit, native protocol routing flush **prior** to initializing the new interface container. Adding the following block to the teardown sequence clears the routing indices from live memory, ensuring Netlink encounters a pristine workspace:
```bash
# Force the kernel to drop sticky server routing mappings before regenerating the link
sudo ip route flush dev <physical_interface> proto boot
sudo ip route flush cache
```
More information about the WireGuard
mailing list