cosmetics
[tomato.git] / release / src / router / openvpn / tun.h
blob210278a28eca508518ab76492c9c06f557677190
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-2009 OpenVPN Technologies, 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 bool is_dev_type (const char *dev, const char *dev_type, const char *match_type);
235 int dev_type_enum (const char *dev, const char *dev_type);
236 const char *dev_type_string (const char *dev, const char *dev_type);
238 const char *ifconfig_options_string (const struct tuntap* tt, bool remote, bool disable, struct gc_arena *gc);
240 bool is_tun_p2p (const struct tuntap *tt);
242 void check_subnet_conflict (const in_addr_t ip,
243 const in_addr_t netmask,
244 const char *prefix);
246 void warn_on_use_of_common_subnets (void);
249 * Inline functions
252 static inline void
253 tun_adjust_frame_parameters (struct frame* frame, int size)
255 frame_add_to_extra_tun (frame, size);
259 * Should ifconfig be called before or after
260 * tun dev open?
263 #define IFCONFIG_BEFORE_TUN_OPEN 0
264 #define IFCONFIG_AFTER_TUN_OPEN 1
266 #define IFCONFIG_DEFAULT IFCONFIG_AFTER_TUN_OPEN
268 static inline int
269 ifconfig_order(void)
271 #if defined(TARGET_LINUX)
272 return IFCONFIG_AFTER_TUN_OPEN;
273 #elif defined(TARGET_SOLARIS)
274 return IFCONFIG_AFTER_TUN_OPEN;
275 #elif defined(TARGET_OPENBSD)
276 return IFCONFIG_BEFORE_TUN_OPEN;
277 #elif defined(TARGET_DARWIN)
278 return IFCONFIG_AFTER_TUN_OPEN;
279 #elif defined(TARGET_NETBSD)
280 return IFCONFIG_AFTER_TUN_OPEN;
281 #elif defined(WIN32)
282 return IFCONFIG_BEFORE_TUN_OPEN;
283 #else
284 return IFCONFIG_DEFAULT;
285 #endif
288 #ifdef WIN32
290 #define TUN_PASS_BUFFER
292 struct tap_reg
294 const char *guid;
295 struct tap_reg *next;
298 struct panel_reg
300 const char *name;
301 const char *guid;
302 struct panel_reg *next;
305 int ascii2ipset (const char* name);
306 const char *ipset2ascii (int index);
307 const char *ipset2ascii_all (struct gc_arena *gc);
309 void verify_255_255_255_252 (in_addr_t local, in_addr_t remote);
311 const IP_ADAPTER_INFO *get_adapter_info_list (struct gc_arena *gc);
312 const IP_ADAPTER_INFO *get_tun_adapter (const struct tuntap *tt, const IP_ADAPTER_INFO *list);
314 const IP_ADAPTER_INFO *get_adapter_info (DWORD index, struct gc_arena *gc);
315 const IP_PER_ADAPTER_INFO *get_per_adapter_info (const DWORD index, struct gc_arena *gc);
316 const IP_ADAPTER_INFO *get_adapter (const IP_ADAPTER_INFO *ai, DWORD index);
318 bool is_adapter_up (const struct tuntap *tt, const IP_ADAPTER_INFO *list);
319 bool is_ip_in_adapter_subnet (const IP_ADAPTER_INFO *ai, const in_addr_t ip, in_addr_t *highest_netmask);
321 DWORD adapter_index_of_ip (const IP_ADAPTER_INFO *list,
322 const in_addr_t ip,
323 int *count,
324 in_addr_t *netmask);
326 void show_tap_win32_adapters (int msglev, int warnlev);
327 void show_adapters (int msglev);
329 void tap_allow_nonadmin_access (const char *dev_node);
331 void show_valid_win32_tun_subnets (void);
332 const char *tap_win32_getinfo (const struct tuntap *tt, struct gc_arena *gc);
333 void tun_show_debug (struct tuntap *tt);
335 bool dhcp_release_by_adapter_index(const DWORD adapter_index);
336 bool dhcp_renew_by_adapter_index (const DWORD adapter_index);
338 void tun_standby_init (struct tuntap *tt);
339 bool tun_standby (struct tuntap *tt);
341 int tun_read_queue (struct tuntap *tt, int maxsize);
342 int tun_write_queue (struct tuntap *tt, struct buffer *buf);
343 int tun_finalize (HANDLE h, struct overlapped_io *io, struct buffer *buf);
345 static inline bool
346 tuntap_stop (int status)
349 * This corresponds to the STATUS_NO_SUCH_DEVICE
350 * error in tapdrvr.c.
352 if (status < 0)
354 return openvpn_errno () == ERROR_FILE_NOT_FOUND;
356 return false;
359 static inline int
360 tun_write_win32 (struct tuntap *tt, struct buffer *buf)
362 int err = 0;
363 int status = 0;
364 if (overlapped_io_active (&tt->writes))
366 status = tun_finalize (tt->hand, &tt->writes, NULL);
367 if (status < 0)
368 err = GetLastError ();
370 tun_write_queue (tt, buf);
371 if (status < 0)
373 SetLastError (err);
374 return status;
376 else
377 return BLEN (buf);
380 static inline int
381 read_tun_buffered (struct tuntap *tt, struct buffer *buf, int maxsize)
383 return tun_finalize (tt->hand, &tt->reads, buf);
386 static inline int
387 write_tun_buffered (struct tuntap *tt, struct buffer *buf)
389 return tun_write_win32 (tt, buf);
392 #else
394 static inline bool
395 tuntap_stop (int status)
397 return false;
400 static inline void
401 tun_standby_init (struct tuntap *tt)
405 static inline bool
406 tun_standby (struct tuntap *tt)
408 return true;
411 #endif
414 * TUN/TAP I/O wait functions
417 static inline event_t
418 tun_event_handle (const struct tuntap *tt)
420 #ifdef WIN32
421 return &tt->rw_handle;
422 #else
423 return tt->fd;
424 #endif
427 static inline unsigned int
428 tun_set (struct tuntap *tt,
429 struct event_set *es,
430 unsigned int rwflags,
431 void *arg,
432 unsigned int *persistent)
434 if (tuntap_defined (tt))
436 /* if persistent is defined, call event_ctl only if rwflags has changed since last call */
437 if (!persistent || *persistent != rwflags)
439 event_ctl (es, tun_event_handle (tt), rwflags, arg);
440 if (persistent)
441 *persistent = rwflags;
443 #ifdef WIN32
444 if (rwflags & EVENT_READ)
445 tun_read_queue (tt, 0);
446 #endif
447 tt->rwflags_debug = rwflags;
449 return rwflags;
452 const char *tun_stat (const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc);
454 #endif /* TUN_H */