简体   繁体   English

为什么我的路由表存储一个mac地址?

[英]Why my routing tables stores a mac address?

I am a student learning computer network.我是一名学习计算机网络的学生。

Here's how my network look like:这是我的网络的样子:

   Router(NAT)
  /         \
My PC     My iPad

I have looked up my computer's routing table, and it shows the following:我查看了我的计算机的路由表,它显示以下内容:

$ netstat -r
Routing tables

Internet:
Destination        Gateway            Flags           Netif Expire
default            172.22.128.1       UGScg             en0
127.0.0.1          127.0.0.1          UH                lo0
172.22.128.1       0:74:9c:96:72:55   UHLWIir           en0   1200
172.22.161.13      ba:2e:b1:6f:69:39  UHLWI             en0    740

I confirmed that the ip 172.22.161.13 's gateway ba:2e:b1:6f:69:39 is the MAC address of my iPad.我确认 ip 172.22.161.13的网关ba:2e:b1:6f:69:39是我的 iPad 的 MAC 地址。

I have a few doubts:我有几个疑问:

  1. Shouldn't the routing table records ip address of next hop?路由表不应该记录下一跳的ip地址吗? how can it store a MAC address?它如何存储MAC地址?
  2. If the command netstat -r also lookups the arp table, how should it send packets to my iPad without going through the router?如果命令netstat -r也查找 arp 表,它应该如何在不通过路由器的情况下向我的 iPad 发送数据包?

The issue here is understanding the functionality of layer 2 and layer 3 in a network.这里的问题是理解网络中第 2 层和第 3 层的功能。

There is a difference between forwarding packets to the device located on the same subnet and on the device located on the different subnet.将数据包转发到位于同一子网的设备和位于不同子网的设备之间是有区别的。

I don't think there is a better definition of subnet, than devices that can communicate via a layer 2 protocol with each other.我认为没有比可以通过第 2 层协议相互通信的设备更好的子网定义了。 Routers are used to forward packets between devices in different subnets, that is, two devices in the same subnet do not communicate over the router, at least not over the "router" part of the router.路由器用于在不同子网的设备之间转发数据包,也就是说,同一子网中的两个设备不通过路由器进行通信,至少不通过路由器的“路由器”部分。

Devices that are located in the same subnet should be assigned the same IP prefix (ie, the bits covered by network mask are the same).位于同一子网中的设备应分配相同的 IP 前缀(即网络掩码覆盖的位相同)。 "Should" means that if it is not the case, then your network is configured wrong and may not work properly. “应该”表示如果不是这样,那么你的网络配置错误,可能无法正常工作。 This way your PC and your IPad can determine that they are on the same subnet and do not need to go over their default gateway.这样,您的 PC 和 IPad 可以确定它们在同一子网中,并且不需要通过默认网关进行 go。

Since your PC and your IPad are on the same subnet, they will talk to each other using layer 2. I am not sure from your configuration how many interfaces are actually wireless.由于您的 PC 和您的 IPad 位于同一子网中,因此它们将使用第 2 层相互通信。从您的配置中,我不确定实际上有多少接口是无线的。 If both interfaces are wireless, they may actually be capable of talking directly (i am not sure if wifi protocol in AP mode allows it, i think it does).如果两个接口都是无线的,它们实际上可能能够直接通话(我不确定 AP 模式下的 wifi 协议是否允许,我认为可以)。 If your PC is connected over Ethernet, then your Router also acts as a switch/bridge (which is layer 2 device) and forwards packets according to ethernet specification.如果您的 PC 通过以太网连接,那么您的路由器还充当交换机/网桥(这是第 2 层设备)并根据以太网规范转发数据包。 You can learn about it by googling MAC learning.您可以通过谷歌搜索 MAC 学习来了解它。

This should answer the second question, now to the first one.这应该回答第二个问题,现在回答第一个问题。

Actually, I wonder why the table is storing IP addresses.实际上,我想知道为什么该表存储 IP 地址。 IP address of the next hop is not used in forwarding. IP 下一跳地址不用于转发。 First, note, that communication between devices which are two hops from layer 3 (ie, routers) standpoint happens using layer 2. When a device makes routing decision, in takes the destination address from the IP header and looks up next hop, where what the device needs is a) outgoing interface b) how to reach next hop using layer 2 protocol of the interface.首先,请注意,从第 3 层(即路由器)的角度来看,距离为两跳的设备之间的通信使用第 2 层进行。当设备做出路由决策时,in 从 IP header 获取目标地址并查找下一跳,其中设备需要的是 a) 出接口 b) 如何使用接口的第 2 层协议到达下一跳。 Then the device constructs an appropriate layer 2 header and sends the packet to the next hop device, which more or less does the same.然后设备构造一个适当的第 2 层 header 并将数据包发送到下一跳设备,这或多或少是相同的。 So, actually your PC needs mac address of your router's interface to which it is connected to send the packet somewhere outside of the subnet.因此,实际上您的 PC 需要路由器接口的 MAC 地址,以便将数据包发送到子网之外的某个位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM