Skip to contentSkip to conversation

ulogd: add linux namespace helper

PATCH Superseded Corubba Smith 2 participants 8 messages Mar 25, 2025 – Apr 11, 2025 netfilter

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/4 ulogd: add linux namespace helper

The new namespace helper provides an internal stable interface for plugins to use for switching various linux namespaces. Currently only network namespaces are supported/implemented, but can easily be extended if needed. autoconf will enable it automatically if the required symbols are available. If ulogd is compiled without namespace support, the functions will simply return an error, there is no need for conditional compilation or special handling in plugin code.

---
Changes in v2:
- Split the single patch into multiple
- Moved the namespace code to a dedicated helper (Florian Westphal)
- Implemented network namespace support for NFCT polling mode, NFLOG
and NFACCT plugins. I skipped ULOG because it's removed from the
kernel since 7200135bc1e6 ("netfilter: kill ulog targets") aka v3.17
- Link to v1: https://lore.kernel.org/netfilter-devel/[email protected]/

 configure.ac              |  22 ++++
 include/ulogd/Makefile.am |   4 +-
 include/ulogd/namespace.h |   8 ++
 src/Makefile.am           |   3 +-
 src/namespace.c           | 237 ++++++++++++++++++++++++++++++++++++++
 5 files changed, 272 insertions(+), 2 deletions(-)
 create mode 100644 include/ulogd/namespace.h
 create mode 100644 src/namespace.c
