From patchwork Mon Nov 16 12:05:08 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gert Doering X-Patchwork-Id: 5712 Return-Path: Delivered-To: patchwork@openvpn.net Received: by 2002:a05:6022:9292:b0:11a:f58a:e788 with SMTP id dc18csp1101725lab; Mon, 16 Nov 2026 04:05:26 -0800 (PST) X-Forwarded-Encrypted: i=2; AL+hErEaybpshwb56wlrqQH3TLXsowkCL4AVDweDRiZbsJyrtosEbcz9/s4BVVDV3TbZ9YoqAJNRUsgtWC8=@openvpn.net X-Received: by 2002:a05:7301:d812:b0:373:d25b:8cf0 with SMTP id 5a478bee46e88-379681cc558mr10922264eec.29.1794830725748; Mon, 16 Nov 2026 04:05:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1794830725; cv=none; d=google.com; s=arc-20260327; b=fNtY1HgeTnc1G5YXUyu1vg9rithPFzsuSxwIYbWsO4g2Un6T1ThixLoJm2FB3Qp6/L JYm2RnP4uw9S+DX295vhZxMUtFW1RVVqqfmJNtEplq4PlHEm1EQf+ERiKCLTFiBM28PD TLe1u1l0UsKsCmE4cxe0wYyMSfz4tbTVtCVyLz39Sb3h0JM3yL1C3jIY+XUF3kwN04hq CG2zFJSRUKvGLW+wV3gLsZasO98r9riMKijQ8d5Jkzk7N1oPRTP0XXswhxbDa3UvLwVv ysP7agge7kG3DkKv1OesWXKPBOXzzbxd6vZ0cxZ6Vq8KDaCoCMce1bRiwzZ8cPuE9FFl TYrg== 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=aA18IFa93U85vwP9fmtobbJdsbirmO3xfv2fcdXiN1U=; fh=4NbAC/LsuMLI0S0hprUlLSLCiHwg6SCAifhH718Jh0Q=; b=pstQ0THuwPOSItvljG8BLRrdMm/0aAnT5cffanff2afEoCwrdx32wb3dWH51+tjQZp qIsP8X5Ucb/Xf+rFIteEaGG7tJSTkFhELFZj3URqRPJW59rHH6RJUKr0ieLq2QiPwZQA NF3PEhqIkpHuB1Thus8KX73ERZS2ZJlv2cHk8QJr+LQWHBykKWD0ZTxadLTdo30tNtLs ppO90zYh+N87txz7b6JULwBRomVvbJAD962FbYrPVtL9qrn50884nNbqI8tY+h0JNQIU +rq7OcW4nxkZ5tWIVsn4rSDo6wj+XB3p3f9z0jefewuogJfN13Uh0VIC2NGKXBKyTcT5 QJvQ==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@lists.sourceforge.net header.s=beta header.b=dV33oojl; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=m8CuOOqH; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=dDOG14Wu; 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 5a478bee46e88-379592dbd97si29225247eec.10.2026.11.16.04.05.24 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 16 Nov 2026 04:05:25 -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=dV33oojl; dkim=neutral (body hash did not verify) header.i=@sourceforge.net header.s=x header.b=m8CuOOqH; dkim=neutral (body hash did not verify) header.i=@sf.net header.s=x header.b=dDOG14Wu; 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=aA18IFa93U85vwP9fmtobbJdsbirmO3xfv2fcdXiN1U=; b=dV33oojlSG++AB IX6serUwjPoqsMVP1NEadWobn7mDGdz05RmoMmgkkFqGTouXGj351l4wc9oGIGADVkNNjtcMtErWJ LKKyHKfPTnsaGIe2PcNxD9XyB90mCPRqwg1quDOKdDXEEBW4Ac4HXi6S71xIIAH8UzXo2dmxNYdo7 m24=; 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 1xSvSd-0000000058J-3oNY; Mon, 16 Nov 2026 12:05:23 +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 1xSvSc-0000000058C-1qpA for openvpn-devel@lists.sourceforge.net; Mon, 16 Nov 2026 12:05:22 +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=FaunkVGQVt+2u4iSXpgip+RUmUk1TH1SuArEy2WHfr8=; b=m8CuOOqHYoTH0q73gsFe1kRtoi 6gaA2LwPfb+F/uUT0GHVKwN1Oy1Zw+Vd+vDlvTSwrHrSHcpvEz6pQY7tff0xUE2Npo40xUZBoQv7G 00ajklYer7PwSXpEtYOgSQfOtdNCHm9PFfSuAuAQiX4bbSa7bcyA/YC8Qmd0s2KKz5/k=; 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=FaunkVGQVt+2u4iSXpgip+RUmUk1TH1SuArEy2WHfr8=; b=dDOG14Wu0F3ud5EsNecEhOCwPr 2CiJNGLKSGOMtRXlfipdlP7ozC2ZE2B8CIMLwaIYkJVSE+RUbi1TotLKLMS85mWmArPwceNiJkIsu GOEqeK0nrqavOOVKuDuQ4lMqc29aEKyaB57GGaNfTfWzloySOsHnAOlWti0IKdHSuHQc=; 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 1xSvSa-000000002xy-32GJ for openvpn-devel@lists.sourceforge.net; Mon, 16 Nov 2026 12:05:22 +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 6AGC5DLd024150 for ; Mon, 16 Nov 2026 13:05:13 +0100 Received: (from gert@localhost) by blue.greenie.muc.de (8.18.2/8.18.1/Submit) id 6AGC5DiR024149 for openvpn-devel@lists.sourceforge.net; Mon, 16 Nov 2026 13:05:13 +0100 From: Gert Doering To: openvpn-devel@lists.sourceforge.net Date: Mon, 16 Nov 2026 13:05:08 +0100 Message-ID: <20261116120513.24130-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-2.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: Frank Lichtenheld Also removes some inconsistencies and some sign conversion warnings. Change-Id: I6f5ca53d4288b0921610f7a3f84ad55d2ab49118 Signed-off-by: Frank Lichtenheld Acked-by: Razvan Cojocaru Gerrit URL: https://gerrit.openvpn.net/c/o [...] 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: 1xSvSa-000000002xy-32GJ Subject: [Openvpn-devel] [PATCH v3] networking_sitnl: Reduce boilerplate code for parameter checks 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: 1881675102849014122 X-GMAIL-MSGID: 1882016423034547814 From: Frank Lichtenheld Also removes some inconsistencies and some sign conversion warnings. Change-Id: I6f5ca53d4288b0921610f7a3f84ad55d2ab49118 Signed-off-by: Frank Lichtenheld Acked-by: Razvan Cojocaru Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/2177 --- 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/+/2177 This mail reflects revision 3 of this Change. Acked-by according to Gerrit (reflected above): Razvan Cojocaru diff --git a/src/openvpn/networking_sitnl.c b/src/openvpn/networking_sitnl.c index d5ec6bb..b7c0f58 100644 --- a/src/openvpn/networking_sitnl.c +++ b/src/openvpn/networking_sitnl.c @@ -677,25 +677,37 @@ #ifdef ENABLE_SITNL +static int +get_ifindex(const char *func, const char *iface, uint32_t *ifindex) +{ + if (!iface) + { + msg(M_WARN, "%s: passed NULL interface", func); + return -EINVAL; + } + + *ifindex = if_nametoindex(iface); + if (*ifindex == 0) + { + msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", func, iface); + return -ENOENT; + } + return 0; +} + + int net_iface_up(const openvpn_net_ctx_t *ctx, const char *iface, bool up) { struct sitnl_link_req req; - int ifindex; + uint32_t ifindex; CLEAR(req); - if (!iface) + const int ret = get_ifindex(__func__, iface, &ifindex); + if (ret < 0) { - msg(M_WARN, "%s: passed NULL interface", __func__); - return -EINVAL; - } - - ifindex = if_nametoindex(iface); - if (ifindex == 0) - { - msg(M_WARN, "%s: rtnl: cannot get ifindex for %s: %s", __func__, iface, strerror(errno)); - return -ENOENT; + return ret; } req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i)); @@ -703,7 +715,7 @@ req.n.nlmsg_type = RTM_SETLINK; req.i.ifi_family = AF_PACKET; - req.i.ifi_index = ifindex; + req.i.ifi_index = (int)ifindex; req.i.ifi_change |= IFF_UP; if (up) { @@ -711,7 +723,7 @@ } else { - req.i.ifi_flags &= ~IFF_UP; + req.i.ifi_flags &= ~(unsigned int)IFF_UP; } msg(M_INFO, "%s: set %s %s", __func__, iface, up ? "up" : "down"); @@ -723,14 +735,13 @@ net_iface_mtu_set(const openvpn_net_ctx_t *ctx, const char *iface, uint32_t mtu) { struct sitnl_link_req req; - int ifindex, ret = -1; + int ret = -1; + uint32_t ifindex; CLEAR(req); - ifindex = if_nametoindex(iface); - if (ifindex == 0) + if (get_ifindex(__func__, iface, &ifindex) < 0) { - msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __func__, iface); return -1; } @@ -739,7 +750,7 @@ req.n.nlmsg_type = RTM_SETLINK; req.i.ifi_family = AF_PACKET; - req.i.ifi_index = ifindex; + req.i.ifi_index = (int)ifindex; SITNL_ADDATTR(&req.n, sizeof(req), IFLA_MTU, &mtu, 4); @@ -754,14 +765,13 @@ net_addr_ll_set(const openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, uint8_t *addr) { struct sitnl_link_req req; - int ifindex, ret = -1; + int ret = -1; + uint32_t ifindex; CLEAR(req); - ifindex = if_nametoindex(iface); - if (ifindex == 0) + if (get_ifindex(__func__, iface, &ifindex) < 0) { - msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __func__, iface); return -1; } @@ -770,7 +780,7 @@ req.n.nlmsg_type = RTM_SETLINK; req.i.ifi_family = AF_PACKET; - req.i.ifi_index = ifindex; + req.i.ifi_index = (int)ifindex; SITNL_ADDATTR(&req.n, sizeof(req), IFLA_ADDRESS, addr, OPENVPN_ETH_ALEN); @@ -782,22 +792,19 @@ } static int -sitnl_addr_set(uint16_t cmd, uint16_t flags, int ifindex, sa_family_t af_family, +sitnl_addr_set(uint16_t cmd, uint16_t flags, const char *iface, sa_family_t af_family, const inet_address_t *local, const inet_address_t *remote, int prefixlen) { struct sitnl_addr_req req; - uint32_t size; - int ret = -EINVAL; + size_t size; + uint32_t ifindex; - CLEAR(req); - - req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i)); - req.n.nlmsg_type = cmd; - req.n.nlmsg_flags = NLM_F_REQUEST | flags; - - req.i.ifa_index = ifindex; - ASSERT(af_family <= UINT8_MAX); - req.i.ifa_family = (uint8_t)af_family; + int ret = get_ifindex(__func__, iface, &ifindex); + if (ret < 0) + { + return ret; + } + ret = -EINVAL; /* for SITNL_ADDATTR */ switch (af_family) { @@ -814,10 +821,20 @@ return -EINVAL; } + CLEAR(req); + + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(req.i)); + req.n.nlmsg_type = cmd; + req.n.nlmsg_flags = NLM_F_REQUEST | flags; + + req.i.ifa_index = ifindex; + ASSERT(af_family <= UINT8_MAX); + req.i.ifa_family = (uint8_t)af_family; + /* if no prefixlen has been specified, assume host address */ if (prefixlen == 0) { - prefixlen = size * 8; + prefixlen = (int)size * 8; } ASSERT(prefixlen <= UINT8_MAX); req.i.ifa_prefixlen = (uint8_t)prefixlen; @@ -852,76 +869,37 @@ sitnl_addr_ptp_add(sa_family_t af_family, const char *iface, const inet_address_t *local, const inet_address_t *remote) { - int ifindex; - - switch (af_family) - { - case AF_INET: - case AF_INET6: - break; - - default: - return -EINVAL; - } - - if (!iface) - { - msg(M_WARN, "%s: passed NULL interface", __func__); - return -EINVAL; - } - - ifindex = if_nametoindex(iface); - if (ifindex == 0) - { - msg(M_WARN, "%s: cannot get ifindex for %s: %s", __func__, np(iface), strerror(errno)); - return -ENOENT; - } - - return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, ifindex, af_family, local, + return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, iface, af_family, local, remote, 0); } static int sitnl_addr_ptp_del(sa_family_t af_family, const char *iface, const inet_address_t *local) { - int ifindex; - - switch (af_family) - { - case AF_INET: - case AF_INET6: - break; - - default: - return -EINVAL; - } - - if (!iface) - { - msg(M_WARN, "%s: passed NULL interface", __func__); - return -EINVAL; - } - - ifindex = if_nametoindex(iface); - if (ifindex == 0) - { - msg(M_WARN | M_ERRNO, "%s: cannot get ifindex for %s", __func__, iface); - return -ENOENT; - } - - return sitnl_addr_set(RTM_DELADDR, 0, ifindex, af_family, local, NULL, 0); + return sitnl_addr_set(RTM_DELADDR, 0, iface, af_family, local, NULL, 0); } static int -sitnl_route_set(uint16_t cmd, uint16_t flags, int ifindex, sa_family_t af_family, const void *dst, +sitnl_route_set(uint16_t cmd, uint16_t flags, const char *iface, sa_family_t af_family, const void *dst, int prefixlen, const void *gw, enum rt_class_t table, int metric, enum rt_scope_t scope, unsigned char protocol, unsigned char type) { struct sitnl_route_req req; - int ret = -1, size; + int ret = -1; /* for SITNL_ADDATTR */ + size_t size; + uint32_t ifindex = 0; CLEAR(req); + if (iface) + { + const int ifi_ret = get_ifindex(__func__, iface, &ifindex); + if (ifi_ret < 0) + { + return ifi_ret; + } + } + switch (af_family) { case AF_INET: @@ -986,64 +964,14 @@ static int sitnl_addr_add(sa_family_t af_family, const char *iface, const inet_address_t *addr, int prefixlen) { - int ifindex; - - switch (af_family) - { - case AF_INET: - case AF_INET6: - break; - - default: - return -EINVAL; - } - - if (!iface) - { - msg(M_WARN, "%s: passed NULL interface", __func__); - return -EINVAL; - } - - ifindex = if_nametoindex(iface); - if (ifindex == 0) - { - msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __func__, iface); - return -ENOENT; - } - - return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, ifindex, af_family, addr, NULL, + return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, iface, af_family, addr, NULL, prefixlen); } static int sitnl_addr_del(sa_family_t af_family, const char *iface, const inet_address_t *addr, int prefixlen) { - int ifindex; - - switch (af_family) - { - case AF_INET: - case AF_INET6: - break; - - default: - return -EINVAL; - } - - if (!iface) - { - msg(M_WARN, "%s: passed NULL interface", __func__); - return -EINVAL; - } - - ifindex = if_nametoindex(iface); - if (ifindex == 0) - { - msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __func__, iface); - return -ENOENT; - } - - return sitnl_addr_set(RTM_DELADDR, 0, ifindex, af_family, addr, NULL, prefixlen); + return sitnl_addr_set(RTM_DELADDR, 0, iface, af_family, addr, NULL, prefixlen); } int @@ -1190,17 +1118,6 @@ const void *gw, uint32_t table, int metric) { enum rt_scope_t scope = RT_SCOPE_UNIVERSE; - int ifindex = 0; - - if (iface) - { - ifindex = if_nametoindex(iface); - if (ifindex == 0) - { - msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifindex for %s", __func__, iface); - return -ENOENT; - } - } if (table == 0) { @@ -1212,7 +1129,7 @@ scope = RT_SCOPE_LINK; } - return sitnl_route_set(RTM_NEWROUTE, NLM_F_CREATE, ifindex, af_family, dst, prefixlen, gw, + return sitnl_route_set(RTM_NEWROUTE, NLM_F_CREATE, iface, af_family, dst, prefixlen, gw, table, metric, scope, RTPROT_BOOT, RTN_UNICAST); } @@ -1277,24 +1194,12 @@ sitnl_route_del(const char *iface, sa_family_t af_family, const inet_address_t *dst, int prefixlen, const inet_address_t *gw, uint32_t table, int metric) { - int ifindex = 0; - - if (iface) - { - ifindex = if_nametoindex(iface); - if (ifindex == 0) - { - msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifindex for %s", __func__, iface); - return -ENOENT; - } - } - if (table == 0) { table = RT_TABLE_MAIN; } - return sitnl_route_set(RTM_DELROUTE, 0, ifindex, af_family, dst, prefixlen, gw, table, metric, + return sitnl_route_set(RTM_DELROUTE, 0, iface, af_family, dst, prefixlen, gw, table, metric, RT_SCOPE_NOWHERE, 0, 0); } @@ -1464,7 +1369,7 @@ net_iface_type(const openvpn_net_ctx_t *ctx, const char *iface, char type[IFACE_TYPE_LEN_MAX]) { struct sitnl_link_req req = {}; - int ifindex = if_nametoindex(iface); + int ifindex = (int)if_nametoindex(iface); if (!ifindex) { @@ -1496,7 +1401,7 @@ net_iface_del(const openvpn_net_ctx_t *ctx, const char *iface) { struct sitnl_link_req req = {}; - int ifindex = if_nametoindex(iface); + int ifindex = (int)if_nametoindex(iface); if (!ifindex) {