m4nifest0-tech

Search the site

Networking

A VLAN that only exits through the VPN: WireGuard and policy routing on NethSecurity

Routing a single network segment through a commercial VPN with WireGuard on NethSecurity 8: dedicated zone, policy routing, kill switch and DNS checks.
Published on Updated on 8 min readLevel: Advanced

My homelab has a network segment dedicated to testing: throwaway virtual machines, software under evaluation, devices I do not fully trust. I wanted that traffic to reach the Internet only through a commercial VPN, without installing a client on every device and without touching the rest of the network, which must keep using the ISP connection. The whole solution lives on the firewall: a VLAN, a dedicated zone, a WireGuard tunnel and two policy routing rules with a kill switch.

Note

The addresses in this guide are examples taken from the ranges reserved for documentation: the main network is 192.0.2.0/24, the test segment 198.51.100.0/24, and the VLAN uses the example ID 60. RFC 1918 private networks only appear in the generic rules. Replace everything with your own values.

Goal and constraints

  • Only the test segment goes through the tunnel; the main network keeps behaving as before.
  • No VPN client on the devices: anything connected to the segment is already "inside".
  • If the tunnel goes down, the test segment loses Internet access instead of leaking out in clear through the ISP connection (kill switch).
  • The segment must not reach the main network; the main network can reach the segment to manage it.
  • No DNS leaks: name resolution requests from the segment must go to the provider's resolvers.

Layout

SegmentAddresses (example)Internet exit
Main network192.0.2.0/24ISP connection
Test segment (VLAN 60)198.51.100.0/24WireGuard tunnel to the provider only

The firewall is the only place where the segment is routed: the VLAN reaches the firewall over an 802.1Q trunk, and the firewall decides where the traffic leaves.

VLAN on the switch

The VLAN is created once on the managed switch. The port towards the firewall is a trunk: the main network stays untagged, the test VLAN is tagged. The ports where test devices connect (or the hypervisor NIC dedicated to the segment) are access ports on VLAN 60, untagged. The general reasoning about trunk and access ports is in Segmenting the network with VLANs.

Interface and zone on the firewall

On NethSecurity 8, create an 802.1Q VLAN sub-interface on the LAN interface facing the switch, with ID 60, address 198.51.100.1/24 and a dedicated DHCP scope.

Assign the interface to a new firewall zone, called TEST here, with these policies:

From → toPolicy
TEST → TESTblocked: test devices cannot see each other
TEST → VPN zoneallowed: it is the only exit
TEST → WANnot configured: no direct forwarding to the Internet
TEST → LANblocked
LAN → TESTallowed, to manage the segment

The fact that TEST has no forwarding to the WAN is already a first barrier: even if routing went wrong, the firewall would not let the traffic out through the ISP connection.

Important

Also restrict traffic from the segment to the firewall itself to the services it needs, that is DHCP and DNS. Leaving it open exposes the firewall's admin interface and SSH to the very network where you run the software you trust least.

The WireGuard tunnel to the provider

NethSecurity includes an OpenVPN client configurable from the web interface, but it did not accept my provider's profile. WireGuard (which the provider calls NordLynx) turned out to be simpler and faster. The provider does not supply a ready-made WireGuard configuration file: the private key, tunnel address, server public key and endpoint have to be obtained from the account.

The WireGuard interface can be created from the web interface or over SSH. The delicate setting is route_allowed_ips:

On the firewall, over SSH
uci set network.wg_vpn=interface
uci set network.wg_vpn.proto='wireguard'
uci set network.wg_vpn.private_key='<PRIVATE_KEY>'
uci add_list network.wg_vpn.addresses='<TUNNEL_ADDRESS>/32'
uci add network wireguard_wg_vpn
uci set network.@wireguard_wg_vpn[-1].public_key='<SERVER_PUBLIC_KEY>'
uci set network.@wireguard_wg_vpn[-1].endpoint_host='<PROVIDER_SERVER>'
uci set network.@wireguard_wg_vpn[-1].endpoint_port='51820'
uci add_list network.@wireguard_wg_vpn[-1].allowed_ips='0.0.0.0/0'
uci set network.@wireguard_wg_vpn[-1].route_allowed_ips='0'
uci set network.@wireguard_wg_vpn[-1].persistent_keepalive='25'
uci commit network

