BTap.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @file BTap.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * TAP device abstraction.
  32. */
  33. #ifndef BADVPN_TUNTAP_BTAP_H
  34. #define BADVPN_TUNTAP_BTAP_H
  35. #if (defined(BADVPN_USE_WINAPI) + defined(BADVPN_LINUX) + defined(BADVPN_FREEBSD)) != 1
  36. #error Unknown TAP backend or too many TAP backends
  37. #endif
  38. #include <stdint.h>
  39. #ifdef BADVPN_USE_WINAPI
  40. #else
  41. #include <net/if.h>
  42. #endif
  43. #include <misc/debug.h>
  44. #include <misc/debugerror.h>
  45. #include <base/DebugObject.h>
  46. #include <system/BReactor.h>
  47. #include <flow/PacketRecvInterface.h>
  48. #define BTAP_ETHERNET_HEADER_LENGTH 14
  49. /**
  50. * Handler called when an error occurs on the device.
  51. * The object must be destroyed from the job context of this
  52. * handler, and no further I/O may occur.
  53. *
  54. * @param user as in {@link BTap_Init}
  55. */
  56. typedef void (*BTap_handler_error) (void *used);
  57. typedef struct {
  58. BReactor *reactor;
  59. BTap_handler_error handler_error;
  60. void *handler_error_user;
  61. int frame_mtu;
  62. PacketRecvInterface output;
  63. uint8_t *output_packet;
  64. #ifdef BADVPN_USE_WINAPI
  65. HANDLE device;
  66. BReactorIOCPOverlapped send_olap;
  67. BReactorIOCPOverlapped recv_olap;
  68. #else
  69. int close_fd;
  70. int fd;
  71. BFileDescriptor bfd;
  72. int poll_events;
  73. #endif
  74. DebugError d_err;
  75. DebugObject d_obj;
  76. } BTap;
  77. /**
  78. * Initializes the TAP device.
  79. *
  80. * @param o the object
  81. * @param BReactor {@link BReactor} we live in
  82. * @param devname name of the devece to open.
  83. * On Linux: a network interface name. If it is NULL, no
  84. * specific device will be requested, and the operating system
  85. * may create a new device.
  86. * On Windows: a string "component_id:device_name", where
  87. * component_id is a string identifying the driver, and device_name
  88. * is the name of the network interface. If component_id is empty,
  89. * a hardcoded default will be used instead. If device_name is empty,
  90. * the first device found with a matching component_id will be used.
  91. * Specifying a NULL devname is equivalent to specifying ":".
  92. * @param handler_error error handler function
  93. * @param handler_error_user value passed to error handler
  94. * @param tun whether to create a TUN (IP) device or a TAP (Ethernet) device. Must be 0 or 1.
  95. * @return 1 on success, 0 on failure
  96. */
  97. int BTap_Init (BTap *o, BReactor *bsys, char *devname, BTap_handler_error handler_error, void *handler_error_user, int tun) WARN_UNUSED;
  98. enum BTap_dev_type {BTAP_DEV_TUN, BTAP_DEV_TAP};
  99. enum BTap_init_type {
  100. BTAP_INIT_STRING,
  101. #ifndef BADVPN_USE_WINAPI
  102. BTAP_INIT_FD,
  103. #endif
  104. };
  105. struct BTap_init_data {
  106. enum BTap_dev_type dev_type;
  107. enum BTap_init_type init_type;
  108. union {
  109. char *string;
  110. struct {
  111. int fd;
  112. int mtu;
  113. } fd;
  114. } init;
  115. };
  116. /**
  117. * Initializes the TAP device.
  118. *
  119. * @param o the object
  120. * @param BReactor {@link BReactor} we live in
  121. * @param init_data struct containing initialization parameters (to allow transparent passing).
  122. * init.data.dev_type must be either BTAP_DEV_TUN for an IP device, or
  123. * BTAP_DEV_TAP for an Ethernet device.
  124. * init_data.init_type must be either BTAP_INIT_STRING or BTAP_INIT_FD.
  125. * For BTAP_INIT_STRING, init_data.init.string specifies the TUN or TAP
  126. * device, as described next.
  127. * On Linux: a network interface name. If it is NULL, no
  128. * specific device will be requested, and the operating system
  129. * may create a new device.
  130. * On Windows: a string "component_id:device_name", where
  131. * component_id is a string identifying the driver, and device_name
  132. * is the name of the network interface. If component_id is empty,
  133. * a hardcoded default will be used instead. If device_name is empty,
  134. * the first device found with a matching component_id will be used.
  135. * Specifying NULL is equivalent to specifying ":".
  136. * For BTAP_INIT_FD, the device is initialized using a file descriptor.
  137. * In this case, init_data.init.fd.fd must be set to the file descriptor,
  138. * and init_data.init.fd.mtu must be set to the largest IP packet or
  139. * Ethernet frame supported, for a TUN or TAP device, respectively.
  140. * File descriptor initialization is not supported on Windows.
  141. * @param handler_error error handler function
  142. * @param handler_error_user value passed to error handler
  143. * @return 1 on success, 0 on failure
  144. */
  145. int BTap_Init2 (BTap *o, BReactor *reactor, struct BTap_init_data init_data, BTap_handler_error handler_error, void *handler_error_user) WARN_UNUSED;
  146. /**
  147. * Frees the TAP device.
  148. *
  149. * @param o the object
  150. */
  151. void BTap_Free (BTap *o);
  152. /**
  153. * Returns the device's maximum transmission unit (including any protocol headers).
  154. *
  155. * @param o the object
  156. * @return device's MTU
  157. */
  158. int BTap_GetMTU (BTap *o);
  159. /**
  160. * Sends a packet to the device.
  161. * Any errors will be reported via a job.
  162. *
  163. * @param o the object
  164. * @param data packet to send
  165. * @param data_len length of packet. Must be >=0 and <=MTU, as reported by {@link BTap_GetMTU}.
  166. */
  167. void BTap_Send (BTap *o, uint8_t *data, int data_len);
  168. /**
  169. * Returns a {@link PacketRecvInterface} for reading packets from the device.
  170. * The MTU of the interface will be {@link BTap_GetMTU}.
  171. *
  172. * @param o the object
  173. * @return output interface
  174. */
  175. PacketRecvInterface * BTap_GetOutput (BTap *o);
  176. #endif