From: "its_Giaan (Code Review)" Date: Wed, 18 Mar 2026 16:14:47 +0000 In-Reply-To: References: Reply-To: gianmarco@...2726..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <9eb2adcc601a99a293a54e5da1f0b9bbd38c96f7-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="Gv3U9A3pHic="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Add support for user defined network namespace List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --Gv3U9A3pHic= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, its_Giaan, plaisthos. Hello flichtenheld, plaisthos, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/1521?usp=3Demail to look at the new patch set (#5). The following approvals got outdated and were removed: Code-Review-1 by flichtenheld Change subject: Add support for user defined network namespace ...................................................................... Add support for user defined network namespace Introduce the --netns option to allow moving the tunnel interface 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 --- M doc/man-sections/vpn-network-options.rst M src/openvpn/dco.c M src/openvpn/networking.h M src/openvpn/networking_sitnl.c M src/openvpn/networking_sitnl.h M src/openvpn/options.c M src/openvpn/options.h M src/openvpn/tun.c 8 files changed, 678 insertions(+), 81 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/21/1521/5 diff --git a/doc/man-sections/vpn-network-options.rst b/doc/man-sections/vp= n-network-options.rst index 33ebedb..cd54cd4 100644 --- a/doc/man-sections/vpn-network-options.rst +++ b/doc/man-sections/vpn-network-options.rst @@ -304,6 +304,12 @@ Specify the link layer address, more commonly known as the MAC address. Only applied to TAP devices. +--netns name + Move the created tunnel interface into the specified network + namespace. The namespace must already exist. + + (Supported on Linux only, on other platforms this is a no-op). + --persist-tun Don't close and reopen TUN/TAP device or run up/down scripts across :code:`SIGUSR1` or ``--ping-restart`` restarts. diff --git a/src/openvpn/dco.c b/src/openvpn/dco.c index 26b8645..a61e587 100644 --- a/src/openvpn/dco.c +++ b/src/openvpn/dco.c @@ -388,6 +388,12 @@ ret); } } + + if (o->netns) + { + msg(msglevel, "Note: --netns not supported by DCO, disabling data = channel offload."); + return false; + } #endif /* if defined(_WIN32) */ #if defined(HAVE_LIBCAPNG) diff --git a/src/openvpn/networking.h b/src/openvpn/networking.h index bce0c19..526dd49 100644 --- a/src/openvpn/networking.h +++ b/src/openvpn/networking.h @@ -42,7 +42,7 @@ /* Only the iproute2 backend implements these functions, * the rest can rely on these stubs */ -#if !defined(ENABLE_IPROUTE) +#if !defined(ENABLE_IPROUTE) && !defined(ENABLE_SITNL) || defined(TARGET_A= NDROID) static inline int net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx) { @@ -63,7 +63,7 @@ { (void)ctx; } -#endif /* !defined(ENABLE_IPROUTE) */ +#endif /* !defined(ENABLE_IPROUTE) && !defined(ENABLE_SITNL) || defined(TA= RGET_ANDROID) */ #if defined(ENABLE_SITNL) || defined(ENABLE_IPROUTE) diff --git a/src/openvpn/networking_sitnl.c b/src/openvpn/networking_sitnl.c index b88f03c..d40cde8 100644 --- a/src/openvpn/networking_sitnl.c +++ b/src/openvpn/networking_sitnl.c @@ -33,14 +33,17 @@ #include "networking.h" #include "proto.h" #include "route.h" +#include "openvpn.h" #include #include #include +#include #include #include #include #include +#include #define SNDBUF_SIZE (1024 * 2) #define RCVBUF_SIZE (1024 * 4) @@ -65,6 +68,12 @@ _nest->rta_len =3D (unsigned short)((void *)sitnl_nlmsg_tail(_msg)= - (void *)_nest); \ } +#define SITNL_NETNS_RTA(r) \ + ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct rtgenmsg)= ))) + +#define NETNS_RUN_DIR "/run/netns" +#define NETNS_ORIG_DIR "/proc/self/ns/net" + /* This function was originally implemented as a macro, but compiling with * gcc and -O3 was getting confused about the math and thus raising * security warnings on subsequent memcpy() calls. @@ -121,6 +130,16 @@ char buf[256]; }; +/** + * Network namespace NSID request message + */ +struct sitnl_nsid_req +{ + struct nlmsghdr n; + struct rtgenmsg g; + char buf[256]; +}; + typedef int (*sitnl_parse_reply_cb)(struct nlmsghdr *msg, void *arg); /** @@ -165,6 +184,108 @@ return 0; } +#if defined(TARGET_LINUX) && defined(ENABLE_SITNL) + +static int netns_id_from_name_sitnl(const char *name); +static int set_netns_id_from_name_sitnl(const char *name); + +int +net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx) +{ + struct stat st; + char path[PATH_MAX]; + + ctx->netns =3D NULL; + + if (c && c->options.netns) + { + snprintf(path, sizeof(path), "%s/%s", NETNS_RUN_DIR, c->options.ne= tns); + + if (stat(path, &st) !=3D 0) + { + msg(M_WARN | M_ERRNO, "%s: Network namespace %s does not exist= ", __func__, path); + return -1; + } + + ctx->netns =3D c->options.netns; + } + + ctx->gc =3D gc_new(); + return 0; +} + +void +net_ctx_reset(openvpn_net_ctx_t *ctx) +{ + gc_reset(&ctx->gc); +} + +void +net_ctx_free(openvpn_net_ctx_t *ctx) +{ + gc_free(&ctx->gc); +} + +int +netns_switch(openvpn_net_ctx_t *ctx) +{ + char net_path[PATH_MAX]; + int orig_fd =3D -1, netns_fd; + const char *netns =3D ctx ? ctx->netns : NULL; + + if (!netns) + { + return -1; + } + + /* Save current netns descriptor */ + orig_fd =3D open(NETNS_ORIG_DIR, O_RDONLY | O_CLOEXEC); + if (orig_fd < 0) + { + msg(M_WARN | M_ERRNO, "%s: Cannot open original network namespace"= , __func__); + return -1; + } + + snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, netns); + netns_fd =3D open(net_path, O_RDONLY | O_CLOEXEC); + if (netns_fd < 0) + { + msg(M_WARN | M_ERRNO, "%s: Cannot open network namespace \"%s\"", = __func__, netns); + close(orig_fd); + return -1; + } + + if (setns(netns_fd, CLONE_NEWNET) < 0) + { + msg(M_WARN | M_ERRNO, "%s: setting the network namespace \"%s\" fa= iled", __func__, netns); + close(netns_fd); + close(orig_fd); + return -1; + } + + close(netns_fd); + return orig_fd; +} + +void +netns_restore(int orig_fd) +{ + if (orig_fd < 0) + { + return; + } + + if (setns(orig_fd, CLONE_NEWNET) < 0) + { + msg(M_WARN | M_ERRNO, "%s: Cannot restore original network namespa= ce", __func__); + close(orig_fd); + return; + } + + close(orig_fd); +} +#endif /* defined (TARGET_LINUX) && defined (ENABLE_SITNL) */ + /** * Open RTNL socket */ @@ -454,6 +575,292 @@ return ret; } +#ifdef ENABLE_SITNL +/** + * Netlink callback to extract the interface index from a RTM_NEWLINK mess= age. + * + * This function is used as a Netlink response handler. It inspects incomi= ng + * Netlink messages and, when a RTM_NEWLINK message is received, copies the + * contained `struct ifinfomsg` into the caller-provided result structure. + * + * @param n Netlink message header received from the kernel. + * @param arg Pointer to a `struct sitnl_link_req` used to store the resu= lt. + * + * @return 0 to stop Netlink message processing once the interface is foun= d, + * 1 to continue processing other messages. + */ +static int +sitnl_link_get_ifindex(struct nlmsghdr *n, void *arg) +{ + struct sitnl_link_req *res =3D arg; + struct ifinfomsg *ifi; + + if (n->nlmsg_type !=3D RTM_NEWLINK) + { + return 1; + } + + ifi =3D NLMSG_DATA(n); + res->i =3D *ifi; + + return 0; +} + +/** + * Retrieve the interface index of a network device in a specific network = namespace. + * + * Sends an RTM_GETLINK Netlink request targeting the given network namesp= ace ID + * and interface name, and extracts the resulting interface index from the= kernel + * response. + * + * On failure, this function returns 0, matching the behavior of + * `if_nametoindex()`. + * + * @param ifname Name of the network interface. + * @param netnsid Target network namespace ID. + * + * @return Interface index on success, or 0 on error. + */ +static int +get_ifindex_in_netns(const char *ifname, int netnsid) +{ + struct sitnl_link_req req; + struct sitnl_link_req res; + int ret =3D -1, ifindex =3D 0; + + CLEAR(req); + CLEAR(res); + + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i)); + req.n.nlmsg_type =3D RTM_GETLINK; + req.n.nlmsg_flags =3D NLM_F_REQUEST; + + SITNL_ADDATTR(&req.n, sizeof(req), IFLA_TARGET_NETNSID, &netnsid, size= of(int)); + SITNL_ADDATTR(&req.n, sizeof(req), IFLA_IFNAME, ifname, strlen(ifname)= + 1); + + ret =3D sitnl_send(&req.n, 0, 0, sitnl_link_get_ifindex, &res); + + if (ret < 0) + { + return ret; + } + + ifindex =3D res.i.ifi_index; + +err: + return ifindex; +} + +static int +sitnl_parse_rtattr_flags(struct rtattr *tb[], size_t max, struct rtattr *r= ta, size_t len, + unsigned short flags) +{ + unsigned short type; + + memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); + + while (RTA_OK(rta, len)) + { + type =3D rta->rta_type & ~flags; + + if ((type <=3D max) && (!tb[type])) + { + tb[type] =3D rta; + } + + rta =3D RTA_NEXT(rta, len); + } + + if (len) + { + msg(D_ROUTE, "%s: %zu bytes not parsed! (rta_len=3D%u)", __func__,= len, rta->rta_len); + } + + return 0; +} + +static int +sitnl_parse_rtattr(struct rtattr *tb[], size_t max, struct rtattr *rta, si= ze_t len) +{ + return sitnl_parse_rtattr_flags(tb, max, rta, len, 0); +} + +/** + * Netlink callback to extract and store a network namespace ID (NSID). + * + * This function processes Netlink responses to RTM_GETNSID requests. + * If a NETNSA_NSID attribute is present, its value is copied into the + * caller-provided storage. + * + * If a Netlink error message is received, the contained error code is + * returned directly. + * + * @param n Netlink message header received from the kernel. + * @param arg Pointer to an integer where the NSID will be stored. + * + * @return 0 on success, a negative error code on failure. + */ +static int +sitnl_nsid_save(struct nlmsghdr *n, void *arg) +{ + int *nsid =3D arg; + struct rtgenmsg *g =3D NLMSG_DATA(n); + struct rtattr *tb[NETNSA_MAX + 1]; + int ret; + + if (n->nlmsg_type =3D=3D NLMSG_ERROR) + { + struct nlmsgerr *err =3D NLMSG_DATA(n); + return err->error; + } + + ret =3D sitnl_parse_rtattr(tb, NETNSA_MAX, SITNL_NETNS_RTA(g), NLMSG_P= AYLOAD(n, sizeof(*g))); + + if (ret < 0) + { + return ret; + } + + if (!tb[NETNSA_NSID]) + { + return -ENOENT; + } + + memcpy(nsid, RTA_DATA(tb[NETNSA_NSID]), sizeof(*nsid)); + return 0; +} + +/** + * Retrieve the network namespace ID (NSID) associated with a namespace na= me. + * + * This function opens the network namespace identified by the given path + * and sends an RTM_GETNSID Netlink request to retrieve its assigned NSID. + * + * @param name Name of the network namespace. + * + * @return The network namespace ID on success, or a negative value on fai= lure. + */ +static int +netns_id_from_name_sitnl(const char *name) +{ + struct sitnl_nsid_req req; + + int netnsid =3D -1; + CLEAR(req); + + int netns_fd =3D open(name, O_RDONLY); + if (netns_fd < 0) + { + goto err; + } + + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.g)); + req.n.nlmsg_type =3D RTM_GETNSID; + req.n.nlmsg_flags =3D NLM_F_REQUEST; + req.g.rtgen_family =3D AF_UNSPEC; + + SITNL_ADDATTR(&req.n, sizeof(req), NETNSA_FD, &netns_fd, sizeof(int)); + + sitnl_send(&req.n, 0, 0, sitnl_nsid_save, &netnsid); + + if (netns_fd >=3D 0) + { + close(netns_fd); + } + +err: + return netnsid; +} + +/** + * Assign a network namespace ID (NSID) to a namespace. + * + * This function sends an RTM_NEWNSID Netlink request to associate a + * NSID with the network namespace identified by the given path. + * + * @param name Name of the network namespace. + * + * @return 0 on success, or a negative error code on failure. + */ +static int +set_netns_id_from_name_sitnl(const char *name) +{ + struct sitnl_nsid_req req; + + int ret =3D -1, netnsid =3D -1; + CLEAR(req); + + int netns_fd =3D open(name, O_RDONLY); + if (netns_fd < 0) + { + goto err; + } + + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.g)); + req.n.nlmsg_type =3D RTM_NEWNSID; + req.n.nlmsg_flags =3D NLM_F_REQUEST; + req.g.rtgen_family =3D AF_UNSPEC; + + SITNL_ADDATTR(&req.n, sizeof(req), NETNSA_FD, &netns_fd, sizeof(int)); + SITNL_ADDATTR(&req.n, sizeof(req), NETNSA_NSID, &netnsid, sizeof(int)); + + ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL); + + if (netns_fd >=3D 0) + { + close(netns_fd); + } + +err: + return ret; +} + +int +get_or_create_netnsid_sitnl(const char *name) +{ + char net_path[PATH_MAX]; + int netnsid; + snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, name); + + /* First get */ + netnsid =3D netns_id_from_name_sitnl(net_path); + if (netnsid >=3D 0) + { + return netnsid; + } + + /* No netnsid yet? Try to assign one */ + netnsid =3D set_netns_id_from_name_sitnl(net_path); + if (netnsid < 0) + { + return -1; + } + + /* Second get */ + netnsid =3D netns_id_from_name_sitnl(net_path); + if (netnsid >=3D 0) + { + return netnsid; + } + + return -1; +} + +int +openvpn_if_nametoindex(const char *ifname, openvpn_net_ctx_t *ctx) +{ + const char *netns =3D ctx ? ctx->netns : NULL; + int netnsid =3D get_or_create_netnsid_sitnl(netns); + + if (netnsid < 0) + { + return if_nametoindex(ifname); + } + + return get_ifindex_in_netns(ifname, netnsid); +} +#endif /* ifdef ENABLE_SITNL */ + typedef struct { size_t addr_size; @@ -624,6 +1031,54 @@ return ret; } +int +net_iface_move_netns(const char *iface, const char *netns) +{ + struct sitnl_link_req req =3D {}; + int ret =3D -1, ifindex; + char net_path[PATH_MAX]; + + ASSERT(iface); + ASSERT(netns); + + snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, netns); + + ifindex =3D if_nametoindex(iface); + if (ifindex =3D=3D 0) + { + msg(M_WARN | M_ERRNO, "%s: if_nametoindex(%s) failed", __func__, i= face); + goto err; + } + + int netns_fd =3D open(net_path, O_RDONLY); + if (netns_fd < 0) + { + msg(M_WARN | M_ERRNO, "%s: open(%s) failed: %s", __func__, netns, = strerror(errno)); + goto err; + } + + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i)); + req.n.nlmsg_type =3D RTM_SETLINK; + req.n.nlmsg_flags =3D NLM_F_REQUEST | NLM_F_ACK; + + req.i.ifi_family =3D AF_UNSPEC; + req.i.ifi_index =3D ifindex; + + SITNL_ADDATTR(&req.n, sizeof(req), IFLA_NET_NS_FD, &netns_fd, sizeof(n= etns_fd)); + + msg(D_ROUTE, "%s: move %s to netns %s", __func__, iface, netns); + + ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL); + + if (netns_fd >=3D 0) + { + close(netns_fd); + } + +err: + return ret; +} + /* used by iproute2 implementation too */ int net_route_v4_best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst, in_addr= _t *best_gw, @@ -661,7 +1116,7 @@ net_iface_up(openvpn_net_ctx_t *ctx, const char *iface, bool up) { struct sitnl_link_req req; - int ifindex; + int ifindex, orig =3D -1; CLEAR(req); @@ -671,13 +1126,15 @@ return -EINVAL; } - ifindex =3D if_nametoindex(iface); + ifindex =3D openvpn_if_nametoindex(iface, ctx); if (ifindex =3D=3D 0) { - msg(M_WARN, "%s: rtnl: cannot get ifindex for %s: %s", __func__, i= face, strerror(errno)); + msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __fun= c__, iface); return -ENOENT; } + orig =3D netns_switch(ctx); + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i)); req.n.nlmsg_flags =3D NLM_F_REQUEST; req.n.nlmsg_type =3D RTM_NEWLINK; @@ -696,24 +1153,30 @@ msg(M_INFO, "%s: set %s %s", __func__, iface, up ? "up" : "down"); - return sitnl_send(&req.n, 0, 0, NULL, NULL); + int ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL); + + netns_restore(orig); + + return ret; } int net_iface_mtu_set(openvpn_net_ctx_t *ctx, const char *iface, uint32_t mtu) { struct sitnl_link_req req; - int ifindex, ret =3D -1; + int ifindex, orig =3D -1, ret =3D -1; CLEAR(req); - ifindex =3D if_nametoindex(iface); + ifindex =3D openvpn_if_nametoindex(iface, ctx); if (ifindex =3D=3D 0) { msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __fun= c__, iface); return -1; } + orig =3D netns_switch(ctx); + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i)); req.n.nlmsg_flags =3D NLM_F_REQUEST; req.n.nlmsg_type =3D RTM_NEWLINK; @@ -726,7 +1189,9 @@ msg(M_INFO, "%s: mtu %u for %s", __func__, mtu, iface); ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL); + err: + netns_restore(orig); return ret; } @@ -734,17 +1199,19 @@ net_addr_ll_set(openvpn_net_ctx_t *ctx, const openvpn_net_iface_t *iface, = uint8_t *addr) { struct sitnl_link_req req; - int ifindex, ret =3D -1; + int ifindex, orig =3D -1, ret =3D -1; CLEAR(req); - ifindex =3D if_nametoindex(iface); + ifindex =3D openvpn_if_nametoindex(iface, ctx); if (ifindex =3D=3D 0) { msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __fun= c__, iface); return -1; } + orig =3D netns_switch(ctx); + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i)); req.n.nlmsg_flags =3D NLM_F_REQUEST; req.n.nlmsg_type =3D RTM_NEWLINK; @@ -757,17 +1224,19 @@ msg(M_INFO, "%s: lladdr " MAC_FMT " for %s", __func__, MAC_PRINT_ARG(a= ddr), iface); ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL); + err: + netns_restore(orig); return ret; } static int -sitnl_addr_set(uint16_t cmd, uint16_t flags, int ifindex, sa_family_t af_f= amily, +sitnl_addr_set(openvpn_net_ctx_t *ctx, uint16_t cmd, uint16_t flags, int i= findex, 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 =3D -EINVAL; + int ret =3D -EINVAL, orig =3D -1; CLEAR(req); @@ -819,17 +1288,22 @@ SITNL_ADDATTR(&req.n, sizeof(req), IFA_BROADCAST, &broadcast, size= ); } + orig =3D netns_switch(ctx); + ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL); + if (ret =3D=3D -EEXIST) { ret =3D 0; } + err: + netns_restore(orig); return ret; } static int -sitnl_addr_ptp_add(sa_family_t af_family, const char *iface, const inet_ad= dress_t *local, +sitnl_addr_ptp_add(openvpn_net_ctx_t *ctx, sa_family_t af_family, const ch= ar *iface, const inet_address_t *local, const inet_address_t *remote) { int ifindex; @@ -850,19 +1324,19 @@ return -EINVAL; } - ifindex =3D if_nametoindex(iface); + ifindex =3D openvpn_if_nametoindex(iface, ctx); if (ifindex =3D=3D 0) { - msg(M_WARN, "%s: cannot get ifindex for %s: %s", __func__, np(ifac= e), strerror(errno)); + msg(M_WARN | M_ERRNO, "%s: cannot get ifindex for %s", __func__, n= p(iface)); return -ENOENT; } - return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, ifind= ex, af_family, local, + return sitnl_addr_set(ctx, RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, = ifindex, af_family, local, remote, 0); } static int -sitnl_addr_ptp_del(sa_family_t af_family, const char *iface, const inet_ad= dress_t *local) +sitnl_addr_ptp_del(openvpn_net_ctx_t *ctx, sa_family_t af_family, const ch= ar *iface, const inet_address_t *local) { int ifindex; @@ -882,23 +1356,23 @@ return -EINVAL; } - ifindex =3D if_nametoindex(iface); + ifindex =3D openvpn_if_nametoindex(iface, ctx); if (ifindex =3D=3D 0) { msg(M_WARN | M_ERRNO, "%s: cannot get ifindex for %s", __func__, i= face); return -ENOENT; } - return sitnl_addr_set(RTM_DELADDR, 0, ifindex, af_family, local, NULL,= 0); + return sitnl_addr_set(ctx, RTM_DELADDR, 0, ifindex, 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(openvpn_net_ctx_t *ctx, uint16_t cmd, uint16_t flags, int = ifindex, 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 ch= ar type) { struct sitnl_route_req req; - int ret =3D -1, size; + int ret =3D -1, size, orig =3D -1; CLEAR(req); @@ -957,14 +1431,17 @@ { SITNL_ADDATTR(&req.n, sizeof(req), RTA_PRIORITY, &metric, 4); } + orig =3D netns_switch(ctx); ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL); + err: + netns_restore(orig); return ret; } static int -sitnl_addr_add(sa_family_t af_family, const char *iface, const inet_addres= s_t *addr, int prefixlen) +sitnl_addr_add(openvpn_net_ctx_t *ctx, sa_family_t af_family, const char *= iface, const inet_address_t *addr, int prefixlen) { int ifindex; @@ -984,19 +1461,19 @@ return -EINVAL; } - ifindex =3D if_nametoindex(iface); + ifindex =3D openvpn_if_nametoindex(iface, ctx); if (ifindex =3D=3D 0) { msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __fun= c__, iface); return -ENOENT; } - return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, ifind= ex, af_family, addr, NULL, + return sitnl_addr_set(ctx, RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, = ifindex, af_family, addr, NULL, prefixlen); } static int -sitnl_addr_del(sa_family_t af_family, const char *iface, inet_address_t *a= ddr, int prefixlen) +sitnl_addr_del(openvpn_net_ctx_t *ctx, sa_family_t af_family, const char *= iface, inet_address_t *addr, int prefixlen) { int ifindex; @@ -1016,14 +1493,14 @@ return -EINVAL; } - ifindex =3D if_nametoindex(iface); + ifindex =3D openvpn_if_nametoindex(iface, ctx); if (ifindex =3D=3D 0) { msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex for %s", __fun= c__, iface); return -ENOENT; } - return sitnl_addr_set(RTM_DELADDR, 0, ifindex, af_family, addr, NULL, = prefixlen); + return sitnl_addr_set(ctx, RTM_DELADDR, 0, ifindex, af_family, addr, N= ULL, prefixlen); } int @@ -1042,7 +1519,7 @@ msg(M_INFO, "%s: %s/%d dev %s", __func__, inet_ntop(AF_INET, &addr_v4.= ipv4, buf, sizeof(buf)), prefixlen, iface); - return sitnl_addr_add(AF_INET, iface, &addr_v4, prefixlen); + return sitnl_addr_add(ctx, AF_INET, iface, &addr_v4, prefixlen); } int @@ -1062,7 +1539,7 @@ msg(M_INFO, "%s: %s/%d dev %s", __func__, inet_ntop(AF_INET6, &addr_v6= .ipv6, buf, sizeof(buf)), prefixlen, iface); - return sitnl_addr_add(AF_INET6, iface, &addr_v6, prefixlen); + return sitnl_addr_add(ctx, AF_INET6, iface, &addr_v6, prefixlen); } int @@ -1081,7 +1558,7 @@ msg(M_INFO, "%s: %s dev %s", __func__, inet_ntop(AF_INET, &addr_v4.ipv= 4, buf, sizeof(buf)), iface); - return sitnl_addr_del(AF_INET, iface, &addr_v4, prefixlen); + return sitnl_addr_del(ctx, AF_INET, iface, &addr_v4, prefixlen); } int @@ -1101,7 +1578,7 @@ msg(M_INFO, "%s: %s/%d dev %s", __func__, inet_ntop(AF_INET6, &addr_v6= .ipv6, buf, sizeof(buf)), prefixlen, iface); - return sitnl_addr_del(AF_INET6, iface, &addr_v6, prefixlen); + return sitnl_addr_del(ctx, AF_INET6, iface, &addr_v6, prefixlen); } int @@ -1129,7 +1606,7 @@ inet_ntop(AF_INET, &local_v4.ipv4, buf1, sizeof(buf1)), inet_ntop(AF_INET, &remote_v4.ipv4, buf2, sizeof(buf2)), iface); - return sitnl_addr_ptp_add(AF_INET, iface, &local_v4, &remote_v4); + return sitnl_addr_ptp_add(ctx, AF_INET, iface, &local_v4, &remote_v4); } int @@ -1150,11 +1627,11 @@ msg(M_INFO, "%s: %s dev %s", __func__, inet_ntop(AF_INET, &local_v4.ip= v4, buf, sizeof(buf)), iface); - return sitnl_addr_ptp_del(AF_INET, iface, &local_v4); + return sitnl_addr_ptp_del(ctx, AF_INET, iface, &local_v4); } static int -sitnl_route_add(const char *iface, sa_family_t af_family, const void *dst,= int prefixlen, +sitnl_route_add(openvpn_net_ctx_t *ctx, const char *iface, sa_family_t af_= family, const void *dst, int prefixlen, const void *gw, uint32_t table, int metric) { enum rt_scope_t scope =3D RT_SCOPE_UNIVERSE; @@ -1162,7 +1639,7 @@ if (iface) { - ifindex =3D if_nametoindex(iface); + ifindex =3D openvpn_if_nametoindex(iface, ctx); if (ifindex =3D=3D 0) { msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifindex for %s", __= func__, iface); @@ -1180,7 +1657,7 @@ scope =3D RT_SCOPE_LINK; } - return sitnl_route_set(RTM_NEWROUTE, NLM_F_CREATE, ifindex, af_family,= dst, prefixlen, gw, + return sitnl_route_set(ctx, RTM_NEWROUTE, NLM_F_CREATE, ifindex, af_fa= mily, dst, prefixlen, gw, table, metric, scope, RTPROT_BOOT, RTN_UNICAST); } @@ -1209,7 +1686,7 @@ inet_ntop(AF_INET, &dst_be, dst_str, sizeof(dst_str)), prefixlen, inet_ntop(AF_INET, &gw_be, gw_str, sizeof(gw_str)), np(iface), tab= le, metric); - return sitnl_route_add(iface, AF_INET, dst_ptr, prefixlen, gw_ptr, tab= le, metric); + return sitnl_route_add(ctx, iface, AF_INET, dst_ptr, prefixlen, gw_ptr= , table, metric); } int @@ -1235,18 +1712,18 @@ inet_ntop(AF_INET6, &dst_v6.ipv6, dst_str, sizeof(dst_str)), prefi= xlen, inet_ntop(AF_INET6, &gw_v6.ipv6, gw_str, sizeof(gw_str)), np(iface= ), table, metric); - return sitnl_route_add(iface, AF_INET6, dst, prefixlen, gw, table, met= ric); + return sitnl_route_add(ctx, iface, AF_INET6, dst, prefixlen, gw, table= , metric); } static int -sitnl_route_del(const char *iface, sa_family_t af_family, inet_address_t *= dst, int prefixlen, +sitnl_route_del(openvpn_net_ctx_t *ctx, const char *iface, sa_family_t af_= family, inet_address_t *dst, int prefixlen, inet_address_t *gw, uint32_t table, int metric) { int ifindex =3D 0; if (iface) { - ifindex =3D if_nametoindex(iface); + ifindex =3D openvpn_if_nametoindex(iface, ctx); if (ifindex =3D=3D 0) { msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifindex for %s", __= func__, iface); @@ -1259,7 +1736,7 @@ table =3D RT_TABLE_MAIN; } - return sitnl_route_set(RTM_DELROUTE, 0, ifindex, af_family, dst, prefi= xlen, gw, table, metric, + return sitnl_route_set(ctx, RTM_DELROUTE, 0, ifindex, af_family, dst, = prefixlen, gw, table, metric, RT_SCOPE_NOWHERE, 0, 0); } @@ -1286,7 +1763,7 @@ inet_ntop(AF_INET, &dst_v4.ipv4, dst_str, sizeof(dst_str)), prefix= len, inet_ntop(AF_INET, &gw_v4.ipv4, gw_str, sizeof(gw_str)), np(iface)= , table, metric); - return sitnl_route_del(iface, AF_INET, &dst_v4, prefixlen, &gw_v4, tab= le, metric); + return sitnl_route_del(ctx, iface, AF_INET, &dst_v4, prefixlen, &gw_v4= , table, metric); } int @@ -1312,7 +1789,7 @@ inet_ntop(AF_INET6, &dst_v6.ipv6, dst_str, sizeof(dst_str)), prefi= xlen, inet_ntop(AF_INET6, &gw_v6.ipv6, gw_str, sizeof(gw_str)), np(iface= ), table, metric); - return sitnl_route_del(iface, AF_INET6, &dst_v6, prefixlen, &gw_v6, ta= ble, metric); + return sitnl_route_del(ctx, iface, AF_INET6, &dst_v6, prefixlen, &gw_v= 6, table, metric); } @@ -1356,44 +1833,11 @@ msg(D_ROUTE, "%s: add %s type %s", __func__, iface, type); ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL); + err: return ret; } -static int -sitnl_parse_rtattr_flags(struct rtattr *tb[], size_t max, struct rtattr *r= ta, size_t len, - unsigned short flags) -{ - unsigned short type; - - memset(tb, 0, sizeof(struct rtattr *) * (max + 1)); - - while (RTA_OK(rta, len)) - { - type =3D rta->rta_type & ~flags; - - if ((type <=3D max) && (!tb[type])) - { - tb[type] =3D rta; - } - - rta =3D RTA_NEXT(rta, len); - } - - if (len) - { - msg(D_ROUTE, "%s: %zu bytes not parsed! (rta_len=3D%u)", __func__,= len, rta->rta_len); - } - - return 0; -} - -static int -sitnl_parse_rtattr(struct rtattr *tb[], size_t max, struct rtattr *rta, si= ze_t len) -{ - return sitnl_parse_rtattr_flags(tb, max, rta, len, 0); -} - #define sitnl_parse_rtattr_nested(tb, max, rta) \ (sitnl_parse_rtattr_flags(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta), NL= A_F_NESTED)) @@ -1436,13 +1880,16 @@ net_iface_type(openvpn_net_ctx_t *ctx, const char *iface, char type[IFACE_= TYPE_LEN_MAX]) { struct sitnl_link_req req =3D {}; - int ifindex =3D if_nametoindex(iface); + int ifindex =3D openvpn_if_nametoindex(iface, ctx); + int orig =3D -1; if (!ifindex) { return -errno; } + orig =3D netns_switch(ctx); + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i)); req.n.nlmsg_flags =3D NLM_F_REQUEST; req.n.nlmsg_type =3D RTM_GETLINK; @@ -1455,26 +1902,32 @@ int ret =3D sitnl_send(&req.n, 0, 0, sitnl_type_save, type); if (ret < 0) { + netns_restore(orig); msg(D_ROUTE, "%s: cannot retrieve iface %s: %s (%d)", __func__, if= ace, strerror(-ret), ret); return ret; } msg(D_ROUTE, "%s: type of %s: %s", __func__, iface, type); + netns_restore(orig); + return 0; } int net_iface_del(openvpn_net_ctx_t *ctx, const char *iface) { + int ret =3D -1, orig =3D -1; struct sitnl_link_req req =3D {}; - int ifindex =3D if_nametoindex(iface); + int ifindex =3D openvpn_if_nametoindex(iface, ctx); if (!ifindex) { return -errno; } + orig =3D netns_switch(ctx); + req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i)); req.n.nlmsg_flags =3D NLM_F_REQUEST; req.n.nlmsg_type =3D RTM_DELLINK; @@ -1484,7 +1937,11 @@ msg(D_ROUTE, "%s: delete %s", __func__, iface); - return sitnl_send(&req.n, 0, 0, NULL, NULL); + ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL); + + netns_restore(orig); + + return ret; } #endif /* !ENABLE_SITNL */ diff --git a/src/openvpn/networking_sitnl.h b/src/openvpn/networking_sitnl.h index 481cc36..824f3c5 100644 --- a/src/openvpn/networking_sitnl.h +++ b/src/openvpn/networking_sitnl.h @@ -21,7 +21,115 @@ #ifndef NETWORKING_SITNL_H_ #define NETWORKING_SITNL_H_ +#include "env_set.h" + typedef char openvpn_net_iface_t; -typedef void *openvpn_net_ctx_t; + +struct openvpn_net_ctx +{ + const char *netns; + struct gc_arena gc; +}; + +typedef struct openvpn_net_ctx openvpn_net_ctx_t; + +/** + * @brief Switch the current thread to the network namespace specified + * in the given OpenVPN network context. + * + * This function changes the calling thread's network namespace to the one + * identified by ctx->netns. The current (original) network namespace file + * descriptor is saved and returned, so it can later be restored using + * netns_restore(). + * + * If @p ctx is NULL or ctx->netns is NULL, the function fails and returns= -1. + * + * The switch is performed using setns(2). This approach is required becau= se + * the netlink library does not support performing operations in an arbitr= ary + * target network namespace, except for interface creation and deletion. + * Therefore, in order to execute generic netlink operations inside a + * specific network namespace, the thread must temporarily enter + * that namespace via setns(). + * + * @param ctx Pointer to an OpenVPN network context structure containing + * the target network namespace name (ctx->netns). The namespa= ce + * is expected to exist under NETNS_RUN_DIR (e.g. /run/netns/). + * + * @return On success, returns a file descriptor referring to the original + * network namespace. This descriptor must be passed to + * netns_restore() to switch back. + * @return -1 on failure (an error is logged and no namespace switch is ke= pt). + * + * @note The returned file descriptor must be passed to netns_restore(), + * which will restore the original namespace and close it. + */ +int netns_switch(openvpn_net_ctx_t *ctx); + +/** + * @brief Restore the previously saved network namespace. + * + * This function restores the network namespace saved by netns_switch() + * using the file descriptor returned by that function. + * + * If @p orig_fd is negative, the function does nothing and returns + * immediately. + * + * The restoration is performed using setns(2), switching the calling + * thread back to its original network namespace. + * + * @param orig_fd File descriptor of the original network namespace, + * as returned by netns_switch(). + * + * @note This function always closes @p orig_fd if it is >=3D 0, + * regardless of whether setns() succeeds or fails. + * After calling this function, @p orig_fd must not be reused. + */ +void netns_restore(int orig_fd); + +/** + * Resolve a network interface name to its interface index. + * + * If a valid network namespace ID is provided, the lookup is performed in= side + * that network namespace using Netlink. Otherwise, this function falls ba= ck + * to the standard `if_nametoindex()` call in the current namespace. + * + * @param ifname Name of the network interface. + * @param ctx The network context where we retrieve + * the network namespace name. + * + * @return Interface index on success, or 0 on error. + */ +int openvpn_if_nametoindex(const char *ifname, openvpn_net_ctx_t *ctx); + +/** + * Move a network interface to a different network namespace. + * + * This function moves the specified network interface from the current + * network namespace into the target network namespace identified by name. + * The operation is performed via a RTM_SETLINK Netlink request using the + * IFLA_NET_NS_FD attribute. + * + * The target network namespace is expected to exist under NETNS_RUN_DIR + * (/var/run/netns). + * + * @param iface Name of the network interface to move. + * @param netns Name of the target network namespace. + * + * @return 0 on success, or a negative error code on failure. + */ +int net_iface_move_netns(const char *iface, const char *netns); + +/** + * Retrieve or create a network namespace ID (NSID) for a given namespace. + * + * This function first attempts to retrieve the NSID associated with the + * specified network namespace. If no NSID is currently assigned, it + * requests the kernel to create one and then retries the lookup. + * + * @param name Name of the network namespace. + * + * @return The network namespace ID on success, or -1 on failure. + */ +int get_or_create_netnsid_sitnl(const char *name); #endif /* NETWORKING_SITNL_H_ */ diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 1db781d..d29f835 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -6462,6 +6462,14 @@ VERIFY_PERMISSION(OPT_P_GENERAL); options->mtu_test =3D true; } + else if (streq(p[0], "netns") && p[1] && !p[2]) + { +#ifndef ENABLE_SITNL + msg(M_WARN, "NOTE: --netns is supported only on Linux when compile= d with SITNL"); +#endif + VERIFY_PERMISSION(OPT_P_GENERAL); + options->netns =3D p[1]; + } else if (streq(p[0], "nice") && p[1] && !p[2]) { VERIFY_PERMISSION(OPT_P_NICE); diff --git a/src/openvpn/options.h b/src/openvpn/options.h index 3d8b505..6e54d8f 100644 --- a/src/openvpn/options.h +++ b/src/openvpn/options.h @@ -315,6 +315,7 @@ struct dns_options dns_options; + const char *netns; bool remote_random; const char *ipchange; const char *dev; diff --git a/src/openvpn/tun.c b/src/openvpn/tun.c index f46802f..4adb9d2 100644 --- a/src/openvpn/tun.c +++ b/src/openvpn/tun.c @@ -2178,6 +2178,17 @@ set_nonblock(tt->fd); set_cloexec(tt->fd); tt->actual_name =3D string_alloc(ifr.ifr_name, NULL); + +#if defined(TARGET_LINUX) && defined(ENABLE_SITNL) + if (ctx->netns) + { + int ret =3D net_iface_move_netns(ifr.ifr_name, ctx->netns); + if (ret < 0) + { + msg(M_WARN | M_ERRNO, "Note: Cannot move dev %s to network= namespace %s", ifr.ifr_name, ctx->netns); + } + } +#endif /* if defined (TARGET_LINUX) && defined(ENABLE_SITNL) */ } return; } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1521?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I8b0d1cad7062856abcc40c4e16ec93b45295bbd3 Gerrit-Change-Number: 1521 Gerrit-PatchSet: 5 Gerrit-Owner: its_Giaan Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: plaisthos Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: its_Giaan Gerrit-Attention: flichtenheld --Gv3U9A3pHic= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: flichtenheld, its_Giaan, plaisthos.

its_Giaan uploaded patch set #5 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/1521?usp=3Demail">View Chan= ge

The following approvals got outdated and were removed: Code-Review-1 by flichtenheld

Add support for user defined=
 network namespace

Introduce the --netns option to allow moving the = tunnel interface
into a user-defined network namespace.
This allows t= he VPN data plane to be isolated from the main
OpenVPN process namespace= .

The current netlink library integration supports interface creatio= n
and deletion in a target namespace. However, subsequent configuration<= br>operations (e.g. address or mtu set) are executed in the caller'snamespace, as they rely on the default netlink socket context.
As a res= ult, interface-related configuration performed after creation
may be app= lied in the wrong namespace.
Introduce helper functions to temporarily s= witch the process to the
requested network namespace using setns(2), exe= cute the required
netlink operations, and then restore the original name= space.
The namespace switch is temporary and scoped to each netlink
o= peration. Once the operation completes, the original namespace
is restor= ed 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: I8b0d1cad7062856abcc40c4e16ec93b4= 5295bbd3
Signed-off-by: Gianmarco De Gregori <gianmarco@...2726...>= ;
---
M doc/man-sections/vpn-network-options.rst
M src/openvpn/dco= .c
M src/openvpn/networking.h
M src/openvpn/networking_sitnl.c
M s= rc/openvpn/networking_sitnl.h
M src/openvpn/options.c
M src/openvpn/o= ptions.h
M src/openvpn/tun.c
8 files changed, 678 insertions(+), 81 d= eletions(-)

git pull ssh://gerrit.openvpn.net:29=
418/openvpn refs/changes/21/1521/5
diff --git a/doc/man-sections/vp=
n-network-options.rst b/doc/man-sections/vpn-network-options.rst
= index 33ebedb..cd54cd4 100644
--- a/doc/man-sections/= vpn-network-options.rst
+++ b/doc/man-sections/vpn-network-= options.rst
@@ -304,6 +304,12 @@
Specify= the link layer address, more commonly known as the MAC address.
= Only applied to TAP devices.

+--netns name
+ Move the created tunnel interface into the s= pecified network
+ n= amespace. The namespace must already exist.
+
+ (Supported on Linux only, on other platforms this is a no-op).
+
--persi= st-tun
Don't close and reopen TUN/TAP device or run = up/down scripts across
:code:`SIGUSR1` or ``--ping-resta= rt`` restarts.
diff --git a/src/openvpn/dco.c b/src/openvpn= /dco.c
index 26b8645..a61e587 100644
--- a/= src/openvpn/dco.c
+++ b/src/openvpn/dco.c
@= @ -388,6 +388,12 @@
ret);
= }
}
+
+ i= f (o->netns)
+ = {
+ msg(msglev= el, "Note: --netns not supported by DCO, disabling data channel offloa= d.");
+ r= eturn false;
+ }
#endif /* if defined(_WIN32) */
#if defined(HAVE_LIBCAPNG)
diff --git a/src/openvpn= /networking.h b/src/openvpn/networking.h
index bce0c19..526= dd49 100644
--- a/src/openvpn/networking.h
= +++ b/src/openvpn/networking.h
@@ -42,7 +42,7 @@
= /* Only the iproute2 backend implements these functions,
<= span> * the rest can rely on these stubs

*/
-#if !defined(ENABLE_IPROUTE)
+#if !defined(ENABLE_IPROU= TE) && !defined(ENABLE_SITNL) || defined(TARGET_ANDROID)
= static inline int
net_ctx_init(struct context *c, o= penvpn_net_ctx_t *ctx)
{
@@ -63,7 +63,7 @@=
{
(void)ctx;
}
-#endif /* !defined(ENABLE_= IPROUTE) */
+#endif /= * !defined(ENABLE_IPROUTE) && !defined(ENABLE_SITNL) || defined(TAR= GET_ANDROID) */

#if defined(ENABLE_SITNL= ) || defined(ENABLE_IPROUTE)

diff --git a= /src/openvpn/networking_sitnl.c b/src/openvpn/networking_sitnl.c
= index b88f03c..d40cde8 100644
--- a/src/openvpn/netwo= rking_sitnl.c
+++ b/src/openvpn/networking_sitnl.c@@ -33,14 +33,17 @@
#include "networking.h&qu= ot;
#include "proto.h"
#include= "route.h"
= +#include "openvpn.h"

#include= <errno.h>
#include <string.h>
#include <unistd.h>

+#include <sched.h>
#include <sys/types.h&= gt;
#include <sys/socket.h>
#includ= e <linux/netlink.h>
#include <linux/rtnetlink.h&g= t;
+#include <linu= x/net_namespace.h>

#define SNDBUF_SIZ= E (1024 * 2)
#define RCVBUF_SIZE (1024 * 4)
@@ -65,6 +68,12 @@

_nest->rta_len =3D (unsign= ed short)((void *)sitnl_nlmsg_tail(_msg) - (void *)_nest); \
}

