1. 程式人生 > >IP協議首部結構分析

IP協議首部結構分析

IP協議首部主要欄位

IP資料報的格式如圖1所示。普通的IP首部長為20個位元組(不含選項欄位)。

圖1 資料報格式

圖1 資料報格式

IP目前的協議版本號是4,因此IP有時也稱作IPv4。IP協議首部的具體格式內容:

◆首部長度(IHL):首部佔32 bit字的數目,包括任何選項。由於它是一個4位元欄位,因此首部最長為60個位元組。普通IP資料報(不含選項欄位)欄位的值是5,首部長度為20位元組。

◆服務型別(TOS):包括一個3 bit的優先權子欄位(現在已被忽略),4 bit的TOS子欄位和1 bit未用位(必須置0)。

◆總長度欄位(Total Length):整個IP資料報的長度,以位元組為單位。利用首部長度欄位和總長度欄位,可以知道IP資料報中資料內容的起始位置和長度。該欄位長16位元,所以,IP資料報最長可達65535位元組

◆標識欄位(Identification)、標誌欄位(Flags)、片偏移量欄位(Fragment Offset):用來控制資料報的分片和重組。其中,標識欄位唯一標識主機發送的每一份資料報,通常每傳送一份報文它的值就會加1。

◆生存時間欄位TTL(Time to Live):資料報可以經過的最多路由裝置數。

◆首部檢驗和欄位(Header Checksum):根據IP首部計算的檢驗和碼。它不對首部後面的資料進行計算。

◆源IP地址和目的IP地址:每一份IP資料報都包含源IP地址和目的IP地址,分別指定傳送方和接收方。

◆選項(Options):選項是最後一個欄位,是可變長的可選資訊。

An IP packet consists of a header section and a data section.

An IP packet has no data checksum or any other footer after the data section. Typically thelink layer encapsulates IP packets in frames with a CRC footer that detects most errors, and typically the end-to-end TCP layer checksum detects most other errors.[11]

Header

The IPv4 packet header consists of 14 fields, of which 13 are required. The 14th field is optional (red background in table) and aptly named: options. The fields in the header are packed with the most significant byte first (

big endian), and for the diagram and discussion, the most significant bits are considered to come first (MSB 0 bit numbering). The most significant bit is numbered 0, so the version field is actually found in the four most significant bits of the first byte, for example.

IPv4 Header Format
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Version IHL DSCP ECN Total Length
4 32 Identification Flags Fragment Offset
8 64 Time To Live Protocol Header Checksum
12 96 Source IP Address
16 128 Destination IP Address
20 160 Options (if IHL > 5)
Version 
The first header field in an IP packet is the four-bit version field. For IPv4, this has a value of 4 (hence the name IPv4).
Internet Header Length (IHL) 
The second field (4 bits) is the Internet Header Length (IHL), which is the number of 32-bitwords in the header. Since an IPv4 header may contain a variable number of options, this field specifies the size of the header (this also coincides with the offset to the data). The minimum value for this field is 5 (RFC 791), which is a length of 5×32 = 160 bits = 20 bytes. Being a 4-bit value, the maximum length is 15 words (15×32 bits) or 480 bits = 60 bytes.
Differentiated Services Code Point (DSCP)
Originally defined as the Type of service field, this field is now defined by RFC 2474 for Differentiated services (DiffServ). New technologies are emerging that require real-time data streaming and therefore make use of the DSCP field. An example isVoice over IP (VoIP), which is used for interactive data voice exchange.
This field is defined in RFC 3168 and allows end-to-end notification of network congestion without dropping packets. ECN is an optional feature that is only used when both endpoints support it and are willing to use it. It is only effective when supported by the underlying network.
Total Length 
This 16-bit field defines the entire packet (fragment) size, including header and data, in bytes. The minimum-length packet is 20 bytes (20-byte header + 0 bytes data) and the maximum is 65,535 bytes — the maximum value of a 16-bit word. The largest datagram that any host is required to be able to reassemble is 576 bytes, but most modern hosts handle much larger packets. Sometimessubnetworks impose further restrictions on the packet size, in which case datagrams must be fragmented. Fragmentation is handled in either the host or router in IPv4.
Identification 
This field is an identification field and is primarily used for uniquely identifying fragments of an original IP datagram. Some experimental work has suggested using the ID field for other purposes, such as for adding packet-tracing information to help trace datagrams with spoofed source addresses.[12]
Flags 
A three-bit field follows and is used to control or identify fragments. They are (in order, from high order to low order):
  • bit 0: Reserved; must be zero.
  • bit 1: Don't Fragment (DF)
  • bit 2: More Fragments (MF)
