计算机网络04—网络层

网络层

学习参考资料:

  1. 湖南科技大学—计算机网络
  2. 谢希仁 计算机网络(第7版)

1. 概述

数据链路层是实现设备之间通信的非常重要的一层,但是数据链路层的工作只能让通信在同一种网络中进行,如果需要跨越网络,就需要上升到第三层——网络层。

网络层的主要任务是实现网络互连,进而实现数据包在各网络之间的传输

在这里插入图片描述

要实现网络层任务,需要解决以下主要问题:

  • 网络层向运输层提供怎样的服务(可靠传输还是不可靠传输
  • 网络层寻址问题
  • 路由选择问题

1.1 IP协议及配套协议

在这里插入图片描述

直接交付:同一网络中不同主机之间通信。

间接交付:不同网络中不同主机之间通信。

2. 两种服务

2.1 面向连接的虚电路服务

在这里插入图片描述

2.2 无连接的数据报服务

在这里插入图片描述

2.3 对比

在这里插入图片描述

网络层向上只提供简单灵活的、无连接的、尽最大努力交付的数据报服务。

网络层不提供服务质量的承诺。即所传送的分组可能出错、丢失、重复和失序(不按序到达终点),也不保证分组传送的时限。

如果主机(即端系统)中的进程之间的通信需要可靠,那么就由网络的主机中的运输层负责可靠交付(包括差错处理、流量控制等)

3. IPv4地址

3.1 概述

在这里插入图片描述

3.2 点分十进制记法

在这里插入图片描述

3.3 分类编址的IPv4地址

在这里插入图片描述

3.3.1 A类地址

在这里插入图片描述

3.3.2 B类地址

在这里插入图片描述

3.3.3 C类地址

在这里插入图片描述

3.3.4 特殊IP地址

在这里插入图片描述

3.4 划分子网的IPv4地址

在这里插入图片描述

给定一个分类的IP地址和其相应的子网掩码,就知道子网划分的细节:

  • 划分出的子网数量
  • 每个子网可分配的IP地址数量
  • 每个子网的网络地址和广播地址
  • 每个子网可分配的最小和最大地址

在这里插入图片描述

3.5 无分类编址的IPv4地址

在这里插入图片描述

在这里插入图片描述

路由聚合(构造超网)的方法是找共同前缀。网络前缀越长,地址块越小,路由越具体。若路由器查表转发分组时发现有多条路由可选,则选择网络前缀最长的那条,这称为最长前缀匹配,因为这样的路由更具体。

在这里插入图片描述

3.6 IPv4地址的应用规划

在这里插入图片描述

4. IP数据报的发送和转发过程

判断目的主机是否与自己在同一个网络:

  • 源主机的IP地址与其子网掩码相与
  • 目的主机的IP地址与其子网掩码相与
  • 两者结果相同则处在同一个网络,反之不在同一个网络

在这里插入图片描述

4.1 路由器分组转发算法

在这里插入图片描述

5. IPv4数据报的格式

一个 IP 数据报由首部数据两部分组成。

5.1 首部格式

首部分为两部分:第一部分是固定长度,共 20 字节,是所有 IP 数据报必须具有的;第二部分是可选字段,其长度是可变的。

在这里插入图片描述

  • 版本:占4比特,指 IP 协议的版本,通信双方使用的IP协议的版本必须一致。目前广泛使用的 IP 协议版本号为 4 (即 IPv4)。
  • 首部长度:占4比特,表示IP数据报首部的长度,该字段的取值以4字节为单位。最小十进制取值为5,表示IP数据报首部只有20字节固定部分;最大十进制取值为15,表示IP数据报首部包含20字节固定部分和最大40字节可变部分。
  • 可选字段:长度从1个字节到40个字节不等,用来支持排错、测量及安全等措施。
  • 填充字段:确保首部长度为4字节的整数倍,使用全0进行填充。
  • 区分服务:占8比特,用来获得更好的服务。一般情况下不使用该字段。
  • 总长度:占16比特,表示IP数据报的总长度(首部+数据)。最大取值为十进制的65535,以字节为单位。

标识、标志和片偏移这三个字段共同用于IP数据报分片,当IPv4数据报长度超过MTU时,无法封装,需要进行分片。以太网规定MTU值为1500字节。

  • 标识:占16比特,属于同一个数据报的各分片数据报应该具有相同的标识。IP软件维持一个计数器,每产生一个数据报,计数器值加1,并将此值赋给标识字段。
  • 标志:占3比特。MF位:1表示后面还有分片;0表示这是最后一个分片。DF位:1表示不允许分片;0表示允许分片。
  • 片偏移:占13比特,指出分片数据报的数据部分偏移其在原数据报的位置有多少个单位。以8个字节为单位。

在这里插入图片描述

  • 生存时间TTL:占8比特,指示数据报在网络中可通过的路由器数的最大值。
  • 协议:占8比特,指明IPv4数据报的数据部分是何种协议数据单元。
协议名称 协议字段值
ICMP 1
IGMP 2
TCP 6
UDP 17
IPv6 41
OSPF 89
  • 首部检验和:占16比特,用来检测首部在传输过程中是否出现差错。不采用CRC检验码而采用简单的计算方法。

在这里插入图片描述

在这里插入图片描述

  • 源IP地址和目的地址:各占32比特,用来填写发送该IP数据报的源主机的IP地址和接收该IP数据报的目的主机的IP地址。

5.2 网际控制报文协议ICMP

为了更有效地转发 IP 数据报和提高交付成功的机会,在网际层使用了网际控制报文协议 ICMP (Internet Control Message Protocol)。ICMP报文被封装在IP数据报中发送。

ICMP 是互联网的标准协议。ICMP 允许主机或路由器报告差错情况和提供有关异常情况的报告。

ICMP差错报告报文共有5种:

  • 终点不可达

在这里插入图片描述

  • 源点抑制

在这里插入图片描述

  • 时间超过

在这里插入图片描述

  • 参数问题

在这里插入图片描述

  • 改变路由(重定向)

在这里插入图片描述

不发送ICMP差错报告报文的情况:

  • 对 ICMP 差错报告报文不再发送 ICMP 差错报告报文。
  • 对第一个分片的数据报片的所有后续数据报片都不发送ICMP 差错报告报文。
  • 对具有多播地址的数据报都不发送 ICMP 差错报告报文。
  • 对具有特殊地址(如127.0.0.0 或 0.0.0.0)的数据报不发送 ICMP 差错报告报文。

5.2.1 ICMP询问报文的类型

在这里插入图片描述

5.2.2 ICMP的应用举例

在这里插入图片描述

在这里插入图片描述

6. 路由选择协议

6.1 基本概念

  • 静态路由选择策略:即非自适应路由选择,例如人工配置的路由等。其特点是简单和开销较小,但不能及时适应网络状态的变化,一般在小规模网络中使用。
  • 动态路由选择策略:即自适应路由选择,路由器通过路由选择协议自动获取路由信息。其特点是能较好地适应网络状态的变化,但实现起来较为复杂,开销也比较大,适用于大规模网络。

自治系统 AS :在单一的技术管理下的一组路由器,而这些路由器使用一种 AS 内部的路由选择协议和共同的度量以确定分组在该 AS 内的路由,同时还使用一种 AS 之间的路由选择协议用以确定分组在 AS之间的路由。(尽管一个 AS 使用了多种内部路由选择协议和度量,但重要的是一个 AS 对其他 AS 表现出的是一个单一的和一致的路由选择策略。)

在这里插入图片描述

在这里插入图片描述

常见的路由选择协议如下:

在这里插入图片描述

6.2 内部网关协议RIP

在这里插入图片描述

RIP 不能在两个网络之间同时使用多条路由。RIP 选择一个具有最少路由器的路由(即最短路由),哪怕还存在另一条高速(低时延)但路由器较多的路由。

RIP 协议的三个特点:

  • 仅和相邻路由器交换信息。
  • 交换的信息是当前本路由器所知道的全部信息,即自己的路由表。
  • 按固定的时间间隔交换路由信息,例如间隔30秒。当网络拓扑发生变化时,路由器也及时向相邻路由器通告拓扑变化后的路由信息。

6.2.1 工作原理

在这里插入图片描述

“收敛”就是在自治系统中所有的结点都得到正确的路由选择信息的过程。

距离向量算法:

在这里插入图片描述

6.2.2 优缺点

优点:

  • 实现简单,开销较小。

缺点:

  • RIP 限制了网络的规模,它能使用的最大距离为 15(16 表示不可达)。
  • 路由器之间交换的路由信息是路由器中的完整路由表,因而随着网络规模的扩大,开销也就增加。
  • “坏消息传播得慢”,网络出故障的传播时间往往需要较长的时间(例如数分钟),使更新过程的收敛时间过长。

6.3 内部网关协议OSPF

6.3.1 工作原理

在这里插入图片描述

  • OSPF 不用 UDP 而是直接用 IP 数据报传送。
  • OSPF 构成的数据报很短。这样做可减少路由信息的通信量。
  • 数据报很短的另一好处是可以不必将长的数据报分片传送。
  • 分片传送的数据报只要丢失一个,就无法组装成原来的数据报,整个数据报必须重传。

在这里插入图片描述

  • 向本自治系统中所有路由器发送信息,这里使用的方法是洪泛法。
  • 发送的信息就是与本路由器相邻的所有路由器的链路状态,但这只是路由器所知道的部分信息。
  • 只有当链路状态发生变化时,路由器才用洪泛法向所有路由器发送此信息。

在这里插入图片描述

  • 由于各路由器之间频繁地交换链路状态信息,因此所有的路由器最终都能建立一个链路状态数据库。
  • 这个数据库实际上就是全网的拓扑结构图,它在全网范围内是一致的(这称为链路状态数据库的同步)。
  • OSPF 的链路状态数据库能较快地进行更新,使各个路由器能及时更新其路由表。
  • OSPF 的更新过程收敛得快是其重要优点。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

6.3.2 划分区域

  • 为了使 OSPF 能够用于规模很大的网络,OSPF 将一个自治系统再划分为若干个更小的范围,叫做区域。
  • 每一个区域都有一个 32 位的区域标识符(用点分十进制表示)。
  • 区域也不能太大,在一个区域内的路由器最好不超过 200 个。

在这里插入图片描述

  • 划分区域的好处就是将利用洪泛法交换链路状态信息的范围局限于每一个区域而不是整个的自治系统,这就减少了整个网络上的通信量
  • 在一个区域内部的路由器只知道本区域的完整网络拓扑,而不知道其他区域的网络拓扑的情况。
  • OSPF 使用层次结构的区域划分。在上层的区域叫做主干区域 (backbone area)。
  • 主干区域的标识符规定为0.0.0.0。主干区域的作用是用来连通其他在下层的区域。

6.3.3 Dijkstra最短路径算法

在这里插入图片描述

6.4 外部网关协议BGP

BGP 是不同自治系统的路由器之间交换路由信息的协议。

在这里插入图片描述

  • 自治系统之间的路由选择必须考虑有关策略。
  • 边界网关协议 BGP 只是寻找一条能够到达目的网络且比较好的路由(不能兜圈子),而并非要寻找一条最佳路由

在这里插入图片描述

6.4.1 工作原理

Generally speaking, two BGP speakers are connected through a shared network, and a BGP speaker is often a BGP border router, but it may not be a BGP border router.

在这里插入图片描述

在这里插入图片描述

  • BGP is suitable for Internet with multi-level structure

在这里插入图片描述

6.4.2 Message type

  • Open (OPEN) message: used to establish a relationship with another adjacent BGP speaker.
  • Update (UPDATE) message: used to send information about a certain route and list multiple routes to be withdrawn.
  • Keep alive (KEEPALIVE) message: used to confirm the open message and periodically confirm the neighbor relationship.
  • Notification (NOTIFICATION) message, used to send detected errors.

在这里插入图片描述

6.4.3 Features

  • The magnitude of the number of nodes exchanging routing information in the BGP protocol is the magnitude of the number of autonomous systems, which is much less than the number of networks in these autonomous systems.
  • The number of BGP speakers (or border routers) in each autonomous system is very small. This makes routing between autonomous systems less complicated.
  • BGP supports CIDR, so the BGP routing table should include the destination network prefix, the next-hop router, and the sequences of autonomous systems that must pass through to reach the destination network.
  • When BGP is just running, BGP neighbors exchange the entire BGP routing table. But in the future only need to update the changed part when there is a change. Doing so is good for saving network bandwidth and reducing the processing overhead of routers.

7. Composition of the router

  • A router is a specialized computer with multiple input ports and multiple output ports, and its task is 转发分组to That is to say, the packet received by an input port of the router is forwarded to the next-hop router from a suitable output port of the router according to the destination of the packet (that is, the destination network).
  • The next-hop router also processes the packet in this way until the packet reaches the destination.
  • 路由器的转发分组正是网络层的主要工作。

在这里插入图片描述

7.1 Structure

在这里插入图片描述

The entire router structure can be divided into two parts: routing part and packet forwarding part.

Routing section:

  • In the control part, the core component is the routing processor.
  • The task of the routing processor is to construct a routing table according to the selected routing protocol, and at the same time exchange routing information with adjacent routers frequently or periodically to continuously update and maintain the routing table.

Packet forwarding part:

  • Switching fabric: Also known as switching fabric, its function is to process packets according to the forwarding table.
  • Input ports and output ports (ports are hardware interfaces).

在这里插入图片描述


Input port processing of packets received on the wire

在这里插入图片描述

在这里插入图片描述


The output port sends the packet from the switch fabric to the wire:

在这里插入图片描述

在这里插入图片描述

路由器中的输入或输出队列产生溢出是造成分组丢失的重要原因。

8. Virtual Private Network VPN and Network Address Translation NAT

8.1 Virtual Private Network VPN

  • Due to the shortage of IP addresses, the number of IP addresses that an organization can apply for is often far less than the number of hosts owned by the organization.
  • Considering that the Internet is not very secure, it is not necessary for an organization to connect all hosts to the external Internet.
  • Assuming that the computer communication within an organization also uses the TCP/IP protocol, then in principle, these 机构内部computers that are only in use can be controlled by the organization 自行分配其 IP 地址.

在这里插入图片描述

专用地址只能用作本地地址而不能用作全球地址. All routers in the Internet will not forward datagrams whose destination address is a private address. An interconnected network using such dedicated IP addresses is called a private Internet or local Internet, or private network. Because these private addresses are only used within the institution. Dedicated IP addresses are also called reusable addresses.

  • The public Internet is used as the communication carrier between the private networks of the institution, and such a private network is also called a virtual private network (VPN) (Virtual Private Network).
  • "Private network" is because this kind of network is used for the internal communication of the organization's host computer, rather than for communicating with the non-institutional host computer outside the network.
  • "Virtual" means "as if it is", but it is not, because the dedicated communication line is not really used now, and the VPN is only the same as a real private network in effect.

在这里插入图片描述

在这里插入图片描述

8.2 Network Address Translation NAT

Although the Internet uses unclassified addressing to slow down the exhaustion of IPv4 address space, due to the rapid increase in the number of Internet users, especially the increasing demand for a large number of small office networks and home networks to access the Internet, the IPv4 address space will soon face depletion. The danger of exhaustion has not been lifted.

In 1994, a network address translation (NAT) method was proposed to alleviate the problem that the IPv4 address space was about to be exhausted.

NAT能使在专用网上使用专用地址的主机与互联网上的主机通信,且不用加密。

NAT software needs to be installed on the router that connects the private network to the Internet. A router equipped with NAT software is called a NAT router, and it has at least one valid external global IP address.

When all hosts using local addresses communicate with the outside world, their local addresses must be converted into global IP addresses on the NAT router in order to connect to the Internet.

在这里插入图片描述

8.2.1 Conversion process

在这里插入图片描述

在这里插入图片描述

当 NAT 路由器具有 n 个全球 IP 地址时,专用网内最多可以同时有 n 台主机接入到互联网。这样就可以使专用网内较多数量的主机,轮流使用 NAT 路由器有限数量的全球 IP 地址。

通过 NAT 路由器的通信必须由专用网内的主机发起。专用网内部的主机不能充当服务器用,因为互联网上的客户无法请求专用网内的服务器提供服务。

8.2.2 网络地址与端口号转换NAPT

为了更加有效地利用 NAT 路由器上的全球IP地址,现在常用的 NAT 转换表把运输层的端口号也利用上。这样,就可以使多个拥有本地地址的主机,共用一个 NAT 路由器上的全球 IP 地址,因而可以同时和互联网上的不同主机进行通信。

使用端口号的 NAT 叫做网络地址与端口号转换NAPT (Network Address and Port Translation),而不使用端口号的 NAT 就叫做传统的 NAT (traditional NAT)。

在这里插入图片描述

在这里插入图片描述

Guess you like

Origin blog.csdn.net/weixin_46003347/article/details/123690328