Skip to contentSkip to conversation

netfilter: nft_ct: labels get support

PATCH Superseded Florian Westphal 2 participants 7 messages Feb 18, 2014

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/3 netfilter: nft_ct: labels get support

Takes advantage of the fact that the number of labels is currently restricted to 2**128, ie. the extension area will always fit into nft register.

Patrick says the kernel registers need to be changed anyway to deal with concatentations so we will probably not run into issues when the number of labels increases in a future kernel release.

---
 include/uapi/linux/netfilter/nf_tables.h |  1 +
 net/netfilter/nft_ct.c                   | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +1 -0
index 83c985a..c84c452 100644
@@ -601,6 +601,7 @@ enum nft_ct_keys {
 	NFT_CT_PROTOCOL,
 	NFT_CT_PROTO_SRC,
 	NFT_CT_PROTO_DST,
+	NFT_CT_LABELS,
 };
 
 /**
net/netfilter/nft_ct.c +22 -0
index 917052e..cb0ffd2 100644
@@ -19,6 +19,7 @@
 #include <net/netfilter/nf_conntrack_tuple.h>
 #include <net/netfilter/nf_conntrack_helper.h>
 #include <net/netfilter/nf_conntrack_ecache.h>
+#include <net/netfilter/nf_conntrack_labels.h>
 
 struct nft_ct {
 	enum nft_ct_keys	key:8;
@@ -97,6 +98,24 @@ static void nft_ct_get_eval(const struct nft_expr *expr,
 			goto err;
 		strncpy((char *)dest->data, helper->name, sizeof(dest->data));
 		return;
+#ifdef CONFIG_NF_CONNTRACK_LABELS
+	case NFT_CT_LABELS: {
+		struct nf_conn_labels *labels = nf_ct_labels_find(ct);
+		unsigned int size;
+
+		if (!labels)
+			goto err;
+		size = labels->words * sizeof(long);
+		if (size > sizeof(dest->data))
+			goto err;
+		memcpy(dest->data, labels->bits, size);
+
+		if (size < sizeof(dest->data))
+			memset(((char *) dest->data) + size, 0,
+			       sizeof(dest->data) - size);
+		return;
+	}
+#endif
 	}
 
 	tuple = &ct->tuplehash[priv->dir].tuple;
@@ -221,6 +240,9 @@ static int nft_ct_init_validate_get(const struct nft_expr *expr,
 #ifdef CONFIG_NF_CONNTRACK_SECMARK
 	case NFT_CT_SECMARK:
 #endif
+#ifdef CONFIG_NF_CONNTRACK_LABELS
+	case NFT_CT_LABELS:
+#endif
 	case NFT_CT_EXPIRATION:
 	case NFT_CT_HELPER:
 		if (tb[NFTA_CT_DIRECTION] != NULL)
Signature · 1 line
1.8.1.5
author maintainer
#1 lore raw

PATCH 2/3 expr: add conntrack label match support

---
 include/linux/netfilter/nf_tables.h | 2 ++
 src/expr/ct.c                       | 5 +++--
 2 files changed, 5 insertions(+), 2 deletions(-)
include/linux/netfilter/nf_tables.h +2 -0
index 31ddd06..5bb7b72 100644
@@ -585,6 +585,7 @@ enum nft_meta_attributes {
  * @NFT_CT_PROTOCOL: conntrack layer 4 protocol
  * @NFT_CT_PROTO_SRC: conntrack layer 4 protocol source
  * @NFT_CT_PROTO_DST: conntrack layer 4 protocol destination
+ * @NFT_CT_LABEL: conntrack label
  */
 enum nft_ct_keys {
 	NFT_CT_STATE,
@@ -600,6 +601,7 @@ enum nft_ct_keys {
 	NFT_CT_PROTOCOL,
 	NFT_CT_PROTO_SRC,
 	NFT_CT_PROTO_DST,
+	NFT_CT_LABEL,
 };
 
 /**
src/expr/ct.c +3 -2
index 2df761c..4c18dde 100644
@@ -33,7 +33,7 @@ struct nft_expr_ct {
 #define IP_CT_DIR_REPLY		1
 
 #ifndef NFT_CT_MAX
-#define NFT_CT_MAX (NFT_CT_PROTO_DST + 1)
+#define NFT_CT_MAX (NFT_CT_LABEL + 1)
 #endif
 
 static int
@@ -170,7 +170,8 @@ const char *ctkey2str_array[NFT_CT_MAX] = {
 	[NFT_CT_SRC]		= "src",
 	[NFT_CT_DST]		= "dst",
 	[NFT_CT_PROTO_SRC]	= "proto_src",
-	[NFT_CT_PROTO_DST]	= "proto_dst"
+	[NFT_CT_PROTO_DST]	= "proto_dst",
+	[NFT_CT_LABEL]		= "label",
 };
 
 static const char *ctkey2str(uint32_t ctkey)
Signature · 1 line
1.8.1.5
author maintainer
#2 lore raw

PATCH 3/3 ct: connlabel matching support

In reply to Florian Westphal

Takes advantage of the fact that the current maximum label storage area is 128 bits, i.e. the dynamically allocated extension area in the kernel will always fit into a nft register.

Currently this re-uses rt_symbol_table_init() to read connlabel.conf. This works since the format is pretty much the same.

---
Changes since RFC:
- print function should only output one single label
- use singular ('label', not 'labels')
- use extra __init function to init label symtable

 include/datatype.h                  |  2 ++
 include/linux/netfilter/nf_tables.h |  2 ++
 src/ct.c                            | 72 +++++++++++++++++++++++++++++++++++++
 src/parser.y                        |  2 ++
 src/scanner.l                       |  1 +
 5 files changed, 79 insertions(+)
include/datatype.h +2 -0
index 9e609cf..2c66e9d 100644
@@ -34,6 +34,7 @@
  * @TYPE_CT_DIR:	conntrack direction
  * @TYPE_CT_STATUS:	conntrack status (bitmask subtype)
  * @TYPE_ICMP6_TYPE:	ICMPv6 type codes (integer subtype)
+ * @TYPE_CT_LABEL:	Conntrack Label (bitmask subtype)
  */
 enum datatypes {
 	TYPE_INVALID,
@@ -66,6 +67,7 @@ enum datatypes {
 	TYPE_CT_DIR,
 	TYPE_CT_STATUS,
 	TYPE_ICMP6_TYPE,
+	TYPE_CT_LABEL,
 	__TYPE_MAX
 };
 #define TYPE_MAX		(__TYPE_MAX - 1)
include/linux/netfilter/nf_tables.h +2 -0
index 448593c..ff9b0a7 100644
@@ -586,6 +586,7 @@ enum nft_meta_attributes {
  * @NFT_CT_PROTOCOL: conntrack layer 4 protocol
  * @NFT_CT_PROTO_SRC: conntrack layer 4 protocol source
  * @NFT_CT_PROTO_DST: conntrack layer 4 protocol destination
+ * @NFT_CT_LABELS: conntrack label bitset (stored in conntrack extension)
  */
 enum nft_ct_keys {
 	NFT_CT_STATE,
@@ -601,6 +602,7 @@ enum nft_ct_keys {
 	NFT_CT_PROTOCOL,
 	NFT_CT_PROTO_SRC,
 	NFT_CT_PROTO_DST,
+	NFT_CT_LABEL,
 };
 
 /**
src/ct.c +72 -0

92 lines not on this page · raw message

src/parser.y +2 -0
index 07613e2..b3acc74 100644
@@ -314,6 +314,7 @@ static void location_update(struct location *loc, struct location *rhs, int n)
 %token L3PROTOCOL		"l3proto"
 %token PROTO_SRC		"proto-src"
 %token PROTO_DST		"proto-dst"
+%token LABEL			"label"
 
 %token COUNTER			"counter"
 %token PACKETS			"packets"
@@ -1551,6 +1552,7 @@ ct_key			:	STATE		{ $$ = NFT_CT_STATE; }
 			|	PROTOCOL	{ $$ = NFT_CT_PROTOCOL; }
 			|	PROTO_SRC	{ $$ = NFT_CT_PROTO_SRC; }
 			|	PROTO_DST	{ $$ = NFT_CT_PROTO_DST; }
+			|	LABEL		{ $$ = NFT_CT_LABEL; }
 			;
 
 payload_expr		:	payload_raw_expr
src/scanner.l +1 -0
index e4cb398..45c6476 100644
@@ -411,6 +411,7 @@ addrstring	({macaddr}|{ip4addr}|{ip6addr})
 "l3proto"		{ return L3PROTOCOL; }
 "proto-src"		{ return PROTO_SRC; }
 "proto-dst"		{ return PROTO_DST; }
+"label"			{ return LABEL; }
 
 "xml"			{ return XML; }
 "json"			{ return JSON; }
Signature · 1 line
1.8.1.5
#3 lore raw

Re: [PATCH 3/3 nft] ct: connlabel matching support

Quoted 7 lines · Florian Westphal

Takes advantage of the fact that the current maximum label storage area is 128 bits, i.e. the dynamically allocated extension area in the kernel will always fit into a nft register.

Currently this re-uses rt_symbol_table_init() to read connlabel.conf. This works since the format is pretty much the same.

Quoted 6 lines

---
Changes since RFC:
- print function should only output one single label
- use singular ('label', not 'labels')
- use extra __init function to init label symtable

Looks very good to me.

#4 lore raw
In reply to Florian Westphal
Quoted 8 lines · Florian Westphal

Takes advantage of the fact that the number of labels is currently restricted to 2**128, ie. the extension area will always fit into nft register.

Patrick says the kernel registers need to be changed anyway to deal with concatentations so we will probably not run into issues when the number of labels increases in a future kernel release.

net/netfilter/nft_ct.c static void nft_ct_get_eval(const struct nft_expr *expr, · line 107

Quoted 7 lines
net/netfilter/nft_ct.c +7 -0
+#ifdef CONFIG_NF_CONNTRACK_LABELS
+	case NFT_CT_LABELS: {
+		struct nf_conn_labels *labels = nf_ct_labels_find(ct);
+		unsigned int size;
+
+		if (!labels)
+			goto err;

Is that really an error? I'd expect it to be equivalent with "no labels set", which can also be matched on.

net/netfilter/nft_ct.c static void nft_ct_get_eval(const struct nft_expr *expr, · line 110

Quoted 3 lines
net/netfilter/nft_ct.c +3 -0
+		size = labels->words * sizeof(long);
+		if (size > sizeof(dest->data))
+			goto err;

Can't we check that during ->init() if the number is limited anyway?

Quoted 8 lines
net/netfilter/nft_ct.c +8 -0
+		memcpy(dest->data, labels->bits, size);
+
+		if (size < sizeof(dest->data))
+			memset(((char *) dest->data) + size, 0,
+			       sizeof(dest->data) - size);
+		return;
+	}
+#endif
author maintainer
#5 lore raw
Quoted 9 lines · Patrick McHardy
Quoted 8 lines · Florian Westphal

Takes advantage of the fact that the number of labels is currently restricted to 2**128, ie. the extension area will always fit into nft register.

Patrick says the kernel registers need to be changed anyway to deal with concatentations so we will probably not run into issues when the number of labels increases in a future kernel release.

Quoted 10 lines

net/netfilter/nft_ct.c static void nft_ct_get_eval(const struct nft_expr *expr, · line 107

Quoted 7 lines
net/netfilter/nft_ct.c +7 -0
+#ifdef CONFIG_NF_CONNTRACK_LABELS
+	case NFT_CT_LABELS: {
+		struct nf_conn_labels *labels = nf_ct_labels_find(ct);
+		unsigned int size;
+
+		if (!labels)
+			goto err;

Is that really an error? I'd expect it to be equivalent with "no labels set", which can also be matched on.

I think you're right. I'll change it.

Quoted 5 lines

net/netfilter/nft_ct.c static void nft_ct_get_eval(const struct nft_expr *expr, · line 110

Quoted 3 lines
net/netfilter/nft_ct.c +3 -0
+		size = labels->words * sizeof(long);
+		if (size > sizeof(dest->data))
+			goto err;

Can't we check that during ->init() if the number is limited anyway?

True.

Thanks Patrick.

I'll wait a bit for more feedback and repost the kernel patch with these changes incorporated.

#6 lore raw
Quoted 23 lines · Florian Westphal
Quoted 9 lines · Patrick McHardy
Quoted 8 lines · Florian Westphal

Takes advantage of the fact that the number of labels is currently restricted to 2**128, ie. the extension area will always fit into nft register.

Patrick says the kernel registers need to be changed anyway to deal with concatentations so we will probably not run into issues when the number of labels increases in a future kernel release.

Quoted 10 lines

net/netfilter/nft_ct.c static void nft_ct_get_eval(const struct nft_expr *expr, · line 107

Quoted 7 lines
net/netfilter/nft_ct.c +7 -0
+#ifdef CONFIG_NF_CONNTRACK_LABELS
+	case NFT_CT_LABELS: {
+		struct nf_conn_labels *labels = nf_ct_labels_find(ct);
+		unsigned int size;
+
+		if (!labels)
+			goto err;

Is that really an error? I'd expect it to be equivalent with "no labels set", which can also be matched on.

I think you're right. I'll change it.

Quoted 7 lines
Quoted 5 lines

net/netfilter/nft_ct.c static void nft_ct_get_eval(const struct nft_expr *expr, · line 110

Quoted 3 lines
net/netfilter/nft_ct.c +3 -0
+		size = labels->words * sizeof(long);
+		if (size > sizeof(dest->data))
+			goto err;

Can't we check that during ->init() if the number is limited anyway?

True.

Quick follow up: BUILD_BUG_ON() is probably the best choice. I couldn't find the limit though.

Quoted 4 lines

Thanks Patrick.

I'll wait a bit for more feedback and repost the kernel patch with these changes incorporated.