Skip to contentSkip to conversation

conntrack: introduce --labelmap option to specify connlabel.conf path

PATCH Superseded Christoph Heiss 2 participants 4 messages Jun 13, 2025 netfilter

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 conntrack: introduce --labelmap option to specify connlabel.conf path

Enables specifying a path to a connlabel.conf to load instead of the default one at /etc/xtables/connlabel.conf.

nfct_labelmap_new() already allows supplying a custom path to load labels from, so it just needs to be passed in there.

---
 conntrack.8         |  7 +++++
 include/conntrack.h |  2 +-
 src/conntrack.c     | 62 +++++++++++++++++++++++++++------------------
 3 files changed, 46 insertions(+), 25 deletions(-)
conntrack.8 +7 -0
index 3b6a15b..2b6da25 100644
@@ -189,6 +189,13 @@ This option is only available in conjunction with "\-L, \-\-dump",
 Match entries whose labels include those specified as arguments.
 Use multiple \-l options to specify multiple labels that need to be set.
 .TP
+.BI "--labelmap " "PATH"
+Specify the path to a connlabel.conf file to load instead of the default one.
+This option is only available in conjunction with "\-L, \-\-dump", "\-E,
+\-\-event", "\-U \-\-update" or "\-D \-\-delete". Must come before any of
+"\-l, \-\-label", "\-\-label\-add" or "\-\-label\-del", otherwise the argument is
+ignored.
+.TP
 .BI "--label-add " "LABEL"
 Specify the conntrack label to add to the selected conntracks.
 This option is only available in conjunction with "\-I, \-\-create",
include/conntrack.h +1 -1
index 6dad4a1..317cab6 100644
@@ -78,7 +78,7 @@ enum ct_command {
 };
 
 #define NUMBER_OF_CMD   _CT_BIT_MAX
-#define NUMBER_OF_OPT   29
+#define NUMBER_OF_OPT   30
 
 struct nf_conntrack;
 
src/conntrack.c +38 -24

128 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 5 lines · Christoph Heiss

Enables specifying a path to a connlabel.conf to load instead of the default one at /etc/xtables/connlabel.conf.

nfct_labelmap_new() already allows supplying a custom path to load labels from, so it just needs to be passed in there.

Makes sense, patch looks good to me.

Quoted 38 lines
 3 files changed, 46 insertions(+), 25 deletions(-)
conntrack.8 +7 -0
index 3b6a15b..2b6da25 100644
@@ -189,6 +189,13 @@ This option is only available in conjunction with "\-L, \-\-dump",
 Match entries whose labels include those specified as arguments.
 Use multiple \-l options to specify multiple labels that need to be set.
 .TP
+.BI "--labelmap " "PATH"
+Specify the path to a connlabel.conf file to load instead of the default one.
+This option is only available in conjunction with "\-L, \-\-dump", "\-E,
+\-\-event", "\-U \-\-update" or "\-D \-\-delete". Must come before any of
+"\-l, \-\-label", "\-\-label\-add" or "\-\-label\-del", otherwise the argument is
+ignored.
+.TP
 .BI "--label-add " "LABEL"
 Specify the conntrack label to add to the selected conntracks.
 This option is only available in conjunction with "\-I, \-\-create",
include/conntrack.h +1 -1
index 6dad4a1..317cab6 100644
@@ -78,7 +78,7 @@ enum ct_command {
 };
 
 #define NUMBER_OF_CMD   _CT_BIT_MAX
-#define NUMBER_OF_OPT   29
+#define NUMBER_OF_OPT   30
 
 struct nf_conntrack;
 
src/conntrack.c +0 -0
index 2d4e864..9850825 100644

src/conntrack.c enum ct_options { · line 254

Quoted 6 lines
src/conntrack.c +3 -0
@@ -249,6 +249,9 @@ enum ct_options {
 
 	CT_OPT_REPL_ZONE_BIT	= 28,
 	CT_OPT_REPL_ZONE	= (1 << CT_OPT_REPL_ZONE_BIT),
+
+	CT_OPT_LABELMAP_BIT	= 29,
+	CT_OPT_LABELMAP		= (1 << CT_OPT_LABELMAP_BIT),

Why is this needed?

Quoted 14 lines
src/conntrack.c +2 -1
+static char *labelmap_path;
 static struct nfct_labelmap *labelmap;
 static int filter_family;
@@ -2756,7 +2764,7 @@ static void labelmap_init(void)
 {
 	if (labelmap)
 		return;
-	labelmap = nfct_labelmap_new(NULL);
+	labelmap = nfct_labelmap_new(labelmap_path);
 	if (!labelmap)
 		perror("nfct_labelmap_new");
 }

src/conntrack.c static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[]) · line 3224

Quoted 5 lines
src/conntrack.c +2 -0
@@ -3212,6 +3220,10 @@ static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[])
 			socketbuffersize = atol(optarg);
 			options |= CT_OPT_BUFFERSIZE;
 			break;
