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) Extended netlink error reporting if nfnetlink attribute parser fails, from Donald Hunter.
2) Incorrect request_module() module, from Simon Horman.
3) A series of patches to reduce memory consumption for set element transactions. Florian Westphal says:
"When doing a flush on a set or mass adding/removing elements from a set, each element needs to allocate 96 bytes to hold the transactional state.
In such cases, virtually all the information in struct nft_trans_elem is the same.
Change nft_trans_elem to a flex-array, i.e. a single nft_trans_elem can hold multiple set element pointers.
The number of elements that can be stored in one nft_trans_elem is limited by the slab allocator, this series limits the compaction to at most 62 elements as it caps the reallocation to 2048 bytes of memory."
4) A series of patches to prepare the transition to dscp_t in .flowi_tos. From Guillaume Nault.
5) Support for bitwise operations with two source registers, from Jeremy Sowden.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git nf-next-24-11-15
Thanks.
----------------------------------------------------------------
The following changes since commit 544070db6c8b0c403e4c6befbc76b52831b897da:
Merge branch 'mlx5-esw-qos-refactor-and-shampo-cleanup' (2024-11-11 19:28:20 -0800)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git tags/nf-next-24-11-15
for you to fetch changes up to b0ccf4f53d968e794a4ea579d5135cc1aaf1a53f:
netfilter: bitwise: add support for doing AND, OR and XOR directly (2024-11-15 12:07:04 +0100)
----------------------------------------------------------------
netfilter pull request 24-11-15
----------------------------------------------------------------
Donald Hunter (1):
netfilter: nfnetlink: Report extack policy errors for batched ops
Florian Westphal (5):
netfilter: nf_tables: add nft_trans_commit_list_add_elem helper
netfilter: nf_tables: prepare for multiple elements in nft_trans_elem structure
netfilter: nf_tables: prepare nft audit for set element compaction
netfilter: nf_tables: switch trans_elem to real flex array
netfilter: nf_tables: allocate element update information dynamically
Guillaume Nault (5):
netfilter: ipv4: Convert ip_route_me_harder() to dscp_t.
netfilter: flow_offload: Convert nft_flow_route() to dscp_t.
netfilter: rpfilter: Convert rpfilter_mt() to dscp_t.
netfilter: nft_fib: Convert nft_fib4_eval() to dscp_t.
netfilter: nf_dup4: Convert nf_dup_ipv4_route() to dscp_t.
Jeremy Sowden (2):
netfilter: bitwise: rename some boolean operation functions
netfilter: bitwise: add support for doing AND, OR and XOR directly
Simon Horman (1):
netfilter: bpf: Pass string literal as format argument of request_module()
include/net/netfilter/nf_tables.h | 25 +- include/uapi/linux/netfilter/nf_tables.h | 18 +- net/ipv4/netfilter.c | 2 +- net/ipv4/netfilter/ipt_rpfilter.c | 2 +- net/ipv4/netfilter/nf_dup_ipv4.c | 2 +- net/ipv4/netfilter/nft_fib_ipv4.c | 3 +- net/netfilter/nf_bpf_link.c | 2 +- net/netfilter/nf_tables_api.c | 385 ++++++++++++++++++++++++------- net/netfilter/nfnetlink.c | 2 +- net/netfilter/nft_bitwise.c | 166 ++++++++++--- net/netfilter/nft_flow_offload.c | 4 +- 11 files changed, 484 insertions(+), 127 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/14 netfilter: nfnetlink: Report extack policy errors for batched ops
From: Donald Hunter <[email protected]>
The nftables batch processing does not currently populate extack with policy errors. Fix this by passing extack when parsing batch messages.
--- net/netfilter/nfnetlink.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nfnetlink.c +1 -1
@@ -517,7 +517,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, err =nla_parse_deprecated (cda, ss->cb[cb_id].attr_count, attr, attrlen, - ss->cb[cb_id].policy,NULL ); + ss->cb[cb_id].policy, &extack);if (err <0 ) goto ack;
Signature · 1 line
2.30.2
Re: [PATCH net-next 01/14] netfilter: nfnetlink: Report extack policy errors for batched ops
This series was applied to netdev/net-next.git (main) by Pablo Neira Ayuso <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Pablo Neira Ayuso <[email protected]>:
Quoted 10 lines · On Fri, 15 Nov 2024 14:31:54 +0100 you wrote:
From: Donald Hunter <[email protected]>
The nftables batch processing does not currently populate extack with policy errors. Fix this by passing extack when parsing batch messages.
Signed-off-by Donald Hunter <[email protected]>Reviewed-by Simon Horman <[email protected]>Signed-off-by Pablo Neira Ayuso <[email protected]>[...]
Here is the summary with links:
- [net-next,01/14] netfilter: nfnetlink: Report extack policy errors for batched ops
https://git.kernel.org/netdev/net-next/c/3f5495962824 - [net-next,02/14] netfilter: bpf: Pass string literal as format argument of request_module() https://git.kernel.org/netdev/net-next/c/8340b0056ac7 - [net-next,03/14] netfilter: nf_tables: add nft_trans_commit_list_add_elem helper https://git.kernel.org/netdev/net-next/c/4ee29181216d - [net-next,04/14] netfilter: nf_tables: prepare for multiple elements in nft_trans_elem structure https://git.kernel.org/netdev/net-next/c/a8ee6b900c14 - [net-next,05/14] netfilter: nf_tables: prepare nft audit for set element compaction https://git.kernel.org/netdev/net-next/c/466c9b3b2a92 - [net-next,06/14] netfilter: nf_tables: switch trans_elem to real flex array https://git.kernel.org/netdev/net-next/c/b0c49466043a - [net-next,07/14] netfilter: nf_tables: allocate element update information dynamically https://git.kernel.org/netdev/net-next/c/508180850b73 - [net-next,08/14] netfilter: ipv4: Convert ip_route_me_harder() to dscp_t. https://git.kernel.org/netdev/net-next/c/0608746f95b2 - [net-next,09/14] netfilter: flow_offload: Convert nft_flow_route() to dscp_t. https://git.kernel.org/netdev/net-next/c/6f9615a6e686 - [net-next,10/14] netfilter: rpfilter: Convert rpfilter_mt() to dscp_t. https://git.kernel.org/netdev/net-next/c/f694ce6de589 - [net-next,11/14] netfilter: nft_fib: Convert nft_fib4_eval() to dscp_t. https://git.kernel.org/netdev/net-next/c/f12b67cc7d1b - [net-next,12/14] netfilter: nf_dup4: Convert nf_dup_ipv4_route() to dscp_t. https://git.kernel.org/netdev/net-next/c/f0d839c13ed5 - [net-next,13/14] netfilter: bitwise: rename some boolean operation functions https://git.kernel.org/netdev/net-next/c/a12143e6084c - [net-next,14/14] netfilter: bitwise: add support for doing AND, OR and XOR directly https://git.kernel.org/netdev/net-next/c/b0ccf4f53d96
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Pablo Neira Ayuso
maintainerPATCH 2/14 netfilter: bpf: Pass string literal as format argument of request_module()
In reply to Pablo Neira AyusoFrom: Simon Horman <[email protected]>
Both gcc-14 and clang-18 report that passing a non-string literal as the format argument of request_module() is potentially insecure.
E.g. clang-18 says:
.../nf_bpf_link.c:46:24: warning: format string is not a string literal (potentially insecure) [-Wformat-security]
46 | err = request_module(mod);
| ^~~.../kmod.h:25:55: note: expanded from macro 'request_module'
25 | #define request_module(mod...) __request_module(true, mod)
| ^~~.../nf_bpf_link.c:46:24: note: treat the string as an argument to avoid this
46 | err = request_module(mod);
| ^
| "%s",.../kmod.h:25:55: note: expanded from macro 'request_module'
25 | #define request_module(mod...) __request_module(true, mod)
| ^
It is always the case where the contents of mod is safe to pass as the format argument. That is, in my understanding, it never contains any format escape sequences.
But, it seems better to be safe than sorry. And, as a bonus, compiler output becomes less verbose by addressing this issue as suggested by clang-18.
Compile tested only.
--- net/netfilter/nf_bpf_link.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/netfilter/nf_bpf_link.c +1 -1
@@ -43,7 +43,7 @@ get_proto_defrag_hook(struct bpf_nf_link *link, hook =rcu_dereference (*ptr_global_hook);if (!hook) {rcu_read_unlock (); - err =request_module (mod); + err =request_module ("%s" , mod);if (err)return ERR_PTR (err <0 ? err : -EINVAL );
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/14 netfilter: nf_tables: add nft_trans_commit_list_add_elem helper
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Add and use a wrapper to append trans_elem structures to the transaction log.
Unlike the existing helper, pass a gfp_t to indicate if sleeping is allowed.
This will be used by a followup patch to realloc nft_trans_elem structures after they gain a flexible array member to reduce number of such container structures on the transaction list.
--- net/netfilter/nf_tables_api.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-)
net/netfilter/nf_tables_api.c +16 -5
@@ -421,6 +421,17 @@ static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *tr } } +static void nft_trans_commit_list_add_elem (struct net *net,struct nft_trans *trans, +gfp_t gfp) +{ +WARN_ON_ONCE (trans->msg_type !=NFT_MSG_NEWSETELEM && + trans->msg_type !=NFT_MSG_DELSETELEM ); + +might_alloc (gfp); + +nft_trans_commit_list_add_tail (net, trans); +} +static int nft_trans_table_add (struct nft_ctx *ctx,int msg_type) {struct nft_trans *trans;
@@ -7205,7 +7216,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,if (update_flags) {nft_trans_elem_priv (trans) = elem_priv;nft_trans_elem_update_flags (trans) = update_flags; -nft_trans_commit_list_add_tail (ctx->net, trans); +nft_trans_commit_list_add_elem (ctx->net, trans,GFP_KERNEL ); goto err_elem_free; } }
@@ -7229,7 +7240,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set, }nft_trans_elem_priv (trans) = elem.priv; -nft_trans_commit_list_add_tail (ctx->net, trans); +nft_trans_commit_list_add_elem (ctx->net, trans,GFP_KERNEL );return 0 ; err_set_full:
@@ -7446,7 +7457,7 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,nft_setelem_data_deactivate (ctx->net, set, elem.priv);nft_trans_elem_priv (trans) = elem.priv; -nft_trans_commit_list_add_tail (ctx->net, trans); +nft_trans_commit_list_add_elem (ctx->net, trans,GFP_KERNEL );return 0 ; fail_ops:
@@ -7482,7 +7493,7 @@ static int nft_setelem_flush(const struct nft_ctx *ctx,nft_setelem_data_deactivate (ctx->net, set, elem_priv);nft_trans_elem_set (trans) = set;nft_trans_elem_priv (trans) = elem_priv; -nft_trans_commit_list_add_tail (ctx->net, trans); +nft_trans_commit_list_add_elem (ctx->net, trans,GFP_ATOMIC );return 0 ; }
@@ -7499,7 +7510,7 @@ static int __nft_set_catchall_flush(const struct nft_ctx *ctx,nft_setelem_data_deactivate (ctx->net, set, elem_priv);nft_trans_elem_priv (trans) = elem_priv; -nft_trans_commit_list_add_tail (ctx->net, trans); +nft_trans_commit_list_add_elem (ctx->net, trans,GFP_KERNEL );return 0 ; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/14 netfilter: nf_tables: prepare for multiple elements in nft_trans_elem structure
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Add helpers to release the individual elements contained in the trans_elem container structure.
No functional change intended.
Followup patch will add 'nelems' member and will turn 'priv' into a flexible array.
These helpers can then loop over all elements.
Care needs to be taken to handle a mix of new elements and existing elements that are being updated (e.g. timeout refresh).
Before this patch, NEWSETELEM transaction with update is released early so nft_trans_set_elem_destroy() won't get called, so we need to skip elements marked as update.
--- include/net/netfilter/nf_tables.h | 21 ++- net/netfilter/nf_tables_api.c | 228 +++++++++++++++++++++--------- 2 files changed, 173 insertions(+), 76 deletions(-)
include/net/netfilter/nf_tables.h +9 -12
@@ -1759,28 +1759,25 @@ enum nft_trans_elem_flags { NFT_TRANS_UPD_EXPIRATION = (1 << 1), }; -struct nft_trans_elem { -struct nft_trans nft_trans; -struct nft_set *set; -struct nft_elem_priv *elem_priv; +struct nft_trans_one_elem { +struct nft_elem_priv *priv;u64 timeout;u64 expiration;u8 update_flags; +}; + +struct nft_trans_elem { +struct nft_trans nft_trans; +struct nft_set *set;bool bound; +unsigned int nelems; + struct nft_trans_one_elem elems[] __counted_by(nelems); };#define nft_trans_container_elem (t) \ container_of(t, struct nft_trans_elem, nft_trans)#define nft_trans_elem_set (trans) \ nft_trans_container_elem(trans)->set -#define nft_trans_elem_priv (trans) \ - nft_trans_container_elem(trans)->elem_priv -#define nft_trans_elem_update_flags (trans) \ - nft_trans_container_elem(trans)->update_flags -#define nft_trans_elem_timeout (trans) \ - nft_trans_container_elem(trans)->timeout -#define nft_trans_elem_expiration (trans) \ - nft_trans_container_elem(trans)->expiration#define nft_trans_elem_set_bound (trans) \ nft_trans_container_elem(trans)->bound
net/netfilter/nf_tables_api.c +164 -64
372 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/14 netfilter: nf_tables: prepare nft audit for set element compaction
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
nftables audit log format emits the number of added/deleted rules, sets, set elements and so on, to userspace:
table=t1 family=2 entries=4 op=nft_register_set
~~~~~~~~~
At this time, the 'entries' key is the number of transactions that will be applied.
The upcoming set element compression will coalesce subsequent adds/deletes to the same set requests in the same transaction request to conseve memory.
Without this patch, we'd under-report the number of altered elements.
Increment the audit counter by the number of elements to keep the reported entries value the same.
Without this, nft_audit.sh selftest fails because the recorded (expected) entries key is smaller than the expected one.
--- net/netfilter/nf_tables_api.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-)
net/netfilter/nf_tables_api.c +18 -3
@@ -10398,9 +10398,24 @@ static void nf_tables_commit_audit_free(struct list_head *adl) } } +/* nft audit emits the number of elements that get added/removed/updated, +* so NEW/DELSETELEM needs to increment based on the total elem count. +*/ +static unsigned int nf_tables_commit_audit_entrycount (const struct nft_trans *trans) +{ +switch (trans->msg_type) { +case NFT_MSG_NEWSETELEM : +case NFT_MSG_DELSETELEM : +return nft_trans_container_elem (trans)->nelems; + } + +return 1 ; +} +static void nf_tables_commit_audit_collect (struct list_head *adl, -struct nft_table *table,u32 op) +const struct nft_trans *trans,u32 op) { +const struct nft_table *table = trans->table;struct nft_audit_data *adp;list_for_each_entry (adp, adl, list) {
@@ -10410,7 +10425,7 @@ static void nf_tables_commit_audit_collect(struct list_head *adl,WARN_ONCE (1 ,"table=%s not expected in commit list" , table->name);return ; found: - adp->entries++; + adp->entries +=nf_tables_commit_audit_entrycount (trans);if (!adp->op || adp->op > op) adp->op = op; }
@@ -10569,7 +10584,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)nft_ctx_update (&ctx, trans); -nf_tables_commit_audit_collect (&adl, table, trans->msg_type); +nf_tables_commit_audit_collect (&adl, trans, trans->msg_type);switch (trans->msg_type) {case NFT_MSG_NEWTABLE :if (nft_trans_table_update (trans)) {
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 6/14 netfilter: nf_tables: switch trans_elem to real flex array
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
When queueing a set element add or removal operation to the transaction log, check if the previous operation already asks for a the identical operation on the same set.
If so, store the element reference in the preceding operation. This significantlty reduces memory consumption when many set add/delete operations appear in a single transaction.
Example: 10k elements require 937kb of memory (10k allocations from kmalloc-96 slab).
Assuming we can compact 4 elements in the same set, 468 kbytes are needed (64 bytes for base struct, nft_trans_elemn, 32 bytes for nft_trans_one_elem structure, so 2500 allocations from kmalloc-192 slab).
For large batch updates we can compact up to 62 elements
into one single nft_trans_elem structure (~65% mem reduction):
(64 bytes for base struct, nft_trans_elem, 32 byte for nft_trans_one_elem
struct).
We can halve size of nft_trans_one_elem struct by moving timeout/expire/update_flags into a dynamically allocated structure, this allows to store 124 elements in a 2k slab nft_trans_elem struct. This is done in a followup patch.
--- net/netfilter/nf_tables_api.c | 90 +++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+)
net/netfilter/nf_tables_api.c +90 -0
@@ -26,6 +26,9 @@#define NFT_MODULE_AUTOLOAD_LIMIT (MODULE_NAME_LEN - sizeof("nft-expr-255-"))#define NFT_SET_MAX_ANONLEN 16 +/* limit compaction to avoid huge kmalloc/krealloc sizes. */ +#define NFT_MAX_SET_NELEMS ((2048 - sizeof(struct nft_trans_elem)) / sizeof(struct nft_trans_one_elem)) +unsigned int nf_tables_net_id__read_mostly ;static LIST_HEAD (nf_tables_expressions);
@@ -391,6 +394,86 @@ static void nf_tables_unregister_hook(struct net *net,return __nf_tables_unregister_hook (net, table, chain, false); } +static bool nft_trans_collapse_set_elem_allowed (const struct nft_trans_elem *a,const struct nft_trans_elem *b) +{ +/* NB: the ->bound equality check is defensive, at this time we only merge +* a new nft_trans_elem transaction request with the transaction tail +* element, but a->bound != b->bound would imply a NEWRULE transaction +* is queued in-between. +* +* The set check is mandatory, the NFT_MAX_SET_NELEMS check prevents +* huge krealloc() requests. +*/ +return a->set == b->set && a->bound == b->bound && a->nelems <NFT_MAX_SET_NELEMS ; +} + +static bool nft_trans_collapse_set_elem (struct nftables_pernet *nft_net, +struct nft_trans_elem *tail, +struct nft_trans_elem *trans, +gfp_t gfp) +{ +unsigned int nelems, old_nelems = tail->nelems; +struct nft_trans_elem *new_trans; + +if (!nft_trans_collapse_set_elem_allowed (tail, trans)) +return false; + +/* "cannot happen", at this time userspace element add +* requests always allocate a new transaction element. +* +* This serves as a reminder to adjust the list_add_tail +* logic below in case this ever changes. +*/ +if (WARN_ON_ONCE (trans->nelems !=1 )) +return false; + +if (check_add_overflow (old_nelems, trans->nelems, &nelems)) +return false; + +/* krealloc might free tail which invalidates list pointers */ +list_del_init (&tail->nft_trans.list); + + new_trans =krealloc (tail,struct_size (tail, elems, nelems), gfp); +if (!new_trans) { +list_add_tail (&tail->nft_trans.list, &nft_net->commit_list); +return false; + } + +/* +* new_trans->nft_trans.list contains garbage, but +* list_add_tail() doesn't care. +*/ + new_trans->nelems = nelems; + new_trans->elems[old_nelems] = trans->elems[0 ]; +list_add_tail (&new_trans->nft_trans.list, &nft_net->commit_list); + +return true; +} + +static bool nft_trans_try_collapse (struct nftables_pernet *nft_net, +struct nft_trans *trans,gfp_t gfp) +{ +struct nft_trans *tail; + +if (list_empty (&nft_net->commit_list)) +return false; + + tail = list_last_entry(&nft_net->commit_list, struct nft_trans, list); + +if (tail->msg_type != trans->msg_type) +return false; + +switch (trans->msg_type) { +case NFT_MSG_NEWSETELEM : +case NFT_MSG_DELSETELEM : +return nft_trans_collapse_set_elem (nft_net, +nft_trans_container_elem (tail), +nft_trans_container_elem (trans), gfp); + } + +return false; +} +static void nft_trans_commit_list_add_tail (struct net *net,struct nft_trans *trans) {struct nftables_pernet *nft_net =nft_pernet (net);
@@ -424,11 +507,18 @@ static void nft_trans_commit_list_add_tail(struct net *net, struct nft_trans *trstatic void nft_trans_commit_list_add_elem (struct net *net,struct nft_trans *trans,gfp_t gfp) { +struct nftables_pernet *nft_net =nft_pernet (net); +WARN_ON_ONCE (trans->msg_type !=NFT_MSG_NEWSETELEM && trans->msg_type !=NFT_MSG_DELSETELEM );might_alloc (gfp); +if (nft_trans_try_collapse (nft_net, trans, gfp)) { +kfree (trans); +return ; + } +nft_trans_commit_list_add_tail (net, trans); }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 7/14 netfilter: nf_tables: allocate element update information dynamically
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Move the timeout/expire/flag members from nft_trans_one_elem struct into a dybamically allocated structure, only needed when timeout update was requested.
This halves size of nft_trans_one_elem struct and allows to compact up to 124 elements in one transaction container rather than 62.
This halves memory requirements for a large flush or insert transaction, where ->update remains NULL.
Care has to be taken to release the extra data in all spots, including abort path.
--- include/net/netfilter/nf_tables.h | 10 ++++-- net/netfilter/nf_tables_api.c | 57 +++++++++++++++++++------------ 2 files changed, 43 insertions(+), 24 deletions(-)
include/net/netfilter/nf_tables.h +7 -3
@@ -1759,11 +1759,15 @@ enum nft_trans_elem_flags { NFT_TRANS_UPD_EXPIRATION = (1 << 1), }; -struct nft_trans_one_elem { -struct nft_elem_priv *priv; +struct nft_elem_update {u64 timeout;u64 expiration; -u8 update_flags; +u8 flags; +}; + +struct nft_trans_one_elem { +struct nft_elem_priv *priv; +struct nft_elem_update *update; };struct nft_trans_elem {
net/netfilter/nf_tables_api.c +36 -21
123 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 8/14 netfilter: ipv4: Convert ip_route_me_harder() to dscp_t.
In reply to Pablo Neira AyusoFrom: Guillaume Nault <[email protected]>
Use ip4h_dscp()instead of reading iph->tos directly.
ip4h_dscp() returns a dscp_t value which is temporarily converted back to __u8 with inet_dscp_to_dsfield(). When converting ->flowi4_tos to dscp_t in the future, we'll only have to remove that inet_dscp_to_dsfield() call.
--- net/ipv4/netfilter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/netfilter.c +1 -1
@@ -44,7 +44,7 @@ int ip_route_me_harder(struct net *net, struct sock *sk, struct sk_buff *skb, un*/ fl4.daddr = iph->daddr; fl4.saddr = saddr; - fl4.flowi4_tos = iph->tos &INET_DSCP_MASK ; + fl4.flowi4_tos =inet_dscp_to_dsfield (ip4h_dscp (iph)); fl4.flowi4_oif = sk ? sk->sk_bound_dev_if :0 ; fl4.flowi4_l3mdev =l3mdev_master_ifindex (dev); fl4.flowi4_mark = skb->mark;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 9/14 netfilter: flow_offload: Convert nft_flow_route() to dscp_t.
In reply to Pablo Neira AyusoFrom: Guillaume Nault <[email protected]>
Use ip4h_dscp()instead of reading ip_hdr()->tos directly.
ip4h_dscp() returns a dscp_t value which is temporarily converted back to __u8 with inet_dscp_to_dsfield(). When converting ->flowi4_tos to dscp_t in the future, we'll only have to remove that inet_dscp_to_dsfield() call.
Also, remove the comment about the net/ip.h include file, since it's now required for the ip4h_dscp() helper too.
--- net/netfilter/nft_flow_offload.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
net/netfilter/nft_flow_offload.c +2 -2
@@ -8,7 +8,7 @@#include <linux/spinlock.h> #include <linux/netfilter/nf_conntrack_common.h> #include <linux/netfilter/nf_tables.h> -#include <net/ip.h> /* for ipv4 options. */ +#include <net/ip.h> #include <net/inet_dscp.h> #include <net/netfilter/nf_tables.h> #include <net/netfilter/nf_tables_core.h>
@@ -236,7 +236,7 @@ static int nft_flow_route(const struct nft_pktinfo *pkt, fl.u.ip4.saddr = ct->tuplehash[!dir].tuple.src.u3.ip; fl.u.ip4.flowi4_oif =nft_in (pkt)->ifindex; fl.u.ip4.flowi4_iif = this_dst->dev->ifindex; - fl.u.ip4.flowi4_tos =ip_hdr (pkt->skb)->tos &INET_DSCP_MASK ; + fl.u.ip4.flowi4_tos =inet_dscp_to_dsfield (ip4h_dscp (ip_hdr (pkt->skb))); fl.u.ip4.flowi4_mark = pkt->skb->mark; fl.u.ip4.flowi4_flags =FLOWI_FLAG_ANYSRC ;break ;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 10/14 netfilter: rpfilter: Convert rpfilter_mt() to dscp_t.
In reply to Pablo Neira AyusoFrom: Guillaume Nault <[email protected]>
Use ip4h_dscp() instead of reading iph->tos directly.
ip4h_dscp() returns a dscp_t value which is temporarily converted back to __u8 with inet_dscp_to_dsfield(). When converting ->flowi4_tos to dscp_t in the future, we'll only have to remove that inet_dscp_to_dsfield() call.
--- net/ipv4/netfilter/ipt_rpfilter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/netfilter/ipt_rpfilter.c +1 -1
@@ -76,7 +76,7 @@ static bool rpfilter_mt(const struct sk_buff *skb, struct xt_action_param *par) flow.daddr = iph->saddr; flow.saddr =rpfilter_get_saddr (iph->daddr); flow.flowi4_mark = info->flags &XT_RPFILTER_VALID_MARK ? skb->mark :0 ; - flow.flowi4_tos = iph->tos &INET_DSCP_MASK ; + flow.flowi4_tos =inet_dscp_to_dsfield (ip4h_dscp (iph)); flow.flowi4_scope =RT_SCOPE_UNIVERSE ; flow.flowi4_l3mdev =l3mdev_master_ifindex_rcu (xt_in (par)); flow.flowi4_uid =sock_net_uid (xt_net (par),NULL );
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 11/14 netfilter: nft_fib: Convert nft_fib4_eval() to dscp_t.
In reply to Pablo Neira AyusoFrom: Guillaume Nault <[email protected]>
Use ip4h_dscp() instead of reading iph->tos directly.
ip4h_dscp() returns a dscp_t value which is temporarily converted back to __u8 with inet_dscp_to_dsfield(). When converting ->flowi4_tos to dscp_t in the future, we'll only have to remove that inet_dscp_to_dsfield() call.
--- net/ipv4/netfilter/nft_fib_ipv4.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
net/ipv4/netfilter/nft_fib_ipv4.c +2 -1
@@ -11,6 +11,7 @@#include <net/netfilter/nft_fib.h> #include <net/inet_dscp.h> +#include <net/ip.h> #include <net/ip_fib.h> #include <net/route.h>
@@ -107,7 +108,7 @@ void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,if (priv->flags &NFTA_FIB_F_MARK ) fl4.flowi4_mark = pkt->skb->mark; - fl4.flowi4_tos = iph->tos &INET_DSCP_MASK ; + fl4.flowi4_tos =inet_dscp_to_dsfield (ip4h_dscp (iph));if (priv->flags &NFTA_FIB_F_DADDR ) { fl4.daddr = iph->daddr;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 12/14 netfilter: nf_dup4: Convert nf_dup_ipv4_route() to dscp_t.
In reply to Pablo Neira AyusoFrom: Guillaume Nault <[email protected]>
Use ip4h_dscp() instead of reading iph->tos directly.
ip4h_dscp() returns a dscp_t value which is temporarily converted back to __u8 with inet_dscp_to_dsfield(). When converting ->flowi4_tos to dscp_t in the future, we'll only have to remove that inet_dscp_to_dsfield() call.
--- net/ipv4/netfilter/nf_dup_ipv4.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/netfilter/nf_dup_ipv4.c +1 -1
@@ -33,7 +33,7 @@ static bool nf_dup_ipv4_route(struct net *net, struct sk_buff *skb, fl4.flowi4_oif = oif; fl4.daddr = gw->s_addr; - fl4.flowi4_tos = iph->tos &INET_DSCP_MASK ; + fl4.flowi4_tos =inet_dscp_to_dsfield (ip4h_dscp (iph)); fl4.flowi4_scope =RT_SCOPE_UNIVERSE ; fl4.flowi4_flags =FLOWI_FLAG_KNOWN_NH ; rt =ip_route_output_key (net, &fl4);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 13/14 netfilter: bitwise: rename some boolean operation functions
In reply to Pablo Neira AyusoFrom: Jeremy Sowden <[email protected]>
In the next patch we add support for doing AND, OR and XOR operations directly in the kernel, so rename some functions and an enum constant related to mask-and-xor boolean operations.
--- include/uapi/linux/netfilter/nf_tables.h | 10 ++++--- net/netfilter/nft_bitwise.c | 34 ++++++++++++------------ 2 files changed, 24 insertions(+), 20 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +7 -3
@@ -564,16 +564,20 @@ enum nft_immediate_attributes {/** * enum nft_bitwise_ops - nf_tables bitwise operations * -* @NFT_BITWISE_BOOL: mask-and-xor operation used to implement NOT, AND, OR and -* XOR boolean operations +* @NFT_BITWISE_MASK_XOR: mask-and-xor operation used to implement NOT, AND, OR +* and XOR boolean operations * @NFT_BITWISE_LSHIFT: left-shift operation * @NFT_BITWISE_RSHIFT: right-shift operation */ enum nft_bitwise_ops { -NFT_BITWISE_BOOL , +NFT_BITWISE_MASK_XOR ,NFT_BITWISE_LSHIFT ,NFT_BITWISE_RSHIFT , }; +/* +* Old name for NFT_BITWISE_MASK_XOR. Retained for backwards-compatibility. +*/ +#define NFT_BITWISE_BOOL NFT_BITWISE_MASK_XOR/** * enum nft_bitwise_attributes - nf_tables bitwise expression netlink attributes
net/netfilter/nft_bitwise.c +17 -17
@@ -25,8 +25,8 @@ struct nft_bitwise {struct nft_data data; }; -static void nft_bitwise_eval_bool (u32 *dst,const u32 *src, -const struct nft_bitwise *priv) +static void nft_bitwise_eval_mask_xor (u32 *dst,const u32 *src, +const struct nft_bitwise *priv) {unsigned int i;
@@ -68,8 +68,8 @@ void nft_bitwise_eval(const struct nft_expr *expr,u32 *dst = ®s->data[priv->dreg];switch (priv->op) { -case NFT_BITWISE_BOOL : -nft_bitwise_eval_bool (dst, src, priv); +case NFT_BITWISE_MASK_XOR : +nft_bitwise_eval_mask_xor (dst, src, priv);break ;case NFT_BITWISE_LSHIFT :nft_bitwise_eval_lshift (dst, src, priv);
@@ -90,8 +90,8 @@ static const struct nla_policy nft_bitwise_policy[NFTA_BITWISE_MAX + 1] = { [NFTA_BITWISE_DATA] = { .type = NLA_NESTED }, }; -static int nft_bitwise_init_bool (struct nft_bitwise *priv, -const struct nlattr *const tb[]) +static int nft_bitwise_init_mask_xor (struct nft_bitwise *priv, +const struct nlattr *const tb[]) {struct nft_data_desc mask = { .type =NFT_DATA_VALUE ,
@@ -185,7 +185,7 @@ static int nft_bitwise_init(const struct nft_ctx *ctx,if (tb[NFTA_BITWISE_OP ]) { priv->op =ntohl (nla_get_be32 (tb[NFTA_BITWISE_OP ]));switch (priv->op) { -case NFT_BITWISE_BOOL : +case NFT_BITWISE_MASK_XOR :case NFT_BITWISE_LSHIFT :case NFT_BITWISE_RSHIFT :break ;
@@ -193,12 +193,12 @@ static int nft_bitwise_init(const struct nft_ctx *ctx,return -EOPNOTSUPP ; } }else { - priv->op =NFT_BITWISE_BOOL ; + priv->op =NFT_BITWISE_MASK_XOR ; }switch (priv->op) { -case NFT_BITWISE_BOOL : - err =nft_bitwise_init_bool (priv, tb); +case NFT_BITWISE_MASK_XOR : + err =nft_bitwise_init_mask_xor (priv, tb);break ;case NFT_BITWISE_LSHIFT :case NFT_BITWISE_RSHIFT :
@@ -209,8 +209,8 @@ static int nft_bitwise_init(const struct nft_ctx *ctx,return err; } -static int nft_bitwise_dump_bool (struct sk_buff *skb, -const struct nft_bitwise *priv) +static int nft_bitwise_dump_mask_xor (struct sk_buff *skb, +const struct nft_bitwise *priv) {if (nft_data_dump (skb,NFTA_BITWISE_MASK , &priv->mask,NFT_DATA_VALUE , priv->len) <0 )
@@ -248,8 +248,8 @@ static int nft_bitwise_dump(struct sk_buff *skb,return -1 ;switch (priv->op) { -case NFT_BITWISE_BOOL : - err =nft_bitwise_dump_bool (skb, priv); +case NFT_BITWISE_MASK_XOR : + err =nft_bitwise_dump_mask_xor (skb, priv);break ;case NFT_BITWISE_LSHIFT :case NFT_BITWISE_RSHIFT :
@@ -269,7 +269,7 @@ static int nft_bitwise_offload(struct nft_offload_ctx *ctx,const struct nft_bitwise *priv =nft_expr_priv (expr);struct nft_offload_reg *reg = &ctx->regs[priv->dreg]; -if (priv->op !=NFT_BITWISE_BOOL ) +if (priv->op !=NFT_BITWISE_MASK_XOR )return -EOPNOTSUPP ; if (memcmp(&priv->xor, &zero, sizeof(priv->xor)) ||
@@ -406,7 +406,7 @@ nft_bitwise_fast_dump(struct sk_buff *skb,return -1 ;if (nla_put_be32 (skb,NFTA_BITWISE_LEN ,htonl (sizeof (u32))))return -1 ; -if (nla_put_be32 (skb,NFTA_BITWISE_OP ,htonl (NFT_BITWISE_BOOL ))) +if (nla_put_be32 (skb,NFTA_BITWISE_OP ,htonl (NFT_BITWISE_MASK_XOR )))return -1 ; data.data[0 ] = priv->mask;
@@ -501,7 +501,7 @@ nft_bitwise_select_ops(const struct nft_ctx *ctx,return &nft_bitwise_ops;if (tb[NFTA_BITWISE_OP ] && -ntohl (nla_get_be32 (tb[NFTA_BITWISE_OP ])) !=NFT_BITWISE_BOOL ) +ntohl (nla_get_be32 (tb[NFTA_BITWISE_OP ])) !=NFT_BITWISE_MASK_XOR )return &nft_bitwise_ops;return &nft_bitwise_fast_ops;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 14/14 netfilter: bitwise: add support for doing AND, OR and XOR directly
In reply to Pablo Neira AyusoFrom: Jeremy Sowden <[email protected]>
Hitherto, these operations have been converted in user space to mask-and-xor operations on one register and two immediate values, and it is the latter which have been evaluated by the kernel. We add support for evaluating these operations directly in kernel space on one register and either an immediate value or a second register.
Pablo made a few changes to the original patch:
- EINVAL if NFTA_BITWISE_SREG2 is used with fast version.
- Allow _AND,_OR,_XOR with _DATA != sizeof(u32)
- Dump _SREG2 or _DATA with _AND,_OR,_XOR
--- include/uapi/linux/netfilter/nf_tables.h | 8 ++ net/netfilter/nft_bitwise.c | 134 +++++++++++++++++++++-- 2 files changed, 131 insertions(+), 11 deletions(-)
include/uapi/linux/netfilter/nf_tables.h +8 -0
@@ -568,11 +568,17 @@ enum nft_immediate_attributes {* and XOR boolean operations * @NFT_BITWISE_LSHIFT: left-shift operation * @NFT_BITWISE_RSHIFT: right-shift operation +* @NFT_BITWISE_AND: and operation +* @NFT_BITWISE_OR: or operation +* @NFT_BITWISE_XOR: xor operation */ enum nft_bitwise_ops {NFT_BITWISE_MASK_XOR ,NFT_BITWISE_LSHIFT ,NFT_BITWISE_RSHIFT , +NFT_BITWISE_AND , +NFT_BITWISE_OR , +NFT_BITWISE_XOR , };/* * Old name for NFT_BITWISE_MASK_XOR. Retained for backwards-compatibility.
@@ -590,6 +596,7 @@ enum nft_bitwise_ops {* @NFTA_BITWISE_OP: type of operation (NLA_U32: nft_bitwise_ops) * @NFTA_BITWISE_DATA: argument for non-boolean operations * (NLA_NESTED: nft_data_attributes) +* @NFTA_BITWISE_SREG2: second source register (NLA_U32: nft_registers) * * The bitwise expression supports boolean and shift operations. It implements * the boolean operations by performing the following operation:
@@ -613,6 +620,7 @@ enum nft_bitwise_attributes {NFTA_BITWISE_XOR ,NFTA_BITWISE_OP ,NFTA_BITWISE_DATA , +NFTA_BITWISE_SREG2 , __NFTA_BITWISE_MAX };#define NFTA_BITWISE_MAX (__NFTA_BITWISE_MAX - 1)
net/netfilter/nft_bitwise.c +123 -11
214 lines not on this page · raw message
Signature · 1 line
2.30.2