IP數據報格式和IP地址路由


一、IP數據報格式

IP數據報格式如下:


注:需要注意的是網絡數據包以大端字節序傳輸,當然頭部也得是大端字節序,也就是說:

The most significant bit is numbered 0 at the left, and the least significant bit of a 32-bit value is numbered 31 on the right.

The 4 bytes in the 32-bit value are transmitted in the order: bits 0-7 first, then bits 8-15, then 16-23, and bits 24-31 last. This is called big endian byte ordering, which is the byte ordering required for all binary integers in the TCP/IP headers as they traverse a network. This is called the network byte order. Machines that store binary integers in other formats, such as the little endian format, must convert the header values into the network byte order before transmitting the data.


版本
IP協議版本號,長度為4位,IPv4此字段值為4,IPv6此字段值為6


首部長度
以32位的字為單位,該字段長度為4位,最小值為5,即不帶任何選項的IP首部20個字節;最大值為15,所以首部長度最大為60個字節


服務類型(TOS)
長度為8位。此字段包含3位的優先權(現已忽略),4位的服務類型子字段(只能有一位置1)和1位的保留位(必須置0)。4位的服務類型分別為最小延遲(D)、最大吞吐量(T)、最高可靠性(R)、最小費用(F),如下圖。



總長度
該字段長度為16位,以字節為單位,該字段長度包含IP的頭部和數據部分(payload)。IP數據報最大可達65535個字節。

The total length field is required in the IP header since some data links (e.g., Ethernet) pad small frames to be a minimum length.

when a datagram is fragmented the total length field of each fragment is changed to be the size of that fragment.


標識
16位標識,用來標識一個IP數據報,每發送一個此值會加1,可用於分片和重新組裝成數據報。


標志與片偏移

3位標志其中第一位不使用, 每二位DF(Don’t Fragment),該位如果為1,如果傳輸的數據報超過最大傳輸單元,該數據報會被丟棄,並發送一個ICMP差錯報文。第三位MF(More Fragment)表示是否有更多的片,該位為1,說明后續有分片。最后一片MF為0。


注:在這里稍微講一下IP層分片的問題。假設一個IP數據報大於最大傳輸單元MTU,那么如果設置了分片標志位,將會被分片傳輸。

每一片都有自己的IP 頭部,IP頭部中的標識是一樣的,但是片偏移不同(以8字節為單位)。除了最后一片,分片要求其他片除去IP頭部的大小必須是8字節的整數倍。除了第一片有tcp/udp頭部,其他片都沒有。分片完成后,每一片獨自成為一個數據包(跟數據報概念不同,參見這里),可以走不同的路由,最后到達目的地的時候IP層根據它們各自IP頭部的信息重新組成一個IP數據報。

分片是有風險的,因為一旦某一片丟失,就需要重傳這個IP數據報,因為IP層本身並沒有超時重傳的機制,可靠性需要TCP層來保證(一些UDP協議的可靠性由應用程序保證),一旦一個TCP段中的某一片丟失,TCP協議層會超時重傳。此外,分片可以發生在源主機或者中間的路由,如果發生在中間的路由,源主機根本不知道是怎樣分片,所以要盡量避免分片。

應用數據的多個IP數據報由TCP層根據seq number 進行重組成原始數據,存放到TCP接收緩沖區。

Using sequence numbers, a receiving TCP discards duplicate segments and reorders segments that arrive out of order. Recall that any of these anomalies can happen because TCP uses IP to deliver its segments, and IP does not provide duplicate elimination or guarantee correct ordering. Because it is a byte stream protocol, however, TCP never delivers data to the receiving application out of order. Thus, the receiving TCP may be forced to hold on to data with larger sequence numbers before giving it to an application until a missing lower-sequence-numbered segment (a “hole”) is filled in.



實戰經驗:在使用tcpdump 並指定端口時,如果分片傳輸,除第一片外,其他片可能捕捉不到。



TTL
TTL(Time To Live)表示數據報最多可經過的路由器的數量。數據報每經過一個路由器,TTL減1,減為0時丟棄,並發送ICMP報文通知源主機。TTL可以避免數據報在路由器之間不斷循環。


