netfilter: nf_tables: dynamic set updates
Subsystems (2)
cover netfilter: nf_tables: dynamic set updates
The following patches contain a small bug fix for set selection with timeouts, as well as support for dynamic set updates.
A new "dynset" expression type is added to dynamically add or update set elements. The name is not my first choice, however due to clashes with the regular set attributes simply "set" wasn't possible. Userspace exposes this in a nicer fashion.
As preparation, set size accounting is changed to use an atomic_t for the number of elements to avoid races, additionally set bindings are changed to not assume every binding is related to a lookup or map operation.
Please apply, thanks!
Patrick McHardy (4):
netfilter: nf_tables: fix set selection when timeouts are requested
netfilter: nf_tables: prepare set element accounting for async updates
netfilter: nf_tables: support different set binding types
netfilter: nf_tables: add support for dynamic set updates
include/net/netfilter/nf_tables.h | 23 +++- include/net/netfilter/nf_tables_core.h | 3 + include/uapi/linux/netfilter/nf_tables.h | 27 ++++ net/netfilter/Makefile | 2 +- net/netfilter/nf_tables_api.c | 44 ++++--- net/netfilter/nf_tables_core.c | 7 + net/netfilter/nft_dynset.c | 218 +++++++++++++++++++++++++++++++ net/netfilter/nft_hash.c | 40 +++++- net/netfilter/nft_lookup.c | 2 + 9 files changed, 345 insertions(+), 21 deletions(-) create mode 100644 net/netfilter/nft_dynset.c
Signature · 1 line
2.1.0
PATCH 1/4 netfilter: nf_tables: fix set selection when timeouts are requested
The NFT_SET_TIMEOUT flag is ignore in nft_select_set_ops, which may lead to selection of a set implementation that doesn't actually support timeouts.
--- net/netfilter/nf_tables_api.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_tables_api.c +1 -1
@@ -2159,7 +2159,7 @@ nft_select_set_ops(const struct nlattr * const nla[], features =0 ;if (nla[NFTA_SET_FLAGS ] !=NULL ) { features =ntohl (nla_get_be32 (nla[NFTA_SET_FLAGS ])); - features &=NFT_SET_INTERVAL |NFT_SET_MAP ; + features &=NFT_SET_INTERVAL |NFT_SET_MAP |NFT_SET_TIMEOUT ; } bops =NULL ;
Signature · 1 line
2.1.0
PATCH 2/4 netfilter: nf_tables: prepare set element accounting for async updates
In reply to Patrick McHardyUse atomic operations for the element count to avoid races with async updates.
To properly handle the transactional semantics during netlink updates, deleted but not yet committed elements are accounted for seperately and are treated as being already removed. This means for the duration of a netlink transaction, the limit might be exceeded by the amount of elements deleted. Set implementations must be prepared to handle this.
--- include/net/netfilter/nf_tables.h | 4 +++- net/netfilter/nf_tables_api.c | 21 ++++++++++++--------- net/netfilter/nft_hash.c | 3 ++- 3 files changed, 17 insertions(+), 11 deletions(-)
include/net/netfilter/nf_tables.h +3 -1
@@ -258,6 +258,7 @@ void nft_unregister_set(struct nft_set_ops *ops);* @dtype: data type (verdict or numeric type defined by userspace) * @size: maximum set size * @nelems: number of elements +* @ndeact: number of deactivated elements queued for removal * @timeout: default timeout value in msecs * @gc_int: garbage collection interval in msecs * @policy: set parameterization (see enum nft_set_policies)
@@ -275,7 +276,8 @@ struct nft_set {u32 ktype;u32 dtype;u32 size; -u32 nelems; +atomic_t nelems; +u32 ndeact;u64 timeout;u32 gc_int;u16 policy;
net/netfilter/nf_tables_api.c +12 -9
@@ -3238,9 +3238,6 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,u32 flags;int err; -if (set->size && set->nelems == set->size) -return -ENFILE ; - err =nla_parse_nested (nla,NFTA_SET_ELEM_MAX , attr, nft_set_elem_policy);if (err <0 )
@@ -3391,11 +3388,15 @@ static int nf_tables_newsetelem(struct sock *nlsk, struct sk_buff *skb,return -EBUSY ;nla_for_each_nested (attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS ], rem) { +if (set->size && + !atomic_add_unless (&set->nelems,1 , set->size + set->ndeact)) +return -ENFILE ; + err =nft_add_set_elem (&ctx, set, attr); -if (err <0 ) +if (err <0 ) { +atomic_dec (&set->nelems);break ; - - set->nelems++; + } }return err; }
@@ -3477,7 +3478,7 @@ static int nf_tables_delsetelem(struct sock *nlsk, struct sk_buff *skb,if (err <0 )break ; - set->nelems--; + set->ndeact++; }return err; }
@@ -3810,6 +3811,8 @@ static int nf_tables_commit(struct sk_buff *skb) &te->elem, NFT_MSG_DELSETELEM, 0); te->set->ops->remove (te->set, &te->elem); +atomic_dec (&te->set->nelems); + te->set->ndeact--;break ; } }
@@ -3913,16 +3916,16 @@ static int nf_tables_abort(struct sk_buff *skb)nft_trans_destroy (trans);break ;case NFT_MSG_NEWSETELEM : -nft_trans_elem_set (trans)->nelems--; te = (struct nft_trans_elem *)trans->data; te->set->ops->remove (te->set, &te->elem); +atomic_dec (&te->set->nelems);break ;case NFT_MSG_DELSETELEM : te = (struct nft_trans_elem *)trans->data; -nft_trans_elem_set (trans)->nelems++; te->set->ops->activate (te->set, &te->elem); + te->set->ndeact--;nft_trans_destroy (trans);break ;
net/netfilter/nft_hash.c +2 -1
@@ -203,7 +203,7 @@ out:static void nft_hash_gc (struct work_struct *work) { -const struct nft_set *set; +struct nft_set *set;struct nft_hash_elem *he;struct nft_hash *priv;struct nft_set_gc_batch *gcb =NULL ;
@@ -237,6 +237,7 @@ static void nft_hash_gc(struct work_struct *work)if (gcb ==NULL ) goto out;rhashtable_remove_fast (&priv->ht, &he->node, nft_hash_params); +atomic_dec (&set->nelems);nft_set_gc_batch_add (gcb, he); } out:
Signature · 1 line
2.1.0
PATCH 3/4 netfilter: nf_tables: support different set binding types
In reply to Patrick McHardyCurrently a set binding is assumed to be related to a lookup and, in case of maps, a data load.
In order to use bindings for set updates, the loop detection checks must be restricted to map operations only. Add a flags member to the binding struct to hold the set "action" flags such as NFT_SET_MAP, and perform loop detection based on these.
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 11 ++++++++--- net/netfilter/nft_lookup.c | 2 ++ 3 files changed, 12 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -316,6 +316,7 @@ static inline unsigned long nft_set_gc_interval(const struct nft_set *set)* * @list: set bindings list node * @chain: chain containing the rule bound to the set +* @flags: set action flags * * A set binding contains all information necessary for validation * of new elements added to a bound set.
@@ -323,6 +324,7 @@ static inline unsigned long nft_set_gc_interval(const struct nft_set *set)struct nft_set_binding {struct list_head list;const struct nft_chain *chain; +u32 flags; }; int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,
net/netfilter/nf_tables_api.c +8 -3
@@ -2811,12 +2811,13 @@ int nf_tables_bind_set(const struct nft_ctx *ctx, struct nft_set *set,if (!list_empty (&set->bindings) && set->flags &NFT_SET_ANONYMOUS )return -EBUSY ; -if (set->flags &NFT_SET_MAP ) { +if (binding->flags &NFT_SET_MAP ) {/* If the set is already bound to the same chain all * jumps are already validated for that chain. */ list_for_each_entry (i, &set->bindings, list) { -if (i->chain == binding->chain) +if (binding->flags &NFT_SET_MAP && + i->chain == binding->chain) goto bind; }
@@ -3312,6 +3313,9 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, .chain = (struct nft_chain *)binding->chain, }; +if (!(binding->flags &NFT_SET_MAP )) +continue ; + err =nft_validate_data_load (&bind_ctx, dreg, &data, d2.type);if (err <0 )
@@ -4063,7 +4067,8 @@ static int nf_tables_check_loops(const struct nft_ctx *ctx,continue ;list_for_each_entry (binding, &set->bindings, list) { -if (binding->chain != chain) +if (!(binding->flags &NFT_SET_MAP ) || + binding->chain != chain)continue ; iter.skip =0 ;
net/netfilter/nft_lookup.c +2 -0
@@ -92,6 +92,8 @@ static int nft_lookup_init(const struct nft_ctx *ctx, }else if (set->flags &NFT_SET_MAP )return -EINVAL ; + priv->binding.flags = set->flags &NFT_SET_MAP ; + err =nf_tables_bind_set (ctx, set, &priv->binding);if (err <0 )return err;
Signature · 1 line
2.1.0
PATCH 4/4 netfilter: nf_tables: add support for dynamic set updates
In reply to Patrick McHardyAdd a new "dynset" expression for dynamic set updates.
A new set op ->update() is added which, for non existant elements, invokes an initialization callback and inserts the new element. For both new or existing elements the extenstion pointer is returned to the caller to optionally perform timer updates or other actions.
Element removal is not supported so far, however that seems to be a rather exotic need and can be added later on.
--- include/net/netfilter/nf_tables.h | 17 +++ include/net/netfilter/nf_tables_core.h | 3 + include/uapi/linux/netfilter/nf_tables.h | 27 ++++ net/netfilter/Makefile | 2 +- net/netfilter/nf_tables_api.c | 10 +- net/netfilter/nf_tables_core.c | 7 + net/netfilter/nft_dynset.c | 218 +++++++++++++++++++++++++++++++ net/netfilter/nft_hash.c | 37 ++++++ 8 files changed, 315 insertions(+), 6 deletions(-)
include/net/netfilter/nf_tables.h +17 -0
@@ -196,6 +196,7 @@ struct nft_set_estimate { };struct nft_set_ext ; +struct nft_expr ;/** * struct nft_set_ops - nf_tables set operations
@@ -218,6 +219,15 @@ struct nft_set_ops {bool (*lookup)(const struct nft_set *set,const struct nft_data *key,const struct nft_set_ext **ext); +bool (*update)(struct nft_set *set, +const struct nft_data *key, +void *(*new)(struct nft_set *, +const struct nft_expr *, +struct nft_data []), +const struct nft_expr *expr, +struct nft_data data[], +const struct nft_set_ext **ext); +int (*insert)(const struct nft_set *set,const struct nft_set_elem *elem); void (*activate)(const struct nft_set *set,
@@ -466,6 +476,11 @@ static inline struct nft_set_ext *nft_set_elem_ext(const struct nft_set *set,return elem + set->ops->elemsize; } +void *nft_set_elem_init (const struct nft_set *set, +const struct nft_set_ext_tmpl *tmpl, +const struct nft_data *key, +const struct nft_data *data, +u64 timeout,gfp_t gfp);void nft_set_elem_destroy (const struct nft_set *set,void *elem);/**
@@ -845,6 +860,8 @@ static inline u8 nft_genmask_cur(const struct net *net)return 1 <<ACCESS_ONCE (net->nft.gencursor); } +#define NFT_GENMASK_ANY ((1 << 0) | (1 << 1)) +/* * Set element transaction helpers */
include/net/netfilter/nf_tables_core.h +3 -0
@@ -31,6 +31,9 @@ void nft_cmp_module_exit(void);int nft_lookup_module_init (void );void nft_lookup_module_exit (void ); +int nft_dynset_module_init (void ); +void nft_dynset_module_exit (void ); +int nft_bitwise_module_init (void );void nft_bitwise_module_exit (void );
include/uapi/linux/netfilter/nf_tables.h +27 -0
@@ -515,6 +515,33 @@ enum nft_lookup_attributes { };#define NFTA_LOOKUP_MAX (__NFTA_LOOKUP_MAX - 1) +enum nft_dynset_ops { +NFT_DYNSET_OP_ADD , +NFT_DYNSET_OP_UPDATE , +}; + +/** +* enum nft_dynset_attributes - dynset expression attributes +* +* @NFTA_DYNSET_SET_NAME: name of set the to add data to (NLA_STRING) +* @NFTA_DYNSET_SET_ID: uniquely identifier of the set in the transaction (NLA_U32) +* @NFTA_DYNSET_OP: operation (NLA_U32) +* @NFTA_DYNSET_SREG_KEY: source register of the key (NLA_U32) +* @NFTA_DYNSET_SREG_DATA: source register of the data (NLA_U32) +* @NFTA_DYNSET_TIMEOUT: timeout value for the new element (NLA_U64) +*/ +enum nft_dynset_attributes { +NFTA_DYNSET_UNSPEC , +NFTA_DYNSET_SET_NAME , +NFTA_DYNSET_SET_ID , +NFTA_DYNSET_OP , +NFTA_DYNSET_SREG_KEY , +NFTA_DYNSET_SREG_DATA , +NFTA_DYNSET_TIMEOUT , + __NFTA_DYNSET_MAX, +}; +#define NFTA_DYNSET_MAX (__NFTA_DYNSET_MAX - 1) +/** * enum nft_payload_bases - nf_tables payload expression offset bases *
net/netfilter/Makefile +1 -1
@@ -70,7 +70,7 @@ obj-$(CONFIG_NETFILTER_SYNPROXY) += nf_synproxy_core.o # nf_tables nf_tables-objs += nf_tables_core.o nf_tables_api.o -nf_tables-objs += nft_immediate.o nft_cmp.o nft_lookup.o +nf_tables-objs += nft_immediate.o nft_cmp.o nft_lookup.o nft_dynset.o nf_tables-objs += nft_bitwise.o nft_byteorder.o nft_payload.o obj-$(CONFIG_NF_TABLES) += nf_tables.o
net/netfilter/nf_tables_api.c +5 -5
@@ -3183,11 +3183,11 @@ static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,return trans; } -static void *nft_set_elem_init (const struct nft_set *set, -const struct nft_set_ext_tmpl *tmpl, -const struct nft_data *key, -const struct nft_data *data, -u64 timeout,gfp_t gfp) +void *nft_set_elem_init (const struct nft_set *set, +const struct nft_set_ext_tmpl *tmpl, +const struct nft_data *key, +const struct nft_data *data, +u64 timeout,gfp_t gfp) {struct nft_set_ext *ext;void *elem;
net/netfilter/nf_tables_core.c +7 -0
@@ -239,8 +239,14 @@ int __init nf_tables_core_module_init(void)if (err <0 ) goto err6; + err =nft_dynset_module_init (); +if (err <0 ) + goto err7; +return 0 ; +err7: +nft_payload_module_exit (); err6:nft_byteorder_module_exit (); err5:
@@ -257,6 +263,7 @@ err1:void nf_tables_core_module_exit (void ) { +nft_dynset_module_exit ();nft_payload_module_exit ();nft_byteorder_module_exit ();nft_bitwise_module_exit ();
net/netfilter/nft_dynset.c new file +218 -0
218 lines not on this page · raw message
net/netfilter/nft_hash.c +37 -0
@@ -90,6 +90,42 @@ static bool nft_hash_lookup(const struct nft_set *set,return !!he; } +static bool nft_hash_update (struct nft_set *set,const struct nft_data *key, +void *(*new)(struct nft_set *, +const struct nft_expr *, +struct nft_data []), +const struct nft_expr *expr, +struct nft_data data[], +const struct nft_set_ext **ext) +{ +struct nft_hash *priv =nft_set_priv (set); +struct nft_hash_elem *he; +struct nft_hash_cmp_arg arg = { + .genmask =NFT_GENMASK_ANY , + .set = set, + .key = key, + }; + + he =rhashtable_lookup_fast (&priv->ht, &arg, nft_hash_params); +if (he !=NULL ) + goto out; + + he =new (set, expr, data); +if (he ==NULL ) + goto err1; +if (rhashtable_lookup_insert_key (&priv->ht, &arg, &he->node, + nft_hash_params)) + goto err2; +out: + *ext = &he->ext; +return true; + +err2: +nft_set_elem_destroy (set, he); +err1: +return false; +} +static int nft_hash_insert (const struct nft_set *set,const struct nft_set_elem *elem) {
@@ -335,6 +371,7 @@ static struct nft_set_ops nft_hash_ops __read_mostly = { .deactivate = nft_hash_deactivate, .remove = nft_hash_remove, .lookup = nft_hash_lookup, + .update = nft_hash_update, .walk = nft_hash_walk, .features = NFT_SET_MAP | NFT_SET_TIMEOUT, .owner = THIS_MODULE,
Signature · 1 line
2.1.0
Pablo Neira Ayuso
maintainerQuoted 8 lines · Patrick McHardy
The following patches contain a small bug fix for set selection with timeouts, as well as support for dynamic set updates.
A new "dynset" expression type is added to dynamically add or update set elements. The name is not my first choice, however due to clashes with the regular set attributes simply "set" wasn't possible. Userspace exposes this in a nicer fashion.
Quoted 4 lines
As preparation, set size accounting is changed to use an
atomic_tfor the number of elements to avoid races, additionally set bindings are changed to not assume every binding is related to a lookup or map operation.
Series applied, thanks Patrick.