+#define SITNL_NETNS_RTA(r) \
+ ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(s= truct rtgenmsg))))
+<= /span>
+#define NETNS_RUN_DI= R "/run/netns"
+#define NETNS_ORIG_DIR "/proc/self/ns/net"
+
/* This function w= as originally implemented as a macro, but compiling with
= * gcc and -O3 was getting confused about the math and thus raising * security warnings on subsequent memcpy() calls.
@@ -121,6 +130,16 @@

char buf[256];
= };

= +/**
+ * Network name= space NSID request message
+ */
+struct si= tnl_nsid_req
+{
+ struct nlmsghdr n;
+ struct rtgenmsg g;=
+ char buf[256];<= /span>
+};
+
typedef int (*sitnl= _parse_reply_cb)(struct nlmsghdr *msg, void *arg);
=
/**
@@ -165,6 +184,108 @@
r= eturn 0;
}

+#if defined(TARGET_LINUX) && defined(ENAB= LE_SITNL)
++static int netns_id_from_name= _sitnl(const char *name);
+static int set_netns_id_from_name_sitnl(const char *name);
= +
+int
+net_ctx_init(struct context *c, openvpn_net_ctx_t *ctx)+{
+ struct stat st;
+ char path[PATH_MAX];
+
+ ctx->netns =3D NULL;
+
+ if (c && c->options.netns)
+ {
+ snprintf(path, sizeof(path), "%s/%s", NETNS_R= UN_DIR, c->options.netns);
+
+ i= f (stat(path, &st) !=3D 0)
+ {
= + msg(M_WARN | M_ERRNO, "%s: Network namespace %s does not = exist", __func__, path);
+ return -1;
+ }
+ ctx->= netns =3D c->options.netns;
+ }
+
+ ctx->gc =3D gc_= new();
+ return 0;=
+}
+
+void
+net_ctx_reset(openvpn_net_ctx_t *ctx)
+{
+ gc_reset(&ctx->gc);
+}
+
+void
+net_ctx_free(openvpn_net_ctx_t *ctx)<= /span>
+{
+ gc_free(&ctx->gc);+}
+
+int
+netns_= switch(openvpn_net_ctx_t *ctx)
+{
+ cha= r net_path[PATH_MAX];
+ int orig_fd =3D -1, netns_fd;
+ const char *netns =3D ctx ? ctx->netns : NULL;
+
+ if (!netns)
+ {
+ return -1;
+ }
+
+ /* Save current n= etns descriptor */
+ = orig_fd =3D open(NETNS_ORIG_DIR, O_RDONLY | O_CLOEXEC);
+ if (orig_fd < 0)
+ {

+ msg(M_WARN | M_ERRNO, "%s: Cannot= open original network namespace", __func__);
+ return -1;
+ }
+
+ = snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, net= ns);
+ netns_fd = =3D open(net_path, O_RDONLY | O_CLOEXEC);
+ if (netns_fd < 0)
+ {
+ msg(M_WARN | M_ERRNO, "%s: Cannot open network na= mespace \"%s\"", __func__, netns);
+ close(orig_fd);
+ return -1;
+ }
+
= + if (setns(netns_fd, CLONE_NEWNET) < 0)
+ {
+ msg(M_WARN | M_ERRNO, "%s: setting the network na= mespace \"%s\" failed", __func__, netns);
+ close(netns_fd);
+ close(orig_fd);
+ return -1;
= + }
+
+ close(netns_fd);
+ return orig_fd;
+}
+

