r/linuxquestions • u/DeerLow4961 • 13d ago
Does this killswitch look solid to you?
#!/usr/bin/nft -f
#
# WireGuard VPN kill switch (Arch Linux)
# Only loopback, the WireGuard interface and the VPN handshake/transport
# packets on the WAN are allowed. Static network config required (no DHCP).
#
# Check: sudo nft -c -f /etc/nftables.conf
# Enable: sudo nft -f /etc/nftables.conf
# Disable: sudo nft delete table inet killswitch
define WAN_IF = "wlp1s0" # physical interface
define WG_IF = "wg" # WireGuard interface
define VPN_IP = xxxxxxxxxxxxx # VPN server address
define VPN_PORT = 1637 # VPN server UDP port
define WG_LOCAL_PORT = 39110 # must match ListenPort in wg.conf
define FWD_PORT = 8538 # AirVPN forwarded port (TCP + UDP)
destroy table inet killswitch # clear old table, no error if missing
table inet killswitch {
chain input {
type filter hook input priority -10; policy drop; # drop by default
ct state invalid drop # bad packets
iifname "lo" accept # loopback
iifname $WG_IF ct state established,related accept # replies via VPN
iifname $WG_IF tcp dport $FWD_PORT accept # forwarded port, TCP
iifname $WG_IF udp dport $FWD_PORT accept # forwarded port, UDP
iifname $WAN_IF ip saddr $VPN_IP udp sport $VPN_PORT udp dport $WG_LOCAL_PORT accept # WireGuard from VPN server
}
chain output {
type filter hook output priority -10; policy drop; # drop by default
ct state invalid drop # bad packets
oifname "lo" accept # loopback
oifname $WG_IF accept # all host traffic via VPN
oifname $WAN_IF ip daddr $VPN_IP udp dport $VPN_PORT udp sport $WG_LOCAL_PORT accept # WireGuard to VPN server
}
chain forward {
type filter hook forward priority -10; policy drop; # host doesn't route
}
}
4
Upvotes
4
u/sidusnare Senior Systems Engineer 13d ago
wg-quick does this better with tagging.
This isn’t a kill switch, it’s isolation.
A kill switch would be something like a watchdog dropping all local links if a ‘www-data’ process spawns a UID 0 child process.
2
u/Open-Adhesiveness-86 12d ago
One change I'd make: instead of pinning the WAN rule to a literal VPN_IP, match the fwmark wg puts on its own encrypted packets (wg-quick defaults to 0xca6c), so
oifname $WAN_IF meta mark 0xca6c accept. Hardcoding the endpoint breaks the moment your provider's hostname resolves to a different A record or you roam to another server. Also add a forward chain with policy drop if you ever run docker or libvirt, since those insert their own iptables-nft rules and can push container traffic straight out the WAN past your table.1
u/wowsomuchempty 13d ago
I just use ufw..