Add OpenVPN 2.1rc12 source (unconfigured)
[tomato.git] / release / src / router / openvpn / tun.h
blob072d550a3c50c2e00906a9bc3ed97b039e6bbb91
1 /*
2 * OpenVPN -- An application to securely tunnel IP networks
3 * over a single TCP/UDP port, with support for SSL/TLS-based
4 * session authentication and key exchange,
5 * packet encryption, packet authentication, and
6 * packet compression.
8 * Copyright (C) 2002-2008 Telethra, Inc. <sales@openvpn.net>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2
12 * as published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program (see the file COPYING included with this
21 * distribution); if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifndef TUN_H
26 #define TUN_H
28 #ifdef WIN32
29 #include <winioctl.h>
30 #include "tap-win32/common.h"
31 #endif
33 #include "buffer.h"
34 #include "error.h"
35 #include "mtu.h"
36 #include "win32.h"
37 #include "event.h"
38 #include "proto.h"
39 #include "misc.h"
41 #ifdef WIN32
43 /* time constants for --ip-win32 adaptive */
44 #define IPW32_SET_ADAPTIVE_DELAY_WINDOW 300
45 #define IPW32_SET_ADAPTIVE_TRY_NETSH 20
47 struct tuntap_options {
48 /* --ip-win32 options */
49 bool ip_win32_defined;
51 # define IPW32_SET_MANUAL 0 /* "--ip-win32 manual" */
52 # define IPW32_SET_NETSH 1 /* "--ip-win32 netsh" */
53 # define IPW32_SET_IPAPI 2 /* "--ip-win32 ipapi" */
54 # define IPW32_SET_DHCP_MASQ 3 /* "--ip-win32 dynamic" */
55 # define IPW32_SET_ADAPTIVE 4 /* "--ip-win32 adaptive" */
56 # define IPW32_SET_N 5
57 int ip_win32_type;
59 /* --ip-win32 dynamic options */
60 bool dhcp_masq_custom_offset;
61 int dhcp_masq_offset;
62 int dhcp_lease_time;
64 /* --tap-sleep option */
65 int tap_sleep;
67 /* --dhcp-option options */
69 bool dhcp_options;
71 const char *domain; /* DOMAIN (15) */
73 const char *netbios_scope; /* NBS (47) */
75 int netbios_node_type; /* NBT 1,2,4,8 (46) */
77 #define N_DHCP_ADDR 4 /* Max # of addresses allowed for
78 DNS, WINS, etc. */
80 /* DNS (6) */
81 in_addr_t dns[N_DHCP_ADDR];
82 int dns_len;
84 /* WINS (44) */
85 in_addr_t wins[N_DHCP_ADDR];
86 int wins_len;
88 /* NTP (42) */
89 in_addr_t ntp[N_DHCP_ADDR];
90 int ntp_len;
92 /* NBDD (45) */
93 in_addr_t nbdd[N_DHCP_ADDR];
94 int nbdd_len;
96 /* DISABLE_NBT (43, Vendor option 001) */
97 bool disable_nbt;
99 bool dhcp_renew;
100 bool dhcp_pre_release;
101 bool dhcp_release;
104 #elif TARGET_LINUX
106 struct tuntap_options {
107 int txqueuelen;
110 #else
112 struct tuntap_options {
113 int dummy; /* not used */
116 #endif
119 * Define a TUN/TAP dev.
122 struct tuntap
124 # define TUNNEL_TYPE(tt) ((tt) ? ((tt)->type) : DEV_TYPE_UNDEF)
125 int type; /* DEV_TYPE_x as defined in proto.h */
127 # define TUNNEL_TOPOLOGY(tt) ((tt) ? ((tt)->topology) : TOP_UNDEF)
128 int topology; /* one of the TOP_x values */
130 bool did_ifconfig_setup;
131 bool did_ifconfig;
133 bool ipv6;
135 struct tuntap_options options; /* options set on command line */
137 char *actual_name; /* actual name of TUN/TAP dev, usually including unit number */
139 /* number of TX buffers */
140 int txqueuelen;
142 /* ifconfig parameters */
143 in_addr_t local;
144 in_addr_t remote_netmask;
145 in_addr_t broadcast;
147 #ifdef WIN32
148 HANDLE hand;
149 struct overlapped_io reads;
150 struct overlapped_io writes;
151 struct rw_handle rw_handle;
153 /* used for setting interface address via IP Helper API
154 or DHCP masquerade */
155 bool ipapi_context_defined;
156 ULONG ipapi_context;
157 ULONG ipapi_instance;
158 in_addr_t adapter_netmask;
160 /* Windows adapter index for TAP-Win32 adapter,
161 ~0 if undefined */
162 DWORD adapter_index;
164 int standby_iter;
165 #else
166 int fd; /* file descriptor for TUN/TAP dev */
167 #endif
169 #ifdef TARGET_SOLARIS
170 int ip_fd;
171 #endif
173 /* used for printing status info only */
174 unsigned int rwflags_debug;
176 /* Some TUN/TAP drivers like to be ioctled for mtu
177 after open */
178 int post_open_mtu;
181 static inline bool
182 tuntap_defined (const struct tuntap *tt)
184 #ifdef WIN32
185 return tt && tt->hand != NULL;
186 #else
187 return tt && tt->fd >= 0;
188 #endif
192 * Function prototypes
195 void clear_tuntap (struct tuntap *tuntap);
197 void open_tun (const char *dev, const char *dev_type, const char *dev_node,
198 bool ipv6, struct tuntap *tt);
200 void close_tun (struct tuntap *tt);
202 int write_tun (struct tuntap* tt, uint8_t *buf, int len);
204 int read_tun (struct tuntap* tt, uint8_t *buf, int len);
206 void tuncfg (const char *dev, const char *dev_type, const char *dev_node,
207 bool ipv6, int persist_mode, const char *username,
208 const char *groupname, const struct tuntap_options *options);
210 const char *guess_tuntap_dev (const char *dev,
211 const char *dev_type,
212 const char *dev_node,
213 struct gc_arena *gc);
215 struct tuntap *init_tun (const char *dev, /* --dev option */
216 const char *dev_type, /* --dev-type option */
217 int topology, /* one of the TOP_x values */
218 const char *ifconfig_local_parm, /* --ifconfig parm 1 */
219 const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
220 in_addr_t local_public,
221 in_addr_t remote_public,
222 const bool strict_warn,
223 struct env_set *es);
225 void init_tun_post (struct tuntap *tt,
226 const struct frame *frame,
227 const struct tuntap_options *options);
229 void do_ifconfig (struct tuntap *tt,
230 const char *actual, /* actual device name */
231 int tun_mtu,
232 const struct env_set *es);
234 const char *dev_component_in_dev_node (const char *dev_node);
236 bool is_dev_type (const char *dev, const char *dev_type, const char *match_type);
237 int dev_type_enum (const char *dev, const char *dev_type);
238 const char *dev_type_string (const char *dev, const char *dev_type);
240 const char *ifconfig_options_string (const struct tuntap* tt, bool remote, bool disable, struct gc_arena *gc);
242 bool is_tun_p2p (const struct tuntap *tt);
244 void check_subnet_conflict (const in_addr_t ip,
245 const in_addr_t netmask,
246 const char *prefix);
248 void warn_on_use_of_common_subnets (void);
251 * Inline functions
254 static inline void
255 tun_adjust_frame_parameters (struct frame* frame, int size)
257 frame_add_to_extra_tun (frame, size);
261 * Should ifconfig be called before or after
262 * tun dev open?
265 #define IFCONFIG_BEFORE_TUN_OPEN 0
266 #define IFCONFIG_AFTER_TUN_OPEN 1
268 #define IFCONFIG_DEFAULT IFCONFIG_AFTER_TUN_OPEN
270 static inline int
271 ifconfig_order(void)
273 #if defined(TARGET_LINUX)
274 return IFCONFIG_AFTER_TUN_OPEN;
275 #elif defined(TARGET_SOLARIS)
276 return IFCONFIG_AFTER_TUN_OPEN;
277 #elif defined(TARGET_OPENBSD)
278 return IFCONFIG_BEFORE_TUN_OPEN;
279 #elif defined(TARGET_DARWIN)
280 return IFCONFIG_AFTER_TUN_OPEN;
281 #elif defined(TARGET_NETBSD)
282 return IFCONFIG_AFTER_TUN_OPEN;
283 #elif defined(WIN32)
284 return IFCONFIG_BEFORE_TUN_OPEN;
285 #else
286 return IFCONFIG_DEFAULT;
287 #endif
290 #ifdef WIN32
292 #define TUN_PASS_BUFFER
294 struct tap_reg
296 const char *guid;
297 struct tap_reg *next;
300 struct panel_reg
302 const char *name;
303 const char *guid;
304 struct panel_reg *next;
307 int ascii2ipset (const char* name);
308 const char *ipset2ascii (int index);
309 const char *ipset2ascii_all (struct gc_arena *gc);
311 void verify_255_255_255_252 (in_addr_t local, in_addr_t remote);
313 const IP_ADAPTER_INFO *get_adapter_info_list (struct gc_arena *gc);
314 const IP_ADAPTER_INFO *get_tun_adapter (const struct tuntap *tt, const IP_ADAPTER_INFO *list);
316 const IP_ADAPTER_INFO *get_adapter_info (DWORD index, struct gc_arena *gc);
317 const IP_PER_ADAPTER_INFO *get_per_adapter_info (const DWORD index, struct gc_arena *gc);
318 const IP_ADAPTER_INFO *get_adapter (const IP_ADAPTER_INFO *ai, DWORD index);
320 bool is_adapter_up (const struct tuntap *tt, const IP_ADAPTER_INFO *list);
321 bool is_ip_in_adapter_subnet (const IP_ADAPTER_INFO *ai, const in_addr_t ip, in_addr_t *highest_netmask);
323 DWORD adapter_index_of_ip (const IP_ADAPTER_INFO *list,
324 const in_addr_t ip,
325 int *count,
326 in_addr_t *netmask);
328 void show_tap_win32_adapters (int msglev, int warnlev);
329 void show_adapters (int msglev);
331 void tap_allow_nonadmin_access (const char *dev_node);
333 void show_valid_win32_tun_subnets (void);
334 const char *tap_win32_getinfo (const struct tuntap *tt, struct gc_arena *gc);
335 void tun_show_debug (struct tuntap *tt);
337 bool dhcp_release (const struct tuntap *tt);
338 bool dhcp_renew (const struct tuntap *tt);
340 void tun_standby_init (struct tuntap *tt);
341 bool tun_standby (struct tuntap *tt);
343 int tun_read_queue (struct tuntap *tt, int maxsize);
344 int tun_write_queue (struct tuntap *tt, struct buffer *buf);
345 int tun_finalize (HANDLE h, struct overlapped_io *io, struct buffer *buf);
347 static inline bool
348 tuntap_stop (int status)
351 * This corresponds to the STATUS_NO_SUCH_DEVICE
352 * error in tapdrvr.c.
354 if (status < 0)
356 return openvpn_errno () == ERROR_FILE_NOT_FOUND;
358 return false;
361 static inline int
362 tun_write_win32 (struct tuntap *tt, struct buffer *buf)
364 int err = 0;
365 int status = 0;
366 if (overlapped_io_active (&tt->writes))
368 status = tun_finalize (tt->hand, &tt->writes, NULL);
369 if (status < 0)
370 err = GetLastError ();
372 tun_write_queue (tt, buf);
373 if (status < 0)
375 SetLastError (err);
376 return status;
378 else
379 return BLEN (buf);
382 static inline int
383 read_tun_buffered (struct tuntap *tt, struct buffer *buf, int maxsize)
385 return tun_finalize (tt->hand, &tt->reads, buf);
388 static inline int
389 write_tun_buffered (struct tuntap *tt, struct buffer *buf)
391 return tun_write_win32 (tt, buf);
394 #else
396 static inline bool
397 tuntap_stop (int status)
399 return false;
402 static inline void
403 tun_standby_init (struct tuntap *tt)
407 static inline bool
408 tun_standby (struct tuntap *tt)
410 return true;
413 #endif
416 * TUN/TAP I/O wait functions
419 static inline event_t
420 tun_event_handle (const struct tuntap *tt)
422 #ifdef WIN32
423 return &tt->rw_handle;
424 #else
425 return tt->fd;
426 #endif
429 static inline unsigned int
430 tun_set (struct tuntap *tt,
431 struct event_set *es,
432 unsigned int rwflags,
433 void *arg,
434 unsigned int *persistent)
436 if (tuntap_defined (tt))
438 /* if persistent is defined, call event_ctl only if rwflags has changed since last call */
439 if (!persistent || *persistent != rwflags)
441 event_ctl (es, tun_event_handle (tt), rwflags, arg);
442 if (persistent)
443 *persistent = rwflags;
445 #ifdef WIN32
446 if (rwflags & EVENT_READ)
447 tun_read_queue (tt, 0);
448 #endif
449 tt->rwflags_debug = rwflags;
451 return rwflags;
454 const char *tun_stat (const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc);
456 #endif /* TUN_H */