OpenVPN
helper.c
Go to the documentation of this file.
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-2026 OpenVPN 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 along
20 * with this program; if not, see <https://www.gnu.org/licenses/>.
21 */
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#include "syshead.h"
28
29#include "forward.h"
30#include "helper.h"
31#include "pool.h"
32#include "push.h"
33
34#include "memdbg.h"
35
36
37static const char *
38print_netmask(int netbits, struct gc_arena *gc)
39{
40 struct buffer out = alloc_buf_gc(128, gc);
41 const in_addr_t netmask = netbits_to_netmask(netbits);
42
43 buf_printf(&out, "%s (/%d)", print_in_addr_t(netmask, 0, gc), netbits);
44
45 return BSTR(&out);
46}
47
48static const char *
50{
51 struct buffer out = alloc_buf_gc(128, gc);
53 buf_printf(&out, "route-gateway %s", print_in_addr_t(route_gateway, 0, gc));
54 return BSTR(&out);
55}
56
57static const char *
59{
60 struct buffer out = alloc_buf_gc(32, gc);
61 buf_printf(&out, "route-gateway dhcp");
62 return BSTR(&out);
63}
64
65static const char *
66print_opt_route(const in_addr_t network, const in_addr_t netmask, struct gc_arena *gc)
67{
68 struct buffer out = alloc_buf_gc(128, gc);
69 ASSERT(network);
70
71 if (netmask)
72 {
73 buf_printf(&out, "route %s %s", print_in_addr_t(network, 0, gc),
74 print_in_addr_t(netmask, 0, gc));
75 }
76 else
77 {
78 buf_printf(&out, "route %s", print_in_addr_t(network, 0, gc));
79 }
80
81 return BSTR(&out);
82}
83
84static const char *
85print_opt_topology(const int topology, struct gc_arena *gc)
86{
87 struct buffer out = alloc_buf_gc(128, gc);
88
89 buf_printf(&out, "topology %s", print_topology(topology));
90
91 return BSTR(&out);
92}
93
94static const char *
95print_str_int(const char *str, const int i, struct gc_arena *gc)
96{
97 struct buffer out = alloc_buf_gc(128, gc);
98 buf_printf(&out, "%s %d", str, i);
99 return BSTR(&out);
100}
101
102static void
103helper_add_route(const in_addr_t network, const in_addr_t netmask, struct options *o)
104{
106 add_route_to_option_list(o->routes, print_in_addr_t(network, 0, &o->gc),
107 print_in_addr_t(netmask, 0, &o->gc), NULL, NULL,
108 o->route_default_table_id);
109}
110
111static void
112verify_common_subnet(const char *opt, const in_addr_t a, const in_addr_t b, const in_addr_t subnet)
113{
114 struct gc_arena gc = gc_new();
115 if ((a & subnet) != (b & subnet))
116 {
117 msg(M_USAGE, "%s IP addresses %s and %s are not in the same %s subnet", opt,
118 print_in_addr_t(a, 0, &gc), print_in_addr_t(b, 0, &gc),
119 print_in_addr_t(subnet, 0, &gc));
120 }
121 gc_free(&gc);
122}
123
124
128void
130{
131 if (o->topology != TOP_UNDEF)
132 {
133 return;
134 }
135 int dev = dev_type_enum(o->dev, o->dev_type);
136 if (dev != DEV_TYPE_TUN)
137 {
138 return;
139 }
140 if (o->mode == MODE_SERVER)
141 {
142 o->topology = TOP_SUBNET;
143 }
144 else
145 {
146 o->topology = TOP_NET30;
147 }
148}
149
150
151#if defined(__GNUC__) || defined(__clang__)
152#pragma GCC diagnostic push
153#pragma GCC diagnostic ignored "-Wsign-conversion"
154#endif
155
156/*
157 * Process server, server-bridge, and client helper
158 * directives after the parameters themselves have been
159 * parsed and placed in struct options.
160 */
161void
163{
164 struct gc_arena gc = gc_new();
165
166 /*
167 * Get tun/tap/null device type
168 */
169 const int dev = dev_type_enum(o->dev, o->dev_type);
170
171 /*
172 *
173 * HELPER DIRECTIVE for IPv6
174 *
175 * server-ipv6 2001:db8::/64
176 *
177 * EXPANDS TO:
178 *
179 * tun-ipv6
180 * push "tun-ipv6"
181 * ifconfig-ipv6 2001:db8::1 2001:db8::2
182 * if !nopool:
183 * ifconfig-ipv6-pool 2001:db8::1000/64
184 *
185 */
186 if (o->server_ipv6_defined)
187 {
188 if (o->client)
189 {
190 msg(M_USAGE, "--server-ipv6 and --client cannot be used together");
191 }
192
193 if (o->server_flags & SF_NOPOOL)
194 {
195 msg(M_USAGE, "--server-ipv6 is incompatible with 'nopool' option");
196 }
198 {
199 msg(M_USAGE,
200 "--server-ipv6 already defines an ifconfig-ipv6-pool, so you can't also specify --ifconfig-pool explicitly");
201 }
202
203 o->mode = MODE_SERVER;
204 o->tls_server = true;
205
206 /* local ifconfig is "base address + 1" and "+2" */
211
212 /* basic sanity check */
213 ASSERT(o->server_netbits_ipv6 >= 64 && o->server_netbits_ipv6 <= 124);
214
216 /* For large enough pools we keep the original behaviour of adding
217 * 0x1000 when computing the base.
218 *
219 * Smaller pools can't get that far, therefore we just increase by 2
220 */
222 add_in6_addr(o->server_network_ipv6, o->server_netbits_ipv6 < 112 ? 0x1000 : 2);
224
225 push_option(o, "tun-ipv6", M_USAGE);
226 }
227
228 /*
229 *
230 * HELPER DIRECTIVE:
231 *
232 * server 10.8.0.0 255.255.255.0
233 *
234 * EXPANDS TO:
235 *
236 * mode server
237 * tls-server
238 * push "topology [topology]"
239 *
240 * if tun AND (topology == net30 OR topology == p2p):
241 * ifconfig 10.8.0.1 10.8.0.2
242 * if !nopool:
243 * ifconfig-pool 10.8.0.4 10.8.0.251
244 * route 10.8.0.0 255.255.255.0
245 * if client-to-client:
246 * push "route 10.8.0.0 255.255.255.0"
247 * else if topology == net30:
248 * push "route 10.8.0.1"
249 *
250 * if tap OR (tun AND topology == subnet):
251 * ifconfig 10.8.0.1 255.255.255.0
252 * if !nopool:
253 * ifconfig-pool 10.8.0.2 10.8.0.254 255.255.255.0
254 * push "route-gateway 10.8.0.1"
255 * if route-gateway unset:
256 * route-gateway 10.8.0.2
257 */
258
259 if (o->server_defined)
260 {
261 int netbits = -2;
262 bool status = false;
263
264 if (o->client)
265 {
266 msg(M_USAGE, "--server and --client cannot be used together");
267 }
268
270 {
271 msg(M_USAGE, "--server and --server-bridge cannot be used together");
272 }
273
275 {
276 msg(M_USAGE,
277 "--server already defines an ifconfig-pool, so you can't also specify --ifconfig-pool explicitly");
278 }
279
280 if (!(dev == DEV_TYPE_TAP || dev == DEV_TYPE_TUN))
281 {
282 msg(M_USAGE, "--server directive only makes sense with --dev tun or --dev tap");
283 }
284
286 if (!status)
287 {
288 msg(M_USAGE, "--server directive network/netmask combination is invalid");
289 }
290
291 if (netbits < 0)
292 {
293 msg(M_USAGE, "--server directive netmask is invalid");
294 }
295
296 if (netbits < IFCONFIG_POOL_MIN_NETBITS)
297 {
298 msg(M_USAGE,
299 "--server directive netmask allows for too many host addresses (subnet must be %s or higher)",
301 }
302
303 if (dev == DEV_TYPE_TUN)
304 {
305 int pool_end_reserve = 4;
306
307 if (netbits > 29)
308 {
309 msg(M_USAGE,
310 "--server directive when used with --dev tun must define a subnet of %s or lower",
311 print_netmask(29, &gc));
312 }
313
314 if (netbits == 29)
315 {
316 pool_end_reserve = 0;
317 }
318
319 o->mode = MODE_SERVER;
320 o->tls_server = true;
321 /* Need to know topology now */
323
324 if (o->topology == TOP_NET30 || o->topology == TOP_P2P)
325 {
326 o->ifconfig_local = print_in_addr_t(o->server_network + 1, 0, &o->gc);
328
329 if (!(o->server_flags & SF_NOPOOL))
330 {
331 o->ifconfig_pool_defined = true;
334 (o->server_network | ~o->server_netmask) - pool_end_reserve;
337 }
338
340 if (o->enable_c2c)
341 {
343 M_USAGE);
344 }
345 else if (o->topology == TOP_NET30)
346 {
348 }
349 }
350 else if (o->topology == TOP_SUBNET)
351 {
352 o->ifconfig_local = print_in_addr_t(o->server_network + 1, 0, &o->gc);
354
355 if (!(o->server_flags & SF_NOPOOL))
356 {
357 o->ifconfig_pool_defined = true;
359 o->ifconfig_pool_end = (o->server_network | ~o->server_netmask) - 1;
362 }
364
366 if (!o->route_default_gateway)
367 {
369 }
370 }
371 else
372 {
373 ASSERT(0);
374 }
375
377
378 if (o->topology == TOP_NET30 && !(o->server_flags & SF_NOPOOL))
379 {
380 msg(M_WARN, "WARNING: --topology net30 support for server "
381 "configs with IPv4 pools will be removed in a future "
382 "release. Please migrate to --topology subnet as soon "
383 "as possible.");
384 }
385 }
386 else if (dev == DEV_TYPE_TAP)
387 {
388 if (netbits > 30)
389 {
390 msg(M_USAGE,
391 "--server directive when used with --dev tap must define a subnet of %s or lower",
392 print_netmask(30, &gc));
393 }
394
395 o->mode = MODE_SERVER;
396 o->tls_server = true;
397 o->ifconfig_local = print_in_addr_t(o->server_network + 1, 0, &o->gc);
399
400 if (!(o->server_flags & SF_NOPOOL))
401 {
402 o->ifconfig_pool_defined = true;
404 o->ifconfig_pool_end = (o->server_network | ~o->server_netmask) - 1;
406 }
408
410 }
411 else
412 {
413 ASSERT(0);
414 }
415
416 /* set push-ifconfig-constraint directive */
417 if ((dev == DEV_TYPE_TAP || o->topology == TOP_SUBNET))
418 {
422 }
423 }
424
425 /*
426 * HELPER DIRECTIVE:
427 *
428 * server-bridge 10.8.0.4 255.255.255.0 10.8.0.128 10.8.0.254
429 *
430 * EXPANDS TO:
431 *
432 * mode server
433 * tls-server
434 *
435 * ifconfig-pool 10.8.0.128 10.8.0.254 255.255.255.0
436 * push "route-gateway 10.8.0.4"
437 *
438 * OR
439 *
440 * server-bridge
441 *
442 * EXPANDS TO:
443 *
444 * mode server
445 * tls-server
446 *
447 * if !nogw:
448 * push "route-gateway dhcp"
449 */
451 {
452 if (o->client)
453 {
454 msg(M_USAGE, "--server-bridge and --client cannot be used together");
455 }
456
458 {
459 msg(M_USAGE,
460 "--server-bridge already defines an ifconfig-pool, so you can't also specify --ifconfig-pool explicitly");
461 }
462
463 if (dev != DEV_TYPE_TAP)
464 {
465 msg(M_USAGE, "--server-bridge directive only makes sense with --dev tap");
466 }
467
469 {
470 verify_common_subnet("--server-bridge", o->server_bridge_ip,
476 }
477
478 o->mode = MODE_SERVER;
479 o->tls_server = true;
480
482 {
483 o->ifconfig_pool_defined = true;
489 }
491 {
493 }
494 }
495
496 /*
497 * HELPER DIRECTIVE:
498 *
499 * client
500 *
501 * EXPANDS TO:
502 *
503 * pull
504 * tls-client
505 */
506 else if (o->client)
507 {
508 o->pull = true;
509 o->tls_client = true;
510 }
511
512 gc_free(&gc);
513}
514
515#if defined(__GNUC__) || defined(__clang__)
516#pragma GCC diagnostic pop
517#endif
518
519/*
520 *
521 * HELPER DIRECTIVE:
522 *
523 * keepalive 10 60
524 *
525 * EXPANDS TO:
526 *
527 * if mode server:
528 * ping 10
529 * ping-restart 120
530 * push "ping 10"
531 * push "ping-restart 60"
532 * else
533 * ping 10
534 * ping-restart 60
535 */
536void
538{
540 {
541 /*
542 * Sanity checks.
543 */
544 if (o->keepalive_ping * 2 > o->keepalive_timeout)
545 {
546 msg(M_USAGE,
547 "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.",
549 }
551 {
552 msg(M_USAGE,
553 "--keepalive conflicts with --ping, --ping-exit, or --ping-restart. If you use --keepalive, you don't need any of the other --ping directives.");
554 }
556 {
557 msg(M_USAGE,
558 "The second parameter to --keepalive must not exceed %d in server mode.",
559 PING_TIMEOUT_MAX / 2);
560 }
561
562 /*
563 * Expand.
564 */
565 if (o->mode == MODE_POINT_TO_POINT)
566 {
570 }
571 else if (o->mode == MODE_SERVER)
572 {
576 push_option(o, print_str_int("ping", o->keepalive_ping, &o->gc), M_USAGE);
577 push_option(o, print_str_int("ping-restart", o->keepalive_timeout, &o->gc), M_USAGE);
578 }
579 else
580 {
581 ASSERT(0);
582 }
583 }
584}
585
586/*
587 *
588 * HELPER DIRECTIVE:
589 *
590 * tcp-nodelay
591 *
592 * EXPANDS TO:
593 *
594 * if mode server:
595 * push "socket-flags TCP_NODELAY"
596 */
597void
599{
601 {
602 push_option(o, "socket-flags TCP_NODELAY", M_USAGE);
603 }
604}
bool buf_printf(struct buffer *buf, const char *format,...)
printf-style append to a buffer with overflow check.
Definition buffer.c:231
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
Interface functions to the internal and external multiplexers.
void helper_setdefault_topology(struct options *o)
Set –topology default depending on –mode.
Definition helper.c:129
static const char * print_opt_route_gateway_dhcp(struct gc_arena *gc)
Definition helper.c:58
void helper_tcp_nodelay(struct options *o)
Definition helper.c:598
static void verify_common_subnet(const char *opt, const in_addr_t a, const in_addr_t b, const in_addr_t subnet)
Definition helper.c:112
static void helper_add_route(const in_addr_t network, const in_addr_t netmask, struct options *o)
Definition helper.c:103
static const char * print_opt_route_gateway(const in_addr_t route_gateway, struct gc_arena *gc)
Definition helper.c:49
static const char * print_opt_route(const in_addr_t network, const in_addr_t netmask, struct gc_arena *gc)
Definition helper.c:66
void helper_client_server(struct options *o)
Definition helper.c:162
void helper_keepalive(struct options *o)
Definition helper.c:537
static const char * print_str_int(const char *str, const int i, struct gc_arena *gc)
Definition helper.c:95
static const char * print_netmask(int netbits, struct gc_arena *gc)
Definition helper.c:38
static const char * print_opt_topology(const int topology, struct gc_arena *gc)
Definition helper.c:85
static SERVICE_STATUS status
Definition interactive.c:52
#define M_USAGE
Definition error.h:107
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
void rol_check_alloc(struct options *options)
Definition options.c:1280
const char * print_topology(const int topology)
Definition options.c:3292
#define MODE_POINT_TO_POINT
Definition options.h:264
#define SF_TCP_NODELAY_HELPER
Definition options.h:475
#define MODE_SERVER
Definition options.h:265
#define SF_NOPOOL
Definition options.h:474
#define PING_RESTART
Definition options.h:356
#define SF_NO_PUSH_ROUTE_GATEWAY
Definition options.h:476
#define PING_TIMEOUT_MAX
Definition options.h:59
bool ifconfig_pool_verify_range(const msglvl_t msglevel, const in_addr_t start, const in_addr_t end)
Definition pool.c:132
#define IFCONFIG_POOL_MIN_NETBITS
Definition pool.h:32
#define DEV_TYPE_TAP
Definition proto.h:36
#define TOP_UNDEF
Definition proto.h:40
#define TOP_NET30
Definition proto.h:41
#define DEV_TYPE_TUN
Definition proto.h:35
#define TOP_P2P
Definition proto.h:42
#define TOP_SUBNET
Definition proto.h:43
void push_option(struct options *o, const char *opt, msglvl_t msglevel)
Definition push.c:963
bool netmask_to_netbits(const in_addr_t network, const in_addr_t netmask, int *netbits)
Definition route.c:3805
void add_route_to_option_list(struct route_option_list *l, const char *network, const char *netmask, const char *gateway, const char *metric, int table_id)
Definition route.c:482
static in_addr_t netbits_to_netmask(const int netbits)
Definition route.h:399
struct in6_addr add_in6_addr(struct in6_addr base, uint32_t add)
const char * print_in6_addr(struct in6_addr a6, unsigned int flags, struct gc_arena *gc)
const char * print_in_addr_t(in_addr_t addr, unsigned int flags, struct gc_arena *gc)
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
int len
Length in bytes of the actual content within the allocated memory.
Definition buffer.h:76
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
const char * ifconfig_ipv6_remote
Definition options.h:331
in_addr_t push_ifconfig_constraint_network
Definition options.h:519
struct in6_addr server_network_ipv6
Definition options.h:471
in_addr_t server_network
Definition options.h:468
bool server_bridge_defined
Definition options.h:481
in_addr_t ifconfig_pool_netmask
Definition options.h:491
in_addr_t server_netmask
Definition options.h:469
bool server_bridge_proxy_dhcp
Definition options.h:479
const char * ifconfig_ipv6_local
Definition options.h:329
const char * dev_type
Definition options.h:323
bool ifconfig_pool_defined
Definition options.h:488
in_addr_t server_bridge_netmask
Definition options.h:483
in_addr_t ifconfig_pool_end
Definition options.h:490
int keepalive_timeout
Definition options.h:343
bool ifconfig_ipv6_pool_defined
Definition options.h:495
unsigned int server_flags
Definition options.h:477
bool server_defined
Definition options.h:467
const char * ifconfig_local
Definition options.h:327
const char * route_default_gateway
Definition options.h:426
int topology
Definition options.h:326
int mode
Definition options.h:266
bool tls_server
Definition options.h:588
bool client
Definition options.h:556
bool pull
Definition options.h:557
int ifconfig_ipv6_pool_netbits
Definition options.h:497
in_addr_t push_ifconfig_constraint_netmask
Definition options.h:520
bool tls_client
Definition options.h:589
int ping_rec_timeout_action
Definition options.h:357
struct gc_arena gc
Definition options.h:258
bool push_ifconfig_constraint_defined
Definition options.h:518
int ping_rec_timeout
Definition options.h:351
int ping_send_timeout
Definition options.h:350
bool server_ipv6_defined
Definition options.h:470
int keepalive_ping
Definition options.h:342
in_addr_t server_bridge_pool_start
Definition options.h:484
const char * ifconfig_remote_netmask
Definition options.h:328
bool enable_c2c
Definition options.h:527
in_addr_t server_bridge_pool_end
Definition options.h:485
in_addr_t ifconfig_pool_start
Definition options.h:489
unsigned int server_netbits_ipv6
Definition options.h:472
in_addr_t server_bridge_ip
Definition options.h:482
const char * dev
Definition options.h:322
struct in6_addr ifconfig_ipv6_pool_base
Definition options.h:496
int ifconfig_ipv6_netbits
Definition options.h:330
uint32_t in_addr_t
Definition syshead.h:52
struct gc_arena gc
Definition test_ssl.c:138
int dev_type_enum(const char *dev, const char *dev_type)
Definition tun.c:527