Skip to contentSkip to conversation

conntrack: minor cleanup

PATCH In review Florian Westphal 2 participants 4 messages Sep 3, 2013 netfilter

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/3 conntrack: minor cleanup

Rename get_table to generic "optional argument handling" helper, so it can be re-used in upcoming patch.

While at it, avoid copy&paste of "labelmap" handling.

---
 src/conntrack.c |   41 +++++++++++++++++++----------------------
 1 files changed, 19 insertions(+), 22 deletions(-)
src/conntrack.c +19 -22
index 7d2a365..2567c08 100644
@@ -890,20 +890,20 @@ add_command(unsigned int *cmd, const int newcmd)
 	*cmd |= newcmd;
 }
 
-static char *get_table(int argc, char *argv[])
+static char *get_optional_arg(int argc, char *argv[])
 {
-	char *table = NULL;
+	char *arg = NULL;
 
 	/* Nasty bug or feature in getopt_long ?
 	 * It seems that it behaves badly with optional arguments.
 	 * Fortunately, I just stole the fix from iptables ;) */
 	if (optarg)
-		return 0;
+		return arg;
 	else if (optind < argc && argv[optind][0] != '-' &&
 		 argv[optind][0] != '!')
-		table = argv[optind++];
+		arg = argv[optind++];
 
-	return table;
+	return arg;
 }
 
 enum {
@@ -915,7 +915,7 @@ enum {
 
 static unsigned int check_type(int argc, char *argv[])
 {
-	const char *table = get_table(argc, argv);
+	const char *table = get_optional_arg(argc, argv);
 
 	/* default to conntrack subsystem if nothing has been specified. */
 	if (table == NULL)
@@ -1818,6 +1818,15 @@ static int mnl_nfct_dump_cb(const struct nlmsghdr *nlh, void *data)
 
 static struct ctproto_handler *h;
 
+static void labelmap_init(void)
+{
+	if (labelmap)
+		return;
+	labelmap = nfct_labelmap_new(NULL);
+	if (!labelmap)
+		perror("nfct_labelmap_new");
+}
+
 int main(int argc, char *argv[])
 {
 	int c, cmd;
@@ -1970,12 +1979,8 @@ int main(int argc, char *argv[])
 		case 'o':
 			options |= CT_OPT_OUTPUT;
 			parse_parameter(optarg, &output_mask, PARSE_OUTPUT);
-			if (output_mask & _O_CL) {
-				if (!labelmap)
-					labelmap = nfct_labelmap_new(NULL);
-				if (!labelmap)
-					perror("nfct_labelmap_new");
-			}
+			if (output_mask & _O_CL)
+				labelmap_init();
 			break;
 		case 'z':
 			options |= CT_OPT_ZERO;
@@ -1987,12 +1992,7 @@ int main(int argc, char *argv[])
 
 			options |= opt2type[c];
 
-			if (optarg)
-				continue;
-			else if (optind < argc && argv[optind][0] != '-'
-				 && argv[optind][0] != '!')
-				tmp = argv[optind++];
-
+			tmp = get_optional_arg(argc, argv);
 			if (tmp == NULL)
 				continue;
 
@@ -2023,10 +2023,7 @@ int main(int argc, char *argv[])
 			options |= opt2type[c];
 			char *optarg2 = strdup(optarg);
 
-			if (!labelmap)
-				labelmap = nfct_labelmap_new(NULL);
-			if (!labelmap)
-				exit_error(OTHER_PROBLEM, "unable to open labelmap file");
+			labelmap_init();
 
 			unsigned int max = parse_label_get_max(optarg);
 			struct nfct_bitmask * b = nfct_bitmask_new(max);
Signature · 1 line
1.7.8.6
author maintainer
#1 lore raw

PATCH 2/3 conntrack: favor multiple -l options over -l foo,bar

Its more straightforward to use -l foo -l bar when only wanting to see conntracks that have both labels set.

It also simplifies things a lot.

---
 conntrack.8     |    7 ++--
 src/conntrack.c |  106 ++++++++++++++++++++++++------------------------------
 2 files changed, 51 insertions(+), 62 deletions(-)
conntrack.8 +4 -3
index f273434..6410e5b 100644
@@ -144,10 +144,11 @@ the MARK value into the ctmark.
 Otherwise, the mask is logically ANDed with the existing mark before the comparision.
 In "--create" mode, the mask is ignored.
 .TP
-.BI "-l, --label " "LABEL,..."
-Specify the conntrack labels.
+.BI "-l, --label " "LABEL"
+Specify a conntrack label.
 This option is only available in conjunction with "-L, --dump" or "-E, --event".
-Match entries whose labels matches at least those specified as arguments.
+Match entries whose labels match at least those specified.
+Use multiple -l commands to specify multiple labels that need to be set.
 .TP
 .BI "-c, --secmark " "SECMARK"
 Specify the conntrack selinux security mark.
src/conntrack.c +47 -59

128 lines not on this page · raw message

Signature · 1 line
1.7.8.6
#2 lore raw

Re: [PATCH 2/3] conntrack: favor multiple -l options over -l foo,bar

Quoted 4 lines · Florian Westphal

Its more straightforward to use -l foo -l bar when only wanting to see conntracks that have both labels set.

It also simplifies things a lot.

We need to keep the old syntax, 1.4.2 already supports -l foo,bar so we cannot delete it.

author maintainer
#3 lore raw

PATCH 3/3 conntrack: allow adding/deleting labels from conntrack entries

In reply to Florian Westphal

From: Clemence Faure <[email protected]>

new options "--label-add" and "--label-delete" to alter connlabels assigned to a connection.

---
 conntrack.8         |   10 +++-
 include/conntrack.h |    2 +-
 src/conntrack.c     |  152 ++++++++++++++++++++++++++++++++++++++++++--------
 3 files changed, 137 insertions(+), 27 deletions(-)
conntrack.8 +9 -1
index 6410e5b..6910fc9 100644
@@ -146,9 +146,17 @@ In "--create" mode, the mask is ignored.
 .TP
 .BI "-l, --label " "LABEL"
 Specify a conntrack label.
-This option is only available in conjunction with "-L, --dump" or "-E, --event".
+This option is only available in conjunction with "-L, --dump", "-E, --event", or "-U --update".
 Match entries whose labels match at least those specified.
 Use multiple -l commands to specify multiple labels that need to be set.
+Match entries whose labels matches at least those specified as arguments.
+.BI "--label-add " "LABEL"
+Specify the conntrack labels to add to to the selected conntracks.
+This option is only available in conjunction with "-I, --create" or "-U, --update".
+.BI "--label-del " "[LABEL]"
+Specify the conntrack label to delete from the selected conntracks.
+If no label is given, all labels are deleted.
+This option is only available in conjunction with "-U, --update".
 .TP
 .BI "-c, --secmark " "SECMARK"
 Specify the conntrack selinux security mark.
include/conntrack.h +1 -1
index 6cd9962..c2a0c8f 100644
@@ -10,7 +10,7 @@
 #include <netinet/in.h>
 
 #define NUMBER_OF_CMD   19
-#define NUMBER_OF_OPT   25
+#define NUMBER_OF_OPT   27
 
 struct ctproto_handler {
 	struct list_head 	head;
src/conntrack.c +127 -25

258 lines not on this page · raw message

Signature · 1 line
1.7.8.6