iptables转发被忽略

我遇到了将stream量从一个接口转发到另一个接口的问题。 看来我的转发规则没有得到尊重。

sysctl net.ipv4.ip_forward net.ipv4.ip_forward = 1 

以下是我的IPtable的外观:

 Chain INPUT (policy ACCEPT 60 packets, 8184 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 LOG all -- any any 192.168.0.0/24 anywhere LOG level warning 0 0 ACCEPT all -- eth0 tun0 192.168.0.0/24 anywhere ctstate NEW Chain OUTPUT (policy ACCEPT 82 packets, 11632 bytes) pkts bytes target prot opt in out source destination 

我希望来自192.168.0.0/24的任何东西都被转发到接口tun0。 我想要转发的stream量logging。

现在,如果我继续并执行以下命令:

 ping 10.8.0.2 PING 10.8.0.2 (10.8.0.2) 56(84) bytes of data. 64 bytes from 10.8.0.2: icmp_req=1 ttl=64 time=1974 ms 64 bytes from 10.8.0.2: icmp_req=2 ttl=64 time=1069 ms 64 bytes from 10.8.0.2: icmp_req=3 ttl=64 time=669 ms 64 bytes from 10.8.0.2: icmp_req=4 ttl=64 time=670 ms 

但是,如果我这样做:

 ping 10.8.0.2 -I eth0 PING 10.8.0.2 (10.8.0.2) from 192.168.0.10 eth0: 56(84) bytes of data. From 192.168.0.10 icmp_seq=1 Destination Host Unreachable From 192.168.0.10 icmp_seq=2 Destination Host Unreachable From 192.168.0.10 icmp_seq=3 Destination Host Unreachable 

stream量未被转发。 任何想法为什么? 我在这里错过了什么?

Dadriel对本地stream量不是通过FORWARD链路的情况做了很好的说明,但是你有一个更基本的问题:这不是iptables规则的工作原理。 他们不决定如何转发数据包(至less不容易),他们决定是否应该转发数据包。 您的规则上面logging并允许来自192.168.0.0/24任何NEWstream量通过tun0离开,但是他们不强制符合前一条件的stream量采用后一条路由。

路由表决定了stream量如何转发,听起来就像是基于策略的路由select(也就是说,如何select路由以基于除目的地IP地址以外的东西 – 在这种情况下是源IP地址) 。

你应该看看iproute2

您不能像这样testing转发,因为从您用作转发器的服务器发出的数据包不会遍历该链。

但真正的问题在于,你只是转发新的连接,这是错误的。 这将导致该语句仅在您的服务器首次查看连接时起作用。 之后的一切都不再转发了。