Netfilter-Devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH v3] netfilter: nft_dynset: support for element deletion
@ 2019-08-13  6:58 Ander Juaristi
  2019-08-14  8:17 ` Pablo Neira Ayuso
  2019-08-14 13:59 ` Florian Westphal
  0 siblings, 2 replies; 4+ messages in thread
From: Ander Juaristi @ 2019-08-13  6:58 UTC (permalink / raw)
  To: netfilter-devel

This patch implements the delete operation from the ruleset.

It implements a new delete() function in nft_set_rhash. It is simpler
to use than the already existing remove(), because it only takes the set
and the key as arguments, whereas remove() expects a full
nft_set_elem structure.

Signed-off-by: Ander Juaristi <a@juaristi.eus>
---
 include/net/netfilter/nf_tables.h        | 10 +++++-
 include/uapi/linux/netfilter/nf_tables.h |  1 +
 net/netfilter/nft_dynset.c               | 44 +++++++++++++++++-------
 net/netfilter/nft_set_hash.c             | 19 ++++++++++
 4 files changed, 60 insertions(+), 14 deletions(-)

diff --git a/include/net/netfilter/nf_tables.h b/include/net/netfilter/nf_tables.h
index 9e8493aad49d..d1f02f89913f 100644
--- a/include/net/netfilter/nf_tables.h
+++ b/include/net/netfilter/nf_tables.h
@@ -287,17 +287,23 @@ struct nft_expr;
  *	struct nft_set_ops - nf_tables set operations
  *
  *	@lookup: look up an element within the set
+ *	@update: update an element if exists, add it if doesn't exist
+ *	@delete: delete an element
  *	@insert: insert new element into set
  *	@activate: activate new element in the next generation
  *	@deactivate: lookup for element and deactivate it in the next generation
  *	@flush: deactivate element in the next generation
  *	@remove: remove element from set
- *	@walk: iterate over all set elemeennts
+ *	@walk: iterate over all set elements
  *	@get: get set elements
  *	@privsize: function to return size of set private data
  *	@init: initialize private data of new set instance
  *	@destroy: destroy private data of set instance
  *	@elemsize: element private size
