Skip to content

Commit

Permalink
docs: ip docs
Browse files Browse the repository at this point in the history
  • Loading branch information
suyuan32 committed Feb 17, 2024
1 parent c49f602 commit c473f86
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
143 changes: 143 additions & 0 deletions src/en/guide/concepts/network/2-ip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
---
order: 2
title: "IP"
---

## IP

IP (Internet Protocol) and TCP together form the TCP/IP protocol stack, which is one of the core protocols of the Internet.

IP has the following characteristics:

- **Stateless**: It does not store any state information, which means that IP packets can be unordered and duplicated.
- **Connectionless**: It does not require establishing a connection, and both the sender and receiver do not need to store information about each other. The destination IP address needs to be specified for each transmission.
- **Unreliable**: IP cannot guarantee that packets will always be delivered. They may be lost or discarded due to timeouts.

### **IPv4** Structure

![ipv4 header](/assets/image/article/network/ip.png)

::: tip Field Descriptions

| Field | Description |
| ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
| Version | Occupies 4 bits and is used to identify the version number of the IP header. The IPv4 version number is 4. |
| Header Length | Represents the size of the IP header, with a minimum value of 5 (5*4 = 20 bytes) and a maximum value of 15 (15*4 = 60 bytes). |
| Differentiated Services (DS) | Used to represent the type of service. It can be divided into DSCP and ECN (Explicit Congestion Notification). |
| Total Length | Represents the total number of bytes of the IP header and the data section combined. |
| Identification | Used for fragmentation and reassembly. This field is incremented by 1 for each generated datagram during fragmentation. |
| Flags | Represents the relevant information about packet fragmentation. |
| Fragment Offset | Used to identify the position of each segment being fragmented relative to the original data, in units of 8 bytes. |
| Time to Live (TTL) | Decreases by one for each router the packet passes through. When it reaches zero, the packet is discarded. |
| Protocol | The protocol used in the data section of the packet. |
| Header Checksum | Used to check if the packet is damaged. |
| Source Address | Represents the IP address of the sender. |
| Destination Address | Represents the IP address of the receiver. |
| Options | Additional information. |
| Data | The content of the transmitted data. |


:::

::: details Version Number Corresponding to Digits

| Version | Description | Status |
| ------- | ------------------------------------- | ---------- |
| 0 | Pre-Internet Protocol, before v4 | Reserved |
| 1–3 | Unassigned | |
| 4 | Internet Protocol version 4 (IPv4) | Active |
| 5 | Internet Stream Protocol or ST | Deprecated |
| 5 | Internet Stream Protocol or ST-II | Deprecated |
| 6 | Simple Internet Protocol (SIP) | Deprecated |
| 6 | Internet Protocol version 6 (IPv6) | Active |
| 7 | TP/IX Next Generation Internet (IPv7) | Deprecated |
| 8 | P Internet Protocol (PIP) | Deprecated |
| 9 | TCP and UDP Large Addresses (TUBA) | Deprecated |
| 10–14 | Unassigned | |
| 15 | Version field reserved value | Reserved |

:::

::: details Flags Description
This 3-bit field is used to control and identify fragmentation. They are:
| Bit | Description |
| --- | -------------------------- |
| 0 | Reserved, must be set to 0 |
| 1 | Don't Fragment (DF) |
| 2 | More Fragments (MF) |

::: warning
- **If the DF flag is set to 1 but the route requires the packet to be fragmented, the packet will be discarded.**

- **For fragmented packets, except for the last fragment, the MF flag is set to 1, and the fragment offset field of the last fragment is not 0.**
:::

::: details Common IP Options

- **Security Options**: Used to provide network security functions, such as data encryption, authentication, and access control.

- **Record Route Option**: Enables the IP datagram to record the IP addresses of the routers it passes through in the transmission path, for diagnosing network problems or tracing the path of packets.

- **Loose Source Routing**: Specifies the transmission path of the data packet, requiring the packet to pass through specified intermediate nodes, but allowing other nodes on the path to make certain choices.

- **Strict Source Routing**: Specifies the transmission path of the data packet, requiring the packet to be delivered in the specified node order, and other nodes cannot participate.

- **Timestamp Option**: Inserts timestamp information into the packet to measure the time required for the packet to be transmitted from the source to the destination.

:::

### Fragmentation

::: tip MTU
Maximum Transmission Unit (MTU) refers to the maximum packet size (in bytes) that can be transmitted at the data link layer.
:::

When an `IP` packet is transmitted over a link layer and the packet size exceeds the link layer's `MTU`, the packet needs to be fragmented. The length of each fragment must be less than or equal to the MTU minus the IP header length.

