Skip to contentSkip to conversation

nfct: add network namespace support

PATCH In review Corubba Smith 2 participants 3 messages Mar 8, 2025 – Mar 12, 2025 netfilter

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfct: add network namespace support

Add a new option which allows opening the netlink socket in a different network namespace. This way you can run ulogd in one (management) network namespace which is able to talk with your export target (e.g. database or IPFIX collector), and import flows from multiple (customer) network namespaces.

The config option is a path and not just a name so you can also use anonymous network namespaces (e.g. /proc/20/ns/net)

---
Since the required setns() syscall is part of the libc GNU extension, _GNU_SOURCE needs to be defined before importing the headers. Even after reading about it, I am still unsure about the ramifications of that with regard to compatibility. That is why I maybe went a bit overboard with the autoconf feature toggle, autodetection and conditional compilation. Any feedback is appreciated.

Also tested to compile and run against musl.

This commit only implements it for NFCT. I wanted to gather some feedback before also implementing it for the other netlink-based plugins.

 configure.ac                    | 39 ++++++++++++++++++
 input/flow/ulogd_inpflow_NFCT.c | 72 ++++++++++++++++++++++++++++++++-
 2 files changed, 109 insertions(+), 2 deletions(-)
configure.ac +39 -0
index 3c9249e..70aa1d7 100644
@@ -243,6 +243,44 @@ AS_IF([test "x$enable_json" != "xno"],
 AS_IF([test "x$libjansson_LIBS" != "x"], [enable_json=yes], [enable_json=no])
 AM_CONDITIONAL([HAVE_JANSSON], [test "x$libjansson_LIBS" != "x"])
 
+AC_ARG_ENABLE([netns],
+              [AS_HELP_STRING([--enable-netns], [Enable network namespace support for netlink-based plugins [default=test]])])
+AS_IF([test "x$enable_netns" != "xno"], [
+  AC_CHECK_DECLS([setns, CLONE_NEWNET], [
+    enable_netns=yes
+  ], [], [[#include <fcntl.h>], [#include <sched.h>]])
+])
+AS_IF([test "x$enable_netns" != "xno"], [
+  AC_MSG_CHECKING([whether setns and CLONE_NEWNET are declared using _GNU_SOURCE])
+  AC_LINK_IFELSE([
+    AC_LANG_SOURCE([
+      #define _GNU_SOURCE
+      #include <fcntl.h>
+      #include <sched.h>
+      int main() {
+        setns(0, CLONE_NEWNET);
+        return 0;
+      }
+    ])
+  ],[
+    AC_MSG_RESULT([yes])
+    enable_netns=yes
+    AC_DEFINE([HAVE_DECL_SETNS], [1], [])
+    AC_DEFINE([HAVE_DECL_CLONE_NEWNET], [1], [])
+    AC_DEFINE([NETNS_REQUIRES_GNUSOURCE], [1], [Define if network namespace functionality requires _GNU_SOURCE])
+  ],[
+    AC_MSG_RESULT([no])
+    AS_IF([test "x$enable_netns" = "xyes"], [
+      AC_MSG_ERROR([network namespace support enabled, but required symbols not available])
+    ], [
+      enable_netns=no
+    ])
+  ])
+])
+AS_IF([test "x$enable_netns" = "xyes"], [
+  AC_DEFINE([NETNS_SUPPORT], [1], [Define if network namespace support is enabled])
+], [])
+
 AC_ARG_WITH([ulogd2libdir],
             [AS_HELP_STRING([--with-ulogd2libdir=PATH], [Default directory to load ulogd2 plugin from [[LIBDIR/ulogd]]])],
             [ulogd2libdir="$withval"],
@@ -293,6 +331,7 @@ EXPAND_VARIABLE(ulogd2libdir, e_ulogd2libdir)
 echo "
 Ulogd configuration:
   Default plugins directory:		${e_ulogd2libdir}
+  Network namespace support:		${enable_netns}
   Input plugins:
     NFLOG plugin:			${enable_nflog}
     NFCT plugin:			${enable_nfct}
input/flow/ulogd_inpflow_NFCT.c +70 -2

128 lines not on this page · raw message

--
2.48.1

maintainer
#1 lore raw
Quoted 5 lines · Corubba Smith

Add a new option which allows opening the netlink socket in a different network namespace. This way you can run ulogd in one (management) network namespace which is able to talk with your export target (e.g. database or IPFIX collector), and import flows from multiple (customer) network namespaces.

Makes sense to me.

Quoted 3 lines

This commit only implements it for NFCT. I wanted to gather some feedback before also implementing it for the other netlink-based plugins.

Does it make sense to have this configured on a per-plugin basis?

input/flow/ulogd_inpflow_NFCT.c static int constructor_nfct_events(struct ulogd_pluginstance *upi) · line 1313

Quoted 12 lines
   Input plugins:
     NFLOG plugin:			${enable_nflog}
     NFCT plugin:			${enable_nfct}
input/flow/ulogd_inpflow_NFCT.c +8 -0
+#ifdef NETNS_SUPPORT
+	if (strlen(target_netns_path) > 0) {
+		errno = 0;
+		original_netns_fd = open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC);
+		if (original_netns_fd < 0) {
+			ulogd_log(ULOGD_FATAL, "error opening original network namespace: %s\n", strerror(errno));
+			goto err_ons;
+		}

I think that in order to not have copypastry in all relevant plugins it would be better to turn code in the NETNS_SUPPORT ifdefs section into library helpers.

The helpers would always exist; in case ulogd2 is built without support they would raise an error.

That would also keep the ifdef out of plugin code.

#2 lore raw
Quoted 7 lines · Florian Westphal
Quoted 5 lines · Corubba Smith

Add a new option which allows opening the netlink socket in a different network namespace. This way you can run ulogd in one (management) network namespace which is able to talk with your export target (e.g. database or IPFIX collector), and import flows from multiple (customer) network namespaces.

Quoted 7 lines

Makes sense to me.

Quoted 3 lines

This commit only implements it for NFCT. I wanted to gather some feedback before also implementing it for the other netlink-based plugins.

Does it make sense to have this configured on a per-plugin basis?

I honestly don't see the usecase for it. Enabling namespace support depends on whether the used libc supports the required syscall, which is the same for all plugins. But even if namespace support is compiled in globally, you still need to activate it per plugin instance by setting the namespace path config option. That option is not mandatory, and if not set the plugin will behave the same as before, opening the netlink socket in the current network namespace. So compiling in namespace support alone does not change the runtime behaviour (or actually use the syscall), it only does when you also explicitly use the namespace path option on a plugin instance.

Quoted 18 lines

input/flow/ulogd_inpflow_NFCT.c static int constructor_nfct_events(struct ulogd_pluginstance *upi) · line 1313

Quoted 13 lines
   Input plugins:
     NFLOG plugin:			${enable_nflog}
     NFCT plugin:			${enable_nfct}
input/flow/ulogd_inpflow_NFCT.c +8 -0
+#ifdef NETNS_SUPPORT
+	if (strlen(target_netns_path) > 0) {
+		errno = 0;
+		original_netns_fd = open("/proc/self/ns/net", O_RDONLY | O_CLOEXEC);
+		if (original_netns_fd < 0) {
+			ulogd_log(ULOGD_FATAL, "error opening original network namespace: %s\n", strerror(errno));
+			goto err_ons;
+		}

I think that in order to not have copypastry in all relevant plugins it would be better to turn code in the NETNS_SUPPORT ifdefs section into library helpers.

Quoted 4 lines

The helpers would always exist; in case ulogd2 is built without support they would raise an error.

That would also keep the ifdef out of plugin code.

Very good point, will do that. Thanks!

--
Corubba