~ubuntu-branches/ubuntu/saucy/openvpn/saucy-proposed

« back to all changes in this revision

Viewing changes to src/openvpn/tun.h

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2013-05-24 17:42:45 UTC
  • mfrom: (1.1.19) (10.2.22 sid)
  • Revision ID: package-import@ubuntu.com-20130524174245-g9y6wlforycufqy5
Tags: 2.3.1-2ubuntu1
* Merge from Debian unstable. Remaining changes:
  - debian/openvpn.init.d:
    + Do not use start-stop-daemon and </dev/null to avoid blocking boot.
    + Show per-VPN result messages.
    + Add "--script-security 2" by default for backwards compatabliity.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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.
 
7
 *
 
8
 *  Copyright (C) 2002-2010 OpenVPN Technologies, Inc. <sales@openvpn.net>
 
9
 *
 
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.
 
13
 *
 
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.
 
18
 *
 
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
 
23
 */
 
24
 
 
25
#ifndef TUN_H
 
26
#define TUN_H
 
27
 
 
28
#ifdef WIN32
 
29
#include <winioctl.h>
 
30
#include <tap-windows.h>
 
31
#endif
 
32
 
 
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"
 
40
 
 
41
#ifdef WIN32
 
42
 
 
43
#define TUN_ADAPTER_INDEX_INVALID ((DWORD)-1)
 
44
 
 
45
/* time constants for --ip-win32 adaptive */
 
46
#define IPW32_SET_ADAPTIVE_DELAY_WINDOW 300
 
47
#define IPW32_SET_ADAPTIVE_TRY_NETSH    20
 
48
 
 
49
struct tuntap_options {
 
50
  /* --ip-win32 options */
 
51
  bool ip_win32_defined;
 
52
 
 
53
# define IPW32_SET_MANUAL       0  /* "--ip-win32 manual" */
 
54
# define IPW32_SET_NETSH        1  /* "--ip-win32 netsh" */
 
55
# define IPW32_SET_IPAPI        2  /* "--ip-win32 ipapi" */
 
56
# define IPW32_SET_DHCP_MASQ    3  /* "--ip-win32 dynamic" */
 
57
# define IPW32_SET_ADAPTIVE     4  /* "--ip-win32 adaptive" */
 
58
# define IPW32_SET_N            5
 
59
  int ip_win32_type;
 
60
 
 
61
  /* --ip-win32 dynamic options */
 
62
  bool dhcp_masq_custom_offset;
 
63
  int dhcp_masq_offset;
 
64
  int dhcp_lease_time;
 
65
 
 
66
  /* --tap-sleep option */
 
67
  int tap_sleep;
 
68
 
 
69
  /* --dhcp-option options */
 
70
 
 
71
  bool dhcp_options;
 
72
 
 
73
  const char *domain;        /* DOMAIN (15) */
 
74
 
 
75
  const char *netbios_scope; /* NBS (47) */
 
76
 
 
77
  int netbios_node_type;     /* NBT 1,2,4,8 (46) */
 
78
 
 
79
#define N_DHCP_ADDR 4        /* Max # of addresses allowed for
 
80
                                DNS, WINS, etc. */
 
81
 
 
82
  /* DNS (6) */
 
83
  in_addr_t dns[N_DHCP_ADDR];
 
84
  int dns_len;
 
85
 
 
86
  /* WINS (44) */
 
87
  in_addr_t wins[N_DHCP_ADDR];
 
88
  int wins_len;
 
89
 
 
90
  /* NTP (42) */
 
91
  in_addr_t ntp[N_DHCP_ADDR];
 
92
  int ntp_len;
 
93
 
 
94
  /* NBDD (45) */
 
95
  in_addr_t nbdd[N_DHCP_ADDR];
 
96
  int nbdd_len;
 
97
 
 
98
  /* DISABLE_NBT (43, Vendor option 001) */
 
99
  bool disable_nbt;
 
100
 
 
101
  bool dhcp_renew;
 
102
  bool dhcp_pre_release;
 
103
  bool dhcp_release;
 
104
 
 
105
  bool register_dns;
 
106
};
 
107
 
 
108
#elif TARGET_LINUX
 
109
 
 
110
struct tuntap_options {
 
111
  int txqueuelen;
 
112
};
 
113
 
 
114
#else
 
115
 
 
116
struct tuntap_options {
 
117
  int dummy; /* not used */
 
118
};
 
119
 
 
120
#endif
 
121
 
 
122
/*
 
123
 * Define a TUN/TAP dev.
 
124
 */
 
125
 
 
126
struct tuntap
 
127
{
 
128
# define TUNNEL_TYPE(tt) ((tt) ? ((tt)->type) : DEV_TYPE_UNDEF)
 
129
  int type; /* DEV_TYPE_x as defined in proto.h */
 
130
 
 
131
# define TUNNEL_TOPOLOGY(tt) ((tt) ? ((tt)->topology) : TOP_UNDEF)
 
132
  int topology; /* one of the TOP_x values */
 
133
 
 
134
  bool did_ifconfig_setup;
 
135
  bool did_ifconfig_ipv6_setup;
 
136
  bool did_ifconfig;
 
137
 
 
138
  bool ipv6;
 
139
 
 
140
  bool persistent_if;           /* if existed before, keep on program end */
 
141
 
 
142
  struct tuntap_options options; /* options set on command line */
 
143
 
 
144
  char *actual_name; /* actual name of TUN/TAP dev, usually including unit number */
 
145
 
 
146
  /* number of TX buffers */
 
147
  int txqueuelen;
 
148
 
 
149
  /* ifconfig parameters */
 
150
  in_addr_t local;
 
151
  in_addr_t remote_netmask;
 
152
  in_addr_t broadcast;
 
153
 
 
154
  struct in6_addr local_ipv6;
 
155
  struct in6_addr remote_ipv6;
 
156
  int netbits_ipv6;
 
157
 
 
158
#ifdef WIN32
 
159
  HANDLE hand;
 
160
  struct overlapped_io reads;
 
161
  struct overlapped_io writes;
 
162
  struct rw_handle rw_handle;
 
163
 
 
164
  /* used for setting interface address via IP Helper API
 
165
     or DHCP masquerade */
 
166
  bool ipapi_context_defined;
 
167
  ULONG ipapi_context;
 
168
  ULONG ipapi_instance;
 
169
  in_addr_t adapter_netmask;
 
170
 
 
171
  /* Windows adapter index for TAP-Windows adapter,
 
172
     ~0 if undefined */
 
173
  DWORD adapter_index;
 
174
 
 
175
  int standby_iter;
 
176
#else
 
177
  int fd;   /* file descriptor for TUN/TAP dev */
 
178
#endif
 
179
 
 
180
#ifdef TARGET_SOLARIS
 
181
  int ip_fd;
 
182
#endif
 
183
 
 
184
  /* used for printing status info only */
 
185
  unsigned int rwflags_debug;
 
186
 
 
187
  /* Some TUN/TAP drivers like to be ioctled for mtu
 
188
     after open */
 
189
  int post_open_mtu;
 
190
};
 
191
 
 
192
static inline bool
 
193
tuntap_defined (const struct tuntap *tt)
 
194
{
 
195
#ifdef WIN32
 
196
  return tt && tt->hand != NULL;
 
197
#else
 
198
  return tt && tt->fd >= 0;
 
199
#endif
 
200
}
 
201
 
 
202
/*
 
203
 * Function prototypes
 
204
 */
 
205
 
 
206
void open_tun (const char *dev, const char *dev_type, const char *dev_node,
 
207
               struct tuntap *tt);
 
208
 
 
209
void close_tun (struct tuntap *tt);
 
210
 
 
211
int write_tun (struct tuntap* tt, uint8_t *buf, int len);
 
212
 
 
213
int read_tun (struct tuntap* tt, uint8_t *buf, int len);
 
214
 
 
215
void tuncfg (const char *dev, const char *dev_type, const char *dev_node,
 
216
             int persist_mode, const char *username,
 
217
             const char *groupname, const struct tuntap_options *options);
 
218
 
 
219
const char *guess_tuntap_dev (const char *dev,
 
220
                              const char *dev_type,
 
221
                              const char *dev_node,
 
222
                              struct gc_arena *gc);
 
223
 
 
224
struct tuntap *init_tun (const char *dev,       /* --dev option */
 
225
                         const char *dev_type,  /* --dev-type option */
 
226
                         int topology,          /* one of the TOP_x values */
 
227
                         const char *ifconfig_local_parm,          /* --ifconfig parm 1 */
 
228
                         const char *ifconfig_remote_netmask_parm, /* --ifconfig parm 2 */
 
229
                         const char *ifconfig_ipv6_local_parm,     /* --ifconfig parm 1 / IPv6 */
 
230
                         int ifconfig_ipv6_netbits_parm,           /* --ifconfig parm 1 / bits */
 
231
                         const char *ifconfig_ipv6_remote_parm,    /* --ifconfig parm 2 / IPv6 */
 
232
                         in_addr_t local_public,
 
233
                         in_addr_t remote_public,
 
234
                         const bool strict_warn,
 
235
                         struct env_set *es);
 