::: important Fragmentation Process
Each fragmented data segment is placed in an independent IP datagram, and the corresponding fields in the datagram need to be modified:

- Modify the total length field to the length of the current fragment.
- Except for the last fragment, set the More Fragments (MF) flag to 1.
- Adjust the fragment offset.
- Recalculate the checksum.

:::

::: details Example

An IP datagram with a header length of `20` bytes and a data payload of `4000` bytes (total length: `4020` bytes) is transmitted over a link with an `MTU` of `2500` bytes. It will be divided into two fragments:

| Total Length | Header | Data | More Fragments (MF)? | DF | Fragment Offset |
| ------------ | ------ | ---- | -------------------- | --- | --------------- |
| 2500 | 20 | 2480 | Yes | 0 | 0 |
| 1540 | 20 | 1520 | No | 0 | 310 (2480/8) |

Similarly, when the MTU becomes smaller, the cut fragments will be further divided.

:::

### Reassembly

::: tip How to Determine if Data is Fragmented?
- DF flag is 0
- Fragment offset field is not 0
:::

The receiver collects the fragments and sorts them based on the offset. Once all the fragments are collected, they are reassembled and submitted to the upper protocol stack.

### IPV6

![IPV6](/assets/image/article/network/comparing-ipv4-and-ipv6-headers.png)

::: tip Differences between IPv4 and IPv6

| Feature | IPv4 | IPv6 |
| :--------------------------- | :--------------------------------------------- | :------------------------------------------------------------------------------------ |
| **Address Length** | 32 bits, providing about 4.3 billion addresses | 128 bits, providing a vast number of addresses |
| **Address Notation** | Dotted decimal notation, e.g., 192.168.0.1 | Colon hexadecimal notation, e.g., 2001:0db8:85a3:0000:0000:8a2e:0370:7334 |
| **Header Length** | Fixed, 20 bytes | Fixed, 40 bytes |
| **Quality of Service (QoS)** | Limited native support | Native support, including flow labels and flow priorities |
| **Security** | Requires the use of IPSec | Native support for IPSec |
| **Configuration** | Manual or DHCP | Manual, automatic (based on link-local addresses and router announcements), or DHCPv6 |
:::
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 2
order: 3
title: "TCP/UDP"
---
### TCP
Expand Down
143 changes: 143 additions & 0 deletions src/guide/concepts/network/2-ip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
---
order: 2
title: "IP"
---

## IP

IP (Internet Protocol) 协议和 TCP 组成的 TCP/IP 协议栈是互联网的核心协议之一。

IP 协议有以下特点:

- **无状态**: 不储存状态信息,意味着 IP 协议传递的数据包可能是无序的、重复的。
- **无连接**: 不需要建立连接,发送方和接收方都不需要存储对方的信息,每次发送都需要指定目标的 IP 地址。
- **不可靠**: IP 协议不能保证数据包一定能送达,可能会丢包,或者因超时而被丢弃。

### **IPV4** 结构

![ipv4 header](/assets/image/article/network/ip_zh.png)

::: tip 字段介绍

| 字段 | 描述 |
| ------------- | -------------------------------------------------------------------------- |
| 版本 | 占用 4 bit,用来标识 IP 首部的版本号。IPv4 版本号是 4 |
| 首部长度 | 表示 IP 首部的大小,最小值为5 (5*4 = 20 byte),最大值为 15 (15*4 = 60byte) |
| 区分服务(DS) | 用来表示服务的类型。可划分为 DSCP 和 ECN (显式拥塞通告) |
| 总长度 | 表示的是 IP 首部与数据部分合起来的总字节数 |
| 标识 | 用于分片重组,分片时每产生一个数报文该字段就加1 |
| 标志 | 表示包被分片的相关信息 |
| 片偏移 | 用来标识被分片的每一个分段相对于原始数据的位置,单位为 **8** 字节 |
| 生存时间(TTL) | 每经过一个路由器该字段就会减一,当为零的时候会被丢弃 |
| 协议 | 该报文数据区使用的协议 |
| 首部校验和 | 用于检测报文是否损坏 |
| 源地址 | 表示发送端的 IP 地址 |
| 目标地址 | 表示接收端的 IP 地址 |
| 选项 | 附加的信息 |
| 数据 | 传输的数据内容 |


:::

::: details 版本对应数字列表

