用空间户应用程序可以通过TUN/TAP直接从网络层或者数据链路层获取IP数据包或者以太帧
3.2 Frame format:
If flag IFF_NO_PI is not set each frame format is:
Flags [2 bytes]
Proto [2 bytes]
Raw protocol(IP, IPv6, etc) frame.
发送的 ack 序列号表示直到 ack 之前(不包括 ack)的字节都已经接收到了
ip addr
目前为止在尝试读取IP数据报,针对其中的TCP请求做出响应,目前尝试返回了一个SYN ACK响应
- 先运行程序
- 然后
tshark -i tun0
抓取IP数据报 - 再通过
nc 192.168.0.2 443
发起Ping命令,查看程序的运行结果
接收到的raw frame
[00, 00, 08, 00, 45, 00, 00, 3c, 1b, 4c, 40, 00, 40, 06, 9e, 1c, c0, a8, 00, 01, c0, a8, 00, 02, ae, da, 01, bb, 50, fb, 43, f1, 00, 00, 00, 00, a0, 02, fa, f0, a3, 64, 00, 00, 02, 04, 05, b4, 04, 02, 08, 0a, f3, 74, ef, 5f, 00, 00, 00, 00, 01, 03, 03, 07]
对应tshark抓到的包为192.168.0.1 → 192.168.0.2 TCP 60 44762 → 443 [SYN] Seq=0 Win=64240 Len=0 MSS=1460 SACK_PERM=1 TSval=4084525925 TSecr=0 WS=128
其中flags是[00, 00]也就是0
proto是[08, 00]也就是0x0800 Internet Protocol version 4 (IPv4)
然后剩下的IP数据报的内容,其中IP
header的数据为[45, 00, 00, 3c, 1b, 4c, 40, 00, 40, 06, 9e, 1c, c0, a8, 00, 01, c0, a8, 00, 02]
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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|Version| IHL |Type of Service| Total Length |
| 4 | 5 | 00 | 003C |
|0 1 0 0|0 1 0 1|0 0 0 0 0 0 0 0|0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0|
| 4 | 5 | Routine | 60 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Identification |Flags| Fragment Offset |
| 1B4C | 4000 |
|0 0 0 1 1 0 1 1 0 1 0 0 1 1 0 0|0 1 0|0 0 0 0 0 0 0 0 0 0 0 0 0|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Time to Live | Protocol | Header Checksum |
| 40 | 06 | 9E1C |
|0 1 0 0 0 0 0 0|0 0 0 0 0 1 1 0|1 0 0 1 1 1 1 0 0 0 0 1 1 1 0 0|
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Address |
| C0A80001 |
|1 1 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1|
| 192.168.0.1 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Destination Address |
| C0A80002 |
|1 1 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0|
| 192.168.0.2 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
再剩下的是TCP的数据[ae, da, 01, bb, 50, fb, 43, f1, 00, 00, 00, 00, a0, 02, fa, f0, a3, 64, 00, 00, 02, 04, 05, b4, 04, 02, 08, 0a, f3, 74, ef, 5f, 00, 00, 00, 00, 01, 03, 03, 07]
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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Source Port | Destination Port |
| AE DA | 01 BB |
| 44762 | 443 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Sequence Number |
| 50 FB 43 F1 |
| 1358644209 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Acknowledgment Number |
| 00 00 00 00 |
| 0 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Data | |U|A|P|R|S|F| |
| Offset| Reserved |R|C|S|S|Y|I| Window |
| | |G|K|H|T|N|N| |
| A | 002 | FA F0 |
| 10 |0 0 0 0 0 0 0 0 0 0 1 0| 64240 |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Checksum | Urgent Pointer |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Options | Padding |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| data |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+