netfilter: nf_tables: fix compilation if CONFIG_COMPAT is disabled
Subsystems (2)
[email protected]
maintainerPATCH 1/9 netfilter: nf_tables: fix compilation if CONFIG_COMPAT is disabled
From: Pablo Neira Ayuso <[email protected]>
--- net/netfilter/nft_compat.c | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-)
net/netfilter/nft_compat.c +34 -8
@@ -85,17 +85,28 @@ nft_target_set_tgchk_param(struct xt_tgchk_param *par,static void target_compat_from_user (struct xt_target *t,void *in,void *out) { -int pad; - +#ifdef CONFIG_COMPAT if (t->compat_from_user) { +int pad; + t->compat_from_user (out, in); pad =XT_ALIGN (t->targetsize) - t->targetsize;if (pad >0 )memset (out + t->targetsize,0 , pad); } else +#endif memcpy (out, in,XT_ALIGN (t->targetsize)); } +static inline int nft_compat_target_offset (struct xt_target *target) +{ +#ifdef CONFIG_COMPAT +return xt_compat_target_offset (target); +#else +return 0 ; +#endif +} +static int nft_target_init (const struct nft_ctx *ctx,const struct nft_expr *expr,const struct nlattr *const tb[])
@@ -138,11 +149,12 @@ nft_target_destroy(const struct nft_expr *expr)static int target_dump_info (struct sk_buff *skb,const struct xt_target *t,const void *in) { -void *out;int ret; +#ifdef CONFIG_COMPAT if (t->compat_to_user) {mm_segment_t old_fs; +void *out; out =kmalloc (XT_ALIGN (t->targetsize),GFP_ATOMIC );if (out ==NULL )
@@ -156,6 +168,7 @@ target_dump_info(struct sk_buff *skb, const struct xt_target *t, const void *in) ret =nla_put (skb,NFTA_TARGET_INFO ,XT_ALIGN (t->targetsize), out);kfree (out); } else +#endif ret =nla_put (skb,NFTA_TARGET_INFO ,XT_ALIGN (t->targetsize), in);return ret;
@@ -255,14 +268,16 @@ nft_match_set_mtchk_param(struct xt_mtchk_param *par, const struct nft_ctx *ctx,static void match_compat_from_user (struct xt_match *m,void *in,void *out) { -int pad; - +#ifdef CONFIG_COMPAT if (m->compat_from_user) { +int pad; + m->compat_from_user (out, in); pad =XT_ALIGN (m->matchsize) - m->matchsize;if (pad >0 )memset (out + m->matchsize,0 , pad); } else +#endif memcpy (out, in,XT_ALIGN (m->matchsize)); }
@@ -302,11 +317,12 @@ nft_match_destroy(const struct nft_expr *expr)static int match_dump_info (struct sk_buff *skb,const struct xt_match *m,const void *in) { -void *out;int ret; +#ifdef CONFIG_COMPAT if (m->compat_to_user) {mm_segment_t old_fs; +void *out; out =kmalloc (XT_ALIGN (m->matchsize),GFP_ATOMIC );if (out ==NULL )
@@ -320,11 +336,21 @@ match_dump_info(struct sk_buff *skb, const struct xt_match *m, const void *in) ret =nla_put (skb,NFTA_MATCH_INFO ,XT_ALIGN (m->matchsize), out); kfree(out); } else +#endif ret =nla_put (skb,NFTA_MATCH_INFO ,XT_ALIGN (m->matchsize), in);return ret; } +static inline int nft_compat_match_offset (struct xt_match *match) +{ +#ifdef CONFIG_COMPAT +return xt_compat_match_offset (match); +#else +return 0 ; +#endif +} +static int nft_match_dump (struct sk_buff *skb,const struct nft_expr *expr) {void *info =nft_expr_priv (expr);
@@ -528,7 +554,7 @@ nft_match_select_ops(const struct nft_ctx *ctx, nft_match->ops.type = &nft_match_type; nft_match->ops.size =NFT_EXPR_SIZE (XT_ALIGN (match->matchsize) + -xt_compat_match_offset (match)); +nft_compat_match_offset (match)); nft_match->ops.eval = nft_match_eval; nft_match->ops.init = nft_match_init; nft_match->ops.destroy = nft_match_destroy;
@@ -599,7 +625,7 @@ nft_target_select_ops(const struct nft_ctx *ctx, nft_target->ops.type = &nft_target_type; nft_target->ops.size =NFT_EXPR_SIZE (XT_ALIGN (target->targetsize) + -xt_compat_target_offset (target)); +nft_compat_target_offset (target)); nft_target->ops.eval = nft_target_eval; nft_target->ops.init = nft_target_init; nft_target->ops.destroy = nft_target_destroy;
Signature · 1 line
1.7.10.4
[email protected]
maintainerPATCH 2/9 netfilter: nf_tables: fix chain after rule deletion
From: Pablo Neira Ayuso <[email protected]>
Rules are released via RCU and they may take longer to be released than the chain. Since the chain refcounter is decremented by the rcu callback, we may hit -EBUSY while deleting a chain since the chain is released faster than the rule.
Make sure the chain is released after the rule by forcing that the chain is released after all rules that reference it.
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 2 deletions(-)
include/net/netfilter/nf_tables.h +2 -0
@@ -367,6 +367,7 @@ enum nft_chain_flags {* * @rules: list of rules in the chain * @list: used internally +* @rcu_head: used internally * @handle: chain handle * @flags: bitmask of enum nft_chain_flags * @use: number of jump references to this chain
@@ -376,6 +377,7 @@ enum nft_chain_flags {struct nft_chain {struct list_head rules;struct list_head list; +struct rcu_head rcu_head;u64 handle;u8 flags;u16 use;
net/netfilter/nf_tables_api.c +12 -2
@@ -882,6 +882,15 @@ notify:return 0 ; } +static void nf_tables_rcu_chain_destroy (struct rcu_head *head) +{ + struct nft_chain *chain = container_of(head, struct nft_chain, rcu_head); + +BUG_ON (chain->use >0 ); + +kfree (chain); +} +static int nf_tables_delchain (struct sock *nlsk,struct sk_buff *skb,const struct nlmsghdr *nlh,const struct nlattr *const nla[])
@@ -905,7 +914,7 @@ static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,if (IS_ERR (chain))return PTR_ERR (chain); -if (!list_empty (&chain->rules) || chain->use >0 ) +if (!list_empty (&chain->rules))return -EBUSY ;list_del (&chain->list);
@@ -917,7 +926,8 @@ static int nf_tables_delchain(struct sock *nlsk, struct sk_buff *skb,nf_tables_chain_notify (skb, nlh, table, chain,NFT_MSG_DELCHAIN , family); -kfree (chain); +/* Make sure all rule references are gone before this is released */ +call_rcu (&chain->rcu_head, nf_tables_rcu_chain_destroy);return 0 ; }
Signature · 1 line
1.7.10.4
[email protected]
maintainerPATCH 3/9 netfilter: nf_tables: atomic rule updates and dumps
In reply to [email protected]From: Pablo Neira Ayuso <[email protected]>
This patch adds global atomic rule updates and dumps based on bitmask generations. This allows to atomically commit a set of rule-set updates incrementally without altering the internal state of existing nf_tables expressions/matches/targets.
The idea consists of using a generation cursor of 1 bit and a bitmask of 2 bits per rule. Assuming the gencursor is 0, then the genmask (expressed as a bitmask) can be interpreted as:
00 active in the present, will be active in the next generation. 01 inactive in the present, will be active in the next generation. 10 active in the present, will be deleted in the next generation.
^
gencursor
Once you invoke the transition to the next generation, the global gencursor is updated:
00 active in the present, will be active in the next generation. 01 active in the present, needs to zero its future, it becomes 00. 10 inactive in the present, delete now.
^
gencursor
If a dump is in progress and nf_tables enters a new generation, the dump will stop and return -EBUSY to let userspace know that it has to retry again. In order to invalidate dumps, a global genctr counter is increased everytime nf_tables enters a new generation.
This new operation can be used from the user-space utility that controls the firewall, eg.
nft restore < file
The rule updates contained in `file' will be applied atomically.
cat file
-----
add filter INPUT ip saddr 1.1.1.1 counter accept #1
del filter INPUT ip daddr 2.2.2.2 counter drop #2
commit #3
-EOF-
Note that the rule 1 will be inactive until the transition to the next generation, the rule 2 will be evicted in the next generation.
There is a penalty during the rule update due to the branch misprediction in the packet matching framework. But that should be quickly resolved once the iteration over the dirty list that contain rules that require updates is finished.
Event notification happens once the rule-set update has been committed. So we skip notifications is case the rule-set update is aborted, which can happen in case that the rule-set is tested to apply correctly.
This patch is based on ideas extracted from discussions with Patrick McHardy.
--- include/linux/netfilter/nf_tables.h | 8 ++ include/net/netfilter/nf_tables.h | 10 +- include/net/netns/nftables.h | 2 + net/netfilter/nf_tables_api.c | 227 ++++++++++++++++++++++++++++++++--- net/netfilter/nf_tables_core.c | 10 ++ 5 files changed, 240 insertions(+), 17 deletions(-)
include/linux/netfilter/nf_tables.h +8 -0
@@ -37,6 +37,8 @@ enum nf_tables_msg_types {NFT_MSG_NEWSETELEM ,NFT_MSG_GETSETELEM ,NFT_MSG_DELSETELEM , +NFT_MSG_COMMIT , +NFT_MSG_ABORT , NFT_MSG_MAX, };
@@ -85,12 +87,18 @@ enum nft_chain_attributes { };#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1) +enum { +NFT_RULE_F_COMMIT = (1 <<0 ), +NFT_RULE_F_MASK =NFT_RULE_F_COMMIT , +}; +enum nft_rule_attributes {NFTA_RULE_UNSPEC ,NFTA_RULE_TABLE ,NFTA_RULE_CHAIN ,NFTA_RULE_HANDLE ,NFTA_RULE_EXPRESSIONS , +NFTA_RULE_FLAGS , __NFTA_RULE_MAX };#define NFTA_RULE_MAX (__NFTA_RULE_MAX - 1)
include/net/netfilter/nf_tables.h +9 -1
@@ -319,15 +319,19 @@ static inline void *nft_expr_priv(const struct nft_expr *expr)* struct nft_rule - nf_tables rule * * @list: used internally +* @dirty_list: this rule needs an update after new generation * @rcu_head: used internally for rcu * @handle: rule handle +* @genmask: generation mask * @dlen: length of expression data * @data: expression data */ struct nft_rule {struct list_head list; +struct list_head dirty_list;struct rcu_head rcu_head; -u64 handle:48 , +u64 handle:46 , + genmask:2 , dlen:16 ;unsigned char data[] __attribute__((aligned (__alignof__(struct nft_expr ))));
@@ -366,8 +370,10 @@ enum nft_chain_flags {* struct nft_chain - nf_tables chain * * @rules: list of rules in the chain +* @dirty_rules: rules that need an update after next generation * @list: used internally * @rcu_head: used internally +* @net: net namespace that this chain belongs to * @handle: chain handle * @flags: bitmask of enum nft_chain_flags * @use: number of jump references to this chain
@@ -376,8 +382,10 @@ enum nft_chain_flags {*/ struct nft_chain {struct list_head rules; +struct list_head dirty_rules;struct list_head list;struct rcu_head rcu_head; +struct net *net;u64 handle;u8 flags;u16 use;
include/net/netns/nftables.h +2 -0
@@ -10,6 +10,8 @@ struct netns_nftables {struct nft_af_info *ipv4;struct nft_af_info *ipv6;struct nft_af_info *bridge; + u8 gencursor:1, + genctr:7; };#endif
net/netfilter/nf_tables_api.c +211 -16
325 lines not on this page · raw message
net/netfilter/nf_tables_core.c +10 -0
@@ -72,12 +72,22 @@ nft_do_chain_pktinfo(struct nft_pktinfo *pkt, const struct nf_hook_ops *ops)const struct nft_chain *chain;const struct nft_rule *rule; } jumpstack[NFT_JUMP_STACK_SIZE ]; +/* +* Cache cursor to avoid problems in case that the cursor is updated +* while traversing the ruleset. +*/ +unsigned int gencursor = chain->net->nft.gencursor; do_chain: rule = list_entry(&chain->rules, struct nft_rule, list); next_rule: data[NFT_REG_VERDICT ].verdict =NFT_CONTINUE ;list_for_each_entry_continue_rcu (rule, &chain->rules, list) { + +/* This rule is not active, skip. */ +if (unlikely (rule->genmask & (1 << gencursor))) +continue ; +nft_rule_for_each_expr (expr, last, rule) {if (expr->ops == &nft_cmp_fast_ops)nft_cmp_fast_eval (expr, data);
Signature · 1 line
1.7.10.4
Re: [nftables 3/9] netfilter: nf_tables: atomic rule updates and dumps
Hi Pablo,
Hopefully you mentioned such patchset, went far away in my mail box. I am fine with most of them but this particular one.
Some small parts should be reworked a bit.
The overall idea is nice, especially the bit field so it does not make the nft_rule bigger.
Quoted 14 lines
include/linux/netfilter/nf_tables.h +2
-0@@ -37,6 +37,8 @@ enum nf_tables_msg_types {NFT_MSG_NEWSETELEM ,NFT_MSG_GETSETELEM ,NFT_MSG_DELSETELEM , +NFT_MSG_COMMIT , +NFT_MSG_ABORT , NFT_MSG_MAX, };
include/linux/netfilter/nf_tables.h enum nft_chain_attributes { · line 94
Quoted 8 lines
include/linux/netfilter/nf_tables.h +5
-0@@ -85,12 +87,18 @@ enum nft_chain_attributes { };#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1) +enum { +NFT_RULE_F_COMMIT = (1 <<0 ), +NFT_RULE_F_MASK =NFT_RULE_F_COMMIT , +}; +
I guess you added this flags to add rules on non-transaction based use-case, so it commits right away.
Wouldn't it be better to have no such flags in the API, but instead keep track of an on going transaction in the struct nft_ctx (let's have an internal flag here). I.E: no on-going transaction -> we directly commit. If not, it's handled as being part of the transaction.
I just took a look at nfnetlink however, it does not seem possible to keep a data pointer per connection on the subsystem.
Wait, there is a field in struct sock that is meant for such usage. It would require to change nf_tables_api.c only then.
Would it make sense or am I wrong with nft_ctx usage? (there would be an issue: to free such context when the connection goes down, we could abort an on-going transaction this way then)
include/linux/netfilter/nf_tables.h enum nft_chain_attributes { · line 103
Quoted 9 lines
include/linux/netfilter/nf_tables.h +1
-0enum nft_rule_attributes {NFTA_RULE_UNSPEC ,NFTA_RULE_TABLE ,NFTA_RULE_CHAIN ,NFTA_RULE_HANDLE ,NFTA_RULE_EXPRESSIONS , +NFTA_RULE_FLAGS , __NFTA_RULE_MAX };
... then it would not require such extra arguments, we would keep a simple api.
Quoted 17 lines
include/linux/netfilter/nf_tables.h +0
-0* @rcu_head: used internally for rcu * @handle: rule handle
+ * @genmask: generation mask
* @dlen: length of expression data
* @data: expression data
*/
structnft_rule{
structlist_headlist;include/linux/netfilter/nf_tables.h +3
-1+struct list_head dirty_list;struct rcu_head rcu_head; - u64 handle:48, + u64 handle:46, + genmask:2, dlen:16; unsigned char data[] __attribute__((aligned(__alignof__(struct nft_expr))));
include/linux/netfilter/nf_tables.h enum nft_chain_flags { · line 373
Quoted 4 lines
include/linux/netfilter/nf_tables.h +1
-0@@ -366,8 +370,10 @@ enum nft_chain_flags {* struct nft_chain - nf_tables chain * * @rules: list of rules in the chain +* @dirty_rules: rules that need an update after next generation
See the end of the patch, on abort or commit function. I think we should try to do something better (performance wise)
include/linux/netfilter/nf_tables.h enum nft_chain_flags { · line 376
Quoted 3 lines
include/linux/netfilter/nf_tables.h +1
-0* @list: used internally * @rcu_head: used internally +* @net: net namespace that this chain belongs to
I would see that in another patch, even if it's a really tiny one. (preceding this current one) Moreover that we will have to full support the namespaces at some point, right?
Quoted 25 lines
include/linux/netfilter/nf_tables.h +25
-0+static int nf_tables_commit (struct sock *nlsk,struct sk_buff *skb, +const struct nlmsghdr *nlh, +const struct nlattr *const nla[]) +{ +const struct nfgenmsg *nfmsg =nlmsg_data (nlh); +const struct nft_af_info *afi; +struct net *net =sock_net (skb->sk); +struct nft_table *table; +struct nft_chain *chain; +struct nft_rule *rule, *tmp; +int family = nfmsg->nfgen_family; +bool create; + + create = nlh->nlmsg_flags &NLM_F_CREATE ? true : false; + + afi =nf_tables_afinfo_lookup (net, nfmsg->nfgen_family, create); +if (IS_ERR (afi)) +return PTR_ERR (afi); + +/* Bump generation counter, invalidate any dump in progress */ + net->nft.genctr++; + +/* A new generation has just started */ + net->nft.gencursor++; +
include/linux/netfilter/nf_tables.h enum nft_chain_flags {
Quoted 10 lines
include/linux/netfilter/nf_tables.h +10
-0+/* Make sure all packets have left the previous generation before +* purging old rules. +*/ +synchronize_rcu (); + +list_for_each_entry (table, &afi->tables, list) { +list_for_each_entry (chain, &table->chains, list) { +list_for_each_entry_safe (rule, tmp, &chain->dirty_rules, dirty_list) { +/* Delete this rule from the dirty list */ +list_del (&rule->dirty_list);
Ok here it sounds we could do better. Instead of storing the dirty list inside each chain, we may try an external one so we won't have to go through the whole table/chain/rule base like we do in a dump. If such dirty list is external (keeping a pointer on targeted table and chain too for each rule), it will be possible to loop only on it.
And having this list_for_each_entry() make the line out of 80 chars anyway. (there is the same issue for nft_dump_rules and some other)
Quoted 11 lines
include/linux/netfilter/nf_tables.h +11
-0+static int nf_tables_abort (struct sock *nlsk,struct sk_buff *skb, +const struct nlmsghdr *nlh, +const struct nlattr *const nla[]) +{ +const struct nfgenmsg *nfmsg =nlmsg_data (nlh); +const struct nft_af_info *afi; +struct net *net =sock_net (skb->sk); +struct nft_table *table; +struct nft_chain *chain; +struct nft_rule *rule, *tmp; +bool create;
include/linux/netfilter/nf_tables.h
Quoted 10 lines
include/linux/netfilter/nf_tables.h +10
-0+ + create = nlh->nlmsg_flags &NLM_F_CREATE ? true : false; + + afi =nf_tables_afinfo_lookup (net, nfmsg->nfgen_family, create); +if (IS_ERR (afi)) +return PTR_ERR (afi); + +list_for_each_entry (table, &afi->tables, list) { +list_for_each_entry (chain, &table->chains, list) { +list_for_each_entry_safe (rule, tmp, &chain->dirty_rules, dirty_list) {
Same here.
Br,
Tomasz
Pablo Neira Ayuso
maintainerRe: [nftables 3/9] netfilter: nf_tables: atomic rule updates and dumps
Hi Tomasz,
Quoted 38 lines · Tomasz Bursztyka
Hi Pablo,
Hopefully you mentioned such patchset, went far away in my mail box. I am fine with most of them but this particular one.
Some small parts should be reworked a bit.The overall idea is nice, especially the bit field so it does not make the
nft_rulebigger.Quoted 13 lines
include/linux/netfilter/nf_tables.h +2
-0@@ -37,6 +37,8 @@ enum nf_tables_msg_types {NFT_MSG_NEWSETELEM ,NFT_MSG_GETSETELEM ,NFT_MSG_DELSETELEM , +NFT_MSG_COMMIT , +NFT_MSG_ABORT , NFT_MSG_MAX, };include/linux/netfilter/nf_tables.h enum nft_chain_attributes { · line 93
Quoted 7 lines
include/linux/netfilter/nf_tables.h +5
-0@@ -85,12 +87,18 @@ enum nft_chain_attributes { };#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1) +enum { +NFT_RULE_F_COMMIT = (1 <<0 ), +NFT_RULE_F_MASK =NFT_RULE_F_COMMIT , +}; +I guess you added this flags to add rules on non-transaction based use-case, so it commits right away.
Wouldn't it be better to have no such flags in the API, but instead keep track of an on going transaction in the structnft_ctx(let's have an internal flag here).
I.E: no on-going transaction -> we directly commit. If not, it's handled as being part of the transaction.
Quoted 7 lines
I just took a look at nfnetlink however, it does not seem possible to keep a data pointer per connection on the subsystem. Wait, there is a field in struct sock that is meant for such usage. It would require to change
nf_tables_api.conly then. Would it make sense or am I wrong withnft_ctxusage? (there would be an issue: to free such context when the connection goes down, we could abort an on-going transaction this way then)
We can define some container structure to store rules in the dirty list:
struct nft_rule_update {
struct list_head head;
uint32_t nl_portid;
struct nft_rule *rule;
struct nft_table *table;
struct nft_chain *chain;
}That should allows us to remove the struct list_head dirty_list in struct nft_rule that I needed for this.
The nl_portid would be the netlink portid so we know what updates belong to what netlink connection. Still I don't see how to get rid of the commit flag.
Could you develop your idea?
Quoted 38 lines
include/linux/netfilter/nf_tables.h enum nft_chain_attributes { · line 103
Quoted 9 lines
include/linux/netfilter/nf_tables.h +1
-0enum nft_rule_attributes {NFTA_RULE_UNSPEC ,NFTA_RULE_TABLE ,NFTA_RULE_CHAIN ,NFTA_RULE_HANDLE ,NFTA_RULE_EXPRESSIONS , +NFTA_RULE_FLAGS , __NFTA_RULE_MAX };... then it would not require such extra arguments, we would keep a simple api.
Quoted 17 lines
include/linux/netfilter/nf_tables.h +4
-1* @rcu_head: used internally for rcu * @handle: rule handle +* @genmask: generation mask * @dlen: length of expression data * @data: expression data */ struct nft_rule {struct list_head list; +struct list_head dirty_list;struct rcu_head rcu_head; -u64 handle:48 , +u64 handle:46 , + genmask:2 , dlen:16 ;unsigned char data[] __attribute__((aligned (__alignof__(struct nft_expr ))));include/linux/netfilter/nf_tables.h enum nft_chain_flags { · line 373
Quoted 4 lines
include/linux/netfilter/nf_tables.h +1
-0@@ -366,8 +370,10 @@ enum nft_chain_flags {* struct nft_chain - nf_tables chain * * @rules: list of rules in the chain +* @dirty_rules: rules that need an update after next generation See the end of the patch, on abort or commit function. I think we should try to do something better (performance wise)
Quoted 8 lines
include/linux/netfilter/nf_tables.h enum nft_chain_flags { · line 376
Quoted 3 lines
include/linux/netfilter/nf_tables.h +1
-0* @list: used internally * @rcu_head: used internally +* @net: net namespace that this chain belongs to I would see that in another patch, even if it's a really tiny one. (preceding this current one)
Moreover that we will have to full support the namespaces at some point, right?
I need that net for the code in nft_do_chain_pktinfo added in this patch. Probably it can be added to base chains only, would need to check that.
Quoted 34 lines
Quoted 34 lines
include/linux/netfilter/nf_tables.h +34
-0+static int nf_tables_commit (struct sock *nlsk,struct sk_buff *skb, +const struct nlmsghdr *nlh, +const struct nlattr *const nla[]) +{ +const struct nfgenmsg *nfmsg =nlmsg_data (nlh); +const struct nft_af_info *afi; +struct net *net =sock_net (skb->sk); +struct nft_table *table; +struct nft_chain *chain; +struct nft_rule *rule, *tmp; +int family = nfmsg->nfgen_family; +bool create; + + create = nlh->nlmsg_flags &NLM_F_CREATE ? true : false; + + afi =nf_tables_afinfo_lookup (net, nfmsg->nfgen_family, create); +if (IS_ERR (afi)) +return PTR_ERR (afi); + +/* Bump generation counter, invalidate any dump in progress */ + net->nft.genctr++; + +/* A new generation has just started */ + net->nft.gencursor++; + +/* Make sure all packets have left the previous generation before +* purging old rules. +*/ +synchronize_rcu (); + +list_for_each_entry (table, &afi->tables, list) { +list_for_each_entry (chain, &table->chains, list) { +list_for_each_entry_safe (rule, tmp, &chain->dirty_rules, dirty_list) { +/* Delete this rule from the dirty list */
Quoted 8 lines
include/linux/netfilter/nf_tables.h
Quoted 1 line
include/linux/netfilter/nf_tables.h +1
-0+list_del (&rule->dirty_list);Ok here it sounds we could do better. Instead of storing the dirty list inside each chain, we may try an external one so we won't have to go through the whole table/chain/rule base like we do in a dump. If such dirty list is external (keeping a pointer on targeted table and chain too for each rule), it will be possible to loop only on it.
Yes, that's doable. We can store a pointer to the table and the chain in the nft_rule_update container structure that I proposed above.
Quoted 35 lines
And having this
list_for_each_entry()make the line out of 80 chars anyway. (there is the same issue fornft_dump_rulesand some other)Quoted 11 lines
include/linux/netfilter/nf_tables.h +11
-0+static int nf_tables_abort (struct sock *nlsk,struct sk_buff *skb, +const struct nlmsghdr *nlh, +const struct nlattr *const nla[]) +{ +const struct nfgenmsg *nfmsg =nlmsg_data (nlh); +const struct nft_af_info *afi; +struct net *net =sock_net (skb->sk); +struct nft_table *table; +struct nft_chain *chain; +struct nft_rule *rule, *tmp; +bool create;include/linux/netfilter/nf_tables.h
Quoted 10 lines
include/linux/netfilter/nf_tables.h +10
-0+ + create = nlh->nlmsg_flags &NLM_F_CREATE ? true : false; + + afi =nf_tables_afinfo_lookup (net, nfmsg->nfgen_family, create); +if (IS_ERR (afi)) +return PTR_ERR (afi); + +list_for_each_entry (table, &afi->tables, list) { +list_for_each_entry (chain, &table->chains, list) { +list_for_each_entry_safe (rule, tmp, &chain->dirty_rules, dirty_list) {Same here.
Br,
Tomasz
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [nftables 3/9] netfilter: nf_tables: atomic rule updates and dumps
Hi Pablo,
Quoted 47 lines
Quoted 29 lines
Quoted 14 lines
include/linux/netfilter/nf_tables.h +2
-0@@ -37,6 +37,8 @@ enum nf_tables_msg_types {NFT_MSG_NEWSETELEM ,NFT_MSG_GETSETELEM ,NFT_MSG_DELSETELEM , +NFT_MSG_COMMIT , +NFT_MSG_ABORT , NFT_MSG_MAX, };include/linux/netfilter/nf_tables.h enum nft_chain_attributes { · line 93
Quoted 7 lines
include/linux/netfilter/nf_tables.h +5
-0@@ -85,12 +87,18 @@ enum nft_chain_attributes { };#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1) +enum { +NFT_RULE_F_COMMIT = (1 <<0 ), +NFT_RULE_F_MASK =NFT_RULE_F_COMMIT , +}; +I guess you added this flags to add rules on non-transaction based use-case, so it commits right away.
Wouldn't it be better to have no such flags in the API, but instead keep track of an on going transaction in the structnft_ctx(let's have an internal flag here).
I.E: no on-going transaction -> we directly commit. If not, it's handled as being part of the transaction.Quoted 7 lines
I just took a look at nfnetlink however, it does not seem possible to keep a data pointer per connection on the subsystem. Wait, there is a field in struct sock that is meant for such usage. It would require to change
nf_tables_api.conly then. Would it make sense or am I wrong withnft_ctxusage? (there would be an issue: to free such context when the connection goes down, we could abort an on-going transaction this way then)We can define some container structure to store rules in the dirty list:
struct nft_rule_update { struct list_head head; uint32_t nl_portid; struct nft_rule *rule; struct nft_table *table; struct nft_chain *chain; }
Quoted 8 lines
That should allows us to remove the struct
list_headdirty_listin structnft_rulethat I needed for this.The
nl_portidwould be the netlink portid so we know what updates belong to what netlink connection. Still I don't see how to get rid of the commit flag.Could you develop your idea?
I was exactly thinking about such external list. But it would be tighten to the netlink connection more deeply: as a user data to the socket, instead of storing the nl_portid. I will explain below why.
To me iptables-nftables is a non-transaction based tool. There is no point to start a transaction for one rule. Btw it would then require a NFT_MSG_START or some sort, to start the transaction based manipulation.
Let's say now you have a software, which require to do rules manipulation, very often, which will be always connected through netlink to nftables. starts a transaction:
- manip 1
- manip 2
- ...
- manip n
Commit or Abort.
done.
Now, for whatever reason: the software crashes in the middle of a transaction. When it restarts it has no idea what it was doing and why. Here is why we should tight the transaction per netlink connection: if the connections breaks, we abort right away. It's transparent. It's consistent also with the fact that you raise a notification only when the rule has been activated. Until it comes: no one knows about those rules in the transaction but the one who owns the transaction.
We could do that via the struct sock { ... user_data ... }; related to the netlink connection, storing the transaction list.
Now, no need of a flag: if the transaction list for the current netlink connection is not NULL: you know you are on a transaction-based work, so whatever manipulation comes: it will be part of the transaction. If it's NULL, you do as usual: activating the rule right away.
Quoted 11 lines
Quoted 8 lines
include/linux/netfilter/nf_tables.h enum nft_chain_attributes { · line 96
Quoted 4 lines
include/linux/netfilter/nf_tables.h +1
-0* @list: used internally * @rcu_head: used internally +* @net: net namespace that this chain belongs to I would see that in another patch, even if it's a really tiny one. (preceding this current one)
Moreover that we will have to full support the namespaces at some point, right?I need that net for the code in
nft_do_chain_pktinfoadded in this patch. Probably it can be added to base chains only, would need to check that.
Indeed. I missed that.
Tomasz
Pablo Neira Ayuso
maintainerRe: [nftables 3/9] netfilter: nf_tables: atomic rule updates and dumps
Hi Tomasz,
On Wed, Feb 20, 2013 at 10:16:29AM +0200, Tomasz Bursztyka wrote:
[...]
Quoted 20 lines
Quoted 11 lines
We can define some container structure to store rules in the dirty list:
struct nft_rule_update { struct list_head head; uint32_t nl_portid; struct nft_rule *rule; struct nft_table *table; struct nft_chain *chain; }Quoted 8 lines
That should allows us to remove the struct
list_headdirty_listin structnft_rulethat I needed for this.The
nl_portidwould be the netlink portid so we know what updates belong to what netlink connection. Still I don't see how to get rid of the commit flag.Could you develop your idea?
Quoted 7 lines
I was exactly thinking about such external list. But it would be tighten to the netlink connection more deeply: as a user data to the socket, instead of storing the
nl_portid.
I will explain below why.To me iptables-nftables is a non-transaction based tool. There is no point to start a transaction for one rule.
Agreed, ie. Not for iptables, only for iptables-restore.
Quoted 2 lines
Btw it would then require a
NFT_MSG_STARTor some sort, to start the transaction based manipulation.
What I had in mind is that the transaction is considered to implicitly start once you open a netlink socket from user-space, you add rule updates that you want to happen atomically and then you call commit.
Quoted 10 lines
Let's say now you have a software, which require to do rules manipulation, very often, which will be always connected through netlink to nftables.
starts a transaction:
- manip 1
- manip 2
- ...
- manip n
Commit or Abort.
Quoted 7 lines
done.
Now, for whatever reason: the software crashes in the middle of a transaction. When it restarts it has no idea what it was doing and why. Here is why we should tight the transaction per netlink
connection: if the connections breaks, we abort right away. It's transparent.
Good point, I have a patch for that as well. We can catch the NETLINK_URELEASE event to remove all entries in the dirty list for the program that crashed / exited without committing.
See net/netfilter/nfnetlink_queue_core.c for instance.
Quoted 5 lines
It's consistent also with the fact that you raise a notification only when the rule has been activated. Until it comes: no one knows about those rules in the transaction but the one who owns the transaction.
Quoted 8 lines
We could do that via the struct sock { ...
user_data... }; related to the netlink connection, storing the transaction list.Now, no need of a flag: if the transaction list for the current netlink connection is not NULL: you know you are on a transaction-based work.
whatever manipulation comes: it will be part of the transaction. If it's NULL, you do as usual: activating the rule right away.
This reminds me that we can use netlink's NLM_F_ATOMIC and remove that flags.
Re: [nftables 3/9] netfilter: nf_tables: atomic rule updates and dumps
In reply to [email protected]Quoted 55 lines · On Thu, Jan 31, 2013 at 01:03:59AM +0100, [email protected] wrote:
From: Pablo Neira Ayuso <[email protected]>
This patch adds global atomic rule updates and dumps based on bitmask generations. This allows to atomically commit a set of rule-set updates incrementally without altering the internal state of existing
nf_tablesexpressions/matches/targets.The idea consists of using a generation cursor of 1 bit and a bitmask of 2 bits per rule. Assuming the gencursor is 0, then the genmask (expressed as a bitmask) can be interpreted as:
00 active in the present, will be active in the next generation. 01 inactive in the present, will be active in the next generation. 10 active in the present, will be deleted in the next generation.
^
gencursorOnce you invoke the transition to the next generation, the global gencursor is updated:
00 active in the present, will be active in the next generation. 01 active in the present, needs to zero its future, it becomes 00. 10 inactive in the present, delete now.
^
gencursorIf a dump is in progress and
nf_tablesenters a new generation, the dump will stop and return-EBUSYto let userspace know that it has to retry again. In order to invalidate dumps, a global genctr counter is increased everytimenf_tablesenters a new generation.This new operation can be used from the user-space utility that controls the firewall, eg.
nft restore < file
The rule updates contained in `file' will be applied atomically.
cat file
-----
add filter INPUT ip saddr 1.1.1.1 counter accept #1
del filter INPUT ip daddr 2.2.2.2 counter drop #2
commit #3
-EOF-Note that the rule 1 will be inactive until the transition to the next generation, the rule 2 will be evicted in the next generation.
There is a penalty during the rule update due to the branch misprediction in the packet matching framework. But that should be quickly resolved once the iteration over the dirty list that contain rules that require updates is finished.
Quoted 7 lines
Event notification happens once the rule-set update has been committed. So we skip notifications is case the rule-set update is aborted, which can happen in case that the rule-set is tested to apply correctly.
This patch is based on ideas extracted from discussions with Patrick McHardy.
Generally, I think this has a pretty high penalty both memory-wise and performance-wise, so we should reconsider the idea of moving most of the costs to userspace by temporarily adding "skip" rules that skip over uncommitted rules.
A couple of comments on the patch below though.
include/linux/netfilter/nf_tables.h enum nf_tables_msg_types { · line 41
Quoted 6 lines
include/linux/netfilter/nf_tables.h +2
-0@@ -37,6 +37,8 @@ enum nf_tables_msg_types {NFT_MSG_NEWSETELEM ,NFT_MSG_GETSETELEM ,NFT_MSG_DELSETELEM , +NFT_MSG_COMMIT , + NFT_MSG_ABORT,
I see why you're doing this, but this is similar to the NEWCHAINNAME attribute, it doesn't really fit into the scheme how the netlink API is designed.
Quoted 7 lines
index 3ba63b6..1131e49 100644
include/net/netfilter/nf_tables.h +0
-0@@ -319,15 +319,19 @@ static inline void *nft_expr_priv(const struct nft_expr *expr)* struct nft_rule - nf_tables rule * * @list: used internally
include/net/netfilter/nf_tables.h static inline void *nft_expr_priv(const struct nft_expr *expr) · line 331
Quoted 10 lines
include/net/netfilter/nf_tables.h +3
-0+* @dirty_list: this rule needs an update after new generation * @rcu_head: used internally for rcu * @handle: rule handle +* @genmask: generation mask * @dlen: length of expression data * @data: expression data */ struct nft_rule {struct list_head list; +struct list_head dirty_list;
This is a quite heavy price. To reduce memory overhead, we could just have dirty chains and iterate over all rules within them.
Quoted 16 lines
net/netfilter/nf_tables_api.c +1
-0static int nf_tables_dump_rules (struct sk_buff *skb,struct netlink_callback *cb) {@@ -1250,6 +1288,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb,unsigned int idx =0 , s_idx = cb->args[0 ];struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family; +unsigned int genctr = net->nft.genctr, gencursor = net->nft.gencursor;list_for_each_entry (afi, &net->nft.af_info, list) {if (family !=NFPROTO_UNSPEC && family != afi->family)
net/netfilter/nf_tables_api.c static int nf_tables_dump_rules(struct sk_buff *skb, · line 1301
Quoted 5 lines
net/netfilter/nf_tables_api.c +2
-0@@ -1258,6 +1297,8 @@ static int nf_tables_dump_rules(struct sk_buff *skb,list_for_each_entry (table, &afi->tables, list) {list_for_each_entry (chain, &table->chains, list) {list_for_each_entry (rule, &chain->rules, list) { +if (!nft_rule_is_active (net, rule)) + goto cont;
Not sure whether we should really skip them during the dump. That hides information from userspace, we could just as well include an INACTIVE flag and have userspace decide whether to process them or not.
net/netfilter/nf_tables_api.c err1: · line 1543
Quoted 7 lines
net/netfilter/nf_tables_api.c +7
-0+static void +nf_tables_delrule_one (struct nft_ctx *ctx,struct nft_rule *rule,u32 flags) +{ +if (flags &NFT_RULE_F_COMMIT ) { +struct nft_chain *chain = (struct nft_chain *)ctx->chain; + +nft_rule_disactivate_next (ctx->net, rule);
deactivate :)
Quoted 32 lines
net/netfilter/nf_tables_api.c +29
-0+list_add (&rule->dirty_list, &chain->dirty_rules); + }else { +list_del_rcu (&rule->list); +nf_tables_rule_notify (ctx->skb, ctx->nlh, ctx->table, + ctx->chain, rule,NFT_MSG_DELRULE , +0 , ctx->afi->family); +nf_tables_rule_destroy (rule); + } +} +static int nf_tables_delrule (struct sock *nlsk,struct sk_buff *skb,const struct nlmsghdr *nlh,const struct nlattr *const nla[]) +static int nf_tables_commit(struct sock *nlsk, struct sk_buff *skb, + const struct nlmsghdr *nlh, + const struct nlattr * const nla[]) +{ +const struct nfgenmsg *nfmsg =nlmsg_data (nlh); +const struct nft_af_info *afi; +struct net *net =sock_net (skb->sk); +struct nft_table *table; +struct nft_chain *chain; +struct nft_rule *rule, *tmp; +int family = nfmsg->nfgen_family; +bool create; + + create = nlh->nlmsg_flags &NLM_F_CREATE ? true : false; + + afi =nf_tables_afinfo_lookup (net, nfmsg->nfgen_family, create); +if (IS_ERR (afi)) +return PTR_ERR (afi); +
net/netfilter/nf_tables_api.c
Quoted 10 lines
net/netfilter/nf_tables_api.c +10
-0+/* Bump generation counter, invalidate any dump in progress */ + net->nft.genctr++; + +/* A new generation has just started */ + net->nft.gencursor++; + +/* Make sure all packets have left the previous generation before +* purging old rules. +*/ +synchronize_rcu ();
This doesn't work. As soon as synchronize_rcu() is finished, new packets might enter nft_do_chain(). And this also seems to be the main problem with this approach, you can't iteratively flip the active bits without locking out packet processing, otherwise it will be just as non-atomic as it is now. For this to work you'd need a bigger generation counter in the rules that doesn't overflow and doesn't require changes to its values.
Quoted 29 lines
net/netfilter/nf_tables_api.c +29
-0+ +list_for_each_entry (table, &afi->tables, list) { +list_for_each_entry (chain, &table->chains, list) { +list_for_each_entry_safe (rule, tmp, &chain->dirty_rules, dirty_list) { +/* Delete this rule from the dirty list */ +list_del (&rule->dirty_list); + +/* This rule was inactive in the past and just +* became active. Clear the next bit of the +* genmask since its meaning has changed, now +* it is the future. +*/ +if (nft_rule_is_active (net, rule)) { +nft_rule_clear (net, rule); +nf_tables_rule_notify (skb, nlh, table, + chain, rule, +NFT_MSG_NEWRULE , +0 , + nfmsg->nfgen_family); +continue ; + } + +/* This rule is in the past, get rid of it */ +list_del_rcu (&rule->list); +nf_tables_rule_notify (skb, nlh, table, chain, + rule,NFT_MSG_DELRULE ,0 , + family); +nf_tables_rule_destroy (rule); + }
Pablo Neira Ayuso
maintainerRe: [nftables 3/9] netfilter: nf_tables: atomic rule updates and dumps
Hi Patrick,
Quoted 71 lines · Patrick McHardy
Quoted 55 lines · On Thu, Jan 31, 2013 at 01:03:59AM +0100, [email protected] wrote:
From: Pablo Neira Ayuso <[email protected]>
This patch adds global atomic rule updates and dumps based on bitmask generations. This allows to atomically commit a set of rule-set updates incrementally without altering the internal state of existing
nf_tablesexpressions/matches/targets.The idea consists of using a generation cursor of 1 bit and a bitmask of 2 bits per rule. Assuming the gencursor is 0, then the genmask (expressed as a bitmask) can be interpreted as:
00 active in the present, will be active in the next generation. 01 inactive in the present, will be active in the next generation. 10 active in the present, will be deleted in the next generation.
^
gencursorOnce you invoke the transition to the next generation, the global gencursor is updated:
00 active in the present, will be active in the next generation. 01 active in the present, needs to zero its future, it becomes 00. 10 inactive in the present, delete now.
^
gencursorIf a dump is in progress and
nf_tablesenters a new generation, the dump will stop and return-EBUSYto let userspace know that it has to retry again. In order to invalidate dumps, a global genctr counter is increased everytimenf_tablesenters a new generation.This new operation can be used from the user-space utility that controls the firewall, eg.
nft restore < file
The rule updates contained in `file' will be applied atomically.
cat file
-----
add filter INPUT ip saddr 1.1.1.1 counter accept #1
del filter INPUT ip daddr 2.2.2.2 counter drop #2
commit #3
-EOF-Note that the rule 1 will be inactive until the transition to the next generation, the rule 2 will be evicted in the next generation.
There is a penalty during the rule update due to the branch misprediction in the packet matching framework. But that should be quickly resolved once the iteration over the dirty list that contain rules that require updates is finished.
Quoted 7 lines
Event notification happens once the rule-set update has been committed. So we skip notifications is case the rule-set update is aborted, which can happen in case that the rule-set is tested to apply correctly.
This patch is based on ideas extracted from discussions with Patrick McHardy.
Generally, I think this has a pretty high penalty both memory-wise and performance-wise, so we should reconsider the idea of moving most of the costs to userspace by temporarily adding "skip" rules that skip over uncommitted rules.
A couple of comments on the patch below though.
Quoted 10 lines
include/linux/netfilter/nf_tables.h enum nf_tables_msg_types { · line 41
Quoted 6 lines
include/linux/netfilter/nf_tables.h +2
-0@@ -37,6 +37,8 @@ enum nf_tables_msg_types {NFT_MSG_NEWSETELEM ,NFT_MSG_GETSETELEM ,NFT_MSG_DELSETELEM , +NFT_MSG_COMMIT , + NFT_MSG_ABORT,I see why you're doing this, but this is similar to the NEWCHAINNAME attribute, it doesn't really fit into the scheme how the netlink API is designed.
What would you suggest as an alternative?
Quoted 12 lines
Quoted 12 lines
index 3ba63b6..1131e49 100644
include/net/netfilter/nf_tables.h +2
-0@@ -319,15 +319,19 @@ static inline void *nft_expr_priv(const struct nft_expr *expr)* struct nft_rule - nf_tables rule * * @list: used internally +* @dirty_list: this rule needs an update after new generation * @rcu_head: used internally for rcu * @handle: rule handle +* @genmask: generation mask * @dlen: length of expression data
Quoted 8 lines
include/net/netfilter/nf_tables.h static inline void *nft_expr_priv(const struct nft_expr *expr) · line 331
Quoted 5 lines
include/net/netfilter/nf_tables.h +1
-0* @data: expression data */ struct nft_rule {struct list_head list; +struct list_head dirty_list;This is a quite heavy price. To reduce memory overhead, we could just have dirty chains and iterate over all rules within them.
We can save that the extra struct list_head for the dirty_list in the rule by allocating temporary container structure, I have a follow up patch for that.
Quoted 13 lines
Quoted 13 lines
net/netfilter/nf_tables_api.c +1
-0static int nf_tables_dump_rules (struct sk_buff *skb,struct netlink_callback *cb) {@@ -1250,6 +1288,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb,unsigned int idx =0 , s_idx = cb->args[0 ];struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family; +unsigned int genctr = net->nft.genctr, gencursor = net->nft.gencursor;
Quoted 12 lines
net/netfilter/nf_tables_api.c static int nf_tables_dump_rules(struct sk_buff *skb, · line 1301
Quoted 8 lines
net/netfilter/nf_tables_api.c +2
-0list_for_each_entry (afi, &net->nft.af_info, list) {if (family !=NFPROTO_UNSPEC && family != afi->family)@@ -1258,6 +1297,8 @@ static int nf_tables_dump_rules(struct sk_buff *skb,list_for_each_entry (table, &afi->tables, list) {list_for_each_entry (chain, &table->chains, list) {list_for_each_entry (rule, &chain->rules, list) { +if (!nft_rule_is_active (net, rule)) + goto cont;Not sure whether we should really skip them during the dump. That hides information from userspace, we could just as well include an INACTIVE flag and have userspace decide whether to process them or not.
Agreed, they should be dumped.
Quoted 9 lines
net/netfilter/nf_tables_api.c err1: · line 1543
Quoted 7 lines
net/netfilter/nf_tables_api.c +7
-0+static void +nf_tables_delrule_one (struct nft_ctx *ctx,struct nft_rule *rule,u32 flags) +{ +if (flags &NFT_RULE_F_COMMIT ) { +struct nft_chain *chain = (struct nft_chain *)ctx->chain; + +nft_rule_disactivate_next (ctx->net, rule);deactivate :)
Hm, I swear I checked this in the dictionary before writing it, you know how broken my English can be ;-)
Quoted 41 lines
Quoted 41 lines
net/netfilter/nf_tables_api.c +38
-0+list_add (&rule->dirty_list, &chain->dirty_rules); + }else { +list_del_rcu (&rule->list); +nf_tables_rule_notify (ctx->skb, ctx->nlh, ctx->table, + ctx->chain, rule,NFT_MSG_DELRULE , +0 , ctx->afi->family); +nf_tables_rule_destroy (rule); + } +} +static int nf_tables_delrule (struct sock *nlsk,struct sk_buff *skb,const struct nlmsghdr *nlh,const struct nlattr *const nla[]) +static int nf_tables_commit(struct sock *nlsk, struct sk_buff *skb, + const struct nlmsghdr *nlh, + const struct nlattr * const nla[]) +{ +const struct nfgenmsg *nfmsg =nlmsg_data (nlh); +const struct nft_af_info *afi; +struct net *net =sock_net (skb->sk); +struct nft_table *table; +struct nft_chain *chain; +struct nft_rule *rule, *tmp; +int family = nfmsg->nfgen_family; +bool create; + + create = nlh->nlmsg_flags &NLM_F_CREATE ? true : false; + + afi =nf_tables_afinfo_lookup (net, nfmsg->nfgen_family, create); +if (IS_ERR (afi)) +return PTR_ERR (afi); + +/* Bump generation counter, invalidate any dump in progress */ + net->nft.genctr++; + +/* A new generation has just started */ + net->nft.gencursor++; + +/* Make sure all packets have left the previous generation before +* purging old rules. +*/
Quoted 8 lines
net/netfilter/nf_tables_api.c static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb, · line 1632
Quoted 1 line
net/netfilter/nf_tables_api.c +1
-0+synchronize_rcu ();This doesn't work. As soon as
synchronize_rcu()is finished, new packets might enternft_do_chain(). And this also seems to be the main problem with this approach, you can't iteratively flip the active bits without locking out packet processing, otherwise it will be just as non-atomic as it is now. For this to work you'd need a bigger generation counter in the rules that doesn't overflow and doesn't require changes to its values.
The gencursor is being cached at the entrace of nft_do_chain_pktinfo to avoid that intermediate state. So AFAICS we wait until old packets have finished iterating over the rule-set with the old gencursor and, then, new packets will see the new rule-set using the new gencursor.
Quoted 28 lines
Quoted 28 lines
net/netfilter/nf_tables_api.c +28
-0+list_for_each_entry (table, &afi->tables, list) { +list_for_each_entry (chain, &table->chains, list) { +list_for_each_entry_safe (rule, tmp, &chain->dirty_rules, dirty_list) { +/* Delete this rule from the dirty list */ +list_del (&rule->dirty_list); + +/* This rule was inactive in the past and just +* became active. Clear the next bit of the +* genmask since its meaning has changed, now +* it is the future. +*/ +if (nft_rule_is_active (net, rule)) { +nft_rule_clear (net, rule); +nf_tables_rule_notify (skb, nlh, table, + chain, rule, +NFT_MSG_NEWRULE , +0 , + nfmsg->nfgen_family); +continue ; + } + +/* This rule is in the past, get rid of it */ +list_del_rcu (&rule->list); +nf_tables_rule_notify (skb, nlh, table, chain, + rule,NFT_MSG_DELRULE ,0 , + family); +nf_tables_rule_destroy (rule); + }
Re: [nftables 3/9] netfilter: nf_tables: atomic rule updates and dumps
In reply to Patrick McHardyHi Patrick,
Quoted 10 lines
include/linux/netfilter/nf_tables.h enum nf_tables_msg_types { · line 41
Quoted 7 lines
include/linux/netfilter/nf_tables.h +2
-0@@ -37,6 +37,8 @@ enum nf_tables_msg_types {NFT_MSG_NEWSETELEM ,NFT_MSG_GETSETELEM ,NFT_MSG_DELSETELEM , +NFT_MSG_COMMIT , + NFT_MSG_ABORT,I see why you're doing this, but this is similar to the NEWCHAINNAME attribute, it doesn't really fit into the scheme how the netlink API is designed.
Why doesn't it fit? Do you mean you would prefer one command leads to
one result, so no transaction based manipulation?
I would rather see a NFT_MSG_START added actually.
Quoted 14 lines
Quoted 14 lines
net/netfilter/nf_tables_api.c +1
-0static int nf_tables_dump_rules (struct sk_buff *skb,struct netlink_callback *cb) {@@ -1250,6 +1288,7 @@ static int nf_tables_dump_rules(struct sk_buff *skb,unsigned int idx =0 , s_idx = cb->args[0 ];struct net *net =sock_net (skb->sk);int family = nfmsg->nfgen_family; +unsigned int genctr = net->nft.genctr, gencursor = net->nft.gencursor;
Quoted 11 lines
net/netfilter/nf_tables_api.c static int nf_tables_dump_rules(struct sk_buff *skb, · line 1301
Quoted 8 lines
net/netfilter/nf_tables_api.c +2
-0list_for_each_entry (afi, &net->nft.af_info, list) {if (family !=NFPROTO_UNSPEC && family != afi->family)@@ -1258,6 +1297,8 @@ static int nf_tables_dump_rules(struct sk_buff *skb,list_for_each_entry (table, &afi->tables, list) {list_for_each_entry (chain, &table->chains, list) {list_for_each_entry (rule, &chain->rules, list) { +if (!nft_rule_is_active (net, rule)) + goto cont;Not sure whether we should really skip them during the dump. That hides information from userspace, we could just as well include an INACTIVE flag and have userspace decide whether to process them or not.
Personally I don't see much point dumping non-active (yet to be committed) rules to anybody.
The current way of notifying the rule change when committed, as described at the end of this patch message, is I think just right. That would be better though within the proposal I made earlier to Pablo.
Unless you don't want transaction based manipulation. What would be the idea then?
Tomasz
[email protected]
maintainerPATCH 4/9 netfilter: nf_tables: fix error path in newchain
In reply to [email protected]From: Pablo Neira Ayuso <[email protected]>
Make sure the chain is not in the table list until the hook registration returns success.
--- net/netfilter/nf_tables_api.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
net/netfilter/nf_tables_api.c +1 -2
@@ -868,8 +868,6 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb, chain->net = net;nla_strlcpy (chain->name, name,NFT_CHAIN_MAXNAMELEN ); -list_add_tail (&chain->list, &table->chains); -if (!(table->flags &NFT_TABLE_F_DORMANT ) && chain->flags &NFT_BASE_CHAIN ) { err =nf_register_hook (&nft_base_chain (chain)->ops);
@@ -878,6 +876,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,return err; } } +list_add_tail (&chain->list, &table->chains); notify:nf_tables_chain_notify (skb, nlh, table, chain,NFT_MSG_NEWCHAIN , family);
Signature · 1 line
1.7.10.4
[email protected]
maintainerPATCH 5/9 netfilter: nf_tables: add packet and byte counters per chain
In reply to [email protected]From: Pablo Neira Ayuso <[email protected]>
This patch adds per-cpu chain counters. Only base chains are allowed to have them to emulate x_tables behaviour.
--- include/linux/netfilter/nf_tables.h | 1 + include/net/netfilter/nf_tables.h | 7 +++ net/netfilter/nf_tables_api.c | 100 +++++++++++++++++++++++++++++++++++ net/netfilter/nf_tables_core.c | 9 ++++ 4 files changed, 117 insertions(+)
include/linux/netfilter/nf_tables.h +1 -0
@@ -83,6 +83,7 @@ enum nft_chain_attributes {NFTA_CHAIN_POLICY ,NFTA_CHAIN_USE ,NFTA_CHAIN_TYPE , +NFTA_CHAIN_COUNTERS , __NFTA_CHAIN_MAX };#define NFTA_CHAIN_MAX (__NFTA_CHAIN_MAX - 1)
include/net/netfilter/nf_tables.h +7 -0
@@ -400,18 +400,25 @@ enum nft_chain_type { NFT_CHAIN_T_MAX }; +struct nft_stats { +u64 bytes; +u64 pkts; +}; +/** * struct nft_base_chain - nf_tables base chain * * @ops: netfilter hook ops * @type: chain type * @policy: default policy +* @stats: per-cpu chain stats * @chain: the chain */ struct nft_base_chain {struct nf_hook_ops ops;enum nft_chain_type type;u8 policy; +struct nft_stats __percpu *stats;struct nft_chain chain; };
net/netfilter/nf_tables_api.c +100 -0
147 lines not on this page · raw message
net/netfilter/nf_tables_core.c +9 -0
@@ -137,6 +137,15 @@ next_rule: goto next_rule; } +if (rcu_access_pointer (nft_base_chain (chain)->stats)) { +struct nft_stats __percpu *stats; + +rcu_read_lock_bh (); + stats =rcu_dereference (nft_base_chain (chain)->stats); +__this_cpu_inc (stats->pkts); +__this_cpu_add (stats->bytes, pkt->skb->len); +rcu_read_unlock_bh (); + }return nft_base_chain (chain)->policy; }EXPORT_SYMBOL_GPL (nft_do_chain_pktinfo);
Signature · 1 line
1.7.10.4
[email protected]
maintainerPATCH 6/9 netfilter: nf_tables: add protocol and flags for xtables over nf_tables
In reply to [email protected]From: Pablo Neira Ayuso <[email protected]>
This patch adds the missing protocol and flags information in order to pass it to xt_check_target / xt_check_match.
--- include/linux/netfilter/nf_tables.h | 14 +++++++++++ include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 15 +++++++----- net/netfilter/nft_compat.c | 44 +++++++++++++++++++++++++++++++---- 4 files changed, 65 insertions(+), 10 deletions(-)
include/linux/netfilter/nf_tables.h +14 -0
@@ -100,10 +100,24 @@ enum nft_rule_attributes {NFTA_RULE_HANDLE ,NFTA_RULE_EXPRESSIONS ,NFTA_RULE_FLAGS , +NFTA_RULE_COMPAT , __NFTA_RULE_MAX };#define NFTA_RULE_MAX (__NFTA_RULE_MAX - 1) +enum nft_rule_compat_flags { +NFT_RULE_COMPAT_F_INV = (1 <<1 ), +NFT_RULE_COMPAT_F_MASK =NFT_RULE_COMPAT_F_INV , +}; + +enum nft_rule_compat_attributes { +NFTA_RULE_COMPAT_UNSPEC , +NFTA_RULE_COMPAT_PROTO , +NFTA_RULE_COMPAT_FLAGS , + __NFTA_RULE_COMPAT_MAX +}; +#define NFTA_RULE_COMPAT_MAX (__NFTA_RULE_COMPAT_MAX - 1) +/** * enum nft_set_flags - nf_tables set flags *
include/net/netfilter/nf_tables.h +2 -0
@@ -74,6 +74,7 @@ static inline void nft_data_debug(const struct nft_data *data)* @afi: address family info * @table: the table the chain is contained in * @chain: the chain the rule is contained in +* @nla: netlink attributes */ struct nft_ctx {struct net *net;
@@ -82,6 +83,7 @@ struct nft_ctx {const struct nft_af_info *afi;const struct nft_table *table;const struct nft_chain *chain; +const struct nlattr *const *nla; };struct nft_data_desc {
net/netfilter/nf_tables_api.c +9 -6
@@ -1037,7 +1037,8 @@ static void nft_ctx_init(struct nft_ctx *ctx,const struct nlmsghdr *nlh, const struct nft_af_info *afi, const struct nft_table *table, - const struct nft_chain *chain) + const struct nft_chain *chain, + const struct nlattr * const *nla) { ctx->net = sock_net(skb->sk); ctx->skb = skb;
@@ -1045,6 +1046,7 @@ static void nft_ctx_init(struct nft_ctx *ctx, ctx->afi = afi; ctx->table = table; ctx->chain = chain; + ctx->nla = nla; }/*
@@ -1252,6 +1254,7 @@ static const struct nla_policy nft_rule_policy[NFTA_RULE_MAX + 1] = { [NFTA_RULE_HANDLE] = { .type = NLA_U64 }, [NFTA_RULE_EXPRESSIONS] = { .type = NLA_NESTED }, [NFTA_RULE_FLAGS] = { .type = NLA_U32 }, + [NFTA_RULE_COMPAT] = { .type = NLA_NESTED }, }; static int nf_tables_fill_rule_info(struct sk_buff *skb, u32 portid, u32 seq,
@@ -1555,7 +1558,7 @@ static int nf_tables_newrule(struct sock *nlsk, struct sk_buff *skb, handle =nf_tables_alloc_handle (table); } -nft_ctx_init (&ctx, skb, nlh, afi, table, chain); +nft_ctx_init (&ctx, skb, nlh, afi, table, chain, nla); n =0 ; size =0 ;
@@ -1676,7 +1679,7 @@ static int nf_tables_delrule(struct sock *nlsk, struct sk_buff *skb,if (IS_ERR (chain))return PTR_ERR (chain); -nft_ctx_init (&ctx, skb, nlh, afi, table, chain); +nft_ctx_init (&ctx, skb, nlh, afi, table, chain, nla);if (nla[NFTA_RULE_FLAGS ]) { flags =ntohl (nla_get_be32 (nla[NFTA_RULE_FLAGS ]));
@@ -1885,7 +1888,7 @@ static int nft_ctx_init_from_setattr(struct nft_ctx *ctx,if (IS_ERR (table))return PTR_ERR (table); -nft_ctx_init (ctx, skb, nlh, afi, table,NULL ); +nft_ctx_init (ctx, skb, nlh, afi, table,NULL , nla);return 0 ; }
@@ -2168,7 +2171,7 @@ static int nf_tables_newset(struct sock *nlsk, struct sk_buff *skb,if (IS_ERR (table))return PTR_ERR (table); -nft_ctx_init (&ctx, skb, nlh, afi, table,NULL ); +nft_ctx_init (&ctx, skb, nlh, afi, table,NULL , nla); set =nf_tables_set_lookup (table, nla[NFTA_SET_NAME ]);if (IS_ERR (set)) {
@@ -2353,7 +2356,7 @@ static int nft_ctx_init_from_elemattr(struct nft_ctx *ctx,if (IS_ERR (table))return PTR_ERR (table); -nft_ctx_init (ctx, skb, nlh, afi, table,NULL ); +nft_ctx_init (ctx, skb, nlh, afi, table,NULL , nla);return 0 ; }
net/netfilter/nft_compat.c +40 -4
74 lines not on this page · raw message
Signature · 1 line
1.7.10.4
[email protected]
maintainerPATCH 7/9 netfilter: nf_tables: add trace support
In reply to [email protected]From: Pablo Neira Ayuso <[email protected]>
--- include/net/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 1 + net/netfilter/nf_tables_core.c | 55 +++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+)
include/net/netfilter/nf_tables.h +2 -0
@@ -376,6 +376,7 @@ enum nft_chain_flags {* @list: used internally * @rcu_head: used internally * @net: net namespace that this chain belongs to +* @table: table that this chain belongs to * @handle: chain handle * @flags: bitmask of enum nft_chain_flags * @use: number of jump references to this chain
@@ -388,6 +389,7 @@ struct nft_chain {struct list_head list;struct rcu_head rcu_head;struct net *net; +struct nft_table *table;u64 handle;u8 flags;u16 use;
net/netfilter/nf_tables_api.c +1 -0
@@ -962,6 +962,7 @@ static int nf_tables_newchain(struct sock *nlsk, struct sk_buff *skb,INIT_LIST_HEAD (&chain->dirty_rules); chain->handle =nf_tables_alloc_handle (table); chain->net = net; + chain->table = table;nla_strlcpy (chain->name, name,NFT_CHAIN_MAXNAMELEN ); if (!(table->flags & NFT_TABLE_F_DORMANT) &&
net/netfilter/nf_tables_core.c +55 -0
109 lines not on this page · raw message
Signature · 1 line
1.7.10.4
[email protected]
maintainerPATCH 8/9 netfilter: nf_tables: add missing code in route chain type
In reply to [email protected]From: Pablo Neira Ayuso <[email protected]>
Add missing code for the route chain type based on the mangle table code from x_tables.
--- net/ipv4/netfilter/nft_chain_route_ipv4.c | 24 +++++++++++++++++++++--- net/ipv6/netfilter/nft_chain_route_ipv6.c | 24 +++++++++++++++++++----- 2 files changed, 40 insertions(+), 8 deletions(-)
net/ipv4/netfilter/nft_chain_route_ipv4.c +21 -3
@@ -29,15 +29,33 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,unsigned int ret;struct nft_pktinfo pkt;u32 mark; +__be32 saddr, daddr; +u_int8_t tos; +const struct iphdr *iph;nft_set_pktinfo_ipv4 (&pkt, ops, skb, in, out); -// FIXME: length validation +/* root is playing with raw sockets. */ +if (skb->len <sizeof (struct iphdr ) || +ip_hdrlen (skb) <sizeof (struct iphdr )) +return NF_ACCEPT ; + mark = skb->mark; + iph =ip_hdr (skb); + saddr = iph->saddr; + daddr = iph->daddr; + tos = iph->tos; + ret =nft_do_chain_pktinfo (&pkt, ops);if (ret !=NF_DROP && ret !=NF_QUEUE ) { -if (skb->mark != mark &&ip_route_me_harder (skb,RTN_UNSPEC )) - ret =NF_DROP ; + iph =ip_hdr (skb); + +if (iph->saddr != saddr || + iph->daddr != daddr || + skb->mark != mark || + iph->tos != tos) +if (ip_route_me_harder (skb,RTN_UNSPEC )) + ret =NF_DROP ; }return ret; }
net/ipv6/netfilter/nft_chain_route_ipv6.c +19 -5
@@ -30,17 +30,31 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops, {unsigned int ret;struct nft_pktinfo pkt; -u32 mark; +struct in6_addr saddr, daddr; +u_int8_t hop_limit; +u32 mark, flowlabel;if (nft_set_pktinfo_ipv6 (&pkt, ops, skb, in, out) <0 )return NF_DROP ; +/* save source/dest address, mark, hoplimit, flowlabel, priority */ +memcpy (&saddr, &ipv6_hdr (skb)->saddr,sizeof (saddr)); +memcpy (&daddr, &ipv6_hdr (skb)->daddr,sizeof (daddr)); mark = skb->mark; + hop_limit =ipv6_hdr (skb)->hop_limit; + +/* flowlabel and prio (includes version, which shouldn't change either */ + flowlabel = *((u32 *)ipv6_hdr (skb)); + ret =nft_do_chain_pktinfo (&pkt, ops); -if (ret !=NF_DROP && ret !=NF_QUEUE ) { -if (skb->mark != mark &&ip6_route_me_harder (skb)) - ret =NF_DROP ; - } +if (ret !=NF_DROP && ret !=NF_QUEUE && + (memcmp (&ipv6_hdr (skb)->saddr, &saddr,sizeof (saddr)) || +memcmp (&ipv6_hdr (skb)->daddr, &daddr,sizeof (daddr)) || + skb->mark != mark || +ipv6_hdr (skb)->hop_limit != hop_limit || + flowlabel != *((u_int32_t *)ipv6_hdr (skb)))) +return ip6_route_me_harder (skb) ==0 ? ret :NF_DROP ; +return ret; }
Signature · 1 line
1.7.10.4
[email protected]
maintainerPATCH 9/9 netfilter: nf_tables: statify chain definition to fix sparse warning
In reply to [email protected]From: Pablo Neira Ayuso <[email protected]>
CHECK net/ipv4/netfilter/nft_chain_route_ipv4.cnet/ipv4/netfilter/nft_chain_route_ipv4.c:63:22: warning: symbol 'nft_chain_route_ipv4' was not declared. Should it be static?
--- net/ipv4/netfilter/nft_chain_nat_ipv4.c | 2 +- net/ipv4/netfilter/nft_chain_route_ipv4.c | 2 +- net/ipv6/netfilter/nft_chain_nat_ipv6.c | 2 +- net/ipv6/netfilter/nft_chain_route_ipv6.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-)
net/ipv4/netfilter/nft_chain_nat_ipv4.c +1 -1
@@ -164,7 +164,7 @@ static unsigned int nf_nat_output(const struct nf_hook_ops *ops,return ret; } -struct nf_chain_type nft_chain_nat_ipv4 = { +static struct nf_chain_type nft_chain_nat_ipv4 = { .family =NFPROTO_IPV4 , .name ="nat" , .type =NFT_CHAIN_T_NAT ,
net/ipv4/netfilter/nft_chain_route_ipv4.c +1 -1
@@ -60,7 +60,7 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,return ret; } -struct nf_chain_type nft_chain_route_ipv4 = { +static struct nf_chain_type nft_chain_route_ipv4 = { .family =NFPROTO_IPV4 , .name ="route" , .type =NFT_CHAIN_T_ROUTE ,
net/ipv6/netfilter/nft_chain_nat_ipv6.c +1 -1
@@ -170,7 +170,7 @@ static unsigned int nf_nat_ipv6_output(const struct nf_hook_ops *ops,return ret; } -struct nf_chain_type nft_chain_nat_ipv6 = { +static struct nf_chain_type nft_chain_nat_ipv6 = { .family =NFPROTO_IPV6 , .name ="nat" , .type =NFT_CHAIN_T_NAT ,
net/ipv6/netfilter/nft_chain_route_ipv6.c +1 -1
@@ -58,7 +58,7 @@ static unsigned int nf_route_table_hook(const struct nf_hook_ops *ops,return ret; } -struct nf_chain_type nft_chain_route_ipv6 = { +static struct nf_chain_type nft_chain_route_ipv6 = { .family =NFPROTO_IPV6 , .name ="route" , .type =NFT_CHAIN_T_ROUTE ,
Signature · 1 line
1.7.10.4