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

« back to all changes in this revision

Viewing changes to src/openvpn/helper.c

  • 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
#ifdef HAVE_CONFIG_H
 
26
#include "config.h"
 
27
#elif defined(_MSC_VER)
 
28
#include "config-msvc.h"
 
29
#endif
 
30
 
 
31
#include "syshead.h"
 
32
 
 
33
#include "forward.h"
 
34
#include "helper.h"
 
35
#include "pool.h"
 
36
#include "push.h"
 
37
 
 
38
#include "memdbg.h"
 
39
 
 
40
#if P2MP_SERVER
 
41
 
 
42
static const char *
 
43
print_netmask (int netbits, struct gc_arena *gc)
 
44
{
 
45
  struct buffer out = alloc_buf_gc (128, gc);
 
46
  const in_addr_t netmask = netbits_to_netmask (netbits);
 
47
 
 
48
  buf_printf (&out, "%s (/%d)", print_in_addr_t (netmask, 0, gc), netbits);
 
49
 
 
50
  return BSTR (&out);
 
51
}
 
52
 
 
53
static const char *
 
54
print_opt_route_gateway (const in_addr_t route_gateway, struct gc_arena *gc)
 
55
{
 
56
  struct buffer out = alloc_buf_gc (128, gc);
 
57
  ASSERT (route_gateway);
 
58
  buf_printf (&out, "route-gateway %s", print_in_addr_t (route_gateway, 0, gc));
 
59
  return BSTR (&out);
 
60
}
 
61
 
 
62
static const char *
 
63
print_opt_route_gateway_dhcp (struct gc_arena *gc)
 
64
{
 
65
  struct buffer out = alloc_buf_gc (32, gc);
 
66
  buf_printf (&out, "route-gateway dhcp");
 
67
  return BSTR (&out);
 
68
}
 
69
 
 
70
static const char *
 
71
print_opt_route (const in_addr_t network, const in_addr_t netmask, struct gc_arena *gc)
 
72
{
 
73
  struct buffer out = alloc_buf_gc (128, gc);
 
74
  ASSERT (network);
 
75
 
 
76
  if (netmask)
 
77
    buf_printf (&out, "route %s %s",
 
78
                print_in_addr_t (network, 0, gc),
 
79
                print_in_addr_t (netmask, 0, gc));
 
80
  else
 
81
    buf_printf (&out, "route %s",
 
82
                print_in_addr_t (network, 0, gc));
 
83
 
 
84
  return BSTR (&out);
 
85
}
 
86
 
 
87
static const char *
 
88
print_opt_topology (const int topology, struct gc_arena *gc)
 
89
{
 
90
  struct buffer out = alloc_buf_gc (128, gc);
 
91
 
 
92
  buf_printf (&out, "topology %s", print_topology (topology));
 
93
 
 
94
  return BSTR (&out);
 
95
}
 
96
 
 
97
static const char *
 
98
print_str_int (const char *str, const int i, struct gc_arena *gc)
 
99
{
 
100
  struct buffer out = alloc_buf_gc (128, gc);
 
101
  buf_printf (&out, "%s %d", str, i);
 
102
  return BSTR (&out);
 
103
}
 
104
 
 
105
static const char *
 
106
print_str (const char *str, struct gc_arena *gc)
 
107
{
 
108
  struct buffer out = alloc_buf_gc (128, gc);
 
109
  buf_printf (&out, "%s", str);
 
110
  return BSTR (&out);
 
111
}
 
112
 
 
113
static void
 
114
helper_add_route (const in_addr_t network, const in_addr_t netmask, struct options *o)
 
115
{
 
116
  rol_check_alloc (o);
 
117
  add_route_to_option_list (o->routes,
 
118
                            print_in_addr_t (network, 0, &o->gc),
 
119
                            print_in_addr_t (netmask, 0, &o->gc),
 
120
                            NULL,
 
121
                            NULL);
 
122
}
 
