1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/netlink.h>
6 #include <linux/netfilter.h>
7 #include <linux/netfilter/nf_tables.h>
8 #include <net/netfilter/nf_tables_core.h>
9 #include <net/netfilter/nf_tables.h>
11 struct nft_last_priv {
12 unsigned long last_jiffies;
13 unsigned int last_set;
16 static const struct nla_policy nft_last_policy[NFTA_LAST_MAX + 1] = {
17 [NFTA_LAST_SET] = { .type = NLA_U32 },
18 [NFTA_LAST_MSECS] = { .type = NLA_U64 },
21 static int nft_last_init(const struct nft_ctx *ctx, const struct nft_expr *expr,
22 const struct nlattr * const tb[])
24 struct nft_last_priv *priv = nft_expr_priv(expr);
29 if (tb[NFTA_LAST_SET]) {
30 last_set = ntohl(nla_get_be32(tb[NFTA_LAST_SET]));
35 if (last_set && tb[NFTA_LAST_MSECS]) {
36 err = nf_msecs_to_jiffies64(tb[NFTA_LAST_MSECS], &last_jiffies);
40 priv->last_jiffies = jiffies + (unsigned long)last_jiffies;
46 static void nft_last_eval(const struct nft_expr *expr,
47 struct nft_regs *regs, const struct nft_pktinfo *pkt)
49 struct nft_last_priv *priv = nft_expr_priv(expr);
51 priv->last_jiffies = jiffies;
55 static int nft_last_dump(struct sk_buff *skb, const struct nft_expr *expr)
57 struct nft_last_priv *priv = nft_expr_priv(expr);
60 if (time_before(jiffies, priv->last_jiffies))
64 msecs = nf_jiffies64_to_msecs(jiffies - priv->last_jiffies);
68 if (nla_put_be32(skb, NFTA_LAST_SET, htonl(priv->last_set)) ||
69 nla_put_be64(skb, NFTA_LAST_MSECS, msecs, NFTA_LAST_PAD))
78 static const struct nft_expr_ops nft_last_ops = {
79 .type = &nft_last_type,
80 .size = NFT_EXPR_SIZE(sizeof(struct nft_last_priv)),
81 .eval = nft_last_eval,
82 .init = nft_last_init,
83 .dump = nft_last_dump,
86 struct nft_expr_type nft_last_type __read_mostly = {
89 .policy = nft_last_policy,
90 .maxattr = NFTA_LAST_MAX,
91 .flags = NFT_EXPR_STATEFUL,