From patchwork Sun Apr 1 03:16:14 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonio Quartulli X-Patchwork-Id: 278 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director11.mail.ord1d.rsapps.net ([172.27.255.1]) by backend30.mail.ord1d.rsapps.net (Dovecot) with LMTP id +w/uIvDhwFpvXAAAIUCqbw for ; Sun, 01 Apr 2018 09:43:12 -0400 Received: from proxy20.mail.iad3a.rsapps.net ([172.27.255.1]) by director11.mail.ord1d.rsapps.net (Dovecot) with LMTP id 441dG/DhwFpHVgAAvGGmqA ; Sun, 01 Apr 2018 09:43:12 -0400 Received: from smtp39.gate.iad3a ([172.27.255.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy20.mail.iad3a.rsapps.net with LMTP id WNluGfDhwFreSwAAtfLT2w ; Sun, 01 Apr 2018 09:43:12 -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: smtp39.gate.iad3a.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: 9e5b936e-35b2-11e8-8c96-525400eea4e4-1-1 Received: from [216.105.38.7] ([216.105.38.7:24461] helo=lists.sourceforge.net) by smtp39.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.1.56364 r(Core:4.2.1.14)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 99/DD-19544-FE1E0CA5; Sun, 01 Apr 2018 09:43:12 -0400 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.90_1) (envelope-from ) id 1f2dFh-0004b1-8W; Sun, 01 Apr 2018 13:42:33 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1f2dFf-0004aq-Ha for openvpn-devel@lists.sourceforge.net; Sun, 01 Apr 2018 13:42:31 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=References:In-Reply-To:Message-Id:Date:Subject:Cc: To:From:Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: 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=rB9IvX6i1hQNKe3O0fKz8720rp/erpvc+qCDlphwt8w=; b=XCp5x/2yQMvZqafgBI9sxP2oPN xwbGb0asLMh0HcvkP0z9qkZpg0DieDYy0e/DjJ4iMxqKfpCdxxcRBmyxHMw2+/sCTvq/9s6JckYJZ vTlBroAqMwqmp6ZwiY+jwX534ANhiEL4eaPVpbDtqbtQfpjiX9qKvpuqDVDmOgtsTwC8=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To :MIME-Version:Content-Type:Content-Transfer-Encoding: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=rB9IvX6i1hQNKe3O0fKz8720rp/erpvc+qCDlphwt8w=; b=iHbzvNPgKIjTZcrs3go1KlbVnb z/pPvuLfxXb55RRbd4kmMZIAzfVWebfbTxqrHjfpeRtaFQ8m8QeA9q8IMt3hkOPYnef1FoWvJP6MS 4vwAXG07k0Y9vj/JKK3i0bDkHS+9x553DHD8vxduRQ2lh+s2E5SMDD04Vhun0vq45PGg=; Received: from s2.neomailbox.net ([5.148.176.60]) by sfi-mx-3.v28.lw.sourceforge.com with esmtps (TLSv1.2:DHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) id 1f2dFd-004ubA-6A for openvpn-devel@lists.sourceforge.net; Sun, 01 Apr 2018 13:42:31 +0000 From: Antonio Quartulli To: openvpn-devel@lists.sourceforge.net Date: Sun, 1 Apr 2018 21:16:14 +0800 Message-Id: <20180401131615.12567-4-a@unstable.cc> In-Reply-To: <20180401131615.12567-1-a@unstable.cc> References: <20180401131615.12567-1-a@unstable.cc> X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [5.148.176.60 listed in list.dnswl.org] -0.0 SPF_HELO_PASS SPF: HELO matches SPF record -0.0 SPF_PASS SPF: sender matches SPF record X-Headers-End: 1f2dFd-004ubA-6A Subject: [Openvpn-devel] [RFC 3/4] tun.c: use sitnl to handle tun configuration on Linux 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 MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox Signed-off-by: Antonio Quartulli --- src/openvpn/tun.c | 199 ++++++++++++++++-------------------------------------- 1 file changed, 58 insertions(+), 141 deletions(-) diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index 263cacdf..4e0b3f90 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -46,6 +46,7 @@ #include "route.h" #include "win32.h" #include "block_dns.h" +#include "sitnl.h" #include "memdbg.h" @@ -883,10 +884,12 @@ do_ifconfig(struct tuntap *tt, if (tt->did_ifconfig_setup) { bool tun = false; +#if !defined(TARGET_LINUX) const char *ifconfig_local = NULL; const char *ifconfig_remote_netmask = NULL; const char *ifconfig_broadcast = NULL; const char *ifconfig_ipv6_local = NULL; +#endif bool do_ipv6 = false; struct argv argv = argv_new(); @@ -898,18 +901,23 @@ do_ifconfig(struct tuntap *tt, */ tun = is_tun_p2p(tt); +#if !defined(TARGET_LINUX) /* * Set ifconfig parameters */ ifconfig_local = print_in_addr_t(tt->local, 0, &gc); ifconfig_remote_netmask = print_in_addr_t(tt->remote_netmask, 0, &gc); +#endif if (tt->did_ifconfig_ipv6_setup) { +#if !defined(TARGET_LINUX) ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, &gc); +#endif do_ipv6 = true; } +#if !defined(TARGET_LINUX) /* * If TAP-style device, generate broadcast address. */ @@ -917,6 +925,7 @@ do_ifconfig(struct tuntap *tt, { ifconfig_broadcast = print_in_addr_t(tt->broadcast, 0, &gc); } +#endif #ifdef ENABLE_MANAGEMENT if (management) @@ -933,102 +942,43 @@ do_ifconfig(struct tuntap *tt, #if defined(TARGET_LINUX) -#ifdef ENABLE_IPROUTE - /* - * Set the MTU for the device - */ - argv_printf(&argv, - "%s link set dev %s up mtu %d", - iproute_path, - actual, - tun_mtu - ); - argv_msg(M_INFO, &argv); - openvpn_execve_check(&argv, es, S_FATAL, "Linux ip link set failed"); - - if (tun) - { - - /* - * Set the address for the device - */ - argv_printf(&argv, - "%s addr add dev %s local %s peer %s", - iproute_path, - actual, - ifconfig_local, - ifconfig_remote_netmask - ); - argv_msg(M_INFO, &argv); - openvpn_execve_check(&argv, es, S_FATAL, "Linux ip addr add failed"); - } - else + if (sitnl_iface_mtu_set(actual, tun_mtu) < 0) { - argv_printf(&argv, - "%s addr add dev %s %s/%d broadcast %s", - iproute_path, - actual, - ifconfig_local, - netmask_to_netbits2(tt->remote_netmask), - ifconfig_broadcast - ); - argv_msg(M_INFO, &argv); - openvpn_execve_check(&argv, es, S_FATAL, "Linux ip addr add failed"); + msg(M_FATAL, "Linux can't set mtu (%d) on %s", tun_mtu, actual); } - if (do_ipv6) + + if (sitnl_iface_up(actual, true) < 0) { - argv_printf( &argv, - "%s -6 addr add %s/%d dev %s", - iproute_path, - ifconfig_ipv6_local, - tt->netbits_ipv6, - actual - ); - argv_msg(M_INFO, &argv); - openvpn_execve_check(&argv, es, S_FATAL, "Linux ip -6 addr add failed"); + msg(M_FATAL, "Linux can't bring %s up", actual); } - tt->did_ifconfig = true; -#else /* ifdef ENABLE_IPROUTE */ + if (tun) { - argv_printf(&argv, - "%s %s %s pointopoint %s mtu %d", - IFCONFIG_PATH, - actual, - ifconfig_local, - ifconfig_remote_netmask, - tun_mtu - ); + if (sitnl_addr_ptp_v4_add(actual, &tt->local, + &tt->remote_netmask) < 0) + { + msg(M_FATAL, "Linux can't add IP to TUN interface %s", actual); + } } else { - argv_printf(&argv, - "%s %s %s netmask %s mtu %d broadcast %s", - IFCONFIG_PATH, - actual, - ifconfig_local, - ifconfig_remote_netmask, - tun_mtu, - ifconfig_broadcast - ); + if (sitnl_addr_v4_add(actual, &tt->local, + netmask_to_netbits2(tt->remote_netmask), + &tt->remote_netmask) < 0) + { + msg(M_FATAL, "Linux can't add IP to TAP interface %s", actual); + } } - argv_msg(M_INFO, &argv); - openvpn_execve_check(&argv, es, S_FATAL, "Linux ifconfig failed"); if (do_ipv6) { - argv_printf(&argv, - "%s %s add %s/%d", - IFCONFIG_PATH, - actual, - ifconfig_ipv6_local, - tt->netbits_ipv6 - ); - argv_msg(M_INFO, &argv); - openvpn_execve_check(&argv, es, S_FATAL, "Linux ifconfig inet6 failed"); + if (sitnl_addr_v6_add(actual, &tt->local_ipv6, + tt->netbits_ipv6) < 0) + { + msg(M_FATAL, "Linux can't add IPv6 to interface %s", actual); + } } tt->did_ifconfig = true; -#endif /*ENABLE_IPROUTE*/ #elif defined(TARGET_ANDROID) if (do_ipv6) @@ -2103,77 +2053,44 @@ tuncfg(const char *dev, const char *dev_type, const char *dev_node, int persist_ void close_tun(struct tuntap *tt) { - if (tt) + if (!tt) { - if (tt->type != DEV_TYPE_NULL && tt->did_ifconfig) - { - struct argv argv = argv_new(); - struct gc_arena gc = gc_new(); + return; + } -#ifdef ENABLE_IPROUTE - if (is_tun_p2p(tt)) + if (tt->type != DEV_TYPE_NULL && tt->did_ifconfig) + { + int netbits = netmask_to_netbits2(tt->remote_netmask); + if (is_tun_p2p(tt)) + { + if (sitnl_addr_ptp_v4_del(tt->actual_name, &tt->local) < 0) { - argv_printf(&argv, - "%s addr del dev %s local %s peer %s", - iproute_path, - tt->actual_name, - print_in_addr_t(tt->local, 0, &gc), - print_in_addr_t(tt->remote_netmask, 0, &gc) - ); + msg(M_WARN, "Linux can't del IP from TUN iface %s", + tt->actual_name); } - else + } + else + { + if (sitnl_addr_v4_del(tt->actual_name, &tt->local, netbits) < 0) { - argv_printf(&argv, - "%s addr del dev %s %s/%d", - iproute_path, - tt->actual_name, - print_in_addr_t(tt->local, 0, &gc), - netmask_to_netbits2(tt->remote_netmask) - ); + msg(M_WARN, "Linux can't del IP from TAP iface %s", + tt->actual_name); } -#else /* ifdef ENABLE_IPROUTE */ - argv_printf(&argv, - "%s %s 0.0.0.0", - IFCONFIG_PATH, - tt->actual_name - ); -#endif /* ifdef ENABLE_IPROUTE */ - - argv_msg(M_INFO, &argv); - openvpn_execve_check(&argv, NULL, 0, "Linux ip addr del failed"); + } - if (tt->did_ifconfig_ipv6_setup) + if (tt->did_ifconfig_ipv6_setup) + { + if (sitnl_addr_v6_del(tt->actual_name, &tt->local_ipv6, + tt->netbits_ipv6) < 0) { - const char *ifconfig_ipv6_local = print_in6_addr(tt->local_ipv6, 0, &gc); - -#ifdef ENABLE_IPROUTE - argv_printf(&argv, "%s -6 addr del %s/%d dev %s", - iproute_path, - ifconfig_ipv6_local, - tt->netbits_ipv6, - tt->actual_name - ); - argv_msg(M_INFO, &argv); - openvpn_execve_check(&argv, NULL, 0, "Linux ip -6 addr del failed"); -#else /* ifdef ENABLE_IPROUTE */ - argv_printf(&argv, - "%s %s del %s/%d", - IFCONFIG_PATH, - tt->actual_name, - ifconfig_ipv6_local, - tt->netbits_ipv6 - ); - argv_msg(M_INFO, &argv); - openvpn_execve_check(&argv, NULL, 0, "Linux ifconfig inet6 del failed"); -#endif + msg(M_WARN, "Linux can't del IPv6 from iface %s", + tt->actual_name); } - - argv_reset(&argv); - gc_free(&gc); } - close_tun_generic(tt); - free(tt); } + + close_tun_generic(tt); + free(tt); } int