蓝色天空网mbsky.com · 知识 · 实践 · 创造网络协议 · 本地阅读预览
教程 / 计算机基础 / 网络协议

RARP 协议

解析 RARP 反向地址解析协议(RFC 903)如何以 0x8035 以太网帧与 OPER=3/4 报文为无盘工作站完成 MAC 到 IP 的查询,剖析其局限性以及向 BOOTP 和 DHCP 的演进替代。

本站独立整理的内容草稿。实例显示的是预期结果;浏览器、服务端运行与技术内容验收尚待完成,本页只用于阅读和图文核对。

学完这一章,你能够

RARP 定位与反向解析工作流程

RARP(Reverse Address Resolution Protocol,反向地址解析协议,RFC 903)用于将数据链路层的物理 MAC 地址反向解析为网络层 IPv4 地址。作为 ARP 的逆向操作,RARP 诞生于早期无盘工作站(Diskless Workstation)普及时代——这类设备没有本地硬盘保存 IP 配置,仅能从网卡固件读取自身 MAC 地址,因而需要借助广播请求与单播响应在启动阶段获知自己的 IP 地址。

RARP 的交互复用与 ARP 相同的 28 字节分组布局,但在以太网帧头采用独立类型号 `0x8035`:第一步,无盘设备加电后向局域网广播发送 RARP 请求包(操作码 `OPER=3`,发送方与目标硬件地址均填入自身 MAC,IP 字段填 `0.0.0.0`),询问“谁知道我的 MAC 对应的 IP 地址?”;第二步,局域网内专门维护静态 MAC-IP 映射表的 RARP 服务器检索匹配记录,构造 RARP 响应包(操作码 `OPER=4`)并以单播形式发回请求主机;第三步,设备提取应答中的目标 IP 地址配置本机网络接口,正式接入 IP 通信。

核心特性与历史应用场景

RARP 具有四项核心特征:实现数据链路层 MAC 地址向网络层 IP 地址的逆向映射;沿用广播请求与单播应答的局域网交互模式;专门面向启动阶段无持久化存储的无盘工作站;并且直接封装在二层以太网帧中,仅限单一局域网广播域内使用,无法被三层路由器跨网段转发。

在历史网络运维中,RARP 主要应用于两类场景:一是企业或高校机房中批量部署的无盘工作站开机自举获取 IP 地址;二是在 BOOTP 与 DHCP 尚未部署的早期局域网环境中,作为最基础的集中式静态 IP 下发手段。

协议局限性与 BOOTP、DHCP 演进替代

随着网络规模扩大,RARP 暴露出三项难以克服的局限性:第一,功能过于单一,响应报文只能携带 32 位 IP 地址,无法同时下发子网掩码、默认网关、DNS 服务器或启动镜像路径等必要参数;第二,运维复杂度高,由于 RARP 运行在数据链路层而不能跨路由器中继,管理员不得不在每一个物理子网中单独部署并手工维护 RARP 服务器;第三,安全性薄弱,报文无认证校验机制,极易遭局域网内恶意节点伪造 RARP 响应实施欺骗。

正是由于上述瓶颈,RARP 已被运行在 UDP/IP 之上的更完善协议全面取代:先是 BOOTP(Bootstrap Protocol,RFC 951),借助 UDP 中继支持跨子网并为无盘主机一次性提供 IP、掩码、网关及引导文件信息;随后演进为现代网络标准的 DHCP(Dynamic Host Configuration Protocol),在兼容 BOOTP 基础上进一步引入地址池动态分配与租期回收机制。本站离线脚本实现了 RFC 903 28 字节 RARP 请求/应答编解码、未登记 MAC 与伪造应答风险检验以及 RARP/BOOTP/DHCP 能力对比。

离线解析 RFC 903 28 字节 RARP 反向请求/应答帧,校验广播域限制与 BOOTP/DHCP 替代对比

python · 阅读源码
"""Offline network protocol verification lab for Chapter 21 (rarp-protocol)."""
import ipaddress
import json
import struct

VERSION = "CPython 3.12.14 offline network protocol frame & state-machine verifier"

RARP_FMT = "!HHBBH6s4s6s4s"
ETHERTYPE_ARP = 0x0806
ETHERTYPE_RARP = 0x8035


