Skip to contentSkip to conversation

conntrack-tools: introduce conntrackdctl

RFC PATCH In review Arturo Borrero Gonzalez 2 participants 3 messages Feb 4, 2021 – Feb 6, 2021 netfilter

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 conntrack-tools: introduce conntrackdctl

Separate conntrackd command line client functionalities into a different binary.

This should help with several things:
* avoid reading and parsing the config file, which can fail in some enviroments, for example if conntrackd is running inside a netns and the referenced interfaces/addresses doesn't exist in the namespace conntrackd client command is running from.
* easily update conntrakdctl with more functionalities without polluting the daemon binary
* easier code maintenance

---
NOTE: this is just an idea, comments welcome. I have plans to extend this new binary with more options in the future.

 include/local.h     |   10 +-
 src/.gitignore      |    1 
 src/Makefile.am     |    5 +
 src/conntrackdctl.c |  292 +++++++++++++++++++++++++++++++++++++++++++++++++++
 src/local.c         |   18 +--
 src/main.c          |  223 +++------------------------------------
 6 files changed, 325 insertions(+), 224 deletions(-)
 create mode 100644 src/conntrackdctl.c
include/local.h +4 -6
index 9379446..505708b 100644
@@ -5,6 +5,8 @@
 #define UNIX_PATH_MAX   108
 #endif
 
