aboutsummaryrefslogtreecommitdiff
path: root/src/openvpn/networking_freebsd.c
blob: 1c94756e969ce4b138186b7b45d692e82cd167ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "syshead.h"
#include "errlevel.h"
#include "run_command.h"
#include "networking.h"

#if defined(TARGET_FREEBSD)

static int
net_route_v4(const char *op, const in_addr_t *dst, int prefixlen,
             const in_addr_t *gw, const char *iface, uint32_t table,
             int metric)
{
    char buf1[INET_ADDRSTRLEN], buf2[INET_ADDRSTRLEN];
    in_addr_t _dst, _gw;
    struct argv argv = argv_new();
    bool status;

    _dst = ntohl(*dst);
    _gw = ntohl(*gw);

    argv_printf(&argv, "%s %s -net %s/%d %s -fib %d",
                ROUTE_PATH, op,
                inet_ntop(AF_INET, &_dst, buf1, sizeof(buf1)),
                prefixlen,
                inet_ntop(AF_INET, &_gw, buf2, sizeof(buf2)),
                table);

    argv_msg(M_INFO, &argv);
    status = openvpn_execve_check(&argv, NULL, 0,
                                  "ERROR: FreeBSD route command failed");

    argv_free(&argv);

    return (!status);
}

static int
net_route_v6(const char *op, const struct in6_addr *dst,
             int prefixlen, const struct in6_addr *gw, const char *iface,
             uint32_t table, int metric)
{
    char buf1[INET6_ADDRSTRLEN], buf2[INET6_ADDRSTRLEN];
    struct argv argv = argv_new();
    bool status;

    argv_printf(&argv, "%s -6 %s -net %s/%d %s -fib %d",
                ROUTE_PATH, op,
                inet_ntop(AF_INET6, dst, buf1, sizeof(buf1)),
                prefixlen,
                inet_ntop(AF_INET6, gw, buf2, sizeof(buf2)),
                table);

    argv_msg(M_INFO, &argv);
    status = openvpn_execve_check(&argv, NULL, 0,
                                  "ERROR: FreeBSD route command failed");

    argv_free(&argv);

    return (!status);
}

int
net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
                 const in_addr_t *gw, const char *iface, uint32_t table,
                 int metric)
{
    return net_route_v4("add", dst, prefixlen, gw, iface, table, metric);
}

int
net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
                 int prefixlen, const struct in6_addr *gw, const char *iface,
                 uint32_t table, int metric)
{
    return net_route_v6("add", dst, prefixlen, gw, iface, table, metric);
}

int
net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
                 const in_addr_t *gw, const char *iface, uint32_t table,
                 int metric)
{
    return net_route_v4("del", dst, prefixlen, gw, iface, table, metric);
}

int
net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
                 int prefixlen, const struct in6_addr *gw, const char *iface,
                 uint32_t table, int metric)
{
    return net_route_v6("del", dst, prefixlen, gw, iface, table, metric);
}

#endif /* if defined(TARGET_FREEBSD) */