First, let’s discuss the overall idea—creating a convenient debugging TCP/IP protocol stack, level-ip. The specific operations can be divided such that we do not have to use the system’s socket interface for TCP communication. Now, this goal has been preliminarily achieved. One required component is tun/tap, which forwards data between the virtual device and the actual network card, allowing the user-space protocol stack to relay through tun0. More content needs to be explored by yourself!
Testing Environment:VMWare virtual machine ubuntu16.04 (must support tun/tap), Windows TCP Server debugging assistant
1.Installation of level-ip on Ubuntu16.04
1.1Introduction to level-ip
1.1.1 Level-IP is a TCP protocol stack running in the Linux user space, with the following features:
Runs independently, completely isolated from applications
Uses Linux virtual network cards for communication
Compatible with the Linux Socket API
Provides network link services for other applications
1.1.2It is important to note that this TCP protocol stack is not perfect; it currently implements the following functions:
ARP requests, replies, and caching
ICMP ping and replies
IPv4 packet processing, checksum
TCPv4 handshake
TCP data transmission
TCP segmentation and reassembly
Timeout retransmission and RTT estimation
1.1.3 The following functions are yet to be improved:
IP fragmentation and reassembly
Sliding window
Confusing window and avoidance
Zero window probe
Congestion control
select sockets
1.2Using Level-IP
1.2.1 Installation
Get the Level-IP source code
git clone https://github.com/EmbedHacker/level-ip
Install libcap-dev tools to modify the permissions of executable programs
sudo apt install libcab-dev (error on git)
sudo apt install libcap-dev

Ensure that the system has installed gcc, make tools, then compile all target files
make all

Enable routing function
sudo sysctl -w net.ipv4.ip_forward=1
Accept input information from the virtual network card
sudo iptables -I INPUT –source 10.0.0.0/24 -j ACCEPT
Masquerade the physical network card’s IP, note that enp0s3 is the physical network card,
Users should modify according to the results of the ifconfig command
sudo iptables -t nat -I POSTROUTING –out-interface enp0s3 -j MASQUERADE
Set data forwarding from the physical network card to the virtual network card, remember to modify the physical network card!!
sudo iptables -I FORWARD –in-interface enp0s3 –out-interface tap0 -j ACCEPT
Set data forwarding from the virtual network card to the physical network card, remember to modify the physical network card!!
sudo iptables -I FORWARD –in-interface tap0 –out-interface enp0s3 -j ACCEPT
1.3Add virtual devices
tun network card operates at layer 3 without a MAC address; add a network card named tun0:ip tuntap add tun0 mode tuntap network card operates at layer 2 with a MAC address; add a network card named tap0:ip tuntap add tap0 mode tap

1.4Run the TCP protocol stack
./lvl-ip

1.5 Use level-ip to create your own TCP client
Not sure if you noticed this sentence: “Compatible with the Linux Socket API“. Below is a specific example for understanding.
In level-ip-master/tools/ directory

Link to liblevelip.so
// Define sockaddr_instruct sockaddr_in skaddr_in; skaddr_in.sin_family= AF_INET;skaddr_in.sin_port =htons(2048);skaddr_in.sin_addr.s_addr = inet_addr("192.168.99.1");struct sockaddr *addr = (sockaddr *)&skaddr_in;sock = socket(AF_INET, SOCK_STREAM, 0);if (connect(sock, addr, 16) == -1) {perror("Client could not establish connection");return 1;}std::cout << "tcp_v4_connect!" <<ret <<std::endl;char str[]="hello";int len = strlen(str);if (write(sock, str, len) != len) {printf("Write error\n");return 1;}else{printf("success !!! ");}

At this point, there may be questions, the socket interface is system-based, how does it switch to level-ip?

If you do not run level-ip, then you will:

Thus, we can debug the TCP/IP protocol stack on Linux.
Welcome to follow:
Linux tun/tap ping operation code example_3
Introduction to Linux virtual network card device tun/tap_2
Detailed tutorial on installing tun module on Linux
Please open in the WeChat client