netfilter: nftables: ctnetlink event type set support
Subsystems (2)
Florian Westphal
maintainernftables equivalent for -j CT --ctevents.
Unlike iptables this doesn't work with conntrack templates, it must be used with the real conntrack objects instead (i.e., after conntrack picked the packet up for processing).
patch #1 is for kernel, 2 and 3 for libnftnl/nftables userland.
Florian Westphal
maintainerPATCH 1/3 netfilter: nft_ct: allow to set ctnetlink event types of a connection
by default the kernel emits all ctnetlink events for a connection. This allows to select the types of events to generate for a connection.
This allows to e.g. only send DESTROY events but no NEW/UPDATE ones.
This was already possible via iptables' CT target. The nft version has the advantage that it can also be used with already-established conntracks.
--- include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nft_ct.c | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-)
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -901,6 +901,7 @@ enum nft_rt_attributes {* @NFT_CT_BYTES: conntrack bytes * @NFT_CT_AVGPKT: conntrack average bytes per packet * @NFT_CT_ZONE: conntrack zone +* @NFT_CT_EVENTMASK: ctnetlink events to be generated for this conntrack */ enum nft_ct_keys {NFT_CT_STATE ,
@@ -921,6 +922,7 @@ enum nft_ct_keys {NFT_CT_BYTES ,NFT_CT_AVGPKT ,NFT_CT_ZONE , + NFT_CT_EVENTMASK, };/**
net/netfilter/nft_ct.c +18 -1
@@ -264,7 +264,7 @@ static void nft_ct_set_eval(const struct nft_expr *expr,struct nf_conn *ct; ct =nf_ct_get (skb, &ctinfo); -if (ct ==NULL ) +if (ct ==NULL ||nf_ct_is_template (ct))return ;switch (priv->key) {
@@ -284,6 +284,16 @@ static void nft_ct_set_eval(const struct nft_expr *expr, NF_CT_LABELS_MAX_SIZE / sizeof(u32));break ;#endif +#ifdef CONFIG_NF_CONNTRACK_EVENTS +case NFT_CT_EVENTMASK : { +struct nf_conntrack_ecache *e =nf_ct_ecache_find (ct); +u16 ctmask = (u16 )regs->data[priv->sreg]; + +if (e && e->ctmask != ctmask) + e->ctmask = ctmask; +break ; + } +#endif default :break ; }
@@ -539,6 +549,13 @@ static int nft_ct_set_init(const struct nft_ctx *ctx, len =sizeof (u16);break ;#endif +#ifdef CONFIG_NF_CONNTRACK_EVENTS +case NFT_CT_EVENTMASK : +if (tb[NFTA_CT_DIRECTION ]) +return -EINVAL ; + len =sizeof (u32); +break ; +#endif default :return -EOPNOTSUPP ; }
Signature · 1 line
2.10.2
Florian Westphal
maintainerPATCH 2/3 src: ct eventmask support
In reply to Florian Westphal--- include/linux/netfilter/nf_tables.h | 2 ++ src/expr/ct.c | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-)
include/linux/netfilter/nf_tables.h +2 -0
@@ -901,6 +901,7 @@ enum nft_rt_attributes {* @NFT_CT_BYTES: conntrack bytes * @NFT_CT_AVGPKT: conntrack average bytes per packet * @NFT_CT_ZONE: conntrack zone +* @NFT_CT_EVENTMASK: ctnetlink events to be generated for this conntrack */ enum nft_ct_keys {NFT_CT_STATE ,
@@ -921,6 +922,7 @@ enum nft_ct_keys {NFT_CT_BYTES ,NFT_CT_AVGPKT ,NFT_CT_ZONE , + NFT_CT_EVENTMASK, };/**
src/expr/ct.c +2 -1
@@ -32,7 +32,7 @@ struct nftnl_expr_ct {#define IP_CT_DIR_REPLY 1#ifndef NFT_CT_MAX -#define NFT_CT_MAX (NFT_CT_ZONE + 1) +#define NFT_CT_MAX (NFT_CT_EVENTMASK + 1)#endif static int
@@ -171,6 +171,7 @@ static const char *ctkey2str_array[NFT_CT_MAX] = { [NFT_CT_BYTES] = "bytes", [NFT_CT_AVGPKT] = "avgpkt", [NFT_CT_ZONE] = "zone", + [NFT_CT_EVENTMASK] = "eventmask", };static const char *ctkey2str (uint32_t ctkey)
Signature · 1 line
2.10.2
Florian Westphal
maintainerPATCH 3/3 ct: add conntrack event mask support
In reply to Florian Westphal--- doc/nft.xml | 15 +++++ include/datatype.h | 1 + include/linux/netfilter/nf_conntrack_common.h | 80 ++++++--------------------- include/linux/netfilter/nf_tables.h | 2 + src/ct.c | 30 ++++++++++ tests/py/any/ct.t | 6 ++ tests/py/any/ct.t.payload | 20 +++++++ 7 files changed, 90 insertions(+), 64 deletions(-)
doc/nft.xml +15 -0
@@ -3864,6 +3864,7 @@ ip6 filter output log flags all <command>ct</command> <group choice="req"> <arg>mark</arg> + <arg>eventmask</arg> <arg>label</arg> <arg>zone</arg> </group>
@@ -3894,6 +3895,12 @@ ip6 filter output log flags all </thead> <tbody> <row> + <entry>eventmask</entry> + <entry>conntrack event bits</entry> + <entry>bitmask, integer (32 bit)</entry> + </row> + + <row> <entry>helper</entry> <entry>name of ct helper object to assign to the connection</entry> <entry>quoted string</entry>
@@ -3940,6 +3947,14 @@ table inet raw { } </programlisting> </example> + <example> + <title>restrict events reported by ctnetlink</title> + <programlisting> +ct eventmask set new or related or destroy + </programlisting> + </example> + + </para> </refsect2> <refsect2>
include/datatype.h +1 -0
@@ -83,6 +83,7 @@ enum datatypes {TYPE_ECN ,TYPE_FIB_ADDR ,TYPE_BOOLEAN , +TYPE_CT_EVENTBIT , __TYPE_MAX };#define TYPE_MAX (__TYPE_MAX - 1)
include/linux/netfilter/nf_conntrack_common.h +16 -64
89 lines not on this page · raw message
include/linux/netfilter/nf_tables.h +2 -0
@@ -901,6 +901,7 @@ enum nft_rt_attributes {* @NFT_CT_BYTES: conntrack bytes * @NFT_CT_AVGPKT: conntrack average bytes per packet * @NFT_CT_ZONE: conntrack zone +* @NFT_CT_EVENTMASK: ctnetlink events to be generated for this conntrack */ enum nft_ct_keys {NFT_CT_STATE ,
@@ -921,6 +922,7 @@ enum nft_ct_keys {NFT_CT_BYTES ,NFT_CT_AVGPKT ,NFT_CT_ZONE , + NFT_CT_EVENTMASK, };/**
src/ct.c +30 -0
@@ -100,6 +100,34 @@ static const struct datatype ct_status_type = { .sym_tbl = &ct_status_tbl, }; +static const struct symbol_table ct_events_tbl = { + .base =BASE_HEXADECIMAL , + .symbols = { +SYMBOL ("new" ,1 <<IPCT_NEW ), +SYMBOL ("related" ,1 <<IPCT_RELATED ), +SYMBOL ("destroy" ,1 <<IPCT_DESTROY ), +SYMBOL ("reply" ,1 <<IPCT_REPLY ), +SYMBOL ("assured" ,1 <<IPCT_ASSURED ), +SYMBOL ("protoinfo" ,1 <<IPCT_PROTOINFO ), +SYMBOL ("helper" ,1 <<IPCT_HELPER ), +SYMBOL ("mark" ,1 <<IPCT_MARK ), +SYMBOL ("seqadj" ,1 <<IPCT_SEQADJ ), +SYMBOL ("secmark" ,1 <<IPCT_SECMARK ), +SYMBOL ("label" ,1 <<IPCT_LABEL ), +SYMBOL_LIST_END + }, +}; + +static const struct datatype ct_event_type = { + .type =TYPE_CT_EVENTBIT , + .name ="ct_event" , + .desc ="conntrack event bits" , + .byteorder =BYTEORDER_HOST_ENDIAN , + .size =4 *BITS_PER_BYTE , + .basetype = &bitmask_type, + .sym_tbl = &ct_events_tbl, +}; +static struct symbol_table *ct_label_tbl;#define CT_LABEL_BIT_SIZE 128
@@ -236,6 +264,8 @@ static const struct ct_template ct_templates[] = { BYTEORDER_HOST_ENDIAN, 64), [NFT_CT_ZONE ] =CT_TEMPLATE ("zone" , &integer_type, BYTEORDER_HOST_ENDIAN, 16), + [NFT_CT_EVENTMASK ] =CT_TEMPLATE ("eventmask" , &ct_event_type, + BYTEORDER_HOST_ENDIAN, 32), };static void ct_print (enum nft_ct_keys key,int8_t dir)
tests/py/any/ct.t +6 -0
@@ -96,6 +96,12 @@ ct original mark 42;fail # swapped key and direction ct mark original;fail +ct eventmask set new;ok +ct eventmask set new or related or destroy or foobar;fail +ct eventmask set 'new | related | destroy | label';ok;ct eventmask set new | related | destroy | label +ct eventmask set 1;ok;ct eventmask set new +ct eventmask set 0x0;ok + ct label 127;ok ct label set 127;ok ct label 128;fail
tests/py/any/ct.t.payload +20 -0
@@ -391,6 +391,26 @@ ip test-ip4 output [ bitwise reg 1 = (reg=1 & 0x00000020 ) ^ 0x00000000 ] [ cmp neq reg 1 0x00000000 ] +# ct eventmask set new +ip test-ip4 output + [ immediate reg 1 0x00000001 ] + [ ct set eventmask with reg 1 ] + +# ct eventmask set 'new | related | destroy | label' +ip test-ip4 output + [ immediate reg 1 0x00000407 ] + [ ct set eventmask with reg 1 ] + +# ct eventmask set 1 +ip test-ip4 output + [ immediate reg 1 0x00000001 ] + [ ct set eventmask with reg 1 ] + +# ct eventmask set 0x0 +ip test-ip4 output + [ immediate reg 1 0x00000000 ] + [ ct set eventmask with reg 1 ] + # ct label 127 ip test-ip4 output [ ct load label => reg 1 ]
Signature · 1 line
2.10.2
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next 1/3] netfilter: nft_ct: allow to set ctnetlink event types of a connection
In reply to Florian WestphalQuoted 40 lines · Florian Westphal
by default the kernel emits all ctnetlink events for a connection. This allows to select the types of events to generate for a connection.
This allows to e.g. only send DESTROY events but no NEW/UPDATE ones.
This was already possible via iptables' CT target. The nft version has the advantage that it can also be used with already-established conntracks.
Signed-off-by Florian Westphal <[email protected]>--- include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nft_ct.c | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-)include/uapi/linux/netfilter/nf_tables.h +2
-0@@ -901,6 +901,7 @@ enum nft_rt_attributes {* @NFT_CT_BYTES: conntrack bytes * @NFT_CT_AVGPKT: conntrack average bytes per packet * @NFT_CT_ZONE: conntrack zone +* @NFT_CT_EVENTMASK: ctnetlink events to be generated for this conntrack */ enum nft_ct_keys {NFT_CT_STATE ,@@ -921,6 +922,7 @@ enum nft_ct_keys {NFT_CT_BYTES ,NFT_CT_AVGPKT ,NFT_CT_ZONE , + NFT_CT_EVENTMASK, };/** net/netfilter/nft_ct.c +0
-0
net/netfilter/nft_ct.c static void nft_ct_set_eval(const struct nft_expr *expr, · line 267
Quoted 5 lines
net/netfilter/nft_ct.c +1
-1@@ -264,7 +264,7 @@ static void nft_ct_set_eval(const struct nft_expr *expr,struct nf_conn *ct; ct =nf_ct_get (skb, &ctinfo); -if (ct ==NULL ) +if (ct ==NULL ||nf_ct_is_template (ct))
I wonder if this should go in a oneliner, given this is fixing the fact that we may end up using the template. So someone has a chance to pass it to -stable. I'll be fine either way, no problem.
Another comment below.
net/netfilter/nft_ct.c static void nft_ct_set_eval(const struct nft_expr *expr, · line 290
Quoted 11 lines
net/netfilter/nft_ct.c +4
-0return ;switch (priv->key) {@@ -284,6 +284,16 @@ static void nft_ct_set_eval(const struct nft_expr *expr, NF_CT_LABELS_MAX_SIZE / sizeof(u32));break ;#endif +#ifdef CONFIG_NF_CONNTRACK_EVENTS +case NFT_CT_EVENTMASK : { +struct nf_conntrack_ecache *e =nf_ct_ecache_find (ct); +u16 ctmask = (u16 )regs->data[priv->sreg];
Liping added helpers to fetch data from registers, I think it applies to this case too.
Thanks!
Florian Westphal
maintainerRe: [PATCH nf-next 1/3] netfilter: nft_ct: allow to set ctnetlink event types of a connection
Quoted 3 lines · Pablo Neira Ayuso
I wonder if this should go in a oneliner, given this is fixing the fact that we may end up using the template. So someone has a chance to pass it to -stable. I'll be fine either way, no problem.
Ok. will do.
Quoted 7 lines
net/netfilter/nft_ct.c static void nft_ct_set_eval(const struct nft_expr *expr, · line 290
Quoted 4 lines
net/netfilter/nft_ct.c +4
-0+#ifdef CONFIG_NF_CONNTRACK_EVENTS +case NFT_CT_EVENTMASK : { +struct nf_conntrack_ecache *e =nf_ct_ecache_find (ct); +u16 ctmask = (u16 )regs->data[priv->sreg];Liping added helpers to fetch data from registers, I think it applies to this case too.
Right, I forgot about this, thanks for noticing. Having such huge backlog is crap, I won't do this again, ever :(
Pablo Neira Ayuso
maintainerRe: [PATCH nf-next 1/3] netfilter: nft_ct: allow to set ctnetlink event types of a connection
Quoted 6 lines · Florian Westphal
Quoted 3 lines · Pablo Neira Ayuso
I wonder if this should go in a oneliner, given this is fixing the fact that we may end up using the template. So someone has a chance to pass it to -stable. I'll be fine either way, no problem.
Ok. will do.
Thanks Florian.
Quoted 10 lines
Quoted 7 lines
net/netfilter/nft_ct.c static void nft_ct_set_eval(const struct nft_expr *expr, · line 290
Quoted 4 lines
net/netfilter/nft_ct.c +4
-0+#ifdef CONFIG_NF_CONNTRACK_EVENTS +case NFT_CT_EVENTMASK : { +struct nf_conntrack_ecache *e =nf_ct_ecache_find (ct); +u16 ctmask = (u16 )regs->data[priv->sreg];Liping added helpers to fetch data from registers, I think it applies to this case too.
Right, I forgot about this, thanks for noticing. Having such huge backlog is crap, I won't do this again, ever :(
I tend to repeat this to myself... but sometimes it doesn't seem to work ;)