From patchwork Tue Dec 7 01:11:32 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonio Quartulli X-Patchwork-Id: 2113 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director12.mail.ord1d.rsapps.net ([172.30.191.6]) by backend41.mail.ord1d.rsapps.net with LMTP id 8AK8HtVPr2EmLgAAqwncew (envelope-from ) for ; Tue, 07 Dec 2021 07:13:09 -0500 Received: from proxy9.mail.ord1d.rsapps.net ([172.30.191.6]) by director12.mail.ord1d.rsapps.net with LMTP id 8NUmBNVPr2HdUAAAIasKDg (envelope-from ) for ; Tue, 07 Dec 2021 07:13:09 -0500 Received: from smtp24.gate.ord1c ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy9.mail.ord1d.rsapps.net with LMTPS id QKYOLtVPr2H1WAAA7h+8OQ (envelope-from ) for ; Tue, 07 Dec 2021 07:13:09 -0500 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: smtp24.gate.ord1c.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: 09ebcad0-5757-11ec-aab2-b8ca3a674470-1-1 Received: from [216.105.38.7] ([216.105.38.7:34254] helo=lists.sourceforge.net) by smtp24.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 5B/84-36340-4DF4FA16; Tue, 07 Dec 2021 07:13:08 -0500 Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.94.2) (envelope-from ) id 1muZKU-0002w4-P7; Tue, 07 Dec 2021 12:12:18 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1muZKN-0002vh-8A for openvpn-devel@lists.sourceforge.net; Tue, 07 Dec 2021 12:12:11 +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=6tl45DPnRguNb7CtOOr3MAJ3eFOVpDqbiMKuiq8hzLs=; b=Jey3mesVouOjg15hKv5sSyXaCP N0DCtbG8S3gtk2UOf67BUVW0xgIY2yjMtdH3PpH2e7boOFTIXbXvPMN2GCTMn9Jjt2+9UAzfcleZd Xh56p0zKakBD3BNNC4bylvoxC3bzR++vyRgdMVL0KVGsENSVYl6TqDkJVZbv2iEXWymI=; 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=6tl45DPnRguNb7CtOOr3MAJ3eFOVpDqbiMKuiq8hzLs=; b=Fs7YvtXaCTQb4t8zQxXucCkb8Z EThhOI//Ua3LvR3hpdRywpinzXiPTfPP//cp0as4vC7sm36DGX71UN/JLPE/PKBUnLYI9BORRv5HB F2pNSuH6n3Vy6tYVWqbhJpLdEeZLJPAN6aWEoEIrmLYa1w6m8e7uQQ29KiHU04Kr/zvk=; Received: from s2.neomailbox.net ([5.148.176.60]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92.3) id 1muZKJ-0008P4-1n for openvpn-devel@lists.sourceforge.net; Tue, 07 Dec 2021 12:12:11 +0000 From: Antonio Quartulli To: openvpn-devel@lists.sourceforge.net Date: Tue, 7 Dec 2021 13:11:32 +0100 Message-Id: <20211207121137.3221-4-a@unstable.cc> In-Reply-To: <20211207121137.3221-1-a@unstable.cc> References: <20211207121137.3221-1-a@unstable.cc> MIME-Version: 1.0 X-Spam-Report: Spam detection software, running on the system "util-spamd-1.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: From: Arne Schwabe These two new methods can be used to create and delete a tun or an ovpn-dco interface via RTNL API. Signed-off-by: Arne Schwabe Signed-off-by: Antonio Quartulli --- src/openvpn/networking_sitnl.c | 94 ++++++++++++++++++++++ src/openvpn/networking_sitnl.h | 28 +++++ [...] Content analysis details: (0.0 points, 6.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.0 SPF_PASS SPF: sender matches SPF record X-Headers-End: 1muZKJ-0008P4-1n Subject: [Openvpn-devel] [RFC 3/8] sitnl: implement net_iface_new and net_iface_del 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 From: Arne Schwabe These two new methods can be used to create and delete a tun or an ovpn-dco interface via RTNL API. Signed-off-by: Arne Schwabe Signed-off-by: Antonio Quartulli --- src/openvpn/networking_sitnl.c | 94 ++++++++++++++++++++++ src/openvpn/networking_sitnl.h | 28 +++++++ tests/unit_tests/openvpn/test_networking.c | 22 ++++- 3 files changed, 143 insertions(+), 1 deletion(-) diff --git a/src/openvpn/networking_sitnl.c b/src/openvpn/networking_sitnl.c index e0003f5c..e6ffdb64 100644 --- a/src/openvpn/networking_sitnl.c +++ b/src/openvpn/networking_sitnl.c @@ -1312,6 +1312,100 @@ net_route_v6_del(openvpn_net_ctx_t *ctx, const struct in6_addr *dst, table, metric); } + +int +net_iface_new(const char *iface, const char *type) +{ + struct sitnl_link_req req = { }; + struct rtattr *tail = NULL; + int ret = -1; + + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i)); + req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_EXCL ; + req.n.nlmsg_type = RTM_NEWLINK; + + if (iface) + { + SITNL_ADDATTR(&req.n, sizeof(req), IFLA_IFNAME, iface, strlen(iface) + 1); + } + tail = NLMSG_TAIL(&req.n); + SITNL_ADDATTR(&req.n, sizeof(req), IFLA_LINKINFO, NULL, 0); + SITNL_ADDATTR(&req.n, sizeof(req), IFLA_INFO_KIND, type, + strlen(type) + 1); + tail->rta_len = (uint8_t *)NLMSG_TAIL(&req.n) - (uint8_t *)tail; + + req.i.ifi_family = AF_PACKET; + req.i.ifi_change = 0xFFFFFFFF; + + msg(D_ROUTE, "%s: add %s type %s", __func__, np(iface), type); + + if (iface) + { + /* if we have an interface name we can use that name to later + * lookup what interface index we created */ + ret = sitnl_send(&req.n, 0, 0, NULL, NULL); + if (!ret) + { + req.i.ifi_index = if_nametoindex(iface); + } + + } + else + { + req.i.ifi_index = 1194; + do + { + /* for some reason RTM_NEWLINK does not have a reply */ + /* Therefore we use try using different if indices untiles + * we get one that does not exist already */ + req.i.ifi_index++; + ret = sitnl_send(&req.n, 0, 0, NULL, NULL); + } + while (ret == -EEXIST); + } + if (!ret) + { + return req.i.ifi_index; + } + +err: + return ret; +} + +int +net_iface_del_name(const char *iface) +{ + int ifindex; + + ifindex = if_nametoindex(iface); + + msg(D_ROUTE,"%s: idel %s", __func__, iface); + + if (ifindex == 0) + { + msg(D_ROUTE|M_ERRNO, "%s: rtnl: cannot get ifindex for %s:", + __func__, iface); + return -ENOENT; + } + + return net_iface_del_index(ifindex); +} + +int +net_iface_del_index(int ifindex) +{ + struct sitnl_link_req req = { }; + + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i)); + req.n.nlmsg_flags = NLM_F_REQUEST; + req.n.nlmsg_type = RTM_DELLINK; + + req.i.ifi_family = AF_PACKET; + req.i.ifi_index = ifindex; + + return sitnl_send(&req.n, 0, 0, NULL, NULL); +} + #endif /* !ENABLE_SITNL */ #endif /* TARGET_LINUX */ diff --git a/src/openvpn/networking_sitnl.h b/src/openvpn/networking_sitnl.h index f040020e..b88ffd4b 100644 --- a/src/openvpn/networking_sitnl.h +++ b/src/openvpn/networking_sitnl.h @@ -25,4 +25,32 @@ typedef char openvpn_net_iface_t; typedef void *openvpn_net_ctx_t; +/** + * @brief Add new interface (similar to ip link add) + * + * @param iface interface name + * @param type interface link type (for example "ovpn-dco") + * @return int 0 on success, negative error code on error + */ +int +net_iface_new(const char *iface, const char *type); + +/** + * @brief Remove an interface (similar to ip link remove) + * + * @param iface interface name + * @return int 0 on success, negative error code on error + */ +int +net_iface_del_name(const char *iface); + +/** + * @brief Remove an interface (similar to ip link remove) + * + * @param ifindex interface index + * @return int 0 on success, negative error code on error + */ +int +net_iface_del_index(int ifindex); + #endif /* NETWORKING_SITNL_H_ */ diff --git a/tests/unit_tests/openvpn/test_networking.c b/tests/unit_tests/openvpn/test_networking.c index 9e9744f4..37b97188 100644 --- a/tests/unit_tests/openvpn/test_networking.c +++ b/tests/unit_tests/openvpn/test_networking.c @@ -13,6 +13,20 @@ net__iface_up(bool up) return net_iface_up(NULL, iface, up); } +static int +net__iface_new(const char *name, const char* type) +{ + printf("CMD: ip link add %s type %s\n", name, type); + return net_iface_new(name, type); +} + +static int +net__iface_del(const char *name) +{ + printf("CMD: ip link del %s\n", name); + return net_iface_del_name(name); +} + static int net__iface_mtu_set(int mtu) { @@ -191,7 +205,7 @@ net__route_v6_add_gw(const char *dst_str, int prefixlen, const char *gw_str, static void usage(char *name) { - printf("Usage: %s <0-7>\n", name); + printf("Usage: %s <0-9>\n", name); } int @@ -243,6 +257,12 @@ main(int argc, char *argv[]) case 7: return net__route_v6_add_gw("2001:cafe:babe::", 48, "2001::2", 600); + case 8: + return net__iface_new("dummy0815", "dummy"); + + case 9: + return net__iface_del("dummy0815"); + default: printf("invalid test: %d\n", test); break;