BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 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  
34 #ifndef BADVPN_TUNTAP_BTAP_H
35 #define BADVPN_TUNTAP_BTAP_H
36  
37 #if (defined(BADVPN_USE_WINAPI) + defined(BADVPN_LINUX) + defined(BADVPN_FREEBSD)) != 1
38 #error Unknown TAP backend or too many TAP backends
39 #endif
40  
41 #include <stdint.h>
42  
43 #ifdef BADVPN_USE_WINAPI
44 #else
45 #include <net/if.h>
46 #endif
47  
48 #include <misc/debug.h>
49 #include <misc/debugerror.h>
50 #include <base/DebugObject.h>
51 #include <system/BReactor.h>
52 #include <flow/PacketRecvInterface.h>
53  
54 #define BTAP_ETHERNET_HEADER_LENGTH 14
55  
56 /**
57 * Handler called when an error occurs on the device.
58 * The object must be destroyed from the job context of this
59 * handler, and no further I/O may occur.
60 *
61 * @param user as in {@link BTap_Init}
62 */
63 typedef void (*BTap_handler_error) (void *used);
64  
65 typedef struct {
66 BReactor *reactor;
67 BTap_handler_error handler_error;
68 void *handler_error_user;
69 int frame_mtu;
70 PacketRecvInterface output;
71 uint8_t *output_packet;
72  
73 #ifdef BADVPN_USE_WINAPI
74 HANDLE device;
75 BReactorIOCPOverlapped send_olap;
76 BReactorIOCPOverlapped recv_olap;
77 #else
78 int close_fd;
79 int fd;
80 BFileDescriptor bfd;
81 int poll_events;
82 #endif
83  
84 DebugError d_err;
85 DebugObject d_obj;
86 } BTap;
87  
88 /**
89 * Initializes the TAP device.
90 *
91 * @param o the object
92 * @param BReactor {@link BReactor} we live in
93 * @param devname name of the devece to open.
94 * On Linux: a network interface name. If it is NULL, no
95 * specific device will be requested, and the operating system
96 * may create a new device.
97 * On Windows: a string "component_id:device_name", where
98 * component_id is a string identifying the driver, and device_name
99 * is the name of the network interface. If component_id is empty,
100 * a hardcoded default will be used instead. If device_name is empty,
101 * the first device found with a matching component_id will be used.
102 * Specifying a NULL devname is equivalent to specifying ":".
103 * @param handler_error error handler function
104 * @param handler_error_user value passed to error handler
105 * @param tun whether to create a TUN (IP) device or a TAP (Ethernet) device. Must be 0 or 1.
106 * @return 1 on success, 0 on failure
107 */
108 int BTap_Init (BTap *o, BReactor *bsys, char *devname, BTap_handler_error handler_error, void *handler_error_user, int tun) WARN_UNUSED;
109  
110 enum BTap_dev_type {BTAP_DEV_TUN, BTAP_DEV_TAP};
111  
112 enum BTap_init_type {
113 BTAP_INIT_STRING,
114 #ifndef BADVPN_USE_WINAPI
115 BTAP_INIT_FD,
116 #endif
117 };
118  
119 struct BTap_init_data {
120 enum BTap_dev_type dev_type;
121 enum BTap_init_type init_type;
122 union {
123 char *string;
124 struct {
125 int fd;
126 int mtu;
127 } fd;
128 } init;
129 };
130  
131 /**
132 * Initializes the TAP device.
133 *
134 * @param o the object
135 * @param BReactor {@link BReactor} we live in
136 * @param init_data struct containing initialization parameters (to allow transparent passing).
137 * init.data.dev_type must be either BTAP_DEV_TUN for an IP device, or
138 * BTAP_DEV_TAP for an Ethernet device.
139 * init_data.init_type must be either BTAP_INIT_STRING or BTAP_INIT_FD.
140 * For BTAP_INIT_STRING, init_data.init.string specifies the TUN or TAP
141 * device, as described next.
142 * On Linux: a network interface name. If it is NULL, no
143 * specific device will be requested, and the operating system
144 * may create a new device.
145 * On Windows: a string "component_id:device_name", where
146 * component_id is a string identifying the driver, and device_name
147 * is the name of the network interface. If component_id is empty,
148 * a hardcoded default will be used instead. If device_name is empty,
149 * the first device found with a matching component_id will be used.
150 * Specifying NULL is equivalent to specifying ":".
151 * For BTAP_INIT_FD, the device is initialized using a file descriptor.
152 * In this case, init_data.init.fd.fd must be set to the file descriptor,
153 * and init_data.init.fd.mtu must be set to the largest IP packet or
154 * Ethernet frame supported, for a TUN or TAP device, respectively.
155 * File descriptor initialization is not supported on Windows.
156 * @param handler_error error handler function
157 * @param handler_error_user value passed to error handler
158 * @return 1 on success, 0 on failure
159 */
160 int BTap_Init2 (BTap *o, BReactor *reactor, struct BTap_init_data init_data, BTap_handler_error handler_error, void *handler_error_user) WARN_UNUSED;
161  
162 /**
163 * Frees the TAP device.
164 *
165 * @param o the object
166 */
167 void BTap_Free (BTap *o);
168  
169 /**
170 * Returns the device's maximum transmission unit (including any protocol headers).
171 *
172 * @param o the object
173 * @return device's MTU
174 */
175 int BTap_GetMTU (BTap *o);
176  
177 /**
178 * Sends a packet to the device.
179 * Any errors will be reported via a job.
180 *
181 * @param o the object
182 * @param data packet to send
183 * @param data_len length of packet. Must be >=0 and <=MTU, as reported by {@link BTap_GetMTU}.
184 */
185 void BTap_Send (BTap *o, uint8_t *data, int data_len);
186  
187 /**
188 * Returns a {@link PacketRecvInterface} for reading packets from the device.
189 * The MTU of the interface will be {@link BTap_GetMTU}.
190 *
191 * @param o the object
192 * @return output interface
193 */
194 PacketRecvInterface * BTap_GetOutput (BTap *o);
195  
196 #endif