Netfilter updates for net-next
Subsystems (4)
Pablo Neira Ayuso
maintainercover Netfilter updates for net-next
Hi,
The following patchset contains Netfilter updates for net-next:
1) Rename 'searched' column to 'clashres' in conntrack /proc/ stats to amend a recent patch, from Florian Westphal.
2) Remove unused nft_data_debug(), from YueHaibing.
3) Remove unused definitions in IPVS, also from YueHaibing.
4) Fix user data memleak in tables and objects, this is also amending a recent patch, from Jose M. Guisado.
5) Use nla_memdup() to allocate user data in table and objects, also from Jose M. Guisado
6) User data support for chains, from Jose M. Guisado
7) Remove unused definition in nf_tables_offload, from YueHaibing.
8) Use kvzalloc() in ip_set_alloc(), from Vasily Averin.
9) Fix false positive reported by lockdep in nfnetlink mutexes, from Florian Westphal.
10) Extend fast variant of cmp for neq operation, from Phil Sutter.
11) Implement fast bitwise variant, also from Phil Sutter.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Thank you.
----------------------------------------------------------------
The following changes since commit c5a2a132a38619d24d6d115c66cc277594b4fe01:
Merge tag 'linux-can-next-for-5.10-20200921' of git://git.kernel.org/pub/scm/linux/kernel/git/mkl/linux-can-next (2020-09-21 14:57:05 -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 10fdd6d80e4c21ad48f3860d723f5b3b5965477b:
netfilter: nf_tables: Implement fast bitwise expression (2020-10-04 21:08:33 +0200)
----------------------------------------------------------------
Florian Westphal (2):
netfilter: conntrack: proc: rename stat column
netfilter: nfnetlink: place subsys mutexes in distinct lockdep classes
Jose M. Guisado Gomez (3):
netfilter: nf_tables: fix userdata memleak
netfilter: nf_tables: use nla_memdup to copy udata
netfilter: nf_tables: add userdata attributes to nft_chain
Phil Sutter (2):
netfilter: nf_tables: Enable fast nft_cmp for inverted matches
netfilter: nf_tables: Implement fast bitwise expression
Vasily Averin (1):
netfilter: ipset: enable memory accounting for ipset allocations
YueHaibing (3):
netfilter: nf_tables: Remove ununsed function nft_data_debug
ipvs: Remove unused macros
netfilter: nf_tables_offload: Remove unused macro FLOW_SETUP_BLOCK
include/net/netfilter/nf_tables.h | 9 +- include/net/netfilter/nf_tables_core.h | 11 +++ include/uapi/linux/netfilter/nf_tables.h | 2 + net/netfilter/ipset/ip_set_core.c | 17 +--- net/netfilter/ipvs/ip_vs_sync.c | 3 - net/netfilter/nf_conntrack_standalone.c | 4 +- net/netfilter/nf_tables_api.c | 49 +++++++---- net/netfilter/nf_tables_core.c | 15 +++- net/netfilter/nf_tables_offload.c | 2 - net/netfilter/nfnetlink.c | 19 ++++- net/netfilter/nft_bitwise.c | 141 +++++++++++++++++++++++++++++-- net/netfilter/nft_cmp.c | 13 +-- 12 files changed, 222 insertions(+), 63 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/11 netfilter: conntrack: proc: rename stat column
From: Florian Westphal <[email protected]>
Rename 'searched' column to 'clashres' (same len).
conntrack(8) using the old /proc interface (ctnetlink not available) shows:
cpu=0 entries=4784 clashres=2292 [..]
Another alternative is to add another column, but this increases the number of always-0 columns.
--- net/netfilter/nf_conntrack_standalone.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nf_conntrack_standalone.c +2 -2
@@ -428,14 +428,14 @@ static int ct_cpu_seq_show(struct seq_file *seq, void *v)const struct ip_conntrack_stat *st = v;if (v ==SEQ_START_TOKEN ) { -seq_puts (seq,"entries searched found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete search_restart\n" ); +seq_puts (seq,"entries clashres found new invalid ignore delete delete_list insert insert_failed drop early_drop icmp_error expect_new expect_create expect_delete search_restart\n" );return 0 ; } seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x " "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n", nr_conntracks, - st->clash_resolve, /* was: searched */ + st->clash_resolve, st->found, 0, st->invalid,
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 2/11 netfilter: nf_tables: Remove ununsed function nft_data_debug
In reply to Pablo Neira AyusoFrom: YueHaibing <[email protected]>
It is never used, so can be removed.
--- include/net/netfilter/nf_tables.h | 7 ------- 1 file changed, 7 deletions(-)
include/net/netfilter/nf_tables.h +0 -7
@@ -148,13 +148,6 @@ static inline void nft_data_copy(u32 *dst, const struct nft_data *src,memcpy (dst, src, len); } -static inline void nft_data_debug (const struct nft_data *data) -{ -pr_debug ("data[0]=%x data[1]=%x data[2]=%x data[3]=%x\n" , - data->data[0 ], data->data[1 ], - data->data[2 ], data->data[3 ]); -} -/** * struct nft_ctx - nf_tables rule/set context *
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 3/11 ipvs: Remove unused macros
In reply to Pablo Neira AyusoFrom: YueHaibing <[email protected]>
They are not used since commit e4ff67513096 ("ipvs: add sync_maxlen parameter for the sync daemon")
--- net/netfilter/ipvs/ip_vs_sync.c | 3 --- 1 file changed, 3 deletions(-)
net/netfilter/ipvs/ip_vs_sync.c +0 -3
@@ -242,9 +242,6 @@ struct ip_vs_sync_thread_data {| IPVS Sync Connection (1) | */ -#define SYNC_MESG_HEADER_LEN 4 -#define MAX_CONNS_PER_SYNCBUFF 255/* nr_conns in ip_vs_sync_mesg is 8 bit */ -/* Version 0 header */ struct ip_vs_sync_mesg_v0 {__u8 nr_conns;
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 4/11 netfilter: nf_tables: fix userdata memleak
In reply to Pablo Neira AyusoFrom: "Jose M. Guisado Gomez" <[email protected]>
When userdata was introduced for tables and objects its allocation was only freed inside the error path of the new{table, object} functions.
Free user data inside corresponding destroy functions for tables and objects.
--- net/netfilter/nf_tables_api.c | 2 ++ 1 file changed, 2 insertions(+)
net/netfilter/nf_tables_api.c +2 -0
@@ -1211,6 +1211,7 @@ static void nf_tables_table_destroy(struct nft_ctx *ctx)rhltable_destroy (&ctx->table->chains_ht);kfree (ctx->table->name); +kfree (ctx->table->udata);kfree (ctx->table); }
@@ -6231,6 +6232,7 @@ static void nft_obj_destroy(const struct nft_ctx *ctx, struct nft_object *obj)module_put (obj->ops->type->owner);kfree (obj->key.name); +kfree (obj->udata);kfree (obj); }
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 5/11 netfilter: nf_tables: use nla_memdup to copy udata
In reply to Pablo Neira AyusoFrom: "Jose M. Guisado Gomez" <[email protected]>
When userdata support was added to tables and objects, user data coming from user space was allocated and copied using kzalloc + nla_memcpy.
Use nla_memdup to copy userdata of tables and objects.
--- net/netfilter/nf_tables_api.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-)
net/netfilter/nf_tables_api.c +4 -10
@@ -986,7 +986,6 @@ static int nf_tables_newtable(struct net *net, struct sock *nlsk,struct nft_table *table;struct nft_ctx ctx;u32 flags =0 ; -u16 udlen =0 ;int err;lockdep_assert_held (&net->nft.commit_mutex);
@@ -1023,13 +1022,11 @@ static int nf_tables_newtable(struct net *net, struct sock *nlsk, goto err_strdup;if (nla[NFTA_TABLE_USERDATA ]) { - udlen =nla_len (nla[NFTA_TABLE_USERDATA ]); - table->udata =kzalloc (udlen,GFP_KERNEL ); + table->udata =nla_memdup (nla[NFTA_TABLE_USERDATA ],GFP_KERNEL );if (table->udata ==NULL ) goto err_table_udata; -nla_memcpy (table->udata, nla[NFTA_TABLE_USERDATA ], udlen); - table->udlen = udlen; + table->udlen =nla_len (nla[NFTA_TABLE_USERDATA ]); } err =rhltable_init (&table->chains_ht, &nft_chain_ht_params);
@@ -5900,7 +5897,6 @@ static int nf_tables_newobj(struct net *net, struct sock *nlsk,struct nft_object *obj;struct nft_ctx ctx;u32 objtype; -u16 udlen;int err; if (!nla[NFTA_OBJ_TYPE] ||
@@ -5957,13 +5953,11 @@ static int nf_tables_newobj(struct net *net, struct sock *nlsk, }if (nla[NFTA_OBJ_USERDATA ]) { - udlen =nla_len (nla[NFTA_OBJ_USERDATA ]); - obj->udata =kzalloc (udlen,GFP_KERNEL ); + obj->udata =nla_memdup (nla[NFTA_OBJ_USERDATA ],GFP_KERNEL );if (obj->udata ==NULL ) goto err_userdata; -nla_memcpy (obj->udata, nla[NFTA_OBJ_USERDATA ], udlen); - obj->udlen = udlen; + obj->udlen =nla_len (nla[NFTA_OBJ_USERDATA ]); } err =nft_trans_obj_add (&ctx,NFT_MSG_NEWOBJ , obj);
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 6/11 netfilter: nf_tables: add userdata attributes to nft_chain
In reply to Pablo Neira AyusoFrom: "Jose M. Guisado Gomez" <[email protected]>
Enables storing userdata for nft_chain. Field udata points to user data and udlen stores its length.
Adds new attribute flag NFTA_CHAIN_USERDATA.
--- include/net/netfilter/nf_tables.h | 2 ++ include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 33 ++++++++++++++++++------ 3 files changed, 29 insertions(+), 8 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -945,6 +945,8 @@ struct nft_chain { bound:1 , genmask:2;char *name; +u16 udlen; +u8 *udata;/* Only used during control plane commit phase: */ struct nft_rule **rules_next;
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -208,6 +208,7 @@ enum nft_chain_flags {* @NFTA_CHAIN_COUNTERS: counter specification of the chain (NLA_NESTED: nft_counter_attributes) * @NFTA_CHAIN_FLAGS: chain flags * @NFTA_CHAIN_ID: uniquely identifies a chain in a transaction (NLA_U32) +* @NFTA_CHAIN_USERDATA: user data (NLA_BINARY) */ enum nft_chain_attributes {NFTA_CHAIN_UNSPEC ,
@@ -222,6 +223,7 @@ enum nft_chain_attributes {NFTA_CHAIN_PAD ,NFTA_CHAIN_FLAGS ,NFTA_CHAIN_ID , +NFTA_CHAIN_USERDATA , __NFTA_CHAIN_MAX };#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1)
net/netfilter/nf_tables_api.c +25 -8
@@ -1304,6 +1304,8 @@ static const struct nla_policy nft_chain_policy[NFTA_CHAIN_MAX + 1] = { [NFTA_CHAIN_COUNTERS] = { .type = NLA_NESTED }, [NFTA_CHAIN_FLAGS] = { .type = NLA_U32 }, [NFTA_CHAIN_ID] = { .type = NLA_U32 }, + [NFTA_CHAIN_USERDATA ] = { .type =NLA_BINARY , + .len = NFT_USERDATA_MAXLEN }, };static const struct nla_policy nft_hook_policy[NFTA_HOOK_MAX +1 ] = {
@@ -1445,6 +1447,10 @@ static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,if (nla_put_be32 (skb,NFTA_CHAIN_USE ,htonl (chain->use))) goto nla_put_failure; +if (chain->udata && +nla_put (skb,NFTA_CHAIN_USERDATA , chain->udlen, chain->udata)) + goto nla_put_failure; +nlmsg_end (skb, nlh);return 0 ;
@@ -1682,9 +1688,11 @@ void nf_tables_chain_destroy(struct nft_ctx *ctx)free_percpu (rcu_dereference_raw (basechain->stats)); }kfree (chain->name); +kfree (chain->udata);kfree (basechain); }else {kfree (chain->name); +kfree (chain->udata);kfree (chain); } }
@@ -2038,7 +2046,7 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, }else {if (!(flags &NFT_CHAIN_BINDING )) { err = -EINVAL ; - goto err1; + goto err_destroy_chain; }snprintf (name,sizeof (name),"__chain%llu" , ++chain_id);
@@ -2047,13 +2055,22 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,if (!chain->name) { err = -ENOMEM ; - goto err1; + goto err_destroy_chain; + } + +if (nla[NFTA_CHAIN_USERDATA ]) { + chain->udata =nla_memdup (nla[NFTA_CHAIN_USERDATA ],GFP_KERNEL ); +if (chain->udata ==NULL ) { + err = -ENOMEM ; + goto err_destroy_chain; + } + chain->udlen =nla_len (nla[NFTA_CHAIN_USERDATA ]); } rules =nf_tables_chain_alloc_rules (chain,0 );if (!rules) { err = -ENOMEM ; - goto err1; + goto err_destroy_chain; } *rules =NULL ;
@@ -2062,12 +2079,12 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, err =nf_tables_register_hook (net, table, chain);if (err <0 ) - goto err1; + goto err_destroy_chain; trans =nft_trans_chain_add (ctx,NFT_MSG_NEWCHAIN );if (IS_ERR (trans)) { err =PTR_ERR (trans); - goto err2; + goto err_unregister_hook; }nft_trans_chain_policy (trans) =NFT_CHAIN_POLICY_UNSET ;
@@ -2077,15 +2094,15 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, err =nft_chain_add (table, chain);if (err <0 ) {nft_trans_destroy (trans); - goto err2; + goto err_unregister_hook; } table->use++;return 0 ; -err2: +err_unregister_hook:nf_tables_unregister_hook (net, table, chain); -err1: +err_destroy_chain:nf_tables_chain_destroy (ctx);return err;
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 7/11 netfilter: nf_tables_offload: Remove unused macro FLOW_SETUP_BLOCK
In reply to Pablo Neira AyusoFrom: YueHaibing <[email protected]>
commit 9a32669fecfb ("netfilter: nf_tables_offload: support indr block call") left behind this.
--- net/netfilter/nf_tables_offload.c | 2 -- 1 file changed, 2 deletions(-)
net/netfilter/nf_tables_offload.c +0 -2
@@ -323,8 +323,6 @@ static int nft_indr_block_offload_cmd(struct nft_base_chain *basechain,return nft_block_setup (basechain, &bo, cmd); } -#define FLOW_SETUP_BLOCK TC_SETUP_BLOCK -static int nft_chain_offload_cmd (struct nft_base_chain *basechain,struct net_device *dev,enum flow_block_command cmd)
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 8/11 netfilter: ipset: enable memory accounting for ipset allocations
In reply to Pablo Neira AyusoFrom: Vasily Averin <[email protected]>
Currently netadmin inside non-trusted container can quickly allocate whole node's memory via request of huge ipset hashtable. Other ipset-related memory allocations should be restricted too.
v2: fixed typo ALLOC -> ACCOUNT
--- net/netfilter/ipset/ip_set_core.c | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-)
net/netfilter/ipset/ip_set_core.c +1 -16
@@ -250,22 +250,7 @@ EXPORT_SYMBOL_GPL(ip_set_type_unregister);void *ip_set_alloc (size_t size) { -void *members =NULL ; - -if (size <KMALLOC_MAX_SIZE ) - members =kzalloc (size,GFP_KERNEL | __GFP_NOWARN); - -if (members) { -pr_debug ("%p: allocated with kmalloc\n" , members); -return members; - } - - members =vzalloc (size); -if (!members) -return NULL ; -pr_debug ("%p: allocated with vmalloc\n" , members); - -return members; +return kvzalloc (size,GFP_KERNEL_ACCOUNT ); }EXPORT_SYMBOL_GPL (ip_set_alloc);
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 9/11 netfilter: nfnetlink: place subsys mutexes in distinct lockdep classes
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
From time to time there are lockdep reports similar to this one:
WARNING: possible circular locking dependency detected
------------------------------------------------------
000000004f61aa56 (&table[i].mutex){+.+.}, at: nfnl_lock [nfnetlink] but task is already holding lock:
[..] (&net->nft.commit_mutex){+.+.}, at: nf_tables_valid_genid [nf_tables] which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #1 (&net->nft.commit_mutex){+.+.}:
[..]
nf_tables_valid_genid+0x18/0x60 [nf_tables]
nfnetlink_rcv_batch+0x24c/0x620 [nfnetlink]
nfnetlink_rcv+0x110/0x140 [nfnetlink]
netlink_unicast+0x12c/0x1e0
[..]
sys_sendmsg+0x18/0x40
linux_sparc_syscall+0x34/0x44
-> #0 (&table[i].mutex){+.+.}:
[..]
nfnl_lock+0x24/0x40 [nfnetlink]
ip_set_nfnl_get_byindex+0x19c/0x280 [ip_set]
set_match_v1_checkentry+0x14/0xc0 [xt_set]
xt_check_match+0x238/0x260 [x_tables]
__nft_match_init+0x160/0x180 [nft_compat]
[..]
sys_sendmsg+0x18/0x40
linux_sparc_syscall+0x34/0x44
other info that might help us debug this:
Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&net->nft.commit_mutex);
lock(&table[i].mutex);
lock(&net->nft.commit_mutex);
lock(&table[i].mutex);Lockdep considers this an ABBA deadlock because the different nfnl subsys mutexes reside in the same lockdep class, but this is a false positive.
CPU1 table[i] refers to the nftables subsys mutex, whereas CPU1 locks the ipset subsys mutex.
Yi Che reported a similar lockdep splat, this time between ipset and ctnetlink subsys mutexes.
Time to place them in distinct classes to avoid these warnings.
--- net/netfilter/nfnetlink.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-)
net/netfilter/nfnetlink.c +18 -1
@@ -46,6 +46,23 @@ static struct {const struct nfnetlink_subsystem __rcu *subsys; } table[NFNL_SUBSYS_COUNT ]; +static struct lock_class_key nfnl_lockdep_keys[NFNL_SUBSYS_COUNT ]; + +static const char *const nfnl_lockdep_names[NFNL_SUBSYS_COUNT ] = { + [NFNL_SUBSYS_NONE ] ="nfnl_subsys_none" , + [NFNL_SUBSYS_CTNETLINK ] ="nfnl_subsys_ctnetlink" , + [NFNL_SUBSYS_CTNETLINK_EXP ] ="nfnl_subsys_ctnetlink_exp" , + [NFNL_SUBSYS_QUEUE ] ="nfnl_subsys_queue" , + [NFNL_SUBSYS_ULOG ] ="nfnl_subsys_ulog" , + [NFNL_SUBSYS_OSF ] ="nfnl_subsys_osf" , + [NFNL_SUBSYS_IPSET ] ="nfnl_subsys_ipset" , + [NFNL_SUBSYS_ACCT ] ="nfnl_subsys_acct" , + [NFNL_SUBSYS_CTNETLINK_TIMEOUT ] ="nfnl_subsys_cttimeout" , + [NFNL_SUBSYS_CTHELPER ] ="nfnl_subsys_cthelper" , + [NFNL_SUBSYS_NFTABLES ] ="nfnl_subsys_nftables" , + [NFNL_SUBSYS_NFT_COMPAT ] ="nfnl_subsys_nftcompat" , +}; +static const int nfnl_group2type[NFNLGRP_MAX +1 ] = { [NFNLGRP_CONNTRACK_NEW ] =NFNL_SUBSYS_CTNETLINK , [NFNLGRP_CONNTRACK_UPDATE ] =NFNL_SUBSYS_CTNETLINK ,
@@ -632,7 +649,7 @@ static int __init nfnetlink_init(void)BUG_ON (nfnl_group2type[i] ==NFNL_SUBSYS_NONE );for (i=0 ; i<NFNL_SUBSYS_COUNT ; i++) -mutex_init (&table[i].mutex); +__mutex_init (&table[i].mutex, nfnl_lockdep_names[i], &nfnl_lockdep_keys[i]);return register_pernet_subsys (&nfnetlink_net_ops); }
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 10/11 netfilter: nf_tables: Enable fast nft_cmp for inverted matches
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
Add a boolean indicating NFT_CMP_NEQ. To include it into the match decision, it is sufficient to XOR it with the data comparison's result.
While being at it, store the mask that is calculated during expression init and free the eval routine from having to recalculate it each time.
--- include/net/netfilter/nf_tables_core.h | 2 ++ net/netfilter/nf_tables_core.c | 3 +-- net/netfilter/nft_cmp.c | 13 +++++++------ 3 files changed, 10 insertions(+), 8 deletions(-)
include/net/netfilter/nf_tables_core.h +2 -0
@@ -25,8 +25,10 @@ void nf_tables_core_module_exit(void);struct nft_cmp_fast_expr {u32 data; +u32 mask;enum nft_registers sreg:8 ;u8 len; +bool inv; };struct nft_immediate_expr {
net/netfilter/nf_tables_core.c +1 -2
@@ -51,9 +51,8 @@ static void nft_cmp_fast_eval(const struct nft_expr *expr, struct nft_regs *regs) {const struct nft_cmp_fast_expr *priv =nft_expr_priv (expr); -u32 mask =nft_cmp_fast_mask (priv->len); -if ((regs->data[priv->sreg] & mask) == priv->data) +if (((regs->data[priv->sreg] & priv->mask) == priv->data) ^ priv->inv)return ; regs->verdict.code =NFT_BREAK ; }
net/netfilter/nft_cmp.c +7 -6
@@ -167,7 +167,6 @@ static int nft_cmp_fast_init(const struct nft_ctx *ctx,struct nft_cmp_fast_expr *priv =nft_expr_priv (expr);struct nft_data_desc desc;struct nft_data data; -u32 mask;int err; err = nft_data_init(NULL, &data, sizeof(data), &desc,
@@ -181,10 +180,11 @@ static int nft_cmp_fast_init(const struct nft_ctx *ctx,return err; desc.len *=BITS_PER_BYTE ; - mask =nft_cmp_fast_mask (desc.len); - priv->data = data.data[0 ] & mask; + priv->mask =nft_cmp_fast_mask (desc.len); + priv->data = data.data[0 ] & priv->mask; priv->len = desc.len; + priv->inv =ntohl (nla_get_be32 (tb[NFTA_CMP_OP ])) !=NFT_CMP_EQ ;return 0 ; }
@@ -201,7 +201,7 @@ static int nft_cmp_fast_offload(struct nft_offload_ctx *ctx, }, .sreg = priv->sreg, .len = priv->len / BITS_PER_BYTE, - .op = NFT_CMP_EQ, + .op = priv->inv ? NFT_CMP_NEQ : NFT_CMP_EQ, };return __nft_cmp_offload (ctx, flow, &cmp);
@@ -210,11 +210,12 @@ static int nft_cmp_fast_offload(struct nft_offload_ctx *ctx,static int nft_cmp_fast_dump (struct sk_buff *skb,const struct nft_expr *expr) {const struct nft_cmp_fast_expr *priv =nft_expr_priv (expr); +enum nft_cmp_ops op = priv->inv ?NFT_CMP_NEQ :NFT_CMP_EQ ;struct nft_data data;if (nft_dump_register (skb,NFTA_CMP_SREG , priv->sreg)) goto nla_put_failure; -if (nla_put_be32 (skb,NFTA_CMP_OP ,htonl (NFT_CMP_EQ ))) +if (nla_put_be32 (skb,NFTA_CMP_OP ,htonl (op))) goto nla_put_failure; data.data[0 ] = priv->data;
@@ -272,7 +273,7 @@ nft_cmp_select_ops(const struct nft_ctx *ctx, const struct nlattr * const tb[]) goto err1; } -if (desc.len <=sizeof (u32) && op ==NFT_CMP_EQ ) +if (desc.len <=sizeof (u32) && (op ==NFT_CMP_EQ || op ==NFT_CMP_NEQ ))return &nft_cmp_fast_ops;return &nft_cmp_ops;
Signature · 1 line
2.20.1
Pablo Neira Ayuso
maintainerPATCH 11/11 netfilter: nf_tables: Implement fast bitwise expression
In reply to Pablo Neira AyusoFrom: Phil Sutter <[email protected]>
A typical use of bitwise expression is to mask out parts of an IP address when matching on the network part only. Optimize for this common use with a fast variant for NFT_BITWISE_BOOL-type expressions operating on 32bit-sized values.
--- include/net/netfilter/nf_tables_core.h | 9 ++ net/netfilter/nf_tables_core.c | 12 +++ net/netfilter/nft_bitwise.c | 141 +++++++++++++++++++++++-- 3 files changed, 156 insertions(+), 6 deletions(-)
include/net/netfilter/nf_tables_core.h +9 -0
@@ -23,6 +23,13 @@ extern struct nft_object_type nft_secmark_obj_type;int nf_tables_core_module_init (void );void nf_tables_core_module_exit (void ); +struct nft_bitwise_fast_expr { +u32 mask; +u32 xor; +enum nft_registers sreg:8 ; +enum nft_registers dreg:8 ; +}; +struct nft_cmp_fast_expr {u32 data;u32 mask;
@@ -68,6 +75,8 @@ struct nft_payload_set {extern const struct nft_expr_ops nft_payload_fast_ops; +extern const struct nft_expr_ops nft_bitwise_fast_ops; +extern struct static_key_false nft_counters_enabled;extern struct static_key_false nft_trace_enabled;
net/netfilter/nf_tables_core.c +12 -0
@@ -47,6 +47,16 @@ static inline void nft_trace_packet(struct nft_traceinfo *info, } } +static void nft_bitwise_fast_eval (const struct nft_expr *expr, +struct nft_regs *regs) +{ +const struct nft_bitwise_fast_expr *priv =nft_expr_priv (expr); +u32 *src = ®s->data[priv->sreg]; +u32 *dst = ®s->data[priv->dreg]; + + *dst = (*src & priv->mask) ^ priv->xor; +} +static void nft_cmp_fast_eval (const struct nft_expr *expr,struct nft_regs *regs) {
@@ -175,6 +185,8 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)nft_rule_for_each_expr (expr, last, rule) {if (expr->ops == &nft_cmp_fast_ops)nft_cmp_fast_eval (expr, ®s); +else if (expr->ops == &nft_bitwise_fast_ops) +nft_bitwise_fast_eval (expr, ®s);else if (expr->ops != &nft_payload_fast_ops || !nft_payload_fast_eval (expr, ®s, pkt))expr_call_ops_eval (expr, ®s, pkt);
net/netfilter/nft_bitwise.c +135 -6
155 lines not on this page · raw message
Signature · 1 line
2.20.1
David Miller
maintainerFrom: Pablo Neira Ayuso <[email protected]>
Date: Sun, 4 Oct 2020 21:49:29 +0200
Quoted 1 line
git://git.kernel.org/pub/scm/linux/kernel/git/pablo/nf-next.git
Pulled, thank you.