Browse code

introduce sitnl: Simplified Interface To NetLink

This patch introduces a tiny netlink interface, optimized
for the openvpn use case.

It basically exposes all those operations that are currently
handled by directly calling the /sbin/ip command (or even
ifconfig/route, if configured).

By using netlink, openvpn won't need to spawn new processes
when configuring the tun interface or routes.
This new approach will also allow openvpn to be granted
CAP_NET_ADMIN and be able to properly work even though it
dropped the root privileges (currently handled via workarounds).

By moving this logic into the sitnl module, tun.c and route.c
also benefit from some code simplification

Signed-off-by: Antonio Quartulli <a@unstable.cc>
Acked-by: Arne Schwabe <arne@rfc2549.org>
Message-Id: <20181219050118.6568-3-a@unstable.cc>
URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg18030.html
Signed-off-by: Gert Doering <gert@greenie.muc.de>

Antonio Quartulli authored on 2018/12/19 14:01:13
Showing 6 changed files
... ...
@@ -298,6 +298,7 @@ case "$host" in
298 298
 	*-*-linux*)
299 299
 		AC_DEFINE([TARGET_LINUX], [1], [Are we running on Linux?])
300 300
 		AC_DEFINE_UNQUOTED([TARGET_PREFIX], ["L"], [Target prefix])
301
+		have_sitnl="yes"
301 302
 		;;
302 303
 	*-*-solaris*)
303 304
 		AC_DEFINE([TARGET_SOLARIS], [1], [Are we running on Solaris?])
... ...
@@ -1226,11 +1227,13 @@ fi
1226 1226
 if test "${enable_iproute2}" = "yes"; then
1227 1227
 	test -z "${IPROUTE}" && AC_MSG_ERROR([ip utility is required but missing])
1228 1228
 	AC_DEFINE([ENABLE_IPROUTE], [1], [enable iproute2 support])
1229
-else
1230
-	if test "${WIN32}" != "yes"; then
1231
-		test -z "${ROUTE}" && AC_MSG_ERROR([route utility is required but missing])
1232
-		test -z "${IFCONFIG}" && AC_MSG_ERROR([ifconfig utility is required but missing])
1233
-	fi
1229
+else if test "${have_sitnl}" = "yes"; then
1230
+	AC_DEFINE([ENABLE_SITNL], [1], [enable sitnl support])
1231
+else if test "${WIN32}" != "yes" -a "${have_sitnl}" != "yes"; then
1232
+	test -z "${ROUTE}" && AC_MSG_ERROR([route utility is required but missing])
1233
+	test -z "${IFCONFIG}" && AC_MSG_ERROR([ifconfig utility is required but missing])
1234
+fi
1235
+fi
1234 1236
 fi
1235 1237
 
1236 1238
 if test "${enable_selinux}" = "yes"; then
... ...
@@ -80,7 +80,9 @@ openvpn_SOURCES = \
80 80
 	mtu.c mtu.h \
81 81
 	mudp.c mudp.h \
82 82
 	multi.c multi.h \
83
-	networking_iproute2.c networking_iproute2.h networking.h \
83
+	networking_iproute2.c networking_iproute2.h \
84
+	networking_sitnl.c networking_sitnl.h \
85
+	networking.h \
84 86
 	ntlm.c ntlm.h \
85 87
 	occ.c occ.h \
86 88
 	openssl_compat.h \
... ...
@@ -109,6 +109,7 @@
109 109
 
110 110
 #define D_LOG_RW             LOGLEV(5, 0,  0)        /* Print 'R' or 'W' to stdout for read/write */
111 111
 
112
+#define D_RTNL               LOGLEV(6, 68, M_DEBUG)  /* show RTNL low level operations */
112 113
 #define D_LINK_RW            LOGLEV(6, 69, M_DEBUG)  /* show TCP/UDP reads/writes (terse) */
113 114
 #define D_TUN_RW             LOGLEV(6, 69, M_DEBUG)  /* show TUN/TAP reads/writes */
114 115
 #define D_TAP_WIN_DEBUG      LOGLEV(6, 69, M_DEBUG)  /* show TAP-Windows driver debug info */
