Netfilter/IPVS updates for net-next
Subsystems (5)
Pablo Neira Ayuso
maintainercover Netfilter/IPVS updates for net-next
Hi,
The following patchset contains Netfilter updates for net-next:
1) DCCP option matching support, from Jeremy Sowden.
2) Reduce jumpstack footprint: Stash chain in last rule marker in blob for tracing. Remove last rule and chain from jumpstack. From Florian Westphal.
3) nf_tables validates all tables before committing the new rules. Unfortunately, this has two drawbacks:
- Since addition of the transaction mutex pernet state gets written to outside of the locked section from the cleanup callback, this is wrong so do this cleanup directly after table has passed all checks.
- Revalidate tables that saw no changes. This can be avoided by keeping the validation state per table, not per netns.
From Florian Westphal.
4) Get rid of a few redundant pointers in the traceinfo structure. The three removed pointers are used in the expression evaluation loop, so gcc keeps them in registers. Passing them to the (inlined) helpers thus doesn't increase nft_do_chain text size, while stack is reduced by another 24 bytes on 64bit arches. From Florian Westphal.
5) IPVS cleanups in several ways without implementing any functional changes, aside from removing some debugging output:
- Update width of source for ip_vs_sync_conn_options
The operation is safe, use an annotation to describe it properly.
- Consistently use array_size() in ip_vs_conn_init() It seems better to use helpers consistently.
- Remove {Enter,Leave}Function. These seem to be well past their use-by date.
- Correct spelling in comments.
From Simon Horman.
6) Extended netlink error report for netdevice in flowtables and netdev/chains. Allow for incrementally add/delete devices to netdev basechain. Allow to create netdev chain without device.
Please, pull these changes from:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git
Thanks.
----------------------------------------------------------------
The following changes since commit ca288965801572fe41386560d4e6c5cc0e5cc56d:
Merge tag 'wireless-next-2023-04-21' of git://git.kernel.org/pub/scm/linux/kernel/git/wireless/wireless-next (2023-04-21 07:35:51 -0700)
are available in the Git repository at:
git://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git nf-next-23-04-22
for you to fetch changes up to c7ce03c47ae836cad7d95fc08dc564452fe9b6e3:
netfilter: nf_tables: allow to create netdev chain without device (2023-04-22 00:23:35 +0200)
----------------------------------------------------------------
netfilter pull request 23-04-22
----------------------------------------------------------------
Florian Westphal (9):
netfilter: nf_tables: merge nft_rules_old structure and end of ruleblob marker
netfilter: nf_tables: don't store address of last rule on jump
netfilter: nf_tables: don't store chain address on jump
netfilter: nf_tables: don't write table validation state without mutex
netfilter: nf_tables: make validation state per table
netfilter: nf_tables: remove unneeded conditional
netfilter: nf_tables: do not store pktinfo in traceinfo structure
netfilter: nf_tables: do not store verdict in traceinfo structure
netfilter: nf_tables: do not store rule in traceinfo structure
Jeremy Sowden (1):
netfilter: nft_exthdr: add boolean DCCP option matching
Pablo Neira Ayuso (6):
netfilter: nf_tables: extended netlink error reporting for netdevice
netfilter: nf_tables: do not send complete notification of deletions
netfilter: nf_tables: rename function to destroy hook list
netfilter: nf_tables: support for adding new devices to an existing netdev chain
netfilter: nf_tables: support for deleting devices in an existing netdev chain
netfilter: nf_tables: allow to create netdev chain without device
Simon Horman (4):
ipvs: Update width of source for ip_vs_sync_conn_options
ipvs: Consistently use array_size() in ip_vs_conn_init()
ipvs: Remove {Enter,Leave}Function
ipvs: Correct spelling in comments
include/linux/netfilter/nfnetlink.h | 1 - include/net/ip_vs.h | 32 +- include/net/netfilter/nf_tables.h | 35 +- include/uapi/linux/netfilter/nf_tables.h | 2 + net/netfilter/ipvs/ip_vs_conn.c | 12 +- net/netfilter/ipvs/ip_vs_core.c | 8 - net/netfilter/ipvs/ip_vs_ctl.c | 26 +- net/netfilter/ipvs/ip_vs_sync.c | 7 +- net/netfilter/ipvs/ip_vs_xmit.c | 62 +--- net/netfilter/nf_tables_api.c | 539 ++++++++++++++++++++----------- net/netfilter/nf_tables_core.c | 59 ++-- net/netfilter/nf_tables_trace.c | 62 ++-- net/netfilter/nfnetlink.c | 2 - net/netfilter/nft_exthdr.c | 105 ++++++ 14 files changed, 570 insertions(+), 382 deletions(-)
Pablo Neira Ayuso
maintainerPATCH 1/20 netfilter: nft_exthdr: add boolean DCCP option matching
From: Jeremy Sowden <[email protected]>
The xt_dccp iptables module supports the matching of DCCP packets based on the presence or absence of DCCP options. Extend nft_exthdr to add this functionality to nftables.
--- include/uapi/linux/netfilter/nf_tables.h | 2 + net/netfilter/nft_exthdr.c | 105 +++++++++++++++++++++++ 2 files changed, 107 insertions(+)
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -859,12 +859,14 @@ enum nft_exthdr_flags {* @NFT_EXTHDR_OP_TCP: match against tcp options * @NFT_EXTHDR_OP_IPV4: match against ipv4 options * @NFT_EXTHDR_OP_SCTP: match against sctp chunks +* @NFT_EXTHDR_OP_DCCP: match against dccp otions */ enum nft_exthdr_op {NFT_EXTHDR_OP_IPV6 ,NFT_EXTHDR_OP_TCPOPT ,NFT_EXTHDR_OP_IPV4 ,NFT_EXTHDR_OP_SCTP , +NFT_EXTHDR_OP_DCCP , __NFT_EXTHDR_OP_MAX };#define NFT_EXTHDR_OP_MAX (__NFT_EXTHDR_OP_MAX - 1)
net/netfilter/nft_exthdr.c +105 -0
135 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 2/20 netfilter: nf_tables: merge nft_rules_old structure and end of ruleblob marker
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
In order to free the rules in a chain via call_rcu, the rule array used to stash a rcu_head and space for a pointer at the end of the rule array.
When the current nft_rule_dp blob format got added in 2c865a8a28a1 ("netfilter: nf_tables: add rule blob layout"), this results in a double-trailer:
size (unsigned long)
struct nft_rule_dp
struct nft_expr
...
struct nft_rule_dp
struct nft_expr
...
struct nft_rule_dp (is_last=1) // TrailerThe trailer, struct nft_rule_dp (is_last=1), is not accounted for in size, so it can be located via start_addr + size.
Because the rcu_head is stored after 'start+size' as well this means the is_last trailer is *aliased* to the rcu_head (struct nft_rules_old).
This is harmless, because at this time the nft_do_chain function never evaluates/accesses the trailer, it only checks the address boundary:
for (; rule < last_rule; rule = nft_rule_next(rule)) {...
But this way the last_rule address has to be stashed in the jump structure to restore it after returning from a chain.
nft_do_chain stack usage has become way too big, so put it on a diet.
Without this patch is impossible to use
for (; !rule->is_last; rule = nft_rule_next(rule)) {... because on free, the needed update of the rcu_head will clobber the nft_rule_dp is_last bit.
Furthermore, also stash the chain pointer in the trailer, this allows to recover the original chain structure from nf_tables_trace infra without a need to place them in the jump struct.
After this patch it is trivial to diet the jump stack structure, done in the next two patches.
--- net/netfilter/nf_tables_api.c | 55 +++++++++++++++++------------------ 1 file changed, 27 insertions(+), 28 deletions(-)
net/netfilter/nf_tables_api.c +27 -28
@@ -2110,38 +2110,41 @@ static void nft_chain_release_hook(struct nft_chain_hook *hook)module_put (hook->type->owner); } -struct nft_rules_old { +struct nft_rule_dp_last { +struct nft_rule_dp end;/* end of nft_rule_blob marker */ struct rcu_head h;struct nft_rule_blob *blob; +const struct nft_chain *chain;/* for tracing */ }; -static void nft_last_rule (struct nft_rule_blob *blob,const void *ptr) +static void nft_last_rule (const struct nft_chain *chain,const void *ptr) { -struct nft_rule_dp *prule; +struct nft_rule_dp_last *lrule; + +BUILD_BUG_ON (offsetof(struct nft_rule_dp_last , end) !=0 ); - prule = (struct nft_rule_dp *)ptr; - prule->is_last =1 ; + lrule = (struct nft_rule_dp_last *)ptr; + lrule->end.is_last =1 ; + lrule->chain = chain;/* blob size does not include the trailer rule */ } -static struct nft_rule_blob *nf_tables_chain_alloc_rules (unsigned int size) +static struct nft_rule_blob *nf_tables_chain_alloc_rules (const struct nft_chain *chain, +unsigned int size) {struct nft_rule_blob *blob; -/* size must include room for the last rule */ -if (size < offsetof(struct nft_rule_dp , data)) -return NULL ; - - size +=sizeof (struct nft_rule_blob ) +sizeof (struct nft_rules_old );if (size >INT_MAX )return NULL ; + size +=sizeof (struct nft_rule_blob ) +sizeof (struct nft_rule_dp_last ); + blob =kvmalloc (size,GFP_KERNEL_ACCOUNT );if (!blob)return NULL ; blob->size =0 ; -nft_last_rule (blob, blob->data); +nft_last_rule (chain, blob->data);return blob; }
@@ -2220,7 +2223,6 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,struct nft_rule_blob *blob;struct nft_trans *trans;struct nft_chain *chain; -unsigned int data_size;int err;if (table->use ==UINT_MAX )
@@ -2308,8 +2310,7 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask, chain->udlen =nla_len (nla[NFTA_CHAIN_USERDATA ]); } - data_size = offsetof(struct nft_rule_dp , data);/* last rule */ - blob =nf_tables_chain_alloc_rules (data_size); + blob =nf_tables_chain_alloc_rules (chain,0 );if (!blob) { err = -ENOMEM ; goto err_destroy_chain;
@@ -8817,9 +8818,8 @@ static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *chareturn -ENOMEM ; } } - data_size += offsetof(struct nft_rule_dp , data);/* last rule */ - chain->blob_next =nf_tables_chain_alloc_rules (data_size); + chain->blob_next =nf_tables_chain_alloc_rules (chain, data_size);if (!chain->blob_next)return -ENOMEM ;
@@ -8864,12 +8864,11 @@ static int nf_tables_commit_chain_prepare(struct net *net, struct nft_chain *cha chain->blob_next->size += (unsigned long )(data - (void *)prule); } - prule = (struct nft_rule_dp *)data; - data += offsetof(struct nft_rule_dp , data);if (WARN_ON_ONCE (data > data_boundary))return -ENOMEM ; -nft_last_rule (chain->blob_next, prule); + prule = (struct nft_rule_dp *)data; +nft_last_rule (chain, prule);return 0 ; }
@@ -8890,22 +8889,22 @@ static void nf_tables_commit_chain_prepare_cancel(struct net *net) } } -static void __nf_tables_commit_chain_free_rules_old (struct rcu_head *h) +static void __nf_tables_commit_chain_free_rules (struct rcu_head *h) { - struct nft_rules_old *o = container_of(h, struct nft_rules_old, h); + struct nft_rule_dp_last *l = container_of(h, struct nft_rule_dp_last, h); -kvfree (o->blob); +kvfree (l->blob); }static void nf_tables_commit_chain_free_rules_old (struct nft_rule_blob *blob) { -struct nft_rules_old *old; +struct nft_rule_dp_last *last; -/* rcu_head is after end marker */ - old = (void *)blob +sizeof (*blob) + blob->size; - old->blob = blob; +/* last rule trailer is after end marker */ + last = (void *)blob +sizeof (*blob) + blob->size; + last->blob = blob; -call_rcu (&old->h, __nf_tables_commit_chain_free_rules_old); +call_rcu (&last->h, __nf_tables_commit_chain_free_rules); }static void nf_tables_commit_chain (struct net *net,struct nft_chain *chain)
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 3/20 netfilter: nf_tables: don't store address of last rule on jump
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Walk the rule headers until the trailer one (last_bit flag set) instead of stopping at last_rule address.
This avoids the need to store the address when jumping to another chain.
This cuts size of jumpstack array by one third, on 64bit from 384 to 256 bytes. Still, stack usage is still quite large:
scripts/stackusage:
nf_tables_core.c:258 nft_do_chain 496 static
Next patch will also remove chain pointer.
--- net/netfilter/nf_tables_core.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
net/netfilter/nf_tables_core.c +2 -6
@@ -205,7 +205,6 @@ static noinline void nft_update_chain_stats(const struct nft_chain *chain,struct nft_jumpstack {const struct nft_chain *chain;const struct nft_rule_dp *rule; -const struct nft_rule_dp *last_rule; }; static void expr_call_ops_eval(const struct nft_expr *expr,
@@ -259,9 +258,9 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv) { const struct nft_chain *chain = priv, *basechain = chain; -const struct nft_rule_dp *rule, *last_rule;const struct net *net =nft_net (pkt);const struct nft_expr *expr, *last; +const struct nft_rule_dp *rule;struct nft_regs regs = {};unsigned int stackptr =0 ;struct nft_jumpstack jumpstack[NFT_JUMP_STACK_SIZE ];
@@ -279,10 +278,9 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv) blob =rcu_dereference (chain->blob_gen_0); rule = (struct nft_rule_dp *)blob->data; - last_rule = (void *)blob->data + blob->size; next_rule: regs.verdict.code =NFT_CONTINUE ; -for (; rule < last_rule; rule =nft_rule_next (rule)) { +for (; !rule->is_last ; rule =nft_rule_next (rule)) {nft_rule_dp_for_each_expr (expr, last, rule) {if (expr->ops == &nft_cmp_fast_ops)nft_cmp_fast_eval (expr, ®s);
@@ -327,7 +325,6 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)return NF_DROP ; jumpstack[stackptr].chain = chain; jumpstack[stackptr].rule =nft_rule_next (rule); - jumpstack[stackptr].last_rule = last_rule; stackptr++; fallthrough;case NFT_GOTO :
@@ -344,7 +341,6 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv) stackptr--; chain = jumpstack[stackptr].chain; rule = jumpstack[stackptr].rule; - last_rule = jumpstack[stackptr].last_rule; goto next_rule; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 4/20 netfilter: nf_tables: don't store chain address on jump
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Now that the rule trailer/end marker and the rcu head reside in the same structure, we no longer need to save/restore the chain pointer when performing/returning from a jump.
We can simply let the trace infra walk the evaluated rule until it hits the end marker and then fetch the chain pointer from there.
When the rule is NULL (policy tracing), then chain and basechain pointers were already identical, so just use the basechain.
This cuts size of jumpstack in half, from 256 to 128 bytes in 64bit, scripts/stackusage says:
nf_tables_core.c:251 nft_do_chain 328 static
--- include/net/netfilter/nf_tables.h | 14 ++++++++++++-- net/netfilter/nf_tables_api.c | 7 ------- net/netfilter/nf_tables_core.c | 21 ++++++--------------- net/netfilter/nf_tables_trace.c | 30 ++++++++++++++++++++++++++---- 4 files changed, 44 insertions(+), 28 deletions(-)
include/net/netfilter/nf_tables.h +12 -2
@@ -1046,6 +1046,18 @@ struct nft_rule_dp { __attribute__((aligned(__alignof__(struct nft_expr)))); }; +struct nft_rule_dp_last { +struct nft_rule_dp end;/* end of nft_rule_blob marker */ +struct rcu_head h;/* call_rcu head */ +struct nft_rule_blob *blob;/* ptr to free via call_rcu */ +const struct nft_chain *chain;/* for nftables tracing */ +}; + +static inline const struct nft_rule_dp *nft_rule_next (const struct nft_rule_dp *rule) +{ +return (void *)rule +sizeof (*rule) + rule->dlen; +} +struct nft_rule_blob {unsigned long size;unsigned char data[]
@@ -1396,7 +1408,6 @@ void nft_unregister_flowtable_type(struct nf_flowtable_type *type);* @packet_dumped: packet headers sent in a previous traceinfo message * @pkt: pktinfo currently processed * @basechain: base chain currently processed -* @chain: chain currently processed * @rule: rule that was evaluated * @verdict: verdict given by rule */
@@ -1408,7 +1419,6 @@ struct nft_traceinfo {u32 skbid;const struct nft_pktinfo *pkt;const struct nft_base_chain *basechain; -const struct nft_chain *chain;const struct nft_rule_dp *rule;const struct nft_verdict *verdict; };
net/netfilter/nf_tables_api.c +0 -7
@@ -2110,13 +2110,6 @@ static void nft_chain_release_hook(struct nft_chain_hook *hook)module_put (hook->type->owner); } -struct nft_rule_dp_last { -struct nft_rule_dp end;/* end of nft_rule_blob marker */ -struct rcu_head h; -struct nft_rule_blob *blob; -const struct nft_chain *chain;/* for tracing */ -}; -static void nft_last_rule (const struct nft_chain *chain,const void *ptr) {struct nft_rule_dp_last *lrule;
net/netfilter/nf_tables_core.c +6 -15
@@ -42,13 +42,11 @@ static inline void nf_skip_indirect_calls_enable(void) { }#endif static noinline void __nft_trace_packet(struct nft_traceinfo *info, -const struct nft_chain *chain,enum nft_trace_types type) {if (!info->trace || !info->nf_trace)return ; - info->chain = chain; info->type = type;nft_trace_notify (info);
@@ -56,14 +54,13 @@ static noinline void __nft_trace_packet(struct nft_traceinfo *info,static inline void nft_trace_packet (const struct nft_pktinfo *pkt,struct nft_traceinfo *info, -const struct nft_chain *chain,const struct nft_rule_dp *rule,enum nft_trace_types type) {if (static_branch_unlikely (&nft_trace_enabled)) { info->nf_trace = pkt->skb->nf_trace; info->rule = rule; -__nft_trace_packet (info, chain, type); +__nft_trace_packet (info, type); } }
@@ -111,7 +108,6 @@ static void nft_cmp16_fast_eval(const struct nft_expr *expr, } static noinline void __nft_trace_verdict(struct nft_traceinfo *info, -const struct nft_chain *chain,const struct nft_regs *regs) {enum nft_trace_types type;
@@ -133,17 +129,16 @@ static noinline void __nft_trace_verdict(struct nft_traceinfo *info,break ; } -__nft_trace_packet (info, chain, type); +__nft_trace_packet (info, type); }static inline void nft_trace_verdict (struct nft_traceinfo *info, -const struct nft_chain *chain,const struct nft_rule_dp *rule,const struct nft_regs *regs) {if (static_branch_unlikely (&nft_trace_enabled)) { info->rule = rule; -__nft_trace_verdict (info, chain, regs); +__nft_trace_verdict (info, regs); } }
@@ -203,7 +198,6 @@ static noinline void nft_update_chain_stats(const struct nft_chain *chain, }struct nft_jumpstack { -const struct nft_chain *chain;const struct nft_rule_dp *rule; };
@@ -247,7 +241,6 @@ static void expr_call_ops_eval(const struct nft_expr *expr,#define nft_rule_expr_first (rule) (struct nft_expr *)&rule->data[0]#define nft_rule_expr_next (expr) ((void *)expr) + expr->ops->size#define nft_rule_expr_last (rule) (struct nft_expr *)&rule->data[rule->dlen] -#define nft_rule_next (rule) (void *)rule + sizeof(*rule) + rule->dlen#define nft_rule_dp_for_each_expr (expr, last, rule) \ for ((expr) = nft_rule_expr_first(rule), (last) = nft_rule_expr_last(rule); \
@@ -302,14 +295,14 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)nft_trace_copy_nftrace (pkt, &info);continue ;case NFT_CONTINUE : -nft_trace_packet (pkt, &info, chain, rule, +nft_trace_packet (pkt, &info, rule,NFT_TRACETYPE_RULE );continue ; }break ; } -nft_trace_verdict (&info, chain, rule, ®s); +nft_trace_verdict (&info, rule, ®s);switch (regs.verdict.code &NF_VERDICT_MASK ) {case NF_ACCEPT :
@@ -323,7 +316,6 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)case NFT_JUMP :if (WARN_ON_ONCE (stackptr >=NFT_JUMP_STACK_SIZE ))return NF_DROP ; - jumpstack[stackptr].chain = chain; jumpstack[stackptr].rule =nft_rule_next (rule); stackptr++; fallthrough;
@@ -339,12 +331,11 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)if (stackptr >0 ) { stackptr--; - chain = jumpstack[stackptr].chain; rule = jumpstack[stackptr].rule; goto next_rule; } -nft_trace_packet (pkt, &info, basechain,NULL ,NFT_TRACETYPE_POLICY ); +nft_trace_packet (pkt, &info,NULL ,NFT_TRACETYPE_POLICY );if (static_branch_unlikely (&nft_counters_enabled))nft_update_chain_stats (basechain, pkt);
net/netfilter/nf_tables_trace.c +26 -4
@@ -164,9 +164,29 @@ static bool nft_trace_have_verdict_chain(struct nft_traceinfo *info)return true; } +static const struct nft_chain *nft_trace_get_chain (const struct nft_traceinfo *info) +{ +const struct nft_rule_dp *rule = info->rule; +const struct nft_rule_dp_last *last; + +if (!rule) +return &info->basechain->chain; + +while (!rule->is_last) + rule =nft_rule_next (rule); + + last = (const struct nft_rule_dp_last *)rule; + +if (WARN_ON_ONCE (!last->chain)) +return &info->basechain->chain; + +return last->chain; +} +void nft_trace_notify (struct nft_traceinfo *info) {const struct nft_pktinfo *pkt = info->pkt; +const struct nft_chain *chain;struct nlmsghdr *nlh;struct sk_buff *skb;unsigned int size;
@@ -176,9 +196,11 @@ void nft_trace_notify(struct nft_traceinfo *info)if (!nfnetlink_has_listeners (nft_net (pkt),NFNLGRP_NFTRACE ))return ; + chain =nft_trace_get_chain (info); + size =nlmsg_total_size (sizeof (struct nfgenmsg )) + -nla_total_size (strlen (info->chain->table->name)) + -nla_total_size (strlen (info->chain->name)) + +nla_total_size (strlen (chain->table->name)) + +nla_total_size (strlen (chain->name)) +nla_total_size_64bit (sizeof (__be64)) +/* rule handle */ nla_total_size (sizeof (__be32)) +/* trace type */ nla_total_size(0) + /* VERDICT, nested */
@@ -217,10 +239,10 @@ void nft_trace_notify(struct nft_traceinfo *info)if (nla_put_u32 (skb,NFTA_TRACE_ID , info->skbid)) goto nla_put_failure; -if (nla_put_string (skb,NFTA_TRACE_CHAIN , info->chain->name)) +if (nla_put_string (skb,NFTA_TRACE_CHAIN , chain->name)) goto nla_put_failure; -if (nla_put_string (skb,NFTA_TRACE_TABLE , info->chain->table->name)) +if (nla_put_string (skb,NFTA_TRACE_TABLE , chain->table->name)) goto nla_put_failure;if (nf_trace_fill_rule_info (skb, info))
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 5/20 netfilter: nf_tables: don't write table validation state without mutex
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
The ->cleanup callback needs to be removed, this doesn't work anymore as the transaction mutex is already released in the ->abort function.
Just do it after a successful validation pass, this either happens from commit or abort phases where transaction mutex is held.
--- include/linux/netfilter/nfnetlink.h | 1 - net/netfilter/nf_tables_api.c | 8 ++------ net/netfilter/nfnetlink.c | 2 -- 3 files changed, 2 insertions(+), 9 deletions(-)
include/linux/netfilter/nfnetlink.h +0 -1
@@ -45,7 +45,6 @@ struct nfnetlink_subsystem {int (*commit)(struct net *net,struct sk_buff *skb);int (*abort)(struct net *net,struct sk_buff *skb,enum nfnl_abort_action action); -void (*cleanup)(struct net *net);bool (*valid_genid)(struct net *net,u32 genid); };
net/netfilter/nf_tables_api.c +2 -6
@@ -8639,6 +8639,8 @@ static int nf_tables_validate(struct net *net)if (nft_table_validate (net, table) <0 )return -EAGAIN ; } + +nft_validate_state_update (net,NFT_VALIDATE_SKIP );break ; }
@@ -9578,11 +9580,6 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)return 0 ; } -static void nf_tables_cleanup (struct net *net) -{ -nft_validate_state_update (net,NFT_VALIDATE_SKIP ); -} -static int nf_tables_abort (struct net *net,struct sk_buff *skb,enum nfnl_abort_action action) {
@@ -9616,7 +9613,6 @@ static const struct nfnetlink_subsystem nf_tables_subsys = { .cb = nf_tables_cb, .commit = nf_tables_commit, .abort = nf_tables_abort, - .cleanup = nf_tables_cleanup, .valid_genid = nf_tables_valid_genid, .owner = THIS_MODULE, };
net/netfilter/nfnetlink.c +0 -2
@@ -590,8 +590,6 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, goto replay_abort; } } -if (ss->cleanup) - ss->cleanup (net);nfnl_err_deliver (&err_list, oskb);kfree_skb (skb);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 6/20 netfilter: nf_tables: make validation state per table
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
We only need to validate tables that saw changes in the current transaction.
The existing code revalidates all tables, but this isn't needed as cross-table jumps are not allowed (chains have table scope).
--- include/net/netfilter/nf_tables.h | 3 ++- net/netfilter/nf_tables_api.c | 38 +++++++++++++++---------------- 2 files changed, 20 insertions(+), 21 deletions(-)
include/net/netfilter/nf_tables.h +2 -1
@@ -1209,6 +1209,7 @@ unsigned int nft_do_chain(struct nft_pktinfo *pkt, void *priv);* @genmask: generation mask * @afinfo: address family info * @name: name of the table +* @validate_state: internal, set when transaction adds jumps */ struct nft_table {struct list_head list;
@@ -1227,6 +1228,7 @@ struct nft_table {char *name;u16 udlen;u8 *udata; +u8 validate_state; };static inline bool nft_table_has_owner (const struct nft_table *table)
@@ -1698,7 +1700,6 @@ struct nftables_pernet {struct mutex commit_mutex;u64 table_handle;unsigned int base_seq; -u8 validate_state; };extern unsigned int nf_tables_net_id;
net/netfilter/nf_tables_api.c +18 -20
@@ -102,11 +102,9 @@ static const u8 nft2audit_op[NFT_MSG_MAX] = { // enum nf_tables_msg_types [NFT_MSG_DELFLOWTABLE] = AUDIT_NFT_OP_FLOWTABLE_UNREGISTER, }; -static void nft_validate_state_update (struct net *net,u8 new_validate_state) +static void nft_validate_state_update (struct nft_table *table,u8 new_validate_state) { -struct nftables_pernet *nft_net =nft_pernet (net); - -switch (nft_net->validate_state) { +switch (table->validate_state) {case NFT_VALIDATE_SKIP :WARN_ON_ONCE (new_validate_state ==NFT_VALIDATE_DO );break ;
@@ -117,7 +115,7 @@ static void nft_validate_state_update(struct net *net, u8 new_validate_state)return ; } - nft_net->validate_state = new_validate_state; + table->validate_state = new_validate_state; }static void nf_tables_trans_destroy_work (struct work_struct *w); static DECLARE_WORK(trans_destroy_work, nf_tables_trans_destroy_work);
@@ -1224,6 +1222,7 @@ static int nf_tables_newtable(struct sk_buff *skb, const struct nfnl_info *info,if (table ==NULL ) goto err_kzalloc; + table->validate_state =NFT_VALIDATE_SKIP ; table->name =nla_strdup (attr,GFP_KERNEL_ACCOUNT );if (table->name ==NULL ) goto err_strdup;
@@ -3660,7 +3659,7 @@ static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info, }if (expr_info[i].ops->validate) -nft_validate_state_update (net,NFT_VALIDATE_NEED ); +nft_validate_state_update (table,NFT_VALIDATE_NEED ); expr_info[i].ops =NULL ; expr =nft_expr_next (expr);
@@ -3710,7 +3709,7 @@ static int nf_tables_newrule(struct sk_buff *skb, const struct nfnl_info *info,if (flow)nft_trans_flow_rule (trans) = flow; -if (nft_net->validate_state ==NFT_VALIDATE_DO ) +if (table->validate_state ==NFT_VALIDATE_DO )return nft_table_validate (net, table);return 0 ;
@@ -6312,7 +6311,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,if (desc.type ==NFT_DATA_VERDICT && (elem.data.val.verdict.code ==NFT_GOTO || elem.data.val.verdict.code ==NFT_JUMP )) -nft_validate_state_update (ctx->net, +nft_validate_state_update (ctx->table,NFT_VALIDATE_NEED ); }
@@ -6437,7 +6436,6 @@ static int nf_tables_newsetelem(struct sk_buff *skb,const struct nfnl_info *info, const struct nlattr * const nla[]) { - struct nftables_pernet *nft_net = nft_pernet(info->net); struct netlink_ext_ack *extack = info->extack;u8 genmask =nft_genmask_next (info->net);u8 family = info->nfmsg->nfgen_family;
@@ -6476,7 +6474,7 @@ static int nf_tables_newsetelem(struct sk_buff *skb, } } -if (nft_net->validate_state ==NFT_VALIDATE_DO ) +if (table->validate_state ==NFT_VALIDATE_DO )return nft_table_validate (net, table);return 0 ;
@@ -8628,19 +8626,20 @@ static int nf_tables_validate(struct net *net)struct nftables_pernet *nft_net =nft_pernet (net);struct nft_table *table; -switch (nft_net->validate_state) { -case NFT_VALIDATE_SKIP : -break ; -case NFT_VALIDATE_NEED : -nft_validate_state_update (net,NFT_VALIDATE_DO ); - fallthrough; -case NFT_VALIDATE_DO : -list_for_each_entry (table, &nft_net->tables, list) { +list_for_each_entry (table, &nft_net->tables, list) { +switch (table->validate_state) { +case NFT_VALIDATE_SKIP : +continue ; +case NFT_VALIDATE_NEED : +nft_validate_state_update (table,NFT_VALIDATE_DO ); + fallthrough; +case NFT_VALIDATE_DO :if (nft_table_validate (net, table) <0 )return -EAGAIN ; + +nft_validate_state_update (table,NFT_VALIDATE_SKIP ); } -nft_validate_state_update (net,NFT_VALIDATE_SKIP );break ; }
@@ -10355,7 +10354,6 @@ static int __net_init nf_tables_init_net(struct net *net)INIT_LIST_HEAD (&nft_net->notify_list);mutex_init (&nft_net->commit_mutex); nft_net->base_seq =1 ; - nft_net->validate_state =NFT_VALIDATE_SKIP ;return 0 ; }
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 7/20 netfilter: nf_tables: remove unneeded conditional
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
This helper is inlined, so keep it as small as possible.
If the static key is true, there is only a very small chance that info->trace is false:
1. tracing was enabled at this very moment, the static key was updated to active right after nft_do_table was called.
2. tracing was disabled at this very moment. trace->info is already false, the static key is about to be patched to false soon.
In both cases, no event will be sent because info->trace
is false (checked in noinline slowpath). info->nf_trace is irrelevant.
The nf_trace update is redunant in this case, but this will only happen for short duration, when static key flips.
text data bss dec hex filename old: 2980 192 32 3204 c84 nf_tables_core.o new: 2964 192 32 3188 c74i nf_tables_core.o
--- net/netfilter/nf_tables_core.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_core.c +2 -4
@@ -67,10 +67,8 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,static inline void nft_trace_copy_nftrace (const struct nft_pktinfo *pkt,struct nft_traceinfo *info) { -if (static_branch_unlikely (&nft_trace_enabled)) { -if (info->trace) - info->nf_trace = pkt->skb->nf_trace; - } +if (static_branch_unlikely (&nft_trace_enabled)) + info->nf_trace = pkt->skb->nf_trace; } static void nft_bitwise_fast_eval(const struct nft_expr *expr,
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 8/20 netfilter: nf_tables: do not store pktinfo in traceinfo structure
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
pass it as argument. No change in object size.
stack usage decreases by 8 byte:
nf_tables_core.c:254 nft_do_chain 320 static
--- include/net/netfilter/nf_tables.h | 5 ++--- net/netfilter/nf_tables_core.c | 21 ++++++++++++--------- net/netfilter/nf_tables_trace.c | 5 ++--- 3 files changed, 16 insertions(+), 15 deletions(-)
include/net/netfilter/nf_tables.h +2 -3
@@ -1408,7 +1408,6 @@ void nft_unregister_flowtable_type(struct nf_flowtable_type *type);* @type: event type (enum nft_trace_types) * @skbid: hash of skb to be used as trace id * @packet_dumped: packet headers sent in a previous traceinfo message -* @pkt: pktinfo currently processed * @basechain: base chain currently processed * @rule: rule that was evaluated * @verdict: verdict given by rule
@@ -1419,7 +1418,6 @@ struct nft_traceinfo {bool packet_dumped; enum nft_trace_types type:8;u32 skbid; -const struct nft_pktinfo *pkt;const struct nft_base_chain *basechain;const struct nft_rule_dp *rule;const struct nft_verdict *verdict;
@@ -1429,7 +1427,8 @@ void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,const struct nft_verdict *verdict, const struct nft_chain *basechain); -void nft_trace_notify (struct nft_traceinfo *info); +void nft_trace_notify (const struct nft_pktinfo *pkt, +struct nft_traceinfo *info);#define MODULE_ALIAS_NFT_CHAIN (family, name) \ MODULE_ALIAS("nft-chain-" __stringify(family) "-" name)
net/netfilter/nf_tables_core.c +12 -9
@@ -41,7 +41,8 @@ static inline bool nf_skip_indirect_calls(void) { return false; }static inline void nf_skip_indirect_calls_enable (void ) { }#endif -static noinline void __nft_trace_packet(struct nft_traceinfo *info, +static noinline void __nft_trace_packet(const struct nft_pktinfo *pkt, +struct nft_traceinfo *info,enum nft_trace_types type) {if (!info->trace || !info->nf_trace)
@@ -49,7 +50,7 @@ static noinline void __nft_trace_packet(struct nft_traceinfo *info, info->type = type; -nft_trace_notify (info); +nft_trace_notify (pkt, info); } static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
@@ -60,7 +61,7 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,if (static_branch_unlikely (&nft_trace_enabled)) { info->nf_trace = pkt->skb->nf_trace; info->rule = rule; -__nft_trace_packet (info, type); +__nft_trace_packet (pkt, info, type); } }
@@ -105,7 +106,8 @@ static void nft_cmp16_fast_eval(const struct nft_expr *expr, regs->verdict.code =NFT_BREAK ; } -static noinline void __nft_trace_verdict(struct nft_traceinfo *info, +static noinline void __nft_trace_verdict(const struct nft_pktinfo *pkt, +struct nft_traceinfo *info,const struct nft_regs *regs) {enum nft_trace_types type;
@@ -123,20 +125,21 @@ static noinline void __nft_trace_verdict(struct nft_traceinfo *info, type =NFT_TRACETYPE_RULE ;if (info->trace) - info->nf_trace = info->pkt->skb->nf_trace; + info->nf_trace = pkt->skb->nf_trace;break ; } -__nft_trace_packet (info, type); +__nft_trace_packet (pkt, info, type); } -static inline void nft_trace_verdict (struct nft_traceinfo *info, +static inline void nft_trace_verdict (const struct nft_pktinfo *pkt, +struct nft_traceinfo *info,const struct nft_rule_dp *rule,const struct nft_regs *regs) {if (static_branch_unlikely (&nft_trace_enabled)) { info->rule = rule; -__nft_trace_verdict (info, regs); +__nft_trace_verdict (pkt, info, regs); } }
@@ -300,7 +303,7 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)break ; } -nft_trace_verdict (&info, rule, ®s); +nft_trace_verdict (pkt, &info, rule, ®s);switch (regs.verdict.code &NF_VERDICT_MASK ) {case NF_ACCEPT :
net/netfilter/nf_tables_trace.c +2 -3
@@ -183,9 +183,9 @@ static const struct nft_chain *nft_trace_get_chain(const struct nft_traceinfo *ireturn last->chain; } -void nft_trace_notify (struct nft_traceinfo *info) +void nft_trace_notify (const struct nft_pktinfo *pkt, +struct nft_traceinfo *info) { -const struct nft_pktinfo *pkt = info->pkt;const struct nft_chain *chain;struct nlmsghdr *nlh;struct sk_buff *skb;
@@ -305,7 +305,6 @@ void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt, info->trace = true; info->nf_trace = pkt->skb->nf_trace; info->packet_dumped = false; - info->pkt = pkt; info->verdict = verdict;net_get_random_once (&trace_key,sizeof (trace_key));
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 9/20 netfilter: nf_tables: do not store verdict in traceinfo structure
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
Just pass it as argument to nft_trace_notify. Stack is reduced by 8 bytes:
nf_tables_core.c:256 nft_do_chain 312 static
--- include/net/netfilter/nf_tables.h | 4 +--- net/netfilter/nf_tables_core.c | 14 ++++++++------ net/netfilter/nf_tables_trace.c | 21 +++++++++++---------- 3 files changed, 20 insertions(+), 19 deletions(-)
include/net/netfilter/nf_tables.h +1 -3
@@ -1410,7 +1410,6 @@ void nft_unregister_flowtable_type(struct nf_flowtable_type *type);* @packet_dumped: packet headers sent in a previous traceinfo message * @basechain: base chain currently processed * @rule: rule that was evaluated -* @verdict: verdict given by rule */ struct nft_traceinfo {bool trace;
@@ -1420,14 +1419,13 @@ struct nft_traceinfo {u32 skbid;const struct nft_base_chain *basechain;const struct nft_rule_dp *rule; -const struct nft_verdict *verdict; };void nft_trace_init (struct nft_traceinfo *info,const struct nft_pktinfo *pkt, -const struct nft_verdict *verdict,const struct nft_chain *basechain);void nft_trace_notify (const struct nft_pktinfo *pkt, +const struct nft_verdict *verdict,struct nft_traceinfo *info);#define MODULE_ALIAS_NFT_CHAIN (family, name) \
net/netfilter/nf_tables_core.c +8 -6
@@ -42,6 +42,7 @@ static inline void nf_skip_indirect_calls_enable(void) { }#endif static noinline void __nft_trace_packet(const struct nft_pktinfo *pkt, +const struct nft_verdict *verdict,struct nft_traceinfo *info,enum nft_trace_types type) {
@@ -50,10 +51,11 @@ static noinline void __nft_trace_packet(const struct nft_pktinfo *pkt, info->type = type; -nft_trace_notify (pkt, info); +nft_trace_notify (pkt, verdict, info); }static inline void nft_trace_packet (const struct nft_pktinfo *pkt, +struct nft_verdict *verdict,struct nft_traceinfo *info,const struct nft_rule_dp *rule,enum nft_trace_types type)
@@ -61,7 +63,7 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt,if (static_branch_unlikely (&nft_trace_enabled)) { info->nf_trace = pkt->skb->nf_trace; info->rule = rule; -__nft_trace_packet (pkt, info, type); +__nft_trace_packet (pkt, verdict, info, type); } }
@@ -129,7 +131,7 @@ static noinline void __nft_trace_verdict(const struct nft_pktinfo *pkt,break ; } -__nft_trace_packet (pkt, info, type); +__nft_trace_packet (pkt, ®s->verdict, info, type); } static inline void nft_trace_verdict(const struct nft_pktinfo *pkt,
@@ -264,7 +266,7 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv) info.trace = false;if (static_branch_unlikely (&nft_trace_enabled)) -nft_trace_init (&info, pkt, ®s.verdict, basechain); +nft_trace_init (&info, pkt, basechain); do_chain:if (genbit) blob =rcu_dereference (chain->blob_gen_1);
@@ -296,7 +298,7 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv)nft_trace_copy_nftrace (pkt, &info);continue ;case NFT_CONTINUE : -nft_trace_packet (pkt, &info, rule, +nft_trace_packet (pkt, ®s.verdict, &info, rule,NFT_TRACETYPE_RULE );continue ; }
@@ -336,7 +338,7 @@ nft_do_chain(struct nft_pktinfo *pkt, void *priv) goto next_rule; } -nft_trace_packet (pkt, &info,NULL ,NFT_TRACETYPE_POLICY ); +nft_trace_packet (pkt, ®s.verdict, &info,NULL ,NFT_TRACETYPE_POLICY );if (static_branch_unlikely (&nft_counters_enabled))nft_update_chain_stats (basechain, pkt);
net/netfilter/nf_tables_trace.c +11 -10
@@ -124,6 +124,7 @@ static int nf_trace_fill_pkt_info(struct sk_buff *nlskb, }static int nf_trace_fill_rule_info (struct sk_buff *nlskb, +const struct nft_verdict *verdict,const struct nft_traceinfo *info) {if (!info->rule || info->rule->is_last)
@@ -135,7 +136,7 @@ static int nf_trace_fill_rule_info(struct sk_buff *nlskb,* Since no rule matched, the ->rule pointer is invalid. */ if (info->type ==NFT_TRACETYPE_RETURN && - info->verdict->code ==NFT_CONTINUE ) + verdict->code ==NFT_CONTINUE )return 0 ; return nla_put_be64(nlskb, NFTA_TRACE_RULE_HANDLE,
@@ -143,7 +144,8 @@ static int nf_trace_fill_rule_info(struct sk_buff *nlskb, NFTA_TRACE_PAD); } -static bool nft_trace_have_verdict_chain (struct nft_traceinfo *info) +static bool nft_trace_have_verdict_chain (const struct nft_verdict *verdict, +struct nft_traceinfo *info) {switch (info->type) {case NFT_TRACETYPE_RETURN :
@@ -153,7 +155,7 @@ static bool nft_trace_have_verdict_chain(struct nft_traceinfo *info)return false; } -switch (info->verdict->code) { +switch (verdict->code) {case NFT_JUMP :case NFT_GOTO :break ;
@@ -184,6 +186,7 @@ static const struct nft_chain *nft_trace_get_chain(const struct nft_traceinfo *i }void nft_trace_notify (const struct nft_pktinfo *pkt, +const struct nft_verdict *verdict,struct nft_traceinfo *info) {const struct nft_chain *chain;
@@ -217,8 +220,8 @@ void nft_trace_notify(const struct nft_pktinfo *pkt,nla_total_size (sizeof (u32)) +/* nfproto */ nla_total_size (sizeof (u32));/* policy */ -if (nft_trace_have_verdict_chain (info)) - size +=nla_total_size (strlen (info->verdict->chain->name));/* jump target */ +if (nft_trace_have_verdict_chain (verdict, info)) + size +=nla_total_size (strlen (verdict->chain->name));/* jump target */ skb =nlmsg_new (size,GFP_ATOMIC );if (!skb)
@@ -245,7 +248,7 @@ void nft_trace_notify(const struct nft_pktinfo *pkt,if (nla_put_string (skb,NFTA_TRACE_TABLE , chain->table->name)) goto nla_put_failure; -if (nf_trace_fill_rule_info (skb, info)) +if (nf_trace_fill_rule_info (skb, verdict, info)) goto nla_put_failure;switch (info->type) {
@@ -254,11 +257,11 @@ void nft_trace_notify(const struct nft_pktinfo *pkt,break ;case NFT_TRACETYPE_RETURN :case NFT_TRACETYPE_RULE : -if (nft_verdict_dump (skb,NFTA_TRACE_VERDICT , info->verdict)) +if (nft_verdict_dump (skb,NFTA_TRACE_VERDICT , verdict)) goto nla_put_failure;/* pkt->skb undefined iff NF_STOLEN, disable dump */ -if (info->verdict->code ==NF_STOLEN ) +if (verdict->code ==NF_STOLEN ) info->packet_dumped = true;else mark = pkt->skb->mark;
@@ -295,7 +298,6 @@ void nft_trace_notify(const struct nft_pktinfo *pkt, }void nft_trace_init (struct nft_traceinfo *info,const struct nft_pktinfo *pkt, -const struct nft_verdict *verdict,const struct nft_chain *chain) {static siphash_key_t trace_key__read_mostly ;
@@ -305,7 +307,6 @@ void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt, info->trace = true; info->nf_trace = pkt->skb->nf_trace; info->packet_dumped = false; - info->verdict = verdict;net_get_random_once (&trace_key,sizeof (trace_key));
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 10/20 netfilter: nf_tables: do not store rule in traceinfo structure
In reply to Pablo Neira AyusoFrom: Florian Westphal <[email protected]>
pass it as argument instead. This reduces size of traceinfo to 16 bytes. Total stack usage:
nf_tables_core.c:252 nft_do_chain 304 static
While its possible to also pass basechain as argument, doing so increases nft_do_chaininfo function size.
Unlike pktinfo/verdict/rule the basechain info isn't used in the expression evaluation path. gcc places it on the stack, which results in extra push/pop when it gets passed to the trace helpers as argument rather than as part of the traceinfo structure.
--- include/net/netfilter/nf_tables.h | 3 +-- net/netfilter/nf_tables_core.c | 15 +++++++-------- net/netfilter/nf_tables_trace.c | 14 ++++++++------ 3 files changed, 16 insertions(+), 16 deletions(-)
include/net/netfilter/nf_tables.h +1 -2
@@ -1409,7 +1409,6 @@ void nft_unregister_flowtable_type(struct nf_flowtable_type *type);* @skbid: hash of skb to be used as trace id * @packet_dumped: packet headers sent in a previous traceinfo message * @basechain: base chain currently processed -* @rule: rule that was evaluated */ struct nft_traceinfo {bool trace;
@@ -1418,7 +1417,6 @@ struct nft_traceinfo { enum nft_trace_types type:8;u32 skbid;const struct nft_base_chain *basechain; -const struct nft_rule_dp *rule; }; void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,
@@ -1426,6 +1424,7 @@ void nft_trace_init(struct nft_traceinfo *info, const struct nft_pktinfo *pkt,void nft_trace_notify (const struct nft_pktinfo *pkt,const struct nft_verdict *verdict, +const struct nft_rule_dp *rule,struct nft_traceinfo *info);#define MODULE_ALIAS_NFT_CHAIN (family, name) \
net/netfilter/nf_tables_core.c +7 -8
@@ -43,6 +43,7 @@ static inline void nf_skip_indirect_calls_enable(void) { } static noinline void __nft_trace_packet(const struct nft_pktinfo *pkt,const struct nft_verdict *verdict, +const struct nft_rule_dp *rule,struct nft_traceinfo *info,enum nft_trace_types type) {
@@ -51,7 +52,7 @@ static noinline void __nft_trace_packet(const struct nft_pktinfo *pkt, info->type = type; -nft_trace_notify (pkt, verdict, info); +nft_trace_notify (pkt, verdict, rule, info); } static inline void nft_trace_packet(const struct nft_pktinfo *pkt,
@@ -62,8 +63,7 @@ static inline void nft_trace_packet(const struct nft_pktinfo *pkt, {if (static_branch_unlikely (&nft_trace_enabled)) { info->nf_trace = pkt->skb->nf_trace; - info->rule = rule; -__nft_trace_packet (pkt, verdict, info, type); +__nft_trace_packet (pkt, verdict, rule, info, type); } }
@@ -110,6 +110,7 @@ static void nft_cmp16_fast_eval(const struct nft_expr *expr, static noinline void __nft_trace_verdict(const struct nft_pktinfo *pkt,struct nft_traceinfo *info, +const struct nft_rule_dp *rule,const struct nft_regs *regs) {enum nft_trace_types type;
@@ -131,7 +132,7 @@ static noinline void __nft_trace_verdict(const struct nft_pktinfo *pkt,break ; } -__nft_trace_packet (pkt, ®s->verdict, info, type); +__nft_trace_packet (pkt, ®s->verdict, rule, info, type); } static inline void nft_trace_verdict(const struct nft_pktinfo *pkt,
@@ -139,10 +140,8 @@ static inline void nft_trace_verdict(const struct nft_pktinfo *pkt, const struct nft_rule_dp *rule, const struct nft_regs *regs) { - if (static_branch_unlikely(&nft_trace_enabled)) { - info->rule = rule; -__nft_trace_verdict (pkt, info, regs); - } + if (static_branch_unlikely(&nft_trace_enabled)) + __nft_trace_verdict(pkt, info, rule, regs); } static bool nft_payload_fast_eval(const struct nft_expr *expr,
net/netfilter/nf_tables_trace.c +8 -6
@@ -125,9 +125,10 @@ static int nf_trace_fill_pkt_info(struct sk_buff *nlskb,static int nf_trace_fill_rule_info (struct sk_buff *nlskb,const struct nft_verdict *verdict, +const struct nft_rule_dp *rule,const struct nft_traceinfo *info) { -if (!info->rule || info->rule->is_last) +if (!rule || rule->is_last)return 0 ;/* a continue verdict with ->type == RETURN means that this is
@@ -140,7 +141,7 @@ static int nf_trace_fill_rule_info(struct sk_buff *nlskb,return 0 ;return nla_put_be64 (nlskb,NFTA_TRACE_RULE_HANDLE , -cpu_to_be64 (info->rule->handle), +cpu_to_be64 (rule->handle),NFTA_TRACE_PAD ); }
@@ -166,9 +167,9 @@ static bool nft_trace_have_verdict_chain(const struct nft_verdict *verdict,return true; } -static const struct nft_chain *nft_trace_get_chain (const struct nft_traceinfo *info) +static const struct nft_chain *nft_trace_get_chain (const struct nft_rule_dp *rule, +const struct nft_traceinfo *info) { -const struct nft_rule_dp *rule = info->rule;const struct nft_rule_dp_last *last;if (!rule)
@@ -187,6 +188,7 @@ static const struct nft_chain *nft_trace_get_chain(const struct nft_traceinfo *ivoid nft_trace_notify (const struct nft_pktinfo *pkt,const struct nft_verdict *verdict, +const struct nft_rule_dp *rule,struct nft_traceinfo *info) {const struct nft_chain *chain;
@@ -199,7 +201,7 @@ void nft_trace_notify(const struct nft_pktinfo *pkt,if (!nfnetlink_has_listeners (nft_net (pkt),NFNLGRP_NFTRACE ))return ; - chain =nft_trace_get_chain (info); + chain =nft_trace_get_chain (rule, info); size =nlmsg_total_size (sizeof (struct nfgenmsg )) + nla_total_size(strlen(chain->table->name)) +
@@ -248,7 +250,7 @@ void nft_trace_notify(const struct nft_pktinfo *pkt,if (nla_put_string (skb,NFTA_TRACE_TABLE , chain->table->name)) goto nla_put_failure; -if (nf_trace_fill_rule_info (skb, verdict, info)) +if (nf_trace_fill_rule_info (skb, verdict, rule, info)) goto nla_put_failure;switch (info->type) {
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 11/20 ipvs: Update width of source for ip_vs_sync_conn_options
In reply to Pablo Neira AyusoFrom: Simon Horman <[email protected]>
In ip_vs_sync_conn_v0() copy is made to struct ip_vs_sync_conn_options. That structure looks like this:
struct ip_vs_sync_conn_options {
struct ip_vs_seq in_seq;
struct ip_vs_seq out_seq;
};The source of the copy is the in_seq field of struct ip_vs_conn. Whose type is struct ip_vs_seq. Thus we can see that the source - is not as wide as the amount of data copied, which is the width of struct ip_vs_sync_conn_option.
The copy is safe because the next field in is another struct ip_vs_seq. Make use of struct_group() to annotate this.
Flagged by gcc-13 as:
In file included from ./include/linux/string.h:254,
from ./include/linux/bitmap.h:11,
from ./include/linux/cpumask.h:12,
from ./arch/x86/include/asm/paravirt.h:17,
from ./arch/x86/include/asm/cpuid.h:62,
from ./arch/x86/include/asm/processor.h:19,
from ./arch/x86/include/asm/timex.h:5,
from ./include/linux/timex.h:67,
from ./include/linux/time32.h:13,
from ./include/linux/time.h:60,
from ./include/linux/stat.h:19,
from ./include/linux/module.h:13,
from net/netfilter/ipvs/ip_vs_sync.c:38:
In function 'fortify_memcpy_chk',
inlined from 'ip_vs_sync_conn_v0' at net/netfilter/ipvs/ip_vs_sync.c:606:3:
./include/linux/fortify-string.h:529:25: error: call to '__read_overflow2_field' declared with attribute warning: detected read beyond size of field (2nd parameter); maybe use struct_group()? [-Werror=attribute-warning]
529 | __read_overflow2_field(q_size_field, size);
|Compile tested only.
--- include/net/ip_vs.h | 6 ++++-- net/netfilter/ipvs/ip_vs_sync.c | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-)
include/net/ip_vs.h +4 -2
@@ -630,8 +630,10 @@ struct ip_vs_conn {*/ struct ip_vs_app *app;/* bound ip_vs_app object */ void *app_data;/* Application private data */ -struct ip_vs_seq in_seq;/* incoming seq. struct */ -struct ip_vs_seq out_seq;/* outgoing seq. struct */ +struct_group (sync_conn_opt, + struct ip_vs_seq in_seq; /* incoming seq. struct */ +struct ip_vs_seq out_seq;/* outgoing seq. struct */ + );const struct ip_vs_pe *pe;char *pe_data;
net/netfilter/ipvs/ip_vs_sync.c +1 -1
@@ -603,7 +603,7 @@ static void ip_vs_sync_conn_v0(struct netns_ipvs *ipvs, struct ip_vs_conn *cp,if (cp->flags &IP_VS_CONN_F_SEQ_MASK ) {struct ip_vs_sync_conn_options *opt = (struct ip_vs_sync_conn_options *)&s[1 ]; -memcpy (opt, &cp->in_seq,sizeof (*opt)); +memcpy (opt, &cp->sync_conn_opt,sizeof (*opt)); } m->nr_conns++;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 12/20 ipvs: Consistently use array_size() in ip_vs_conn_init()
In reply to Pablo Neira AyusoFrom: Simon Horman <[email protected]>
Consistently use array_size() to calculate the size of ip_vs_conn_tab in bytes.
Flagged by Coccinelle:
WARNING: array_size is already used (line 1498) to compute the same size
No functional change intended.
Compile tested only.
--- net/netfilter/ipvs/ip_vs_conn.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
net/netfilter/ipvs/ip_vs_conn.c +6 -6
@@ -1481,6 +1481,7 @@ void __net_exit ip_vs_conn_net_cleanup(struct netns_ipvs *ipvs)int __init ip_vs_conn_init (void ) { +size_t tab_array_size;int idx;/* Compute size and mask */
@@ -1494,8 +1495,9 @@ int __init ip_vs_conn_init(void)/* * Allocate the connection hash table and initialize its list heads */ - ip_vs_conn_tab =vmalloc (array_size (ip_vs_conn_tab_size, -sizeof (*ip_vs_conn_tab))); + tab_array_size =array_size (ip_vs_conn_tab_size, +sizeof (*ip_vs_conn_tab)); + ip_vs_conn_tab =vmalloc (tab_array_size);if (!ip_vs_conn_tab)return -ENOMEM ;
@@ -1508,10 +1510,8 @@ int __init ip_vs_conn_init(void)return -ENOMEM ; } -pr_info ("Connection hash table configured " -"(size=%d, memory=%ldKbytes)\n" , - ip_vs_conn_tab_size, - (long )(ip_vs_conn_tab_size*sizeof (*ip_vs_conn_tab))/1024 ); +pr_info ("Connection hash table configured (size=%d, memory=%zdKbytes)\n" , + ip_vs_conn_tab_size, tab_array_size /1024 );IP_VS_DBG (0 ,"Each connection entry needs %zd bytes at least\n" ,sizeof (struct ip_vs_conn ));
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 13/20 ipvs: Remove {Enter,Leave}Function
In reply to Pablo Neira AyusoFrom: Simon Horman <[email protected]>
Remove EnterFunction and LeaveFunction.
These debugging macros seem well past their use-by date. And seem to have little value these days. Removing them allows some trivial cleanup of some exit paths for some functions. These are also included in this patch. There is likely scope for further cleanup of both debugging and unwind paths. But let's leave that for another day.
Only intended to change debug output, and only when CONFIG_IP_VS_DEBUG is enabled. Compile tested only.
--- include/net/ip_vs.h | 20 ----------- net/netfilter/ipvs/ip_vs_core.c | 8 ----- net/netfilter/ipvs/ip_vs_ctl.c | 26 +------------- net/netfilter/ipvs/ip_vs_sync.c | 5 --- net/netfilter/ipvs/ip_vs_xmit.c | 62 +++++---------------------------- 5 files changed, 9 insertions(+), 112 deletions(-)
include/net/ip_vs.h +0 -20
@@ -265,26 +265,6 @@ static inline const char *ip_vs_dbg_addr(int af, char *buf, size_t buf_len, pr_err(msg, ##__VA_ARGS__); \ }while (0 ) -#ifdef CONFIG_IP_VS_DEBUG -#define EnterFunction (level) \ - do { \ - if (level <= ip_vs_get_debug_level()) \ - printk(KERN_DEBUG \ - pr_fmt("Enter: %s, %s line %i\n"), \ - __func__, __FILE__, __LINE__); \ - } while (0) -#define LeaveFunction (level) \ - do { \ - if (level <= ip_vs_get_debug_level()) \ - printk(KERN_DEBUG \ - pr_fmt("Leave: %s, %s line %i\n"), \ - __func__, __FILE__, __LINE__); \ - } while (0) -#else -#define EnterFunction (level) do {} while (0) -#define LeaveFunction (level) do {} while (0) -#endif - /* The port number of FTP service (in network order). */#define FTPPORT cpu_to_be16(21)#define FTPDATA cpu_to_be16(20)
net/netfilter/ipvs/ip_vs_core.c +0 -8
@@ -1140,7 +1140,6 @@ struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,__be16 vport;unsigned int flags; -EnterFunction (12 ); vaddr = &svc->addr; vport = svc->port; daddr = &iph->saddr;
@@ -1208,7 +1207,6 @@ struct ip_vs_conn *ip_vs_new_conn_out(struct ip_vs_service *svc,IP_VS_DBG_ADDR (cp->af, &cp->vaddr),ntohs (cp->vport),IP_VS_DBG_ADDR (cp->af, &cp->daddr),ntohs (cp->dport), cp->flags, refcount_read(&cp->refcnt)); -LeaveFunction (12 );return cp; }
@@ -1316,13 +1314,11 @@ handle_response(int af, struct sk_buff *skb, struct ip_vs_proto_data *pd,ip_vs_update_conntrack (skb, cp,0 );ip_vs_conn_put (cp); -LeaveFunction (11 );return NF_ACCEPT ; drop:ip_vs_conn_put (cp);kfree_skb (skb); -LeaveFunction (11 );return NF_STOLEN ; }
@@ -1341,8 +1337,6 @@ ip_vs_out_hook(void *priv, struct sk_buff *skb, const struct nf_hook_state *statint af = state->pf;struct sock *sk; -EnterFunction (11 ); -/* Already marked as IPVS request or reply? */ if (skb->ipvs_property)return NF_ACCEPT ;
@@ -2365,7 +2359,6 @@ static void __net_exit __ip_vs_dev_cleanup_batch(struct list_head *net_list)struct netns_ipvs *ipvs;struct net *net; -EnterFunction (2 );list_for_each_entry (net, net_list, exit_list) { ipvs =net_ipvs (net);ip_vs_unregister_hooks (ipvs,AF_INET );
@@ -2374,7 +2367,6 @@ static void __net_exit __ip_vs_dev_cleanup_batch(struct list_head *net_list)smp_wmb ();ip_vs_sync_net_cleanup (ipvs); } -LeaveFunction (2 ); }static struct pernet_operations ipvs_core_ops = {
net/netfilter/ipvs/ip_vs_ctl.c +1 -25
@@ -1061,8 +1061,6 @@ ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)unsigned int atype;int ret; -EnterFunction (2 ); -#ifdef CONFIG_IP_VS_IPV6 if (udest->af ==AF_INET6 ) { atype =ipv6_addr_type (&udest->addr.in6);
@@ -1111,7 +1109,6 @@ ip_vs_new_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)spin_lock_init (&dest->dst_lock);__ip_vs_update_dest (svc, dest, udest,1 ); -LeaveFunction (2 );return 0 ; err_stats:
@@ -1134,8 +1131,6 @@ ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)__be16 dport = udest->port;int ret; -EnterFunction (2 ); -if (udest->weight <0 ) {pr_err ("%s(): server weight less than zero\n" , __func__);return -ERANGE ;
@@ -1183,7 +1178,7 @@ ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) ret =ip_vs_start_estimator (svc->ipvs, &dest->stats);if (ret <0 ) - goto err; +return ret;__ip_vs_update_dest (svc, dest, udest,1 ); }else {/*
@@ -1192,9 +1187,6 @@ ip_vs_add_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) ret =ip_vs_new_dest (svc, udest); } -err: -LeaveFunction (2 ); -return ret; }
@@ -1209,8 +1201,6 @@ ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)union nf_inet_addr daddr;__be16 dport = udest->port; -EnterFunction (2 ); -if (udest->weight <0 ) {pr_err ("%s(): server weight less than zero\n" , __func__);return -ERANGE ;
@@ -1242,7 +1232,6 @@ ip_vs_edit_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest) }__ip_vs_update_dest (svc, dest, udest,0 ); -LeaveFunction (2 );return 0 ; }
@@ -1317,8 +1306,6 @@ ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)struct ip_vs_dest *dest;__be16 dport = udest->port; -EnterFunction (2 ); -/* We use function that requires RCU lock */ rcu_read_lock (); dest =ip_vs_lookup_dest (svc, udest->af, &udest->addr, dport);
@@ -1339,8 +1326,6 @@ ip_vs_del_dest(struct ip_vs_service *svc, struct ip_vs_dest_user_kern *udest)*/ __ip_vs_del_dest (svc->ipvs, dest, false); -LeaveFunction (2 ); -return 0 ; }
@@ -1746,7 +1731,6 @@ void ip_vs_service_nets_cleanup(struct list_head *net_list)struct netns_ipvs *ipvs;struct net *net; -EnterFunction (2 );/* Check for "full" addressed entries */ mutex_lock (&__ip_vs_mutex);list_for_each_entry (net, net_list, exit_list) {
@@ -1754,7 +1738,6 @@ void ip_vs_service_nets_cleanup(struct list_head *net_list)ip_vs_flush (ipvs, true); }mutex_unlock (&__ip_vs_mutex); -LeaveFunction (2 ); }/* Put all references for device (dst_cache) */
@@ -1792,7 +1775,6 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event,if (event !=NETDEV_DOWN || !ipvs)return NOTIFY_DONE ;IP_VS_DBG (3 ,"%s() dev=%s\n" , __func__, dev->name); -EnterFunction (2 );mutex_lock (&__ip_vs_mutex);for (idx =0 ; idx <IP_VS_SVC_TAB_SIZE ; idx++) {hlist_for_each_entry (svc, &ip_vs_svc_table[idx], s_list) {
@@ -1821,7 +1803,6 @@ static int ip_vs_dst_event(struct notifier_block *this, unsigned long event, }spin_unlock_bh (&ipvs->dest_trash_lock);mutex_unlock (&__ip_vs_mutex); -LeaveFunction (2 );return NOTIFY_DONE ; }
@@ -4537,8 +4518,6 @@ int __init ip_vs_control_init(void)int idx;int ret; -EnterFunction (2 ); -/* Initialize svc_table, ip_vs_svc_fwm_table */ for (idx =0 ; idx <IP_VS_SVC_TAB_SIZE ; idx++) {INIT_HLIST_HEAD (&ip_vs_svc_table[idx]);
@@ -4551,15 +4530,12 @@ int __init ip_vs_control_init(void)if (ret <0 )return ret; -LeaveFunction (2 );return 0 ; }void ip_vs_control_cleanup (void ) { -EnterFunction (2 );unregister_netdevice_notifier (&ip_vs_dst_notifier);/* relying on common rcu_barrier() in ip_vs_cleanup() */ -LeaveFunction (2 ); }
net/netfilter/ipvs/ip_vs_sync.c +0 -5
@@ -1582,13 +1582,11 @@ ip_vs_send_async(struct socket *sock, const char *buffer, const size_t length)struct kvec iov;int len; -EnterFunction (7 ); iov.iov_base = (void *)buffer; iov.iov_len = length; len =kernel_sendmsg (sock, &msg, &iov,1 , (size_t )(length)); -LeaveFunction (7 );return len; }
@@ -1614,15 +1612,12 @@ ip_vs_receive(struct socket *sock, char *buffer, const size_t buflen)struct kvec iov = {buffer, buflen};int len; -EnterFunction (7 ); -/* Receive a packet */ iov_iter_kvec (&msg.msg_iter,ITER_DEST , &iov,1 , buflen); len =sock_recvmsg (sock, &msg,MSG_DONTWAIT );if (len <0 )return len; -LeaveFunction (7 );return len; }
net/netfilter/ipvs/ip_vs_xmit.c +8 -54
243 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 14/20 ipvs: Correct spelling in comments
In reply to Pablo Neira AyusoFrom: Simon Horman <[email protected]>
Correct some spelling errors flagged by codespell and found by inspection.
--- include/net/ip_vs.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
include/net/ip_vs.h +3 -3
@@ -584,7 +584,7 @@ struct ip_vs_conn {spinlock_t lock;/* lock for state transition */ volatile __u16 state;/* state info */ volatile __u16 old_state;/* old state, to be used for -* state transition triggerd +* state transition triggered * synchronization */ __u32 fwmark;/* Fire wall mark from skb */
@@ -635,7 +635,7 @@ struct ip_vs_service_user_kern {u16 protocol;union nf_inet_addr addr;/* virtual ip address */ __be16 port; -u32 fwmark;/* firwall mark of service */ +u32 fwmark;/* firewall mark of service */ /* virtual service options */ char *sched_name;
@@ -1036,7 +1036,7 @@ struct netns_ipvs {struct ipvs_sync_daemon_cfg bcfg;/* Backup Configuration */ /* net name space ptr */ struct net *net;/* Needed by timer routines */ -/* Number of heterogeneous destinations, needed becaus heterogeneous +/* Number of heterogeneous destinations, needed because heterogeneous * are not supported when synchronization is enabled. */ unsigned int mixed_address_family_dests;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 15/20 netfilter: nf_tables: extended netlink error reporting for netdevice
In reply to Pablo Neira AyusoFlowtable and netdev chains are bound to one or several netdevice, extend netlink error reporting to specify the the netdevice that triggers the error.
--- net/netfilter/nf_tables_api.c | 38 ++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-)
net/netfilter/nf_tables_api.c +24 -14
@@ -1954,7 +1954,8 @@ static struct nft_hook *nft_hook_list_find(struct list_head *hook_list,static int nf_tables_parse_netdev_hooks (struct net *net,const struct nlattr *attr, -struct list_head *hook_list) +struct list_head *hook_list, +struct netlink_ext_ack *extack) {struct nft_hook *hook, *next;const struct nlattr *tmp;
@@ -1968,10 +1969,12 @@ static int nf_tables_parse_netdev_hooks(struct net *net, hook =nft_netdev_hook_alloc (net, tmp);if (IS_ERR (hook)) { +NL_SET_BAD_ATTR (extack, tmp); err =PTR_ERR (hook); goto err_hook; }if (nft_hook_list_find (hook_list, hook)) { +NL_SET_BAD_ATTR (extack, tmp);kfree (hook); err = -EEXIST ; goto err_hook;
@@ -2004,20 +2007,23 @@ struct nft_chain_hook {static int nft_chain_parse_netdev (struct net *net,struct nlattr *tb[], -struct list_head *hook_list) +struct list_head *hook_list, +struct netlink_ext_ack *extack) {struct nft_hook *hook;int err;if (tb[NFTA_HOOK_DEV ]) { hook =nft_netdev_hook_alloc (net, tb[NFTA_HOOK_DEV ]); -if (IS_ERR (hook)) +if (IS_ERR (hook)) { +NL_SET_BAD_ATTR (extack, tb[NFTA_HOOK_DEV ]);return PTR_ERR (hook); + }list_add_tail (&hook->list, hook_list); }else if (tb[NFTA_HOOK_DEVS ]) { err =nf_tables_parse_netdev_hooks (net, tb[NFTA_HOOK_DEVS ], - hook_list); + hook_list, extack);if (err <0 )return err;
@@ -2085,7 +2091,7 @@ static int nft_chain_parse_hook(struct net *net,INIT_LIST_HEAD (&hook->list);if (nft_base_chain_netdev (family, hook->num)) { - err =nft_chain_parse_netdev (net, ha, &hook->list); + err =nft_chain_parse_netdev (net, ha, &hook->list, extack);if (err <0 ) {module_put (type->owner);return err;
@@ -7560,7 +7566,8 @@ static const struct nla_policy nft_flowtable_hook_policy[NFTA_FLOWTABLE_HOOK_MAXstatic int nft_flowtable_parse_hook (const struct nft_ctx *ctx,const struct nlattr *attr,struct nft_flowtable_hook *flowtable_hook, -struct nft_flowtable *flowtable,bool add) +struct nft_flowtable *flowtable, +struct netlink_ext_ack *extack,bool add) {struct nlattr *tb[NFTA_FLOWTABLE_HOOK_MAX +1 ];struct nft_hook *hook;
@@ -7607,7 +7614,8 @@ static int nft_flowtable_parse_hook(const struct nft_ctx *ctx,if (tb[NFTA_FLOWTABLE_HOOK_DEVS ]) { err =nf_tables_parse_netdev_hooks (ctx->net, tb[NFTA_FLOWTABLE_HOOK_DEVS ], - &flowtable_hook->list); + &flowtable_hook->list, + extack);if (err <0 )return err; }
@@ -7750,7 +7758,8 @@ static void nft_flowtable_hooks_destroy(struct list_head *hook_list) }static int nft_flowtable_update (struct nft_ctx *ctx,const struct nlmsghdr *nlh, -struct nft_flowtable *flowtable) +struct nft_flowtable *flowtable, +struct netlink_ext_ack *extack) {const struct nlattr *const *nla = ctx->nla;struct nft_flowtable_hook flowtable_hook;
@@ -7761,7 +7770,7 @@ static int nft_flowtable_update(struct nft_ctx *ctx, const struct nlmsghdr *nlh,int err; err =nft_flowtable_parse_hook (ctx, nla[NFTA_FLOWTABLE_HOOK ], - &flowtable_hook, flowtable, false); + &flowtable_hook, flowtable, extack, false);if (err <0 )return err;
@@ -7866,7 +7875,7 @@ static int nf_tables_newflowtable(struct sk_buff *skb,nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla); -return nft_flowtable_update (&ctx, info->nlh, flowtable); +return nft_flowtable_update (&ctx, info->nlh, flowtable, extack); }nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla);
@@ -7907,7 +7916,7 @@ static int nf_tables_newflowtable(struct sk_buff *skb, goto err3; err =nft_flowtable_parse_hook (&ctx, nla[NFTA_FLOWTABLE_HOOK ], - &flowtable_hook, flowtable, true); + &flowtable_hook, flowtable, extack, true);if (err <0 ) goto err4;
@@ -7959,7 +7968,8 @@ static void nft_flowtable_hook_release(struct nft_flowtable_hook *flowtable_hook }static int nft_delflowtable_hook (struct nft_ctx *ctx, -struct nft_flowtable *flowtable) +struct nft_flowtable *flowtable, +struct netlink_ext_ack *extack) {const struct nlattr *const *nla = ctx->nla;struct nft_flowtable_hook flowtable_hook;
@@ -7969,7 +7979,7 @@ static int nft_delflowtable_hook(struct nft_ctx *ctx,int err; err =nft_flowtable_parse_hook (ctx, nla[NFTA_FLOWTABLE_HOOK ], - &flowtable_hook, flowtable, false); + &flowtable_hook, flowtable, extack, false);if (err <0 )return err;
@@ -8051,7 +8061,7 @@ static int nf_tables_delflowtable(struct sk_buff *skb,nft_ctx_init (&ctx, net, skb, info->nlh, family, table,NULL , nla);if (nla[NFTA_FLOWTABLE_HOOK ]) -return nft_delflowtable_hook (&ctx, flowtable); +return nft_delflowtable_hook (&ctx, flowtable, extack);if (flowtable->use >0 ) {NL_SET_BAD_ATTR (extack, attr);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 16/20 netfilter: nf_tables: do not send complete notification of deletions
In reply to Pablo Neira AyusoIn most cases, table, name and handle is sufficient for userspace to identify an object that has been deleted. Skipping unneeded fields in the netlink attributes in the message saves bandwidth (ie. less chances of hitting ENOBUFS).
Rules are an exception: the existing userspace monitor code relies on the rule definition. This exception can be removed by implementing a rule cache in userspace, this is already supported by the tracing infrastructure.
Regarding flowtables, incremental deletion of devices is possible. Skipping a full notification allows userspace to differentiate between flowtable removal and incremental removal of devices.
--- net/netfilter/nf_tables_api.c | 70 +++++++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 19 deletions(-)
net/netfilter/nf_tables_api.c +51 -19
@@ -819,12 +819,20 @@ static int nf_tables_fill_table_info(struct sk_buff *skb, struct net *net, goto nla_put_failure;if (nla_put_string (skb,NFTA_TABLE_NAME , table->name) || -nla_put_be32 (skb,NFTA_TABLE_FLAGS , -htonl (table->flags &NFT_TABLE_F_MASK )) ||nla_put_be32 (skb,NFTA_TABLE_USE ,htonl (table->use)) ||nla_put_be64 (skb,NFTA_TABLE_HANDLE ,cpu_to_be64 (table->handle),NFTA_TABLE_PAD )) goto nla_put_failure; + +if (event ==NFT_MSG_DELTABLE ) { +nlmsg_end (skb, nlh); +return 0 ; + } + +if (nla_put_be32 (skb,NFTA_TABLE_FLAGS , +htonl (table->flags &NFT_TABLE_F_MASK ))) + goto nla_put_failure; +if (nft_table_has_owner (table) &&nla_put_be32 (skb,NFTA_TABLE_OWNER ,htonl (table->nlpid))) goto nla_put_failure;
@@ -1626,13 +1634,16 @@ static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net,if (!nlh) goto nla_put_failure; -if (nla_put_string (skb,NFTA_CHAIN_TABLE , table->name)) - goto nla_put_failure; -if (nla_put_be64 (skb,NFTA_CHAIN_HANDLE ,cpu_to_be64 (chain->handle), +if (nla_put_string (skb,NFTA_CHAIN_TABLE , table->name) || +nla_put_string (skb,NFTA_CHAIN_NAME , chain->name) || +nla_put_be64 (skb,NFTA_CHAIN_HANDLE ,cpu_to_be64 (chain->handle),NFTA_CHAIN_PAD )) goto nla_put_failure; -if (nla_put_string (skb,NFTA_CHAIN_NAME , chain->name)) - goto nla_put_failure; + +if (event ==NFT_MSG_DELCHAIN ) { +nlmsg_end (skb, nlh); +return 0 ; + }if (nft_is_base_chain (chain)) {const struct nft_base_chain *basechain =nft_base_chain (chain);
@@ -4150,6 +4161,12 @@ static int nf_tables_fill_set(struct sk_buff *skb, const struct nft_ctx *ctx,if (nla_put_be64 (skb,NFTA_SET_HANDLE ,cpu_to_be64 (set->handle),NFTA_SET_PAD )) goto nla_put_failure; + +if (event ==NFT_MSG_DELSET ) { +nlmsg_end (skb, nlh); +return 0 ; + } +if (set->flags !=0 )if (nla_put_be32 (skb,NFTA_SET_FLAGS ,htonl (set->flags))) goto nla_put_failure;
@@ -7154,13 +7171,20 @@ static int nf_tables_fill_obj_info(struct sk_buff *skb, struct net *net,if (nla_put_string (skb,NFTA_OBJ_TABLE , table->name) ||nla_put_string (skb,NFTA_OBJ_NAME , obj->key.name) || -nla_put_be32 (skb,NFTA_OBJ_TYPE ,htonl (obj->ops->type->type)) || -nla_put_be32 (skb,NFTA_OBJ_USE ,htonl (obj->use)) || -nft_object_dump (skb,NFTA_OBJ_DATA , obj, reset) ||nla_put_be64 (skb,NFTA_OBJ_HANDLE ,cpu_to_be64 (obj->handle),NFTA_OBJ_PAD )) goto nla_put_failure; +if (event ==NFT_MSG_DELOBJ ) { +nlmsg_end (skb, nlh); +return 0 ; + } + +if (nla_put_be32 (skb,NFTA_OBJ_TYPE ,htonl (obj->ops->type->type)) || +nla_put_be32 (skb,NFTA_OBJ_USE ,htonl (obj->use)) || +nft_object_dump (skb,NFTA_OBJ_DATA , obj, reset)) + goto nla_put_failure; +if (obj->udata &&nla_put (skb,NFTA_OBJ_USERDATA , obj->udlen, obj->udata)) goto nla_put_failure;
@@ -8089,9 +8113,16 @@ static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,if (nla_put_string (skb,NFTA_FLOWTABLE_TABLE , flowtable->table->name) ||nla_put_string (skb,NFTA_FLOWTABLE_NAME , flowtable->name) || -nla_put_be32 (skb,NFTA_FLOWTABLE_USE ,htonl (flowtable->use)) ||nla_put_be64 (skb,NFTA_FLOWTABLE_HANDLE ,cpu_to_be64 (flowtable->handle), -NFTA_FLOWTABLE_PAD ) || +NFTA_FLOWTABLE_PAD )) + goto nla_put_failure; + +if (event ==NFT_MSG_DELFLOWTABLE && !hook_list) { +nlmsg_end (skb, nlh); +return 0 ; + } + +if (nla_put_be32 (skb,NFTA_FLOWTABLE_USE ,htonl (flowtable->use)) ||nla_put_be32 (skb,NFTA_FLOWTABLE_FLAGS ,htonl (flowtable->data.flags))) goto nla_put_failure;
@@ -8106,6 +8137,9 @@ static int nf_tables_fill_flowtable_info(struct sk_buff *skb, struct net *net,if (!nest_devs) goto nla_put_failure; +if (!hook_list) + hook_list = &flowtable->hook_list; +list_for_each_entry_rcu (hook, hook_list, list) {if (nla_put_string (skb,NFTA_DEVICE_NAME , hook->ops.dev->name)) goto nla_put_failure;
@@ -8162,8 +8196,7 @@ static int nf_tables_dump_flowtable(struct sk_buff *skb,NFT_MSG_NEWFLOWTABLE ,NLM_F_MULTI |NLM_F_APPEND , table->family, - flowtable, - &flowtable->hook_list) < 0) + flowtable, NULL) < 0) goto done;nl_dump_check_consistent (cb,nlmsg_hdr (skb));
@@ -8258,7 +8291,7 @@ static int nf_tables_getflowtable(struct sk_buff *skb, err =nf_tables_fill_flowtable_info (skb2, net,NETLINK_CB (skb).portid, info->nlh->nlmsg_seq,NFT_MSG_NEWFLOWTABLE ,0 , family, - flowtable, &flowtable->hook_list); + flowtable,NULL );if (err <0 ) goto err_fill_flowtable_info;
@@ -8271,8 +8304,7 @@ static int nf_tables_getflowtable(struct sk_buff *skb,static void nf_tables_flowtable_notify (struct nft_ctx *ctx,struct nft_flowtable *flowtable, -struct list_head *hook_list, -int event) +struct list_head *hook_list,int event) {struct nftables_pernet *nft_net =nft_pernet (ctx->net);struct sk_buff *skb;
@@ -9333,7 +9365,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)nft_clear (net,nft_trans_flowtable (trans));nf_tables_flowtable_notify (&trans->ctx,nft_trans_flowtable (trans), - &nft_trans_flowtable (trans)->hook_list, +NULL ,NFT_MSG_NEWFLOWTABLE ); }nft_trans_destroy (trans);
@@ -9351,7 +9383,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)list_del_rcu (&nft_trans_flowtable (trans)->list);nf_tables_flowtable_notify (&trans->ctx,nft_trans_flowtable (trans), - &nft_trans_flowtable (trans)->hook_list, +NULL , trans->msg_type);nft_unregister_flowtable_net_hooks (net, &nft_trans_flowtable (trans)->hook_list);
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 17/20 netfilter: nf_tables: rename function to destroy hook list
In reply to Pablo Neira AyusoRename nft_flowtable_hooks_destroy() by nft_hooks_destroy() to prepare for netdev chain device updates.
--- net/netfilter/nf_tables_api.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +4 -4
@@ -7771,7 +7771,7 @@ static int nft_register_flowtable_net_hooks(struct net *net,return err; } -static void nft_flowtable_hooks_destroy (struct list_head *hook_list) +static void nft_hooks_destroy (struct list_head *hook_list) {struct nft_hook *hook, *next;
@@ -7952,7 +7952,7 @@ static int nf_tables_newflowtable(struct sk_buff *skb, &flowtable->hook_list, flowtable);if (err <0 ) { -nft_flowtable_hooks_destroy (&flowtable->hook_list); +nft_hooks_destroy (&flowtable->hook_list); goto err4; }
@@ -8789,7 +8789,7 @@ static void nft_commit_release(struct nft_trans *trans)case NFT_MSG_DELFLOWTABLE :case NFT_MSG_DESTROYFLOWTABLE :if (nft_trans_flowtable_update (trans)) -nft_flowtable_hooks_destroy (&nft_trans_flowtable_hooks (trans)); +nft_hooks_destroy (&nft_trans_flowtable_hooks (trans));else nf_tables_flowtable_destroy (nft_trans_flowtable (trans));break ;
@@ -9440,7 +9440,7 @@ static void nf_tables_abort_release(struct nft_trans *trans)break ;case NFT_MSG_NEWFLOWTABLE :if (nft_trans_flowtable_update (trans)) -nft_flowtable_hooks_destroy (&nft_trans_flowtable_hooks (trans)); +nft_hooks_destroy (&nft_trans_flowtable_hooks (trans));else nf_tables_flowtable_destroy (nft_trans_flowtable (trans));break ;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 18/20 netfilter: nf_tables: support for adding new devices to an existing netdev chain
In reply to Pablo Neira AyusoThis patch allows users to add devices to an existing netdev chain.
--- include/net/netfilter/nf_tables.h | 6 + net/netfilter/nf_tables_api.c | 217 +++++++++++++++++++----------- 2 files changed, 142 insertions(+), 81 deletions(-)
include/net/netfilter/nf_tables.h +6 -0
@@ -1609,6 +1609,8 @@ struct nft_trans_chain {struct nft_stats __percpu *stats;u8 policy;u32 chain_id; +struct nft_base_chain *basechain; +struct list_head hook_list; };#define nft_trans_chain_update (trans) \
@@ -1621,6 +1623,10 @@ struct nft_trans_chain { (((struct nft_trans_chain *)trans->data)->policy)#define nft_trans_chain_id (trans) \ (((struct nft_trans_chain *)trans->data)->chain_id) +#define nft_trans_basechain (trans) \ + (((struct nft_trans_chain *)trans->data)->basechain) +#define nft_trans_chain_hooks (trans) \ + (((struct nft_trans_chain *)trans->data)->hook_list)struct nft_trans_table {bool update;
net/netfilter/nf_tables_api.c +136 -81
410 lines not on this page · raw message
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 19/20 netfilter: nf_tables: support for deleting devices in an existing netdev chain
In reply to Pablo Neira AyusoThis patch allows for deleting devices in an existing netdev chain.
--- net/netfilter/nf_tables_api.c | 99 +++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 11 deletions(-)
net/netfilter/nf_tables_api.c +88 -11
@@ -1646,7 +1646,7 @@ static int nf_tables_fill_chain_info(struct sk_buff *skb, struct net *net, NFTA_CHAIN_PAD)) goto nla_put_failure; -if (event ==NFT_MSG_DELCHAIN ) { +if (event ==NFT_MSG_DELCHAIN && !hook_list) {nlmsg_end (skb, nlh);return 0 ; }
@@ -2667,6 +2667,59 @@ static int nf_tables_newchain(struct sk_buff *skb, const struct nfnl_info *info,return nf_tables_addchain (&ctx, family, genmask, policy, flags, extack); } +static int nft_delchain_hook (struct nft_ctx *ctx,struct nft_chain *chain, +struct netlink_ext_ack *extack) +{ +const struct nlattr *const *nla = ctx->nla; +struct nft_chain_hook chain_hook = {}; +struct nft_base_chain *basechain; +struct nft_hook *this, *hook; +LIST_HEAD (chain_del_list); +struct nft_trans *trans; +int err; + +if (!nft_is_base_chain (chain)) +return -EOPNOTSUPP ; + + basechain =nft_base_chain (chain); + err =nft_chain_parse_hook (ctx->net, basechain, nla, &chain_hook, + ctx->family, extack); +if (err <0 ) +return err; + +list_for_each_entry (this, &chain_hook.list, list) { + hook =nft_hook_list_find (&basechain->hook_list, this); +if (!hook) { + err = -ENOENT ; + goto err_chain_del_hook; + } +list_move (&hook->list, &chain_del_list); + } + + trans =nft_trans_alloc (ctx,NFT_MSG_DELCHAIN , +sizeof (struct nft_trans_chain )); +if (!trans) { + err = -ENOMEM ; + goto err_chain_del_hook; + } + +nft_trans_basechain (trans) = basechain; +nft_trans_chain_update (trans) = true; +INIT_LIST_HEAD (&nft_trans_chain_hooks (trans)); +list_splice (&chain_del_list, &nft_trans_chain_hooks (trans)); +nft_chain_release_hook (&chain_hook); + +nft_trans_commit_list_add_tail (ctx->net, trans); + +return 0 ; + +err_chain_del_hook: +list_splice (&chain_del_list, &basechain->hook_list); +nft_chain_release_hook (&chain_hook); + +return err; +} +static int nf_tables_delchain (struct sk_buff *skb,const struct nfnl_info *info,const struct nlattr *const nla[]) {
@@ -2707,12 +2760,19 @@ static int nf_tables_delchain(struct sk_buff *skb, const struct nfnl_info *info,return PTR_ERR (chain); } +nft_ctx_init (&ctx, net, skb, info->nlh, family, table, chain, nla); + +if (nla[NFTA_CHAIN_HOOK ]) { +if (chain->flags &NFT_CHAIN_HW_OFFLOAD ) +return -EOPNOTSUPP ; + +return nft_delchain_hook (&ctx, chain, extack); + } +if (info->nlh->nlmsg_flags &NLM_F_NONREC && chain->use >0 )return -EBUSY ; -nft_ctx_init (&ctx, net, skb, info->nlh, family, table, chain, nla); - use = chain->use;list_for_each_entry (rule, &chain->rules, list) {if (!nft_is_active_next (net, rule))
@@ -8812,7 +8872,10 @@ static void nft_commit_release(struct nft_trans *trans)break ;case NFT_MSG_DELCHAIN :case NFT_MSG_DESTROYCHAIN : -nf_tables_chain_destroy (&trans->ctx); +if (nft_trans_chain_update (trans)) +nft_hooks_destroy (&nft_trans_chain_hooks (trans)); +else +nf_tables_chain_destroy (&trans->ctx);break ;case NFT_MSG_DELRULE :case NFT_MSG_DESTROYRULE :
@@ -9304,11 +9367,20 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb)break ;case NFT_MSG_DELCHAIN :case NFT_MSG_DESTROYCHAIN : -nft_chain_del (trans->ctx.chain); -nf_tables_chain_notify (&trans->ctx, trans->msg_type,NULL ); -nf_tables_unregister_hook (trans->ctx.net, - trans->ctx.table, - trans->ctx.chain); +if (nft_trans_chain_update (trans)) { +nf_tables_chain_notify (&trans->ctx,NFT_MSG_DELCHAIN , + &nft_trans_chain_hooks (trans)); +nft_netdev_unregister_hooks (net, + &nft_trans_chain_hooks (trans), + true); + }else { +nft_chain_del (trans->ctx.chain); +nf_tables_chain_notify (&trans->ctx,NFT_MSG_DELCHAIN , +NULL ); +nf_tables_unregister_hook (trans->ctx.net, + trans->ctx.table, + trans->ctx.chain); + }break ;case NFT_MSG_NEWRULE :nft_clear (trans->ctx.net,nft_trans_rule (trans));
@@ -9558,8 +9630,13 @@ static int __nf_tables_abort(struct net *net, enum nfnl_abort_action action)break ;case NFT_MSG_DELCHAIN :case NFT_MSG_DESTROYCHAIN : - trans->ctx.table->use++; -nft_clear (trans->ctx.net, trans->ctx.chain); +if (nft_trans_chain_update (trans)) { +list_splice (&nft_trans_chain_hooks (trans), + &nft_trans_basechain (trans)->hook_list); + }else { + trans->ctx.table->use++; +nft_clear (trans->ctx.net, trans->ctx.chain); + }nft_trans_destroy (trans);break ;case NFT_MSG_NEWRULE :
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerPATCH 20/20 netfilter: nf_tables: allow to create netdev chain without device
In reply to Pablo Neira AyusoRelax netdev chain creation to allow for loading the ruleset, then adding/deleting devices at a later stage. Hardware offload does not support for this feature yet.
--- net/netfilter/nf_tables_api.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-)
net/netfilter/nf_tables_api.c +11 -12
@@ -2023,10 +2023,9 @@ struct nft_chain_hook {struct list_head list; }; -static int nft_chain_parse_netdev (struct net *net, -struct nlattr *tb[], +static int nft_chain_parse_netdev (struct net *net,struct nlattr *tb[],struct list_head *hook_list, -struct netlink_ext_ack *extack) +struct netlink_ext_ack *extack,u32 flags) {struct nft_hook *hook;int err;
@@ -2045,12 +2044,12 @@ static int nft_chain_parse_netdev(struct net *net,if (err <0 )return err; -if (list_empty (hook_list)) -return -EINVAL ; - }else { -return -EINVAL ; } +if (flags &NFT_CHAIN_HW_OFFLOAD && +list_empty (hook_list)) +return -EINVAL ; +return 0 ; }
@@ -2058,7 +2057,7 @@ static int nft_chain_parse_hook(struct net *net,struct nft_base_chain *basechain, const struct nlattr * const nla[], struct nft_chain_hook *hook, u8 family, - struct netlink_ext_ack *extack) + u32 flags, struct netlink_ext_ack *extack) { struct nftables_pernet *nft_net = nft_pernet(net);struct nlattr *ha[NFTA_HOOK_MAX +1 ];
@@ -2125,7 +2124,7 @@ static int nft_chain_parse_hook(struct net *net,INIT_LIST_HEAD (&hook->list);if (nft_base_chain_netdev (family, hook->num)) { - err =nft_chain_parse_netdev (net, ha, &hook->list, extack); + err =nft_chain_parse_netdev (net, ha, &hook->list, extack, flags);if (err <0 ) {module_put (type->owner);return err;
@@ -2263,7 +2262,7 @@ static int nf_tables_addchain(struct nft_ctx *ctx, u8 family, u8 genmask,if (flags &NFT_CHAIN_BINDING )return -EOPNOTSUPP ; - err =nft_chain_parse_hook (net,NULL , nla, &hook, family, + err =nft_chain_parse_hook (net,NULL , nla, &hook, family, flags, extack);if (err <0 )return err;
@@ -2407,7 +2406,7 @@ static int nf_tables_updchain(struct nft_ctx *ctx, u8 genmask, u8 policy, basechain =nft_base_chain (chain); err =nft_chain_parse_hook (ctx->net, basechain, nla, &hook, - ctx->family, extack); + ctx->family, flags, extack);if (err <0 )return err;
@@ -2683,7 +2682,7 @@ static int nft_delchain_hook(struct nft_ctx *ctx, struct nft_chain *chain, basechain =nft_base_chain (chain); err =nft_chain_parse_hook (ctx->net, basechain, nla, &chain_hook, - ctx->family, extack); + ctx->family, chain->flags, extack);if (err <0 )return err;
Signature · 1 line
2.30.2
Pablo Neira Ayuso
maintainerQuoted 3 lines · Pablo Neira Ayuso
Hi,
The following patchset contains Netfilter updates for net-next:
Scratch this, I will send a new pull request.