Skip to content

Commit 558efe2

Browse files
committed
Add support for user defined network namespace
Introduce the --netns option to allow interface creation into a user-defined network namespace. This allows the VPN data plane to be isolated from the main OpenVPN process namespace. The current netlink library integration supports interface creation and deletion in a target namespace. However, subsequent configuration operations (e.g. address or mtu set) are executed in the caller's namespace, as they rely on the default netlink socket context. As a result, interface-related configuration performed after creation may be applied in the wrong namespace. Introduce helper functions to temporarily switch the process to the requested network namespace using setns(2), execute the required netlink operations, and then restore the original namespace. The namespace switch is temporary and scoped to each netlink operation. Once the operation completes, the original namespace is restored to preserve the process execution context. Note: This feature is Linux-only and depends on setns(2). It is not compatible (yet) with Data Channel Offload (DCO). Change-Id: I8b0d1cad7062856abcc40c4e16ec93b45295bbd3 Signed-off-by: Gianmarco De Gregori <gianmarco@mandelbit.com>
1 parent ee2af66 commit 558efe2

8 files changed

Lines changed: 610 additions & 82 deletions

File tree

doc/man-sections/vpn-network-options.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,12 @@ routing.
304304
Specify the link layer address, more commonly known as the MAC address.
305305
Only applied to TAP devices.
306306

307+
--netns name
308+
Move the created tunnel interface into the specified network
309+
namespace. The namespace must already exist.
310+
311+
(Supported on Linux only, on other platforms this is a no-op).
312+
307313
--persist-tun
308314
Don't close and reopen TUN/TAP device or run up/down scripts across
309315
:code:`SIGUSR1` or ``--ping-restart`` restarts.

src/openvpn/dco.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,12 @@ dco_check_startup_option(msglvl_t msglevel, const struct options *o)
388388
ret);
389389
}
390390
}
391+
392+
if (o->netns)
393+
{
394+
msg(msglevel, "Note: --netns not supported by DCO, disabling data channel offload.");
395+
return false;
396+
}
391397
#endif /* if defined(_WIN32) */
392398

393399
#if defined(HAVE_LIBCAPNG)

src/openvpn/networking.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ typedef void *openvpn_net_ctx_t;
3939
typedef void *openvpn_net_iface_t;
4040
#endif /* ifdef ENABLE_SITNL */
4141

42-
/* Only the iproute2 backend implements these functions,
42+
/* Only the iproute2 and sitnl backend implements these functions,
4343
* the rest can rely on these stubs
4444
*/
45-
#if !defined(ENABLE_IPROUTE)
45+
#if !defined(ENABLE_IPROUTE) && !defined(ENABLE_SITNL) || defined(TARGET_ANDROID)
4646
static inline int
4747
net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)
4848
{
@@ -63,7 +63,7 @@ net_ctx_free(openvpn_net_ctx_t *ctx)
6363
{
6464
(void)ctx;
6565
}
66-
#endif /* !defined(ENABLE_IPROUTE) */
66+
#endif /* !defined(ENABLE_IPROUTE) && !defined(ENABLE_SITNL) || defined(TARGET_ANDROID) */
6767

6868
#if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE)
6969

0 commit comments

Comments
 (0)