Skip to content

Commit 9cc0b02

Browse files
dguidoclaude
andauthored
Fix VPN traffic routing issue with iptables NAT rules (#14825)
* Fix VPN traffic routing issue with iptables NAT rules The MASQUERADE rules had policy matching (-m policy --pol none --dir out) which was preventing both WireGuard AND IPsec traffic from being NAT'd properly. This policy match was incorrect and broke internet routing for all VPN clients. The confusion arose because: - IPsec FORWARD rules check for --pol ipsec (encrypted traffic) - But POSTROUTING happens AFTER decryption, so packets no longer have policy - The --pol none match was blocking these decrypted packets from NAT Changes: - Removed policy matching from both IPsec and WireGuard NAT rules - Both VPN types now use simple source-based NAT rules - Applied to both IPv4 and IPv6 rule templates This fixes the issue where VPN clients (both WireGuard and IPsec) could connect but not route traffic to the internet. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Remove unnecessary policy matching from iptables rules The policy matching (-m policy --pol none) was causing routing issues for both WireGuard and IPsec VPN traffic. This was based on a misunderstanding of how iptables processes VPN traffic: 1. FORWARD chain: IPsec needs --pol ipsec to identify encrypted traffic, but WireGuard doesn't need any policy match (it's not IPsec) 2. POSTROUTING NAT: Both VPN types see decrypted packets here, so policy matching is unnecessary and was blocking NAT Changes: - Removed policy matching from all NAT rules (both VPN types) - Removed policy matching from WireGuard FORWARD rules - Kept policy matching only for IPsec FORWARD (where it's needed) - Added comprehensive unit tests to prevent regression This fully fixes VPN routing for both WireGuard and IPsec clients. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> * Fix Python linting issues in iptables test file Fixed all ruff linting issues: - Removed unused yaml import - Fixed import sorting (pathlib before third-party imports) - Removed trailing whitespace from blank lines - Added newline at end of file All tests still pass after formatting fixes. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent 454faa9 commit 9cc0b02

File tree

3 files changed

+206
-4
lines changed

3 files changed

+206
-4
lines changed

roles/common/templates/rules.v4.j2

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ COMMIT
3636
-A PREROUTING --in-interface {{ ansible_default_ipv4['interface'] }} -p udp --dport {{ wireguard_port_avoid }} -j REDIRECT --to-port {{ wireguard_port_actual }}
3737
{% endif %}
3838
# Allow traffic from the VPN network to the outside world, and replies
39-
-A POSTROUTING -s {{ subnets | join(',') }} -m policy --pol none --dir out {{ '-j SNAT --to ' + snat_aipv4 if snat_aipv4 else '-j MASQUERADE' }}
39+
{% if ipsec_enabled %}
40+
# For IPsec traffic - NAT the decrypted packets from the VPN subnet
41+
-A POSTROUTING -s {{ strongswan_network }} {{ '-j SNAT --to ' + snat_aipv4 if snat_aipv4 else '-j MASQUERADE' }}
42+
{% endif %}
43+
{% if wireguard_enabled %}
44+
# For WireGuard traffic - NAT packets from the VPN subnet
45+
-A POSTROUTING -s {{ wireguard_network_ipv4 }} {{ '-j SNAT --to ' + snat_aipv4 if snat_aipv4 else '-j MASQUERADE' }}
46+
{% endif %}
4047

4148

4249
COMMIT
@@ -106,7 +113,7 @@ COMMIT
106113

107114
{% if wireguard_enabled %}
108115
# Forward any traffic from the WireGuard VPN network
109-
-A FORWARD -m conntrack --ctstate NEW -s {{ wireguard_network_ipv4 }} -m policy --pol none --dir in -j ACCEPT
116+
-A FORWARD -m conntrack --ctstate NEW -s {{ wireguard_network_ipv4 }} -j ACCEPT
110117
{% endif %}
111118

112119
COMMIT

roles/common/templates/rules.v6.j2

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ COMMIT
3535
-A PREROUTING --in-interface {{ ansible_default_ipv6['interface'] }} -p udp --dport {{ wireguard_port_avoid }} -j REDIRECT --to-port {{ wireguard_port_actual }}
3636
{% endif %}
3737
# Allow traffic from the VPN network to the outside world, and replies
38-
-A POSTROUTING -s {{ subnets | join(',') }} -m policy --pol none --dir out {{ '-j SNAT --to ' + ipv6_egress_ip | ansible.utils.ipaddr('address') if alternative_ingress_ip else '-j MASQUERADE' }}
38+
{% if ipsec_enabled %}
39+
# For IPsec traffic - NAT the decrypted packets from the VPN subnet
40+
-A POSTROUTING -s {{ strongswan_network_ipv6 }} {{ '-j SNAT --to ' + ipv6_egress_ip | ansible.utils.ipaddr('address') if alternative_ingress_ip else '-j MASQUERADE' }}
41+
{% endif %}
42+
{% if wireguard_enabled %}
43+
# For WireGuard traffic - NAT packets from the VPN subnet
44+
-A POSTROUTING -s {{ wireguard_network_ipv6 }} {{ '-j SNAT --to ' + ipv6_egress_ip | ansible.utils.ipaddr('address') if alternative_ingress_ip else '-j MASQUERADE' }}
45+
{% endif %}
3946

4047
COMMIT
4148

@@ -106,7 +113,7 @@ COMMIT
106113
-A FORWARD -m conntrack --ctstate NEW -s {{ strongswan_network_ipv6 }} -m policy --pol ipsec --dir in -j ACCEPT
107114
{% endif %}
108115
{% if wireguard_enabled %}
109-
-A FORWARD -m conntrack --ctstate NEW -s {{ wireguard_network_ipv6 }} -m policy --pol none --dir in -j ACCEPT
116+
-A FORWARD -m conntrack --ctstate NEW -s {{ wireguard_network_ipv6 }} -j ACCEPT
110117
{% endif %}
111118

112119
# Use the ICMPV6-CHECK chain, described above

tests/unit/test_iptables_rules.py

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test iptables rules logic for VPN traffic routing.
4+
5+
These tests verify that the iptables rules templates generate correct
6+
NAT rules for both WireGuard and IPsec VPN traffic.
7+
"""
8+
9+
from pathlib import Path
10+
11+
import pytest
12+
from jinja2 import Environment, FileSystemLoader
13+
14+
15+
def load_template(template_name):
16+
"""Load a Jinja2 template from the roles/common/templates directory."""
17+
template_dir = Path(__file__).parent.parent.parent / 'roles' / 'common' / 'templates'
18+
env = Environment(loader=FileSystemLoader(str(template_dir)))
19+
return env.get_template(template_name)
20+
21+
22+
def test_wireguard_nat_rules_ipv4():
23+
"""Test that WireGuard traffic gets proper NAT rules without policy matching."""
24+
template = load_template('rules.v4.j2')
25+
26+
# Test with WireGuard enabled
27+
result = template.render(
28+
ipsec_enabled=False,
29+
wireguard_enabled=True,
30+
wireguard_network_ipv4='10.49.0.0/16',
31+
wireguard_port=51820,
32+
wireguard_port_avoid=53,
33+
wireguard_port_actual=51820,
34+
ansible_default_ipv4={'interface': 'eth0'},
35+
snat_aipv4=None,
36+
BetweenClients_DROP=True,
37+
block_smb=True,
38+
block_netbios=True,
39+
local_service_ip='10.49.0.1',
40+
ansible_ssh_port=22,
41+
reduce_mtu=0
42+
)
43+
44+
# Verify NAT rule exists without policy matching
45+
assert '-A POSTROUTING -s 10.49.0.0/16 -j MASQUERADE' in result
46+
# Verify no policy matching in WireGuard NAT rules
47+
assert '-A POSTROUTING -s 10.49.0.0/16 -m policy' not in result
48+
49+
50+
def test_ipsec_nat_rules_ipv4():
51+
"""Test that IPsec traffic gets proper NAT rules without policy matching."""
52+
template = load_template('rules.v4.j2')
53+
54+
# Test with IPsec enabled
55+
result = template.render(
56+
ipsec_enabled=True,
57+
wireguard_enabled=False,
58+
strongswan_network='10.48.0.0/16',
59+
strongswan_network_ipv6='2001:db8::/48',
60+
ansible_default_ipv4={'interface': 'eth0'},
61+
snat_aipv4=None,
62+
BetweenClients_DROP=True,
63+
block_smb=True,
64+
block_netbios=True,
65+
local_service_ip='10.48.0.1',
66+
ansible_ssh_port=22,
67+
reduce_mtu=0
68+
)
69+
70+
# Verify NAT rule exists without policy matching
71+
assert '-A POSTROUTING -s 10.48.0.0/16 -j MASQUERADE' in result
72+
# Verify no policy matching in IPsec NAT rules (this was the bug)
73+
assert '-A POSTROUTING -s 10.48.0.0/16 -m policy --pol none' not in result
74+
75+
76+
def test_both_vpns_nat_rules_ipv4():
77+
"""Test NAT rules when both VPN types are enabled."""
78+
template = load_template('rules.v4.j2')
79+
80+
result = template.render(
81+
ipsec_enabled=True,
82+
wireguard_enabled=True,
83+
strongswan_network='10.48.0.0/16',
84+
wireguard_network_ipv4='10.49.0.0/16',
85+
strongswan_network_ipv6='2001:db8::/48',
86+
wireguard_network_ipv6='2001:db8:a160::/48',
87+
wireguard_port=51820,
88+
wireguard_port_avoid=53,
89+
wireguard_port_actual=51820,
90+
ansible_default_ipv4={'interface': 'eth0'},
91+
snat_aipv4=None,
92+
BetweenClients_DROP=True,
93+
block_smb=True,
94+
block_netbios=True,
95+
local_service_ip='10.49.0.1',
96+
ansible_ssh_port=22,
97+
reduce_mtu=0
98+
)
99+
100+
# Both should have NAT rules
101+
assert '-A POSTROUTING -s 10.48.0.0/16 -j MASQUERADE' in result
102+
assert '-A POSTROUTING -s 10.49.0.0/16 -j MASQUERADE' in result
103+
104+
# Neither should have policy matching
105+
assert '-m policy --pol none' not in result
106+
107+
108+
def test_alternative_ingress_snat():
109+
"""Test that alternative ingress IP uses SNAT instead of MASQUERADE."""
110+
template = load_template('rules.v4.j2')
111+
112+
result = template.render(
113+
ipsec_enabled=True,
114+
wireguard_enabled=True,
115+
strongswan_network='10.48.0.0/16',
116+
wireguard_network_ipv4='10.49.0.0/16',
117+
strongswan_network_ipv6='2001:db8::/48',
118+
wireguard_network_ipv6='2001:db8:a160::/48',
119+
wireguard_port=51820,
120+
wireguard_port_avoid=53,
121+
wireguard_port_actual=51820,
122+
ansible_default_ipv4={'interface': 'eth0'},
123+
snat_aipv4='192.168.1.100', # Alternative ingress IP
124+
BetweenClients_DROP=True,
125+
block_smb=True,
126+
block_netbios=True,
127+
local_service_ip='10.49.0.1',
128+
ansible_ssh_port=22,
129+
reduce_mtu=0
130+
)
131+
132+
# Should use SNAT with specific IP instead of MASQUERADE
133+
assert '-A POSTROUTING -s 10.48.0.0/16 -j SNAT --to 192.168.1.100' in result
134+
assert '-A POSTROUTING -s 10.49.0.0/16 -j SNAT --to 192.168.1.100' in result
135+
assert 'MASQUERADE' not in result
136+
137+
138+
def test_ipsec_forward_rule_has_policy_match():
139+
"""Test that IPsec FORWARD rules still use policy matching (this is correct)."""
140+
template = load_template('rules.v4.j2')
141+
142+
result = template.render(
143+
ipsec_enabled=True,
144+
wireguard_enabled=False,
145+
strongswan_network='10.48.0.0/16',
146+
strongswan_network_ipv6='2001:db8::/48',
147+
ansible_default_ipv4={'interface': 'eth0'},
148+
snat_aipv4=None,
149+
BetweenClients_DROP=True,
150+
block_smb=True,
151+
block_netbios=True,
152+
local_service_ip='10.48.0.1',
153+
ansible_ssh_port=22,
154+
reduce_mtu=0
155+
)
156+
157+
# FORWARD rule should have policy match (this is correct and should stay)
158+
assert '-A FORWARD -m conntrack --ctstate NEW -s 10.48.0.0/16 -m policy --pol ipsec --dir in -j ACCEPT' in result
159+
160+
161+
def test_wireguard_forward_rule_no_policy_match():
162+
"""Test that WireGuard FORWARD rules don't use policy matching."""
163+
template = load_template('rules.v4.j2')
164+
165+
result = template.render(
166+
ipsec_enabled=False,
167+
wireguard_enabled=True,
168+
wireguard_network_ipv4='10.49.0.0/16',
169+
wireguard_port=51820,
170+
wireguard_port_avoid=53,
171+
wireguard_port_actual=51820,
172+
ansible_default_ipv4={'interface': 'eth0'},
173+
snat_aipv4=None,
174+
BetweenClients_DROP=True,
175+
block_smb=True,
176+
block_netbios=True,
177+
local_service_ip='10.49.0.1',
178+
ansible_ssh_port=22,
179+
reduce_mtu=0
180+
)
181+
182+
# WireGuard FORWARD rule should NOT have any policy match
183+
assert '-A FORWARD -m conntrack --ctstate NEW -s 10.49.0.0/16 -j ACCEPT' in result
184+
assert '-A FORWARD -m conntrack --ctstate NEW -s 10.49.0.0/16 -m policy' not in result
185+
186+
187+
if __name__ == '__main__':
188+
pytest.main([__file__, '-v'])

0 commit comments

Comments
 (0)