netfilter updates for next
Subsystems (3)
Florian Westphal
maintainercover netfilter updates for next
Hello,
The following request contains updates for your *net-next* tree.
First 5 patches, from Phil Sutter, clean up nftables dumpers to use the context buffer in the netlink_callback structure rather than a kmalloc'd buffer.
Patch 6, from myself, zaps dead code and replaces the helper function with a small inlined helper.
Patch 7, also from myself, removes another pr_debug and replaces it with the existing nf_log-based debug helpers.
Last patch, from George Guo, gets nft_table comments back in sync with the structure members.
The following changes since commit f0107b864f004bc6fa19bf6d5074b4a366f3e16a:
atm: fore200e: Drop unnecessary of_match_device() (2023-10-10 12:41:17 +0200)
are available in the Git repository at:
https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git tags/nf-next-23-10-10
for you to fetch changes up to 94ecde833be5779f8086c3a094dfa51e1dbce75f:
netfilter: cleanup struct nft_table (2023-10-10 16:34:28 +0200)
----------------------------------------------------------------
netfilter net-next pull request 2023-10-10
----------------------------------------------------------------
Florian Westphal (2):
netfilter: conntrack: simplify nf_conntrack_alter_reply
netfilter: conntrack: prefer tcp_error_log to pr_debug
George Guo (1):
netfilter: cleanup struct nft_table
Phil Sutter (5):
netfilter: nf_tables: Always allocate nft_rule_dump_ctx
netfilter: nf_tables: Drop pointless memset when dumping rules
netfilter: nf_tables: Carry reset flag in nft_rule_dump_ctx
netfilter: nf_tables: Carry s_idx in nft_rule_dump_ctx
netfilter: nf_tables: Don't allocate nft_rule_dump_ctx
include/net/netfilter/nf_conntrack.h | 14 ++++-- include/net/netfilter/nf_tables.h | 5 ++- net/netfilter/nf_conntrack_core.c | 18 -------- net/netfilter/nf_conntrack_helper.c | 7 +-- net/netfilter/nf_conntrack_proto_tcp.c | 7 +-- net/netfilter/nf_tables_api.c | 80 +++++++++++++--------------------- 6 files changed, 50 insertions(+), 81 deletions(-)
Florian Westphal
maintainerPATCH 1/8 netfilter: nf_tables: Always allocate nft_rule_dump_ctx
From: Phil Sutter <[email protected]>
It will move into struct netlink_callback's scratch area later, just put nf_tables_dump_rules_start in shape to reduce churn later.
--- net/netfilter/nf_tables_api.c | 48 +++++++++++++++-------------------- 1 file changed, 21 insertions(+), 27 deletions(-)
net/netfilter/nf_tables_api.c +21 -27
@@ -3521,10 +3521,10 @@ static int nf_tables_dump_rules(struct sk_buff *skb,if (family !=NFPROTO_UNSPEC && family != table->family)continue ; -if (ctx && ctx->table &&strcmp (ctx->table, table->name) !=0 ) +if (ctx->table &&strcmp (ctx->table, table->name) !=0 )continue ; -if (ctx && ctx->table && ctx->chain) { +if (ctx->table && ctx->chain) {struct rhlist_head *list, *tmp; list = rhltable_lookup(&table->chains_ht, ctx->chain,
@@ -3548,7 +3548,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb, goto done; } -if (ctx && ctx->table) +if (ctx->table)break ; } done:
@@ -3563,27 +3563,23 @@ static int nf_tables_dump_rules_start(struct netlink_callback *cb)const struct nlattr *const *nla = cb->data;struct nft_rule_dump_ctx *ctx =NULL ; -if (nla[NFTA_RULE_TABLE ] || nla[NFTA_RULE_CHAIN ]) { - ctx =kzalloc (sizeof (*ctx),GFP_ATOMIC ); -if (!ctx) -return -ENOMEM ; + ctx =kzalloc (sizeof (*ctx),GFP_ATOMIC ); +if (!ctx) +return -ENOMEM ; -if (nla[NFTA_RULE_TABLE ]) { - ctx->table =nla_strdup (nla[NFTA_RULE_TABLE ], -GFP_ATOMIC ); -if (!ctx->table) { -kfree (ctx); -return -ENOMEM ; - } +if (nla[NFTA_RULE_TABLE ]) { + ctx->table =nla_strdup (nla[NFTA_RULE_TABLE ],GFP_ATOMIC ); +if (!ctx->table) { +kfree (ctx); +return -ENOMEM ; } -if (nla[NFTA_RULE_CHAIN ]) { - ctx->chain =nla_strdup (nla[NFTA_RULE_CHAIN ], -GFP_ATOMIC ); -if (!ctx->chain) { -kfree (ctx->table); -kfree (ctx); -return -ENOMEM ; - } + } +if (nla[NFTA_RULE_CHAIN ]) { + ctx->chain =nla_strdup (nla[NFTA_RULE_CHAIN ],GFP_ATOMIC ); +if (!ctx->chain) { +kfree (ctx->table); +kfree (ctx); +return -ENOMEM ; } }
@@ -3595,11 +3591,9 @@ static int nf_tables_dump_rules_done(struct netlink_callback *cb) {struct nft_rule_dump_ctx *ctx = cb->data; -if (ctx) { -kfree (ctx->table); -kfree (ctx->chain); -kfree (ctx); - } +kfree (ctx->table); +kfree (ctx->chain); +kfree (ctx);return 0 ; }
Signature · 1 line
2.41.0
Re: [PATCH net-next 1/8] netfilter: nf_tables: Always allocate nft_rule_dump_ctx
This series was applied to netdev/net-next.git (main) by Florian Westphal <[email protected]>:
Hello:
This series was applied to netdev/net-next.git (main) by Florian Westphal <[email protected]>:
Quoted 10 lines · On Tue, 10 Oct 2023 16:53:31 +0200 you wrote:
From: Phil Sutter <[email protected]>
It will move into struct netlink_callback's scratch area later, just put
nf_tables_dump_rules_startin shape to reduce churn later.Suggested-by Pablo Neira Ayuso <[email protected]>Signed-off-by Phil Sutter <[email protected]>Signed-off-by Florian Westphal <[email protected]>[...]
Here is the summary with links:
- [net-next,1/8] netfilter: nf_tables: Always allocate nft_rule_dump_ctx
https://git.kernel.org/netdev/net-next/c/afed2b54c540 - [net-next,2/8] netfilter: nf_tables: Drop pointless memset when dumping rules https://git.kernel.org/netdev/net-next/c/30fa41a0f6df - [net-next,3/8] netfilter: nf_tables: Carry reset flag in nft_rule_dump_ctx https://git.kernel.org/netdev/net-next/c/405c8fd62d61 - [net-next,4/8] netfilter: nf_tables: Carry s_idx in nft_rule_dump_ctx https://git.kernel.org/netdev/net-next/c/8194d599bc01 - [net-next,5/8] netfilter: nf_tables: Don't allocate nft_rule_dump_ctx https://git.kernel.org/netdev/net-next/c/99ab9f84b85e - [net-next,6/8] netfilter: conntrack: simplify nf_conntrack_alter_reply https://git.kernel.org/netdev/net-next/c/8a23f4ab92f9 - [net-next,7/8] netfilter: conntrack: prefer tcp_error_log to pr_debug https://git.kernel.org/netdev/net-next/c/6ac9c51eebe8 - [net-next,8/8] netfilter: cleanup struct nft_table https://git.kernel.org/netdev/net-next/c/94ecde833be5
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Florian Westphal
maintainerPATCH 2/8 netfilter: nf_tables: Drop pointless memset when dumping rules
In reply to Florian WestphalFrom: Phil Sutter <[email protected]>
None of the dump callbacks uses netlink_callback::args beyond the first element, no need to zero the data.
--- net/netfilter/nf_tables_api.c | 4 ---- 1 file changed, 4 deletions(-)
net/netfilter/nf_tables_api.c +0 -4
@@ -3465,10 +3465,6 @@ static int __nf_tables_dump_rules(struct sk_buff *skb, goto cont_skip;if (*idx < s_idx) goto cont; -if (*idx > s_idx) { -memset (&cb->args[1 ],0 , -sizeof (cb->args) -sizeof (cb->args[0 ])); - }if (prule) handle = prule->handle;else
Signature · 1 line
2.41.0
Florian Westphal
maintainerPATCH 3/8 netfilter: nf_tables: Carry reset flag in nft_rule_dump_ctx
In reply to Florian WestphalFrom: Phil Sutter <[email protected]>
This relieves the dump callback from having to check nlmsg_type upon each call and instead performs the check once in .start callback.
--- net/netfilter/nf_tables_api.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-)
net/netfilter/nf_tables_api.c +9 -10
@@ -3443,15 +3443,16 @@ static void audit_log_rule_reset(const struct nft_table *table,struct nft_rule_dump_ctx {char *table;char *chain; +bool reset; };static int __nf_tables_dump_rules (struct sk_buff *skb,unsigned int *idx,struct netlink_callback *cb,const struct nft_table *table, -const struct nft_chain *chain, -bool reset) +const struct nft_chain *chain) { +struct nft_rule_dump_ctx *ctx = cb->data;struct net *net =sock_net (skb->sk);const struct nft_rule *rule, *prule;unsigned int s_idx = cb->args[0 ];
@@ -3475,7 +3476,7 @@ static int __nf_tables_dump_rules(struct sk_buff *skb, NFT_MSG_NEWRULE, NLM_F_MULTI | NLM_F_APPEND, table->family, - table, chain, rule, handle, reset) < 0) { + table, chain, rule, handle, ctx->reset) < 0) { ret =1 ;break ; }
@@ -3487,7 +3488,7 @@ static int __nf_tables_dump_rules(struct sk_buff *skb, (*idx)++; } -if (reset && entries) +if (ctx->reset && entries)audit_log_rule_reset (table, cb->seq, entries);return ret;
@@ -3504,10 +3505,6 @@ static int nf_tables_dump_rules(struct sk_buff *skb,struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family;struct nftables_pernet *nft_net; -bool reset = false; - -if (NFNL_MSG_TYPE (cb->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) - reset = true;rcu_read_lock (); nft_net =nft_pernet (net);
@@ -3532,7 +3529,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb,if (!nft_is_active (net, chain))continue ;__nf_tables_dump_rules (skb, &idx, - cb, table, chain, reset); + cb, table, chain);break ; } goto done;
@@ -3540,7 +3537,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb,list_for_each_entry_rcu (chain, &table->chains, list) {if (__nf_tables_dump_rules (skb, &idx, - cb, table, chain, reset)) + cb, table, chain)) goto done; }
@@ -3578,6 +3575,8 @@ static int nf_tables_dump_rules_start(struct netlink_callback *cb)return -ENOMEM ; } } +if (NFNL_MSG_TYPE (cb->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) + ctx->reset = true; cb->data = ctx;return 0 ;
Signature · 1 line
2.41.0
Florian Westphal
maintainerPATCH 4/8 netfilter: nf_tables: Carry s_idx in nft_rule_dump_ctx
In reply to Florian WestphalFrom: Phil Sutter <[email protected]>
In order to move the context into struct netlink_callback's scratch area, the latter must be unused first.
--- net/netfilter/nf_tables_api.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +4 -4
@@ -3441,6 +3441,7 @@ static void audit_log_rule_reset(const struct nft_table *table, }struct nft_rule_dump_ctx { +unsigned int s_idx;char *table;char *chain;bool reset;
@@ -3455,7 +3456,6 @@ static int __nf_tables_dump_rules(struct sk_buff *skb,struct nft_rule_dump_ctx *ctx = cb->data;struct net *net =sock_net (skb->sk);const struct nft_rule *rule, *prule; -unsigned int s_idx = cb->args[0 ];unsigned int entries =0 ;int ret =0 ;u64 handle;
@@ -3464,7 +3464,7 @@ static int __nf_tables_dump_rules(struct sk_buff *skb,list_for_each_entry_rcu (rule, &chain->rules, list) {if (!nft_is_active (net, rule)) goto cont_skip; -if (*idx < s_idx) +if (*idx < ctx->s_idx) goto cont;if (prule) handle = prule->handle;
@@ -3498,7 +3498,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb, struct netlink_callback *cb) {const struct nfgenmsg *nfmsg =nlmsg_data (cb->nlh); -const struct nft_rule_dump_ctx *ctx = cb->data; +struct nft_rule_dump_ctx *ctx = cb->data;struct nft_table *table;const struct nft_chain *chain;unsigned int idx =0 ;
@@ -3547,7 +3547,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb, done:rcu_read_unlock (); - cb->args[0 ] = idx; + ctx->s_idx = idx;return skb->len; }
Signature · 1 line
2.41.0
Florian Westphal
maintainerPATCH 5/8 netfilter: nf_tables: Don't allocate nft_rule_dump_ctx
In reply to Florian WestphalFrom: Phil Sutter <[email protected]>
Since struct netlink_callback::args is not used by rule dumpers anymore, use it to hold nft_rule_dump_ctx. Add a build-time check to make sure it won't ever exceed the available space.
--- net/netfilter/nf_tables_api.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-)
net/netfilter/nf_tables_api.c +6 -13
@@ -3453,7 +3453,7 @@ static int __nf_tables_dump_rules(struct sk_buff *skb,const struct nft_table *table, const struct nft_chain *chain) { - struct nft_rule_dump_ctx *ctx = cb->data; + struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;struct net *net =sock_net (skb->sk);const struct nft_rule *rule, *prule;unsigned int entries =0 ;
@@ -3498,7 +3498,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb, struct netlink_callback *cb) {const struct nfgenmsg *nfmsg =nlmsg_data (cb->nlh); -struct nft_rule_dump_ctx *ctx = cb->data; +struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;struct nft_table *table;const struct nft_chain *chain;unsigned int idx =0 ;
@@ -3553,42 +3553,35 @@ static int nf_tables_dump_rules(struct sk_buff *skb,static int nf_tables_dump_rules_start (struct netlink_callback *cb) { +struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;const struct nlattr *const *nla = cb->data; -struct nft_rule_dump_ctx *ctx =NULL ; - ctx =kzalloc (sizeof (*ctx),GFP_ATOMIC ); -if (!ctx) -return -ENOMEM ; +BUILD_BUG_ON (sizeof (*ctx) >sizeof (cb->ctx));if (nla[NFTA_RULE_TABLE ]) { ctx->table =nla_strdup (nla[NFTA_RULE_TABLE ],GFP_ATOMIC ); -if (!ctx->table) { -kfree (ctx); +if (!ctx->table)return -ENOMEM ; - } }if (nla[NFTA_RULE_CHAIN ]) { ctx->chain =nla_strdup (nla[NFTA_RULE_CHAIN ],GFP_ATOMIC );if (!ctx->chain) {kfree (ctx->table); -kfree (ctx);return -ENOMEM ; } }if (NFNL_MSG_TYPE (cb->nlh->nlmsg_type) ==NFT_MSG_GETRULE_RESET ) ctx->reset = true; - cb->data = ctx;return 0 ; }static int nf_tables_dump_rules_done (struct netlink_callback *cb) { -struct nft_rule_dump_ctx *ctx = cb->data; +struct nft_rule_dump_ctx *ctx = (void *)cb->ctx;kfree (ctx->table);kfree (ctx->chain); -kfree (ctx);return 0 ; }
Signature · 1 line
2.41.0
Florian Westphal
maintainerPATCH 6/8 netfilter: conntrack: simplify nf_conntrack_alter_reply
In reply to Florian Westphalnf_conntrack_alter_reply doesn't do helper reassignment anymore. Remove the comments that make this claim.
Furthermore, remove dead code from the function and place ot in nf_conntrack.h.
--- include/net/netfilter/nf_conntrack.h | 14 ++++++++++---- net/netfilter/nf_conntrack_core.c | 18 ------------------ net/netfilter/nf_conntrack_helper.c | 7 +------ 3 files changed, 11 insertions(+), 28 deletions(-)
include/net/netfilter/nf_conntrack.h +10 -4
@@ -160,10 +160,6 @@ static inline struct net *nf_ct_net(const struct nf_conn *ct)return read_pnet (&ct->ct_net); } -/* Alter reply tuple (maybe alter helper). */ -void nf_conntrack_alter_reply (struct nf_conn *ct, -const struct nf_conntrack_tuple *newreply); -/* Is this tuple taken? (ignoring any belonging to the given conntrack). */ int nf_conntrack_tuple_taken(const struct nf_conntrack_tuple *tuple,
@@ -284,6 +280,16 @@ static inline bool nf_is_loopback_packet(const struct sk_buff *skb)return skb->dev && skb->skb_iif && skb->dev->flags &IFF_LOOPBACK ; } +static inline void nf_conntrack_alter_reply (struct nf_conn *ct, +const struct nf_conntrack_tuple *newreply) +{ +/* Must be unconfirmed, so not in hash table yet */ +if (WARN_ON (nf_ct_is_confirmed (ct))) +return ; + + ct->tuplehash[IP_CT_DIR_REPLY ].tuple = *newreply; +} +#define nfct_time_stamp ((u32)(jiffies))/* jiffies until ct expires, 0 if already expired */
net/netfilter/nf_conntrack_core.c +0 -18
@@ -2042,24 +2042,6 @@ nf_conntrack_in(struct sk_buff *skb, const struct nf_hook_state *state) }EXPORT_SYMBOL_GPL (nf_conntrack_in); -/* Alter reply tuple (maybe alter helper). This is for NAT, and is -implicitly racy: see __nf_conntrack_confirm */ -void nf_conntrack_alter_reply (struct nf_conn *ct, -const struct nf_conntrack_tuple *newreply) -{ -struct nf_conn_help *help =nfct_help (ct); - -/* Should be unconfirmed, so not in hash table yet */ -WARN_ON (nf_ct_is_confirmed (ct)); - -nf_ct_dump_tuple (newreply); - - ct->tuplehash[IP_CT_DIR_REPLY ].tuple = *newreply; -if (ct->master || (help && !hlist_empty (&help->expectations))) -return ; -} -EXPORT_SYMBOL_GPL (nf_conntrack_alter_reply); -/* Refresh conntrack for this many jiffies and do accounting if do_acct is 1 */ void __nf_ct_refresh_acct(struct nf_conn *ct, enum ip_conntrack_info ctinfo,
net/netfilter/nf_conntrack_helper.c +1 -6
@@ -194,12 +194,7 @@ int __nf_ct_try_assign_helper(struct nf_conn *ct, struct nf_conn *tmpl,struct nf_conntrack_helper *helper =NULL ;struct nf_conn_help *help; -/* We already got a helper explicitly attached. The function -* nf_conntrack_alter_reply - in case NAT is in use - asks for looking -* the helper up again. Since now the user is in full control of -* making consistent helper configurations, skip this automatic -* re-lookup, otherwise we'll lose the helper. -*/ +/* We already got a helper explicitly attached (e.g. nft_ct) */ if (test_bit (IPS_HELPER_BIT , &ct->status))return 0 ;
Signature · 1 line
2.41.0
Florian Westphal
maintainerPATCH 7/8 netfilter: conntrack: prefer tcp_error_log to pr_debug
In reply to Florian Westphalpr_debug doesn't provide any information other than that a packet did not match existing state but also was found to not create a new connection.
Replaces this with tcp_error_log, which will also dump packets' content so one can see if this is a stray FIN or RST.
--- net/netfilter/nf_conntrack_proto_tcp.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
net/netfilter/nf_conntrack_proto_tcp.c +4 -3
@@ -835,7 +835,8 @@ static bool tcp_error(const struct tcphdr *th, static noinline bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,unsigned int dataoff, -const struct tcphdr *th) +const struct tcphdr *th, +const struct nf_hook_state *state) {enum tcp_conntrack new_state;struct net *net =nf_ct_net (ct);
@@ -846,7 +847,7 @@ static noinline bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,/* Invalid: delete conntrack */ if (new_state >=TCP_CONNTRACK_MAX ) { -pr_debug ("nf_ct_tcp: invalid new deleting.\n" ); +tcp_error_log (skb, state,"invalid new" );return false; }
@@ -980,7 +981,7 @@ int nf_conntrack_tcp_packet(struct nf_conn *ct,if (tcp_error (th, skb, dataoff, state))return -NF_ACCEPT ; -if (!nf_ct_is_confirmed (ct) && !tcp_new (ct, skb, dataoff, th)) +if (!nf_ct_is_confirmed (ct) && !tcp_new (ct, skb, dataoff, th, state))return -NF_ACCEPT ;spin_lock_bh (&ct->lock);
Signature · 1 line
2.41.0
Florian Westphal
maintainerPATCH 8/8 netfilter: cleanup struct nft_table
In reply to Florian WestphalFrom: George Guo <[email protected]>
Add comments for nlpid, family, udlen and udata in struct nft_table, and afinfo is no longer a member of struct nft_table, so remove the comment for it.
--- include/net/netfilter/nf_tables.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
include/net/netfilter/nf_tables.h +4 -1
@@ -1198,10 +1198,13 @@ static inline void nft_use_inc_restore(u32 *use)* @hgenerator: handle generator state * @handle: table handle * @use: number of chain references to this table +* @family:address family * @flags: table flag (see enum nft_table_flags) * @genmask: generation mask -* @afinfo: address family info +* @nlpid: netlink port ID * @name: name of the table +* @udlen: length of the user data +* @udata: user data * @validate_state: internal, set when transaction adds jumps */ struct nft_table {
Signature · 1 line
2.41.0