为什么 Linux 在不正确的接口上应答 ARP?

juh*_*ist 9 linux arp linux-networking linux-kernel

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

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

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

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

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

这个问题的另一个解决方案是使用 netmap 为用户空间应用程序保留整个接口,防止内核在用户空间应用程序运行时使用它。但我希望我的测试在没有 netmap 的情况下运行。

Mic*_*ton 12

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

如果您真的想这样做,您可以将sysctls arp_ignore和设置arp_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.
Run Code Online (Sandbox Code Playgroud)

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}
Run Code Online (Sandbox Code Playgroud)

因此,您可能希望设置arp_ignore为 1(或可能为 2)和arp_announce2。

net.ipv4.conf.all.arp_ignore=1
net.ipv4.conf.all.arp_announce=2
Run Code Online (Sandbox Code Playgroud)

对于测试,这样做可能没问题。但是真正的生产系统很可能会按照您所经历的方式运行,您的程序需要能够处理这种情况。

  • “为什么你称 ARP 回复‘错误’?” ——你为什么这么问?很明显为什么人们会认为这对我来说是错误的。实际上有 2 个主机模型:https://en.wikipedia.org/wiki/Host_model (2认同)