236
 
 
237
void init_tun_post (struct tuntap *tt,
 
238
                    const struct frame *frame,
 
239
                    const struct tuntap_options *options);
 
240
 
 
241
void do_ifconfig (struct tuntap *tt,
 
242
                  const char *actual,    /* actual device name */
 
243
                  int tun_mtu,
 
244
                  const struct env_set *es);
 
245
 
 
246
bool is_dev_type (const char *dev, const char *dev_type, const char *match_type);
 
247
int dev_type_enum (const char *dev, const char *dev_type);
 
248
const char *dev_type_string (const char *dev, const char *dev_type);
 
249
 
 
250
const char *ifconfig_options_string (const struct tuntap* tt, bool remote, bool disable, struct gc_arena *gc);
 
251
 
 
252
bool is_tun_p2p (const struct tuntap *tt);
 
253
 
 
254
void check_subnet_conflict (const in_addr_t ip,
 
255
                            const in_addr_t netmask,
 
256
                            const char *prefix);
 
257
 
 
258
void warn_on_use_of_common_subnets (void);
 
259
 
 
260
/*
 
261
 * Inline functions
 
262
 */
 
263
 
 
264
static inline void
 
265
tun_adjust_frame_parameters (struct frame* frame, int size)
 
266
{
 
267
  frame_add_to_extra_tun (frame, size);
 
268
}
 
269
 
 
270
/*
 
271
 * Should ifconfig be called before or after
 
272
 * tun dev open?
 
273
 */
 
274
 
 
275
#define IFCONFIG_BEFORE_TUN_OPEN 0
 
276
#define IFCONFIG_AFTER_TUN_OPEN  1
 
277
 
 
278
#define IFCONFIG_DEFAULT         IFCONFIG_AFTER_TUN_OPEN
 
279
 
 
280
static inline int
 
281
ifconfig_order(void)
 
282
{
 
283
#if defined(TARGET_LINUX)
 
284
  return IFCONFIG_AFTER_TUN_OPEN;
 
285
#elif defined(TARGET_SOLARIS)
 
286
  return IFCONFIG_AFTER_TUN_OPEN;
 
287
#elif defined(TARGET_OPENBSD)
 
288
  return IFCONFIG_AFTER_TUN_OPEN;
 
289
#elif defined(TARGET_DARWIN)
 
290
  return IFCONFIG_AFTER_TUN_OPEN;
 
291
#elif defined(TARGET_NETBSD)
 
292
  return IFCONFIG_AFTER_TUN_OPEN;
 
293
#elif defined(WIN32)
 
294
  return IFCONFIG_BEFORE_TUN_OPEN;
 
295
#else
 
296
  return IFCONFIG_DEFAULT;
 
297
#endif
 
298
}
 
299
 
 
300
#ifdef WIN32
 
301
 
 
302
#define TUN_PASS_BUFFER
 
303
 
 
304
struct tap_reg
 
305
{
 
306
  const char *guid;
 
307
  struct tap_reg *next;
 
308
};
 
309
 
 
310
struct panel_reg
 
311
{
 
312
  const char *name;
 
313
  const char *guid;
 
314
  struct panel_reg *next;
 
315
};
 
316
 
 
317
int ascii2ipset (const char* name);
 
318
const char *ipset2ascii (int index);
 
319
const char *ipset2ascii_all (struct gc_arena *gc);
 
320
 
 
321
void verify_255_255_255_252 (in_addr_t local, in_addr_t remote);
 
322
 
 
323
const IP_ADAPTER_INFO *get_adapter_info_list (struct gc_arena *gc);
 
324
const IP_ADAPTER_INFO *get_tun_adapter (const struct tuntap *tt, const IP_ADAPTER_INFO *list);
 
325
 
 
326
const IP_ADAPTER_INFO *get_adapter_info (DWORD index, struct gc_arena *gc);
 
327
const IP_PER_ADAPTER_INFO *get_per_adapter_info (const DWORD index, struct gc_arena *gc);
 
328
const IP_ADAPTER_INFO *get_adapter (const IP_ADAPTER_INFO *ai, DWORD index);
 
329
 
 
330
bool is_adapter_up (const struct tuntap *tt, const IP_ADAPTER_INFO *list);
 
331
bool is_ip_in_adapter_subnet (const IP_ADAPTER_INFO *ai, const in_addr_t ip, in_addr_t *highest_netmask);
 
332
 
 
333
DWORD adapter_index_of_ip (const IP_ADAPTER_INFO *list,
 
334
                           const in_addr_t ip,
 
335
                           int *count,
 
336
                           in_addr_t *netmask);
 
337
 
 
338
void show_tap_win_adapters (int msglev, int warnlev);
 
