iptables:转发数据包不起作用

我有3个Linux系统A,B和C.A是一个TCP客户端,并且向C上的TCP服务器发送一个消息.A只能看到位于wlan0接口的B的外部IP,192.168.0.3

------------------ --------------------- -------------------------- System A System B System C 192.168.0.5 wlan0 <-----> 192.168.0.3 wlan0 192.168.61.73 eth0 <---> 192.168.61.81 eth0 TCP Client TCP Server on 192.168.61.81 ------------------ ---------------------- ------------------------- 

TCP客户端发送消息到192.168.0.3。

这应该被redirect到系统C的端口8036上的192.168.61.81上运行的TCP服务器(通过B的eth0接口)。

因此,我写了下面的ip表规则,然后在C上启动服务器,并从A上的TCP客户端发送消息。我可以在wlan0上从A上看到系统B上的数据包,但是它们永远不会被转发。 系统B似乎是从A接收SYN数据包,但将其丢弃(请参阅日志)。

这是我在系统B上做的:

 #Enable IP Forwarding for NAT echo "1" > /proc/sys/net/ipv4/ip_forward #Flush all iptable chains and start afresh sudo iptables -F #Forwarding rules sudo iptables -A PREROUTING -p tcp -m tcp -d 192.168.0.3 --dport 8036 -j DNAT --to-destination 192.168.61.81:8036 sudo iptables -A FORWARD -m state -p tcp -d 192.168.61.81 --dport 8036 --state NEW,ESTABLISHED,RELATED -j ACCEPT sudo iptables -t nat -A POSTROUTING -p tcp -m tcp -s 192.168.61.81 --sport 8036 -j SNAT --to-source 192.168.0.3 #Enable logging sudo iptables -A INPUT -j LOG --log-prefix INPUT sudo iptables -A OUTPUT -j LOG --log-prefix OUTPUT sudo iptables -A FORWARD -j LOG --log-prefix FORWARD sudo iptables -A INPUT -j LOG --log-prefix 'drop:' sudo iptables -A OUTPUT -j LOG --log-prefix 'drop:' sudo iptables -A FORWARD -j LOG --log-prefix 'drop:' tail -F /var/log/messages 

系统B从/ var / log / messageslogging日志:(SYN数据包被丢弃)

 May 2 11:53:17 my-laptop kernel: [42879.905449] FORWARDIN=wlan0 OUT=eth0 SRC=192.168.0.5 DST=192.168.61.81 LEN=60 TOS=0x00 PREC=0x00 TTL=63 ID=30394 DF PROTO=TCP SPT=40582 DPT=8036 WINDOW=5840 RES=0x00 SYN URGP=0 May 2 11:53:17 my-laptop kernel: [42879.905459] drop:IN=wlan0 OUT=eth0 SRC=192.168.0.5 DST=192.168.61.81 LEN=60 TOS=0x00 PREC=0x00 TTL=63 ID=30394 DF PROTO=TCP SPT=40582 DPT=8036 WINDOW=5840 RES=0x00 SYN URGP=0 

系统B上的内核IP路由表:

 Destination Gateway Genmask Flags Metric Ref Use Iface 192.168.61.0 * 255.255.255.0 U 0 0 0 eth0 default localhost 0.0.0.0 UG 0 0 0 eth0 

请帮忙。

更新:

 sudo iptables -L -n -v Chain INPUT (policy ACCEPT 39844 packets, 25M bytes) pkts bytes target prot opt in out source destination 26926 10M LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 12866 8559K LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 prefix `INPUT' 12862 8558K LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 prefix `drop:' Chain FORWARD (policy ACCEPT 43 packets, 2580 bytes) pkts bytes target prot opt in out source destination 79 4740 ACCEPT tcp -- * * 0.0.0.0/0 192.168.61.81 state NEW,RELATED,ESTABLISHED tcp dpt:8036 16 960 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 prefix `FORWARD' 16 960 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 prefix `drop:' Chain OUTPUT (policy ACCEPT 36130 packets, 4943K bytes) pkts bytes target prot opt in out source destination 27863 4093K LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 14035 2296K LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 prefix `OUTPUT' 14034 2296K LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 prefix `drop:' sudo iptables -t nat -L -n -v Chain PREROUTING (policy ACCEPT 252 packets, 63782 bytes) pkts bytes target prot opt in out source destination 8 480 DNAT tcp -- * * 0.0.0.0/0 192.168.0.3 tcp dpt:8036 to:192.168.61.81:8036 236 59206 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 Chain OUTPUT (policy ACCEPT 3098 packets, 208K bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 3121 packets, 210K bytes) pkts bytes target prot opt in out source destination 0 0 SNAT tcp -- * * 192.168.61.81 0.0.0.0/0 tcp spt:8036 to:192.168.0.3 2975 199K LOG all -- * * 0.0.0.0/0 0.0.0.0/0 LOG flags 0 level 4 

我发现我必须将系统B和系统C的默认gw设置为192.168.0.3,否则默认192.168.0.1(WLAN路由器)设置为默认路由。

一旦我设置了默认路由,一切都像一个魅力:)

奇怪的configuration为简单的configuration我会说,不需要太多与iptables,snat等。 configuration完B后,只需要在A和C之间设置静态路由,就可以通过B到达对方的networking。或者,如果在本地networking中已经有路由器/网关,则可以直接设置静态路由他们(虽然不那么高效,因为通信数据包会多跳两次 – 到路由器,然后到B)