+void
+netns_restore(int orig_fd)
+{
+ if (orig_fd < 0)
+ {
+ return;
+ }
+

+ if (setns(orig_fd,= CLONE_NEWNET) < 0)
+ msg= (M_WARN | M_ERRNO, "%s: Cannot restore original network namespace"= ;, __func__);
+ = close(orig_fd);
+ = return;
+ }<= /span>
+
+ close(orig_fd);
+}
+#endif /* defined (TARGET_LINUX) && defined (ENAB= LE_SITNL) */
+=
/**
* Open RTNL socket
*/
@@ -454,6 +575,292 @@
return ret;=
}

+#ifdef ENABLE_SITNL
+/**
+ * Ne= tlink callback to extract the interface index from a RTM_NEWLINK message.
+ *
+ * This function is used as a Netlink = response handler. It inspects incoming
+ * Netlink messages and, when a RTM_NEWLINK message is r= eceived, copies the
+= * contained `struct ifinfomsg` into the caller-provided result structure.<= /span>
+ *
+ * @param n Netlink message header= received from the kernel.
+ * @param arg Pointer to a `struct sitnl_link_req` used to store th= e result.
+ *<= br>+ * @return 0 to stop Netlin= k message processing once the interface is found,
+ * 1 to continue processing other mes= sages.
+ */+static int
+sitnl_link_get_ifindex(struct nlmsghdr= *n, void *arg)
+{
+ struct sitnl_link_= req *res =3D arg;
+ = struct ifinfomsg *ifi;
+
+ if (n->= nlmsg_type !=3D RTM_NEWLINK)
+ {
+ = return 1;
+ }
+
+ ifi =3D NLMSG_DATA(n);
+ res->i =3D *ifi;
+
+ return 0;
+}
+/**
+ * Retrieve the interface index = of a network device in a specific network namespace.

+ *
+ * Sends an RTM_GETLINK Netlink request targeting the give= n network namespace ID
+ * respons= e.
+ *
+ * On failure, this function retur= ns 0, matching the behavior of

+ * `if_nametoindex()`.
+ *
+ * = @param ifname Name of the network interface.
+ * @param netnsid Target network namespace ID.<= /span>
+ *
+ * @return Interface index on success= , or 0 on error.
+ */=
+static int+get_ifindex_in_netns(const c= har *ifname, int netnsid)
+{
+ struct s= itnl_link_req req;
+ = struct sitnl_link_req res;
+ int ret =3D -1, ifindex =3D 0;
+
+ CLEAR(req);
+ CLEAR(res);
+
+ req.n.nlmsg_len = =3D NLMSG_LENGTH(sizeof(req.i));
+ req.n.nlmsg_type =3D RTM_GETLINK;
+ req.n.nlmsg_flags =3D NLM_F_REQUEST;
+
+ SITNL_ADDATTR(&req.n, sizeof(req)= , IFLA_TARGET_NETNSID, &netnsid, sizeof(int));
+ SITNL_ADDATTR(&req.n, sizeof(req), I= FLA_IFNAME, ifname, strlen(ifname) + 1);
+
+ ret =3D sitnl_send(&req.n, 0, 0, sitnl_link_get_ifindex, &res= );
+
+ if (ret < 0)
+ {
+ return ret;
+ }
+
+ ifin= dex =3D res.i.ifi_index;
+
+err:+ return ifindex;+}
+
+static int
+= sitnl_parse_rtattr_flags(struct rtattr *tb[], size_t max, struct rtattr *rt= a, size_t len,
+ = unsigned short flags)
+{
+ unsigned short type;
+
+ memset= (tb, 0, sizeof(struct rtattr *) * (max + 1));
+
+ while (RTA_OK(rta, len))
+ {
+ type =3D rta->rta_type & ~flags;
+
+ if ((type <=3D max) && (!tb[type]))+ {
+ tb[type] =3D rta;+ }
+
+ rta =3D RTA_NEXT(rta, len);
+ }
+
= + if (len)
+ {<= /span>
+ msg(D_ROUTE,= "%s: %zu bytes not parsed! (rta_len=3D%u)", __func__, len, rta-&= gt;rta_len);
+ }
+
+ return 0;
+}
+
+static i= nt
+sitnl_parse_rtatt= r(struct rtattr *tb[], size_t max, struct rtattr *rta, size_t len)+{
+ return sitnl_parse_rtattr_flags(tb, max, r= ta, len, 0);
+}
+
+/**
+ * Netlink callback to extract and store a network namespace= ID (NSID).
+ *
+ * This function processe= s Netlink responses to RTM_GETNSID requests.
+ * If a NETNSA_NSID attribute is present, its valu= e is copied into the
= + * caller-provided storage.
+ *
+ * If a = Netlink error message is received, the contained error code is
+ * returned directly.

= + *
+ * @param n Netlink message header received = from the kernel.
+ * = @param arg Pointer to an integer where the NSID will be stored.
= + *
+ * @return 0 on success, a negative error code = on failure.
+ */
+static int
+sitnl_nsid_save(struct nlmsghdr *= n, void *arg)

