Iptables

TcpDump 顯示 ICMP 回复但 PING 超時

  • February 3, 2022

我正在嘗試將特定使用者帳戶 (vpnnet) 的網路流量重定向到 OpenVPN 伺服器。拉出預設路由時,隧道工作正常,但我不想通過 VPN 路由所有流量,僅適用於使用給定 UID 執行的程序。所以我禁用了預設路由的自動拉取並想出了以下內容:

iptables -t mangle -A OUTPUT -m owner --uid-owner vpnnet -j MARK --set-mark 42
ip route add default via 10.1.1.1 table 42
ip rule add fwmark 42 table 42
iptables -t nat -A POSTROUTING -o tun0 -m mark --mark 42 -j SNAT --to-source 10.1.1.100

本地地址是10.1.1.100並且10.1.1.1是遠端網關。

ifconfig
tun0: flags=4305<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST>  mtu 1500
       inet 10.1.1.100  netmask 255.255.255.0  destination 10.1.1.100
       inet6 ****::****::****::****::****  prefixlen 64  scopeid 0x20<link>
       unspec 00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00  txqueuelen 100  (UNSPEC)
       RX packets 128  bytes 10032 (9.7 KiB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 108  bytes 8832 (8.6 KiB)
       TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
ip route show
default via 172.31.1.1 dev eth0 proto dhcp metric 100
10.1.1.0/24 dev tun0 proto kernel scope link src 10.1.1.100 metric 50
172.31.1.0/24 dev eth0 proto kernel scope link src 172.31.1.100 metric 100
172.31.1.1 dev eth0 proto static scope link metric 100
ip route show table 42
default via 10.1.1.1 dev tun0

在使用者下啟動 shellvpnuser並 ping Google DNS 伺服器8.8.8.8時,ping 有 100% 的封包遺失。TcpDump 顯示請求正在發出並且回復正在正確的介面上進入:

tcpdump -nn -vv -i tun0
tcpdump: listening on tun0, link-type RAW (Raw IP), capture size 262144 bytes
20:33:28.938772 ip: (tos 0x0, ttl 64, id 7713, offset 0, flags [DF], proto ICMP (1), length 84)
   10.1.1.100 > 8.8.8.8: ICMP echo request, id 5078, seq 1, length 64
20:33:29.029533 ip: (tos 0x48, ttl 115, id 0, offset 0, flags [none], proto ICMP (1), length 84)
   8.8.8.8 > 10.1.1.100: ICMP echo reply, id 5078, seq 1, length 64
20:33:29.938962 ip: (tos 0x0, ttl 64, id 7790, offset 0, flags [DF], proto ICMP (1), length 84)
   10.1.1.100 > 8.8.8.8: ICMP echo request, id 5078, seq 2, length 64
20:33:30.029685 ip: (tos 0x48, ttl 115, id 0, offset 0, flags [none], proto ICMP (1), length 84)
   8.8.8.8 > 10.1.1.100: ICMP echo reply, id 5078, seq 2, length 64
20:33:30.938838 ip: (tos 0x0, ttl 64, id 8152, offset 0, flags [DF], proto ICMP (1), length 84)
   10.1.1.100 > 8.8.8.8: ICMP echo request, id 5078, seq 3, length 64
20:33:31.029179 ip: (tos 0x48, ttl 115, id 0, offset 0, flags [none], proto ICMP (1), length 84)
   8.8.8.8 > 10.1.1.100: ICMP echo reply, id 5078, seq 3, length 64
ping 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
^C
--- 8.8.8.8 ping statistics ---
12 packets transmitted, 0 received, 100% packet loss, time 11001ms

iptables 日誌不顯示任何丟棄的數據包。

作業系統為 CentOS 7。

如果有人能指出我正確的方向,我將不勝感激。

要回答我自己的問題並幫助其他遇到此問題的人… 反向路徑過濾啟動並rp_filter丟棄了數據包,請參閱說明。解決方案是將介面的 rp_filter 值設置2tun0

引用自:https://serverfault.com/questions/1091878