Linux

為什麼 Linux 在不正確的介面上響應 ARP?

  • February 23, 2017

我有以下 Linux 網路設置:有一個分配地址為 10.11.0.1/24 的 eth10 網路介面。然後是一個 tap0 網路介面,分配了虛擬地址 0.0.0.1/32(我分配了一個虛擬地址來啟動該介面),並且來自/到該介面的流量由最初創建 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,它為使用者空間應用程序保留整個介面,防止核心在使用者空間應用程序執行時使用它來做任何事情。但我希望我的測試在沒有網路圖的情況下執行。

為什麼稱 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.

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(或可能arp_announce為 2)和 2。

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

對於測試,這樣做可能很好。但是一個真正的生產系統可能會以您所經歷的方式執行,您的程序需要能夠處理這種情況。

引用自:https://serverfault.com/questions/834512