2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2015-04-05 12:41:08 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015 Patrick McHardy <kaber@trash.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/netfilter/nf_tables.h>
|
|
|
|
#include <net/netfilter/nf_tables.h>
|
|
|
|
#include <net/netfilter/nf_tables_core.h>
|
|
|
|
|
|
|
|
struct nft_dynset {
|
|
|
|
struct nft_set *set;
|
|
|
|
struct nft_set_ext_tmpl tmpl;
|
|
|
|
enum nft_dynset_ops op:8;
|
|
|
|
enum nft_registers sreg_key:8;
|
|
|
|
enum nft_registers sreg_data:8;
|
2016-09-07 10:22:18 +00:00
|
|
|
bool invert;
|
2015-04-05 12:41:08 +00:00
|
|
|
u64 timeout;
|
2015-04-11 09:46:42 +00:00
|
|
|
struct nft_expr *expr;
|
2015-04-05 12:41:08 +00:00
|
|
|
struct nft_set_binding binding;
|
|
|
|
};
|
|
|
|
|
2019-04-15 20:43:14 +00:00
|
|
|
static int nft_expr_clone(struct nft_expr *dst, struct nft_expr *src)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (src->ops->clone) {
|
|
|
|
dst->ops = src->ops;
|
|
|
|
err = src->ops->clone(dst, src);
|
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
} else {
|
|
|
|
memcpy(dst, src, src->ops->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
__module_get(src->ops->type->owner);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-05 12:41:08 +00:00
|
|
|
static void *nft_dynset_new(struct nft_set *set, const struct nft_expr *expr,
|
2015-04-11 01:27:31 +00:00
|
|
|
struct nft_regs *regs)
|
2015-04-05 12:41:08 +00:00
|
|
|
{
|
|
|
|
const struct nft_dynset *priv = nft_expr_priv(expr);
|
2015-04-11 09:46:42 +00:00
|
|
|
struct nft_set_ext *ext;
|
2015-04-05 12:41:08 +00:00
|
|
|
u64 timeout;
|
|
|
|
void *elem;
|
|
|
|
|
2018-04-16 16:52:58 +00:00
|
|
|
if (!atomic_add_unless(&set->nelems, 1, set->size))
|
2015-04-05 12:41:08 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
timeout = priv->timeout ? : set->timeout;
|
|
|
|
elem = nft_set_elem_init(set, &priv->tmpl,
|
2015-04-11 01:27:31 +00:00
|
|
|
®s->data[priv->sreg_key],
|
|
|
|
®s->data[priv->sreg_data],
|
2019-06-18 09:11:02 +00:00
|
|
|
timeout, 0, GFP_ATOMIC);
|
2016-10-22 10:51:25 +00:00
|
|
|
if (elem == NULL)
|
|
|
|
goto err1;
|
2015-04-11 09:46:42 +00:00
|
|
|
|
|
|
|
ext = nft_set_elem_ext(set, elem);
|
2015-11-10 12:39:42 +00:00
|
|
|
if (priv->expr != NULL &&
|
|
|
|
nft_expr_clone(nft_set_ext_expr(ext), priv->expr) < 0)
|
2016-10-22 10:51:25 +00:00
|
|
|
goto err2;
|
2015-04-11 09:46:42 +00:00
|
|
|
|
2015-04-05 12:41:08 +00:00
|
|
|
return elem;
|
2016-10-22 10:51:25 +00:00
|
|
|
|
|
|
|
err2:
|
|
|
|
nft_set_elem_destroy(set, elem, false);
|
|
|
|
err1:
|
|
|
|
if (set->size)
|
|
|
|
atomic_dec(&set->nelems);
|
|
|
|
return NULL;
|
2015-04-05 12:41:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 16:35:34 +00:00
|
|
|
void nft_dynset_eval(const struct nft_expr *expr,
|
|
|
|
struct nft_regs *regs, const struct nft_pktinfo *pkt)
|
2015-04-05 12:41:08 +00:00
|
|
|
{
|
|
|
|
const struct nft_dynset *priv = nft_expr_priv(expr);
|
|
|
|
struct nft_set *set = priv->set;
|
|
|
|
const struct nft_set_ext *ext;
|
2015-04-11 09:46:42 +00:00
|
|
|
const struct nft_expr *sexpr;
|
2015-04-05 12:41:08 +00:00
|
|
|
u64 timeout;
|
|
|
|
|
2019-08-17 11:26:52 +00:00
|
|
|
if (priv->op == NFT_DYNSET_OP_DELETE) {
|
|
|
|
set->ops->delete(set, ®s->data[priv->sreg_key]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-11 01:27:31 +00:00
|
|
|
if (set->ops->update(set, ®s->data[priv->sreg_key], nft_dynset_new,
|
|
|
|
expr, regs, &ext)) {
|
2015-04-11 09:46:42 +00:00
|
|
|
sexpr = NULL;
|
|
|
|
if (nft_set_ext_exists(ext, NFT_SET_EXT_EXPR))
|
|
|
|
sexpr = nft_set_ext_expr(ext);
|
|
|
|
|
2015-04-05 12:41:08 +00:00
|
|
|
if (priv->op == NFT_DYNSET_OP_UPDATE &&
|
|
|
|
nft_set_ext_exists(ext, NFT_SET_EXT_EXPIRATION)) {
|
|
|
|
timeout = priv->timeout ? : set->timeout;
|
2018-04-27 20:37:43 +00:00
|
|
|
*nft_set_ext_expiration(ext) = get_jiffies_64() + timeout;
|
2017-04-23 10:29:30 +00:00
|
|
|
}
|
2015-04-05 12:41:08 +00:00
|
|
|
|
2015-04-11 09:46:42 +00:00
|
|
|
if (sexpr != NULL)
|
|
|
|
sexpr->ops->eval(sexpr, regs, pkt);
|
2016-09-07 10:22:18 +00:00
|
|
|
|
|
|
|
if (priv->invert)
|
|
|
|
regs->verdict.code = NFT_BREAK;
|
2015-04-11 09:46:42 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-04-23 10:29:30 +00:00
|
|
|
|
2016-09-07 10:22:18 +00:00
|
|
|
if (!priv->invert)
|
|
|
|
regs->verdict.code = NFT_BREAK;
|
2015-04-05 12:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct nla_policy nft_dynset_policy[NFTA_DYNSET_MAX + 1] = {
|
2017-01-20 13:03:03 +00:00
|
|
|
[NFTA_DYNSET_SET_NAME] = { .type = NLA_STRING,
|
|
|
|
.len = NFT_SET_MAXNAMELEN - 1 },
|
2015-04-05 12:41:08 +00:00
|
|
|
[NFTA_DYNSET_SET_ID] = { .type = NLA_U32 },
|
|
|
|
[NFTA_DYNSET_OP] = { .type = NLA_U32 },
|
|
|
|
[NFTA_DYNSET_SREG_KEY] = { .type = NLA_U32 },
|
|
|
|
[NFTA_DYNSET_SREG_DATA] = { .type = NLA_U32 },
|
|
|
|
[NFTA_DYNSET_TIMEOUT] = { .type = NLA_U64 },
|
2015-04-11 09:46:42 +00:00
|
|
|
[NFTA_DYNSET_EXPR] = { .type = NLA_NESTED },
|
2016-09-07 10:22:18 +00:00
|
|
|
[NFTA_DYNSET_FLAGS] = { .type = NLA_U32 },
|
2015-04-05 12:41:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int nft_dynset_init(const struct nft_ctx *ctx,
|
|
|
|
const struct nft_expr *expr,
|
|
|
|
const struct nlattr * const tb[])
|
|
|
|
{
|
|
|
|
struct nft_dynset *priv = nft_expr_priv(expr);
|
2016-06-12 20:52:45 +00:00
|
|
|
u8 genmask = nft_genmask_next(ctx->net);
|
2015-04-05 12:41:08 +00:00
|
|
|
struct nft_set *set;
|
|
|
|
u64 timeout;
|
|
|
|
int err;
|
|
|
|
|
2018-07-11 11:45:14 +00:00
|
|
|
lockdep_assert_held(&ctx->net->nft.commit_mutex);
|
|
|
|
|
2015-04-05 12:41:08 +00:00
|
|
|
if (tb[NFTA_DYNSET_SET_NAME] == NULL ||
|
|
|
|
tb[NFTA_DYNSET_OP] == NULL ||
|
|
|
|
tb[NFTA_DYNSET_SREG_KEY] == NULL)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2016-09-07 10:22:18 +00:00
|
|
|
if (tb[NFTA_DYNSET_FLAGS]) {
|
|
|
|
u32 flags = ntohl(nla_get_be32(tb[NFTA_DYNSET_FLAGS]));
|
|
|
|
|
|
|
|
if (flags & ~NFT_DYNSET_F_INV)
|
2020-12-27 11:33:44 +00:00
|
|
|
return -EOPNOTSUPP;
|
2016-09-07 10:22:18 +00:00
|
|
|
if (flags & NFT_DYNSET_F_INV)
|
|
|
|
priv->invert = true;
|
|
|
|
}
|
|
|
|
|
2018-03-28 10:06:49 +00:00
|
|
|
set = nft_set_lookup_global(ctx->net, ctx->table,
|
|
|
|
tb[NFTA_DYNSET_SET_NAME],
|
|
|
|
tb[NFTA_DYNSET_SET_ID], genmask);
|
2017-03-06 16:46:20 +00:00
|
|
|
if (IS_ERR(set))
|
|
|
|
return PTR_ERR(set);
|
2015-04-05 12:41:08 +00:00
|
|
|
|
2016-10-22 10:51:24 +00:00
|
|
|
if (set->ops->update == NULL)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2015-04-05 12:41:08 +00:00
|
|
|
if (set->flags & NFT_SET_CONSTANT)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
priv->op = ntohl(nla_get_be32(tb[NFTA_DYNSET_OP]));
|
2021-09-25 20:40:26 +00:00
|
|
|
if (priv->op > NFT_DYNSET_OP_DELETE)
|
2015-04-05 12:41:08 +00:00
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
|
|
|
timeout = 0;
|
|
|
|
if (tb[NFTA_DYNSET_TIMEOUT] != NULL) {
|
|
|
|
if (!(set->flags & NFT_SET_TIMEOUT))
|
2020-12-27 11:33:44 +00:00
|
|
|
return -EOPNOTSUPP;
|
2020-12-08 17:25:53 +00:00
|
|
|
|
|
|
|
err = nf_msecs_to_jiffies64(tb[NFTA_DYNSET_TIMEOUT], &timeout);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2015-04-05 12:41:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-11 01:27:36 +00:00
|
|
|
priv->sreg_key = nft_parse_register(tb[NFTA_DYNSET_SREG_KEY]);
|
2018-01-16 14:51:01 +00:00
|
|
|
err = nft_validate_register_load(priv->sreg_key, set->klen);
|
2015-04-05 12:41:08 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (tb[NFTA_DYNSET_SREG_DATA] != NULL) {
|
|
|
|
if (!(set->flags & NFT_SET_MAP))
|
2020-12-27 11:33:44 +00:00
|
|
|
return -EOPNOTSUPP;
|
2015-04-05 12:41:08 +00:00
|
|
|
if (set->dtype == NFT_DATA_VERDICT)
|
|
|
|
return -EOPNOTSUPP;
|
|
|
|
|
2015-04-11 01:27:36 +00:00
|
|
|
priv->sreg_data = nft_parse_register(tb[NFTA_DYNSET_SREG_DATA]);
|
2015-04-11 01:27:30 +00:00
|
|
|
err = nft_validate_register_load(priv->sreg_data, set->dlen);
|
2015-04-05 12:41:08 +00:00
|
|
|
if (err < 0)
|
|
|
|
return err;
|
|
|
|
} else if (set->flags & NFT_SET_MAP)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-04-11 09:46:42 +00:00
|
|
|
if (tb[NFTA_DYNSET_EXPR] != NULL) {
|
|
|
|
if (!(set->flags & NFT_SET_EVAL))
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
priv->expr = nft_expr_init(ctx, tb[NFTA_DYNSET_EXPR]);
|
|
|
|
if (IS_ERR(priv->expr))
|
|
|
|
return PTR_ERR(priv->expr);
|
|
|
|
|
|
|
|
err = -EOPNOTSUPP;
|
2018-06-02 21:38:49 +00:00
|
|
|
if (priv->expr->ops->type->flags & NFT_EXPR_GC) {
|
|
|
|
if (set->flags & NFT_SET_TIMEOUT)
|
|
|
|
goto err1;
|
|
|
|
if (!set->ops->gc_init)
|
|
|
|
goto err1;
|
|
|
|
set->ops->gc_init(set);
|
|
|
|
}
|
2018-06-11 15:18:29 +00:00
|
|
|
}
|
2015-04-11 09:46:42 +00:00
|
|
|
|
2015-04-05 12:41:08 +00:00
|
|
|
nft_set_ext_prepare(&priv->tmpl);
|
|
|
|
nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_KEY, set->klen);
|
|
|
|
if (set->flags & NFT_SET_MAP)
|
|
|
|
nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_DATA, set->dlen);
|
2015-04-11 09:46:42 +00:00
|
|
|
if (priv->expr != NULL)
|
|
|
|
nft_set_ext_add_length(&priv->tmpl, NFT_SET_EXT_EXPR,
|
|
|
|
priv->expr->ops->size);
|
2015-04-05 12:41:08 +00:00
|
|
|
if (set->flags & NFT_SET_TIMEOUT) {
|
2021-01-16 18:20:15 +00:00
|
|
|
if (timeout || set->timeout) {
|
|
|
|
nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_TIMEOUT);
|
2015-04-05 12:41:08 +00:00
|
|
|
nft_set_ext_add(&priv->tmpl, NFT_SET_EXT_EXPIRATION);
|
2021-01-16 18:20:15 +00:00
|
|
|
}
|
2015-04-05 12:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
priv->timeout = timeout;
|
|
|
|
|
|
|
|
err = nf_tables_bind_set(ctx, set, &priv->binding);
|
|
|
|
if (err < 0)
|
2015-04-11 09:46:42 +00:00
|
|
|
goto err1;
|
2015-04-05 12:41:08 +00:00
|
|
|
|
2018-04-16 16:52:58 +00:00
|
|
|
if (set->size == 0)
|
|
|
|
set->size = 0xffff;
|
|
|
|
|
2015-04-05 12:41:08 +00:00
|
|
|
priv->set = set;
|
|
|
|
return 0;
|
2015-04-11 09:46:42 +00:00
|
|
|
|
|
|
|
err1:
|
|
|
|
if (priv->expr != NULL)
|
|
|
|
nft_expr_destroy(ctx, priv->expr);
|
|
|
|
return err;
|
2015-04-05 12:41:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 12:41:30 +00:00
|
|
|
static void nft_dynset_deactivate(const struct nft_ctx *ctx,
|
2019-02-02 09:49:13 +00:00
|
|
|
const struct nft_expr *expr,
|
|
|
|
enum nft_trans_phase phase)
|
2018-08-29 12:41:30 +00:00
|
|
|
{
|
|
|
|
struct nft_dynset *priv = nft_expr_priv(expr);
|
|
|
|
|
2019-03-08 14:30:03 +00:00
|
|
|
nf_tables_deactivate_set(ctx, priv->set, &priv->binding, phase);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nft_dynset_activate(const struct nft_ctx *ctx,
|
|
|
|
const struct nft_expr *expr)
|
|
|
|
{
|
|
|
|
struct nft_dynset *priv = nft_expr_priv(expr);
|
2019-02-02 09:49:13 +00:00
|
|
|
|
2019-03-08 14:30:03 +00:00
|
|
|
priv->set->use++;
|
2018-08-29 12:41:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-05 12:41:08 +00:00
|
|
|
static void nft_dynset_destroy(const struct nft_ctx *ctx,
|
|
|
|
const struct nft_expr *expr)
|
|
|
|
{
|
|
|
|
struct nft_dynset *priv = nft_expr_priv(expr);
|
|
|
|
|
2015-04-11 09:46:42 +00:00
|
|
|
if (priv->expr != NULL)
|
|
|
|
nft_expr_destroy(ctx, priv->expr);
|
2018-08-29 12:41:30 +00:00
|
|
|
|
|
|
|
nf_tables_destroy_set(ctx, priv->set);
|
2015-04-05 12:41:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int nft_dynset_dump(struct sk_buff *skb, const struct nft_expr *expr)
|
|
|
|
{
|
|
|
|
const struct nft_dynset *priv = nft_expr_priv(expr);
|
2016-09-07 10:22:18 +00:00
|
|
|
u32 flags = priv->invert ? NFT_DYNSET_F_INV : 0;
|
2015-04-05 12:41:08 +00:00
|
|
|
|
2015-04-11 01:27:36 +00:00
|
|
|
if (nft_dump_register(skb, NFTA_DYNSET_SREG_KEY, priv->sreg_key))
|
2015-04-05 12:41:08 +00:00
|
|
|
goto nla_put_failure;
|
|
|
|
if (priv->set->flags & NFT_SET_MAP &&
|
2015-04-11 01:27:36 +00:00
|
|
|
nft_dump_register(skb, NFTA_DYNSET_SREG_DATA, priv->sreg_data))
|
2015-04-05 12:41:08 +00:00
|
|
|
goto nla_put_failure;
|
|
|
|
if (nla_put_be32(skb, NFTA_DYNSET_OP, htonl(priv->op)))
|
|
|
|
goto nla_put_failure;
|
|
|
|
if (nla_put_string(skb, NFTA_DYNSET_SET_NAME, priv->set->name))
|
|
|
|
goto nla_put_failure;
|
2016-10-09 13:49:02 +00:00
|
|
|
if (nla_put_be64(skb, NFTA_DYNSET_TIMEOUT,
|
2020-12-08 17:25:53 +00:00
|
|
|
nf_jiffies64_to_msecs(priv->timeout),
|
2016-04-22 15:31:18 +00:00
|
|
|
NFTA_DYNSET_PAD))
|
2015-04-05 12:41:08 +00:00
|
|
|
goto nla_put_failure;
|
2015-04-11 09:46:42 +00:00
|
|
|
if (priv->expr && nft_expr_dump(skb, NFTA_DYNSET_EXPR, priv->expr))
|
|
|
|
goto nla_put_failure;
|
2016-09-07 10:22:18 +00:00
|
|
|
if (nla_put_be32(skb, NFTA_DYNSET_FLAGS, htonl(flags)))
|
|
|
|
goto nla_put_failure;
|
2015-04-05 12:41:08 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct nft_expr_ops nft_dynset_ops = {
|
|
|
|
.type = &nft_dynset_type,
|
|
|
|
.size = NFT_EXPR_SIZE(sizeof(struct nft_dynset)),
|
|
|
|
.eval = nft_dynset_eval,
|
|
|
|
.init = nft_dynset_init,
|
|
|
|
.destroy = nft_dynset_destroy,
|
2019-03-08 14:30:03 +00:00
|
|
|
.activate = nft_dynset_activate,
|
2018-08-29 12:41:30 +00:00
|
|
|
.deactivate = nft_dynset_deactivate,
|
2015-04-05 12:41:08 +00:00
|
|
|
.dump = nft_dynset_dump,
|
|
|
|
};
|
|
|
|
|
2016-11-06 13:15:51 +00:00
|
|
|
struct nft_expr_type nft_dynset_type __read_mostly = {
|
2015-04-05 12:41:08 +00:00
|
|
|
.name = "dynset",
|
|
|
|
.ops = &nft_dynset_ops,
|
|
|
|
.policy = nft_dynset_policy,
|
|
|
|
.maxattr = NFTA_DYNSET_MAX,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|