+		case 'M':
+			labelmap_path = strdup(optarg);

Should this exit() if labelmap_path != NULL?

src/conntrack.c try_proc: · line 3692

Quoted 2 lines
src/conntrack.c +2 -0
+	if (labelmap_path)
+		free(labelmap_path);

free(NULL) is ok, so no need for the conditional.

Patch looks fine, but I think it would be good to have a preparation patch that moves all labelmap_init() calls from do_parse() to into do_command_ct().

Then the option ordering would not matter anymore.

#2 lore raw

Thanks for the review!

Quoted 17 lines · Florian Westphal
Quoted 5 lines · Christoph Heiss

Enables specifying a path to a connlabel.conf to load instead of the default one at /etc/xtables/connlabel.conf.

nfct_labelmap_new() already allows supplying a custom path to load labels from, so it just needs to be passed in there.

Makes sense, patch looks good to me.

Quoted 8 lines
 3 files changed, 46 insertions(+), 25 deletions(-)

[..]

src/conntrack.c +0 -0
index 2d4e864..9850825 100644
@@ -249,6 +249,9 @@ enum ct_options {
 
Quoted 7 lines

src/conntrack.c enum ct_options { · line 254

Quoted 5 lines
src/conntrack.c +3 -0
 	CT_OPT_REPL_ZONE_BIT	= 28,
 	CT_OPT_REPL_ZONE	= (1 << CT_OPT_REPL_ZONE_BIT),
+
+	CT_OPT_LABELMAP_BIT	= 29,
+	CT_OPT_LABELMAP		= (1 << CT_OPT_LABELMAP_BIT),

Why is this needed?

Honestly, the option parsing is quite convoluted.

But it's used for indexing into optflags, which in turned is used by generic_opt_check().

As --labelmap can only be used with --label{-add,-del} and thus -L, -E, -U and -D, this is appropriately reflected in commands_v_options.

Based on that, generic_opt_check() will then throw an error/abort if --labelmap is used with any other command, e.g.:

conntrack v1.4.8 (conntrack-tools): Illegal option --labelmap' with this command Try conntrack -h' or 'conntrack --help' for more information.

At least what I got after tracing quite a bit through the code.

Quoted 9 lines

src/conntrack.c static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[]) · line 3224

Quoted 7 lines

[..]

src/conntrack.c +2 -0
@@ -3212,6 +3220,10 @@ static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[])
 			socketbuffersize = atol(optarg);
 			options |= CT_OPT_BUFFERSIZE;
 			break;
+		case 'M':
+			labelmap_path = strdup(optarg);

Should this exit() if labelmap_path != NULL?

Don't have a strong preference on this, but probably makes sense to abort in case the user ever specifies the option multiple times. I'll add that.

Quoted 5 lines

src/conntrack.c try_proc: · line 3692

Quoted 3 lines
src/conntrack.c +2 -0
+	if (labelmap_path)
+		free(labelmap_path);

free(NULL) is ok, so no need for the conditional.

Ack.

Quoted 6 lines

Patch looks fine, but I think it would be good to have a preparation patch that moves all labelmap_init() calls from do_parse() to into do_command_ct().

Then the option ordering would not matter anymore.

Can do that. Will just entail a bit more refactoring around the --label{-add,-del} option parsing, as that relies on an already initialized labelmap.

maintainer
#3 lore raw
Quoted 7 lines · Christoph Heiss

But it's used for indexing into optflags, which in turned is used by generic_opt_check().

As --labelmap can only be used with --label{-add,-del} and thus -L, -E, -U and -D, this is appropriately reflected in commands_v_options.

Quoted 5 lines

Based on that, generic_opt_check() will then throw an error/abort if --labelmap is used with any other command, e.g.:

conntrack v1.4.8 (conntrack-tools): Illegal option --labelmap' with this command Try conntrack -h' or 'conntrack --help' for more information.

Makes sense, thanks.

Quoted 5 lines
Quoted 1 line

Should this exit() if labelmap_path != NULL?

Don't have a strong preference on this, but probably makes sense to abort in case the user ever specifies the option multiple times. I'll add that.

Thanks!

Quoted 3 lines

Can do that. Will just entail a bit more refactoring around the --label{-add,-del} option parsing, as that relies on an already initialized labelmap.

Right, if this becomes too convoluted then another alternative is to error out if the --labelmap option is seen but the initalisation was already done with an error message indicating that the labelmap option must come first.