【Network】路由(Routing)

Posted by 西维蜀黍 on 2021-04-25, Last Modified on 2021-09-21

路由(Routing)

Routing is the process of selecting a path for traffic in a network or between or across multiple networks.

A route is a defined pair of addresses which represent the “destination” and a “gateway”. The route indicates that when trying to get to the specified destination, send the packets through the specified gateway. There are three types of destinations: individual hosts, subnets, and “default”. The “default route” is used if no other routes apply. There are also three types of gateways: individual hosts, interfaces, also called links, and Ethernet hardware (MAC) addresses. Known routes are stored in a routing table.

Delivery schemes

Routing schemes differ in how they deliver messages:

  • Unicast delivers a message to a single specific node using a one-to-one association between a sender and destination: each destination address uniquely identifies a single receiver endpoint.
  • Broadcast delivers a message to all nodes in the network using a one-to-all association; a single datagram from one sender is routed to all of the possibly multiple endpoints associated with the broadcast address. The network automatically replicates datagrams as needed to reach all the recipients within the scope of the broadcast, which is generally an entire network subnet. Multicast delivers a message to a group of nodes that have expressed interest in receiving the message using a one-to-many-of-many or many-to-many-of-many association; datagrams are routed simultaneously in a single transmission to many recipients. Multicast differs from broadcast in that the destination address designates a subset, not necessarily all, of the accessible nodes.
  • Anycast delivers a message to any one out of a group of nodes, typically the one nearest to the source using a one-to-one-of-many association where datagrams are routed to any single member of a group of potential receivers that are all identified by the same destination address. The routing algorithm selects the single receiver from the group based on which is the nearest according to some distance measure.
  • Geocast delivers a message to a group of nodes in a network based on their geographic location. It is a specialized form of multicast addressing used by some routing protocols for mobile ad hoc networks.

Unicast is the dominant form of message delivery on the Internet.

路由器(Router)

路由器(Router)是一种电讯网络设备,提供路由与转送两种重要机制,可以决定数据包从来源端到目的端所经过的路由路径(host到host之间的传输路径),这个过程称为路由;将路由器输入端的数据包移送至适当的路由器输出端(在路由器内部进行),这称为转送。路由工作在OSI模型的第三层——即网络层,例如网际协议(IP)。

路由器、交换机(Switch)和集线器(Hub)

路由器与交换机的差别,路由器是属于OSI第三层的产品,交換机是OSI第二层的产品。

第二层(交換机)的产品功能在于,将网络上各个电脑的MAC地址记在MAC地址表中,当局域网中的电脑要经过交換机去交换传递数据时,就查询交換机上的MAC地址表中的信息,将数据包发送给指定的电脑,而不会像第一层的产品(如集线器)每台在网络中的电脑都发送。

而路由器除了有交換机的功能外,更拥有路由表作为发送数据包时的依据,在有多种选择的路径中选择最佳的路径。此外,并可以连接两个以上不同网段的网络,而交換机只能连接两个。并具有IP分享的功能,如:区分哪些数据包是要发送至WAN。路由表存储了(向前往)某一网络的最佳路径,该路径的“路由度量值”以及下一个(跳路由器)。参考条目路由获得这个过程的详细描述。

路由表(Routing Table)

In computer networking a routing table, or routing information base (RIB), is a data table stored in a router or a network host that lists the routes to particular network destinations, and in some cases, metrics (distances) associated with those routes. The routing table contains information about the topology of the network immediately around it.

The construction of routing tables is the primary goal of routing protocols. Static routes are entries made in a routing table by non-automatic means and which are fixed rather than being the result of routing protocols and associated network topology-discovery procedures.

路由表创建的主要目标是为了实现路由协议和静态路由选择。

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         192.168.2.1     0.0.0.0         UG    0      0        0 wan
192.168.1.0     *               255.255.255.0   U     0      0        0 br-lan
192.168.2.0     *               255.255.255.0   U     0      0        0 wan

各列字段说明:

