高级路由问题

yoz*_*pho 3 nat iptables iproute2

我有 2 个使用 2 个 ADSL 路由器的 Internet 链接,我需要为 192.168.0.0/24 网络提供 Internet 访问权限。

我必须根据端口号、协议……在 Linux 路由器上使用 iproute2 和 iptables 来路由传出流量。

这是我的网络:

     (ISP-1)                              (ISP-2)
Dynamic public IP                    Dynamic public IP 
        |                                    |
+---------------+                    +---------------+
|ADSL Router (1)|                    |ADSL Router (2)|
+---------------+                    +---------------+
        |                                    |
   192.168.1.1                          192.168.2.1
        |                                    |
        |                                    |
        |                                    |
        |        +------------------+        |
        |        |                  |        |
   192.168.1.2 --|eth1          eth2|-- 192.168.2.2
                 |                  |
                 |   Linux Router   |
                 |                  |
                 |       eth0       |
                 +------------------+
                          |
                     192.168.0.1
                          |
                          |
                    Local Network:
                    192.168.0.0/24
Run Code Online (Sandbox Code Playgroud)

我使用以下脚本在 Linux 路由器上设置网络配置:

#!/bin/bash

echo 1 >| /proc/sys/net/ipv4/ip_forward
echo 0 >| /proc/sys/net/ipv4/conf/all/rp_filter

# flush all iptables entries
iptables -t filter -F
iptables -t filter -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X
iptables -t filter -P INPUT ACCEPT
iptables -t filter -P OUTPUT ACCEPT
iptables -t filter -P FORWARD ACCEPT

# marking packets
iptables -t mangle -A PREROUTING -i eth0 -p icmp -j MARK --set-mark 1
iptables -t mangle -A PREROUTING -i eth0 -p udp  -j MARK --set-mark 1
iptables -t mangle -A PREROUTING -i eth0 -p tcp  -j MARK --set-mark 2

# create routing tables and default routes
echo '1     ISP1' >> /etc/iproute2/rt_tables
echo '2     ISP2' >> /etc/iproute2/rt_tables
ip route add default via 192.168.1.1 dev eth1 table ISP1
ip route add default via 192.168.2.1 dev eth2 table ISP2

# routing based on previous marks
ip rule add fwmark 1 table ISP1
ip rule add fwmark 2 table ISP2

ip route flush cache

# NAT
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
iptables -t nat -A POSTROUTING -o eth2 -j MASQUERADE
Run Code Online (Sandbox Code Playgroud)

问题是我无法从 192.168.0.0/24 网络连接到互联网。

当我从这个网络 ping 到远程服务器时,我可以看到(使用 Wireshark)从远程服务器返回到 Linux 路由器的 eth1 的回复,但它们没有到达 eth0。

请帮忙。并提前致谢。

(编辑)

我尝试解决这个奇怪的问题一个星期。

故障排除命令输出:

ip rule
0:  from all lookup local 
32764:  from all fwmark 0x1 lookup ISP1
32765:  from all fwmark 0x2 lookup ISP2 
32766:  from all lookup main 
32767:  from all lookup default 

ip route show table ISP1
default via 192.168.1.1 dev eth1

ip route show table ISP2
default via 192.168.2.1 dev eth2

ip route show table main
192.168.2.0/24 dev eth2  proto kernel  scope link  src 192.168.2.2
192.168.1.0/24 dev eth1  proto kernel  scope link  src 192.168.1.2
192.168.0.0/24 dev eth0  proto kernel  scope link  src 192.168.0.1
169.254.0.0/16 dev eth0  scope link  metric 1000 
default via 192.168.1.1 dev eth1  metric 100
Run Code Online (Sandbox Code Playgroud)

我可以通过键入以下命令部分解决这个问题:

ip rule del fwmark 1 table ISP1
ip rule del fwmark 2 table ISP2
ip rule add from 192.168.0.0/24 table ISP1
Run Code Online (Sandbox Code Playgroud)

因此,我通过 ISP1 链接正确路由了来自本地网络的所有流量,并且所有 PC 都可以访问 Internet。

但我对基于数据包标记的路由感兴趣。

yoz*_*pho 10

经过一番努力,我终于找到了问题所在。

实际上这不是路由问题,脚本是正确的,但缺少某些内容。

此命令不足以禁用 rp_filter:

echo 0 >| /proc/sys/net/ipv4/conf/all/rp_filter
Run Code Online (Sandbox Code Playgroud)

所以从互联网返回的流量在 eth1 和 eth2 处下降。

当我通过添加以下命令为两个接口显式禁用 rp_filter 时:

echo 0 >| /proc/sys/net/ipv4/conf/eth1/rp_filter
echo 0 >| /proc/sys/net/ipv4/conf/eth2/rp_filter
Run Code Online (Sandbox Code Playgroud)

问题解决了,我让一切正常工作。

Linux 教程和文档并不总是完整的证明。