From patchwork Wed Jul 6 04:29:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonio Quartulli X-Patchwork-Id: 2575 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director11.mail.ord1d.rsapps.net ([172.31.255.6]) by backend30.mail.ord1d.rsapps.net with LMTP id 2DLkJGucxWJkcAAAIUCqbw (envelope-from ) for ; Wed, 06 Jul 2022 10:30:03 -0400 Received: from proxy5.mail.iad3b.rsapps.net ([172.31.255.6]) by director11.mail.ord1d.rsapps.net with LMTP id QKGzJGucxWJbLwAAvGGmqA (envelope-from ) for ; Wed, 06 Jul 2022 10:30:03 -0400 Received: from smtp32.gate.iad3b ([172.31.255.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy5.mail.iad3b.rsapps.net with LMTPS id 6K2DHWucxWL0VAAA13hMnw (envelope-from ) for ; Wed, 06 Jul 2022 10:30:03 -0400 X-Spam-Threshold: 95 X-Spam-Score: 0 X-Spam-Flag: NO X-Virus-Scanned: OK X-Orig-To: openvpnslackdevel@openvpn.net X-Originating-Ip: [216.105.38.7] Authentication-Results: smtp32.gate.iad3b.rsapps.net; iprev=pass policy.iprev="216.105.38.7"; spf=pass smtp.mailfrom="openvpn-devel-bounces@lists.sourceforge.net" smtp.helo="lists.sourceforge.net"; dkim=fail (signature verification failed) header.d=sourceforge.net; dkim=fail (signature verification failed) header.d=sf.net; dmarc=none (p=nil; dis=none) header.from=unstable.cc X-Suspicious-Flag: YES X-Classification-ID: 1eaa2250-fd38-11ec-a0d2-5254006a2e70-1-1 Received: from [216.105.38.7] ([216.105.38.7:48390] helo=lists.sourceforge.net) by smtp32.gate.iad3b.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 34/A2-25519-A6C95C26; Wed, 06 Jul 2022 10:30:02 -0400 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.94.2) (envelope-from ) id 1o961S-0003BT-KW; Wed, 06 Jul 2022 14:28:58 +0000 Received: from [172.30.20.202] (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.94.2) (envelope-from ) id 1o961Q-0003BB-Ta for openvpn-devel@lists.sourceforge.net; Wed, 06 Jul 2022 14:28:56 +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:Cc:To:From:Sender:Reply-To: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-Subscribe:List-Post:List-Owner:List-Archive; bh=MP1YjWvOR86rVPMTAeIAllFNuebXacbHoslNYxUNwOM=; b=X863UfZoOTN3AI/J0FnFWHD/dL /E+EitIuy0s/CoFxBmr83uwpitkF1R00aDPYzdBYlIFpQUEskhShJzfcgdgV7CQCNC0lBVtqD0rv/ 1BzKS2xJ+L8cU7iwLNje4ZZCf3MxG6kgPO02zilPFZBV4BTMsZ/TIV4zmXIqxJRMh82k=; 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:Cc:To:From:Sender:Reply-To: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-Subscribe: List-Post:List-Owner:List-Archive; bh=MP1YjWvOR86rVPMTAeIAllFNuebXacbHoslNYxUNwOM=; b=f5fCLAv2I7K2pxgsAcWcUDNmOS c7BRuzA+iPYZVeQC3rXBO00bTgB8ZTKA4WNuTf9eF3FIwnShKHWJjtFXf6qBROibXBFP8bfm5GjL9 jC/kUE8mKk/OlHfvmuMgMtxzUTbf+D6oUymqVVxL/3XWjkYQu+bql3T8UrNOZIi86twg=; Received: from s2.neomailbox.net ([5.148.176.60]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.94.2) id 1o961P-008sg7-1x for openvpn-devel@lists.sourceforge.net; Wed, 06 Jul 2022 14:28:56 +0000 From: Antonio Quartulli To: openvpn-devel@lists.sourceforge.net Date: Wed, 6 Jul 2022 16:29:06 +0200 Message-Id: <20220706142907.3962-1-a@unstable.cc> In-Reply-To: <20220630140804.8277-1-a@unstable.cc> References: <20220630140804.8277-1-a@unstable.cc> MIME-Version: 1.0 X-Spam-Report: Spam detection software, running on the system "util-spamd-2.v13.lw.sourceforge.com", 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: This new API can be used to retrieve the type of a specific interface. It's mostly platform dependant, but right now expected values are "ovpn-dco", "tun" or "tap". Other values are possible too, but they are not of interest to us. Content analysis details: (-0.0 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 T_SCC_BODY_TEXT_LINE No description available. X-Headers-End: 1o961P-008sg7-1x Subject: [Openvpn-devel] [PATCH pre-05/25] networking: add net_iface_type API 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: , Cc: Antonio Quartulli Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox This new API can be used to retrieve the type of a specific interface. It's mostly platform dependant, but right now expected values are "ovpn-dco", "tun" or "tap". Other values are possible too, but they are not of interest to us. Signed-off-by: Antonio Quartulli --- This patch is required by 05/25 v6 as it introduces the networking API required to retrieve an interafce type. This is then used by 05/25 v6. src/openvpn/networking.h | 14 ++++ src/openvpn/networking_iproute2.c | 9 +++ src/openvpn/networking_sitnl.c | 109 ++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+) diff --git a/src/openvpn/networking.h b/src/openvpn/networking.h index 79963756..cf6d39ac 100644 --- a/src/openvpn/networking.h +++ b/src/openvpn/networking.h @@ -23,6 +23,8 @@ #include "syshead.h" +#define IFACE_TYPE_LEN_MAX 64 + struct context; #ifdef ENABLE_SITNL @@ -100,6 +102,18 @@ void net_ctx_free(openvpn_net_ctx_t *ctx); int net_iface_new(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, const char *type, void *arg); +/** + * Retrieve the interface type + * + * @param ctx the implementation specific context + * @param iface interface to query + * @param type buffer where the type will be stored + * + * @return 0 on success, a negative error code otherwise + */ +int net_iface_type(openvpn_net_ctx_t *ctx, const char *iface, + char type[IFACE_TYPE_LEN_MAX]); + /** * Remove an interface * diff --git a/src/openvpn/networking_iproute2.c b/src/openvpn/networking_iproute2.c index 4b220576..a81e6b33 100644 --- a/src/openvpn/networking_iproute2.c +++ b/src/openvpn/networking_iproute2.c @@ -78,6 +78,15 @@ net_iface_new(openvpn_net_ctx_t *ctx, const char *iface, const char *type, return 0; } +int +net_iface_type(openvpn_net_ctx_t *ctx, const char *iface, + char type[IFACE_TYPE_LEN_MAX]) +{ + /* not supported by iproute2 */ + msg(M_WARN, "%s: operation not supported by iproute2 backend"); + return -1; +} + int net_iface_del(openvpn_net_ctx_t *ctx, const char *iface) { diff --git a/src/openvpn/networking_sitnl.c b/src/openvpn/networking_sitnl.c index 0944ad0a..e97db3f7 100644 --- a/src/openvpn/networking_sitnl.c +++ b/src/openvpn/networking_sitnl.c @@ -1366,6 +1366,115 @@ err: return ret; } +static int +sitnl_parse_rtattr_flags(struct rtattr *tb[], int max, struct rtattr *rta, + int len, unsigned short flags) +{ + unsigned short type; + + memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); + + while (RTA_OK(rta, len)) + { + type = rta->rta_type & ~flags; + + if ((type <= max) && (!tb[type])) + { + tb[type] = rta; + } + + rta = RTA_NEXT(rta, len); + } + + if (len) + { + msg(D_ROUTE, "%s: %d bytes not parsed! (rta_len=%d)", __func__, len, + rta->rta_len); + } + + return 0; +} + +static int +sitnl_parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, int len) +{ + return sitnl_parse_rtattr_flags(tb, max, rta, len, 0); +} + +#define sitnl_parse_rtattr_nested(tb, max, rta) \ + (sitnl_parse_rtattr_flags(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta), \ + NLA_F_NESTED)) + +static int +sitnl_type_save(struct nlmsghdr *n, void *arg) +{ + char *type = arg; + struct ifinfomsg *ifi = NLMSG_DATA(n); + struct rtattr *tb[IFLA_MAX + 1]; + int ret; + + ret = sitnl_parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n)); + if (ret < 0) + { + return ret; + } + + if (tb[IFLA_LINKINFO]) + { + struct rtattr *tb_link[IFLA_INFO_MAX + 1]; + + ret = sitnl_parse_rtattr_nested(tb_link, IFLA_INFO_MAX, + tb[IFLA_LINKINFO]); + if (ret < 0) + { + return ret; + } + + if (!tb_link[IFLA_INFO_KIND]) + { + return -ENOENT; + } + + strncpynt(type, RTA_DATA(tb_link[IFLA_INFO_KIND]), IFACE_TYPE_LEN_MAX); + } + + return 0; +} + +int +net_iface_type(openvpn_net_ctx_t *ctx, const char *iface, + char type[IFACE_TYPE_LEN_MAX]) +{ + struct sitnl_link_req req = { }; + int ifindex = if_nametoindex(iface); + + if (!ifindex) + { + return errno; + } + + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i)); + req.n.nlmsg_flags = NLM_F_REQUEST; + req.n.nlmsg_type = RTM_GETLINK; + + req.i.ifi_family = AF_PACKET; + req.i.ifi_index = ifindex; + + memset(type, 0, IFACE_TYPE_LEN_MAX); + + int ret = sitnl_send(&req.n, 0, 0, sitnl_type_save, type); + if (ret < 0) + { + msg(D_ROUTE, "%s: cannot retrieve iface %s: %s (%d)", __func__, iface, + strerror(-ret), ret); + return ret; + } + + msg(D_ROUTE, "%s: type of %s: %s", __func__, iface, type); + + return 0; +} + int net_iface_del(openvpn_net_ctx_t *ctx, const char *iface) {