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 
11 static int
12 net_route_v4(const char *op, const in_addr_t *dst, int prefixlen,
13  const in_addr_t *gw, const char *iface, uint32_t table,
14  int metric)
15 {
16  char buf1[INET_ADDRSTRLEN], buf2[INET_ADDRSTRLEN];
17  in_addr_t _dst, _gw;
18  struct argv argv = argv_new();
19  bool status;
20 
21  _dst = ntohl(*dst);
22  _gw = ntohl(*gw);
23 
24  argv_printf(&argv, "%s %s -net %s/%d %s -fib %d",
25  ROUTE_PATH, op,
26  inet_ntop(AF_INET, &_dst, buf1, sizeof(buf1)),
27  prefixlen,
28  inet_ntop(AF_INET, &_gw, buf2, sizeof(buf2)),
29  table);
30 
31  argv_msg(M_INFO, &argv);
32  status = openvpn_execve_check(&argv, NULL, 0,
33  "ERROR: FreeBSD route command failed");
34 
35  argv_free(&argv);
36 
37  return (!status);
38 }
39 
40 static int
41 net_route_v6(const char *op, const struct in6_addr *dst,
42  int prefixlen, const struct in6_addr *gw, const char *iface,
43  uint32_t table, int metric)
44 {
45  char buf1[INET6_ADDRSTRLEN], buf2[INET6_ADDRSTRLEN];
46  struct argv argv = argv_new();
47  bool status;
48 
49  argv_printf(&argv, "%s -6 %s -net %s/%d %s -fib %d",
50  ROUTE_PATH, op,
51  inet_ntop(AF_INET6, dst, buf1, sizeof(buf1)),
52  prefixlen,
53  inet_ntop(AF_INET6, gw, buf2, sizeof(buf2)),
54  table);
55 
56  argv_msg(M_INFO, &argv);
57  status = openvpn_execve_check(&argv, NULL, 0,
58  "ERROR: FreeBSD route command failed");
59 
60  argv_free(&argv);
61 
62  return (!status);
63 }
64 
65 int
66 net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
67  const in_addr_t *gw, const char *iface, uint32_t table,
68  int metric)
69 {
70  return net_route_v4("add", dst, prefixlen, gw, iface, table, metric);
71 }
72 
73 int
74 net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
75  int prefixlen, const struct in6_addr *gw, const char *iface,
76  uint32_t table, int metric)
77 {
78  return net_route_v6("add", dst, prefixlen, gw, iface, table, metric);
79 }
80 
81 int
82 net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
83  const in_addr_t *gw, const char *iface, uint32_t table,
84  int metric)
85 {
86  return net_route_v4("del", dst, prefixlen, gw, iface, table, metric);
87 }
88 
89 int
90 net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
91  int prefixlen, const struct in6_addr *gw, const char *iface,
92  uint32_t table, int metric)
93 {
94  return net_route_v6("del", dst, prefixlen, gw, iface, table, metric);
95 }
96 
97 #endif /* if defined(TARGET_FREEBSD) */
M_INFO
#define M_INFO
Definition: errlevel.h:55
run_command.h
networking.h
ROUTE_PATH
#define ROUTE_PATH
Definition: config.h:513
argv
Definition: argv.h:35
argv_free
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
Definition: argv.c:102
openvpn_net_ctx_t
void * openvpn_net_ctx_t
Definition: networking.h:28
openvpn_execve_check
int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_message)
Definition: run_command.c:193
errlevel.h
iface
static char * iface
Definition: test_networking.c:7
syshead.h
argv_new
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
Definition: argv.c:88
argv_printf
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
Definition: argv.c:440
status
static SERVICE_STATUS status
Definition: interactive.c:53
argv_msg
void argv_msg(const int msglev, const struct argv *a)
Write the arguments stored in a struct argv via the msg() command.
Definition: argv.c:243
config.h