def mac_to_bytes(mac: str) -> bytes:
    return bytes(int(x, 16) for x in mac.split(":"))


def bytes_to_mac(raw: bytes) -> str:
    return ":".join(f"{b:02x}" for b in raw)


def pack_rarp(oper: int, sha: str, spa: str, tha: str, tpa: str) -> bytes:
    return struct.pack(
        RARP_FMT,
        1,
        0x0800,
        6,
        4,
        oper,  # 3=RARP Request, 4=RARP Reply
        mac_to_bytes(sha),
        ipaddress.IPv4Address(spa).packed,
        mac_to_bytes(tha),
        ipaddress.IPv4Address(tpa).packed,
    )


def unpack_rarp(pkt: bytes) -> dict:
    htype, ptype, hlen, plen, oper, sha, spa, tha, tpa = struct.unpack(RARP_FMT, pkt)
    return {
        "htype": htype,
        "ptype": ptype,
        "hlen": hlen,
        "plen": plen,
        "oper": oper,
        "sha": bytes_to_mac(sha),
        "spa": str(ipaddress.IPv4Address(spa)),
        "tha": bytes_to_mac(tha),
        "tpa": str(ipaddress.IPv4Address(tpa)),
    }


def case_rarp_28byte_reverse_lookup():
    client_mac = "02:00:5e:10:20:30"
    server_mac = "02:00:5e:00:00:01"
    server_ip = "10.0.5.1"
    rarp_table = {client_mac: "10.0.5.42"}

    # Diskless workstation broadcasts RARP Request (OPER=3) with its own MAC in both SHA and THA
    req = pack_rarp(3, client_mac, "0.0.0.0", client_mac, "0.0.0.0")
    req_d = unpack_rarp(req)
    assigned_ip = rarp_table[req_d["tha"]]

    # RARP server unicasts RARP Reply (OPER=4) with assigned TPA
    rep = pack_rarp(4, server_mac, server_ip, req_d["tha"], assigned_ip)
    rep_d = unpack_rarp(rep)

    expected = {
        "ethertype_hex": "0x8035",
        "packet_bytes": 28,
        "req_oper": 3,
        "rep_oper": 4,
        "assigned_ip": "10.0.5.42",
        "unicast_dst_mac": client_mac,
    }
    actual = {
        "ethertype_hex": hex(ETHERTYPE_RARP),
        "packet_bytes": len(req),
        "req_oper": req_d["oper"],
        "rep_oper": rep_d["oper"],
        "assigned_ip": rep_d["tpa"],
        "unicast_dst_mac": rep_d["tha"],
    }
    return {
        "name": "rarp_28byte_reverse_lookup",
        "summary": "打包并解析 RFC 903 28 字节 RARP 反向请求(OPER=3)与单播应答(OPER=4)实现无盘站 MAC→IP 映射",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_rarp_unknown_mac_and_spoof_risk():
    rarp_table = {"02:00:5e:10:20:30": "10.0.5.42"}
    unknown_mac = "02:00:5e:99:99:99"
    has_mapping = unknown_mac in rarp_table

    # RARP has no authentication: a rogue responder can answer first with an invalid IP
    rogue_rep = unpack_rarp(pack_rarp(4, "de:ad:00:00:00:01", "10.0.5.99", "02:00:5e:10:20:30", "10.0.5.250"))
    expected = {
        "unknown_mac_found": False,
        "rarp_has_auth_field": False,
        "rogue_reply_oper": 4,
        "rogue_assigned_ip": "10.0.5.250",
    }
    actual = {
        "unknown_mac_found": has_mapping,
        "rarp_has_auth_field": False,
        "rogue_reply_oper": rogue_rep["oper"],
        "rogue_assigned_ip": rogue_rep["tpa"],
    }
    return {
        "name": "rarp_unknown_mac_and_spoof_risk",
        "summary": "验证未登记 MAC 查询无结果以及 RARP 协议缺少认证字段易遭伪造应答欺骗的局限",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_rarp_l2_broadcast_domain_limit():
    # RARP runs directly over Ethernet (L2 EtherType 0x8035), so L3 routers do not forward it across subnets
    router_forwards_l2_broadcast = False
    subnets = ["VLAN10", "VLAN20", "VLAN30"]
    required_rarp_servers = len(subnets)
    required_dhcp_servers_with_relay = 1

    expected = {
        "router_forwards_rarp": False,
        "rarp_servers_needed_for_3_subnets": 3,
        "dhcp_servers_needed_with_relay": 1,
    }
    actual = {
        "router_forwards_rarp": router_forwards_l2_broadcast,
        "rarp_servers_needed_for_3_subnets": required_rarp_servers,
        "dhcp_servers_needed_with_relay": required_dhcp_servers_with_relay,
    }
    return {
        "name": "rarp_l2_broadcast_domain_limit",
        "summary": "对比链路层 RARP 无法跨路由广播域部署与 DHCP 中继集中分配在多子网中的运维差异",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_rarp_vs_bootp_dhcp_capabilities():
    matrix = {
        "RARP": {"layer": "L2", "ip": True, "mask": False, "gateway": False, "dns": False, "dynamic_lease": False},
        "BOOTP": {"layer": "L7/UDP", "ip": True, "mask": True, "gateway": True, "dns": True, "dynamic_lease": False},
        "DHCP": {"layer": "L7/UDP", "ip": True, "mask": True, "gateway": True, "dns": True, "dynamic_lease": True},
    }
    expected = {
        "rarp_only_ip": True,
        "bootp_provides_full_static_cfg": True,
        "dhcp_adds_dynamic_lease": True,
    }
    actual = {
        "rarp_only_ip": matrix["RARP"]["ip"] and not (matrix["RARP"]["mask"] or matrix["RARP"]["gateway"] or matrix["RARP"]["dns"]),
        "bootp_provides_full_static_cfg": matrix["BOOTP"]["mask"] and matrix["BOOTP"]["gateway"] and not matrix["BOOTP"]["dynamic_lease"],
        "dhcp_adds_dynamic_lease": matrix["DHCP"]["dynamic_lease"],
    }
    return {
        "name": "rarp_vs_bootp_dhcp_capabilities",
        "summary": "核对 RARP、BOOTP 与 DHCP 在网络配置项完整度与动态租期管理上的演进替代关系",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_rarp_28byte_reverse_lookup(),
        case_rarp_unknown_mac_and_spoof_risk(),
        case_rarp_l2_broadcast_domain_limit(),
        case_rarp_vs_bootp_dhcp_capabilities(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
预期结果 · 待实际运行核验
  • 在 CPython 3.12.14 离线脚本中验证 EtherType 0x8035 与 OPER=3/4 反向 MAC→IP 查询、无认证欺骗风险、多子网部署成本及 RARP/BOOTP/DHCP 能力矩阵全部通过。
离线解析 RFC 903 28 字节 RARP 反向请求/应答帧,校验广播域限制与 BOOTP/DHCP 替代对比

所需环境:Windows x64、CPython 3.12.14 离线网络协议帧编解码与状态机验证脚本(纯本地内存执行,未发起外部网络连接)。

当前源码 SHA256:0936e482a94f4fa1ded35dc7b05cfcbcf7e170e5881450a1804d2cb94f78d96e

下载本站示例源码

本地运行

解压后按 README 说明在对应本地环境运行脚本。

下载RARP 协议实验

所需环境:Windows x64、CPython 3.12.14 离线网络协议帧编解码与状态机验证脚本(纯本地内存执行,未发起外部网络连接)。

ZIP SHA256:431139ca59368bcbaf3f4001401b30cd89e3fdf6ee565324f4f208ce89eec502

常见错误与边界

  • 实际端口、文档资源与应用配置都应和测试条件一致。

练习与解答

为什么在划分了多个子网的企业网络中,使用 RARP 需要在每个子网单独部署服务器,而 DHCP 却可以集中部署?

展开参考解答

RARP 直接工作在数据链路层(EtherType `0x8035`),三层路由器默认隔离二层广播且不支持 RARP 中继;DHCP 运行在 UDP/IP 之上,可借助路由器上的 DHCP Relay Agent 将请求转发给远端集中服务器。

规范与官方依据