From patchwork Mon Jun 29 00:49:07 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maximilian Wilhelm X-Patchwork-Id: 1185 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director7.mail.ord1d.rsapps.net ([172.27.255.59]) by backend30.mail.ord1d.rsapps.net with LMTP id qItuK23H+V54EQAAIUCqbw for ; Mon, 29 Jun 2020 06:50:21 -0400 Received: from proxy8.mail.iad3a.rsapps.net ([172.27.255.59]) by director7.mail.ord1d.rsapps.net with LMTP id aPI6KW3H+V7sDwAAovjBpQ ; Mon, 29 Jun 2020 06:50:21 -0400 Received: from smtp3.gate.iad3a ([172.27.255.59]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy8.mail.iad3a.rsapps.net with LMTP id qH4yJG3H+V7dcgAAsBr/qg ; Mon, 29 Jun 2020 06:50:21 -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: smtp3.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=rfc2324.org X-Suspicious-Flag: YES X-Classification-ID: 53a1faa0-b9f6-11ea-a22c-525400af4d07-1-1 Received: from [216.105.38.7] ([216.105.38.7:41432] helo=lists.sourceforge.net) by smtp3.gate.iad3a.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id BD/34-24029-C67C9FE5; Mon, 29 Jun 2020 06:50:21 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1jprLw-0004lj-7C; Mon, 29 Jun 2020 10:49:32 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jprLv-0004lR-4d for openvpn-devel@lists.sourceforge.net; Mon, 29 Jun 2020 10:49:31 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Subject:References:In-Reply-To:Message-Id:Date: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=o/LrCo9QDL7gBK/B7utbhKurLCRgMqLUvkcsh3aaeK4=; b=E3uAizY7L0MHwnLeWtTafGvtey BVjTxDwQiPRZWYYJPStpa/gIlZF8CtZ9HoopkZDwod69vMSnpw5p080MVrXaSmHfNsvLmx/3G5ek7 J3nzcKB5QwhSCRO8Ee6BfrWxn2ejHqTT4LXffPdLzao9WtZuPuDbUVr7qf9QNDUKzbEU=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Subject:References:In-Reply-To:Message-Id:Date: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=o/LrCo9QDL7gBK/B7utbhKurLCRgMqLUvkcsh3aaeK4=; b=ERKWA4MX3NVruf0C2W4NvpdfKQ aa1xaOKvKVpoNfv9j5zvx6h0FHkt5mH6BMgAPUV6jpwBP0ILUR3mIY+txiOlQhsC0H3FTPJEJAq73 1JMamZkBEk6ZZ1Hxc2UPoTtl3doKETDzTiXw0bhrs9rFMPkQsQVgrPnAzHAuz7+xClcE=; Received: from mail.rfc2324.org ([31.172.8.67]) by sfi-mx-3.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.2) id 1jprLs-001yk2-NS for openvpn-devel@lists.sourceforge.net; Mon, 29 Jun 2020 10:49:31 +0000 Received: from rfc2324.org ([31.172.8.84] helo=principal.rfc2324.org) by mail.rfc2324.org with esmtp rfc2324.org Mailserver id 1jprLh-0005qm-0J; Mon, 29 Jun 2020 12:49:18 +0200 Received: by principal.rfc2324.org (Postfix, from userid 666) id C43A18F2E3; Mon, 29 Jun 2020 12:49:16 +0200 (CEST) From: Maximilian Wilhelm To: openvpn-devel@lists.sourceforge.net Date: Mon, 29 Jun 2020 12:49:07 +0200 Message-Id: <1593427748-29801-2-git-send-email-max@rfc2324.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1593427748-29801-1-git-send-email-max@rfc2324.org> References: <1593197385-30618-2-git-send-email-max@rfc2324.org> <1593427748-29801-1-git-send-email-max@rfc2324.org> X-SA-Exim-Connect-IP: 31.172.8.84 X-SA-Exim-Mail-From: max@principal.rfc2324.org X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.rfc2324.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.2 X-SA-Exim-Version: 4.2.1 (built Mon, 26 Dec 2011 16:24:06 +0000) X-SA-Exim-Scanned: Yes (on mail.rfc2324.org) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-Headers-End: 1jprLs-001yk2-NS Subject: [Openvpn-devel] [PATCH v2 1/2] Add --bind-dev option. 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: Maximilian Wilhelm MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Maximilian Wilhelm This options allows the user to specify a network device the OpenVPN process should use when making a connection or binding to an address. This translates in setting the SO_BINDTODEVICE option to the corresponding socket (on Linux). When for example using VRFs on Linux [0] this allows making connections using the non-default VRF and having the tun/tap interface in the default VRF. Thanks to David Ahern (Cumulus Networks) for insights on this. [0] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/Documentation/networking/vrf.txt Signed-off-by: Maximilian Wilhelm Acked-by: Gert Doering --- src/openvpn/init.c | 1 + src/openvpn/options.c | 10 ++++++++++ src/openvpn/options.h | 1 + src/openvpn/socket.c | 14 ++++++++++++++ src/openvpn/socket.h | 2 ++ 5 files changed, 28 insertions(+) diff --git a/src/openvpn/init.c b/src/openvpn/init.c index 2af25c1..dd1747f 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -3445,6 +3445,7 @@ do_init_socket_1(struct context *c, const int mode) c->options.rcvbuf, c->options.sndbuf, c->options.mark, + c->options.bind_dev, &c->c2.server_poll_interval, sockflags); } diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 9580d1d..34e2bfd 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -294,6 +294,9 @@ static const char usage_message[] = #if defined(TARGET_LINUX) && HAVE_DECL_SO_MARK "--mark value : Mark encrypted packets being sent with value. The mark value\n" " can be matched in policy routing and packetfilter rules.\n" + "--bind-dev dev : Bind to the given device when making connection to a peer or\n" + " listening for connections. This allows sending encrypted packets\n" + " via a VRF present on the system.\n" #endif "--txqueuelen n : Set the tun/tap TX queue length to n (Linux only).\n" #ifdef ENABLE_MEMSTATS @@ -6084,6 +6087,13 @@ add_option(struct options *options, } } } +#ifdef TARGET_LINUX + else if (streq (p[0], "bind-dev") && p[1]) + { + VERIFY_PERMISSION (OPT_P_SOCKFLAGS); + options->bind_dev = p[1]; + } +#endif else if (streq(p[0], "txqueuelen") && p[1] && !p[2]) { VERIFY_PERMISSION(OPT_P_GENERAL); diff --git a/src/openvpn/options.h b/src/openvpn/options.h index 375a4fc..c83a46a 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -352,6 +352,7 @@ struct options /* mark value */ int mark; + char *bind_dev; /* socket flags */ unsigned int sockflags; diff --git a/src/openvpn/socket.c b/src/openvpn/socket.c index f2fb54d..61463bc 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1138,6 +1138,18 @@ create_socket(struct link_socket *sock, struct addrinfo *addr) /* set socket to --mark packets with given value */ socket_set_mark(sock->sd, sock->mark); +#if defined(TARGET_LINUX) + if (sock->bind_dev) + { + msg (M_INFO, "Using bind-dev %s", sock->bind_dev); + if (setsockopt (sock->sd, SOL_SOCKET, SO_BINDTODEVICE, sock->bind_dev, strlen (sock->bind_dev) + 1) != 0) + { + msg(M_WARN|M_ERRNO, "WARN: setsockopt SO_BINDTODEVICE=%s failed", sock->bind_dev); + } + + } +#endif + bind_local(sock, addr->ai_family); } @@ -1880,6 +1892,7 @@ link_socket_init_phase1(struct link_socket *sock, int rcvbuf, int sndbuf, int mark, + const char *bind_dev, struct event_timeout *server_poll_timeout, unsigned int sockflags) { @@ -1906,6 +1919,7 @@ link_socket_init_phase1(struct link_socket *sock, sock->sockflags = sockflags; sock->mark = mark; + sock->bind_dev = bind_dev; sock->info.proto = proto; sock->info.af = af; diff --git a/src/openvpn/socket.h b/src/openvpn/socket.h index 38e5138..8db9e8b 100644 --- a/src/openvpn/socket.h +++ b/src/openvpn/socket.h @@ -212,6 +212,7 @@ struct link_socket #define SF_GETADDRINFO_DGRAM (1<<4) unsigned int sockflags; int mark; + const char *bind_dev; /* for stream sockets */ struct stream_buf stream_buf; @@ -326,6 +327,7 @@ link_socket_init_phase1(struct link_socket *sock, int rcvbuf, int sndbuf, int mark, + const char *bind_dev, struct event_timeout *server_poll_timeout, unsigned int sockflags); /* Reenable uncrustify *INDENT-ON* */ From patchwork Mon Jun 29 00:49:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maximilian Wilhelm X-Patchwork-Id: 1186 X-Patchwork-Delegate: davids@openvpn.net Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director9.mail.ord1d.rsapps.net ([172.30.191.6]) by backend30.mail.ord1d.rsapps.net with LMTP id aIvvJm7H+V49QAAAIUCqbw for ; Mon, 29 Jun 2020 06:50:22 -0400 Received: from proxy20.mail.ord1d.rsapps.net ([172.30.191.6]) by director9.mail.ord1d.rsapps.net with LMTP id ICjDJm7H+V57egAAalYnBA ; Mon, 29 Jun 2020 06:50:22 -0400 Received: from smtp17.gate.ord1d ([172.30.191.6]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy20.mail.ord1d.rsapps.net with LMTP id SCmfJm7H+V6JeAAAsk8m8w ; Mon, 29 Jun 2020 06:50:22 -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: smtp17.gate.ord1d.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=rfc2324.org X-Suspicious-Flag: YES X-Classification-ID: 537944a2-b9f6-11ea-b187-5254008de1cb-1-1 Received: from [216.105.38.7] ([216.105.38.7:47832] helo=lists.sourceforge.net) by smtp17.gate.ord1d.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 19/77-26787-C67C9FE5; Mon, 29 Jun 2020 06:50:20 -0400 Received: from [127.0.0.1] (helo=sfs-ml-2.v29.lw.sourceforge.com) by sfs-ml-2.v29.lw.sourceforge.com with esmtp (Exim 4.90_1) (envelope-from ) id 1jprLu-0004kn-1m; Mon, 29 Jun 2020 10:49:30 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-2.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jprLs-0004ka-T9 for openvpn-devel@lists.sourceforge.net; Mon, 29 Jun 2020 10:49:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Subject:References:In-Reply-To:Message-Id:Date: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=X+Hzw9rR6Nolpifxflckevo2N874Rv9USY7FqKZdHIU=; b=i9fFP25irpTtUbP0hufsPYrHW5 3YFTEyK5/ozxgcYkUD+sh9yhdsKgZ+1voEfZ+vzmaXkWrxBb1bsTrkobXHhBWoTFSnDTXg0unXs4k X0XiGPr8F4gqvasEdP+DN7ec+k4VQddl3DhpZwYCfKVAGQEDLkBb70jwHNcL1Raavmkc=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Subject:References:In-Reply-To:Message-Id:Date: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=X+Hzw9rR6Nolpifxflckevo2N874Rv9USY7FqKZdHIU=; b=SCxnSuaDSUwfuRhyJvVsSFaZ+M izL/kYE8SkJONxN8ghKVN6rlCmHE6WwrdyuzJsn0jc3fYAEgWsWetBHKxUMK3sTXWy4GoY4tZpWro PoFw2V7Bjk7ObTazmOx7XFW1PeYsOt7NctMYGr55jA/0Owq9F4auwa8F6bhg6GWclbxw=; Received: from mail.rfc2324.org ([31.172.8.67]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.2) id 1jprLq-005zvE-PM for openvpn-devel@lists.sourceforge.net; Mon, 29 Jun 2020 10:49:28 +0000 Received: from rfc2324.org ([31.172.8.84] helo=principal.rfc2324.org) by mail.rfc2324.org with esmtp rfc2324.org Mailserver id 1jprLh-0005qn-0K; Mon, 29 Jun 2020 12:49:19 +0200 Received: by principal.rfc2324.org (Postfix, from userid 666) id C84998F30A; Mon, 29 Jun 2020 12:49:16 +0200 (CEST) From: Maximilian Wilhelm To: openvpn-devel@lists.sourceforge.net Date: Mon, 29 Jun 2020 12:49:08 +0200 Message-Id: <1593427748-29801-3-git-send-email-max@rfc2324.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1593427748-29801-1-git-send-email-max@rfc2324.org> References: <1593197385-30618-2-git-send-email-max@rfc2324.org> <1593427748-29801-1-git-send-email-max@rfc2324.org> X-SA-Exim-Connect-IP: 31.172.8.84 X-SA-Exim-Mail-From: max@principal.rfc2324.org X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on mail.rfc2324.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.2 X-SA-Exim-Version: 4.2.1 (built Mon, 26 Dec 2011 16:24:06 +0000) X-SA-Exim-Scanned: Yes (on mail.rfc2324.org) X-Spam-Report: Spam Filtering performed by mx.sourceforge.net. See http://spamassassin.org/tag/ for more details. 0.0 SPF_NONE SPF: sender does not publish an SPF Record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record X-Headers-End: 1jprLq-005zvE-PM Subject: [Openvpn-devel] [PATCH v2 2/2] Documentation for --bind-dev / VRFs 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: Maximilian Wilhelm MIME-Version: 1.0 Errors-To: openvpn-devel-bounces@lists.sourceforge.net X-getmail-retrieved-from-mailbox: Inbox From: Maximilian Wilhelm Signed-off-by: Maximilian Wilhelm --- doc/man-sections/vrf.rst | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 doc/man-sections/vrf.rst diff --git a/doc/man-sections/vrf.rst b/doc/man-sections/vrf.rst new file mode 100644 index 0000000..f395db1 --- /dev/null +++ b/doc/man-sections/vrf.rst @@ -0,0 +1,75 @@ +Virtual Routing and Forwarding +--------------------------------------- + +Options in this section relates to configuration of virtual routing and +forwarding in combination with the underlying operating system. + +As of today this is only supported on Linux, a kernel >= 4.9 is recommended. + +This could come in handy when for example the external network should be only +used as a means to connect to some VPN endpoints and all regular traffic +should only be routed through any tunnel(s). +This could be achieved by setting up a VRF and configuring the interface +connected to the external network to be part of the VRF. The examples below +will cover this setup. + +Another option would be to put the tun/tap interface into a VRF. This could +be done by an up-script which uses the `ip link set` command shown below. + + +**VRF setup with iproute2** + +Create VRF `vrf_external` and map it to routing table `1023` + +:: + + ip link add vrf_external type vrf table 1023 + +Move `eth0` into `vrf_external` + +:: + + ip link set master vrf_external dev eth0 + +Any prefixes configured on `eth0` will be moved from the `main` routing +table into routing table `1023` + + +**VRF setup with ifupdown** + +For Debian based Distributions `ifupdown2` provides an almost drop-in +replacement for `ifupdown` including VRFs and other features. +A configuration for an interface `eth0` being part of VRF `vrf_external` +could look like this: + +:: + + auto eth0 + iface eth0 + address 192.0.2.42/24 + address 2001:db8:08:15::42/64 + gateway 192.0.2.1 + gateway 2001:db8:08:15::1 + vrf vrf_external + + auto vrf_external + iface vrf_external + vrf-table 1023 + + +**OpenVPN config** + +--bind-dev device + Set the device to bind the server socket to to the VRF + + --bind-dev vrf_external + + +**Further reading** + +Wikipedia has nice page one VRFs: https://en.wikipedia.org/wiki/Virtual_routing_and_forwarding + +This talk from the Network Track of FrOSCon 2018 provides an overview about +advanced layer 2 and layer 3 features of Linux + - Slides: https://www.slideshare.net/BarbarossaTM/l2l3-fr-fortgeschrittene-helle-und-dunkle-magie-im-linuxnetzwerkstack + - Video (german): https://media.ccc.de/v/froscon2018-2247-l2_l3_fur_fortgeschrittene_-_helle_und_dunkle_magie_im_linux-netzwerkstack