+{
+ int *nsid =3D arg;
+ struct rtgenmsg *= g =3D NLMSG_DATA(n);
= + struct rtattr *tb[NETNSA_MAX + 1];
+ int ret;
+
+ if= (n->nlmsg_type =3D=3D NLMSG_ERROR)
+ {
+ struct nlmsgerr *err =3D NLMSG_DATA(n);
+ return err->error;
<= span style=3D"color: hsl(120, 100%, 40%);">+ }

+
+ ret =3D sitnl_parse_rtattr(tb, NETNSA_MAX, SITNL_NETNS_RTA(g= ), NLMSG_PAYLOAD(n, sizeof(*g)));
+
+ i= f (ret < 0)
+ {=
+ return ret;=
+ }
+

+ if (!tb[NETNSA_NSID])
+ {
+ return -ENOENT;
+ }
+
+ memcpy(nsi= d, RTA_DATA(tb[NETNSA_NSID]), sizeof(*nsid));
+ return 0;
+}
+
+/**
+ * Retrieve the network namespace= ID (NSID) associated with a namespace name.

+ *
+ * This function opens the network namespace identified by the give= n path
+ * and sends = an RTM_GETNSID Netlink request to retrieve its assigned NSID.
+ *

+ * @param name Name of the network namespace.
+ *
+ * @return The network namespace ID on s= uccess, or a negative value on failure.
+ */
+static int
+netns= _id_from_name_sitnl(const char *name)
+{
+= struct sitnl_nsid_req req;
+
+ int = netnsid =3D -1;
+ = CLEAR(req);
+<= br>+ int netns_fd =3D open(n= ame, O_RDONLY);
+ = if (netns_fd < 0)
= + {
+ goto = err;
+ }+
+ req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.= g));
+ req.n.nlmsg= _type =3D RTM_GETNSID;
+ req.g.rtgen_family =3D AF_UNSPEC;
+