+ *
+ *	Operations lookup, update and delete have simpler interfaces, are faster
+ *	and currently only used in the packet path. All the rest are slower,
+ *	control plane functions.
  */
 struct nft_set_ops {
 	bool				(*lookup)(const struct net *net,
@@ -312,6 +318,8 @@ struct nft_set_ops {
 						  const struct nft_expr *expr,
 						  struct nft_regs *regs,
 						  const struct nft_set_ext **ext);
+	bool				(*delete)(const struct nft_set *set,
+						  const u32 *key);
 
 	int				(*insert)(const struct net *net,
 						  const struct nft_set *set,
diff --git a/include/uapi/linux/netfilter/nf_tables.h b/include/uapi/linux/netfilter/nf_tables.h
index c6c8ec5c7c00..e8483e1e7146 100644
--- a/include/uapi/linux/netfilter/nf_tables.h
+++ b/include/uapi/linux/netfilter/nf_tables.h
@@ -634,6 +634,7 @@ enum nft_lookup_attributes {
 enum nft_dynset_ops {
 	NFT_DYNSET_OP_ADD,
 	NFT_DYNSET_OP_UPDATE,
+	NFT_DYNSET_OP_DELETE,
 };
 
 enum nft_dynset_flags {
diff --git a/net/netfilter/nft_dynset.c b/net/netfilter/nft_dynset.c
index bfb9f7463b03..0cb775be1f2e 100644
--- a/net/netfilter/nft_dynset.c
+++ b/net/netfilter/nft_dynset.c
@@ -79,37 +79,54 @@ static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
 	return NULL;
 }
 
-void nft_dynset_eval(const struct nft_expr *expr,
-		     struct nft_regs *regs, const struct nft_pktinfo *pkt)
+static bool nft_dynset_update(u8 sreg_key, int op, u64 timeout,
+			      const struct nft_expr *expr,
+			      const struct nft_pktinfo *pkt,
+			      struct nft_regs *regs, struct nft_set *set)
 {
-	const struct nft_dynset *priv = nft_expr_priv(expr);
-	struct nft_set *set = priv->set;
 	const struct nft_set_ext *ext;
 	const struct nft_expr *sexpr;
-	u64 timeout;
 
-	if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
+	if (set->ops->update(set, &regs->data[sreg_key], nft_dynset_new,
 			     expr, regs, &ext)) {
 		sexpr = NULL;
 		if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
 			sexpr = nft_set_ext_expr(ext);
 
-		if (priv->op == NFT_DYNSET_OP_UPDATE &&
+		if (op == NFT_DYNSET_OP_UPDATE &&
 		    nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
-			timeout = priv->timeout ? : set->timeout;
+			timeout = timeout ? : set->timeout;
 			*nft_set_ext_expiration(ext) = get_jiffies_64() + timeout;
 		}
 
 		if (sexpr != NULL)
 			sexpr->ops->eval(sexpr, regs, pkt);
 
-		if (priv->invert)
-			regs->verdict.code = NFT_BREAK;
-		return;
+		return true;
 	}
 
-	if (!priv->invert)
-		regs->verdict.code = NFT_BREAK;
+	return false;
+}
+
+void nft_dynset_eval(const struct nft_expr *expr,
+		     struct nft_regs *regs, const struct nft_pktinfo *pkt)
+{
+	const struct nft_dynset *priv = nft_expr_priv(expr);
+	struct nft_set *set = priv->set;
+
+	if (priv->op == NFT_DYNSET_OP_DELETE) {
+		set->ops->delete(set, &regs->data[priv->sreg_key]);
+	} else {
+		if (nft_dynset_update(priv->sreg_key, priv->op, priv->timeout,
+				      expr, pkt, regs, set)) {
+			if (priv->invert)
+				regs->verdict.code = NFT_BREAK;
+			return;
+		}
+
+		if (!priv->invert)
+			regs->verdict.code = NFT_BREAK;
+	}
 }
 
 static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
@@ -165,6 +182,7 @@ static int nft_dynset_init(const struct nft_ctx *ctx,
 	priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
 	switch (priv->op) {
 	case NFT_DYNSET_OP_ADD:
+	case NFT_DYNSET_OP_DELETE:
 		break;
 	case NFT_DYNSET_OP_UPDATE:
 		if (!(set->flags & NFT_SET_TIMEOUT))
diff --git a/net/netfilter/nft_set_hash.c b/net/netfilter/nft_set_hash.c
index 03df08801e28..07499296f673 100644
--- a/net/netfilter/nft_set_hash.c
+++ b/net/netfilter/nft_set_hash.c
@@ -237,6 +237,24 @@ static void nft_rhash_remove(const struct net *net,
 	rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
 }
 
+static bool nft_rhash_delete(const struct nft_set *set,
+			     const u32 *key)
+{
+	struct nft_rhash *priv = nft_set_priv(set);
+	struct nft_rhash_elem *he;
+	struct nft_rhash_cmp_arg arg = {
+		.genmask = NFT_GENMASK_ANY,
+		.set = set,
+		.key = key,
+	};
+
+	he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
+	if (he == NULL)
+		return false;
+
+	return rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params) == 0;
+}
+
 static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
 			   struct nft_set_iter *iter)
 {
@@ -665,6 +683,7 @@ struct nft_set_type nft_set_rhash_type __read_mostly = {
 		.remove		= nft_rhash_remove,
 		.lookup		= nft_rhash_lookup,
 		.update		= nft_rhash_update,
+		.delete		= nft_rhash_delete,
 		.walk		= nft_rhash_walk,
 		.get		= nft_rhash_get,
 	},
-- 
2.17.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] netfilter: nft_dynset: support for element deletion
  2019-08-13  6:58 [PATCH v3] netfilter: nft_dynset: support for element deletion Ander Juaristi
@ 2019-08-14  8:17 ` Pablo Neira Ayuso
  2019-08-14 13:56   ` Florian Westphal
  2019-08-14 13:59 ` Florian Westphal
  1 sibling, 1 reply; 4+ messages in thread
From: Pablo Neira Ayuso @ 2019-08-14  8:17 UTC (permalink / raw)
  To: Ander Juaristi; +Cc: netfilter-devel

On Tue, Aug 13, 2019 at 08:58:49AM +0200, Ander Juaristi wrote:
> This patch implements the delete operation from the ruleset.
> 
> It implements a new delete() function in nft_set_rhash. It is simpler
> to use than the already existing remove(), because it only takes the set
> and the key as arguments, whereas remove() expects a full
> nft_set_elem structure.

Userspace patches for this? Thanks.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] netfilter: nft_dynset: support for element deletion
  2019-08-14  8:17 ` Pablo Neira Ayuso
@ 2019-08-14 13:56   ` Florian Westphal
  0 siblings, 0 replies; 4+ messages in thread
From: Florian Westphal @ 2019-08-14 13:56 UTC (permalink / raw)
  To: Pablo Neira Ayuso; +Cc: Ander Juaristi, netfilter-devel

Pablo Neira Ayuso <pablo@netfilter.org> wrote:
> On Tue, Aug 13, 2019 at 08:58:49AM +0200, Ander Juaristi wrote:
> > This patch implements the delete operation from the ruleset.
> > 
> > It implements a new delete() function in nft_set_rhash. It is simpler
> > to use than the already existing remove(), because it only takes the set
> > and the key as arguments, whereas remove() expects a full
> > nft_set_elem structure.
> 
> Userspace patches for this? Thanks.

Yes, please also add at least one test case -- thanks.

As a reminder, this needs a small patch to libnftnl to avoid
  [ dynset unknown reg_key 1 set y timeout 0ms ]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v3] netfilter: nft_dynset: support for element deletion
  2019-08-13  6:58 [PATCH v3] netfilter: nft_dynset: support for element deletion Ander Juaristi
  2019-08-14  8:17 ` Pablo Neira Ayuso
@ 2019-08-14 13:59 ` Florian Westphal
  1 sibling, 0 replies; 4+ messages in thread
From: Florian Westphal @ 2019-08-14 13:59 UTC (permalink / raw)
  To: Ander Juaristi; +Cc: netfilter-devel

Ander Juaristi <a@juaristi.eus> wrote:
> This patch implements the delete operation from the ruleset.
> 
> It implements a new delete() function in nft_set_rhash. It is simpler
> to use than the already existing remove(), because it only takes the set
> and the key as arguments, whereas remove() expects a full
> nft_set_elem structure.
> 
> Signed-off-by: Ander Juaristi <a@juaristi.eus>

Patch looks ok, but why did you add so many changes?

> -void nft_dynset_eval(const struct nft_expr *expr,
> -		     struct nft_regs *regs, const struct nft_pktinfo *pkt)
> +static bool nft_dynset_update(u8 sreg_key, int op, u64 timeout,
> +			      const struct nft_expr *expr,
> +			      const struct nft_pktinfo *pkt,
> +			      struct nft_regs *regs, struct nft_set *set)

Why is this new helper needed?

> +void nft_dynset_eval(const struct nft_expr *expr,
> +		     struct nft_regs *regs, const struct nft_pktinfo *pkt)
> +{
> +	const struct nft_dynset *priv = nft_expr_priv(expr);
> +	struct nft_set *set = priv->set;
> +
> +	if (priv->op == NFT_DYNSET_OP_DELETE) {
> +		set->ops->delete(set, &regs->data[priv->sreg_key]);
		return;
	}

... so no need for 'else' clause and no need to reformat all of this.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-13  6:58 [PATCH v3] netfilter: nft_dynset: support for element deletion Ander Juaristi
2019-08-14  8:17 ` Pablo Neira Ayuso
2019-08-14 13:56   ` Florian Westphal
2019-08-14 13:59 ` Florian Westphal

Netfilter-Devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/netfilter-devel/0 netfilter-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 netfilter-devel netfilter-devel/ https://lore.kernel.org/netfilter-devel \
		netfilter-devel@vger.kernel.org netfilter-devel@archiver.kernel.org
	public-inbox-index netfilter-devel


Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.netfilter-devel


AGPL code for this site: git clone https://public-inbox.org/ public-inbox