OpenVPN
networking_freebsd.c
Go to the documentation of this file.
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4#include "syshead.h"
5#include "errlevel.h"
6#include "run_command.h"
7#include "networking.h"
8
9#if defined(TARGET_FREEBSD)
10
11static int
12net_route_v4(const char *op, const in_addr_t *dst, int prefixlen, const in_addr_t *gw,
13 const char *iface, uint32_t table, int metric)
14{
15 char buf1[INET_ADDRSTRLEN], buf2[INET_ADDRSTRLEN];
16 in_addr_t _dst, _gw;
17 struct argv argv = argv_new();
18
19 ASSERT(gw || iface);
20
21 _dst = ntohl(*dst);
22
23 /* if we have a gateway (GW != NULL) install route to gateway IP
24 * if not, install "connected" route to interface
25 * (needed to make 'ifconfig-push IPs outside server subnet' work)
26 */
27 if (gw)
28 {
29 _gw = ntohl(*gw);
30 argv_printf(&argv, "%s %s -net %s/%d %s -fib %d", ROUTE_PATH, op,
31 inet_ntop(AF_INET, &_dst, buf1, sizeof(buf1)), prefixlen,
32 inet_ntop(AF_INET, &_gw, buf2, sizeof(buf2)), table);
33 }
34 else
35 {
36 argv_printf(&argv, "%s %s -net %s/%d -iface %s -fib %d", ROUTE_PATH, op,
37 inet_ntop(AF_INET, &_dst, buf1, sizeof(buf1)), prefixlen,
38 iface, table);
39 }
40
42 bool status = openvpn_execve_check(&argv, NULL, 0, "ERROR: FreeBSD route command failed");
43
45
46 return (!status);
47}
48
49static int
50net_route_v6(const char *op, const struct in6_addr *dst, int prefixlen, const struct in6_addr *gw,
51 const char *iface, uint32_t table, int metric)
52{
53 char buf1[INET6_ADDRSTRLEN], buf2[INET6_ADDRSTRLEN];
54 struct argv argv = argv_new();
55
56 ASSERT(gw || iface);
57
58 /* if we have a gateway (GW != NULL) install route to gateway IP
59 * if not, install "connected" route to interface
60 * (needed to make 'ifconfig-push IPs outside server subnet' work)
61 */
62 if (gw)
63 {
64 argv_printf(&argv, "%s -6 %s -net %s/%d %s -fib %d", ROUTE_PATH, op,
65 inet_ntop(AF_INET6, dst, buf1, sizeof(buf1)), prefixlen,
66 inet_ntop(AF_INET6, gw, buf2, sizeof(buf2)), table);
67 }
68 else
69 {
70 argv_printf(&argv, "%s -6 %s -net %s/%d -iface %s -fib %d", ROUTE_PATH, op,
71 inet_ntop(AF_INET6, dst, buf1, sizeof(buf1)), prefixlen,
72 iface, table);
73 }
74
75
77 bool status = openvpn_execve_check(&argv, NULL, 0, "ERROR: FreeBSD route command failed");
78
80
81 return (!status);
82}
83
84int
85net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen, const in_addr_t *gw,
86 const char *iface, uint32_t table, int metric)
87{
88 return net_route_v4("add", dst, prefixlen, gw, iface, table, metric);
89}
90
91int
92net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst, int prefixlen,
93 const struct in6_addr *gw, const char *iface, uint32_t table, int metric)
94{
95 return net_route_v6("add", dst, prefixlen, gw, iface, table, metric);
96}
97
98int
99net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen, const in_addr_t *gw,
100 const char *iface, uint32_t table, int metric)
101{
102 return net_route_v4("del", dst, prefixlen, gw, iface, table, metric);
103}
104
105int
106net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst, int prefixlen,
107 const struct in6_addr *gw, const char *iface, uint32_t table, int metric)
108{
109 return net_route_v6("del", dst, prefixlen, gw, iface, table, metric);
110}
111
112#endif /* if defined(TARGET_FREEBSD) */
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:438
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
static SERVICE_STATUS status
Definition interactive.c:51
void * openvpn_net_ctx_t
Definition networking.h:38
#define ASSERT(x)
Definition error.h:219
int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_message)
Definition argv.h:35
static char * iface