# ARP

链路层通信根据48bit以太网地址（硬件地址）来确定目的接口，而地址解析协议负责32bit IP地址与48bit以太网地址之间的映射：

* （1）ARP负责将IP地址映射到对应的硬件地址
* （2）RARP负责相反的过程，通常用于无盘系统。

## ARP高速缓存

ARP高效运行的关键是每台主机上都有一个ARP高速缓存，缓存中每一项的生存时间一般为20分钟，但不完整表项超时时间一般为3分钟（如192.168.13.254）。

```bash
# arp -a

? (192.168.0.16) at 00:1b:21:b9:9f:d4 [ether] on eth0
? (192.168.0.6) at 00:1b:21:b9:9f:d4 [ether] on eth0
? (192.168.13.233) at 00:16:3e:01:7a:b2 [ether] on eth0
? (192.168.13.254) at <incomplete> on eth0
```

可以通过arp命令来操作ARP高速缓存：

* arp 显示当前的ARP缓存列表。
* arp -s ip mac 添加静态ARP记录，如果需要永久保存，应该编辑/etc/ethers文件。
* arp -f 使/etc/ethers中的静态ARP记录生效。

## ARP分组格式

![](/files/-LDZS6KTxlltQ9PE0eTs)

其中：

* ARP协议的帧类型为0x0806
* 硬件类型：1表示以太网地址
* 协议类型：0x800表示IP协议
* 硬件地址长度：值为6
* 协议地址长度：值为4
* op：1为ARP请求，2为ARP应答，3为RARP请求，4为RARP应答
* 对于ARP请求来说，目的端硬件地址为广播地址f:ff:ff:ff:ff:ff），由ARP相应的主机填入。

一个完整ARP请求应答的抓包：

```bash
# tcpdump -e -p arp -n -vv
21:08:10.329163 00:16:3e:01:79:43 > ff:ff:ff:ff:ff:ff, ethertype ARP (0x0806), length 42: Ethernet (len 6), IPv4 (len 4), Request who-has 192.168.14.23 tell 192.168.13.43, length 28
21:08:10.329626 00:16:3e:01:7b:17 > 00:16:3e:01:79:43, ethertype ARP (0x0806), length 60: Ethernet (len 6), IPv4 (len 4), Reply 192.168.14.23 is-at 00:16:3e:01:7b:17, length 46
```

## ARP代理

当向位于不同网络的主机发送ARP请求时，由两个网络之间的路由器响应该ARP请求，这个过程称为ARP代理。

ARP代理也称为混合ARP：通过两个网络之间的路由器可以互相隐藏物理网络。

## 免费ARP

主机发送ARP请求查找自己的IP地址。通常有两个用途：

* （1）确认网络中是否有其他主机设置了相同的IP地址；
* （2）当主机的物理地址改变了，可以通过免费ARP更新更新路由器和其他主机中的高速缓存。

## RARP

1. RARP通常用于无盘系统，无盘系统从物理网卡上读到硬件地址后，发送一个RARP请求查询自己的IP地址。
2. RARP的协议格式：与ARP协议一致，只不过帧类型代码为0x8035
3. RARP使用链路层广播，这样阻止了大多数路由器转发ARAP请求，只返回很小的信息，即IP地址。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sdn.feisky.xyz/wang-luo-ji-chu/index/arp.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
