Skip to content

Commit 79ebb5b

Browse files
Laura Garcia Liebanaummakynes
authored andcommitted
netfilter: nf_tables: enable set expiration time for set elements
Currently, the expiration of every element in a set or map is a read-only parameter generated at kernel side. This change will permit to set a certain expiration date per element that will be required, for example, during stateful replication among several nodes. This patch handles the NFTA_SET_ELEM_EXPIRATION in order to configure the expiration parameter per element, or will use the timeout in the case that the expiration is not set. Signed-off-by: Laura Garcia Liebana <nevola@gmail.com> Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
1 parent 2f0513d commit 79ebb5b

3 files changed

Lines changed: 22 additions & 8 deletions

File tree

‎include/net/netfilter/nf_tables.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ static inline struct nft_object **nft_set_ext_obj(const struct nft_set_ext *ext)
636636
void *nft_set_elem_init(const struct nft_set *set,
637637
const struct nft_set_ext_tmpl *tmpl,
638638
const u32 *key, const u32 *data,
639-
u64 timeout, gfp_t gfp);
639+
u64 timeout, u64 expiration, gfp_t gfp);
640640
void nft_set_elem_destroy(const struct nft_set *set, void *elem,
641641
bool destroy_expr);
642642

‎net/netfilter/nf_tables_api.c‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3873,6 +3873,7 @@ static const struct nla_policy nft_set_elem_policy[NFTA_SET_ELEM_MAX + 1] = {
38733873
[NFTA_SET_ELEM_DATA] = { .type = NLA_NESTED },
38743874
[NFTA_SET_ELEM_FLAGS] = { .type = NLA_U32 },
38753875
[NFTA_SET_ELEM_TIMEOUT] = { .type = NLA_U64 },
3876+
[NFTA_SET_ELEM_EXPIRATION] = { .type = NLA_U64 },
38763877
[NFTA_SET_ELEM_USERDATA] = { .type = NLA_BINARY,
38773878
.len = NFT_USERDATA_MAXLEN },
38783879
[NFTA_SET_ELEM_EXPR] = { .type = NLA_NESTED },
@@ -4326,7 +4327,7 @@ static struct nft_trans *nft_trans_elem_alloc(struct nft_ctx *ctx,
43264327
void *nft_set_elem_init(const struct nft_set *set,
43274328
const struct nft_set_ext_tmpl *tmpl,
43284329
const u32 *key, const u32 *data,
4329-
u64 timeout, gfp_t gfp)
4330+
u64 timeout, u64 expiration, gfp_t gfp)
43304331
{
43314332
struct nft_set_ext *ext;
43324333
void *elem;
@@ -4341,9 +4342,11 @@ void *nft_set_elem_init(const struct nft_set *set,
43414342
memcpy(nft_set_ext_key(ext), key, set->klen);
43424343
if (nft_set_ext_exists(ext, NFT_SET_EXT_DATA))
43434344
memcpy(nft_set_ext_data(ext), data, set->dlen);
4344-
if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION))
4345-
*nft_set_ext_expiration(ext) =
4346-
get_jiffies_64() + timeout;
4345+
if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
4346+
*nft_set_ext_expiration(ext) = get_jiffies_64() + expiration;
4347+
if (expiration == 0)
4348+
*nft_set_ext_expiration(ext) += timeout;
4349+
}
43474350
if (nft_set_ext_exists(ext, NFT_SET_EXT_TIMEOUT))
43484351
*nft_set_ext_timeout(ext) = timeout;
43494352

@@ -4408,6 +4411,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
44084411
struct nft_trans *trans;
44094412
u32 flags = 0;
44104413
u64 timeout;
4414+
u64 expiration;
44114415
u8 ulen;
44124416
int err;
44134417

@@ -4451,6 +4455,16 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
44514455
timeout = set->timeout;
44524456
}
44534457

4458+
expiration = 0;
4459+
if (nla[NFTA_SET_ELEM_EXPIRATION] != NULL) {
4460+
if (!(set->flags & NFT_SET_TIMEOUT))
4461+
return -EINVAL;
4462+
err = nf_msecs_to_jiffies64(nla[NFTA_SET_ELEM_EXPIRATION],
4463+
&expiration);
4464+
if (err)
4465+
return err;
4466+
}
4467+
44544468
err = nft_data_init(ctx, &elem.key.val, sizeof(elem.key), &d1,
44554469
nla[NFTA_SET_ELEM_KEY]);
44564470
if (err < 0)
@@ -4533,7 +4547,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
45334547

45344548
err = -ENOMEM;
45354549
elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, data.data,
4536-
timeout, GFP_KERNEL);
4550+
timeout, expiration, GFP_KERNEL);
45374551
if (elem.priv == NULL)
45384552
goto err3;
45394553

@@ -4735,7 +4749,7 @@ static int nft_del_setelem(struct nft_ctx *ctx, struct nft_set *set,
47354749

47364750
err = -ENOMEM;
47374751
elem.priv = nft_set_elem_init(set, &tmpl, elem.key.val.data, NULL, 0,
4738-
GFP_KERNEL);
4752+
0, GFP_KERNEL);
47394753
if (elem.priv == NULL)
47404754
goto err2;
47414755

‎net/netfilter/nft_dynset.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
6060
elem = nft_set_elem_init(set, &priv->tmpl,
6161
&regs->data[priv->sreg_key],
6262
&regs->data[priv->sreg_data],
63-
timeout, GFP_ATOMIC);
63+
timeout, 0, GFP_ATOMIC);
6464
if (elem == NULL)
6565
goto err1;
6666

0 commit comments

Comments
 (0)