+#define DEFAULT_SOCKET_PATH "/var/run/conntrackd.ctl"
+
 struct local_conf {
 	int reuseaddr;
 	char path[UNIX_PATH_MAX + 1];
@@ -26,11 +28,7 @@ void local_server_destroy(struct local_server *server);
 int do_local_server_step(struct local_server *server, void *data, 
 			 int (*process)(int fd, void *data));
 
-/* local client */
-int local_client_create(struct local_conf *conf);
-void local_client_destroy(int fd);
-int do_local_client_step(int fd, void (*process)(char *buf));
-int do_local_request(int, struct local_conf *,void (*step)(char *buf));
-void local_step(char *buf);
+/* local client, conntrackdctl */
+int do_local_request(int request, const char *socket_path);
 
 #endif
src/.gitignore +1 -0
index 55a0d27..fd09ef1 100644
@@ -1,6 +1,7 @@
 /conntrack
 /conntrackd
 /nfct
+/conntrackdctl
 
 /read_config_lex.c
 /read_config_yy.c
src/Makefile.am +4 -1
index 2e66ee9..ca9e43a 100644
@@ -8,7 +8,7 @@ AM_YFLAGS = -d
 
 CLEANFILES = read_config_yy.c read_config_lex.c
 
-sbin_PROGRAMS = conntrack conntrackd nfct
+sbin_PROGRAMS = conntrack conntrackd nfct conntrackdctl
 
 conntrack_SOURCES = conntrack.c
 conntrack_LDADD = ../extensions/libct_proto_tcp.la ../extensions/libct_proto_udp.la ../extensions/libct_proto_udplite.la ../extensions/libct_proto_icmp.la ../extensions/libct_proto_icmpv6.la ../extensions/libct_proto_sctp.la ../extensions/libct_proto_dccp.la ../extensions/libct_proto_gre.la ../extensions/libct_proto_unknown.la ${LIBNETFILTER_CONNTRACK_LIBS} ${LIBMNL_LIBS} ${LIBNFNETLINK_LIBS}
@@ -54,6 +54,9 @@ conntrackd_SOURCES = alarm.c main.c run.c hash.c queue.c queue_tx.c rbtree.c \
 		    read_config_yy.y read_config_lex.l \
 		    stack.c resync.c
 
+conntrackdctl_SOURCES = conntrackdctl.c local.c
+conntrackdctl_LDADD   = ${libdl_LIBS}
+
 if HAVE_CTHELPER
 conntrackd_SOURCES += cthelper.c helpers.c utils.c expect.c
 endif
src/conntrackdctl.c new file +292 -0

292 lines not on this page · raw message

src/local.c +8 -10
index 2b67885..dfb0d4c 100644
@@ -91,7 +91,7 @@ int do_local_server_step(struct local_server *server, void *data,
 	return 0;
 }
 
-int local_client_create(struct local_conf *conf)
+static int local_client_create(const char *socket_path)
 {
 	socklen_t len;
 	struct sockaddr_un local;
@@ -101,7 +101,7 @@ int local_client_create(struct local_conf *conf)
 		return -1;
 
 	local.sun_family = AF_UNIX;
-	strcpy(local.sun_path, conf->path);
+	strcpy(local.sun_path, socket_path);
 	len = strlen(local.sun_path) + sizeof(local.sun_family);
 
 	if (connect(fd, (struct sockaddr *) &local, len) == -1) {
@@ -112,12 +112,12 @@ int local_client_create(struct local_conf *conf)
 	return fd;
 }
 
-void local_client_destroy(int fd)
+static void local_client_destroy(int fd)
 {
 	close(fd);
 }
 
-int do_local_client_step(int fd, void (*process)(char *buf))
+static int do_local_client_step(int fd, void (*process)(char *buf))
 {
 	char buf[1024];
 
@@ -132,18 +132,16 @@ int do_local_client_step(int fd, void (*process)(char *buf))
 	return 0;
 }
 
-void local_step(char *buf)
+static void local_step(char *buf)
 {
 	printf("%s", buf);
 }
 
-int do_local_request(int request,
-		     struct local_conf *conf,
-		     void (*step)(char *buf))
+int do_local_request(int request, const char *socket_path)
 {
 	int fd, ret;
 
-	fd = local_client_create(conf);
+	fd = local_client_create(socket_path);
 	if (fd == -1)
 		return -1;
 
@@ -151,7 +149,7 @@ int do_local_request(int request,
 	if (ret == -1)
 		goto err1;
 
-	do_local_client_step(fd, step);
+	do_local_client_step(fd, local_step);
 
 	local_client_destroy(fd);
 
src/main.c +16 -207

279 lines not on this page · raw message

#1 lore raw
Quoted 9 lines · Arturo Borrero Gonzalez

Separate conntrackd command line client functionalities into a different binary.

This should help with several things:
* avoid reading and parsing the config file, which can fail in some enviroments, for example if conntrackd is running inside a netns and the referenced interfaces/addresses doesn't exist in the namespace conntrackd client command is running from.
* easily update conntrakdctl with more functionalities without polluting the daemon binary
* easier code maintenance

[...]

src/main.c int main(int argc, char *argv[]) · line 343

Quoted 12 lines
src/main.c +0 -8
@@ -333,15 +151,6 @@ int main(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	if (type == REQUEST) {
-		if (do_local_request(action, &conf.local, local_step) == -1) {
-			dlog(LOG_ERR, "can't connect: is conntrackd "
-			     "running? appropriate permissions?");
-			exit(EXIT_FAILURE);
-		}
-		exit(EXIT_SUCCESS);
-	}

The existing interface needs to be in place to be backward compatible.

I'm fine for you to add this, but we'll have to leave the existing interface for quite a bit of time, it might take years before we can deprecate the old way.

#2 lore raw
Quoted 26 lines · Pablo Neira Ayuso
Quoted 9 lines · Arturo Borrero Gonzalez

Separate conntrackd command line client functionalities into a different binary.

This should help with several things:
* avoid reading and parsing the config file, which can fail in some enviroments, for example if conntrackd is running inside a netns and the referenced interfaces/addresses doesn't exist in the namespace conntrackd client command is running from.
* easily update conntrakdctl with more functionalities without polluting the daemon binary
* easier code maintenance

[...]

src/main.c int main(int argc, char *argv[]) · line 343

Quoted 12 lines
src/main.c +0 -8
@@ -333,15 +151,6 @@ int main(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	if (type == REQUEST) {
-		if (do_local_request(action, &conf.local, local_step) == -1) {
-			dlog(LOG_ERR, "can't connect: is conntrackd "
-			     "running? appropriate permissions?");
-			exit(EXIT_FAILURE);
-		}
-		exit(EXIT_SUCCESS);
-	}

The existing interface needs to be in place to be backward compatible.

Quoted 3 lines

I'm fine for you to add this, but we'll have to leave the existing interface for quite a bit of time, it might take years before we can deprecate the old way.

You could probably update conntrackd to invoke conntrackdctl when you detect options that represent queries to the daemon. That would allow you to retain backward compatibility. Hence, old scripts still work while new ones might start using the new conntrackdctl command.

If the problem is that you would like to skip the config parsing (which is what has triggered the errors when running things from the netns environment IIRC), then, you could update the code to skip the parsing:

- the user does not specify a configuration file

AND

- the options refer to a query to the daemon via UNIX socket.

That's another possibility to consider.