339
void show_adapters (int msglev);
 
340
 
 
341
void tap_allow_nonadmin_access (const char *dev_node);
 
342
 
 
343
void show_valid_win32_tun_subnets (void);
 
344
const char *tap_win_getinfo (const struct tuntap *tt, struct gc_arena *gc);
 
345
void tun_show_debug (struct tuntap *tt);
 
346
 
 
347
bool dhcp_release_by_adapter_index(const DWORD adapter_index);
 
348
bool dhcp_renew_by_adapter_index (const DWORD adapter_index);
 
349
 
 
350
void fork_register_dns_action (struct tuntap *tt);
 
351
void ipconfig_register_dns (const struct env_set *es);
 
352
 
 
353
void tun_standby_init (struct tuntap *tt);
 
354
bool tun_standby (struct tuntap *tt);
 
355
 
 
356
int tun_read_queue (struct tuntap *tt, int maxsize);
 
357
int tun_write_queue (struct tuntap *tt, struct buffer *buf);
 
358
int tun_finalize (HANDLE h, struct overlapped_io *io, struct buffer *buf);
 
359
 
 
360
static inline bool
 
361
tuntap_stop (int status)
 
362
{
 
363
  /*
 
364
   * This corresponds to the STATUS_NO_SUCH_DEVICE
 
365
   * error in tapdrvr.c.
 
366
   */
 
367
  if (status < 0)
 
368
    {
 
369
      return openvpn_errno () == ERROR_FILE_NOT_FOUND;
 
370
    }
 
371
  return false;
 
372
}
 
373
 
 
374
static inline int
 
375
tun_write_win32 (struct tuntap *tt, struct buffer *buf)
 
376
{
 
377
  int err = 0;
 
378
  int status = 0;
 
379
  if (overlapped_io_active (&tt->writes))
 
380
    {
 
381
      status = tun_finalize (tt->hand, &tt->writes, NULL);
 
382
      if (status < 0)
 
383
        err = GetLastError ();
 
384
    }
 
385
  tun_write_queue (tt, buf);
 
386
  if (status < 0)
 
387
    {
 
388
      SetLastError (err);
 
389
      return status;
 
390
    }
 
391
  else
 
392
    return BLEN (buf);
 
393
}
 
394
 
 
395
static inline int
 
396
read_tun_buffered (struct tuntap *tt, struct buffer *buf, int maxsize)
 
397
{
 
398
  return tun_finalize (tt->hand, &tt->reads, buf);
 
399
}
 
400
 
 
401
static inline int
 
402
write_tun_buffered (struct tuntap *tt, struct buffer *buf)
 
403
{
 
404
  return tun_write_win32 (tt, buf);
 
405
}
 
406
 
 
407
#else
 
408
 
 
409
static inline bool
 
410
tuntap_stop (int status)
 
411
{
 
412
  return false;
 
413
}
 
414
 
 
415
static inline void
 
416
tun_standby_init (struct tuntap *tt)
 
417
{
 
418
}
 
419
 
 
420
static inline bool
 
421
tun_standby (struct tuntap *tt)
 
422
{
 
423
  return true;
 
424
}
 
425
 
 
426
#endif
 
427
 
 
428
/*
 
429
 * TUN/TAP I/O wait functions
 
430
 */
 
431
 
 
432
static inline event_t
 
433
tun_event_handle (const struct tuntap *tt)
 
434
{
 
435
#ifdef WIN32
 
436
  return &tt->rw_handle;
 
437
#else
 
438
  return tt->fd;
 
439
#endif
 
440
}
 
441
 
 
442
static inline unsigned int
 
443
tun_set (struct tuntap *tt,
 
444
         struct event_set *es,
 
445
         unsigned int rwflags,
 
446
         void *arg,
 
447
         unsigned int *persistent)
 
448
{
 
449
  if (tuntap_defined (tt))
 
450
    {
 
451
      /* if persistent is defined, call event_ctl only if rwflags has changed since last call */
 
452
      if (!persistent || *persistent != rwflags)
 
453
        {
 
454
          event_ctl (es, tun_event_handle (tt), rwflags, arg);
 
455
          if (persistent)
 
456
            *persistent = rwflags;
 
457
        }
 
458
#ifdef WIN32
 
459
      if (rwflags & EVENT_READ)
 
460
        tun_read_queue (tt, 0);
 
461
#endif
 
462
      tt->rwflags_debug = rwflags;
 
463
    }
 
464
  return rwflags;
 
465
}
 
466
 
 
467
const char *tun_stat (const struct tuntap *tt, unsigned int rwflags, struct gc_arena *gc);
 
468
 
 
469
#endif /* TUN_H */