| 版本 | 描述 | 状态 |
| ----- | ------------------------- | ---- |
| 0 | 互联网协议,v4 之前 | 保留 |
| 1–3 | 未分配 | |
| 4 | 互联网协议版本 4 (IPv4) | 活跃 |
| 5 | 互联网流协议或 ST | 废弃 |
| 5 | 互联网流协议或 ST-II | 废弃 |
| 6 | 简单互联网协议 (SIP) | 废弃 |
| 6 | 互联网协议版本 6 (IPv6) | 活跃 |
| 7 | TP/IX 下一代互联网 (IPv7) | 废弃 |
| 8 | P 互联网协议 (PIP) | 废弃 |
| 9 | TCP 和 UDP 大地址 (TUBA) | 废弃 |
| 10–14 | 未分配 | |
| 15 | 版本字段标记值 | 保留 |

:::

::: details 标志 (Flags) 介绍
这个3位字段用于控制和识别分片,它们是:
|| 描述 |
| --- | ------------------------------ |
| 0 | 保留,必须为0 |
| 1 | 禁止分片(Don’t Fragment,DF) |
| 2 | 更多分片(More Fragment,MF) |

::: warning
- **如果DF标志位被设置为1,但路由要求必须分片报文,则此报文会被丢弃。**

- **被分片的报文除了最后一个分片以外其他分片的 MF 标志位都为 1,且最后一个分片的分片偏移字段不为 0**
:::

::: details 常见的 IP 选项有哪些?

- **安全性选项(Security Options)**:用于提供网络安全功能,例如数据加密、身份验证和访问控制。

- **记录路由选项(Record Route Option)**:启用IP数据报在传输路径上记录途经的路由器的IP地址,以便诊断网络问题或跟踪数据包的路径。

- **宽松源站选路(Loose Source Routing)**:指定数据包的传输路径,要求数据包经过指定的中间节点,但允许在路径上的其他节点进行一定的选择。

- **严格源站选路(Strict Source Routing)**:指定数据包的传输路径,要求数据包必须按照指定的节点顺序传递,其他节点不能参与。

- **时间戳选项(Timestamp Option)**:在数据包中插入时间戳信息,用于测量数据包从源到目的地传输所需的时间。

:::

### 分片

::: tip MTU
最大传输单元 (MTU, Maximum Transmission Unit) 指的是在数据链路层能通过的最大数据包大小(单位: 字节)。
:::

`IP` 数据包在链路层传输的时候,若数据包大于链路层的 `MTU`, 则需要对数据包进行分片。分片的长度需小于等于 MTU 减去 IP 首部长度。

::: important 分片过程
分片后的每一个数据分片都会放到独立的 IP 报文中,报文中的对应字段需要进行修改:

- 修改总长字段为此时分片的长度
- 除了最后一片分片外,其他分片的更多分片(MF)标志位被设置为 1
- 调整分片偏移量
- 重新计算校验和

:::

::: details 例子

一个IP报文首部长度 `20` 字节且有 `4000` 字节的数据载荷(总长:`4020` 字节),在 `MTU``2500` 字节的链路上传输,他将被分割成两个分片:

| 总长 | 首部 | 数据 | 更多分片(MF)? | DF | 分片偏移量 |
| ---- | ---- | ---- | ---------------- | --- | ---------------- |
| 2500 | 20 | 2480 || 0 | 0 |
| 1540 | 20 | 1520 || 0 | 310 (2480/8) |

同理,切割后的分片在 MTU 变小时会被再次切割。

:::

### 重组

::: tip 如何判断数据已被分片?
- DF 标志位为 0
- 分片偏移字段不为 0
:::

接收者会将分片收集起来,并通过偏移量进行排序,一旦收集齐就会组装起来提交给上传协议栈。

### IPV6

![IPV6](/assets/image/article/network/comparing-ipv4-and-ipv6-headers.png)

::: tip IPV4 和 IPV6 区别

| 特性 | IPv4 | IPv6 |
| :------------------ | :---------------------------------- | :---------------------------------------------------------------- |
| **地址长度** | 32位,提供约43亿个地址 | 128位,提供极大数量的地址 |
| **地址表示** | 点分十进制表示法,例如:192.168.0.1 | 冒号十六进制表示法,例如:2001:0db8:85a3:0000:0000:8a2e:0370:7334 |
| **头部长度** | 固定,20字节 | 固定,40字节 |
| **服务质量(QoS)** | 原生支持有限 | 原生支持,包括流标签和流优先级 |
| **安全性** | 需要使用 IPSec | 原生支持 IPSec |
| **配置** | 手动或 DHCP | 手动,自动(基于链接的本地地址和路由公告)或 DHCPv6 |
:::
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
order: 2
order: 3
title: "TCP/UDP"
---

Expand Down

0 comments on commit c473f86

Please sign in to comment.