当前位置:网站首页>Tun equipment principle
Tun equipment principle
2022-04-23 15:04:00 【Mrpre】
TUN Equipment principle
What this article wants to elaborate is sslvpn Operation mechanism of . Technical refinement of its problems , It is to solve two private networks that are separated by the public network environment , How to establish a channel . The core detail is tun equipment .TUN The device is also used in flannel in , Network communication between containers as a cross host .
simpletun
Let's start with a simple example https://github.com/gregnietsky/simpletun.git Then have an intuitive feeling
Let's find 2 Taiwan machine Separate use make Command to compile the above code , Generate Binary system simpletun
A Physics ip 11.238.116.73
B Physics ip 11.238.116.75
stay B Executed on machine
sudo ./simpletun -i tun90 -s -d &
sudo ifconfig tun90 6.6.6.2/24
stay A Executed on machine
sudo ./simpletun -i tun90 -c 11.238.116.75 -d &
sudo ifconfig tun90 6.6.6.1/24
simpletun Binary will help generate a file called tun90 Virtual network card , And then we use ifconfig To carry out ip Distribution of addresses
secondly ,B On the machine , adopt -s tell simpletun monitor This machine TCP Of 55555 port , And as a server ;A Machine pass -c Command attach B The physical ip.
Once the boot is complete , stay A Executed on machine ping 6.6.6.2 The following log will appear , Some logs are ping Output , Express ping success , Part of it is simpletun Printed logs .
PING 6.6.6.2 (6.6.6.2) 56(84) bytes of data.
TAP2NET 4: Read 84 bytes from the tap interface
TAP2NET 4: Written 84 bytes to the network
NET2TAP 4: Read 84 bytes from the network
NET2TAP 4: Written 84 bytes to the tap interface
64 bytes from 6.6.6.2: icmp_seq=1 ttl=64 time=39.7 ms
TAP2NET 5: Read 84 bytes from the tap interface
TAP2NET 5: Written 84 bytes to the network
NET2TAP 5: Read 84 bytes from the network
NET2TAP 5: Written 84 bytes to the tap interface
64 bytes from 6.6.6.2: icmp_seq=2 ttl=64 time=39.9 ms
alike , We are B Execute above nc -l -k 9090 monitor This machine 9090 Port program , And then again A Execute above nc 6.6.6.2 9090 Rear knock in abc
A Output
$nc 6.6.6.2 9090
TAP2NET 12: Read 60 bytes from the tap interface
TAP2NET 12: Written 60 bytes to the network
NET2TAP 11: Read 60 bytes from the network
NET2TAP 11: Written 60 bytes to the tap interface
TAP2NET 13: Read 52 bytes from the tap interface
TAP2NET 13: Written 52 bytes to the network
abc
TAP2NET 14: Read 56 bytes from the tap interface
TAP2NET 14: Written 56 bytes to the network
NET2TAP 12: Read 52 bytes from the network
NET2TAP 12: Written 52 bytes to the tap interface
B Output
NET2TAP 12: Read 60 bytes from the network
NET2TAP 12: Written 60 bytes to the tap interface
TAP2NET 11: Read 60 bytes from the tap interface
TAP2NET 11: Written 60 bytes to the network
NET2TAP 13: Read 52 bytes from the network
NET2TAP 13: Written 52 bytes to the tap interface
NET2TAP 14: Read 56 bytes from the network
NET2TAP 14: Written 56 bytes to the tap interface
TAP2NET 12: Read 52 bytes from the tap interface
TAP2NET 12: Written 52 bytes to the network
abc
We found that ,A The machine passes through tcp visit 6.6.6.2 Of 9090 port , This 6.6.6.0/24 Equivalent to private network request , and A and B There is no network between 6.6.6.0/24 Route of network segment , that B How to receive A What about the private network request sent , Of course, this is the doubt that we need to explain in this article .
TUN Equipment characteristics
about tun The equipment write operation (open tun, Then on fd Conduct write),tun After the user state data is obtained in the kernel state , call netif_rx, Usually ,netif_rx This function is called and submitted to the protocol stack only when the machine receives the message , Show that we are right tun The equipment write operation , It's equivalent to letting the machine simulate receiving packets . So the key point is ,tunwrite The data must contain three protocol headers (IP), otherwise linux The protocol stack cannot route it .
about tun Data acquisition operation of the device , First tun Under what circumstances does the equipment , Will get the data ? That is, when we have no perception in external applications tun In the presence of equipment ,tun How does the device get data from external programs .
First tun The equipment has three layers ip Address of the , Which means ,tun The device can be used as a three-layer forwarding interface to send data , We just need to let External procedures Requests made , Be routed to tun Equipment can . for example We visit 200.100.1.1 This public network address goes tun equipment ,route add -net 200.100.1.1/32 dev tun90, So the request will go tun Go out .
then tun The equipment The driver and Conventional similar eth Different , It doesn't really send data from its own interface .
tun The sending interface of the device , The operation is to data Save it and put it in your list On , Then notify the listener tun The user program of the device reads .
So I want to TUN Intercepted data , Then you need the destination address of the application , Can from TUN Equipment port “ get out ”, Configuring a route is a common operation .
Be careful tun Data acquired by the device , When carrying the data of the three-tier head .
TUN practice
The above explanation says TUN The characteristics of the device , All features are TUN Character driver and device driver implementation , We combine this feature , You can make a simple private network traversal function , That's the beginning simpletun The function of

