Skip to contentSkip to conversation

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

PATCH Applied Christoph Heiss 2 participants 4 messages Jun 23, 2025 netfilter

lore
Share & tools
lore
#0 lore raw

v3 cover 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.

First patch is preparatory only; to make --labelmap position-independent.

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

Changes v2 -> v3:
* addressed minor cosmetic nits, no functional changes

Changes v1 -> v2:
* introduced preparatory patch moving label merging after arg parsing
* removed redundant if around free() call
* abort if --labelmap is specified multiple times

Christoph Heiss (2):
conntrack: move label parsing to after argument parsing
conntrack: introduce --labelmap option to specify connlabel.conf path

 conntrack.8         |   5 ++
 include/conntrack.h |   2 +-
 src/conntrack.c     | 126 +++++++++++++++++++++++++++++---------------
 3 files changed, 90 insertions(+), 43 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH v3 1/2 conntrack: move label parsing to after argument parsing

Instead of parsing them directly inline during argument parsing, put them into a list and do it afterwards.

Preparation for introduction a new --labelmap option to specify the path to the label mapping file.

---
 src/conntrack.c | 62 +++++++++++++++++++++++++++++++++++--------------
 1 file changed, 44 insertions(+), 18 deletions(-)
src/conntrack.c +44 -18
index 2d4e864..96f19b5 100644
@@ -122,6 +122,12 @@ struct ct_cmd {
 	struct ct_tmpl	tmpl;
 };
 
+struct ct_label {
+	struct list_head list;
+	char *name;
+	bool is_modify;
+};
+
 static int alloc_tmpl_objects(struct ct_tmpl *tmpl)
 {
 	tmpl->ct = nfct_new();
@@ -2963,6 +2969,32 @@ static int print_stats(const struct ct_cmd *cmd)
 	return 0;
 }
 
+static void parse_and_merge_labels(struct list_head *label_list, struct ct_tmpl *tmpl)
+{
+	struct ct_label *label, *next;
+	struct nfct_bitmask *bitmask;
+	unsigned int max;
+
+	list_for_each_entry_safe(label, next, label_list, list) {
+		max = parse_label_get_max(label->name);
+		bitmask = nfct_bitmask_new(max);
+		if (!bitmask)
+			exit_error(OTHER_PROBLEM, "out of memory");
+
+		parse_label(bitmask, label->name);
+
+		/* join "-l foo -l bar" into single bitmask object */
+		if (label->is_modify)
+			merge_bitmasks(&tmpl->label_modify, bitmask);
+		else
+			merge_bitmasks(&tmpl->label, bitmask);
+
+		list_del(&label->list);
+		free(label->name);
+		free(label);
+	}
+}
+
 static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[])
 {
 	unsigned int type = 0, event_mask = 0, l4flags = 0, status = 0;
@@ -2973,6 +3005,7 @@ static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[])
 	struct ct_tmpl *tmpl;
 	int res = 0, partial;
 	union ct_address ad;
+	LIST_HEAD(labels);
 	uint32_t value;
 	int c, cmd;
 
@@ -3088,8 +3121,6 @@ static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[])
 		case 'o':
 			options |= CT_OPT_OUTPUT;
 			parse_parameter(optarg, &output_mask, PARSE_OUTPUT);
-			if (output_mask & _O_CL)
-				labelmap_init();
 			if ((output_mask & _O_SAVE) &&
 			    (output_mask & (_O_EXT |_O_TMS |_O_ID | _O_KTMS | _O_CL | _O_XML)))
 				exit_error(OTHER_PROBLEM,
@@ -3162,8 +3193,6 @@ static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[])
 		case '>':
 			options |= opt2type[c];
 
-			labelmap_init();
-
 			if ((options & (CT_OPT_DEL_LABEL|CT_OPT_ADD_LABEL)) ==
 			    (CT_OPT_DEL_LABEL|CT_OPT_ADD_LABEL))
 				exit_error(OTHER_PROBLEM, "cannot use --label-add and "
@@ -3176,22 +3205,13 @@ static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[])
 				optarg = tmp;
 			}
 
-			char *optarg2 = strdup(optarg);
-			unsigned int max = parse_label_get_max(optarg);
-			struct nfct_bitmask * b = nfct_bitmask_new(max);
-			if (!b)
+			struct ct_label *l = calloc(1, sizeof(*l));
+			if (!l)
 				exit_error(OTHER_PROBLEM, "out of memory");
 
-			parse_label(b, optarg2);
-
-			/* join "-l foo -l bar" into single bitmask object */
-			if (c == 'l') {
-				merge_bitmasks(&tmpl->label, b);
-			} else {
-				merge_bitmasks(&tmpl->label_modify, b);
-			}
-
-			free(optarg2);
+			l->name = strdup(optarg);
+			l->is_modify = c == '<' || c == '>';
+			list_add_tail(&l->list, &labels);
 			break;
 		case 'a':
 			fprintf(stderr, "WARNING: ignoring -%c, "
@@ -3246,6 +3266,12 @@ static void do_parse(struct ct_cmd *ct_cmd, int argc, char *argv[])
 		}
 	}
 
+	/* any of these options (might) use labels */
+	if ((options & (CT_OPT_LABEL | CT_OPT_ADD_LABEL | CT_OPT_DEL_LABEL)) ||
+	    ((options & CT_OPT_OUTPUT) && (output_mask & _O_CL))) {
+		labelmap_init();
+		parse_and_merge_labels(&labels, tmpl);
+	}
 
 	/* we cannot check this combination with generic_opt_check. */
 	if (options & CT_OPT_ANY_NAT &&
Signature · 1 line
2.49.0
#2 lore raw

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

In reply to 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.

---
 conntrack.8         |  5 ++++
 include/conntrack.h |  2 +-
 src/conntrack.c     | 64 ++++++++++++++++++++++++++++-----------------
 3 files changed, 46 insertions(+), 25 deletions(-)
conntrack.8 +5 -0
index 3b6a15b..2bfd80e 100644
@@ -189,6 +189,11 @@ 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".
+.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 +40 -24

130 lines not on this page · raw message

Signature · 1 line
2.49.0
maintainer Applied
#3 lore raw
In reply to Christoph Heiss
Quoted 6 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.

Quoted 8 lines

First patch is preparatory only; to make --labelmap position-independent.

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

Changes v2 -> v3:
* addressed minor cosmetic nits, no functional changes

Applied, thanks for following up and addressing all comments.