+ SITNL_ADDATTR(&req.n, sizeof(req), NETNSA_FD= , &netns_fd, sizeof(int));
+
+ sitn= l_send(&req.n, 0, 0, sitnl_nsid_save, &netnsid);
+
+ if (netns_fd >=3D 0)
+ {
+ close(netns_fd);
+ }
+err:
<= span style=3D"color: hsl(120, 100%, 40%);">+ return netnsid;

<= span style=3D"color: hsl(120, 100%, 40%);">+}

+
+/**
+ * Assign= a network namespace ID (NSID) to a namespace.
+ *
+ * This function sends an RTM_NEWNSID Netlink request to associat= e a
+ * NSID with the= network namespace identified by the given path.
+ *
+ * @param name Name of the network namespace.
+ *
+ * @return 0 on success, or a negative error code on = failure.
+ */<= br>+static int
+set_netns_id_from_name_sitnl(const c= har *name)
+{<= br>+ struct sitnl_nsid_req r= eq;
+
+ int ret =3D -1, netnsid =3D -1;=

+ CLEAR(req);
+
+ int netns_fd =3D open(name, O_RDONLY)= ;
+ if (netns_fd &= lt; 0)
+ {<= br>+ goto err;+ }
+
+ req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.g));<= br>+ req.n.nlmsg_type =3D RT= M_NEWNSID;
+ req.n= .nlmsg_flags =3D NLM_F_REQUEST;
+ req.g.rtgen_family =3D AF_UNSPEC;
+
+ SITNL_ADDATTR(&req.n, sizeof(req), NETNSA_FD, &netns= _fd, sizeof(int));
+ = SITNL_ADDATTR(&req.n, sizeof(req), NETNSA_NSID, &netnsid, sizeof= (int));
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ ret =3D sitnl_send(&req= .n, 0, 0, NULL, NULL);

