ct_set: port CT target to nftables
Subsystems (2)
cover ct_set: port CT target to nftables
Hello,
Here's a patchset implementing CT features into a nftables modules named ct_set. I've used same technique as my previous work by factoring code in first patch and introducing the new module in second one.
libnftables implementation is done and will be attached to this thread.
I will work on nft support in the coming days.
Patchset statistics:
include/net/netfilter/nf_ct_set.h | 137 ++++++++++++++++ include/uapi/linux/netfilter/nf_tables.h | 36 +++++ net/netfilter/Kconfig | 5 + net/netfilter/Makefile | 1 + net/netfilter/nft_ct_set.c | 262 +++++++++++++++++++++++++++++++ net/netfilter/xt_CT.c | 143 +---------------- 6 files changed, 449 insertions(+), 135 deletions(-)
BR,
--
Eric Leblond
PATCH 1/2 netfilter: CT: factorize reusable code
--- include/net/netfilter/nf_ct_set.h | 137 ++++++++++++++++++++++++++++++++++++ net/netfilter/xt_CT.c | 143 +++----------------------------------- 2 files changed, 145 insertions(+), 135 deletions(-) create mode 100644 include/net/netfilter/nf_ct_set.h
include/net/netfilter/nf_ct_set.h new file +137 -0
@@ -0,0 +1,137 @@ +/* +* Copyright (c) 2014 Eric Leblond <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License version 2 as +* published by the Free Software Foundation. +* +*/ + +#include <net/netfilter/nf_conntrack_l4proto.h> +#include <net/netfilter/nf_conntrack_helper.h> +#include <net/netfilter/nf_conntrack_ecache.h> +#include <net/netfilter/nf_conntrack_timeout.h> +#include <net/netfilter/nf_conntrack_zones.h> + +static int +nf_ct_set_helper (struct nf_conn *ct,const char *helper_name,u8 proto, +u8 family) +{ +struct nf_conntrack_helper *helper; +struct nf_conn_help *help; + +if (!proto) { +pr_info ("You must specify a L4 protocol, and not use " +"inversions on it.\n" ); +return -ENOENT ; + } + + helper =nf_conntrack_helper_try_module_get (helper_name, family, + proto); +if (helper ==NULL ) { +pr_info ("No such helper \"%s\"\n" , helper_name); +return -ENOENT ; + } + + help =nf_ct_helper_ext_add (ct, helper,GFP_KERNEL ); +if (help ==NULL ) { +module_put (helper->me); +return -ENOMEM ; + } + + help->helper = helper; +return 0 ; +} + +#ifdef CONFIG_NF_CONNTRACK_TIMEOUT +static void __nf_ct_tg_timeout_put (struct ctnl_timeout *timeout) +{ +typeof (nf_ct_timeout_put_hook) timeout_put; + + timeout_put =rcu_dereference (nf_ct_timeout_put_hook); +if (timeout_put) +timeout_put (timeout); +} +#endif + +static int +nf_ct_set_timeout (struct nf_conn *ct,u8 proto,u8 family, +const char *timeout_name) +{ +#ifdef CONFIG_NF_CONNTRACK_TIMEOUT +typeof (nf_ct_timeout_find_get_hook) timeout_find_get; +struct ctnl_timeout *timeout; +struct nf_conn_timeout *timeout_ext; +struct nf_conntrack_l4proto *l4proto; +int ret =0 ; + +rcu_read_lock (); + timeout_find_get =rcu_dereference (nf_ct_timeout_find_get_hook); +if (timeout_find_get ==NULL ) { + ret = -ENOENT ; +pr_info ("Timeout policy base is empty\n" ); + goto out; + } + +if (!proto) { + ret = -EINVAL ; +pr_info ("You must specify a L4 protocol, and not use " +"inversions on it.\n" ); + goto out; + } + + timeout =timeout_find_get (timeout_name); +if (timeout ==NULL ) { + ret = -ENOENT ; +pr_info ("No such timeout policy \"%s\"\n" , timeout_name); + goto out; + } + +if (timeout->l3num != family) { + ret = -EINVAL ; +pr_info ("Timeout policy `%s' can only be used by L3 protocol " +"number %d\n" , timeout_name, timeout->l3num); + goto err_put_timeout; + } +/* Make sure the timeout policy matches any existing protocol tracker, +* otherwise default to generic. +*/ + l4proto =__nf_ct_l4proto_find (family, proto); +if (timeout->l4proto->l4proto != l4proto->l4proto) { + ret = -EINVAL ; +pr_info ("Timeout policy `%s' can only be used by L4 protocol " +"number %d\n" , + timeout_name, timeout->l4proto->l4proto); + goto err_put_timeout; + } + timeout_ext =nf_ct_timeout_ext_add (ct, timeout,GFP_ATOMIC ); +if (timeout_ext ==NULL ) + ret = -ENOMEM ; + +err_put_timeout: +__nf_ct_tg_timeout_put (timeout); +out: +rcu_read_unlock (); +return ret; +#else +return -EOPNOTSUPP ; +#endif +} + +static void nf_ct_destroy_timeout (struct nf_conn *ct) +{ +#ifdef CONFIG_NF_CONNTRACK_TIMEOUT +struct nf_conn_timeout *timeout_ext; +typeof (nf_ct_timeout_put_hook) timeout_put; + +rcu_read_lock (); + timeout_put =rcu_dereference (nf_ct_timeout_put_hook); + +if (timeout_put) { + timeout_ext =nf_ct_timeout_find (ct); +if (timeout_ext) +timeout_put (timeout_ext->timeout); + } +rcu_read_unlock (); +#endif +}
net/netfilter/xt_CT.c +8 -135
184 lines not on this page · raw message
Signature · 1 line
1.8.5.2
PATCH 2/2 netfilter: nftables: introduce ct_set module
In reply to Eric LeblondThis module is a port of iptables CT target. It allows user to setup connection tracking for selected packets.
The context is not containing information about layer 4 so it is necessary to ask to userspace explicitely for which layer 4 protocol the connection template has to be created. This is the reason why the NFTA_CT_SET_PROTO field has been introduced.
--- include/uapi/linux/netfilter/nf_tables.h | 36 +++++ net/netfilter/Kconfig | 5 + net/netfilter/Makefile | 1 + net/netfilter/nft_ct_set.c | 262 +++++++++++++++++++++++++++++++ 4 files changed, 304 insertions(+) create mode 100644 net/netfilter/nft_ct_set.c
include/uapi/linux/netfilter/nf_tables.h +36 -0
@@ -707,6 +707,42 @@ enum nft_reject_attributes {#define NFTA_REJECT_MAX (__NFTA_REJECT_MAX - 1)/** +* enum nft_ct_set_attributes - nf_tables ct_set expression netlink attributes +* +* @NFTA_CT_SET_PROTO: layer 4 protocol +* @NFTA_CT_SET_FLAGS: flags for connection as NOTRACK for example +* @NFTA_CT_SET_HELPER: protocol helper to link with matching connections +* @NFTA_CT_SET_CTEVENTS: only generate the specified conntrack events +* @NFTA_CT_SET_EXPEVENTS: only generate the specified expectation events +* @NFTA_CT_SET_ZONEID: assign packet to sone id +* @NFTA_CT_SET_TIMEOUT: use timeout policy for the connection +* +*/ +enum nft_ct_set_attributes { +NFTA_CT_SET_UNSPEC , +NFTA_CT_SET_PROTO , +NFTA_CT_SET_FLAGS , +NFTA_CT_SET_HELPER , +NFTA_CT_SET_CTEVENTS , +NFTA_CT_SET_EXPEVENTS , +NFTA_CT_SET_ZONEID , +NFTA_CT_SET_TIMEOUT , + __NFTA_CT_SET_MAX +}; +#define NFTA_CT_SET_MAX (__NFTA_CT_SET_MAX - 1) + +/** +* enum nft_ct_set_flags - nf_tables ct_set flags +* +* @NFT_CT_SET_F_NOTRACK: do not track this connection +*/ +enum nft_ct_set_flags { + +enum nft_ct_set_flags { +NFT_CT_SET_F_NOTRACK =0x1 , +}; + +/** * enum nft_nat_types - nf_tables nat expression NAT types * * @NFT_NAT_SNAT: source NAT
net/netfilter/Kconfig +5 -0
@@ -430,6 +430,11 @@ config NFT_CT depends on NF_CONNTRACK tristate "Netfilter nf_tables conntrack module" +config NFT_CT_SET + depends on NF_TABLES + depends on NF_CONNTRACK + tristate "Netfilter nf_tables conntrack setup module" + config NFT_RBTREE depends on NF_TABLES tristate "Netfilter nf_tables rbtree set module"
net/netfilter/Makefile +1 -0
@@ -74,6 +74,7 @@ obj-$(CONFIG_NFT_COMPAT) += nft_compat.o obj-$(CONFIG_NFT_EXTHDR) += nft_exthdr.o obj-$(CONFIG_NFT_META) += nft_meta.o obj-$(CONFIG_NFT_CT) += nft_ct.o +obj-$(CONFIG_NFT_CT_SET) += nft_ct_set.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o obj-$(CONFIG_NFT_NAT) += nft_nat.o obj-$(CONFIG_NFT_QUEUE) += nft_queue.o
net/netfilter/nft_ct_set.c new file +262 -0
262 lines not on this page · raw message
Signature · 1 line
1.8.5.2
PATCH 1/1 expr: add ct_set expression
In reply to Eric LeblondThis patch adds support for 'ct_set' expression which is a port of iptables CT target.
--- include/libnftables/expr.h | 11 ++ include/linux/netfilter/nf_tables.h | 35 ++++ src/Makefile.am | 1 + src/expr/ct_set.c | 373 ++++++++++++++++++++++++++++++++++++ 4 files changed, 420 insertions(+) create mode 100644 src/expr/ct_set.c
include/libnftables/expr.h +11 -0
@@ -149,6 +149,17 @@ enum { NFT_EXPR_QUEUE_TOTAL, NFT_EXPR_QUEUE_FLAGS, }; + +enum { +NFT_EXPR_CT_SET_FLAGS =NFT_RULE_EXPR_ATTR_BASE , +NFT_EXPR_CT_SET_PROTO , +NFT_EXPR_CT_SET_ZONEID , +NFT_EXPR_CT_SET_CTEVENTS , +NFT_EXPR_CT_SET_EXPEVENTS , +NFT_EXPR_CT_SET_HELPER , +NFT_EXPR_CT_SET_TIMEOUT , +}; +#ifdef __cplusplus } /* extern "C" */#endif
include/linux/netfilter/nf_tables.h +35 -0
@@ -628,6 +628,41 @@ enum nft_reject_attributes {#define NFTA_REJECT_MAX (__NFTA_REJECT_MAX - 1)/** +* enum nft_ct_set_attributes - nf_tables ct_set expression netlink attributes +* +* @NFTA_CT_SET_PROTO: layer 4 protocol +* @NFTA_CT_SET_FLAGS: flags for connection as NOTRACK for example +* @NFTA_CT_SET_HELPER: protocol helper to link with matching connections +* @NFTA_CT_SET_CTEVENTS: only generate the specified conntrack events +* @NFTA_CT_SET_EXPEVENTS: only generate the specified expectation events +* @NFTA_CT_SET_ZONEID: assign packet to sone id +* @NFTA_CT_SET_TIMEOUT: use timeout policy for the connection +* +*/ +enum nft_ct_set_attributes { +NFTA_CT_SET_UNSPEC , +NFTA_CT_SET_PROTO , +NFTA_CT_SET_FLAGS , +NFTA_CT_SET_HELPER , +NFTA_CT_SET_CTEVENTS , +NFTA_CT_SET_EXPEVENTS , +NFTA_CT_SET_ZONEID , +NFTA_CT_SET_TIMEOUT , + __NFTA_CT_SET_MAX +}; +#define NFTA_CT_SET_MAX (__NFTA_CT_SET_MAX - 1) + +/** +* enum nft_ct_set_flags - nf_tables ct_set flags +* +* @NFT_CT_SET_F_NOTRACK: do not track this connection +*/ +enum nft_ct_set_flags { +NFT_CT_SET_F_NOTRACK =0x1 , +}; + + +/** * enum nft_nat_types - nf_tables nat expression NAT types * * @NFT_NAT_SNAT: source NAT
src/Makefile.am +1 -0
@@ -21,6 +21,7 @@ libnftables_la_SOURCES = utils.c \ expr/cmp.c \ expr/counter.c \ expr/ct.c \ + expr/ct_set.c \ expr/data_reg.c \ expr/exthdr.c \ expr/limit.c \
src/expr/ct_set.c new file +373 -0
373 lines not on this page · raw message
Signature · 1 line
1.8.5.2
Re: [libnftables PATCH] expr: add ct_set expression
Quoted 4 lines · Eric Leblond
This patch adds support for '
ct_set' expression which is a port of iptables CT target.Signed-off-by Eric Leblond <[email protected]>
Hi Eric, this patch looks good. Some comments below:
Quoted 17 lines
src/expr/ct_set.c new file +17
-0+static const void * +nft_rule_expr_ct_set_get (const struct nft_rule_expr *e,uint16_t type, +uint32_t *data_len) +{ +struct nft_expr_ct_set *ct_set =nft_expr_data (e); + +switch (type) { +case NFT_EXPR_CT_SET_FLAGS : + *data_len =sizeof (ct_set->flags); +return &ct_set->flags; +case NFT_EXPR_CT_SET_PROTO : + *data_len =sizeof (ct_set->proto); +return &ct_set->proto; +case NFT_EXPR_CT_SET_ZONEID : + *data_len =sizeof (ct_set->zoneid); +return &ct_set->zoneid; +case NFT_EXPR_CT_SET_CTEVENTS :
src/expr/ct_set.c line 100
Quoted 10 lines
src/expr/ct_set.c new file +10
-0+ *data_len =sizeof (ct_set->ct_events); +return &ct_set->ct_events; +case NFT_EXPR_CT_SET_EXPEVENTS : + *data_len =sizeof (ct_set->exp_events); +return &ct_set->exp_events; +case NFT_EXPR_CT_SET_HELPER : + *data_len =strlen (ct_set->helper)+1 ; +return ct_set->helper; +case NFT_EXPR_CT_SET_TIMEOUT : + *data_len =strlen (ct_set->timeout)+1 ;
Why +1 here?
Others expressions, like 'log' or 'lookup' simply sets data_len with strlen().
I guess either this or the others aren't good, don't you?
src/expr/ct_set.c line 262
Quoted 10 lines
src/expr/ct_set.c new file +10
-0+ +static int nft_rule_expr_ct_set_xml_parse (struct nft_rule_expr *e,mxml_node_t *tree) +{ +#ifdef XML_PARSING +struct nft_expr_ct_set *ct_set =nft_expr_data (e); +const char *uvalstr; + +if (nft_mxml_num_parse (tree,"flags" ,MXML_DESCEND_FIRST ,BASE_DEC , + &ct_set->flags,NFT_TYPE_U16 ,NFT_XML_MAND ) !=0 ) +return -1 ;
I see now that errno is not set anywhere in the error path of XML/JSON expression parsing code.
I will patch nft_mxml_expr_parse() and nft_jansson_expr_parse().
regards
Signature · 5 lines
Arturo Borrero González -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [libnftables PATCH] expr: add ct_set expression
Hello,
Quoted 30 lines · Arturo Borrero Gonzalez
Quoted 4 lines · Eric Leblond
This patch adds support for '
ct_set' expression which is a port of iptables CT target.Signed-off-by Eric Leblond <[email protected]>Hi Eric, this patch looks good. Some comments below:
Quoted 22 lines
src/expr/ct_set.c new file +22
-0+static const void * +nft_rule_expr_ct_set_get (const struct nft_rule_expr *e,uint16_t type, +uint32_t *data_len) +{ +struct nft_expr_ct_set *ct_set =nft_expr_data (e); + +switch (type) { +case NFT_EXPR_CT_SET_FLAGS : + *data_len =sizeof (ct_set->flags); +return &ct_set->flags; +case NFT_EXPR_CT_SET_PROTO : + *data_len =sizeof (ct_set->proto); +return &ct_set->proto; +case NFT_EXPR_CT_SET_ZONEID : + *data_len =sizeof (ct_set->zoneid); +return &ct_set->zoneid; +case NFT_EXPR_CT_SET_CTEVENTS : + *data_len =sizeof (ct_set->ct_events); +return &ct_set->ct_events; +case NFT_EXPR_CT_SET_EXPEVENTS : + *data_len =sizeof (ct_set->exp_events); +return &ct_set->exp_events;
Quoted 8 lines
src/expr/ct_set.c line 100
Quoted 5 lines
src/expr/ct_set.c new file +5
-0+case NFT_EXPR_CT_SET_HELPER : + *data_len =strlen (ct_set->helper)+1 ; +return ct_set->helper; +case NFT_EXPR_CT_SET_TIMEOUT : + *data_len =strlen (ct_set->timeout)+1 ;Why +1 here?
Others expressions, like 'log' or 'lookup' simply setsdata_lenwithstrlen().
'log' is is using a char * and in length computation it is using +1 to get the terminal 0 at the end of the string:
*data_len = strlen(log->prefix)+1; return log->prefix;
and 'lookup' is using a direct access. I think it is because we have:
char set_name[IFNAMSIZ];
and in setter function:
memcpy(lookup->set_name, data, IFNAMSIZ); lookup->set_name[IFNAMSIZ-1] = '\0';
So doing the following is ok in the getter function. We are sure we will have a terminal 0 and we know the size of data:
case NFT_EXPR_LOOKUP_SET: return lookup->set_name;
Quoted 1 line
I guess either this or the others aren't good, don't you?
No ;)
Quoted 15 lines
src/expr/ct_set.c line 262
Quoted 11 lines
src/expr/ct_set.c new file +10
-0+ +static int nft_rule_expr_ct_set_xml_parse (struct nft_rule_expr *e,mxml_node_t *tree) +{ +#ifdef XML_PARSING +struct nft_expr_ct_set *ct_set =nft_expr_data (e); +const char *uvalstr; + +if (nft_mxml_num_parse (tree,"flags" ,MXML_DESCEND_FIRST ,BASE_DEC , + &ct_set->flags,NFT_TYPE_U16 ,NFT_XML_MAND ) !=0 ) +return -1 ;I see now that errno is not set anywhere in the error path of XML/JSON expression parsing code.
Quoted 1 line
I will patch
nft_mxml_expr_parse()andnft_jansson_expr_parse().
Good catch!
BR,
Signature · 1 line
Eric Leblond <[email protected]>
Re: [libnftables PATCH] expr: add ct_set expression
Quoted 4 lines · Eric Leblond
Quoted 1 line
I guess either this or the others aren't good, don't you?
No ;)
Ok!
Quoted 5 lines
Quoted 2 lines
I will patch
nft_mxml_expr_parse()andnft_jansson_expr_parse().Good catch!
False-positive, they do set errno through concrete helpers, like nft_xx_reg_parse() etc..
Signature · 5 lines
Arturo Borrero González -- To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
Pablo Neira Ayuso
maintainerRe: [PATCH 2/2] netfilter: nftables: introduce ct_set module
In reply to Eric LeblondHi Eric,
net/netfilter/nft_ct_set.c
Quoted 98 lines · Eric Leblond
This module is a port of iptables CT target. It allows user to setup connection tracking for selected packets.
The context is not containing information about layer 4 so it is necessary to ask to userspace explicitely for which layer 4 protocol the connection template has to be created. This is the reason why the
NFTA_CT_SET_PROTOfield has been introduced.Signed-off-by Eric Leblond <[email protected]>--- include/uapi/linux/netfilter/nf_tables.h | 36 +++++ net/netfilter/Kconfig | 5 + net/netfilter/Makefile | 1 + net/netfilter/nft_ct_set.c | 262 +++++++++++++++++++++++++++++++ 4 files changed, 304 insertions(+) create mode 100644 net/netfilter/nft_ct_set.cinclude/uapi/linux/netfilter/nf_tables.h +36
-0@@ -707,6 +707,42 @@ enum nft_reject_attributes {#define NFTA_REJECT_MAX (__NFTA_REJECT_MAX - 1)/** +* enum nft_ct_set_attributes - nf_tables ct_set expression netlink attributes +* +* @NFTA_CT_SET_PROTO: layer 4 protocol +* @NFTA_CT_SET_FLAGS: flags for connection as NOTRACK for example +* @NFTA_CT_SET_HELPER: protocol helper to link with matching connections +* @NFTA_CT_SET_CTEVENTS: only generate the specified conntrack events +* @NFTA_CT_SET_EXPEVENTS: only generate the specified expectation events +* @NFTA_CT_SET_ZONEID: assign packet to sone id +* @NFTA_CT_SET_TIMEOUT: use timeout policy for the connection +* +*/ +enum nft_ct_set_attributes { +NFTA_CT_SET_UNSPEC , +NFTA_CT_SET_PROTO , +NFTA_CT_SET_FLAGS , +NFTA_CT_SET_HELPER , +NFTA_CT_SET_CTEVENTS , +NFTA_CT_SET_EXPEVENTS , +NFTA_CT_SET_ZONEID , +NFTA_CT_SET_TIMEOUT , + __NFTA_CT_SET_MAX +}; +#define NFTA_CT_SET_MAX (__NFTA_CT_SET_MAX - 1) + +/** +* enum nft_ct_set_flags - nf_tables ct_set flags +* +* @NFT_CT_SET_F_NOTRACK: do not track this connection +*/ +enum nft_ct_set_flags { + +enum nft_ct_set_flags { +NFT_CT_SET_F_NOTRACK =0x1 , +}; + +/** * enum nft_nat_types - nf_tables nat expression NAT types * * @NFT_NAT_SNAT: source NAT net/netfilter/Kconfig +5
-0@@ -430,6 +430,11 @@ config NFT_CT depends on NF_CONNTRACK tristate "Netfilter nf_tables conntrack module" +config NFT_CT_SET + depends on NF_TABLES + depends on NF_CONNTRACK + tristate "Netfilter nf_tables conntrack setup module" + config NFT_RBTREE depends on NF_TABLES tristate "Netfilter nf_tables rbtree set module"net/netfilter/Makefile +1
-0@@ -74,6 +74,7 @@ obj-$(CONFIG_NFT_COMPAT) += nft_compat.o obj-$(CONFIG_NFT_EXTHDR) += nft_exthdr.o obj-$(CONFIG_NFT_META) += nft_meta.o obj-$(CONFIG_NFT_CT) += nft_ct.o +obj-$(CONFIG_NFT_CT_SET) += nft_ct_set.o obj-$(CONFIG_NFT_LIMIT) += nft_limit.o obj-$(CONFIG_NFT_NAT) += nft_nat.o obj-$(CONFIG_NFT_QUEUE) += nft_queue.onet/netfilter/nft_ct_set.c new file +0
-0
This has to be integrated into the existing nft_ct using the select_ops(...) infrastructure. Depending on SREG or DREG if interprets the rule as set or get operation. Some attributes will turn noop depending on the flavour.
Please, see recent Arturo's work to enable meta set operation for reference. Let me know if you have any question, thanks for working on this.