Analysis and Suggestions of IP tunneling in Linux-general Linux technology-Linux programming and kernel information. The following is a detailed description. This article is based on reading the Linux source code and some related materials.
You are welcome to give suggestions and comments on all aspects of the article. The author hopes to have more exchanges and discussions.
You are welcome to repost the original text on the Internet without retaining the integrity of the original text. Please notify the author of some references.
The repost and reference of this Article by traditional media must be approved by the author.
You are welcome to use the ideas proposed in this article in practical applications, and hope to inform the author at the same time.
Author Email: xiaoman04@hotmail.com
Thank you !!
**************************************** *************************
O,
With the increasing popularity of networks, network security has become a hot topic. This article analyzes the tunnel technology,
It focuses on the security field and provides suggestions on using tunneling technology to implement virtual private networks.
Why do I need an IP tunnel? Those who have never touched this concept Naturally raise such questions. Concept
The original proposal was very simple, in order to transmit data packets of other protocols in the TCP/IP network. Imagine IPX protocol or
How to transmit X.25 encapsulated data packets over the Internet
This is achieved by setting an IP protocol header on the source Protocol packet.
Unmount the IP header, restore it to the source protocol data packet, and send it to the target site. For source protocol data, such as IP Address
With a tunnel. This technology is available in the Amateur Packet Radio network,
Please let me know.) It is the most widely used.
The Protocol packets transmitted through the IP tunnel also include IP packets. This is what we analyze in this article.
To understand the IP address, we just put an IP packet in another IP address package. Why?
Let's do that. In fact, some applications will understand that Mobile IP (Mobile-IP) and IP multi-point Broadcast
(IP-Multicast) is two common examples. Currently, IP tunneling technology is used to construct a Virtual private network (VPC)
Private Network) also shows great charm. This article will also build a VPN using IP tunneling Technology
Simple Idea.
Background: Multiple understandings and implementations of tunnels
Internet researchers began to feel the need to build tunnels in the network many years ago.
Create a fixed path to bypass some potentially invalid gateways. It can be said that a tunnel is
Specific path.
This tunnel is implemented through the source routing option in the IP header. At present, this method is flawed.
Obviously. To set the source route option, you must know the exact path of the data packet.
Source routing is not supported in most routing implementations.
Another tunnel mechanism is to develop a new IP Option to demonstrate the source packet information, the original IP Header
May be part of this option. The meaning of this tunnel is very close to what we call the tunnel. But its
There is a lack of flexibility in implementing and processing the current IP Option.
The most common method is to develop a new IP packet protocol and still apply the current IP Header Format.
Through IP packets, the packets can reach the destination transparently without specifying the network path. You can also use the empty package
To create a virtual network. This method is easy, reliable, and scalable.
Strong scalability. Linux adopts this method, which is also the tunneling concept we understand currently.
I,
Structure and implementation of the packet Protocol
The implementation principle of the packet protocol is very simple. First, let's take a look at how the tunnel data packets flow in the network,
I.
For ease of description, I call the IP packet transmitted in the tunnel a packet.
-------------------------
/Subnet A \/subnet C \
/\/\
|
| & |
| ++ | ***** |
| ++ | ** |
| + | ****** |
\ + /-----------\**/----------
\ ++> # *****> (#) *****> # ++ \
--------------/** \ ------------/+ \
| ** | + |
| ** | + |
| ****** | ++ |
| ***** | V |
| & |
\/\/
\ Subnet B/\ subnet D/
---------------------
++ Original Datagram
* ***** Encapsulated data packets (packets)
# Encapsulation/unblocking
& User host
. Encapsulation Protocol Implementation Model
As shown in the figure, the device # is at both ends of the tunnel, respectively starting from packaging and unblocking)
In the entire data packet transmission path, except for the # device at both ends of the tunnel
Data packets are forwarded as common IP packets.
The device # Is a package based on two implementation components-encapsulated parts and unblocked parts. Encapsulate and unseal
All components (devices) must belong to two subnets at the same time. Encapsulate parts and add a packet header to the received Datagram
And then forward the part address as the destination address.
The original datagram is forwarded to the destination subnet.
The source end (encapsulation component) of the tunnel encapsulates the data packets entering the tunnel to form a packet. A complete
.
/+ --------------- +
| IP Address Header |
Header | + ----------------- +
| Packet protocol header |
\ + ----------------- +
/| Original protocol header |
| + --------------- +
|
Original datagram | original protocol data |
| ..
| ..
|
\ + ----------------- +
. Encapsulation structure
II,
Implementation in Linux
The version I analyzed is Linux2.0.34 (used by RedHat5.2 ).
In Linux, tunneling is implemented based on two files: new_tunnel.c and ipip. c.
At the same time, Linux defines a new protocol type-IPIP (IPPROTO_IPIP ),
The type is similar.
Basic Ideas
In Linux, the implementation of IP Tunnel is also divided into two parts: encapsulation parts and unblocking parts, which are separately delivered and connected
. However, these two parts are implemented in different ways at different levels.
Encapsulated components are implemented by virtual devices on the data link layer. For all source code, see
/Usr/src/linux/drivers/net/new_tunnel.c
For encapsulation, Linux implements a network device called tunl (similar to a loopback device ).
Features shared by other network devices. For upper-layer applications that use this device
Without distinction, the call and processing methods are of course the same.
Tunnel_init () and tunnel_xmit () are two main processes in new_tunnel.c.
Tunnel_init () initializes the device structure related to the device tunl.
Tunnel_xmit () is called when data is sent from the tunl device, and the tunl device is used as an IP tunnel.
In this process, all operations required to encapsulate the corresponding datagram are completed,
Form an IP packet and re-forward this packet (ip_forward ()).
The unseal part is implemented at the upper layer of the IP address, and the system regards it as a virtual transmission layer (in fact, there is no difference with the transport layer
Link). For details, see File
/Usr/src/linux/net/ipv4/ipip. c.
We know that each IP packet is processed by the ip_rcv function. After some necessary judgment, ip_rcv
Data packets sent to the local machine are sent to the upper-layer processing program. For an IPIP package, its processing function is
Ipip_rcv (just as the TCP packet processing function is tcp_rcv, the IP layer is not differentiated ). That is to say, when
After a local packet with the destination address arrives, the ip_rcv function performs some basic checks and removes the IP header. Then
It is unblocked by ipip_rcv.
Ipip_rcv is to remove the packet header, restore the data packet, and then put the restored data packet into the corresponding
Receive Queue (netif_rx ()).
From the above implementation of IP Tunnel, the idea is very clear, but due to the particularity of IP Tunnel, its
Implementation layers are not simple. In fact, its encapsulation and unblocking parts cannot be simply as described above
Layered. Although the tunl device should be included in the link layer, more work has been done in its sending program, such as creating an IPIP header.
And the new IP header (these are generally considered to work at the transport layer or network layer), call ip_forward to forward the new packet
It is not what a network device should do. It can be said that tunl has done a lot of work with the name of a network device.
Work is really 'product '. The unblocking component looks at the network layer in a macro sense. It solves the IPIP header and restores the original data packet.
But after it solves the packet (that is, the original complete Protocol packet), it
In the corresponding protocol receiving queue. This is not an upper-layer protocol, it is a network device interruption.
Obligations of the recipient. As you can see, it seems to be at the data link layer.
Is it a bit messy, and the tunnel mechanism is like this? Do you have a better solution?
III,
VPN Extension
In fact, Linux only provides a framework for implementing the tunneling mechanism. The packet protocol header in
Linux is ignored. That is to say, the packet header only contains the packet IP header, followed by the original IP packet.
This structure is used to transmit public data, but for a VPN, security is
Important functions. We hope that the tunnel data can be reliable and cannot be stolen or impersonated,
Encryption and authentication are essential.
To achieve this idea, design the following packet protocol headers:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service