CS 168 · DISCUSSION 09 · GUIDED REASONING WORKBOOK

主机从本地链路到公网

统一 scenario:一台刚连 Wi-Fi 的 laptop 第一次访问公网 server。每一跳同时写 L2、L3、L4 字段,避免把 next hop 与 end-to-end destination 混掉。

现在轮到你推

无需离开 CourseStack:先预测,再在表格、时间线或状态空间里完成推导;卡住时逐层打开提示,最后才展开解释与变式。

已阅读 Spring 2026 worksheet 与 official solution;以下是原创等价练习。官方 IDs 用于 coverage,对精确原题请回到页末 PDF。

忘记机制?回到 L18 ARP/DHCP/NAT →

对应官方 1.1、1.2、1.3、1.4、1.5、1.6、1.7、1.8

1 · 一个未知量对应一个协议

Why the official problem exists:用“我缺什么 state”选择 DHCP、ARP、NAT,而不是背层次。

Laptop 只知道自己的 NIC MAC;尚无 IP、mask、gateway、DNS。目标稍后是 8.8.8.8:443。

先预测:谁告诉 laptop 自己的 IP 与 default gateway?

Work It Out

为 own IP、own MAC、gateway MAC、public source tuple 四个未知量填 producer、scope、lifetime。

Hint 1 · Concept

own MAC 已由 NIC 配置。

Hint 2 · State / Invariant

ARP 的问题形式是“本地这个 IP 的 MAC?”

Hint 3 · First Step

NAT mapping 在第一枚 outbound flow packet 时创建。

Reveal · 展开完整推导

DHCP 给 IP/mask/gateway/DNS 与 lease;NIC 已知自身 MAC;ARP 得 gateway MAC;NAT 用 private tuple 建 public tuple。port 可复用区分共享 public IP 后的 flows,但这削弱纯端到端透明性。

Why This Works

协议是 state discovery/translation 的责任分工。

Variation

gateway ARP entry 已缓存但 DHCP lease 过期,哪些步骤能跳过,哪些必须重做?

对应官方 2.1、2.2、2.3、2.4、2.5、2.6、2.7、2.8

2 · 第一次公网访问:DHCP → ARP → frame → routing

Why the official problem exists:检查端到端 IP 不变、逐跳 MAC 改变和 next-hop decision。

Laptop A=10.0.0.10/24, MAC AA;gateway R=10.0.0.1, MAC RR;server C=203.0.113.8。A 尚无 ARP entry。

先预测:A 发往 C 的第一帧 dst MAC 是?

Work It Out

阶段src MACdst MACsrc IPdst IP
ARP requestAAff:ff:ff:ff:ff:ff10.0.0.10询问10.0.0.1
A→R dataAA____________
R 下一链路R-nextnext-hop________
Hint 1 · Concept

先用 mask 判断同 subnet。

Hint 2 · State / Invariant

ARP request broadcast,reply unicast。

Hint 3 · First Step

router 去掉旧 L2 frame,再为新 link 封装;IP endpoints 保持。

Reveal · 展开完整推导

A 发现 C 非本地,ARP 广播询问 gateway;R 单播回复。data frame 是 AA→RR,IP 仍 10.0.0.10→203.0.113.8。R 查 FIB,每跳重建 MAC header;除 NAT 边界外 IP endpoints 不变。

Why This Works

routing 选择 next-hop IP/interface,ARP 把该本地 next hop 解析成 frame destination。

Variation

C 改为 10.0.0.88,同一表格哪些字段和 packets 消失?

对应官方 3.1、3.2

3 · NAT table 是可逆 demultiplex state

Why the official problem exists:追 outbound 与 reply 的 5-tuple 改写并说明为什么 port 不只是 host multiplexing。

A=10.0.0.10:50000,NAT public=198.51.100.7,分配 port=60000,server=8.8.8.8:443。

先预测:outbound 经过 NAT 后 destination tuple?

Work It Out

方向/位置src IP:portdst IP:portNAT lookup
outbound before10.0.0.10:500008.8.8.8:443create
outbound after________private→public
reply before________public port
reply after________public→private
Hint 1 · Concept

outbound 改 source tuple。

Hint 2 · State / Invariant

reply 的 destination 是 NAT public tuple。

Hint 3 · First Step

mapping 允许回复恢复 private destination。

Reveal · 展开完整推导

outbound source→198.51.100.7:60000,destination不变。reply 是 8.8.8.8:443→198.51.100.7:60000;NAT 查表改 destination 为 10.0.0.10:50000。

Why This Works

共享 address 依靠 port-indexed state 把 reply demultiplex 回内部 flow。

Variation

两台内网 host 都用 source port 50000 访问同一 server,NAT 必须如何分配 public ports?

Closed-book reconstruction

新 laptop 从无配置到 HTTPS reply,画 DHCP、ARP、outbound frame、NAT mapping、reply reverse mapping;每条消息填 MAC/IP/port。

一手资料