为什么 Linux 在错误的接口上应答 ARP?

为什么 Linux 在错误的接口上应答 ARP?

我有以下 Linux 网络设置:有一个 eth10 网络接口,其分配地址为 10.11.0.1/24。然后有一个 tap0 网络接口,其分配虚拟地址为 0.0.0.1/32(我分配了一个虚拟地址来启动接口),并且来自/到该接口的流量由最初创建 tap0 接口的用户空间程序控制。在 tap0 接口的另一侧,有一个用户空间程序通过原始套接字使用它,该程序查找 ARP 请求并构造响应。

现在,当用户空间程序构造一个 ARP 请求以请求 10.11.0.1 时,我希望另一个原始套接字用户空间程序能够回复它。但是,我收到了两个回复:一个来自原始套接字程序,另一个来自 Linux 内核。

显然,Linux 内核推断出 10.11.0.1 是属于自己的地址,因此作出了回复。但是,10.11.0.1 并不是 tap0 接口的地址。它是 eth10 接口的地址。

我的问题是:Linux 内核为什么会这样做?有没有办法在错误的接口上禁用 ARP 回复?

我对这个问题的临时解决方案是使用 10.11.0.1 以外的其他地址作为原始套接字/tap0 用途。但是,由于此系统应该是针对可以在任何开发机器上运行的应用程序的系统级测试,因此我无法保证不会与其他接口发生 IP 地址冲突。因此,最好在错误的接口上禁用 ARP 回复。

解决此问题的另一个方法是使用 netmap,为用户空间应用程序保留整个接口,防止内核在用户空间应用程序运行时将其用于任何用途。但我希望我的测试可以在没有 netmap 的情况下运行。

答案1

为什么您将 ARP 回复称为“错误”?系统的 IP 地址当然可以通过该接口访问。这就是一开始发送 ARP 回复的原因。不这样做可能会导致某些流量通过不太理想的路径流动,或者根本不流动。例如,tap0 可能是 VPN 连接,此 ARP 回复有助于确保到其他 IP 地址的流量将正确通过 VPN。

如果你确实想这样做,你可以设置系统控制寄存器 arp_ignorearp_announce达到期望值。

arp_announce - INTEGER
  Define different restriction levels for announcing the local
  source IP address from IP packets in ARP requests sent on
  interface:
  0 - (default) Use any local address, configured on any interface
  1 - Try to avoid local addresses that are not in the target's
  subnet for this interface. This mode is useful when target
  hosts reachable via this interface require the source IP
  address in ARP requests to be part of their logical network
  configured on the receiving interface. When we generate the
  request we will check all our subnets that include the
  target IP and will preserve the source address if it is from
  such subnet. If there is no such subnet we select source
  address according to the rules for level 2.
  2 - Always use the best local address for this target.
  In this mode we ignore the source address in the IP packet
  and try to select local address that we prefer for talks with
  the target host. Such local address is selected by looking
  for primary IP addresses on all our subnets on the outgoing
  interface that include the target IP address. If no suitable
  local address is found we select the first local address
  we have on the outgoing interface or on all other interfaces,
  with the hope we will receive reply for our request and
  even sometimes no matter the source IP address we announce.

  The max value from conf/{all,interface}/arp_announce is used.

  Increasing the restriction level gives more chance for
  receiving answer from the resolved target while decreasing
  the level announces more valid sender's information.

arp_ignore描述为:

arp_ignore - INTEGER
  Define different modes for sending replies in response to
  received ARP requests that resolve local target IP addresses:
  0 - (default): reply for any local target IP address, configured
  on any interface
  1 - reply only if the target IP address is local address
  configured on the incoming interface
  2 - reply only if the target IP address is local address
  configured on the incoming interface and both with the
  sender's IP address are part from same subnet on this interface
  3 - do not reply for local addresses configured with scope host,
  only resolutions for global and link addresses are replied
  4-7 - reserved
  8 - do not reply for all local addresses

  The max value from conf/{all,interface}/arp_ignore is used
  when ARP request is received on the {interface}

因此,您可能希望将其设置arp_ignore为 1(或可能是 2)并arp_announce设置为 2。

net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2

对于测试来说,这样做可能没问题。但实际生产系统可能会以您体验到的方式运行,您的程序需要能够处理这种情况。

相关内容