115 116
new file mode 100644
... ...
@@ -0,0 +1,1228 @@
0
+/*
1
+ *  Simplified Interface To NetLink
2
+ *
3
+ *  Copyright (C) 2016-2018 Antonio Quartulli <a@unstable.cc>
4
+ *
5
+ *  This program is free software; you can redistribute it and/or modify
6
+ *  it under the terms of the GNU General Public License version 2
7
+ *  as published by the Free Software Foundation.
8
+ *
9
+ *  This program is distributed in the hope that it will be useful,
10
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+ *  GNU General Public License for more details.
13
+ *
14
+ *  You should have received a copy of the GNU General Public License
15
+ *  along with this program (see the file COPYING included with this
16
+ *  distribution); if not, write to the Free Software Foundation, Inc.,
17
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
+ */
19
+
20
+#ifdef HAVE_CONFIG_H
21
+#include "config.h"
22
+#elif defined(_MSC_VER)
23
+#include "config-msvc.h"
24
+#endif
25
+
26
+#ifdef TARGET_LINUX
27
+
28
+#include "syshead.h"
29
+
30
+#include "errlevel.h"
31
+#include "buffer.h"
32
+#include "networking.h"
33
+
34
+#include <errno.h>
35
+#include <string.h>
36
+#include <unistd.h>
37
+#include <sys/types.h>
38
+#include <sys/socket.h>
39
+#include <linux/netlink.h>
40
+#include <linux/rtnetlink.h>
41
+
42
+#define SNDBUF_SIZE (1024 * 2)
43
+#define RCVBUF_SIZE (1024 * 4)
44
+
45
+#define SITNL_ADDATTR(_msg, _max_size, _attr, _data, _size)         \
46
+    {                                                               \
47
+        if (sitnl_addattr(_msg, _max_size, _attr, _data, _size) < 0)\
48
+        {                                                           \
49
+            goto err;                                               \
50
+        }                                                           \
51
+    }
52
+
53
+#define NLMSG_TAIL(nmsg) \
54
+    ((struct rtattr *)(((uint8_t *)(nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len)))
55
+
56
+/**
57
+ * Generic address data structure used to pass addresses and prefixes as
58
+ * argument to AF family agnostic functions
59
+ */
60
+typedef union {
61
+    in_addr_t ipv4;
62
+    struct in6_addr ipv6;
63
+} inet_address_t;
64
+
65
+/**
66
+ * Link state request message
67
+ */
68
+struct sitnl_link_req {
69
+    struct nlmsghdr n;
70
+    struct ifinfomsg i;
71
+    char buf[256];
72
+};
73
+
74
+/**
75
+ * Address request message
76
+ */
77
+struct sitnl_addr_req {
78
+    struct nlmsghdr n;
79
+    struct ifaddrmsg i;
80
+    char buf[256];
81
+};
82
+
83
+/**
84
+ * Route request message
85
+ */
86
+struct sitnl_route_req {
87
+    struct nlmsghdr n;
88
+    struct rtmsg r;
89
+    char buf[256];
90
+};
91
+
92
+typedef int (*sitnl_parse_reply_cb)(struct nlmsghdr *msg, void *arg);
93
+
94
+/**
95
+ * Object returned by route request operation
96
+ */
97
+struct sitnl_route_data_cb {
98
+    unsigned int iface;
99
+    inet_address_t gw;
100
+};
101
+
102
+/**
103
+ * Helper function used to easily add attributes to a rtnl message
104
+ */
105
+static int
106
+sitnl_addattr(struct nlmsghdr *n, int maxlen, int type, const void *data,
107
+              int alen)
108
+{
109
+    int len = RTA_LENGTH(alen);
110
+    struct rtattr *rta;
111
+
112
+    if ((int)(NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len)) > maxlen)
113
+    {
114
+        msg(M_WARN, "%s: rtnl: message exceeded bound of %d", __func__,
115
+            maxlen);
116
+        return -EMSGSIZE;
117
+    }
118
+
119
+    rta = NLMSG_TAIL(n);
120
+    rta->rta_type = type;
121
+    rta->rta_len = len;
122
+
123
+    if (!data)
124
+    {
125
+        memset(RTA_DATA(rta), 0, alen);
126
+    }
127
+    else
128
+    {
129
+        memcpy(RTA_DATA(rta), data, alen);
130
+    }
131
+
132
+    n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
133
+
134
+    return 0;
135
+}
136
+
137
+/**
138
+ * Open RTNL socket
139
+ */
140
+static int
141
+sitnl_socket(void)
142
+{
143
+    int sndbuf = SNDBUF_SIZE;
144
+    int rcvbuf = RCVBUF_SIZE;
145
+    int fd;
146
+
147
+    fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
148
+    if (fd < 0)
149
+    {
150
+        msg(M_WARN, "%s: cannot open netlink socket", __func__);
151
+        return fd;
152
+    }
153
+
154
+    if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) < 0)
155
+    {
156
+        msg(M_WARN | M_ERRNO, "%s: SO_SNDBUF", __func__);
157
+        close(fd);
158
+        return -1;
159
+    }
160
+
161
+    if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) < 0)
162
+    {
163
+        msg(M_WARN | M_ERRNO, "%s: SO_RCVBUF", __func__);
164
+        close(fd);
165
+        return -1;
166
+    }
167
+
168
+    return fd;
169
+}
170
+
171
+/**
172
+ * Bind socket to Netlink subsystem
173
+ */
174
+static int
175
+sitnl_bind(int fd, uint32_t groups)
176
+{
177
+    socklen_t addr_len;
178
+    struct sockaddr_nl local;
179
+
180
+    CLEAR(local);
181
+
182
+    local.nl_family = AF_NETLINK;
183
+    local.nl_groups = groups;
184
+
185
+    if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0)
186
+    {
187
+        msg(M_WARN | M_ERRNO, "%s: cannot bind netlink socket", __func__);
188
+        return -errno;
189
+    }
190
+
191
+    addr_len = sizeof(local);
192
+    if (getsockname(fd, (struct sockaddr *)&local, &addr_len) < 0)
193
+    {
194
+        msg(M_WARN | M_ERRNO, "%s: cannot getsockname", __func__);
195
+        return -errno;
196
+    }
197
+
198
+    if (addr_len != sizeof(local))
199
+    {
200
+        msg(M_WARN, "%s: wrong address length %d", __func__, addr_len);
201
+        return -EINVAL;
202
+    }
203
+
204
+    if (local.nl_family != AF_NETLINK)
205
+    {
206
+        msg(M_WARN, "%s: wrong address family %d", __func__, local.nl_family);
207
+        return -EINVAL;
208
+    }
209
+
210
+    return 0;
211
+}
212
+
213
+/**
214
+ * Send Netlink message and run callback on reply (if specified)
215
+ */
216
+static int
217
+sitnl_send(struct nlmsghdr *payload, pid_t peer, unsigned int groups,
218
+           sitnl_parse_reply_cb cb, void *arg_cb)
219
+{
220
+    int len, rem_len, fd, ret, rcv_len;
221
+    struct sockaddr_nl nladdr;
222
+    struct nlmsgerr *err;
223
+    struct nlmsghdr *h;
224
+    unsigned int seq;
225
+    char buf[1024 * 16];
226
+    struct iovec iov =
227
+    {
228
+        .iov_base = payload,
229
+        .iov_len = payload->nlmsg_len,
230
+    };
231
+    struct msghdr nlmsg =
232
+    {
233
+        .msg_name = &nladdr,
234
+        .msg_namelen = sizeof(nladdr),
235
+        .msg_iov = &iov,
236
+        .msg_iovlen = 1,
237
+    };
238
+
239
+    CLEAR(nladdr);
240
+
241
+    nladdr.nl_family = AF_NETLINK;
242
+    nladdr.nl_pid = peer;
243
+    nladdr.nl_groups = groups;
244
+
245
+    payload->nlmsg_seq = seq = time(NULL);
246
+
247
+    /* no need to send reply */
248
+    if (!cb)
249
+    {
250
+        payload->nlmsg_flags |= NLM_F_ACK;
251
+    }
252
+
253
+    fd = sitnl_socket();
254
+    if (fd < 0)
255
+    {
256
+        msg(M_WARN | M_ERRNO, "%s: can't open rtnl socket", __func__);
257
+        return -errno;
258
+    }
259
+
260
+    ret = sitnl_bind(fd, 0);
261
+    if (ret < 0)
262
+    {
263
+        msg(M_WARN | M_ERRNO, "%s: can't bind rtnl socket", __func__);
264
+        ret = -errno;
265
+        goto out;
266
+    }
267
+
268
+    ret = sendmsg(fd, &nlmsg, 0);
269
+    if (ret < 0)
270
+    {
271
+        msg(M_WARN | M_ERRNO, "%s: rtnl: error on sendmsg()", __func__);
272
+        ret = -errno;
273
+        goto out;
274
+    }
275
+
276
+    /* prepare buffer to store RTNL replies */
277
+    memset(buf, 0, sizeof(buf));
278
+    iov.iov_base = buf;
279
+
280
+    while (1)
281
+    {
282
+        /*
283
+         * iov_len is modified by recvmsg(), therefore has to be initialized before
284
+         * using it again
285
+         */
286
+        msg(D_RTNL, "%s: checking for received messages", __func__);
287
+        iov.iov_len = sizeof(buf);
288
+        rcv_len = recvmsg(fd, &nlmsg, 0);
289
+        msg(D_RTNL, "%s: rtnl: received %d bytes", __func__, rcv_len);
290
+        if (rcv_len < 0)
291
+        {
292
+            if ((errno == EINTR) || (errno == EAGAIN))
293
+            {
294
+                msg(D_RTNL, "%s: interrupted call", __func__);
295
+                continue;
296
+            }
297
+            msg(M_WARN | M_ERRNO, "%s: rtnl: error on recvmsg()", __func__);
298
+            ret = -errno;
299
+            goto out;
300
+        }
301
+
302
+        if (rcv_len == 0)
303
+        {
304
+            msg(M_WARN, "%s: rtnl: socket reached unexpected EOF", __func__);
305
+            ret = -EIO;
306
+            goto out;
307
+        }
308
+
309
+        if (nlmsg.msg_namelen != sizeof(nladdr))
310
+        {
311
+            msg(M_WARN, "%s: sender address length: %u (expected %zu)",
312
+                __func__, nlmsg.msg_namelen, sizeof(nladdr));
313
+            ret = -EIO;
314
+            goto out;
315
+        }
316
+
317
+        h = (struct nlmsghdr *)buf;
318
+        while (rcv_len >= (int)sizeof(*h))
319
+        {
320
+            len = h->nlmsg_len;
321
+            rem_len = len - sizeof(*h);
322
+
323
+            if ((rem_len < 0) || (len > rcv_len))
324
+            {
325
+                if (nlmsg.msg_flags & MSG_TRUNC)
326
+                {
327
+                    msg(M_WARN, "%s: truncated message", __func__);
328
+                    ret = -EIO;
329
+                    goto out;
330
+                }
331
+                msg(M_WARN, "%s: malformed message: len=%d", __func__, len);
332
+                ret = -EIO;
333
+                goto out;
334
+            }
335
+
336
+/*            if (((int)nladdr.nl_pid != peer) || (h->nlmsg_pid != nladdr.nl_pid)
337
+                || (h->nlmsg_seq != seq))
338
+            {
339
+                rcv_len -= NLMSG_ALIGN(len);
340
+                h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
341
+                msg(M_DEBUG, "%s: skipping unrelated message. nl_pid:%d (peer:%d) nl_msg_pid:%d nl_seq:%d seq:%d",
342
+                    __func__, (int)nladdr.nl_pid, peer, h->nlmsg_pid,
343
+                    h->nlmsg_seq, seq);
344
+                continue;
345
+            }
346
+*/
347
+            if (h->nlmsg_type == NLMSG_ERROR)
348
+            {
349
+                err = (struct nlmsgerr *)NLMSG_DATA(h);
350
+                if (rem_len < (int)sizeof(struct nlmsgerr))
351
+                {
352
+                    msg(M_WARN, "%s: ERROR truncated", __func__);
353
+                    ret = -EIO;
354
+                }
355
+                else
356
+                {
357
+                    if (!err->error)
358
+                    {
359
+                        ret = 0;
360
+                        if (cb)
361
+                            ret = cb(h, arg_cb);
362
+                    }
363
+                    else
364
+                    {
365
+                        msg(M_WARN, "%s: rtnl: generic error: %s",
366
+                            __func__, strerror(-err->error));
367
+                        ret = -err->error;
368
+                    }
369
+                }
370
+                goto out;
371
+            }
372
+
373
+            if (cb)
374
+            {
375
+                ret = cb(h, arg_cb);
376
+                goto out;
377
+            }
378
+            else
379
+            {
380
+                msg(M_WARN, "%s: RTNL: unexpected reply", __func__);
381
+            }
382
+
383
+            rcv_len -= NLMSG_ALIGN(len);
384
+            h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
385
+        }
386
+
387
+        if (nlmsg.msg_flags & MSG_TRUNC)
388
+        {
389
+            msg(M_WARN, "%s: message truncated", __func__);
390
+            continue;
391
+        }
392
+
393
+        if (rcv_len)
394
+        {
395
+            msg(M_WARN, "%s: rtnl: %d not parsed bytes", __func__, rcv_len);
396
+            ret = -1;
397
+            goto out;
398
+        }
399
+    }
400
+out:
401
+    close(fd);
402
+
403
+    return ret;
404
+}
405
+
406
+typedef struct {
407
+    int addr_size;
408
+    inet_address_t gw;
409
+    char iface[IFNAMSIZ];
410
+} route_res_t;
411
+
412
+static int
413
+sitnl_route_save(struct nlmsghdr *n, void *arg)
414
+{
415
+    route_res_t *res = arg;
416
+    struct rtmsg *r = NLMSG_DATA(n);
417
+    struct rtattr *rta = RTM_RTA(r);
418
+    int len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
419
+    unsigned int ifindex = 0;
420
+
421
+    while (RTA_OK(rta, len))
422
+    {
423
+        switch (rta->rta_type)
424
+        {
425
+            /* route interface */
426
+            case RTA_OIF:
427
+                ifindex = *(unsigned int *)RTA_DATA(rta);
428
+                break;
429
+            /* route prefix */
430
+            case RTA_DST:
431
+                break;
432
+            /* GW for the route */
433
+            case RTA_GATEWAY:
434
+                memcpy(&res->gw, RTA_DATA(rta), res->addr_size);
435
+                break;
436
+        }
437
+
438
+        rta = RTA_NEXT(rta, len);
439
+    }
440
+
441
+    if (!if_indextoname(ifindex, res->iface))
442
+    {
443
+        msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifname for index %d",
444
+            __func__, ifindex);
445
+        return -1;
446
+    }
447
+
448
+    return 0;
449
+}
450
+
451
+static int
452
+sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst,
453
+                    int prefixlen, void *best_gw, char *best_iface)
454
+{
455
+    struct sitnl_route_req req;
456
+    route_res_t res;
457
+    int ret = -EINVAL;
458
+
459
+    ASSERT(best_gw);
460
+    ASSERT(best_iface);
461
+
462
+    CLEAR(req);
463
+    CLEAR(res);
464
+
465
+    req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r));
466
+    req.n.nlmsg_type = RTM_GETROUTE;
467
+    req.n.nlmsg_flags = NLM_F_REQUEST;
468
+
469
+    req.r.rtm_family = af_family;
470
+    req.r.rtm_dst_len = prefixlen;
471
+
472
+    switch (af_family)
473
+    {
474
+        case AF_INET:
475
+            res.addr_size = sizeof(in_addr_t);
476
+            req.n.nlmsg_flags |= NLM_F_DUMP;
477
+            break;
478
+        case AF_INET6:
479
+            res.addr_size = sizeof(struct in6_addr);
480
+            break;
481
+        default:
482
+            /* unsupported */
483
+            return -EINVAL;
484
+    }
485
+
486
+    SITNL_ADDATTR(&req.n, sizeof(req), RTA_DST, dst, res.addr_size);
487
+
488
+    ret = sitnl_send(&req.n, 0, 0, sitnl_route_save, &res);
489
+    if (ret < 0)
490
+    {
491
+        goto err;
492
+    }
493
+
494
+    /* save result in output variables */
495
+    memcpy(best_gw, &res.gw, res.addr_size);
496
+    strcpy(best_iface, res.iface);
497
+err:
498
+    return ret;
499
+
500
+}
501
+
502
+/* used by iproute2 implementation too */
503
+int
504
+net_route_v6_best_gw(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
505
+                     int prefixlen, struct in6_addr *best_gw, char *best_iface)
506
+{
507
+    inet_address_t dst_v6 = {0};
508
+    char buf[INET6_ADDRSTRLEN];
509
+    int ret;
510
+
511
+    if (dst)
512
+    {
513
+        dst_v6.ipv6 = *dst;
514
+    }
515
+
516
+    msg(D_ROUTE, "%s query: dst %s/%d", __func__,
517
+        inet_ntop(AF_INET6, &dst_v6.ipv6, buf, sizeof(buf)), prefixlen);
518
+
519
+    ret = sitnl_route_best_gw(AF_INET6, &dst_v6, prefixlen, best_gw,
520
+                              best_iface);
521
+    if (ret < 0)
522
+    {
523
+        return ret;
524
+    }
525
+
526
+    msg(D_ROUTE, "%s result: via %s dev %s", __func__,
527
+        inet_ntop(AF_INET6, best_gw, buf, sizeof(buf)), best_iface);
528
+
529
+    return ret;
530
+
531
+}
532
+
533
+#ifdef ENABLE_SITNL
534
+
535
+int
536
+net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)
537
+{
538
+    (void)c;
539
+    (void)ctx;
540
+
541
+    return 0;
542
+}
543
+
544
+int
545
+net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst,
546
+                     int prefixlen, in_addr_t *best_gw, char *best_iface)
547
+{
548
+    inet_address_t dst_v4 = {0};
549
+    char buf[INET_ADDRSTRLEN];
550
+    int ret;
551
+
552
+    if (dst)
553
+    {
554
+        dst_v4.ipv4 = htonl(*dst);
555
+    }
556
+
557
+    msg(D_ROUTE, "%s query: dst %s/%d", __func__,
558
+        inet_ntop(AF_INET, &dst_v4.ipv4, buf, sizeof(buf)), prefixlen);
559
+
560
+    ret = sitnl_route_best_gw(AF_INET, &dst_v4, prefixlen, best_gw, best_iface);
561
+    if (ret < 0)
562
+    {
563
+        return ret;
564
+    }
565
+
566
+    msg(D_ROUTE, "%s result: via %s dev %s", __func__,
567
+        inet_ntop(AF_INET, best_gw, buf, sizeof(buf)), best_iface);
568
+
569
+    /* result is expected in Host Order */
570
+    *best_gw = ntohl(*best_gw);
571
+
572
+    return ret;
573
+}
574
+
575
+int
576
+net_iface_up(openvpn_net_ctx_t *ctx, const char *iface, bool up)
577
+{
578
+    struct sitnl_link_req req;
579
+    int ifindex;
580
+
581
+    CLEAR(req);
582
+
583
+    if (!iface)
584
+    {
585
+        msg(M_WARN, "%s: passed NULL interface", __func__);
586
+        return -EINVAL;
587
+    }
588
+
589
+    ifindex = if_nametoindex(iface);
590
+    if (ifindex == 0) {
591
+        msg(M_WARN, "%s: rtnl: cannot get ifindex for %s: %s", __func__, iface,
592
+            strerror(errno));
593
+        return -ENOENT;
594
+    }
595
+
596
+    req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
597
+    req.n.nlmsg_flags = NLM_F_REQUEST;
598
+    req.n.nlmsg_type = RTM_NEWLINK;
599
+
600
+    req.i.ifi_family = AF_PACKET;
601
+    req.i.ifi_index = ifindex;
602
+    req.i.ifi_change |= IFF_UP;
603
+    if (up)
604
+        req.i.ifi_flags |= IFF_UP;
605
+    else
606
+        req.i.ifi_flags &= ~IFF_UP;
607
+
608
+    msg(M_INFO, "%s: set %s %s", __func__, iface, up ? "up" : "down");
609
+
610
+    return sitnl_send(&req.n, 0, 0, NULL, NULL);
611
+}
612
+
613
+int
614
+net_iface_mtu_set(openvpn_net_ctx_t *ctx, const char *iface,
615
+                  uint32_t mtu)
616
+{
617
+    struct sitnl_link_req req;
618
+    int ifindex, ret = -1;
619
+
620
+    CLEAR(req);
621
+
622
+    ifindex = if_nametoindex(iface);
623
+    if (ifindex == 0) {
624
+        msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __func__,
625
+            iface);
626
+        return -1;
627
+    }
628
+
629
+    req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
630
+    req.n.nlmsg_flags = NLM_F_REQUEST;
631
+    req.n.nlmsg_type = RTM_NEWLINK;
632
+
633
+    req.i.ifi_family = AF_PACKET;
634
+    req.i.ifi_index = ifindex;
635
+
636
+    SITNL_ADDATTR(&req.n, sizeof(req), IFLA_MTU, &mtu, 4);
637
+
638
+    msg(M_INFO, "%s: mtu %u for %s", __func__, mtu, iface);
639
+
640
+    ret = sitnl_send(&req.n, 0, 0, NULL, NULL);
641
+err:
642
+    return ret;
643
+}
644
+
645
+static int
646
+sitnl_addr_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family,
647
+               const inet_address_t *local, const inet_address_t *remote,
648
+               int prefixlen, const inet_address_t *broadcast)
649
+{
650
+    struct sitnl_addr_req req;
651
+    uint32_t size;
652
+    int ret = -EINVAL;
653
+
654
+    CLEAR(req);
655
+
656
+    req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
657
+    req.n.nlmsg_type = cmd;
658
+    req.n.nlmsg_flags = NLM_F_REQUEST | flags;
659
+
660
+    req.i.ifa_index = ifindex;
661
+    req.i.ifa_family = af_family;
662
+
663
+    switch (af_family)
664
+    {
665
+        case AF_INET:
666
+            size = sizeof(struct in_addr);
667
+            break;
668
+        case AF_INET6:
669
+            size = sizeof(struct in6_addr);
670
+            break;
671
+        default:
672
+            msg(M_WARN, "%s: rtnl: unknown address family %d", __func__,
673
+                af_family);
674
+            return -EINVAL;
675
+    }
676
+
677
+    /* if no prefixlen has been specified, assume host address */
678
+    if (prefixlen == 0)
679
+    {
680
+        prefixlen = size * 8;
681
+    }
682
+    req.i.ifa_prefixlen = prefixlen;
683
+
684
+    if (remote)
685
+    {
686
+        SITNL_ADDATTR(&req.n, sizeof(req), IFA_ADDRESS, remote, size);
687
+    }
688
+
689
+    if (local)
690
+    {
691
+        SITNL_ADDATTR(&req.n, sizeof(req), IFA_LOCAL, local, size);
692
+    }
693
+
694
+    if (broadcast)
695
+    {
696
+        SITNL_ADDATTR(&req.n, sizeof(req), IFA_BROADCAST, broadcast, size);
697
+    }
698
+
699
+    ret = sitnl_send(&req.n, 0, 0, NULL, NULL);
700
+    if ((ret < 0) && (errno == EEXIST))
701
+    {
702
+        ret = 0;
703
+    }
704
+err:
705
+    return ret;
706
+}
707
+
708
+static int
709
+sitnl_addr_ptp_add(sa_family_t af_family, const char *iface,
710
+                   const inet_address_t *local,
711
+                   const inet_address_t *remote)
712
+{
713
+    int ifindex;
714
+
715
+    switch (af_family) {
716
+        case AF_INET:
717
+        case AF_INET6:
718
+            break;
719
+        default:
720
+            return -EINVAL;
721
+    }
722
+
723
+    if (!iface)
724
+    {
725
+        msg(M_WARN, "%s: passed NULL interface", __func__);
726
+        return -EINVAL;
727
+    }
728
+
729
+    ifindex = if_nametoindex(iface);
730
+    if (ifindex == 0)
731
+    {
732
+        msg(M_WARN, "%s: cannot get ifindex for %s: %s", __func__, np(iface),
733
+            strerror(errno));
734
+        return -ENOENT;
735
+    }
736
+
737
+    return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, ifindex,
738
+                          af_family, local, remote, 0, NULL);
739
+}
740
+
741
+static int
742
+sitnl_addr_ptp_del(sa_family_t af_family, const char *iface,
743
+                   const inet_address_t *local)
744
+{
745
+    int ifindex;
746
+
747
+    switch (af_family) {
748
+        case AF_INET:
749
+        case AF_INET6:
750
+            break;
751
+        default:
752
+            return -EINVAL;
753
+    }
754
+
755
+    if (!iface)
756
+    {
757
+        msg(M_WARN, "%s: passed NULL interface", __func__);
758
+        return -EINVAL;
759
+    }
760
+
761
+    ifindex = if_nametoindex(iface);
762
+    if (ifindex == 0)
763
+    {
764
+        msg(M_WARN | M_ERRNO, "%s: cannot get ifindex for %s", __func__, iface);
765
+        return -ENOENT;
766
+    }
767
+
768
+    return sitnl_addr_set(RTM_DELADDR, 0, ifindex, af_family, local, NULL, 0,
769
+                          NULL);
770
+}
771
+
772
+static int
773
+sitnl_route_set(int cmd, uint32_t flags, int ifindex, sa_family_t af_family,
774
+                const void *dst, int prefixlen,
775
+                const void *gw, enum rt_class_t table, int metric,
776
+                enum rt_scope_t scope, int protocol, int type)
777
+{
778
+    struct sitnl_route_req req;
779
+    int ret = -1, size;
780
+
781
+    CLEAR(req);
782
+
783
+    switch (af_family)
784
+    {
785
+        case AF_INET:
786
+            size = sizeof(in_addr_t);
787
+            break;
788
+        case AF_INET6:
789
+            size = sizeof(struct in6_addr);
790
+            break;
791
+        default:
792
+            return -EINVAL;
793
+    }
794
+
795
+    req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r));
796
+    req.n.nlmsg_type = cmd;
797
+    req.n.nlmsg_flags = NLM_F_REQUEST | flags;
798
+
799
+    req.r.rtm_family = af_family;
800
+    req.r.rtm_scope = scope;
801
+    req.r.rtm_protocol = protocol;
802
+    req.r.rtm_type = type;
803
+    req.r.rtm_dst_len = prefixlen;
804
+
805
+    if (table < 256)
806
+    {
807
+        req.r.rtm_table = table;
808
+    }
809
+    else
810
+    {
811
+        req.r.rtm_table = RT_TABLE_UNSPEC;
812
+        SITNL_ADDATTR(&req.n, sizeof(req), RTA_TABLE, &table, 4);
813
+    }
814
+
815
+    if (dst)
816
+    {
817
+        SITNL_ADDATTR(&req.n, sizeof(req), RTA_DST, dst, size);
818
+    }
819
+
820
+    if (gw)
821
+    {
822
+        SITNL_ADDATTR(&req.n, sizeof(req), RTA_GATEWAY, gw, size);
823
+    }
824
+
825
+    if (ifindex > 0)
826
+    {
827
+        SITNL_ADDATTR(&req.n, sizeof(req), RTA_OIF, &ifindex, 4);
828
+    }
829
+
830
+    if (metric > 0)
831
+    {
832
+        SITNL_ADDATTR(&req.n, sizeof(req), RTA_PRIORITY, &metric, 4);
833
+    }
834
+
835
+    ret = sitnl_send(&req.n, 0, 0, NULL, NULL);
836
+    if ((ret < 0) && (errno == EEXIST))
837
+    {
838
+        ret = 0;
839
+    }
840
+err:
841
+    return ret;
842
+}
843
+
844
+static int
845
+sitnl_addr_add(sa_family_t af_family, const char *iface,
846
+               const inet_address_t *addr, int prefixlen,
847
+               const inet_address_t *broadcast)
848
+{
849
+    int ifindex;
850
+
851
+    switch (af_family) {
852
+        case AF_INET:
853
+        case AF_INET6:
854
+            break;
855
+        default:
856
+            return -EINVAL;;
857
+    }
858
+
859
+    if (!iface)
860
+    {
861
+        msg(M_WARN, "%s: passed NULL interface", __func__);
862
+        return -EINVAL;
863
+    }
864
+
865
+    ifindex = if_nametoindex(iface);
866
+    if (ifindex == 0)
867
+    {
868
+        msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __func__,
869
+            iface);
870
+        return -ENOENT;
871
+    }
872
+
873
+    return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, ifindex,
874
+                          af_family, addr, NULL, prefixlen, broadcast);
875
+}
876
+
877
+static int
878
+sitnl_addr_del(sa_family_t af_family, const char *iface, inet_address_t *addr,
879
+               int prefixlen)
880
+{
881
+    int ifindex;
882
+
883
+    switch (af_family) {
884
+        case AF_INET:
885
+        case AF_INET6:
886
+            break;
887
+        default:
888
+            return -EINVAL;;
889
+    }
890
+
891
+    if (!iface)
892
+    {
893
+        msg(M_WARN, "%s: passed NULL interface", __func__);
894
+        return -EINVAL;
895
+    }
896
+
897
+    ifindex = if_nametoindex(iface);
898
+    if (ifindex == 0)
899
+    {
900
+        msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __func__,
901
+            iface);
902
+        return -ENOENT;
903
+    }
904
+
905
+    return sitnl_addr_set(RTM_DELADDR, 0, ifindex, af_family, addr, NULL,
906
+                          prefixlen, NULL);
907
+}
908
+
909
+int
910
+net_addr_v4_add(openvpn_net_ctx_t *ctx, const char *iface,
911
+                const in_addr_t *addr, int prefixlen,
912
+                const in_addr_t *broadcast)
913
+{
914
+    inet_address_t addr_v4 = { 0 };
915
+    inet_address_t brd_v4 = { 0 };
916
+    char buf1[INET_ADDRSTRLEN];
917
+    char buf2[INET_ADDRSTRLEN];
918
+
919
+    if (!addr)
920
+    {
921
+        return -EINVAL;
922
+    }
923
+
924
+    addr_v4.ipv4 = htonl(*addr);
925
+
926
+    if (broadcast)
927
+    {
928
+        brd_v4.ipv4 = htonl(*broadcast);
929
+    }
930
+
931
+    msg(M_INFO, "%s: %s/%d brd %s dev %s", __func__,
932
+        inet_ntop(AF_INET, &addr_v4.ipv4, buf1, sizeof(buf1)), prefixlen,
933
+        inet_ntop(AF_INET, &brd_v4.ipv4, buf2, sizeof(buf2)), iface);
934
+
935
+    return sitnl_addr_add(AF_INET, iface, &addr_v4, prefixlen, &brd_v4);
936
+}
937
+
938
+int
939
+net_addr_v6_add(openvpn_net_ctx_t *ctx, const char *iface,
940
+                const struct in6_addr *addr, int prefixlen)
941
+{
942
+    inet_address_t addr_v6 = { 0 };
943
+    char buf[INET6_ADDRSTRLEN];
944
+
945
+    if (!addr)
946
+    {
947
+        return -EINVAL;
948
+    }
949
+
950
+    addr_v6.ipv6 = *addr;
951
+
952
+    msg(M_INFO, "%s: %s/%d dev %s", __func__,
953
+        inet_ntop(AF_INET6, &addr_v6.ipv6, buf, sizeof(buf)), prefixlen, iface);
954
+
955
+    return sitnl_addr_add(AF_INET6, iface, &addr_v6, prefixlen, NULL);
956
+}
957
+
958
+int
959
+net_addr_v4_del(openvpn_net_ctx_t *ctx, const char *iface,
960
+                const in_addr_t *addr, int prefixlen)
961
+{
962
+    inet_address_t addr_v4 = { 0 };
963
+    char buf[INET_ADDRSTRLEN];
964
+
965
+    if (!addr)
966
+    {
967
+        return -EINVAL;
968
+    }
969
+
970
+    addr_v4.ipv4 = htonl(*addr);
971
+
972
+    msg(M_INFO, "%s: %s dev %s", __func__,
973
+        inet_ntop(AF_INET, &addr_v4.ipv4, buf, sizeof(buf)), iface);
974
+
975
+    return sitnl_addr_del(AF_INET, iface, &addr_v4, prefixlen);
976
+}
977
+
978
+int
979
+net_addr_v6_del(openvpn_net_ctx_t *ctx, const char *iface,
980
+                const struct in6_addr *addr, int prefixlen)
981
+{
982
+    inet_address_t addr_v6 = { 0 };
983
+    char buf[INET6_ADDRSTRLEN];
984
+
985
+    if (!addr)
986
+    {
987
+        return -EINVAL;
988
+    }
989
+
990
+    addr_v6.ipv6 = *addr;
991
+
992
+    msg(M_INFO, "%s: %s/%d dev %s", __func__,
993
+        inet_ntop(AF_INET6, &addr_v6.ipv6, buf, sizeof(buf)), prefixlen, iface);
994
+
995
+    return sitnl_addr_del(AF_INET6, iface, &addr_v6, prefixlen);
996
+}
997
+
998
+int
999
+net_addr_ptp_v4_add(openvpn_net_ctx_t *ctx, const char *iface,
1000
+                    const in_addr_t *local, const in_addr_t *remote)
1001
+{
1002
+    inet_address_t local_v4 = { 0 };
1003
+    inet_address_t remote_v4 = { 0 };
1004
+    char buf1[INET_ADDRSTRLEN];
1005
+    char buf2[INET_ADDRSTRLEN];
1006
+
1007
+    if (!local)
1008
+    {
1009
+        return -EINVAL;
1010
+    }
1011
+
1012
+    local_v4.ipv4 = htonl(*local);
1013
+
1014
+    if (remote)
1015
+    {
1016
+        remote_v4.ipv4 = htonl(*remote);
1017
+    }
1018
+
1019
+    msg(M_INFO, "%s: %s peer %s dev %s", __func__,
1020
+        inet_ntop(AF_INET, &local_v4.ipv4, buf1, sizeof(buf1)),
1021
+        inet_ntop(AF_INET, &remote_v4.ipv4, buf2, sizeof(buf2)), iface);
1022
+
1023
+    return sitnl_addr_ptp_add(AF_INET, iface, &local_v4, &remote_v4);
1024
+}
1025
+
1026
+int
1027
+net_addr_ptp_v4_del(openvpn_net_ctx_t *ctx, const char *iface,
1028
+                    const in_addr_t *local, const in_addr_t *remote)
1029
+{
1030
+    inet_address_t local_v4 = { 0 };
1031
+    char buf[INET6_ADDRSTRLEN];
1032
+
1033
+
1034
+    if (!local)
1035
+    {
1036
+        return -EINVAL;
1037
+    }
1038
+
1039
+    local_v4.ipv4 = htonl(*local);
1040
+
1041
+    msg(M_INFO, "%s: %s dev %s", __func__,
1042
+        inet_ntop(AF_INET, &local_v4.ipv4, buf, sizeof(buf)), iface);
1043
+
1044
+    return sitnl_addr_ptp_del(AF_INET, iface, &local_v4);
1045
+}
1046
+
1047
+static int
1048
+sitnl_route_add(const char *iface, sa_family_t af_family, const void *dst,
1049
+                int prefixlen, const void *gw, uint32_t table, int metric)
1050
+{
1051
+    enum rt_scope_t scope = RT_SCOPE_UNIVERSE;
1052
+    int ifindex = 0;
1053
+
1054
+    if (iface)
1055
+    {
1056
+        ifindex = if_nametoindex(iface);
1057
+        if (ifindex == 0)
1058
+        {
1059
+            msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifindex for %s",
1060
+                __func__, iface);
1061
+            return -ENOENT;
1062
+        }
1063
+    }
1064
+
1065
+    if (table == 0)
1066
+    {
1067
+        table = RT_TABLE_MAIN;
1068
+    }
1069
+
1070
+    if (!gw && iface)
1071
+    {
1072
+        scope = RT_SCOPE_LINK;
1073
+    }
1074
+
1075
+    return sitnl_route_set(RTM_NEWROUTE, NLM_F_CREATE | NLM_F_REPLACE, ifindex,
1076
+                           af_family, dst, prefixlen, gw, table, metric, scope,
1077
+                           RTPROT_BOOT, RTN_UNICAST);
1078
+}
1079
+
1080
+int
1081
+net_route_v4_add(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
1082
+                 const in_addr_t *gw, const char *iface,
1083
+                 uint32_t table, int metric)
1084
+{
1085
+    in_addr_t *dst_ptr = NULL, *gw_ptr = NULL;
1086
+    in_addr_t dst_be = 0, gw_be = 0;
1087
+    char dst_str[INET_ADDRSTRLEN];
1088
+    char gw_str[INET_ADDRSTRLEN];
1089
+
1090
+    if (dst)
1091
+    {
1092
+        dst_be = htonl(*dst);
1093
+        dst_ptr = &dst_be;
1094
+    }
1095
+
1096
+    if (gw)
1097
+    {
1098
+        gw_be = htonl(*gw);
1099
+        gw_ptr = &gw_be;
1100
+    }
1101
+
1102
+    msg(D_ROUTE, "%s: %s/%d via %s dev %s table %d metric %d", __func__,
1103
+        inet_ntop(AF_INET, &dst_be, dst_str, sizeof(dst_str)),
1104
+        prefixlen, inet_ntop(AF_INET, &gw_be, gw_str, sizeof(gw_str)),
1105
+        np(iface), table, metric);
1106
+
1107
+    return sitnl_route_add(iface, AF_INET, dst_ptr, prefixlen, gw_ptr, table,
1108
+                           metric);
1109
+}
1110
+
1111
+int
1112
+net_route_v6_add(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
1113
+                 int prefixlen, const struct in6_addr *gw,
1114
+                 const char *iface, uint32_t table, int metric)
1115
+{
1116
+    inet_address_t dst_v6 = { 0 };
1117
+    inet_address_t gw_v6 = { 0 };
1118
+    char dst_str[INET6_ADDRSTRLEN];
1119
+    char gw_str[INET6_ADDRSTRLEN];
1120
+
1121
+    if (dst)
1122
+    {
1123
+        dst_v6.ipv6 = *dst;
1124
+    }
1125
+
1126
+    if (gw)
1127
+    {
1128
+        gw_v6.ipv6 = *gw;
1129
+    }
1130
+
1131
+    msg(D_ROUTE, "%s: %s/%d via %s dev %s table %d metric %d", __func__,
1132
+        inet_ntop(AF_INET6, &dst_v6.ipv6, dst_str, sizeof(dst_str)),
1133
+        prefixlen, inet_ntop(AF_INET6, &gw_v6.ipv6, gw_str, sizeof(gw_str)),
1134
+        np(iface), table, metric);
1135
+
1136
+    return sitnl_route_add(iface, AF_INET6, dst, prefixlen, gw, table,
1137
+                           metric);
1138
+}
1139
+
1140
+static int
1141
+sitnl_route_del(const char *iface, sa_family_t af_family, inet_address_t *dst,
1142
+                int prefixlen, inet_address_t *gw, uint32_t table,
1143
+                int metric)
1144
+{
1145
+    int ifindex = 0;
1146
+
1147
+    if (iface)
1148
+    {
1149
+        ifindex = if_nametoindex(iface);
1150
+        if (ifindex == 0)
1151
+        {
1152
+            msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifindex for %s",
1153
+                __func__, iface);
1154
+            return -ENOENT;
1155
+        }
1156
+    }
1157
+
1158
+    if (table == 0)
1159
+    {
1160
+        table = RT_TABLE_MAIN;
1161
+    }
1162
+
1163
+    return sitnl_route_set(RTM_DELROUTE, 0, ifindex, af_family, dst, prefixlen,
1164
+                           gw, table, metric, RT_SCOPE_NOWHERE, 0, 0);
1165
+}
1166
+
1167
+int
1168
+net_route_v4_del(openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen,
1169
+                 const in_addr_t *gw, const char *iface, uint32_t table,
1170
+                 int metric)
1171
+{
1172
+    inet_address_t dst_v4 = { 0 };
1173
+    inet_address_t gw_v4 = { 0 };
1174
+    char dst_str[INET_ADDRSTRLEN];
1175
+    char gw_str[INET_ADDRSTRLEN];
1176
+
1177
+    if (dst)
1178
+    {
1179
+        dst_v4.ipv4 = htonl(*dst);
1180
+    }
1181
+
1182
+    if (gw)
1183
+    {
1184
+        gw_v4.ipv4 = htonl(*gw);
1185
+    }
1186
+
1187
+    msg(D_ROUTE, "%s: %s/%d via %s dev %s table %d metric %d", __func__,
1188
+        inet_ntop(AF_INET, &dst_v4.ipv4, dst_str, sizeof(dst_str)),
1189
+        prefixlen, inet_ntop(AF_INET, &gw_v4.ipv4, gw_str, sizeof(gw_str)),
1190
+        np(iface), table, metric);
1191
+
1192
+    return sitnl_route_del(iface, AF_INET, &dst_v4, prefixlen, &gw_v4, table,
1193
+                           metric);
1194
+}
1195
+
1196
+int
1197
+net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst,
1198
+                 int prefixlen, const struct in6_addr *gw,
1199
+                 const char *iface, uint32_t table, int metric)
1200
+{
1201
+    inet_address_t dst_v6 = { 0 };
1202
+    inet_address_t gw_v6 = { 0 };
1203
+    char dst_str[INET6_ADDRSTRLEN];
1204
+    char gw_str[INET6_ADDRSTRLEN];
1205
+
1206
+    if (dst)
1207
+    {
1208
+        dst_v6.ipv6 = *dst;
1209
+    }
1210
+
1211
+    if (gw)
1212
+    {
1213
+        gw_v6.ipv6 = *gw;
1214
+    }
1215
+
1216
+    msg(D_ROUTE, "%s: %s/%d via %s dev %s table %d metric %d", __func__,
1217
+        inet_ntop(AF_INET6, &dst_v6.ipv6, dst_str, sizeof(dst_str)),
1218
+        prefixlen, inet_ntop(AF_INET6, &gw_v6.ipv6, gw_str, sizeof(gw_str)),
1219
+        np(iface), table, metric);
1220
+
1221
+    return sitnl_route_del(iface, AF_INET6, &dst_v6, prefixlen, &gw_v6,
1222
+                           table, metric);
1223
+}
1224
+
1225
+#endif /* !ENABLE_SITNL */
1226
+
1227
+#endif /* TARGET_LINUX */
0 1228
new file mode 100644
... ...
@@ -0,0 +1,28 @@
0
+/*
1
+ *  Generic interface to platform specific networking code
2
+ *
3
+ *  Copyright (C) 2016-2018 Antonio Quartulli <a@unstable.cc>
4
+ *
5
+ *  This program is free software; you can redistribute it and/or modify
6
+ *  it under the terms of the GNU General Public License version 2
7
+ *  as published by the Free Software Foundation.
8
+ *
9
+ *  This program is distributed in the hope that it will be useful,
10
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+ *  GNU General Public License for more details.
13
+ *
14
+ *  You should have received a copy of the GNU General Public License
15
+ *  along with this program (see the file COPYING included with this
16
+ *  distribution); if not, write to the Free Software Foundation, Inc.,
17
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
+ */
19
+
20
+
21
+#ifndef NETWORKING_SITNL_H_
22
+#define NETWORKING_SITNL_H_
23
+
24
+typedef char openvpn_net_iface_t;
25
+typedef void * openvpn_net_ctx_t;
26
+
27
+#endif /* NETWORKING_SITNL_H_ */
0 28
new file mode 100644
... ...
@@ -0,0 +1,217 @@
0
+/*
1
+ *  Simplified Interface To NetLink
2
+ *
3
+ *  Copyright (C) 2016-2018 Antonio Quartulli <a@unstable.cc>
4
+ *
5
+ *  This program is free software; you can redistribute it and/or modify
6
+ *  it under the terms of the GNU General Public License version 2
7
+ *  as published by the Free Software Foundation.
8
+ *
9
+ *  This program is distributed in the hope that it will be useful,
10
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
+ *  GNU General Public License for more details.
13
+ *
14
+ *  You should have received a copy of the GNU General Public License
15
+ *  along with this program (see the file COPYING included with this
16
+ *  distribution); if not, write to the Free Software Foundation, Inc.,
17
+ *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
+ */
19
+
20
+#ifndef SITNL_H_
21
+#define SITNL_H_
22
+
23
+#ifdef TARGET_LINUX
24
+
25
+#include <stdbool.h>
26
+#include <netinet/in.h>
27
+
28
+/**
29
+ * Bring interface up or down.
30
+ *
31
+ * @param iface     the interface to modify
32
+ * @param up        true if the interface has to be brought up, false otherwise
33
+ *
34
+ * @return          0 on success, a negative error code otherwise
35
+ */
36
+int sitnl_iface_up(const char *iface, bool up);
37
+
38
+/**
39
+ * Set the MTU for an interface
40
+ *
41
+ * @param iface     the interface to modify
42
+ * @param mtru      the new MTU
43
+ *
44
+ * @return          0 on success, a negative error code otherwise
45
+ */
46
+int sitnl_iface_mtu_set(const char *iface, uint32_t mtu);
47
+
48
+/**
49
+ * Add an IPv4 address to an interface
50
+ *
51
+ * @param iface     the interface where the address has to be added
52
+ * @param addr      the address to add
53
+ * @param prefixlen the prefix length of the network associated with the address
54
+ * @param broadcast the broadcast address to configure on the interface
55
+ *
56
+ * @return          0 on success, a negative error code otherwise
57
+ */
58
+int sitnl_addr_v4_add(const char *iface, const in_addr_t *addr, int prefixlen,
59
+                      const in_addr_t *broadcast);
60
+
61
+/**
62
+ * Add an IPv6 address to an interface
63
+ *
64
+ * @param iface     the interface where the address has to be added
65
+ * @param addr      the address to add
66
+ * @param prefixlen the prefix length of the network associated with the address
67
+ *
68
+ * @return          0 on success, a negative error code otherwise
69
+ */
70
+
71
+int sitnl_addr_v6_add(const char *iface, const struct in6_addr *addr,
72
+                      int prefixlen);
73
+
74
+/**
75
+ * Remove an IPv4 from an interface
76
+ *
77
+ * @param iface     the interface to remove the address from
78
+ * @param prefixlen the prefix length of the network associated with the address
79
+ *
80
+ * @return          0 on success, a negative error code otherwise
81
+ */
82
+int sitnl_addr_v4_del(const char *iface, const in_addr_t *addr, int prefixlen);
83
+
84
+/**
85
+ * Remove an IPv6 from an interface
86
+ *
87
+ * @param iface     the interface to remove the address from
88
+ * @param prefixlen the prefix length of the network associated with the address
89
+ *
90
+ * @return          0 on success, a negative error code otherwise
91
+ */
92
+int sitnl_addr_v6_del(const char *iface, const struct in6_addr *addr,
93
+                      int prefixlen);
94
+
95
+/**
96
+ * Add a point-to-point IPv4 address to an interface
97
+ *
98
+ * @param iface     the interface where the address has to be added
99
+ * @param local     the address to add
100
+ * @param remote    the associated p-t-p remote address
101
+ *
102
+ * @return          0 on success, a negative error code otherwise
103
+ */
104
+int sitnl_addr_ptp_v4_add(const char *iface, const in_addr_t *local,
105
+                          const in_addr_t *remote);
106
+
107
+/**
108
+ * Remove a point-to-point IPv4 address from an interface
109
+ *
110
+ * @param iface     the interface to remove the address from
111
+ * @param local     the address to remove
112
+ *
113
+ * @return          0 on success, a negative error code otherwise
114
+ */
115
+int sitnl_addr_ptp_v4_del(const char *iface, const in_addr_t *local);
116
+
117
+
118
+/**
119
+ * Add a route for an IPv4 address/network
120
+ *
121
+ * @param dst       the destination of the route
122
+ * @param prefixlen the length of the prefix of the destination
123
+ * @param gw        the gateway for this route
124
+ * @param iface     the interface for this route (can be NULL)
125
+ * @param table     the table to add this route to (if 0, will be added to the
126
+ *                  main table)
127
+ * @param metric    the metric associated with the route
128
+ *
129
+ * @return          0 on success, a negative error code otherwise
130
+ */
131
+int sitnl_route_v4_add(const in_addr_t *dst, int prefixlen,
132
+                       const in_addr_t *gw, const char *iface, uint32_t table,
133
+                       int metric);
134
+
135
+/**
136
+ * Add a route for an IPv6 address/network
137
+ *
138
+ * @param dst       the destination of the route
139
+ * @param prefixlen the length of the prefix of the destination
140
+ * @param gw        the gateway for this route
141
+ * @param iface     the interface for this route (can be NULL)
142
+ * @param table     the table to add this route to (if 0, will be added to the
143
+ *                  main table)
144
+ * @param metric    the metric associated with the route
145
+ *
146
+ * @return          0 on success, a negative error code otherwise
147
+ */
148
+int sitnl_route_v6_add(const struct in6_addr *dst, int prefixlen,
149
+                       const struct in6_addr *gw, const char *iface,
150
+                       uint32_t table, int metric);
151
+
152
+/**
153
+ * Delete a route for an IPv4 address/network
154
+ *
155
+ * @param dst       the destination of the route
156
+ * @param prefixlen the length of the prefix of the destination
157
+ * @param gw        the gateway for this route
158
+ * @param iface     the interface for this route (can be NULL)
159
+ * @param table     the table to add this route to (if 0, will be added to the
160
+ *                  main table)
161
+ * @param metric    the metric associated with the route
162
+ *
163
+ * @return          0 on success, a negative error code otherwise
164
+ */
165
+int sitnl_route_v4_del(const in_addr_t *dst, int prefixlen,
166
+                       const in_addr_t *gw, const char *iface, uint32_t table,
167
+                       int metric);
168
+
169
+/**
170
+ * Delete a route for an IPv4 address/network
171
+ *
172
+ * @param dst       the destination of the route
173
+ * @param prefixlen the length of the prefix of the destination
174
+ * @param gw        the gateway for this route
175
+ * @param iface     the interface for this route (can be NULL)
176
+ * @param table     the table to add this route to (if 0, will be added to the
177
+ *                  main table)
178
+ * @param metric    the metric associated with the route
179
+ *
180
+ * @return          0 on success, a negative error code otherwise
181
+ */
182
+int sitnl_route_v6_del(const struct in6_addr *dst, int prefixlen,
183
+                       const struct in6_addr *gw, const char *iface,
184
+                       uint32_t table, int metric);
185
+
186
+/**
187
+ * Retrieve the gateway and outgoing interface for the specified IPv4
188
+ * address/network
189
+ *
190
+ * @param dst           The destination to lookup
191
+ * @param prefixlen     The length of the prefix of the destination
192
+ * @param best_gw       Location where the retrieved GW has to be stored
193
+ * @param best_iface    Location where the retrieved interface has to be stored
194
+ *
195
+ * @return              0 on success, a negative error code otherwise
196
+ */
197
+int sitnl_route_v4_best_gw(const in_addr_t *dst, int prefixlen,
198
+                           in_addr_t *best_gw, char *best_iface);
199
+
200
+/**
201
+ * Retrieve the gateway and outgoing interface for the specified IPv6
202
+ * address/network
203
+ *
204
+ * @param dst           The destination to lookup
205
+ * @param prefixlen     The length of the prefix of the destination
206
+ * @param best_gw       Location where the retrieved GW has to be stored
207
+ * @param best_iface    Location where the retrieved interface has to be stored
208
+ *
209
+ * @return              0 on success, a negative error code otherwise
210
+ */
211
+int sitnl_route_v6_best_gw(const struct in6_addr *dst, int prefixlen,
212
+                           struct in6_addr *best_gw, char *best_iface);
213
+
214
+#endif /* TARGET_LINUX */
215
+
216
+#endif /* SITNL_H_ */