From patchwork Fri Jun 26 08:49:44 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maximilian Wilhelm X-Patchwork-Id: 1184 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director11.mail.ord1d.rsapps.net ([172.28.255.1]) by backend30.mail.ord1d.rsapps.net with LMTP id sBgzIplD9l7YCAAAIUCqbw for ; Fri, 26 Jun 2020 14:51:05 -0400 Received: from proxy5.mail.ord1c.rsapps.net ([172.28.255.1]) by director11.mail.ord1d.rsapps.net with LMTP id YFcIIplD9l7UAQAAvGGmqA ; Fri, 26 Jun 2020 14:51:05 -0400 Received: from smtp12.gate.ord1c ([172.28.255.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy5.mail.ord1c.rsapps.net with LMTP id CJjPIZlD9l4FRgAAPBRIyg ; Fri, 26 Jun 2020 14:51:05 -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: smtp12.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=rfc2324.org X-Suspicious-Flag: YES X-Classification-ID: fcb71e10-b7dd-11ea-93f2-bc305bf03e5c-1-1 Received: from [216.105.38.7] ([216.105.38.7:58354] helo=lists.sourceforge.net) by smtp12.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 7A/3D-02225-89346FE5; Fri, 26 Jun 2020 14:51:05 -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 1jotQU-0002Ib-Ur; Fri, 26 Jun 2020 18:50:14 +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 1jotQP-0002IL-89 for openvpn-devel@lists.sourceforge.net; Fri, 26 Jun 2020 18:50:09 +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=yGh3jDlQqJPM3uvrzxAmRe7HMN2tXFhQkTiLNHaXKz8=; b=QFCrxWZxlpiprII9fVCC1vZjWb Yec4m+cUIH8eo+GAyNwE3KpIMDSWdFjHIrRrTDkhVGncAbffA1n+2XC9yGasVfPCp8kYskEfdtDds n5v7FE0/04SlwEQsH7gJLdrlV5kPA4qkIj7NkkwjeY4kTJM663uL8AWeKqP5WQE+6BZg=; 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=yGh3jDlQqJPM3uvrzxAmRe7HMN2tXFhQkTiLNHaXKz8=; b=OX1WJOZ3hmfHjq/N2yup7OHYRJ /4hHhi8ptSiqqV4OQsC85c8htANKAcc3rAao6rkymzR3jUXkeuP1a9LPFg9Qiq94AJo+snL8E2zhC 2r/RD4xlHS9bbRooyHUyEHqe73Bs1pk1KoqG7L3PD5NGLfXqijvqV3p6Pur++0EUVaq8=; 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 1jotQN-002I36-L6 for openvpn-devel@lists.sourceforge.net; Fri, 26 Jun 2020 18:50:09 +0000 Received: from rfc2324.org ([31.172.8.84] helo=principal.rfc2324.org) by mail.rfc2324.org with esmtp rfc2324.org Mailserver id 1jotQC-0007PN-A5; Fri, 26 Jun 2020 20:49:57 +0200 Received: by principal.rfc2324.org (Postfix, from userid 666) id C26A98F2CD; Fri, 26 Jun 2020 20:49:55 +0200 (CEST) From: Maximilian Wilhelm To: openvpn-devel@lists.sourceforge.net Date: Fri, 26 Jun 2020 20:49:44 +0200 Message-Id: <1593197385-30618-2-git-send-email-max@rfc2324.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1593197385-30618-1-git-send-email-max@rfc2324.org> References: <1593197385-30618-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: 1jotQN-002I36-L6 Subject: [Openvpn-devel] [PATCH 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 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 --- src/openvpn/init.c | 1 + src/openvpn/options.c | 10 ++++++++++ src/openvpn/options.h | 1 + src/openvpn/socket.c | 10 ++++++++++ src/openvpn/socket.h | 2 ++ 5 files changed, 24 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 3484f7d..6b36269 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 @@ -6064,6 +6067,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..fd92d52 100644 --- a/src/openvpn/socket.c +++ b/src/openvpn/socket.c @@ -1138,6 +1138,14 @@ 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); + setsockopt (sock->sd, SOL_SOCKET, SO_BINDTODEVICE, sock->bind_dev, strlen (sock->bind_dev) + 1); + } +#endif + bind_local(sock, addr->ai_family); } @@ -1880,6 +1888,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 +1915,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 Fri Jun 26 08:49:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Maximilian Wilhelm X-Patchwork-Id: 1183 Return-Path: Delivered-To: patchwork@openvpn.net Delivered-To: patchwork@openvpn.net Received: from director12.mail.ord1d.rsapps.net ([172.28.255.1]) by backend30.mail.ord1d.rsapps.net with LMTP id MNwbCo9D9l7YCAAAIUCqbw for ; Fri, 26 Jun 2020 14:50:55 -0400 Received: from proxy7.mail.ord1c.rsapps.net ([172.28.255.1]) by director12.mail.ord1d.rsapps.net with LMTP id wKL3CY9D9l60QQAAIasKDg ; Fri, 26 Jun 2020 14:50:55 -0400 Received: from smtp33.gate.ord1c ([172.28.255.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by proxy7.mail.ord1c.rsapps.net with LMTP id oJuDCY9D9l5GeAAAknS3pQ ; Fri, 26 Jun 2020 14:50:55 -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: smtp33.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=rfc2324.org X-Suspicious-Flag: YES X-Classification-ID: f688e3e8-b7dd-11ea-aa14-54520067fec4-1-1 Received: from [216.105.38.7] ([216.105.38.7:38362] helo=lists.sourceforge.net) by smtp33.gate.ord1c.rsapps.net (envelope-from ) (ecelerity 4.2.38.62370 r(:)) with ESMTPS (cipher=DHE-RSA-AES256-GCM-SHA384) id 30/1C-32062-E8346FE5; Fri, 26 Jun 2020 14:50:54 -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.90_1) (envelope-from ) id 1jotQU-0001pZ-0e; Fri, 26 Jun 2020 18:50:14 +0000 Received: from [172.30.20.202] (helo=mx.sourceforge.net) by sfs-ml-1.v29.lw.sourceforge.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jotQO-0001p3-DR for openvpn-devel@lists.sourceforge.net; Fri, 26 Jun 2020 18:50:08 +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=lGrxVtIkbgEMOOHZYzM7VWNKbS elU79fIU9RgHfjPxFAoZS5MRLWIqB8MMqq/gP3nXRe/drkVr1hbfvy17Ux0mVokYmvtk7b+2W5tQx ZaRdoBwO2VfjzYyO6KCXkW3tK/oK0/UkxXOAEvVmVf2vpULMg/GJXa20eVdjkdTpflSQ=; 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=gy2JtOmbfkNnF48aRgm/0wapHQ hG4d6JUSc8SjMZANcB6xzjxlqmGOuufHp8sgSuG6z4sugxzazkUlEKEAwbrnQGgHV9U0MugP177Yt mVt0b75uk5jPjfHDXBTEHnF6nnd84oxYlgex5UGFZZAp0StiC/7Vz0F3CY9gj9OJUC0k=; 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 1jotQM-00Gd96-QX for openvpn-devel@lists.sourceforge.net; Fri, 26 Jun 2020 18:50:08 +0000 Received: from rfc2324.org ([31.172.8.84] helo=principal.rfc2324.org) by mail.rfc2324.org with esmtp rfc2324.org Mailserver id 1jotQC-0007PO-9x; Fri, 26 Jun 2020 20:49:57 +0200 Received: by principal.rfc2324.org (Postfix, from userid 666) id C695A8F30D; Fri, 26 Jun 2020 20:49:55 +0200 (CEST) From: Maximilian Wilhelm To: openvpn-devel@lists.sourceforge.net Date: Fri, 26 Jun 2020 20:49:45 +0200 Message-Id: <1593197385-30618-3-git-send-email-max@rfc2324.org> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1593197385-30618-1-git-send-email-max@rfc2324.org> References: <1593197385-30618-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: 1jotQM-00Gd96-QX Subject: [Openvpn-devel] [PATCH 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