OpenVPN
networking.h
Go to the documentation of this file.
1 /*
2  * Generic interface to platform specific networking code
3  *
4  * Copyright (C) 2016-2023 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, write to the Free Software Foundation, Inc.,
18  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 #ifndef NETWORKING_H_
22 #define NETWORKING_H_
23 
24 #include "syshead.h"
25 
26 #define IFACE_TYPE_LEN_MAX 64
27 
28 struct context;
29 
30 #ifdef ENABLE_SITNL
31 #include "networking_sitnl.h"
32 #elif ENABLE_IPROUTE
33 #include "networking_iproute2.h"
34 #elif defined(TARGET_FREEBSD)
35 typedef void *openvpn_net_ctx_t;
36 typedef char openvpn_net_iface_t;
37 #else /* ifdef ENABLE_SITNL */
38 /* define mock types to ensure code builds on any platform */
39 typedef void *openvpn_net_ctx_t;
40 typedef void *openvpn_net_iface_t;
41 #endif /* ifdef ENABLE_SITNL */
42 
43 /* Only the iproute2 backend implements these functions,
44  * the rest can rely on these stubs
45  */
46 #if !defined(ENABLE_IPROUTE)
47 static inline int
49 {
50  (void)c;
51  (void)ctx;
52 
53  return 0;
54 }
55 
56 static inline void
58 {
59  (void)ctx;
60 }
61 
62 static inline void
64 {
65  (void)ctx;
66 }
67 #endif /* !defined(ENABLE_IPROUTE) */
68 
69 #if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE)
70 
79 int net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx);
80 
87 
94 
105 int net_iface_new(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
106  const char *type, void *arg);
107 
117 int net_iface_type(openvpn_net_ctx_t *ctx, const char *iface,
118  char type[IFACE_TYPE_LEN_MAX]);
119 
127 int net_iface_del(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface);
128 
138 int net_iface_up(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
139  bool up);
140 
150 int net_iface_mtu_set(openvpn_net_ctx_t *ctx,
151  const openvpn_net_iface_t *iface, uint32_t mtu);
152 
162 int net_addr_ll_set(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
163  uint8_t *addr);
164 
175 int net_addr_v4_add(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
176  const in_addr_t *addr, int prefixlen);
177 
189 int net_addr_v6_add(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
190  const struct in6_addr *addr, int prefixlen);
191 
201 int net_addr_v4_del(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
202  const in_addr_t *addr, int prefixlen);
203 
213 int net_addr_v6_del(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface,
214  const struct in6_addr *addr, int prefixlen);
215 
226 int net_addr_ptp_v4_add(openvpn_net_ctx_t *ctx,
227  const openvpn_net_iface_t *iface,
228  const in_addr_t *local, const in_addr_t *remote);
229 
240 int net_addr_ptp_v4_del(openvpn_net_ctx_t *ctx,
241  const openvpn_net_iface_t *iface,
242  const in_addr_t *local, const in_addr_t *remote);
243 
244 #endif /* ENABLE_SITNL || ENABLE_IPROUTE */
245 
246 #if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE) || defined(TARGET_FREEBSD)
247 
261 int net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst,
262  int prefixlen, const in_addr_t *gw,
263  const openvpn_net_iface_t *iface, uint32_t table,
264  int metric);
265 
280 int net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
281  int prefixlen, const struct in6_addr *gw,
282  const openvpn_net_iface_t *iface,
283  uint32_t table, int metric);
284 
299 int net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst,
300  int prefixlen, const in_addr_t *gw,
301  const openvpn_net_iface_t *iface, uint32_t table,
302  int metric);
303 
318 int net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
319  int prefixlen, const struct in6_addr *gw,
320  const openvpn_net_iface_t *iface,
321  uint32_t table, int metric);
322 
323 #endif /* ENABLE_SITNL || ENABLE_IPROUTE || TARGET_FREEBSD */
324 
325 #if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE)
326 
338 int net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst,
339  in_addr_t *best_gw, openvpn_net_iface_t *best_iface);
340 
352 int net_route_v6_best_gw(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
353  struct in6_addr *best_gw,
354  openvpn_net_iface_t *best_iface);
355 
356 #endif /* ENABLE_SITNL || ENABLE_IPROUTE */
357 
358 #endif /* NETWORKING_H_ */
IFACE_TYPE_LEN_MAX
#define IFACE_TYPE_LEN_MAX
Definition: networking.h:26
context
Contains all state information for one tunnel.
Definition: openvpn.h:476
openvpn_net_ctx_t
void * openvpn_net_ctx_t
Definition: networking.h:28
net_ctx_init
static int net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)
Definition: networking.h:48
networking_sitnl.h
net_ctx_reset
static void net_ctx_reset(openvpn_net_ctx_t *ctx)
Definition: networking.h:57
iface
static char * iface
Definition: test_networking.c:7
net_ctx_free
static void net_ctx_free(openvpn_net_ctx_t *ctx)
Definition: networking.h:63
syshead.h
networking_iproute2.h
openvpn_net_iface_t
void * openvpn_net_iface_t
Definition: networking.h:40