CentOS 端口转发不起作用

use*_*140 7 iptables port-forwarding

我正在使用 CentOS 6.5,并且在我的 iptables 中添加了以下命令以将端口 8088 上的所有传入流量转发到 4569:

iptables -A PREROUTING -t nat -p udp --dport 8088 -i eth0 -j DNAT --to-destination 127.0.0.1:4569
iptables -I FORWARD 1 -d 127.0.0.1 -p udp --dport 4569 -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

iptables --list 显示以下输出:

iptables --list
Chain INPUT (policy ACCEPT)
target     prot opt source               destination

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
ACCEPT     udp  --  anywhere             localhost.localdomain udp dpt:iax

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination
Run Code Online (Sandbox Code Playgroud)

但是当我在 udp 端口​​ 4569 上进行数据包跟踪时,我在该端口上看不到任何数据包。然后我添加了这个:

iptables -A PREROUTING -t nat -p udp --dport 8088 -i eth0 -j REDIRECT --to-ports 4569
Run Code Online (Sandbox Code Playgroud)

我的 iptable 看起来像这样:

Table: nat
Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination
1    DNAT       udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:8088 to:127.0.0.1:4569
2    REDIRECT   udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:8088 redir ports 4569
Run Code Online (Sandbox Code Playgroud)

但仍然没有运气。我究竟做错了什么?

Xav*_*cas 7

要将数据包重定向到环回接口,您需要使用REDIRECT目标。

iptables -A PREROUTING -t nat -p udp --dport 8088 -i eth0 -j REDIRECT --to-ports 4569
Run Code Online (Sandbox Code Playgroud)

否则,您将在做出路由决定之前更改目标地址127.0.0.1。这意味着它会被内核视为火星数据包,并被您的反向路径过滤策略丢弃。

负责此行为的两个内核参数是:

  • net.ipv4.conf.eth0.route_localnet

route_localnet - 布尔值

路由时不要将环回地址视为 martian 源或目标。这样就可以将 127/8 用于本地路由目的。

默认错误

  • net.ipv4.conf.eth0.rp_filter

rp_filter - 整数

0 - 无源验证。
1 - RFC3704 Strict Reverse Path 中定义的严格模式每个传入数据包都针对 FIB 进行测试,如果接口不是最佳反向路径,则数据包检查将失败。默认情况下,丢弃失败的数据包。
2 - RFC3704 松散反向路径中定义的松散模式 每个传入数据包的源地址也针对 FIB 进行测试,如果源地址无法通过任何接口访问,则数据包检查将失败。

Current recommended practice in RFC3704 is to enable strict mode to prevent IP spoofing from DDos attacks. If using asymmetric routing or other complicated routing, then loose mode is recommended.

The max value from conf/{all,interface}/rp_filter is used when doing source validation on the {interface}.

Default value is 0. Note that some distributions enable it in startup scripts.

As you totally want to keep this legitimate behaviour, the REDIRECT chain must be used to bypass this condition for a certain rule.