When configuring NAT with a VPN on OpenBSD, it's important to ensure that your NAT and routing rules are correctly targeting the VPN tunnel interface, since establishing a VPN connection changes the network paths that traffic needs to take. Here's a step-by-step guide to properly configuring NAT and routing in this context:
1. Review Your PF Rules
Edit your /etc/pf.conf to ensure that NAT and rules accommodate both the VPN tunnel and the regular internet connection:
Example PF Configuration
# Interface names
EXT_IF="em0" # Replace with your actual external network interface
VPN_IF="enc0" # enc0 typically represents the encapsulated VPN interface
# Local network
LAN_IF="em1" # Replace with your LAN interface
LAN_NET="192.168.1.0/24" # Replace with your actual local subnet
# Enable NAT for the external interface
match out on $EXT_IF from $LAN_NET to any nat-to ($EXT_IF)
# Enable NAT for the VPN tunnel interface
match out on $VPN_IF from $LAN_NET to any nat-to ($VPN_IF)
# Allow all traffic with established, related connection
pass out on $EXT_IF from ($EXT_IF:network) to any keep state
pass out on $VPN_IF from ($VPN_IF:network) to any keep state
# Allow internal traffic
pass in on $LAN_IF from $LAN_NET to any keep state
2. IP Forwarding
Ensure that IP forwarding is enabled, which is necessary for packets to move between interfaces:
In /etc/sysctl.conf:
net.inet.ip.forwarding=1
Apply this setting immediately with:
sysctl net.inet.ip.forwarding=1
3. Routing Configuration
Ensure that your routes are correctly configured. When the VPN is up, you want the specific traffic to go through the VPN. Often, OpenIKED will handle this routing, but if you need specific configurations, use route commands or define routes in /etc/hostname.* files.
4. Restart Services
After making changes, restart the appropriate services to apply the configurations:
pfctl -f /etc/pf.conf # To reload the PF configuration
/etc/rc.d/iked restart # To restart the OpenIKED service
5. Debugging
Use tcpdump to monitor traffic on your network interfaces to ensure packets are NATed and routed correctly. For example:
tcpdump -ni enc0 # Monitor the VPN tunnel interface
tcpdump -ni lan_if # Monitor the local LAN interface
tcpdump -ni ext_if # Monitor the external network interface
6. Test Connectivity
- Connect a client device on your LAN and try accessing an endpoint through the VPN.
- Verify that packets appear on the monitored interfaces as expected.
By ensuring correct NAT rules are set up for both your external and VPN interfaces, and that IP forwarding is enabled, your router should be able to properly NAT and route traffic via the VPN when OpenIKED is active.