OpenVPN
tun_afunix.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 "tun.h"
30#include "fdmisc.h"
31#include "run_command.h"
32#include "manage.h"
33#include "win32.h"
34#include "wfp_block.h"
35#include "argv.h"
36#include "options.h"
37#include "socket.h"
38#include "tun_afunix.h"
39
40#ifndef WIN32
41/* Windows does implement some AF_UNIX functionality but key features
42 * like socketpair() and SOCK_DGRAM are missing */
43
44#include <string.h>
45#include <unistd.h>
46#include <sys/wait.h>
47#include <signal.h>
48#include <stdlib.h>
49
50
51#if defined(__GNUC__) || defined(__clang__)
52#pragma GCC diagnostic push
53#pragma GCC diagnostic ignored "-Wsign-conversion"
54#endif
55
56static void
57tun_afunix_exec_child(const char *dev_node, struct tuntap *tt, const struct env_set *env)
58{
59 const char *msgprefix = "ERROR: failure executing process for tun:";
60 struct argv argv = argv_new();
61
62 /* we should always called with a proper unix: dev node string */
63 ASSERT(dev_node && strncmp(dev_node, "unix:", strlen("unix:")) == 0);
64 /* since we know that dev-node starts with unix: we can just skip that
65 * to get the program name */
66 const char *program = dev_node + strlen("unix:");
67
68 argv_printf(&argv, "%s", program);
69
70 /* exit when executing fails to easier spot errors here and treat this
71 * command like an external script */
72 int flags = S_NOWAITPID | S_SCRIPT | S_FATAL;
73 tt->afunix.childprocess = openvpn_execve_check(&argv, env, flags, msgprefix);
74 if (!openvpn_waitpid_check(tt->afunix.childprocess, msgprefix, M_WARN))
75 {
76 tt->afunix.childprocess = 0;
77 }
79}
80
81void
82open_tun_afunix(const struct options *o, int mtu, struct tuntap *tt, const struct env_set *orig_env)
83{
84 struct gc_arena gc = gc_new();
85
86 int fds[2];
87 if (!(socketpair(AF_UNIX, SOCK_DGRAM, 0, fds) == 0))
88 {
89 msg(M_ERR, "Cannot create socket pair for AF_UNIX socket to external "
90 "program");
91 return;
92 }
93
94
95 /* Ensure that the buffer sizes are decently sized. Otherwise macOS will
96 * just have 2048 */
97 struct socket_buffer_size newsizes = { 65536, 65536 };
98 socket_set_buffers(fds[0], &newsizes, false);
99 socket_set_buffers(fds[1], &newsizes, false);
100
101 /* Use the first file descriptor for our side and avoid passing it
102 * to the child */
103 tt->fd = fds[1];
104 set_cloexec(tt->fd);
105
106 /* Make a copy of the env, so we do not need to delete our custom
107 * environment variables later */
108 struct env_set *env = env_set_create(&gc);
109 env_set_inherit(env, orig_env);
110
111 setenv_int(env, "TUNTAP_SOCKET_FD", fds[0]);
112 setenv_str(env, "TUNTAP_DEV_TYPE", dev_type_string(o->dev, o->dev_type));
113 setenv_int(env, "TUNTAP_MTU", mtu);
115 {
116 setenv_str(env, "ifconfig_gateway", o->route_default_gateway);
117 }
118 if (o->lladdr)
119 {
120 setenv_str(env, "TUNTAP_LLADDR", o->lladdr);
121 }
122
123 tun_afunix_exec_child(o->dev_node, tt, env);
124
125 close(fds[0]);
126
127 /* tt->actual_name is passed to up and down scripts and used as the ifconfig dev name */
128 tt->actual_name = string_alloc("internal:af_unix", NULL);
129
130 gc_free(&gc);
131}
132
133void
135{
136 ASSERT(tt);
137 if (tt->fd >= 0)
138 {
139 close(tt->fd);
140 tt->fd = 0;
141 }
142 /* only kill the child process if the PID is not 0 to avoid killing
143 * ourselves by accident */
144 if (tt->afunix.childprocess)
145 {
146 kill(tt->afunix.childprocess, SIGINT);
147 }
148
149 free(tt->actual_name);
150 free(tt);
151}
152
153ssize_t
154write_tun_afunix(struct tuntap *tt, const uint8_t *buf, int len)
155{
156 const char *msg = "ERROR: failure during write to AF_UNIX socket: ";
158 {
159 tt->afunix.childprocess = 0;
160 return -ENXIO;
161 }
162
163 return write(tt->fd, buf, len);
164}
165
166ssize_t
167read_tun_afunix(struct tuntap *tt, uint8_t *buf, int len)
168{
169 const char *msg = "ERROR: failure during read from AF_UNIX socket: ";
171 {
172 tt->afunix.childprocess = 0;
173 }
174 /* do an actual read on the file descriptor even in the error case since
175 * we otherwise loop on this on this from select and spam the console
176 * with error messages */
177 return read(tt->fd, buf, len);
178}
179
180#if defined(__GNUC__) || defined(__clang__)
181#pragma GCC diagnostic pop
182#endif
183
184#else /* ifndef WIN32 */
185
186// cppcheck-suppress-begin constParameterPointer ; stubs
187
188void
189open_tun_afunix(const struct options *o, int mtu, struct tuntap *tt, const struct env_set *orig_env)
190{
191 msg(M_ERR, "AF_UNIX socket support not available on this platform");
192}
193
194void
195close_tun_afunix(struct tuntap *tt)
196{
197 /* should never be called as open_tun_afunix always fails */
198 ASSERT(0);
199}
200
201ssize_t
202write_tun_afunix(struct tuntap *tt, const uint8_t *buf, int len)
203{
204 /* should never be called as open_tun_afunix always fails */
205 ASSERT(0);
206}
207
208ssize_t
209read_tun_afunix(struct tuntap *tt, uint8_t *buf, int len)
210{
211 /* should never be called as open_tun_afunix always fails */
212 ASSERT(0);
213}
214
215// cppcheck-suppress-end constParameterPointer
216
217#endif /* ifndef WIN32 */
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition argv.c:101
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition argv.c:447
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:87
char * string_alloc(const char *str, struct gc_arena *gc)
Duplicate a string, allocating memory under garbage collection.
Definition buffer.c:618
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
void setenv_int(struct env_set *es, const char *name, int value)
Definition env_set.c:289
void setenv_str(struct env_set *es, const char *name, const char *value)
Definition env_set.c:305
void env_set_inherit(struct env_set *es, const struct env_set *src)
Definition env_set.c:260
struct env_set * env_set_create(struct gc_arena *gc)
Definition env_set.c:155
void set_cloexec(socket_descriptor_t fd)
Definition fdmisc.c:87
@ write
@ read
#define M_ERR
Definition error.h:106
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_message)
bool openvpn_waitpid_check(pid_t pid, const char *msg_prefix, msglvl_t msglevel)
Checks if a running process is still running.
#define S_FATAL
Definition run_command.h:50
#define S_SCRIPT
Definition run_command.h:49
#define S_NOWAITPID
instead of waiting for child process to exit and report the status, return the pid of the child proce...
Definition run_command.h:56
void socket_set_buffers(socket_descriptor_t fd, const struct socket_buffer_size *sbs, bool reduce_size)
Sets the receive and send buffer sizes of a socket descriptor.
Definition socket.c:103
pid_t childprocess
Definition tun.h:170
Definition argv.h:35
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
const char * dev_type
Definition options.h:323
const char * route_default_gateway
Definition options.h:426
const char * dev_node
Definition options.h:324
const char * lladdr
Definition options.h:325
const char * dev
Definition options.h:322
Definition tun.h:181
char * actual_name
Definition tun.h:205
afunix_context_t afunix
Definition tun.h:248
#define SIGINT
Definition syshead.h:56
struct gc_arena gc
Definition test_ssl.c:138
const char * dev_type_string(const char *dev, const char *dev_type)
Definition tun.c:546
ssize_t write_tun_afunix(struct tuntap *tt, const uint8_t *buf, int len)
Writes a packet to a AF_UNIX based tun device.
Definition tun_afunix.c:154
void open_tun_afunix(const struct options *o, int mtu, struct tuntap *tt, const struct env_set *orig_env)
Opens an AF_UNIX based tun device.
Definition tun_afunix.c:82
ssize_t read_tun_afunix(struct tuntap *tt, uint8_t *buf, int len)
Reads a packet from a AF_UNIX based tun device.
Definition tun_afunix.c:167
void close_tun_afunix(struct tuntap *tt)
Closes the socket used for the AF_UNIX based device.
Definition tun_afunix.c:134
static void tun_afunix_exec_child(const char *dev_node, struct tuntap *tt, const struct env_set *env)
Definition tun_afunix.c:57