+ if (netns_fd= >=3D 0)
+ {
+ close(netns_fd= );
+ }
<= span style=3D"color: hsl(120, 100%, 40%);">+

+err:
+ return ret;
+}
+
<= span style=3D"color: hsl(120, 100%, 40%);">+int

+get_or_create_netnsid_sitnl(const char *name)
+{
+ char net_path[PATH_MAX];
= + int netnsid;
+ snprintf(net_path, sizeof(net= _path), "%s/%s", NETNS_RUN_DIR, name);

+
+ /* First get */
+ netnsid =3D netns_id_from_name_sitnl(net_path);
+ if (netnsid >=3D 0)
=
+ {
+ return netnsid;
+ }
+
+ /* No netnsid yet? Try to assign one */
+ netnsid =3D set_netns_id_from_name_sitnl(= net_path);
+ if (n= etnsid < 0)
+ {=
+ return -1;<= /span>
+ }
+

+ /* Second get */
+ netnsid =3D netns_id_from_name_sitnl(net_path);<= /span>
+ if (netnsid >= =3D 0)
+ {<= br>+ return netnsid;
+ }
+
+ return -1;
+}
+
+int
+openvpn_if_nametoindex(const char *ifna= me, openvpn_net_ctx_t *ctx)
+{
+ const = char *netns =3D ctx ? ctx->netns : NULL;
+ int netnsid =3D get_or_create_netnsid_sitnl(net= ns);
+
+ if (netnsid < 0)

= + {
+ return if_nametoindex(ifname);
+ }
+
+ return get_ifindex_in_netns(ifname, netnsid);+}
+#endif /* ifdef ENABLE_SITNL */
+

typedef struct
{
size_t addr_size;
@@= -624,6 +1031,54 @@
return ret;
}

+int
+net_iface_move_netns(= const char *iface, const char *netns)
+{
+= struct sitnl_link_req req =3D {};
+ int ret =3D -1, ifindex;
+ char net_path[PATH_MAX];
+
+ ASSERT(iface);
+ ASSERT(netns);
+
+ = snprintf(net_path, sizeof(net_path), "%s/%s", NETNS_RUN_DIR, netn= s);
+
+ ifindex =3D if_nametoindex(ifac= e);

+ if (ifindex = =3D=3D 0)
+ {
+ msg(M_WARN | M_E= RRNO, "%s: if_nametoindex(%s) failed", __func__, iface);+ goto err;
= + }
+
+ int netns_fd =3D open(net_path, O_RDONLY);
+ if (netns_fd < 0)
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ msg(M_WARN | M_ERRNO, "%s: open(= %s) failed: %s", __func__, netns, strerror(errno));
+ goto err;
+ }
+
+ req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i));
+ req.n.nlmsg_type =3D RTM_SETLINK;
+ req.n.nlmsg_flags = =3D NLM_F_REQUEST | NLM_F_ACK;
+
+ req.= i.ifi_family =3D AF_UNSPEC;
+ req.i.ifi_index =3D ifindex;
+
+ SITNL_ADDATTR(&req.n, sizeof(req), IFLA_NET_NS_FD, &netns_fd,= sizeof(netns_fd));
+=
+ msg(D_ROUTE, &q= uot;%s: move %s to netns %s", __func__, iface, netns);
+

+ ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL);<= /span>
+
+ if (netns_fd >=3D 0)
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ close(netns_fd);
+ }
+
+err:
+ return re= t;
+}
+

/* used by iprout= e2 implementation too */
int
net_route_v4= _best_gw(openvpn_net_ctx_t *ctx, const in_addr_t *dst, in_addr_t *best_gw,<= /span>
@@ -661,7 +1116,7 @@
net_iface_up(openvpn_= net_ctx_t *ctx, const char *iface, bool up)
{
struct sitnl_link_req req;

- int ifindex;
+ int ifindex, orig =3D -1;

= CLEAR(req);

@@ -671,13 +1126,15 @@
return -EINVAL;
}

- ifindex =3D = if_nametoindex(iface);
- msg(M_WARN, "%s: rtnl: cannot get ifin= dex for %s: %s", __func__, iface, strerror(errno));
+ msg(M_WARN | M_ERRNO, "%s= : rtnl: cannot get ifindex for %s", __func__, iface);
= return -ENOENT;
}
+ orig =3D netns_switch(ctx= );
+
= req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i));
= req.n.nlmsg_flags =3D NLM_F_REQUEST;
req.n.nlmsg_type = =3D RTM_NEWLINK;
@@ -696,24 +1153,30 @@

msg(M_INFO, "%s: set %s %s", __func__, iface,= up ? "up" : "down");

- return sitnl_send(&req.n, 0, = 0, NULL, NULL);

+ = int ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL);
+
+ netns_restore(orig);
+
= + return ret;
}

int<= /span>
net_iface_mtu_set(openvpn_net_ctx_t *ctx, const char *ifac= e, uint32_t mtu)
{
struct sitnl_link_= req req;
- int ifind= ex, ret =3D -1;
+ = int ifindex, orig =3D -1, ret =3D -1;

= CLEAR(req);

- ifindex =3D if_nametoindex(iface);
+ ifindex =3D openvpn_if_nametoindex(iface, = ctx);
if (ifindex =3D=3D 0)
{
msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifin= dex for %s", __func__, iface);
return -1;
}

+ orig =3D netns_switch(ctx);
+
req.n.nlmsg_len =3D NLM= SG_LENGTH(sizeof(req.i));
req.n.nlmsg_flags =3D NLM_F_= REQUEST;
req.n.nlmsg_type =3D RTM_NEWLINK;
<= span>@@ -726,7 +1189,9 @@

msg(M_INFO, "%s: mtu %u= for %s", __func__, mtu, iface);

= ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL);
+
err:
+ netns_restore(orig);
return ret;

}

@@= -734,17 +1199,19 @@
net_addr_ll_set(openvpn_net_ctx_t *ct= x, const openvpn_net_iface_t *iface, uint8_t *addr)
{
struct sitnl_link_req req;
- int ifindex, ret =3D -1;
+ int ifindex, orig =3D -1, ret =3D -1;

CLEAR(req);
- ifindex =3D if_nametoindex= (iface);
+ ifindex= =3D openvpn_if_nametoindex(iface, ctx);
if (ifindex = =3D=3D 0)
{
msg(M_WARN | M_ER= RNO, "%s: rtnl: cannot get ifindex for %s", __func__, iface);
return -1;
}

+ orig =3D netns_sw= itch(ctx);
+ req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i));
req.n.nlmsg_flags =3D NLM_F_REQUEST;

req.n.nlm= sg_type =3D RTM_NEWLINK;
@@ -757,17 +1224,19 @@
<= span> msg(M_INFO, "%s: lladdr " MAC_FMT " for %s", = __func__, MAC_PRINT_ARG(addr), iface);


= ret =3D sitnl_send(&req.n, 0, 0, NULL, NULL);
+
err:
+ netns_restore(orig);
return ret;

}

s= tatic int
-sitnl_addr_s= et(uint16_t cmd, uint16_t flags, int ifindex, sa_family_t af_family,=
+sitnl_addr_set(openvpn_net= _ctx_t *ctx, uint16_t cmd, uint16_t flags, int ifindex, sa_family_t af_fami= ly,
const inet_address_t *local, const inet= _address_t *remote, int prefixlen)
{
= struct sitnl_addr_req req;
uint32_t size;
- int ret =3D -EINVAL;
+ int ret =3D -EINVAL, orig= =3D -1;

CLEAR(req);

@@ -819,17 +1288,22 @@
SITNL_AD= DATTR(&req.n, sizeof(req), IFA_BROADCAST, &broadcast, size);=
}

+ orig =3D netns_switch(ctx);
+
ret =3D sitnl_send(&re= q.n, 0, 0, NULL, NULL);
+
if (ret =3D=3D -EEXIST)
{
ret =3D 0;
}
+
err:
+ netns_restore(orig);

= return ret;
}

static int

