OpenVPN
dco_linux.c
Go to the documentation of this file.
1/*
2 * Interface to linux dco networking code
3 *
4 * Copyright (C) 2020-2026 Antonio Quartulli <a@unstable.cc>
5 * Copyright (C) 2020-2026 Arne Schwabe <arne@rfc2549.org>
6 * Copyright (C) 2020-2026 OpenVPN Inc <sales@openvpn.net>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program (see the file COPYING included with this
19 * distribution); if not, see <https://www.gnu.org/licenses/>.
20 */
21
22
23#ifdef HAVE_CONFIG_H
24#include "config.h"
25#endif
26
27#if defined(ENABLE_DCO) && defined(TARGET_LINUX)
28
29#include "syshead.h"
30
31#include "dco_linux.h"
32#include "errlevel.h"
33#include "buffer.h"
34#include "networking.h"
35#include "openvpn.h"
36
37#include "socket.h"
38#include "tun.h"
39#include "ssl.h"
40#include "fdmisc.h"
41#include "multi.h"
42#include "ssl_verify.h"
43
44#include "ovpn_dco_linux.h"
45
46#include <netlink/socket.h>
47#include <netlink/netlink.h>
48#include <netlink/genl/genl.h>
49#include <netlink/genl/family.h>
50#include <netlink/genl/ctrl.h>
51
52/* libnl < 3.5.0 does not set the NLA_F_NESTED on its own, therefore we
53 * have to explicitly do it to prevent the kernel from failing upon
54 * parsing of the message
55 */
56#define nla_nest_start(_msg, _type) nla_nest_start(_msg, (_type) | NLA_F_NESTED)
57
58static int ovpn_get_mcast_id(dco_context_t *dco);
59
60void dco_check_key_ctx(const struct key_ctx_bi *key);
61
62typedef int (*ovpn_nl_cb)(struct nl_msg *msg, void *arg);
63
75static int
76resolve_ovpn_netlink_id(msglvl_t msglevel)
77{
78 int ret;
79 struct nl_sock *nl_sock = nl_socket_alloc();
80
81 if (!nl_sock)
82 {
83 msg(msglevel, "Allocating net link socket failed");
84 return -ENOMEM;
85 }
86
87 ret = genl_connect(nl_sock);
88 if (ret)
89 {
90 msg(msglevel, "Cannot connect to generic netlink: %s", nl_geterror(ret));
91 goto err_sock;
92 }
93 set_cloexec(nl_socket_get_fd(nl_sock));
94
95 ret = genl_ctrl_resolve(nl_sock, OVPN_FAMILY_NAME);
96 if (ret < 0)
97 {
98 msg(msglevel, "Cannot find ovpn_dco netlink component: %s", nl_geterror(ret));
99 }
100
101err_sock:
102 nl_socket_free(nl_sock);
103 return ret;
104}
105
106static struct nl_msg *
107ovpn_dco_nlmsg_create(const dco_context_t *dco, uint8_t cmd)
108{
109 struct nl_msg *nl_msg = nlmsg_alloc();
110 if (!nl_msg)
111 {
112 msg(M_FATAL, "cannot allocate netlink message");
113 return NULL;
114 }
115
116 genlmsg_put(nl_msg, 0, 0, dco->ovpn_dco_id, 0, 0, cmd, 0);
117 NLA_PUT_U32(nl_msg, OVPN_A_IFINDEX, dco->ifindex);
118
119 return nl_msg;
120nla_put_failure:
121 nlmsg_free(nl_msg);
122 msg(M_INFO, "cannot put into netlink message");
123 return NULL;
124}
125
126static int
127ovpn_nl_recvmsgs_report(int ret, const char *prefix)
128{
129 switch (ret)
130 {
131 case -NLE_INTR:
132 msg(M_WARN, "%s: netlink received interrupt due to signal - ignoring", prefix);
133 break;
134
135 case -NLE_NOMEM:
136 msg(M_FATAL, "%s: netlink out of memory error", prefix);
137 break;
138
139 case -NLE_AGAIN:
140 msg(M_WARN, "%s: netlink reports blocking read - aborting wait", prefix);
141 break;
142
143 case -NLE_NODEV:
144 msg(M_FATAL, "%s: netlink reports device not found:", prefix);
145 break;
146
147 case -NLE_OBJ_NOTFOUND:
148 msg(M_INFO, "%s: netlink reports object not found, ovpn kernel module unloaded?", prefix);
149 break;
150
151 default:
152 if (ret)
153 {
154 msg(M_NONFATAL, "%s: netlink reports error (%d): %s", prefix, ret,
155 nl_geterror(-ret));
156 }
157 break;
158 }
159
160 return ret;
161}
162
169static int
170ovpn_nl_recvmsgs(dco_context_t *dco, const char *prefix)
171{
172 int ret = nl_recvmsgs(dco->nl_sock, dco->nl_cb);
173
174 return ovpn_nl_recvmsgs_report(ret, prefix);
175}
176
186static int
187ovpn_nl_msg_send(dco_context_t *dco, struct nl_msg *nl_msg, const char *prefix)
188{
189 dco->status = 1;
190
191 nl_send_auto(dco->nl_sock, nl_msg);
192
193 while (dco->status == 1)
194 {
195 ovpn_nl_recvmsgs(dco, prefix);
196 }
197
198 if (dco->status < 0)
199 {
200 msg(M_INFO, "%s: failed to send netlink message: %s (%d)", prefix, strerror(-dco->status),
201 dco->status);
202 }
203
204 return dco->status;
205}
206
207struct sockaddr *
208mapped_v4_to_v6(struct sockaddr *sock, struct gc_arena *gc)
209{
210 struct sockaddr_in6 *sock6 = (struct sockaddr_in6 *)sock;
211 if (sock->sa_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED(&sock6->sin6_addr))
212 {
213 struct sockaddr_in *sock4;
214 ALLOC_OBJ_CLEAR_GC(sock4, struct sockaddr_in, gc);
215 memcpy(&sock4->sin_addr, sock6->sin6_addr.s6_addr + 12, 4);
216 sock4->sin_port = sock6->sin6_port;
217 sock4->sin_family = AF_INET;
218 return (struct sockaddr *)sock4;
219 }
220 return sock;
221}
222
223#if defined(__GNUC__) || defined(__clang__)
224#pragma GCC diagnostic push
225#pragma GCC diagnostic ignored "-Wsign-conversion"
226#endif
227
228int
229dco_new_peer(dco_context_t *dco, unsigned int peerid, int sd, struct sockaddr *localaddr,
230 struct sockaddr *remoteaddr, const struct in_addr *vpn_ipv4,
231 const struct in6_addr *vpn_ipv6)
232{
233 struct gc_arena gc = gc_new();
234 const char *remotestr = "[undefined]";
235 if (remoteaddr)
236 {
237 remotestr = print_sockaddr(remoteaddr, &gc);
238 }
239 msg(D_DCO_DEBUG, "%s: peer-id %u, fd %d, remote addr: %s", __func__, peerid, sd, remotestr);
240
241 struct nl_msg *nl_msg = ovpn_dco_nlmsg_create(dco, OVPN_CMD_PEER_NEW);
242 if (!nl_msg)
243 {
244 gc_free(&gc);
245 return -ENOMEM;
246 }
247
248 struct nlattr *attr = nla_nest_start(nl_msg, OVPN_A_PEER);
249 int ret = -EMSGSIZE;
250
251 NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, peerid);
252 NLA_PUT_U32(nl_msg, OVPN_A_PEER_SOCKET, sd);
253
254 /* Set the remote endpoint if defined (for UDP) */
255 if (remoteaddr)
256 {
257 remoteaddr = mapped_v4_to_v6(remoteaddr, &gc);
258
259 if (remoteaddr->sa_family == AF_INET)
260 {
261 NLA_PUT(nl_msg, OVPN_A_PEER_REMOTE_IPV4, sizeof(struct in_addr),
262 &((struct sockaddr_in *)remoteaddr)->sin_addr);
263 NLA_PUT_U16(nl_msg, OVPN_A_PEER_REMOTE_PORT,
264 ((struct sockaddr_in *)remoteaddr)->sin_port);
265 }
266 else if (remoteaddr->sa_family == AF_INET6)
267 {
268 NLA_PUT(nl_msg, OVPN_A_PEER_REMOTE_IPV6, sizeof(struct in6_addr),
269 &((struct sockaddr_in6 *)remoteaddr)->sin6_addr);
270 NLA_PUT_U16(nl_msg, OVPN_A_PEER_REMOTE_PORT,
271 ((struct sockaddr_in6 *)remoteaddr)->sin6_port);
272 NLA_PUT_U32(nl_msg, OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID,
273 ((struct sockaddr_in6 *)remoteaddr)->sin6_scope_id);
274 }
275 }
276
277 if (localaddr)
278 {
279 localaddr = mapped_v4_to_v6(localaddr, &gc);
280 if (localaddr->sa_family == AF_INET)
281 {
282 NLA_PUT(nl_msg, OVPN_A_PEER_LOCAL_IPV4, sizeof(struct in_addr),
283 &((struct sockaddr_in *)localaddr)->sin_addr);
284 }
285 else if (localaddr->sa_family == AF_INET6)
286 {
287 NLA_PUT(nl_msg, OVPN_A_PEER_LOCAL_IPV6, sizeof(struct in6_addr),
288 &((struct sockaddr_in6 *)localaddr)->sin6_addr);
289 }
290 }
291
292 /* Set the primary VPN IP addresses of the peer */
293 if (vpn_ipv4)
294 {
295 NLA_PUT_U32(nl_msg, OVPN_A_PEER_VPN_IPV4, vpn_ipv4->s_addr);
296 }
297 if (vpn_ipv6)
298 {
299 NLA_PUT(nl_msg, OVPN_A_PEER_VPN_IPV6, sizeof(struct in6_addr), vpn_ipv6);
300 }
301 nla_nest_end(nl_msg, attr);
302
303 ret = ovpn_nl_msg_send(dco, nl_msg, __func__);
304
305nla_put_failure:
306 nlmsg_free(nl_msg);
307 gc_free(&gc);
308 return ret;
309}
310
311static int
312ovpn_nl_cb_finish(struct nl_msg(*msg) __attribute__((unused)), void *arg)
313{
314 int *status = arg;
315
316 *status = 0;
317 return NL_SKIP;
318}
319
320/* The following enum members exist in netlink.h since linux-6.1.
321 * However, some distro we support still ship an old header, thus
322 * failing the OpenVPN compilation.
323 *
324 * For the time being we add the needed defines manually.
325 * We will drop this definition once we stop supporting those old
326 * distros.
327 *
328 * @NLMSGERR_ATTR_MISS_TYPE: type of a missing required attribute,
329 * %NLMSGERR_ATTR_MISS_NEST will not be present if the attribute was
330 * missing at the message level
331 * @NLMSGERR_ATTR_MISS_NEST: offset of the nest where attribute was missing
332 */
333enum ovpn_nlmsgerr_attrs
334{
335 OVPN_NLMSGERR_ATTR_MISS_TYPE = 5,
336 OVPN_NLMSGERR_ATTR_MISS_NEST = 6,
337 OVPN_NLMSGERR_ATTR_MAX = 6,
338};
339
340/* This function is used as error callback on the netlink socket.
341 * When something goes wrong and the kernel returns an error, this function is
342 * invoked.
343 *
344 * We pass the error code to the user by means of a variable pointed by *arg
345 * (supplied by the user when setting this callback) and we parse the kernel
346 * reply to see if it contains a human-readable error. If found, it is printed.
347 */
348static int
349ovpn_nl_cb_error(struct sockaddr_nl(*nla) __attribute__((unused)), struct nlmsgerr *err, void *arg)
350{
351 struct nlmsghdr *nlh = (struct nlmsghdr *)err - 1;
352 struct nlattr *tb_msg[OVPN_NLMSGERR_ATTR_MAX + 1];
353 int len = nlh->nlmsg_len;
354 struct nlattr *attrs;
355 int *ret = arg;
356 int ack_len = sizeof(*nlh) + sizeof(int) + sizeof(*nlh);
357
358 *ret = err->error;
359
360 if (!(nlh->nlmsg_flags & NLM_F_ACK_TLVS))
361 {
362 return NL_STOP;
363 }
364
365 if (!(nlh->nlmsg_flags & NLM_F_CAPPED))
366 {
367 ack_len += err->msg.nlmsg_len - (int)sizeof(*nlh);
368 }
369
370 if (len <= ack_len)
371 {
372 return NL_STOP;
373 }
374
375 attrs = (void *)((unsigned char *)nlh + ack_len);
376 len -= ack_len;
377
378 nla_parse(tb_msg, OVPN_NLMSGERR_ATTR_MAX, attrs, len, NULL);
379 if (tb_msg[NLMSGERR_ATTR_MSG])
380 {
381 len = (int)strnlen((char *)nla_data(tb_msg[NLMSGERR_ATTR_MSG]),
382 nla_len(tb_msg[NLMSGERR_ATTR_MSG]));
383 msg(M_WARN, "kernel error: %*s", len, (char *)nla_data(tb_msg[NLMSGERR_ATTR_MSG]));
384 }
385
386 if (tb_msg[OVPN_NLMSGERR_ATTR_MISS_NEST])
387 {
388 msg(M_WARN, "kernel error: missing required nesting type %u",
389 nla_get_u32(tb_msg[OVPN_NLMSGERR_ATTR_MISS_NEST]));
390 }
391
392 if (tb_msg[OVPN_NLMSGERR_ATTR_MISS_TYPE])
393 {
394 msg(M_WARN, "kernel error: missing required attribute type %u",
395 nla_get_u32(tb_msg[OVPN_NLMSGERR_ATTR_MISS_TYPE]));
396 }
397
398 return NL_STOP;
399}
400
401static void
402ovpn_dco_register(dco_context_t *dco)
403{
404 msg(D_DCO_DEBUG, __func__);
405 ovpn_get_mcast_id(dco);
406
407 if (dco->ovpn_dco_mcast_id < 0)
408 {
409 msg(M_FATAL, "cannot get mcast group: %s", nl_geterror(dco->ovpn_dco_mcast_id));
410 }
411
412 /* Register for ovpn-dco specific multicast messages that the kernel may
413 * send. These are subscribed on the dedicated notification socket only, so
414 * that they are never delivered while a request/reply transaction on
415 * dco->nl_sock is being parsed.
416 */
417 int ret = nl_socket_add_membership(dco->nl_sock_notify, dco->ovpn_dco_mcast_id);
418 if (ret)
419 {
420 msg(M_FATAL, "%s: failed to join groups: %d", __func__, ret);
421 }
422}
423
424static int ovpn_handle_msg(struct nl_msg *msg, void *arg);
425
426static void
427ovpn_dco_init_netlink(dco_context_t *dco)
428{
429 dco->ovpn_dco_id = resolve_ovpn_netlink_id(M_FATAL);
430
431 dco->nl_sock = nl_socket_alloc();
432
433 if (!dco->nl_sock)
434 {
435 msg(M_FATAL, "Cannot create netlink socket");
436 }
437
438 int ret = genl_connect(dco->nl_sock);
439 if (ret)
440 {
441 msg(M_FATAL, "Cannot connect to generic netlink: %s", nl_geterror(ret));
442 }
443
444 /* enable Extended ACK for detailed error reporting */
445 ret = 1;
446 setsockopt(nl_socket_get_fd(dco->nl_sock), SOL_NETLINK, NETLINK_EXT_ACK, &ret, sizeof(ret));
447
448 /* set close on exec and non-block on the netlink socket */
449 set_cloexec(nl_socket_get_fd(dco->nl_sock));
450 set_nonblock(nl_socket_get_fd(dco->nl_sock));
451
452 dco->nl_cb = nl_cb_alloc(NL_CB_DEFAULT);
453 if (!dco->nl_cb)
454 {
455 msg(M_FATAL, "failed to allocate netlink callback");
456 }
457
458 nl_socket_set_cb(dco->nl_sock, dco->nl_cb);
459
460 dco->dco_message_peer_id = -1;
461 nl_cb_err(dco->nl_cb, NL_CB_CUSTOM, ovpn_nl_cb_error, &dco->status);
462 nl_cb_set(dco->nl_cb, NL_CB_FINISH, NL_CB_CUSTOM, ovpn_nl_cb_finish, &dco->status);
463 nl_cb_set(dco->nl_cb, NL_CB_ACK, NL_CB_CUSTOM, ovpn_nl_cb_finish, &dco->status);
464 nl_cb_set(dco->nl_cb, NL_CB_VALID, NL_CB_CUSTOM, ovpn_handle_msg, dco);
465
466 /* Set up the dedicated multicast notification socket. It shares the message
467 * handler (ovpn_handle_msg) with the request/reply socket, but is read only
468 * from dco_read_and_process() in the event loop, so notifications never
469 * fire while a request/reply transaction is being parsed. */
470 dco->nl_sock_notify = nl_socket_alloc();
471 if (!dco->nl_sock_notify)
472 {
473 msg(M_FATAL, "Cannot create netlink notification socket");
474 }
475
476 ret = genl_connect(dco->nl_sock_notify);
477 if (ret)
478 {
479 msg(M_FATAL, "Cannot connect to generic netlink (notify): %s", nl_geterror(ret));
480 }
481
482 set_cloexec(nl_socket_get_fd(dco->nl_sock_notify));
483 set_nonblock(nl_socket_get_fd(dco->nl_sock_notify));
484
485 dco->nl_cb_notify = nl_cb_alloc(NL_CB_DEFAULT);
486 if (!dco->nl_cb_notify)
487 {
488 msg(M_FATAL, "failed to allocate netlink notification callback");
489 }
490
491 nl_socket_set_cb(dco->nl_sock_notify, dco->nl_cb_notify);
492 nl_cb_err(dco->nl_cb_notify, NL_CB_CUSTOM, ovpn_nl_cb_error, &dco->status);
493 nl_cb_set(dco->nl_cb_notify, NL_CB_VALID, NL_CB_CUSTOM, ovpn_handle_msg, dco);
494
495 ovpn_dco_register(dco);
496
497 /* The async PACKET messages confuse libnl and it will drop them with
498 * wrong sequence numbers (NLE_SEQ_MISMATCH), so disable libnl's sequence
499 * number check. Multicast notifications are unsolicited and likewise carry
500 * no matching sequence number, so disable the check on both sockets. */
501 nl_socket_disable_seq_check(dco->nl_sock);
502 nl_socket_disable_seq_check(dco->nl_sock_notify);
503
504 /* nl library sets the buffer size to 32k/32k by default which is sometimes
505 * overrun with very fast connecting/disconnecting clients.
506 * TODO: fix this in a better and more reliable way */
507 ASSERT(!nl_socket_set_buffer_size(dco->nl_sock, 1024 * 1024, 1024 * 1024));
508 ASSERT(!nl_socket_set_buffer_size(dco->nl_sock_notify, 1024 * 1024, 1024 * 1024));
509}
510
511bool
512ovpn_dco_init(struct context *c)
513{
514 dco_context_t *dco = &c->c1.tuntap->dco;
515
516 switch (c->mode)
517 {
518 case CM_TOP:
519 dco->ifmode = OVPN_MODE_MP;
520 break;
521
522 case CM_P2P:
523 dco->ifmode = OVPN_MODE_P2P;
524 break;
525
526 default:
527 ASSERT(false);
528 }
529
530 /* store pointer to context as it may be required by message
531 * parsing routines
532 */
533 dco->c = c;
534 ovpn_dco_init_netlink(dco);
535 return true;
536}
537
538static void
539ovpn_dco_uninit_netlink(dco_context_t *dco)
540{
541 nl_socket_free(dco->nl_sock);
542 dco->nl_sock = NULL;
543
544 nl_socket_free(dco->nl_sock_notify);
545 dco->nl_sock_notify = NULL;
546
547 /* Decrease reference count */
548 nl_cb_put(dco->nl_cb);
549 nl_cb_put(dco->nl_cb_notify);
550
551 CLEAR(*dco);
552}
553
554int
555open_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx, const char *dev)
556{
557 msg(D_DCO_DEBUG, "%s: %s", __func__, dev);
558 ASSERT(tt->type == DEV_TYPE_TUN);
559
560 int ret = net_iface_new(ctx, dev, OVPN_FAMILY_NAME, &tt->dco);
561 if (ret < 0)
562 {
563 msg(D_DCO_DEBUG, "Cannot create DCO interface %s: %d", dev, ret);
564 return ret;
565 }
566
567 tt->dco.ifindex = if_nametoindex(dev);
568 if (!tt->dco.ifindex)
569 {
570 msg(M_FATAL, "DCO: cannot retrieve ifindex for interface %s", dev);
571 }
572
573 return 0;
574}
575
576void
577close_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx)
578{
579 msg(D_DCO_DEBUG, __func__);
580
581 net_iface_del(ctx, tt->actual_name);
582 ovpn_dco_uninit_netlink(&tt->dco);
583}
584
585int
586dco_swap_keys(dco_context_t *dco, unsigned int peerid)
587{
588 msg(D_DCO_DEBUG, "%s: peer-id %u", __func__, peerid);
589
590 struct nl_msg *nl_msg = ovpn_dco_nlmsg_create(dco, OVPN_CMD_KEY_SWAP);
591 if (!nl_msg)
592 {
593 return -ENOMEM;
594 }
595
596 struct nlattr *attr = nla_nest_start(nl_msg, OVPN_A_KEYCONF);
597 int ret = -EMSGSIZE;
598 NLA_PUT_U32(nl_msg, OVPN_A_KEYCONF_PEER_ID, peerid);
599 nla_nest_end(nl_msg, attr);
600
601 ret = ovpn_nl_msg_send(dco, nl_msg, __func__);
602
603nla_put_failure:
604 nlmsg_free(nl_msg);
605 return ret;
606}
607
608
609int
610dco_del_peer(dco_context_t *dco, unsigned int peerid)
611{
612 msg(D_DCO_DEBUG | M_NOIPREFIX, "%s: peer-id %u", __func__, peerid);
613
614 struct nl_msg *nl_msg = ovpn_dco_nlmsg_create(dco, OVPN_CMD_PEER_DEL);
615 if (!nl_msg)
616 {
617 return -ENOMEM;
618 }
619
620 struct nlattr *attr = nla_nest_start(nl_msg, OVPN_A_PEER);
621 int ret = -EMSGSIZE;
622 NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, peerid);
623 nla_nest_end(nl_msg, attr);
624
625 ret = ovpn_nl_msg_send(dco, nl_msg, __func__);
626
627nla_put_failure:
628 nlmsg_free(nl_msg);
629 return ret;
630}
631
632
633int
634dco_del_key(dco_context_t *dco, unsigned int peerid, dco_key_slot_t slot)
635{
636 int ret = -EMSGSIZE;
637 msg(D_DCO_DEBUG, "%s: peer-id %u, slot %d", __func__, peerid, slot);
638
639 struct nl_msg *nl_msg = ovpn_dco_nlmsg_create(dco, OVPN_CMD_KEY_DEL);
640 if (!nl_msg)
641 {
642 return -ENOMEM;
643 }
644
645 struct nlattr *keyconf = nla_nest_start(nl_msg, OVPN_A_KEYCONF);
646 NLA_PUT_U32(nl_msg, OVPN_A_KEYCONF_PEER_ID, peerid);
647 NLA_PUT_U32(nl_msg, OVPN_A_KEYCONF_SLOT, slot);
648 nla_nest_end(nl_msg, keyconf);
649
650 ret = ovpn_nl_msg_send(dco, nl_msg, __func__);
651
652nla_put_failure:
653 nlmsg_free(nl_msg);
654 return ret;
655}
656
657int
658dco_new_key(dco_context_t *dco, unsigned int peerid, int keyid, dco_key_slot_t slot,
659 const uint8_t *encrypt_key, const uint8_t *encrypt_iv, const uint8_t *decrypt_key,
660 const uint8_t *decrypt_iv, const char *ciphername, bool epoch)
661{
662 msg(D_DCO_DEBUG, "%s: slot %d, key-id %d, peer-id %u, cipher %s, epoch %d", __func__, slot, keyid, peerid,
663 ciphername, epoch);
664
665 const size_t key_len = cipher_kt_key_size(ciphername);
666 ASSERT(key_len <= INT_MAX);
667 const int nonce_tail_len = 8;
668
669 struct nl_msg *nl_msg = ovpn_dco_nlmsg_create(dco, OVPN_CMD_KEY_NEW);
670 if (!nl_msg)
671 {
672 return -ENOMEM;
673 }
674
675 dco_cipher_t dco_cipher = dco_get_cipher(ciphername);
676
677 int ret = -EMSGSIZE;
678
679 struct nlattr *key_conf = nla_nest_start(nl_msg, OVPN_A_KEYCONF);
680 NLA_PUT_U32(nl_msg, OVPN_A_KEYCONF_PEER_ID, peerid);
681 NLA_PUT_U32(nl_msg, OVPN_A_KEYCONF_SLOT, slot);
682 NLA_PUT_U32(nl_msg, OVPN_A_KEYCONF_KEY_ID, keyid);
683 NLA_PUT_U32(nl_msg, OVPN_A_KEYCONF_CIPHER_ALG, dco_cipher);
684
685 struct nlattr *key_enc = nla_nest_start(nl_msg, OVPN_A_KEYCONF_ENCRYPT_DIR);
686 if (dco_cipher != OVPN_CIPHER_ALG_NONE)
687 {
688 NLA_PUT(nl_msg, OVPN_A_KEYDIR_CIPHER_KEY, (int)key_len, encrypt_key);
689 NLA_PUT(nl_msg, OVPN_A_KEYDIR_NONCE_TAIL, nonce_tail_len, encrypt_iv);
690 }
691 nla_nest_end(nl_msg, key_enc);
692
693 struct nlattr *key_dec = nla_nest_start(nl_msg, OVPN_A_KEYCONF_DECRYPT_DIR);
694 if (dco_cipher != OVPN_CIPHER_ALG_NONE)
695 {
696 NLA_PUT(nl_msg, OVPN_A_KEYDIR_CIPHER_KEY, (int)key_len, decrypt_key);
697 NLA_PUT(nl_msg, OVPN_A_KEYDIR_NONCE_TAIL, nonce_tail_len, decrypt_iv);
698 }
699 nla_nest_end(nl_msg, key_dec);
700
701 nla_nest_end(nl_msg, key_conf);
702
703
704 ret = ovpn_nl_msg_send(dco, nl_msg, __func__);
705
706nla_put_failure:
707 nlmsg_free(nl_msg);
708 return ret;
709}
710
711int
712dco_set_peer(dco_context_t *dco, unsigned int peerid, int keepalive_interval, int keepalive_timeout,
713 int mss)
714{
715 msg(D_DCO_DEBUG, "%s: peer-id %u, keepalive %d/%d, mss %d", __func__, peerid,
716 keepalive_interval, keepalive_timeout, mss);
717
718 struct nl_msg *nl_msg = ovpn_dco_nlmsg_create(dco, OVPN_CMD_PEER_SET);
719 if (!nl_msg)
720 {
721 return -ENOMEM;
722 }
723
724 struct nlattr *attr = nla_nest_start(nl_msg, OVPN_A_PEER);
725 int ret = -EMSGSIZE;
726 NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, peerid);
727 NLA_PUT_U32(nl_msg, OVPN_A_PEER_KEEPALIVE_INTERVAL, keepalive_interval);
728 NLA_PUT_U32(nl_msg, OVPN_A_PEER_KEEPALIVE_TIMEOUT, keepalive_timeout);
729 nla_nest_end(nl_msg, attr);
730
731 ret = ovpn_nl_msg_send(dco, nl_msg, __func__);
732
733nla_put_failure:
734 nlmsg_free(nl_msg);
735 return ret;
736}
737
738/* This function parses the reply provided by the kernel to the CTRL_CMD_GETFAMILY
739 * message. We parse the reply and we retrieve the multicast group ID associated
740 * with the "ovpn-dco" netlink family.
741 *
742 * The ID is later used to subscribe to the multicast group and be notified
743 * about any multicast message sent by the ovpn-dco kernel module.
744 */
745static int
746mcast_family_handler(struct nl_msg *msg, void *arg)
747{
748 dco_context_t *dco = arg;
749 struct nlattr *tb[CTRL_ATTR_MAX + 1];
750 const struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
751
752 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
753
754 if (!tb[CTRL_ATTR_MCAST_GROUPS])
755 {
756 return NL_SKIP;
757 }
758
759 const struct nlattr *mcgrp;
760 int rem_mcgrp;
761 nla_for_each_nested(mcgrp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mcgrp)
762 {
763 struct nlattr *tb_mcgrp[CTRL_ATTR_MCAST_GRP_MAX + 1];
764
765 nla_parse(tb_mcgrp, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mcgrp), nla_len(mcgrp), NULL);
766
767 if (!tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME] || !tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID])
768 {
769 continue;
770 }
771
772 if (strncmp(nla_data(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]), OVPN_MCGRP_PEERS,
773 nla_len(tb_mcgrp[CTRL_ATTR_MCAST_GRP_NAME]))
774 != 0)
775 {
776 continue;
777 }
778 dco->ovpn_dco_mcast_id = nla_get_u32(tb_mcgrp[CTRL_ATTR_MCAST_GRP_ID]);
779 break;
780 }
781
782 return NL_SKIP;
783}
789static int
790ovpn_get_mcast_id(dco_context_t *dco)
791{
792 dco->ovpn_dco_mcast_id = -ENOENT;
793
794 /* Even though 'nlctrl' is a constant, there seem to be no library
795 * provided define for it */
796 dco->ctrlid = genl_ctrl_resolve(dco->nl_sock, "nlctrl");
797
798 struct nl_msg *nl_msg = nlmsg_alloc();
799 if (!nl_msg)
800 {
801 return -ENOMEM;
802 }
803
804 genlmsg_put(nl_msg, 0, 0, dco->ctrlid, 0, 0, CTRL_CMD_GETFAMILY, 0);
805
806 int ret = -EMSGSIZE;
807 NLA_PUT_STRING(nl_msg, CTRL_ATTR_FAMILY_NAME, OVPN_FAMILY_NAME);
808
809 ret = ovpn_nl_msg_send(dco, nl_msg, __func__);
810
811nla_put_failure:
812 nlmsg_free(nl_msg);
813 return ret;
814}
815
816static bool
817ovpn_parse_float_addr(struct nlattr **attrs, struct sockaddr *out)
818{
819 if (!attrs[OVPN_A_PEER_REMOTE_PORT])
820 {
821 msg(D_DCO, "ovpn-dco: no remote port in PEER_FLOAT_NTF message");
822 return false;
823 }
824
825 if (attrs[OVPN_A_PEER_REMOTE_IPV4])
826 {
827 struct sockaddr_in *addr4 = (struct sockaddr_in *)out;
828 CLEAR(*addr4);
829 addr4->sin_family = AF_INET;
830 addr4->sin_port = nla_get_u16(attrs[OVPN_A_PEER_REMOTE_PORT]);
831 addr4->sin_addr.s_addr = nla_get_u32(attrs[OVPN_A_PEER_REMOTE_IPV4]);
832 return true;
833 }
834 else if (attrs[OVPN_A_PEER_REMOTE_IPV6]
835 && nla_len(attrs[OVPN_A_PEER_REMOTE_IPV6]) == sizeof(struct in6_addr))
836 {
837 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)out;
838 CLEAR(*addr6);
839 addr6->sin6_family = AF_INET6;
840 addr6->sin6_port = nla_get_u16(attrs[OVPN_A_PEER_REMOTE_PORT]);
841 memcpy(&addr6->sin6_addr, nla_data(attrs[OVPN_A_PEER_REMOTE_IPV6]),
842 sizeof(addr6->sin6_addr));
844 {
845 addr6->sin6_scope_id = nla_get_u32(attrs[OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID]);
846 }
847 return true;
848 }
849
850 msg(D_DCO, "ovpn-dco: no valid remote IP address in PEER_FLOAT_NTF message");
851 return false;
852}
853
854/* libnl < 3.11.0 does not implement nla_get_uint() */
855static uint64_t
856ovpn_nla_get_uint(const struct nlattr *attr)
857{
858 if (nla_len(attr) == sizeof(uint32_t))
859 {
860 return nla_get_u32(attr);
861 }
862 else
863 {
864 return nla_get_u64(attr);
865 }
866}
867
868static void
869dco_update_peer_stat(struct context_2 *c2, struct nlattr *tb[], uint32_t id)
870{
872 {
873 c2->dco_read_bytes = ovpn_nla_get_uint(tb[OVPN_A_PEER_LINK_RX_BYTES]);
874 msg(D_DCO_DEBUG, "%s / dco_read_bytes: " counter_format, __func__, c2->dco_read_bytes);
875 }
876 else
877 {
878 msg(M_WARN, "%s: no link RX bytes provided in reply for peer %u", __func__, id);
879 }
880
882 {
883 c2->dco_write_bytes = ovpn_nla_get_uint(tb[OVPN_A_PEER_LINK_TX_BYTES]);
884 msg(D_DCO_DEBUG, "%s / dco_write_bytes: " counter_format, __func__, c2->dco_write_bytes);
885 }
886 else
887 {
888 msg(M_WARN, "%s: no link TX bytes provided in reply for peer %u", __func__, id);
889 }
890
892 {
893 c2->tun_read_bytes = ovpn_nla_get_uint(tb[OVPN_A_PEER_VPN_RX_BYTES]);
894 msg(D_DCO_DEBUG, "%s / tun_read_bytes: " counter_format, __func__, c2->tun_read_bytes);
895 }
896 else
897 {
898 msg(M_WARN, "%s: no VPN RX bytes provided in reply for peer %u", __func__, id);
899 }
900
902 {
903 c2->tun_write_bytes = ovpn_nla_get_uint(tb[OVPN_A_PEER_VPN_TX_BYTES]);
904 msg(D_DCO_DEBUG, "%s / tun_write_bytes: " counter_format, __func__, c2->tun_write_bytes);
905 }
906 else
907 {
908 msg(M_WARN, "%s: no VPN TX bytes provided in reply for peer %u", __func__, id);
909 }
910}
911
912static int
913ovpn_handle_peer(dco_context_t *dco, struct nlattr *attrs[])
914{
915 if (!attrs[OVPN_A_PEER])
916 {
917 msg(D_DCO_DEBUG, "%s: malformed reply", __func__);
918 return NL_SKIP;
919 }
920
921 struct nlattr *tb_peer[OVPN_A_PEER_MAX + 1];
922 nla_parse_nested(tb_peer, OVPN_A_PEER_MAX, attrs[OVPN_A_PEER], NULL);
923
924 if (!tb_peer[OVPN_A_PEER_ID])
925 {
926 msg(M_WARN, "ovpn-dco: no peer-id provided in PEER_GET reply");
927 return NL_SKIP;
928 }
929
930 uint32_t peer_id = nla_get_u32(tb_peer[OVPN_A_PEER_ID]);
931 struct context_2 *c2;
932
933 msg(D_DCO_DEBUG | M_NOIPREFIX, "%s: parsing message for peer %u...", __func__, peer_id);
934
935 if (dco->ifmode == OVPN_MODE_P2P)
936 {
937 c2 = &dco->c->c2;
938 if (c2->tls_multi->dco_peer_id != (int)peer_id)
939 {
940 return NL_SKIP;
941 }
942 }
943 else
944 {
945 if (peer_id >= dco->c->multi->max_clients)
946 {
947 msg(M_WARN, "%s: received out of bound peer_id %u (max=%u)", __func__, peer_id,
948 dco->c->multi->max_clients);
949 return NL_SKIP;
950 }
951
952 struct multi_instance *mi = dco->c->multi->instances[peer_id];
953 if (!mi)
954 {
955 msg(M_WARN | M_NOIPREFIX, "%s: received data for a non-existing peer %u", __func__, peer_id);
956 return NL_SKIP;
957 }
958
959 c2 = &mi->context.c2;
960 }
961
962 dco_update_peer_stat(c2, tb_peer, peer_id);
963
964 return NL_OK;
965}
966
967static bool
968ovpn_iface_check(const dco_context_t *dco, struct nlattr *attrs[])
969{
970 /* we must know which interface this message is referring to in order to
971 * avoid mixing messages for other instances
972 */
973 if (!attrs[OVPN_A_IFINDEX])
974 {
975 msg(D_DCO, "ovpn-dco: Received message without ifindex");
976 return false;
977 }
978
979 uint32_t ifindex = nla_get_u32(attrs[OVPN_A_IFINDEX]);
980 if (ifindex != dco->ifindex)
981 {
982 msg(D_DCO_DEBUG, "ovpn-dco: ignoring message for foreign ifindex %u", ifindex);
983 return false;
984 }
985
986 return true;
987}
988
989static int
990ovpn_handle_peer_del_ntf(dco_context_t *dco, struct nlattr *attrs[])
991{
992 if (!ovpn_iface_check(dco, attrs))
993 {
994 return NL_STOP;
995 }
996
997 if (!attrs[OVPN_A_PEER])
998 {
999 msg(D_DCO | M_NOIPREFIX, "ovpn-dco: no peer in PEER_DEL_NTF message");
1000 return NL_STOP;
1001 }
1002
1003 struct nlattr *dp_attrs[OVPN_A_PEER_MAX + 1];
1004 if (nla_parse_nested(dp_attrs, OVPN_A_PEER_MAX, attrs[OVPN_A_PEER], NULL))
1005 {
1006 msg(D_DCO | M_NOIPREFIX, "ovpn-dco: can't parse peer in PEER_DEL_NTF messsage");
1007 return NL_STOP;
1008 }
1009
1010 if (!dp_attrs[OVPN_A_PEER_DEL_REASON])
1011 {
1012 msg(D_DCO | M_NOIPREFIX, "ovpn-dco: no reason in PEER_DEL_NTF message");
1013 return NL_STOP;
1014 }
1015 if (!dp_attrs[OVPN_A_PEER_ID])
1016 {
1017 msg(D_DCO | M_NOIPREFIX, "ovpn-dco: no peer-id in PEER_DEL_NTF message");
1018 return NL_STOP;
1019 }
1020
1021 int reason = nla_get_u32(dp_attrs[OVPN_A_PEER_DEL_REASON]);
1022 unsigned int peerid = nla_get_u32(dp_attrs[OVPN_A_PEER_ID]);
1023
1024 msg(D_DCO_DEBUG | M_NOIPREFIX, "ovpn-dco: received CMD_PEER_DEL_NTF, ifindex: %u, peer-id %u, reason: %d",
1025 dco->ifindex, peerid, reason);
1026 dco->dco_message_peer_id = peerid;
1027 dco->dco_del_peer_reason = reason;
1028 dco->dco_message_type = OVPN_CMD_PEER_DEL_NTF;
1029
1030 return NL_OK;
1031}
1032
1033static int
1034ovpn_handle_peer_float_ntf(dco_context_t *dco, struct nlattr *attrs[])
1035{
1036 if (!ovpn_iface_check(dco, attrs))
1037 {
1038 return NL_STOP;
1039 }
1040
1041 if (!attrs[OVPN_A_PEER])
1042 {
1043 msg(D_DCO, "ovpn-dco: no peer in PEER_FLOAT_NTF message");
1044 return NL_STOP;
1045 }
1046
1047 struct nlattr *fp_attrs[OVPN_A_PEER_MAX + 1];
1048 if (nla_parse_nested(fp_attrs, OVPN_A_PEER_MAX, attrs[OVPN_A_PEER], NULL))
1049 {
1050 msg(D_DCO, "ovpn-dco: can't parse peer in PEER_FLOAT_NTF messsage");
1051 return NL_STOP;
1052 }
1053
1054 if (!fp_attrs[OVPN_A_PEER_ID])
1055 {
1056 msg(D_DCO, "ovpn-dco: no peer-id in PEER_FLOAT_NTF message");
1057 return NL_STOP;
1058 }
1059 uint32_t peerid = nla_get_u32(fp_attrs[OVPN_A_PEER_ID]);
1060
1061 if (!ovpn_parse_float_addr(fp_attrs, (struct sockaddr *)&dco->dco_float_peer_ss))
1062 {
1063 return NL_STOP;
1064 }
1065
1066 struct gc_arena gc = gc_new();
1067 msg(D_DCO_DEBUG, "ovpn-dco: received CMD_PEER_FLOAT_NTF, ifindex: %u, peer-id %u, address: %s",
1068 dco->ifindex, peerid, print_sockaddr((struct sockaddr *)&dco->dco_float_peer_ss, &gc));
1069 dco->dco_message_peer_id = (int)peerid;
1070 dco->dco_message_type = OVPN_CMD_PEER_FLOAT_NTF;
1071
1072 gc_free(&gc);
1073
1074 return NL_OK;
1075}
1076
1077static int
1078ovpn_handle_key_swap_ntf(dco_context_t *dco, struct nlattr *attrs[])
1079{
1080 if (!ovpn_iface_check(dco, attrs))
1081 {
1082 return NL_STOP;
1083 }
1084
1085 if (!attrs[OVPN_A_KEYCONF])
1086 {
1087 msg(D_DCO, "ovpn-dco: no keyconf in KEY_SWAP_NTF message");
1088 return NL_STOP;
1089 }
1090
1091 struct nlattr *dp_attrs[OVPN_A_KEYCONF_MAX + 1];
1092 if (nla_parse_nested(dp_attrs, OVPN_A_KEYCONF_MAX, attrs[OVPN_A_KEYCONF], NULL))
1093 {
1094 msg(D_DCO, "ovpn-dco: can't parse keyconf in KEY_SWAP_NTF message");
1095 return NL_STOP;
1096 }
1097 if (!dp_attrs[OVPN_A_KEYCONF_PEER_ID])
1098 {
1099 msg(D_DCO, "ovpn-dco: no peer-id in KEY_SWAP_NTF message");
1100 return NL_STOP;
1101 }
1102 if (!dp_attrs[OVPN_A_KEYCONF_KEY_ID])
1103 {
1104 msg(D_DCO, "ovpn-dco: no key-id in KEY_SWAP_NTF message");
1105 return NL_STOP;
1106 }
1107
1108 int key_id = nla_get_u16(dp_attrs[OVPN_A_KEYCONF_KEY_ID]);
1109 unsigned int peer_id = nla_get_u32(dp_attrs[OVPN_A_KEYCONF_PEER_ID]);
1110
1111 msg(D_DCO_DEBUG, "ovpn-dco: received CMD_KEY_SWAP_NTF, ifindex: %u, peer-id %u, key-id: %d",
1112 dco->ifindex, peer_id, key_id);
1113 dco->dco_message_peer_id = peer_id;
1114 dco->dco_message_key_id = key_id;
1115 dco->dco_message_type = OVPN_CMD_KEY_SWAP_NTF;
1116
1117 return NL_OK;
1118}
1119
1120/* This function parses any netlink message sent by ovpn-dco to userspace */
1121static int
1122ovpn_handle_msg(struct nl_msg *msg, void *arg)
1123{
1124 dco_context_t *dco = arg;
1125
1126 struct nlattr *attrs[OVPN_A_MAX + 1];
1127 struct nlmsghdr *nlh = nlmsg_hdr(msg);
1128 struct genlmsghdr *gnlh = genlmsg_hdr(nlh);
1129
1130 msg(D_DCO_DEBUG | M_NOIPREFIX, "ovpn-dco: received netlink message type=%u cmd=%u flags=%#.4x",
1131 nlh->nlmsg_type, gnlh->cmd, nlh->nlmsg_flags);
1132
1133 /* if we get a message from the NLCTRL family, it means
1134 * this is the reply to the mcast ID resolution request
1135 * and we parse it accordingly.
1136 */
1137 if (nlh->nlmsg_type == dco->ctrlid)
1138 {
1139 msg(D_DCO_DEBUG, "ovpn-dco: received CTRLID message");
1140 return mcast_family_handler(msg, dco);
1141 }
1142
1143 if (!genlmsg_valid_hdr(nlh, 0))
1144 {
1145 msg(D_DCO, "ovpn-dco: invalid header");
1146 return NL_STOP;
1147 }
1148
1149 if (nla_parse(attrs, OVPN_A_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL))
1150 {
1151 msg(D_DCO, "received bogus data from ovpn-dco");
1152 return NL_STOP;
1153 }
1154
1155 /* based on the message type, we parse the subobject contained in the
1156 * message, that stores the type-specific attributes.
1157 *
1158 * the "dco" object is then filled accordingly with the information
1159 * retrieved from the message, so that *process_incoming_dco can react
1160 * as need be.
1161 */
1162 int ret;
1163 switch (gnlh->cmd)
1164 {
1165 case OVPN_CMD_PEER_GET:
1166 {
1167 /* return directly, there are no messages to pass to *process_incoming_dco() */
1168 return ovpn_handle_peer(dco, attrs);
1169 }
1170
1172 {
1173 ret = ovpn_handle_peer_del_ntf(dco, attrs);
1174 break;
1175 }
1176
1178 {
1179 ret = ovpn_handle_peer_float_ntf(dco, attrs);
1180 break;
1181 }
1182
1184 {
1185 ret = ovpn_handle_key_swap_ntf(dco, attrs);
1186 break;
1187 }
1188
1189 default:
1190 msg(D_DCO, "ovpn-dco: received unknown command: %d", gnlh->cmd);
1191 dco->dco_message_type = 0;
1192 return NL_STOP;
1193 }
1194
1195 if (ret != NL_OK)
1196 {
1197 return ret;
1198 }
1199
1200 if (dco->c->mode == CM_TOP)
1201 {
1203 }
1204 else
1205 {
1207 }
1208
1209 return NL_OK;
1210}
1211
1212int
1214{
1215 msg(D_DCO_DEBUG, __func__);
1216
1217 /* Drain the multicast notification socket. This is the only place where
1218 * asynchronous notifications (peer del/float, key swap) are processed, so
1219 * the multi_close_instance() they may trigger happens at a safe point in
1220 * the event loop rather than re-entrantly inside a request/reply. */
1221 int ret = nl_recvmsgs(dco->nl_sock_notify, dco->nl_cb_notify);
1222
1223 return ovpn_nl_recvmsgs_report(ret, __func__);
1224}
1225
1226static int
1227dco_get_peer(dco_context_t *dco, int peer_id, const bool raise_sigusr1_on_err)
1228{
1229 ASSERT(dco);
1230
1231 /* peer_id == -1 means "dump all peers", but this is allowed in MP mode only.
1232 * If it happens in P2P mode it means that the DCO peer was deleted and we
1233 * can simply bail out
1234 */
1235 if (peer_id == -1 && dco->ifmode == OVPN_MODE_P2P)
1236 {
1237 return 0;
1238 }
1239
1240 msg(D_DCO_DEBUG | M_NOIPREFIX, "%s: peer-id %d", __func__, peer_id);
1241
1242 struct nl_msg *nl_msg = ovpn_dco_nlmsg_create(dco, OVPN_CMD_PEER_GET);
1243 if (!nl_msg)
1244 {
1245 return -ENOMEM;
1246 }
1247
1248 struct nlattr *attr = nla_nest_start(nl_msg, OVPN_A_PEER);
1249 int ret = -EMSGSIZE;
1250
1251 if (peer_id != -1)
1252 {
1253 NLA_PUT_U32(nl_msg, OVPN_A_PEER_ID, peer_id);
1254 }
1255 else
1256 {
1257 nlmsg_hdr(nl_msg)->nlmsg_flags |= NLM_F_DUMP;
1258 }
1259 nla_nest_end(nl_msg, attr);
1260
1261 ret = ovpn_nl_msg_send(dco, nl_msg, __func__);
1262
1263nla_put_failure:
1264 nlmsg_free(nl_msg);
1265
1266 if (raise_sigusr1_on_err && ret < 0)
1267 {
1268 msg(M_WARN, "Error retrieving DCO peer stats: the underlying DCO peer"
1269 "may have been deleted from the kernel without notifying "
1270 "userspace. Restarting the session");
1271 register_signal(dco->c->sig, SIGUSR1, "dco peer stats error");
1272 }
1273 return ret;
1274}
1275
1276#if defined(__GNUC__) || defined(__clang__)
1277#pragma GCC diagnostic pop
1278#endif
1279
1280int
1281dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err)
1282{
1283 if (!c->c1.tuntap || c->c1.tuntap->dco.ifindex == 0)
1284 {
1285 return -1;
1286 }
1287
1288 return dco_get_peer(&c->c1.tuntap->dco, c->c2.tls_multi->dco_peer_id, raise_sigusr1_on_err);
1289}
1290
1291int
1292dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err)
1293{
1294 return dco_get_peer(dco, -1, raise_sigusr1_on_err);
1295}
1296
1297bool
1298dco_available(msglvl_t msglevel)
1299{
1300 if (resolve_ovpn_netlink_id(D_DCO_DEBUG) < 0)
1301 {
1302 msg(msglevel, "Note: Kernel support for ovpn interfaces missing, "
1303 "disabling data channel offload. Use Linux 6.16.0 or "
1304 "newer with ovpn support or use ovpn-backports for "
1305 "interface support.");
1306 return false;
1307 }
1308
1309 return true;
1310}
1311
1316static const char *
1317dco_version_string_in_tree(struct gc_arena *gc)
1318{
1319 struct buffer buf = alloc_buf_gc(256, gc);
1320 struct utsname system;
1321
1322 if (uname(&system))
1323 {
1324 return "ERR";
1325 }
1326
1327 buf_puts(&buf, system.release);
1328 buf_puts(&buf, " ");
1329 buf_puts(&buf, system.version);
1330 return BSTR(&buf);
1331}
1332
1339static const char *
1340dco_version_string_backports(FILE *fp, struct gc_arena *gc)
1341{
1342 char *str = gc_malloc(PATH_MAX, false, gc);
1343
1344 if (!fgets(str, PATH_MAX, fp))
1345 {
1346 return "ERR";
1347 }
1348
1349 /* remove potential newline at the end of the string */
1350 char *nl = strchr(str, '\n');
1351 if (nl)
1352 {
1353 *nl = '\0';
1354 }
1355
1356 return str;
1357}
1358
1359const char *
1361{
1362 const char *version;
1363 struct stat sb;
1364 FILE *fp;
1365
1366 if (stat("/sys/module/ovpn", &sb) != 0 || !S_ISDIR(sb.st_mode))
1367 {
1368 return "N/A";
1369 }
1370
1371 /* now that we know for sure that the module is loaded, if there's no
1372 * version file it means we're dealing with the in-tree version, otherwise
1373 * it's backports */
1374 fp = fopen("/sys/module/ovpn/version", "r");
1375 if (!fp)
1376 {
1377 return dco_version_string_in_tree(gc);
1378 }
1379 version = dco_version_string_backports(fp, gc);
1380
1381 fclose(fp);
1382 return version;
1383}
1384
1385void
1386dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
1387{
1388 if (dco && dco->nl_sock_notify)
1389 {
1390 /* Only the notification socket is monitored by the event loop. The
1391 * request/reply socket (dco->nl_sock) is drained synchronously by
1392 * ovpn_nl_msg_send(). */
1393 event_ctl(es, nl_socket_get_fd(dco->nl_sock_notify), EVENT_READ, arg);
1394 }
1395}
1396
1397const char *
1399{
1400 return "AES-128-GCM:AES-256-GCM:AES-192-GCM:CHACHA20-POLY1305";
1401}
1402
1403bool
1404dco_supports_epoch_data(const struct context *c)
1405{
1406 return false;
1407}
1408
1409#endif /* defined(ENABLE_DCO) && defined(TARGET_LINUX) */
void * gc_malloc(size_t size, bool clear, struct gc_arena *a)
Allocate memory and, optionally, zero it.
Definition buffer.c:321
void buf_puts(struct buffer *buf, const char *str)
Append a string to a buffer with overflow check.
Definition buffer.c:256
struct buffer alloc_buf_gc(size_t size, struct gc_arena *gc)
Allocate a buffer of the given size under garbage collection.
Definition buffer.c:77
Buffer management functions and garbage collection.
#define BSTR(buf)
Return the buffer content pointer cast to char *.
Definition buffer.h:157
#define ALLOC_OBJ_CLEAR_GC(dptr, type, gc)
Allocate and zero-initialise a garbage-collected object of the given type.
Definition buffer.h:2132
static void gc_free(struct gc_arena *a)
Free all allocations in a garbage collection arena.
Definition buffer.h:1974
static struct gc_arena gc_new(void)
Allocate and return a new, empty garbage collection arena.
Definition buffer.h:1958
#define counter_format
Definition common.h:32
unsigned int cipher_kt_key_size(const char *ciphername)
Returns the size of keys used by the cipher, in bytes.
static int dco_get_peer_stats_multi(dco_context_t *dco, const bool raise_sigusr1_on_err)
Definition dco.h:369
static int dco_set_peer(dco_context_t *dco, unsigned int peerid, int keepalive_interval, int keepalive_timeout, int mss)
Definition dco.h:341
static const char * dco_get_supported_ciphers(void)
Definition dco.h:381
static void dco_event_set(dco_context_t *dco, struct event_set *es, void *arg)
Definition dco.h:316
static int open_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx, const char *dev)
Definition dco.h:298
static int dco_read_and_process(dco_context_t *dco)
Definition dco.h:309
static bool dco_supports_epoch_data(const struct context *c)
Definition dco.h:387
static bool dco_available(msglvl_t msglevel)
Definition dco.h:262
static bool ovpn_dco_init(struct context *c)
Definition dco.h:292
void * dco_context_t
Definition dco.h:259
static const char * dco_version_string(struct gc_arena *gc)
Definition dco.h:268
static void close_tun_dco(struct tuntap *tt, openvpn_net_ctx_t *ctx)
Definition dco.h:304
static int dco_get_peer_stats(struct context *c, const bool raise_sigusr1_on_err)
Definition dco.h:375
int dco_del_key(dco_context_t *dco, unsigned int peerid, dco_key_slot_t slot)
Definition dco_win.c:582
int dco_del_peer(dco_context_t *dco, unsigned int peerid)
Definition dco_win.c:467
int dco_swap_keys(dco_context_t *dco, unsigned int peer_id)
Definition dco_win.c:590
int dco_new_peer(dco_context_t *dco, unsigned int peerid, socket_descriptor_t sd, struct sockaddr *localaddr, struct sockaddr *remoteaddr, const struct in_addr *vpn_ipv4, const struct in6_addr *vpn_ipv6)
Definition dco_win.c:414
int dco_new_key(dco_context_t *dco, unsigned int peerid, int keyid, dco_key_slot_t slot, const uint8_t *encrypt_key, const uint8_t *encrypt_iv, const uint8_t *decrypt_key, const uint8_t *decrypt_iv, const char *ciphername, bool epoch)
Definition dco_win.c:527
#define D_DCO
Definition errlevel.h:92
#define D_DCO_DEBUG
Definition errlevel.h:115
#define M_INFO
Definition errlevel.h:54
#define EVENT_READ
Definition event.h:37
static void event_ctl(struct event_set *es, event_t event, unsigned int rwflags, void *arg)
Definition event.h:180
void set_nonblock(socket_descriptor_t fd)
Definition fdmisc.c:77
void set_cloexec(socket_descriptor_t fd)
Definition fdmisc.c:87
void process_incoming_dco(dco_context_t *dco)
Process an incoming DCO message (from kernel space).
Definition forward.c:1264
static SERVICE_STATUS status
Definition interactive.c:52
Header file for server-mode related structures and functions.
void multi_process_incoming_dco(dco_context_t *dco)
Process an incoming DCO message (from kernel space).
void * openvpn_net_ctx_t
Definition networking.h:38
#define CLEAR(x)
Definition basic.h:32
#define M_NOIPREFIX
Definition error.h:103
#define M_FATAL
Definition error.h:90
#define M_NONFATAL
Definition error.h:91
#define msg(flags,...)
Definition error.h:152
unsigned int msglvl_t
Definition error.h:77
#define ASSERT(x)
Definition error.h:221
#define M_WARN
Definition error.h:92
#define CM_P2P
Definition openvpn.h:453
#define CM_TOP
Definition openvpn.h:454
@ OVPN_CIPHER_ALG_NONE
@ OVPN_A_KEYCONF
@ OVPN_A_MAX
@ OVPN_A_PEER
@ OVPN_A_IFINDEX
@ OVPN_CMD_PEER_FLOAT_NTF
@ OVPN_CMD_KEY_DEL
@ OVPN_CMD_PEER_NEW
@ OVPN_CMD_PEER_GET
@ OVPN_CMD_KEY_SWAP
@ OVPN_CMD_KEY_SWAP_NTF
@ OVPN_CMD_PEER_DEL_NTF
@ OVPN_CMD_PEER_SET
@ OVPN_CMD_KEY_NEW
@ OVPN_CMD_PEER_DEL
@ OVPN_A_PEER_LINK_RX_BYTES
@ OVPN_A_PEER_VPN_IPV6
@ OVPN_A_PEER_LINK_TX_BYTES
@ OVPN_A_PEER_VPN_IPV4
@ OVPN_A_PEER_KEEPALIVE_TIMEOUT
@ OVPN_A_PEER_VPN_TX_BYTES
@ OVPN_A_PEER_LOCAL_IPV6
@ OVPN_A_PEER_REMOTE_IPV6
@ OVPN_A_PEER_VPN_RX_BYTES
@ OVPN_A_PEER_MAX
@ OVPN_A_PEER_LOCAL_IPV4
@ OVPN_A_PEER_DEL_REASON
@ OVPN_A_PEER_ID
@ OVPN_A_PEER_SOCKET
@ OVPN_A_PEER_REMOTE_IPV4
@ OVPN_A_PEER_KEEPALIVE_INTERVAL
@ OVPN_A_PEER_REMOTE_IPV6_SCOPE_ID
@ OVPN_A_PEER_REMOTE_PORT
#define OVPN_FAMILY_NAME
#define OVPN_MCGRP_PEERS
@ OVPN_A_KEYCONF_SLOT
@ OVPN_A_KEYCONF_PEER_ID
@ OVPN_A_KEYCONF_MAX
@ OVPN_A_KEYCONF_DECRYPT_DIR
@ OVPN_A_KEYCONF_CIPHER_ALG
@ OVPN_A_KEYCONF_ENCRYPT_DIR
@ OVPN_A_KEYCONF_KEY_ID
@ OVPN_A_KEYDIR_CIPHER_KEY
@ OVPN_A_KEYDIR_NONCE_TAIL
@ OVPN_MODE_MP
@ OVPN_MODE_P2P
#define DEV_TYPE_TUN
Definition proto.h:35
void register_signal(struct signal_info *si, int signum, const char *signal_text)
Register a soft signal in the signal_info struct si respecting priority.
Definition sig.c:228
static const char * print_sockaddr(const struct sockaddr *addr, struct gc_arena *gc)
Definition socket_util.h:77
Control Channel SSL/Data channel negotiation module.
Control Channel Verification Module.
Wrapper structure for dynamically allocated memory.
Definition buffer.h:71
struct tuntap * tuntap
Tun/tap virtual network interface.
Definition openvpn.h:155
Level 2 context containing state that is reset on both SIGHUP and SIGUSR1 restarts.
Definition openvpn.h:207
counter_type dco_read_bytes
Definition openvpn.h:250
counter_type tun_read_bytes
Definition openvpn.h:247
counter_type dco_write_bytes
Definition openvpn.h:253
struct tls_multi * tls_multi
TLS state structure for this VPN tunnel.
Definition openvpn.h:300
counter_type tun_write_bytes
Definition openvpn.h:248
Contains all state information for one tunnel.
Definition openvpn.h:445
int mode
Role of this context within the OpenVPN process.
Definition openvpn.h:458
struct context_2 c2
Level 2 context.
Definition openvpn.h:490
struct context_1 c1
Level 1 context.
Definition openvpn.h:489
Garbage collection arena used to keep track of dynamically allocated memory.
Definition buffer.h:127
Container for two sets of OpenSSL cipher and/or HMAC contexts for both sending and receiving directio...
Definition crypto.h:273
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:145
Server-mode state structure for one single VPN tunnel.
Definition multi.h:102
struct context context
The context structure storing state for this VPN tunnel.
Definition multi.h:150
int dco_peer_id
This is the handle that DCO uses to identify this session with the kernel.
Definition ssl_common.h:733
Definition tun.h:181
int type
Definition tun.h:183
dco_context_t dco
Definition tun.h:247
char * actual_name
Definition tun.h:205
#define SIGUSR1
Definition syshead.h:57
__attribute__((unused))
Definition test.c:42
struct gc_arena gc
Definition test_ssl.c:138