Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danieljarolim/1865862f474b75c8e81b799d23285932 to your computer and use it in GitHub Desktop.
Save danieljarolim/1865862f474b75c8e81b799d23285932 to your computer and use it in GitHub Desktop.
OpenWrt route single subnet through WireGuard interface

This is a manual workaround because vpn-policy-routing has intermitent issues and is causing constant dropouts.

This relies on hotplug.d to update the routing table any time the wireguard tunnel resets or reconnects. Anything on the WG_ROUTED_SUBNET will go through the WireGuard interface.

Below: wg is the name given to the wireguard interface setup in luci wg is also the routing table name assigned to 202 WG_ROUTED_SUBNET will get routed through the wg interface

cat << "EOF" > /etc/hotplug.d/iface/99-ifup-wg
#!/bin/sh
[ "$ACTION" = ifup -a "$INTERFACE" = wg ] || exit 0

WG_ROUTED_SUBNET='192.168.1.64/27'
WG_GATEWAY=$(ip -f inet address show dev wg scope global | awk '/inet / {split($2,var,"/"); print var[1]}')

logger -t "/etc/hotplug.d/iface/99-ifup-wg" "Adding default route for $WG_ROUTED_SUBNET via $WG_GATEWAY to wg interface due to $ACTION of $INTERFACE ($DEVICE)"

grep -qxF '202     wg' /etc/iproute2/rt_tables || echo '202     wg' >> /etc/iproute2/rt_tables
ip route add default via $WG_GATEWAY dev wg table wg
ip rule add from $WG_ROUTED_SUBNET table wg priority 1000
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment