Linux under the TUN/TAP virtual network card use-Heidsoft__linux

Source: Internet
Author: User

Http://www.cnblogs.com/heidsoft/p/3525646.html?utm_source=tuicool&utm_medium=referral

The Tun/tap driver realizes the function of the virtual NIC, Tun means the virtual point-to-point device, tap means the virtual Ethernet device, the two devices implement different encapsulation for the network package. With Tun/tap Drive, the TCP/IP protocol stack can be transmitted to any one of the tun/tap-driven processes, which is processed and then sent to the physical link.
Open source Projects OpenVPN (http://openvpn.sourceforge.net) and Vtun (http://vtun.sourceforge.net) are tunnel packages that are implemented using the Tun/tap driver.

first, the Tun/tap driver working principle

As a virtual NIC driver, TUN/TAP driver data receive and send not directly with the real network card, he added a TUN/TAP virtual network device driver and a related character device/dev/net/tun in the Linux kernel. A character device Tun an interface for exchanging data between user space and kernel space. When the kernel sends packets to a virtual network device, the packet is guaranteed to be in a device-related queue until the user-space program is read by the Tun descriptor of the open character device, and it is copied into the buffer of the user space, and the effect is equivalent to the packet being sent directly to the user space. This is similar to the principle of sending a packet through system call write.

Under Linux, in order to realize the interaction between kernel space and user's spatial data, there are many ways: the universal socket can create special sockets, use sockets to realize data interaction, create files through the proc file system to interact with the data, and use the method of device files. Access to device files will call the device driver corresponding routines, device driver itself is the kernel space and user space an interface, TUN/TAP drive is the use of device files to achieve user space and kernel space data interaction.

Structurally speaking, the Tun/tap driver is not simply the implementation of the NIC driver, but it also implements the character device drive part. Connect user space and kernel space in the form of character devices. Here is the schematic:

The Tun/tap driver contains two parts, part of the character device driver, and part of the NIC driver. Using the NIC driver to receive the network subcontracting from the TCP/IP protocol stack and to send or reverse the received network packet to the protocol stack, the character driver part transmits the network packet between the user space and the kernel space, and simulates the data receiving and sending of the physical link. The Tun/tap drive is a good combination of two drives.

Second, TUN/TAP network card creation

1. Confirm if the kernel supports Tun/tap

Confirm if the kernel has a Tun module

  [root@hunterfu]# modinfo tun
  filename:       /lib/modules/2.6.34.7-56.fc13.i686.pae/kernel/drivers/net/tun.ko
  alias:          char-major-10-200
  License:        GPL
  Author:         (C) 1999-2004 Max Krasnyansky < maxk@qualcomm.com>
  Description:    Universal Tun/tap Device driver Srcversion
  :     880de258930fe60d765b735
  depends:        
  vermagic:       2.6.34.7-56.fc13.i686.pae SMP mod_unload 686 

Load Kernel module-

  [Root@hunterfu ~]#  modprobe tun
  [root@hunterfu ~]# lsmod | grep tun
  tun                    10548  1 
After executing the above command, the output appears as above, indicating that the module was loaded successfully

2. Create and configure virtual network adapters

Confirm that there is a tunctl command, if it is not installed via Yum

  [ROOT@HUNTERFU ~]# Yum Install Tunctl

Create a virtual network adapter device

  [Root@hunterfu ~]# tunctl-t tap0-u Root

Set up a virtual network card

  [Root@hunterfu ~]# ifconfig tap0 192.168.0.1 netmask 255.255.255.0 promisc  
After the operation, the virtual network card has been established and configured well.

3. Create virtual network card as system service automatically starts with system

Writing configuration scripts (conforming to the CHKCONFIG specification)

  [Root@hunterfu ~]# Cat/etc/init.d/config_tap 
  #!/bin/bash
  #
  Config_tap Start up the          tun/tap virtual Nic
  #
  chkconfig:2345
  
  user= "root"
  tap_network= "192.168.0.1
  tap_dev_num=0
  desc=" TAP config "
  
  Do_start () {
    if [!-x/usr/sbin/tunctl]; then
      echo"/usr/sbin/tunctl is not found! "
      Exit 1
    fi
    tunctl-t tap$tap_dev_num-u root
    ifconfig tap$tap_dev_num ${tap_network}  netmask 255.255.255.0 promisc
    ifconfig tap$tap_dev_num
  }
  
  do_stop () {ifconfig
    tap$tap_dev_num down 
  }
  Do_restart () {
    do_stop
    do_start
  }
  check_status () {
    ifconfig tap$tap_dev_num 
  }
  
  Case "in 
    start"    do_start;;
    stop)     do_stop;;
    restart)  Do_restart;;
    Status)
              echo "Status of $DESC:"
              check_status
              exit "$?"
              ;
    *)
  	echo "Usage: $ {start|stop|restart|status}"
  	exit 1 
  Esac
This script can be modified to suit specific requirements

Adding to the system service

  [Root@hunterfu ~]# chkconfig--add config_tap 
  [root@hunterfu ~]# chkconfig--level 345-Config_tap on
Once the operation is complete, you can, like other standard services, pass Service Config_tap StartTo create and initiate the operation of the black hole @heidsoft
Github:https://github.com/heidsoft
Micro Blog: http://weibo.com/liuganbin
Passionate about cloud computing and big data
Focus on Cloudstack,openstack,linux C/c++/python/java
Focus on new technologies

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.