If the DF flag is set, and fragmentation is required to route the packet, then the packet is dropped. This can be used when sending packets to a host that does not have sufficient resources to handle fragmentation. It can also be used forPath MTU Discovery, either automatically by the host IP software, or manually using diagnostic tools such asping ortraceroute.
For unfragmented packets, the MF flag is cleared. For fragmented packets, all fragments except the last have the MF flag set. The last fragment has a non-zero Fragment Offset field, differentiating it from an unfragmented packet.
Fragment Offset 
The fragment offset field, measured in units of eight-byte blocks, is 13 bits long and specifies the offset of a particular fragment relative to the beginning of the original unfragmented IP datagram. The first fragment has an offset of zero. This allows a maximum offset of (213 – 1) × 8 = 65,528 bytes, which would exceed the maximum IP packet length of 65,535 bytes with the header length included (65,528 + 20 = 65,548 bytes).
Time To Live (TTL) 
An eight-bit time to live field helps prevent datagrams from persisting (e.g. going in circles) on an internet. This field limits a datagram's lifetime. It is specified in seconds, but time intervals less than 1 second are rounded up to 1. In practice, the field has become a hop count—when the datagram arrives at a router, the router decrements the TTL field by one. When the TTL field hits zero, the router discards the packet and typically sends anICMP Time Exceeded message to the sender.
The program traceroute uses these ICMP Time Exceeded messages to print the routers used by packets to go from the source to the destination.
Protocol 
This field defines the protocol used in the data portion of the IP datagram. TheInternet Assigned Numbers Authority maintains alist of IP protocol numbers which was originally defined in RFC 790.
Header Checksum 
The 16-bit checksum field is used for error-checking of the header. When a packet arrives at a router, the router calculates the checksum of the header and compares it to the checksum field. If the values do not match, the router discards the packet. Errors in the data field must be handled by the encapsulated protocol. BothUDP andTCP have checksum fields.
When a packet arrives at a router, the router decreases the TTL field. Consequently, the router must calculate a new checksum.RFC 1071 defines the checksum calculation:
The checksum field is the 16-bit one's complement of the one's complement sum of all 16-bit words in the header. For purposes of computing the checksum, the value of the checksum field is zero.
For example, consider Hex 4500003044224000800600008c7c19acae241e2b (20 bytes IP header):
Step 1) 4500 + 0030 + 4422 + 4000 + 8006 + 0000 + 8c7c + 19ac + ae24 + 1e2b = 2BBCF (16-bit sum)
Step 2) 2 + BBCF = BBD1 = 1011101111010001 (1's complement 16-bit sum)
Step 3) ~BBD1 = 0100010000101110 = 442E (1's complement of 1's complement 16-bit sum)
To validate a header's checksum the same algorithm may be used – the checksum of a header which contains a correct checksum field is a word containing all zeros (value 0):
2BBCF + 442E = 2FFFD. 2 + FFFD = FFFF. the 1'S of FFFF = 0.
Source address
This field is the IPv4 address of the sender of the packet. Note that this address may be changed in transit by anetwork address translation device.
Destination address
This field is the IPv4 address of the receiver of the packet. As with the source address, this may be changed in transit by anetwork address translation device.
Options
The options field is not often used. Note that the value in the IHL field must include enough extra 32-bit words to hold all the options (plus any padding needed to ensure that the header contains an integral number of 32-bit words). The list of options may be terminated with an EOL (End of Options List, 0x00) option; this is only necessary if the end of the options would not otherwise coincide with the end of the header. The possible options that can be put in the header are as follows:
Field Size (bits) Description
Copied 1 Set to 1 if the options need to be copied into all fragments of a fragmented packet.
Option Class 2 A general options category. 0 is for "control" options, and 2 is for "debugging and measurement". 1, and 3 are reserved.
Option Number 5 Specifies an option.
Option Length 8 Indicates the size of the entire option (including this field). This field may not exist for simple options.
Option Data Variable Option-specific data. This field may not exist for simple options.
  • Note: If the header length is greater than 5, i.e. it is from 6 to 15, it means that the options field is present and must be considered.
  • Note: Copied, Option Class, and Option Number are sometimes referred to as a single eight-bit field – theOption Type.
The following two options are discouraged because they create security concerns:Loose Source and Record Route (LSRR) andStrict Source and Record Route (SSRR). Many routers block packets containing these options.[13]

Data

The data portion of the packet is not included in the packet checksum. Its contents are interpreted based on the value of the Protocol header field.

In a typical IP implementation, standard protocols such as TCP and UDP are implemented in theOS kernel, for performance reasons. Other protocols such as ICMP may be partially implemented by the kernel, or implemented purely in user software. Protocols not implemented in-kernel, and not exposed by standard APIs such asBSD sockets, are typically implemented using a 'raw socket' API.

Some of the common protocols for the data portion are listed below:

用Wireshark抓包: