Fix VPN traffic routing issue with iptables NAT rules - #14825
Merged
Merged
Conversation
Contributor
|
Claude finished @dguido's task —— View job Comprehensive PR ReviewI've conducted a thorough review of this critical iptables NAT routing fix. Here's my detailed analysis: Review Checklist:
Review Summary: ✅ EXCELLENT - Strongly Recommend MergeThis PR addresses a critical VPN functionality bug with a well-researched solution, comprehensive testing, and excellent technical execution. Key Strengths🎯 Problem Analysis & Solution
🧪 Exceptional Test CoverageThe new test file (
🔒 Security Assessment: SAFE
⚡ Performance Assessment: NEUTRAL TO POSITIVE
Technical Implementation ReviewTemplate Changes (
|
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 <noreply@anthropic.com>
dguido
force-pushed
the
fix/wireguard-iptables-nat
branch
from
August 17, 2025 20:17
4b0f3b1 to
d5f88dd
Compare
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 <noreply@anthropic.com>
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 <noreply@anthropic.com>
This was referenced Aug 17, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes critical issue where both WireGuard AND IPsec VPN clients could connect but not route traffic to the internet.
Root Cause Analysis
The NAT and FORWARD rules incorrectly used policy matching (
-m policy --pol none) which broke routing:NAT Issue: The POSTROUTING chain sees decrypted packets from VPN clients. The
--pol nonematch was blocking these packets from being NAT'd, preventing internet access.FORWARD Issue: WireGuard packets don't use IPsec policies at all. The
--pol nonematch in FORWARD was unnecessary and potentially problematic.Why it existed: The policy matching was likely added to handle edge cases (site-to-site VPNs, preventing double NAT) that don't apply to Algo's road warrior VPN design.
Solution
Removed all unnecessary policy matching:
--pol ipsec(correctly identifies encrypted traffic)Changes
Fixed iptables templates:
roles/common/templates/rules.v4.j2roles/common/templates/rules.v6.j2Added comprehensive tests (
tests/unit/test_iptables_rules.py):Testing
Impact
🤖 Generated with Claude Code