Skip to content

Commit d0a8d87

Browse files
juaristiummakynes
authored andcommitted
netfilter: nft_dynset: support for element deletion
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>
1 parent 65af4a1 commit d0a8d87

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

include/net/netfilter/nf_tables.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,17 +302,23 @@ struct nft_expr;
302302
* struct nft_set_ops - nf_tables set operations
303303
*
304304
* @lookup: look up an element within the set
305+
* @update: update an element if exists, add it if doesn't exist
306+
* @delete: delete an element
305307
* @insert: insert new element into set
306308
* @activate: activate new element in the next generation
307309
* @deactivate: lookup for element and deactivate it in the next generation
308310
* @flush: deactivate element in the next generation
309311
* @remove: remove element from set
310-
* @walk: iterate over all set elemeennts
312+
* @walk: iterate over all set elements
311313
* @get: get set elements
312314
* @privsize: function to return size of set private data
313315
* @init: initialize private data of new set instance
314316
* @destroy: destroy private data of set instance
315317
* @elemsize: element private size
318+
*
319+
* Operations lookup, update and delete have simpler interfaces, are faster
320+
* and currently only used in the packet path. All the rest are slower,
321+
* control plane functions.
316322
*/
317323
struct nft_set_ops {
318324
bool (*lookup)(const struct net *net,
@@ -327,6 +333,8 @@ struct nft_set_ops {
327333
const struct nft_expr *expr,
328334
struct nft_regs *regs,
329335
const struct nft_set_ext **ext);
336+
bool (*delete)(const struct nft_set *set,
337+
const u32 *key);
330338

331339
int (*insert)(const struct net *net,
332340
const struct nft_set *set,

include/uapi/linux/netfilter/nf_tables.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,7 @@ enum nft_lookup_attributes {
636636
enum nft_dynset_ops {
637637
NFT_DYNSET_OP_ADD,
638638
NFT_DYNSET_OP_UPDATE,
639+
NFT_DYNSET_OP_DELETE,
639640
};
640641

641642
enum nft_dynset_flags {

net/netfilter/nft_dynset.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ void nft_dynset_eval(const struct nft_expr *expr,
8484
const struct nft_expr *sexpr;
8585
u64 timeout;
8686

87+
if (priv->op == NFT_DYNSET_OP_DELETE) {
88+
set->ops->delete(set, &regs->data[priv->sreg_key]);
89+
return;
90+
}
91+
8792
if (set->ops->update(set, &regs->data[priv->sreg_key], nft_dynset_new,
8893
expr, regs, &ext)) {
8994
sexpr = NULL;
@@ -161,6 +166,7 @@ static int nft_dynset_init(const struct nft_ctx *ctx,
161166
priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
162167
switch (priv->op) {
163168
case NFT_DYNSET_OP_ADD:
169+
case NFT_DYNSET_OP_DELETE:
164170
break;
165171
case NFT_DYNSET_OP_UPDATE:
166172
if (!(set->flags & NFT_SET_TIMEOUT))

net/netfilter/nft_set_hash.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,24 @@ static void nft_rhash_remove(const struct net *net,
234234
rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params);
235235
}
236236

237+
static bool nft_rhash_delete(const struct nft_set *set,
238+
const u32 *key)
239+
{
240+
struct nft_rhash *priv = nft_set_priv(set);
241+
struct nft_rhash_cmp_arg arg = {
242+
.genmask = NFT_GENMASK_ANY,
243+
.set = set,
244+
.key = key,
245+
};
246+
struct nft_rhash_elem *he;
247+
248+
he = rhashtable_lookup(&priv->ht, &arg, nft_rhash_params);
249+
if (he == NULL)
250+
return false;
251+
252+
return rhashtable_remove_fast(&priv->ht, &he->node, nft_rhash_params) == 0;
253+
}
254+
237255
static void nft_rhash_walk(const struct nft_ctx *ctx, struct nft_set *set,
238256
struct nft_set_iter *iter)
239257
{
@@ -662,6 +680,7 @@ struct nft_set_type nft_set_rhash_type __read_mostly = {
662680
.remove = nft_rhash_remove,
663681
.lookup = nft_rhash_lookup,
664682
.update = nft_rhash_update,
683+
.delete = nft_rhash_delete,
665684
.walk = nft_rhash_walk,
666685
.get = nft_rhash_get,
667686
},

0 commit comments

Comments
 (0)