linux - 通过 linux 路由表 Ping 不工作[或]这是怎么回事?

标签 linux networking routing ip ping

长题简而言之:

Ping over r1-r4-r2 path works using 10.0.1.* or 10.0.2.* IP addresses, but fails if we alter the path to r1-r3-r2 using 1.0.0.* or 1.0.1.* IP addresses for the exactly same packets (except for the fact that packets' src and dst IP fields are changed from 10.* to 1.* and vice-versa at s1 and s2 respectively). Why?


问题详细:

我有一个小拓扑如下

 h1 -- s1 -- r1 -- r4 -- r2 -- s2 -- h2
              \         /
               \       /
                \     /
                  r3

s 是 OpenvSwitch 实例,而 r 是 Ubuntu 16 Linux 机器。

IP 地址是:

h1-eth0 - 10.0.1.10/24
s1      - 10.0.1.50/24
h2-eth0 - 10.0.2.10/24
s2      - 10.0.2.50/24
r1-eth0 - 10.0.1.1/24
r1-eth1 - 10.0.11.2/24
r1-eth2 - 10.0.12.2/24
r2-eth0 - 10.0.2.1/24
r2-eth1 - 10.0.13.1/24
r2-eth2 - 10.0.5.1/24
r3-eth0 - 10.0.12.1/24
r3-eth1 - 10.0.5.2/24
r4-eth0 - 10.0.11.1/24
r4-eth1 - 10.0.13.2/24

可以看到,r1和r2之间有两条相似的路径。我添加了以下静态条目。

r1

sudo ip route add 10.0.2.0/24 via 10.0.11.1

r2

sudo ip route add 10.0.1.0/24 via 10.0.13.2

r4

sudo ip route add 10.0.1.0/24 via 10.0.11.2
sudo ip route add 10.0.2.0/24 via 10.0.13.1

h1 和 h2 之间的 ping 按预期工作。现在,由于交换机是 OVS(因此支持 OpenFlow),我在 s1 中安装条目以将目标 IP 映射到不同的子网

i.e. the IP 10.0.1.10 would be mapped to 1.0.0.10 while the IP 10.0.2.10 would be mapped to 1.0.1.10 when such a packet is received at s1, while the destination IPs would be mapped back to original at s2.

(我已经检查过这些条目确实正确并且按预期工作。另外我添加这个条目只是为了匹配 ICMP 数据包)。当 h1 发送 ping 回复时,将执行类似的过程。

除此之外,我还在路由器中安装了静态路由来路由这些 IP。

r1

sudo ip route add 1.0.0.0/24 via 10.0.1.50
sudo ip route add 1.0.1.0/24 via 10.0.12.1

r2

sudo ip route add 1.0.0.0/24 via 10.0.5.2
sudo ip route add 1.0.1.0/24 via 10.0.2.50

r3

sudo ip route add 1.0.0.0/24 via 10.0.12.2
sudo ip route add 1.0.1.0/24 via 10.0.5.1

现在,如果我从 h2 ping h1,数据包以目标 IP 10.0.1.10 开始,它映射到 s2 的 1.0.0.10,r2 路由它并将其发送到 r3,r3 路由它并发送到 r1。 但是 r1,即使在一个接口(interface)接收到数据包并且在 Linux 路由表中有匹配的条目后,也不会路由和转发数据包。

甚至 ip route get 也会输出数据包应该转发到的正确端口。 ip 表 中也没有防火墙条目。


一些附加信息:

  • 如果我将新添加的路由条目更改为使用原始路径 r1-r4-r2(即,我们使用映射的 ip 在这条路径上路由),它的行为符合预期并且 ping 符合预期。

  • 或者,如果我在 r1 和
    中更改 10.0.2.0/24 的旧路由条目 r2 中的 10.0.1.0/24(现在理想情况下甚至不必与新的匹配 数据包,因为它们的目标 IP 在 1.0.0.* 范围内或仅在 1.0.1.* 范围内)以使用 新路径 r1-r3-r4 连同这个映射的 IP 数据包,ping 之间 r2 和 r1 按预期工作。

可能需要的详细信息:

最终的路由表如下:

r1

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.11.1       0.0.0.0         UG    0      0        0 eth1
1.0.0.0         10.0.1.10       255.255.255.0   UG    0      0        0 eth0
1.0.1.0         10.0.12.1       255.255.255.0   UG    0      0        0 eth2
10.0.1.0        0.0.0.0         255.255.255.0   U     1      0        0 eth0
10.0.2.0        10.0.11.1       255.255.255.0   UG    0      0        0 eth1
10.0.11.0       0.0.0.0         255.255.255.0   U     1      0        0 eth1
10.0.12.0       0.0.0.0         255.255.255.0   U     1      0        0 eth2

r2

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.13.2       0.0.0.0         UG    0      0        0 eth1
1.0.0.0         10.0.5.2        255.255.255.0   UG    0      0        0 eth1
1.0.1.0         10.0.2.50       255.255.255.0   UG    0      0        0 eth0
10.0.1.0        10.0.13.2       255.255.255.0   UG    0      0        0 eth1
10.0.2.0        0.0.0.0         255.255.255.0   U     1      0        0 eth0
10.0.5.0        0.0.0.0         255.255.255.0   U     1      0        0 eth2
10.0.13.0       0.0.0.0         255.255.255.0   U     1      0        0 eth1

r3

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.5.1        0.0.0.0         UG    0      0        0 eth1
1.0.0.0         10.0.12.2       255.255.255.0   UG    0      0        0 eth0
1.0.1.0         10.0.5.1        255.255.255.0   UG    0      0        0 eth1
10.0.1.0        10.0.12.2       255.255.255.0   UG    0      0        0 eth0
10.0.2.0        10.0.5.1        255.255.255.0   U     1      0       0 eth1
10.0.5.0        0.0.0.0         255.255.255.0   U     1      0        0 eth1
10.0.12.0       0.0.0.0         255.255.255.0   U     1      0        0 eth0

r4

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.0.1     0.0.0.0         UG    0      0        0 eth4
1.0.0.0         10.0.11.2       255.255.255.0   UG    0      0        0 eth0
1.0.1.0         10.0.13.1       255.255.255.0   UG    0      0        0 eth1
10.0.1.0        10.0.11.2       255.255.255.0   UG    0      0        0 eth0
10.0.2.0        10.0.13.1       255.255.255.0   UG    0      0        0 eth1
10.0.11.0       0.0.0.0         255.255.255.0   U     1      0        0 eth0
10.0.13.0       0.0.0.0         255.255.255.0   U     1      0        0 eth1
192.168.0.0     0.0.0.0         255.255.255.0   U     1      0        0 eth4

注意:192.168.0.* 是连接到外部 Internet 的子网。

您认为问题是什么?看着这个问题,我完全感到困惑。

最佳答案

此处 Linux 路由的行为符合预期。

反向路径过滤器
的标志 即 /proc/sys/net/ipv4/conf/<interfacename>/rp_filter默认情况下打开(通过将值设置为 1)。

反向路径过滤器是作为 Linux 内核的一项安全功能提供的。 一个常见的例子是私有(private) IP 空间逃逸到 Internet 上。如果您有一个路由为 195.96.96.0/24 的接口(interface),您不希望来自 212.64.94.1 的数据包到达那里。因此,如果标志设置为 1,内核将丢弃这样的数据包。

更正式地说,

Reverse path filtering is a mechanism adopted by the Linux kernel to check whether the source IP address of the packet that is been received is routable.

So in other words, when a machine with reverse path filtering enabled receives a packet, the machine will first check whether the source of the received packet is reachable through the interface it came in.

  • If it is routable through the interface which it came, then the machine will accept the packet.
  • If it is not routable through the interface, which it came, then the machine will drop that packet.

Latest kernels provide one more option value of 2. This option is slightly more liberal in terms of accepting traffic.

If the received packet's source address is routable through any of the interfaces on the machine, the machine will accept the packet.

关于linux - 通过 linux 路由表 Ping 不工作[或]这是怎么回事?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40996715/

相关文章:

php - 在 Windows 机器上为 Linux 安装 PHP Eclipse 编辑器

c# - 在 asp.net web 表单中获取消息的网页导航被取消

c# - 奇怪的网络设备

docker - Traefik 路径中的服务不会重定向到服务路径以获取其他请求

ruby-on-rails - 除了在 Ruby on Rails 中使用 namespace 的操作之外,如何重写 URL?

linux - 我正在尝试发送邮件,该邮件将重定向同一目录中 logfile.txt 中日志文件的内容。但它失败

java - 为什么 mmap()(内存映射文件)比 read() 快

php - 使用 Yii::app()->user 时出现问题

c - 路由设置 : SIOCADDRT failed: Inappropriate ioctl for device

c# - 网站在 MVC Controller 上请求 "Authentication Required",而不是其他 Controller