123
 
 
124
static void
 
125
verify_common_subnet (const char *opt, const in_addr_t a, const in_addr_t b, const in_addr_t subnet)
 
126
{
 
127
  struct gc_arena gc = gc_new ();
 
128
  if ((a & subnet) != (b & subnet))
 
129
    msg (M_USAGE, "%s IP addresses %s and %s are not in the same %s subnet",
 
130
         opt,
 
131
         print_in_addr_t (a, 0, &gc),
 
132
         print_in_addr_t (b, 0, &gc),
 
133
         print_in_addr_t (subnet, 0, &gc));
 
134
  gc_free (&gc);
 
135
}
 
136
 
 
137
#endif
 
138
 
 
139
/*
 
140
 * Process server, server-bridge, and client helper
 
141
 * directives after the parameters themselves have been
 
142
 * parsed and placed in struct options.
 
143
 */
 
144
void
 
145
helper_client_server (struct options *o)
 
146
{
 
147
  struct gc_arena gc = gc_new ();
 
148
 
 
149
#if P2MP
 
150
#if P2MP_SERVER
 
151
 
 
152
/*
 
153
   * Get tun/tap/null device type
 
154
   */
 
155
  const int dev = dev_type_enum (o->dev, o->dev_type);
 
156
  const int topology = o->topology;
 
157
 
 
158
  /* 
 
159
   *
 
160
   * HELPER DIRECTIVE for IPv6
 
161
   *
 
162
   * server-ipv6 2001:db8::/64
 
163
   *
 
164
   * EXPANDS TO:
 
165
   *
 
166
   * tun-ipv6
 
167
   * push "tun-ipv6"
 
168
   * ifconfig-ipv6 2001:db8::1 2001:db8::2
 
169
   * if !nopool: 
 
170
   *   ifconfig-ipv6-pool 2001:db8::1:0/64
 
171
   * 
 
172
   */
 
173
   if ( o->server_ipv6_defined )
 
174
     {
 
175
        if ( ! o->server_defined )
 
176
          {
 
177
            msg (M_USAGE, "--server-ipv6 must be used together with --server");
 
178
          }
 
179
        if ( o->server_flags & SF_NOPOOL )
 
180
          {
 
181
            msg( M_USAGE, "--server-ipv6 is incompatible with 'nopool' option" );
 
182
          }
 
183
        if ( o->ifconfig_ipv6_pool_defined )
 
184
          {
 
185
            msg( M_USAGE, "--server-ipv6 already defines an ifconfig-ipv6-pool, so you can't also specify --ifconfig-pool explicitly");
 
186
          }
 
187
 
 
188
        /* local ifconfig is "base address + 1" and "+2" */
 
189
        o->ifconfig_ipv6_local = 
 
190
                print_in6_addr( add_in6_addr( o->server_network_ipv6, 1), 0, &o->gc );
 
191
        o->ifconfig_ipv6_remote = 
 
192
                print_in6_addr( add_in6_addr( o->server_network_ipv6, 2), 0, &o->gc );
 
193
        o->ifconfig_ipv6_netbits = o->server_netbits_ipv6;
 
194
 
 
195
        /* pool starts at "base address + 0x1000" - leave enough room */
 
196
        ASSERT( o->server_netbits_ipv6 <= 112 );        /* want 16 bits */
 
197
 
 
198
        o->ifconfig_ipv6_pool_defined = true;
 
199
        o->ifconfig_ipv6_pool_base = 
 
200
                add_in6_addr( o->server_network_ipv6, 0x1000 );
 
201
        o->ifconfig_ipv6_pool_netbits = o->server_netbits_ipv6;
 
202
 
 
203
        o->tun_ipv6 = true;
 
204
 
 
205
        push_option( o, "tun-ipv6", M_USAGE );
 
206
     }
 
207
 
 
208
  /*
 
209
   *
 
210
   * HELPER DIRECTIVE:
 
211
   *
 
212
   * server 10.8.0.0 255.255.255.0
 
213
   *
 
214
   * EXPANDS TO:
 
215
   *
 
216
   * mode server
 
217
   * tls-server
 
218
   * push "topology [topology]"
 
219
   *
 
220
   * if tun AND (topology == net30 OR topology == p2p):
 
221
   *   ifconfig 10.8.0.1 10.8.0.2
 
222
   *   if !nopool: 
 
223
   *     ifconfig-pool 10.8.0.4 10.8.0.251
 
224
   *   route 10.8.0.0 255.255.255.0
 
225
   *   if client-to-client:
 
226
   *     push "route 10.8.0.0 255.255.255.0"
 
227
   *   else if topology == net30:
 
228
   *     push "route 10.8.0.1"
 
229
   *
 
230
   * if tap OR (tun AND topology == subnet):
 
231
   *   ifconfig 10.8.0.1 255.255.255.0
 
232
   *   if !nopool: 
 
233
   *     ifconfig-pool 10.8.0.2 10.8.0.254 255.255.255.0
 
234
   *   push "route-gateway 10.8.0.1"
 
235
   */
 
236
 
 
237
  if (o->server_defined)
 
238
    {
 
239
      int netbits = -2;
 
240
      bool status = false;
 
241
 
 
242
      if (o->client)
 
243
        msg (M_USAGE, "--server and --client cannot be used together");
 
244
 
 
245
      if (o->server_bridge_defined || o->server_bridge_proxy_dhcp)
 
246
        msg (M_USAGE, "--server and --server-bridge cannot be used together");
 
247
 
 
248
      if (o->shared_secret_file)
 
249
        msg (M_USAGE, "--server and --secret cannot be used together (you must use SSL/TLS keys)");
 
250
 
 
251
      if (!(o->server_flags & SF_NOPOOL) && o->ifconfig_pool_defined)
 
252
        msg (M_USAGE, "--server already defines an ifconfig-pool, so you can't also specify --ifconfig-pool explicitly");
 
253
 
 
254
      if (!(dev == DEV_TYPE_TAP || dev == DEV_TYPE_TUN))
 
255
        msg (M_USAGE, "--server directive only makes sense with --dev tun or --dev tap");
 
256
 
 
257
      status = netmask_to_netbits (o->server_network, o->server_netmask, &netbits);
 
258
      if (!status)
 
259
        msg (M_USAGE, "--server directive network/netmask combination is invalid");
 
260
 
 
261
      if (netbits < 0)
 
262
        msg (M_USAGE, "--server directive netmask is invalid");
 
263
 
 
264
      if (netbits < IFCONFIG_POOL_MIN_NETBITS)
 
265
        msg (M_USAGE, "--server directive netmask allows for too many host addresses (subnet must be %s or higher)",
 
266
             print_netmask (IFCONFIG_POOL_MIN_NETBITS, &gc));
 
267
 
 
268
      if (dev == DEV_TYPE_TUN)
 
269
        {
 
270
          int pool_end_reserve = 4;
 
271
 
 
272
          if (netbits > 29)
 
273
            msg (M_USAGE, "--server directive when used with --dev tun must define a subnet of %s or lower",
 
274
                 print_netmask (29, &gc));
 
275
 
 
276
          if (netbits == 29)
 
277
            pool_end_reserve = 0;
 
278
 
 
279
          o->mode = MODE_SERVER;
 
280
          o->tls_server = true;
 
281
 
 
282
          if (topology == TOP_NET30 || topology == TOP_P2P)
 
283
            {
 
284
              o->ifconfig_local = print_in_addr_t (o->server_network + 1, 0, &o->gc);
 
285
              o->ifconfig_remote_netmask = print_in_addr_t (o->server_network + 2, 0, &o->gc);
 
286
 
 
287
              if (!(o->server_flags & SF_NOPOOL))
 
288
                {
 
289
                  o->ifconfig_pool_defined = true;
 
290
                  o->ifconfig_pool_start = o->server_network + 4;
 
291
                  o->ifconfig_pool_end = (o->server_network | ~o->server_netmask) - pool_end_reserve;
 
292
                  ifconfig_pool_verify_range (M_USAGE, o->ifconfig_pool_start, o->ifconfig_pool_end);
 
293
                }
 
294
 
 
295
              helper_add_route (o->server_network, o->server_netmask, o);
 
296
              if (o->enable_c2c)
 
297
                push_option (o, print_opt_route (o->server_network, o->server_netmask, &o->gc), M_USAGE);
 
298
              else if (topology == TOP_NET30)
 
299
                push_option (o, print_opt_route (o->server_network + 1, 0, &o->gc), M_USAGE);
 
300
            }
 
301
          else if (topology == TOP_SUBNET)
 
302
            {
 
303
              o->ifconfig_local = print_in_addr_t (o->server_network + 1, 0, &o->gc);
 
304
              o->ifconfig_remote_netmask = print_in_addr_t (o->server_netmask, 0, &o->gc);
 
305
 
 
306
              if (!(o->server_flags & SF_NOPOOL))
 
307
                {
 
308
                  o->ifconfig_pool_defined = true;
 
309
                  o->ifconfig_pool_start = o->server_network + 2;
 
310
                  o->ifconfig_pool_end = (o->server_network | ~o->server_netmask) - 2;
 
311
                  ifconfig_pool_verify_range (M_USAGE, o->ifconfig_pool_start, o->ifconfig_pool_end);
 
312
                }
 
313
              o->ifconfig_pool_netmask = o->server_netmask;
 
314
                  
 
315
              push_option (o, print_opt_route_gateway (o->server_network + 1, &o->gc), M_USAGE);
 
316
            }
 
317
          else
 
318
            ASSERT (0);
 
319
 
 
320
          push_option (o, print_opt_topology (topology, &o->gc), M_USAGE);
 
321
        }
 
322
      else if (dev == DEV_TYPE_TAP)
 
323
        {
 
324
          if (netbits > 30)
 
325
            msg (M_USAGE, "--server directive when used with --dev tap must define a subnet of %s or lower",
 
326
                 print_netmask (30, &gc));
 
327
 
 
328
          o->mode = MODE_SERVER;
 
329
          o->tls_server = true;
 
330
          o->ifconfig_local = print_in_addr_t (o->server_network + 1, 0, &o->gc);
 
331
          o->ifconfig_remote_netmask = print_in_addr_t (o->server_netmask, 0, &o->gc);
 
332
 
 
333
          if (!(o->server_flags & SF_NOPOOL))
 
334
            {
 
335
              o->ifconfig_pool_defined = true;
 
336
              o->ifconfig_pool_start = o->server_network + 2;
 
337
              o->ifconfig_pool_end = (o->server_network | ~o->server_netmask) - 1;
 
338
              ifconfig_pool_verify_range (M_USAGE, o->ifconfig_pool_start, o->ifconfig_pool_end);
 
339
            }
 
340
          o->ifconfig_pool_netmask = o->server_netmask;
 
341
 
 
342
          push_option (o, print_opt_route_gateway (o->server_network + 1, &o->gc), M_USAGE);
 
343
        }
 
344
      else
 
345
        {
 
346
          ASSERT (0);
 
347
        }
 
348
 
 
349
      /* set push-ifconfig-constraint directive */
 
350
      if ((dev == DEV_TYPE_TAP || topology == TOP_SUBNET))
 
351
        {
 
352
          o->push_ifconfig_constraint_defined = true;
 
353
          o->push_ifconfig_constraint_network = o->server_network;
 
354
          o->push_ifconfig_constraint_netmask = o->server_netmask;
 
355
        }
 
356
    }
 
357
 
 
358
  /*
 
359
   * HELPER DIRECTIVE:
 
360
   *
 
361
   * server-bridge 10.8.0.4 255.255.255.0 10.8.0.128 10.8.0.254
 
362
   *
 
363
   * EXPANDS TO:
 
364
   *
 
365
   * mode server
 
366
   * tls-server
 
367
   *
 
368
   * ifconfig-pool 10.8.0.128 10.8.0.254 255.255.255.0
 
369
   * push "route-gateway 10.8.0.4"
 
370
   *
 
371
   * OR
 
372
   *
 
373
   * server-bridge
 
374
   *
 
375
   * EXPANDS TO:
 
376
   *
 
377
   * mode server
 
378
   * tls-server
 
379
   *
 
380
   * if !nogw:
 
381
   *   push "route-gateway dhcp"
 
382
   */
 
383
  else if (o->server_bridge_defined | o->server_bridge_proxy_dhcp)
 
384
    {
 
385
      if (o->client)
 
386
        msg (M_USAGE, "--server-bridge and --client cannot be used together");
 
387
 
 
388
      if (!(o->server_flags & SF_NOPOOL) && o->ifconfig_pool_defined)
 
389
        msg (M_USAGE, "--server-bridge already defines an ifconfig-pool, so you can't also specify --ifconfig-pool explicitly");
 
390
 
 
391
      if (o->shared_secret_file)
 
392
        msg (M_USAGE, "--server-bridge and --secret cannot be used together (you must use SSL/TLS keys)");
 
393
 
 
394
      if (dev != DEV_TYPE_TAP)
 
395
        msg (M_USAGE, "--server-bridge directive only makes sense with --dev tap");
 
396
 
 
397
      if (o->server_bridge_defined)
 
398
        {
 
399
          verify_common_subnet ("--server-bridge", o->server_bridge_ip, o->server_bridge_pool_start, o->server_bridge_netmask); 
 
400
          verify_common_subnet ("--server-bridge", o->server_bridge_pool_start, o->server_bridge_pool_end, o->server_bridge_netmask); 
 
401
          verify_common_subnet ("--server-bridge", o->server_bridge_ip, o->server_bridge_pool_end, o->server_bridge_netmask); 
 
402
        }
 
403
 
 
404
      o->mode = MODE_SERVER;
 
405
      o->tls_server = true;
 
406
 
 
407
      if (o->server_bridge_defined)
 
408
        {
 
409
          o->ifconfig_pool_defined = true;
 
410
          o->ifconfig_pool_start = o->server_bridge_pool_start;
 
411
          o->ifconfig_pool_end = o->server_bridge_pool_end;
 
412
          ifconfig_pool_verify_range (M_USAGE, o->ifconfig_pool_start, o->ifconfig_pool_end);
 
413
          o->ifconfig_pool_netmask = o->server_bridge_netmask;
 
414
          push_option (o, print_opt_route_gateway (o->server_bridge_ip, &o->gc), M_USAGE);
 
415
        }
 
416
      else if (o->server_bridge_proxy_dhcp && !(o->server_flags & SF_NO_PUSH_ROUTE_GATEWAY))
 
417
        {
 
418
          push_option (o, print_opt_route_gateway_dhcp (&o->gc), M_USAGE);
 
419
        }
 
420
    }
 
421
  else
 
422
#endif /* P2MP_SERVER */
 
423
 
 
424
  /*
 
425
   * HELPER DIRECTIVE:
 
426
   *
 
427
   * client
 
428
   *
 
429
   * EXPANDS TO:
 
430
   *
 
431
   * pull
 
432
   * tls-client
 
433
   */
 
434
  if (o->client)
 
435
    {
 
436
      if (o->key_method != 2)
 
437
        msg (M_USAGE, "--client requires --key-method 2");
 
438
 
 
439
      o->pull = true;
 
440
      o->tls_client = true;
 
441
    }
 
442
 
 
443
#endif /* P2MP */
 
444
 
 
445
  gc_free (&gc);
 
446
}
 
