L4 傳輸層 (Transport)
概觀
資料來源:
TCP (Transmission Control Protocol) Header (Segment)
TCP Header:
0 1 2 3 (10 bits)
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| (DOFF)| |G|K|H|T|N|N| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
欄位
- source, dest:IP 位址已經紀錄在 L3 的 IP header 中了,這裡只紀錄 port
- TCP/UDP 埠號(Port numbers)
- 序列號(Sequence numbers,TCP)
協定不保證資料會照發送順序抵達接收端,接收端可利用表頭中的序列號 (Sequence Number, SEQ) 欄位進行排序、消除重複 (eliminate duplicates),以保證資料接收的正確順序,並以位元組串流 (byte-stream) 的方式,傳遞給應用層。
TCP 使用確認號 (Acknowledgment Number) 欄位,指出下一個期望接收的序列號 (Sequence Number, SEQ),表示編號小於此值的位元組,皆已正確接收
- 封包標誌(Flags)
- 視窗大小(Window size)
- 控制位元 (Control Bits)
- SYN (Synchronize sequence numbers) 同步序列號
- ACK (Acknowledgment field significant)
其他參考資料
L3 網路層
概觀
定義:RFC 791 - Internet Protocol 資料來源:
IP(v4) Header (Datagram)
IPv4 Header:
0 1 2 3 (10 bits)
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
IP Header 通常為 20 bytes,也就是至 Destination Address 欄位而已。 IHL (Internet Header Length) 欄位值乘以 4 就是 header 的總長度。
欄位
- Version:IP 協定版本,IPv4 (4)、IPv6 (6)
- IHL:Internet Header Length,標頭長度,也就是 IP header 長度,IHL 介於 5~15 之間,故 IP Header 為 20 ~ 60 之間
- ToS:Type of Service,封包的種類
- Precedence:封包是一般資料的封包?或是網路的控制封包?
- DTR:Delay, Throughput, Relibility
Bits 0-2: Precedence.
Bit 3: 0 = Normal Delay, 1 = Low Delay.
Bits 4: 0 = Normal Throughput, 1 = High Throughput.
Bits 5: 0 = Normal Relibility, 1 = High Relibility.
Bit 6-7: Reserved for Future Use.
0 1 2 3 4 5 6 7
+-----+-----+-----+-----+-----+-----+-----+-----+
| | | | | | |
| PRECEDENCE | D | T | R | 0 | 0 |
| | | | | | |
+-----+-----+-----+-----+-----+-----+-----+-----+
Precedence:
111 - Network Control
110 - Internetwork Control
101 - CRITIC/ECP
100 - Flash Override
011 - Flash
010 - Immediate
001 - Priority
000 - Routine
- tot_len:Total Length,封包的長度
- Identification:IP 封包最大可達 65535 Bytes,為了能夠放到 MAC frame 裡面,所以 IP 封包會分割,然後到了目的地電腦之後,再利用 Identification 來重組
- Flags:封包的屬性
- DF:封包能不能被切割
- MF:是不是某個封包的片段。如果設為
1,表示目前這個封包不是最後一個片段
Bit 0: reserved, must be zero
Bit 1: (DF) 0 = May Fragment, 1 = Don't Fragment.
Bit 2: (MF) 0 = Last Fragment, 1 = More Fragments.
0 1 2
+---+---+---+
| | D | M |
| 0 | F | F |
+---+---+---+
- Fragment Offset:被切割的 IP 封包在原本封包裡面的 offset,用來重組
- TTL:Time to Live,封包還可以被路由器轉送幾次,
ttl欄位歸零時,就會捨棄這個封包 - Protocol:上層協定是什麼,表示這一個 IP 封包是拿來裝 L4(上層)中哪個協定的資料,常用的為 TCP (6; 0x06) 或者 UDP(17; 0x11) 封包協定
- check, hc:Header Checksum,用來確認傳輸資料的正確與否
- SA:來源 IP 位址
- DA:目標 IP 位址
L2 網路層
概觀
Ethernet Frame
0 1 2 3 (10 byte)
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |S|Destination| Source |Eth| Payload & | |
| Preamble |F| Address | Address |Typ| PAD (Padding) | FCS |
| |D| | | | (大小不定) | |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
DA (6, Destination Address) + SA (6) + Type (2) 三者合起來又稱為 Ethernet header = 14 bytes
欄位
- Preamble:一連串的 1010…10,用來同步
- SFD (start of frame delimiter):為 10101011,用來表示經同步之後,資料的起始
- DA (Destination Address):目標的 MAC 位址,MAC 為 6 bytes 的硬體碼,從
00:00:00:00:00:00到FF:FF:FF:FF:FF:FF,前 3 碼為製造廠商碼,後 3 碼為廠商自訂的流水號 - SA (Source Address):來源的 MAC 位址
- DA & SA:Broadcast 的 MAC Address 是
FF:FF:FF:FF:FF:FF - Type (Ether Type):長度或者類別,payload 是哪一個類型的資訊,如 L3 是用什麼協定,IPv4 封包為
0x0800、ARP 封包為0x0806、IPv6 為0x86DD - Payload:MAC 封包所要傳送的資料內容,也就是 IP 封包或者 ARP 封包等等
- PAD (Padding):Ethernet 封包長度介於 46~1500 bytes 之間,因此假設 IP 封包長度沒有符合就必須做補滿的動作,aka 46 bytes ⇐ LLC+PAD ⇐ 1500 bytes
- FCS (Frame check sequence, CRC32):Checksum,用來確認傳送資料是否有錯誤