Request to send
1、 Applications send data , for example ping To 6.6.6.2, Because of the routing and other rules configured locally , The data is linux The kernel routes to tun90 Send .
2、tun90 After getting the data , There will be no real sending operation ,, Instead, it is encapsulated into three-tier messages , Then put it in your own queue , Wait for the user program to read , This is it. tun The characteristics of the device .
here tun client Will get the data . Data is data that contains three-tier headers , One of the sources ip yes tun The equipment 6.6.6.1 The destination address is the destination address accessed by the application 6.6.6.2.
3、tun client take This contains three-tier header data , Send through the public network , natural The five tuples on this public network are the local physical address and the destination physical address , Naturally, this message can be sent through the public network .
4、tun server received data , The data is The previous step 2 Data in , Contains 6.6.6.1->6.6.6.2 The data of this three-tier header ,tun server writes tun equipment .
5、tun90 Received from the user write After the operation , Will throw data into linux Protocol stack ,linux The protocol stack will parse 6.6.6.1->6.6.6.2 How to forward the data of the three-tier header , natural 6.6.6.2 Is your local address , Nature can handle .
Be careful 1 Sending of , the truth is that tun The equipment xmit send out ,4 Of write the truth is that tun The equipment receive, It's different .
This example is only based on PING As an example . If The physical machine A Of Applications What I visited was One The address of the public network , for example 100.100.100.100, that , The physical machine B If it's on ip_forward And myself and 100.100.100.100 through , that , The request is forwarded to 100.100.100.100, Turn on nat Under the circumstances , Will send to the public network 11.238.116.75 -> 100.100.100.100 Request , The response data 100.100.100.100->11.238.116.75 Come back and rely on nat restore 100.100.100.100->6.6.6.1.
Data response
The back package is also very interesting
6、 hypothesis machine B To reply 6.6.6.1->6.6.6.2 PING Corresponding Response package , So the machine B The response of IP The head is 6.6.6.2->6.6.6.1, This packet will be routed to the machine tun90, This process is equivalent to the above sending process 1,tun90 After intercepting this packet , expect tun server Read it .
7、tun server Detected tun The device has data , So I read the data , The data contains 6.6.6.2->6.6.6.1 This ip Header data .
8、tun server Use tun client Established access , Transmit data to via the public network client.
9、tun client Writes data to machine A Of tun.
10、 according to tun characteristic ,tun Opportunity will contain 6.6.6.2->6.6.6.1 This ip Header data The data will be thrown into the protocol stack ,linux can such , Externally executed ping You can get the response data .
版权声明
本文为[Mrpre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231409587869.html
边栏推荐
- Share 3 tools, edit 5 works at home and earn more than 400
- Leetcode162 - find peak - dichotomy - array
- Flink datastream type system typeinformation
- Introduction to distributed transaction Seata
- Pnpm installation and use
- Bingbing learning notes: take you step by step to realize the sequence table
- 2-GO variable operation
- ffmpeg安装遇错:nasm/yasm not found or too old. Use --disable-x86asm for a crippled build.
- Nuxt project: Global get process Env information
- Is asemi ultrafast recovery diode interchangeable with Schottky diode
猜你喜欢

OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt

Lotus DB design and Implementation - 1 Basic Concepts

LeetCode165-比较版本号-双指针-字符串

My raspberry PI zero 2W tossing notes record some problems encountered and solutions

Reptile exercises (1)

What is the main purpose of PCIe X1 slot?

博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA

Swift protocol Association object resource name management multithreading GCD delay once

eolink 如何助力遠程辦公

Don't you know the usage scenario of the responsibility chain model?
随机推荐
Explanation and example application of the principle of logistic regression in machine learning
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
On the day of entry, I cried (mushroom street was laid off and fought for seven months to win the offer)
如何设计一个良好的API接口?
[thymeleaf] handle null values and use safe operators
Programming philosophy - automatic loading, dependency injection and control inversion
Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
The art of automation
22年了你还不知道文件包含漏洞?
SQLSERVER事物与锁的问题
async void 导致程序崩溃
Difference between like and regexp
Detailed explanation of C language knowledge points -- first understanding of C language [1] - vs2022 debugging skills and code practice [1]
For 22 years, you didn't know the file contained vulnerabilities?
Three uses of kprobe
Comment eolink facilite le télétravail
冰冰学习笔记:一步一步带你实现顺序表
牛客网数据库SQL实战详细剖析(26-30)
Brute force of DVWA low -- > High
Progress in the treatment of depression