nf_tables: rule deletion by description
Subsystems (2)
Pablo Neira Ayuso
maintainercover nf_tables: rule deletion by description
Hi,
Rule deletion by description is a feature missing these days in nf_tables that has been requested already several times by different people.
I'm talking specifically of this feature that is available in iptables:
# iptables -D INPUT -p tcp --dport 22 -j DROP
that allows us to delete the first rule matching this description.
So far, nf_tables allows users to delete rules through unique handle numbers that you can list via -a option.
The approach I decided to follow adds most of this logic to userspace. Basically, if the user wants to delete a given rule, the idea is to dump the ruleset and compare if this rule matches any of the existing ones. In case of matching found, annotate the rule handle and use it to delete it from the kernel.
I noticed two corner cases that are problematic though:
1) A robot performs a rule deletion-after-addition from the same batch. This rule has no public u64 handle yet, therefore we cannot reference it. Add a new NFTA_RULE_ID, which is similar to NFTA_SET_ID in essence, that indicates an unique rule identifier inside the batch. This can be used to delete this rule not yet with the public handle.
1) Two threads compete to delete duplicated a rule. Even if duplicated rules make no sense, since the first rule would shadow the duplicated one, two robots inserting rules may lead us to this situation. Then, if robot A and robot B race to delete these duplicated rules, they may end up pointing to the same rule handle, so the one losing race will indicate that rule doesn't exist, while one of the two rules will still remain there, leaving the ruleset in inconsistent state. To catch this rare case, this patch introduces a way to validate what ruleset generation userspace expects to operate with. In case, kernel notices that userspace operates with a stale generation number, the kernel returns -ERESTART so userspace can transparently relookup for the rule handle.
I have a userspace patchset to implement rule deletion by description for nft, still incomplete (I may push it to a branch though for previous). Main problem so far is that rule comparisons require that the representation in both linearize and delinearize paths are the same. This feature needs quite a bit of several userspace patches still because there are a number of asymmetries in the rule representation that need to be fixed, eg. I had to split bitfield transformations that we're performing in the evaluation phase into a new specific step between evaluation and linearization.
Another solution for this problem is to handle this entirely from kernelspace. However, this is specifically cumbersome with anonymous sets, that require a full comparison of all elements given that name are meaningless. Moreover, on top of that, elements need to be ordered which is not guaranteed when iterating over set backends such as hashtable. So solving this from kernel look even more complex to me.
Let me know if you have any comment on these, thanks.
Pablo Neira Ayuso (5):
netfilter: nfnetlink: get rid of u_intX_t types
netfilter: nfnetlink: add nfnetlink_rcv_skb_batch()
netfilter: nfnetlink: allow to check for generation ID
netfilter: nf_tables: add check_genid to the nfnetlink subsystem
netfilter: nf_tables: add NFTA_RULE_ID attribute
include/linux/netfilter/nfnetlink.h | 1 + include/net/netfilter/nf_tables.h | 3 ++ include/uapi/linux/netfilter/nf_tables.h | 2 + include/uapi/linux/netfilter/nfnetlink.h | 12 +++++ net/netfilter/nf_tables_api.c | 32 ++++++++++++ net/netfilter/nfnetlink.c | 90 +++++++++++++++++++++----------- 6 files changed, 109 insertions(+), 31 deletions(-)
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 1/5 netfilter: nfnetlink: get rid of u_intX_t types
Use uX types instead.
--- net/netfilter/nfnetlink.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
net/netfilter/nfnetlink.c +8 -8
@@ -100,9 +100,9 @@ int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n) }EXPORT_SYMBOL_GPL (nfnetlink_subsys_unregister); -static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys (u_int16_t type) +static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys (u16 type) { -u_int8_t subsys_id =NFNL_SUBSYS_ID (type); +u8 subsys_id =NFNL_SUBSYS_ID (type);if (subsys_id >=NFNL_SUBSYS_COUNT )return NULL ;
@@ -111,9 +111,9 @@ static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t t }static inline const struct nfnl_callback * -nfnetlink_find_client (u_int16_t type,const struct nfnetlink_subsystem *ss) +nfnetlink_find_client (u16 type,const struct nfnetlink_subsystem *ss) { -u_int8_t cb_id =NFNL_MSG_TYPE (type); +u8 cb_id =NFNL_MSG_TYPE (type);if (cb_id >= ss->cb_count)return NULL ;
@@ -185,7 +185,7 @@ static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) {int min_len =nlmsg_total_size (sizeof (struct nfgenmsg )); -u_int8_t cb_id =NFNL_MSG_TYPE (nlh->nlmsg_type); +u8 cb_id =NFNL_MSG_TYPE (nlh->nlmsg_type);struct nlattr *cda[ss->cb[cb_id].attr_count +1 ];struct nlattr *attr = (void *)nlh + min_len;int attrlen = nlh->nlmsg_len - min_len;
@@ -273,7 +273,7 @@ enum { };static void nfnetlink_rcv_batch (struct sk_buff *skb,struct nlmsghdr *nlh, -u_int16_t subsys_id) +u16 subsys_id) {struct sk_buff *oskb = skb;struct net *net =sock_net (skb->sk);
@@ -365,7 +365,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh, {int min_len =nlmsg_total_size (sizeof (struct nfgenmsg )); -u_int8_t cb_id =NFNL_MSG_TYPE (nlh->nlmsg_type); +u8 cb_id =NFNL_MSG_TYPE (nlh->nlmsg_type);struct nlattr *cda[ss->cb[cb_id].attr_count +1 ];struct nlattr *attr = (void *)nlh + min_len;int attrlen = nlh->nlmsg_len - min_len;
@@ -439,7 +439,7 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,static void nfnetlink_rcv (struct sk_buff *skb) {struct nlmsghdr *nlh =nlmsg_hdr (skb); -u_int16_t res_id; +u16 res_id;int msglen; if (nlh->nlmsg_len < NLMSG_HDRLEN ||
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 2/5 netfilter: nfnetlink: add nfnetlink_rcv_skb_batch()
In reply to Pablo Neira AyusoAdd new nfnetlink_rcv_skb_batch() to wrap initial nfnetlink batch handling.
--- net/netfilter/nfnetlink.c | 51 ++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 23 deletions(-)
net/netfilter/nfnetlink.c +28 -23
@@ -436,12 +436,35 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,kfree_skb (skb); } -static void nfnetlink_rcv (struct sk_buff *skb) +static void nfnetlink_rcv_skb_batch (struct sk_buff *skb,struct nlmsghdr *nlh) { -struct nlmsghdr *nlh =nlmsg_hdr (skb); +struct nfgenmsg *nfgenmsg;u16 res_id;int msglen; + msglen =NLMSG_ALIGN (nlh->nlmsg_len); +if (msglen > skb->len) + msglen = skb->len; + +if (nlh->nlmsg_len <NLMSG_HDRLEN || + skb->len <NLMSG_HDRLEN +sizeof (struct nfgenmsg )) +return ; + + nfgenmsg =nlmsg_data (nlh); +skb_pull (skb, msglen); +/* Work around old nft using host byte order */ +if (nfgenmsg->res_id ==NFNL_SUBSYS_NFTABLES ) + res_id =NFNL_SUBSYS_NFTABLES ; +else + res_id =ntohs (nfgenmsg->res_id); + +nfnetlink_rcv_batch (skb, nlh, res_id); +} + +static void nfnetlink_rcv (struct sk_buff *skb) +{ +struct nlmsghdr *nlh =nlmsg_hdr (skb); +if (nlh->nlmsg_len <NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)return ;
@@ -451,28 +474,10 @@ static void nfnetlink_rcv(struct sk_buff *skb)return ; } -if (nlh->nlmsg_type ==NFNL_MSG_BATCH_BEGIN ) { -struct nfgenmsg *nfgenmsg; - - msglen =NLMSG_ALIGN (nlh->nlmsg_len); -if (msglen > skb->len) - msglen = skb->len; - -if (nlh->nlmsg_len <NLMSG_HDRLEN || - skb->len <NLMSG_HDRLEN +sizeof (struct nfgenmsg )) -return ; - - nfgenmsg =nlmsg_data (nlh); -skb_pull (skb, msglen); -/* Work around old nft using host byte order */ -if (nfgenmsg->res_id ==NFNL_SUBSYS_NFTABLES ) - res_id =NFNL_SUBSYS_NFTABLES ; -else - res_id =ntohs (nfgenmsg->res_id); -nfnetlink_rcv_batch (skb, nlh, res_id); - }else { +if (nlh->nlmsg_type ==NFNL_MSG_BATCH_BEGIN ) +nfnetlink_rcv_skb_batch (skb, nlh); +else netlink_rcv_skb (skb, &nfnetlink_rcv_msg); - } }#ifdef CONFIG_MODULES
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 3/5 netfilter: nfnetlink: allow to check for generation ID
In reply to Pablo Neira AyusoThis patch allows userspace to specify the generation ID that has been used to build an incremental batch update.
If userspace specifies the generation ID in the batch message as attribute, then nfnetlink compares it to the current generation ID so you make sure that you work against the right baseline. Otherwise, bail out with ERESTART so userspace knows that its changeset is stale and needs to respin. Userspace can do this transparently at the cost of taking slightly more time to refresh caches and rework the changeset.
This check is optional, if there is no NFNL_BATCH_GENID attribute in the batch begin message, then no check is performed.
--- include/linux/netfilter/nfnetlink.h | 1 + include/uapi/linux/netfilter/nfnetlink.h | 12 ++++++++++++ net/netfilter/nfnetlink.c | 31 +++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 4 deletions(-)
include/linux/netfilter/nfnetlink.h +1 -0
@@ -28,6 +28,7 @@ struct nfnetlink_subsystem {const struct nfnl_callback *cb;/* callback for individual types */ int (*commit)(struct net *net,struct sk_buff *skb);int (*abort)(struct net *net,struct sk_buff *skb); +bool (*valid_genid)(struct net *net,u32 genid); };int nfnetlink_subsys_register (const struct nfnetlink_subsystem *n);
include/uapi/linux/netfilter/nfnetlink.h +12 -0
@@ -65,4 +65,16 @@ struct nfgenmsg {#define NFNL_MSG_BATCH_BEGIN NLMSG_MIN_TYPE#define NFNL_MSG_BATCH_END NLMSG_MIN_TYPE+1 +/** +* enum nfnl_batch_attributes - nfnetlink batch netlink attributes +* +* @NFNL_BATCH_GENID: generation ID for this changeset (NLA_U32) +*/ +enum nfnl_batch_attributes { +NFNL_BATCH_UNSPEC , +NFNL_BATCH_GENID , + __NFNL_BATCH_MAX +}; +#define NFNL_BATCH_MAX (__NFNL_BATCH_MAX - 1) +#endif /* _UAPI_NFNETLINK_H */
net/netfilter/nfnetlink.c +27 -4
@@ -3,7 +3,7 @@* * (C) 2001 by Jay Schulist <[email protected]>, * (C) 2002-2005 by Harald Welte <[email protected]> -* (C) 2005,2007 by Pablo Neira Ayuso <[email protected]> +* (C) 2005-2017 by Pablo Neira Ayuso <[email protected]> * * Initial netfilter messages via netlink development funded and * generally made possible by Network Robots, Inc. (www.networkrobots.com)
@@ -273,7 +273,7 @@ enum { };static void nfnetlink_rcv_batch (struct sk_buff *skb,struct nlmsghdr *nlh, -u16 subsys_id) +u16 subsys_id,u32 genid) {struct sk_buff *oskb = skb;struct net *net =sock_net (skb->sk);
@@ -315,6 +315,12 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,return kfree_skb (skb); } +if (genid && ss->valid_genid && !ss->valid_genid (net, genid)) { +nfnl_unlock (subsys_id); +netlink_ack (oskb, nlh, -ERESTART ); +return kfree_skb (skb); + } +while (skb->len >=nlmsg_total_size (0 )) {int msglen, type;
@@ -436,11 +442,20 @@ static void nfnetlink_rcv_batch(struct sk_buff *skb, struct nlmsghdr *nlh,kfree_skb (skb); } +static const struct nla_policy nfnl_batch_policy[NFNL_BATCH_MAX +1 ] = { + [NFNL_BATCH_GENID ] = { .type =NLA_U32 }, +}; +static void nfnetlink_rcv_skb_batch (struct sk_buff *skb,struct nlmsghdr *nlh) { +int min_len =nlmsg_total_size (sizeof (struct nfgenmsg )); +struct nlattr *attr = (void *)nlh + min_len; +struct nlattr *cda[NFNL_BATCH_MAX +1 ]; +int attrlen = nlh->nlmsg_len - min_len;struct nfgenmsg *nfgenmsg; +int msglen, err; +u32 gen_id =0 ;u16 res_id; -int msglen; msglen =NLMSG_ALIGN (nlh->nlmsg_len);if (msglen > skb->len)
@@ -450,6 +465,14 @@ static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh) skb->len < NLMSG_HDRLEN + sizeof(struct nfgenmsg))return ; + err =nla_parse (cda,NFNL_BATCH_MAX , attr, attrlen, nfnl_batch_policy); +if (err <0 ) { +netlink_ack (skb, nlh, err); +return ; + } +if (cda[NFNL_BATCH_GENID ]) + gen_id =ntohl (nla_get_be32 (cda[NFNL_BATCH_GENID ])); + nfgenmsg =nlmsg_data (nlh);skb_pull (skb, msglen);/* Work around old nft using host byte order */
@@ -458,7 +481,7 @@ static void nfnetlink_rcv_skb_batch(struct sk_buff *skb, struct nlmsghdr *nlh)else res_id =ntohs (nfgenmsg->res_id); -nfnetlink_rcv_batch (skb, nlh, res_id); +nfnetlink_rcv_batch (skb, nlh, res_id, gen_id); }static void nfnetlink_rcv (struct sk_buff *skb)
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 4/5 netfilter: nf_tables: add check_genid to the nfnetlink subsystem
In reply to Pablo Neira AyusoThis patch implements the check generation id as provided by nfnetlink. This allows us to reject ruleset updates against stale baseline, so userspace can retry update with a fresh ruleset cache.
--- net/netfilter/nf_tables_api.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/netfilter/nf_tables_api.c +6 -0
@@ -4972,6 +4972,11 @@ static int nf_tables_abort(struct net *net, struct sk_buff *skb)return 0 ; } +static bool nf_tables_valid_genid (struct net *net,u32 genid) +{ +return net->nft.base_seq == genid; +} +static const struct nfnetlink_subsystem nf_tables_subsys = { .name ="nf_tables" , .subsys_id =NFNL_SUBSYS_NFTABLES ,
@@ -4979,6 +4984,7 @@ static const struct nfnetlink_subsystem nf_tables_subsys = { .cb = nf_tables_cb, .commit = nf_tables_commit, .abort = nf_tables_abort, + .valid_genid = nf_tables_valid_genid, }; int nft_chain_validate_dependency(const struct nft_chain *chain,
Signature · 1 line
2.1.4
Pablo Neira Ayuso
maintainerPATCH 5/5 netfilter: nf_tables: add NFTA_RULE_ID attribute
In reply to Pablo Neira AyusoThis new attribute allows us to uniquely identify a rule in transaction. Robots may trigger an insertion followed by deletion in a batch, in that scenario we still don't have a public rule handle that we can use to delete the rule. This is similar to the NFTA_SET_ID attribute that allows us to refer to an anonymous set from a batch.
--- include/net/netfilter/nf_tables.h | 3 +++ include/uapi/linux/netfilter/nf_tables.h | 2 ++ net/netfilter/nf_tables_api.c | 26 ++++++++++++++++++++++++++ 3 files changed, 31 insertions(+)
include/net/netfilter/nf_tables.h +3 -0
@@ -1202,10 +1202,13 @@ struct nft_trans {struct nft_trans_rule {struct nft_rule *rule; +u32 rule_id; };#define nft_trans_rule (trans) \ (((struct nft_trans_rule *)trans->data)->rule) +#define nft_trans_rule_id (trans) \ + (((struct nft_trans_rule *)trans->data)->rule_id)struct nft_trans_set {struct nft_set *set;
include/uapi/linux/netfilter/nf_tables.h +2 -0
@@ -207,6 +207,7 @@ enum nft_chain_attributes {* @NFTA_RULE_COMPAT: compatibility specifications of the rule (NLA_NESTED: nft_rule_compat_attributes) * @NFTA_RULE_POSITION: numeric handle of the previous rule (NLA_U64) * @NFTA_RULE_USERDATA: user data (NLA_BINARY, NFT_USERDATA_MAXLEN) +* @NFTA_RULE_ID: uniquely identifies a rule in a transaction (NLA_U32) */ enum nft_rule_attributes {NFTA_RULE_UNSPEC ,
@@ -218,6 +219,7 @@ enum nft_rule_attributes {NFTA_RULE_POSITION ,NFTA_RULE_USERDATA ,NFTA_RULE_PAD , +NFTA_RULE_ID , __NFTA_RULE_MAX };#define NFTA_RULE_MAX (__NFTA_RULE_MAX - 1)
net/netfilter/nf_tables_api.c +26 -0
@@ -240,6 +240,10 @@ static struct nft_trans *nft_trans_rule_add(struct nft_ctx *ctx, int msg_type,if (trans ==NULL )return NULL ; +if (msg_type ==NFT_MSG_NEWRULE && ctx->nla[NFTA_RULE_ID ] !=NULL ) { +nft_trans_rule_id (trans) = +ntohl (nla_get_be32 (ctx->nla[NFTA_RULE_ID ])); + }nft_trans_rule (trans) = rule;list_add_tail (&trans->list, &ctx->net->nft.commit_list);
@@ -2293,6 +2297,22 @@ static int nf_tables_newrule(struct net *net, struct sock *nlsk,return err; } +static struct nft_rule *nft_rule_lookup_byid (const struct net *net, +const struct nlattr *nla) +{ +u32 id =ntohl (nla_get_be32 (nla)); +struct nft_trans *trans; + +list_for_each_entry (trans, &net->nft.commit_list, list) { +struct nft_rule *rule =nft_trans_rule (trans); + +if (trans->msg_type ==NFT_MSG_NEWRULE && + id ==nft_trans_rule_id (trans)) +return rule; + } +return ERR_PTR (-ENOENT ); +} +static int nf_tables_delrule (struct net *net,struct sock *nlsk,struct sk_buff *skb,const struct nlmsghdr *nlh,const struct nlattr *const nla[])
@@ -2331,6 +2351,12 @@ static int nf_tables_delrule(struct net *net, struct sock *nlsk,return PTR_ERR (rule); err =nft_delrule (&ctx, rule); + }else if (nla[NFTA_RULE_ID ]) { + rule =nft_rule_lookup_byid (net, nla[NFTA_RULE_ID ]); +if (IS_ERR (rule)) +return PTR_ERR (rule); + + err =nft_delrule (&ctx, rule); }else { err =nft_delrule_by_chain (&ctx); }
Signature · 1 line
2.1.4