configure.ac +22 -0
index 3c9249e..2b193d8 100644
@@ -243,6 +243,27 @@ 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([namespace],
+              [AS_HELP_STRING([--enable-namespace], [Enable linux namespace functionality in plugins supporting it [default=test]])])
+AS_IF([test "x$enable_namespace" != "xno"], [
+  AC_CHECK_DECLS([setns, CLONE_NEWNET], [
+    enable_namespace=yes
+  ], [
+    AS_IF([test "x$enable_namespace" = "xyes"], [
+      AC_MSG_ERROR([linux namespace support enabled, but required symbols not available])
+    ], [
+      enable_namespace=no
+    ])
+  ], [[
+    #define _GNU_SOURCE 1
+    #include <fcntl.h>
+    #include <sched.h>
+  ]])
+])
+AS_IF([test "x$enable_namespace" = "xyes"], [
+  AC_DEFINE([ENABLE_NAMESPACE], [1], [Define to 1 if you want linux namespace support.])
+])
+
 AC_ARG_WITH([ulogd2libdir],
             [AS_HELP_STRING([--with-ulogd2libdir=PATH], [Default directory to load ulogd2 plugin from [[LIBDIR/ulogd]]])],
             [ulogd2libdir="$withval"],
@@ -293,6 +314,7 @@ EXPAND_VARIABLE(ulogd2libdir, e_ulogd2libdir)
 echo "
 Ulogd configuration:
   Default plugins directory:		${e_ulogd2libdir}
+  Linux namespace support:		${enable_namespace}
   Input plugins:
     NFLOG plugin:			${enable_nflog}
     NFCT plugin:			${enable_nfct}
include/ulogd/Makefile.am +3 -1
index e4b41c4..65d74ba 100644
@@ -1 +1,3 @@
-noinst_HEADERS = conffile.h db.h ipfix_protocol.h linuxlist.h ulogd.h printpkt.h printflow.h common.h linux_rbtree.h timer.h slist.h hash.h jhash.h addr.h
+noinst_HEADERS = addr.h common.h conffile.h db.h hash.h ipfix_protocol.h \
+                 jhash.h linux_rbtree.h linuxlist.h namespace.h printflow.h \
+                 printpkt.h slist.h timer.h ulogd.h
include/ulogd/namespace.h new file +8 -0
new file mode 100644
index 0000000..48e2e9a
@@ -0,0 +1,8 @@
+#ifndef _NAMESPACE_H_
+#define _NAMESPACE_H_
+
+int join_netns_fd(const int target_netns_fd, int *const source_netns_fd_ptr);
+int join_netns_path(const char *const target_netns_path,
+                    int *const source_netns_fd_ptr);
+
+#endif
src/Makefile.am +2 -1
index 7a12a72..4004c2b 100644
@@ -6,6 +6,7 @@ AM_CPPFLAGS += -DULOGD_CONFIGFILE='"$(sysconfdir)/ulogd.conf"' \
 
 sbin_PROGRAMS = ulogd
 
-ulogd_SOURCES = ulogd.c select.c timer.c rbtree.c conffile.c hash.c addr.c
+ulogd_SOURCES = ulogd.c select.c timer.c rbtree.c conffile.c hash.c \
+                addr.c namespace.c
 ulogd_LDADD   = ${libdl_LIBS} ${libpthread_LIBS}
 ulogd_LDFLAGS = -export-dynamic
src/namespace.c new file +237 -0

237 lines not on this page · raw message

--
2.49.0

#1 lore raw

PATCH v2 2/4 nfct: add network namespace support

Allow the plugin to fetch data from a different network namespace. This is possible by changing the network namespace before opening the netlink socket, and immediately changing back to the original network namespace once the socket is open. The number of nfct_open usages here warranted a dedicated wrapper function.

If changing back to the original network namespace fails, ulogd will log an error, but continue to run in a different network namespace than it was started in, which may cause unexpected behaviour. But I don't see a way to properly "escalate" it such that ulogd aborts entirely.

Also slightly adjust the error log messages to specify which socket failed to open.

---
 input/flow/ulogd_inpflow_NFCT.c | 81 +++++++++++++++++++++++++++------
 ulogd.conf.in                   |  1 +
 2 files changed, 69 insertions(+), 13 deletions(-)
input/flow/ulogd_inpflow_NFCT.c +68 -13
index 93edb76..7168b24 100644
@@ -45,6 +45,7 @@
 #include <ulogd/timer.h>
 #include <ulogd/ipfix_protocol.h>
 #include <ulogd/addr.h>
+#include <ulogd/namespace.h>
 
 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
 
@@ -78,7 +79,7 @@ struct nfct_pluginstance {
 #define EVENT_MASK	NF_NETLINK_CONNTRACK_NEW | NF_NETLINK_CONNTRACK_DESTROY
 
 static struct config_keyset nfct_kset = {
-	.num_ces = 12,
+	.num_ces = 13,
 	.ces = {
 		{
 			.key	 = "pollinterval",
@@ -149,6 +150,11 @@ static struct config_keyset nfct_kset = {
 			.type	 = CONFIG_TYPE_STRING,
 			.options = CONFIG_OPT_NONE,
 		},
+		{
+			.key     = "network_namespace_path",
+			.type    = CONFIG_TYPE_STRING,
+			.options = CONFIG_OPT_NONE,
+		},
 	},
 };
 #define pollint_ce(x)	(x->ces[0])
@@ -163,6 +169,7 @@ static struct config_keyset nfct_kset = {
 #define src_filter_ce(x)	((x)->ces[9])
 #define dst_filter_ce(x)	((x)->ces[10])
 #define proto_filter_ce(x)	((x)->ces[11])
+#define network_namespace_path_ce(x)	((x)->ces[12])
 
 enum nfct_keys {
 	NFCT_ORIG_IP_SADDR = 0,
@@ -979,6 +986,54 @@ static int read_cb_ovh(int fd, unsigned int what, void *param)
 	return 0;
 }
 
+/**
+ * nfct_open_in_netns() - Open conntrack netlink socket in a namespace
+ * @subscriptions: ctnetlink groups to subscribe to events
+ * @target_netns_path: path to the network namespace, can be NULL
+ *
+ * On error, NULL is returned and errno is explicitly set.
+ */
+struct nfct_handle *nfct_open_in_netns(unsigned int subscriptions,
+                                       const char *const target_netns_path)
+{
+	struct nfct_handle *result = NULL;
+	int source_netns_fd = -1;
+
+	if ((target_netns_path != NULL) &&
+	    (strlen(target_netns_path) > 0) &&
+	    (join_netns_path(target_netns_path, &source_netns_fd) != ULOGD_IRET_OK)) {
+		ulogd_log(ULOGD_FATAL, "error joining target network "
+		                       "namespace\n");
+		goto err_tns;
+	}
+
+	result = nfct_open(NFNL_SUBSYS_CTNETLINK, subscriptions);
+	if (result == NULL) {
+		ulogd_log(ULOGD_FATAL, "error opening ctnetlink: %s\n",
+		                       strerror(errno));
+		goto err_nfct;
+	}
+
+	if ((target_netns_path != NULL) &&
+	    (strlen(target_netns_path) > 0) &&
+	    (join_netns_fd(source_netns_fd, NULL) != ULOGD_IRET_OK)) {
+		ulogd_log(ULOGD_FATAL, "error joining source network "
+		                       "namespace\n");
+		goto err_sns;
+	}
+	source_netns_fd = -1;
+
+	return result;
+
+err_sns:
+	nfct_close(result);
+err_nfct:
+	if (source_netns_fd >= 0)
+		close(source_netns_fd);
+err_tns:
+	return NULL;
+}
+
 static int
 dump_reset_handler(enum nf_conntrack_msg_type type,
 		   struct nf_conntrack *ct, void *data)
@@ -1025,7 +1080,7 @@ static void get_ctr_zero(struct ulogd_pluginstance *upi)
 	struct nfct_handle *h;
 	int family = AF_UNSPEC;
 
-	h = nfct_open(CONNTRACK, 0);
+	h = nfct_open_in_netns(0, network_namespace_path_ce(upi->config_kset).u.string);
 	if (h == NULL) {
 		ulogd_log(ULOGD_FATAL, "Cannot dump and reset counters\n");
 		return;
@@ -1301,10 +1356,10 @@ static int constructor_nfct_events(struct ulogd_pluginstance *upi)
 			(struct nfct_pluginstance *)upi->private;
 
 
-	cpi->cth = nfct_open(NFNL_SUBSYS_CTNETLINK,
-			     eventmask_ce(upi->config_kset).u.value);
+	cpi->cth = nfct_open_in_netns(eventmask_ce(upi->config_kset).u.value,
+	                              network_namespace_path_ce(upi->config_kset).u.string);
 	if (!cpi->cth) {
-		ulogd_log(ULOGD_FATAL, "error opening ctnetlink\n");
+		ulogd_log(ULOGD_FATAL, "error opening event netlink socket\n");
 		goto err_cth;
 	}
 
@@ -1372,9 +1427,9 @@ static int constructor_nfct_events(struct ulogd_pluginstance *upi)
 		/* populate the hashtable: we use a disposable handler, we
 		 * may hit overrun if we use cpi->cth. This ensures that the
 		 * initial dump is successful. */
-		h = nfct_open(CONNTRACK, 0);
+		h = nfct_open_in_netns(0, network_namespace_path_ce(upi->config_kset).u.string);
 		if (!h) {
-			ulogd_log(ULOGD_FATAL, "error opening ctnetlink\n");
+			ulogd_log(ULOGD_FATAL, "error opening initial-fill netlink socket\n");
 			goto err_ovh;
 		}
 		nfct_callback_register(h, NFCT_T_ALL,
@@ -1384,9 +1439,9 @@ static int constructor_nfct_events(struct ulogd_pluginstance *upi)
 
 		/* the overrun handler only make sense with the hashtable,
 		 * if we hit overrun, we resync with ther kernel table. */
-		cpi->ovh = nfct_open(NFNL_SUBSYS_CTNETLINK, 0);
+		cpi->ovh = nfct_open_in_netns(0, network_namespace_path_ce(upi->config_kset).u.string);
 		if (!cpi->ovh) {
-			ulogd_log(ULOGD_FATAL, "error opening ctnetlink\n");
+			ulogd_log(ULOGD_FATAL, "error opening overrun-read netlink socket\n");
 			goto err_ovh;
 		}
 
@@ -1403,9 +1458,9 @@ static int constructor_nfct_events(struct ulogd_pluginstance *upi)
 		ulogd_register_fd(&cpi->nfct_ov);
 
 		/* we use this to purge old entries during overruns.*/
-		cpi->pgh = nfct_open(NFNL_SUBSYS_CTNETLINK, 0);
+		cpi->pgh = nfct_open_in_netns(0, network_namespace_path_ce(upi->config_kset).u.string);
 		if (!cpi->pgh) {
-			ulogd_log(ULOGD_FATAL, "error opening ctnetlink\n");
+			ulogd_log(ULOGD_FATAL, "error opening overrun-purge netlink socket\n");
 			goto err_pgh;
 		}
 	}
@@ -1438,9 +1493,9 @@ static int constructor_nfct_polling(struct ulogd_pluginstance *upi)
 		goto err;
 	}
 
-	cpi->pgh = nfct_open(NFNL_SUBSYS_CTNETLINK, 0);
+	cpi->pgh = nfct_open_in_netns(0, network_namespace_path_ce(upi->config_kset).u.string);
 	if (!cpi->pgh) {
-		ulogd_log(ULOGD_FATAL, "error opening ctnetlink\n");
+		ulogd_log(ULOGD_FATAL, "error opening polling netlink socket\n");
 		goto err;
 	}
 	nfct_callback_register(cpi->pgh, NFCT_T_ALL, &polling_handler, upi);
ulogd.conf.in +1 -0
index 9a04bf7..f7e3fa3 100644
@@ -139,6 +139,7 @@ logfile="/var/log/ulogd.log"
 #netlink_socket_buffer_size=217088
 #netlink_socket_buffer_maxsize=1085440
 #reliable=1 # enable reliable flow-based logging (may drop packets)
+#network_namespace_path=/run/netns/other # import flows from a different network namespace
 hash_enable=0
 
 # Logging of system packet through NFLOG

--
2.49.0

#2 lore raw

PATCH v2 3/4 nflog: add network namespace support

In reply to Corubba Smith
---
 input/packet/ulogd_inppkt_NFLOG.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)
input/packet/ulogd_inppkt_NFLOG.c +30 -1
index 62b3963..f99272e 100644
@@ -10,6 +10,7 @@
 #include <stdbool.h>
 
 #include <ulogd/ulogd.h>
+#include <ulogd/namespace.h>
 #include <libnfnetlink/libnfnetlink.h>
 #include <libnetfilter_log/libnetfilter_log.h>
 #ifdef BUILD_NFCT
@@ -40,7 +41,7 @@ struct nflog_input {
 /* configuration entries */
 
 static struct config_keyset libulog_kset = {
-	.num_ces = 12,
+	.num_ces = 13,
 	.ces = {
 		{
 			.key 	 = "bufsize",
@@ -115,6 +116,11 @@ static struct config_keyset libulog_kset = {
 			.options = CONFIG_OPT_NONE,
 			.u.value = 0,
 		},
+		{
+			.key     = "network_namespace_path",
+			.type    = CONFIG_TYPE_STRING,
+			.options = CONFIG_OPT_NONE,
+		},
 	}
 };
 
@@ -130,6 +136,7 @@ static struct config_keyset libulog_kset = {
 #define nlthreshold_ce(x) (x->ces[9])
 #define nltimeout_ce(x) (x->ces[10])
 #define attach_conntrack_ce(x) (x->ces[11])
+#define network_namespace_path_ce(x) (x->ces[12])
 
 enum nflog_keys {
 	NFLOG_KEY_RAW_MAC = 0,
@@ -585,11 +592,31 @@ static int start(struct ulogd_pluginstance *upi)
 	if (!ui->nfulog_buf)
 		goto out_buf;
 
+	const char *const target_netns_path =
+			network_namespace_path_ce(upi->config_kset).u.string;
+	int source_netns_fd = -1;
+	if ((strlen(target_netns_path) > 0) &&
+	    (join_netns_path(target_netns_path, &source_netns_fd) != ULOGD_IRET_OK)
+	   ) {
+		ulogd_log(ULOGD_FATAL, "error joining target network "
+		                       "namespace\n");
+		goto out_ns;
+	}
+
 	ulogd_log(ULOGD_DEBUG, "opening nfnetlink socket\n");
 	ui->nful_h = nflog_open();
 	if (!ui->nful_h)
 		goto out_handle;
 
+	if ((strlen(target_netns_path) > 0) &&
+	    (join_netns_fd(source_netns_fd, NULL) != ULOGD_IRET_OK)
+	   ) {
+		ulogd_log(ULOGD_FATAL, "error joining source network "
+		                       "namespace\n");
+		goto out_handle;
+	}
+	source_netns_fd = -1;
+
 	/* This is the system logging (conntrack, ...) facility */
 	if ((group_ce(upi->config_kset).u.value == 0) ||
 			(bind_ce(upi->config_kset).u.value > 0)) {
@@ -685,6 +712,8 @@ out_bind:
 	}
 	nflog_close(ui->nful_h);
 out_handle:
+	if (source_netns_fd >= 0) close(source_netns_fd);
+out_ns:
 	free(ui->nfulog_buf);
 out_buf:
 	return -1;

--
2.49.0

maintainer
#3 lore raw

Re: [PATCH ulogd2,v2 3/4] nflog: add network namespace support

input/packet/ulogd_inppkt_NFLOG.c static int start(struct ulogd_pluginstance *upi) · line 619

Quoted 9 lines · Corubba Smith
input/packet/ulogd_inppkt_NFLOG.c +9 -0
+	if ((strlen(target_netns_path) > 0) &&
+	    (join_netns_fd(source_netns_fd, NULL) != ULOGD_IRET_OK)
+	   ) {
+		ulogd_log(ULOGD_FATAL, "error joining source network "
+		                       "namespace\n");
+		goto out_handle;
+	}
+	source_netns_fd = -1;
+

This looks buggy, but I do realize that join_netns_fd() closes this for us.

Maybe a comment would help?

#4 lore raw

PATCH v2 4/4 nfacct: add network namespace support

In reply to Corubba Smith
---
 input/sum/ulogd_inpflow_NFACCT.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
input/sum/ulogd_inpflow_NFACCT.c +30 -2
index bd45df4..97bfd8b 100644
@@ -20,6 +20,7 @@
 
 #include <ulogd/ulogd.h>
 #include <ulogd/timer.h>
+#include <ulogd/namespace.h>
 
 #include <libmnl/libmnl.h>
 #include <libnetfilter_acct/libnetfilter_acct.h>
@@ -52,13 +53,19 @@ static struct config_keyset nfacct_kset = {
 			.type	 = CONFIG_TYPE_INT,
 			.options = CONFIG_OPT_NONE,
 			.u.value = 0,
-		}
+		},
+		{
+			.key	 = "network_namespace_path",
+			.type	 = CONFIG_TYPE_STRING,
+			.options = CONFIG_OPT_NONE,
+		},
 	},
-	.num_ces = 3,
+	.num_ces = 4,
 };
 #define pollint_ce(x)	(x->ces[0])
 #define zerocounter_ce(x) (x->ces[1])
 #define timestamp_ce(x) (x->ces[2])
+#define network_namespace_path_ce(x) (x->ces[3])
 
 enum ulogd_nfacct_keys {
 	ULOGD_NFACCT_NAME,
@@ -240,12 +247,33 @@ static int constructor_nfacct(struct ulogd_pluginstance *upi)
 	if (pollint_ce(upi->config_kset).u.value == 0)
 		return -1;
 
+	const char *const target_netns_path =
+			network_namespace_path_ce(upi->config_kset).u.string;
+	int source_netns_fd = -1;
+	if ((strlen(target_netns_path) > 0) &&
+	    (join_netns_path(target_netns_path, &source_netns_fd) != ULOGD_IRET_OK)
+	   ) {
+		ulogd_log(ULOGD_FATAL, "error joining target network "
+		                       "namespace\n");
+		return -1;
+	}
+
 	cpi->nl = mnl_socket_open(NETLINK_NETFILTER);
 	if (cpi->nl == NULL) {
 		ulogd_log(ULOGD_FATAL, "cannot open netlink socket\n");
 		return -1;
 	}
 
+	if ((strlen(target_netns_path) > 0) &&
+	    (join_netns_fd(source_netns_fd, NULL) != ULOGD_IRET_OK)
+	   ) {
+		ulogd_log(ULOGD_FATAL, "error joining source network "
+		                       "namespace\n");
+		close(source_netns_fd);
+		return -1;
+	}
+	source_netns_fd = -1;
+
 	if (mnl_socket_bind(cpi->nl, 0, MNL_SOCKET_AUTOPID) < 0) {
 		ulogd_log(ULOGD_FATAL, "cannot bind netlink socket\n");
 		return -1;

--
2.49.0

maintainer
#5 lore raw
In reply to Corubba Smith
Quoted 9 lines · Corubba Smith

The new namespace helper provides an internal stable interface for plugins to use for switching various linux namespaces. Currently only network namespaces are supported/implemented, but can easily be extended if needed. autoconf will enable it automatically if the required symbols are available. If ulogd is compiled without namespace support, the functions will simply return an error, there is no need for conditional compilation or special handling in plugin code.

Looks good to me, I intend to apply this later this week unless there are objections.

Quoted 2 lines

and NFACCT plugins. I skipped ULOG because it's removed from the kernel since 7200135bc1e6 ("netfilter: kill ulog targets") aka v3.17

Yeah, ULOG code should just be axed, there is no point in carrying this in the tree anymore.

Quoted 17 lines
src/Makefile.am +2 -1
@@ -6,6 +6,7 @@ AM_CPPFLAGS += -DULOGD_CONFIGFILE='"$(sysconfdir)/ulogd.conf"' \
 
 sbin_PROGRAMS = ulogd
 
-ulogd_SOURCES = ulogd.c select.c timer.c rbtree.c conffile.c hash.c addr.c
+ulogd_SOURCES = ulogd.c select.c timer.c rbtree.c conffile.c hash.c \
+                addr.c namespace.c
 ulogd_LDADD   = ${libdl_LIBS} ${libpthread_LIBS}
 ulogd_LDFLAGS = -export-dynamic
src/namespace.c new file +0 -0
new file mode 100644
index 0000000..f9f23d4

src/namespace.c line 9

Quoted 9 lines
src/namespace.c new file +9 -0
@@ -0,0 +1,237 @@
+/* namespace helper
+ *
+ * userspace logging daemon for the netfilter subsystem
+ *
+ * (C) 2025 The netfilter project
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License version 2
+ *  as published by the Free Software Foundation.

I intend to replace all of this with

/* SPDX-License-Identifier: GPL-2.0 */

No need for license boilerplate, IMO.

#6 lore raw
Quoted 9 lines · Florian Westphal
Quoted 8 lines · Corubba Smith

The new namespace helper provides an internal stable interface for plugins to use for switching various linux namespaces. Currently only network namespaces are supported/implemented, but can easily be extended if needed. autoconf will enable it automatically if the required symbols are available. If ulogd is compiled without namespace support, the functions will simply return an error, there is no need for conditional compilation or special handling in plugin code.

Quoted 4 lines
Quoted 1 line

Looks good to me, I intend to apply this later this week unless there are objections.

If I may be so bold: Friendly reminder that this patchset is not yet applied, and in the meantime I also sent a v3 [0] incorporating your feedback.

[0] https://lore.kernel.org/netfilter-devel/[email protected]/

--
Corubba

maintainer
#7 lore raw
Quoted 10 lines · Corubba Smith
Quoted 9 lines · Florian Westphal
Quoted 8 lines · Corubba Smith

The new namespace helper provides an internal stable interface for plugins to use for switching various linux namespaces. Currently only network namespaces are supported/implemented, but can easily be extended if needed. autoconf will enable it automatically if the required symbols are available. If ulogd is compiled without namespace support, the functions will simply return an error, there is no need for conditional compilation or special handling in plugin code.

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

Looks good to me, I intend to apply this later this week unless there are objections.

If I may be so bold: Friendly reminder that this patchset is not yet applied, and in the meantime I also sent a v3 [0] incorporating your feedback.

I know, the patches are deferred until after next ulogd2 release which should happen next week.