Linux ARP缓存配置和状态查看命令

时间:2022-01-25 17:28:13
  • 查看Linux ARP缓存老化时间

cat /proc/sys/net/ipv4/neigh/eth0/base_reachable_time
同目录下还有一个文件gc_stale_time,官方解释如下:
Determines how often to check for stale neighbour entries.
When a neighbour entry is considered stale it is resolved again before sending data to it.
Defaults to 60 seconds.

  • 查看Linux ARP缓存状态

arp -a    #代码对应于ioctl(s, SIOCGARP, &arpreq),没法看到每条缓存的状态是REACHABLE还是STALE

/isam/slot_1101/run # arp -a
? (135.251.197.136) at 00:19:8f:5f:bd:87 [ether] on eth0
? (135.251.196.1) at 00:e0:b1:ca:5a:48 [ether] on eth0

ip neigh show    #实现上是通过另一种系统调用netlink来获取的

/proc/sys/net/ipv4/neigh/eth0 # ip neigh
135.251.197.136 dev eth0 lladdr 00:19:8f:5f:bd:87 REACHABLE
135.251.196.1 dev eth0 lladdr 00:e0:b1:ca:5a:48 STALE

  • 应用程序如何触发arp缓存的添加和刷新呢?

经过一系列测试,结论如下:

执行arping命令,无法添加新的arp缓存。但是可以把STALE的缓存刷新为REACHABLE状态。

arping有如下选项,是不是我少加了什么选项呢?注释符#后面的解释是man命令里面摘过来的。

arping -I eth0 135.251.196.1 -c 10

/isam/slot_default/run # arping -h
arping: invalid option -- 'h'
BusyBox v1.22.1 (2015-07-11 21:41:15 CEST) multi-call binary.

Usage: arping [-fqbDUA] [-c CNT] [-w TIMEOUT] [-I IFACE] [-s SRC_IP] DST_IP

Send ARP requests/replies

-f Quit on first ARP reply    #Finish after the first reply confirming that target is alive.
-q Quiet    #Quiet output. Nothing is displayed.
-b Keep broadcasting, don't go unicast    #Send only MAC level broadcasts. Normally arping starts from sending broadcast, and switch to unicast after reply received.
-D Duplicated address detection mode    #Duplicate address detection mode (DAD). See RFC2131, 4.4.1.  Returns 0, if DAD succeeded i.e. no replies are received    #如果是DAD模式,则原源主机地址一直没有设置,那么就意味着源地址为0.0.0.0。这样当目的主机接到之后,就会向0.0.0.0发送回复,就相当于广播给以太网中所有的主机。因为进行D重复地址检测模式的原因很可能是由于源主机的IP地址没有设置,从而想设置自身的IP地址。在IP地址没有设置的时候,主机只能接受到地址为0.0.0.0的广播信号。
-U Unsolicited ARP mode, update your neighbors    #Unsolicited ARP mode to update neighbours<80><99> ARP caches.  No replies are expected.    #为了更新以太网邻居的ARP快速缓存而主动进行的ARP。也就是免费ARP(gratuitous ARP),即请求自己的ip地址的mac地址。
-A ARP answer mode, update your neighbors    #与-U选项类似,但是发送的是ARP 回复报文,而不是ARP请求报文。
-c N Stop after sending N ARP requests
-w TIMEOUT Time to wait for ARP reply, seconds
-I IFACE Interface to use (default eth0)
-s SRC_IP Sender IP address
DST_IP Target IP address

从以上选项可以看出,我们并没有漏掉什么选项,所以内核就是这么设计的。

还进行过如下测试,Linux shell ping竟然只能添加arp缓存,但是并不能把STALE的缓存刷新为REACHABLE状态。而且ping是通的,这也说明了STALE还在被引用。