-sitnl_add= r_ptp_add(sa_family_t af_family, const char *iface, const inet_address_t *l= ocal,
+sitnl_addr_ptp= _add(openvpn_net_ctx_t *ctx, sa_family_t af_family, const char *iface, cons= t inet_address_t *local,
const inet_add= ress_t *remote)
{
int ifindex;=
@@ -850,19 +1324,19 @@
return -EINVAL;
}

- ifindex =3D if_nametoindex(iface);
+ ifindex =3D openvpn_if_nametoindex= (iface, ctx);
if (ifindex =3D=3D 0)
= {
- msg(M_WAR= N, "%s: cannot get ifindex for %s: %s", __func__, np(iface), stre= rror(errno));
+ = msg(M_WARN | M_ERRNO, "%s: cannot get ifindex for %s", __func__= , np(iface));
return -ENOENT;
= }

- = return sitnl_addr_set(RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, ifindex= , af_family, local,
+= return sitnl_addr_set(ctx, RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, i= findex, af_family, local,
remote= , 0);
}

static int
-sitnl_addr_ptp_del(sa_fami= ly_t af_family, const char *iface, const inet_address_t *local)
<= span style=3D"color: hsl(120, 100%, 40%);">+sitnl_addr_ptp_del(openvpn_net_= ctx_t *ctx, sa_family_t af_family, const char *iface, const inet_address_t = *local)

{
int ifindex;

@@ -882,23 +1356,23 @@
return = -EINVAL;
}

- ifindex =3D if_nametoindex(iface);+ ifindex =3D openvpn_if_n= ametoindex(iface, ctx);
if (ifindex =3D=3D 0) {
msg(M_WARN | M_ERRNO, "%s: can= not get ifindex for %s", __func__, iface);
re= turn -ENOENT;
}

- return sitnl_addr_set(RTM_DELADDR, 0, = ifindex, af_family, local, NULL, 0);
+ return sitnl_addr_set(ctx, RTM_DELADDR, 0, ifindex, 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,
+si= tnl_route_set(openvpn_net_ctx_t *ctx, uint16_t cmd, uint16_t flags, int ifi= ndex, 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_ro= ute_req req;
- int r= et =3D -1, size;
+ = int ret =3D -1, size, orig =3D -1;

= CLEAR(req);

@@ -957,14 +1431,17 @@=
{
SITNL_ADDATTR(&req.n, sizeof(= req), RTA_PRIORITY, &metric, 4);
}
+ orig =3D netns_switch(ctx);

ret =3D sitnl_send(&req.n, 0, 0, NU= LL, NULL);
+ err:
+ ne= tns_restore(orig);
return ret;
}

static int
-sitnl_addr_add(sa_family_t af_family, const char *iface= , const inet_address_t *addr, int prefixlen)
+sitnl_addr_add(openvpn_net_ctx_t *ctx, sa_family_t= af_family, const char *iface, const inet_address_t *addr, int prefixlen)
{
int ifindex;

@@ -984,19 +1461,19 @@
return -EINVAL;<= /span>
}

- ifindex =3D if_nametoindex(iface);
+ ifindex =3D openvpn_if_nametoinde= x(iface, ctx);
if (ifindex =3D=3D 0)
= {
msg(M_WARN | M_ERRNO, "%s: rtnl: cannot= get ifindex for %s", __func__, iface);
retur= n -ENOENT;
}

- return sitnl_addr_set(RTM_NEWADDR, NLM_= F_CREATE | NLM_F_REPLACE, ifindex, af_family, addr, NULL,
+ return sitnl_addr_set(ctx, RTM_NE= WADDR, NLM_F_CREATE | NLM_F_REPLACE, ifindex, af_family, addr, NULL,=
prefixlen);
}<= br>
static int
-sitnl_addr_del(sa_family_t af_family, const char *iface, i= net_address_t *addr, int prefixlen)
+sitnl_addr_del(openvpn_net_ctx_t *ctx, sa_family_t af_famil= y, const char *iface, inet_address_t *addr, int prefixlen)
= {
int ifindex;

@@ -= 1016,14 +1493,14 @@
return -EINVAL;
}


- ifindex =3D if_nametoindex(iface);
+ ifindex =3D openvpn_if_nametoindex(iface, ctx);=
if (ifindex =3D=3D 0)
{ msg(M_WARN | M_ERRNO, "%s: rtnl: cannot get ifindex f= or %s", __func__, iface);
return -ENOENT;
}

- return sitnl_addr_set(RTM_DELADDR, 0, ifindex, af_famil= y, addr, NULL, prefixlen);
+ return sitnl_addr_set(ctx, RTM_DELADDR, 0, ifindex, af_family, a= ddr, NULL, prefixlen);
}

int

@@ -1042,7 +1519,7 @@
msg(M_INFO= , "%s: %s/%d dev %s", __func__, inet_ntop(AF_INET, &addr_v4.i= pv4, buf, sizeof(buf)),
prefixlen, iface);<= br>
- return = sitnl_addr_add(AF_INET, iface, &addr_v4, prefixlen);
+ return sitnl_addr_add(ctx, AF_INET= , iface, &addr_v4, prefixlen);
}

int
@@ -1062,7 +1539,7 @@
= msg(M_INFO, "%s: %s/%d dev %s", __func__, inet_ntop(AF_INET6, &= amp;addr_v6.ipv6, buf, sizeof(buf)),
prefixlen, if= ace);

- return sitnl_addr_add(AF_INET6, iface, &addr_v6, prefixlen);
+ return sitnl_addr_ad= d(ctx, AF_INET6, iface, &addr_v6, prefixlen);
}=

int
@@ -1081,7 +1558,7 @@
msg(M_INFO, "%s: %s dev %s", __func__, inet_ntop= (AF_INET, &addr_v4.ipv4, buf, sizeof(buf)),
if= ace);

- return sitnl_addr_del(AF_INET, iface, &addr_v4, prefixlen);
+ return sitnl_addr_del= (ctx, AF_INET, iface, &addr_v4, prefixlen);
}
int
@@ -1101,7 +1578,7 @@=
msg(M_INFO, "%s: %s/%d dev %s", __func__, inet_nto= p(AF_INET6, &addr_v6.ipv6, buf, sizeof(buf)),
= prefixlen, iface);

- return sitnl_addr_del(AF_INET6, iface, &addr_v6, pre= fixlen);
+ return = sitnl_addr_del(ctx, AF_INET6, iface, &addr_v6, prefixlen);
}


int
@@ -1129,7 +1= 606,7 @@
inet_ntop(AF_INET, &local_v4.ipv4, bu= f1, sizeof(buf1)),
inet_ntop(AF_INET, &remote_= v4.ipv4, buf2, sizeof(buf2)), iface);

- return sitnl_addr_ptp_add(AF_INET, if= ace, &local_v4, &remote_v4);
+ return sitnl_addr_ptp_add(ctx, AF_INET, iface, &lo= cal_v4, &remote_v4);
}

int

@@ -1150,11 +1627,11 @@
msg(M_= INFO, "%s: %s dev %s", __func__, inet_ntop(AF_INET, &local_v4= .ipv4, buf, sizeof(buf)),
iface);
=
- return sitnl_add= r_ptp_del(AF_INET, iface, &local_v4);
+ return sitnl_addr_ptp_del(ctx, AF_INET, iface, &a= mp;local_v4);
}

static = int
-sitnl_route_add(co= nst char *iface, sa_family_t af_family, const void *dst, int prefixlen,
+sitnl_route_add(openvpn= _net_ctx_t *ctx, const char *iface, sa_family_t af_family, const void *dst,= int prefixlen,
const void *gw, uint32_t t= able, int metric)
{
enum rt_scope_t s= cope =3D RT_SCOPE_UNIVERSE;
@@ -1162,7 +1639,7 @@
if (iface)
{
<= span style=3D"color: hsl(0, 100%, 40%);">- ifindex =3D if_nametoinde= x(iface);

+ if= index =3D openvpn_if_nametoindex(iface, ctx);
if (= ifindex =3D=3D 0)
{
m= sg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifindex for %s", __= func__, iface);
@@ -1180,7 +1657,7 @@
= scope =3D RT_SCOPE_LINK;
}

- return sitnl_route_set(= RTM_NEWROUTE, NLM_F_CREATE, ifindex, af_family, dst, prefixlen, gw,<= br>+ return sitnl_route_set(= ctx, RTM_NEWROUTE, NLM_F_CREATE, ifindex, af_family, dst, prefixlen, gw,
table, metric, scope, RTPROT_BOOT= , RTN_UNICAST);
}

@@ -12= 09,7 +1686,7 @@
inet_ntop(AF_INET, &dst_be, ds= t_str, sizeof(dst_str)), prefixlen,
inet_ntop(AF_I= NET, &gw_be, gw_str, sizeof(gw_str)), np(iface), table, metric);=

- return= sitnl_route_add(iface, AF_INET, dst_ptr, prefixlen, gw_ptr, table, metric)= ;
+ return sitnl_r= oute_add(ctx, iface, AF_INET, dst_ptr, prefixlen, gw_ptr, table, metric);
}

int
@@= -1235,18 +1712,18 @@
inet_ntop(AF_INET6, &dst= _v6.ipv6, dst_str, sizeof(dst_str)), prefixlen,
in= et_ntop(AF_INET6, &gw_v6.ipv6, gw_str, sizeof(gw_str)), np(iface), tabl= e, metric);

- return sitnl_route_add(iface, AF_INET6, dst, prefixlen, gw, tab= le, metric);
+ ret= urn sitnl_route_add(ctx, iface, AF_INET6, dst, prefixlen, gw, table, metric= );
}

static int<= br>-sitnl_route_del(const char *i= face, sa_family_t af_family, inet_address_t *dst, int prefixlen,
= +sitnl_route_del(openvpn_net_ct= x_t *ctx, const char *iface, sa_family_t af_family, inet_address_t *dst, in= t prefixlen,
inet_address_t *gw, uint32_t = table, int metric)
{
int ifindex =3D = 0;

if (iface)
{=
- ifindex =3D i= f_nametoindex(iface);
+ ifindex =3D openvpn_if_nametoindex(iface, ctx);
= if (ifindex =3D=3D 0)
{
= msg(M_WARN | M_ERRNO, "%s: rtnl: can't get ifindex for = %s", __func__, iface);
@@ -1259,7 +1736,7 @@ table =3D RT_TABLE_MAIN;
}
<= span>

- return sitn= l_route_set(RTM_DELROUTE, 0, ifindex, af_family, dst, prefixlen, gw, table,= metric,
+ return = sitnl_route_set(ctx, RTM_DELROUTE, 0, ifindex, af_family, dst, prefixlen, g= w, table, metric,
RT_SCOPE_NOWH= ERE, 0, 0);
}

@@ -1286,7= +1763,7 @@
inet_ntop(AF_INET, &dst_v4.ipv4, d= st_str, sizeof(dst_str)), prefixlen,
inet_ntop(AF_= INET, &gw_v4.ipv4, gw_str, sizeof(gw_str)), np(iface), table, metric);<= /span>

- = return sitnl_route_del(iface, AF_INET, &dst_v4, prefixlen, &gw_v4, = table, metric);
+ = return sitnl_route_del(ctx, iface, AF_INET, &dst_v4, prefixlen, &gw= _v4, table, metric);
}

= int
@@ -1312,7 +1789,7 @@
inet_nto= p(AF_INET6, &dst_v6.ipv6, dst_str, sizeof(dst_str)), prefixlen,<= br> inet_ntop(AF_INET6, &gw_v6.ipv6, gw_str, sizeof(gw_st= r)), np(iface), table, metric);

- return sitnl_route_del(iface, AF_INET6, &am= p;dst_v6, prefixlen, &gw_v6, table, metric);
+ return sitnl_route_del(ctx, iface, AF_INET= 6, &dst_v6, prefixlen, &gw_v6, table, metric);
}


@@ -1356,44 +1833,11 @@<= /span>
msg(D_ROUTE, "%s: add %s type %s", __func__,= iface, type);

