tuntap.txt 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. Universal TUN/TAP device driver.
  2. Copyright (C) 1999-2000 Maxim Krasnyansky <max_mk@yahoo.com>
  3. Linux, Solaris drivers
  4. Copyright (C) 1999-2000 Maxim Krasnyansky <max_mk@yahoo.com>
  5. FreeBSD TAP driver
  6. Copyright (c) 1999-2000 Maksim Yevmenkin <m_evmenkin@yahoo.com>
  7. Revision of this document 2002 by Florian Thiel <florian.thiel@gmx.net>
  8. 1. Description
  9. TUN/TAP provides packet reception and transmission for user space programs.
  10. It can be seen as a simple Point-to-Point or Ethernet device, which,
  11. instead of receiving packets from physical media, receives them from
  12. user space program and instead of sending packets via physical media
  13. writes them to the user space program.
  14. In order to use the driver a program has to open /dev/net/tun and issue a
  15. corresponding ioctl() to register a network device with the kernel. A network
  16. device will appear as tunXX or tapXX, depending on the options chosen. When
  17. the program closes the file descriptor, the network device and all
  18. corresponding routes will disappear.
  19. Depending on the type of device chosen the userspace program has to read/write
  20. IP packets (with tun) or ethernet frames (with tap). Which one is being used
  21. depends on the flags given with the ioctl().
  22. The package from http://vtun.sourceforge.net/tun contains two simple examples
  23. for how to use tun and tap devices. Both programs work like a bridge between
  24. two network interfaces.
  25. br_select.c - bridge based on select system call.
  26. br_sigio.c - bridge based on async io and SIGIO signal.
  27. However, the best example is VTun http://vtun.sourceforge.net :))
  28. 2. Configuration
  29. Create device node:
  30. mkdir /dev/net (if it doesn't exist already)
  31. mknod /dev/net/tun c 10 200
  32. Set permissions:
  33. e.g. chmod 0666 /dev/net/tun
  34. There's no harm in allowing the device to be accessible by non-root users,
  35. since CAP_NET_ADMIN is required for creating network devices or for
  36. connecting to network devices which aren't owned by the user in question.
  37. If you want to create persistent devices and give ownership of them to
  38. unprivileged users, then you need the /dev/net/tun device to be usable by
  39. those users.
  40. Driver module autoloading
  41. Make sure that "Kernel module loader" - module auto-loading
  42. support is enabled in your kernel. The kernel should load it on
  43. first access.
  44. Manual loading
  45. insert the module by hand:
  46. modprobe tun
  47. If you do it the latter way, you have to load the module every time you
  48. need it, if you do it the other way it will be automatically loaded when
  49. /dev/net/tun is being opened.
  50. 3. Program interface
  51. 3.1 Network device allocation:
  52. char *dev should be the name of the device with a format string (e.g.
  53. "tun%d"), but (as far as I can see) this can be any valid network device name.
  54. Note that the character pointer becomes overwritten with the real device name
  55. (e.g. "tun0")
  56. #include <linux/if.h>
  57. #include <linux/if_tun.h>
  58. int tun_alloc(char *dev)
  59. {
  60. struct ifreq ifr;
  61. int fd, err;
  62. if( (fd = open("/dev/net/tun", O_RDWR)) < 0 )
  63. return tun_alloc_old(dev);
  64. memset(&ifr, 0, sizeof(ifr));
  65. /* Flags: IFF_TUN - TUN device (no Ethernet headers)
  66. * IFF_TAP - TAP device
  67. *
  68. * IFF_NO_PI - Do not provide packet information
  69. */
  70. ifr.ifr_flags = IFF_TUN;
  71. if( *dev )
  72. strncpy(ifr.ifr_name, dev, IFNAMSIZ);
  73. if( (err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0 ){
  74. close(fd);
  75. return err;
  76. }
  77. strcpy(dev, ifr.ifr_name);
  78. return fd;
  79. }
  80. 3.2 Frame format:
  81. If flag IFF_NO_PI is not set each frame format is:
  82. Flags [2 bytes]
  83. Proto [2 bytes]
  84. Raw protocol(IP, IPv6, etc) frame.
  85. 3.3 Multiqueue tuntap interface:
  86. From version 3.8, Linux supports multiqueue tuntap which can uses multiple
  87. file descriptors (queues) to parallelize packets sending or receiving. The
  88. device allocation is the same as before, and if user wants to create multiple
  89. queues, TUNSETIFF with the same device name must be called many times with
  90. IFF_MULTI_QUEUE flag.
  91. char *dev should be the name of the device, queues is the number of queues to
  92. be created, fds is used to store and return the file descriptors (queues)
  93. created to the caller. Each file descriptor were served as the interface of a
  94. queue which could be accessed by userspace.
  95. #include <linux/if.h>
  96. #include <linux/if_tun.h>
  97. int tun_alloc_mq(char *dev, int queues, int *fds)
  98. {
  99. struct ifreq ifr;
  100. int fd, err, i;
  101. if (!dev)
  102. return -1;
  103. memset(&ifr, 0, sizeof(ifr));
  104. /* Flags: IFF_TUN - TUN device (no Ethernet headers)
  105. * IFF_TAP - TAP device
  106. *
  107. * IFF_NO_PI - Do not provide packet information
  108. * IFF_MULTI_QUEUE - Create a queue of multiqueue device
  109. */
  110. ifr.ifr_flags = IFF_TAP | IFF_NO_PI | IFF_MULTI_QUEUE;
  111. strcpy(ifr.ifr_name, dev);
  112. for (i = 0; i < queues; i++) {
  113. if ((fd = open("/dev/net/tun", O_RDWR)) < 0)
  114. goto err;
  115. err = ioctl(fd, TUNSETIFF, (void *)&ifr);
  116. if (err) {
  117. close(fd);
  118. goto err;
  119. }
  120. fds[i] = fd;
  121. }
  122. return 0;
  123. err:
  124. for (--i; i >= 0; i--)
  125. close(fds[i]);
  126. return err;
  127. }
  128. A new ioctl(TUNSETQUEUE) were introduced to enable or disable a queue. When
  129. calling it with IFF_DETACH_QUEUE flag, the queue were disabled. And when
  130. calling it with IFF_ATTACH_QUEUE flag, the queue were enabled. The queue were
  131. enabled by default after it was created through TUNSETIFF.
  132. fd is the file descriptor (queue) that we want to enable or disable, when
  133. enable is true we enable it, otherwise we disable it
  134. #include <linux/if.h>
  135. #include <linux/if_tun.h>
  136. int tun_set_queue(int fd, int enable)
  137. {
  138. struct ifreq ifr;
  139. memset(&ifr, 0, sizeof(ifr));
  140. if (enable)
  141. ifr.ifr_flags = IFF_ATTACH_QUEUE;
  142. else
  143. ifr.ifr_flags = IFF_DETACH_QUEUE;
  144. return ioctl(fd, TUNSETQUEUE, (void *)&ifr);
  145. }
  146. Universal TUN/TAP device driver Frequently Asked Question.
  147. 1. What platforms are supported by TUN/TAP driver ?
  148. Currently driver has been written for 3 Unices:
  149. Linux kernels 2.2.x, 2.4.x
  150. FreeBSD 3.x, 4.x, 5.x
  151. Solaris 2.6, 7.0, 8.0
  152. 2. What is TUN/TAP driver used for?
  153. As mentioned above, main purpose of TUN/TAP driver is tunneling.
  154. It is used by VTun (http://vtun.sourceforge.net).
  155. Another interesting application using TUN/TAP is pipsecd
  156. (http://perso.enst.fr/~beyssac/pipsec/), a userspace IPSec
  157. implementation that can use complete kernel routing (unlike FreeS/WAN).
  158. 3. How does Virtual network device actually work ?
  159. Virtual network device can be viewed as a simple Point-to-Point or
  160. Ethernet device, which instead of receiving packets from a physical
  161. media, receives them from user space program and instead of sending
  162. packets via physical media sends them to the user space program.
  163. Let's say that you configured IPX on the tap0, then whenever
  164. the kernel sends an IPX packet to tap0, it is passed to the application
  165. (VTun for example). The application encrypts, compresses and sends it to
  166. the other side over TCP or UDP. The application on the other side decompresses
  167. and decrypts the data received and writes the packet to the TAP device,
  168. the kernel handles the packet like it came from real physical device.
  169. 4. What is the difference between TUN driver and TAP driver?
  170. TUN works with IP frames. TAP works with Ethernet frames.
  171. This means that you have to read/write IP packets when you are using tun and
  172. ethernet frames when using tap.
  173. 5. What is the difference between BPF and TUN/TAP driver?
  174. BPF is an advanced packet filter. It can be attached to existing
  175. network interface. It does not provide a virtual network interface.
  176. A TUN/TAP driver does provide a virtual network interface and it is possible
  177. to attach BPF to this interface.
  178. 6. Does TAP driver support kernel Ethernet bridging?
  179. Yes. Linux and FreeBSD drivers support Ethernet bridging.