> ## Content Index
> Fetch the complete content index at: https://www.cxy.hk/llms.txt
> Use this file to discover other available public pages before exploring further.

# Linux 对流入流量的 srcnat
- URL: https://www.cxy.hk/linux-dui-liu-ru-liu-liang-de-srcnat/
- Published: 2026-08-07T14:20:00.000Z
- Updated: 2026-10-20T11:28:33.000Z
- Author: Xinyu Chen

最近维护 AS151673 个人网时用到了某公共的 Wireguard 隧道服务。链接质量非常好，但是由于其限制了可通过地址，且其实际上存在多跳。

A（我的机器） B （该 WG 服务器）C（我的第二台机器）

故准备在套一层 GRE 来建立 OSPF。

但是当 GRE 建立后 两侧都无法联通。

抓包发现实际上应该 src 为 A ，dst 为 C 的流量，变成了 src 为 B ，dst 为 C，另一侧同理。很明显是在 B 有一层 SNAT/MASQUERADE

这导致 GRE 没有将这样的流量匹配到 A-C 的 GRE 中。

解决方式有两种：1.改为使用 UDP 传输 2.在接收前修改掉 SRC

如果改用 UDP , 那么只有 VXLAN 和 WG 可选，但是这两者 MTU 开销都过大。

所以选择方案2.

当尝试使用 IPtables PREROUTING + SNAT，其不出意外的报错了，结果在意料之中，snat 一般是用于本机发出的包，而不是接收到包。

所以我们选择使用 nftables

`sudo nft add table ip rx-snat  
sudo nft add chain ip rx-snat prerouting '{ type filter hook prerouting priority -150; }'  
sudo nft add rule ip rx-snat prerouting ip saddr xxx.xxx.xxx.1 ip protocol gre ip saddr set xxx.xxx.xxx.2`

两侧全部执行一下，完美解决。唯一的缺点就是只能当作 PTP 了……