Warning

With allowed_ips='0.0.0.0/0' and route_allowed_ips='1' the firewall adds a default route through the tunnel to the main table: the whole network, not just the test segment, tries to go out through the VPN. In my case the main network stopped browsing. With route_allowed_ips='0' the tunnel leaves the main table alone and routing is decided by the rules in the next step.

The tunnel gets its own firewall zone (VPN here) with masquerading enabled, because the provider only accepts the address assigned to the tunnel, and with the MSS/MTU fix, to avoid pages that fail to load because of packet size inside the tunnel.

Policy routing: only the test segment in the tunnel

The idea is to use a separate routing table, 100, containing a single default route through the tunnel, and to send only traffic originating from the test segment to that table.

On the firewall, over SSH
# table 100: default route through the tunnel
uci add network route
uci set network.@route[-1].interface='wg_vpn'
uci set network.@route[-1].target='0.0.0.0/0'
uci set network.@route[-1].table='100'
 
# kill switch: if the tunnel goes down, table 100 only holds "unreachable"
uci add network route
uci set network.@route[-1].interface='loopback'
uci set network.@route[-1].type='unreachable'
uci set network.@route[-1].target='0.0.0.0/0'
uci set network.@route[-1].table='100'
uci set network.@route[-1].metric='500'
 
# private destinations stay on the main table (higher priority)
for net in 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16; do
  uci add network rule
  uci set network.@rule[-1].dest="$net"
  uci set network.@rule[-1].lookup='main'
  uci set network.@rule[-1].priority='90'
done
 
# all other traffic from the test segment goes to table 100
uci add network rule
uci set network.@rule[-1].src='198.51.100.0/24'
uci set network.@rule[-1].lookup='100'
uci set network.@rule[-1].priority='100'
 
uci commit network
service network reload

The exceptions for private networks are there for internal traffic: replies from the test segment to connections opened by the main network must return through the main table, not end up in the tunnel. If your main network uses non-private addresses, add an exception for it as well.

Tip

Multi WAN is not needed: the exit depends only on the source, and two policy routing rules are easier to read and verify than a load-balancing setup.

Kill switch

The kill switch works on two levels. In table 100 the unreachable route has a higher metric than the tunnel route, so it only kicks in when the WireGuard interface is missing: the segment's packets are dropped instead of looking for another path. On top of that, the TEST zone has no forwarding to the WAN, so even a routing mistake would be blocked by the firewall.

DNS without leaks

If the devices in the segment used the ISP's resolvers, DNS requests would leave the tunnel in clear, revealing what is being visited. The segment's DHCP therefore hands out the VPN provider's DNS resolvers, which are reachable only through the tunnel thanks to policy routing. Alternatively, you can hand out the firewall's DNS and configure the firewall to forward the segment's requests to the provider's resolvers.

Checks

On the firewall
ip rule show
ip route show table 100
wg show

ip rule show must list the rules with priority 90 and 100, ip route show table 100 the default route through the tunnel and the unreachable one, wg show a recent handshake with the provider's server.

From a device in the test segment
curl -s https://ipinfo.io/country
TestExpected result
Public IP country from the test segmentthe VPN server's, not yours
Public IP country from the main networkyours, unchanged
DNS leak test from the test segmentprovider resolvers only
Ping from the test segment to the main networkno reply
Access from the main network to a test deviceworks
Tunnel brought down with ifdown wg_vpnthe test segment can no longer browse

The last test matters most: an untested kill switch is just a hope.

Problems encountered

  • OpenVPN profile rejected. The web interface's OpenVPN client did not accept the provider's profile: WireGuard solved it and turned out to be faster too.
  • Whole network in the tunnel. With route_allowed_ips enabled the tunnel's default route ended up in the main table and the main network stopped browsing. The fix is to disable it and let the rules decide the routing.
  • Pages that do not load. Inside a tunnel the usable packet space is smaller: the MSS fix on the VPN zone prevents loads from stalling halfway.

Related articles