OpenVPN
networking_iproute2.c
Go to the documentation of this file.
1/*
2 * Networking API implementation for iproute2
3 *
4 * Copyright (C) 2018-2026 Antonio Quartulli <a@unstable.cc>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program (see the file COPYING included with this
17 * distribution); if not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#if defined(TARGET_LINUX) && defined(ENABLE_IPROUTE)
25
26#include "syshead.h"
27
28#include "argv.h"
29#include "networking.h"
30#include "misc.h"
31#include "openvpn.h"
32#include "run_command.h"
33#include "socket_util.h"
34
35#include <stdbool.h>
36#include <netinet/in.h>
37
38int
39net_ctx_init(const struct context *c, openvpn_net_ctx_t *ctx)
40{
41 ctx->es = NULL;
42 if (c)
43 {
44 ctx->es = c->es;
45 }
46 return 0;
47}
48
49void
51{
52 (void)ctx;
53}
54
55void
57{
58 (void)ctx;
59}
60
61int
62net_iface_new(const openvpn_net_ctx_t *ctx, const char *iface, const char *type, const void *arg)
63{
64 struct argv argv = argv_new();
65
66 argv_printf(&argv, "%s link add %s type %s", iproute_path, iface, type);
68 openvpn_execve_check(&argv, ctx->es, S_FATAL, "Linux ip link add failed");
69
71
72 return 0;
73}
74
75int
76net_iface_type(const openvpn_net_ctx_t *ctx, const char *iface, char type[IFACE_TYPE_LEN_MAX])
77{
78 /* not supported by iproute2 */
79 msg(M_WARN, "%s: operation not supported by iproute2 backend", __func__);
80 return -1;
81}
82
83int
84net_iface_del(const openvpn_net_ctx_t *ctx, const char *iface)
85{
86 struct argv argv = argv_new();
87
88 argv_printf(&argv, "%s link del %s", iproute_path, iface);
89 openvpn_execve_check(&argv, ctx->es, 0, "Linux ip link del failed");
90
92
93 return 0;
94}
95
96int
97net_iface_up(const openvpn_net_ctx_t *ctx, const char *iface, bool up)
98{
99 struct argv argv = argv_new();
100
101 argv_printf(&argv, "%s link set dev %s %s", iproute_path, iface, up ? "up" : "down");
103 openvpn_execve_check(&argv, ctx->es, S_FATAL, "Linux ip link set failed");
104
105 argv_free(&argv);
106
107 return 0;
108}
109
110int
111net_iface_mtu_set(const openvpn_net_ctx_t *ctx, const char *iface, uint32_t mtu)
112{
113 struct argv argv = argv_new();
114
115 argv_printf(&argv, "%s link set dev %s up mtu %d", iproute_path, iface, mtu);
117 openvpn_execve_check(&argv, ctx->es, S_FATAL, "Linux ip link set failed");
118
119 argv_free(&argv);
120
121 return 0;
122}
123
124int
125net_addr_ll_set(const openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, uint8_t *addr)
126{
127 struct argv argv = argv_new();
128 int ret = 0;
129
130 argv_printf(&argv, "%s link set addr " MAC_FMT " dev %s", iproute_path, MAC_PRINT_ARG(addr),
131 iface);
132
134 if (!openvpn_execve_check(&argv, ctx->es, 0, "Linux ip link set addr failed"))
135 {
136 ret = -1;
137 }
138
139 argv_free(&argv);
140
141 return ret;
142}
143
144int
145net_addr_v4_add(const openvpn_net_ctx_t *ctx, const char *iface, const in_addr_t *addr, int prefixlen)
146{
147 struct argv argv = argv_new();
148
149 const char *addr_str = print_in_addr_t(*addr, 0, &argv.gc);
150
151 argv_printf(&argv, "%s addr add dev %s %s/%d broadcast +", iproute_path, iface, addr_str, prefixlen);
153 openvpn_execve_check(&argv, ctx->es, S_FATAL, "Linux ip addr add failed");
154
155 argv_free(&argv);
156
157 return 0;
158}
159
160int
161net_addr_v6_add(const openvpn_net_ctx_t *ctx, const char *iface, const struct in6_addr *addr,
162 int prefixlen)
163{
164 struct argv argv = argv_new();
165 char *addr_str = (char *)print_in6_addr(*addr, 0, &argv.gc);
166
167 argv_printf(&argv, "%s -6 addr add %s/%d dev %s", iproute_path, addr_str, prefixlen, iface);
169 openvpn_execve_check(&argv, ctx->es, S_FATAL, "Linux ip -6 addr add failed");
170
171 argv_free(&argv);
172
173 return 0;
174}
175
176int
177net_addr_v4_del(const openvpn_net_ctx_t *ctx, const char *iface, const in_addr_t *addr, int prefixlen)
178{
179 struct argv argv = argv_new();
180 const char *addr_str = print_in_addr_t(*addr, 0, &argv.gc);
181
182 argv_printf(&argv, "%s addr del dev %s %s/%d", iproute_path, iface, addr_str, prefixlen);
183
185 openvpn_execve_check(&argv, ctx->es, 0, "Linux ip addr del failed");
186
187 argv_free(&argv);
188
189 return 0;
190}
191
192int
193net_addr_v6_del(const openvpn_net_ctx_t *ctx, const char *iface, const struct in6_addr *addr,
194 int prefixlen)
195{
196 struct argv argv = argv_new();
197 char *addr_str = (char *)print_in6_addr(*addr, 0, &argv.gc);
198
199 argv_printf(&argv, "%s -6 addr del %s/%d dev %s", iproute_path, addr_str, prefixlen, iface);
201 openvpn_execve_check(&argv, ctx->es, 0, "Linux ip -6 addr del failed");
202
203 argv_free(&argv);
204
205 return 0;
206}
207
208int
209net_addr_ptp_v4_add(const openvpn_net_ctx_t *ctx, const char *iface, const in_addr_t *local,
210 const in_addr_t *remote)
211{
212 struct argv argv = argv_new();
213 const char *local_str = print_in_addr_t(*local, 0, &argv.gc);
214 const char *remote_str = print_in_addr_t(*remote, 0, &argv.gc);
215
216 argv_printf(&argv, "%s addr add dev %s local %s peer %s", iproute_path, iface, local_str,
217 remote_str);
219 openvpn_execve_check(&argv, ctx->es, S_FATAL, "Linux ip addr add failed");
220
221 argv_free(&argv);
222
223 return 0;
224}
225
226int
227net_addr_ptp_v4_del(const openvpn_net_ctx_t *ctx, const char *iface, const in_addr_t *local,
228 const in_addr_t *remote)
229{
230 struct argv argv = argv_new();
231 const char *local_str = print_in_addr_t(*local, 0, &argv.gc);
232 const char *remote_str = print_in_addr_t(*remote, 0, &argv.gc);
233
234 argv_printf(&argv, "%s addr del dev %s local %s peer %s", iproute_path, iface, local_str,
235 remote_str);
237 openvpn_execve_check(&argv, ctx->es, 0, "Linux ip addr del failed");
238
239 argv_free(&argv);
240
241 return 0;
242}
243
244int
245net_route_v4_add(const openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen, const in_addr_t *gw,
246 const char *iface, uint32_t table, int metric)
247{
248 struct argv argv = argv_new();
249 const char *dst_str = print_in_addr_t(*dst, 0, &argv.gc);
250 int ret = 0;
251
252 argv_printf(&argv, "%s route add %s/%d", iproute_path, dst_str, prefixlen);
253
254 if (metric > 0)
255 {
256 argv_printf_cat(&argv, "metric %d", metric);
257 }
258
259 if (iface)
260 {
261 argv_printf_cat(&argv, "dev %s", iface);
262 }
263
264 if (gw)
265 {
266 const char *gw_str = print_in_addr_t(*gw, 0, &argv.gc);
267
268 argv_printf_cat(&argv, "via %s", gw_str);
269 }
270
272 if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route add command failed"))
273 {
274 ret = -1;
275 }
276
277 argv_free(&argv);
278
279 return ret;
280}
281
282int
283net_route_v6_add(const openvpn_net_ctx_t *ctx, const struct in6_addr *dst, int prefixlen,
284 const struct in6_addr *gw, const char *iface, uint32_t table, int metric)
285{
286 struct argv argv = argv_new();
287 char *dst_str = (char *)print_in6_addr(*dst, 0, &argv.gc);
288 int ret = 0;
289
290 argv_printf(&argv, "%s -6 route add %s/%d dev %s", iproute_path, dst_str, prefixlen, iface);
291
292 if (gw)
293 {
294 char *gw_str = (char *)print_in6_addr(*gw, 0, &argv.gc);
295
296 argv_printf_cat(&argv, "via %s", gw_str);
297 }
298
299 if (metric > 0)
300 {
301 argv_printf_cat(&argv, "metric %d", metric);
302 }
303
305 if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route -6 add command failed"))
306 {
307 ret = -1;
308 }
309
310 argv_free(&argv);
311
312 return ret;
313}
314
315int
316net_route_v4_del(const openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen, const in_addr_t *gw,
317 const char *iface, uint32_t table, int metric)
318{
319 struct argv argv = argv_new();
320 const char *dst_str = print_in_addr_t(*dst, 0, &argv.gc);
321 int ret = 0;
322
323 argv_printf(&argv, "%s route del %s/%d", iproute_path, dst_str, prefixlen);
324
325 if (metric > 0)
326 {
327 argv_printf_cat(&argv, "metric %d", metric);
328 }
329
331 if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route delete command failed"))
332 {
333 ret = -1;
334 }
335
336 argv_free(&argv);
337
338 return ret;
339}
340
341int
342net_route_v6_del(const openvpn_net_ctx_t *ctx, const struct in6_addr *dst, int prefixlen,
343 const struct in6_addr *gw, const char *iface, uint32_t table, int metric)
344{
345 struct argv argv = argv_new();
346 char *dst_str = (char *)print_in6_addr(*dst, 0, &argv.gc);
347 int ret = 0;
348
349 argv_printf(&argv, "%s -6 route del %s/%d dev %s", iproute_path, dst_str, prefixlen, iface);
350
351 if (gw)
352 {
353 char *gw_str = (char *)print_in6_addr(*gw, 0, &argv.gc);
354
355 argv_printf_cat(&argv, "via %s", gw_str);
356 }
357
358 if (metric > 0)
359 {
360 argv_printf_cat(&argv, "metric %d", metric);
361 }
362
364 if (!openvpn_execve_check(&argv, ctx->es, 0, "ERROR: Linux route -6 del command failed"))
365 {
366 ret = -1;
367 }
368
369 argv_free(&argv);
370
371 return ret;
372}
373
374/*
375 * The following functions are not implemented in the iproute backend as it
376 * uses the sitnl implementation from networking_sitnl.c.
377 *
378 * int
379 * net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst,
380 * in_addr_t *best_gw, char *best_iface)
381 *
382 * int
383 * net_route_v6_best_gw(const struct in6_addr *dst,
384 * struct in6_addr *best_gw, char *best_iface)
385 */
386
387#endif /* ENABLE_IPROUTE && TARGET_LINUX */
void argv_msg(const msglvl_t msglevel, const struct argv *a)
Write the arguments stored in a struct argv via the msg() command.
Definition argv.c:242
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
bool argv_printf_cat(struct argv *argres, const char *format,...)
printf() inspired argv concatenation.
Definition argv.c:471
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition argv.c:87
#define M_INFO
Definition errlevel.h:54
#define D_ROUTE
Definition errlevel.h:78
#define MAC_PRINT_ARG(_mac)
Definition misc.h:227
#define MAC_FMT
Definition misc.h:225
static void net_ctx_reset(openvpn_net_ctx_t *ctx)
Definition networking.h:56
void * openvpn_net_iface_t
Definition networking.h:39
#define IFACE_TYPE_LEN_MAX
Definition networking.h:25
static int net_ctx_init(const struct context *c, openvpn_net_ctx_t *ctx)
Definition networking.h:47
static void net_ctx_free(openvpn_net_ctx_t *ctx)
Definition networking.h:62
void * openvpn_net_ctx_t
Definition networking.h:38
#define msg(flags,...)
Definition error.h:152
#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)
#define S_FATAL
Definition run_command.h:50
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)
Definition argv.h:35
struct gc_arena gc
Definition argv.h:36
Contains all state information for one tunnel.
Definition openvpn.h:445
struct env_set * es
Set of environment variables.
Definition openvpn.h:470
uint32_t in_addr_t
Definition syshead.h:52
static char * iface