linux - ip_forward 不适用于 tcp

标签 linux iptables

我有一个带有 2 个接口(interface)的 linux 机器,wlan0 和 wlan1。 wlan0 连接到子网 192.168.43.x 的外部 wlan AP。在 wlan1 上,softap 在子网 192.168.60.x 上运行。

我运行了以下命令来启动 nat 和 ip_forward:

iptables -F
iptables -F INPUT
iptables -F OUTPUT
iptables -F FORWARD
iptables -t nat -F
iptables -t mangle -F

iptables -A INPUT -j ACCEPT
iptables -A OUTPUT -j ACCEPT
iptables -A FORWARD -j ACCEPT
iptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADE
echo 1 > /proc/sys/net/ipv4/ip_forward

另一台 pc e6220 连接到 192.168.60.x softap。我想把pc的流量转发到外网192.168.43.x。

网络图:

外部 AP (192.168.43.1) <~~~> {wlan0(192.168.43.2), <-- forward --> wlan1(192.168.60.1) 上的内部 AP} <~~~> e6220 (192.168.60.46 )

在pc上添加route后,icmp和udp都可以,都可以到192.168.43.1,但是tcp不行!

userg@e6220:~ $ ping 192.168.43.1
PING 192.168.43.1 (192.168.43.1) 56(84) bytes of data.
64 bytes from 192.168.43.1: icmp_seq=1 ttl=63 time=9.74 ms
64 bytes from 192.168.43.1: icmp_seq=2 ttl=63 time=9.57 ms
^C

user@e6220:~ $ telnet 192.168.43.1
Trying 192.168.43.1...
telnet: Unable to connect to remote host: Network is unreachable

在 linux box wlan1 上捕获:

17:10:57.579994 IP 192.168.60.46.56412 > 192.168.43.1.telnet: Flags [S], seq     1764532258, win 29200, options [mss 1460,sackOK,TS val 2283575078 ecr 0,nop,wscale 7], length 0
17:10:57.580075 IP 192.168.60.1 > 192.168.60.46: ICMP net 192.168.43.1 unreachable, length 68

在 wlan0 上没有看到数据包。

奇怪的是内核说无法访问,但 UDP 可以工作。

// route -n output
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.43.1    0.0.0.0         UG    0      0        0 wlan0
192.168.43.0    0.0.0.0         255.255.255.0   U     0      0        0 wlan0
192.168.60.0    0.0.0.0         255.255.255.0   U     0      0        0 wlan1

我认为这可能是特定于内核版本的。在 Linux 3.4.x 上,tcp 和 udp 都很好。此问题出现在 Linux 3.10.61 上。我想知道是否有任何方法可以修复它,内核配置或 iptables 命令。谢谢!

最佳答案

终于找到了解决方案。

路由盒配置CONFIG_IP_MULTIPLE_TABLES,即策略路由。路由规则是:

# ip rule list
0:  from all lookup local 
10000:  from all fwmark 0xc0000/0xd0000 lookup legacy_system 
13000:  from all fwmark 0x10063/0x1ffff lookup local_network 
15000:  from all fwmark 0x0/0x10000 lookup legacy_system 
16000:  from all fwmark 0x0/0x10000 lookup legacy_network 
17000:  from all fwmark 0x0/0x10000 lookup local_network 
23000:  from all fwmark 0x0/0xffff uidrange 0-0 lookup main 
32000:  from all unreachable

所以每个数据包都“无法访问”。添加以下规则后 TCP 变得良好:

# ip rule add iif wlan0 table main
# ip rule add iff wlan1 table main

# ip rule list
0:  from all lookup local 
9998:   from all iif wlan0 lookup main 
9999:   from all iif wlan1 lookup main
10000:  from all fwmark 0xc0000/0xd0000 lookup legacy_system 
13000:  from all fwmark 0x10063/0x1ffff lookup local_network 
15000:  from all fwmark 0x0/0x10000 lookup legacy_system 
16000:  from all fwmark 0x0/0x10000 lookup legacy_network 
17000:  from all fwmark 0x0/0x10000 lookup local_network 
23000:  from all fwmark 0x0/0xffff uidrange 0-0 lookup main 
32000:  from all unreachable

但我仍然不知道为什么在添加这 2 个规则之前 UDP 是好的。

关于linux - ip_forward 不适用于 tcp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33732076/

相关文章:

linux - 当 CPU 密集型应用程序在后台运行时进行多核编程?

java - 抑制 GUI?

kubernetes - 在Kubernetes节点上重启后删除iptables规则

linux - libipq 性能问题

linux - linux 的 IP 地址阻止列表

regex - linux中目录的半复杂重组

C++数学函数问题(Linux下)

linux - 每个命令都返回 'bash: <command>: command not found...'

linux - 将IP连接限制为具有iptables的端口,但允许一个IP无限连接

ssh - 无家可归的盒子和iptables