Netfilter updates for net-next
Subsystems (3)
Pablo Neira Ayuso
maintainercover Netfilter updates for net-next
Hi,
The following patchset contains Netfilter updates for net-next:
1) Add support for the catch-all set element. This special element can be used to define a default action to be applied in case that the set lookup returns no matching element.
2) Fix incorrect #ifdef dependencies in the nftables cgroupsv2 support, from Arnd Bergmann.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Thanks!
----------------------------------------------------------------
The following changes since commit 6d72e7c767acbbdd44ebc7d89c6690b405b32b57:
net:emac/emac-mac: Fix a use after free in emac_mac_tx_buf_send (2021-04-26 13:07:30 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git HEAD
for you to fetch changes up to 7acc0bb490c85012bcbda142b6755fd1fdf1fba1:
netfilter: nft_socket: fix build with CONFIG_SOCK_CGROUP_DATA=n (2021-04-27 22:34:05 +0200)
----------------------------------------------------------------
Arnd Bergmann (2):
netfilter: nft_socket: fix an unused variable warning
netfilter: nft_socket: fix build with CONFIG_SOCK_CGROUP_DATA=n
Pablo Neira Ayuso (5):
netfilter: nftables: rename set element data activation/deactivation functions
netfilter: nftables: add loop check helper function
netfilter: nftables: add helper function to flush set elements
netfilter: nftables: add helper function to validate set element data
netfilter: nftables: add catch-all set element support
include/net/netfilter/nf_tables.h | 5 + include/uapi/linux/netfilter/nf_tables.h | 2 + net/netfilter/nf_tables_api.c | 576 ++++++++++++++++++++++++++----- net/netfilter/nft_lookup.c | 12 +- net/netfilter/nft_objref.c | 11 +- net/netfilter/nft_set_hash.c | 6 + net/netfilter/nft_set_pipapo.c | 6 +- net/netfilter/nft_set_rbtree.c | 6 + net/netfilter/nft_socket.c | 11 +- 9 files changed, 532 insertions(+), 103 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/7 netfilter: nftables: rename set element data activation/deactivation functions
Rename:
- nft_set_elem_activate() to nft_set_elem_data_activate().
- nft_set_elem_deactivate() to nft_set_elem_data_deactivate().
To prepare for updates in the set element infrastructure to add support for the special catch-all element.
--- net/netfilter/nf_tables_api.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-)
net/netfilter/nf_tables_api.c +11 -11
@@ -5263,8 +5263,8 @@ void nft_set_elem_destroy(const struct nft_set *set, void *elem, }EXPORT_SYMBOL_GPL (nft_set_elem_destroy); -/* Only called from commit path, nft_set_elem_deactivate() already deals with -* the refcounting from the preparation phase. +/* Only called from commit path, nft_setelem_data_deactivate() already deals +* with the refcounting from the preparation phase. */ static void nf_tables_set_elem_destroy (const struct nft_ctx *ctx,const struct nft_set *set,void *elem)
@@ -5733,9 +5733,9 @@ void nft_data_hold(const struct nft_data *data, enum nft_data_types type) } } -static void nft_set_elem_activate (const struct net *net, -const struct nft_set *set, -struct nft_set_elem *elem) +static void nft_setelem_data_activate (const struct net *net, +const struct nft_set *set, +struct nft_set_elem *elem) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem->priv);
@@ -5745,9 +5745,9 @@ static void nft_set_elem_activate(const struct net *net, (*nft_set_ext_obj (ext))->use++; } -static void nft_set_elem_deactivate (const struct net *net, -const struct nft_set *set, -struct nft_set_elem *elem) +static void nft_setelem_data_deactivate (const struct net *net, +const struct nft_set *set, +struct nft_set_elem *elem) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem->priv);
@@ -5824,7 +5824,7 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,kfree (elem.priv); elem.priv = priv; -nft_set_elem_deactivate (ctx->net, set, &elem); +nft_setelem_data_deactivate (ctx->net, set, &elem);nft_trans_elem (trans) = elem;nft_trans_commit_list_add_tail (ctx->net, trans);
@@ -5858,7 +5858,7 @@ static int nft_flush_set(const struct nft_ctx *ctx, } set->ndeact++; -nft_set_elem_deactivate (ctx->net, set, elem); +nft_setelem_data_deactivate (ctx->net, set, elem);nft_trans_elem_set (trans) = set;nft_trans_elem (trans) = *elem;nft_trans_commit_list_add_tail (ctx->net, trans);
@@ -8479,7 +8479,7 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)case NFT_MSG_DELSETELEM : te = (struct nft_trans_elem *)trans->data; -nft_set_elem_activate (net, te->set, &te->elem); +nft_setelem_data_activate (net, te->set, &te->elem); te->set->ops->activate (net, te->set, &te->elem); te->set->ndeact--;
Signature · 1 line
2.30.2
Re: [PATCH net-next 1/7] netfilter: nftables: rename set element data activation/deactivation functions
This series was applied to netdev/net-next.git (refs/heads/master):
Hello:
This series was applied to netdev/net-next.git (refs/heads/master):
Quoted 9 lines · On Tue, 27 Apr 2021 22:43:39 +0200 you wrote:
Rename:
-
nft_set_elem_activate()tonft_set_elem_data_activate().
-nft_set_elem_deactivate()tonft_set_elem_data_deactivate().To prepare for updates in the set element infrastructure to add support for the special catch-all element.
[...]
Here is the summary with links:
- [net-next,1/7] netfilter: nftables: rename set element data activation/deactivation functions
https://git.kernel.org/netdev/net-next/c/f8bb7889af58 - [net-next,2/7] netfilter: nftables: add loop check helper function https://git.kernel.org/netdev/net-next/c/6387aa6e59be - [net-next,3/7] netfilter: nftables: add helper function to flush set elements https://git.kernel.org/netdev/net-next/c/e6ba7cb63b8a - [net-next,4/7] netfilter: nftables: add helper function to validate set element data https://git.kernel.org/netdev/net-next/c/97c976d662fb - [net-next,5/7] netfilter: nftables: add catch-all set element support https://git.kernel.org/netdev/net-next/c/aaa31047a6d2 - [net-next,6/7] netfilter: nft_socket: fix an unused variable warning https://git.kernel.org/netdev/net-next/c/8a7363f84979 - [net-next,7/7] netfilter: nft_socket: fix build with CONFIG_SOCK_CGROUP_DATA=n https://git.kernel.org/netdev/net-next/c/7acc0bb490c8
You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
Pablo Neira Ayuso
maintainerPATCH 2/7 netfilter: nftables: add loop check helper function
In reply to Pablo Neira AyusoThis patch adds nft_check_loops() to reuse it in the new catch-all element codebase.
--- net/netfilter/nf_tables_api.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-)
net/netfilter/nf_tables_api.c +21 -9
@@ -8626,26 +8626,38 @@ EXPORT_SYMBOL_GPL(nft_chain_validate_hooks);static int nf_tables_check_loops (const struct nft_ctx *ctx,const struct nft_chain *chain); +static int nft_check_loops (const struct nft_ctx *ctx, +const struct nft_set_ext *ext) +{ +const struct nft_data *data; +int ret; + + data =nft_set_ext_data (ext); +switch (data->verdict.code) { +case NFT_JUMP : +case NFT_GOTO : + ret =nf_tables_check_loops (ctx, data->verdict.chain); +break ; +default : + ret =0 ; +break ; + } + +return ret; +} +static int nf_tables_loop_check_setelem (const struct nft_ctx *ctx,struct nft_set *set,const struct nft_set_iter *iter,struct nft_set_elem *elem) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem->priv); -const struct nft_data *data;if (nft_set_ext_exists (ext,NFT_SET_EXT_FLAGS ) && *nft_set_ext_flags (ext) &NFT_SET_ELEM_INTERVAL_END )return 0 ; - data =nft_set_ext_data (ext); -switch (data->verdict.code) { -case NFT_JUMP : -case NFT_GOTO : -return nf_tables_check_loops (ctx, data->verdict.chain); -default : -return 0 ; - } +return nft_check_loops (ctx, ext); } static int nf_tables_check_loops(const struct nft_ctx *ctx,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/7 netfilter: nftables: add helper function to flush set elements
In reply to Pablo Neira AyusoThis patch adds nft_set_flush() which prepares for the catch-all element support.
--- net/netfilter/nf_tables_api.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)
net/netfilter/nf_tables_api.c +18 -13
@@ -5839,10 +5839,10 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,return err; } -static int nft_flush_set (const struct nft_ctx *ctx, -struct nft_set *set, -const struct nft_set_iter *iter, -struct nft_set_elem *elem) +static int nft_setelem_flush (const struct nft_ctx *ctx, +struct nft_set *set, +const struct nft_set_iter *iter, +struct nft_set_elem *elem) {struct nft_trans *trans;int err;
@@ -5869,6 +5869,18 @@ static int nft_flush_set(const struct nft_ctx *ctx,return err; } +static int nft_set_flush (struct nft_ctx *ctx,struct nft_set *set,u8 genmask) +{ +struct nft_set_iter iter = { + .genmask = genmask, + .fn = nft_setelem_flush, + }; + + set->ops->walk (ctx, set, &iter); + +return iter.err; +} +static int nf_tables_delsetelem (struct sk_buff *skb,const struct nfnl_info *info,const struct nlattr *const nla[])
@@ -5892,15 +5904,8 @@ static int nf_tables_delsetelem(struct sk_buff *skb,if (!list_empty (&set->bindings) && set->flags &NFT_SET_CONSTANT )return -EBUSY ; -if (nla[NFTA_SET_ELEM_LIST_ELEMENTS ] ==NULL ) { -struct nft_set_iter iter = { - .genmask = genmask, - .fn = nft_flush_set, - }; - set->ops->walk (&ctx, set, &iter); - -return iter.err; - } +if (!nla[NFTA_SET_ELEM_LIST_ELEMENTS ]) +return nft_set_flush (&ctx, set, genmask);nla_for_each_nested (attr, nla[NFTA_SET_ELEM_LIST_ELEMENTS ], rem) { err =nft_del_setelem (&ctx, set, attr);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/7 netfilter: nftables: add helper function to validate set element data
In reply to Pablo Neira AyusoWhen binding sets to rule, validate set element data according to set definition. This patch adds a helper function to be reused by the catch-all set element support.
--- net/netfilter/nf_tables_api.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +11 -4
@@ -4499,10 +4499,9 @@ static int nft_validate_register_store(const struct nft_ctx *ctx,enum nft_data_types type, unsigned int len); -static int nf_tables_bind_check_setelem (const struct nft_ctx *ctx, -struct nft_set *set, -const struct nft_set_iter *iter, -struct nft_set_elem *elem) +static int nft_setelem_data_validate (const struct nft_ctx *ctx, +struct nft_set *set, +struct nft_set_elem *elem) {const struct nft_set_ext *ext =nft_set_elem_ext (set, elem->priv);enum nft_registers dreg;
@@ -4514,6 +4513,14 @@ static int nf_tables_bind_check_setelem(const struct nft_ctx *ctx, set->dlen); } +static int nf_tables_bind_check_setelem (const struct nft_ctx *ctx, +struct nft_set *set, +const struct nft_set_iter *iter, +struct nft_set_elem *elem) +{ +return nft_setelem_data_validate (ctx, set, elem); +} +int nf_tables_bind_set (const struct nft_ctx *ctx,struct nft_set *set,struct nft_set_binding *binding) {
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/7 netfilter: nftables: add catch-all set element support
In reply to Pablo Neira AyusoThis patch extends the set infrastructure to add a special catch-all set element. If the lookup fails to find an element (or range) in the set, then the catch-all element is selected. Users can specify a mapping, expression(s) and timeout to be attached to the catch-all element.
This patch adds a catchall list to the set, this list might contain more than one single catch-all element (e.g. in case that the catch-all element is removed and a new one is added in the same transaction). However, most of the time, there will be either one element or no elements at all in this list.
The catch-all element is identified via NFT_SET_ELEM_CATCHALL flag and such special element has no NFTA_SET_ELEM_KEY attribute. There is a new nft_set_elem_catchall object that stores a reference to the dummy catch-all element (catchall->elem) whose layout is the same of the set element type to reuse the existing set element codebase.
The set size does not apply to the catch-all element, users can define a catch-all element even if the set is full.
The check for valid set element flags hava been updates to report EOPNOTSUPP in case userspace requests flags that are not supported when using new userspace nftables and old kernel.
--- include/net/netfilter/nf_tables.h | 5 + include/uapi/linux/netfilter/nf_tables.h | 2 + net/netfilter/nf_tables_api.c | 480 ++++++++++++++++++++--- net/netfilter/nft_lookup.c | 12 +- net/netfilter/nft_objref.c | 11 +- net/netfilter/nft_set_hash.c | 6 + net/netfilter/nft_set_pipapo.c | 6 +- net/netfilter/nft_set_rbtree.c | 6 + 8 files changed, 465 insertions(+), 63 deletions(-)
include/net/netfilter/nf_tables.h +5 -0
@@ -497,6 +497,7 @@ struct nft_set {u8 dlen;u8 num_exprs;struct nft_expr *exprs[NFT_SET_EXPR_MAX ]; +struct list_head catchall_list; unsigned char data[] __attribute__((aligned(__alignof__(u64)))); };
@@ -522,6 +523,10 @@ struct nft_set *nft_set_lookup_global(const struct net *net,const struct nlattr *nla_set_id, u8 genmask); +struct nft_set_ext *nft_set_catchall_lookup (const struct net *net, +const struct nft_set *set); +void *nft_set_catchall_gc (const struct nft_set *set); +static inline unsigned long nft_set_gc_interval (const struct nft_set *set) {return set->gc_int ?msecs_to_jiffies (set->gc_int) :HZ ;
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -398,9 +398,11 @@ enum nft_set_attributes {* enum nft_set_elem_flags - nf_tables set element flags * * @NFT_SET_ELEM_INTERVAL_END: element ends the previous interval +* @NFT_SET_ELEM_CATCHALL: special catch-all element */ enum nft_set_elem_flags {NFT_SET_ELEM_INTERVAL_END =0x1 , +NFT_SET_ELEM_CATCHALL =0x2 , };/**
net/netfilter/nf_tables_api.c +426 -54
690 lines not on this page · raw message
net/netfilter/nft_lookup.c +8 -4
@@ -30,13 +30,17 @@ void nft_lookup_eval(const struct nft_expr *expr,const struct nft_lookup *priv =nft_expr_priv (expr);const struct nft_set *set = priv->set;const struct nft_set_ext *ext =NULL ; +const struct net *net =nft_net (pkt);bool found; - found = set->ops->lookup (nft_net (pkt), set, ®s->data[priv->sreg], - &ext) ^ priv->invert; + found = set->ops->lookup (net, set, ®s->data[priv->sreg], &ext) ^ + priv->invert;if (!found) { - regs->verdict.code =NFT_BREAK ; -return ; + ext =nft_set_catchall_lookup (net, set); +if (!ext) { + regs->verdict.code =NFT_BREAK ; +return ; + } }if (ext) {
net/netfilter/nft_objref.c +7 -4
@@ -105,15 +105,18 @@ static void nft_objref_map_eval(const struct nft_expr *expr, {struct nft_objref_map *priv =nft_expr_priv (expr);const struct nft_set *set = priv->set; +struct net *net =nft_net (pkt);const struct nft_set_ext *ext;struct nft_object *obj;bool found; - found = set->ops->lookup (nft_net (pkt), set, ®s->data[priv->sreg], - &ext); + found = set->ops->lookup (net, set, ®s->data[priv->sreg], &ext);if (!found) { - regs->verdict.code =NFT_BREAK ; -return ; + ext =nft_set_catchall_lookup (net, set); +if (!ext) { + regs->verdict.code =NFT_BREAK ; +return ; + } } obj = *nft_set_ext_obj (ext); obj->ops->eval (obj, regs, pkt);
net/netfilter/nft_set_hash.c +6 -0
@@ -350,6 +350,12 @@ static void nft_rhash_gc(struct work_struct *work)rhashtable_walk_stop (&hti);rhashtable_walk_exit (&hti); + he =nft_set_catchall_gc (set); +if (he) { + gcb =nft_set_gc_batch_check (set, gcb,GFP_ATOMIC ); +if (gcb) +nft_set_gc_batch_add (gcb, he); + }nft_set_gc_batch_complete (gcb);queue_delayed_work (system_power_efficient_wq, &priv->gc_work,nft_set_gc_interval (set));
net/netfilter/nft_set_pipapo.c +5 -1
@@ -1529,11 +1529,11 @@ static void pipapo_gc(const struct nft_set *set, struct nft_pipapo_match *m) {struct nft_pipapo *priv =nft_set_priv (set);int rules_f0, first_rule =0 ; +struct nft_pipapo_elem *e;while ((rules_f0 =pipapo_rules_same_key (m->f, first_rule))) {union nft_pipapo_map_bucket rulemap[NFT_PIPAPO_MAX_FIELDS ];struct nft_pipapo_field *f; -struct nft_pipapo_elem *e;int i, start, rules_fx; start = first_rule;
@@ -1569,6 +1569,10 @@ static void pipapo_gc(const struct nft_set *set, struct nft_pipapo_match *m) } } + e =nft_set_catchall_gc (set); +if (e) +nft_set_elem_destroy (set, e, true); + priv->last_gc = jiffies; }
net/netfilter/nft_set_rbtree.c +6 -0
@@ -541,6 +541,12 @@ static void nft_rbtree_gc(struct work_struct *work)write_seqcount_end (&priv->count);write_unlock_bh (&priv->lock); + rbe =nft_set_catchall_gc (set); +if (rbe) { + gcb =nft_set_gc_batch_check (set, gcb,GFP_ATOMIC ); +if (gcb) +nft_set_gc_batch_add (gcb, rbe); + }nft_set_gc_batch_complete (gcb); queue_delayed_work(system_power_efficient_wq, &priv->gc_work,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 6/7 netfilter: nft_socket: fix an unused variable warning
In reply to Pablo Neira AyusoFrom: Arnd Bergmann <[email protected]>
The variable is only used in an #ifdef, causing a harmless warning:
net/netfilter/nft_socket.c: In function 'nft_socket_init':net/netfilter/nft_socket.c:137:27: error: unused variable 'level' [-Werror=unused-variable]
137 | unsigned int len, level;
| ^~~~~
Move it into the same #ifdef block.
--- net/netfilter/nft_socket.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
net/netfilter/nft_socket.c +5 -2
@@ -134,7 +134,7 @@ static int nft_socket_init(const struct nft_ctx *ctx, const struct nlattr * const tb[]) {struct nft_socket *priv =nft_expr_priv (expr); -unsigned int len, level; +unsigned int len;if (!tb[NFTA_SOCKET_DREG ] || !tb[NFTA_SOCKET_KEY ])return -EINVAL ;
@@ -160,7 +160,9 @@ static int nft_socket_init(const struct nft_ctx *ctx, len =sizeof (u32);break ;#ifdef CONFIG_CGROUPS -case NFT_SOCKET_CGROUPV2 : +case NFT_SOCKET_CGROUPV2 : { +unsigned int level; +if (!tb[NFTA_SOCKET_LEVEL ])return -EINVAL ;
@@ -171,6 +173,7 @@ static int nft_socket_init(const struct nft_ctx *ctx, priv->level = level; len =sizeof (u64); break; + }#endif default :return -EOPNOTSUPP ;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 7/7 netfilter: nft_socket: fix build with CONFIG_SOCK_CGROUP_DATA=n
In reply to Pablo Neira AyusoFrom: Arnd Bergmann <[email protected]>
In some configurations, the sock_cgroup_ptr() function is not available:
net/netfilter/nft_socket.c: In function 'nft_sock_get_eval_cgroupv2':net/netfilter/nft_socket.c:47:16: error: implicit declaration of function 'sock_cgroup_ptr'; did you mean 'obj_cgroup_put'? [-Werror=implicit-function-declaration]
47 | cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
| ^~~~~~~~~~~~~~~
| obj_cgroup_putnet/netfilter/nft_socket.c:47:14: error: assignment to 'struct cgroup *' from 'int' makes pointer from integer without a cast [-Werror=int-conversion]
47 | cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);
| ^
Change the caller to match the same #ifdef check, only calling it when the function is defined.
--- net/netfilter/nft_socket.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nft_socket.c +2 -2
@@ -34,7 +34,7 @@ static void nft_socket_wildcard(const struct nft_pktinfo *pkt, } } -#ifdef CONFIG_CGROUPS +#ifdef CONFIG_SOCK_CGROUP_DATA static noinline boolnft_sock_get_eval_cgroupv2 (u32 *dest,const struct nft_pktinfo *pkt,u32 level) {
@@ -106,7 +106,7 @@ static void nft_socket_eval(const struct nft_expr *expr, }nft_socket_wildcard (pkt, regs, sk, dest);break ; -#ifdef CONFIG_CGROUPS +#ifdef CONFIG_SOCK_CGROUP_DATA case NFT_SOCKET_CGROUPV2 :if (!nft_sock_get_eval_cgroupv2 (dest, pkt, priv->level)) { regs->verdict.code =NFT_BREAK ;
Signature · 1 line
2.30.2