2021-06-16 20:25:05 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/netlink.h>
|
|
|
|
#include <linux/netfilter.h>
|
|
|
|
#include <linux/netfilter/nf_tables.h>
|
|
|
|
#include <net/netfilter/nf_tables_core.h>
|
|
|
|
#include <net/netfilter/nf_tables.h>
|
|
|
|
|
2022-01-09 16:11:14 +00:00
|
|
|
struct nft_last {
|
|
|
|
unsigned long jiffies;
|
|
|
|
unsigned int set;
|
|
|
|
};
|
|
|
|
|
2021-06-16 20:25:05 +00:00
|
|
|
struct nft_last_priv {
|
2022-01-09 16:11:14 +00:00
|
|
|
struct nft_last *last;
|
2021-06-16 20:25:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct nla_policy nft_last_policy[NFTA_LAST_MAX + 1] = {
|
|
|
|
[NFTA_LAST_SET] = { .type = NLA_U32 },
|
|
|
|
[NFTA_LAST_MSECS] = { .type = NLA_U64 },
|
|
|
|
};
|
|
|
|
|
|
|
|
static int nft_last_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
|
|
|
|
const struct nlattr * const tb[])
|
|
|
|
{
|
|
|
|
struct nft_last_priv *priv = nft_expr_priv(expr);
|
2022-01-09 16:11:14 +00:00
|
|
|
struct nft_last *last;
|
2021-06-16 20:25:05 +00:00
|
|
|
u64 last_jiffies;
|
|
|
|
int err;
|
|
|
|
|
2022-04-02 09:50:37 +00:00
|
|
|
last = kzalloc(sizeof(*last), GFP_KERNEL_ACCOUNT);
|
2022-01-09 16:11:14 +00:00
|
|
|
if (!last)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
if (tb[NFTA_LAST_SET])
|
|
|
|
last->set = ntohl(nla_get_be32(tb[NFTA_LAST_SET]));
|
2021-07-05 15:45:36 +00:00
|
|
|
|
2022-01-09 16:11:14 +00:00
|
|
|
if (last->set && tb[NFTA_LAST_MSECS]) {
|
2021-06-16 20:25:05 +00:00
|
|
|
err = nf_msecs_to_jiffies64(tb[NFTA_LAST_MSECS], &last_jiffies);
|
|
|
|
if (err < 0)
|
2022-01-09 16:11:14 +00:00
|
|
|
goto err;
|
2021-06-16 20:25:05 +00:00
|
|
|
|
2022-01-09 16:11:14 +00:00
|
|
|
last->jiffies = jiffies - (unsigned long)last_jiffies;
|
2021-06-16 20:25:05 +00:00
|
|
|
}
|
2022-01-09 16:11:14 +00:00
|
|
|
priv->last = last;
|
2021-06-16 20:25:05 +00:00
|
|
|
|
|
|
|
return 0;
|
2022-01-09 16:11:14 +00:00
|
|
|
err:
|
|
|
|
kfree(last);
|
|
|
|
|
|
|
|
return err;
|
2021-06-16 20:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void nft_last_eval(const struct nft_expr *expr,
|
|
|
|
struct nft_regs *regs, const struct nft_pktinfo *pkt)
|
|
|
|
{
|
|
|
|
struct nft_last_priv *priv = nft_expr_priv(expr);
|
2022-01-09 16:11:14 +00:00
|
|
|
struct nft_last *last = priv->last;
|
2021-06-16 20:25:05 +00:00
|
|
|
|
2022-01-09 16:11:14 +00:00
|
|
|
if (READ_ONCE(last->jiffies) != jiffies)
|
|
|
|
WRITE_ONCE(last->jiffies, jiffies);
|
|
|
|
if (READ_ONCE(last->set) == 0)
|
|
|
|
WRITE_ONCE(last->set, 1);
|
2021-06-16 20:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 21:45:58 +00:00
|
|
|
static int nft_last_dump(struct sk_buff *skb,
|
|
|
|
const struct nft_expr *expr, bool reset)
|
2021-06-16 20:25:05 +00:00
|
|
|
{
|
|
|
|
struct nft_last_priv *priv = nft_expr_priv(expr);
|
2022-01-09 16:11:14 +00:00
|
|
|
struct nft_last *last = priv->last;
|
|
|
|
unsigned long last_jiffies = READ_ONCE(last->jiffies);
|
|
|
|
u32 last_set = READ_ONCE(last->set);
|
2021-06-16 20:25:05 +00:00
|
|
|
__be64 msecs;
|
|
|
|
|
2021-07-17 08:20:08 +00:00
|
|
|
if (time_before(jiffies, last_jiffies)) {
|
2022-01-09 16:11:14 +00:00
|
|
|
WRITE_ONCE(last->set, 0);
|
2021-07-17 08:20:08 +00:00
|
|
|
last_set = 0;
|
|
|
|
}
|
2021-06-16 20:25:05 +00:00
|
|
|
|
2021-07-17 08:20:08 +00:00
|
|
|
if (last_set)
|
|
|
|
msecs = nf_jiffies64_to_msecs(jiffies - last_jiffies);
|
2021-06-16 20:25:05 +00:00
|
|
|
else
|
|
|
|
msecs = 0;
|
|
|
|
|
2021-07-17 08:20:08 +00:00
|
|
|
if (nla_put_be32(skb, NFTA_LAST_SET, htonl(last_set)) ||
|
2021-06-16 20:25:05 +00:00
|
|
|
nla_put_be64(skb, NFTA_LAST_MSECS, msecs, NFTA_LAST_PAD))
|
|
|
|
goto nla_put_failure;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nla_put_failure:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2022-01-09 16:11:14 +00:00
|
|
|
static void nft_last_destroy(const struct nft_ctx *ctx,
|
|
|
|
const struct nft_expr *expr)
|
|
|
|
{
|
|
|
|
struct nft_last_priv *priv = nft_expr_priv(expr);
|
|
|
|
|
|
|
|
kfree(priv->last);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int nft_last_clone(struct nft_expr *dst, const struct nft_expr *src)
|
|
|
|
{
|
|
|
|
struct nft_last_priv *priv_dst = nft_expr_priv(dst);
|
|
|
|
|
|
|
|
priv_dst->last = kzalloc(sizeof(*priv_dst->last), GFP_ATOMIC);
|
2022-01-10 19:48:17 +00:00
|
|
|
if (!priv_dst->last)
|
2022-01-09 16:11:14 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:25:05 +00:00
|
|
|
static const struct nft_expr_ops nft_last_ops = {
|
|
|
|
.type = &nft_last_type,
|
|
|
|
.size = NFT_EXPR_SIZE(sizeof(struct nft_last_priv)),
|
|
|
|
.eval = nft_last_eval,
|
|
|
|
.init = nft_last_init,
|
2022-01-09 16:11:14 +00:00
|
|
|
.destroy = nft_last_destroy,
|
|
|
|
.clone = nft_last_clone,
|
2021-06-16 20:25:05 +00:00
|
|
|
.dump = nft_last_dump,
|
2022-03-14 17:23:00 +00:00
|
|
|
.reduce = NFT_REDUCE_READONLY,
|
2021-06-16 20:25:05 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nft_expr_type nft_last_type __read_mostly = {
|
|
|
|
.name = "last",
|
|
|
|
.ops = &nft_last_ops,
|
|
|
|
.policy = nft_last_policy,
|
|
|
|
.maxattr = NFTA_LAST_MAX,
|
|
|
|
.flags = NFT_EXPR_STATEFUL,
|
|
|
|
.owner = THIS_MODULE,
|
|
|
|
};
|