tuntap.rst 8.1 KB

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