OSDN Git Service

netfilter: nft_dynset: support for element deletion
authorAnder Juaristi <a@juaristi.eus>
Sat, 17 Aug 2019 11:26:52 +0000 (13:26 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Tue, 27 Aug 2019 15:27:08 +0000 (17:27 +0200)
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>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/net/netfilter/nf_tables.h
include/uapi/linux/netfilter/nf_tables.h
net/netfilter/nft_dynset.c
net/netfilter/nft_set_hash.c

index 6476514..4986651 100644 (file)
@@ -302,17 +302,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,
@@ -327,6 +333,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,
index b83b62e..0ff932d 100644 (file)
@@ -636,6 +636,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 {
index 33833a0..8887295 100644 (file)
@@ -84,6 +84,11 @@ void nft_dynset_eval(const struct nft_expr *expr,
        const struct nft_expr *sexpr;
        u64 timeout;
 
+       if (priv->op == NFT_DYNSET_OP_DELETE) {
+               set->ops->delete(set, &regs->data[priv->sreg_key]);
+               return;
+       }
+
        if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
                             expr, regs, &ext)) {
                sexpr = NULL;
@@ -161,6 +166,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))
index c490451..b331a3c 100644 (file)
@@ -234,6 +234,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_cmp_arg arg = {
+               .genmask = NFT_GENMASK_ANY,
+               .set = set,
+               .key = key,
+       };
+       struct nft_rhash_elem *he;
+
+       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)
 {
@@ -662,6 +680,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,
        },