IPsec and SNAT

本文介绍如何通过调整iptables规则来确保使用IPSec隧道的数据包不会被NAT或IP伪装所影响,包括使用例外规则、passthrough规则及IPsec策略匹配等方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Do not MASQ or NAT packets to be tunneled
If you are using IP masquerade or Network Address Translation (NAT) on either gateway, you must now exempt the packets you wish to tunnel from this treatment. For example, if you have a rule like:

# iptables -t nat -A POSTROUTING -o eth0 -s 10.0.0.0/24 -j MASQUERADE

change it to something like:

# iptables -t nat -I POSTROUTING -o eth0 -s 10.0.0.0/24 -d ! 172.16.0.0/24 -j MASQUERADE

or use passthrough rule
# iptables -t nat -I POSTROUTING 1 -o eth0 -s 10.0.0.0/24 -d 172.16.0.0/24 -j RETURN/ACCEPT

or use policy matching !

with iptables 1.3.5 and a Linux kernel > 2.6.15, IPsec policy matching developed by Patrick McHardy was introduced into Linux Netfilter, changing the behaviour of NAT rules in regard to IPsec tunnels. If you e.g. have a general NAT rule for mapping internal addresses to the external interface and want to exempt tunneled traffic from NAT then you must insert an IPsec policy matching rule in front of the SNAT or MASQUERADE rule in the POSTROUTING chain. This is what I'm doing on my productive system:

# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            policy match dir in pol ipsec mode tunnel

Chain POSTROUTING (policy ACCEPT)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere            policy match dir out pol ipsec mode tunnel
MASQUERADE  all  --  10.0.0.0/24          anywhere


# iptables -t nat -I PREROUTING -m policy --dir out /
--pol ipsec --mode tunnel -j ACCEPT

# iptables -t nat -I POSTROUTING -m policy --dir in /
--pol ipsec --mode tunnel -j ACCEPT

This may be necessary on both gateways.

 

 

 

 

get xt_policy's help info

# iptables -m policy --help

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值