協議類型
表示IP層上承載的是哪個高級協議。在封裝與分用的過程中,協議棧知道該交給哪個層的協議處理。1 ICMP 2 IGMP 6 TCP 17UDP


頭部校驗和
保證數據報頭部的數據完整性,但校驗不包括數據部分。這樣做的目的有二:一是所有將數據封裝在IP數據包中的高層協議均含有覆蓋整個數據的校驗和,因此IP數據報沒有必要再對其所承載的數據部分進行校驗。二是每經過一個路由器,IP數據報的頭部要發生改變(如TTL),而數據部分不變,這樣只對發生改變的頭部進行校驗,顯然不會浪費太多的時間。為了減少計算時間,一般不用CRC校驗碼,而是采用更簡單的網際校驗和(Internet Checksum)。

Since a router often changes only the TTL field (decrementing it by 1), a router can incrementally update the checksum when it forwards a received datagram, instead of calculating the checksum over the entire IP header again.

The standard BSD implementation, however, does not use this incremental update feature when forwarding a datagram.


源IP地址
發送數據的主機IP地址


目的IP地址
接收數據的主機IP地址


選項與填充(選項為4字節整數倍,否則用0填充)
安全和處理限制
路徑記錄:記錄所經歷路由器的IP地址
時間戳:記錄所經歷路由器的IP地址和時間
寬松源站路由:指定數據報文必須經歷的IP地址,可以經過沒有指定的IP地址。
嚴格的源站路由:指定數據報文必須經歷的IP地址,不能經過沒有指定的IP地址。


二、IP地址與路由

IPv4的IP地址長度為4字節,通常采用點分十進制表示法(dotted decimal representation)例如0xc0a80002表示為192.168.0.2。Internet被各種路由器和網關設備分隔成很多網段,為了標識不同的網段,需要把32位的IP地址划分成網絡號和主機號兩部分,網絡號相同的各主機位於同一網段,相互間可以直接通信,網絡號不同的主機之間通信則需要通過路由器轉發。


In our general scheme, IP can receive a datagram from TCP, UDP, ICMP, or IGMP (that is, a locally generated datagram) to send, or one that has been received from a network interface (a datagram to forward). The IP layer has a routing table in memory that it searches each time it receives a datagram to send. When a datagram is received from a network interface, IP first checks if the destination IP address is one of its own IP addresses or an IP broadcast address. If so, the datagram is delivered to the protocol module specified by the protocol field in the IP header. If the datagram is not destined for this IP layer, then (1) if the IP layer was configured to act as a router the packet is forwarded (that is, handled as an outgoing datagram as described below), else (2) the datagram is silently discarded.


假設某主機上的網絡接口配置和路由表如下:


這台主機只有一個網絡接口連到192.168.232.0/24網絡。路由表的Destination是目的網絡地址,Genmask是子網掩碼,Gateway是下一跳地址,Iface是發送接口,Flags中的U標志表示此條目有效(可以禁用某些條目),G標志表示此條目的下一跳地址是某個路由器的地址,沒有G標志的條目表示目的網絡地址是與本機接口直接相連的網絡,不必經路由器轉發,因此下一跳地址處記為*號。
如果要發送的數據包的目的地址是192.168.232.1,跟第三行的子網掩碼做與運算得到192.168.232.0,正是第三行的目的網絡地址,因此從eth0接口發送出去,由於192.168.232.0/24正是與eth0接口直接相連的網絡,因此可以直接發到目的主機,不需要經路由器轉發。
如果要發送的數據包的目的地址是202.10.1.2,跟后兩行路由表條目都不匹配,那么就要按缺省路由條目,從eth0接口發出去,首先發往192.168.232.2 路由器,再讓路由器根據它的路由表決定下一跳地址。

A complete matching host address is searched for before a matching network ID. Only if both of these fail is a default route used.


路由的處理過程如下,ARP部分可以參考這里

 


參考:

《Linux C 編程一站式學習》

《TCP/IP詳解 卷一》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM