Analysis and Suggestions on IP tunneling in Linux

Source: Internet
Author: User
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:



0 4 8 16 24 31

+ ----- + ----------- + ------------------------ +

| Ver | type | hlen | OldPacketLen |

+ ----------------------- + ------------------------ +

| DeviceID | EncapID |

+ ----------------------- + ------------------------ +

| Flags | CheckSum |

+ ------------------------------------------------ +

| IPIP Options (If any) |

+ ------------------------------------------------ +

. | Padding |

..

+ ------------------------------------------------ +



IPIP header Concept



Ver: version to facilitate expansion

Type: used to build tunnels for different purposes (may be handled differently)

OldPacketLen: the length of the original packet that enters the tunnel

DeviceID: device ID for encapsulation of data packets

EncapID: ID of the packet

Flags: Flag bit, a total of 16 bits. The preliminary definition is as follows:

0 Reserved

1. Whether encrypted

2. Have you prepared a summary?

3. Signature?

4 Reserved

5. Whether message keys have been sent

6. Whether the Message key is encrypted

7. Whether the Message key needs to be retained

8-15 Reserved



CheckSum: Header Verification

IPIP Options: used to transmit necessary data, such as message keys and signatures.

Format: + ------------------------------------- +

| Type | length | data... |

+ ------------------------------------- +



Now, we can extend the Linux IP Tunnel to provide VPN services.

First, rewrite the new_tunnel.c and ipip. c files and add the IPIP header processing.



Next, we need to implement a key management and transfer mechanism.

Of course, symmetric keys are required, and sequential passwords are used to encrypt IP packets. From all considerations,

We can propose the logical steps for establishing a VPN;

1. Preparations: complete the configuration of the network-based installation system.

2. both ends of the tunnel send their own public passwords and device numbers to each other.

3. Generate a sequence password if necessary, and then encrypt the signature and send it to the other party.

4. normal communication ---- rest assured that your data is safe.



In a VPN tunnel, the format of a packet should be shown in four.



/+ --------------- +

| IP Address Header |

Header | + ----------------- +

| Packet protocol header |

\ + ----------------- +

/|

| Original protocol header |

| And |

Packet Data | original protocol data |

|. (Ciphertext ).

| ..

|

\ + ----------------- +





. VPN packet structure







Your usage methods.

Things are often not the best of both worlds. You must make a choice on the security intensity and communication speed,

(Otherwise, you need to choose between the security intensity and the cost of Money .)

You can use different protocols based on your needs.

Some:

0. Communication between multiple intranets across the Internet is not critical to confidentiality.

Direct use of the original framework mechanism without any encryption measures

This is fast and efficient, and the company does not need to apply for multiple IP addresses, which is convenient and feasible.

1. General commercial applications with confidentiality requirements

Encrypt the original data packet each time using the pre-generated sequence Password

Security is improved, which is a very practical method. As long as the strength is sufficient, it is generally difficult to crack

Fast

2. You think the password is not safe enough.

You can implement a password transfer method by yourself, and change the password at intervals.

Some of these handshakes need to be improved. If you are interested, you are welcome to discuss them.

If the development is mature, this method is promising.

4. Highly confidential Fields

Please use one password at a time and sign each time.

It takes a lot of time to generate new keys and signatures.

Almost not feasible.

However, it is believed that the Department in need can also try to increase its network bandwidth to make the network condition suitable.

This type of application.



In addition, you can also choose the encryption intensity, such as 128 bits, 512 bits, and 1024 bits.









IV,

To be improved



It mainly involves tunnel management. It is normal if an error occurs during packet transmission,

When a router detects an error, it sends an ICMP packet to the sender of the tunnel, but unfortunately

In addition to the IP header, data returned by ICMP only contains eight bytes of upper-layer protocol information. It's hard

The ICMP Information responds. Therefore, it is necessary to retain some status information on the tunnel end. This information

It mainly includes:

Accessibility at the other end of the tunnel

Tunnel congestion

Tunnel MTU

The packet information sent also needs to be retained. For example, when a route is inaccessible

At the time of arrival, the packet sender should be able to find out where the encapsulated data comes from and send the corresponding ICMP packet.



To emphasize one point, MTU update will be very important to the tunnel, because the next-level device of a flexible tunnel is

No. At the same time, some data packets require MTU change.


All of these are not handled in Linux. Everyone is working hard!

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.