含义
Destination 目标网络段(如192.168.1.0 就表示 192.168.1.0)或目标主机。Destination 为 default(0.0.0.0)时,表示这个是默认网关,所有未在路由表中声明的流量都会发到这个网关(这里是 192.168.19.1)
Gateway 网关地址,0.0.0.0 (或 *)表示当前记录对应的 Destination 跟本机在同一个网段,因此通信时不需要经过网关
Genmask Destination 字段的网络掩码,当Destination 是主机时,Genmask为 255.255.255.255,是默认路由时(即 Destination 为 default 时)会设置为 0.0.0.0
Flags 标记,含义参考表格后面的解释
Metric 路由距离,到达指定网络所需的中转数,是大型局域网和广域网设置所必需的 (不在Linux内核中使用。)
Ref 路由项引用次数 (不在Linux内核中使用,恒为0)
Use 该路由被使用的次数,可以粗略估计通向指定网络地址的网络流量。
Iface 网络接口名字,例如 eth0

​ Flags 含义:

  • U(Up):表示该路由是活跃的,因此可以被使用
  • H(Host):表示目标是一个主机(而不是一个网络段)
    • 如果没有设置H标志,说明 Destination 是一个网络段(主机号部分为0)。
    • 当为某个目的IP地址搜索路由表时,主机地址项必须与目的地址完全匹配,而网络地址项只需要匹配目的地址的网络号和子网号就可以了。
  • G(Gateway):表示需要经过网关
    • 如果没有设置该标志,说明目的地是直接相连的。标志G是非常重要的,因为它区分了间接路由和直接路由(对于直接路由来说是不设置标志G的)
  • R :恢复动态路由产生的表项
  • D :表示该路由是由重定向报文创建的。
  • M:表示该路由已被重定向报文修改。
  • ! :表示拒绝路由
  • S(Static):This route was configured manually, not automatically generated by the system.

Linux 内核的路由种类

主机路由(Flag -> H)

路由表中指向单个 IP 地址或主机名的路由记录,其 Flags 字段为 H。下面示例中,表示对于目的地为 10.0.0.10 这个主机的流量,路由到网关 10.139.128.1 (包含 G 说明需要经过网关,如果不包含 G ,则说明不需要经过网关,这意味着 eth0 interface 与该目的地主机直接相连):

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.10       10.139.128.1    255.255.255.255 UGH   0      0        0 eth0
...

网络路由(Destination)

表示路由到一个网络段(通常包含多个主机)。下面示例中,对于目的地为 10.0.0.0/24 这个网络的流量,路由到网关 10.139.128.1 :

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
10.0.0.0        10.139.128.1    255.255.255.0   UG    0      0        0 eth0

默认路由

当目标主机的 IP 地址或网络不在路由表中时,数据包就被路由到默认路由(默认网关)。默认路由的 Destination 是 default 或 0.0.0.0。

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         gateway         0.0.0.0         UG    0      0        0 eth0

路由类型

下表显示了不同的路由类型,以及各个路由类型分别最适用于哪种网络方案。

路由类型 最适用于
静态 小型网络、从缺省路由器获取其路由的主机,以及仅需要知晓接下来几个跃点上一个或两个路由器的缺省路由器。
动态 较大的互联网络、具有多个主机的本地网络中的路由器以及大型自治系统上的主机。动态路由是大多数网络中系统的最佳选择。
组合的静态和动态路由 将静态路由网络和动态路由网络连接在一起的路由器,以及将内部自治系统与外部网络连接在一起的边界路由器。将系统上的静态路由和动态路由组合在一起是一种常见的做法。

Test

$ traceroute 74.125.24.138

Simple Case

我们先看一种最简单的情况,两台设备在一个VLAN内,他们都连接了一个Router,并进行 TCP 通讯:

我们假设PC2后于 PC1 连接Router。在PC2(192.168.1.227)连接时,会(向当前网段内所有主机)进行ARP广播:

这样各个设备就获得了 PC2 MAC 地址和 IP地址的关系,同时,PC2也获得了其他设备的 MAC 地址和 IP地址的关系。

当 PC2 连接前,PC1中的ARP表:

$ arp -a
openwrt.lan (192.168.1.1) at 88:c3:97:9b:51:60 on en8 ifscope [ethernet]
weishi-mac.lan (192.168.1.173) at 0:e0:4c:6a:56:1b on en8 ifscope permanent [ethernet]

当PC2 连接后:

$ arp -a
openwrt.lan (192.168.1.1) at 88:c3:97:9b:51:60 on en8 ifscope [ethernet]
weishi-mac.lan (192.168.1.173) at 0:e0:4c:6a:56:1b on en8 ifscope permanent [ethernet]
weis-mbp-2.lan (192.168.1.227) at 8c:85:90:24:c7:dd on en8 ifscope [ethernet]

Reference