447
 
 
448
/*
 
449
 *
 
450
 * HELPER DIRECTIVE:
 
451
 *
 
452
 * keepalive 10 60
 
453
 *
 
454
 * EXPANDS TO:
 
455
 *
 
456
 * if mode server:
 
457
 *   ping 10
 
458
 *   ping-restart 120
 
459
 *   push "ping 10"
 
460
 *   push "ping-restart 60"
 
461
 * else
 
462
 *   ping 10
 
463
 *   ping-restart 60
 
464
 */
 
465
void
 
466
helper_keepalive (struct options *o)
 
467
{
 
468
  if (o->keepalive_ping || o->keepalive_timeout)
 
469
    {
 
470
      /*
 
471
       * Sanity checks.
 
472
       */
 
473
      if (o->keepalive_ping <= 0 || o->keepalive_timeout <= 0)
 
474
        msg (M_USAGE, "--keepalive parameters must be > 0");
 
475
      if (o->keepalive_ping * 2 > o->keepalive_timeout)
 
476
        msg (M_USAGE, "the second parameter to --keepalive (restart timeout=%d) must be at least twice the value of the first parameter (ping interval=%d).  A ratio of 1:5 or 1:6 would be even better.  Recommended setting is --keepalive 10 60.",
 
477
             o->keepalive_timeout,
 
478
             o->keepalive_ping);
 
479
      if (o->ping_send_timeout || o->ping_rec_timeout)
 
480
        msg (M_USAGE, "--keepalive conflicts with --ping, --ping-exit, or --ping-restart.  If you use --keepalive, you don't need any of the other --ping directives.");
 
481
 
 
482
      /*
 
483
       * Expand.
 
484
       */
 
485
      if (o->mode == MODE_POINT_TO_POINT)
 
486
        {
 
487
          o->ping_rec_timeout_action = PING_RESTART;
 
488
          o->ping_send_timeout = o->keepalive_ping;
 
489
          o->ping_rec_timeout = o->keepalive_timeout;
 
490
        }
 
491
#if P2MP_SERVER
 
492
      else if (o->mode == MODE_SERVER)
 
493
        {
 
494
          o->ping_rec_timeout_action = PING_RESTART;
 
495
          o->ping_send_timeout = o->keepalive_ping;
 
496
          o->ping_rec_timeout = o->keepalive_timeout * 2;
 
497
          push_option (o, print_str_int ("ping", o->keepalive_ping, &o->gc), M_USAGE);
 
498
          push_option (o, print_str_int ("ping-restart", o->keepalive_timeout, &o->gc), M_USAGE);
 
499
        }
 
500
#endif
 
501
      else
 
502
        {
 
503
          ASSERT (0);
 
504
        }
 
505
    }
 
506
}
 
507
 
 
508
/*
 
509
 *
 
510
 * HELPER DIRECTIVE:
 
511
 *
 
512
 * tcp-nodelay
 
513
 *
 
514
 * EXPANDS TO:
 
515
 *
 
516
 * if mode server:
 
517
 *   socket-flags TCP_NODELAY
 
518
 *   push "socket-flags TCP_NODELAY"
 
519
 */
 
520
void
 
521
helper_tcp_nodelay (struct options *o)
 
522
{
 
523
#if P2MP_SERVER
 
524
  if (o->server_flags & SF_TCP_NODELAY_HELPER)
 
525
    {
 
526
      if (o->mode == MODE_SERVER)
 
527
        {
 
528
          o->sockflags |= SF_TCP_NODELAY;         
 
529
          push_option (o, print_str ("socket-flags TCP_NODELAY", &o->gc), M_USAGE);
 
530
        }
 
531
      else
 
532
        {
 
533
          ASSERT (0);
 
534
        }
 
535
    }
 
536
#endif
 
537
}