OpenVPN
networking.h
Go to the documentation of this file.
1/*
2 * Generic interface to platform specific networking code
3 *
4 * Copyright (C) 2016-2025 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#ifndef NETWORKING_H_
21#define NETWORKING_H_
22
23#include "syshead.h"
24
25#define IFACE_TYPE_LEN_MAX 64
26
27struct context;
28
29#ifdef ENABLE_SITNL
30#include "networking_sitnl.h"
31#elif ENABLE_IPROUTE
32#include "networking_iproute2.h"
33#elif defined(TARGET_FREEBSD)
34typedef void *openvpn_net_ctx_t;
35typedef char openvpn_net_iface_t;
36#else /* ifdef ENABLE_SITNL */
37/* define mock types to ensure code builds on any platform */
38typedef void *openvpn_net_ctx_t;
39typedef void *openvpn_net_iface_t;
40#endif /* ifdef ENABLE_SITNL */
41
42/* Only the iproute2 backend implements these functions,
43 * the rest can rely on these stubs
44 */
45#if !defined(ENABLE_IPROUTE)
46static inline int
48{
49 (void)c;
50 (void)ctx;
51
52 return 0;
53}
54
55static inline void
57{
58 (void)ctx;
59}
60
61static inline void
63{
64 (void)ctx;
65}
66#endif /* !defined(ENABLE_IPROUTE) */
67
68#if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE)
69
78int net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx);
79
86
93
104int net_iface_new(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, const char *type,
105 void *arg);
106
116int net_iface_type(openvpn_net_ctx_t *ctx, const char *iface, char type[IFACE_TYPE_LEN_MAX]);
117
125int net_iface_del(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface);
126
136int net_iface_up(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, bool up);
137
147int net_iface_mtu_set(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, uint32_t mtu);
148
158int net_addr_ll_set(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, uint8_t *addr);
159
170int net_addr_v4_add(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, const in_addr_t *addr,
171 int prefixlen);
172
184int net_addr_v6_add(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
185 const struct in6_addr *addr, int prefixlen);
186
196int net_addr_v4_del(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, const in_addr_t *addr,
197 int prefixlen);
198
208int net_addr_v6_del(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
209 const struct in6_addr *addr, int prefixlen);
210
221int net_addr_ptp_v4_add(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
222 const in_addr_t *local, const in_addr_t *remote);
223
234int net_addr_ptp_v4_del(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
235 const in_addr_t *local, const in_addr_t *remote);
236
237#endif /* ENABLE_SITNL || ENABLE_IPROUTE */
238
239#if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE) || defined(TARGET_FREEBSD)
254int net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
255 const in_addr_t *gw, const openvpn_net_iface_t *iface, uint32_t table,
256 int metric);
257
272int net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst, int prefixlen,
273 const struct in6_addr *gw, const openvpn_net_iface_t *iface, uint32_t table,
274 int metric);
275
290int net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
291 const in_addr_t *gw, const openvpn_net_iface_t *iface, uint32_t table,
292 int metric);
293
308int net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst, int prefixlen,
309 const struct in6_addr *gw, const openvpn_net_iface_t *iface, uint32_t table,
310 int metric);
311
312#endif /* ENABLE_SITNL || ENABLE_IPROUTE || TARGET_FREEBSD */
313
314#if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE)
315
327int net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst, in_addr_t *best_gw,
328 openvpn_net_iface_t *best_iface);
329
341int net_route_v6_best_gw(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
342 struct in6_addr *best_gw, openvpn_net_iface_t *best_iface);
343
344#endif /* ENABLE_SITNL || ENABLE_IPROUTE */
345
346#endif /* NETWORKING_H_ */
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(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
Contains all state information for one tunnel.
Definition openvpn.h:474
static char * iface