netfilter: nft_ct: labels get support
Subsystems (2)
Florian Westphal
maintainerPATCH 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
@@ -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
@@ -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
Florian Westphal
maintainerPATCH 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
@@ -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
@@ -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
Florian Westphal
maintainerPATCH 3/3 ct: connlabel matching support
In reply to Florian WestphalTakes 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
@@ -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
@@ -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
@@ -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
@@ -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
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
Signed-off-by Florian Westphal <[email protected]>---
Changes since RFC:
- print function should only output one single label
- use singular ('label', not 'labels')
- use extra__initfunction to init label symtable
Looks very good to me.
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
Florian Westphal
maintainerQuoted 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.
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.