ret =3D sitnl_send(&a= mp;req.n, 0, 0, NULL, NULL);
+
err:
return ret;
= }

-static int
-sitnl_= parse_rtattr_flags(struct rtattr *tb[], size_t max, struct rtattr *rta, siz= e_t len,
- = unsigned short flags)
-{
- unsi= gned short type;
-
- memset(tb, 0, sizeof(s= truct rtattr *) * (max + 1));
-
- while (RT= A_OK(rta, len))
- {<= /span>
- type =3D rta-&= gt;rta_type & ~flags;
-
- if ((type= <=3D max) && (!tb[type]))
- {
- tb[type] =3D rta;
- }
-=
- rta =3D RTA_N= EXT(rta, len);
- }
-
- if (len)
- {
- msg(D_ROUTE, "%s: %zu bytes not parsed! (rta_len=3D%u)&= quot;, __func__, len, rta->rta_len);
- }
-
- return 0;
-}
-
-static int
-sitn= l_parse_rtattr(struct rtattr *tb[], size_t max, struct rtattr *rta, size_t = len)
-{
- return sitnl_parse_rtattr_flags(t= b, max, rta, len, 0);

-= }
-
#d= efine sitnl_parse_rtattr_nested(tb, max, rta) \
(sitnl= _parse_rtattr_flags(tb, max, RTA_DATA(rta), RTA_PAYLOAD(rta), NLA_F_NESTED)= )

@@ -1436,13 +1880,16 @@
net_iface_type(openvpn_net_ctx_t *ctx, const char *iface, char type[IFACE= _TYPE_LEN_MAX])

{
struct sitnl_link_r= eq req =3D {};
- int= ifindex =3D if_nametoindex(iface);
+ int ifindex =3D openvpn_if_nametoindex(iface, ctx);
+ int orig =3D -1;

if (!ifindex)
{
return -errno;
}

+ orig =3D ne= tns_switch(ctx);
+
req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i));<= br> req.n.nlmsg_flags =3D NLM_F_REQUEST;
req= .n.nlmsg_type =3D RTM_GETLINK;
@@ -1455,26 +1902,32 @@
int ret =3D sitnl_send(&req.n, 0, 0, sitnl_type_save, = type);
if (ret < 0)
{+ netns_restore(orig);=
msg(D_ROUTE, "%s: cannot retrieve iface %s: = %s (%d)", __func__, iface, strerror(-ret), ret);
= return ret;
}

= msg(D_ROUTE, "%s: type of %s: %s", __func__, iface, type);

+ n= etns_restore(orig);
+=
return 0;
}
=
int
net_iface_del(openvpn_net_ctx_t *ctx, const= char *iface)
{
+ int ret =3D -1, orig =3D -1;
struct s= itnl_link_req req =3D {};
- int ifindex =3D if_nametoindex(iface);
+ int ifindex =3D openvpn_if_nametoindex(iface= , ctx);

if (!ifindex)
{

return -errno;
}

+ o= rig =3D netns_switch(ctx);
+
req.n.nlmsg_len =3D NLMSG_LENGTH(sizeof(req.i)= );
req.n.nlmsg_flags =3D NLM_F_REQUEST;
req.n.nlmsg_type =3D RTM_DELLINK;

@@ -1484,7 +1937,1= 1 @@

msg(D_ROUTE, "%s: delete %= s", __func__, iface);

- return sitnl_send(&req.n, 0, 0, NULL, NULL);=
+ ret =3D sitnl_s= end(&req.n, 0, 0, NULL, NULL);
+
+ = netns_restore(orig);
= +
+ return ret;
}

#endif /* !ENABLE_SITNL= */
diff --git a/src/openvpn/networking_sitnl.h b/src/openv= pn/networking_sitnl.h
index 481cc36..824f3c5 100644<= br>--- a/src/openvpn/networking_sitnl.h
+++ b/src/ope= nvpn/networking_sitnl.h
@@ -21,7 +21,115 @@
#ifndef NETWORKING_SITNL_H_

#define NETWORKING_SITNL_H_<= /span>

+#i= nclude "env_set.h"
+
typedef char openvpn_net_iface_t;
-typedef void *openvpn_net_ctx_t;
+
+struct openvpn_net_ctx
+{
+ const char *netns;
+ struct gc_arena gc;
+};
+
+typedef st= ruct openvpn_net_ctx openvpn_net_ctx_t;
+
= +/**
+ * @brief Switc= h the current thread to the network namespace specified
+ * in the given OpenVPN network = context.
+ *+ * This function changes the= calling thread's network namespace to the one
+ * identified by ctx->netns. The current = (original) network namespace file
+ * descriptor is saved and returned, so it can later be resto= red using
+ * netns_r= estore().
+ *<= br>+ * If @p ctx is NULL or ctx= ->netns is NULL, the function fails and returns -1.
+ *
+ * The switch is performed using setns(2). This approach = is required because
+= * the netlink library does not support performing operations in an arbitra= ry
+ * target network= namespace, except for interface creation and deletion.
+ * Therefore, in order to execute gener= ic netlink operations inside a
+ * specific network namespace, the thread must temporarily enter=
+ * that namespace v= ia setns().
+ *
+ * @param ctx Pointer to= an OpenVPN network context structure containing
+ * the target network namespace na= me (ctx->netns). The namespace
+ * is expected to exist under NETNS_RUN_DIR (e.g.= /run/netns/).
+ *
+ * @return On success,= returns a file descriptor referring to the original
+ * network namespace. This descri= ptor must be passed to
+ * @return -1 on failure (an error is logged and = no namespace switch is kept).
+ *
+ * @not= e The returned file descriptor must be passed to netns_restore(),+ * which will restore t= he original namespace and close it.
+ */
+= int netns_switch(openvpn_net_ctx_t *ctx);
+
+ * @brief Res= tore the previously saved network namespace.
+ *
+ * This function restores the network namespace saved by netns_swit= ch()
+ * using the fi= le descriptor returned by that function.
+ *
+ * If @p orig_fd is negative, the function does nothing and returns
+ * immediately.<= br>+ *
+ * The restoration is performed using setns(= 2), switching the calling
+ * thread back to its original network namespace.
+ *
+ * @param orig_fd File descriptor of the original netwo= rk namespace,
+ * = as returned by netns_switch().
+ *
+ * @note This function always closes @p orig_fd if it is >=3D 0,<= /span>
+ * regardless = of whether setns() succeeds or fails.
+ * After calling this function, @p orig_fd must not= be reused.
+ */

+void netns_restore(int o= rig_fd);
+
= +/**
+ * Resolve a network interface name to its int= erface index.
+ *
+ * If a valid network n= amespace ID is provided, the lookup is performed inside
+ * that network namespace using Netlink= . Otherwise, this function falls back
+ * to the standard `if_nametoindex()` call in the current= namespace.
+ *
+ * @param ifname Name o= f the network interface.
+ * @param ctx The network context where we retrieve
+ * the network na= mespace name.

+ *
+ * @return Interface in= dex on success, or 0 on error.
+ */
+int o= penvpn_if_nametoindex(const char *ifname, openvpn_net_ctx_t *ctx);+
+/**
+ * Move a network interface to a different network namespace.
+ *
+ * This function moves the specified n= etwork interface from the current
+ * network namespace into the target network namespace identi= fied by name.
+ * The= operation is performed via a RTM_SETLINK Netlink request using the<= br>+ * IFLA_NET_NS_FD attribute= .
+ *
+ * The target network namespace is = expected to exist under NETNS_RUN_DIR

+ * (/var/run/netns).
+ *
+ * @param iface Name of the network interface to move.
+ * @param netns Name of the target n= etwork namespace.
+ *=
+ * @return 0 on suc= cess, or a negative error code on failure.
+ */
+int net_iface_move_netns(const char *iface, const char *netns);
+
+/**
+ * Retrieve or create a network namespace ID (NSID) for a= given namespace.
+ *=
+ * This function fi= rst attempts to retrieve the NSID associated with the
+ * specified network namespace. If no NSI= D is currently assigned, it
+ * requests the kernel to create one and then retries the lookup.
+ *
+ * @param name Name of the network na= mespace.
+ *+ * @return The network names= pace ID on success, or -1 on failure.
+ */
+int get_or_create_netnsid_sitnl(const char *name);

#endif /* NETWORKING_SITNL_H_ */
diff --git a/= src/openvpn/options.c b/src/openvpn/options.c
index 1db781d= ..d29f835 100644
--- a/src/openvpn/options.c
+++ b/src/openvpn/options.c

@@ -6462,6 +6462,14 @@=
VERIFY_PERMISSION(OPT_P_GENERAL);
= options->mtu_test =3D true;
}
+ else if (streq(p[0], "netns&q= uot;) && p[1] && !p[2])
+ {
+#ifndef ENABLE_SITNL
+ msg(M_WARN, "NOTE: --netns is supported only on Linux w= hen compiled with SITNL");
+#endif
+ = VERIFY_PERMISSION(OPT_P_GENERAL);
+ options->netns =3D p[1];
+ }
else if (st= req(p[0], "nice") && p[1] && !p[2])
{

VERIFY_PERMISSION(OPT_P_NICE);diff --git a/src/openvpn/options.h b/src/openvpn/options.hindex 3d8b505..6e54d8f 100644
--- a/src/openvpn/opti= ons.h
+++ b/src/openvpn/options.h
@@ -315,6= +315,7 @@

struct dns_options dns_op= tions;

+ const char *netns;
bool remote_random;=
const char *ipchange;
const char *dev;<= /span>
diff --git a/src/openvpn/tun.c b/src/openvpn/tun.cindex f46802f..4adb9d2 100644
--- a/src/openvpn/tun= .c
+++ b/src/openvpn/tun.c
@@ -2178,6 +2178= ,17 @@
set_nonblock(tt->fd);
= set_cloexec(tt->fd);
tt->actual_name = =3D string_alloc(ifr.ifr_name, NULL);
+
+#= if defined(TARGET_LINUX) && defined(ENABLE_SITNL)
+ if (ctx->netns)
= + {
+ int ret =3D net_iface_move_n= etns(ifr.ifr_name, ctx->netns);
+ if (ret < 0)
+ {
+ msg(M_WARN | M_ERRNO, "Note: Cannot m= ove dev %s to network namespace %s", ifr.ifr_name, ctx->netns);
+ }+ }
+#endif /* if defined (TARGET_LINUX) &am= p;& defined(ENABLE_SITNL) */
}
= return;
}

To view, v= isit cha= nge 1521. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I8b0d1cad7062856abcc40c4e16e= c93b45295bbd3
Gerrit-Change-Number: 1521
Gerrit-PatchSet: 5
Gerrit-Owner: its_Giaan <gianmarco@...2786...= ..>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: plaisthos <arne-openvpn@...1186...= .1227...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: its_Giaan <gianmarco@...1665...= 726...>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
= --Gv3U9A3pHic=--