OpenVPN
networking_sitnl.c
Go to the documentation of this file.
1/*
2 * Simplified Interface To NetLink
3 *
4 * Copyright (C) 2016-2026 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, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#ifdef TARGET_LINUX
25
26#include "syshead.h"
27
28#include "dco.h"
29#include "errlevel.h"
30#include "fdmisc.h"
31#include "buffer.h"
32#include "misc.h"
33#include "networking.h"
34#include "proto.h"
35#include "route.h"
36
37#include <errno.h>
38#include <string.h>
39#include <unistd.h>
40#include <sys/types.h>
41#include <sys/socket.h>
42#include <linux/netlink.h>
43#include <linux/rtnetlink.h>
44
45#define SNDBUF_SIZE (1024 * 2)
46#define RCVBUF_SIZE (1024 * 4)
47
48#define SITNL_ADDATTR(_msg, _max_size, _attr, _data, _size) \
49 { \
50 if (sitnl_addattr(_msg, _max_size, _attr, _data, _size) < 0) \
51 { \
52 goto err; \
53 } \
54 }
55
56#define SITNL_NEST(_msg, _max_size, _attr) \
57 ({ \
58 struct rtattr *_nest = sitnl_nlmsg_tail(_msg); \
59 SITNL_ADDATTR(_msg, _max_size, _attr, NULL, 0); \
60 _nest; \
61 })
62
63#define SITNL_NEST_END(_msg, _nest) \
64 { \
65 _nest->rta_len = (unsigned short)((void *)sitnl_nlmsg_tail(_msg) - (void *)_nest); \
66 }
67
68/* This function was originally implemented as a macro, but compiling with
69 * gcc and -O3 was getting confused about the math and thus raising
70 * security warnings on subsequent memcpy() calls.
71 *
72 * Converting the macro to a function was not enough, because gcc was still
73 * inlining it and falling in the same math trap.
74 *
75 * The only way out to avoid any warning/error is to force the function to
76 * not be inline'd.
77 */
78static __attribute__((noinline)) void *
79sitnl_nlmsg_tail(const struct nlmsghdr *nlh)
80{
81 return (unsigned char *)nlh + NLMSG_ALIGN(nlh->nlmsg_len);
82}
83
88typedef union
89{
90 in_addr_t ipv4;
91 struct in6_addr ipv6;
93
97struct sitnl_link_req
98{
99 struct nlmsghdr n;
100 struct ifinfomsg i;
101 char buf[256];
102};
103
107struct sitnl_addr_req
108{
109 struct nlmsghdr n;
110 struct ifaddrmsg i;
111 char buf[256];
112};
113
117struct sitnl_route_req
118{
119 struct nlmsghdr n;
120 struct rtmsg r;
121 char buf[256];
122};
123
124typedef int (*sitnl_parse_reply_cb)(struct nlmsghdr *msg, void *arg);
125
129static int
130sitnl_addattr(struct nlmsghdr *n, size_t maxlen, unsigned short type, const void *data, size_t alen)
131{
132 size_t len = RTA_LENGTH(alen);
133
134 if ((NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len)) > maxlen)
135 {
136 msg(M_WARN, "%s: rtnl: message exceeded bound of %zu", __func__, maxlen);
137 return -EMSGSIZE;
138 }
139
140 struct rtattr *rta = sitnl_nlmsg_tail(n);
141 rta->rta_type = type;
142 ASSERT(len <= USHRT_MAX);
143 rta->rta_len = (unsigned short)len;
144
145 if (!data)
146 {
147 memset(RTA_DATA(rta), 0, alen);
148 }
149 else
150 {
151 memcpy(RTA_DATA(rta), data, alen);
152 }
153
154 n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
155
156 return 0;
157}
158
162static int
163sitnl_socket(void)
164{
165 int sndbuf = SNDBUF_SIZE;
166 int rcvbuf = RCVBUF_SIZE;
167 int fd;
168
169 fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
170 if (fd < 0)
171 {
172 msg(M_WARN, "%s: cannot open netlink socket", __func__);
173 return fd;
174 }
175
176 /* set close on exec to avoid child processes access the socket */
177 set_cloexec(fd);
178
179 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) < 0)
180 {
181 msg(M_WARN | M_ERRNO, "%s: SO_SNDBUF", __func__);
182 close(fd);
183 return -1;
184 }
185
186 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)) < 0)
187 {
188 msg(M_WARN | M_ERRNO, "%s: SO_RCVBUF", __func__);
189 close(fd);
190 return -1;
191 }
192
193 return fd;
194}
195
199static int
200sitnl_bind(int fd, uint32_t groups, uint32_t *local_pid)
201{
202 socklen_t addr_len;
203 struct sockaddr_nl local;
204
205 CLEAR(local);
206
207 local.nl_family = AF_NETLINK;
208 local.nl_groups = groups;
209
210 if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0)
211 {
212 msg(M_WARN | M_ERRNO, "%s: cannot bind netlink socket", __func__);
213 return -errno;
214 }
215
216 addr_len = sizeof(local);
217 if (getsockname(fd, (struct sockaddr *)&local, &addr_len) < 0)
218 {
219 msg(M_WARN | M_ERRNO, "%s: cannot getsockname", __func__);
220 return -errno;
221 }
222
223 if (addr_len != sizeof(local))
224 {
225 msg(M_WARN, "%s: wrong address length %u", __func__, (unsigned int)addr_len);
226 return -EINVAL;
227 }
228
229 if (local.nl_family != AF_NETLINK)
230 {
231 msg(M_WARN, "%s: wrong address family %d", __func__, local.nl_family);
232 return -EINVAL;
233 }
234
235 /* We bound with nl_pid=0, so the kernel assigned this socket a unique port
236 * id (it is not the process pid - a process may own several netlink
237 * sockets). getsockname() above is the only way to learn it: hand it back
238 * to the caller, which uses it to check that replies are addressed to this
239 * socket.
240 */
241 *local_pid = local.nl_pid;
242
243 return 0;
244}
245
246#if defined(__GNUC__) || defined(__clang__)
247#pragma GCC diagnostic push
248#pragma GCC diagnostic ignored "-Wsign-conversion"
249#endif
250
254static int
255sitnl_send(struct nlmsghdr *payload, pid_t peer, unsigned int groups, sitnl_parse_reply_cb cb,
256 void *arg_cb)
257{
258 int fd, ret;
259 uint32_t local_pid = 0;
260 struct sockaddr_nl nladdr;
261 struct nlmsgerr *err;
262 struct nlmsghdr *h;
263 char buf[1024 * 16];
264 struct iovec iov = {
265 .iov_base = payload,
266 .iov_len = payload->nlmsg_len,
267 };
268 struct msghdr nlmsg = {
269 .msg_name = &nladdr,
270 .msg_namelen = sizeof(nladdr),
271 .msg_iov = &iov,
272 .msg_iovlen = 1,
273 };
274
275 CLEAR(nladdr);
276
277 nladdr.nl_family = AF_NETLINK;
278 nladdr.nl_pid = peer;
279 nladdr.nl_groups = groups;
280
281 /* Match replies to requests with a monotonically increasing sequence
282 * number, seeded once from wall-clock time so it differs between runs.
283 * The kernel echoes this seq (and our port id) in its replies, letting the
284 * receive loop below discard any unrelated or spoofed message.
285 */
286 static uint32_t sitnl_seq;
287 if (!sitnl_seq)
288 {
289 sitnl_seq = (uint32_t)time(NULL);
290 }
291 payload->nlmsg_seq = ++sitnl_seq;
292
293 /* no need to send reply */
294 if (!cb)
295 {
296 payload->nlmsg_flags |= NLM_F_ACK;
297 }
298
299 fd = sitnl_socket();
300 if (fd < 0)
301 {
302 msg(M_WARN | M_ERRNO, "%s: can't open rtnl socket", __func__);
303 return -errno;
304 }
305
306 if (sitnl_bind(fd, 0, &local_pid) < 0)
307 {
308 msg(M_WARN | M_ERRNO, "%s: can't bind rtnl socket", __func__);
309 ret = -errno;
310 goto out;
311 }
312
313 if (sendmsg(fd, &nlmsg, 0) < 0)
314 {
315 msg(M_WARN | M_ERRNO, "%s: rtnl: error on sendmsg()", __func__);
316 ret = -errno;
317 goto out;
318 }
319
320 /* prepare buffer to store RTNL replies */
321 memset(buf, 0, sizeof(buf));
322 iov.iov_base = buf;
323
324 while (1)
325 {
326 /*
327 * iov_len is modified by recvmsg(), therefore has to be initialized before
328 * using it again
329 */
330 msg(D_RTNL, "%s: checking for received messages", __func__);
331 iov.iov_len = sizeof(buf);
332 ssize_t rcv_len = recvmsg(fd, &nlmsg, 0);
333 msg(D_RTNL, "%s: rtnl: received %zd bytes", __func__, rcv_len);
334 if (rcv_len < 0)
335 {
336 if ((errno == EINTR) || (errno == EAGAIN))
337 {
338 msg(D_RTNL, "%s: interrupted call", __func__);
339 continue;
340 }
341 msg(M_WARN | M_ERRNO, "%s: rtnl: error on recvmsg()", __func__);
342 ret = -errno;
343 goto out;
344 }
345
346 if (rcv_len == 0)
347 {
348 msg(M_WARN, "%s: rtnl: socket reached unexpected EOF", __func__);
349 ret = -EIO;
350 goto out;
351 }
352
353 if (nlmsg.msg_namelen != sizeof(nladdr))
354 {
355 msg(M_WARN, "%s: sender address length: %u (expected %zu)", __func__, nlmsg.msg_namelen,
356 sizeof(nladdr));
357 ret = -EIO;
358 goto out;
359 }
360
361 h = (struct nlmsghdr *)buf;
362 while (rcv_len >= (int)sizeof(*h))
363 {
364 uint32_t len = h->nlmsg_len;
365 ssize_t rem_len = len - sizeof(*h);
366
367 if ((rem_len < 0) || (len > rcv_len))
368 {
369 if (nlmsg.msg_flags & MSG_TRUNC)
370 {
371 msg(M_WARN, "%s: truncated message", __func__);
372 ret = -EIO;
373 goto out;
374 }
375 msg(M_WARN, "%s: malformed message: len=%u", __func__, len);
376 ret = -EIO;
377 goto out;
378 }
379
380 /* Discard any message that did not come from the kernel or does
381 * not match the request we sent: only the kernel (nl_pid 0) can
382 * legitimately reply, and a valid reply echoes our port id and
383 * sequence number. This prevents a local process from injecting a
384 * spoofed reply that the callback would otherwise act on.
385 */
386 if ((nladdr.nl_pid != 0) || (h->nlmsg_pid != local_pid)
387 || (h->nlmsg_seq != payload->nlmsg_seq))
388 {
389 msg(D_RTNL,
390 "%s: skipping unrelated message. nl_pid:%u nlmsg_pid:%u (local:%u) nlmsg_seq:%u (seq:%u)",
391 __func__, nladdr.nl_pid, h->nlmsg_pid, local_pid, h->nlmsg_seq,
392 payload->nlmsg_seq);
393 rcv_len -= NLMSG_ALIGN(len);
394 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
395 continue;
396 }
397
398 if (h->nlmsg_type == NLMSG_DONE)
399 {
400 ret = 0;
401 goto out;
402 }
403
404 if (h->nlmsg_type == NLMSG_ERROR)
405 {
406 err = (struct nlmsgerr *)NLMSG_DATA(h);
407 if (rem_len < (int)sizeof(struct nlmsgerr))
408 {
409 msg(M_WARN, "%s: ERROR truncated", __func__);
410 ret = -EIO;
411 }
412 else
413 {
414 if (!err->error)
415 {
416 ret = 0;
417 if (cb)
418 {
419 int r = cb(h, arg_cb);
420 if (r <= 0)
421 {
422 ret = r;
423 }
424 }
425 }
426 else
427 {
428 msg(M_WARN, "%s: rtnl: generic error (%d): %s", __func__, err->error,
429 strerror(-err->error));
430 ret = err->error;
431 }
432 }
433 goto out;
434 }
435
436 if (cb)
437 {
438 int r = cb(h, arg_cb);
439 if (r <= 0)
440 {
441 ret = r;
442 goto out;
443 }
444 }
445 else
446 {
447 msg(M_WARN, "%s: RTNL: unexpected reply", __func__);
448 }
449
450 rcv_len -= NLMSG_ALIGN(len);
451 h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
452 }
453
454 if (nlmsg.msg_flags & MSG_TRUNC)
455 {
456 msg(M_WARN, "%s: message truncated", __func__);
457 continue;
458 }
459
460 if (rcv_len)
461 {
462 msg(M_WARN, "%s: rtnl: %zd not parsed bytes", __func__, rcv_len);
463 ret = -1;
464 goto out;
465 }
466 }
467out:
468 close(fd);
469
470 return ret;
471}
472
473typedef struct
474{
475 size_t addr_size;
477 char iface[IFNAMSIZ];
478 bool default_only;
479 unsigned int table;
480} route_res_t;
481
482static int
483sitnl_route_save(struct nlmsghdr *n, void *arg)
484{
485 route_res_t *res = arg;
486 struct rtmsg *r = NLMSG_DATA(n);
487 struct rtattr *rta = RTM_RTA(r);
488 size_t len = n->nlmsg_len - NLMSG_LENGTH(sizeof(*r));
489 unsigned int table, ifindex = 0;
490 const void *gw = NULL;
491
492 /* filter-out non-zero dst prefixes */
493 if (res->default_only && r->rtm_dst_len != 0)
494 {
495 return 1;
496 }
497
498 /* route table, ignored with RTA_TABLE */
499 table = r->rtm_table;
500
501 while (RTA_OK(rta, len))
502 {
503 switch (rta->rta_type)
504 {
505 /* route interface */
506 case RTA_OIF:
507 ifindex = *(unsigned int *)RTA_DATA(rta);
508 break;
509
510 /* route prefix */
511 case RTA_DST:
512 break;
513
514 /* GW for the route */
515 case RTA_GATEWAY:
516 gw = RTA_DATA(rta);
517 break;
518
519 /* route table */
520 case RTA_TABLE:
521 table = *(unsigned int *)RTA_DATA(rta);
522 break;
523 }
524
525 rta = RTA_NEXT(rta, len);
526 }
527
528 /* filter out any route not coming from the selected table */
529 if (res->table && res->table != table)
530 {
531 return 1;
532 }
533
534 if (!if_indextoname(ifindex, res->iface))
535 {
536 msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifname for index %u", __func__, ifindex);
537 return -1;
538 }
539
540 if (gw)
541 {
542 memcpy(&res->gw, gw, res->addr_size);
543 }
544
545 return 0;
546}
547
548static int
549sitnl_route_best_gw(sa_family_t af_family, const inet_address_t *dst, void *best_gw,
550 char *best_iface)
551{
552 struct sitnl_route_req req;
553 route_res_t res;
554 int ret = -EINVAL;
555
556 ASSERT(best_gw);
557 ASSERT(best_iface);
558
559 CLEAR(req);
560 CLEAR(res);
561
562 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r));
563 req.n.nlmsg_type = RTM_GETROUTE;
564 req.n.nlmsg_flags = NLM_F_REQUEST;
565
566 ASSERT(af_family <= UCHAR_MAX);
567 req.r.rtm_family = (unsigned char)af_family;
568
569 switch (af_family)
570 {
571 case AF_INET:
572 res.addr_size = sizeof(in_addr_t);
573 /*
574 * kernel can't return 0.0.0.0/8 host route, dump all
575 * the routes and filter for 0.0.0.0/0 in cb()
576 */
577 if (!dst || !dst->ipv4)
578 {
579 req.n.nlmsg_flags |= NLM_F_DUMP;
580 res.default_only = true;
581 res.table = RT_TABLE_MAIN;
582 }
583 else
584 {
585 req.r.rtm_dst_len = 32;
586 }
587 break;
588
589 case AF_INET6:
590 res.addr_size = sizeof(struct in6_addr);
591 /* kernel can return ::/128 host route */
592 req.r.rtm_dst_len = 128;
593 break;
594
595 default:
596 /* unsupported */
597 return -EINVAL;
598 }
599
600 SITNL_ADDATTR(&req.n, sizeof(req), RTA_DST, dst, res.addr_size);
601
602 ret = sitnl_send(&req.n, 0, 0, sitnl_route_save, &res);
603 if (ret < 0)
604 {
605 goto err;
606 }
607
608 /* save result in output variables */
609 memcpy(best_gw, &res.gw, res.addr_size);
610 strlcpy(best_iface, res.iface, IFNAMSIZ);
611err:
612 return ret;
613}
614
615/* used by iproute2 implementation too */
616int
617net_route_v6_best_gw(openvpn_net_ctx_t *ctx, const struct in6_addr *dst, struct in6_addr *best_gw,
618 char *best_iface)
619{
620 inet_address_t dst_v6;
621 char buf[INET6_ADDRSTRLEN];
622 int ret;
623
624 CLEAR(dst_v6);
625
626 if (dst)
627 {
628 dst_v6.ipv6 = *dst;
629 }
630
631 msg(D_ROUTE, "%s query: dst %s", __func__, inet_ntop(AF_INET6, &dst_v6.ipv6, buf, sizeof(buf)));
632
633 ret = sitnl_route_best_gw(AF_INET6, &dst_v6, best_gw, best_iface);
634 if (ret < 0)
635 {
636 return ret;
637 }
638
639 msg(D_ROUTE, "%s result: via %s dev %s", __func__,
640 inet_ntop(AF_INET6, best_gw, buf, sizeof(buf)), best_iface);
641
642 return ret;
643}
644
645/* used by iproute2 implementation too */
646int
647net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst, in_addr_t *best_gw,
648 char *best_iface)
649{
650 inet_address_t dst_v4;
651 char buf[INET_ADDRSTRLEN];
652 int ret;
653
654 CLEAR(dst_v4);
655
656 if (dst)
657 {
658 dst_v4.ipv4 = htonl(*dst);
659 }
660
661 msg(D_ROUTE, "%s query: dst %s", __func__, inet_ntop(AF_INET, &dst_v4.ipv4, buf, sizeof(buf)));
662
663 ret = sitnl_route_best_gw(AF_INET, &dst_v4, best_gw, best_iface);
664 if (ret < 0)
665 {
666 return ret;
667 }
668
669 msg(D_ROUTE, "%s result: via %s dev %s", __func__,
670 inet_ntop(AF_INET, best_gw, buf, sizeof(buf)), best_iface);
671
672 /* result is expected in Host Order */
673 *best_gw = ntohl(*best_gw);
674
675 return ret;
676}
677
678#ifdef ENABLE_SITNL
679
680static int
681get_ifindex(const char *func, const char *iface, uint32_t *ifindex)
682{
683 if (!iface)
684 {
685 msg(M_WARN, "%s: passed NULL interface", func);
686 return -EINVAL;
687 }
688
689 *ifindex = if_nametoindex(iface);
690 if (*ifindex == 0)
691 {
692 msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", func, iface);
693 return -ENOENT;
694 }
695 return 0;
696}
697
698
699int
700net_iface_up(const openvpn_net_ctx_t *ctx, const char *iface, bool up)
701{
702 struct sitnl_link_req req;
703 uint32_t ifindex;
704
705 CLEAR(req);
706
707 const int ret = get_ifindex(__func__, iface, &ifindex);
708 if (ret < 0)
709 {
710 return ret;
711 }
712
713 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
714 req.n.nlmsg_flags = NLM_F_REQUEST;
715 req.n.nlmsg_type = RTM_SETLINK;
716
717 req.i.ifi_family = AF_PACKET;
718 req.i.ifi_index = (int)ifindex;
719 req.i.ifi_change |= IFF_UP;
720 if (up)
721 {
722 req.i.ifi_flags |= IFF_UP;
723 }
724 else
725 {
726 req.i.ifi_flags &= ~(unsigned int)IFF_UP;
727 }
728
729 msg(M_INFO, "%s: set %s %s", __func__, iface, up ? "up" : "down");
730
731 return sitnl_send(&req.n, 0, 0, NULL, NULL);
732}
733
734int
735net_iface_mtu_set(const openvpn_net_ctx_t *ctx, const char *iface, uint32_t mtu)
736{
737 struct sitnl_link_req req;
738 int ret = -1;
739 uint32_t ifindex;
740
741 CLEAR(req);
742
743 if (get_ifindex(__func__, iface, &ifindex) < 0)
744 {
745 return -1;
746 }
747
748 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
749 req.n.nlmsg_flags = NLM_F_REQUEST;
750 req.n.nlmsg_type = RTM_SETLINK;
751
752 req.i.ifi_family = AF_PACKET;
753 req.i.ifi_index = (int)ifindex;
754
755 SITNL_ADDATTR(&req.n, sizeof(req), IFLA_MTU, &mtu, 4);
756
757 msg(M_INFO, "%s: mtu %u for %s", __func__, mtu, iface);
758
759 ret = sitnl_send(&req.n, 0, 0, NULL, NULL);
760err:
761 return ret;
762}
763
764int
765net_addr_ll_set(const openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, uint8_t *addr)
766{
767 struct sitnl_link_req req;
768 int ret = -1;
769 uint32_t ifindex;
770
771 CLEAR(req);
772
773 if (get_ifindex(__func__, iface, &ifindex) < 0)
774 {
775 return -1;
776 }
777
778 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
779 req.n.nlmsg_flags = NLM_F_REQUEST;
780 req.n.nlmsg_type = RTM_SETLINK;
781
782 req.i.ifi_family = AF_PACKET;
783 req.i.ifi_index = (int)ifindex;
784
785 SITNL_ADDATTR(&req.n, sizeof(req), IFLA_ADDRESS, addr, OPENVPN_ETH_ALEN);
786
787 msg(M_INFO, "%s: lladdr " MAC_FMT " for %s", __func__, MAC_PRINT_ARG(addr), iface);
788
789 ret = sitnl_send(&req.n, 0, 0, NULL, NULL);
790err:
791 return ret;
792}
793
794static int
795sitnl_addr_set(uint16_t cmd, uint16_t flags, const char *iface, sa_family_t af_family,
796 const inet_address_t *local, const inet_address_t *remote, int prefixlen)
797{
798 struct sitnl_addr_req req;
799 size_t size;
800 uint32_t ifindex;
801
802 int ret = get_ifindex(__func__, iface, &ifindex);
803 if (ret < 0)
804 {
805 return ret;
806 }
807 ret = -EINVAL; /* for SITNL_ADDATTR */
808
809 switch (af_family)
810 {
811 case AF_INET:
812 size = sizeof(struct in_addr);
813 break;
814
815 case AF_INET6:
816 size = sizeof(struct in6_addr);
817 break;
818
819 default:
820 msg(M_WARN, "%s: rtnl: unknown address family %d", __func__, af_family);
821 return -EINVAL;
822 }
823
824 CLEAR(req);
825
826 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
827 req.n.nlmsg_type = cmd;
828 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
829
830 req.i.ifa_index = ifindex;
831 ASSERT(af_family <= UINT8_MAX);
832 req.i.ifa_family = (uint8_t)af_family;
833
834 /* if no prefixlen has been specified, assume host address */
835 if (prefixlen == 0)
836 {
837 prefixlen = (int)size * 8;
838 }
839 ASSERT(prefixlen <= UINT8_MAX);
840 req.i.ifa_prefixlen = (uint8_t)prefixlen;
841
842 if (remote)
843 {
844 SITNL_ADDATTR(&req.n, sizeof(req), IFA_ADDRESS, remote, size);
845 }
846
847 if (local)
848 {
849 SITNL_ADDATTR(&req.n, sizeof(req), IFA_LOCAL, local, size);
850 }
851
852 if (af_family == AF_INET && local && !remote && prefixlen <= 30)
853 {
854 inet_address_t broadcast = *local;
855 broadcast.ipv4 |= htonl(~netbits_to_netmask(prefixlen));
856 SITNL_ADDATTR(&req.n, sizeof(req), IFA_BROADCAST, &broadcast, size);
857 }
858
859 ret = sitnl_send(&req.n, 0, 0, NULL, NULL);
860 if (ret == -EEXIST)
861 {
862 ret = 0;
863 }
864err:
865 return ret;
866}
867
868static int
869sitnl_addr_ptp_add(sa_family_t af_family, const char *iface, const inet_address_t *local,
870 const inet_address_t *remote)
871{
872 return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, iface, af_family, local,
873 remote, 0);
874}
875
876static int
877sitnl_addr_ptp_del(sa_family_t af_family, const char *iface, const inet_address_t *local)
878{
879 return sitnl_addr_set(RTM_DELADDR, 0, iface, af_family, local, NULL, 0);
880}
881
882static int
883sitnl_route_set(uint16_t cmd, uint16_t flags, const char *iface, sa_family_t af_family, const void *dst,
884 int prefixlen, const void *gw, enum rt_class_t table, int metric,
885 enum rt_scope_t scope, unsigned char protocol, unsigned char type)
886{
887 struct sitnl_route_req req;
888 int ret = -1; /* for SITNL_ADDATTR */
889 size_t size;
890 uint32_t ifindex = 0;
891
892 CLEAR(req);
893
894 if (iface)
895 {
896 const int ifi_ret = get_ifindex(__func__, iface, &ifindex);
897 if (ifi_ret < 0)
898 {
899 return ifi_ret;
900 }
901 }
902
903 switch (af_family)
904 {
905 case AF_INET:
906 size = sizeof(in_addr_t);
907 break;
908
909 case AF_INET6:
910 size = sizeof(struct in6_addr);
911 break;
912
913 default:
914 return -EINVAL;
915 }
916
917 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.r));
918 req.n.nlmsg_type = cmd;
919 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
920
921 ASSERT(af_family <= UCHAR_MAX);
922 req.r.rtm_family = (unsigned char)af_family;
923 req.r.rtm_scope = (unsigned char)scope;
924 req.r.rtm_protocol = protocol;
925 req.r.rtm_type = type;
926 ASSERT(prefixlen >= 0 && prefixlen <= UCHAR_MAX);
927 req.r.rtm_dst_len = (unsigned char)prefixlen;
928
929 if (table <= UCHAR_MAX)
930 {
931 req.r.rtm_table = (unsigned char)table;
932 }
933 else
934 {
935 req.r.rtm_table = RT_TABLE_UNSPEC;
936 SITNL_ADDATTR(&req.n, sizeof(req), RTA_TABLE, &table, 4);
937 }
938
939 if (dst)
940 {
941 SITNL_ADDATTR(&req.n, sizeof(req), RTA_DST, dst, size);
942 }
943
944 if (gw)
945 {
946 SITNL_ADDATTR(&req.n, sizeof(req), RTA_GATEWAY, gw, size);
947 }
948
949 if (ifindex > 0)
950 {
951 SITNL_ADDATTR(&req.n, sizeof(req), RTA_OIF, &ifindex, 4);
952 }
953
954 if (metric > 0)
955 {
956 SITNL_ADDATTR(&req.n, sizeof(req), RTA_PRIORITY, &metric, 4);
957 }
958
959 ret = sitnl_send(&req.n, 0, 0, NULL, NULL);
960err:
961 return ret;
962}
963
964static int
965sitnl_addr_add(sa_family_t af_family, const char *iface, const inet_address_t *addr, int prefixlen)
966{
967 return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, iface, af_family, addr, NULL,
968 prefixlen);
969}
970
971static int
972sitnl_addr_del(sa_family_t af_family, const char *iface, const inet_address_t *addr, int prefixlen)
973{
974 return sitnl_addr_set(RTM_DELADDR, 0, iface, af_family, addr, NULL, prefixlen);
975}
976
977int
978net_addr_v4_add(const openvpn_net_ctx_t *ctx, const char *iface, const in_addr_t *addr, int prefixlen)
979{
980 inet_address_t addr_v4;
981 char buf[INET_ADDRSTRLEN];
982
983 CLEAR(addr_v4);
984
985 if (!addr)
986 {
987 return -EINVAL;
988 }
989
990 addr_v4.ipv4 = htonl(*addr);
991
992 msg(M_INFO, "%s: %s/%d dev %s", __func__, inet_ntop(AF_INET, &addr_v4.ipv4, buf, sizeof(buf)),
993 prefixlen, iface);
994
995 return sitnl_addr_add(AF_INET, iface, &addr_v4, prefixlen);
996}
997
998int
999net_addr_v6_add(const openvpn_net_ctx_t *ctx, const char *iface, const struct in6_addr *addr,
1000 int prefixlen)
1001{
1002 inet_address_t addr_v6;
1003 char buf[INET6_ADDRSTRLEN];
1004
1005 CLEAR(addr_v6);
1006
1007 if (!addr)
1008 {
1009 return -EINVAL;
1010 }
1011
1012 addr_v6.ipv6 = *addr;
1013
1014 msg(M_INFO, "%s: %s/%d dev %s", __func__, inet_ntop(AF_INET6, &addr_v6.ipv6, buf, sizeof(buf)),
1015 prefixlen, iface);
1016
1017 return sitnl_addr_add(AF_INET6, iface, &addr_v6, prefixlen);
1018}
1019
1020int
1021net_addr_v4_del(const openvpn_net_ctx_t *ctx, const char *iface, const in_addr_t *addr, int prefixlen)
1022{
1023 inet_address_t addr_v4;
1024 char buf[INET_ADDRSTRLEN];
1025
1026 CLEAR(addr_v4);
1027
1028 if (!addr)
1029 {
1030 return -EINVAL;
1031 }
1032
1033 addr_v4.ipv4 = htonl(*addr);
1034
1035 msg(M_INFO, "%s: %s dev %s", __func__, inet_ntop(AF_INET, &addr_v4.ipv4, buf, sizeof(buf)),
1036 iface);
1037
1038 return sitnl_addr_del(AF_INET, iface, &addr_v4, prefixlen);
1039}
1040
1041int
1042net_addr_v6_del(const openvpn_net_ctx_t *ctx, const char *iface, const struct in6_addr *addr,
1043 int prefixlen)
1044{
1045 inet_address_t addr_v6;
1046 char buf[INET6_ADDRSTRLEN];
1047
1048 CLEAR(addr_v6);
1049
1050 if (!addr)
1051 {
1052 return -EINVAL;
1053 }
1054
1055 addr_v6.ipv6 = *addr;
1056
1057 msg(M_INFO, "%s: %s/%d dev %s", __func__, inet_ntop(AF_INET6, &addr_v6.ipv6, buf, sizeof(buf)),
1058 prefixlen, iface);
1059
1060 return sitnl_addr_del(AF_INET6, iface, &addr_v6, prefixlen);
1061}
1062
1063int
1064net_addr_ptp_v4_add(const openvpn_net_ctx_t *ctx, const char *iface, const in_addr_t *local,
1065 const in_addr_t *remote)
1066{
1067 inet_address_t local_v4;
1068 inet_address_t remote_v4;
1069 char buf1[INET_ADDRSTRLEN];
1070 char buf2[INET_ADDRSTRLEN];
1071
1072 CLEAR(local_v4);
1073 CLEAR(remote_v4);
1074
1075 if (!local)
1076 {
1077 return -EINVAL;
1078 }
1079
1080 local_v4.ipv4 = htonl(*local);
1081
1082 if (remote)
1083 {
1084 remote_v4.ipv4 = htonl(*remote);
1085 }
1086
1087 msg(M_INFO, "%s: %s peer %s dev %s", __func__,
1088 inet_ntop(AF_INET, &local_v4.ipv4, buf1, sizeof(buf1)),
1089 inet_ntop(AF_INET, &remote_v4.ipv4, buf2, sizeof(buf2)), iface);
1090
1091 return sitnl_addr_ptp_add(AF_INET, iface, &local_v4, &remote_v4);
1092}
1093
1094int
1095net_addr_ptp_v4_del(const openvpn_net_ctx_t *ctx, const char *iface, const in_addr_t *local,
1096 const in_addr_t *remote)
1097{
1098 inet_address_t local_v4;
1099 char buf[INET6_ADDRSTRLEN];
1100
1101 CLEAR(local_v4);
1102
1103 if (!local)
1104 {
1105 return -EINVAL;
1106 }
1107
1108 local_v4.ipv4 = htonl(*local);
1109
1110 msg(M_INFO, "%s: %s dev %s", __func__, inet_ntop(AF_INET, &local_v4.ipv4, buf, sizeof(buf)),
1111 iface);
1112
1113 return sitnl_addr_ptp_del(AF_INET, iface, &local_v4);
1114}
1115
1116static int
1117sitnl_route_add(const char *iface, sa_family_t af_family, const void *dst, int prefixlen,
1118 const void *gw, int table, int metric)
1119{
1120 enum rt_scope_t scope = RT_SCOPE_UNIVERSE;
1121 /* table 0 is RT_TABLE_UNSPEC */
1122 const uint32_t rt_table = table > 0 ? (uint32_t)table : RT_TABLE_MAIN;
1123
1124 if (!gw && iface)
1125 {
1126 scope = RT_SCOPE_LINK;
1127 }
1128
1129 return sitnl_route_set(RTM_NEWROUTE, NLM_F_CREATE, iface, af_family, dst, prefixlen, gw,
1130 rt_table, metric, scope, RTPROT_BOOT, RTN_UNICAST);
1131}
1132
1133int
1134net_route_v4_add(const openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen, const in_addr_t *gw,
1135 const char *iface, int table, int metric)
1136{
1137 const in_addr_t *dst_ptr = NULL, *gw_ptr = NULL;
1138 in_addr_t dst_be = 0, gw_be = 0;
1139 char dst_str[INET_ADDRSTRLEN];
1140 char gw_str[INET_ADDRSTRLEN];
1141
1142 if (dst)
1143 {
1144 dst_be = htonl(*dst);
1145 dst_ptr = &dst_be;
1146 }
1147
1148 if (gw)
1149 {
1150 gw_be = htonl(*gw);
1151 gw_ptr = &gw_be;
1152 }
1153
1154 msg(D_ROUTE, "%s: %s/%d via %s dev %s table %d metric %d", __func__,
1155 inet_ntop(AF_INET, &dst_be, dst_str, sizeof(dst_str)), prefixlen,
1156 inet_ntop(AF_INET, &gw_be, gw_str, sizeof(gw_str)), np(iface), table, metric);
1157
1158 return sitnl_route_add(iface, AF_INET, dst_ptr, prefixlen, gw_ptr, table, metric);
1159}
1160
1161int
1162net_route_v6_add(const openvpn_net_ctx_t *ctx, const struct in6_addr *dst, int prefixlen,
1163 const struct in6_addr *gw, const char *iface, int table, int metric)
1164{
1165 inet_address_t dst_v6;
1166 inet_address_t gw_v6;
1167 char dst_str[INET6_ADDRSTRLEN];
1168 char gw_str[INET6_ADDRSTRLEN];
1169
1170 CLEAR(dst_v6);
1171 CLEAR(gw_v6);
1172
1173 if (dst)
1174 {
1175 dst_v6.ipv6 = *dst;
1176 }
1177
1178 if (gw)
1179 {
1180 gw_v6.ipv6 = *gw;
1181 }
1182
1183 msg(D_ROUTE, "%s: %s/%d via %s dev %s table %d metric %d", __func__,
1184 inet_ntop(AF_INET6, &dst_v6.ipv6, dst_str, sizeof(dst_str)), prefixlen,
1185 inet_ntop(AF_INET6, &gw_v6.ipv6, gw_str, sizeof(gw_str)), np(iface), table, metric);
1186
1187 return sitnl_route_add(iface, AF_INET6, dst, prefixlen, gw, table, metric);
1188}
1189
1190static int
1191sitnl_route_del(const char *iface, sa_family_t af_family, const inet_address_t *dst, int prefixlen,
1192 const inet_address_t *gw, int table, int metric)
1193{
1194 /* table 0 is RT_TABLE_UNSPEC */
1195 const uint32_t rt_table = table > 0 ? (uint32_t)table : RT_TABLE_MAIN;
1196
1197 return sitnl_route_set(RTM_DELROUTE, 0, iface, af_family, dst, prefixlen, gw, rt_table, metric,
1198 RT_SCOPE_NOWHERE, 0, 0);
1199}
1200
1201int
1202net_route_v4_del(const openvpn_net_ctx_t *ctx, const in_addr_t *dst, int prefixlen, const in_addr_t *gw,
1203 const char *iface, int table, int metric)
1204{
1205 inet_address_t dst_v4;
1206 inet_address_t gw_v4;
1207 char dst_str[INET_ADDRSTRLEN];
1208 char gw_str[INET_ADDRSTRLEN];
1209
1210 CLEAR(dst_v4);
1211 CLEAR(gw_v4);
1212
1213 if (dst)
1214 {
1215 dst_v4.ipv4 = htonl(*dst);
1216 }
1217
1218 if (gw)
1219 {
1220 gw_v4.ipv4 = htonl(*gw);
1221 }
1222
1223 msg(D_ROUTE, "%s: %s/%d via %s dev %s table %d metric %d", __func__,
1224 inet_ntop(AF_INET, &dst_v4.ipv4, dst_str, sizeof(dst_str)), prefixlen,
1225 inet_ntop(AF_INET, &gw_v4.ipv4, gw_str, sizeof(gw_str)), np(iface), table, metric);
1226
1227 return sitnl_route_del(iface, AF_INET, &dst_v4, prefixlen, &gw_v4, table, metric);
1228}
1229
1230int
1231net_route_v6_del(const openvpn_net_ctx_t *ctx, const struct in6_addr *dst, int prefixlen,
1232 const struct in6_addr *gw, const char *iface, int table, int metric)
1233{
1234 inet_address_t dst_v6;
1235 inet_address_t gw_v6;
1236 char dst_str[INET6_ADDRSTRLEN];
1237 char gw_str[INET6_ADDRSTRLEN];
1238
1239 CLEAR(dst_v6);
1240 CLEAR(gw_v6);
1241
1242 if (dst)
1243 {
1244 dst_v6.ipv6 = *dst;
1245 }
1246
1247 if (gw)
1248 {
1249 gw_v6.ipv6 = *gw;
1250 }
1251
1252 msg(D_ROUTE, "%s: %s/%d via %s dev %s table %d metric %d", __func__,
1253 inet_ntop(AF_INET6, &dst_v6.ipv6, dst_str, sizeof(dst_str)), prefixlen,
1254 inet_ntop(AF_INET6, &gw_v6.ipv6, gw_str, sizeof(gw_str)), np(iface), table, metric);
1255
1256 return sitnl_route_del(iface, AF_INET6, &dst_v6, prefixlen, &gw_v6, table, metric);
1257}
1258
1259
1260int
1261net_iface_new(const openvpn_net_ctx_t *ctx, const char *iface, const char *type, const void *arg)
1262{
1263 struct sitnl_link_req req = {};
1264 int ret = -1;
1265
1266 ASSERT(iface);
1267
1268 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
1269 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL;
1270 req.n.nlmsg_type = RTM_NEWLINK;
1271
1272 SITNL_ADDATTR(&req.n, sizeof(req), IFLA_IFNAME, iface, strlen(iface) + 1);
1273
1274 struct rtattr *linkinfo = SITNL_NEST(&req.n, sizeof(req), IFLA_LINKINFO);
1275 SITNL_ADDATTR(&req.n, sizeof(req), IFLA_INFO_KIND, type, strlen(type) + 1);
1276#if defined(ENABLE_DCO)
1277 if (arg && (strcmp(type, OVPN_FAMILY_NAME) == 0))
1278 {
1279 const dco_context_t *dco = arg;
1280 struct rtattr *data = SITNL_NEST(&req.n, sizeof(req), IFLA_INFO_DATA);
1281
1282 /* the netlink format is uint8_t for this and using something
1283 * other than uint8_t here (enum underlying type is undefined but
1284 * commonly int) causes the values to be 0 when passed
1285 * on big endian arch as we only take the (biggest endian) byte
1286 * directly at the address
1287 */
1288 uint8_t ifmode = (uint8_t)dco->ifmode;
1289 SITNL_ADDATTR(&req.n, sizeof(req), IFLA_OVPN_MODE, &ifmode, sizeof(uint8_t));
1290 SITNL_NEST_END(&req.n, data);
1291 }
1292#endif
1293 SITNL_NEST_END(&req.n, linkinfo);
1294
1295 req.i.ifi_family = AF_PACKET;
1296
1297 msg(D_ROUTE, "%s: add %s type %s", __func__, iface, type);
1298
1299 ret = sitnl_send(&req.n, 0, 0, NULL, NULL);
1300err:
1301 return ret;
1302}
1303
1304static void
1305sitnl_parse_rtattr_flags(struct rtattr *tb[], size_t max, struct rtattr *rta, size_t len,
1306 unsigned short flags)
1307{
1308 memset(tb, 0, sizeof(struct rtattr *) * (max + 1));
1309
1310 while (RTA_OK(rta, len))
1311 {
1312 const unsigned short type = rta->rta_type & ~flags;
1313
1314 if ((type <= max) && (!tb[type]))
1315 {
1316 tb[type] = rta;
1317 }
1318
1319 rta = RTA_NEXT(rta, len);
1320 }
1321
1322 if (len)
1323 {
1324 msg(D_ROUTE, "%s: %zu bytes not parsed! (rta_len=%u)", __func__, len, rta->rta_len);
1325 }
1326}
1327
1328static void
1329sitnl_parse_rtattr(struct rtattr *tb[], size_t max, struct rtattr *rta, size_t len)
1330{
1331 sitnl_parse_rtattr_flags(tb, max, rta, len, 0);
1332}
1333
1334#define sitnl_parse_rtattr_nested(tb, max, rta) \
1335 (sitnl_parse_rtattr_flags(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta), NLA_F_NESTED))
1336
1337static int
1338sitnl_type_save(struct nlmsghdr *n, void *arg)
1339{
1340 struct ifinfomsg *ifi = NLMSG_DATA(n);
1341 struct rtattr *tb[IFLA_MAX + 1];
1342
1343 sitnl_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
1344
1345 if (tb[IFLA_LINKINFO])
1346 {
1347 struct rtattr *tb_link[IFLA_INFO_MAX + 1];
1348 char *type = arg;
1349
1350 sitnl_parse_rtattr_nested(tb_link, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
1351
1352 if (!tb_link[IFLA_INFO_KIND])
1353 {
1354 return -ENOENT;
1355 }
1356
1357 strlcpy(type, RTA_DATA(tb_link[IFLA_INFO_KIND]), IFACE_TYPE_LEN_MAX);
1358 }
1359
1360 return 0;
1361}
1362
1363int
1364net_iface_type(const openvpn_net_ctx_t *ctx, const char *iface, char type[IFACE_TYPE_LEN_MAX])
1365{
1366 struct sitnl_link_req req = {};
1367 int ifindex = (int)if_nametoindex(iface);
1368
1369 if (!ifindex)
1370 {
1371 return -errno;
1372 }
1373
1374 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
1375 req.n.nlmsg_flags = NLM_F_REQUEST;
1376 req.n.nlmsg_type = RTM_GETLINK;
1377
1378 req.i.ifi_family = AF_PACKET;
1379 req.i.ifi_index = ifindex;
1380
1381 memset(type, 0, IFACE_TYPE_LEN_MAX);
1382
1383 int ret = sitnl_send(&req.n, 0, 0, sitnl_type_save, type);
1384 if (ret < 0)
1385 {
1386 msg(D_ROUTE, "%s: cannot retrieve iface %s: %s (%d)", __func__, iface, strerror(-ret), ret);
1387 return ret;
1388 }
1389
1390 msg(D_ROUTE, "%s: type of %s: %s", __func__, iface, type);
1391
1392 return 0;
1393}
1394
1395int
1396net_iface_del(const openvpn_net_ctx_t *ctx, const char *iface)
1397{
1398 struct sitnl_link_req req = {};
1399 int ifindex = (int)if_nametoindex(iface);
1400
1401 if (!ifindex)
1402 {
1403 return -errno;
1404 }
1405
1406 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i));
1407 req.n.nlmsg_flags = NLM_F_REQUEST;
1408 req.n.nlmsg_type = RTM_DELLINK;
1409
1410 req.i.ifi_family = AF_PACKET;
1411 req.i.ifi_index = ifindex;
1412
1413 msg(D_ROUTE, "%s: delete %s", __func__, iface);
1414
1415 return sitnl_send(&req.n, 0, 0, NULL, NULL);
1416}
1417
1418#if defined(__GNUC__) || defined(__clang__)
1419#pragma GCC diagnostic pop
1420#endif
1421
1422#endif /* !ENABLE_SITNL */
1423
1424#endif /* TARGET_LINUX */
Buffer management functions and garbage collection.
size_t strlcpy(char *dest, const char *src, size_t maxlen)
void * dco_context_t
Definition dco.h:270
#define D_RTNL
Definition errlevel.h:109
#define M_INFO
Definition errlevel.h:54
#define D_ROUTE
Definition errlevel.h:78
void set_cloexec(socket_descriptor_t fd)
Definition fdmisc.c:78
#define MAC_PRINT_ARG(_mac)
Definition misc.h:227
#define MAC_FMT
Definition misc.h:225
int net_iface_new(const openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, const char *type, const void *arg)
int net_iface_del(const openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface)
static const char * np(const char *str)
Definition multi-auth.c:146
void * openvpn_net_iface_t
Definition networking.h:42
#define IFACE_TYPE_LEN_MAX
Definition networking.h:25
void * openvpn_net_ctx_t
Definition networking.h:41
#define CLEAR(x)
Definition basic.h:32
#define msg(flags,...)
Definition error.h:152
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
#define M_ERRNO
Definition error.h:95
#define OVPN_FAMILY_NAME
#define OPENVPN_ETH_ALEN
Definition proto.h:52
static in_addr_t netbits_to_netmask(const int netbits)
Definition route.h:399
unsigned short sa_family_t
Definition syshead.h:405
uint32_t in_addr_t
Definition syshead.h:52
__attribute__((unused))
Definition test.c:42
static char * iface
struct in6_addr ipv6
Definition openvpn-msg.h:64
struct in_addr ipv4
Definition openvpn-msg.h:63