From patchwork Mon Nov 30 09:06:45 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 5818 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:6022:91c9:10b0:11b:d6c:f273 with SMTP id tl9-n1csp12569847lad; Mon, 30 Nov 2026 01:07:23 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErFcG/17fe2Bs4KKrzV2VrkOWN0yV1ccKbruEoQVdOqhMv33DEbW7Y5v6txA+GEBUivxOh7P6ovPT98=@openvpn.net X-Received: by 2002:a05:7022:b908:b0:1b5:25e:9582 with SMTP id a92af1059eb24-1b55691dbe2mr28126307c88.37.1796029642977; Mon, 30 Nov 2026 01:07:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1796029642; cv=none; d=google.com; s=arc-20260327; b=htuVm5PZfpxZT4psGZoPEy7iUR4/AilaJWBzG2ZyrQ95y0E3e/YCg8fKrEl8wekI+a gpaWwwPztR6E9MV3ixSPsNiIGYAV8q2uI+695nhQFYjxpT1lVHegZftbhl2SdPu1azOu VvGhp8oUJif3xRetgFhL1AcK0ZPETjo3MdUUa32e3b1vng2RujY2z72/1lT6YmcHx6Zl 335VzQMzCaM7Tz/+xIiIhXkI42sBzjmCEXbtIQAli99eY4pxe4ZeSgPtBo8cQFoOfXN0 dVe91RbydW0eq/5vNzYfRfDwQQ6y5bXoezY9rkDi05+CYTXgo0fhZ1zjIscw52OI/KM3 HKlg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20260327; h=errors-to:content-transfer-encoding:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence:subject :mime-version:references:in-reply-to:message-id:date:to:from :dkim-signature:dkim-signature:dkim-signature; bh=vO7wMWsLYwhIS47R2ByoJpFTVmkLLgYjmRe+KU5f4Mg=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=iAtMtS3QEBHhGrEH+KSn5XU/Sl9wqRBVed0kimd+Sexvye1RL4Zi8kyW24KyImZn24 xkfQopJjinIb/WJRhQpw8e6+NcTqrCWiniRgGAd3q+NG21x/QB2GpV1Vo2njD7L7+A4y bMBBSZW/ussR5I9YPyC4zzUGMoNXA0kaE8WC82nP5lk6/u6+A+r5CRLTXkn0dduhK2ux f5Q2GrM5u4EA8B4UK/2kLB0wqh17yT3XNdDc4KF7869QB8CMwP8SMgWKDDSJrZAiNSWk LN3+Rf424Q2eNRwmWgkcplW5rkBK3Nho4606Fn1/MEX1NqGCyCOsPn87kc65nxwgF950 7BaQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=EHkvyuC9; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=Q7erY2Jg; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=hACyEQJ3; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de Received: from lists.sourceforge.net (lists.sourceforge.net. [216.105.38.7]) by mx.google.com with ESMTPS id a92af1059eb24-1b5567a864fsi100583207c88.16.2026.11.30.01.07.22 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 30 Nov 2026 01:07:22 -0800 (PST) Received-SPF: pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) client-ip=216.105.38.7; Authentication-Results: mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=EHkvyuC9; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=Q7erY2Jg; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=hACyEQJ3; spf=pass (google.com: domain of openvpn-devel-bounces@lists.sourceforge.net designates 216.105.38.7 as permitted sender) smtp.mailfrom=openvpn-devel-bounces@lists.sourceforge.net; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=muc.de DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: Subject:MIME-Version:References:In-Reply-To:Message-ID:Date:To:From:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Unsubscribe-Post: List-Owner; bh=vO7wMWsLYwhIS47R2ByoJpFTVmkLLgYjmRe+KU5f4Mg=; b=EHkvyuC9s8YSZz xSmAz38w5IBpVr+JcT8wqnAXRzfbP9EQYVYMKuDS8KXMK1yPztjZ6GG4BaqgJYq07rAmq4rJsrzXP 29FFVFm6lHGZy/Pr50+9zxCN387hHPL58ydQcqUjo71o01wuRep55Sk0LJ89czAhHzxR8FG5Pktwt TJA=; Received: from [127.0.0.1] (helo=sfs-ml-1.v29.lw.sourceforge.com) by sfs-ml-1.v29.lw.sourceforge.com with esmtp (Exim 4.100.1) (envelope-from ) id 1xXxM1-000000008Gc-32Ua; Mon, 30 Nov 2026 09:07:21 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.100.1) (envelope-from ) id 1xXxLo-000000008CC-0RDk for openvpn-devel@lists.sourceforge.net; Mon, 30 Nov 2026 09:07:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Unsubscribe-Post:List-Subscribe:List-Post:List-Owner:List-Archive; bh=MFbn8fa1aXrlP/pCYrOyRQfhK2mn8BbPYjGworJGzqY=; b=Q7erY2JgT0JB/INF1K/bS+fDQv nAJNRfiXRiHONue+XIkgpnSzvq3IlxfppeC/VgEz52TQaT6eYhLoZ5ifQRGfQdXNjztiosr/cGk+u OIL5tgWcB3X28IuEkUggwkom5RVwLZOxGGLlvf61wrEKORUz7lo/h8QyPavJaQYCHum0=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Unsubscribe-Post: List-Subscribe:List-Post:List-Owner:List-Archive; bh=MFbn8fa1aXrlP/pCYrOyRQfhK2mn8BbPYjGworJGzqY=; b=hACyEQJ3Pgro9c0N8hOW5gl9Xx ggurl54+7nZcQKlb9/sTnpXh/krd27Uic891UjWmrK30/p2ZKUwaFdQrj0LuPrLQdOKzWmTF+rn1S oWShn+raEeiV5CTP4Hzhr6hXwJsIgEeg0mpNae65V6qtQMuGh3ANTPGUCMhjqPotGDnM=; Received: from [193.149.48.129] (helo=blue.greenie.muc.de) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.100.1) id 1xXxLf-0000000060n-49nO for openvpn-devel@lists.sourceforge.net; Mon, 30 Nov 2026 09:07:03 +0000 Received: from blue.greenie.muc.de (localhost [127.0.0.1]) by blue.greenie.muc.de (8.18.1/8.18.1) with ESMTP id 6AU96rU1012039 for ; Mon, 30 Nov 2026 10:06:53 +0100 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.2/8.18.1/Submit) id 6AU96rd8012038 for openvpn-devel@lists.sourceforge.net; Mon, 30 Nov 2026 10:06:53 +0100 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Mon, 30 Nov 2026 10:06:45 +0100 Message-ID: <20261130090650.11956-1-gert@greenie.muc.de> X-Mailer: git-send-email 2.55.0 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Score: 1.3 (+) X-Spam-Report: Spam detection software, running on the system "sfi-spamd-1.hosts.colo.sdot.me", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: From: Gianmarco De Gregori The ovpn module lives in the kernel, so the attributes it accepts may differ from what we were built against. CTRL_CMD_GETPOLICY dumps the validation policy of a family: which attributes each command [...] Content analysis details: (1.3 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 1.3 RDNS_NONE Delivered to internal network by a host with no rDNS X-Headers-End: 1xXxLf-0000000060n-49nO Subject: [Openvpn-devel] [PATCH v6] dco: detect local kernel capabilities through netlink policies X-BeenThere: openvpn-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox X-GMAIL-THRID: 1883018958039555808 X-GMAIL-MSGID: 1883273578732861158 From: Gianmarco De Gregori The ovpn module lives in the kernel, so the attributes it accepts may differ from what we were built against. CTRL_CMD_GETPOLICY dumps the validation policy of a family: which attributes each command takes, of what type, and for a nested one the sub-policy that validates it. Add a helper to query it, wrapped in dco_probe_capabilities() so the rest of the tree never talks to netlink itself. Answering a requirement means following a chain - which policy validates the command, where its nested attribute points if it has one, what type the target attribute has there. One dump of the whole family answers every requirement: it is recorded as it arrives and the chains are followed once it is complete. The answer has three values rather than two: without "could not ask", a netlink error would look the same as a kernel without the feature. The query needs uapi headers from 5.10 or later. Built against older ones, it is left out and dco_probe_capabilities() reports no capability. No capability is defined yet, so the bitmap comes back empty; the rows that fill it arrive with the features that need them. The unit test feeds hand-built dumps to the pass and checks what each requirement resolves to. Change-Id: I0051effe7f0636e30557d9ce92c50c458002f9e6 Signed-off-by: Gianmarco De Gregori Acked-by: Ralf Lici Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1981 --- This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master. Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/1981 This mail reflects revision 6 of this Change. Acked-by according to Gerrit (reflected above): Ralf Lici diff --git a/CMakeLists.txt b/CMakeLists.txt index c6dcf65..f365b44 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ include(CheckLinkerFlag OPTIONAL) include(CheckTypeSize) include(CheckStructHasMember) +include(CheckCSourceCompiles) include(CTest) option(UNSUPPORTED_BUILDS "Allow unsupported builds" OFF) @@ -205,6 +206,11 @@ check_symbol_exists(getpeereid "unistd.h;sys/socket.h" HAVE_GETPEEREID) endif() +if (ENABLE_LINUXDCO) + check_c_source_compiles("#include +int main(void) { return CTRL_ATTR_OP_POLICY; }" HAVE_DECL_CTRL_ATTR_OP_POLICY) +endif () + check_include_files(grp.h HAVE_GRP_H) if (HAVE_GRP_H) check_symbol_exists(getgrnam grp.h HAVE_GETGRNAM) @@ -730,6 +736,12 @@ ) endif () + if (ENABLE_LINUXDCO AND HAVE_DECL_CTRL_ATTR_OP_POLICY) + list(APPEND unit_tests + "test_dco" + ) + endif () + if (NOT WIN32 AND ${ENABLE_PKCS11}) set(_HAVE_SOFTHSM2 YES) find_program(P11TOOL p11tool) @@ -973,6 +985,14 @@ ) endif () + if (TARGET test_dco) + target_sources(test_dco PRIVATE + tests/unit_tests/openvpn/mock_dco_dependencies.c + tests/unit_tests/openvpn/mock_get_random.c + src/openvpn/fdmisc.c + ) + endif () + target_compile_definitions(test_dhcp PRIVATE DHCP_UNIT_TEST) target_sources(test_dhcp PRIVATE tests/unit_tests/openvpn/mock_get_random.c diff --git a/config.h.cmake.in b/config.h.cmake.in index f7818cf..aa21532 100644 --- a/config.h.cmake.in +++ b/config.h.cmake.in @@ -87,6 +87,9 @@ /* Define to 1 if you have the `daemon' function. */ #cmakedefine HAVE_DAEMON +/* Define to 1 if linux/genetlink.h has netlink policy introspection (5.10) */ +#cmakedefine01 HAVE_DECL_CTRL_ATTR_OP_POLICY + /* Define to 1 if you have the header file. */ #cmakedefine HAVE_DIRECT_H diff --git a/configure.ac b/configure.ac index 05fa964..ef05253 100644 --- a/configure.ac +++ b/configure.ac @@ -664,6 +664,7 @@ ;; esac +dco_unit_tests="no" if test "$enable_dco" != "no"; then enable_dco_arg="$enable_dco" if test "${enable_iproute2}" = "yes"; then @@ -692,6 +693,12 @@ OPTIONAL_LIBNL_GENL_CFLAGS="${LIBNL_GENL_CFLAGS}" OPTIONAL_LIBNL_GENL_LIBS="${LIBNL_GENL_LIBS}" + dnl + dnl Netlink policy introspection needs uapi headers from 5.10 + dnl + AC_CHECK_DECLS([CTRL_ATTR_OP_POLICY], , , [[#include ]]) + dco_unit_tests="${ac_cv_have_decl_CTRL_ATTR_OP_POLICY}" + AC_DEFINE(ENABLE_DCO, 1, [Enable shared data channel offload]) AC_MSG_NOTICE([Enabled ovpn-dco (via ovpn kernel module) support for Linux]) fi @@ -1340,6 +1347,7 @@ AC_SUBST([PLUGIN_AUTH_PAM_LIBS]) AM_CONDITIONAL([WIN32], [test "${WIN32}" = "yes"]) +AM_CONDITIONAL([DCO_UNIT_TESTS], [test "${dco_unit_tests}" = "yes"]) AM_CONDITIONAL([GIT_CHECKOUT], [test "${GIT_CHECKOUT}" = "yes"]) AM_CONDITIONAL([ENABLE_PLUGIN_AUTH_PAM], [test "${enable_plugin_auth_pam}" = "yes"]) AM_CONDITIONAL([ENABLE_PLUGIN_DOWN_ROOT], [test "${enable_plugin_down_root}" = "yes"]) diff --git a/src/openvpn/dco.h b/src/openvpn/dco.h index c435f8c..b0a8621 100644 --- a/src/openvpn/dco.h +++ b/src/openvpn/dco.h @@ -254,6 +254,17 @@ */ bool dco_supports_epoch_data(const struct context *c); + +/** + * Return a bitmap of DCO_CAP_* bits describing the negotiable features the + * local kernel supports. Everything DCO does without negotiating is available + * regardless. Resolved once and reused; platforms without capability probing + * return 0. + * + * @return bitmask of DCO_CAP_* flags; 0 both when the kernel supports none + * of them and when it could not be asked + */ +unsigned int dco_probe_capabilities(void); #else /* if defined(ENABLE_DCO) */ typedef void *dco_context_t; @@ -388,5 +399,11 @@ { return false; } + +static inline unsigned int +dco_probe_capabilities(void) +{ + return 0; +} #endif /* defined(ENABLE_DCO) */ #endif /* ifndef DCO_H */ diff --git a/src/openvpn/dco_freebsd.c b/src/openvpn/dco_freebsd.c index 11b8dff..9613c63 100644 --- a/src/openvpn/dco_freebsd.c +++ b/src/openvpn/dco_freebsd.c @@ -707,6 +707,12 @@ return 0; } +unsigned int +dco_probe_capabilities(void) +{ + return 0; +} + bool dco_available(msglvl_t msglevel) { diff --git a/src/openvpn/dco_linux.c b/src/openvpn/dco_linux.c index 3a55b6e..5d11d9d 100644 --- a/src/openvpn/dco_linux.c +++ b/src/openvpn/dco_linux.c @@ -103,6 +103,360 @@ return ret; } +/* What the local kernel supports. It cannot change while we run, so the first + * caller resolves it and the rest just read it. */ +static unsigned int dco_caps = 0; +static bool dco_caps_resolved = false; + +#if HAVE_DECL_CTRL_ATTR_OP_POLICY +/* + * Netlink policy introspection: the ovpn module lives in the kernel, so the + * attributes it accepts may differ from what we were built against. + * CTRL_CMD_GETPOLICY says which attributes each command takes and of what type. + */ + +/* One row per capability, all resolved from the same dump. A row can + * only ask for an attribute the dump reports: UNSPEC and REJECT ones never + * appear, and before Linux 7.4 neither do BE16/BE32 ones (b785f5c56fb3). */ +struct ovpn_cap_req +{ + uint16_t cmd; + uint16_t nested_attr; /* 0 if target_attr is not nested */ + uint16_t target_attr; + int expected_type; +}; + +/** the outcome of resolving one capability requirement */ +enum ovpn_cap_status +{ + OVPN_CAP_UNKNOWN, /**< the question could not be asked */ + OVPN_CAP_SUPPORTED, /**< the kernel accepts the attribute */ + OVPN_CAP_UNSUPPORTED, /**< the kernel answered, and does not accept it */ +}; + +/* The command map is kept with the policies under this index: there `id` is a + * command and `nested` the policy of its doit. -1 means "not reported". */ +#define OVPN_CAP_CMD_MAP (-2) + +/* ovpn on Linux 7.3 reports 77 links */ +#define OVPN_CAP_INITIAL_LINKS 128 + +/* inside `policy`, attribute `id` has type `type` and, if nested, is + * validated by policy `nested` */ +struct ovpn_cap_link +{ + int policy; + uint16_t id; + int type; + int nested; +}; + +struct ovpn_cap_query +{ + struct ovpn_cap_link *links; + size_t n_links; + size_t max_links; + + bool cmd_map; /* the dump carried a command map */ + int err; /* a message we could not parse, or no memory to record it */ +}; + +static struct nla_policy ovpn_cap_ctrl_policy[CTRL_ATTR_MAX + 1] = { + [CTRL_ATTR_POLICY] = { .type = NLA_NESTED }, + [CTRL_ATTR_OP_POLICY] = { .type = NLA_NESTED }, +}; + +static struct nla_policy ovpn_cap_op_policy[CTRL_ATTR_POLICY_DUMP_MAX + 1] = { + [CTRL_ATTR_POLICY_DO] = { .type = NLA_U32 }, +}; + +static struct nla_policy ovpn_cap_attr_policy[NL_POLICY_TYPE_ATTR_MAX + 1] = { + [NL_POLICY_TYPE_ATTR_TYPE] = { .type = NLA_U32 }, + [NL_POLICY_TYPE_ATTR_POLICY_IDX] = { .type = NLA_U32 }, +}; + +static int +ovpn_cap_add_link(struct ovpn_cap_query *query, int policy, uint16_t id, int type, int nested) +{ + if (query->n_links == query->max_links) + { + size_t max_links = query->max_links ? 2 * query->max_links : OVPN_CAP_INITIAL_LINKS; + struct ovpn_cap_link *links = realloc(query->links, max_links * sizeof(*links)); + if (!links) + { + return -NLE_NOMEM; + } + + query->links = links; + query->max_links = max_links; + } + + query->links[query->n_links++] = (struct ovpn_cap_link){ policy, id, type, nested }; + + return 0; +} + +static const struct ovpn_cap_link * +ovpn_cap_find(const struct ovpn_cap_query *query, int policy, uint16_t id) +{ + for (size_t i = 0; i < query->n_links; i++) + { + if (query->links[i].policy == policy && query->links[i].id == id) + { + return &query->links[i]; + } + } + + return NULL; +} + +/* + * One message of the dump carries either the command map or a slice of one + * policy. Everything is recorded and the chain is followed only once the + * dump is complete, so the order of the messages does not matter. + */ +static int +ovpn_cap_query_cb(struct nl_msg *msg, void *arg) +{ + struct ovpn_cap_query *query = arg; + struct nlattr *tb[CTRL_ATTR_MAX + 1]; + const struct genlmsghdr *ghdr = nlmsg_data(nlmsg_hdr(msg)); + + query->err = nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_attrlen(ghdr, 0), ovpn_cap_ctrl_policy); + if (query->err) + { + return NL_STOP; + } + + /* CTRL_ATTR_OP_POLICY: which policy validates the doit of each command */ + if (tb[CTRL_ATTR_OP_POLICY]) + { + const struct nlattr *nla_cmd; + int rem_cmd; + + query->cmd_map = true; + + nla_for_each_nested(nla_cmd, tb[CTRL_ATTR_OP_POLICY], rem_cmd) + { + struct nlattr *tb_cmd[CTRL_ATTR_POLICY_DUMP_MAX + 1]; + + query->err = nla_parse(tb_cmd, CTRL_ATTR_POLICY_DUMP_MAX, nla_data(nla_cmd), + nla_len(nla_cmd), ovpn_cap_op_policy); + if (query->err) + { + return NL_STOP; + } + + if (tb_cmd[CTRL_ATTR_POLICY_DO]) + { + query->err = ovpn_cap_add_link(query, OVPN_CAP_CMD_MAP, (uint16_t)nla_type(nla_cmd), + -1, (int)nla_get_u32(tb_cmd[CTRL_ATTR_POLICY_DO])); + if (query->err) + { + return NL_STOP; + } + } + } + } + + /* CTRL_ATTR_POLICY: which attributes each policy accepts */ + if (!tb[CTRL_ATTR_POLICY]) + { + return NL_OK; + } + + const struct nlattr *nla_policy; + int rem_policy; + + nla_for_each_nested(nla_policy, tb[CTRL_ATTR_POLICY], rem_policy) + { + struct nlattr *nla_attr; + int rem_attr; + int policy_id = nla_type(nla_policy); + + nla_for_each_nested(nla_attr, nla_policy, rem_attr) + { + struct nlattr *tb_attr[NL_POLICY_TYPE_ATTR_MAX + 1]; + int attr_id = nla_type(nla_attr); + int type = -1; + int nested = -1; + + query->err = nla_parse_nested(tb_attr, NL_POLICY_TYPE_ATTR_MAX, nla_attr, + ovpn_cap_attr_policy); + if (query->err) + { + return NL_STOP; + } + + if (tb_attr[NL_POLICY_TYPE_ATTR_TYPE]) + { + type = (int)nla_get_u32(tb_attr[NL_POLICY_TYPE_ATTR_TYPE]); + } + if (tb_attr[NL_POLICY_TYPE_ATTR_POLICY_IDX]) + { + nested = (int)nla_get_u32(tb_attr[NL_POLICY_TYPE_ATTR_POLICY_IDX]); + } + + query->err = ovpn_cap_add_link(query, policy_id, (uint16_t)attr_id, type, nested); + if (query->err) + { + return NL_STOP; + } + } + } + + return NL_OK; +} + +/* Dump the policies of the family into query->links. Returns 0, or the + * netlink error that kept us from reading it all. */ +static int +ovpn_cap_ask(struct ovpn_cap_query *query) +{ + struct nl_msg *nl_msg = NULL; + int ret = 0; + + struct nl_sock *nl_sock = nl_socket_alloc(); + if (!nl_sock) + { + return -NLE_NOMEM; + } + + ret = genl_connect(nl_sock); + if (ret < 0) + { + goto out; + } + set_cloexec(nl_socket_get_fd(nl_sock)); + + nl_msg = nlmsg_alloc(); + if (!nl_msg) + { + ret = -NLE_NOMEM; + goto out; + } + + /* nlctrl has a fixed family id, so there is nothing to resolve first. + * Unlike CTRL_CMD_GETFAMILY, CTRL_CMD_GETPOLICY does not load the module: + * this relies on dco_available() having resolved the family already. */ + if (!genlmsg_put(nl_msg, NL_AUTO_PORT, NL_AUTO_SEQ, GENL_ID_CTRL, 0, NLM_F_DUMP, + CTRL_CMD_GETPOLICY, 0)) + { + ret = -NLE_MSGSIZE; + goto out; + } + + ret = nla_put_string(nl_msg, CTRL_ATTR_FAMILY_NAME, OVPN_FAMILY_NAME); + if (ret < 0) + { + goto out; + } + + ret = nl_socket_modify_cb(nl_sock, NL_CB_VALID, NL_CB_CUSTOM, ovpn_cap_query_cb, query); + if (ret < 0) + { + goto out; + } + + ret = nl_send_auto(nl_sock, nl_msg); + if (ret < 0) + { + goto out; + } + + ret = nl_recvmsgs_default(nl_sock); + if (ret >= 0) + { + ret = query->err; + } + +out: + nlmsg_free(nl_msg); + nl_socket_free(nl_sock); + + return ret < 0 ? ret : 0; +} + +/* + * Decide whether the kernel supports the requirement, from the links the pass + * collected. A link the kernel never mentioned is a no. A dump without a + * command map is not an answer either way, and is reported as such, but the + * capability stays off just the same. + */ +static enum ovpn_cap_status +ovpn_cap_resolve(const struct ovpn_cap_query *query, const struct ovpn_cap_req *req) +{ + /* no command map: the dump cannot say which policy validates the command */ + if (!query->cmd_map) + { + return OVPN_CAP_UNKNOWN; + } + + /* a link is missing: the command, the container or the target is not there */ + const struct ovpn_cap_link *link = ovpn_cap_find(query, OVPN_CAP_CMD_MAP, req->cmd); + if (link && req->nested_attr) + { + link = ovpn_cap_find(query, link->nested, req->nested_attr); + } + if (link) + { + link = ovpn_cap_find(query, link->nested, req->target_attr); + } + if (!link) + { + return OVPN_CAP_UNSUPPORTED; + } + + /* the whole chain is there: a yes only if the attribute has the type we + * would be writing into it */ + return (link->type == req->expected_type) ? OVPN_CAP_SUPPORTED : OVPN_CAP_UNSUPPORTED; +} + +unsigned int +dco_probe_capabilities(void) +{ + if (dco_caps_resolved) + { + return dco_caps; + } + + /* no capability is defined yet: resolve one whose answer we already know */ + static const struct ovpn_cap_req self_test = { OVPN_CMD_PEER_NEW, OVPN_A_PEER, + OVPN_A_PEER_ID, NL_ATTR_TYPE_U32 }; + struct ovpn_cap_query query; + CLEAR(query); + int err = ovpn_cap_ask(&query); + enum ovpn_cap_status status = err ? OVPN_CAP_UNKNOWN : ovpn_cap_resolve(&query, &self_test); + free(query.links); + + msg(D_DCO_DEBUG, "%s: policy introspection self-test: %s (status %d, err %d)", __func__, + status == OVPN_CAP_SUPPORTED ? "ok" : "unexpected", (int)status, err); + + dco_caps_resolved = true; + msg(D_DCO_DEBUG, "%s: DCO local capabilities: 0x%x", __func__, dco_caps); + + return dco_caps; +} + +#else /* if HAVE_DECL_CTRL_ATTR_OP_POLICY */ + +unsigned int +dco_probe_capabilities(void) +{ + if (!dco_caps_resolved) + { + msg(D_DCO_DEBUG, + "%s: built without netlink policy introspection, no DCO capability can be detected", + __func__); + dco_caps_resolved = true; + } + + return dco_caps; +} + +#endif /* if HAVE_DECL_CTRL_ATTR_OP_POLICY */ + static struct nl_msg * ovpn_dco_nlmsg_create(const dco_context_t *dco, uint8_t cmd) { @@ -428,6 +782,12 @@ { dco->ovpn_dco_id = resolve_ovpn_netlink_id(M_FATAL); + /* nothing consumes the bitmap yet; resolve it here to exercise the path */ + if (check_debug_level(D_DCO_DEBUG)) + { + dco_probe_capabilities(); + } + dco->nl_sock = nl_socket_alloc(); if (!dco->nl_sock) diff --git a/src/openvpn/dco_win.c b/src/openvpn/dco_win.c index 6074552..0f2b9eb 100644 --- a/src/openvpn/dco_win.c +++ b/src/openvpn/dco_win.c @@ -612,6 +612,12 @@ return 0; } +unsigned int +dco_probe_capabilities(void) +{ + return 0; +} + bool dco_available(msglvl_t msglevel) { diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvpn/Makefile.am index b06ae5e..de498d2 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -40,6 +40,12 @@ endif endif +if HAVE_SITNL +if DCO_UNIT_TESTS +test_binaries += dco_testdriver +endif +endif + if !CROSS_COMPILING TESTS = $(test_binaries) endif @@ -454,4 +460,16 @@ $(top_srcdir)/src/openvpn/run_command.c \ $(top_srcdir)/src/openvpn/socket_util.c +dco_testdriver_CFLAGS = \ + -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \ + @TEST_CFLAGS@ $(OPTIONAL_CRYPTO_CFLAGS) +dco_testdriver_LDFLAGS = @TEST_LDFLAGS@ $(OPTIONAL_CRYPTO_LIBS) +dco_testdriver_SOURCES = test_dco.c \ + mock_msg.c mock_msg.h mock_dco_dependencies.c mock_get_random.c test_common.h \ + $(top_srcdir)/src/compat/compat-strlcpy.c \ + $(top_srcdir)/src/openvpn/buffer.c \ + $(top_srcdir)/src/openvpn/fdmisc.c \ + $(top_srcdir)/src/openvpn/platform.c \ + $(top_srcdir)/src/openvpn/win32-util.c + build-tests-local: $(check_PROGRAMS) diff --git a/tests/unit_tests/openvpn/mock_dco_dependencies.c b/tests/unit_tests/openvpn/mock_dco_dependencies.c new file mode 100644 index 0000000..e7f20f5 --- /dev/null +++ b/tests/unit_tests/openvpn/mock_dco_dependencies.c @@ -0,0 +1,80 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2002-2026 OpenVPN Inc + * Copyright (C) 2026 Gianmarco De Gregori + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +/* + * Stubs for what dco_linux.c drags in but capability probing never reaches: + * the data path, the event loop and the signal machinery. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" +#include "dco.h" +#include "crypto_backend.h" +#include "forward.h" +#include "multi.h" +#include "networking.h" +#include "socket_util.h" + +void +multi_process_incoming_dco(dco_context_t *dco) +{ +} + +void +process_incoming_dco(dco_context_t *dco) +{ +} + +void +register_signal(struct signal_info *si, int signum, const char *signal_text) +{ +} + +int +net_iface_new(const openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, const char *type, + const void *arg) +{ + return 0; +} + +int +net_iface_del(const openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface) +{ + return 0; +} + +unsigned int +cipher_kt_key_size(const char *ciphername) +{ + return 0; +} + +const char * +print_sockaddr_ex(const struct sockaddr *addr, const char *separator, const unsigned int flags, + struct gc_arena *gc) +{ + return "[mock]"; +} diff --git a/tests/unit_tests/openvpn/test_dco.c b/tests/unit_tests/openvpn/test_dco.c new file mode 100644 index 0000000..1be7ffe --- /dev/null +++ b/tests/unit_tests/openvpn/test_dco.c @@ -0,0 +1,484 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2002-2026 OpenVPN Inc + * Copyright (C) 2026 Gianmarco De Gregori + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, see . + */ + +/* + * Unit tests for the DCO capability queries. + * + * Resolving a requirement is a pass over a policy dump that records what it + * reports, followed by a walk along what the pass recorded. The pass is fed + * dumps built by hand, which cover what would otherwise need a particular + * kernel. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "syshead.h" + +#include +#include +#include + +#include "dco_linux.c" + +#define CMD_POLICY 4 +#define SUB_POLICY 9 +#define OTHER_POLICY 7 + +static const struct ovpn_cap_req test_req = { .cmd = OVPN_CMD_PEER_NEW, + .nested_attr = OVPN_A_PEER, + .target_attr = OVPN_A_PEER_KEEPALIVE_INTERVAL, + .expected_type = NL_ATTR_TYPE_U32 }; + +/* the same requirement without a container: the target sits in the command's + * own policy */ +static const struct ovpn_cap_req flat_req = { .cmd = OVPN_CMD_PEER_NEW, + .nested_attr = 0, + .target_attr = OVPN_A_IFINDEX, + .expected_type = NL_ATTR_TYPE_U32 }; + +static struct ovpn_cap_query +new_query(void) +{ + struct ovpn_cap_query query; + + CLEAR(query); + return query; +} + +/* a CTRL_ATTR_OP_POLICY message saying which policy validates `cmd` */ +static struct nl_msg * +msg_op_policy(uint16_t cmd, uint32_t do_policy) +{ + struct nl_msg *msg = nlmsg_alloc(); + struct nlattr *ops, *op; + + assert_non_null(msg); + assert_non_null(genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, 0, 0, 0, CTRL_CMD_GETPOLICY, 0)); + + ops = nla_nest_start(msg, CTRL_ATTR_OP_POLICY); + op = nla_nest_start(msg, cmd); + assert_int_equal(nla_put_u32(msg, CTRL_ATTR_POLICY_DO, do_policy), 0); + nla_nest_end(msg, op); + nla_nest_end(msg, ops); + + return msg; +} + +/* one attribute of a policy: its id, which field of its descriptor to set, + * and the value to put there */ +struct attr_desc +{ + uint16_t attr_id; + int field; + uint32_t value; +}; + +/* a CTRL_ATTR_POLICY message describing `n` attributes of one policy */ +static struct nl_msg * +msg_policy_attrs(uint16_t policy_id, const struct attr_desc *descs, size_t n) +{ + struct nl_msg *msg = nlmsg_alloc(); + struct nlattr *policies, *policy; + + assert_non_null(msg); + assert_non_null(genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, 0, 0, 0, CTRL_CMD_GETPOLICY, 0)); + + policies = nla_nest_start(msg, CTRL_ATTR_POLICY); + policy = nla_nest_start(msg, policy_id); + for (size_t i = 0; i < n; i++) + { + struct nlattr *attr = nla_nest_start(msg, descs[i].attr_id); + assert_int_equal(nla_put_u32(msg, descs[i].field, descs[i].value), 0); + nla_nest_end(msg, attr); + } + nla_nest_end(msg, policy); + nla_nest_end(msg, policies); + + return msg; +} + +/* the shape the kernel actually sends: one attribute per message */ +static struct nl_msg * +msg_policy_attr(uint16_t policy_id, uint16_t attr_id, int field, uint32_t value) +{ + const struct attr_desc one = { attr_id, field, value }; + + return msg_policy_attrs(policy_id, &one, 1); +} + +static void +feed(struct ovpn_cap_query *query, struct nl_msg *msg) +{ + assert_int_equal(ovpn_cap_query_cb(msg, query), NL_OK); + nlmsg_free(msg); +} + +/* Walk the whole chain, with both policies carrying attributes we are not + * after and the ones we are neither first nor last, so that the pass has to + * skip past them rather than stop at the first mismatch. */ +static void +feed_full_chain(struct ovpn_cap_query *query) +{ + const struct attr_desc cmd_attrs[] = { + { OVPN_A_IFINDEX, NL_POLICY_TYPE_ATTR_TYPE, NL_ATTR_TYPE_U32 }, + { OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, SUB_POLICY }, + { OVPN_A_KEYCONF, NL_POLICY_TYPE_ATTR_TYPE, NL_ATTR_TYPE_NESTED }, + }; + const struct attr_desc sub_attrs[] = { + { OVPN_A_PEER_ID, NL_POLICY_TYPE_ATTR_TYPE, NL_ATTR_TYPE_U32 }, + { OVPN_A_PEER_REMOTE_PORT, NL_POLICY_TYPE_ATTR_TYPE, NL_ATTR_TYPE_U16 }, + { test_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, NL_ATTR_TYPE_U32 }, + { OVPN_A_PEER_LOCAL_PORT, NL_POLICY_TYPE_ATTR_TYPE, NL_ATTR_TYPE_U16 }, + }; + + feed(query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(query, msg_policy_attrs(CMD_POLICY, cmd_attrs, SIZE(cmd_attrs))); + feed(query, msg_policy_attrs(SUB_POLICY, sub_attrs, SIZE(sub_attrs))); +} + +/* the map tells us which policy validates the command we asked about */ +static void +test_reads_command_policy(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + const struct ovpn_cap_link *link = ovpn_cap_find(&query, OVPN_CAP_CMD_MAP, OVPN_CMD_PEER_NEW); + assert_non_null(link); + assert_int_equal(link->nested, CMD_POLICY); + free(query.links); +} + +/* and not about another one */ +static void +test_ignores_other_command(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_DEL, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + SUB_POLICY)); + feed(&query, msg_policy_attr(SUB_POLICY, test_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* the nested attribute of that policy points at the sub-policy */ +static void +test_follows_nested_attribute(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + SUB_POLICY)); + const struct ovpn_cap_link *link = ovpn_cap_find(&query, CMD_POLICY, OVPN_A_PEER); + assert_non_null(link); + assert_int_equal(link->nested, SUB_POLICY); + free(query.links); +} + +/* an attribute id only means something inside its own policy, so the same + * number elsewhere says nothing about our question: a command policy without + * the container is a no, whatever comes after it */ +static void +test_ignores_nested_attribute_elsewhere(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_IFINDEX, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + feed(&query, msg_policy_attr(OTHER_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + SUB_POLICY)); + feed(&query, msg_policy_attr(SUB_POLICY, test_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* the type of the target attribute is read in the sub-policy */ +static void +test_reads_target_type(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed_full_chain(&query); + const struct ovpn_cap_link *link = ovpn_cap_find(&query, SUB_POLICY, test_req.target_attr); + assert_non_null(link); + assert_int_equal(link->type, NL_ATTR_TYPE_U32); + free(query.links); +} + +/* and the same holds for the target attribute: a type read off some other + * policy is not an answer to what we asked */ +static void +test_ignores_target_attribute_elsewhere(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + SUB_POLICY)); + feed(&query, msg_policy_attr(OTHER_POLICY, test_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* the command's own policy does get read, so the target attribute has to be + * turned away there too: the same id there is a different attribute */ +static void +test_ignores_target_attribute_in_command_policy(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + SUB_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, test_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* the sub-policy is read too, so the nested attribute has to be turned away + * there as well */ +static void +test_ignores_nested_attribute_in_sub_policy(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + SUB_POLICY)); + feed(&query, msg_policy_attr(SUB_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + OTHER_POLICY)); + feed(&query, msg_policy_attr(OTHER_POLICY, test_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* an attribute that is there but not as a nest has nothing to follow */ +static void +test_nested_attribute_without_policy_idx(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + feed(&query, msg_policy_attr(SUB_POLICY, test_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* and one that is a nest says nothing about the type we are after */ +static void +test_target_attribute_without_type(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + SUB_POLICY)); + feed(&query, msg_policy_attr(SUB_POLICY, test_req.target_attr, + NL_POLICY_TYPE_ATTR_POLICY_IDX, OTHER_POLICY)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* a flat requirement needs no hop: the command's policy is where to look */ +static void +test_flat_requirement_reads_command_policy(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, flat_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + assert_int_equal(ovpn_cap_resolve(&query, &flat_req), OVPN_CAP_SUPPORTED); + free(query.links); +} + +/* and an absent one is a plain no */ +static void +test_flat_requirement_absent_is_unsupported(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(OTHER_POLICY, flat_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + assert_int_equal(ovpn_cap_resolve(&query, &flat_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* a message we cannot parse stops the pass with an error, rather than + * leaving a hole that would read as a no */ +static void +test_malformed_message_stops(void **state) +{ + struct ovpn_cap_query query = new_query(); + struct nl_msg *msg = nlmsg_alloc(); + struct nlattr *ops, *op; + + assert_non_null(msg); + assert_non_null(genlmsg_put(msg, NL_AUTO_PORT, NL_AUTO_SEQ, 0, 0, 0, CTRL_CMD_GETPOLICY, 0)); + ops = nla_nest_start(msg, CTRL_ATTR_OP_POLICY); + op = nla_nest_start(msg, OVPN_CMD_PEER_NEW); + assert_int_equal(nla_put_u16(msg, CTRL_ATTR_POLICY_DO, CMD_POLICY), 0); + nla_nest_end(msg, op); + nla_nest_end(msg, ops); + + assert_int_equal(ovpn_cap_query_cb(msg, &query), NL_STOP); + assert_true(query.err < 0); + assert_int_equal(query.n_links, 0); + nlmsg_free(msg); + free(query.links); +} + +/* nothing to read an answer out of is not the same as a "no" */ +static void +test_resolve_without_command_policy(void **state) +{ + struct ovpn_cap_query query = new_query(); + + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNKNOWN); +} + +/* a kernel that does not nest the attribute simply never mentions it */ +static void +test_resolve_nested_absent(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* the sub-policy is there but does not define the attribute: the ordinary no */ +static void +test_resolve_target_absent(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + SUB_POLICY)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* right name, wrong type: not the attribute we would be writing */ +static void +test_resolve_wrong_type(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + SUB_POLICY)); + feed(&query, msg_policy_attr(SUB_POLICY, test_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U8)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_UNSUPPORTED); + free(query.links); +} + +/* and the whole chain, from a dump to a yes */ +static void +test_resolve_complete_chain(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed_full_chain(&query); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_SUPPORTED); + free(query.links); +} + +/* the kernel's order is not relied upon */ +static void +test_resolve_any_order(void **state) +{ + struct ovpn_cap_query query = new_query(); + + feed(&query, msg_policy_attr(SUB_POLICY, test_req.target_attr, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + feed(&query, msg_policy_attr(CMD_POLICY, OVPN_A_PEER, NL_POLICY_TYPE_ATTR_POLICY_IDX, + SUB_POLICY)); + feed(&query, msg_op_policy(OVPN_CMD_PEER_NEW, CMD_POLICY)); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_SUPPORTED); + free(query.links); +} + +/* a dump larger than the first allocation */ +static void +test_resolve_large_dump(void **state) +{ + struct ovpn_cap_query query = new_query(); + + for (uint16_t attr_id = 1; attr_id <= 3 * OVPN_CAP_INITIAL_LINKS; attr_id++) + { + feed(&query, msg_policy_attr(OTHER_POLICY, attr_id, NL_POLICY_TYPE_ATTR_TYPE, + NL_ATTR_TYPE_U32)); + } + feed_full_chain(&query); + assert_true(query.n_links <= query.max_links); + assert_int_equal(ovpn_cap_resolve(&query, &test_req), OVPN_CAP_SUPPORTED); + free(query.links); +} + +int +main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_reads_command_policy), + cmocka_unit_test(test_ignores_other_command), + cmocka_unit_test(test_follows_nested_attribute), + cmocka_unit_test(test_ignores_nested_attribute_elsewhere), + cmocka_unit_test(test_reads_target_type), + cmocka_unit_test(test_ignores_target_attribute_elsewhere), + cmocka_unit_test(test_ignores_target_attribute_in_command_policy), + cmocka_unit_test(test_ignores_nested_attribute_in_sub_policy), + cmocka_unit_test(test_nested_attribute_without_policy_idx), + cmocka_unit_test(test_target_attribute_without_type), + cmocka_unit_test(test_flat_requirement_reads_command_policy), + cmocka_unit_test(test_flat_requirement_absent_is_unsupported), + cmocka_unit_test(test_malformed_message_stops), + cmocka_unit_test(test_resolve_without_command_policy), + cmocka_unit_test(test_resolve_nested_absent), + cmocka_unit_test(test_resolve_target_absent), + cmocka_unit_test(test_resolve_wrong_type), + cmocka_unit_test(test_resolve_complete_chain), + cmocka_unit_test(test_resolve_any_order), + cmocka_unit_test(test_resolve_large_dump), + }; + + return cmocka_run_group_tests_name("dco", tests, NULL, NULL); +}